request.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "attr_container.h"
  6. #include "request.h"
  7. #include "shared_utils.h"
  8. #include "wasm_app.h"
  9. #include "req_resp_api.h"
  10. #include "timer_api.h"
  11. #define TRANSACTION_TIMEOUT_MS 5000
  12. typedef enum {
  13. Reg_Event, Reg_Request
  14. } reg_type_t;
  15. typedef struct _res_register {
  16. struct _res_register *next;
  17. const char * url;
  18. reg_type_t reg_type;
  19. void (*request_handler)(request_t *);
  20. } res_register_t;
  21. typedef struct transaction {
  22. struct transaction *next;
  23. int mid;
  24. unsigned int time; /* start time */
  25. response_handler_f handler;
  26. void *user_data;
  27. } transaction_t;
  28. static res_register_t * g_resources = NULL;
  29. static transaction_t *g_transactions = NULL;
  30. static user_timer_t g_trans_timer = NULL;
  31. static transaction_t *transaction_find(int mid)
  32. {
  33. transaction_t *t = g_transactions;
  34. while (t) {
  35. if (t->mid == mid)
  36. return t;
  37. t = t->next;
  38. }
  39. return NULL;
  40. }
  41. /*
  42. * new transaction is added to the tail of the list, so the list
  43. * is sorted by expiry time naturally.
  44. */
  45. static void transaction_add(transaction_t *trans)
  46. {
  47. transaction_t *t;
  48. if (g_transactions == NULL) {
  49. g_transactions = trans;
  50. return;
  51. }
  52. t = g_transactions;
  53. while (t) {
  54. if (t->next == NULL) {
  55. t->next = trans;
  56. return;
  57. }
  58. }
  59. }
  60. static void transaction_remove(transaction_t *trans)
  61. {
  62. transaction_t *prev = NULL, *current = g_transactions;
  63. while (current) {
  64. if (current == trans) {
  65. if (prev == NULL) {
  66. g_transactions = current->next;
  67. free(current);
  68. return;
  69. }
  70. prev->next = current->next;
  71. free(current);
  72. return;
  73. }
  74. prev = current;
  75. current = current->next;
  76. }
  77. }
  78. static bool is_event_type(request_t * req)
  79. {
  80. return req->action == COAP_EVENT;
  81. }
  82. static bool register_url_handler(const char *url,
  83. request_handler_f request_handler, reg_type_t reg_type)
  84. {
  85. res_register_t * r = g_resources;
  86. while (r) {
  87. if (reg_type == r->reg_type && strcmp(r->url, url) == 0) {
  88. r->request_handler = request_handler;
  89. return true;
  90. }
  91. r = r->next;
  92. }
  93. r = (res_register_t *) malloc(sizeof(res_register_t));
  94. if (r == NULL)
  95. return false;
  96. memset(r, 0, sizeof(*r));
  97. r->url = strdup(url);
  98. if (!r->url) {
  99. free(r);
  100. return false;
  101. }
  102. r->request_handler = request_handler;
  103. r->reg_type = reg_type;
  104. r->next = g_resources;
  105. g_resources = r;
  106. // tell app mgr to route this url to me
  107. if (reg_type == Reg_Request)
  108. wasm_register_resource(url);
  109. else
  110. wasm_sub_event(url);
  111. return true;
  112. }
  113. bool api_register_resource_handler(const char *url,
  114. request_handler_f request_handler)
  115. {
  116. return register_url_handler(url, request_handler, Reg_Request);
  117. }
  118. static void transaction_timeout_handler(user_timer_t timer)
  119. {
  120. transaction_t *cur, *expired = NULL;
  121. unsigned int elpased_ms, now = wasm_get_sys_tick_ms();
  122. /*
  123. * Since he transaction list is sorted by expiry time naturally,
  124. * we can easily get all expired transactions.
  125. * */
  126. cur = g_transactions;
  127. while (cur) {
  128. if (now < cur->time)
  129. elpased_ms = now + (0xFFFFFFFF - cur->time) + 1;
  130. else
  131. elpased_ms = now - cur->time;
  132. if (elpased_ms >= TRANSACTION_TIMEOUT_MS) {
  133. g_transactions = cur->next;
  134. cur->next = expired;
  135. expired = cur;
  136. cur = g_transactions;
  137. } else {
  138. break;
  139. }
  140. }
  141. /* call each transaction's handler with response set to NULL */
  142. cur = expired;
  143. while (cur) {
  144. transaction_t *tmp = cur;
  145. cur->handler(NULL, cur->user_data);
  146. cur = cur->next;
  147. free(tmp);
  148. }
  149. /*
  150. * If the transaction list is not empty, restart the timer according
  151. * to the first transaction. Otherwise, stop the timer.
  152. */
  153. if (g_transactions != NULL) {
  154. unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
  155. if (now < g_transactions->time) {
  156. elpased_ms = now + (0xFFFFFFFF - g_transactions->time) + 1;
  157. } else {
  158. elpased_ms = now - g_transactions->time;
  159. }
  160. ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
  161. api_timer_restart(g_trans_timer, ms_to_expiry);
  162. } else {
  163. api_timer_cancel(g_trans_timer);
  164. g_trans_timer = NULL;
  165. }
  166. }
  167. void api_send_request(request_t * request, response_handler_f response_handler,
  168. void * user_data)
  169. {
  170. int size;
  171. char *buffer;
  172. transaction_t *trans;
  173. if ((trans = (transaction_t *) malloc(sizeof(transaction_t))) == NULL) {
  174. printf(
  175. "send request: allocate memory for request transaction failed!\n");
  176. return;
  177. }
  178. memset(trans, 0, sizeof(transaction_t));
  179. trans->handler = response_handler;
  180. trans->mid = request->mid;
  181. trans->time = wasm_get_sys_tick_ms();
  182. trans->user_data = user_data;
  183. if ((buffer = pack_request(request, &size)) == NULL) {
  184. printf("send request: pack request failed!\n");
  185. free(trans);
  186. return;
  187. }
  188. transaction_add(trans);
  189. /* if the trans is the 1st one, start the timer */
  190. if (trans == g_transactions) {
  191. /* assert(g_trans_timer == NULL); */
  192. if (g_trans_timer == NULL) {
  193. g_trans_timer = api_timer_create(TRANSACTION_TIMEOUT_MS,
  194. false,
  195. true, transaction_timeout_handler);
  196. }
  197. }
  198. wasm_post_request(buffer, size);
  199. free_req_resp_packet(buffer);
  200. }
  201. /*
  202. *
  203. * APIs for the native layers to callback for request/response arrived to this app
  204. *
  205. */
  206. void on_response(char * buffer, int size)
  207. {
  208. response_t response[1];
  209. transaction_t *trans;
  210. if (NULL == unpack_response(buffer, size, response)) {
  211. printf("unpack response failed\n");
  212. return;
  213. }
  214. if ((trans = transaction_find(response->mid)) == NULL) {
  215. printf("cannot find the transaction\n");
  216. return;
  217. }
  218. /*
  219. * When the 1st transaction get response:
  220. * 1. If the 2nd trans exist, restart the timer according to its expiry time;
  221. * 2. Otherwise, stop the timer since there is no more transactions;
  222. */
  223. if (trans == g_transactions) {
  224. if (trans->next != NULL) {
  225. unsigned int elpased_ms, ms_to_expiry, now = wasm_get_sys_tick_ms();
  226. if (now < trans->next->time) {
  227. elpased_ms = now + (0xFFFFFFFF - trans->next->time) + 1;
  228. } else {
  229. elpased_ms = now - trans->next->time;
  230. }
  231. ms_to_expiry = TRANSACTION_TIMEOUT_MS - elpased_ms;
  232. api_timer_restart(g_trans_timer, ms_to_expiry);
  233. } else {
  234. api_timer_cancel(g_trans_timer);
  235. g_trans_timer = NULL;
  236. }
  237. }
  238. trans->handler(response, trans->user_data);
  239. transaction_remove(trans);
  240. }
  241. void on_request(char *buffer, int size)
  242. {
  243. request_t request[1];
  244. bool is_event;
  245. res_register_t *r = g_resources;
  246. if (NULL == unpack_request(buffer, size, request)) {
  247. printf("unpack request failed\n");
  248. return;
  249. }
  250. is_event = is_event_type(request);
  251. while (r) {
  252. if ((is_event && r->reg_type == Reg_Event)
  253. || (!is_event && r->reg_type == Reg_Request)) {
  254. if (check_url_start(request->url, strlen(request->url), r->url)
  255. > 0) {
  256. r->request_handler(request);
  257. return;
  258. }
  259. }
  260. r = r->next;
  261. }
  262. printf("on_request: exit. no service handler\n");
  263. }
  264. void api_response_send(response_t *response)
  265. {
  266. int size;
  267. char * buffer = pack_response(response, &size);
  268. if (buffer == NULL)
  269. return;
  270. wasm_response_send(buffer, size);
  271. free_req_resp_packet(buffer);
  272. }
  273. /// event api
  274. bool api_publish_event(const char *url, int fmt, void *payload, int payload_len)
  275. {
  276. int size;
  277. request_t request[1];
  278. init_request(request, (char *)url, COAP_EVENT, fmt, payload, payload_len);
  279. char * buffer = pack_request(request, &size);
  280. if (buffer == NULL)
  281. return false;
  282. wasm_post_request(buffer, size);
  283. free_req_resp_packet(buffer);
  284. return true;
  285. }
  286. bool api_subscribe_event(const char * url, request_handler_f handler)
  287. {
  288. return register_url_handler(url, handler, Reg_Event);
  289. }