endpoints.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. #include "c_stdio.h"
  2. #include "c_string.h"
  3. #include "c_stdlib.h"
  4. #include "coap.h"
  5. #include "lua.h"
  6. #include "lauxlib.h"
  7. #include "lualib.h"
  8. #include "os_type.h"
  9. #include "user_interface.h"
  10. #include "user_config.h"
  11. void build_well_known_rsp(char *rsp, uint16_t rsplen);
  12. void endpoint_setup(void)
  13. {
  14. coap_setup();
  15. }
  16. static const coap_endpoint_path_t path_well_known_core = {2, {".well-known", "core"}};
  17. static int handle_get_well_known_core(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
  18. {
  19. outpkt->content.p = (uint8_t *)c_zalloc(MAX_PAYLOAD_SIZE); // this should be free-ed when outpkt is built in coap_server_respond()
  20. if(outpkt->content.p == NULL){
  21. NODE_DBG("not enough memory\n");
  22. return COAP_ERR_BUFFER_TOO_SMALL;
  23. }
  24. outpkt->content.len = MAX_PAYLOAD_SIZE;
  25. build_well_known_rsp(outpkt->content.p, outpkt->content.len);
  26. return coap_make_response(scratch, outpkt, (const uint8_t *)outpkt->content.p, c_strlen(outpkt->content.p), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_APPLICATION_LINKFORMAT);
  27. }
  28. static const coap_endpoint_path_t path_variable = {2, {"v1", "v"}};
  29. static int handle_get_variable(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
  30. {
  31. const coap_option_t *opt;
  32. uint8_t count;
  33. int n;
  34. lua_State *L = lua_getstate();
  35. if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
  36. {
  37. if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
  38. {
  39. NODE_DBG("should never happen.\n");
  40. goto end;
  41. }
  42. if (count == ep->path->count + 1)
  43. {
  44. coap_luser_entry *h = ep->user_entry->next; // ->next: skip the first entry(head)
  45. while(NULL != h){
  46. if (opt[count-1].buf.len != c_strlen(h->name))
  47. {
  48. h = h->next;
  49. continue;
  50. }
  51. if (0 == c_memcmp(h->name, opt[count-1].buf.p, opt[count-1].buf.len))
  52. {
  53. NODE_DBG("/v1/v/");
  54. NODE_DBG((char *)h->name);
  55. NODE_DBG(" match.\n");
  56. if(c_strlen(h->name))
  57. {
  58. n = lua_gettop(L);
  59. lua_getglobal(L, h->name);
  60. if (!lua_isnumber(L, -1) && !lua_isstring(L, -1)) {
  61. NODE_DBG ("should be a number or string.\n");
  62. lua_settop(L, n);
  63. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
  64. } else {
  65. const char *res = lua_tostring(L,-1);
  66. lua_settop(L, n);
  67. return coap_make_response(scratch, outpkt, (const uint8_t *)res, c_strlen(res), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, h->content_type);
  68. }
  69. }
  70. } else {
  71. h = h->next;
  72. }
  73. }
  74. }else{
  75. NODE_DBG("/v1/v match.\n");
  76. goto end;
  77. }
  78. }
  79. NODE_DBG("none match.\n");
  80. end:
  81. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
  82. }
  83. static const coap_endpoint_path_t path_function = {2, {"v1", "f"}};
  84. static int handle_post_function(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
  85. {
  86. const coap_option_t *opt;
  87. uint8_t count;
  88. int n;
  89. lua_State *L = lua_getstate();
  90. if (NULL != (opt = coap_findOptions(inpkt, COAP_OPTION_URI_PATH, &count)))
  91. {
  92. if ((count != ep->path->count ) && (count != ep->path->count + 1)) // +1 for /f/[function], /v/[variable]
  93. {
  94. NODE_DBG("should never happen.\n");
  95. goto end;
  96. }
  97. if (count == ep->path->count + 1)
  98. {
  99. coap_luser_entry *h = ep->user_entry->next; // ->next: skip the first entry(head)
  100. while(NULL != h){
  101. if (opt[count-1].buf.len != c_strlen(h->name))
  102. {
  103. h = h->next;
  104. continue;
  105. }
  106. if (0 == c_memcmp(h->name, opt[count-1].buf.p, opt[count-1].buf.len))
  107. {
  108. NODE_DBG("/v1/f/");
  109. NODE_DBG((char *)h->name);
  110. NODE_DBG(" match.\n");
  111. if(c_strlen(h->name))
  112. {
  113. n = lua_gettop(L);
  114. lua_getglobal(L, h->name);
  115. if (lua_type(L, -1) != LUA_TFUNCTION) {
  116. NODE_DBG ("should be a function\n");
  117. lua_settop(L, n);
  118. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
  119. } else {
  120. lua_pushlstring(L, inpkt->payload.p, inpkt->payload.len); // make sure payload.p is filled with '\0' after payload.len, or use lua_pushlstring
  121. lua_call(L, 1, 1);
  122. if (!lua_isnil(L, -1)){ /* get return? */
  123. if( lua_isstring(L, -1) ) // deal with the return string
  124. {
  125. size_t len = 0;
  126. const char *ret = luaL_checklstring( L, -1, &len );
  127. if(len > MAX_PAYLOAD_SIZE){
  128. lua_settop(L, n);
  129. luaL_error( L, "return string:<MAX_PAYLOAD_SIZE" );
  130. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
  131. }
  132. NODE_DBG((char *)ret);
  133. NODE_DBG("\n");
  134. lua_settop(L, n);
  135. return coap_make_response(scratch, outpkt, ret, len, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
  136. }
  137. } else {
  138. lua_settop(L, n);
  139. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
  140. }
  141. }
  142. }
  143. } else {
  144. h = h->next;
  145. }
  146. }
  147. }else{
  148. NODE_DBG("/v1/f match.\n");
  149. goto end;
  150. }
  151. }
  152. NODE_DBG("none match.\n");
  153. end:
  154. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_NOT_FOUND, COAP_CONTENTTYPE_NONE);
  155. }
  156. extern int lua_put_line(const char *s, size_t l);
  157. static const coap_endpoint_path_t path_command = {2, {"v1", "c"}};
  158. static int handle_post_command(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
  159. {
  160. if (inpkt->payload.len == 0)
  161. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_BAD_REQUEST, COAP_CONTENTTYPE_TEXT_PLAIN);
  162. if (inpkt->payload.len > 0)
  163. {
  164. char line[LUA_MAXINPUT];
  165. if (!coap_buffer_to_string(line, LUA_MAXINPUT, &inpkt->payload) &&
  166. lua_put_line(line, c_strlen(line))) {
  167. NODE_DBG("\nResult(if any):\n");
  168. system_os_post (LUA_TASK_PRIO, LUA_PROCESS_LINE_SIG, 0);
  169. }
  170. return coap_make_response(scratch, outpkt, NULL, 0, id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
  171. }
  172. }
  173. static uint32_t id = 0;
  174. static const coap_endpoint_path_t path_id = {2, {"v1", "id"}};
  175. static int handle_get_id(const coap_endpoint_t *ep, coap_rw_buffer_t *scratch, const coap_packet_t *inpkt, coap_packet_t *outpkt, uint8_t id_hi, uint8_t id_lo)
  176. {
  177. id = system_get_chip_id();
  178. return coap_make_response(scratch, outpkt, (const uint8_t *)(&id), sizeof(uint32_t), id_hi, id_lo, &inpkt->tok, COAP_RSPCODE_CONTENT, COAP_CONTENTTYPE_TEXT_PLAIN);
  179. }
  180. coap_luser_entry var_head = {NULL,NULL,0};
  181. coap_luser_entry *variable_entry = &var_head;
  182. coap_luser_entry func_head = {NULL,NULL,0};
  183. coap_luser_entry *function_entry = &func_head;
  184. const coap_endpoint_t endpoints[] =
  185. {
  186. {COAP_METHOD_GET, handle_get_well_known_core, &path_well_known_core, "ct=40", NULL},
  187. {COAP_METHOD_GET, handle_get_variable, &path_variable, "ct=0", &var_head},
  188. {COAP_METHOD_POST, handle_post_function, &path_function, NULL, &func_head},
  189. {COAP_METHOD_POST, handle_post_command, &path_command, NULL, NULL},
  190. {COAP_METHOD_GET, handle_get_id, &path_id, "ct=0", NULL},
  191. {(coap_method_t)0, NULL, NULL, NULL, NULL}
  192. };
  193. void build_well_known_rsp(char *rsp, uint16_t rsplen)
  194. {
  195. const coap_endpoint_t *ep = endpoints;
  196. int i;
  197. uint16_t len = rsplen;
  198. c_memset(rsp, 0, len);
  199. len--; // Null-terminated string
  200. while(NULL != ep->handler)
  201. {
  202. if (NULL == ep->core_attr) {
  203. ep++;
  204. continue;
  205. }
  206. if (NULL == ep->user_entry){
  207. if (0 < c_strlen(rsp)) {
  208. c_strncat(rsp, ",", len);
  209. len--;
  210. }
  211. c_strncat(rsp, "<", len);
  212. len--;
  213. for (i = 0; i < ep->path->count; i++) {
  214. c_strncat(rsp, "/", len);
  215. len--;
  216. c_strncat(rsp, ep->path->elems[i], len);
  217. len -= c_strlen(ep->path->elems[i]);
  218. }
  219. c_strncat(rsp, ">;", len);
  220. len -= 2;
  221. c_strncat(rsp, ep->core_attr, len);
  222. len -= c_strlen(ep->core_attr);
  223. } else {
  224. coap_luser_entry *h = ep->user_entry->next; // ->next: skip the first entry(head)
  225. while(NULL != h){
  226. if (0 < c_strlen(rsp)) {
  227. c_strncat(rsp, ",", len);
  228. len--;
  229. }
  230. c_strncat(rsp, "<", len);
  231. len--;
  232. for (i = 0; i < ep->path->count; i++) {
  233. c_strncat(rsp, "/", len);
  234. len--;
  235. c_strncat(rsp, ep->path->elems[i], len);
  236. len -= c_strlen(ep->path->elems[i]);
  237. }
  238. c_strncat(rsp, "/", len);
  239. len--;
  240. c_strncat(rsp, h->name, len);
  241. len -= c_strlen(h->name);
  242. c_strncat(rsp, ">;", len);
  243. len -= 2;
  244. c_strncat(rsp, ep->core_attr, len);
  245. len -= c_strlen(ep->core_attr);
  246. h = h->next;
  247. }
  248. }
  249. ep++;
  250. }
  251. }