ble_gatt_demo.c 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498
  1. /*================================================================
  2. Copyright (c) 2020 Quectel Wireless Solution, Co., Ltd. All Rights Reserved.
  3. Quectel Wireless Solution Proprietary and Confidential.
  4. =================================================================*/
  5. /*=================================================================
  6. EDIT HISTORY FOR MODULE
  7. This section contains comments describing changes made to the module.
  8. Notice that changes are listed in reverse chronological order.
  9. WHEN WHO WHAT, WHERE, WHY
  10. ------------ ------- -------------------------------------------------------------------------------
  11. =================================================================*/
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #include "ql_api_osi.h"
  16. #include "ql_api_bt.h"
  17. #include "ql_log.h"
  18. #include "ble_gatt_demo.h"
  19. #include "ql_api_ble_gatt.h"
  20. #include "ql_power.h"
  21. #include "ql_api_dev.h"
  22. #define QL_BLE_GATT_LOG_LEVEL QL_LOG_LEVEL_INFO
  23. #define QL_BLE_GATT_LOG(msg, ...) QL_LOG(QL_BLE_GATT_LOG_LEVEL, "ql_BT_GATT_DEMO", msg, ##__VA_ARGS__)
  24. #define QL_BLE_GATT_LOG_PUSH(msg, ...) QL_LOG_PUSH("ql_BT_GATT_DEMO", msg, ##__VA_ARGS__)
  25. #define QL_BLE_DEMO_ADDR_MAX_SIZE 17
  26. //BLE run in low power mode rot not
  27. #define QL_BLE_DEMO_LOW_POWER_USE 0 //not run in lower power mode
  28. extern ql_errcode_bt_e ql_bt_demo_start();
  29. extern ql_errcode_bt_e ql_bt_demo_stop();
  30. extern ql_errcode_bt_e ql_bt_demo_get_state();
  31. extern ql_errcode_dev_e ql_cfun_comm_set(uint8_t at_dst_cfun, uint8_t nSim);
  32. ql_task_t ble_demo_task = NULL;
  33. ql_bt_ble_local_name_s ble_name =
  34. {
  35. .name = "移远BT",
  36. .code_type = QL_BT_BLE_NAME_CODE_UTF8,
  37. };
  38. ql_bt_addr_s bt_public_whitelist = {{0x66, 0xCA, 0xC9, 0xA2, 0x3E, 0x38}};
  39. ql_bt_addr_s bt_random_whitelist = {{0x66, 0xCA, 0xC9, 0xA2, 0x3E, 0x39}};
  40. ql_ble_whitelist_info_s bt_remove_whiltelist =
  41. {
  42. .mode = QL_BLE_PUBLIC_ADDRESS,
  43. .addr = {{0x66, 0xCA, 0xC9, 0xA2, 0x3E, 0x38}},
  44. };
  45. //gatt server
  46. ql_bt_addr_s ble_adv_addr = {{0xC0, 0x58, 0x65, 0x01, 0x02, 0x05}};
  47. ql_bleadv_typical_addr_param_s ble_adv_typical_param =
  48. {
  49. .max_adv = QL_BLE_ADV_MAX_ADV,
  50. .min_adv = QL_BLE_ADV_MIN_ADV,
  51. .adv_type = QL_BLEADV_INV,
  52. .own_addr_type = QL_BLE_PUBLIC_ADDRESS,
  53. .direct_address_mode = QL_BLE_RANDOM_ADDRESS,
  54. .direct_addr = {{0x66, 0xCA, 0xC9, 0xA2, 0x3E, 0x38}},
  55. .channel_map = QL_BLE_ADV_MAP,
  56. .filter = QL_BLEADV_FILTER_0,
  57. .discov_mode = 2,
  58. .no_br_edr = 1,
  59. .enable_adv = 1,
  60. };
  61. ql_bleadv_param_s ble_adv_param =
  62. {
  63. .min_adv = QL_BLE_ADV_MIN_ADV,
  64. .max_adv = QL_BLE_ADV_MAX_ADV,
  65. .adv_type = QL_BLEADV_INV,
  66. .own_addr_type = QL_BLE_PUBLIC_ADDRESS,
  67. .channel_map = QL_BLE_ADV_MAP,
  68. .filter = QL_BLEADV_FILTER_0,
  69. .discov_mode = 2,
  70. .no_br_edr = 1,
  71. .enable_adv = 1,
  72. };
  73. #define QL_BLE_ADV_DATA_ORIGAL_LEN 3
  74. ql_bleadv_set_data_s ble_adv_data =
  75. {
  76. .date_len = QL_BLE_ADV_DATA_ORIGAL_LEN,
  77. .data = {0x02,0x01,0x05},
  78. };
  79. ql_bleadv_set_data_s ble_adv_scan_rsp_data;
  80. unsigned short ql_bt_ble_mtu = QL_BLE_GATT_MTU_DEFAULT_SIZE;
  81. unsigned char ql_ble_gatt_chara_value[QL_BLE_GATT_MTU_MAX_SIZE - QL_BLE_GATT_MTU_HEAD_SIZE];
  82. ql_bt_addr_s ble_connection_addr;
  83. unsigned short ble_conn_handle = 0;
  84. unsigned short ble_server_hanle = QL_GATT_START_HANDLE_WITHOUT_SYS;
  85. //ibeacon
  86. unsigned short ble_ibeacon_major = 20;
  87. unsigned short ble_ibeacon_minor = 25;
  88. ql_ble_update_conn_infos_s ble_conn_param =
  89. {
  90. .conn_id = 0,
  91. .min_interval = QL_BLE_UPDATE_CONN_MIN_INTERVAL,
  92. .max_interval = QL_BLE_UPDATE_CONN_MAX_INTERVAL,
  93. .latency = QL_BLE_UPDATE_CONN_LATENCY,
  94. .timeout = QL_BLE_UPDATE_CONN_TIMEOUT,
  95. };
  96. //gatt client
  97. ql_blescan_scan_s ble_scan_param =
  98. {
  99. .scan_mode = QL_BLESCAN_ACTIVE,
  100. .interval = QL_BLE_SCAN_INTERVAL,
  101. .window = QL_BLE_SCAN_WINDOW,
  102. .filter = QL_BLESCAN_FILTER_0,
  103. .own_addr_type = QL_BLE_PUBLIC_ADDRESS,
  104. };
  105. ql_bt_addr_s ble_scan_report_addr;
  106. ql_ble_address_mode_e ble_scan_report_addr_type;
  107. unsigned char ble_scan_report_name[] = "UNISOC-8910";//"vivo Y79";//"8910_BTDM";
  108. ql_ble_gatt_state_e ql_ble_gatt_state = QL_BLE_GATT_IDLE;
  109. ql_blegatt_prime_service_s ql_ble_gatt_target_service =
  110. {
  111. .uuid = 0x180F, //short uuid
  112. //.uuid = 0x18FE, //long uuid
  113. };
  114. ql_ble_gatt_chara_s ql_ble_gatt_characteristic[QL_BLE_CHARA_NUM_MAX] =
  115. {
  116. {
  117. .uuid = 0x2A19, //
  118. },
  119. };
  120. unsigned char ql_ble_gatt_uuid_l[QL_BLE_LONG_UUID_SIZE] = {0xf5,0x89,0x9b,0x5f,0x80,0x00,0x00,0x80,0x00,0x10,0x00,0x00,0xFE,0x18,0x00,0x00};
  121. ql_ble_gatt_chara_desc_s ql_ble_gatt_chara_desc[QL_BLE_DESC_NUM_MAX] = {0};
  122. unsigned char ql_ble_gatt_chara_desc_value[2] = {0x12, 0x34};
  123. ql_ble_gatt_discover_service_mode_e ql_ble_gatt_discover_service_mode = QL_BLE_GATT_DISCOVER_SERVICE_ALL;
  124. ql_ble_gatt_uuid_state_e ql_ble_gatt_uuid_state = QL_BLE_GATT_SHORT_UUID;
  125. unsigned short ql_ble_gatt_chara_count = 0;
  126. unsigned short ql_ble_gatt_cur_chara = 0;
  127. unsigned short ql_ble_gatt_chara_desc_index = 0;
  128. unsigned short ql_ble_gatt_chara_cur_desc = 0;
  129. unsigned short ql_ble_gatt_chara_desc_count = 0;
  130. ql_ble_sys_service_mode_e ql_ble_gatt_sys_service = QL_RESERVED_SERVICE_DEL;
  131. #if QL_BLE_DEMO_LOW_POWER_USE
  132. int bt_ble_power_lock = 0;
  133. #endif
  134. static ql_errcode_bt_e ql_ble_demo_get_state()
  135. {
  136. ql_errcode_bt_e ret;
  137. ql_bt_state_e bt_state;
  138. ret = ql_bt_get_state(&bt_state);
  139. if (ret != QL_BT_SUCCESS)
  140. {
  141. QL_BLE_GATT_LOG("error=%x", ret);
  142. }
  143. else
  144. {
  145. QL_BLE_GATT_LOG("bt_state=%d",(int)bt_state);
  146. }
  147. return ret;
  148. }
  149. static ql_errcode_bt_e ql_ble_demo_get_public_addr()
  150. {
  151. ql_errcode_bt_e ret;
  152. ql_bt_addr_s bt_public_addr;
  153. ret = ql_ble_get_public_addr(&bt_public_addr);
  154. if (ret == QL_BT_SUCCESS)
  155. {
  156. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  157. memset(addr_string,0x00,sizeof(addr_string));
  158. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", bt_public_addr.addr[0], bt_public_addr.addr[1], bt_public_addr.addr[2], bt_public_addr.addr[3], bt_public_addr.addr[4], bt_public_addr.addr[5]);
  159. QL_BLE_GATT_LOG("addr_string=%s",addr_string);
  160. }
  161. else
  162. {
  163. QL_BLE_GATT_LOG("error=%x", ret);
  164. }
  165. return ret;
  166. }
  167. static ql_errcode_bt_e ql_ble_demo_get_nv_addr()
  168. {
  169. ql_errcode_bt_e ret;
  170. ql_bt_addr_s bt_mac_addr;
  171. ret = ql_bt_ble_get_nv_addr(&bt_mac_addr);
  172. if (ret == QL_BT_SUCCESS)
  173. {
  174. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  175. memset(addr_string,0x00,sizeof(addr_string));
  176. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", bt_mac_addr.addr[0], bt_mac_addr.addr[1], bt_mac_addr.addr[2], bt_mac_addr.addr[3], bt_mac_addr.addr[4], bt_mac_addr.addr[5]);
  177. QL_BLE_GATT_LOG("addr_string=%s",addr_string);
  178. }
  179. else
  180. {
  181. QL_BLE_GATT_LOG("error=%x", ret);
  182. }
  183. return ret;
  184. }
  185. static ql_errcode_bt_e ql_ble_demo_get_version()
  186. {
  187. ql_errcode_bt_e ret;
  188. char version[QL_BLE_VERSION_SIZE];
  189. ret = ql_ble_get_version(version,sizeof(version));
  190. if (ret == QL_BT_SUCCESS)
  191. {
  192. QL_BLE_GATT_LOG("version=%s", version);
  193. }
  194. else
  195. {
  196. QL_BLE_GATT_LOG("error=%x", ret);
  197. }
  198. return ret;
  199. }
  200. static ql_errcode_bt_e ql_ble_demo_get_random_addr()
  201. {
  202. ql_errcode_bt_e ret;
  203. ql_bt_addr_s bt_random_addr;
  204. ret = ql_ble_get_random_addr(&bt_random_addr);
  205. if (ret == QL_BT_SUCCESS)
  206. {
  207. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  208. memset(addr_string,0x00,sizeof(addr_string));
  209. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", bt_random_addr.addr[0], bt_random_addr.addr[1], bt_random_addr.addr[2], bt_random_addr.addr[3], bt_random_addr.addr[4], bt_random_addr.addr[5]);
  210. QL_BLE_GATT_LOG("addr_string=%s",addr_string);
  211. }
  212. else
  213. {
  214. QL_BLE_GATT_LOG("error=%x", ret);
  215. }
  216. return ret;
  217. }
  218. ql_errcode_bt_e ql_ble_demo_add_public_whitelist()
  219. {
  220. ql_errcode_bt_e ret;
  221. ret = ql_ble_add_public_whitelist(bt_public_whitelist);
  222. if (ret == QL_BT_SUCCESS)
  223. {
  224. QL_BLE_GATT_LOG("sucess");
  225. }
  226. else
  227. {
  228. QL_BLE_GATT_LOG("error=%x", ret);
  229. }
  230. return ret;
  231. }
  232. ql_errcode_bt_e ql_ble_demo_add_random_whitelist()
  233. {
  234. ql_errcode_bt_e ret;
  235. ret = ql_ble_add_random_whitelist(bt_random_whitelist);
  236. if (ret == QL_BT_SUCCESS)
  237. {
  238. QL_BLE_GATT_LOG("sucess");
  239. }
  240. else
  241. {
  242. QL_BLE_GATT_LOG("error=%x", ret);
  243. }
  244. return ret;
  245. }
  246. static ql_errcode_bt_e ql_ble_demo_get_whitelist_info()
  247. {
  248. ql_errcode_bt_e ret;
  249. ql_ble_whitelist_info_s whitelist[QL_BLE_WHITELIST_NUM_MAX];
  250. unsigned char whitelist_count = 0;
  251. ret = ql_ble_get_whitelist_info(QL_BLE_WHITELIST_NUM_MAX,&whitelist_count,whitelist);
  252. if (ret == QL_BT_SUCCESS)
  253. {
  254. unsigned char i= 0;
  255. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  256. QL_BLE_GATT_LOG("whitelist_cont=%d", whitelist_count);
  257. for (i=0; i<whitelist_count && i<QL_BLE_WHITELIST_NUM_MAX; i++)
  258. {
  259. memset(addr_string,0x00,sizeof(addr_string));
  260. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", whitelist[i].addr.addr[0], whitelist[i].addr.addr[1], whitelist[i].addr.addr[2], whitelist[i].addr.addr[3], whitelist[i].addr.addr[4], whitelist[i].addr.addr[5]);
  261. QL_BLE_GATT_LOG("addr_string[%d]=%s", i, addr_string);
  262. }
  263. }
  264. else
  265. {
  266. QL_BLE_GATT_LOG("error=%x", ret);
  267. }
  268. return ret;
  269. }
  270. static ql_errcode_bt_e ql_ble_demo_remove_whitelist()
  271. {
  272. ql_errcode_bt_e ret;
  273. ret = ql_ble_remove_whitelist(bt_remove_whiltelist);
  274. if (ret == QL_BT_SUCCESS)
  275. {
  276. QL_BLE_GATT_LOG("sucess");
  277. }
  278. else
  279. {
  280. QL_BLE_GATT_LOG("error=%x", ret);
  281. }
  282. return ret;
  283. }
  284. static ql_errcode_bt_e ql_ble_demo_clean_whitelist()
  285. {
  286. ql_errcode_bt_e ret;
  287. ret = ql_ble_clean_whitelist();
  288. if (ret == QL_BT_SUCCESS)
  289. {
  290. QL_BLE_GATT_LOG("sucess");
  291. }
  292. else
  293. {
  294. QL_BLE_GATT_LOG("error=%x", ret);
  295. }
  296. return ret;
  297. }
  298. static ql_errcode_bt_e ql_ble_demo_get_name()
  299. {
  300. ql_errcode_bt_e ret;
  301. ql_bt_ble_local_name_s bt_name;
  302. ret = ql_bt_ble_get_localname(&bt_name);
  303. if (ret == QL_BT_SUCCESS)
  304. {
  305. QL_BLE_GATT_LOG("bt_name.name=%s",bt_name.name);
  306. }
  307. else
  308. {
  309. QL_BLE_GATT_LOG("error=%x", ret);
  310. }
  311. return ret;
  312. }
  313. static ql_errcode_bt_e ql_ble_demo_set_name()
  314. {
  315. ql_errcode_bt_e ret;
  316. ret = ql_bt_ble_set_localname(ble_name);
  317. if (ret == QL_BT_SUCCESS)
  318. {
  319. QL_BLE_GATT_LOG("sucess");
  320. }
  321. else
  322. {
  323. QL_BLE_GATT_LOG("error=%x", ret);
  324. }
  325. return ret;
  326. }
  327. ql_errcode_bt_e ql_bleadv_demo_set_param()
  328. {
  329. ql_errcode_bt_e ret;
  330. ret = ql_bleadv_set_param(ble_adv_param);
  331. if (ret == QL_BT_SUCCESS)
  332. {
  333. QL_BLE_GATT_LOG("sucess");
  334. }
  335. else
  336. {
  337. QL_BLE_GATT_LOG("error=%x", ret);
  338. }
  339. return ret;
  340. }
  341. ql_errcode_bt_e ql_bleadv_demo_set_typical_addr_param()
  342. {
  343. ql_errcode_bt_e ret;
  344. ret = ql_bleadv_set_typical_addr_param(ble_adv_typical_param);
  345. if (ret == QL_BT_SUCCESS)
  346. {
  347. QL_BLE_GATT_LOG("sucess");
  348. }
  349. else
  350. {
  351. QL_BLE_GATT_LOG("error=%x", ret);
  352. }
  353. return ret;
  354. }
  355. ql_errcode_bt_e ql_bleadv_demo_set_data()
  356. {
  357. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  358. unsigned char init_len = QL_BLE_ADV_DATA_ORIGAL_LEN;//ble_adv_data.date_len;
  359. unsigned char length = strlen((char *)ble_name.name);
  360. ble_adv_data.date_len = init_len+length+2;
  361. ble_adv_data.data[init_len] = length+1;
  362. ble_adv_data.data[init_len+1] = 0x09;
  363. memcpy(&ble_adv_data.data[init_len+2], ble_name.name, length);
  364. ret = ql_bleadv_set_data(ble_adv_data);
  365. if (ret == QL_BT_SUCCESS)
  366. {
  367. QL_BLE_GATT_LOG("sucess");
  368. }
  369. else
  370. {
  371. QL_BLE_GATT_LOG("error=%x", ret);
  372. }
  373. return ret;
  374. }
  375. ql_errcode_bt_e ql_bleadv_demo_set_ibeacon_data()
  376. {
  377. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  378. ql_ble_ibeacon_cfg_s cfg = {0};
  379. //memcpy(cfg.uuid_l, ql_ble_gatt_uuid_l, QL_BLE_LONG_UUID_SIZE);
  380. //cfg.major = ble_ibeacon_major;
  381. //cfg.minor = ble_ibeacon_minor;
  382. //ret = ql_ble_write_ibeacon_cfg(cfg);
  383. ret = ql_ble_read_ibeacon_cfg(&cfg);
  384. if (ret == QL_BT_SUCCESS)
  385. {
  386. ret = ql_ble_set_ibeacon_data(cfg.uuid_l, cfg.major, cfg.minor);
  387. }
  388. else
  389. {
  390. ret = ql_ble_set_ibeacon_data(ql_ble_gatt_uuid_l, ble_ibeacon_major, ble_ibeacon_minor);
  391. }
  392. if (ret == QL_BT_SUCCESS)
  393. {
  394. QL_BLE_GATT_LOG("sucess");
  395. }
  396. else
  397. {
  398. QL_BLE_GATT_LOG("error=%x", ret);
  399. }
  400. return ret;
  401. }
  402. ql_errcode_bt_e ql_bleadv_demo_set_scan_rsp_data()
  403. {
  404. ql_errcode_bt_e ret;
  405. unsigned char length = strlen((char *)ble_name.name);
  406. ble_adv_scan_rsp_data.date_len = length+2;
  407. ble_adv_scan_rsp_data.data[0] = length+1;
  408. ble_adv_scan_rsp_data.data[1] = 0x09;
  409. memcpy(&ble_adv_scan_rsp_data.data[2], ble_name.name, length);
  410. ret = ql_bleadv_set_scan_rsp_data(ble_adv_scan_rsp_data);
  411. if (ret == QL_BT_SUCCESS)
  412. {
  413. QL_BLE_GATT_LOG("sucess");
  414. }
  415. else
  416. {
  417. QL_BLE_GATT_LOG("error=%x", ret);;
  418. }
  419. return ret;
  420. }
  421. ql_errcode_bt_e ql_ble_gatt_demo_add_service()
  422. {
  423. ql_errcode_bt_e ret;
  424. ql_ble_gatt_uuid_s uuid =
  425. {
  426. .uuid_type = 1,
  427. .uuid_l = {0x00},
  428. .uuid_s = 0x180F,
  429. };
  430. ret = ql_ble_gatt_add_service(0x01, uuid, 1);
  431. if (ret == QL_BT_SUCCESS)
  432. {
  433. QL_BLE_GATT_LOG("sucess");
  434. }
  435. else
  436. {
  437. QL_BLE_GATT_LOG("error=%x", ret);
  438. }
  439. return ret;
  440. }
  441. ql_errcode_bt_e ql_ble_gatt_demo_add_chara()
  442. {
  443. ql_errcode_bt_e ret;
  444. ql_ble_gatt_uuid_s uuid =
  445. {
  446. .uuid_type = 1,
  447. .uuid_l = {0x00},
  448. .uuid_s = 0x2A19,
  449. };
  450. ret = ql_ble_gatt_add_chara(0x01, 0x01, QL_ATT_CHARA_PROP_READ | QL_ATT_CHARA_PROP_NOTIFY | QL_ATT_CHARA_PROP_INDICATE, uuid);
  451. if (ret == QL_BT_SUCCESS)
  452. {
  453. QL_BLE_GATT_LOG("sucess");
  454. }
  455. else
  456. {
  457. QL_BLE_GATT_LOG("error=%x", ret);
  458. }
  459. return ret;
  460. }
  461. ql_errcode_bt_e ql_ble_gatt_demo_add_chara_value()
  462. {
  463. ql_errcode_bt_e ret;
  464. ql_ble_gatt_uuid_s uuid =
  465. {
  466. .uuid_type = 1,
  467. .uuid_l = {0x00},
  468. .uuid_s = 0x2A19,
  469. };
  470. memset(ql_ble_gatt_chara_value, 0x00, sizeof(ql_ble_gatt_chara_value));
  471. ret = ql_ble_gatt_add_chara_value(0x01, 0x01, QL_ATT_PM_READABLE | QL_ATT_PM_WRITEABLE, uuid, sizeof(ql_ble_gatt_chara_value), (unsigned char *)ql_ble_gatt_chara_value);
  472. if (ret == QL_BT_SUCCESS)
  473. {
  474. QL_BLE_GATT_LOG("sucess");
  475. }
  476. else
  477. {
  478. QL_BLE_GATT_LOG("error=%x", ret);
  479. }
  480. return ret;
  481. }
  482. ql_errcode_bt_e ql_ble_gatt_demo_change_chara_value()
  483. {
  484. ql_errcode_bt_e ret;
  485. memset(ql_ble_gatt_chara_value, 0x55, sizeof(ql_ble_gatt_chara_value));
  486. ret = ql_ble_gatt_change_chara_value(0x01, 0x01, sizeof(ql_ble_gatt_chara_value), (unsigned char *)ql_ble_gatt_chara_value);
  487. if (ret == QL_BT_SUCCESS)
  488. {
  489. QL_BLE_GATT_LOG("sucess");
  490. }
  491. else
  492. {
  493. QL_BLE_GATT_LOG("error=%x", ret);
  494. }
  495. return ret;
  496. }
  497. ql_errcode_bt_e ql_ble_gatt_demo_add_chara_desc()
  498. {
  499. ql_errcode_bt_e ret;
  500. unsigned short value = 0x03;
  501. ql_ble_gatt_uuid_s uuid =
  502. {
  503. .uuid_type = 1,
  504. .uuid_l = {0x00},
  505. .uuid_s = 0x2902,
  506. };
  507. ret = ql_ble_gatt_add_chara_desc(0x01, 0x01, QL_ATT_PM_READABLE | QL_ATT_PM_WRITEABLE, uuid, sizeof(value), (unsigned char *)&value);
  508. if (ret == QL_BT_SUCCESS)
  509. {
  510. QL_BLE_GATT_LOG("sucess");
  511. }
  512. else
  513. {
  514. QL_BLE_GATT_LOG("error=%x", ret);
  515. }
  516. return ret;
  517. }
  518. static ql_errcode_bt_e ql_bleadv_demo_start()
  519. {
  520. ql_errcode_bt_e ret;
  521. ret = ql_bleadv_start();
  522. if (ret == QL_BT_SUCCESS)
  523. {
  524. QL_BLE_GATT_LOG("sucess");
  525. }
  526. else
  527. {
  528. QL_BLE_GATT_LOG("error=%x", ret);
  529. }
  530. return ret;
  531. }
  532. static ql_errcode_bt_e ql_ble_demo_get_conection_state()
  533. {
  534. ql_errcode_bt_e ret;
  535. ql_bt_ble_connection_state_e state;
  536. ret = ql_ble_get_connection_state(ble_connection_addr,&state);
  537. if (ret == QL_BT_SUCCESS)
  538. {
  539. QL_BLE_GATT_LOG("state=%d", state);
  540. }
  541. else
  542. {
  543. QL_BLE_GATT_LOG("error=%x", ret);
  544. }
  545. return 0;
  546. }
  547. ql_errcode_bt_e ql_ble_demo_update_conn_param()
  548. {
  549. ql_errcode_bt_e ret;
  550. ble_conn_param.conn_id = ble_conn_handle;
  551. ret = ql_ble_update_conn_param(ble_conn_param);
  552. if (ret == QL_BT_SUCCESS)
  553. {
  554. QL_BLE_GATT_LOG("sucess");
  555. }
  556. else if (ret == QL_BT_PENDING)
  557. {
  558. QL_BLE_GATT_LOG("pending");
  559. }
  560. else
  561. {
  562. QL_BLE_GATT_LOG("error=%x", ret);
  563. }
  564. return ret;
  565. }
  566. ql_errcode_bt_e ql_ble_demo_exchange_mtu()
  567. {
  568. ql_errcode_bt_e ret;
  569. ret = ql_ble_exchange_mtu(ble_conn_handle, 150);
  570. if (ret == QL_BT_SUCCESS)
  571. {
  572. QL_BLE_GATT_LOG("sucess");
  573. }
  574. else if (ret == QL_BT_PENDING)
  575. {
  576. QL_BLE_GATT_LOG("pending");
  577. }
  578. else
  579. {
  580. QL_BLE_GATT_LOG("error=%x", ret);
  581. }
  582. return ret;
  583. }
  584. ql_errcode_bt_e ql_ble_demo_send_data()
  585. {
  586. ql_errcode_bt_e ret = 0;
  587. unsigned char send_data[128] = {0};
  588. int i;
  589. for (i=0; i<sizeof(send_data)-1; i++)
  590. {
  591. send_data[i] = '3';
  592. }
  593. //memcpy(send_data.data,"12345678",8);
  594. //send_data.len = 8;
  595. //send_data.data[8] = 0;
  596. QL_BLE_GATT_LOG("send_data.data=%s", send_data);
  597. ret = ql_ble_send_notification_data(0, ble_server_hanle+2, sizeof(send_data), send_data);
  598. if (ret == QL_BT_SUCCESS)
  599. {
  600. QL_BLE_GATT_LOG("sucess");
  601. }
  602. else
  603. {
  604. QL_BLE_GATT_LOG("error=%x", ret);
  605. }
  606. return ret;
  607. }
  608. ql_errcode_bt_e ql_ble_demo_disconect_device()
  609. {
  610. ql_errcode_bt_e ret;
  611. ret = ql_ble_disconnect(ble_conn_handle);
  612. if (ret == QL_BT_SUCCESS)
  613. {
  614. QL_BLE_GATT_LOG("sucess");
  615. }
  616. else
  617. {
  618. QL_BLE_GATT_LOG("error=%x", ret);
  619. }
  620. return ret;
  621. }
  622. /************************************************************************
  623. * QUEC_BLE_CONNECT_IND
  624. * QUEC_BLE_DISCONNECT_IND
  625. * QUEC_BLE_UPDATE_CONN_PARAM_IND
  626. * QUEC_BLE_GATT_RECV_IND
  627. * QUEC_BLE_GATT_RECV_READ_IND
  628. ************************************************************************/
  629. ql_errcode_bt_e ql_ble_gatt_server_handle_event()
  630. {
  631. ql_event_t test_event;
  632. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  633. QL_BT_STATUS status = 0;
  634. if (ql_event_try_wait(&test_event) == 0)
  635. {
  636. if(test_event.id == 0)
  637. {
  638. return ret;
  639. }
  640. status = (QL_BT_STATUS)(test_event.param1);
  641. switch (test_event.id)
  642. {
  643. case QUEC_BT_START_STATUS_IND:
  644. {
  645. if (QL_BT_STATUS_SUCCESS == status)
  646. {
  647. QL_BLE_GATT_LOG("start sucess");
  648. #if QL_BLE_DEMO_LOW_POWER_USE
  649. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  650. #endif
  651. ret = ql_ble_demo_get_state();
  652. if (ret != QL_BT_SUCCESS)
  653. {
  654. goto QL_BLE_ADV_DEMO_STOP;
  655. }
  656. #if 1
  657. ret = ql_ble_demo_get_public_addr();
  658. if (ret != QL_BT_SUCCESS)
  659. {
  660. goto QL_BLE_ADV_DEMO_STOP;
  661. }
  662. ret = ql_ble_demo_get_version();
  663. if (ret != QL_BT_SUCCESS)
  664. {
  665. goto QL_BLE_ADV_DEMO_STOP;
  666. }
  667. ret = ql_ble_demo_get_random_addr();
  668. if (ret != QL_BT_SUCCESS)
  669. {
  670. goto QL_BLE_ADV_DEMO_STOP;
  671. }
  672. ret = ql_ble_demo_add_public_whitelist();
  673. if (ret != QL_BT_SUCCESS)
  674. {
  675. goto QL_BLE_ADV_DEMO_STOP;
  676. }
  677. ret = ql_ble_demo_add_random_whitelist();
  678. if (ret != QL_BT_SUCCESS)
  679. {
  680. goto QL_BLE_ADV_DEMO_STOP;
  681. }
  682. ret = ql_ble_demo_get_whitelist_info();
  683. if (ret != QL_BT_SUCCESS)
  684. {
  685. goto QL_BLE_ADV_DEMO_STOP;
  686. }
  687. ret = ql_ble_demo_remove_whitelist();
  688. if (ret != QL_BT_SUCCESS)
  689. {
  690. goto QL_BLE_ADV_DEMO_STOP;
  691. }
  692. ret = ql_ble_demo_clean_whitelist();
  693. if (ret != QL_BT_SUCCESS)
  694. {
  695. goto QL_BLE_ADV_DEMO_STOP;
  696. }
  697. //ret = ql_ble_demo_get_name();
  698. //if (ret != QL_BT_SUCCESS)
  699. //{
  700. // goto QL_BLE_ADV_DEMO_STOP;
  701. //}
  702. ret = ql_ble_demo_set_name();
  703. if (ret != QL_BT_SUCCESS)
  704. {
  705. goto QL_BLE_ADV_DEMO_STOP;
  706. }
  707. ret = ql_ble_demo_get_name();
  708. if (ret != QL_BT_SUCCESS)
  709. {
  710. goto QL_BLE_ADV_DEMO_STOP;
  711. }
  712. #endif
  713. ret = ql_bleadv_demo_set_param();
  714. if (ret != QL_BT_SUCCESS)
  715. {
  716. goto QL_BLE_ADV_DEMO_STOP;
  717. }
  718. //ret = ql_bleadv_demo_set_ibeacon_data();
  719. //if (ret != QL_BT_SUCCESS)
  720. //{
  721. // goto QL_BLE_ADV_DEMO_STOP;
  722. //}
  723. ret = ql_bleadv_demo_set_data();
  724. if (ret != QL_BT_SUCCESS)
  725. {
  726. goto QL_BLE_ADV_DEMO_STOP;
  727. }
  728. ret = ql_bleadv_demo_set_scan_rsp_data();
  729. if (ret != QL_BT_SUCCESS)
  730. {
  731. goto QL_BLE_ADV_DEMO_STOP;
  732. }
  733. ret = ql_ble_gatt_demo_add_service();
  734. if (ret != QL_BT_SUCCESS)
  735. {
  736. goto QL_BLE_ADV_DEMO_STOP;
  737. }
  738. ret = ql_ble_gatt_demo_add_chara();
  739. if (ret != QL_BT_SUCCESS)
  740. {
  741. goto QL_BLE_ADV_DEMO_STOP;
  742. }
  743. ret = ql_ble_gatt_demo_add_chara_value();
  744. if (ret != QL_BT_SUCCESS)
  745. {
  746. goto QL_BLE_ADV_DEMO_STOP;
  747. }
  748. ret = ql_ble_gatt_demo_add_chara_desc();
  749. if (ret != QL_BT_SUCCESS)
  750. {
  751. goto QL_BLE_ADV_DEMO_STOP;
  752. }
  753. ret = ql_ble_gatt_add_or_clear_service_complete(QL_BLE_SERVICE_ADD_COMPLETE, ql_ble_gatt_sys_service);
  754. if (ret != QL_BT_SUCCESS)
  755. {
  756. goto QL_BLE_ADV_DEMO_STOP;
  757. }
  758. if (ql_ble_gatt_sys_service == QL_RESERVED_SERVICE_DEL)
  759. {
  760. ble_server_hanle = QL_GATT_START_HANDLE_WITHOUT_SYS;
  761. }
  762. else
  763. {
  764. ble_server_hanle = QL_GATT_START_HANDLE_WITH_SYS;
  765. }
  766. ret = ql_bleadv_demo_start();
  767. if (ret != QL_BT_SUCCESS)
  768. {
  769. goto QL_BLE_ADV_DEMO_STOP;
  770. }
  771. }
  772. else
  773. {
  774. QL_BLE_GATT_LOG("start failed");
  775. }
  776. }
  777. break;
  778. case QUEC_BT_STOP_STATUS_IND:
  779. {
  780. #if QL_BLE_DEMO_LOW_POWER_USE
  781. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  782. #endif
  783. if (QL_BT_STATUS_SUCCESS == status)
  784. {
  785. QL_BLE_GATT_LOG("stop sucess");
  786. ret = QL_BT_ALREADY_STOPED_ERR;
  787. }
  788. else
  789. {
  790. QL_BLE_GATT_LOG("stop failed");
  791. ret = QL_BT_ALREADY_STOPED_ERR;
  792. }
  793. }
  794. break;
  795. case QUEC_BT_BLE_RESET_IND:
  796. {
  797. #if QL_BLE_DEMO_LOW_POWER_USE
  798. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  799. #endif
  800. QL_BLE_GATT_LOG("bt reset");
  801. ret = QL_BT_ALREADY_STOPED_ERR;
  802. }
  803. break;
  804. case QUEC_BLE_CONNECT_IND:
  805. {
  806. if (QL_BT_STATUS_SUCCESS == status)
  807. {
  808. QL_BLE_GATT_LOG("connect sucess");
  809. ql_bt_addr_s * addr = (ql_bt_addr_s *)test_event.param2;
  810. if (addr)
  811. {
  812. //addr need to free
  813. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  814. memset(addr_string,0x00,sizeof(addr_string));
  815. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], addr->addr[4], addr->addr[5]);
  816. QL_BLE_GATT_LOG("addr_string=%s", addr_string);
  817. memcpy(ble_connection_addr.addr, addr->addr, QL_BT_MAC_ADDRESS_SIZE);
  818. free(addr);
  819. }
  820. ble_conn_handle = test_event.param3;
  821. QL_BLE_GATT_LOG("ble_conn_handle=%d", ble_conn_handle);
  822. ret = ql_ble_demo_get_conection_state();
  823. if (ret == QL_BT_SUCCESS)
  824. {
  825. //ql_rtos_task_sleep_s(3);
  826. //ret = ql_ble_demo_send_data();
  827. //ret = ql_ble_demo_update_conn_param();
  828. if (ret != QL_BT_SUCCESS)
  829. {
  830. ret = ql_ble_demo_disconect_device();
  831. if (ret != QL_BT_SUCCESS)
  832. {
  833. goto QL_BLE_ADV_DEMO_STOP;
  834. }
  835. }
  836. }
  837. else
  838. {
  839. goto QL_BLE_ADV_DEMO_STOP;
  840. }
  841. }
  842. else
  843. {
  844. QL_BLE_GATT_LOG("connect failed");
  845. goto QL_BLE_ADV_DEMO_STOP;
  846. }
  847. }
  848. break;
  849. case QUEC_BLE_GATT_MTU:
  850. {
  851. if (QL_BT_STATUS_SUCCESS == status)
  852. {
  853. QL_BLE_GATT_LOG("connect mtu sucess:handle=%d,mtu=%d", test_event.param2, test_event.param3);
  854. ql_bt_ble_mtu = test_event.param3;
  855. }
  856. else
  857. {
  858. QL_BLE_GATT_LOG("connect mtu failed");
  859. goto QL_BLE_ADV_DEMO_STOP;
  860. }
  861. }
  862. break;
  863. case QUEC_BLE_DISCONNECT_IND:
  864. {
  865. if (QL_BT_STATUS_SUCCESS == status)
  866. {
  867. QL_BLE_GATT_LOG("disconnect sucess");
  868. ql_bt_addr_s * addr = (ql_bt_addr_s *)test_event.param2;
  869. if (addr)
  870. {
  871. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  872. memset(addr_string,0x00,sizeof(addr_string));
  873. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], addr->addr[4], addr->addr[5]);
  874. QL_BLE_GATT_LOG("addr_string=%s", addr_string);
  875. free(addr);
  876. }
  877. ret = ql_ble_demo_get_conection_state();
  878. //ret = ql_ble_demo_disconect_device();
  879. //ql_rtos_task_sleep_s(5);
  880. goto QL_BLE_ADV_DEMO_STOP;
  881. }
  882. else
  883. {
  884. QL_BLE_GATT_LOG("disconnect failed");
  885. goto QL_BLE_ADV_DEMO_STOP;
  886. }
  887. }
  888. break;
  889. case QUEC_BLE_UPDATE_CONN_PARAM_IND:
  890. {
  891. if (QL_BT_STATUS_SUCCESS == status)
  892. {
  893. QL_BLE_GATT_LOG("update conn param sucess");
  894. ql_ble_update_conn_infos_s *conn_param = (ql_ble_update_conn_infos_s *)test_event.param2;
  895. if (conn_param)
  896. {
  897. QL_BLE_GATT_LOG("conn_id=%d,min_interval=%d,max_interval=%d,latency=%d,timeout=%d", conn_param->conn_id, \
  898. conn_param->min_interval, conn_param->max_interval, conn_param->latency, conn_param->timeout);
  899. free(conn_param);
  900. }
  901. //ret = ql_ble_demo_update_conn_param();
  902. //if (ret != QL_BT_SUCCESS)
  903. ///{
  904. // goto QL_BLE_ADV_DEMO_STOP;
  905. //}
  906. }
  907. else
  908. {
  909. QL_BLE_GATT_LOG("update conn param failed");
  910. goto QL_BLE_ADV_DEMO_STOP;
  911. }
  912. }
  913. break;
  914. case QUEC_BLE_GATT_SEND_END:
  915. {
  916. //send notification complete
  917. if (QL_BT_STATUS_SUCCESS == status)
  918. {
  919. QL_BLE_GATT_LOG("send data sucess");
  920. //ret = ql_ble_demo_update_conn_param();
  921. //if (ret != QL_BT_SUCCESS)
  922. //{
  923. // goto QL_BLE_ADV_DEMO_STOP;
  924. //}
  925. ql_ble_gatt_demo_change_chara_value();
  926. }
  927. else
  928. {
  929. QL_BLE_GATT_LOG("send data failed");
  930. goto QL_BLE_ADV_DEMO_STOP;
  931. }
  932. }
  933. break;
  934. case QUEC_BLE_GATT_RECV_IND:
  935. {
  936. //client write characteristic
  937. if (QL_BT_STATUS_SUCCESS == status)
  938. {
  939. QL_BLE_GATT_LOG("ble recv sucess");
  940. ql_ble_gatt_data_s *ble_data = (ql_ble_gatt_data_s *)test_event.param2;
  941. if (ble_data && ble_data->data)
  942. {
  943. unsigned char * data=calloc(1,ble_data->len+1);
  944. if(data)
  945. {
  946. memcpy(data,ble_data->data,ble_data->len);
  947. QL_BLE_GATT_LOG("ble_data->len=%d,data=%s", ble_data->len, data);
  948. free(data);
  949. }
  950. free(ble_data->data);
  951. free(ble_data);
  952. }
  953. //ret = ql_ble_demo_disconect_device();
  954. //if (ret != QL_BT_SUCCESS)
  955. //{
  956. // goto QL_BLE_ADV_DEMO_STOP;
  957. //}
  958. }
  959. else
  960. {
  961. QL_BLE_GATT_LOG("ble recv failed");
  962. goto QL_BLE_ADV_DEMO_STOP;
  963. }
  964. }
  965. break;
  966. case QUEC_BLE_GATT_RECV_READ_IND:
  967. {
  968. //client read characterisc
  969. if (QL_BT_STATUS_SUCCESS == status)
  970. {
  971. QL_BLE_GATT_LOG("ble recv sucess");
  972. ql_ble_gatt_data_s *ble_data = (ql_ble_gatt_data_s *)test_event.param2;
  973. if (ble_data && ble_data->data)
  974. {
  975. unsigned char * data=calloc(1,ble_data->len+1);
  976. if(data)
  977. {
  978. memcpy(data,ble_data->data,ble_data->len);
  979. QL_BLE_GATT_LOG("ble_data->len=%d,data=%s", ble_data->len, data);
  980. free(data);
  981. }
  982. free(ble_data->data);
  983. free(ble_data);
  984. }
  985. //ret = ql_ble_demo_disconect_device();
  986. //if (ret != QL_BT_SUCCESS)
  987. //{
  988. // goto QL_BLE_ADV_DEMO_STOP;
  989. //}
  990. }
  991. else
  992. {
  993. QL_BLE_GATT_LOG("ble recv failed");
  994. goto QL_BLE_ADV_DEMO_STOP;
  995. }
  996. }
  997. break;
  998. default:
  999. break;
  1000. }
  1001. return ret;
  1002. QL_BLE_ADV_DEMO_STOP:
  1003. #if QL_BLE_DEMO_LOW_POWER_USE
  1004. ql_lpm_wakelock_lock(bt_ble_power_lock);
  1005. #endif
  1006. ret = ql_bt_demo_stop();
  1007. }
  1008. return ret;
  1009. }
  1010. static void ql_ble_notify_cb(void *ind_msg_buf, void *ctx)
  1011. {
  1012. ql_event_t *event_temp = NULL;
  1013. ql_event_t test_event = {0};
  1014. if (ind_msg_buf == NULL)
  1015. {
  1016. return ;
  1017. }
  1018. event_temp = (ql_event_t *)ind_msg_buf;
  1019. switch (event_temp->id)
  1020. {
  1021. case QUEC_BLE_CONNECT_IND:
  1022. case QUEC_BLE_DISCONNECT_IND:
  1023. {
  1024. ql_bt_addr_s * temp = (ql_bt_addr_s *)event_temp->param2;
  1025. ql_bt_addr_s *ql_addr = (ql_bt_addr_s *)calloc(1,sizeof(ql_bt_addr_s));
  1026. if (ql_addr)
  1027. {
  1028. memcpy(ql_addr->addr, temp->addr, sizeof(ql_bt_addr_s));
  1029. test_event.id = event_temp->id;
  1030. test_event.param2 = (uint32)ql_addr;
  1031. test_event.param3 = event_temp->param3;
  1032. }
  1033. }
  1034. break;
  1035. case QUEC_BLE_UPDATE_CONN_PARAM_IND:
  1036. {
  1037. ql_ble_update_conn_infos_s *temp = (ql_ble_update_conn_infos_s *)event_temp->param2;
  1038. ql_ble_update_conn_infos_s *ql_conn_param = (ql_ble_update_conn_infos_s *)malloc(sizeof(ql_ble_update_conn_infos_s));
  1039. if (ql_conn_param)
  1040. {
  1041. ql_conn_param->conn_id = temp->conn_id;
  1042. ql_conn_param->min_interval = temp->min_interval;
  1043. ql_conn_param->max_interval = temp->max_interval;
  1044. ql_conn_param->latency = temp->latency;
  1045. ql_conn_param->timeout = temp->timeout;
  1046. test_event.id = event_temp->id;
  1047. test_event.param2 = (uint32)ql_conn_param;
  1048. }
  1049. }
  1050. break;
  1051. case QUEC_BLE_GATT_RECV_IND:
  1052. case QUEC_BLE_GATT_RECV_READ_IND:
  1053. {
  1054. ql_ble_gatt_data_s *temp = (ql_ble_gatt_data_s *)event_temp->param2;
  1055. ql_ble_gatt_data_s *gatt = (ql_ble_gatt_data_s *)malloc(sizeof(ql_ble_gatt_data_s));
  1056. if (gatt)
  1057. {
  1058. memset(gatt, 0x00, sizeof(ql_ble_gatt_data_s));
  1059. gatt->data = malloc(temp->len);
  1060. if (gatt->data == NULL)
  1061. {
  1062. free(gatt);
  1063. return;
  1064. }
  1065. gatt->len = temp->len;
  1066. gatt->att_handle = temp->att_handle;
  1067. memcpy(gatt->data, temp->data, temp->len);
  1068. gatt->uuid_s = temp->uuid_s;
  1069. test_event.id = event_temp->id;
  1070. test_event.param2 = (uint32)gatt;
  1071. }
  1072. }
  1073. break;
  1074. case QUEC_BLESCAN_REPORT_IND:
  1075. {
  1076. ql_ble_scan_report_info_s *temp = (ql_ble_scan_report_info_s *)event_temp->param2;
  1077. ql_ble_scan_report_info_s *ql_scan_report = malloc(sizeof(ql_ble_scan_report_info_s));
  1078. if (ql_scan_report)
  1079. {
  1080. memset(ql_scan_report, 0x00, sizeof(ql_ble_scan_report_info_s));
  1081. ql_scan_report->name_length = temp->name_length;
  1082. memcpy(ql_scan_report->name, temp->name, temp->name_length);
  1083. ql_scan_report->addr_type = temp->addr_type;
  1084. memcpy(ql_scan_report->addr.addr, temp->addr.addr, QL_BT_MAC_ADDRESS_SIZE);
  1085. ql_scan_report->event_type = temp->event_type;
  1086. ql_scan_report->data_length = temp->data_length;
  1087. ql_scan_report->rssi = temp->rssi;
  1088. memcpy(ql_scan_report->raw_data, temp->raw_data, temp->data_length);
  1089. test_event.id = event_temp->id;
  1090. test_event.param2 = (uint32)ql_scan_report;
  1091. }
  1092. }
  1093. break;
  1094. case QUEC_BLE_GATT_DISCOVER_SERVICE_IND:
  1095. {
  1096. ql_blegatt_prime_service_s *temp = (ql_blegatt_prime_service_s *)event_temp->param2;
  1097. ql_blegatt_prime_service_s *ql_param = (ql_blegatt_prime_service_s *)malloc(sizeof(ql_blegatt_prime_service_s));
  1098. if (ql_param)
  1099. {
  1100. memcpy(ql_param, temp, sizeof(ql_blegatt_prime_service_s));
  1101. test_event.id = event_temp->id;
  1102. test_event.param2 = (uint32)ql_param;
  1103. }
  1104. }
  1105. break;
  1106. case QUEC_BLE_GATT_DISCOVER_CHARACTERISTIC_DATA_IND:
  1107. case QUEC_BLE_GATT_CHARA_READ_BY_UUID_IND:
  1108. case QUEC_BLE_GATT_DISCOVER_CHARA_DESC_IND:
  1109. case QUEC_BLE_GATT_CHARA_READ_IND:
  1110. case QUEC_BLE_GATT_DESC_READ_IND:
  1111. case QUEC_BLE_GATT_CHARA_MULTI_READ_IND:
  1112. case QUEC_BLE_GATT_RECV_NOTIFICATION_IND:
  1113. case QUEC_BLE_GATT_RECV_INDICATION_IND:
  1114. {
  1115. ql_att_general_rsp_s * temp = (ql_att_general_rsp_s *)event_temp->param2;
  1116. ql_att_general_rsp_s *ql_server = malloc(sizeof(ql_att_general_rsp_s));
  1117. if (ql_server)
  1118. {
  1119. ql_server->pay_load = malloc(temp->length);
  1120. if (ql_server->pay_load == NULL)
  1121. {
  1122. free(ql_server);
  1123. return;
  1124. }
  1125. ql_server->length = temp->length;
  1126. memcpy(ql_server->pay_load, temp->pay_load, ql_server->length);
  1127. test_event.id = event_temp->id;
  1128. test_event.param2 = (uint32)ql_server;
  1129. }
  1130. }
  1131. break;
  1132. default:
  1133. {
  1134. test_event.id = event_temp->id;
  1135. test_event.param1 = event_temp->param1;
  1136. test_event.param2 = event_temp->param2;
  1137. test_event.param3 = event_temp->param3;
  1138. }
  1139. break;
  1140. }
  1141. if (test_event.id != 0)
  1142. {
  1143. ql_rtos_event_send(ble_demo_task,&test_event);
  1144. }
  1145. }
  1146. void ql_ble_gatt_server_demo_task_pthread(void *ctx)
  1147. {
  1148. QlOSStatus err = 0;
  1149. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  1150. #if QL_BLE_DEMO_LOW_POWER_USE
  1151. //ql_dev_set_modem_fun(QL_DEV_CFUN_MIN, 0, 0);
  1152. ql_autosleep_enable(QL_ALLOW_SLEEP);
  1153. QL_BLE_GATT_LOG("i am in sleep mode");
  1154. #endif
  1155. ql_ble_demo_get_nv_addr();
  1156. GATT_SERVER_RETRY:
  1157. ret = ql_ble_gatt_server_init(ql_ble_notify_cb);
  1158. if (ret != QL_BT_SUCCESS)
  1159. {
  1160. goto QL_BLT_GATT_SERVER_NOT_INIT_EXIT;
  1161. }
  1162. #if QL_BLE_DEMO_LOW_POWER_USE
  1163. ql_lpm_wakelock_lock(bt_ble_power_lock);
  1164. #endif
  1165. ret = ql_bt_demo_start();
  1166. if (ret != QL_BT_SUCCESS)
  1167. {
  1168. #if QL_BLE_DEMO_LOW_POWER_USE
  1169. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  1170. #endif
  1171. goto QL_BLT_GATT_SERVER_INIT_EXIT;
  1172. }
  1173. while(1)
  1174. {
  1175. ret = ql_ble_gatt_server_handle_event();
  1176. if (ret != QL_BT_SUCCESS)
  1177. {
  1178. break;
  1179. }
  1180. }
  1181. QL_BLT_GATT_SERVER_INIT_EXIT:
  1182. ql_ble_gatt_server_release();
  1183. goto GATT_SERVER_RETRY;
  1184. QL_BLT_GATT_SERVER_NOT_INIT_EXIT:
  1185. #if QL_BLE_DEMO_LOW_POWER_USE
  1186. ql_lpm_wakelock_delete(bt_ble_power_lock);
  1187. #endif
  1188. err = ql_rtos_task_delete(NULL);
  1189. if(err != QL_OSI_SUCCESS)
  1190. {
  1191. QL_BLE_GATT_LOG("task deleted failed");
  1192. }
  1193. }
  1194. QlOSStatus ql_ble_gatt_server_demo_init(void)
  1195. {
  1196. QlOSStatus err = QL_OSI_SUCCESS;
  1197. QL_BLE_GATT_LOG("enter ql_ble_gatt_server_demo_init");
  1198. #if QL_BLE_DEMO_LOW_POWER_USE
  1199. bt_ble_power_lock = ql_lpm_wakelock_create("bt_ble", strlen("bt_ble"));
  1200. #endif
  1201. err = ql_rtos_task_create(&ble_demo_task, BT_BLE_DEMO_TASK_STACK_SIZE, BT_BLE_DEMO_TASK_PRIO, "gatt_server", ql_ble_gatt_server_demo_task_pthread, NULL, BT_BLE_DEMO_TASK_EVENT_CNT);
  1202. return err;
  1203. }
  1204. //gatt client
  1205. ql_errcode_bt_e ql_blescan_demo_set_param()
  1206. {
  1207. ql_errcode_bt_e ret;
  1208. ret = ql_blescan_set_param(ble_scan_param);
  1209. if (ret == QL_BT_SUCCESS)
  1210. {
  1211. QL_BLE_GATT_LOG("sucess");
  1212. }
  1213. else
  1214. {
  1215. QL_BLE_GATT_LOG("error=%x", ret);
  1216. }
  1217. return ret;
  1218. }
  1219. static ql_errcode_bt_e ql_blescan_demo_start()
  1220. {
  1221. ql_errcode_bt_e ret = 0;
  1222. ret = ql_blescan_start();
  1223. if (ret == QL_BT_SUCCESS)
  1224. {
  1225. QL_BLE_GATT_LOG("sucess");
  1226. }
  1227. else
  1228. {
  1229. QL_BLE_GATT_LOG("error=%x", ret);
  1230. }
  1231. return ret;
  1232. }
  1233. static ql_errcode_bt_e ql_blescan_demo_stop()
  1234. {
  1235. ql_errcode_bt_e ret;
  1236. ret = ql_blescan_stop();
  1237. if (ret == QL_BT_SUCCESS)
  1238. {
  1239. QL_BLE_GATT_LOG("sucess");
  1240. }
  1241. else
  1242. {
  1243. QL_BLE_GATT_LOG("error=%x", ret);
  1244. }
  1245. return ret;
  1246. }
  1247. ql_errcode_bt_e ql_ble_demo_connect_public_addr()
  1248. {
  1249. ql_errcode_bt_e ret;
  1250. ret = ql_ble_conncet_public_addr(ble_scan_report_addr);
  1251. if (ret == QL_BT_SUCCESS)
  1252. {
  1253. QL_BLE_GATT_LOG("sucess");
  1254. }
  1255. else
  1256. {
  1257. QL_BLE_GATT_LOG("error=%x", ret);
  1258. }
  1259. return ret;
  1260. }
  1261. ql_errcode_bt_e ql_ble_demo_connect_random_addr()
  1262. {
  1263. ql_errcode_bt_e ret;
  1264. ret = ql_ble_conncet_random_addr(ble_scan_report_addr);
  1265. if (ret == QL_BT_SUCCESS)
  1266. {
  1267. QL_BLE_GATT_LOG("sucess");
  1268. }
  1269. else
  1270. {
  1271. QL_BLE_GATT_LOG("error=%x", ret);
  1272. }
  1273. return ret;
  1274. }
  1275. ql_errcode_bt_e ql_ble_gatt_demo_discover_all_service()
  1276. {
  1277. ql_errcode_bt_e ret = 0;
  1278. ret = ql_ble_gatt_discover_all_service(ble_conn_handle);
  1279. if (ret == QL_BT_SUCCESS)
  1280. {
  1281. QL_BLE_GATT_LOG("sucess");
  1282. }
  1283. else
  1284. {
  1285. QL_BLE_GATT_LOG("error=%x", ret);
  1286. }
  1287. return ret;
  1288. }
  1289. ql_errcode_bt_e ql_ble_gatt_demo_discover_service_by_uuid()
  1290. {
  1291. ql_errcode_bt_e ret = 0;
  1292. ql_ble_gatt_uuid_s uuid = {0};
  1293. uuid.uuid_type = ql_ble_gatt_uuid_state;
  1294. if (ql_ble_gatt_uuid_state == QL_BLE_GATT_SHORT_UUID)
  1295. {
  1296. uuid.uuid_s = ql_ble_gatt_target_service.uuid;
  1297. ret = ql_ble_gatt_discover_by_uuid(ble_conn_handle, uuid);
  1298. }
  1299. else
  1300. {
  1301. memcpy(uuid.uuid_l, ql_ble_gatt_uuid_l, QL_BLE_LONG_UUID_SIZE);
  1302. ret = ql_ble_gatt_discover_by_uuid(ble_conn_handle, uuid);
  1303. }
  1304. if (ret == QL_BT_SUCCESS)
  1305. {
  1306. QL_BLE_GATT_LOG("sucess");
  1307. }
  1308. else
  1309. {
  1310. QL_BLE_GATT_LOG("error=%x", ret);
  1311. }
  1312. return ret;
  1313. }
  1314. ql_errcode_bt_e ql_ble_gatt_demo_discover_all_includes()
  1315. {
  1316. ql_errcode_bt_e ret = 0;
  1317. ret = ql_ble_gatt_discover_all_includes( ble_conn_handle, ql_ble_gatt_target_service.start_handle, ql_ble_gatt_target_service.end_handle);
  1318. if (ret == QL_BT_SUCCESS)
  1319. {
  1320. QL_BLE_GATT_LOG("sucess");
  1321. }
  1322. else
  1323. {
  1324. QL_BLE_GATT_LOG("error=%x", ret);
  1325. }
  1326. return ret;
  1327. }
  1328. ql_errcode_bt_e ql_ble_gatt_demo_discover_all_characteristic()
  1329. {
  1330. ql_errcode_bt_e ret = 0;
  1331. ret = ql_ble_gatt_discover_all_characteristic(ble_conn_handle, ql_ble_gatt_target_service.start_handle, ql_ble_gatt_target_service.end_handle);
  1332. if (ret == QL_BT_SUCCESS)
  1333. {
  1334. QL_BLE_GATT_LOG("sucess");
  1335. }
  1336. else
  1337. {
  1338. QL_BLE_GATT_LOG("error=%x", ret);
  1339. }
  1340. return ret;
  1341. }
  1342. ql_errcode_bt_e ql_ble_gatt_demo_discover_chara_desc()
  1343. {
  1344. ql_errcode_bt_e ret = 0;
  1345. QL_BLE_GATT_LOG("value_handle=%x,end_handle=%x", ql_ble_gatt_characteristic[ql_ble_gatt_chara_desc_index].value_handle,
  1346. ql_ble_gatt_target_service.end_handle);
  1347. QL_BLE_GATT_LOG("ql_ble_gatt_chara_desc_index=%d,ql_ble_gatt_chara_count=%x", ql_ble_gatt_chara_desc_index, ql_ble_gatt_chara_count);
  1348. if (ql_ble_gatt_chara_desc_index == (ql_ble_gatt_chara_count - 1))
  1349. {
  1350. ret = ql_ble_gatt_discover_chara_desc(ble_conn_handle, ql_ble_gatt_characteristic[ql_ble_gatt_chara_desc_index].value_handle+1,
  1351. ql_ble_gatt_target_service.end_handle);
  1352. }
  1353. else
  1354. {
  1355. ret = ql_ble_gatt_discover_chara_desc(ble_conn_handle, ql_ble_gatt_characteristic[ql_ble_gatt_chara_desc_index].value_handle+1,
  1356. ql_ble_gatt_characteristic[ql_ble_gatt_chara_desc_index+1].handle-1);
  1357. }
  1358. ql_ble_gatt_chara_desc_index++;
  1359. if (ret == QL_BT_SUCCESS)
  1360. {
  1361. QL_BLE_GATT_LOG("sucess");
  1362. }
  1363. else
  1364. {
  1365. QL_BLE_GATT_LOG("error=%x", ret);
  1366. }
  1367. return ret;
  1368. }
  1369. ql_errcode_bt_e ql_ble_gatt_demo_read_chara_value_by_uuid()
  1370. {
  1371. ql_errcode_bt_e ret = 0;
  1372. ql_ble_gatt_uuid_s uuid =
  1373. {
  1374. .uuid_type = 1,
  1375. .uuid_l = {0x00},
  1376. .uuid_s = 0,
  1377. };
  1378. #if 0
  1379. QL_BT_LOG("uuid=%x,start_handle=%x,end_handle=%x", ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].uuid, ql_ble_gatt_target_service.start_handle,
  1380. ql_ble_gatt_target_service.end_handle);
  1381. ret = ql_ble_gatt_read_chara_value_by_uuid(ble_conn_handle, NULL, ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].uuid, ql_ble_gatt_target_service.start_handle,
  1382. ql_ble_gatt_target_service.end_handle);
  1383. #endif
  1384. QL_BLE_GATT_LOG("uuid=%x,handle=%x,value_handle=%x", ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].uuid, ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].handle,
  1385. ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle);
  1386. uuid.uuid_s = ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].uuid;
  1387. ret = ql_ble_gatt_read_chara_value_by_uuid(ble_conn_handle, uuid, ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].handle,
  1388. ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle);
  1389. if (ret == QL_BT_SUCCESS)
  1390. {
  1391. QL_BLE_GATT_LOG("sucess");
  1392. }
  1393. else
  1394. {
  1395. QL_BLE_GATT_LOG("error=%x", ret);
  1396. }
  1397. return ret;
  1398. }
  1399. ql_errcode_bt_e ql_ble_gatt_demo_read_chara_value_by_handle()
  1400. {
  1401. ql_errcode_bt_e ret = 0;
  1402. QL_BLE_GATT_LOG("value_handle=%x", ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle);
  1403. ret = ql_ble_gatt_read_chara_value_by_handle(ble_conn_handle, ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle, 0, 0);
  1404. if (ret == QL_BT_SUCCESS)
  1405. {
  1406. QL_BLE_GATT_LOG("sucess");
  1407. }
  1408. else
  1409. {
  1410. QL_BLE_GATT_LOG("error=%x", ret);
  1411. }
  1412. return ret;
  1413. }
  1414. ql_errcode_bt_e ql_ble_gatt_demo_read_mul_chara_value()
  1415. {
  1416. ql_errcode_bt_e ret = 0;
  1417. QL_BLE_GATT_LOG("value_handle=%x", ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle);
  1418. ret = ql_ble_gatt_read_mul_chara_value(ble_conn_handle, (unsigned char *)&ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle, 2);
  1419. if (ret == QL_BT_SUCCESS)
  1420. {
  1421. QL_BLE_GATT_LOG("sucess");
  1422. }
  1423. else
  1424. {
  1425. QL_BLE_GATT_LOG("error=%x", ret);
  1426. }
  1427. return ret;
  1428. }
  1429. ql_errcode_bt_e ql_ble_gatt_demo_read_chara_desc()
  1430. {
  1431. ql_errcode_bt_e ret = 0;
  1432. QL_BLE_GATT_LOG("handle=%x", ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].handle);
  1433. ret = ql_ble_gatt_read_chara_desc(ble_conn_handle, ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].handle, 0);
  1434. if (ret == QL_BT_SUCCESS)
  1435. {
  1436. QL_BLE_GATT_LOG("sucess");
  1437. }
  1438. else
  1439. {
  1440. QL_BLE_GATT_LOG("error=%x", ret);
  1441. }
  1442. return ret;
  1443. }
  1444. ql_errcode_bt_e ql_ble_gatt_demo_write_chara_desc()
  1445. {
  1446. ql_errcode_bt_e ret = 0;
  1447. unsigned char value[2] = {0x01,0x02};
  1448. //value[2] = (ble_conn_handle >>8) & 0xFF;
  1449. //value[3] = ble_conn_handle & 0xFF;
  1450. QL_BLE_GATT_LOG("handle=%x", ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].handle);
  1451. ret = ql_ble_gatt_write_chara_desc(ble_conn_handle, ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].handle, sizeof(value), value);
  1452. if (ret == QL_BT_SUCCESS)
  1453. {
  1454. QL_BLE_GATT_LOG("sucess");
  1455. }
  1456. else
  1457. {
  1458. QL_BLE_GATT_LOG("error=%x", ret);
  1459. }
  1460. return ret;
  1461. }
  1462. ql_errcode_bt_e ql_ble_gatt_demo_write_chara_value()
  1463. {
  1464. ql_errcode_bt_e ret = 0;
  1465. unsigned char value[2] = {0x40};
  1466. QL_BLE_GATT_LOG("value_handle=%x,uuid=%x", ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle,
  1467. ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].uuid);
  1468. ret = ql_ble_gatt_write_chara_value(ble_conn_handle, ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle,
  1469. sizeof(value), value, 0, 0);
  1470. if (ret == QL_BT_SUCCESS)
  1471. {
  1472. QL_BLE_GATT_LOG("sucess");
  1473. }
  1474. else
  1475. {
  1476. QL_BLE_GATT_LOG("error=%x", ret);
  1477. }
  1478. return ret;
  1479. }
  1480. ql_errcode_bt_e ql_ble_gatt_demo_write_chara_value_no_rsp()
  1481. {
  1482. ql_errcode_bt_e ret = 0;
  1483. unsigned char value[1] = {0x20};
  1484. QL_BLE_GATT_LOG("value_handle=%x,uuid=%x", ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle,
  1485. ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].uuid);
  1486. ret = ql_ble_gatt_write_chara_value_no_rsp(ble_conn_handle, ql_ble_gatt_characteristic[ql_ble_gatt_cur_chara].value_handle,
  1487. sizeof(value), value);
  1488. if (ret == QL_BT_SUCCESS)
  1489. {
  1490. QL_BLE_GATT_LOG("sucess");
  1491. }
  1492. else
  1493. {
  1494. QL_BLE_GATT_LOG("error=%x", ret);
  1495. }
  1496. return ret;
  1497. }
  1498. /************************************************************************
  1499. * QUEC_BLE_CONNECT_IND
  1500. * QUEC_BLE_DISCONNECT_IND
  1501. * QUEC_BLE_UPDATE_CONN_PARAM_IND
  1502. * QUEC_BLESCAN_REPORT_IND
  1503. * QUEC_BLE_GATT_RECV_NOTIFICATION_IND
  1504. * QUEC_BLE_GATT_RECV_INDICATION_IND
  1505. * QUEC_BLE_GATT_DISCOVER_SERVICE_IND
  1506. * QUEC_BLE_GATT_DISCOVER_CHARACTERISTIC_DATA_IND
  1507. * QUEC_BLE_GATT_DISCOVER_CHARA_DESC_IND
  1508. * QUEC_BLE_GATT_CHARA_READ_IND
  1509. * QUEC_BLE_GATT_CHARA_READ_BY_UUID_IND
  1510. * QUEC_BLE_GATT_CHARA_MULTI_READ_IND
  1511. * QUEC_BLE_GATT_DESC_READ_IND
  1512. ************************************************************************/
  1513. static ql_errcode_bt_e ql_ble_gatt_client_handle_event()
  1514. {
  1515. ql_event_t test_event;
  1516. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  1517. QL_BT_STATUS status;
  1518. if (ql_event_try_wait(&test_event) == 0)
  1519. {
  1520. if (test_event.id == 0)
  1521. {
  1522. return ret;
  1523. }
  1524. status = (QL_BT_STATUS)(test_event.param1);
  1525. switch (test_event.id)
  1526. {
  1527. case QUEC_BT_START_STATUS_IND:
  1528. {
  1529. if (QL_BT_STATUS_SUCCESS == status)
  1530. {
  1531. QL_BLE_GATT_LOG("start sucess");
  1532. #if QL_BLE_DEMO_LOW_POWER_USE
  1533. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  1534. #endif
  1535. ret = ql_bt_demo_get_state();
  1536. if (ret != QL_BT_SUCCESS)
  1537. {
  1538. goto QL_BLE_SCAN_DEMO_STOP;
  1539. }
  1540. ret = ql_blescan_demo_set_param();
  1541. if (ret != QL_BT_SUCCESS)
  1542. {
  1543. goto QL_BLE_SCAN_DEMO_STOP;
  1544. }
  1545. ret = ql_blescan_demo_start();
  1546. if (ret != QL_BT_SUCCESS)
  1547. {
  1548. goto QL_BLE_SCAN_DEMO_STOP;
  1549. }
  1550. }
  1551. else
  1552. {
  1553. QL_BLE_GATT_LOG("start failed");
  1554. }
  1555. }
  1556. break;
  1557. case QUEC_BT_STOP_STATUS_IND:
  1558. {
  1559. #if QL_BLE_DEMO_LOW_POWER_USE
  1560. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  1561. #endif
  1562. if (QL_BT_STATUS_SUCCESS == status)
  1563. {
  1564. QL_BLE_GATT_LOG("stop sucess");
  1565. ret = QL_BT_ALREADY_STOPED_ERR;
  1566. }
  1567. else
  1568. {
  1569. QL_BLE_GATT_LOG("stop failed");
  1570. ret = QL_BT_ALREADY_STOPED_ERR;
  1571. }
  1572. }
  1573. break;
  1574. case QUEC_BT_BLE_RESET_IND:
  1575. {
  1576. #if QL_BLE_DEMO_LOW_POWER_USE
  1577. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  1578. #endif
  1579. QL_BLE_GATT_LOG("bt reset");
  1580. ret = QL_BT_ALREADY_STOPED_ERR;
  1581. }
  1582. break;
  1583. case QUEC_BLESCAN_REPORT_IND:
  1584. {
  1585. //scan and report other devices
  1586. if (QL_BT_STATUS_SUCCESS == status)
  1587. {
  1588. QL_BLE_GATT_LOG("scan report sucess");
  1589. ql_ble_scan_report_info_s *bt_scan_report = NULL;
  1590. bt_scan_report = (ql_ble_scan_report_info_s *)test_event.param2;
  1591. if (bt_scan_report)
  1592. {
  1593. char scan_rsp_info[128] = {'\0'};
  1594. sprintf(scan_rsp_info, "%s:%d,%02x:%02x:%02x:%02x:%02x:%02x", bt_scan_report->name, bt_scan_report->addr_type, bt_scan_report->addr.addr[0], bt_scan_report->addr.addr[1], bt_scan_report->addr.addr[2], bt_scan_report->addr.addr[3], bt_scan_report->addr.addr[4], bt_scan_report->addr.addr[5]);
  1595. QL_BLE_GATT_LOG("scan_rsp_info=%s,rssi=%d", scan_rsp_info, bt_scan_report->rssi);
  1596. if (0 == memcmp(ble_scan_report_name, bt_scan_report->name, sizeof(ble_scan_report_name)))
  1597. {
  1598. ble_scan_report_addr_type = bt_scan_report->addr_type;
  1599. memcpy(ble_scan_report_addr.addr, bt_scan_report->addr.addr, QL_BT_MAC_ADDRESS_SIZE);
  1600. ret = ql_blescan_demo_stop();
  1601. if (ret != QL_BT_SUCCESS)
  1602. {
  1603. goto QL_BLE_SCAN_DEMO_STOP;
  1604. }
  1605. if (ble_scan_report_addr_type == QL_BLE_PUBLIC_ADDRESS)
  1606. {
  1607. ret = ql_ble_demo_connect_public_addr();
  1608. }
  1609. else
  1610. {
  1611. ret = ql_ble_demo_connect_random_addr();
  1612. }
  1613. if (ret != QL_BT_SUCCESS)
  1614. {
  1615. goto QL_BLE_SCAN_DEMO_STOP;
  1616. }
  1617. }
  1618. free(bt_scan_report);
  1619. }
  1620. }
  1621. else
  1622. {
  1623. QL_BLE_GATT_LOG("scan report failed");
  1624. ret = ql_blescan_demo_stop();
  1625. if (ret != QL_BT_SUCCESS)
  1626. {
  1627. goto QL_BLE_SCAN_DEMO_STOP;
  1628. }
  1629. }
  1630. }
  1631. break;
  1632. case QUEC_BLE_CONNECT_IND:
  1633. {
  1634. if (QL_BT_STATUS_SUCCESS == status)
  1635. {
  1636. QL_BLE_GATT_LOG("connect sucess");
  1637. ql_bt_addr_s * addr = (ql_bt_addr_s *)test_event.param2;
  1638. if (addr)
  1639. {
  1640. //addr need to free
  1641. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  1642. memset(addr_string,0x00,sizeof(addr_string));
  1643. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], addr->addr[4], addr->addr[5]);
  1644. QL_BLE_GATT_LOG("addr_string=%s", addr_string);
  1645. memcpy(ble_connection_addr.addr, addr->addr, QL_BT_MAC_ADDRESS_SIZE);
  1646. free(addr);
  1647. }
  1648. ble_conn_handle = test_event.param3;
  1649. QL_BLE_GATT_LOG("ble_conn_handle=%d", ble_conn_handle);
  1650. ret = ql_ble_demo_get_conection_state();
  1651. if (ret == QL_BT_SUCCESS)
  1652. {
  1653. //ret = ql_ble_demo_send_data();
  1654. //if (ret != QL_BT_SUCCESS)
  1655. //{
  1656. // goto QL_BLE_SCAN_DEMO_STOP;
  1657. //}
  1658. }
  1659. else
  1660. {
  1661. goto QL_BLE_SCAN_DEMO_STOP;
  1662. }
  1663. }
  1664. else
  1665. {
  1666. QL_BLE_GATT_LOG("connect failed");
  1667. //goto QL_BLE_SCAN_DEMO_STOP;
  1668. }
  1669. }
  1670. break;
  1671. case QUEC_BLE_UPDATE_CONN_PARAM_IND:
  1672. {
  1673. //update connection parameter
  1674. if (QL_BT_STATUS_SUCCESS == status)
  1675. {
  1676. QL_BLE_GATT_LOG("update conn param sucess");
  1677. ql_ble_update_conn_infos_s *conn_param = (ql_ble_update_conn_infos_s *)test_event.param2;
  1678. if (conn_param)
  1679. {
  1680. QL_BLE_GATT_LOG("conn_id=%d,min_interval=%d,max_interval=%d,latency=%d,timeout=%d", conn_param->conn_id, \
  1681. conn_param->min_interval, conn_param->max_interval, conn_param->latency, conn_param->timeout);
  1682. free(conn_param);
  1683. }
  1684. #if 0
  1685. ret = ql_ble_demo_send_data();
  1686. if (ret == QL_BT_SUCCESS)
  1687. {
  1688. ret = ql_ble_demo_disconect_device();
  1689. if (ret != QL_BT_SUCCESS)
  1690. {
  1691. goto QL_BLE_SCAN_DEMO_STOP;
  1692. }
  1693. }
  1694. else
  1695. {
  1696. goto QL_BLE_SCAN_DEMO_STOP;
  1697. }
  1698. #endif
  1699. }
  1700. else
  1701. {
  1702. QL_BLE_GATT_LOG("update conn param failed");
  1703. goto QL_BLE_SCAN_DEMO_STOP;
  1704. }
  1705. }
  1706. break;
  1707. case QUEC_BLE_GATT_MTU:
  1708. {
  1709. if (QL_BT_STATUS_SUCCESS == status)
  1710. {
  1711. QL_BLE_GATT_LOG("connect mtu sucess,handle=%d,mtu=%d", test_event.param2, test_event.param3);
  1712. ql_bt_ble_mtu = test_event.param3;
  1713. }
  1714. else
  1715. {
  1716. QL_BLE_GATT_LOG("connect mtu failed");
  1717. goto QL_BLE_SCAN_DEMO_STOP;
  1718. }
  1719. }
  1720. break;
  1721. case QUEC_BLE_DISCONNECT_IND:
  1722. {
  1723. if (QL_BT_STATUS_SUCCESS == status)
  1724. {
  1725. QL_BLE_GATT_LOG("disconnect sucess");
  1726. ql_bt_addr_s * addr = (ql_bt_addr_s *)test_event.param2;
  1727. if (addr)
  1728. {
  1729. //addr need to free
  1730. unsigned char addr_string[QL_BLE_DEMO_ADDR_MAX_SIZE + 1] = {0};
  1731. memset(addr_string,0x00,sizeof(addr_string));
  1732. sprintf((char *)addr_string, "%02x:%02x:%02x:%02x:%02x:%02x", addr->addr[0], addr->addr[1], addr->addr[2], addr->addr[3], addr->addr[4], addr->addr[5]);
  1733. QL_BLE_GATT_LOG("addr_string=%s", addr_string);
  1734. free(addr);
  1735. }
  1736. goto QL_BLE_SCAN_DEMO_STOP;
  1737. }
  1738. else
  1739. {
  1740. QL_BLE_GATT_LOG("disconnect failed");
  1741. goto QL_BLE_SCAN_DEMO_STOP;
  1742. }
  1743. }
  1744. break;
  1745. case QUEC_BLE_GATT_START_DISCOVER_SERVICE_IND:
  1746. {
  1747. //discover all service or discover service by uuid
  1748. if (QL_BT_STATUS_SUCCESS == status)
  1749. {
  1750. QL_BLE_GATT_LOG("start discover sucess");
  1751. ql_ble_gatt_chara_count = 0;
  1752. ql_ble_gatt_chara_desc_index = 0;
  1753. ql_ble_gatt_chara_desc_count = 0;
  1754. ql_ble_gatt_state = QL_BLE_GATT_DISCOVER_SERVICE;
  1755. if (ql_ble_gatt_discover_service_mode == QL_BLE_GATT_DISCOVER_SERVICE_ALL)
  1756. {
  1757. ret = ql_ble_gatt_demo_discover_all_service();
  1758. }
  1759. else
  1760. {
  1761. ret = ql_ble_gatt_demo_discover_service_by_uuid();
  1762. }
  1763. if (ret != QL_BT_SUCCESS)
  1764. {
  1765. goto QL_BLE_SCAN_DEMO_STOP;
  1766. }
  1767. }
  1768. else
  1769. {
  1770. QL_BLE_GATT_LOG("start discover failed");
  1771. goto QL_BLE_SCAN_DEMO_STOP;
  1772. }
  1773. }
  1774. break;
  1775. case QUEC_BLE_GATT_DISCOVER_SERVICE_IND:
  1776. {
  1777. //discorver service
  1778. if (QL_BT_STATUS_SUCCESS == status)
  1779. {
  1780. QL_BLE_GATT_LOG("discover servie sucess");
  1781. ql_blegatt_prime_service_s *service = (ql_blegatt_prime_service_s *)test_event.param2;
  1782. if (service)
  1783. {
  1784. if (ql_ble_gatt_discover_service_mode == QL_BLE_GATT_DISCOVER_SERVICE_ALL)
  1785. {
  1786. QL_BLE_GATT_LOG("start_handle=%x,end_handle=%x,uuid=%x", service->start_handle, service->end_handle, service->uuid);
  1787. if (ql_ble_gatt_target_service.uuid == service->uuid)
  1788. {
  1789. ql_ble_gatt_target_service.start_handle = service->start_handle;
  1790. ql_ble_gatt_target_service.end_handle = service->end_handle;
  1791. //ql_ble_gatt_demo_discover_all_includes();
  1792. ql_ble_gatt_state = QL_BLE_GATT_DISCOVER_CHARACTERISTIC;
  1793. ret = ql_ble_gatt_demo_discover_all_characteristic();
  1794. if (ret != QL_BT_SUCCESS)
  1795. {
  1796. goto QL_BLE_SCAN_DEMO_STOP;
  1797. }
  1798. }
  1799. }
  1800. else
  1801. {
  1802. QL_BLE_GATT_LOG("start_handle=%x,end_handle=%x", service->start_handle, service->end_handle);
  1803. ql_ble_gatt_target_service.start_handle = service->start_handle;
  1804. ql_ble_gatt_target_service.end_handle = service->end_handle;
  1805. ql_ble_gatt_state = QL_BLE_GATT_DISCOVER_CHARACTERISTIC;
  1806. ret = ql_ble_gatt_demo_discover_all_characteristic();
  1807. if (ret != QL_BT_SUCCESS)
  1808. {
  1809. goto QL_BLE_SCAN_DEMO_STOP;
  1810. }
  1811. }
  1812. free(service);
  1813. }
  1814. }
  1815. else
  1816. {
  1817. QL_BLE_GATT_LOG("discover servie failed");
  1818. goto QL_BLE_SCAN_DEMO_STOP;
  1819. }
  1820. }
  1821. break;
  1822. case QUEC_BLE_GATT_DISCOVER_CHARACTERISTIC_DATA_IND:
  1823. {
  1824. //discover characteristic
  1825. if (QL_BT_STATUS_SUCCESS == status)
  1826. {
  1827. QL_BLE_GATT_LOG("get charactersictic sucess");
  1828. ql_att_general_rsp_s * last_rsp = (ql_att_general_rsp_s *)test_event.param2;
  1829. int i = 0;
  1830. if (last_rsp && last_rsp->pay_load)
  1831. {
  1832. if (ql_ble_gatt_state == QL_BLE_GATT_DISCOVER_CHARACTERISTIC)
  1833. {
  1834. unsigned char pair_len = last_rsp->pay_load[0];
  1835. QL_BLE_GATT_LOG("pair_len=%d,last_rsp.length=%d", pair_len, (int)last_rsp->length);
  1836. while (i < (last_rsp->length - 1) / pair_len)
  1837. {
  1838. ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].handle = (last_rsp->pay_load[i * pair_len + 2] << 8) | last_rsp->pay_load[i * pair_len + 1];
  1839. ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].properties = last_rsp->pay_load[i * pair_len + 3];
  1840. ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].value_handle = (last_rsp->pay_load[i * pair_len + 5] << 8) | last_rsp->pay_load[i * pair_len + 4];
  1841. if (pair_len == QL_BLE_SHORT_UUID_PAIR_LEN)
  1842. {
  1843. ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].uuid = (last_rsp->pay_load[i * pair_len + 7] << 8) | last_rsp->pay_load[i * pair_len + 6];
  1844. QL_BLE_GATT_LOG("uuid = %x", ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].uuid);
  1845. }
  1846. else if (pair_len == QL_BLE_LONG_UUID_PAIR_LEN)
  1847. {
  1848. memcpy(ql_ble_gatt_uuid_l, &last_rsp->pay_load[i * pair_len + 6], QL_BLE_LONG_UUID_SIZE);
  1849. }
  1850. QL_BLE_GATT_LOG("handle=0x%x,properties=0x%x,value_handle=0x%x", ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].handle,
  1851. ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].properties, ql_ble_gatt_characteristic[ql_ble_gatt_chara_count].value_handle);
  1852. i++;
  1853. if (ql_ble_gatt_chara_count < QL_BLE_CHARA_NUM_MAX)
  1854. {
  1855. ql_ble_gatt_chara_count++;
  1856. }
  1857. }
  1858. QL_BLE_GATT_LOG("ql_ble_gatt_chara_count=%x", ql_ble_gatt_chara_count);
  1859. }
  1860. else if (ql_ble_gatt_state == QL_BLE_GATT_READ_CHARA_VALUE)
  1861. {
  1862. QL_BLE_GATT_LOG("last_rsp.length=%d", (int)last_rsp->length);
  1863. QL_BLE_GATT_LOG("last_rsp.pay_load=%x,%x,%x,%x", last_rsp->pay_load[0], last_rsp->pay_load[1], last_rsp->pay_load[2], last_rsp->pay_load[3]);
  1864. }
  1865. free(last_rsp->pay_load);
  1866. free(last_rsp);
  1867. }
  1868. }
  1869. else
  1870. {
  1871. QL_BLE_GATT_LOG("get charactersictic failed");
  1872. goto QL_BLE_SCAN_DEMO_STOP;
  1873. }
  1874. }
  1875. break;
  1876. case QUEC_BLE_GATT_DISCOVER_CHARA_DESC_IND:
  1877. {
  1878. //discover characteristic descriptor
  1879. if (QL_BT_STATUS_SUCCESS == status)
  1880. {
  1881. QL_BLE_GATT_LOG("get chara desc sucess");
  1882. ql_att_general_rsp_s * last_rsp = (ql_att_general_rsp_s *)test_event.param2;
  1883. if (last_rsp && last_rsp->pay_load)
  1884. {
  1885. unsigned char fmt = last_rsp->pay_load[0];
  1886. unsigned char length = last_rsp->length;
  1887. int i = 0;
  1888. QL_BLE_GATT_LOG("fmt=%d,length=%d", fmt, length);
  1889. //fmt:1 - 16 bit uuid
  1890. if (fmt == 1)
  1891. {
  1892. while (i < (length - 1) / 4)
  1893. {
  1894. ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].handle= (last_rsp->pay_load[i * 4 + 2] << 8) | last_rsp->pay_load[i * 4 + 1];
  1895. ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].uuid= (last_rsp->pay_load[i * 4 + 4] << 8) | last_rsp->pay_load[i * 4 + 3];
  1896. QL_BLE_GATT_LOG("handle=%x,uuid=%x", ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].handle, ql_ble_gatt_chara_desc[ql_ble_gatt_chara_cur_desc].uuid);
  1897. i++;
  1898. if (ql_ble_gatt_chara_desc_count < QL_BLE_DESC_NUM_MAX)
  1899. {
  1900. ql_ble_gatt_chara_desc_count++;
  1901. }
  1902. }
  1903. }
  1904. QL_BLE_GATT_LOG("ql_ble_gatt_chara_desc_count=%x", ql_ble_gatt_chara_desc_count);
  1905. free(last_rsp->pay_load);
  1906. free(last_rsp);
  1907. }
  1908. if (ql_ble_gatt_chara_desc_index == ql_ble_gatt_chara_count)
  1909. {
  1910. ql_ble_gatt_state = QL_BLE_GATT_WRITE_CHARA_VALUE;
  1911. //ret = ql_ble_gatt_demo_write_chara_value();
  1912. //ret = ql_ble_gatt_demo_write_chara_value_no_rsp();
  1913. ql_ble_gatt_state = QL_BLE_GATT_READ_CHARA_VALUE;
  1914. ret = ql_ble_gatt_demo_read_chara_value_by_uuid();
  1915. //ret = ql_ble_gatt_demo_read_chara_value_by_handle();
  1916. //ret = ql_ble_gatt_demo_read_mul_chara_value();
  1917. //ql_ble_gatt_state = QL_BLE_GATT_READ_CHARA_DESC;
  1918. //ret = ql_ble_gatt_demo_read_chara_desc();
  1919. //ql_ble_gatt_state = QL_BLE_GATT_WRITE_CHARA_DESC;
  1920. //ret = ql_ble_gatt_demo_write_chara_desc();
  1921. }
  1922. else
  1923. {
  1924. ret = ql_ble_gatt_demo_discover_chara_desc();
  1925. }
  1926. if (ret != QL_BT_SUCCESS)
  1927. {
  1928. goto QL_BLE_SCAN_DEMO_STOP;
  1929. }
  1930. }
  1931. else
  1932. {
  1933. QL_BLE_GATT_LOG("get chara desc failed");
  1934. goto QL_BLE_SCAN_DEMO_STOP;
  1935. }
  1936. }
  1937. break;
  1938. case QUEC_BLE_GATT_CHARA_WRITE_WITH_RSP_IND:
  1939. case QUEC_BLE_GATT_DESC_WRITE_WITH_RSP_IND:
  1940. {
  1941. //write charactersictic value with response
  1942. //write charactersictic descricptor with response
  1943. QL_BLE_GATT_LOG("chara write sucess");
  1944. if (QL_BT_STATUS_SUCCESS == status)
  1945. {
  1946. if (ql_ble_gatt_state == QL_BLE_GATT_WRITE_CHARA_VALUE)
  1947. {
  1948. }
  1949. else if (ql_ble_gatt_state == QL_BLE_GATT_WRITE_CHARA_DESC)
  1950. {
  1951. }
  1952. }
  1953. else
  1954. {
  1955. QL_BLE_GATT_LOG("chara write failed");
  1956. }
  1957. }
  1958. break;
  1959. case QUEC_BLE_GATT_CHARA_WRITE_WITHOUT_RSP_IND:
  1960. {
  1961. //write characteristic value without response
  1962. if (QL_BT_STATUS_SUCCESS == status)
  1963. {
  1964. QL_BLE_GATT_LOG("no rsp chara write sucess");
  1965. }
  1966. else
  1967. {
  1968. QL_BLE_GATT_LOG("no rsp chara write failed");
  1969. }
  1970. }
  1971. break;
  1972. case QUEC_BLE_GATT_CHARA_READ_IND:
  1973. case QUEC_BLE_GATT_DESC_READ_IND:
  1974. {
  1975. //read characteristic value by handle
  1976. //read characteristic descriptor
  1977. if (QL_BT_STATUS_SUCCESS == status)
  1978. {
  1979. QL_BLE_GATT_LOG("chara read sucess");
  1980. ql_att_general_rsp_s * last_rsp = (ql_att_general_rsp_s *)test_event.param2;
  1981. if (last_rsp && last_rsp->pay_load)
  1982. {
  1983. unsigned short length = last_rsp->length;
  1984. QL_BLE_GATT_LOG("length=%d", length);
  1985. if (ql_ble_gatt_state == QL_BLE_GATT_READ_CHARA_VALUE)
  1986. {
  1987. QL_BLE_GATT_LOG("last_rsp.pay_load=%x", last_rsp->pay_load[0]);
  1988. }
  1989. else if (ql_ble_gatt_state == QL_BLE_GATT_READ_CHARA_DESC)
  1990. {
  1991. QL_BLE_GATT_LOG("last_rsp.pay_load=%x,%x", last_rsp->pay_load[0], last_rsp->pay_load[1]);
  1992. }
  1993. free(last_rsp->pay_load);
  1994. free(last_rsp);
  1995. }
  1996. }
  1997. else
  1998. {
  1999. QL_BLE_GATT_LOG("chara read failed");
  2000. }
  2001. }
  2002. break;
  2003. case QUEC_BLE_GATT_CHARA_READ_BY_UUID_IND:
  2004. {
  2005. //read characteristic value by uuid
  2006. if (QL_BT_STATUS_SUCCESS == status)
  2007. {
  2008. QL_BLE_GATT_LOG("chara read sucess");
  2009. ql_att_general_rsp_s * last_rsp = (ql_att_general_rsp_s *)test_event.param2;
  2010. if (last_rsp && last_rsp->pay_load)
  2011. {
  2012. unsigned short length = length;
  2013. QL_BLE_GATT_LOG("length=%d", length);
  2014. unsigned short handle = (last_rsp->pay_load[2] << 8) | last_rsp->pay_load[1];
  2015. QL_BLE_GATT_LOG("handle=%d", handle);
  2016. free(last_rsp->pay_load);
  2017. free(last_rsp);
  2018. }
  2019. }
  2020. else
  2021. {
  2022. QL_BLE_GATT_LOG("chara read failed");
  2023. }
  2024. }
  2025. break;
  2026. case QUEC_BLE_GATT_CHARA_MULTI_READ_IND:
  2027. {
  2028. //read miltiple characteristic value
  2029. if (QL_BT_STATUS_SUCCESS == status)
  2030. {
  2031. QL_BLE_GATT_LOG("chara multi read sucess");
  2032. ql_att_general_rsp_s * last_rsp = (ql_att_general_rsp_s *)test_event.param2;
  2033. if (last_rsp && last_rsp->pay_load)
  2034. {
  2035. unsigned char length = last_rsp->length;
  2036. QL_BLE_GATT_LOG("length=%d", length);
  2037. QL_BLE_GATT_LOG("last_rsp.pay_load=%x", last_rsp->pay_load[0]);
  2038. free(last_rsp->pay_load);
  2039. free(last_rsp);
  2040. }
  2041. }
  2042. else
  2043. {
  2044. QL_BLE_GATT_LOG("chara multi read failed");
  2045. }
  2046. }
  2047. break;
  2048. case QUEC_BLE_GATT_ATT_ERROR_IND:
  2049. {
  2050. //attribute error
  2051. if (QL_BT_STATUS_SUCCESS == status)
  2052. {
  2053. UINT8 error_code = test_event.param2;
  2054. QL_BLE_GATT_LOG("get att error sucess error_code=%x", error_code);
  2055. if (ql_ble_gatt_state == QL_BLE_GATT_DISCOVER_INCLUDES)
  2056. {
  2057. ql_ble_gatt_state = QL_BLE_GATT_DISCOVER_CHARACTERISTIC;
  2058. ret = ql_ble_gatt_demo_discover_all_characteristic();
  2059. if (ret != QL_BT_SUCCESS)
  2060. {
  2061. goto QL_BLE_SCAN_DEMO_STOP;
  2062. }
  2063. }
  2064. else if (ql_ble_gatt_state == QL_BLE_GATT_DISCOVER_CHARACTERISTIC)
  2065. {
  2066. ql_ble_gatt_state = QL_BLE_GATT_IDLE;
  2067. ret = ql_ble_gatt_demo_discover_chara_desc();
  2068. if (ret != QL_BT_SUCCESS)
  2069. {
  2070. goto QL_BLE_SCAN_DEMO_STOP;
  2071. }
  2072. }
  2073. }
  2074. else
  2075. {
  2076. QL_BLE_GATT_LOG("get att error failed");
  2077. goto QL_BLE_SCAN_DEMO_STOP;
  2078. }
  2079. }
  2080. break;
  2081. case QUEC_BLE_GATT_RECV_NOTIFICATION_IND:
  2082. {
  2083. //recieve notification
  2084. if (QL_BT_STATUS_SUCCESS == status)
  2085. {
  2086. QL_BLE_GATT_LOG("recv notifi sucess");
  2087. ql_att_general_rsp_s * last_rsp = (ql_att_general_rsp_s *)test_event.param2;
  2088. if (last_rsp && last_rsp->pay_load)
  2089. {
  2090. unsigned short handle = (last_rsp->pay_load[1] << 8) | last_rsp->pay_load[0];
  2091. QL_BLE_GATT_LOG("length=%d,handle=%d,pay_load=%x,%x", last_rsp->length, handle, last_rsp->pay_load[2], last_rsp->pay_load[3]);
  2092. free(last_rsp->pay_load);
  2093. free(last_rsp);
  2094. }
  2095. }
  2096. else
  2097. {
  2098. QL_BLE_GATT_LOG("recv notifi failed");
  2099. }
  2100. }
  2101. break;
  2102. case QUEC_BLE_GATT_RECV_INDICATION_IND:
  2103. {
  2104. //recieve indication
  2105. if (QL_BT_STATUS_SUCCESS == status)
  2106. {
  2107. QL_BLE_GATT_LOG("recv notifi sucess");
  2108. ql_att_general_rsp_s * last_rsp = (ql_att_general_rsp_s *)test_event.param2;
  2109. if (last_rsp && last_rsp->pay_load)
  2110. {
  2111. unsigned short length = last_rsp->length;
  2112. QL_BLE_GATT_LOG("length=%d", length);
  2113. QL_BLE_GATT_LOG("last_rsp.pay_load=%x,%x,%x,%x", last_rsp->pay_load[0], last_rsp->pay_load[1], last_rsp->pay_load[2], last_rsp->pay_load[3]);
  2114. free(last_rsp->pay_load);
  2115. free(last_rsp);
  2116. }
  2117. }
  2118. else
  2119. {
  2120. QL_BLE_GATT_LOG("recv notifi failed");
  2121. }
  2122. }
  2123. break;
  2124. default:
  2125. break;
  2126. }
  2127. return ret;
  2128. QL_BLE_SCAN_DEMO_STOP:
  2129. #if QL_BLE_DEMO_LOW_POWER_USE
  2130. ql_lpm_wakelock_lock(bt_ble_power_lock);
  2131. #endif
  2132. ret = ql_bt_demo_stop();
  2133. }
  2134. return ret;
  2135. }
  2136. void ql_ble_gatt_client_demo_task_pthread(void *ctx)
  2137. {
  2138. QlOSStatus err = 0;
  2139. ql_errcode_bt_e ret = QL_BT_SUCCESS;
  2140. #if QL_BLE_DEMO_LOW_POWER_USE
  2141. ql_dev_set_modem_fun(QL_DEV_CFUN_MIN, 0, 0);
  2142. ql_autosleep_enable(QL_ALLOW_SLEEP);
  2143. QL_BLE_GATT_LOG("i am in sleep mode");
  2144. #endif
  2145. GATT_CLIENT_RETRY:
  2146. ret = ql_ble_gatt_client_init(ql_ble_notify_cb);
  2147. if (ret != QL_BT_SUCCESS)
  2148. {
  2149. goto QL_BLE_GATT_CLINET_NOT_INIT_EXIT;
  2150. }
  2151. #if QL_BLE_DEMO_LOW_POWER_USE
  2152. ql_lpm_wakelock_lock(bt_ble_power_lock);
  2153. #endif
  2154. ret = ql_bt_demo_start();
  2155. if (ret != QL_BT_SUCCESS)
  2156. {
  2157. #if QL_BLE_DEMO_LOW_POWER_USE
  2158. ql_lpm_wakelock_unlock(bt_ble_power_lock);
  2159. #endif
  2160. goto QL_BLE_GATT_CLINET_INIT_EXIT;
  2161. }
  2162. while(1)
  2163. {
  2164. ret = ql_ble_gatt_client_handle_event();
  2165. if (ret != QL_BT_SUCCESS)
  2166. {
  2167. break;
  2168. }
  2169. }
  2170. QL_BLE_GATT_CLINET_INIT_EXIT:
  2171. ql_ble_gatt_client_release();
  2172. goto GATT_CLIENT_RETRY;
  2173. QL_BLE_GATT_CLINET_NOT_INIT_EXIT:
  2174. #if QL_BLE_DEMO_LOW_POWER_USE
  2175. ql_lpm_wakelock_delete(bt_ble_power_lock);
  2176. #endif
  2177. err = ql_rtos_task_delete(NULL);
  2178. if(err != QL_OSI_SUCCESS)
  2179. {
  2180. QL_BLE_GATT_LOG("task deleted failed");
  2181. }
  2182. }
  2183. QlOSStatus ql_ble_gatt_client_demo_init(void)
  2184. {
  2185. QlOSStatus err = QL_OSI_SUCCESS;
  2186. QL_BLE_GATT_LOG("enter ql_ble_gatt_client_demo_init");
  2187. #if QL_BLE_DEMO_LOW_POWER_USE
  2188. bt_ble_power_lock = ql_lpm_wakelock_create("bt_ble", strlen("bt_ble"));
  2189. #endif
  2190. err = ql_rtos_task_create(&ble_demo_task, BT_BLE_DEMO_TASK_STACK_SIZE, BT_BLE_DEMO_TASK_PRIO, "gatt_client", ql_ble_gatt_client_demo_task_pthread, NULL, BT_BLE_DEMO_TASK_EVENT_CNT);
  2191. return err;
  2192. }