qemu: separated monitor functions & bug fixed. 25/10725/1
authorJinhyung Choi <jinhyung2.choi@samsung.com>
Tue, 1 Oct 2013 07:07:34 +0000 (16:07 +0900)
committerKitae Kim <kt920.kim@samsung.com>
Thu, 10 Oct 2013 07:24:32 +0000 (16:24 +0900)
Change-Id: If646d7156d3e77cb95c2a543f30837b9dd3e7e3a
Signed-off-by: Jinhyung Choi <jinhyung2.choi@samsung.com>
tizen/src/ecs/Makefile.tizen
tizen/src/ecs/ecs.c
tizen/src/ecs/ecs.h
tizen/src/ecs/ecs_mon.c [new file with mode: 0644]
tizen/src/ecs/ecs_msg.c
tizen/src/ecs/genmsg/ecs.pb-c.h
tizen/src/ecs/msg/ecs_ids.proto

index 5ef9b70..8d471a2 100644 (file)
@@ -6,7 +6,8 @@ QEMU_CFLAGS += -I$(SRC_PATH)/tizen/distrib/protobuf
 QEMU_CFLAGS += -I$(SRC_PATH)/tizen/src/ecs/genmsg
 
 obj-y += ecs.pb-c.o ecs_ids.pb-c.o protobuf-c.o
-obj-y += ecs_msg.o ecs.o ecs-json-streamer.o ecs_sensor.o
+obj-y += ecs_msg.o ecs.o ecs_sensor.o
+obj-y += ecs_mon.o ecs-json-streamer.o
 
 # NFC message extension
 obj-y += nfc.pb-c.o
index 974e38e..2d5fe07 100644 (file)
 #include "hw/qdev.h"
 #include "net/net.h"
 #include "ui/console.h"
-#include "migration/migration.h"
-#include "qapi/qmp/qjson.h"
-#include "qapi/qmp/json-parser.h"
-#include "qapi/qmp/qint.h"
-#include "ui/qemu-spice.h"
 
 #include "qemu-common.h"
 #include "qemu/queue.h"
 #include "qemu/timer.h"
 #include "qemu/main-loop.h"
 #include "sysemu/char.h"
-#include "qmp-commands.h"
 #include "config.h"
+#include "qapi/qmp/qint.h"
 
 #include "sdb.h"
 #include "ecs.h"
-#include "hw/maru_virtio_evdi.h"
-#include "hw/maru_virtio_sensor.h"
-#include "hw/maru_virtio_nfc.h"
 
 #include "genmsg/ecs.pb-c.h"
 
 #define DEBUG
 
-typedef struct mon_fd_t mon_fd_t;
-struct mon_fd_t {
-    char *name;
-    int fd;
-    QLIST_ENTRY(mon_fd_t)
-    next;
-};
-
 #ifndef min
 #define min(a,b) ((a)<(b)?(a):(b))
 #endif
 
-typedef struct mon_cmd_t {
-    const char *name;
-    const char *args_type;
-    const char *params;
-    const char *help;
-    void (*user_print)(Monitor *mon, const QObject *data);
-    union {
-        void (*info)(Monitor *mon);
-        void (*cmd)(Monitor *mon, const QDict *qdict);
-        int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
-        int (*cmd_async)(Monitor *mon, const QDict *params,
-                MonitorCompletion *cb, void *opaque);
-    } mhandler;
-    int flags;
-} mon_cmd_t;
-
 static QTAILQ_HEAD(ECS_ClientHead, ECS_Client)
 clients = QTAILQ_HEAD_INITIALIZER(clients);
 
@@ -135,9 +103,16 @@ static inline void start_logging(void) {
 #endif
 }
 
-static int ecs_write(int fd, const uint8_t *buf, int len);
+int ecs_write(int fd, const uint8_t *buf, int len) {
+    LOG("write buflen : %d, buf : %s", len, buf);
+    if (fd < 0) {
+        return -1;
+    }
+
+    return send_all(fd, buf, len);
+}
 
