app_manager_export.h 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _APP_MANAGER_EXPORT_H_
  6. #define _APP_MANAGER_EXPORT_H_
  7. #include "native_interface.h"
  8. #include "bi-inc/shared_utils.h"
  9. #include "bh_queue.h"
  10. #include "host_link.h"
  11. #include "runtime_timer.h"
  12. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. /* Special module IDs */
  16. #define ID_HOST -3
  17. #define ID_APP_MGR -2
  18. /* Invalid module ID */
  19. #define ID_NONE ((uint32)-1)
  20. struct attr_container;
  21. /* Queue message type */
  22. typedef enum QUEUE_MSG_TYPE {
  23. COAP_PARSED = LINK_MSG_TYPE_MAX + 1,
  24. RESTFUL_REQUEST,
  25. RESTFUL_RESPONSE,
  26. TIMER_EVENT = 5,
  27. SENSOR_EVENT = 6,
  28. GPIO_INTERRUPT_EVENT = 7,
  29. BLE_EVENT = 8,
  30. JDWP_REQUEST = 9,
  31. WD_TIMEOUT = 10,
  32. BASE_EVENT_MAX = 100
  33. } QUEUE_MSG_TYPE;
  34. typedef enum {
  35. Module_Jeff,
  36. Module_WASM_App,
  37. Module_WASM_Lib,
  38. Module_Max
  39. } Module_Type;
  40. struct module_data;
  41. /* Watchdog timer of module */
  42. typedef struct watchdog_timer {
  43. /* Timer handle of the platform */
  44. void *timer_handle;
  45. /* Module of the watchdog timer */
  46. struct module_data *module_data;
  47. /* Lock of the watchdog timer */
  48. korp_mutex lock;
  49. /* Flag indicates module is being interrupted by watchdog */
  50. bool is_interrupting;
  51. /* Flag indicates watchdog timer is stopped */
  52. bool is_stopped;
  53. } watchdog_timer;
  54. typedef struct module_data {
  55. struct module_data *next;
  56. /* ID of the module */
  57. uint32 id;
  58. /* Type of the module */
  59. Module_Type module_type;
  60. /* Heap of the module */
  61. void *heap;
  62. /* Heap size of the module */
  63. int heap_size;
  64. /* Module execution timeout in millisecond */
  65. int timeout;
  66. /* Queue of the module */
  67. bh_queue *queue;
  68. /* Watchdog timer of the module*/
  69. struct watchdog_timer wd_timer;
  70. timer_ctx_t timer_ctx;
  71. /* max timers number app can create */
  72. int timers;
  73. /* Internal data of the module */
  74. void *internal_data;
  75. /* Module name */
  76. char module_name[1];
  77. } module_data;
  78. /* Module function types */
  79. typedef bool (*module_init_func)(void);
  80. typedef bool (*module_install_func)(request_t *msg);
  81. typedef bool (*module_uninstall_func)(request_t *msg);
  82. typedef void (*module_watchdog_kill_func)(module_data *module_data);
  83. typedef bool (*module_handle_host_url_func)(void *queue_msg);
  84. typedef module_data *(*module_get_module_data_func)(void *inst);
  85. /**
  86. * @typedef module_on_install_request_byte_arrive_func
  87. *
  88. * @brief Define the signature of function to handle one byte of
  89. * module app install request for struct module_interface.
  90. *
  91. * @param ch the byte to be received and handled
  92. * @param total_size total size of the request
  93. * @param received_total_size currently received total size when
  94. * the function return
  95. *
  96. * @return true if success, false otherwise
  97. */
  98. typedef bool (*module_on_install_request_byte_arrive_func)(
  99. uint8 ch, int total_size, int *received_total_size);
  100. /* Interfaces of each module */
  101. typedef struct module_interface {
  102. module_init_func module_init;
  103. module_install_func module_install;
  104. module_uninstall_func module_uninstall;
  105. module_watchdog_kill_func module_watchdog_kill;
  106. module_handle_host_url_func module_handle_host_url;
  107. module_get_module_data_func module_get_module_data;
  108. module_on_install_request_byte_arrive_func module_on_install;
  109. } module_interface;
  110. /**
  111. * @typedef host_init_func
  112. * @brief Define the host initialize callback function signature for
  113. * struct host_interface.
  114. *
  115. * @return true if success, false if fail
  116. */
  117. typedef bool (*host_init_func)(void);
  118. /**
  119. * @typedef host_send_fun
  120. * @brief Define the host send callback function signature for
  121. * struct host_interface.
  122. *
  123. * @param buf data buffer to send.
  124. * @param size size of the data to send.
  125. *
  126. * @return size of the data sent in bytes
  127. */
  128. typedef int (*host_send_fun)(void *ctx, const char *buf, int size);
  129. /**
  130. * @typedef host_destroy_fun
  131. * @brief Define the host receive callback function signature for
  132. * struct host_interface.
  133. *
  134. */
  135. typedef void (*host_destroy_fun)();
  136. /* Interfaces of host communication */
  137. typedef struct host_interface {
  138. host_init_func init;
  139. host_send_fun send;
  140. host_destroy_fun destroy;
  141. } host_interface;
  142. /**
  143. * Initialize communication with Host
  144. *
  145. * @param interface host communication interface
  146. *
  147. * @return true if success, false otherwise
  148. */
  149. bool
  150. app_manager_host_init(host_interface *intf);
  151. /* Startup app manager */
  152. void
  153. app_manager_startup(host_interface *intf);
  154. /* Return whether app manager is started */
  155. bool
  156. app_manager_is_started(void);
  157. /* Get queue of current applet */
  158. void *
  159. app_manager_get_module_queue(uint32 module_type, void *module_inst);
  160. /* Get applet name of current applet */
  161. const char *
  162. app_manager_get_module_name(uint32 module_type, void *module_inst);
  163. /* Get heap of current applet */
  164. void *
  165. app_manager_get_module_heap(uint32 module_type, void *module_inst);
  166. void *
  167. get_app_manager_queue();
  168. module_data *
  169. app_manager_get_module_data(uint32 module_type, void *module_inst);
  170. unsigned int
  171. app_manager_get_module_id(uint32 module_type, void *module_inst);
  172. module_data *
  173. app_manager_lookup_module_data(const char *name);
  174. module_data *
  175. module_data_list_lookup(const char *module_name);
  176. module_data *
  177. module_data_list_lookup_id(unsigned int module_id);
  178. void
  179. app_manager_post_applets_update_event();
  180. bool
  181. am_register_resource(const char *url,
  182. void (*request_handler)(request_t *, void *),
  183. uint32 register_id);
  184. void
  185. am_cleanup_registeration(uint32 register_id);
  186. bool
  187. am_register_event(const char *url, uint32_t reg_client);
  188. bool
  189. am_unregister_event(const char *url, uint32_t reg_client);
  190. void
  191. am_publish_event(request_t *event);
  192. void *
  193. am_dispatch_request(request_t *request);
  194. void
  195. am_send_response(response_t *response);
  196. void
  197. module_request_handler(request_t *request, void *user_data);
  198. /**
  199. * Send request message to host
  200. *
  201. * @param msg the request or event message.
  202. * It is event when msg->action==COAP_EVENT
  203. *
  204. * @return true if success, false otherwise
  205. */
  206. bool
  207. send_request_to_host(request_t *msg);
  208. /**
  209. * Send response message to host
  210. *
  211. * @param msg the response message
  212. *
  213. * @return true if success, false otherwise
  214. */
  215. bool
  216. send_response_to_host(response_t *msg);
  217. /**
  218. * Send response with mid and code to host
  219. *
  220. * @param mid the message id of response
  221. * @param code the code/status of response
  222. * @param msg the detailed message
  223. *
  224. * @return true if success, false otherwise
  225. */
  226. bool
  227. send_error_response_to_host(int mid, int code, const char *msg);
  228. /**
  229. * Check whether the applet has the permission
  230. *
  231. * @param perm the permission needed to check
  232. *
  233. * @return true if success, false otherwise
  234. */
  235. bool
  236. bh_applet_check_permission(const char *perm);
  237. /**
  238. * Send message to Host
  239. *
  240. * @param buf buffer to send
  241. * @param size size of buffer
  242. *
  243. * @return size of buffer sent
  244. */
  245. int
  246. app_manager_host_send_msg(int msg_type, const char *buf, int size);
  247. #ifdef __cplusplus
  248. } /* end of extern "C" */
  249. #endif
  250. #endif