浏览代码

[test]: add auto test modules

Change-Id: I51e1d0d192bb8d673380e5f1dde97929f15473a4
Signed-off-by: Alpha Lin <alpha.lin@rock-chips.com>
Alpha Lin 8 年之前
父节点
当前提交
acba8f17d3
共有 5 个文件被更改,包括 642 次插入1 次删除
  1. 1 1
      test/CMakeLists.txt
  2. 294 0
      test/mpp_event_trigger.c
  3. 41 0
      test/mpp_event_trigger.h
  4. 267 0
      test/mpp_parse_cfg.c
  5. 39 0
      test/mpp_parse_cfg.h

+ 1 - 1
test/CMakeLists.txt

@@ -12,7 +12,7 @@ macro(add_mpp_test module)
 
     option(${test_tag} "Build mpp ${module} unit test" ON)
     if(${test_tag})
-        add_executable(${test_name} ${test_name}.c)
+        add_executable(${test_name} ${test_name}.c mpp_event_trigger.c mpp_parse_cfg.c)
         target_link_libraries(${test_name} mpp_shared utils)
         set_target_properties(${test_name} PROPERTIES FOLDER "test")
         #install(TARGETS ${test_name} RUNTIME DESTINATION ${TEST_INSTALL_DIR})

+ 294 - 0
test/mpp_event_trigger.c

