_bluetooth.c 77 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006
  1. #include "pikaScript.h"
  2. #include "nimble/ble.h"
  3. #include "nimble/nimble_port.h"
  4. #include "nimble/nimble_port_freertos.h"
  5. // #include "nimble/host/include/host/ble_gap.h"
  6. #include "host/ble_hs.h"
  7. #include "host/util/util.h"
  8. #include "services/gap/ble_svc_gap.h"
  9. #include "services/gatt/ble_svc_gatt.h"
  10. #include "services/ans/ble_svc_ans.h"
  11. #include "esp_log.h"
  12. #include "nvs_flash.h"
  13. #include "esp_log.h"
  14. // #include "ble_uuid.h"
  15. #include "cb_event_id.h"
  16. // TODO:发布的时候怎么将printf隐藏掉
  17. #define printf pika_platform_printf
  18. ble_uuid_any_t GAP_UUID = {
  19. .u.type = 0,
  20. .u16.value = 0x0000
  21. };
  22. // BLE_UUID_base = 0x00000000-0000-1000-8000-00805F9B34FB;
  23. static const char *tag = "NimBLE_BLE";
  24. bool BLE_ONLY = false; //只使用BLE,默认否
  25. bool FLASH_FIRST_INIT = true; //是否第一次初始化,默认是
  26. bool BLE_INIT = false; //蓝牙是否初始化,默认不是
  27. PikaEventListener *g_pika_ble_listener = NULL; //事件监听器
  28. int g_ble_addr_mode = 0; //地址模式
  29. bool g_ble_connectable = 1; //是否可连接
  30. int g_interval = 0; //间隔
  31. bool g_new_service = 0; //间隔有新服务
  32. uint8_t g_all_chr_count = 0; //全部特性数量
  33. uint16_t * g_chrs_handle = NULL; //特性句柄数组
  34. struct ble_gatt_svc_def* gatt_svr_svcs = NULL; // BLE服务句柄,TODO:用于释放对应空间
  35. // 函数声明
  36. // GATT 服务端回调函数
  37. static int ble_gatt_svc_access_cb(uint16_t conn_handle, uint16_t attr_handle,struct ble_gatt_access_ctxt *ctxt, void *arg);
  38. // GATT 客户端写回调函数
  39. static int ble_gatt_client_write_cb(uint16_t conn_handle,
  40. const struct ble_gatt_error *error,
  41. struct ble_gatt_attr *attr,
  42. void *arg);
  43. // GATT 客户端读回调函数
  44. static int ble_gatt_client_read_cb(uint16_t conn_handle,
  45. const struct ble_gatt_error *error,
  46. struct ble_gatt_attr *attr,
  47. void *arg);
  48. // GATT mtu_exchange回调函数
  49. static int ble_gatt_mtu_exchange_cb(uint16_t conn_handle,
  50. const struct ble_gatt_error *error,
  51. uint16_t mtu, void *arg);
  52. // GATT 查找所有服务回调函数
  53. static int ble_gatt_disc_all_svcs_cb(uint16_t conn_handle,
  54. const struct ble_gatt_error *error,
  55. const struct ble_gatt_svc *service,
  56. void *arg);
  57. // GATT 查找所有服务回调函数
  58. static int ble_gatt_disc_svcs_by_uuid_cb(uint16_t conn_handle,
  59. const struct ble_gatt_error *error,
  60. const struct ble_gatt_svc *service,
  61. void *arg);
  62. // GATT 查找所有特性回调函数
  63. static int ble_gatt_disc_all_chrs_cb(uint16_t conn_handle,
  64. const struct ble_gatt_error *error,
  65. const struct ble_gatt_chr *chr, void *arg);
  66. // GATT 查找特定UUID特性回调函数
  67. static int ble_gatt_disc_chrs_by_uuid_cb(uint16_t conn_handle,
  68. const struct ble_gatt_error *error,
  69. const struct ble_gatt_chr *chr, void *arg);
  70. // GATT 查找所有描述符回调函数
  71. static int ble_gatt_disc_all_dscs_cb(uint16_t conn_handle,
  72. const struct ble_gatt_error *error,
  73. uint16_t chr_val_handle,
  74. const struct ble_gatt_dsc *dsc, void *arg);
  75. // GAP层回调函数
  76. static int ble_gap_event_cb(struct ble_gap_event *event, void *arg);
  77. // gatt初始化基本服务
  78. void gatt_svr_init(void);
  79. // 从字符串中读取UUID
  80. int read_uuid_from_str(char* buf, int len, ble_uuid_any_t* uuid_struct);
  81. // 反转数组
  82. void data_inver(const void *addr,uint8_t *addr_inver,uint8_t size);
  83. int _bluetooth_BLE_adv(int interval);
  84. // 填充UUID
  85. int fill_UUID(ble_uuid_any_t* UUID,int UUID_bytes, uint8_t* bytes_UUID)
  86. {
  87. if(UUID_bytes == 2){
  88. UUID->u.type = BLE_UUID_TYPE_16;
  89. UUID->u16.value = bytes_UUID[0] << 8 | bytes_UUID[1];
  90. }else if(UUID_bytes == 4) {
  91. UUID->u.type = BLE_UUID_TYPE_32;
  92. UUID->u32.value = bytes_UUID[0] << 24 | bytes_UUID[1] << 16 | bytes_UUID[2] << 8 | bytes_UUID[3];
  93. }else{
  94. UUID->u.type = BLE_UUID_TYPE_128;
  95. data_inver(bytes_UUID,UUID->u128.value,16);
  96. }
  97. return 0;
  98. }
  99. // 蓝牙任务
  100. void ble_host_task(void *param)
  101. {
  102. ESP_LOGI(tag, "BLE Host Task Started");
  103. /* This function will return only when nimble_port_stop() is executed */
  104. nimble_port_run();
  105. nimble_port_freertos_deinit();
  106. }
  107. // 获取地址类型
  108. uint8_t get_addr_type(int addr_mode)
  109. {
  110. uint8_t own_addr_type;
  111. switch (addr_mode) {
  112. case 0:
  113. own_addr_type = BLE_OWN_ADDR_PUBLIC;
  114. break;
  115. case 1:
  116. own_addr_type = BLE_OWN_ADDR_RANDOM;
  117. break;
  118. case 2:
  119. own_addr_type = BLE_OWN_ADDR_RPA_PUBLIC_DEFAULT;
  120. break;
  121. case 3:
  122. own_addr_type = BLE_OWN_ADDR_RPA_RANDOM_DEFAULT;
  123. break;
  124. }
  125. return own_addr_type;
  126. }
  127. // int _bluetooth_BLE_init(PikaObj *self)
  128. int _bluetooth_BLE_init(PikaObj *self)
  129. {
  130. printf("_bluetooth_BLE___init__\r\n");
  131. if (FLASH_FIRST_INIT)
  132. {
  133. //初始化flash
  134. esp_err_t ret = nvs_flash_init();
  135. if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
  136. ESP_ERROR_CHECK(nvs_flash_erase());
  137. ret = nvs_flash_init();
  138. }
  139. ESP_ERROR_CHECK(ret);
  140. FLASH_FIRST_INIT = false;
  141. }
  142. return 1;
  143. }
  144. pika_bool _bluetooth_BLE_pyi_active(PikaObj *self, pika_bool active)
  145. {
  146. printf("_bluetooth_BLE_pyi_active\r\n");
  147. if(active == true){
  148. //开始任务
  149. // nimble_port_freertos_init(ble_host_task);
  150. // 初始化堆栈
  151. // TODO: 多蓝牙对象时会报错
  152. // printf("ble_hs_is_enabled : %d\r\n",ble_hs_is_enabled());
  153. if(!BLE_INIT){
  154. nimble_port_init();
  155. BLE_INIT = true;
  156. }
  157. return true;
  158. }else {
  159. if(BLE_INIT){
  160. nimble_port_deinit();
  161. BLE_INIT = false;
  162. }
  163. // nimble_port_stop();
  164. return false;
  165. }
  166. }
  167. pika_bool _bluetooth_BLE_pyi_check_active(PikaObj *self)
  168. {
  169. printf("_bluetooth_BLE_pyi_check_active\r\n");
  170. return BLE_INIT;
  171. }
  172. int _bluetooth_BLE_pyi_test(PikaObj *self)
  173. {
  174. printf("_bluetooth_BLE_test\r\n");
  175. return 1;
  176. }
  177. void data_inver(const void *addr,uint8_t *addr_inver,uint8_t size)
  178. {
  179. const uint8_t *u8p;
  180. u8p = addr;
  181. for ( int i = 0; i < size; i++){
  182. addr_inver[i] = u8p[size - i - 1];
  183. }
  184. }
  185. void print_addr(const void *addr)
  186. {
  187. const uint8_t *u8p;
  188. u8p = addr;
  189. printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n",u8p[5], u8p[4], u8p[3], u8p[2], u8p[1], u8p[0]);
  190. }
  191. void print_uuid_128(uint8_t *uuid)
  192. {
  193. for (size_t i = 0; i < 16; i++)
  194. {
  195. printf("%02x",uuid[15 - i]);
  196. }
  197. printf("\r\n");
  198. }
  199. void print_data(uint8_t *data,uint8_t count)
  200. {
  201. for (size_t i = 0; i < count; i++)
  202. {
  203. printf("%02x",data[i]);
  204. }
  205. printf("\r\n");
  206. }
  207. /**
  208. * Logs information about a connection to the console.
  209. */
  210. static void print_conn_desc(struct ble_gap_conn_desc *desc)
  211. {
  212. MODLOG_DFLT(INFO, "handle=%d our_ota_addr_type=%d our_ota_addr=",
  213. desc->conn_handle, desc->our_ota_addr.type);
  214. print_addr(desc->our_ota_addr.val);
  215. MODLOG_DFLT(INFO, " our_id_addr_type=%d our_id_addr=",
  216. desc->our_id_addr.type);
  217. print_addr(desc->our_id_addr.val);
  218. MODLOG_DFLT(INFO, " peer_ota_addr_type=%d peer_ota_addr=",
  219. desc->peer_ota_addr.type);
  220. print_addr(desc->peer_ota_addr.val);
  221. MODLOG_DFLT(INFO, " peer_id_addr_type=%d peer_id_addr=",
  222. desc->peer_id_addr.type);
  223. print_addr(desc->peer_id_addr.val);
  224. MODLOG_DFLT(INFO, " conn_itvl=%d conn_latency=%d supervision_timeout=%d "
  225. "encrypted=%d authenticated=%d bonded=%d\r\n",
  226. desc->conn_itvl, desc->conn_latency,
  227. desc->supervision_timeout,
  228. desc->sec_state.encrypted,
  229. desc->sec_state.authenticated,
  230. desc->sec_state.bonded);
  231. }
  232. // pyi 绑定的advertise函数
  233. int _bluetooth_BLE_advertise(PikaObj *self, int addr, int interval, pika_bool connectable,
  234. uint8_t * adv_data, int adv_data_len, pika_bool adv_data_append, uint8_t * rsp_data, int rsp_data_len)
  235. {
  236. // 如果注册服务之后nimble_port_freertos_init了,就不需要再此处调用
  237. nimble_port_freertos_init(ble_host_task);
  238. printf("_bluetooth_BLE_gap_advertise\r\n");
  239. // 声明并初始化广播结构体
  240. struct ble_hs_adv_fields fields;
  241. memset(&fields, 0, sizeof fields);
  242. fields.flags = BLE_HS_ADV_F_DISC_GEN ;
  243. if(BLE_ONLY == true){
  244. fields.flags |= BLE_HS_ADV_F_BREDR_UNSUP;
  245. }
  246. // 添加广播数据
  247. if(adv_data_append == true) // 添加0XFF自定义数据
  248. {
  249. fields.tx_pwr_lvl_is_present = 0;
  250. fields.tx_pwr_lvl = BLE_HS_ADV_TX_PWR_LVL_AUTO;
  251. char* name = ble_svc_gap_device_name();
  252. fields.name = (uint8_t *)name;
  253. fields.name_len = strlen(name);
  254. fields.name_is_complete = 1;
  255. if (GAP_UUID.u.type != 0){
  256. if(GAP_UUID.u.type == BLE_UUID_TYPE_16){
  257. fields.uuids16 = &GAP_UUID;
  258. fields.num_uuids16 = 1;
  259. fields.uuids16_is_complete = 1;
  260. }
  261. else if (GAP_UUID.u.type == BLE_UUID_TYPE_32)
  262. {
  263. fields.uuids32 = &GAP_UUID;
  264. fields.num_uuids32 = 1;
  265. fields.uuids32_is_complete = 1;
  266. }else if (GAP_UUID.u.type == BLE_UUID_TYPE_128)
  267. {
  268. fields.uuids128 = &GAP_UUID;
  269. fields.num_uuids128 = 1;
  270. fields.uuids128_is_complete = 1;
  271. }
  272. }
  273. if(adv_data_len > 0)
  274. {
  275. fields.mfg_data = (uint8_t *)adv_data;
  276. fields.mfg_data_len = adv_data_len;
  277. }
  278. int rc = ble_gap_adv_set_fields(&fields);
  279. if (rc != 0) {
  280. printf("error setting advertisement data; rc=%d\r\n", rc);
  281. return -1 ;
  282. }
  283. }else{ //所有数据都是自定义数据
  284. ble_gap_adv_set_data(adv_data,adv_data_len);
  285. }
  286. //设置rsp data
  287. if(rsp_data_len > 0) {
  288. uint8_t* rsp_data_new = (uint8_t*)malloc(rsp_data_len + 2);
  289. rsp_data_new[0] = rsp_data_len + 1;
  290. rsp_data_new[1] = 0xff;
  291. memcpy(rsp_data_new + 2, rsp_data, rsp_data_len);
  292. int rc;
  293. rc = ble_gap_adv_rsp_set_data(rsp_data_new,rsp_data_len+2);
  294. if (rc != 0) {
  295. printf("error setting advertisement response data; rc=%d\r\n", rc);
  296. free(rsp_data_new);
  297. return -1 ;
  298. }
  299. free(rsp_data_new);
  300. }
  301. // 设置全局变量
  302. g_ble_addr_mode = addr;
  303. g_ble_connectable = connectable;
  304. g_interval = interval;
  305. return _bluetooth_BLE_adv(interval);
  306. }
  307. //TODO:1. 换个命名 2.再次进行广播,之前的值应该清零?
  308. // 在C文件里调用, 方便没有连接上的时候,能够恢复广播
  309. int _bluetooth_BLE_adv(int interval)
  310. {
  311. printf("_bluetooth_BLE_adv\r\n");
  312. // 声明并初始化广播结构体
  313. struct ble_gap_adv_params *adv_params = NULL;
  314. adv_params = (struct ble_gap_adv_params *)calloc(1, sizeof(struct ble_gap_adv_params));
  315. uint8_t own_addr_type = get_addr_type(g_ble_addr_mode);
  316. // 连接模式
  317. uint8_t connet_mode;
  318. if(g_ble_connectable == true){
  319. connet_mode = BLE_GAP_CONN_MODE_UND;
  320. }else {
  321. connet_mode = BLE_GAP_CONN_MODE_NON;
  322. }
  323. adv_params->conn_mode = connet_mode;
  324. adv_params->disc_mode = BLE_GAP_DISC_MODE_GEN;
  325. if(interval == 0){ // 默认值是104, 65ms
  326. adv_params->itvl_min = 0;
  327. adv_params->itvl_max = 0;
  328. }else if (interval < 35){ //需要大于35,
  329. adv_params->itvl_min = 35;
  330. adv_params->itvl_max = 36;
  331. }else{
  332. adv_params->itvl_min = interval;
  333. adv_params->itvl_max = interval + 1; //只能和adv_params.itvl_min差1
  334. }
  335. if(ble_gap_adv_active() == 1){ //是否原有广播任务
  336. printf("ble_gap_adv_stop\r\n");
  337. ble_gap_adv_stop();
  338. }
  339. int rc = ble_gap_adv_start(own_addr_type, NULL, BLE_HS_FOREVER, adv_params, ble_gap_event_cb, NULL);
  340. if(rc != 0)
  341. return rc;
  342. free(adv_params);
  343. // 清空服务信息结构体
  344. if(g_new_service == 1)
  345. {
  346. uint8_t * one_bytes_handle;
  347. one_bytes_handle = (uint8_t *)malloc(g_all_chr_count);
  348. for (int i = 0; i < g_all_chr_count; i++)
  349. {
  350. one_bytes_handle[i] = g_chrs_handle[i];
  351. }
  352. // 将实际注册到的handle发送给python
  353. pika_eventListener_send(g_pika_ble_listener,_IRQ_DIY_REGISTER_HANDLE,
  354. arg_newObj(New_pikaTupleFrom(
  355. arg_newInt(_IRQ_DIY_REGISTER_HANDLE),
  356. arg_newBytes(one_bytes_handle,g_all_chr_count)
  357. )));
  358. free(one_bytes_handle);
  359. // 释放内存空间的时候需要遍历结构体
  360. // TODO:释放空间内存占比反而高了
  361. // struct ble_gatt_chr_def* gatt_svr_chrs;
  362. // struct ble_gatt_dsc_def* gatt_svr_dscs;
  363. // int i=0,j=0;
  364. // while (true){
  365. // j = 0;
  366. // if(gatt_svr_svcs[i].type == 0) // 服务数量中止符
  367. // break;
  368. // else {
  369. // gatt_svr_chrs = gatt_svr_svcs[i].characteristics;
  370. // while (true){
  371. // if(gatt_svr_chrs[j].uuid == 0) // 特性数量中止
  372. // break;
  373. // else{
  374. // // free(gatt_svr_chrs[j].uuid); // 特性的UUID
  375. // gatt_svr_dscs = gatt_svr_chrs[j].descriptors;
  376. // free(gatt_svr_dscs); // 特性的描述符
  377. // j++;}
  378. // }
  379. // // TODO:这里的uuid释放后,蓝牙连接断开时会报错
  380. // // free((ble_uuid_any_t*)gatt_svr_svcs[i].uuid); // 服务的UUID
  381. // free(gatt_svr_chrs); // 服务的特性
  382. // i++;
  383. // }
  384. // }
  385. // free(gatt_svr_svcs); // 服务组
  386. // gatt_svr_svcs = NULL;
  387. g_new_service = 0;
  388. }
  389. return 0;
  390. }
  391. // 停止广播
  392. int _bluetooth_BLE_stop_advertise(PikaObj *self)
  393. {
  394. printf("_bluetooth_BLE_stop_advertise\r\n");
  395. if(ble_gap_adv_active() == 1){
  396. return ble_gap_adv_stop();
  397. }else{
  398. return 0;
  399. }
  400. }
  401. int _bluetooth_BLE_pyi_gap_connect(PikaObj *self, uint8_t* peer_addr, int peer_addr_type, int scan_duration_ms)
  402. {
  403. // nimble_port_freertos_init(ble_host_task);
  404. printf("_bluetooth_BLE_gap_connect\r\n");
  405. int rc = ble_gap_disc_active();
  406. if (rc != 0) {
  407. printf("Failed to cancel scan; rc=%d\n", rc);
  408. return rc;
  409. }
  410. uint8_t own_addr_type ;
  411. rc = ble_hs_id_infer_auto(0, &own_addr_type);
  412. printf("own_addr_type = %d\r\n", own_addr_type);
  413. if (rc != 0) {
  414. MODLOG_DFLT(ERROR, "error determining address type; rc=%d\n", rc);
  415. return rc;
  416. }
  417. ble_addr_t addr_peer = {
  418. .type = peer_addr_type,
  419. };
  420. data_inver(peer_addr,addr_peer.val,6);
  421. return ble_gap_connect(own_addr_type,&addr_peer,scan_duration_ms,NULL,ble_gap_event_cb,NULL);
  422. }
  423. int _bluetooth_BLE_pyi_gap_disconnect(PikaObj *self, int conn_handle)
  424. {
  425. printf("_bluetooth_BLE_gap_disconnect\r\n");
  426. return ble_gap_terminate(conn_handle, BLE_ERR_REM_USER_CONN_TERM);
  427. }
  428. int _bluetooth_BLE_gap_disc(PikaObj *self, int addr_mode, int duration_ms, int interval, int window, pika_bool active)
  429. {
  430. nimble_port_freertos_init(ble_host_task);
  431. printf("_bluetooth_BLE_gap_disc\r\n");
  432. // 获取地址类型
  433. uint8_t own_addr_type = get_addr_type(addr_mode);
  434. // 声明并初始化结构体实例
  435. struct ble_gap_disc_params disc_params = {
  436. .itvl = interval,
  437. .window = window,
  438. .passive = ~active,
  439. .limited = 0,
  440. .filter_duplicates = 0
  441. };
  442. if(ble_gap_disc_active() == 1)
  443. ble_gap_disc_cancel();
  444. if (duration_ms == 0){
  445. return ble_gap_disc(own_addr_type, BLE_HS_FOREVER, &disc_params, ble_gap_event_cb, NULL);
  446. }else{
  447. return ble_gap_disc(own_addr_type, duration_ms, &disc_params, ble_gap_event_cb, NULL);
  448. }
  449. }
  450. // 停止扫描
  451. // TODO:不会引发扫描中止事件
  452. int _bluetooth_BLE_gap_stop_disc(PikaObj *self)
  453. {
  454. printf("_bluetooth_BLE_gap_stop_scan\r\n");
  455. return ble_gap_disc_cancel();
  456. }
  457. // 注册服务
  458. int _bluetooth_BLE_gatts_register_svcs(PikaObj *self, PikaObj* services_info,int all_chr_count)
  459. {
  460. // nimble_port_stop();
  461. printf("_bluetooth_BLE_gatts_register_svcs\r\n");
  462. ble_gatts_reset(); //重置GATT服务
  463. g_new_service = 1; //有新服务
  464. g_all_chr_count = all_chr_count; //同步全部特性数量
  465. g_chrs_handle = (uint16_t * )calloc(g_all_chr_count,sizeof(uint16_t));
  466. if(g_chrs_handle == NULL){
  467. printf("calloc g_chrs_handle error\r\n");
  468. return -1;
  469. }
  470. size_t service_count , chr_count, dsc_count;
  471. uint8_t i,j,k;
  472. uint8_t UUID_bytes,handle_index = 0;
  473. struct ble_gatt_chr_def* gatt_svr_chrs;
  474. struct ble_gatt_dsc_def* gatt_svr_dscs;
  475. service_count = pikaTuple_getSize(services_info); //服务的个数,是不确定的
  476. // printf("services_info service_count = %d\r\n",service_count);
  477. gatt_svr_svcs = (struct ble_gatt_svc_def*) malloc((service_count + 1) * sizeof(struct ble_gatt_svc_def)); //申请空间
  478. if(gatt_svr_svcs == NULL){
  479. printf("malloc svcs memory error\r\n");
  480. return -1;
  481. }
  482. gatt_svr_svcs[service_count].type = 0; //服务数量中止
  483. for (i = 0;i < service_count;i++){ //对于每个服务
  484. PikaObj* service = arg_getObj(pikaTuple_getArg(services_info, i)); //读取服务
  485. uint8_t* service_bytes_UUID = arg_getBytes(pikaTuple_getArg(service,0)); //获取服务的UUID
  486. UUID_bytes = arg_getInt(pikaTuple_getArg(service,1)); //获取服务的UUID大小
  487. int service_struct_size = pikaTuple_getSize(service);
  488. if(service_struct_size == 2){ // 无属性
  489. // 设置服务
  490. ble_uuid_any_t* service_UUID = (ble_uuid_any_t*) malloc(sizeof(ble_uuid_any_t));
  491. if(service_UUID == NULL){
  492. printf("malloc service_UUID memory error\r\n");
  493. return -1;
  494. }
  495. gatt_svr_svcs[i].type = BLE_GATT_SVC_TYPE_PRIMARY;
  496. gatt_svr_svcs[i].characteristics = NULL;
  497. gatt_svr_svcs[i].uuid = &(service_UUID->u);
  498. gatt_svr_svcs[i].includes = NULL; //很关键
  499. fill_UUID(service_UUID,UUID_bytes,service_bytes_UUID);
  500. }else{ //有属性
  501. PikaObj* chrs = arg_getObj(pikaTuple_getArg(service, 2)); //读取属性合集
  502. chr_count = pikaTuple_getSize(chrs); // 属性的个数,是不确定的
  503. gatt_svr_chrs = (struct ble_gatt_chr_def*) malloc((chr_count + 1)* sizeof(struct ble_gatt_chr_def)); //申请空间
  504. if(gatt_svr_chrs == NULL){
  505. printf("malloc chrs memory error\r\n");
  506. return -1;
  507. }
  508. gatt_svr_chrs[chr_count].uuid = 0; //特性数量中止
  509. // 设置服务
  510. ble_uuid_any_t* service_UUID = (ble_uuid_any_t*) malloc(sizeof(ble_uuid_any_t));
  511. if(service_UUID == NULL){
  512. printf("malloc service_UUID memory error\r\n");
  513. return -1;
  514. }
  515. gatt_svr_svcs[i].type = BLE_GATT_SVC_TYPE_PRIMARY;
  516. gatt_svr_svcs[i].characteristics = gatt_svr_chrs;
  517. gatt_svr_svcs[i].uuid = &(service_UUID->u);
  518. gatt_svr_svcs[i].includes = NULL; //很关键
  519. fill_UUID(service_UUID,UUID_bytes,service_bytes_UUID);
  520. // printf("service %d chrs size %d \r\n",i,chr_count);
  521. for (j = 0;j < chr_count;j++){ // 对于每个属性
  522. PikaObj* chr = arg_getObj(pikaTuple_getArg(chrs, j)); //读取属性
  523. uint8_t * chr_bytes_UUID = arg_getBytes(pikaTuple_getArg(chr,0)); //属性UUID
  524. uint8_t UUID_bytes = arg_getInt(pikaTuple_getArg(chr,1)); //属性UUID大小
  525. uint64_t chr_flags = pikaTuple_getInt(chr,2); //属性FLAG
  526. int chr_struct_size = pikaTuple_getSize(chr);
  527. if(chr_struct_size == 3){ // 无描述符
  528. // 设置属性
  529. ble_uuid_any_t* chr_UUID = (ble_uuid_any_t*) malloc(sizeof(ble_uuid_any_t));
  530. if(chr_UUID == NULL){
  531. printf("malloc chr_UUID memory error\r\n");
  532. return -1;
  533. }
  534. gatt_svr_chrs[j].uuid = &(chr_UUID->u);
  535. gatt_svr_chrs[j].access_cb = ble_gatt_svc_access_cb;
  536. gatt_svr_chrs[j].arg = self;
  537. gatt_svr_chrs[j].flags = chr_flags;
  538. gatt_svr_chrs[j].descriptors = NULL;
  539. gatt_svr_chrs[j].val_handle = &(g_chrs_handle[handle_index]);//蓝牙同步之后,才会有有效值
  540. fill_UUID(chr_UUID,UUID_bytes,chr_bytes_UUID);
  541. handle_index++;
  542. }else{ // 有描述符
  543. PikaObj* dscs = arg_getObj(pikaTuple_getArg(chr, 3)); // dscs = 描述符合集
  544. dsc_count = pikaTuple_getSize(dscs); //描述符的个数,是不确定的
  545. gatt_svr_dscs = (struct ble_gatt_dsc_def*) malloc((dsc_count + 1 )* sizeof(struct ble_gatt_dsc_def)); //申请空间
  546. if(gatt_svr_dscs == NULL){
  547. printf("malloc dscs memory error\r\n");
  548. return -1;
  549. }
  550. gatt_svr_dscs[dsc_count].uuid = 0; //描述符数量中止
  551. // 设置属性
  552. ble_uuid_any_t* chr_UUID = (ble_uuid_any_t*) malloc(sizeof(ble_uuid_any_t));
  553. if(chr_UUID == NULL){
  554. printf("malloc chr_UUID memory error\r\n");
  555. return -1;
  556. }
  557. gatt_svr_chrs[j].uuid = &(chr_UUID->u);
  558. gatt_svr_chrs[j].access_cb = ble_gatt_svc_access_cb;
  559. gatt_svr_chrs[j].arg = self;
  560. gatt_svr_chrs[j].flags = chr_flags;
  561. gatt_svr_chrs[j].descriptors = gatt_svr_dscs;
  562. gatt_svr_chrs[j].val_handle = &(g_chrs_handle[handle_index]);//蓝牙同步之后,才会有有效值
  563. fill_UUID(chr_UUID,UUID_bytes,chr_bytes_UUID);
  564. handle_index++;
  565. // printf("chr_UUID_size : %d chr_flags %d dscs size:%d\r\n",UUID_bytes,chr_flags,dsc_count);
  566. for(k = 0;k < dsc_count;k++){ //对于每个描述符
  567. PikaObj * dsc = arg_getObj(pikaTuple_getArg(dscs, k));
  568. uint8_t * dsc_bytes_UUID = arg_getBytes(pikaTuple_getArg(dsc,0));
  569. uint8_t UUID_bytes = arg_getInt(pikaTuple_getArg(dsc,1));
  570. uint16_t dsc_flags = pikaTuple_getInt(dsc, 2);
  571. // 设置描述符
  572. ble_uuid_any_t* dsc_UUID = (ble_uuid_any_t*) malloc(sizeof(ble_uuid_any_t));
  573. if(dsc_UUID == NULL){
  574. printf("malloc dsc_UUID memory error\r\n");
  575. return -1;
  576. }
  577. gatt_svr_dscs[k].uuid = &(dsc_UUID->u);
  578. gatt_svr_dscs[k].access_cb = ble_gatt_svc_access_cb;
  579. gatt_svr_dscs[k].arg = self;
  580. gatt_svr_dscs[k].att_flags = dsc_flags;
  581. fill_UUID(dsc_UUID,UUID_bytes,dsc_bytes_UUID);
  582. }
  583. }
  584. }
  585. }
  586. }
  587. //注册基本服务
  588. gatt_svr_init();
  589. // 注册服务
  590. int rc = ble_gatts_count_cfg(gatt_svr_svcs);
  591. if (rc != 0) {
  592. return rc;
  593. }
  594. rc = ble_gatts_add_svcs(gatt_svr_svcs);
  595. if (rc != 0) {
  596. return rc;
  597. }
  598. // nimble_port_freertos_init(ble_host_task);
  599. // int rc = 0;
  600. return rc;
  601. }
  602. // 设置广播数据
  603. // 在设置前需要蓝牙协议栈处于同步状态, nimble_port_freertos_init(ble_host_task)
  604. // 直接发送传入内容, 不按照规范补充格式
  605. // 若要按规范补充则需要调用ble_gap_adv_set_fields(const struct ble_hs_adv_fields *rsp_fields)函数
  606. int _bluetooth_BLE_set_adv_data(PikaObj *self, uint8_t* data, int data_len)
  607. {
  608. printf("_bluetooth_BLE_set_adv_data\r\n");
  609. return ble_gap_adv_set_data(data,data_len);
  610. }
  611. // 设置扫描响应数据
  612. // 在设置前需要蓝牙协议栈处于同步状态, nimble_port_freertos_init(ble_host_task)
  613. // 直接发送传入内容, 不按照规范补充格式
  614. // 若要按规范补充则需要调用ble_gap_adv_rsp_set_fields(const struct ble_hs_adv_fields *rsp_fields)函数
  615. // int _bluetooth_BLE_set_rsp_data(PikaObj *self, char* data, int data_len)
  616. int _bluetooth_BLE_set_rsp_data(PikaObj *self, uint8_t* data, int data_len)
  617. {
  618. printf("_bluetooth_BLE_set_rsp_data\r\n");
  619. // printf("%hx %hx %hx %hx\r\n",data[0],data[1],data[2],data[3]);
  620. // printf("%d %d %d %d\r\n",data[0],data[1],data[2],data[3]);
  621. return ble_gap_adv_rsp_set_data(data,data_len);
  622. }
  623. char * _bluetooth_BLE_config_mac_get(PikaObj *self)
  624. {
  625. // nimble_port_freertos_init(ble_host_task);
  626. printf("_bluetooth_BLE_config_mac_get\r\n");
  627. uint8_t own_addr_type;
  628. uint8_t addr_val[6] = {0};
  629. int rc = ble_hs_id_infer_auto(0, &own_addr_type);
  630. if (rc != 0) {
  631. printf("error determining address type; rc=%d\n", rc);
  632. return obj_cacheStr(self,"");
  633. }
  634. rc = ble_hs_id_copy_addr(own_addr_type, addr_val, NULL);
  635. // print_addr(addr_val);
  636. char mac[18];
  637. sprintf(&mac, "%02x:%02x:%02x:%02x:%02x:%02x",addr_val[5], addr_val[4], addr_val[3], addr_val[2], addr_val[1], addr_val[0]);
  638. // nimble_port_stop();
  639. return obj_cacheStr(self,mac);
  640. }
  641. // 测试通过
  642. char* _bluetooth_BLE_config_gap_name_get(PikaObj *self){
  643. printf("_bluetooth_BLE_config_addr_gap_name_get\r\n");
  644. char *name = ble_svc_gap_device_name();
  645. return obj_cacheStr(self, name);
  646. }
  647. int _bluetooth_BLE_config_addr_mode_get(PikaObj *self){
  648. printf("_bluetooth_BLE_config_addr_mode_get\r\n");
  649. // nimble_port_freertos_init(ble_host_task);
  650. uint8_t own_addr_type;
  651. int rc = ble_hs_id_infer_auto(0, &own_addr_type);
  652. // nimble_port_stop();
  653. return own_addr_type;
  654. }
  655. int _bluetooth_BLE_config_mtu_get(PikaObj *self){
  656. printf("_bluetooth_BLE_config_mtu_get\r\n");
  657. return 0;
  658. }
  659. int _bluetooth_BLE_config_addr_rxbuf_get(PikaObj *self){
  660. printf("_bluetooth_BLE_config_addr_rxbuf_get\r\n");
  661. return 0;
  662. }
  663. int _bluetooth_BLE_config_bond_get(PikaObj *self){
  664. printf("_bluetooth_BLE_config_bond_get\r\n");
  665. return 0;
  666. }
  667. int _bluetooth_BLE_config_io_get(PikaObj *self)
  668. {
  669. printf("_bluetooth_BLE_config_io_get\r\n");
  670. return 0;
  671. }
  672. int _bluetooth_BLE_config_le_secure_get(PikaObj *self)
  673. {
  674. printf("_bluetooth_BLE_config_le_secure_get\r\n");
  675. return 0;
  676. }
  677. int _bluetooth_BLE_config_mitm_get(PikaObj *self)
  678. {
  679. printf("_bluetooth_BLE_config_mitm_get\r\n");
  680. return 0;
  681. }
  682. int _bluetooth_BLE_config_addr_mode_update(PikaObj *self)
  683. {
  684. printf("_bluetooth_BLE_config_addr_mode_update\r\n");
  685. return 0;
  686. }
  687. int _bluetooth_BLE_config_bond_update(PikaObj *self, pika_bool bond)
  688. {
  689. printf("_bluetooth_BLE_config_bond_update\r\n");
  690. return 0;
  691. }
  692. // 基本测试通过
  693. int _bluetooth_BLE_config_gap_name_update(PikaObj *self, char* gap_name)
  694. {
  695. printf("_bluetooth_BLE_config_gap_name_update\r\n");
  696. return ble_svc_gap_device_name_set(gap_name);
  697. }
  698. // TODO:未实现
  699. Arg* _bluetooth_BLE_config_gap_uuid_get(PikaObj *self)
  700. {
  701. Arg* arg = arg_newBytes(NULL,0);
  702. return arg;
  703. }
  704. int _bluetooth_BLE_config_gap_uuid_update(PikaObj *self, uint8_t* uuid,int len)
  705. {
  706. printf("_bluetooth_BLE_config_gap_uuid_update\r\n");
  707. if(len == 2){
  708. GAP_UUID.u16.u.type = BLE_UUID_TYPE_16;
  709. GAP_UUID.u16.value = uuid[0] << 8 | uuid[1];
  710. }else if(len == 4){
  711. GAP_UUID.u32.u.type = BLE_UUID_TYPE_32;
  712. GAP_UUID.u32.value = uuid[0] << 24 | uuid[1] << 16 | uuid[2] << 8 | uuid[3];
  713. }else{
  714. GAP_UUID.u128.u.type = BLE_UUID_TYPE_128;
  715. data_inver(uuid,GAP_UUID.u128.value,16);
  716. }
  717. return 0;
  718. }
  719. // TODO:未找到对应函数
  720. int _bluetooth_BLE_config_io_update(PikaObj *self, int io){
  721. printf("_bluetooth_BLE_config_io_update\r\n");
  722. return 0;
  723. }
  724. int _bluetooth_BLE_config_le_secure_update(PikaObj *self, pika_bool le_secure)
  725. {
  726. ESP_LOGD(tag, "_bluetooth_BLE_config_le_secure_update\r\n");
  727. // TODO:需要进行判断,若BLE处于广播状态则不能修改
  728. if(ble_gap_adv_active()){
  729. ESP_LOGI(tag, "an advertisement procedure is currently in progress\r\n");
  730. return -1;
  731. }
  732. else{
  733. BLE_ONLY = le_secure;
  734. ESP_LOGI(tag, "secure update succeed\r\n");
  735. return 0;
  736. }
  737. }
  738. // TODO:未找到实现方法
  739. int _bluetooth_BLE_config_mac_update(PikaObj *self)
  740. {
  741. printf("_bluetooth_BLE_config_mac_update\r\n");
  742. return 0;
  743. }
  744. // TODO:未找到对应函数
  745. int _bluetooth_BLE_config_mitm_update(PikaObj *self, pika_bool mitm)
  746. {
  747. printf("_bluetooth_BLE_config_mitm_update\r\n");
  748. return 0;
  749. }
  750. // TODO:未找到对应函数
  751. int _bluetooth_BLE_config_mtu_update(PikaObj *self, int mtu)
  752. {
  753. printf("_bluetooth_BLE_config_mtu_update\r\n");
  754. return 0;
  755. }
  756. // TODO:未找到对应函数
  757. int _bluetooth_BLE_config_rxbuf_update(PikaObj *self, int rxbuf)
  758. {
  759. printf("_bluetooth_BLE_config_rxbuf_update\r\n");
  760. return 0;
  761. }
  762. int _bluetooth_BLE_pyi_test2(PikaObj *self)
  763. {
  764. uint8_t data[2];
  765. data[0] = 0x00;
  766. data[1] = 0x00;
  767. struct os_mbuf *om = ble_hs_mbuf_from_flat(data, 2);
  768. return ble_gattc_write_no_rsp(1, 38, om);
  769. }
  770. int _bluetooth_BLE_pyi_test3(PikaObj *self, int connhandle, int valuehandle)
  771. {
  772. printf("_bluetooth_BLE_pyi_test3\r\n");
  773. // uint8_t data[2];
  774. // data[0] = 0x00;
  775. // // data[1] = 0x02;
  776. // struct os_mbuf *om = ble_hs_mbuf_from_flat(data, 2);
  777. // return ble_gattc_write_no_rsp(1, 38, om);
  778. // ble_gatts_notify(1,32);
  779. printf("ble_hs_is_enabled(void) %d\r\n", ble_hs_is_enabled());
  780. return 0;
  781. }
  782. int _bluetooth_BLE_test_call_some_name(PikaObj *self)
  783. {
  784. printf("_bluetooth_BLE_test_call_some_name\r\n");
  785. return 0;
  786. }
  787. //查找全部服务
  788. int _bluetooth_BLE_gattc_dis_svcs(PikaObj *self, int conn_handle){
  789. return ble_gattc_disc_all_svcs(conn_handle, ble_gatt_disc_all_svcs_cb, NULL);
  790. }
  791. //通过UUID查找服务
  792. int _bluetooth_BLE_gattc_dis_svcs_by_uuid(PikaObj *self, int conn_handle, uint8_t* uuid,int len){
  793. ble_uuid_any_t uuid_any = {0};
  794. // printf("UUID: %02x %02x\r\n",uuid[0],uuid[1]);
  795. if(len == 2){
  796. uuid_any.u16.u.type = BLE_UUID_TYPE_16;
  797. uuid_any.u16.value = uuid[0] << 8 | uuid[1];
  798. // uuid_any.u16.value = uuid[0] * 256 + uuid[1];
  799. // uuid_any.u16.value = 0x1800;
  800. // uuid_any.u16.value = 0x26;
  801. // memcpy((uint8_t*)&(uuid_any.u16.value),uuid,2);
  802. }else if(len == 4){
  803. uuid_any.u32.u.type = BLE_UUID_TYPE_32;
  804. uuid_any.u32.value = uuid[0] << 24 | uuid[1] << 16 | uuid[2] << 8 | uuid[3];
  805. }else{
  806. // uint8_t data[16];
  807. uuid_any.u128.u.type = BLE_UUID_TYPE_128;
  808. data_inver(uuid,uuid_any.u128.value,16);
  809. }
  810. return ble_gattc_disc_svc_by_uuid(conn_handle,&uuid_any.u,ble_gatt_disc_svcs_by_uuid_cb, NULL);
  811. }
  812. //通过UUID查找全部属性
  813. int _bluetooth_BLE_gattc_dis_chrs(PikaObj *self, int conn_handle, int start_handle, int end_handle){
  814. return ble_gattc_disc_all_chrs(conn_handle,start_handle,end_handle,ble_gatt_disc_all_chrs_cb,NULL);;
  815. }
  816. //通过UUID查找属性
  817. int _bluetooth_BLE_gattc_dis_chrs_by_uuid(PikaObj *self, int conn_handle, int start_handle, int end_handle, uint8_t* uuid,int len){
  818. ble_uuid_any_t uuid_any = {0};
  819. if(len == 2){
  820. uuid_any.u16.u.type = BLE_UUID_TYPE_16;
  821. uuid_any.u16.value = uuid[0] << 8 | uuid[1];
  822. }else if(len == 4){
  823. uuid_any.u32.u.type = BLE_UUID_TYPE_32;
  824. uuid_any.u32.value = uuid[0] << 24 | uuid[1] << 16 | uuid[2] << 8 | uuid[3];
  825. }else{
  826. uuid_any.u128.u.type = BLE_UUID_TYPE_128;
  827. data_inver(uuid,uuid_any.u128.value,16);
  828. }
  829. return ble_gattc_disc_chrs_by_uuid(conn_handle,start_handle,end_handle,&uuid_any.u,ble_gatt_disc_chrs_by_uuid_cb,NULL);
  830. }
  831. //查找全部描述符
  832. int _bluetooth_BLE_gattc_dis_dscs(PikaObj *self, int conn_handle, int start_handle, int end_handle){
  833. return ble_gattc_disc_all_dscs(conn_handle,start_handle,end_handle,ble_gatt_disc_all_dscs_cb,NULL);
  834. }
  835. // GATT读属性、描述符
  836. int _bluetooth_BLE_pyi_gattc_read(PikaObj *self, int conn_handle, int value_handle){
  837. return ble_gattc_read(conn_handle, value_handle,ble_gatt_client_read_cb, NULL);
  838. }
  839. int client_subscribe_vertify_cb(uint16_t conn_handle,
  840. const struct ble_gatt_error *error,
  841. struct ble_gatt_attr *attr,
  842. void *arg){
  843. uint8_t flags = attr->om->om_data[0];
  844. uint8_t data_len = attr->om->om_len;
  845. printf("data = %02x data_len = %d error = %02x\r\n",flags, data_len,error->status);
  846. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTS_SUBSCRIBE,
  847. arg_newObj(New_pikaTupleFrom(
  848. arg_newInt(_IRQ_GATTS_SUBSCRIBE),
  849. arg_newInt(conn_handle),
  850. arg_newInt(attr->handle - 1),
  851. arg_newInt(flags),
  852. arg_newInt(error->status)
  853. )));
  854. return 0;
  855. }
  856. int client_subscribe_cb(uint16_t conn_handle,
  857. const struct ble_gatt_error *error,
  858. struct ble_gatt_attr *attr,
  859. void *arg){
  860. // printf("subscribe callback conn_handle %d value handle error %d\r\n", conn_handle,error->att_handle,error->status);
  861. // printf("ble_gatt_attr_fn handle %d offset %d \r\n", attr->handle,attr->offset);
  862. return ble_gattc_read(conn_handle,attr->handle,client_subscribe_vertify_cb,NULL);
  863. }
  864. int _bluetooth_BLE_pyi_gattc_subscribe(PikaObj *self, int conn_handle, int value_handle, int subscribe){
  865. uint8_t data[2];
  866. data[0] = subscribe;
  867. data[1] = 0x00;
  868. struct os_mbuf * txom = ble_hs_mbuf_from_flat(data,2);
  869. return ble_gattc_write(conn_handle, value_handle + 1 ,txom, client_subscribe_cb, NULL);
  870. }
  871. // int _bluetooth_BLE_pyi_gattc_subscribe_indicate(PikaObj *self, int conn_handle, int value_handle, pika_bool subscribe){
  872. // uint8_t data[2];
  873. // if(subscribe){
  874. // data[0] = 0x02;
  875. // }else{
  876. // data[0] = 0x00;
  877. // }
  878. // data[1] = 0x00;
  879. // struct os_mbuf * txom = ble_hs_mbuf_from_flat(data,2);
  880. // return ble_gattc_write(conn_handle, value_handle + 1 ,txom, client_subscribe_cb, NULL);
  881. // }
  882. // int _bluetooth_BLE_pyi_gattc_subscribe_notify(PikaObj *self, int conn_handle, int value_handle, pika_bool subscribe){
  883. // uint8_t data[2];
  884. // if(subscribe){
  885. // data[0] = 0x01;
  886. // }else{
  887. // data[0] = 0x00;
  888. // }
  889. // data[1] = 0x00;
  890. // struct os_mbuf * txom = ble_hs_mbuf_from_flat(data,2);
  891. // return ble_gattc_write(conn_handle, value_handle + 1,txom,client_subscribe_cb, NULL);
  892. // }
  893. // GATT写属性、描述符
  894. //TODO:还有一个相同功能的函数但不知道区别
  895. // ble_gattc_write_no_rsp(uint16_t conn_handle, uint16_t attr_handle, struct os_mbuf *om)
  896. int _bluetooth_BLE_gattc_write_with_no_rsp(PikaObj *self, int conn_handle, int value_handle, char* data, int data_len){
  897. return ble_gattc_write_no_rsp_flat(conn_handle,value_handle,data,data_len);
  898. }
  899. // GATT写属性、描述符
  900. //TODO:还有一个相同功能的函数但不知道区别
  901. // ble_gattc_write(uint16_t conn_handle, uint16_t attr_handle,struct os_mbuf *txom, ble_gatt_attr_fn *cb, void *cb_arg)
  902. int _bluetooth_BLE_gattc_write_with_rsp(PikaObj *self, int conn_handle, int value_handle, char* data, int data_len){
  903. return ble_gattc_write_flat(conn_handle,value_handle,data,data_len,ble_gatt_client_write_cb,NULL);
  904. }
  905. int _bluetooth_BLE_gatts_chr_updated(PikaObj *self, int chr_val_handle)
  906. {
  907. ble_gatts_chr_updated(chr_val_handle);
  908. return 0;
  909. }
  910. // GATT indicate
  911. int _bluetooth_BLE_pyi_gatts_indicate(PikaObj *self, int conn_handle, int value_handle, uint8_t* data, int data_size)
  912. {
  913. printf("_bluetooth_BLE_pyi_gatts_indicate\r\n");
  914. struct os_mbuf *om = ble_hs_mbuf_from_flat(data, data_size);
  915. if (om == NULL)
  916. {
  917. printf("ble_hs_mbuf_from_flat error\r\n");
  918. }
  919. printf("conn_handle %d value_handle %d data_size %d data %02x\r\n", conn_handle, value_handle, data_size,data[0]);
  920. return ble_gatts_indicate_custom(conn_handle,value_handle,om);
  921. }
  922. int _bluetooth_BLE_pyi_gatts_notify(PikaObj *self, int conn_handle, int value_handle, uint8_t* data, int data_size)
  923. {
  924. printf("_bluetooth_BLE_pyi_gatts_notify\r\n");
  925. struct os_mbuf *om = ble_hs_mbuf_from_flat(data, data_size);
  926. if (om == NULL)
  927. {
  928. printf("ble_hs_mbuf_from_flat error\r\n");
  929. }
  930. printf("conn_handle %d value_handle %d data_size %d data %02x\r\n", conn_handle, value_handle, data_size,data[0]);
  931. return ble_gatts_notify_custom(conn_handle,value_handle,om);
  932. }
  933. int _bluetooth_BLE_pyi_gatts_show_local(PikaObj *self)
  934. {
  935. printf("_bluetooth_BLE_pyi_gatts_show_local");
  936. ble_gatts_show_local();
  937. return 0;
  938. }
  939. // 未找到正确的使用方法与效果
  940. // TODO:待验证
  941. int _bluetooth_BLE_pyi_gattc_exchange_mtu(PikaObj *self, int conn_handle){
  942. return ble_gattc_exchange_mtu(conn_handle,ble_gatt_mtu_exchange_cb,NULL);
  943. }
  944. // 回调函数注册
  945. void _bluetooth_BLE_setCallback(PikaObj *self, Arg* cb)
  946. {
  947. printf("_bluetooth_BLE_setCallback\r\n");
  948. if (g_pika_ble_listener == NULL) {
  949. pika_eventListener_init(&g_pika_ble_listener);
  950. printf("g_pika_ble_listener init\r\n");
  951. }
  952. uint32_t i = 0;
  953. for ( i = 0; i < _IRQ_COUNT + 1; i++){
  954. pika_eventListener_registEventCallback(g_pika_ble_listener,i,cb);
  955. }
  956. for ( i = 101; i < _IRQ_DIY_MAX_ID + 1; i++){
  957. pika_eventListener_registEventCallback(g_pika_ble_listener,i,cb);
  958. }
  959. }
  960. // 基本服务注册
  961. void gatt_svr_init(void)
  962. {
  963. ble_svc_gap_init();
  964. ble_svc_gatt_init();
  965. // ble_svc_ans_init();
  966. }
  967. // 从字符串中读取UUID 结构体
  968. int read_uuid_from_str(char* buf, int len, ble_uuid_any_t* uuid_struct)
  969. {
  970. unsigned int a[16];
  971. if (len == 4){
  972. sscanf(buf, "%02x%02x", &a[0],&a[1]);
  973. uuid_struct->u16.u.type = BLE_UUID_TYPE_16;
  974. uuid_struct->u16.value = (a[0] << 8) | a[1];
  975. return 16;
  976. }
  977. else if (len == 8){
  978. sscanf(buf, "%02x%02x%02x%02x", &a[0],&a[1],&a[2],&a[3]);
  979. uuid_struct->u32.u.type = BLE_UUID_TYPE_32;
  980. uuid_struct->u32.value = (a[0] << 24) | (a[1] << 16) | (a[2] << 8) | a[3];
  981. return 32;
  982. }
  983. else if (len == 36){
  984. sscanf(buf, "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x",\
  985. &a[15],&a[14],&a[13],&a[12],&a[11],&a[10],&a[9],&a[8],&a[7],&a[6],&a[5],&a[4],&a[3],&a[2],&a[1],&a[0]);
  986. // // &a[0],&a[1],&a[2],&a[3],&a[4],&a[5],&a[6],&a[7],&a[8],&a[9],&a[10],&a[11],&a[12],&a[13],&a[14],&a[15]);
  987. uuid_struct->u128.u.type = BLE_UUID_TYPE_128;
  988. memcpy(uuid_struct->u128.value,a,16);
  989. return 128;
  990. }
  991. return 0;
  992. }
  993. // GATT层: 服务端 回调函数
  994. static int ble_gatt_svc_access_cb(uint16_t conn_handle, uint16_t attr_handle,
  995. struct ble_gatt_access_ctxt *ctxt, void *arg){
  996. const ble_uuid_t *uuid;
  997. int rc;
  998. printf("ble_gatt_svc_access_cb\r\n");
  999. switch (ctxt->op) {
  1000. case BLE_GATT_ACCESS_OP_READ_CHR: //读属性值
  1001. if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
  1002. printf("Characteristic read: conn_handle=%d attr_handle=%d\r\n",conn_handle, attr_handle);
  1003. PikaObj * self = arg;
  1004. obj_setArg(self, "data", arg_newObj(New_pikaTupleFrom(
  1005. arg_newInt(_IRQ_GATTS_READ_REQUEST),
  1006. arg_newInt(conn_handle),
  1007. arg_newInt(attr_handle)
  1008. )));
  1009. obj_run(self, "res = _callback(data)");
  1010. Arg* res = obj_getArg(self, "res");
  1011. PikaObj* tuple = arg_getObj(res);
  1012. int res_int = arg_getInt(pikaTuple_getArg(tuple,0));
  1013. int byte_count = arg_getInt(pikaTuple_getArg(tuple,1));
  1014. uint8_t * value = (uint8_t * )calloc(byte_count,sizeof(uint8_t));
  1015. value = arg_getBytes(pikaTuple_getArg(tuple,2));
  1016. printf("res_int %d byte_count %d data %02x\r\n",res_int,byte_count,value[0]);
  1017. // int res_int = arg_getInt(res);
  1018. if(res_int == _GATTS_NO_ERROR){
  1019. rc = os_mbuf_append(ctxt->om, value,byte_count);
  1020. return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  1021. }
  1022. return 0;
  1023. } else {
  1024. printf("Characteristic read by NimBLE stack; attr_handle=%d\n",attr_handle);
  1025. PikaObj * self = arg;
  1026. obj_setArg(self, "data", arg_newObj(New_pikaTupleFrom(
  1027. arg_newInt(_IRQ_DIY_NIMBLE_READ),
  1028. arg_newInt(attr_handle)
  1029. )));
  1030. obj_run(self, "res = _callback(data)");
  1031. Arg* res = obj_getArg(self, "res");
  1032. PikaObj* tuple = arg_getObj(res);
  1033. int byte_count = arg_getInt(pikaTuple_getArg(tuple,0));
  1034. uint8_t * value = (uint8_t * )calloc(byte_count,sizeof(uint8_t));
  1035. value = arg_getBytes(pikaTuple_getArg(tuple,1));
  1036. printf("byte_count %d data %02x\r\n",byte_count,value[0]);
  1037. rc = os_mbuf_append(ctxt->om,value,byte_count);
  1038. return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  1039. }
  1040. return -1;
  1041. case BLE_GATT_ACCESS_OP_WRITE_CHR: //写属性值
  1042. if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
  1043. printf("Characteristic write; conn_handle=%d attr_handle=%d\r\n", conn_handle, attr_handle);
  1044. uint16_t total_length = *(ctxt->om->om_databuf);
  1045. uint8_t * value = (uint8_t*)calloc(total_length,sizeof(uint8_t));
  1046. // uint16_t current_buf_length = ctxt->om->om_len;
  1047. uint16_t current_buf_length;
  1048. uint16_t remain_length = total_length;
  1049. struct os_mbuf* buf_pointer = ctxt->om;
  1050. while (true){
  1051. current_buf_length = buf_pointer->om_len;
  1052. memcpy((value + (total_length - remain_length)), buf_pointer->om_data,current_buf_length);
  1053. remain_length -= current_buf_length;
  1054. if(remain_length > 0)
  1055. buf_pointer = buf_pointer->om_next.sle_next;
  1056. else
  1057. break;
  1058. }
  1059. // printf("ctxt->om->om_pkthdr_len = %d\r\n",ctxt->om->om_pkthdr_len);
  1060. // printf("ctxt->om->om_len = %d\r\n",ctxt->om->om_next.sle_next->om_len);
  1061. // printf("ctxt->om->om_databuf[0] = %02x\r\n",*(ctxt->om->om_databuf));
  1062. // printf("ctxt->om->om_databuf[1] = %02x\r\n",*(ctxt->om->om_databuf+1));
  1063. // printf("ctxt->om->om_databuf[2] = %02x\r\n",*(ctxt->om->om_databuf+2));
  1064. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTS_WRITE,
  1065. arg_newObj(New_pikaTupleFrom(
  1066. arg_newInt(_IRQ_GATTS_WRITE),
  1067. arg_newInt(conn_handle),
  1068. arg_newInt(attr_handle),
  1069. arg_newBytes(value,total_length)
  1070. )));
  1071. } else {
  1072. printf("Characteristic write by NimBLE stack; attr_handle=%d\r\n",attr_handle);
  1073. }
  1074. return 0;
  1075. case BLE_GATT_ACCESS_OP_READ_DSC: //读描述符(回调函数与属性值先不做区分) TODO:读不到描述符吧?
  1076. if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
  1077. printf("Descriptor read; conn_handle=%d attr_handle=%d\r\n",conn_handle, attr_handle);
  1078. Arg* res = pika_eventListener_sendAwaitResult(g_pika_ble_listener,_IRQ_GATTS_READ_REQUEST,
  1079. arg_newObj(New_pikaTupleFrom(
  1080. arg_newInt(_IRQ_GATTS_READ_REQUEST),
  1081. arg_newInt(conn_handle),
  1082. arg_newInt(attr_handle)
  1083. )));
  1084. int res_int = arg_getInt(res);
  1085. if(res_int == _GATTS_NO_ERROR){ //允许读,如何返回被拒绝访问解决结果?
  1086. uint8_t gatt_svr_dsc_val = 0xFF;
  1087. rc = os_mbuf_append(ctxt->om,
  1088. &gatt_svr_dsc_val,
  1089. sizeof(gatt_svr_dsc_val));
  1090. return rc == 0 ? 0 : BLE_ATT_ERR_INSUFFICIENT_RES;
  1091. }
  1092. } else {
  1093. printf("Descriptor read by NimBLE stack; attr_handle=%d\r\n",attr_handle);
  1094. }
  1095. return 0;
  1096. case BLE_GATT_ACCESS_OP_WRITE_DSC: //写描述符
  1097. if (conn_handle != BLE_HS_CONN_HANDLE_NONE) {
  1098. printf("Descriptor write; conn_handle=%d attr_handle=%d\r\n",conn_handle, attr_handle);
  1099. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTS_WRITE,
  1100. arg_newObj(New_pikaTupleFrom(
  1101. arg_newInt(_IRQ_GATTS_WRITE),
  1102. arg_newInt(conn_handle),
  1103. arg_newInt(attr_handle)
  1104. )));
  1105. } else {
  1106. printf("Descriptor read by NimBLE stack; attr_handle=%d\r\n",attr_handle);
  1107. }
  1108. return 0;
  1109. }
  1110. return 0;
  1111. }
  1112. // GATT层:客户端读服务回调函数
  1113. static int ble_gatt_client_read_cb(uint16_t conn_handle,
  1114. const struct ble_gatt_error *error,
  1115. struct ble_gatt_attr *attr,
  1116. void *arg)
  1117. {
  1118. printf("Read complete for the subscribable characteristic; "
  1119. "status=%d conn_handle=%d\r\n", error->status, conn_handle);
  1120. //读取成功
  1121. // TODO:读完之后不会引起其他事件,所以要done 和result一起触发
  1122. if (error->status == 0) {
  1123. //读到数据
  1124. // printf("om_data:%d\r\n",*(attr->om->om_data));
  1125. // printf("om_flags:%d\r\n",attr->om->om_flags);
  1126. // printf("om_pkthdr_len:%d\r\n",attr->om->om_pkthdr_len);
  1127. // printf("om_data:%d\r\n",attr->om->om_len);
  1128. // print_mbuf(attr->om); //TODO:该函数无引用,但在blecentn能够使用
  1129. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_READ_RESULT,
  1130. arg_newObj(New_pikaTupleFrom(
  1131. arg_newInt(_IRQ_GATTC_READ_RESULT),
  1132. arg_newInt(conn_handle),
  1133. arg_newInt(attr->handle), //应该是value handle
  1134. arg_newBytes(attr->om->om_data,attr->om->om_len)
  1135. )));
  1136. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_READ_DONE ,
  1137. arg_newObj(New_pikaTupleFrom(
  1138. arg_newInt(_IRQ_GATTC_READ_DONE),
  1139. arg_newInt(conn_handle),
  1140. arg_newInt(attr->handle),
  1141. arg_newInt(error->status)
  1142. )));
  1143. }else{
  1144. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_READ_DONE ,
  1145. arg_newObj(New_pikaTupleFrom(
  1146. arg_newInt(_IRQ_GATTC_READ_DONE),
  1147. arg_newInt(conn_handle),
  1148. arg_newInt(-1),
  1149. arg_newInt(error->status)
  1150. )));
  1151. }
  1152. return 0;
  1153. }
  1154. // GATT层:客户端写服务回调函数
  1155. static int ble_gatt_client_write_cb(uint16_t conn_handle,
  1156. const struct ble_gatt_error *error,
  1157. struct ble_gatt_attr *attr,
  1158. void *arg)
  1159. {
  1160. const struct peer_chr *chr;
  1161. const struct peer *peer;
  1162. int rc;
  1163. MODLOG_DFLT(INFO,
  1164. "Write to the custom subscribable characteristic complete; "
  1165. "status=%d conn_handle=%d attr_handle=%d\n",
  1166. error->status, conn_handle, attr->handle);
  1167. if (error->status == 0) {
  1168. //读到数据
  1169. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_WRITE_DONE,
  1170. arg_newObj(New_pikaTupleFrom(
  1171. arg_newInt(_IRQ_GATTC_WRITE_DONE ),
  1172. arg_newInt(conn_handle),
  1173. arg_newInt(attr->handle),
  1174. arg_newInt(error->status)
  1175. )));
  1176. }else{
  1177. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_WRITE_DONE,
  1178. arg_newObj(New_pikaTupleFrom(
  1179. arg_newInt(_IRQ_GATTC_WRITE_DONE),
  1180. arg_newInt(conn_handle),
  1181. arg_newInt(-1),
  1182. arg_newInt(error->status)
  1183. )));
  1184. }
  1185. return 0;
  1186. }
  1187. //GATT层 MTU exchange 回调函数
  1188. static int ble_gatt_mtu_exchange_cb(uint16_t conn_handle,
  1189. const struct ble_gatt_error *error,
  1190. uint16_t mtu, void *arg)
  1191. {
  1192. if(error->status == 0){
  1193. pika_eventListener_send(g_pika_ble_listener,_IRQ_MTU_EXCHANGED ,
  1194. arg_newObj(New_pikaTupleFrom(
  1195. arg_newInt(_IRQ_MTU_EXCHANGED),
  1196. arg_newInt(conn_handle),
  1197. arg_newInt(mtu)
  1198. )));
  1199. }
  1200. return 0;
  1201. }
  1202. // GAP层:广播事件回调函数
  1203. static int ble_gap_event_cb(struct ble_gap_event *event, void *arg)
  1204. {
  1205. struct ble_gap_conn_desc desc;
  1206. int rc;
  1207. uint8_t addr[6];
  1208. switch (event->type) {
  1209. case BLE_GAP_EVENT_CONNECT:
  1210. printf("connection %s; status=%d ",
  1211. event->connect.status == 0 ? "established" : "failed", event->connect.status);
  1212. printf("\r\n");
  1213. if (event->connect.status == 0) {
  1214. rc = ble_gap_conn_find(event->connect.conn_handle, &desc);
  1215. assert(rc == 0);
  1216. print_conn_desc(&desc);
  1217. data_inver(desc.peer_ota_addr.val,&addr,6);
  1218. if(desc.role == BLE_GAP_ROLE_SLAVE){
  1219. pika_eventListener_send(g_pika_ble_listener,_IRQ_CENTRAL_CONNECT,
  1220. arg_newObj(New_pikaTupleFrom(
  1221. arg_newInt(_IRQ_CENTRAL_CONNECT),
  1222. arg_newInt(event->connect.conn_handle),
  1223. arg_newInt(desc.peer_id_addr.type),
  1224. arg_newBytes(addr,6)
  1225. )));
  1226. } else if (desc.role == BLE_GAP_ROLE_MASTER){
  1227. pika_eventListener_send(g_pika_ble_listener,_IRQ_PERIPHERAL_CONNECT ,
  1228. arg_newObj(New_pikaTupleFrom(
  1229. arg_newInt(_IRQ_PERIPHERAL_CONNECT),
  1230. arg_newInt(event->connect.conn_handle),
  1231. arg_newInt(desc.peer_id_addr.type),
  1232. arg_newBytes(addr,6)
  1233. )));
  1234. }
  1235. }else if (event->connect.status != 0) {
  1236. /* Connection failed; resume advertising. */
  1237. // bleprph_advertise();
  1238. // TODO: 重新广播,或通知客户端
  1239. if(desc.role == BLE_GAP_ROLE_SLAVE){
  1240. _bluetooth_BLE_adv(g_interval);
  1241. printf("Connection failed; resume advertising.");\
  1242. }
  1243. }
  1244. return 0;
  1245. case BLE_GAP_EVENT_DISCONNECT: //断开连接
  1246. printf("disconnect; reason=%d \r\n", event->disconnect.reason);
  1247. // print_conn_desc(&event->disconnect.conn);
  1248. data_inver(event->disconnect.conn.peer_ota_addr.val,&addr,6);
  1249. if(event->disconnect.conn.role == BLE_GAP_ROLE_SLAVE){
  1250. pika_eventListener_send(g_pika_ble_listener,_IRQ_CENTRAL_DISCONNECT,
  1251. arg_newObj(New_pikaTupleFrom(
  1252. arg_newInt(_IRQ_CENTRAL_DISCONNECT),
  1253. arg_newInt(event->disconnect.conn.conn_handle),
  1254. arg_newInt(desc.peer_id_addr.type),
  1255. arg_newBytes(addr,6)
  1256. )));
  1257. }else if(event->disconnect.conn.role == BLE_GAP_ROLE_MASTER){
  1258. pika_eventListener_send(g_pika_ble_listener,_IRQ_PERIPHERAL_DISCONNECT ,
  1259. arg_newObj(New_pikaTupleFrom(
  1260. arg_newInt(_IRQ_PERIPHERAL_DISCONNECT),
  1261. arg_newInt(event->disconnect.conn.conn_handle),
  1262. arg_newInt(desc.peer_id_addr.type),
  1263. arg_newBytes(addr,6)
  1264. )));
  1265. }
  1266. return 0;
  1267. case BLE_GAP_EVENT_CONN_UPDATE: //返回结果
  1268. /* The central has updated the connection parameters. */
  1269. printf("connection updated; status=%d \r\n",event->conn_update.status);
  1270. rc = ble_gap_conn_find(event->conn_update.conn_handle, &desc);
  1271. assert(rc == 0);
  1272. print_conn_desc(&desc);
  1273. return 0;
  1274. case BLE_GAP_EVENT_CONN_UPDATE_REQ :
  1275. // MicroPython : conn_handle, conn_interval, conn_latency, supervision_timeout, status
  1276. printf("BLE_GAP_EVENT_CONN_UPDATE_REQ\r\n");
  1277. pika_eventListener_send(g_pika_ble_listener,_IRQ_CONNECTION_UPDATE,
  1278. arg_newObj(New_pikaTupleFrom(
  1279. arg_newInt(_IRQ_CONNECTION_UPDATE),
  1280. arg_newInt(event->conn_update_req.conn_handle),
  1281. arg_newInt(event->conn_update_req.peer_params->itvl_min),
  1282. arg_newInt(event->conn_update_req.peer_params->latency),
  1283. arg_newInt(event->conn_update_req.peer_params->supervision_timeout)//,
  1284. // arg_newInt(event->conn_update.status) TODO:status在上一事件中
  1285. )));
  1286. return 0;
  1287. case BLE_GAP_EVENT_L2CAP_UPDATE_REQ :
  1288. printf("BLE_GAP_EVENT_L2CAP_UPDATE_REQ\r\n");
  1289. return 0;
  1290. case BLE_GAP_EVENT_TERM_FAILURE:
  1291. printf("BLE_GAP_EVENT_TERM_FAILURE; conn_handle=%d reason=%d\r\n",event->term_failure.conn_handle,event->term_failure.status);
  1292. return 0;
  1293. case BLE_GAP_EVENT_DISC: //扫描发现
  1294. struct ble_gap_conn_desc desc;
  1295. struct ble_hs_adv_fields fields;
  1296. int rc;
  1297. rc = ble_hs_adv_parse_fields(&fields, event->disc.data,event->disc.length_data);
  1298. if (rc != 0) {
  1299. return 0;
  1300. }
  1301. data_inver(event->disc.addr.val,addr,6);
  1302. pika_eventListener_send(g_pika_ble_listener,_IRQ_SCAN_RESULT,
  1303. arg_newObj(New_pikaTupleFrom(
  1304. arg_newInt(_IRQ_SCAN_RESULT),
  1305. arg_newInt(event->disc.addr.type),
  1306. arg_newBytes(addr,6),
  1307. arg_newInt(event->disc.event_type),
  1308. arg_newInt(event->disc.rssi),
  1309. arg_newBytes(event->disc.data,event->disc.length_data)
  1310. )));
  1311. return 0;
  1312. case BLE_GAP_EVENT_DISC_COMPLETE: // 扫描结束
  1313. // MicroPython None
  1314. printf("discovery complete; reason=%d\r\n",event->disc_complete.reason);
  1315. pika_eventListener_send(g_pika_ble_listener,_IRQ_SCAN_DONE,
  1316. arg_newObj(New_pikaTupleFrom(
  1317. arg_newInt(_IRQ_SCAN_DONE),
  1318. arg_newInt(event->disc_complete.reason)
  1319. )));
  1320. return 0;
  1321. case BLE_GAP_EVENT_ADV_COMPLETE: //广播完成
  1322. // MicroPython 没有这个事件
  1323. printf("advertise complete; reason=%d\r\n",event->adv_complete.reason);
  1324. return 0;
  1325. case BLE_GAP_EVENT_ENC_CHANGE:
  1326. // 暂时不理
  1327. /* Encryption has been enabled or disabled for this connection. */
  1328. printf("encryption change event; status=%d \r\n",event->enc_change.status);
  1329. rc = ble_gap_conn_find(event->enc_change.conn_handle, &desc);
  1330. assert(rc == 0);
  1331. print_conn_desc(&desc);
  1332. MODLOG_DFLT(INFO, "\n");
  1333. return 0;
  1334. case BLE_GAP_EVENT_PASSKEY_ACTION :
  1335. // 暂时不理
  1336. printf("PASSKEY_ACTION_EVENT started \r\n");
  1337. struct ble_sm_io pkey = {0};
  1338. // int key = 0;
  1339. if (event->passkey.params.action == BLE_SM_IOACT_DISP) {
  1340. pkey.action = event->passkey.params.action;
  1341. pkey.passkey = 123456; // This is the passkey to be entered on peer
  1342. ESP_LOGI(tag, "Enter passkey %" PRIu32 "on the peer side", pkey.passkey);
  1343. rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
  1344. ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc);
  1345. } else if (event->passkey.params.action == BLE_SM_IOACT_NUMCMP) {
  1346. ESP_LOGI(tag, "Passkey on device's display: %" PRIu32 , event->passkey.params.numcmp);
  1347. ESP_LOGI(tag, "Accept or reject the passkey through console in this format -> key Y or key N");
  1348. pkey.action = event->passkey.params.action;
  1349. // if (scli_receive_key(&key)) {
  1350. // pkey.numcmp_accept = key;
  1351. // } else {
  1352. // pkey.numcmp_accept = 0;
  1353. // ESP_LOGE(tag, "Timeout! Rejecting the key");
  1354. // }
  1355. rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
  1356. ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc);
  1357. } else if (event->passkey.params.action == BLE_SM_IOACT_OOB) {
  1358. static uint8_t tem_oob[16] = {0};
  1359. pkey.action = event->passkey.params.action;
  1360. for (int i = 0; i < 16; i++) {
  1361. pkey.oob[i] = tem_oob[i];
  1362. }
  1363. rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
  1364. ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc);
  1365. } else if (event->passkey.params.action == BLE_SM_IOACT_INPUT) {
  1366. ESP_LOGI(tag, "Enter the passkey through console in this format-> key 123456");
  1367. pkey.action = event->passkey.params.action;
  1368. // if (scli_receive_key(&key)) {
  1369. // pkey.passkey = key;
  1370. // } else {
  1371. // pkey.passkey = 0;
  1372. // ESP_LOGE(tag, "Timeout! Passing 0 as the key");
  1373. // }
  1374. rc = ble_sm_inject_io(event->passkey.conn_handle, &pkey);
  1375. ESP_LOGI(tag, "ble_sm_inject_io result: %d\n", rc);
  1376. }
  1377. return 0;
  1378. case BLE_GAP_EVENT_NOTIFY_RX: // 客户端
  1379. printf("received %s; conn_handle=%d attr_handle=%d attr_len=%d",
  1380. event->notify_rx.indication ? "indication" : "notification",
  1381. event->notify_rx.conn_handle,
  1382. event->notify_rx.attr_handle,
  1383. OS_MBUF_PKTLEN(event->notify_rx.om));
  1384. printf("\r\n");
  1385. if(event->notify_rx.indication == 1){ // indication
  1386. // MicroPython : conn_handle, value_handle, notify_data
  1387. uint16_t len = event->notify_rx.om->om_len;
  1388. char *indic_str = (char *)malloc(len + 1);
  1389. memcpy(indic_str, event->notify_rx.om->om_data, len);
  1390. indic_str[len] = '\0';
  1391. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_INDICATE,
  1392. arg_newObj(New_pikaTupleFrom(
  1393. arg_newInt(_IRQ_GATTC_INDICATE),
  1394. arg_newInt(event->notify_rx.conn_handle),
  1395. arg_newInt(event->notify_rx.attr_handle),
  1396. arg_newStr(indic_str)
  1397. )));
  1398. free(indic_str);
  1399. }
  1400. else { // notify
  1401. uint16_t len = event->notify_rx.om->om_len;
  1402. char *indic_str = (char *)malloc(len + 1);
  1403. memcpy(indic_str, event->notify_rx.om->om_data, len);
  1404. indic_str[len] = '\0';
  1405. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_NOTIFY,
  1406. arg_newObj(New_pikaTupleFrom(
  1407. arg_newInt(_IRQ_GATTC_NOTIFY),
  1408. arg_newInt(event->notify_rx.conn_handle),
  1409. arg_newInt(event->notify_rx.attr_handle),
  1410. arg_newStr(indic_str)
  1411. )));
  1412. free(indic_str);
  1413. }
  1414. return 0;
  1415. case BLE_GAP_EVENT_NOTIFY_TX: //通知发送完成
  1416. printf("notify_tx event conn_handle=%d attr_handle=%d status=%d is_indication=%d\r\n",
  1417. event->notify_tx.conn_handle,
  1418. event->notify_tx.attr_handle,
  1419. event->notify_tx.status,
  1420. event->notify_tx.indication);
  1421. if(event->notify_tx.indication == 1){ // indication
  1422. // MicroPython : conn_handle, value_handle, notify_data
  1423. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTS_INDICATE_DONE,
  1424. arg_newObj(New_pikaTupleFrom(
  1425. arg_newInt(_IRQ_GATTS_INDICATE_DONE),
  1426. arg_newInt(event->notify_tx.conn_handle),
  1427. arg_newInt(event->notify_tx.attr_handle),
  1428. arg_newInt(event->notify_tx.status)
  1429. )));
  1430. }
  1431. return 0;
  1432. case BLE_GAP_EVENT_SUBSCRIBE://订阅 客户端向服务端订阅的事件(新增)
  1433. printf("subscribe event; conn_handle=%d attr_handle=%d "
  1434. "reason=%d prevn=%d curn=%d previ=%d curi=%d\r\n",
  1435. event->subscribe.conn_handle,
  1436. event->subscribe.attr_handle,
  1437. event->subscribe.reason,
  1438. event->subscribe.prev_notify,
  1439. event->subscribe.cur_notify,
  1440. event->subscribe.prev_indicate,
  1441. event->subscribe.cur_indicate);
  1442. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_SUBSCRIBE,
  1443. arg_newObj(New_pikaTupleFrom(
  1444. arg_newInt(_IRQ_GATTC_SUBSCRIBE),
  1445. arg_newInt(event->subscribe.conn_handle),
  1446. arg_newInt(event->subscribe.attr_handle),
  1447. arg_newInt(event->subscribe.reason),
  1448. arg_newInt(event->subscribe.cur_notify),
  1449. arg_newInt(event->subscribe.cur_indicate)
  1450. )));
  1451. return 0;
  1452. case BLE_GAP_EVENT_MTU:
  1453. printf("mtu update event; conn_handle=%d cid=%d mtu=%d\r\n",
  1454. event->mtu.conn_handle,
  1455. event->mtu.channel_id,
  1456. event->mtu.value);
  1457. return 0;
  1458. case BLE_GAP_EVENT_IDENTITY_RESOLVED:
  1459. printf("BLE_GAP_EVENT_IDENTITY_RESOLVED\r\n");
  1460. return 0;
  1461. case BLE_GAP_EVENT_REPEAT_PAIRING:
  1462. /* We already have a bond with the peer, but it is attempting to
  1463. * establish a new secure link. This app sacrifices security for
  1464. * convenience: just throw away the old bond and accept the new link.
  1465. */
  1466. printf("BLE_GAP_EVENT_REPEAT_PAIRING\r\n");
  1467. /* Delete the old bond. */
  1468. rc = ble_gap_conn_find(event->repeat_pairing.conn_handle, &desc);
  1469. assert(rc == 0);
  1470. ble_store_util_delete_peer(&desc.peer_id_addr);
  1471. /* Return BLE_GAP_REPEAT_PAIRING_RETRY to indicate that the host should
  1472. * continue with the pairing operation.
  1473. */
  1474. return BLE_GAP_REPEAT_PAIRING_RETRY;
  1475. case BLE_GAP_EVENT_PHY_UPDATE_COMPLETE:
  1476. printf("BLE_GAP_EVENT_PHY_UPDATE_COMPLETE\r\n");
  1477. return 0;
  1478. case BLE_GAP_EVENT_EXT_DISC:
  1479. printf("BLE_GAP_EVENT_EXT_DISC\r\n");
  1480. return 0;
  1481. case BLE_GAP_EVENT_PERIODIC_SYNC:
  1482. printf("BLE_GAP_EVENT_PERIODIC_SYNC\r\n");
  1483. return 0;
  1484. case BLE_GAP_EVENT_PERIODIC_REPORT:
  1485. printf("BLE_GAP_EVENT_PERIODIC_REPORT\r\n");
  1486. return 0;
  1487. case BLE_GAP_EVENT_PERIODIC_SYNC_LOST:
  1488. printf("BLE_GAP_EVENT_PERIODIC_SYNC_LOST\r\n");
  1489. return 0;
  1490. case BLE_GAP_EVENT_SCAN_REQ_RCVD:
  1491. printf("BLE_GAP_EVENT_SCAN_REQ_RCVD\r\n");
  1492. return 0;
  1493. case BLE_GAP_EVENT_PERIODIC_TRANSFER:
  1494. printf("BLE_GAP_EVENT_PERIODIC_TRANSFER\r\n");
  1495. return 0;
  1496. case BLE_GAP_EVENT_PATHLOSS_THRESHOLD:
  1497. printf("BLE_GAP_EVENT_PATHLOSS_THRESHOLD\r\n");
  1498. return 0;
  1499. case BLE_GAP_EVENT_TRANSMIT_POWER:
  1500. printf("BLE_GAP_EVENT_TRANSMIT_POWER\r\n");
  1501. return 0;
  1502. case BLE_GAP_EVENT_SUBRATE_CHANGE:// 这个是啥事件?
  1503. printf("BLE_GAP_EVENT_SUBRATE_CHANGE\r\n");
  1504. return 0;
  1505. }
  1506. return 0;
  1507. }
  1508. // GATT 查找所有服务回调函数
  1509. static int ble_gatt_disc_all_svcs_cb(uint16_t conn_handle,
  1510. const struct ble_gatt_error *error,
  1511. const struct ble_gatt_svc *service,
  1512. void *arg){
  1513. printf("ble_gatt_disc_all_svcs_cb\r\n");
  1514. if(error->status == 0)
  1515. {
  1516. uint8_t data_len = service->uuid.u.type;
  1517. uint8_t *uuid;
  1518. if(data_len == 16){ // = 16
  1519. uint16_t value = service->uuid.u16.value;
  1520. uuid = (uint8_t *)malloc(2);
  1521. uuid[0] = (uint8_t)(value >> 8); // 取高位字节
  1522. uuid[1] = (uint8_t) value; // 取低位字节
  1523. }else if(data_len == 32){ // 32
  1524. uint16_t value = service->uuid.u32.value;
  1525. uuid = (uint8_t *)malloc(4);
  1526. uuid[0] = (uint8_t)(value >> 24); // 取高位字节
  1527. uuid[1] = (uint8_t)(value >> 16); // 取次高位字节
  1528. uuid[2] = (uint8_t)(value >> 8); // 取次低位字节
  1529. uuid[3] = (uint8_t)value; // 取低位字节
  1530. }else{ // 128
  1531. uuid = (uint8_t *)malloc(16);
  1532. data_inver(service->uuid.u128.value,uuid,16);
  1533. }
  1534. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_SERVICE_RESULT,
  1535. arg_newObj(New_pikaTupleFrom(
  1536. arg_newInt(_IRQ_GATTC_SERVICE_RESULT),
  1537. arg_newInt(conn_handle),
  1538. arg_newInt(service->start_handle),
  1539. arg_newInt(service->end_handle),
  1540. arg_newBytes(uuid,data_len/8)
  1541. )));
  1542. free(uuid);
  1543. return 0;
  1544. }else if(error->status == 14){
  1545. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_SERVICE_DONE,
  1546. arg_newObj(New_pikaTupleFrom(
  1547. arg_newInt(_IRQ_GATTC_SERVICE_DONE),
  1548. arg_newInt(conn_handle),
  1549. arg_newInt(0)
  1550. )));
  1551. return 0;
  1552. }else{
  1553. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_SERVICE_DONE,
  1554. arg_newObj(New_pikaTupleFrom(
  1555. arg_newInt(_IRQ_GATTC_SERVICE_DONE),
  1556. arg_newInt(conn_handle),
  1557. arg_newInt(error->status)
  1558. )));
  1559. return 0;
  1560. }
  1561. }
  1562. // GATT 查找特定UUID服务回调函数
  1563. static int ble_gatt_disc_svcs_by_uuid_cb(uint16_t conn_handle,
  1564. const struct ble_gatt_error *error,
  1565. const struct ble_gatt_svc *service,
  1566. void *arg){
  1567. printf("ble_gatt_disc_svcs_by_uuid_cb\r\n");
  1568. printf("error: %d,att handle %d\r\n",error->status,error->att_handle);
  1569. // printf("start handle: %d,end handle %d\r\n",service->start_handle,service->end_handle);
  1570. if(error->status == 0)
  1571. {
  1572. uint8_t data_len = service->uuid.u.type;
  1573. uint8_t *uuid;
  1574. if(data_len == 16){ // = 16
  1575. uint16_t value = service->uuid.u16.value;
  1576. uuid = (uint8_t *)malloc(2);
  1577. uuid[0] = (uint8_t)(value >> 8); // 取高位字节
  1578. uuid[1] = (uint8_t) value; // 取低位字节
  1579. }else if(data_len == 32){ // 32
  1580. uint16_t value = service->uuid.u32.value;
  1581. uuid = (uint8_t *)malloc(4);
  1582. uuid[0] = (uint8_t)(value >> 24); // 取高位字节
  1583. uuid[1] = (uint8_t)(value >> 16); // 取次高位字节
  1584. uuid[2] = (uint8_t)(value >> 8); // 取次低位字节
  1585. uuid[3] = (uint8_t)value; // 取低位字节
  1586. }else{ // 128
  1587. uuid = (uint8_t *)malloc(16);
  1588. data_inver(service->uuid.u128.value,uuid,16);
  1589. }
  1590. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_SERVICE_RESULT,
  1591. arg_newObj(New_pikaTupleFrom(
  1592. arg_newInt(_IRQ_GATTC_SERVICE_RESULT),
  1593. arg_newInt(conn_handle),
  1594. arg_newInt(service->start_handle),
  1595. arg_newInt(service->end_handle),
  1596. arg_newBytes(uuid,data_len/8)
  1597. )));
  1598. free(uuid);
  1599. return 0;
  1600. }else if(error->status == 14){
  1601. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_SERVICE_DONE,
  1602. arg_newObj(New_pikaTupleFrom(
  1603. arg_newInt(_IRQ_GATTC_SERVICE_DONE),
  1604. arg_newInt(conn_handle),
  1605. arg_newInt(0)
  1606. )));
  1607. return 0;
  1608. }else{
  1609. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_SERVICE_DONE,
  1610. arg_newObj(New_pikaTupleFrom(
  1611. arg_newInt(_IRQ_GATTC_SERVICE_DONE),
  1612. arg_newInt(conn_handle),
  1613. arg_newInt(error->status)
  1614. )));
  1615. return 0;
  1616. }
  1617. }
  1618. // GATT 查找所有特性回调函数
  1619. static int ble_gatt_disc_all_chrs_cb(uint16_t conn_handle,
  1620. const struct ble_gatt_error *error,
  1621. const struct ble_gatt_chr *chr, void *arg){
  1622. printf("ble_gatt_disc_all_chrs_cb\r\n");
  1623. if(error->status == 0){
  1624. uint8_t data_len = chr->uuid.u.type;
  1625. uint8_t *uuid;
  1626. if(data_len == 16){ // = 16
  1627. uint16_t value = chr->uuid.u16.value;
  1628. uuid = (uint8_t *)malloc(2);
  1629. uuid[0] = (uint8_t)(value >> 8); // 取高位字节
  1630. uuid[1] = (uint8_t) value; // 取低位字节
  1631. }else if(data_len == 32){ // 32
  1632. uint16_t value = chr->uuid.u32.value;
  1633. uuid = (uint8_t *)malloc(4);
  1634. uuid[0] = (uint8_t)(value >> 24); // 取高位字节
  1635. uuid[1] = (uint8_t)(value >> 16); // 取次高位字节
  1636. uuid[2] = (uint8_t)(value >> 8); // 取次低位字节
  1637. uuid[3] = (uint8_t)value; // 取低位字节
  1638. }else{ // 128
  1639. uuid = (uint8_t *)malloc(16);
  1640. data_inver(chr->uuid.u128.value,uuid,16);
  1641. }
  1642. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_CHARACTERISTIC_RESULT,
  1643. arg_newObj(New_pikaTupleFrom(
  1644. arg_newInt(_IRQ_GATTC_CHARACTERISTIC_RESULT),
  1645. arg_newInt(conn_handle),
  1646. arg_newInt(chr->def_handle), // 定义的句柄
  1647. arg_newInt(chr->val_handle), // 值的句柄
  1648. arg_newInt(chr->properties),
  1649. arg_newBytes(uuid,data_len/8)
  1650. )));
  1651. free(uuid);
  1652. return 0;
  1653. }else if(error->status == 14){
  1654. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_CHARACTERISTIC_DONE,
  1655. arg_newObj(New_pikaTupleFrom(
  1656. arg_newInt(_IRQ_GATTC_CHARACTERISTIC_DONE),
  1657. arg_newInt(conn_handle),
  1658. arg_newInt(0)
  1659. )));
  1660. return 0;
  1661. }else{
  1662. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_CHARACTERISTIC_DONE,
  1663. arg_newObj(New_pikaTupleFrom(
  1664. arg_newInt(_IRQ_GATTC_CHARACTERISTIC_DONE),
  1665. arg_newInt(conn_handle),
  1666. arg_newInt(error->status)
  1667. )));
  1668. return 0;
  1669. }
  1670. }
  1671. // GATT 查找特定UUID特性回调函数
  1672. static int ble_gatt_disc_chrs_by_uuid_cb(uint16_t conn_handle,
  1673. const struct ble_gatt_error *error,
  1674. const struct ble_gatt_chr *chr, void *arg){
  1675. printf("ble_gatt_disc_chrs_by_uuid_cb\r\n");
  1676. if(error->status == 0){
  1677. uint8_t data_len = chr->uuid.u.type;
  1678. uint8_t *uuid;
  1679. if(data_len == 16){ // = 16
  1680. uint16_t value = chr->uuid.u16.value;
  1681. uuid = (uint8_t *)malloc(2);
  1682. uuid[0] = (uint8_t)(value >> 8); // 取高位字节
  1683. uuid[1] = (uint8_t) value; // 取低位字节
  1684. }else if(data_len == 32){ // 32
  1685. uint16_t value = chr->uuid.u32.value;
  1686. uuid = (uint8_t *)malloc(4);
  1687. uuid[0] = (uint8_t)(value >> 24); // 取高位字节
  1688. uuid[1] = (uint8_t)(value >> 16); // 取次高位字节
  1689. uuid[2] = (uint8_t)(value >> 8); // 取次低位字节
  1690. uuid[3] = (uint8_t)value; // 取低位字节
  1691. }else{ // 128
  1692. uuid = (uint8_t *)malloc(16);
  1693. data_inver(chr->uuid.u128.value,uuid,16);
  1694. }
  1695. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_CHARACTERISTIC_RESULT,
  1696. arg_newObj(New_pikaTupleFrom(
  1697. arg_newInt(_IRQ_GATTC_CHARACTERISTIC_RESULT),
  1698. arg_newInt(conn_handle),
  1699. arg_newInt(chr->def_handle),
  1700. arg_newInt(chr->val_handle),
  1701. arg_newInt(chr->properties),
  1702. arg_newBytes(uuid,data_len/8)
  1703. )));
  1704. free(uuid);
  1705. return 0;
  1706. }else if(error->status == 14){
  1707. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_CHARACTERISTIC_DONE,
  1708. arg_newObj(New_pikaTupleFrom(
  1709. arg_newInt(_IRQ_GATTC_CHARACTERISTIC_DONE),
  1710. arg_newInt(conn_handle),
  1711. arg_newInt(0)
  1712. )));
  1713. return 0;
  1714. }else{
  1715. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_CHARACTERISTIC_DONE,
  1716. arg_newObj(New_pikaTupleFrom(
  1717. arg_newInt(_IRQ_GATTC_CHARACTERISTIC_DONE),
  1718. arg_newInt(conn_handle),
  1719. arg_newInt(error->status)
  1720. )));
  1721. return 0;
  1722. }
  1723. }
  1724. // GATT 查找所有描述符回调函数
  1725. // TODO:把描述符也显示了
  1726. static int ble_gatt_disc_all_dscs_cb(uint16_t conn_handle,
  1727. const struct ble_gatt_error *error,
  1728. uint16_t chr_val_handle,
  1729. const struct ble_gatt_dsc *dsc,
  1730. void *arg){
  1731. printf("ble_gatt_disc_all_dscs_cb\r\n");
  1732. if(error->status == 0){
  1733. uint8_t data_len = dsc->uuid.u.type;
  1734. uint8_t *uuid;
  1735. if(data_len == 16){ // = 16
  1736. uint16_t value = dsc->uuid.u16.value;
  1737. uuid = (uint8_t *)malloc(2);
  1738. uuid[0] = (uint8_t)(value >> 8); // 取高位字节
  1739. uuid[1] = (uint8_t) value; // 取低位字节
  1740. }else if(data_len == 32){ // 32
  1741. uint16_t value = dsc->uuid.u32.value;
  1742. uuid = (uint8_t *)malloc(4);
  1743. uuid[0] = (uint8_t)(value >> 24); // 取高位字节
  1744. uuid[1] = (uint8_t)(value >> 16); // 取次高位字节
  1745. uuid[2] = (uint8_t)(value >> 8); // 取次低位字节
  1746. uuid[3] = (uint8_t)value; // 取低位字节
  1747. }else{ // 128
  1748. uuid = (uint8_t *)malloc(16);
  1749. data_inver(dsc->uuid.u128.value,uuid,16);
  1750. }
  1751. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_DESCRIPTOR_RESULT,
  1752. arg_newObj(New_pikaTupleFrom(
  1753. arg_newInt(_IRQ_GATTC_DESCRIPTOR_RESULT),
  1754. arg_newInt(conn_handle),
  1755. // arg_newInt(chr_val_handle),
  1756. arg_newInt(dsc->handle),
  1757. arg_newBytes(uuid,data_len/8)
  1758. )));
  1759. free(uuid);
  1760. return 0;
  1761. }else if(error->status == 14){
  1762. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_DESCRIPTOR_DONE,
  1763. arg_newObj(New_pikaTupleFrom(
  1764. arg_newInt(_IRQ_GATTC_DESCRIPTOR_DONE),
  1765. arg_newInt(conn_handle),
  1766. arg_newInt(0)
  1767. )));
  1768. return 0;
  1769. }else{
  1770. pika_eventListener_send(g_pika_ble_listener,_IRQ_GATTC_DESCRIPTOR_DONE,
  1771. arg_newObj(New_pikaTupleFrom(
  1772. arg_newInt(_IRQ_GATTC_DESCRIPTOR_DONE),
  1773. arg_newInt(conn_handle),
  1774. arg_newInt(error->status)
  1775. )));
  1776. return 0;
  1777. }
  1778. return 0;
  1779. }