host_app_sample.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include <pthread.h>
  8. #include <unistd.h>
  9. #include "host_api.h"
  10. #include "bi-inc/attr_container.h"
  11. #include "er-coap-constants.h"
  12. static char *
  13. read_file_to_buffer(const char *filename, int *ret_size);
  14. int send_request_to_applet_success = 0;
  15. const char *label_for_request = "request1";
  16. int event_listener_counter = 0;
  17. char *applet_buf[1024 * 1024];
  18. const char *host_agent_ip = "127.0.0.1";
  19. void
  20. f_aee_response_handler(void *usr_ctx, aee_response_t *response)
  21. {
  22. if (response == NULL) {
  23. printf("########## request timeout!!! \n");
  24. }
  25. else {
  26. char *str = (char *)usr_ctx;
  27. printf("#### dump response ####\n");
  28. printf("#### user data: %s \n", str);
  29. printf("#### status: %d \n", response->status);
  30. if (response->payload != NULL)
  31. attr_container_dump((attr_container_t *)response->payload);
  32. }
  33. }
  34. void
  35. f_aee_event_listener(const char *url, void *event, int fmt)
  36. {
  37. printf("######## event is received. url: %s, fmt:%d ############\n", url,
  38. fmt);
  39. attr_container_t *attr_obj = (attr_container_t *)event;
  40. attr_container_dump(attr_obj);
  41. /*
  42. if (0 == strcmp(url, "alert/overheat"))
  43. {
  44. event_listener_counter++;
  45. printf("event :%d \n", event_listener_counter);
  46. }
  47. */
  48. }
  49. static int
  50. print_menu_and_select(void)
  51. {
  52. char s[256];
  53. int choice;
  54. do {
  55. printf("\n");
  56. printf("1. Install TestApplet1\n");
  57. printf("2. Install TestApplet2\n");
  58. printf("3. Install TestApplet3\n");
  59. printf("4. Uninstall TestApplet1\n");
  60. printf("5. Uninstall TestApplet2\n");
  61. printf("6. Uninstall TestApplet3\n");
  62. printf("7. Send Request to TestApplet1\n");
  63. printf("8. Register Event to TestApplet1\n");
  64. printf("9. UnRegister Event to TestApplet1\n");
  65. printf("a. Query Applets\n");
  66. printf("t. Auto Test\n");
  67. printf("q. Exit\n");
  68. printf("Please Select: ");
  69. if (fgets(s, sizeof(s), stdin)) {
  70. if (!strncmp(s, "q", 1))
  71. return 0;
  72. if (!strncmp(s, "a", 1))
  73. return 10;
  74. if (!strncmp(s, "t", 1))
  75. return 20;
  76. choice = atoi(s);
  77. if (choice >= 1 && choice <= 9)
  78. return choice;
  79. }
  80. } while (1);
  81. return 0;
  82. }
  83. static void
  84. install_applet(int index)
  85. {
  86. char applet_name[64];
  87. char applet_file_name[64];
  88. char *buf;
  89. int size;
  90. int ret;
  91. printf("Installing TestApplet%d...\n", index);
  92. snprintf(applet_name, sizeof(applet_name), "TestApplet%d", index);
  93. snprintf(applet_file_name, sizeof(applet_file_name), "./TestApplet%d.wasm",
  94. index);
  95. buf = read_file_to_buffer(applet_file_name, &size);
  96. if (!buf) {
  97. printf("Install Applet failed: read file %s error.\n",
  98. applet_file_name);
  99. return;
  100. }
  101. // step2. install applet
  102. ret = aee_applet_install(buf, "wasm", size, applet_name, 5000);
  103. if (ret) {
  104. printf("%s install success\n", applet_name);
  105. }
  106. free(buf);
  107. }
  108. static void
  109. uninstall_applet(int index)
  110. {
  111. int ret;
  112. char applet_name[64];
  113. snprintf(applet_name, sizeof(applet_name), "TestApplet%d", index);
  114. ret = aee_applet_uninstall(applet_name, "wasm", 5000);
  115. if (ret) {
  116. printf("uninstall %s success\n", applet_name);
  117. }
  118. else {
  119. printf("uninstall %s failed\n", applet_name);
  120. }
  121. }
  122. static void
  123. send_request(int index)
  124. {
  125. char url[64];
  126. int ret;
  127. aee_request_t req;
  128. const char *user_context = "label for request";
  129. attr_container_t *attr_obj =
  130. attr_container_create("Send Request to Applet");
  131. attr_container_set_string(&attr_obj, "String key", "Hello");
  132. attr_container_set_int(&attr_obj, "Int key", 1000);
  133. attr_container_set_int64(&attr_obj, "Int64 key", 0x77BBCCDD11223344LL);
  134. // specify the target wasm app
  135. snprintf(url, sizeof(url), "/app/TestApplet%d/url1", index);
  136. // not specify the target wasm app
  137. // snprintf(url, sizeof(url), "url1");
  138. aee_request_init(&req, url, COAP_PUT);
  139. aee_request_set_payload(&req, attr_obj,
  140. attr_container_get_serialize_length(attr_obj),
  141. PAYLOAD_FORMAT_ATTRIBUTE_OBJECT);
  142. ret = aee_request_send(&req, f_aee_response_handler, (void *)user_context,
  143. 10000);
  144. if (ret) {
  145. printf("send request to TestApplet1 success\n");
  146. }
  147. }
  148. static void
  149. register_event(const char *event_path)
  150. {
  151. hostclient_register_event(event_path, f_aee_event_listener);
  152. }
  153. static void
  154. unregister_event(const char *event_path)
  155. {
  156. hostclient_unregister_event(event_path);
  157. }
  158. static void
  159. query_applets()
  160. {
  161. aee_applet_list_t applet_lst;
  162. aee_applet_list_init(&applet_lst);
  163. aee_applet_list(5000, &applet_lst);
  164. aee_applet_list_clean(&applet_lst);
  165. }
  166. static char *
  167. read_file_to_buffer(const char *filename, int *ret_size)
  168. {
  169. FILE *fl = NULL;
  170. char *buffer = NULL;
  171. int file_size = 0;
  172. if (!(fl = fopen(filename, "rb"))) {
  173. printf("file open failed\n");
  174. return NULL;
  175. }
  176. fseek(fl, 0, SEEK_END);
  177. file_size = ftell(fl);
  178. if (file_size == 0) {
  179. printf("file length 0\n");
  180. return NULL;
  181. }
  182. if (!(buffer = (char *)malloc(file_size))) {
  183. fclose(fl);
  184. return NULL;
  185. }
  186. fseek(fl, 0, SEEK_SET);
  187. if (!fread(buffer, 1, file_size, fl)) {
  188. printf("file read failed\n");
  189. return NULL;
  190. }
  191. fclose(fl);
  192. *ret_size = file_size;
  193. return buffer;
  194. }
  195. static void
  196. auto_test()
  197. {
  198. int i;
  199. int interval = 1000; /* ms */
  200. while (1) {
  201. uninstall_applet(1);
  202. uninstall_applet(2);
  203. uninstall_applet(3);
  204. install_applet(1);
  205. install_applet(2);
  206. install_applet(3);
  207. for (i = 0; i < 60 * 1000 / interval; i++) {
  208. query_applets();
  209. send_request(1);
  210. send_request(2);
  211. send_request(3);
  212. usleep(interval * 1000);
  213. }
  214. }
  215. }
  216. void
  217. exit_program()
  218. {
  219. hostclient_shutdown();
  220. exit(0);
  221. }
  222. int
  223. main()
  224. {
  225. bool ret;
  226. // step1. host client init
  227. ret = hostclient_initialize(host_agent_ip, 3456);
  228. if (!ret) {
  229. printf("host client initialize failed\n");
  230. return -1;
  231. }
  232. do {
  233. int choice = print_menu_and_select();
  234. printf("\n");
  235. if (choice == 0)
  236. exit_program();
  237. if (choice <= 3)
  238. install_applet(choice);
  239. else if (choice <= 6)
  240. uninstall_applet(choice - 3);
  241. else if (choice <= 7)
  242. send_request(1);
  243. else if (choice <= 8)
  244. register_event("alert/overheat");
  245. else if (choice <= 9)
  246. unregister_event("alert/overheat");
  247. else if (choice == 10)
  248. query_applets();
  249. else if (choice == 20)
  250. auto_test();
  251. } while (1);
  252. return 0;
  253. }
  254. // Run program: Ctrl + F5 or Debug > Start Without Debugging menu
  255. // Debug program: F5 or Debug > Start Debugging menu
  256. // Tips for Getting Started:
  257. // 1. Use the Solution Explorer window to add/manage files
  258. // 2. Use the Team Explorer window to connect to source control
  259. // 3. Use the Output window to see build output and other messages
  260. // 4. Use the Error List window to view errors
  261. // 5. Go to Project > Add New Item to create new code files, or
  262. // Project > Add Existing Item to add existing code files to the project
  263. // 6. In the future, to open this project again, go to File > Open > Project
  264. // and select the .sln file