@@ -0,0 +1,294 @@
+/*
+ * Copyright 2017 Rockchip Electronics Co. LTD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <assert.h>
+#include <errno.h>
+#include <malloc.h>
+#include <memory.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <sys/time.h>
+#include <unistd.h>
+
+#include "mpp_event_trigger.h"
+
+#include "mpp_log.h"
+#include "rk_type.h"
+
+struct event_ctx_impl {
+    int (*notify)(void *param);
+
+    event_trigger trigger;
+
+    pthread_cond_t condition;
+    pthread_mutex_t mutex;
+    RK_U32 semval;
+
+    pthread_t thr;
+    RK_S32 flag;
+
+    struct event_packet *ea;
+    int event_idx;
+
+    void *parent;
+};
+
+static int event_create(struct event_ctx_impl *ctx)
+{
+    int ret;
+
+    ret = pthread_cond_init(&ctx->condition, NULL);
+    if (ret != 0)
+        return -1;
+
+    ret = pthread_mutex_init(&ctx->mutex, NULL);
+    if (ret != 0)
+        return -1;
+
+    ctx->semval = ctx->ea->e[0].idx;
+    mpp_log_f("with %u\n", ctx->semval);
+
+    return 0;
+}
+
+static void event_destroy(struct event_ctx_impl *ctx)
+{
+    pthread_cond_destroy(&ctx->condition);
+    pthread_mutex_destroy(&ctx->mutex);
+}
+
+/* initialize and schedule next event */
+static void event_init(struct event_ctx_impl *ctx)
+{
+    struct ievent *e_curr;
+    struct ievent *e_next;
+
+    pthread_mutex_lock(&ctx->mutex);
+    e_curr = &ctx->ea->e[ctx->event_idx % ctx->ea->cnt];
+    e_next = &ctx->ea->e[(++ctx->event_idx) % ctx->ea->cnt];
+
+    mpp_log("curr %d, next %d\n",
+            e_curr->idx, e_next->idx);
+
+    if (e_next->idx > e_curr->idx)
+        ctx->semval = e_next->idx - e_curr->idx;
+    else if (ctx->ea->loop > 0)
+        ctx->semval = e_next->idx + ctx->ea->loop - e_curr->idx;
+    else
+        ctx->flag = 0;
+
+    mpp_log_f("semval %u\n", ctx->semval);
+    pthread_mutex_unlock(&ctx->mutex);
+}
+
+/* wait the event trigger deadline */
+/*static void event_wait(struct event_ctx_impl *ctx)
+{
+    pthread_mutex_lock(&ctx->mutex);
+    while (ctx->semval > 0)
+        pthread_cond_wait(&ctx->condition, &ctx->mutex);
+
+    ctx->semval--;
+    pthread_mutex_unlock(&ctx->mutex);
+}*/
+
+/* wait the event trigger deadline with timeout, avoid dead lock */
+static int event_timed_wait(struct event_ctx_impl *ctx, unsigned int milli_sec)
+{
+    int err = 0;
+    struct timespec final_time;
+    struct timeval curr_time;
+    long int microdelay;
+
+    gettimeofday(&curr_time, NULL);
+    /*
+     * convert timeval to timespec and add delay in milliseconds
+     * for the timeout
+     */
+    microdelay = ((milli_sec * 1000 + curr_time.tv_usec));
+    final_time.tv_sec = curr_time.tv_sec + (microdelay / 1000000);
+    final_time.tv_nsec = (microdelay % 1000000) * 1000;
+    pthread_mutex_lock(&ctx->mutex);
+    while (ctx->semval > 0) {
+        err = pthread_cond_timedwait(&ctx->condition, &ctx->mutex, &final_time);
+        if (err != 0)
+            ctx->semval--;
+    }
+    ctx->semval--;
+    pthread_mutex_unlock(&ctx->mutex);
+    return err;
+}
+
+/* event heart beat */
+static void event_down(struct event_ctx_impl *ctx)
+{
+    pthread_mutex_lock(&ctx->mutex);
+    ctx->semval--;
+    pthread_cond_signal(&ctx->condition);
+    pthread_mutex_unlock(&ctx->mutex);
+}
+
+/* a callback to notify the event trigger that there is a event heart beat */
+static int event_notify(void *param)
+{
+    struct event_ctx_impl *ctx = (struct event_ctx_impl *)param;
+
+    if (ctx->flag)
+        event_down(ctx);
+
+    return 0;
+}
+
+static void *event_trigger_thread(void *param)
+{
+    struct event_ctx_impl *ctx = (struct event_ctx_impl *)param;
+    struct timeval curr_time;
+    long start_time, curr;
+    RK_S32 ret;
+
+    gettimeofday(&curr_time, NULL);
+
+    start_time = curr_time.tv_sec * 1000 + curr_time.tv_usec / 1000;
+    event_create(ctx);
+
+    while (1) {
+        ret = event_timed_wait(ctx, ctx->semval * 10000);
+        if (!ctx->flag)
+            break;
+
+        if (ret == ETIMEDOUT) {
+            mpp_err("wait event timeout\n");
+            break;
+        }
+
+        gettimeofday(&curr_time, NULL);
+
+        curr = curr_time.tv_sec * 1000 + curr_time.tv_usec / 1000;
+        /* TODO, trigger event */
+        mpp_log("[%ld ms] event idx %d triggered\n",
+                curr - start_time, ctx->event_idx);
+        mpp_log("cnt %d\n", ctx->ea->cnt);
+        ctx->trigger(ctx->parent, ctx->ea->e[ctx->event_idx % ctx->ea->cnt].event);
+
+        /* schedule next event */
+        event_init(ctx);
+        if (!ctx->flag)
+            break;
+    }
+
+    event_destroy(ctx);
+
+    mpp_log_f("exit\n");
+
+    return NULL;
+}
+
+struct event_ctx* event_ctx_create(struct event_packet *ea,
+                                   event_trigger trigger, void *parent)
+{
+    struct event_ctx_impl *ctx = (struct event_ctx_impl *)malloc(sizeof(*ctx));
+
+    if (ctx == NULL) {
+        mpp_err("allocate event ctx failed\n");
+        return NULL;
+    }
+
+    assert(ea->cnt <= 128);
+
+    ctx->event_idx = 0;
+    ctx->ea = ea;
+
+    ctx->notify = event_notify;
+    ctx->trigger = trigger;
+    ctx->parent = parent;
+
+    ctx->flag = 1;
+    pthread_create(&ctx->thr, NULL, event_trigger_thread, ctx);
+
+    return (struct event_ctx *)ctx;
+}
+
+void event_ctx_release(struct event_ctx *ctx)
+{
+    void *ret;
+    struct event_ctx_impl *ictx = (struct event_ctx_impl *)ctx;
+
+    assert(ctx != NULL);
+
+    if (ictx->flag) {
+        ictx->flag = 0;
+        ictx->semval = 0;
+        pthread_cond_signal(&ictx->condition);
+    }
+
+    pthread_join(ictx->thr, &ret);
+
+    free(ictx);
+}
+
+#ifdef EVENT_TRIGGER_TEST
+/*
+ * the following codes is the sample to use the event trigger
+ */
+struct event_impl {
+    int cnt;
+    int loop;
+    int idx[128];
+    int event[128];
+};
+
+/* a callback to notify test that a event occur */
+static void event_occur(void *parent, void *event)
+{
+    int *e = (int *)event;
+    mpp_log("event %d occur\n", *e);
+}
+
+int main(int argc, char **argv)
+{
+    struct event_ctx *ctx;
+    int i = 0;
+    struct event_impl ie;
+    struct event_packet ea;
+
+    ie.cnt = 4;
+    ie.loop = 25;
+    ie.event[0] = ie.idx[0] = 4;
+    ie.event[1] = ie.idx[1] = 8;
+    ie.event[2] = ie.idx[2] = 18;
+    ie.event[3] = ie.idx[3] = 20;
+
+    ea.cnt = ie.cnt;
+    ea.loop = ie.loop;
+
+    for (i = 0; i < ie.cnt; ++i) {
+        ea.e[i].idx = ie.idx[i];
+        ea.e[i].event = &ie.event[i];
+    }
+
+    ctx = event_ctx_create(&ea, event_occur, NULL);
+
+    while (i++ < 100) {
+        usleep(10 * 1000);
+        mpp_log("%03d:\n", i);
+        ctx->notify((void*)ctx);
+    }
+
+    event_ctx_release(ctx);
+
+    return 0;
+}
+#endif

