ble_qiot_template.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
  3. * Licensed under the MIT License (the "License"); you may not use this file except in
  4. * compliance with the License. You may obtain a copy of the License at
  5. * http://opensource.org/licenses/MIT
  6. * Unless required by applicable law or agreed to in writing, software distributed under the License is
  7. * distributed on an "AS IS" basis, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
  8. * either express or implied. See the License for the specific language governing permissions and
  9. * limitations under the License.
  10. *
  11. */
  12. #ifndef BLE_QIOT_TEMPLATE_H_
  13. #define BLE_QIOT_TEMPLATE_H_
  14. #ifdef __cplusplus
  15. extern "C"{
  16. #endif
  17. #include <stdint.h>
  18. #include <stdbool.h>
  19. // data type in template, corresponding to type in json file
  20. enum {
  21. BLE_QIOT_DATA_TYPE_BOOL = 0,
  22. BLE_QIOT_DATA_TYPE_INT,
  23. BLE_QIOT_DATA_TYPE_STRING,
  24. BLE_QIOT_DATA_TYPE_FLOAT,
  25. BLE_QIOT_DATA_TYPE_ENUM,
  26. BLE_QIOT_DATA_TYPE_TIME,
  27. BLE_QIOT_DATA_TYPE_STRUCT,
  28. BLE_QIOT_DATA_TYPE_BUTT,
  29. };
  30. // define property authority, not used
  31. enum {
  32. BLE_QIOT_PROPERTY_AUTH_RW = 0,
  33. BLE_QIOT_PROPERTY_AUTH_READ,
  34. BLE_QIOT_PROPERTY_AUTH_BUTT,
  35. };
  36. // define message flow direction
  37. enum {
  38. BLE_QIOT_EFFECT_REQUEST = 0,
  39. BLE_QIOT_EFFECT_REPLY,
  40. BLE_QIOT_EFFECT_BUTT,
  41. };
  42. #define BLE_QIOT_PACKAGE_MSG_HEAD(_TYPE, _REPLY, _ID) (((_TYPE) << 6) | (((_REPLY) == BLE_QIOT_EFFECT_REPLY) << 5) | ((_ID) & 0X1F))
  43. #define BLE_QIOT_PACKAGE_TLV_HEAD(_TYPE, _ID) (((_TYPE) << 5) | ((_ID) & 0X1F))
  44. // define tlv struct
  45. typedef struct{
  46. uint8_t type;
  47. uint8_t id;
  48. uint16_t len;
  49. char *val;
  50. }e_ble_tlv;
  51. #define BLE_QIOT_INCLUDE_PROPERTY
  52. // define property id
  53. enum {
  54. BLE_QIOT_PROPERTY_ID_T_BOOL = 0,
  55. BLE_QIOT_PROPERTY_ID_T_INT,
  56. BLE_QIOT_PROPERTY_ID_T_STR,
  57. BLE_QIOT_PROPERTY_ID_T_FLOAT,
  58. BLE_QIOT_PROPERTY_ID_T_ENUM,
  59. BLE_QIOT_PROPERTY_ID_T_TIME,
  60. BLE_QIOT_PROPERTY_ID_T_STRUCT,
  61. BLE_QIOT_PROPERTY_ID_T_STRUCT2,
  62. BLE_QIOT_PROPERTY_ID_BUTT,
  63. };
  64. // define t_int attributes
  65. #define BLE_QIOT_PROPERTY_T_INT_MIN (0)
  66. #define BLE_QIOT_PROPERTY_T_INT_MAX (2222222222)
  67. #define BLE_QIOT_PROPERTY_T_INT_START (0)
  68. #define BLE_QIOT_PROPERTY_T_INT_STEP (1)
  69. // define t_str length limit
  70. #define BLE_QIOT_PROPERTY_T_STR_LEN_MIN (0)
  71. #define BLE_QIOT_PROPERTY_T_STR_LEN_MAX (128)
  72. // define t_float attributes
  73. #define BLE_QIOT_PROPERTY_T_FLOAT_MIN (0)
  74. #define BLE_QIOT_PROPERTY_T_FLOAT_MAX (12800)
  75. #define BLE_QIOT_PROPERTY_T_FLOAT_START (0)
  76. #define BLE_QIOT_PROPERTY_T_FLOAT_STEP (1)
  77. // define property t_enum enum
  78. enum {
  79. BLE_QIOT_PROPERTY_T_ENUM_RED = 0,
  80. BLE_QIOT_PROPERTY_T_ENUM_GREEN = 1,
  81. BLE_QIOT_PROPERTY_T_ENUM_BLUE = 2,
  82. BLE_QIOT_PROPERTY_T_ENUM_BUTT = 3,
  83. };
  84. // define property id
  85. enum {
  86. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_S_BOOL = 0,
  87. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_S_INT,
  88. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_S_STR,
  89. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_S_FLOAT,
  90. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_S_ENUM,
  91. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_S_TIME,
  92. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_BUTT,
  93. };
  94. // define s_int attributes
  95. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_INT_MIN (0)
  96. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_INT_MAX (100000000)
  97. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_INT_START (0)
  98. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_INT_STEP (1)
  99. // define s_str length limit
  100. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_STR_LEN_MIN (0)
  101. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_STR_LEN_MAX (2048)
  102. // define s_float attributes
  103. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_FLOAT_MIN (0)
  104. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_FLOAT_MAX (100)
  105. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_FLOAT_START (0)
  106. #define BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_FLOAT_STEP (1)
  107. // define property s_enum enum
  108. enum {
  109. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_ENUM_R = 0,
  110. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_ENUM_G = 1,
  111. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_ENUM_B = 2,
  112. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_ENUM_BUTT = 3,
  113. };
  114. typedef struct{
  115. bool m_s_bool;
  116. int32_t m_s_int;
  117. char m_s_str[BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_S_STR_LEN_MAX];
  118. float m_s_float;
  119. uint16_t m_s_enum;
  120. uint32_t m_s_time;
  121. }struct_property_t_struct;
  122. // define property id
  123. enum {
  124. BLE_QIOT_STRUCT_T_STRUCT2_PROPERTY_ID_S_BOOL = 0,
  125. BLE_QIOT_STRUCT_T_STRUCT2_PROPERTY_ID_BUTT,
  126. };
  127. typedef struct{
  128. bool m_s_bool;
  129. }struct_property_t_struct2;
  130. // define property set handle return 0 if success, other is error
  131. // sdk call the function that inform the server data to the device
  132. typedef int (*property_set_cb)(const char *data, uint16_t len);
  133. // define property get handle. return the data length obtained, -1 is error, 0 is no data
  134. // sdk call the function fetch user data and send to the server, the data should wrapped by user adn skd just transmit
  135. typedef int (*property_get_cb)(char *buf, uint16_t buf_len);
  136. // each property have a struct ble_property_t, make up a array named sg_ble_property_array
  137. typedef struct{
  138. property_set_cb set_cb; //set callback
  139. property_get_cb get_cb; //get callback
  140. uint8_t authority; //property authority
  141. uint8_t type; //data type
  142. }ble_property_t;
  143. #define BLE_QIOT_INCLUDE_EVENT
  144. // define event id
  145. enum {
  146. BLE_QIOT_EVENT_ID_T_EVENT = 0,
  147. BLE_QIOT_EVENT_ID_BUTT,
  148. };
  149. // define param id for event t_event
  150. enum {
  151. BLE_QIOT_EVENT_T_EVENT_PARAM_ID_T_BOOL = 0,
  152. BLE_QIOT_EVENT_T_EVENT_PARAM_ID_T_INT,
  153. BLE_QIOT_EVENT_T_EVENT_PARAM_ID_T_STR,
  154. BLE_QIOT_EVENT_T_EVENT_PARAM_ID_T_ENUM,
  155. BLE_QIOT_EVENT_T_EVENT_PARAM_ID_T_FLOAT,
  156. BLE_QIOT_EVENT_T_EVENT_PARAM_ID_T_TIME,
  157. BLE_QIOT_EVENT_T_EVENT_PARAM_ID_BUTT,
  158. };
  159. // define param t_int attributes
  160. #define BLE_QIOT_EVENT_T_EVENT_T_INT_MIN (0)
  161. #define BLE_QIOT_EVENT_T_EVENT_T_INT_MAX (100)
  162. #define BLE_QIOT_EVENT_T_EVENT_T_INT_START (0)
  163. #define BLE_QIOT_EVENT_T_EVENT_T_INT_STEP (1)
  164. // define range for param t_str
  165. #define BLE_QIOT_EVENT_T_EVENT_T_STR_LEN_MIN (0)
  166. #define BLE_QIOT_EVENT_T_EVENT_T_STR_LEN_MAX (128)
  167. // define enum for param t_enum
  168. enum {
  169. BLE_QIOT_EVENT_T_EVENT_T_ENUM_R = 0,
  170. BLE_QIOT_EVENT_T_EVENT_T_ENUM_G = 1,
  171. BLE_QIOT_EVENT_T_EVENT_T_ENUM_B = 2,
  172. BLE_QIOT_EVENT_T_EVENT_T_ENUM_BUTT = 3,
  173. };
  174. // define param t_float attributes
  175. #define BLE_QIOT_EVENT_T_EVENT_T_FLOAT_MIN (0)
  176. #define BLE_QIOT_EVENT_T_EVENT_T_FLOAT_MAX (100)
  177. #define BLE_QIOT_EVENT_T_EVENT_T_FLOAT_START (0)
  178. #define BLE_QIOT_EVENT_T_EVENT_T_FLOAT_STEP (1)
  179. // define event get handle. return the data length obtained, -1 is error, 0 is no data
  180. // sdk call the function fetch user data and send to the server, the data should wrapped by user adn skd just transmit
  181. typedef int (*event_get_cb)(char *buf, uint16_t buf_len);
  182. // each param have a struct ble_event_param, make up a array for the event
  183. typedef struct{
  184. event_get_cb get_cb; //get param data callback
  185. uint8_t type; //param type
  186. }ble_event_param;
  187. // a array named sg_ble_event_array is composed by all the event array
  188. typedef struct{
  189. ble_event_param *event_array; //array of params data
  190. uint8_t array_size; //array size
  191. }ble_event_t;
  192. #define BLE_QIOT_INCLUDE_ACTION
  193. // define action id
  194. enum {
  195. BLE_QIOT_ACTION_ID_T_ACTION = 0,
  196. BLE_QIOT_ACTION_ID_BUTT,
  197. };
  198. // define input id for action t_action
  199. enum {
  200. BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_BOOL = 0,
  201. BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_INT,
  202. BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_STR,
  203. BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_FLOAT,
  204. BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_ENUM,
  205. BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_TIME,
  206. BLE_QIOT_ACTION_T_ACTION_INPUT_ID_BUTT,
  207. };
  208. // define output id for action t_action
  209. enum {
  210. BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_OUT_BOOL = 0,
  211. BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_OUT_INT,
  212. BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_OUT_STR,
  213. BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_OUT_FLOAT,
  214. BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_OUT_ENUM,
  215. BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_OUT_TIME,
  216. BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_BUTT,
  217. };
  218. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_INT_MIN (0)
  219. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_INT_MAX (100)
  220. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_INT_START (0)
  221. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_INT_STEP (1)
  222. // define input id in_str attributes
  223. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_IN_STR_LEN_MIN (0)
  224. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_IN_STR_LEN_MAX (128)
  225. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_FLOAT_MIN (0)
  226. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_FLOAT_MAX (100)
  227. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_FLOAT_START (0)
  228. #define BLE_QIOT_ACTION_INPUT_T_ACTION_IN_FLOAT_STEP (1)
  229. // define enum for input id in_enum
  230. enum {
  231. BLE_QIOT_ACTION_INPUT_T_ACTION_IN_ENUM_R = 0,
  232. BLE_QIOT_ACTION_INPUT_T_ACTION_IN_ENUM_G = 1,
  233. BLE_QIOT_ACTION_INPUT_T_ACTION_IN_ENUM_B = 2,
  234. BLE_QIOT_ACTION_INPUT_T_ACTION_IN_ENUM_BUTT = 3,
  235. };
  236. // define output id out_int attributes
  237. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_INT_MIN (0)
  238. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_INT_MAX (100)
  239. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_INT_START (0)
  240. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_INT_STEP (1)
  241. // define output id out_str attributes
  242. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_STR_LEN_MIN (0)
  243. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_STR_LEN_MAX (128)
  244. // define output id out_float attributes
  245. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_FLOAT_MIN (0)
  246. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_FLOAT_MAX (100)
  247. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_FLOAT_START (0)
  248. #define BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_FLOAT_STEP (1)
  249. // define enum for output id out_enum
  250. enum {
  251. BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_ENUM_R = 0,
  252. BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_ENUM_G = 1,
  253. BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_ENUM_B = 2,
  254. BLE_QIOT_ACTION_OUTPUT_T_ACTION_OUT_ENUM_BUTT = 3,
  255. };
  256. // define max input id and output id in all of input id and output id above
  257. #define BLE_QIOT_ACTION_INPUT_ID_BUTT 6
  258. #define BLE_QIOT_ACTION_OUTPUT_ID_BUTT 6
  259. // define action input handle, return 0 is success, other is error.
  260. // input_param_array carry the data from server, include input id, data length ,data val
  261. // input_array_size means how many input id
  262. // output_id_array filling with output id numbers that need obtained, sdk will traverse it and call the action_output_handle to obtained data
  263. typedef int (*action_input_handle)(e_ble_tlv *input_param_array, uint8_t input_array_size, uint8_t *output_id_array);
  264. // define action output handle, return length of the data, 0 is no data, -1 is error
  265. // output_id means which id data should be obtained
  266. typedef int (*action_output_handle)(uint8_t output_id, char *buf, uint16_t buf_len);
  267. // each action have a struct ble_action_t, make up a array named sg_ble_action_array
  268. typedef struct{
  269. action_input_handle input_cb; //handle input data
  270. action_output_handle output_cb; // get output data in the callback
  271. uint8_t *input_type_array; //type array for input id
  272. uint8_t *output_type_array; //type array for output id
  273. uint8_t input_id_size; //numbers of input id
  274. uint8_t output_id_size; //numbers of output id
  275. }ble_action_t;
  276. // property module
  277. #ifdef BLE_QIOT_INCLUDE_PROPERTY
  278. uint8_t ble_get_property_type_by_id(uint8_t id);
  279. int ble_user_property_set_data(const e_ble_tlv *tlv);
  280. int ble_user_property_get_data_by_id(uint8_t id, char *buf, uint16_t buf_len);
  281. int ble_user_property_report_reply_handle(uint8_t result);
  282. int ble_lldata_parse_tlv(const char *buf, int buf_len, e_ble_tlv *tlv);
  283. int ble_user_property_struct_handle(const char *in_buf, uint16_t buf_len, ble_property_t *struct_arr, uint8_t arr_size);
  284. int ble_user_property_struct_get_data(char *in_buf, uint16_t buf_len, ble_property_t *struct_arr, uint8_t arr_size);
  285. #endif
  286. // event module
  287. #ifdef BLE_QIOT_INCLUDE_EVENT
  288. int ble_event_get_id_array_size(uint8_t event_id);
  289. uint8_t ble_event_get_param_id_type(uint8_t event_id, uint8_t param_id);
  290. int ble_event_get_data_by_id(uint8_t event_id, uint8_t param_id, char *out_buf, uint16_t buf_len);
  291. int ble_user_event_reply_handle(uint8_t event_id, uint8_t result);
  292. #endif
  293. // action module
  294. #ifdef BLE_QIOT_INCLUDE_ACTION
  295. uint8_t ble_action_get_intput_type_by_id(uint8_t action_id, uint8_t input_id);
  296. uint8_t ble_action_get_output_type_by_id(uint8_t action_id, uint8_t output_id);
  297. int ble_action_get_input_id_size(uint8_t action_id);
  298. int ble_action_get_output_id_size(uint8_t action_id);
  299. int ble_action_user_handle_input_param(uint8_t action_id, e_ble_tlv *input_param_array, uint8_t input_array_size,
  300. uint8_t *output_id_array);
  301. int ble_action_user_handle_output_param(uint8_t action_id, uint8_t output_id, char *buf, uint16_t buf_len);
  302. #endif
  303. #ifdef __cplusplus
  304. }
  305. #endif
  306. #endif //BLE_QIOT_TEMPLATE_H_