endpoints.c 11 KB

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