app_manager.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include "app_manager.h"
  6. #include "app_manager_host.h"
  7. #include "bh_platform.h"
  8. #include "bi-inc/attr_container.h"
  9. #include "event.h"
  10. #include "watchdog.h"
  11. #include "coap_ext.h"
  12. /* Queue of app manager */
  13. static bh_queue *g_app_mgr_queue;
  14. static bool g_app_mgr_started;
  15. void *
  16. get_app_manager_queue()
  17. {
  18. return g_app_mgr_queue;
  19. }
  20. void
  21. app_manager_post_applets_update_event()
  22. {
  23. module_data *m_data;
  24. attr_container_t *attr_cont;
  25. request_t msg;
  26. int num = 0, i = 0;
  27. char *url = "/applets";
  28. if (!event_is_registered(url))
  29. return;
  30. if (!(attr_cont = attr_container_create("All Applets"))) {
  31. app_manager_printf("Post applets update event failed: "
  32. "allocate memory failed.");
  33. return;
  34. }
  35. os_mutex_lock(&module_data_list_lock);
  36. m_data = module_data_list;
  37. while (m_data) {
  38. num++;
  39. m_data = m_data->next;
  40. }
  41. if (!(attr_container_set_int(&attr_cont, "num", num))) {
  42. app_manager_printf("Post applets update event failed: "
  43. "set attr container key failed.");
  44. goto fail;
  45. }
  46. m_data = module_data_list;
  47. while (m_data) {
  48. char buf[32];
  49. i++;
  50. snprintf(buf, sizeof(buf), "%s%d", "applet", i);
  51. if (!(attr_container_set_string(&attr_cont, buf,
  52. m_data->module_name))) {
  53. app_manager_printf("Post applets update event failed: "
  54. "set attr applet name key failed.");
  55. goto fail;
  56. }
  57. snprintf(buf, sizeof(buf), "%s%d", "heap", i);
  58. if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
  59. app_manager_printf("Post applets update event failed: "
  60. "set attr heap key failed.");
  61. goto fail;
  62. }
  63. m_data = m_data->next;
  64. }
  65. memset(&msg, 0, sizeof(msg));
  66. msg.url = url;
  67. msg.action = COAP_EVENT;
  68. msg.payload = (char *)attr_cont;
  69. send_request_to_host(&msg);
  70. app_manager_printf("Post applets update event success!\n");
  71. attr_container_dump(attr_cont);
  72. fail:
  73. os_mutex_unlock(&module_data_list_lock);
  74. attr_container_destroy(attr_cont);
  75. }
  76. static int
  77. get_applets_count()
  78. {
  79. module_data *m_data;
  80. int num = 0;
  81. os_mutex_lock(&module_data_list_lock);
  82. m_data = module_data_list;
  83. while (m_data) {
  84. num++;
  85. m_data = m_data->next;
  86. }
  87. os_mutex_unlock(&module_data_list_lock);
  88. return num;
  89. }
  90. /* Query fw apps info if name = NULL, otherwise query specify app */
  91. static bool
  92. app_manager_query_applets(request_t *msg, const char *name)
  93. {
  94. module_data *m_data;
  95. attr_container_t *attr_cont;
  96. int num = 0, i = 0, len;
  97. bool ret = false, found = false;
  98. response_t response[1] = { 0 };
  99. attr_cont = attr_container_create("Applets Info");
  100. if (!attr_cont) {
  101. SEND_ERR_RESPONSE(msg->mid,
  102. "Query Applets failed: allocate memory failed.");
  103. return false;
  104. }
  105. os_mutex_lock(&module_data_list_lock);
  106. m_data = module_data_list;
  107. while (m_data) {
  108. num++;
  109. m_data = m_data->next;
  110. }
  111. if (name == NULL && !(attr_container_set_int(&attr_cont, "num", num))) {
  112. SEND_ERR_RESPONSE(
  113. msg->mid, "Query Applets failed: set attr container key failed.");
  114. goto fail;
  115. }
  116. m_data = module_data_list;
  117. while (m_data) {
  118. char buf[32];
  119. if (name == NULL) {
  120. i++;
  121. snprintf(buf, sizeof(buf), "%s%d", "applet", i);
  122. if (!(attr_container_set_string(&attr_cont, buf,
  123. m_data->module_name))) {
  124. SEND_ERR_RESPONSE(msg->mid, "Query Applets failed: "
  125. "set attr container key failed.");
  126. goto fail;
  127. }
  128. snprintf(buf, sizeof(buf), "%s%d", "heap", i);
  129. if (!(attr_container_set_int(&attr_cont, buf, m_data->heap_size))) {
  130. SEND_ERR_RESPONSE(msg->mid,
  131. "Query Applets failed: "
  132. "set attr container heap key failed.");
  133. goto fail;
  134. }
  135. }
  136. else if (!strcmp(name, m_data->module_name)) {
  137. found = true;
  138. if (!(attr_container_set_string(&attr_cont, "name",
  139. m_data->module_name))) {
  140. SEND_ERR_RESPONSE(msg->mid, "Query Applet failed: "
  141. "set attr container key failed.");
  142. goto fail;
  143. }
  144. if (!(attr_container_set_int(&attr_cont, "heap",
  145. m_data->heap_size))) {
  146. SEND_ERR_RESPONSE(msg->mid,
  147. "Query Applet failed: "
  148. "set attr container heap key failed.");
  149. goto fail;
  150. }
  151. }
  152. m_data = m_data->next;
  153. }
  154. if (name != NULL && !found) {
  155. SEND_ERR_RESPONSE(msg->mid,
  156. "Query Applet failed: the app is not found.");
  157. goto fail;
  158. }
  159. len = attr_container_get_serialize_length(attr_cont);
  160. make_response_for_request(msg, response);
  161. set_response(response, CONTENT_2_05, FMT_ATTR_CONTAINER, (char *)attr_cont,
  162. len);
  163. send_response_to_host(response);
  164. ret = true;
  165. app_manager_printf("Query Applets success!\n");
  166. attr_container_dump(attr_cont);
  167. fail:
  168. os_mutex_unlock(&module_data_list_lock);
  169. attr_container_destroy(attr_cont);
  170. return ret;
  171. }
  172. void
  173. applet_mgt_reqeust_handler(request_t *request, void *unused)
  174. {
  175. bh_message_t msg;
  176. /* deep copy, but not use app self heap, but use global heap */
  177. request_t *req = clone_request(request);
  178. if (!req)
  179. return;
  180. msg = bh_new_msg(RESTFUL_REQUEST, req, sizeof(*req), request_cleaner);
  181. if (!msg) {
  182. request_cleaner(req);
  183. return;
  184. }
  185. bh_post_msg2(get_app_manager_queue(), msg);
  186. }
  187. /* return -1 for error */
  188. static int
  189. get_module_type(char *kv_str)
  190. {
  191. int module_type = -1;
  192. char type_str[16] = { 0 };
  193. find_key_value(kv_str, strlen(kv_str), "type", type_str,
  194. sizeof(type_str) - 1, '&');
  195. if (strlen(type_str) == 0)
  196. module_type = Module_WASM_App;
  197. else if (strcmp(type_str, "jeff") == 0)
  198. module_type = Module_Jeff;
  199. else if (strcmp(type_str, "wasm") == 0)
  200. module_type = Module_WASM_App;
  201. else if (strcmp(type_str, "wasmlib") == 0)
  202. module_type = Module_WASM_Lib;
  203. return module_type;
  204. }
  205. #define APP_NAME_MAX_LEN 128
  206. /* Queue callback of App Manager */
  207. static void
  208. app_manager_queue_callback(void *message, void *arg)
  209. {
  210. request_t *request = (request_t *)bh_message_payload((bh_message_t)message);
  211. int mid = request->mid, module_type, offset;
  212. (void)arg;
  213. if ((offset =
  214. check_url_start(request->url, strlen(request->url), "/applet"))
  215. > 0) {
  216. module_type = get_module_type(request->url + offset);
  217. if (module_type == -1) {
  218. SEND_ERR_RESPONSE(mid,
  219. "Applet Management failed: invalid module type.");
  220. goto fail;
  221. }
  222. /* Install Applet */
  223. if (request->action == COAP_PUT) {
  224. if (get_applets_count() >= MAX_APP_INSTALLATIONS) {
  225. SEND_ERR_RESPONSE(
  226. mid,
  227. "Install Applet failed: exceed max app installations.");
  228. goto fail;
  229. }
  230. if (!request->payload) {
  231. SEND_ERR_RESPONSE(mid,
  232. "Install Applet failed: invalid payload.");
  233. goto fail;
  234. }
  235. if (g_module_interfaces[module_type]
  236. && g_module_interfaces[module_type]->module_install) {
  237. if (!g_module_interfaces[module_type]->module_install(request))
  238. goto fail;
  239. }
  240. }
  241. /* Uninstall Applet */
  242. else if (request->action == COAP_DELETE) {
  243. module_type = get_module_type(request->url + offset);
  244. if (module_type == -1) {
  245. SEND_ERR_RESPONSE(
  246. mid, "Uninstall Applet failed: invalid module type.");
  247. goto fail;
  248. }
  249. if (g_module_interfaces[module_type]
  250. && g_module_interfaces[module_type]->module_uninstall) {
  251. if (!g_module_interfaces[module_type]->module_uninstall(
  252. request))
  253. goto fail;
  254. }
  255. }
  256. /* Query Applets installed */
  257. else if (request->action == COAP_GET) {
  258. char name[APP_NAME_MAX_LEN] = { 0 };
  259. char *properties = request->url + offset;
  260. find_key_value(properties, strlen(properties), "name", name,
  261. sizeof(name) - 1, '&');
  262. if (strlen(name) > 0)
  263. app_manager_query_applets(request, name);
  264. else
  265. app_manager_query_applets(request, NULL);
  266. }
  267. else {
  268. SEND_ERR_RESPONSE(mid, "Invalid request of applet: invalid action");
  269. }
  270. }
  271. /* Event Register/Unregister */
  272. else if ((offset = check_url_start(request->url, strlen(request->url),
  273. "/event/"))
  274. > 0) {
  275. char url_buf[256] = { 0 };
  276. strncpy(url_buf, request->url + offset, sizeof(url_buf) - 1);
  277. if (!event_handle_event_request(request->action, url_buf, ID_HOST)) {
  278. SEND_ERR_RESPONSE(mid, "Handle event request failed.");
  279. goto fail;
  280. }
  281. send_error_response_to_host(mid, CONTENT_2_05, NULL); /* OK */
  282. }
  283. else {
  284. int i;
  285. for (i = 0; i < Module_Max; i++) {
  286. if (g_module_interfaces[i]
  287. && g_module_interfaces[i]->module_handle_host_url) {
  288. if (g_module_interfaces[i]->module_handle_host_url(request))
  289. break;
  290. }
  291. }
  292. }
  293. fail:
  294. return;
  295. }
  296. static void
  297. module_interfaces_init()
  298. {
  299. int i;
  300. for (i = 0; i < Module_Max; i++) {
  301. if (g_module_interfaces[i] && g_module_interfaces[i]->module_init)
  302. g_module_interfaces[i]->module_init();
  303. }
  304. }
  305. void
  306. app_manager_startup(host_interface *interface)
  307. {
  308. module_interfaces_init();
  309. /* Create queue of App Manager */
  310. g_app_mgr_queue = bh_queue_create();
  311. if (!g_app_mgr_queue)
  312. return;
  313. if (!module_data_list_init())
  314. goto fail1;
  315. if (!watchdog_startup())
  316. goto fail2;
  317. /* Initialize Host */
  318. app_manager_host_init(interface);
  319. am_register_resource("/app/", targeted_app_request_handler, ID_APP_MGR);
  320. /* /app/ and /event/ are both processed by applet_mgt_reqeust_handler */
  321. am_register_resource("/applet", applet_mgt_reqeust_handler, ID_APP_MGR);
  322. am_register_resource("/event/", applet_mgt_reqeust_handler, ID_APP_MGR);
  323. app_manager_printf("App Manager started.\n");
  324. g_app_mgr_started = true;
  325. /* Enter loop run */
  326. bh_queue_enter_loop_run(g_app_mgr_queue, app_manager_queue_callback, NULL);
  327. g_app_mgr_started = false;
  328. /* Destroy registered resources */
  329. am_cleanup_registeration(ID_APP_MGR);
  330. /* Destroy watchdog */
  331. watchdog_destroy();
  332. fail2:
  333. module_data_list_destroy();
  334. fail1:
  335. bh_queue_destroy(g_app_mgr_queue);
  336. }
  337. bool
  338. app_manager_is_started(void)
  339. {
  340. return g_app_mgr_started;
  341. }
  342. #include "module_config.h"
  343. module_interface *g_module_interfaces[Module_Max] = {
  344. #if ENABLE_MODULE_JEFF != 0
  345. &jeff_module_interface,
  346. #else
  347. NULL,
  348. #endif
  349. #if ENABLE_MODULE_WASM_APP != 0
  350. &wasm_app_module_interface,
  351. #else
  352. NULL,
  353. #endif
  354. #if ENABLE_MODULE_WASM_LIB != 0
  355. &wasm_lib_module_interface
  356. #else
  357. NULL
  358. #endif
  359. };