module_wasm_app.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #ifndef _MODULE_WASM_APP_H_
  6. #define _MODULE_WASM_APP_H_
  7. #include "bh_queue.h"
  8. #include "app_manager_export.h"
  9. #include "wasm_export.h"
  10. #ifdef __cplusplus
  11. extern "C" {
  12. #endif
  13. #define SECTION_TYPE_USER 0
  14. #define SECTION_TYPE_TYPE 1
  15. #define SECTION_TYPE_IMPORT 2
  16. #define SECTION_TYPE_FUNC 3
  17. #define SECTION_TYPE_TABLE 4
  18. #define SECTION_TYPE_MEMORY 5
  19. #define SECTION_TYPE_GLOBAL 6
  20. #define SECTION_TYPE_EXPORT 7
  21. #define SECTION_TYPE_START 8
  22. #define SECTION_TYPE_ELEM 9
  23. #define SECTION_TYPE_CODE 10
  24. #define SECTION_TYPE_DATA 11
  25. typedef enum AOTSectionType {
  26. AOT_SECTION_TYPE_TARGET_INFO = 0,
  27. AOT_SECTION_TYPE_INIT_DATA = 1,
  28. AOT_SECTION_TYPE_TEXT = 2,
  29. AOT_SECTION_TYPE_FUNCTION = 3,
  30. AOT_SECTION_TYPE_EXPORT = 4,
  31. AOT_SECTION_TYPE_RELOCATION = 5,
  32. AOT_SECTION_TYPE_SIGANATURE = 6,
  33. AOT_SECTION_TYPE_CUSTOM = 100,
  34. } AOTSectionType;
  35. enum {
  36. WASM_Msg_Start = BASE_EVENT_MAX,
  37. TIMER_EVENT_WASM,
  38. SENSOR_EVENT_WASM,
  39. CONNECTION_EVENT_WASM,
  40. WIDGET_EVENT_WASM,
  41. WASM_Msg_End = WASM_Msg_Start + 100
  42. };
  43. typedef struct wasm_data {
  44. /* for easily access the containing wasm module */
  45. wasm_module_t wasm_module;
  46. wasm_module_inst_t wasm_module_inst;
  47. /* Permissions of the WASM app */
  48. char *perms;
  49. /* thread list mapped with this WASM module */
  50. korp_tid thread_id;
  51. /* for easily access the containing module data */
  52. module_data *m_data;
  53. /* is bytecode or aot */
  54. bool is_bytecode;
  55. /* sections of wasm bytecode or aot file */
  56. void *sections;
  57. /* execution environment */
  58. wasm_exec_env_t exec_env;
  59. } wasm_data;
  60. /* sensor event */
  61. typedef struct _sensor_event_data {
  62. uint32 sensor_id;
  63. int data_fmt;
  64. /* event of attribute container from context core */
  65. void *data;
  66. } sensor_event_data_t;
  67. /* WASM Bytecode File */
  68. typedef struct wasm_bytecode_file {
  69. /* magics */
  70. int magic;
  71. /* current version */
  72. int version;
  73. /* WASM section list */
  74. wasm_section_list_t sections;
  75. /* Last WASM section in the list */
  76. wasm_section_t *section_end;
  77. } wasm_bytecode_file_t;
  78. /* WASM AOT File */
  79. typedef struct wasm_aot_file {
  80. /* magics */
  81. int magic;
  82. /* current version */
  83. int version;
  84. /* AOT section list */
  85. aot_section_list_t sections;
  86. /* Last AOT section in the list */
  87. aot_section_t *section_end;
  88. } wasm_aot_file_t;
  89. /* WASM App File */
  90. typedef struct wasm_app_file_t {
  91. union {
  92. wasm_bytecode_file_t bytecode;
  93. wasm_aot_file_t aot;
  94. } u;
  95. } wasm_app_file_t;
  96. extern module_interface wasm_app_module_interface;
  97. typedef void (*message_type_handler_t)(module_data *m_data, bh_message_t msg);
  98. extern bool
  99. wasm_register_msg_callback(int msg_type,
  100. message_type_handler_t message_handler);
  101. typedef void (*resource_cleanup_handler_t)(uint32 module_id);
  102. extern bool
  103. wasm_register_cleanup_callback(resource_cleanup_handler_t handler);
  104. /**
  105. * Set WASI root dir for modules. On each wasm app installation, a sub dir named
  106. * with the app's name will be created autamically. That wasm app can only
  107. * access this sub dir.
  108. *
  109. * @param root_dir the root dir to set
  110. * @return true for success, false otherwise
  111. */
  112. bool
  113. wasm_set_wasi_root_dir(const char *root_dir);
  114. /**
  115. * Get WASI root dir
  116. *
  117. * @return the WASI root dir
  118. */
  119. const char *
  120. wasm_get_wasi_root_dir();
  121. #ifdef __cplusplus
  122. } /* end of extern "C" */
  123. #endif
  124. #endif /* _MODULE_WASM_APP_H_ */