linkkit_export.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. #ifndef LINKKIT_EXPORT_H
  2. #define LINKKIT_EXPORT_H
  3. #ifdef __cplusplus
  4. extern "C" {
  5. #endif /* __cplusplus */
  6. #include "iot_export_fota.h"
  7. #include "iot_export_dm.h"
  8. #include "lite_queue.h"
  9. #ifdef SERVICE_OTA_ENABLED
  10. #include "iot_export_fota.h"
  11. #include "iot_export_cota.h"
  12. #endif /* SERVICE_OTA_ENABLED */
  13. typedef struct _linkkit_ops {
  14. int (*on_connect)(void *ctx);
  15. int (*on_disconnect)(void *ctx);
  16. int (*raw_data_arrived)(void *thing_id, void *data, int len, void *ctx);
  17. int (*thing_create)(void *thing_id, void *ctx);
  18. int (*thing_enable)(void *thing_id, void *ctx);
  19. int (*thing_disable)(void *thing_id, void *ctx);
  20. #ifdef RRPC_ENABLED
  21. int (*thing_call_service)(void *thing_id, char *service, int request_id, int rrpc, void *ctx);
  22. #else
  23. int (*thing_call_service)(void *thing_id, char *service, int request_id, void *ctx);
  24. #endif /* RRPC_ENABLED */
  25. int (*thing_prop_changed)(void *thing_id, char *property, void *ctx);
  26. } linkkit_ops_t;
  27. typedef enum _linkkit_loglevel {
  28. linkkit_loglevel_emerg = 0,
  29. linkkit_loglevel_crit,
  30. linkkit_loglevel_error,
  31. linkkit_loglevel_warning,
  32. linkkit_loglevel_info,
  33. linkkit_loglevel_debug,
  34. } linkkit_loglevel_t;
  35. /* domain type */
  36. /* please sync with dm_cloud_domain_type_t */
  37. typedef enum {
  38. /* iot-as-mqtt.cn-shanghai.aliyuncs.com */
  39. linkkit_cloud_domain_sh,
  40. /* USA */
  41. linkkit_cloud_domain_usa,
  42. linkkit_cloud_domain_max,
  43. } linkkit_cloud_domain_type_t;
  44. /* device info related operation */
  45. typedef enum {
  46. linkkit_deviceinfo_operate_update,
  47. linkkit_deviceinfo_operate_delete,
  48. linkkit_deviceinfo_operate_max,
  49. } linkkit_deviceinfo_operate_t;
  50. /**
  51. * @brief dispatch message of queue for further process.
  52. *
  53. * @return int, 0 when success, -1 when fail.
  54. */
  55. int linkkit_dispatch(void);
  56. /**
  57. * @brief start linkkit routines, and install callback funstions(async type for cloud connecting).
  58. *
  59. * @param max_buffered_msg, specify max buffered message size.
  60. * @param ops, callback function struct to be installed.
  61. * @param get_tsl_from_cloud, config if device need to get tsl from cloud(!0) or local(0), if local selected, must invoke linkkit_set_tsl to tell tsl to dm after start complete.
  62. * @param log_level, config log level.
  63. * @param user_context, user context pointer.
  64. * @param domain_type, specify the could server domain.
  65. *
  66. * @return int, 0 when success, -1 when fail.
  67. */
  68. int linkkit_start(int max_buffered_msg, int get_tsl_from_cloud, linkkit_loglevel_t log_level, linkkit_ops_t *ops, linkkit_cloud_domain_type_t domain_type, void *user_context);
  69. #ifdef SERVICE_OTA_ENABLED
  70. /**
  71. * @brief init fota service routines, and install callback funstions.
  72. *
  73. * @param callback_fp, callback function to be installed.
  74. *
  75. * @return int, 0 when success, -1 when fail.
  76. */
  77. int linkkit_fota_init(handle_service_fota_callback_fp_t callback_fp);
  78. #ifdef SERVICE_COTA_ENABLED
  79. int linkkit_cota_init(handle_service_cota_callback_fp_t callback_fp);
  80. #endif /**< SERVICE_COTA_ENABLED*/
  81. #endif /* SERVICE_OTA_ENABLED */
  82. /**
  83. * @brief stop linkkit routines.
  84. *
  85. *
  86. * @return 0 when success, -1 when fail.
  87. */
  88. int linkkit_end();
  89. /**
  90. * @brief install user tsl.
  91. *
  92. * @param tsl, tsl string that contains json description for thing object.
  93. * @param tsl_len, tsl string length.
  94. *
  95. * @return pointer to thing object, NULL when fails.
  96. */
  97. extern void* linkkit_set_tsl(const char* tsl, int tsl_len);
  98. /* patterns: */
  99. /* method:
  100. * set_property_/event_output_/service_output_value:
  101. * method_set, thing_id, identifier, value */
  102. typedef enum {
  103. linkkit_method_set_property_value = 0,
  104. linkkit_method_set_event_output_value,
  105. linkkit_method_set_service_output_value,
  106. linkkit_method_set_number,
  107. } linkkit_method_set_t;
  108. /**
  109. * @brief set value to property, event output, service output items.
  110. * if identifier is struct type or service output type or event output type, use '.' as delimeter like "identifier1.ientifier2"
  111. * to point to specific item.
  112. * value and value_str could not be NULL at the same time;
  113. * if value and value_str both as not NULL, value shall be used and value_str will be ignored.
  114. * if value is NULL, value_str not NULL, value_str will be used.
  115. * in brief, value will be used if not NULL, value_str will be used only if value is NULL.
  116. *
  117. * @param method_set, specify set value type.
  118. * @param thing_id, pointer to thing object, specify which thing to set.
  119. * @param identifier, property, event output, service output identifier.
  120. * @param value, value to set.(input int* if target value is int type or enum or bool, float* if float type,
  121. * long long* if date type, char* if text type).
  122. * @param value_str, value to set in string format if value is null.
  123. *
  124. * @return 0 when success, -1 when fail.
  125. */
  126. extern int linkkit_set_value(linkkit_method_set_t method_set, const void* thing_id, const char* identifier,
  127. const void* value, const char* value_str);
  128. typedef enum {
  129. linkkit_method_get_property_value = 0,
  130. linkkit_method_get_event_output_value,
  131. linkkit_method_get_service_input_value,
  132. linkkit_method_get_service_output_value,
  133. linkkit_method_get_number,
  134. } linkkit_method_get_t;
  135. /**
  136. * @brief get value from property, event output, service input/output items.
  137. * if identifier is struct type or service input/output type or event output type, use '.' as delimeter like "identifier1.ientifier2"
  138. * to point to specific item.
  139. * value and value_str could not be NULL at the same time;
  140. * if value and value_str both as not NULL, value shall be used and value_str will be ignored.
  141. * if value is NULL, value_str not NULL, value_str will be used.
  142. * in brief, value will be used if not NULL, value_str will be used only if value is NULL.
  143. * @param method_get, specify get value type.
  144. * @param thing_id, pointer to thing object, specify which thing to get.
  145. * @param identifier, property, event output, service input/output identifier.
  146. * @param value, value to get(input int* if target value is int type or enum or bool, float* if float type,
  147. * long long* if date type, char* if text type).
  148. * @param value_str, value to get in string format. DO NOT modify this when function returns,
  149. * user should copy to user's own buffer for further process.
  150. * user should NOT free the memory.
  151. *
  152. * @return 0 when success, -1 when fail.
  153. */
  154. extern int linkkit_get_value(linkkit_method_get_t method_get, const void* thing_id, const char* identifier,
  155. void* value, char** value_str);
  156. /**
  157. * @brief answer to a service when a service requested by cloud.
  158. *
  159. * @param thing_id, pointer to thing object.
  160. * @param service_identifier, service identifier to answer, user should get this identifier from handle_dm_callback_fp_t type callback
  161. * report that "dm_callback_type_service_requested" happened, use this function to generate response to the service sender.
  162. * @param response_id, id value in response payload. its value is from "dm_callback_type_service_requested" type callback function.
  163. * use the same id as the request to send response as the same communication session.
  164. * @param code, code value in response payload. for example, 200 when service successfully executed, 400 when not successfully executed.
  165. * @param rrpc, specify rrpc service call or not.
  166. *
  167. * @return 0 when success, -1 when fail.
  168. */
  169. #ifdef RRPC_ENABLED
  170. extern int linkkit_answer_service(const void* thing_id, const char* service_identifier, int response_id, int code, int rrpc);
  171. #else
  172. extern int linkkit_answer_service(const void* thing_id, const char* service_identifier, int response_id, int code);
  173. #endif /* RRPC_ENABLED */
  174. /**
  175. * @brief answer a down raw service when a raw service requested by cloud, or invoke a up raw service to cloud.
  176. *
  177. * @param thing_id, pointer to thing object.
  178. * @param is_up_raw, specify up raw(not 0) or down raw reply(0).
  179. * @param raw_data, raw data that sent to cloud.
  180. * @param raw_data_length, raw data length that sent to cloud.
  181. *
  182. * @return 0 when success, -1 when fail.
  183. */
  184. extern int linkkit_invoke_raw_service(const void* thing_id, int is_up_raw, void* raw_data, int raw_data_length);
  185. #ifdef SERVICE_OTA_ENABLED
  186. /**
  187. * @brief perform ota service when "new version detected" reported.
  188. *
  189. * @param is_up_raw, specify up raw(not 0) or down raw reply(0).
  190. * @param data_buf, data buf that used to do ota. ota service will use this buffer to download bin.
  191. * @param data_buf_length, data buf length that used to do ota.
  192. *
  193. * @return 0 when success, -1 when fail.
  194. */
  195. extern int linkkit_invoke_fota_service(void* data_buf, int data_buf_length);
  196. #ifdef SERVICE_COTA_ENABLED
  197. extern int linkkit_invoke_cota_service(void* data_buf, int data_buf_length);
  198. extern int linkkit_invoke_cota_get_config(const char* config_scope, const char* get_type, const char* attribute_Keys, void* option);
  199. #endif /**< SERVICE_COTA_ENABLED*/
  200. #endif /* SERVICE_OTA_ENABLED */
  201. #ifdef DEVICEINFO_ENABLED
  202. /**
  203. * @brief trigger deviceinfo update procedure.
  204. *
  205. * @param thing_id, pointer to thing object.
  206. * @param params, json type string that user to send to cloud.
  207. * @param linkkit_deviceinfo_operation, specify update type or delete type.
  208. *
  209. * @return 0 when success, -1 when fail.
  210. */
  211. int linkkit_trigger_deviceinfo_operate(const void* thing_id, const char* params, linkkit_deviceinfo_operate_t linkkit_deviceinfo_operation);
  212. #endif
  213. /**
  214. * @brief trigger a event to post to cloud.
  215. *
  216. * @param thing_id, pointer to thing object.
  217. * @param event_identifier, event identifier to trigger.
  218. * @param property_identifier, used when trigger event with method "event.property.post", if set, post specified property, if NULL, post all.
  219. *
  220. * @return 0 when success, -1 when fail.
  221. */
  222. extern int linkkit_trigger_event(const void* thing_id, const char* event_identifier, const char* property_identifier);
  223. #ifndef CMP_SUPPORT_MULTI_THREAD
  224. /**
  225. * @brief this function used to yield when want to receive or send data.
  226. * if multi-thread enabled, user should NOT call this function.
  227. *
  228. * @param timeout_ms, timeout value in ms.
  229. *
  230. * @return 0 when success, -1 when fail.
  231. */
  232. extern int linkkit_yield(int timeout_ms);
  233. #endif
  234. #ifdef __cplusplus
  235. }
  236. #endif /* __cplusplus */
  237. #endif /* LINKKIT_EXPORT_H */