ble_qiot_template.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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. // message type, reference data template
  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_POWER_SWITCH = 0,
  55. BLE_QIOT_PROPERTY_ID_COLOR,
  56. BLE_QIOT_PROPERTY_ID_BRIGHTNESS,
  57. BLE_QIOT_PROPERTY_ID_NAME,
  58. BLE_QIOT_PROPERTY_ID_BUTT,
  59. };
  60. // define property color enum
  61. enum {
  62. BLE_QIOT_PROPERTY_COLOR_RED = 0,
  63. BLE_QIOT_PROPERTY_COLOR_GREEN = 1,
  64. BLE_QIOT_PROPERTY_COLOR_BLUE = 2,
  65. BLE_QIOT_PROPERTY_COLOR_BUTT = 3,
  66. };
  67. // define brightness attributes
  68. #define BLE_QIOT_PROPERTY_BRIGHTNESS_STEP (1)
  69. #define BLE_QIOT_PROPERTY_BRIGHTNESS_MIN (0)
  70. #define BLE_QIOT_PROPERTY_BRIGHTNESS_MAX (100)
  71. #define BLE_QIOT_PROPERTY_BRIGHTNESS_START (1)
  72. // define name length limit
  73. #define BLE_QIOT_PROPERTY_NAME_LEN_MIN (0)
  74. #define BLE_QIOT_PROPERTY_NAME_LEN_MAX (640)
  75. // define property set handle return 0 if success, other is error
  76. // sdk call the function that inform the server data to the device
  77. typedef int (*property_set_cb)(const char *data, uint16_t len);
  78. // define property get handle. return the data length obtained, -1 is error, 0 is no data
  79. // sdk call the function fetch user data and send to the server, the data should wrapped by user adn skd just transmit
  80. typedef int (*property_get_cb)(char *buf, uint16_t buf_len);
  81. // each property have a struct ble_property_t, make up a array named sg_ble_property_array
  82. typedef struct{
  83. property_set_cb set_cb; //set callback
  84. property_get_cb get_cb; //get callback
  85. uint8_t authority; //property authority
  86. uint8_t type; //data type
  87. }ble_property_t;
  88. #define BLE_QIOT_INCLUDE_EVENT
  89. // define event id
  90. enum {
  91. BLE_QIOT_EVENT_ID_STATUS_REPORT = 0,
  92. BLE_QIOT_EVENT_ID_LOW_VOLTAGE,
  93. BLE_QIOT_EVENT_ID_HARDWARE_FAULT,
  94. BLE_QIOT_EVENT_ID_BUTT,
  95. };
  96. // define param id for event status_report
  97. enum {
  98. BLE_QIOT_EVENT_STATUS_REPORT_PARAM_ID_STATUS = 0,
  99. BLE_QIOT_EVENT_STATUS_REPORT_PARAM_ID_MESSAGE,
  100. BLE_QIOT_EVENT_STATUS_REPORT_PARAM_ID_BUTT,
  101. };
  102. // define range for param message
  103. #define BLE_QIOT_EVENT_STATUS_REPORT_MESSAGE_LEN_MIN (0)
  104. #define BLE_QIOT_EVENT_STATUS_REPORT_MESSAGE_LEN_MAX (64)
  105. // define param id for event low_voltage
  106. enum {
  107. BLE_QIOT_EVENT_LOW_VOLTAGE_PARAM_ID_VOLTAGE = 0,
  108. BLE_QIOT_EVENT_LOW_VOLTAGE_PARAM_ID_BUTT,
  109. };
  110. // define param voltage attributes
  111. #define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_STEP (1)
  112. #define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_MIN (0.0)
  113. #define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_MAX (24.0)
  114. #define BLE_QIOT_EVENT_LOW_VOLTAGE_VOLTAGE_START (1)
  115. // define param id for event hardware_fault
  116. enum {
  117. BLE_QIOT_EVENT_HARDWARE_FAULT_PARAM_ID_NAME = 0,
  118. BLE_QIOT_EVENT_HARDWARE_FAULT_PARAM_ID_ERROR_CODE,
  119. BLE_QIOT_EVENT_HARDWARE_FAULT_PARAM_ID_BUTT,
  120. };
  121. // define range for param name
  122. #define BLE_QIOT_EVENT_HARDWARE_FAULT_NAME_LEN_MIN (0)
  123. #define BLE_QIOT_EVENT_HARDWARE_FAULT_NAME_LEN_MAX (64)
  124. // define param error_code attributes
  125. #define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_STEP (1)
  126. #define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_MIN (0)
  127. #define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_MAX (2000)
  128. #define BLE_QIOT_EVENT_HARDWARE_FAULT_ERROR_CODE_START (1)
  129. // define event get handle. return the data length obtained, -1 is error, 0 is no data
  130. // sdk call the function fetch user data and send to the server, the data should wrapped by user adn skd just transmit
  131. typedef int (*event_get_cb)(char *buf, uint16_t buf_len);
  132. // each param have a struct ble_event_param, make up a array for the event
  133. typedef struct{
  134. event_get_cb get_cb; //get param data callback
  135. uint8_t type; //param type
  136. }ble_event_param;
  137. // a array named sg_ble_event_array is composed by all the event array
  138. typedef struct{
  139. ble_event_param *event_array; //array of params data
  140. uint8_t array_size; //array size
  141. }ble_event_t;
  142. #define BLE_QIOT_INCLUDE_ACTION
  143. // define action id
  144. enum {
  145. BLE_QIOT_ACTION_ID_LOOP = 0,
  146. BLE_QIOT_ACTION_ID_BUTT,
  147. };
  148. // define input id for action loop
  149. enum {
  150. BLE_QIOT_ACTION_LOOP_INPUT_ID_INTERVAL = 0,
  151. BLE_QIOT_ACTION_LOOP_INPUT_ID_BUTT,
  152. };
  153. // define output id for action loop
  154. enum {
  155. BLE_QIOT_ACTION_LOOP_OUTPUT_ID_RESULT = 0,
  156. BLE_QIOT_ACTION_LOOP_OUTPUT_ID_BUTT,
  157. };
  158. #define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_MIN (0)
  159. #define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_MAX (100)
  160. #define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_START (0)
  161. #define BLE_QIOT_ACTION_INPUT_LOOP_INTERVAL_STEP (1)
  162. // define output id result attributes
  163. #define BLE_QIOT_ACTION_OUTPUT_LOOP_RESULT_LEN_MIN (0)
  164. #define BLE_QIOT_ACTION_OUTPUT_LOOP_RESULT_LEN_MAX (320)
  165. // define max input id and output id in all of input id and output id above
  166. #define BLE_QIOT_ACTION_INPUT_ID_BUTT 1
  167. #define BLE_QIOT_ACTION_OUTPUT_ID_BUTT 1
  168. // define action input handle, return 0 is success, other is error.
  169. // input_param_array carry the data from server, include input id, data length ,data val
  170. // input_array_size means how many input id
  171. // output_id_array filling with output id numbers that need obtained, sdk will traverse it and call the action_output_handle to obtained data
  172. typedef int (*action_input_handle)(e_ble_tlv *input_param_array, uint8_t input_array_size, uint8_t *output_id_array);
  173. // define action output handle, return length of the data, 0 is no data, -1 is error
  174. // output_id means which id data should be obtained
  175. typedef int (*action_output_handle)(uint8_t output_id, char *buf, uint16_t buf_len);
  176. // each action have a struct ble_action_t, make up a array named sg_ble_action_array
  177. typedef struct{
  178. action_input_handle input_cb; //handle input data
  179. action_output_handle output_cb; // get output data in the callback
  180. uint8_t *input_type_array; //type array for input id
  181. uint8_t *output_type_array; //type array for output id
  182. uint8_t input_id_size; //numbers of input id
  183. uint8_t output_id_size; //numbers of output id
  184. }ble_action_t;
  185. // property module
  186. #ifdef BLE_QIOT_INCLUDE_PROPERTY
  187. uint8_t ble_get_property_type_by_id(uint8_t id);
  188. int ble_user_property_set_data(const e_ble_tlv *tlv);
  189. int ble_user_property_get_data_by_id(uint8_t id, char *buf, uint16_t buf_len);
  190. int ble_user_property_report_reply_handle(uint8_t result);
  191. int ble_lldata_parse_tlv(const char *buf, int buf_len, e_ble_tlv *tlv);
  192. int ble_user_property_struct_handle(const char *in_buf, uint16_t buf_len, ble_property_t *struct_arr, uint8_t arr_size);
  193. int ble_user_property_struct_get_data(char *in_buf, uint16_t buf_len, ble_property_t *struct_arr, uint8_t arr_size);
  194. #endif
  195. // event module
  196. #ifdef BLE_QIOT_INCLUDE_EVENT
  197. int ble_event_get_id_array_size(uint8_t event_id);
  198. uint8_t ble_event_get_param_id_type(uint8_t event_id, uint8_t param_id);
  199. int ble_event_get_data_by_id(uint8_t event_id, uint8_t param_id, char *out_buf, uint16_t buf_len);
  200. int ble_user_event_reply_handle(uint8_t event_id, uint8_t result);
  201. #endif
  202. // action module
  203. #ifdef BLE_QIOT_INCLUDE_ACTION
  204. uint8_t ble_action_get_intput_type_by_id(uint8_t action_id, uint8_t input_id);
  205. uint8_t ble_action_get_output_type_by_id(uint8_t action_id, uint8_t output_id);
  206. int ble_action_get_input_id_size(uint8_t action_id);
  207. int ble_action_get_output_id_size(uint8_t action_id);
  208. int ble_action_user_handle_input_param(uint8_t action_id, e_ble_tlv *input_param_array, uint8_t input_array_size,
  209. uint8_t *output_id_array);
  210. int ble_action_user_handle_output_param(uint8_t action_id, uint8_t output_id, char *buf, uint16_t buf_len);
  211. #endif
  212. #ifdef __cplusplus
  213. }
  214. #endif
  215. #endif //BLE_QIOT_TEMPLATE_H_