user_json.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /******************************************************************************
  2. * Copyright 2013-2014 Espressif Systems (Wuxi)
  3. *
  4. * FileName: user_json.c
  5. *
  6. * Description: JSON format set up and parse.
  7. * Check your hardware transmation while use this data format.
  8. *
  9. * Modification history:
  10. * 2014/5/09, v1.0 create this file.
  11. *******************************************************************************/
  12. #include "ets_sys.h"
  13. #include "osapi.h"
  14. #include "os_type.h"
  15. #include "mem.h"
  16. #include "user_json.h"
  17. LOCAL char *json_buf;
  18. LOCAL int pos;
  19. LOCAL int size;
  20. /******************************************************************************
  21. * FunctionName : find_json_path
  22. * Description : find the JSON format tree's path
  23. * Parameters : json -- A pointer to a JSON set up
  24. * path -- A pointer to the JSON format tree's path
  25. * Returns : A pointer to the JSON format tree
  26. *******************************************************************************/
  27. struct jsontree_value *ICACHE_FLASH_ATTR
  28. find_json_path(struct jsontree_context *json, const char *path)
  29. {
  30. struct jsontree_value *v;
  31. const char *start;
  32. const char *end;
  33. int len;
  34. v = json->values[0];
  35. start = path;
  36. do {
  37. end = (const char *)os_strstr(start, "/");
  38. if (end == start) {
  39. break;
  40. }
  41. if (end != NULL) {
  42. len = end - start;
  43. end++;
  44. } else {
  45. len = os_strlen(start);
  46. }
  47. if (v->type != JSON_TYPE_OBJECT) {
  48. v = NULL;
  49. } else {
  50. struct jsontree_object *o;
  51. int i;
  52. o = (struct jsontree_object *)v;
  53. v = NULL;
  54. for (i = 0; i < o->count; i++) {
  55. if (os_strncmp(start, o->pairs[i].name, len) == 0) {
  56. v = o->pairs[i].value;
  57. json->index[json->depth] = i;
  58. json->depth++;
  59. json->values[json->depth] = v;
  60. json->index[json->depth] = 0;
  61. break;
  62. }
  63. }
  64. }
  65. start = end;
  66. } while (end != NULL && *end != '\0' && v != NULL);
  67. json->callback_state = 0;
  68. return v;
  69. }
  70. /******************************************************************************
  71. * FunctionName : json_putchar
  72. * Description : write the value to the JSON format tree
  73. * Parameters : c -- the value which write the JSON format tree
  74. * Returns : result
  75. *******************************************************************************/
  76. int ICACHE_FLASH_ATTR
  77. json_putchar(int c)
  78. {
  79. if (json_buf != NULL && pos <= size) {
  80. json_buf[pos++] = c;
  81. return c;
  82. }
  83. return 0;
  84. }
  85. /******************************************************************************
  86. * FunctionName : json_ws_send
  87. * Description : set up the JSON format tree for string
  88. * Parameters : tree -- A pointer to the JSON format tree
  89. * path -- A pointer to the JSON format tree's path
  90. * pbuf -- A pointer for the data sent
  91. * Returns : none
  92. *******************************************************************************/
  93. void ICACHE_FLASH_ATTR
  94. json_ws_send(struct jsontree_value *tree, const char *path, char *pbuf)
  95. {
  96. struct jsontree_context json;
  97. /* maxsize = 128 bytes */
  98. json_buf = (char *)os_malloc(jsonSize);
  99. /* reset state and set max-size */
  100. /* NOTE: packet will be truncated at 512 bytes */
  101. pos = 0;
  102. size = jsonSize;
  103. json.values[0] = (struct jsontree_value *)tree;
  104. jsontree_reset(&json);
  105. find_json_path(&json, path);
  106. json.path = json.depth;
  107. json.putchar = json_putchar;
  108. while (jsontree_print_next(&json) && json.path <= json.depth);
  109. json_buf[pos] = 0;
  110. os_memcpy(pbuf, json_buf, pos);
  111. os_free(json_buf);
  112. }
  113. /******************************************************************************
  114. * FunctionName : json_parse
  115. * Description : parse the data as a JSON format
  116. * Parameters : js_ctx -- A pointer to a JSON set up
  117. * ptrJSONMessage -- A pointer to the data
  118. * Returns : none
  119. *******************************************************************************/
  120. void ICACHE_FLASH_ATTR
  121. json_parse(struct jsontree_context *json, char *ptrJSONMessage)
  122. {
  123. /* Set value */
  124. struct jsontree_value *v;
  125. struct jsontree_callback *c;
  126. struct jsontree_callback *c_bak = NULL;
  127. while ((v = jsontree_find_next(json, JSON_TYPE_CALLBACK)) != NULL) {
  128. c = (struct jsontree_callback *)v;
  129. if (c == c_bak) {
  130. continue;
  131. }
  132. c_bak = c;
  133. if (c->set != NULL) {
  134. struct jsonparse_state js;
  135. jsonparse_setup(&js, ptrJSONMessage, os_strlen(ptrJSONMessage));
  136. c->set(json, &js);
  137. }
  138. }
  139. }