ble_qiot_template.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. #ifdef __cplusplus
  13. extern "C" {
  14. #endif
  15. #include "ble_qiot_template.h"
  16. #include <stdio.h>
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include "ble_qiot_export.h"
  20. #include "ble_qiot_common.h"
  21. #include "ble_qiot_param_check.h"
  22. static bool sg_test_bool = 0;
  23. static int sg_test_int = 0;
  24. static char sg_test_str[128] = {0};
  25. static float sg_test_float = 0.0;
  26. static uint16_t sg_test_enum = 0;
  27. static uint32_t sg_test_time = 0;
  28. static struct_property_t_struct sg_test_struct;
  29. static struct_property_t_struct2 sg_test_struct2;
  30. static int ble_property_t_bool_set(const char *data, uint16_t len)
  31. {
  32. sg_test_bool = data[0];
  33. printf("set: sg_test_bool: %d\r\n", sg_test_bool);
  34. return 0;
  35. }
  36. static int ble_property_t_bool_get(char *data, uint16_t buf_len)
  37. {
  38. data[0] = sg_test_bool;
  39. printf("get: sg_test_bool: %d\r\n", sg_test_bool);
  40. return sizeof(uint8_t);
  41. }
  42. static int ble_property_t_int_set(const char *data, uint16_t len)
  43. {
  44. int test_int = 0;
  45. memcpy(&test_int, data, sizeof(int));
  46. sg_test_int = NTOHL(test_int);
  47. printf("set: sg_test_int: %d\r\n", sg_test_int);
  48. return 0;
  49. }
  50. static int ble_property_t_int_get(char *data, uint16_t buf_len)
  51. {
  52. int test_int = 0;
  53. printf("get: sg_test_int: %d\r\n", sg_test_int);
  54. test_int = HTONL(sg_test_int);
  55. memcpy(data, &test_int, sizeof(int));
  56. return sizeof(uint32_t);
  57. }
  58. static int ble_property_t_str_set(const char *data, uint16_t len)
  59. {
  60. memcpy(sg_test_str, data, sizeof(sg_test_str) > len ? len : (sizeof(sg_test_str) - 1));
  61. printf("set: sg_test_str: %s\r\n", sg_test_str);
  62. return 0;
  63. }
  64. static int ble_property_t_str_get(char *data, uint16_t buf_len)
  65. {
  66. memcpy(data, sg_test_str, strlen(sg_test_str));
  67. printf("get: sg_test_str: %s\r\n", sg_test_str);
  68. return strlen(sg_test_str);
  69. }
  70. static int ble_property_t_float_set(const char *data, uint16_t len)
  71. {
  72. memcpy(&sg_test_float, data, sizeof(float));
  73. printf("set: sg_test_float: %f\r\n", sg_test_float);
  74. return 0;
  75. }
  76. static int ble_property_t_float_get(char *data, uint16_t buf_len)
  77. {
  78. memcpy(data, &sg_test_float, sizeof(float));
  79. printf("get: sg_test_float: %f\r\n", sg_test_float);
  80. return sizeof(float);
  81. }
  82. static int ble_property_t_enum_set(const char *data, uint16_t len)
  83. {
  84. uint16_t test_enum = 0;
  85. memcpy(&test_enum, data, sizeof(uint16_t));
  86. sg_test_enum = NTOHS(test_enum);
  87. printf("set: sg_test_enum: %d\r\n", sg_test_enum);
  88. return 0;
  89. }
  90. static int ble_property_t_enum_get(char *data, uint16_t buf_len)
  91. {
  92. uint16_t test_enum = 0;
  93. printf("get: sg_test_enum: %d\r\n", sg_test_enum);
  94. test_enum = HTONS(sg_test_enum);
  95. memcpy(data, &test_enum, sizeof(uint16_t));
  96. return sizeof(uint16_t);
  97. }
  98. static int ble_property_t_time_set(const char *data, uint16_t len)
  99. {
  100. int test_int = 0;
  101. memcpy(&test_int, data, sizeof(int));
  102. sg_test_time = NTOHL(test_int);
  103. printf("set: sg_test_time: %d\r\n", sg_test_time);
  104. return 0;
  105. }
  106. static int ble_property_t_time_get(char *data, uint16_t buf_len)
  107. {
  108. int test_int = 0;
  109. printf("get: sg_test_time: %d\r\n", sg_test_time);
  110. test_int = HTONL(sg_test_time);
  111. memcpy(data, &test_int, sizeof(int));
  112. return sizeof(uint32_t);
  113. }
  114. static int ble_property_t_struct_s_bool_set(const char *data, uint16_t len)
  115. {
  116. sg_test_struct.m_s_bool = data[0];
  117. printf("set: sg_test_struct.m_s_bool: %d\r\n", sg_test_struct.m_s_bool);
  118. return 0;
  119. }
  120. static int ble_property_t_struct_s_bool_get(char *data, uint16_t buf_len)
  121. {
  122. data[0] = sg_test_struct.m_s_bool;
  123. printf("get: sg_test_struct.m_s_bool: %d\r\n", sg_test_struct.m_s_bool);
  124. return sizeof(uint8_t);
  125. }
  126. static int ble_property_t_struct_s_int_set(const char *data, uint16_t len)
  127. {
  128. int test_int = 0;
  129. memcpy(&test_int, data, sizeof(int));
  130. sg_test_struct.m_s_int = NTOHL(test_int);
  131. printf("set: sg_test_struct.m_s_int: %d\r\n", sg_test_struct.m_s_int);
  132. return 0;
  133. }
  134. static int ble_property_t_struct_s_int_get(char *data, uint16_t buf_len)
  135. {
  136. int test_int = 0;
  137. printf("get: sg_test_int: %d\r\n", sg_test_struct.m_s_int);
  138. test_int = HTONL(sg_test_struct.m_s_int);
  139. memcpy(data, &test_int, sizeof(int));
  140. return sizeof(uint32_t);
  141. }
  142. static int ble_property_t_struct_s_str_set(const char *data, uint16_t len)
  143. {
  144. memcpy(sg_test_struct.m_s_str, data,
  145. sizeof(sg_test_struct.m_s_str) > len ? len : (sizeof(sg_test_struct.m_s_str) - 1));
  146. printf("set: sg_test_struct.m_s_str: %s\r\n", sg_test_struct.m_s_str);
  147. return 0;
  148. }
  149. static int ble_property_t_struct_s_str_get(char *data, uint16_t buf_len)
  150. {
  151. memcpy(data, sg_test_struct.m_s_str, strlen(sg_test_struct.m_s_str));
  152. printf("get: sg_test_struct.m_s_str: %s\r\n", sg_test_struct.m_s_str);
  153. return strlen(sg_test_struct.m_s_str);
  154. }
  155. static int ble_property_t_struct_s_float_set(const char *data, uint16_t len)
  156. {
  157. memcpy(&sg_test_struct.m_s_float, data, sizeof(float));
  158. printf("set: sg_test_struct.m_s_float: %f\r\n", sg_test_struct.m_s_float);
  159. return 0;
  160. }
  161. static int ble_property_t_struct_s_float_get(char *data, uint16_t buf_len)
  162. {
  163. memcpy(data, &sg_test_struct.m_s_float, sizeof(float));
  164. printf("get: sg_test_struct.m_s_float: %f\r\n", sg_test_struct.m_s_float);
  165. return sizeof(float);
  166. }
  167. static int ble_property_t_struct_s_enum_set(const char *data, uint16_t len)
  168. {
  169. uint16_t test_enum = 0;
  170. memcpy(&test_enum, data, sizeof(uint16_t));
  171. sg_test_struct.m_s_enum = NTOHS(test_enum);
  172. printf("set: sg_test_struct.m_s_enum: %d\r\n", sg_test_struct.m_s_enum);
  173. return 0;
  174. }
  175. static int ble_property_t_struct_s_enum_get(char *data, uint16_t buf_len)
  176. {
  177. uint16_t test_enum = 0;
  178. printf("get: sg_test_struct.m_s_enum: %d\r\n", sg_test_struct.m_s_enum);
  179. test_enum = HTONS(sg_test_struct.m_s_enum);
  180. memcpy(data, &test_enum, sizeof(uint16_t));
  181. return sizeof(uint16_t);
  182. }
  183. static int ble_property_t_struct_s_time_set(const char *data, uint16_t len)
  184. {
  185. int test_int = 0;
  186. memcpy(&test_int, data, sizeof(int));
  187. sg_test_struct.m_s_time = NTOHL(test_int);
  188. printf("set: sg_test_struct.m_s_time: %d\r\n", sg_test_struct.m_s_time);
  189. return 0;
  190. }
  191. static int ble_property_t_struct_s_time_get(char *data, uint16_t buf_len)
  192. {
  193. int test_int = 0;
  194. printf("get: sg_test_struct.m_s_time: %d\r\n", sg_test_struct.m_s_time);
  195. test_int = HTONL(sg_test_struct.m_s_time);
  196. memcpy(data, &test_int, sizeof(int));
  197. return sizeof(uint32_t);
  198. }
  199. static ble_property_t sg_ble_t_struct_property_array[BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_BUTT] = {
  200. {ble_property_t_struct_s_bool_set, ble_property_t_struct_s_bool_get, BLE_QIOT_PROPERTY_AUTH_RW,
  201. BLE_QIOT_DATA_TYPE_BOOL},
  202. {ble_property_t_struct_s_int_set, ble_property_t_struct_s_int_get, BLE_QIOT_PROPERTY_AUTH_RW,
  203. BLE_QIOT_DATA_TYPE_INT},
  204. {ble_property_t_struct_s_str_set, ble_property_t_struct_s_str_get, BLE_QIOT_PROPERTY_AUTH_RW,
  205. BLE_QIOT_DATA_TYPE_STRING},
  206. {ble_property_t_struct_s_float_set, ble_property_t_struct_s_float_get, BLE_QIOT_PROPERTY_AUTH_RW,
  207. BLE_QIOT_DATA_TYPE_FLOAT},
  208. {ble_property_t_struct_s_enum_set, ble_property_t_struct_s_enum_get, BLE_QIOT_PROPERTY_AUTH_RW,
  209. BLE_QIOT_DATA_TYPE_ENUM},
  210. {ble_property_t_struct_s_time_set, ble_property_t_struct_s_time_get, BLE_QIOT_PROPERTY_AUTH_RW,
  211. BLE_QIOT_DATA_TYPE_TIME},
  212. };
  213. static int ble_property_t_struct_set(const char *data, uint16_t len)
  214. {
  215. return ble_user_property_struct_handle(data, len, sg_ble_t_struct_property_array,
  216. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_BUTT);
  217. }
  218. static int ble_property_t_struct_get(char *data, uint16_t len)
  219. {
  220. return ble_user_property_struct_get_data(data, len, sg_ble_t_struct_property_array,
  221. BLE_QIOT_STRUCT_T_STRUCT_PROPERTY_ID_BUTT);
  222. }
  223. static int ble_property_t_struct2_s_bool_set(const char *data, uint16_t len)
  224. {
  225. sg_test_struct2.m_s_bool = data[0];
  226. printf("set: sg_test_struct2.m_s_bool: %d\r\n", sg_test_struct2.m_s_bool);
  227. return 0;
  228. }
  229. static int ble_property_t_struct2_s_bool_get(char *data, uint16_t buf_len)
  230. {
  231. data[0] = sg_test_struct2.m_s_bool;
  232. printf("get: sg_test_struct2.m_s_bool: %d\r\n", sg_test_struct2.m_s_bool);
  233. return sizeof(uint8_t);
  234. }
  235. static ble_property_t sg_ble_t_struct2_property_array[BLE_QIOT_STRUCT_T_STRUCT2_PROPERTY_ID_BUTT] = {
  236. {ble_property_t_struct2_s_bool_set, ble_property_t_struct2_s_bool_get, BLE_QIOT_PROPERTY_AUTH_RW,
  237. BLE_QIOT_DATA_TYPE_BOOL},
  238. };
  239. static int ble_property_t_struct2_set(const char *data, uint16_t len)
  240. {
  241. return ble_user_property_struct_handle(data, len, sg_ble_t_struct2_property_array,
  242. BLE_QIOT_STRUCT_T_STRUCT2_PROPERTY_ID_BUTT);
  243. }
  244. static int ble_property_t_struct2_get(char *data, uint16_t len)
  245. {
  246. return ble_user_property_struct_get_data(data, len, sg_ble_t_struct2_property_array,
  247. BLE_QIOT_STRUCT_T_STRUCT2_PROPERTY_ID_BUTT);
  248. }
  249. static ble_property_t sg_ble_property_array[BLE_QIOT_PROPERTY_ID_BUTT] = {
  250. {ble_property_t_bool_set, ble_property_t_bool_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_BOOL},
  251. {ble_property_t_int_set, ble_property_t_int_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_INT},
  252. {ble_property_t_str_set, ble_property_t_str_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_STRING},
  253. {ble_property_t_float_set, ble_property_t_float_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_FLOAT},
  254. {ble_property_t_enum_set, ble_property_t_enum_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_ENUM},
  255. {ble_property_t_time_set, ble_property_t_time_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_TIME},
  256. {ble_property_t_struct_set, ble_property_t_struct_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_STRUCT},
  257. {ble_property_t_struct2_set, ble_property_t_struct2_get, BLE_QIOT_PROPERTY_AUTH_RW, BLE_QIOT_DATA_TYPE_STRUCT},
  258. };
  259. static bool ble_check_space_enough_by_type(uint8_t type, uint16_t left_size)
  260. {
  261. switch (type) {
  262. case BLE_QIOT_DATA_TYPE_BOOL:
  263. return left_size >= sizeof(uint8_t);
  264. case BLE_QIOT_DATA_TYPE_INT:
  265. case BLE_QIOT_DATA_TYPE_FLOAT:
  266. case BLE_QIOT_DATA_TYPE_TIME:
  267. return left_size >= sizeof(uint32_t);
  268. case BLE_QIOT_DATA_TYPE_ENUM:
  269. return left_size >= sizeof(uint16_t);
  270. default:
  271. // string length is unknow, default true
  272. return true;
  273. }
  274. }
  275. static uint16_t ble_check_ret_value_by_type(uint8_t type, uint16_t buf_len, uint16_t ret_val)
  276. {
  277. switch (type) {
  278. case BLE_QIOT_DATA_TYPE_BOOL:
  279. return ret_val <= sizeof(uint8_t);
  280. case BLE_QIOT_DATA_TYPE_INT:
  281. case BLE_QIOT_DATA_TYPE_FLOAT:
  282. case BLE_QIOT_DATA_TYPE_TIME:
  283. return ret_val <= sizeof(uint32_t);
  284. case BLE_QIOT_DATA_TYPE_ENUM:
  285. return ret_val <= sizeof(uint16_t);
  286. default:
  287. // string length is unknow, default true
  288. return ret_val <= buf_len;
  289. }
  290. }
  291. uint8_t ble_get_property_type_by_id(uint8_t id)
  292. {
  293. if (id >= BLE_QIOT_PROPERTY_ID_BUTT) {
  294. ble_qiot_log_e("invalid property id %d", id);
  295. return BLE_QIOT_DATA_TYPE_BUTT;
  296. }
  297. return sg_ble_property_array[id].type;
  298. }
  299. int ble_user_property_set_data(const e_ble_tlv *tlv)
  300. {
  301. POINTER_SANITY_CHECK(tlv, BLE_QIOT_RS_ERR_PARA);
  302. if (tlv->id >= BLE_QIOT_PROPERTY_ID_BUTT) {
  303. ble_qiot_log_e("invalid property id %d", tlv->id);
  304. return BLE_QIOT_RS_ERR;
  305. }
  306. if (NULL != sg_ble_property_array[tlv->id].set_cb) {
  307. if (0 != sg_ble_property_array[tlv->id].set_cb(tlv->val, tlv->len)) {
  308. ble_qiot_log_e("set property id %d failed", tlv->id);
  309. return BLE_QIOT_RS_ERR;
  310. } else {
  311. return BLE_QIOT_RS_OK;
  312. }
  313. }
  314. ble_qiot_log_e("invalid set callback, id %d", tlv->id);
  315. return BLE_QIOT_RS_ERR;
  316. }
  317. int ble_user_property_get_data_by_id(uint8_t id, char *buf, uint16_t buf_len)
  318. {
  319. int ret_len = 0;
  320. POINTER_SANITY_CHECK(buf, BLE_QIOT_RS_ERR_PARA);
  321. if (id >= BLE_QIOT_PROPERTY_ID_BUTT) {
  322. ble_qiot_log_e("invalid property id %d", id);
  323. return -1;
  324. }
  325. if (NULL != sg_ble_property_array[id].get_cb) {
  326. if (!ble_check_space_enough_by_type(sg_ble_property_array[id].type, buf_len)) {
  327. ble_qiot_log_e("not enough space get property id %d data", id);
  328. return -1;
  329. }
  330. ret_len = sg_ble_property_array[id].get_cb(buf, buf_len);
  331. if (ret_len < 0) {
  332. ble_qiot_log_e("get property id %d data failed", id);
  333. return -1;
  334. } else {
  335. if (ble_check_ret_value_by_type(sg_ble_property_array[id].type, buf_len, ret_len)) {
  336. return ret_len;
  337. } else {
  338. ble_qiot_log_e("property id %d length invalid", id);
  339. return -1;
  340. }
  341. }
  342. }
  343. ble_qiot_log_e("invalid callback, property id %d", id);
  344. return 0;
  345. }
  346. int ble_user_property_report_reply_handle(uint8_t result)
  347. {
  348. ble_qiot_log_d("report reply result %d", result);
  349. return BLE_QIOT_RS_OK;
  350. }
  351. int ble_user_property_struct_handle(const char *in_buf, uint16_t buf_len, ble_property_t struct_arr[], uint8_t arr_size)
  352. {
  353. uint16_t parse_len = 0;
  354. uint16_t ret_len = 0;
  355. e_ble_tlv tlv;
  356. while (parse_len < buf_len) {
  357. memset(&tlv, 0, sizeof(e_ble_tlv));
  358. ret_len = ble_lldata_parse_tlv(in_buf + parse_len, buf_len - parse_len, &tlv);
  359. parse_len += ret_len;
  360. if (parse_len > buf_len) {
  361. ble_qiot_log_e("parse struct failed");
  362. return parse_len;
  363. }
  364. if (tlv.id >= arr_size) {
  365. ble_qiot_log_e("invalid array index %d", tlv.id);
  366. return parse_len;
  367. }
  368. if (NULL == struct_arr[tlv.id].set_cb) {
  369. ble_qiot_log_e("invalid member id %d", tlv.id);
  370. return parse_len;
  371. }
  372. if (BLE_QIOT_RS_OK != struct_arr[tlv.id].set_cb(tlv.val, tlv.len)) {
  373. ble_qiot_log_e("user handle property error, member id %d, type %d, len %d", tlv.id, tlv.type, tlv.len);
  374. return parse_len;
  375. }
  376. }
  377. return 0;
  378. }
  379. int ble_user_property_struct_get_data(char *in_buf, uint16_t buf_len, ble_property_t struct_arr[], uint8_t arr_size)
  380. {
  381. uint8_t property_id = 0;
  382. uint8_t property_type = 0;
  383. int property_len = 0;
  384. char * data_buf = in_buf;
  385. uint16_t data_len = 0;
  386. uint16_t string_len = 0;
  387. for (property_id = 0; property_id < arr_size; property_id++) {
  388. property_type = struct_arr[property_id].type;
  389. if (property_type >= BLE_QIOT_DATA_TYPE_BUTT) {
  390. ble_qiot_log_e("member id %d type %d invalid", property_id, property_type);
  391. return BLE_QIOT_RS_ERR;
  392. }
  393. data_buf[data_len++] = BLE_QIOT_PACKAGE_TLV_HEAD(property_type, property_id);
  394. if (BLE_QIOT_DATA_TYPE_STRING == property_type) {
  395. // reserved 2 bytes for string length
  396. property_len = struct_arr[property_id].get_cb((char *)data_buf + data_len + 2, buf_len - data_len - 2);
  397. } else {
  398. property_len = struct_arr[property_id].get_cb((char *)data_buf + data_len, buf_len - data_len);
  399. }
  400. if (property_len < 0) {
  401. ble_qiot_log_e("too long data, member id %d, data length %d", property_id, data_len);
  402. return BLE_QIOT_RS_ERR;
  403. } else if (property_len == 0) {
  404. // no data to post
  405. data_len--;
  406. data_buf[data_len] = '0';
  407. ble_qiot_log_d("member id %d no data to post", property_id);
  408. } else {
  409. if (BLE_QIOT_DATA_TYPE_STRING == property_type) {
  410. string_len = HTONS(property_len);
  411. memcpy(data_buf + data_len, &string_len, sizeof(uint16_t));
  412. data_len += sizeof(uint16_t);
  413. }
  414. data_len += property_len;
  415. }
  416. }
  417. return data_len;
  418. }
  419. static int ble_event_get_t_event_t_bool(char *data, uint16_t buf_len)
  420. {
  421. data[0] = true;
  422. return sizeof(uint8_t);
  423. }
  424. static int ble_event_get_t_event_t_int(char *data, uint16_t buf_len)
  425. {
  426. int test_int = HTONL(32);
  427. memcpy(data, &test_int, sizeof(int));
  428. return sizeof(uint32_t);
  429. }
  430. static int ble_event_get_t_event_t_str(char *data, uint16_t buf_len)
  431. {
  432. memcpy(data, "event", sizeof("event") - 1);
  433. return sizeof("event") - 1;
  434. }
  435. static int ble_event_get_t_event_t_enum(char *data, uint16_t buf_len)
  436. {
  437. uint16_t test_enum = HTONS(1);
  438. memcpy(data, &test_enum, sizeof(uint16_t));
  439. return sizeof(uint16_t);
  440. }
  441. static int ble_event_get_t_event_t_float(char *data, uint16_t buf_len)
  442. {
  443. float test_float = 1.0;
  444. memcpy(data, &test_float, sizeof(float));
  445. return sizeof(float);
  446. }
  447. static int ble_event_get_t_event_t_time(char *data, uint16_t buf_len)
  448. {
  449. int test_int = HTONL(32);
  450. memcpy(data, &test_int, sizeof(int));
  451. return sizeof(uint32_t);
  452. }
  453. static ble_event_param sg_ble_event_t_event_array[BLE_QIOT_EVENT_T_EVENT_PARAM_ID_BUTT] = {
  454. {ble_event_get_t_event_t_bool, BLE_QIOT_DATA_TYPE_BOOL}, {ble_event_get_t_event_t_int, BLE_QIOT_DATA_TYPE_INT},
  455. {ble_event_get_t_event_t_str, BLE_QIOT_DATA_TYPE_STRING}, {ble_event_get_t_event_t_enum, BLE_QIOT_DATA_TYPE_ENUM},
  456. {ble_event_get_t_event_t_float, BLE_QIOT_DATA_TYPE_FLOAT}, {ble_event_get_t_event_t_time, BLE_QIOT_DATA_TYPE_TIME},
  457. };
  458. static ble_event_t sg_ble_event_array[BLE_QIOT_EVENT_ID_BUTT] = {
  459. {sg_ble_event_t_event_array, sizeof(sg_ble_event_t_event_array) / sizeof(ble_event_param)},
  460. };
  461. int ble_event_get_id_array_size(uint8_t event_id)
  462. {
  463. if (event_id >= BLE_QIOT_EVENT_ID_BUTT) {
  464. ble_qiot_log_e("invalid event id %d", event_id);
  465. return -1;
  466. }
  467. return sg_ble_event_array[event_id].array_size;
  468. }
  469. uint8_t ble_event_get_param_id_type(uint8_t event_id, uint8_t param_id)
  470. {
  471. if (event_id >= BLE_QIOT_EVENT_ID_BUTT) {
  472. ble_qiot_log_e("invalid event id %d", event_id);
  473. return BLE_QIOT_DATA_TYPE_BUTT;
  474. }
  475. if (param_id >= sg_ble_event_array[event_id].array_size) {
  476. ble_qiot_log_e("invalid param id %d", param_id);
  477. return BLE_QIOT_DATA_TYPE_BUTT;
  478. }
  479. return sg_ble_event_array[event_id].event_array[param_id].type;
  480. }
  481. int ble_event_get_data_by_id(uint8_t event_id, uint8_t param_id, char *out_buf, uint16_t buf_len)
  482. {
  483. int ret_len = 0;
  484. if (event_id >= BLE_QIOT_EVENT_ID_BUTT) {
  485. ble_qiot_log_e("invalid event id %d", event_id);
  486. return -1;
  487. }
  488. if (param_id >= sg_ble_event_array[event_id].array_size) {
  489. ble_qiot_log_e("invalid param id %d", param_id);
  490. return -1;
  491. }
  492. if (NULL == sg_ble_event_array[event_id].event_array[param_id].get_cb) {
  493. ble_qiot_log_e("invalid callback, event id %d, param id %d", event_id, param_id);
  494. return 0;
  495. }
  496. if (!ble_check_space_enough_by_type(sg_ble_event_array[event_id].event_array[param_id].type, buf_len)) {
  497. ble_qiot_log_e("not enough space get data, event id %d, param id %d", event_id, param_id);
  498. return -1;
  499. }
  500. ret_len = sg_ble_event_array[event_id].event_array[param_id].get_cb(out_buf, buf_len);
  501. if (ret_len < 0) {
  502. ble_qiot_log_e("get event data failed, event id %d, param id %d", event_id, param_id);
  503. return -1;
  504. } else {
  505. if (ble_check_ret_value_by_type(sg_ble_event_array[event_id].event_array[param_id].type, buf_len, ret_len)) {
  506. return ret_len;
  507. } else {
  508. ble_qiot_log_e("evnet data length invalid, event id %d, param id %d", event_id, param_id);
  509. return -1;
  510. }
  511. }
  512. }
  513. int ble_user_event_reply_handle(uint8_t event_id, uint8_t result)
  514. {
  515. ble_qiot_log_d("event id %d, reply result %d", event_id, result);
  516. return BLE_QIOT_RS_OK;
  517. }
  518. static int ble_action_handle_t_action_input_cb(e_ble_tlv *input_param_array, uint8_t input_array_size,
  519. uint8_t *output_id_array)
  520. {
  521. int i = 0;
  522. int test_int = 0;
  523. float test_float = 0;
  524. uint16_t test_enum = 0;
  525. uint32_t test_time = 0;
  526. for (i = 0; i < input_array_size; i++) {
  527. if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_BOOL == input_param_array[i].id) {
  528. printf("input id: %d, val: %d\r\n", BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_BOOL, input_param_array[i].val[0]);
  529. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_INT == input_param_array[i].id) {
  530. memcpy(&test_int, input_param_array[i].val, sizeof(int));
  531. test_int = NTOHL(test_int);
  532. printf("input id: %d, val: %d\r\n", BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_INT, test_int);
  533. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_STR == input_param_array[i].id) {
  534. printf("input id: %d, val: %s\r\n", BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_STR, input_param_array[i].val);
  535. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_FLOAT == input_param_array[i].id) {
  536. memcpy(&test_float, input_param_array[i].val, sizeof(float));
  537. printf("input id: %d, val: %f\r\n", BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_FLOAT, test_float);
  538. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_ENUM == input_param_array[i].id) {
  539. memcpy(&test_enum, input_param_array[i].val, sizeof(uint16_t));
  540. test_enum = NTOHS(test_enum);
  541. printf("input id: %d, val: %d\r\n", BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_ENUM, test_enum);
  542. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_TIME == input_param_array[i].id) {
  543. memcpy(&test_time, input_param_array[i].val, sizeof(int));
  544. test_time = NTOHL(test_time);
  545. printf("input id: %d, val: %d\r\n", BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_TIME, test_time);
  546. }
  547. output_id_array[input_param_array[i].id] = true;
  548. }
  549. return 0;
  550. }
  551. static int ble_action_handle_t_action_output_cb(uint8_t output_id, char *buf, uint16_t buf_len)
  552. {
  553. uint16_t ret_len = 0;
  554. int test_int = HTONL(32);
  555. int test_float = 3;
  556. uint16_t test_enum = HTONS(1);
  557. uint32_t test_time = HTONL(1613059200);
  558. if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_BOOL == output_id) {
  559. buf[0] = true;
  560. ret_len = 1;
  561. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_INT == output_id) {
  562. memcpy(buf, &test_int, sizeof(int));
  563. ret_len = sizeof(int);
  564. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_STR == output_id) {
  565. memcpy(buf, "output", sizeof("output") - 1);
  566. ret_len = sizeof("output") - 1;
  567. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_FLOAT == output_id) {
  568. memcpy(buf, &test_float, sizeof(float));
  569. ret_len = sizeof(float);
  570. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_ENUM == output_id) {
  571. memcpy(buf, &test_enum, sizeof(uint16_t));
  572. ret_len = sizeof(uint16_t);
  573. } else if (BLE_QIOT_ACTION_T_ACTION_INPUT_ID_IN_TIME == output_id) {
  574. memcpy(buf, &test_time, sizeof(int));
  575. ret_len = sizeof(int);
  576. }
  577. return ret_len;
  578. }
  579. static uint8_t sg_ble_action_t_action_input_type_array[BLE_QIOT_ACTION_T_ACTION_INPUT_ID_BUTT] = {
  580. BLE_QIOT_DATA_TYPE_BOOL, BLE_QIOT_DATA_TYPE_INT, BLE_QIOT_DATA_TYPE_STRING,
  581. BLE_QIOT_DATA_TYPE_FLOAT, BLE_QIOT_DATA_TYPE_ENUM, BLE_QIOT_DATA_TYPE_TIME,
  582. };
  583. static uint8_t sg_ble_action_t_action_output_type_array[BLE_QIOT_ACTION_T_ACTION_OUTPUT_ID_BUTT] = {
  584. BLE_QIOT_DATA_TYPE_BOOL, BLE_QIOT_DATA_TYPE_INT, BLE_QIOT_DATA_TYPE_STRING,
  585. BLE_QIOT_DATA_TYPE_FLOAT, BLE_QIOT_DATA_TYPE_ENUM, BLE_QIOT_DATA_TYPE_TIME,
  586. };
  587. static ble_action_t sg_ble_action_array[BLE_QIOT_ACTION_ID_BUTT] = {
  588. {ble_action_handle_t_action_input_cb, ble_action_handle_t_action_output_cb, sg_ble_action_t_action_input_type_array,
  589. sg_ble_action_t_action_output_type_array, sizeof(sg_ble_action_t_action_input_type_array) / sizeof(uint8_t),
  590. sizeof(sg_ble_action_t_action_output_type_array) / sizeof(uint8_t)},
  591. };
  592. uint8_t ble_action_get_intput_type_by_id(uint8_t action_id, uint8_t input_id)
  593. {
  594. if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
  595. ble_qiot_log_e("invalid action id %d", action_id);
  596. return BLE_QIOT_DATA_TYPE_BUTT;
  597. }
  598. if (input_id >= sg_ble_event_array[action_id].array_size) {
  599. ble_qiot_log_e("invalid input id %d", input_id);
  600. return BLE_QIOT_DATA_TYPE_BUTT;
  601. }
  602. return sg_ble_action_array[action_id].input_type_array[input_id];
  603. }
  604. uint8_t ble_action_get_output_type_by_id(uint8_t action_id, uint8_t output_id)
  605. {
  606. if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
  607. ble_qiot_log_e("invalid action id %d", action_id);
  608. return BLE_QIOT_DATA_TYPE_BUTT;
  609. }
  610. if (output_id >= sg_ble_event_array[action_id].array_size) {
  611. ble_qiot_log_e("invalid output id %d", output_id);
  612. return BLE_QIOT_DATA_TYPE_BUTT;
  613. }
  614. return sg_ble_action_array[action_id].output_type_array[output_id];
  615. }
  616. int ble_action_get_input_id_size(uint8_t action_id)
  617. {
  618. if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
  619. ble_qiot_log_e("invalid action id %d", action_id);
  620. return -1;
  621. }
  622. return sg_ble_action_array[action_id].input_id_size;
  623. }
  624. int ble_action_get_output_id_size(uint8_t action_id)
  625. {
  626. if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
  627. ble_qiot_log_e("invalid action id %d", action_id);
  628. return -1;
  629. }
  630. return sg_ble_action_array[action_id].output_id_size;
  631. }
  632. int ble_action_user_handle_input_param(uint8_t action_id, e_ble_tlv *input_param_array, uint8_t input_array_size,
  633. uint8_t *output_id_array)
  634. {
  635. if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
  636. ble_qiot_log_e("invalid action id %d", action_id);
  637. return -1;
  638. }
  639. if (NULL != sg_ble_action_array[action_id].input_cb) {
  640. if (0 != sg_ble_action_array[action_id].input_cb(input_param_array, input_array_size, output_id_array)) {
  641. ble_qiot_log_e("input handle error");
  642. return -1;
  643. }
  644. }
  645. return 0;
  646. }
  647. int ble_action_user_handle_output_param(uint8_t action_id, uint8_t output_id, char *buf, uint16_t buf_len)
  648. {
  649. int ret_len = 0;
  650. if (action_id >= BLE_QIOT_ACTION_ID_BUTT) {
  651. ble_qiot_log_e("invalid action id %d", action_id);
  652. return -1;
  653. }
  654. if (NULL == sg_ble_action_array[action_id].output_cb) {
  655. ble_qiot_log_e("invalid callback, action id %d", action_id);
  656. return 0;
  657. }
  658. if (!ble_check_space_enough_by_type(sg_ble_action_array[action_id].output_type_array[output_id], buf_len)) {
  659. ble_qiot_log_e("not enough space get data, action id %d, output id %d", action_id, output_id);
  660. return -1;
  661. }
  662. ret_len = sg_ble_action_array[action_id].output_cb(output_id, buf, buf_len);
  663. if (ret_len < 0) {
  664. ble_qiot_log_e("get action data failed, action id %d, output id %d", action_id, output_id);
  665. return -1;
  666. } else {
  667. if (ble_check_ret_value_by_type(sg_ble_action_array[action_id].output_type_array[output_id], buf_len,
  668. ret_len)) {
  669. return ret_len;
  670. } else {
  671. ble_qiot_log_e("action data length invalid, action id %d, output id %d", action_id, output_id);
  672. return -1;
  673. }
  674. }
  675. }
  676. #ifdef __cplusplus
  677. }
  678. #endif