void network_checking(void) { int fd; http_down("/files.json", "http://lab.rt-thread.org/realboard/lpc4088/files.json"); fd = open("/files.json", O_RDONLY, 0); if (fd >= 0) { int length; char *ptr; length = lseek(fd, 0, SEEK_END); if (length > 0) { lseek(fd, 0, SEEK_SET); ptr = (char*)rt_malloc(length); if (ptr != RT_NULL) { struct json_tree* tree; length = read(fd, ptr, length); tree = json_tree_parse(ptr, length); if (tree != RT_NULL) { int index = 0; struct json_node* node; while (1) { const char *url; const char *local_url; node = json_node_get_array(&(tree->root), index, RT_NULL); if (node == RT_NULL) break; index ++; url = json_node_get_string(node, "url", RT_NULL); local_url = json_node_get_string(node, "local_url", RT_NULL); rt_kprintf("%s=>%s\n", local_url, url); } json_tree_destroy(tree); } rt_free(ptr); } } close(fd); } }
static void json_rpc_handle(const char* json, size_t length) { int status = 0; struct json_tree* tree = RT_NULL; tree = json_tree_parse(json, length); if (tree != RT_NULL) { const char *version; const char *method; struct json_node *params; int id, index, position; char cmd[64]; /* check parameter */ version = json_tree_get_string(tree, "jsonrpc", RT_NULL); method = json_tree_get_string(tree, "method", RT_NULL); id = json_tree_get_integer(tree, "id", RT_NULL); params = json_tree_get_node(tree, "params", RT_NULL); /* parameter check */ if (strcmp(version, "2.0") != 0) goto __exit; position = 0; position += rt_sprintf(&cmd[position], "%s(", method); for (index = 0; index < params->count; index ++) { struct json_node *node; node = params->vu.array[index]; if (index != 0) position += rt_sprintf(&cmd[position], ","); if (node->type == JSON_NODE_TYPE_STR) position += rt_sprintf(&cmd[position], "\"%s\"", node->vu.str_value); if (node->type == JSON_NODE_TYPE_INT) position += rt_sprintf(&cmd[position], "%d", node->vu.int_value); if (node->type == JSON_NODE_TYPE_FLOAT) position += rt_sprintf(&cmd[position], "%f", node->vu.float_value); } position += rt_sprintf(&cmd[position], ")"); rt_kprintf("cmd: %s\n", cmd); } __exit: if (tree != RT_NULL) json_tree_destroy(tree); return; }