-static void ecs_client_close(ECS_Client* clii) {
+void ecs_client_close(ECS_Client* clii) {
     pthread_mutex_lock(&mutex_clilist);
 
     if (0 <= clii->client_fd) {
@@ -178,543 +153,6 @@ void send_to_client(int fd, const char* data, const int len)
     ecs_write(fd, (const uint8_t*) data, len);
 }
 
-/*
-void send_to_client(int fd, const char* data, const int len)
-{
-    char c;
-    uint8_t outbuf[OUT_BUF_SIZE];
-    int outbuf_index = 0;
-
-    for (;;) {
-        c = *data++;
-        if (outbuf_index >= OUT_BUF_SIZE - 1) {
-            LOG("string is too long: overflow buffer.");
-            return;
-        }
-#ifndef _WIN32
-        if (c == '\n') {
-            outbuf[outbuf_index++] = '\r';
-        }
-#endif
-        outbuf[outbuf_index++] = c;
-        if (c == '\0') {
-            break;
-        }
-    }
-    ecs_write(fd, outbuf, outbuf_index);
-}
-*/
-
-#define QMP_ACCEPT_UNKNOWNS 1
-static void ecs_monitor_flush(ECS_Client *clii, Monitor *mon) {
-    int ret;
-
-    if (clii && 0 < clii->client_fd && mon && mon->outbuf_index != 0) {
-        ret = ecs_write(clii->client_fd, mon->outbuf, mon->outbuf_index);
-        mon->outbuf_index = 0;
-        if (ret < -1) {
-            ecs_client_close(clii);
-        }
-    }
-}
-
-static void ecs_monitor_puts(ECS_Client *clii, Monitor *mon, const char *str) {
-    char c;
-
-    if (!clii || !mon) {
-        return;
-    }
-
-    for (;;) {
-        c = *str++;
-        if (c == '\0')
-            break;
-#ifndef _WIN32
-        if (c == '\n')
-            mon->outbuf[mon->outbuf_index++] = '\r';
-#endif
-        mon->outbuf[mon->outbuf_index++] = c;
-        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1) || c == '\n')
-            ecs_monitor_flush(clii, mon);
-    }
-}
-
-void ecs_vprintf(const char *type, const char *fmt, va_list ap) {
-    char buf[READ_BUF_LEN];
-    ECS_Client *clii;
-
-    QTAILQ_FOREACH(clii, &clients, next)
-    {
-        vsnprintf(buf, sizeof(buf), fmt, ap);
-        ecs_monitor_puts(clii, clii->cs->mon, buf);
-    }
-}
-
-void ecs_printf(const char* type, const char *fmt, ...) {
-    va_list ap;
-    va_start(ap, fmt);
-    ecs_vprintf(type, fmt, ap);
-    va_end(ap);
-}
-
-static inline int monitor_has_error(const Monitor *mon) {
-    return mon->error != NULL;
-}
-
-static QDict *build_qmp_error_dict(const QError *err) {
-    QObject *obj = qobject_from_jsonf(
-            "{ 'error': { 'class': %s, 'desc': %p } }",
-            ErrorClass_lookup[err->err_class], qerror_human(err));
-
-    return qobject_to_qdict(obj);
-}
-
-static void ecs_json_emitter(ECS_Client *clii, const QObject *data) {
-    QString *json;
-
-    json = qobject_to_json(data);
-
-    assert(json != NULL);
-
-    qstring_append_chr(json, '\n');
-    ecs_monitor_puts(clii, clii->cs->mon, qstring_get_str(json));
-
-    QDECREF(json);
-}
-
-static void ecs_protocol_emitter(ECS_Client *clii, const char* type,
-        QObject *data) {
-    QDict *qmp;
-    QObject *obj;
-
-    LOG("ecs_protocol_emitter called.");
-    trace_monitor_protocol_emitter(clii->cs->mon);
-
-    if (!monitor_has_error(clii->cs->mon)) {
-        /* success response */
-        qmp = qdict_new();
-        if (data) {
-            qobject_incref(data);
-            qdict_put_obj(qmp, "return", data);
-        } else {
-            /* return an empty QDict by default */
-            qdict_put(qmp, "return", qdict_new());
-        }
-
-        if (type == NULL) {
-            obj = qobject_from_jsonf("%s", "unknown");
-        } else {
-            obj = qobject_from_jsonf("%s", type);
-        }
-        qdict_put_obj(qmp, "type", obj);
-
-    } else {
-        /* error response */
-        qmp = build_qmp_error_dict(clii->cs->mon->error);
-        QDECREF(clii->cs->mon->error);
-        clii->cs->mon->error = NULL;
-    }
-
-    ecs_json_emitter(clii, QOBJECT(qmp));
-    QDECREF(qmp);
-}
-
-static void qmp_monitor_complete(void *opaque, QObject *ret_data) {
-    //   ecs_protocol_emitter(opaque, ret_data);
-}
-
-static int qmp_async_cmd_handler(ECS_Client *clii, const mon_cmd_t *cmd,
-        const QDict *params) {
-    return cmd->mhandler.cmd_async(clii->cs->mon, params, qmp_monitor_complete,
-            clii);
-}
-
-static void qmp_call_cmd(ECS_Client *clii, Monitor *mon, const char* type,
-        const mon_cmd_t *cmd, const QDict *params) {
-    int ret;
-    QObject *data = NULL;
-
-    ret = cmd->mhandler.cmd_new(mon, params, &data);
-    if (ret && !monitor_has_error(mon)) {
-        qerror_report(QERR_UNDEFINED_ERROR);
-    }
-    ecs_protocol_emitter(clii, type, data);
-    qobject_decref(data);
-}
-
-static inline bool handler_is_async(const mon_cmd_t *cmd) {
-    return cmd->flags & MONITOR_CMD_ASYNC;
-}
-
-/*
-static void monitor_user_noop(Monitor *mon, const QObject *data) {
-}
-
-static int do_screen_dump(Monitor *mon, const QDict *qdict, QObject **ret_data) {
-    //vga_hw_screen_dump(qdict_get_str(qdict, "filename"));
-    return 0;
-}
-
-static int client_migrate_info(Monitor *mon, const QDict *qdict,
-        MonitorCompletion cb, void *opaque) {
-    return 0;
-}
-
-static int add_graphics_client(Monitor *mon, const QDict *qdict,
-        QObject **ret_data) {
-    return 0;
-}
-
-static int do_qmp_capabilities(Monitor *mon, const QDict *params,
-        QObject **ret_data) {
-    return 0;
-}
-*/
-
-static const mon_cmd_t qmp_cmds[] = {
-//#include "qmp-commands-old.h"
-        { /* NULL */}, };
-
-static int check_mandatory_args(const QDict *cmd_args, const QDict *client_args,
-        int *flags) {
-    const QDictEntry *ent;
-
-    for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
-        const char *cmd_arg_name = qdict_entry_key(ent);
-        QString *type = qobject_to_qstring(qdict_entry_value(ent));
-        assert(type != NULL);
-
-        if (qstring_get_str(type)[0] == 'O') {
-            assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
-            *flags |= QMP_ACCEPT_UNKNOWNS;
-        } else if (qstring_get_str(type)[0] != '-'
-                && qstring_get_str(type)[1] != '?'
-                && !qdict_haskey(client_args, cmd_arg_name)) {
-            qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
-            return -1;
-        }
-    }
-
-    return 0;
-}
-
-static int check_client_args_type(const QDict *client_args,
-        const QDict *cmd_args, int flags) {
-    const QDictEntry *ent;
-
-    for (ent = qdict_first(client_args); ent;
-            ent = qdict_next(client_args, ent)) {
-        QObject *obj;
-        QString *arg_type;
-        const QObject *client_arg = qdict_entry_value(ent);
-        const char *client_arg_name = qdict_entry_key(ent);
-
-        obj = qdict_get(cmd_args, client_arg_name);
-        if (!obj) {
-            if (flags & QMP_ACCEPT_UNKNOWNS) {
-                continue;
-            }
-            qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
-            return -1;
-        }
-
-        arg_type = qobject_to_qstring(obj);
-        assert(arg_type != NULL);
-
-        switch (qstring_get_str(arg_type)[0]) {
-        case 'F':
-        case 'B':
-        case 's':
-            if (qobject_type(client_arg) != QTYPE_QSTRING) {
-                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
-                        "string");
-                return -1;
-            }
-            break;
-        case 'i':
-        case 'l':
-        case 'M':
-        case 'o':
-            if (qobject_type(client_arg) != QTYPE_QINT) {
-                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
-                        "int");
-                return -1;
-            }
-            break;
-        case 'T':
-            if (qobject_type(client_arg) != QTYPE_QINT
-                    && qobject_type(client_arg) != QTYPE_QFLOAT) {
-                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
-                        "number");
-                return -1;
-            }
-            break;
-        case 'b':
-        case '-':
-            if (qobject_type(client_arg) != QTYPE_QBOOL) {
-                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
-                        "bool");
-                return -1;
-            }
-            break;
-        case 'O':
-            assert(flags & QMP_ACCEPT_UNKNOWNS);
-            break;
-        case 'q':
-            break;
-        case '/':
-        case '.':
-        default:
-            abort();
-        }
-    }
-
-    return 0;
-}
-
-static QDict *qdict_from_args_type(const char *args_type) {
-    int i;
-    QDict *qdict;
-    QString *key, *type, *cur_qs;
-
-    assert(args_type != NULL);
-
-    qdict = qdict_new();
-
-    if (args_type == NULL || args_type[0] == '\0') {
-        goto out;
-    }
-
-    key = qstring_new();
-    type = qstring_new();
-
-    cur_qs = key;
-
-    for (i = 0;; i++) {
-        switch (args_type[i]) {
-        case ',':
-        case '\0':
-            qdict_put(qdict, qstring_get_str(key), type);
-            QDECREF(key);
-            if (args_type[i] == '\0') {
-                goto out;
-            }
-            type = qstring_new();
-            cur_qs = key = qstring_new();
-            break;
-        case ':':
-            cur_qs = type;
-            break;
-        default:
-            qstring_append_chr(cur_qs, args_type[i]);
-            break;
-        }
-    }
-
-    out: return qdict;
-}
-
-static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args) {
-    int flags, err;
-    QDict *cmd_args;
-
-    cmd_args = qdict_from_args_type(cmd->args_type);
-
-    flags = 0;
-    err = check_mandatory_args(cmd_args, client_args, &flags);
-    if (err) {
-        goto out;
-    }
-
-    err = check_client_args_type(client_args, cmd_args, flags);
-
-    out:
-    QDECREF(cmd_args);
-    return err;
-}
-
-static QDict *qmp_check_input_obj(QObject *input_obj) {
-    const QDictEntry *ent;
-    int has_exec_key = 0;
-    QDict *input_dict;
-
-    if (qobject_type(input_obj) != QTYPE_QDICT) {
-        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
-        return NULL;
-    }
-
-    input_dict = qobject_to_qdict(input_obj);
-
-    for (ent = qdict_first(input_dict); ent;
-            ent = qdict_next(input_dict, ent)) {
-        const char *arg_name = qdict_entry_key(ent);
-        const QObject *arg_obj = qdict_entry_value(ent);
-
-        if (!strcmp(arg_name, "execute")) {
-            if (qobject_type(arg_obj) != QTYPE_QSTRING) {
-                qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
-                        "string");
-                return NULL;
-            }
-            has_exec_key = 1;
-        } else if (!strcmp(arg_name, "arguments")) {
-            if (qobject_type(arg_obj) != QTYPE_QDICT) {
-                qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
-                        "object");
-                return NULL;
-            }
-        } else if (!strcmp(arg_name, "id")) {
-        } else {
-            qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
-            return NULL;
-        }
-    }
-
-    if (!has_exec_key) {
-        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
-        return NULL;
-    }
-
-    return input_dict;
-}
-
-static int compare_cmd(const char *name, const char *list) {
-    const char *p, *pstart;
-    int len;
-    len = strlen(name);
-    p = list;
-    for (;;) {
-        pstart = p;
-        p = strchr(p, '|');
-        if (!p)
-            p = pstart + strlen(pstart);
-        if ((p - pstart) == len && !memcmp(pstart, name, len))
-            return 1;
-        if (*p == '\0')
-            break;
-        p++;
-    }
-    return 0;
-}
-
-static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
-        const char *cmdname) {
-    const mon_cmd_t *cmd;
-
-    for (cmd = disp_table; cmd->name != NULL; cmd++) {
-        if (compare_cmd(cmdname, cmd->name)) {
-            return cmd;
-        }
-    }
-
-    return NULL;
-}
-
-static const mon_cmd_t *qmp_find_cmd(const char *cmdname) {
-    return search_dispatch_table(qmp_cmds, cmdname);
-}
-
-static void handle_qmp_command(ECS_Client *clii, const char* type_name,
-        QObject *obj) {
-    int err;
-    const mon_cmd_t *cmd;
-    const char *cmd_name;
-    QDict *input = NULL;
-    QDict *args = NULL;
-
-    input = qmp_check_input_obj(obj);
-    if (!input) {
-        qobject_decref(obj);
-        goto err_out;
-    }
-
-    cmd_name = qdict_get_str(input, "execute");
-
-    LOG("execute exists.");
-    cmd = qmp_find_cmd(cmd_name);
-    if (!cmd) {
-        qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
-        goto err_out;
-    }
-
-    obj = qdict_get(input, "arguments");
-    if (!obj) {
-        args = qdict_new();
-    } else {
-        args = qobject_to_qdict(obj);
-        QINCREF(args);
-    }
-
-    err = qmp_check_client_args(cmd, args);
-    if (err < 0) {
-        goto err_out;
-    }
-
-    LOG("argument exists.");
-    if (handler_is_async(cmd)) {
-        err = qmp_async_cmd_handler(clii, cmd, args);
-        if (err) {
-            goto err_out;
-        }
-    } else {
-        LOG("qmp_call_cmd called client fd: %d", clii->client_fd);
-        qmp_call_cmd(clii, clii->cs->mon, type_name, cmd, args);
-    }
-
-    goto out;
-
-    err_out: ecs_protocol_emitter(clii, type_name, NULL);
-    out:
-    QDECREF(input);
-    QDECREF(args);
-
-}
-
-static int check_key(QObject *input_obj, const char *key) {
-    const QDictEntry *ent;
-    QDict *input_dict;
-
-    if (qobject_type(input_obj) != QTYPE_QDICT) {
-        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
-        return -1;
-    }
-
-    input_dict = qobject_to_qdict(input_obj);
-
-    for (ent = qdict_first(input_dict); ent;
-            ent = qdict_next(input_dict, ent)) {
-        const char *arg_name = qdict_entry_key(ent);
-        if (!strcmp(arg_name, key)) {
-            return 1;
-        }
-    }
-
-    return 0;
-}
-
-static QObject* get_data_object(QObject *input_obj) {
-    const QDictEntry *ent;
-    QDict *input_dict;
-
-    if (qobject_type(input_obj) != QTYPE_QDICT) {
-        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
-        return NULL;
-    }
-
-    input_dict = qobject_to_qdict(input_obj);
-
-    for (ent = qdict_first(input_dict); ent;
-            ent = qdict_next(input_dict, ent)) {
-        const char *arg_name = qdict_entry_key(ent);
-        QObject *arg_obj = qdict_entry_value(ent);
-        if (!strcmp(arg_name, COMMANDS_DATA)) {
-            return arg_obj;
-        }
-    }
-
-    return NULL;
-}
-
 void read_val_short(const char* data, unsigned short* ret_val) {
     memcpy(ret_val, data, sizeof(unsigned short));
 }