+ 41 - 0
test/mpp_event_trigger.h

@@ -0,0 +1,41 @@
+/*
+ * Copyright 2017 Rockchip Electronics Co. LTD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MPP_EVENT_TRIGGER_H__
+#define __MPP_EVENT_TRIGGER_H__
+
+typedef void (*event_trigger)(void *parent, void *event);
+
+struct ievent {
+    int idx;
+    void *event;
+};
+
+struct event_packet {
+    int cnt;
+    int loop;
+    struct ievent e[128];
+};
+
+struct event_ctx {
+    int (*notify)(void *param);
+};
+
+struct event_ctx* event_ctx_create(struct event_packet *e,
+                                   event_trigger trigger, void *parent);
+void event_ctx_release(struct event_ctx *ictx);
+
+#endif

+ 267 - 0
test/mpp_parse_cfg.c

@@ -0,0 +1,267 @@
+/*
+ * Copyright 2017 Rockchip Electronics Co. LTD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <stdio.h>
+#include <string.h>
+
+#include "mpp_parse_cfg.h"
+
+#include "mpp_log.h"
+#include "rk_type.h"
+
+#define ARRAY_SIZE(x)   (sizeof(x) / sizeof(x[0]))
+
+enum CONFIG_TYPE {
+    CONFIG_TYPE_OPTION,
+    CONFIG_TYPE_EVENT,
+
+    OPT_TYPE_INDEX_TYPE,
+    OPT_TYPE_LOOP_NUM,
+
+    IDX_TYPE_FRM_NUM,
+    IDX_TYPE_MSEC,
+};
+
+struct options_table {
+    int type;
+    const char *type_str;
+};
+
+/* table for converting string to type */
+static struct options_table op_tbl[] = {
+    {CONFIG_TYPE_OPTION,    "[CONFIG]"},
+    {CONFIG_TYPE_EVENT,     "[EVENT]"},
+
+    {OPT_TYPE_INDEX_TYPE,   "index"},
+    {OPT_TYPE_LOOP_NUM,     "loop"},
+
+    {IDX_TYPE_FRM_NUM,      "frm"},
+    {IDX_TYPE_MSEC,         "msec"},
+};
+
+struct cfg_file {
+    FILE *file;
+    char cache[128];
+    RK_S32 cache_on;
+};
+
+/* get rid of leading and following space from string */
+static char *string_trim(char *string)
+{
+    char *p = string;
+    long len;
+
+    if (string == NULL)
+        return NULL;
+
+    while (*p == ' ')
+        p++;
+
+    len = strlen(p);
+
+    while (p[len - 1] == ' ')
+        len--;
+    p[len] = '\0';
+
+    return p;
+}
+
+static char *read_cfg_line(struct cfg_file *cfg)
+{
+    int ch;
+    int i;
+
+    while (!cfg->cache_on) {
+        i = 0;
+
+        while (1) {
+            ch = fgetc(cfg->file);
+            if (i == 0 && ch == EOF)
+                return NULL;
+            else if (ch == EOF || feof(cfg->file) || ch == '\n')
+                break;
+
+            cfg->cache[i++] = ch;
+        }
+        cfg->cache[i] = '\0';
+
+        /* a note, ignore and get the next line */
+        if (cfg->cache[0] != '#')
+            break;
+    }
+
+    cfg->cache_on = 1;
+    return string_trim(cfg->cache);
+}
+
+static inline void invalid_cfg_cache(struct cfg_file *cfg)
+{
+    cfg->cache_on = 0;
+}
+
+static char *get_opt_value(char *line)
+{
+    size_t i;
+
+    for (i = 0; i < strlen(line); ++i) {
+        if (line[i] == ':')
+            return string_trim(&line[i + 1]);
+    }
+
+    return NULL;
+}
+
+/* convert string to index by look up map table */
+static int lookup_opt_type(char *line)
+{
+    size_t i;
+
+    for (i = 0; i < ARRAY_SIZE(op_tbl); ++i) {
+        if (!strncmp(op_tbl[i].type_str, line,
+                     strlen(op_tbl[i].type_str))) {
+            mpp_log("option type %s find\n", op_tbl[i].type_str);
+            return op_tbl[i].type;
+        }
+    }
+
+    return -1;
+}
+
+static int scan_event_line(struct cfg_file *cfg, struct rc_event *event)
+{
+    char *line = read_cfg_line(cfg);
+
+    if (line != NULL && line[0] != '\n' && line[0] != '[') {
+        sscanf(line, "%d\t%d\t%f",
+               &event->idx, &event->bps, &event->fps);
+        mpp_log("idx: %d, bps %u, fps: %f\n",
+                event->idx, event->bps, event->fps);
+        invalid_cfg_cache(cfg);
+    } else {
+        return -1;
+    }
+
+    return 0;
+}
+
+static int parse_events(struct cfg_file *cfg, struct rc_test_config *ea)
+{
+    int ret;
+    int i;
+
+    for (i = 0; i < 128; ++i) {
+        ret = scan_event_line(cfg, &ea->event[i]);
+        if (ret < 0) {
+            ea->event_cnt = i;
+            break;
+        }
+    }
+
+    return 0;
+}
+
+static int parse_options(struct cfg_file *cfg, struct rc_test_config *ea)
+{
+    char *opt;
+    int type;
+
+    while (1) {
+        opt = read_cfg_line(cfg);
+
+        if (opt && opt[0] != '\n' && opt[0] != '[') {
+            type = lookup_opt_type(opt);
+
+            switch (type) {
+            case OPT_TYPE_INDEX_TYPE:
+                ea->idx_type = lookup_opt_type(get_opt_value(opt));
+                break;
+            case OPT_TYPE_LOOP_NUM:
+                sscanf(get_opt_value(opt), "%d", &ea->loop);
+                mpp_log("loop num: %d\n", ea->loop);
+                break;
+            default:
+                break;
+            }
+
+            invalid_cfg_cache(cfg);
+        } else {
+            break;
+        }
+    }
+    return 0;
+}
+
+int mpp_parse_config(char *cfg_url, struct rc_test_config *ea)
+{
+    struct cfg_file cfg;
+
+    if (cfg_url == NULL || strlen(cfg_url) == 0) {
+        mpp_err("invalid input config url\n");
+        return -1;
+    }
+
+    cfg.file = fopen(cfg_url, "rb");
+    if (cfg.file == NULL) {
+        mpp_err("fopen %s failed\n", cfg_url);
+        return -1;
+    }
+    cfg.cache_on = 0;
+
+    while (1) {
+        char *line = read_cfg_line(&cfg);
+
+        if (!line)
+            break;
+
+        invalid_cfg_cache(&cfg);
+        if (line[0] == '[') {
+            int type = lookup_opt_type(line);
+
+            switch (type) {
+            case CONFIG_TYPE_EVENT:
+                parse_events(&cfg, ea);
+                break;
+            case CONFIG_TYPE_OPTION:
+                parse_options(&cfg, ea);
+                break;
+            default:
+                mpp_err("invalid config type find\n");
+                fclose(cfg.file);
+                return -1;
+            }
+        }
+    }
+
+    fclose(cfg.file);
+
+    return 0;
+}
+
+#ifdef PARSE_CONFIG_TEST
+int main(int argc, char **argv)
+{
+    struct rc_test_config event_array;
+
+    if (argc < 2) {
+        mpp_err("invalid input argument\n");
+        return -1;
+    }
+
+    mpp_parse_config(argv[1], &event_array);
+
+    return 0;
+}
+#endif

+ 39 - 0
test/mpp_parse_cfg.h

@@ -0,0 +1,39 @@
+/*
+ * Copyright 2017 Rockchip Electronics Co. LTD
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __MPP_PARSE_CFG_H__
+#define __MPP_PARSE_CFG_H__
+
+struct rc_event {
+    union {
+        int msec;
+        int frm;
+        int idx;
+    };
+    float fps;
+    int bps;
+};
+
+struct rc_test_config {
+    int idx_type;
+    int loop;
+    struct rc_event event[128];
+    int event_cnt;
+};
+
+int mpp_parse_config(char *cfg_url, struct rc_test_config *ea);
+
+#endif