yloop.h 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  1. /*
  2. * Copyright (C) 2019-2020 Alibaba Group Holding Limited
  3. */
  4. #ifndef AOS_YLOOP_H
  5. #define AOS_YLOOP_H
  6. #ifdef __cplusplus
  7. extern "C" {
  8. #endif
  9. #include <stdint.h>
  10. /** @defgroup Framework API
  11. * @{
  12. */
  13. #ifndef DOXYGEN_MODE
  14. /*General value define */
  15. #define VALUE_FAIL 0
  16. #define VALUE_SUCCESS 1
  17. #define VALUE_NULL 0xffffffff
  18. #define EV_NULL 0xffff
  19. #define CODE_NULL 0xffff
  20. /* special event filter */
  21. #define EV_ALL 0
  22. /** system event */
  23. #define EV_SYS 0x0001
  24. #define CODE_SYS_LOOP_EXIT 0
  25. #define CODE_SYS_ON_READY 1
  26. /** WiFi event */
  27. #define EV_NET 0x0002
  28. #define CODE_NET_CMD_RECONNECT 1
  29. #define CODE_NET_CMD_INIT 2
  30. #define CODE_NET_CMD_RESET 3
  31. #define CODE_NET_ON_CONNECTED 4
  32. #define CODE_NET_ON_DISCONNECT 5
  33. #define CODE_NET_ON_GOT_IP 6
  34. #define CODE_NET_ON_SHOUDOWN 7
  35. #define VALUE_NET_ETH 1
  36. #define VALUE_NET_WIFI 2
  37. #define VALUE_NET_NBIOT 3
  38. /** remote procedure call */
  39. #define EV_RPC 0x0100
  40. /** YunIO event */
  41. #define EV_YIO 0x0101
  42. #define CODE_YIO_ON_CONNECTED 4
  43. #define CODE_YIO_ON_DISCONNECTED 5
  44. #define CODE_YIO_ON_CONNECT_FAILED 7
  45. #define CODE_YIO_ON_RECV_FAILED 8
  46. #define CODE_YIO_ON_RECV_SUCCESS 9
  47. #define CODE_YIO_ON_SEND_FAILED 10
  48. #define CODE_YIO_ON_SEND_SUCCESS 11
  49. #define CODE_YIO_ON_HEARTBIT_ERROR 12
  50. #define CODE_YIO_ON_SLEEP 13
  51. /** FOTA event */
  52. #define EV_FOTA 0x0102
  53. #define CODE_FOTA_START 1
  54. #define CODE_FOTA_END 2
  55. #define VALUE_FOTA_UPDATED 0
  56. /** Combo event */
  57. #define EV_BZ_COMBO 0x0106
  58. #define CODE_COMBO_AP_INFO_READY 1
  59. /** SELF RECOVER event */
  60. #define EV_RCV 0x0200
  61. /** user app start */
  62. #define EV_USER 0x1000
  63. #endif
  64. /**
  65. * @struct input_event_t
  66. * @brief yos event structure
  67. */
  68. typedef struct {
  69. /** The time event is generated, auto filled by yos event system */
  70. uint32_t time;
  71. /** Event type, value < 0x1000 are used by yos system */
  72. uint16_t type;
  73. /** Defined according to type */
  74. uint16_t code;
  75. /** Defined according to type/code */
  76. unsigned long value;
  77. /** Defined according to type/code */
  78. unsigned long extra;
  79. } input_event_t;
  80. /** Event callback */
  81. typedef void (*aos_event_cb)(input_event_t *event, void *private_data);
  82. /** Delayed execution callback */
  83. typedef void (*aos_call_t)(void *arg);
  84. typedef void *aos_loop_t;
  85. //typedef void (*aos_poll_call_t)(int fd, void *arg);
  86. /**
  87. * Register system event filter callback.
  88. *
  89. * @param[in] type event type interested.
  90. * @param[in] cb system event callback.
  91. * @param[in] priv private data past to cb.
  92. *
  93. * @return the operation status, 0 is OK, others is error.
  94. */
  95. int aos_register_event_filter(uint16_t type, aos_event_cb cb, void *priv);
  96. /**
  97. * Unregister native event callback.
  98. *
  99. * @param[in] type event type interested.
  100. * @param[in] cb system event callback.
  101. * @param[in] priv private data past to cb.
  102. *
  103. * @return the operation status, 0 is OK, others is error.
  104. */
  105. int aos_unregister_event_filter(uint16_t type, aos_event_cb cb, void *priv);
  106. /**
  107. * Post local event.
  108. *
  109. * @param[in] type event type.
  110. * @param[in] code event code.
  111. * @param[in] value event value.
  112. *
  113. * @return the operation status, 0 is OK,others is error.
  114. */
  115. int aos_post_event(uint16_t type, uint16_t code, unsigned long value);
  116. /**
  117. * Register a poll event in main loop.
  118. *
  119. * @param[in] fd poll fd.
  120. * @param[in] action action to be executed.
  121. * @param[in] param private data past to action.
  122. *
  123. * @return the operation status, 0 is OK,others is error.
  124. */
  125. /* not support */
  126. //int aos_poll_read_fd(int fd, aos_poll_call_t action, void *param);
  127. /**
  128. * Cancel a poll event to be executed in main loop.
  129. *
  130. * @param[in] fd poll fd.
  131. * @param[in] action action to be executed.
  132. * @param[in] param private data past to action.
  133. */
  134. /* not support */
  135. //void aos_cancel_poll_read_fd(int fd, aos_poll_call_t action, void *param);
  136. /**
  137. * Post a delayed action to be executed in main loop.
  138. *
  139. * @param[in] ms milliseconds to wait.
  140. * @param[in] action action to be executed.
  141. * @param[in] arg private data past to action.
  142. *
  143. * @return the operation status, 0 is OK,others is error.
  144. */
  145. int aos_post_delayed_action(int ms, aos_call_t action, void *arg);
  146. /**
  147. * Cancel a delayed action to be executed in main loop.
  148. *
  149. * @param[in] ms milliseconds to wait, -1 means don't care.
  150. * @param[in] action action to be executed.
  151. * @param[in] arg private data past to action.
  152. */
  153. void aos_cancel_delayed_action(int ms, aos_call_t action, void *arg);
  154. /**
  155. * Schedule a callback in next event loop.
  156. * Unlike aos_post_delayed_action,
  157. * this function can be called from non-aos-main-loop context.
  158. * @param[in] action action to be executed.
  159. * @param[in] arg private data past to action.
  160. *
  161. * @return the operation status, <0 is error,others is OK.
  162. */
  163. int aos_schedule_call(aos_call_t action, void *arg);
  164. /**
  165. * Init a per-task event loop.
  166. *
  167. * @return the handler of aos_loop_t,NULL failure,others success.
  168. */
  169. aos_loop_t aos_loop_init(void);
  170. /**
  171. * Get current event loop.
  172. *
  173. * @return default event loop.
  174. */
  175. aos_loop_t aos_current_loop(void);
  176. /**
  177. * Start event loop.
  178. */
  179. void aos_loop_run(void);
  180. /**
  181. * Exit event loop, aos_loop_run() will return.
  182. */
  183. void aos_loop_exit(void);
  184. /**
  185. * Free event loop resources.
  186. */
  187. void aos_loop_destroy(void);
  188. /**
  189. * Schedule a callback specified event loop.
  190. *
  191. * @param[in] loop event loop to be scheduled, NULL for default main loop.
  192. * @param[in] action action to be executed.
  193. * @param[in] arg private data past to action.
  194. *
  195. * @return the operation status, <0 is error,others is OK.
  196. */
  197. /* not support */
  198. //int aos_loop_schedule_call(aos_loop_t *loop, aos_call_t action, void *arg);
  199. /**
  200. * Schedule a work to be executed in workqueue.
  201. *
  202. * @param[in] ms milliseconds to delay before execution, 0 means immediately.
  203. * @param[in] action action to be executed.
  204. * @param[in] arg1 private data past to action.
  205. * @param[in] fini_cb finish callback to be executed after action is done in current event loop.
  206. * @param[in] arg2 data past to fini_cb.
  207. *
  208. * @return work handle,NULL failure,others is OK.
  209. */
  210. void *aos_loop_schedule_work(int ms, aos_call_t action, void *arg1,
  211. aos_call_t fini_cb, void *arg2);
  212. /**
  213. * Cancel a work.
  214. *
  215. * @param[in] work work to be cancelled.
  216. * @param[in] action action to be executed.
  217. * @param[in] arg1 private data past to action.
  218. */
  219. void aos_cancel_work(void *work, aos_call_t action, void *arg1);
  220. #ifdef __cplusplus
  221. }
  222. #endif
  223. #endif /* AOS_YLOOP_H */