@@ -727,13 +165,6 @@ void read_val_str(const char* data, char* ret_val, int len) {
     memcpy(ret_val, data, len);
 }
 
-void ecs_make_header(QDict* obj, type_length length, type_group group,
-        type_action action) {
-    qdict_put(obj, "length", qint_from_int((int64_t )length));
-    qdict_put(obj, "group", qint_from_int((int64_t )group));
-    qdict_put(obj, "action", qint_from_int((int64_t )action));
-}
-
 bool ntf_to_control(const char* data, const int len) {
     return true;
 }
@@ -742,165 +173,11 @@ bool ntf_to_monitor(const char* data, const int len) {
     return true;
 }
 
-static int ijcount = 0;
-
-static bool injector_command_proc(ECS_Client *clii, QObject *obj) {
-    QDict* header = qdict_get_qdict(qobject_to_qdict(obj), "header");
-
-    char cmd[10];
-    memset(cmd, 0, 10);
-    strcpy(cmd, qdict_get_str(header, "cat"));
-    type_length length = (type_length) qdict_get_int(header, "length");
-    type_group group = (type_action) (qdict_get_int(header, "group") & 0xff);
-    type_action action = (type_group) (qdict_get_int(header, "action") & 0xff);
-
-    // get data
-    const char* data = qdict_get_str(qobject_to_qdict(obj), COMMANDS_DATA);
-    LOG(">> count= %d", ++ijcount);
-    LOG(">> print len = %zu, data\" %s\"", strlen(data), data);
-    LOG(">> header = cmd = %s, length = %d, action=%d, group=%d", cmd, length,
-            action, group);
-    
-    int datalen = strlen(data);
-    int sndlen = datalen + 14;
-    char* sndbuf = (char*) malloc(sndlen + 1);
-    if (!sndbuf) {
-        return false;
-    }
-
-    memset(sndbuf, 0, sndlen + 1);
-
-    // set data
-    memcpy(sndbuf, cmd, 10);
-    memcpy(sndbuf + 10, &length, 2);
-    memcpy(sndbuf + 12, &group, 1);
-    memcpy(sndbuf + 13, &action, 1);
-    memcpy(sndbuf + 14, data, datalen);
-
-    send_to_evdi(route_ij, sndbuf, sndlen);
-
-    free(sndbuf);
-
-    return true;
-}
-
-static bool device_command_proc(ECS_Client *clii, QObject *obj) {
-    QDict* header = qdict_get_qdict(qobject_to_qdict(obj), "header");
-
-    char cmd[10];
-    memset(cmd, 0, 10);
-    strcpy(cmd, qdict_get_str(header, "cat"));
-    type_length length = (type_length) qdict_get_int(header, "length");
-    type_group group = (type_action) (qdict_get_int(header, "group") & 0xff);
-    type_action action = (type_group) (qdict_get_int(header, "action") & 0xff);
-
-    // get data
-    const char* data = qdict_get_str(qobject_to_qdict(obj), COMMANDS_DATA);
-    LOG(">> count= %d", ++ijcount);
-    LOG(">> print len = %zu, data\" %s\"", strlen(data), data);
-    LOG(">> header = cmd = %s, length = %d, action=%d, group=%d", cmd, length,
-            action, group);
-    if (!strncmp(cmd, MSG_TYPE_SENSOR, 6)) {
-        if (group == MSG_GROUP_STATUS) {
-            if (action ==MSG_ACTION_ACCEL) {
-                get_sensor_accel();
-            } else if (action == MSG_ACTION_GYRO) {
-                get_sensor_gyro();
-            } else if (action == MSG_ACTION_MAG) {
-                get_sensor_mag();
-            } else if (action == MSG_ACTION_LIGHT) {
-                get_sensor_light();
-            } else if (action == MSG_ACTION_PROXI) {
-                get_sensor_proxi();
-            }
-        } else {
-            set_sensor_data(length, data);
-        }
-    }
-    else if (!strncmp(cmd, MSG_TYPE_NFC, 3)) {
-        if (group == MSG_GROUP_STATUS) {
-            send_to_nfc(request_nfc_get, data, length);
-        }
-        else
-        {
-            send_to_nfc(request_nfc_set, data, length);
-        }
-    }
-
-
-    return true;
-}
-
-static void handle_ecs_command(JSONMessageParser *parser, QList *tokens,
-        void *opaque) {
-    const char *type_name;
-    int def_target = 0;
-//  int def_data = 0;
-    QObject *obj;
-    ECS_Client *clii = opaque;
-
-    if (NULL == clii) {
-        LOG("ClientInfo is null.");
-        return;
-    }
-
-#ifdef DEBUG
-    LOG("Handle ecs command.");
-#endif
-
-    obj = json_parser_parse(tokens, NULL);
-    if (!obj) {
-        qerror_report(QERR_JSON_PARSING);
-        ecs_protocol_emitter(clii, NULL, NULL);
-        return;
-    }
-
-    def_target = check_key(obj, COMMANDS_TYPE);
-#ifdef DEBUG
-    LOG("check_key(COMMAND_TYPE): %d", def_target);
-#endif
-    if (0 > def_target) {
-        LOG("def_target failed.");
-        return;
-    } else if (0 == def_target) {
-#ifdef DEBUG
-        LOG("call handle_qmp_command");
-#endif
-        handle_qmp_command(clii, NULL, obj);
-        return;
-    }
-
-    type_name = qdict_get_str(qobject_to_qdict(obj), COMMANDS_TYPE);
-
-    /*
-     def_data = check_key(obj, COMMANDS_DATA);
-     if (0 > def_data) {
-     LOG("json format error: data.");
-     return;
-     } else if (0 == def_data) {
-     LOG("data key is not found.");
-     return;
-     }
-     */
-
-    if (!strcmp(type_name, TYPE_DATA_SELF)) {
-        LOG("set client fd %d keep alive 0", clii->client_fd);
-        clii->keep_alive = 0;
-        return;
-    } else if (!strcmp(type_name, COMMAND_TYPE_INJECTOR)) {
-        injector_command_proc(clii, obj);
-    } else if (!strcmp(type_name, COMMAND_TYPE_CONTROL)) {
-        //control_command_proc(clii, obj);
-    } else if (!strcmp(type_name, COMMAND_TYPE_MONITOR)) {
-        handle_qmp_command(clii, type_name, get_data_object(obj));
-    } else if (!strcmp(type_name, COMMAND_TYPE_DEVICE)) {
-        device_command_proc(clii, obj);
-    } else if (!strcmp(type_name, ECS_MSG_STARTINFO_REQ)) {
-        //ecs_startinfo_req(clii);
-    } else {
-        LOG("handler not found");
-    }
+void ecs_make_header(QDict* obj, type_length length, type_group group,
+        type_action action) {
+    qdict_put(obj, "length", qint_from_int((int64_t )length));
+    qdict_put(obj, "group", qint_from_int((int64_t )group));
+    qdict_put(obj, "action", qint_from_int((int64_t )action));
 }
 
 static Monitor *monitor_create(void) {
@@ -945,15 +222,6 @@ static void ecs_close(ECS_State *cs) {
     }
 }
 
-static int ecs_write(int fd, const uint8_t *buf, int len) {
-    LOG("write buflen : %d, buf : %s", len, buf);
-    if (fd < 0) {
-        return -1;
-    }
-
-    return send_all(fd, buf, len);
-}
-
 #ifndef _WIN32
 static ssize_t ecs_recv(int fd, char *buf, size_t len) {
     struct msghdr msg = { NULL, };
@@ -1110,7 +378,7 @@ static int ecs_add_client(ECS_State *cs, int fd) {
     clii->client_fd = fd;
     clii->cs = cs;
 
-    ecs_json_message_parser_init(&clii->parser, handle_ecs_command, clii);
+    ecs_json_message_parser_init(&clii->parser, handle_qmp_command, clii);
 
 #ifdef CONFIG_LINUX
     epoll_cli_add(cs, fd);
index b1e9f5b..29a22d7 100644 (file)
@@ -108,8 +108,6 @@ typedef unsigned char   type_action;
 #define OUT_BUF_SIZE    4096
 #define READ_BUF_LEN    4096
 
-
-
 typedef struct sbuf
 {
     int _netlen;
@@ -117,7 +115,6 @@ typedef struct sbuf
     char _buf[4096];
 }sbuf;
 
-
 struct Monitor {
     int suspend_cnt;
     uint8_t outbuf[OUT_BUF_SIZE];
@@ -163,58 +160,42 @@ int start_ecs(void);
 int stop_ecs(void);
 int get_ecs_port(void);
 
-void ecs_vprintf(const char *type, const char *fmt, va_list ap);
-void ecs_printf(const char *type, const char *fmt, ...) GCC_FMT_ATTR(2, 3);
-
 bool handle_protobuf_msg(ECS_Client* cli, char* data, const int len);
 
 bool ntf_to_injector(const char* data, const int len);
 bool ntf_to_control(const char* data, const int len);
 bool ntf_to_monitor(const char* data, const int len);
 
-
 bool send_to_ecp(ECS__Master* master);
 
-bool send_start_ans(int host_keyboard_onff);
 bool send_injector_ntf(const char* data, const int len);
-bool send_control_ntf(const char* data, const int len);
 bool send_monitor_ntf(const char* data, const int len);
-bool send_hostkeyboard_ntf(int is_on);
 bool send_device_ntf(const char* data, const int len);
 
 bool send_to_all_client(const char* data, const int len);
 void send_to_client(int fd, const char* data, const int len) ;
 
-
+void ecs_client_close(ECS_Client* clii);
+int ecs_write(int fd, const uint8_t *buf, int len);
 void ecs_make_header(QDict* obj, type_length length, type_group group, type_action action);
 
 void read_val_short(const char* data, unsigned short* ret_val);
 void read_val_char(const char* data, unsigned char* ret_val);
 void read_val_str(const char* data, char* ret_val, int len);
 
-
-//bool msgproc_start_req(ECS_Client* ccli, ECS__StartReq* msg);
 bool msgproc_injector_req(ECS_Client* ccli, ECS__InjectorReq* msg);
-//bool msgproc_control_msg(ECS_Client *cli, ECS__ControlMsg* msg);
 bool msgproc_monitor_req(ECS_Client *ccli, ECS__MonitorReq* msg);
 bool msgproc_device_req(ECS_Client* ccli, ECS__DeviceReq* msg);
-//bool msgproc_screen_dump_req(ECS_Client *ccli, ECS__ScreenDumpReq* msg);
-
-
-//enum{
-//    CONTROL_COMMAND_HOST_KEYBOARD_ONOFF_REQ = 1,
-//    CONTROL_COMMAND_SCREENSHOT_REQ = 2
-//};
 
 /* request */
 int accel_min_max(double value);
 void req_set_sensor_accel(int x, int y, int z);
-
-// control sub messages
-//void msgproc_control_hostkeyboard_req(ECS_Client *cli, ECS__HostKeyboardReq* req);
-
 void set_sensor_data(int length, const char* data);
 
+/* Monitor */
+void handle_ecs_command(JSONMessageParser *parser, QList *tokens, void *opaque);
+void handle_qmp_command(JSONMessageParser *parser, QList *tokens, void *opaque);
+
 static QemuOptsList qemu_ecs_opts = {
     .name = ECS_OPTS_NAME,
     .implied_opt_name = ECS_OPTS_NAME,
diff --git a/tizen/src/ecs/ecs_mon.c b/tizen/src/ecs/ecs_mon.c
new file mode 100644 (file)
index 0000000..4f7323b
--- /dev/null
@@ -0,0 +1,758 @@
+/*
+ * Emulator Control Server
+ *
+ * Copyright (c) 2013 Samsung Electronics Co., Ltd All Rights Reserved
+ *
+ * Contact:
+ *  Jinhyung choi   <jinhyung2.choi@samsung.com>
+ *  MunKyu Im       <munkyu.im@samsung.com>
+ *  Daiyoung Kim    <daiyoung777.kim@samsung.com>
+ *  YeongKyoon Lee  <yeongkyoon.lee@samsung.com>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version 2
+ * of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
+
+#include "qemu-common.h"
+#include "qemu/queue.h"
+#include "qemu/sockets.h"
+#include "qemu/option.h"
+
+#include <monitor/monitor.h>
+#include "qmp-commands.h"
+#include "qapi/qmp/qjson.h"
+#include "qapi/qmp/json-parser.h"
+
+#include "ecs.h"
+#include "hw/maru_virtio_evdi.h"
+#include "hw/maru_virtio_sensor.h"
+#include "hw/maru_virtio_nfc.h"
+
+typedef struct mon_fd_t mon_fd_t;
+struct mon_fd_t {
+    char *name;
+    int fd;
+    QLIST_ENTRY(mon_fd_t)
+    next;
+};
+
+typedef struct mon_cmd_t {
+    const char *name;
+    const char *args_type;
+    const char *params;
+    const char *help;
+    void (*user_print)(Monitor *mon, const QObject *data);
+    union {
+        void (*info)(Monitor *mon);
+        void (*cmd)(Monitor *mon, const QDict *qdict);
+        int (*cmd_new)(Monitor *mon, const QDict *params, QObject **ret_data);
+        int (*cmd_async)(Monitor *mon, const QDict *params,
+                MonitorCompletion *cb, void *opaque);
+    } mhandler;
+    int flags;
+} mon_cmd_t;
+
+/*
+void send_to_client(int fd, const char* data, const int len)
+{
+    char c;
+    uint8_t outbuf[OUT_BUF_SIZE];
+    int outbuf_index = 0;
+
+    for (;;) {
+        c = *data++;
+        if (outbuf_index >= OUT_BUF_SIZE - 1) {
+            LOG("string is too long: overflow buffer.");
+            return;
+        }
+#ifndef _WIN32
+        if (c == '\n') {
+            outbuf[outbuf_index++] = '\r';
+        }
+#endif
+        outbuf[outbuf_index++] = c;
+        if (c == '\0') {
+            break;
+        }
+    }
+    ecs_write(fd, outbuf, outbuf_index);
+}
+*/
+
+#define QMP_ACCEPT_UNKNOWNS 1
+
+static void ecs_monitor_flush(ECS_Client *clii, Monitor *mon) {
+    int ret;
+
+    if (clii && 0 < clii->client_fd && mon && mon->outbuf_index != 0) {
+        ret = ecs_write(clii->client_fd, mon->outbuf, mon->outbuf_index);
+        mon->outbuf_index = 0;
+        if (ret < -1) {
+            ecs_client_close(clii);
+        }
+    }
+}
+
+static void ecs_monitor_puts(ECS_Client *clii, Monitor *mon, const char *str) {
+    char c;
+
+    if (!clii || !mon) {
+        return;
+    }
+
+    for (;;) {
+        c = *str++;
+        if (c == '\0')
+            break;
+#ifndef _WIN32
+        if (c == '\n')
+            mon->outbuf[mon->outbuf_index++] = '\r';
+#endif
+        mon->outbuf[mon->outbuf_index++] = c;
+        if (mon->outbuf_index >= (sizeof(mon->outbuf) - 1) || c == '\n')
+            ecs_monitor_flush(clii, mon);
+    }
+}
+
+static inline int monitor_has_error(const Monitor *mon) {
+    return mon->error != NULL;
+}
+
+static QDict *build_qmp_error_dict(const QError *err) {
+    QObject *obj = qobject_from_jsonf(
+            "{ 'error': { 'class': %s, 'desc': %p } }",
+            ErrorClass_lookup[err->err_class], qerror_human(err));
+
+    return qobject_to_qdict(obj);
+}
+
+static void ecs_json_emitter(ECS_Client *clii, const QObject *data) {
+    QString *json;
+
+    json = qobject_to_json(data);
+
+    assert(json != NULL);
+
+    qstring_append_chr(json, '\n');
+    ecs_monitor_puts(clii, clii->cs->mon, qstring_get_str(json));
+
+    QDECREF(json);
+}
+
+static void ecs_protocol_emitter(ECS_Client *clii, const char* type,
+        QObject *data) {
+    QDict *qmp;
+    QObject *obj;
+
+    LOG("ecs_protocol_emitter called.");
+    //trace_monitor_protocol_emitter(clii->cs->mon);
+
+    if (!monitor_has_error(clii->cs->mon)) {
+        /* success response */
+        qmp = qdict_new();
+        if (data) {
+            qobject_incref(data);
+            qdict_put_obj(qmp, "return", data);
+        } else {
+            /* return an empty QDict by default */
+            qdict_put(qmp, "return", qdict_new());
+        }
+
+        if (type == NULL) {
+            obj = qobject_from_jsonf("%s", "unknown");
+        } else {
+            obj = qobject_from_jsonf("%s", type);
+        }
+        qdict_put_obj(qmp, "type", obj);
+
+    } else {
+        /* error response */
+        qmp = build_qmp_error_dict(clii->cs->mon->error);
+        QDECREF(clii->cs->mon->error);
+        clii->cs->mon->error = NULL;
+    }
+
+    ecs_json_emitter(clii, QOBJECT(qmp));
+    QDECREF(qmp);
+}
+
+static void qmp_monitor_complete(void *opaque, QObject *ret_data) {
+    //   ecs_protocol_emitter(opaque, ret_data);
+}
+
+static int ecs_qmp_async_cmd_handler(ECS_Client *clii, const mon_cmd_t *cmd,
+        const QDict *params) {
+    return cmd->mhandler.cmd_async(clii->cs->mon, params, qmp_monitor_complete,
+            clii);
+}
+
+static void ecs_qmp_call_cmd(ECS_Client *clii, Monitor *mon, const char* type,
+        const mon_cmd_t *cmd, const QDict *params) {
+    int ret;
+    QObject *data = NULL;
+
+    ret = cmd->mhandler.cmd_new(mon, params, &data);
+    if (ret && !monitor_has_error(mon)) {
+        qerror_report(QERR_UNDEFINED_ERROR);
+    }
+    ecs_protocol_emitter(clii, type, data);
+    qobject_decref(data);
+}
+
+static inline bool handler_is_async(const mon_cmd_t *cmd) {
+    return cmd->flags & MONITOR_CMD_ASYNC;
+}
+
+static void monitor_user_noop(Monitor *mon, const QObject *data) {
+}
+
+static int client_migrate_info(Monitor *mon, const QDict *qdict,
+        MonitorCompletion cb, void *opaque) {
+    return 0;
+}
+
+static int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data) {
+    return 0;
+}
+
+static int do_qmp_capabilities(Monitor *mon, const QDict *params,
+        QObject **ret_data) {
+    return 0;
+}
+
+static const mon_cmd_t qmp_cmds[] = {
+#include "qmp-commands-old.h"
+        { /* NULL */}, };
+
+static int check_mandatory_args(const QDict *cmd_args, const QDict *client_args,
+        int *flags) {
+    const QDictEntry *ent;
+
+    for (ent = qdict_first(cmd_args); ent; ent = qdict_next(cmd_args, ent)) {
+        const char *cmd_arg_name = qdict_entry_key(ent);
+        QString *type = qobject_to_qstring(qdict_entry_value(ent));
+        assert(type != NULL);
+
+        if (qstring_get_str(type)[0] == 'O') {
+            assert((*flags & QMP_ACCEPT_UNKNOWNS) == 0);
+            *flags |= QMP_ACCEPT_UNKNOWNS;
+        } else if (qstring_get_str(type)[0] != '-'
+                && qstring_get_str(type)[1] != '?'
+                && !qdict_haskey(client_args, cmd_arg_name)) {
+            qerror_report(QERR_MISSING_PARAMETER, cmd_arg_name);
+            return -1;
+        }
+    }
+
+    return 0;
+}
+
+static int check_client_args_type(const QDict *client_args,
+        const QDict *cmd_args, int flags) {
+    const QDictEntry *ent;
+
+    for (ent = qdict_first(client_args); ent;
+            ent = qdict_next(client_args, ent)) {
+        QObject *obj;
+        QString *arg_type;
+        const QObject *client_arg = qdict_entry_value(ent);
+        const char *client_arg_name = qdict_entry_key(ent);
+
+        obj = qdict_get(cmd_args, client_arg_name);
+        if (!obj) {
+            if (flags & QMP_ACCEPT_UNKNOWNS) {
+                continue;
+            }
+            qerror_report(QERR_INVALID_PARAMETER, client_arg_name);
+            return -1;
+        }
+
+        arg_type = qobject_to_qstring(obj);
+        assert(arg_type != NULL);
+
+        switch (qstring_get_str(arg_type)[0]) {
+        case 'F':
+        case 'B':
+        case 's':
+            if (qobject_type(client_arg) != QTYPE_QSTRING) {
+                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
+                        "string");
+                return -1;
+            }
+            break;
+        case 'i':
+        case 'l':
+        case 'M':
+        case 'o':
+            if (qobject_type(client_arg) != QTYPE_QINT) {
+                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
+                        "int");
+                return -1;
+            }
+            break;
+        case 'T':
+            if (qobject_type(client_arg) != QTYPE_QINT
+                    && qobject_type(client_arg) != QTYPE_QFLOAT) {
+                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
+                        "number");
+                return -1;
+            }
+            break;
+        case 'b':
+        case '-':
+            if (qobject_type(client_arg) != QTYPE_QBOOL) {
+                qerror_report(QERR_INVALID_PARAMETER_TYPE, client_arg_name,
+                        "bool");
+                return -1;
+            }
+            break;
+        case 'O':
+            assert(flags & QMP_ACCEPT_UNKNOWNS);
+            break;
+        case 'q':
+            break;
+        case '/':
+        case '.':
+        default:
+            abort();
+        }
+    }
+
+    return 0;
+}
+
+static QDict *qdict_from_args_type(const char *args_type) {
+    int i;
+    QDict *qdict;
+    QString *key, *type, *cur_qs;
+
+    assert(args_type != NULL);
+
+    qdict = qdict_new();
+
+    if (args_type == NULL || args_type[0] == '\0') {
+        goto out;
+    }
+
+    key = qstring_new();
+    type = qstring_new();
+
+    cur_qs = key;
+
+    for (i = 0;; i++) {
+        switch (args_type[i]) {
+        case ',':
+        case '\0':
+            qdict_put(qdict, qstring_get_str(key), type);
+            QDECREF(key);
+            if (args_type[i] == '\0') {
+                goto out;
+            }
+            type = qstring_new();
+            cur_qs = key = qstring_new();
+            break;
+        case ':':
+            cur_qs = type;
+            break;
+        default:
+            qstring_append_chr(cur_qs, args_type[i]);
+            break;
+        }
+    }
+
+    out: return qdict;
+}
+
+static int qmp_check_client_args(const mon_cmd_t *cmd, QDict *client_args) {
+    int flags, err;
+    QDict *cmd_args;
+
+    cmd_args = qdict_from_args_type(cmd->args_type);
+
+    flags = 0;
+    err = check_mandatory_args(cmd_args, client_args, &flags);
+    if (err) {
+        goto out;
+    }
+
+    err = check_client_args_type(client_args, cmd_args, flags);
+
+    out:
+    QDECREF(cmd_args);
+    return err;
+}
+
+static QDict *qmp_check_input_obj(QObject *input_obj) {
+    const QDictEntry *ent;
+    int has_exec_key = 0;
+    QDict *input_dict;
+
+    if (qobject_type(input_obj) != QTYPE_QDICT) {
+        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
+        return NULL;
+    }
+
+    input_dict = qobject_to_qdict(input_obj);
+
+    for (ent = qdict_first(input_dict); ent;
+            ent = qdict_next(input_dict, ent)) {
+        const char *arg_name = qdict_entry_key(ent);
+        const QObject *arg_obj = qdict_entry_value(ent);
+
+        if (!strcmp(arg_name, "execute")) {
+            if (qobject_type(arg_obj) != QTYPE_QSTRING) {
+                qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "execute",
+                        "string");
+                return NULL;
+            }
+            has_exec_key = 1;
+        } else if (!strcmp(arg_name, "arguments")) {
+            if (qobject_type(arg_obj) != QTYPE_QDICT) {
+                qerror_report(QERR_QMP_BAD_INPUT_OBJECT_MEMBER, "arguments",
+                        "object");
+                return NULL;
+            }
+        } else if (!strcmp(arg_name, "id")) {
+        } else {
+            qerror_report(QERR_QMP_EXTRA_MEMBER, arg_name);
+            return NULL;
+        }
+    }
+
+    if (!has_exec_key) {
+        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "execute");
+        return NULL;
+    }
+
+    return input_dict;
+}
+
+static int compare_cmd(const char *name, const char *list) {
+    const char *p, *pstart;
+    int len;
+    len = strlen(name);
+    p = list;
+    for (;;) {
+        pstart = p;
+        p = strchr(p, '|');
+        if (!p)
+            p = pstart + strlen(pstart);
+        if ((p - pstart) == len && !memcmp(pstart, name, len))
+            return 1;
+        if (*p == '\0')
+            break;
+        p++;
+    }
+    return 0;
+}
+
+static const mon_cmd_t *search_dispatch_table(const mon_cmd_t *disp_table,
+        const char *cmdname) {
+    const mon_cmd_t *cmd;
+
+    for (cmd = disp_table; cmd->name != NULL; cmd++) {
+        if (compare_cmd(cmdname, cmd->name)) {
+            return cmd;
+        }
+    }
+
+    return NULL;
+}
+
+static const mon_cmd_t *qmp_find_cmd(const char *cmdname) {
+    return search_dispatch_table(qmp_cmds, cmdname);
+}
+
+void handle_qmp_command(JSONMessageParser *parser, QList *tokens,
+        void *opaque) {
+    int err;
+    QObject *obj;
+    QDict *input, *args;
+    const mon_cmd_t *cmd;
+    const char *cmd_name;
+    Monitor *mon = cur_mon;
+    ECS_Client *clii = opaque;
+
+    args = input = NULL;
+
+    obj = json_parser_parse(tokens, NULL);
+    if (!obj) {
+        // FIXME: should be triggered in json_parser_parse()
+        qerror_report(QERR_JSON_PARSING);
+        goto err_out;
+    }
+
+    input = qmp_check_input_obj(obj);
+    if (!input) {
+        qobject_decref(obj);
+        goto err_out;
+    }
+
+    cmd_name = qdict_get_str(input, "execute");
+    //trace_handle_qmp_command(mon, cmd_name);
+
+    cmd = qmp_find_cmd(cmd_name);
+    if (!cmd) {
+        qerror_report(QERR_COMMAND_NOT_FOUND, cmd_name);
+        goto err_out;
+    }
+
+    obj = qdict_get(input, "arguments");
+    if (!obj) {
+        args = qdict_new();
+    } else {
+        args = qobject_to_qdict(obj);
+        QINCREF(args);
+    }
+
+    err = qmp_check_client_args(cmd, args);
+    if (err < 0) {
+        goto err_out;
+    }
+
+    if (handler_is_async(cmd)) {
+        err = ecs_qmp_async_cmd_handler(clii, cmd, args);
+        if (err) {
+            /* emit the error response */
+            goto err_out;
+        }
+    } else {
+        ecs_qmp_call_cmd(clii, mon, NULL, cmd, args);
+    }
+
+    goto out;
+
+err_out:
+    ecs_protocol_emitter(clii, NULL, NULL);
+out:
+    QDECREF(input);
+    QDECREF(args);
+}
+
+static int check_key(QObject *input_obj, const char *key) {
+    const QDictEntry *ent;
+    QDict *input_dict;
+
+    if (qobject_type(input_obj) != QTYPE_QDICT) {
+        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
+        return -1;
+    }
+
+    input_dict = qobject_to_qdict(input_obj);
+
+    for (ent = qdict_first(input_dict); ent;
+            ent = qdict_next(input_dict, ent)) {
+        const char *arg_name = qdict_entry_key(ent);
+        if (!strcmp(arg_name, key)) {
+            return 1;
+        }
+    }
+
+    return 0;
+}
+#if 0
+static QObject* get_data_object(QObject *input_obj) {
+    const QDictEntry *ent;
+    QDict *input_dict;
+
+    if (qobject_type(input_obj) != QTYPE_QDICT) {
+        qerror_report(QERR_QMP_BAD_INPUT_OBJECT, "object");
+        return NULL;
+    }
+
+    input_dict = qobject_to_qdict(input_obj);
+
+    for (ent = qdict_first(input_dict); ent;
+            ent = qdict_next(input_dict, ent)) {
+        const char *arg_name = qdict_entry_key(ent);
+        QObject *arg_obj = qdict_entry_value(ent);
+        if (!strcmp(arg_name, COMMANDS_DATA)) {
+            return arg_obj;
+        }
+    }
+
+    return NULL;
+}
+#endif
+static int ijcount = 0;
+
+static bool injector_command_proc(ECS_Client *clii, QObject *obj) {
+    QDict* header = qdict_get_qdict(qobject_to_qdict(obj), "header");
+
+    char cmd[10];
+    memset(cmd, 0, 10);
+    strcpy(cmd, qdict_get_str(header, "cat"));
+    type_length length = (type_length) qdict_get_int(header, "length");
+    type_group group = (type_action) (qdict_get_int(header, "group") & 0xff);
+    type_action action = (type_group) (qdict_get_int(header, "action") & 0xff);
+
+    // get data
+    const char* data = qdict_get_str(qobject_to_qdict(obj), COMMANDS_DATA);
+    LOG(">> count= %d", ++ijcount);
+    LOG(">> print len = %zu, data\" %s\"", strlen(data), data);
+    LOG(">> header = cmd = %s, length = %d, action=%d, group=%d", cmd, length,
+            action, group);
+
+    int datalen = strlen(data);
+    int sndlen = datalen + 14;
+    char* sndbuf = (char*) malloc(sndlen + 1);
+    if (!sndbuf) {
+        return false;
+    }
+
+    memset(sndbuf, 0, sndlen + 1);
+
+    // set data
+    memcpy(sndbuf, cmd, 10);
+    memcpy(sndbuf + 10, &length, 2);
+    memcpy(sndbuf + 12, &group, 1);
+    memcpy(sndbuf + 13, &action, 1);
+    memcpy(sndbuf + 14, data, datalen);
+
+    send_to_evdi(route_ij, sndbuf, sndlen);
+
+    free(sndbuf);
+
+    return true;
+}
+
+static bool device_command_proc(ECS_Client *clii, QObject *obj) {
+    QDict* header = qdict_get_qdict(qobject_to_qdict(obj), "header");
+
+    char cmd[10];
+    memset(cmd, 0, 10);
+    strcpy(cmd, qdict_get_str(header, "cat"));
+    type_length length = (type_length) qdict_get_int(header, "length");
+    type_group group = (type_action) (qdict_get_int(header, "group") & 0xff);
+    type_action action = (type_group) (qdict_get_int(header, "action") & 0xff);
+
+    // get data
+    const char* data = qdict_get_str(qobject_to_qdict(obj), COMMANDS_DATA);
+    LOG(">> count= %d", ++ijcount);
+    LOG(">> print len = %zu, data\" %s\"", strlen(data), data);
+    LOG(">> header = cmd = %s, length = %d, action=%d, group=%d", cmd, length,
+            action, group);
+
+    if (!strncmp(cmd, MSG_TYPE_SENSOR, 6)) {
+        if (group == MSG_GROUP_STATUS) {
+            if (action ==MSG_ACTION_ACCEL) {
+                get_sensor_accel();
+            } else if (action == MSG_ACTION_GYRO) {
+                get_sensor_gyro();
+            } else if (action == MSG_ACTION_MAG) {
+                get_sensor_mag();
+            } else if (action == MSG_ACTION_LIGHT) {
+                get_sensor_light();
+            } else if (action == MSG_ACTION_PROXI) {
+                get_sensor_proxi();
+            }
+        } else {
+            set_sensor_data(length, data);
+        }
+    }
+    else if (!strncmp(cmd, MSG_TYPE_NFC, 3)) {
+        if (group == MSG_GROUP_STATUS) {
+            send_to_nfc(request_nfc_get, data, length);
+        }
+        else
+        {
+            send_to_nfc(request_nfc_set, data, length);
+        }
+    }
+
+
+    return true;
+}
+void handle_ecs_command(JSONMessageParser *parser, QList *tokens,
+        void *opaque) {
+    const char *type_name;
+    int def_target = 0;
+//  int def_data = 0;
+    QObject *obj;
+    ECS_Client *clii = opaque;
+
+    if (NULL == clii) {
+        LOG("ClientInfo is null.");
+        return;
+    }
+
+#ifdef DEBUG
+    LOG("Handle ecs command.");
+#endif
+
+    obj = json_parser_parse(tokens, NULL);
+    if (!obj) {
+        qerror_report(QERR_JSON_PARSING);
+        ecs_protocol_emitter(clii, NULL, NULL);
+        return;
+    }
+
+    def_target = check_key(obj, COMMANDS_TYPE);
+#ifdef DEBUG
+    LOG("check_key(COMMAND_TYPE): %d", def_target);
+#endif
+    if (0 > def_target) {
+        LOG("def_target failed.");
+        return;
+    } else if (0 == def_target) {
+#ifdef DEBUG
+        LOG("call handle_qmp_command");
+#endif
+        //handle_qmp_command(clii, NULL, obj);
+        return;
+    }
+
+    type_name = qdict_get_str(qobject_to_qdict(obj), COMMANDS_TYPE);
+
+    /*
+     def_data = check_key(obj, COMMANDS_DATA);
+     if (0 > def_data) {
+     LOG("json format error: data.");
+     return;
+     } else if (0 == def_data) {
+     LOG("data key is not found.");
+     return;
+     }
+     */
+
+    if (!strcmp(type_name, TYPE_DATA_SELF)) {
+        LOG("set client fd %d keep alive 0", clii->client_fd);
+        clii->keep_alive = 0;
+        return;
+    } else if (!strcmp(type_name, COMMAND_TYPE_INJECTOR)) {
+        injector_command_proc(clii, obj);
+    } else if (!strcmp(type_name, COMMAND_TYPE_CONTROL)) {
+        //control_command_proc(clii, obj);
+    } else if (!strcmp(type_name, COMMAND_TYPE_MONITOR)) {
+     //   handle_qmp_command(clii, type_name, get_data_object(obj));
+    } else if (!strcmp(type_name, COMMAND_TYPE_DEVICE)) {
+        device_command_proc(clii, obj);
+    } else if (!strcmp(type_name, ECS_MSG_STARTINFO_REQ)) {
+        //ecs_startinfo_req(clii);
+    } else {
+        LOG("handler not found");
+    }
+}
+
+bool msgproc_monitor_req(ECS_Client *ccli, ECS__MonitorReq* msg)
+{
+    LOG(">> monitor req: data = %s", msg->command);
+    ecs_json_message_parser_feed(&(ccli->parser), (const char *) msg->command, strlen(msg->command));
+    return true;
+}
+
index 77848f7..2b139cd 100644 (file)
@@ -183,11 +183,6 @@ bool msgproc_control_msg(ECS_Client *cli, ECS__ControlMsg* msg)
     return true;
 }
 #endif
-bool msgproc_monitor_req(ECS_Client *ccli, ECS__MonitorReq* msg)
-{
-
-    return true;
-}
 
 bool msgproc_device_req(ECS_Client* ccli, ECS__DeviceReq* msg)
 {
index eeae677..97c52a3 100644 (file)
@@ -3,7 +3,7 @@
 #ifndef PROTOBUF_C_ecs_2eproto__INCLUDED
 #define PROTOBUF_C_ecs_2eproto__INCLUDED
 
-#include <google/protobuf-c/protobuf-c.h>
+#include "../../../distrib/protobuf/protobuf-c.h"
 
 PROTOBUF_C_BEGIN_DECLS
 
index 47f7d2f..1dfac59 100644 (file)
@@ -17,6 +17,7 @@ enum Master_Type {
        MONITOR_ANS = 13;
        MONITOR_NTF = 14;
 
+       // extension from 101 to 150
        NFC_REQ = 101;
        NFC_NTF = 102;
 }