ble_lns.c 37 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028
  1. /**
  2. * Copyright (c) 2015 - 2020, Nordic Semiconductor ASA
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification,
  7. * are permitted provided that the following conditions are met:
  8. *
  9. * 1. Redistributions of source code must retain the above copyright notice, this
  10. * list of conditions and the following disclaimer.
  11. *
  12. * 2. Redistributions in binary form, except as embedded into a Nordic
  13. * Semiconductor ASA integrated circuit in a product or a software update for
  14. * such product, must reproduce the above copyright notice, this list of
  15. * conditions and the following disclaimer in the documentation and/or other
  16. * materials provided with the distribution.
  17. *
  18. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * 4. This software, with or without modification, must only be used with a
  23. * Nordic Semiconductor ASA integrated circuit.
  24. *
  25. * 5. Any software provided in binary form under this license must not be reverse
  26. * engineered, decompiled, modified and/or disassembled.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  29. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  30. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  31. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  34. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  36. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  37. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  38. *
  39. */
  40. #include "ble_lns.h"
  41. #include "ble_ln_db.h"
  42. #include "ble_ln_common.h"
  43. #include "sdk_common.h"
  44. #define NRF_LOG_MODULE_NAME ble_lns
  45. #include "nrf_log.h"
  46. NRF_LOG_MODULE_REGISTER();
  47. // Location and Speed flag bits
  48. #define LOC_SPEED_FLAG_INSTANT_SPEED_PRESENT (0x01 << 0) /**< Instantaneous Speed Present bit. */
  49. #define LOC_SPEED_FLAG_TOTAL_DISTANCE_PRESENT (0x01 << 1) /**< Total Distance Present bit. */
  50. #define LOC_SPEED_FLAG_LOCATION_PRESENT (0x01 << 2) /**< Location Present bit. */
  51. #define LOC_SPEED_FLAG_ELEVATION_PRESENT (0x01 << 3) /**< Elevation Present bit. */
  52. #define LOC_SPEED_FLAG_HEADING_PRESENT (0x01 << 4) /**< Heading Present bit. */
  53. #define LOC_SPEED_FLAG_ROLLING_TIME_PRESENT (0x01 << 5) /**< Rolling Time Present bit. */
  54. #define LOC_SPEED_FLAG_UTC_TIME_PRESENT (0x01 << 6) /**< UTC Time Present bit. */
  55. #define LOC_SPEED_FLAG_POSITION_STATUS (0x03 << 7) /**< Position Status bits(2). */
  56. #define LOC_SPEED_FLAG_SPEED_AND_DIST_FORMAT (0x01 << 9) /**< Speed and Distance Format. */
  57. #define LOC_SPEED_FLAG_ELEVATION_SOURCE (0x03 << 10) /**< Elevation Source bits(2). */
  58. #define LOC_SPEED_FLAG_HEADING_SOURCE (0x01 << 12) /**< Heading Source. */
  59. // Position Quality flag bits
  60. #define POS_QUAL_FLAG_NUM_SATS_IN_SOLUTION_PRESENT (0x01 << 0) /**< Number of Satellites in Solution Present bit. */
  61. #define POS_QUAL_FLAG_NUM_SATS_IN_VIEW_PRESENT (0x01 << 1) /**< Number of Satellites in View Present bit. */
  62. #define POS_QUAL_FLAG_TIME_TO_FIRST_FIX_PRESESNT (0x01 << 2) /**< Time to First Fix Present bit. */
  63. #define POS_QUAL_FLAG_EHPE_PRESENT (0x01 << 3) /**< EHPE Present bit. */
  64. #define POS_QUAL_FLAG_EVPE_PRESENT (0x01 << 4) /**< EVPE Present bit. */
  65. #define POS_QUAL_FLAG_HDOP_PRESENT (0x01 << 5) /**< HDOP Present bit. */
  66. #define POS_QUAL_FLAG_VDOP_PRESENT (0x01 << 6) /**< VDOP Present bit. */
  67. #define POS_QUAL_FLAG_POSITION_STATUS (0x03 << 7) /**< Position Status bits(2). */
  68. // Navigation flag bits
  69. #define NAV_FLAG_REMAINING_DIST_PRESENT (0x01 << 0) /**< Remaining Distance Present bit. */
  70. #define NAV_FLAG_REAMINGING_VERT_DIST_PRESESNT (0x01 << 1) /**< Remaining Vertical Distance Present bit . */
  71. #define NAV_FLAG_ETA_PRESENT (0x01 << 2) /**< Estimated Time of Arrival Present bit. */
  72. #define NAV_FLAG_POSITION_STATUS (0x03 << 3) /**< Position Status bits(2). */
  73. #define NAV_FLAG_HEADING_SOURCE (0x01 << 5) /**< Heading Source bit. */
  74. #define NAV_FLAG_NAVIGATION_INDICATOR_TYPE (0x01 << 6) /**< Navigation Indicator Type bit. */
  75. #define NAV_FLAG_WAYPOINT_REACHED (0x01 << 7) /**< Waypoint Reached bit. */
  76. #define NAV_FLAG_DESTINATION_REACHED (0x01 << 8) /**< Destination Reached bit. */
  77. #define BLE_LNS_NAV_MAX_LEN 19 /**< The length of a navigation notification when all features are enabled. See @ref ble_lns_navigation_t to see what this represents, or check https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicViewer.aspx?u=org.bluetooth.characteristic.navigation.xml. */
  78. static void notification_buffer_process(ble_lns_t * p_lns)
  79. {
  80. notification_t * p_notification;
  81. // See if a notification is pending
  82. if (p_lns->pending_loc_speed_notifications[0].is_pending == true)
  83. {
  84. p_notification = &p_lns->pending_loc_speed_notifications[0];
  85. }
  86. else if (p_lns->pending_loc_speed_notifications[1].is_pending == true)
  87. {
  88. p_notification = &p_lns->pending_loc_speed_notifications[1];
  89. }
  90. else if (p_lns->pending_navigation_notification.is_pending == true)
  91. {
  92. p_notification = &p_lns->pending_navigation_notification;
  93. }
  94. else
  95. {
  96. p_notification = NULL;
  97. }
  98. // send the notification if necessary
  99. if (p_notification != NULL)
  100. {
  101. uint32_t err_code;
  102. ble_gatts_hvx_params_t hvx_params;
  103. memset(&hvx_params, 0, sizeof(hvx_params));
  104. uint16_t hvx_len = p_notification->len;
  105. hvx_params.handle = p_notification->handle;
  106. hvx_params.type = BLE_GATT_HVX_NOTIFICATION;
  107. hvx_params.offset = 0;
  108. hvx_params.p_len = &hvx_len;
  109. hvx_params.p_data = &p_notification->data[0];
  110. err_code = sd_ble_gatts_hvx(p_lns->conn_handle, &hvx_params);
  111. if ((err_code == NRF_SUCCESS) && (hvx_len == p_notification->len))
  112. {
  113. p_notification->is_pending = false;
  114. }
  115. }
  116. }
  117. /**@brief Connect event handler.
  118. *
  119. * @param[in] p_lns Location and Navigation Service structure.
  120. * @param[in] p_ble_evt Event received from the BLE stack.
  121. */
  122. static void on_connect(ble_lns_t * p_lns, ble_evt_t const * p_ble_evt)
  123. {
  124. ret_code_t err_code;
  125. p_lns->conn_handle = p_ble_evt->evt.gap_evt.conn_handle;
  126. // clear pending notifications
  127. p_lns->pending_loc_speed_notifications[0].is_pending = false;
  128. p_lns->pending_loc_speed_notifications[1].is_pending = false;
  129. p_lns->pending_navigation_notification.is_pending = false;
  130. err_code = nrf_ble_gq_conn_handle_register(p_lns->p_gatt_queue,
  131. p_ble_evt->evt.gap_evt.conn_handle);
  132. if ((p_lns->error_handler != NULL) &&
  133. (err_code != NRF_SUCCESS))
  134. {
  135. p_lns->error_handler(err_code);
  136. }
  137. }
  138. /**@brief Disconnect event handler.
  139. *
  140. * @param[in] p_lns Location and Navigation Service structure.
  141. * @param[in] p_ble_evt Event received from the BLE stack.
  142. */
  143. static void on_disconnect(ble_lns_t * p_lns, ble_evt_t const * p_ble_evt)
  144. {
  145. if (p_lns->conn_handle != p_ble_evt->evt.gatts_evt.conn_handle)
  146. {
  147. return;
  148. }
  149. p_lns->conn_handle = BLE_CONN_HANDLE_INVALID;
  150. }
  151. /**@brief Handle write events to the control point cccd.
  152. *
  153. * @param[in] p_lncp Location and Navigation Service structure.
  154. * @param[in] p_evt_write Write event received from the BLE stack.
  155. */
  156. static void on_ctrl_pt_cccd_write(ble_lns_t * p_lns, ble_gatts_evt_write_t const * p_evt_write)
  157. {
  158. if (p_evt_write->len == BLE_CCCD_VALUE_LEN)
  159. {
  160. if (p_lns->evt_handler != NULL)
  161. {
  162. ble_lns_evt_t evt;
  163. if (ble_srv_is_indication_enabled(p_evt_write->data))
  164. {
  165. evt.evt_type = BLE_LNS_CTRLPT_EVT_INDICATION_ENABLED;
  166. }
  167. else
  168. {
  169. evt.evt_type = BLE_LNS_CTRLPT_EVT_INDICATION_DISABLED;
  170. }
  171. p_lns->evt_handler(p_lns, &evt);
  172. }
  173. }
  174. }
  175. /**@brief Handle write events to the Location and Speed cccd.
  176. *
  177. * @param[in] p_lns Location and Navigation Service structure.
  178. * @param[in] p_evt_write Write event received from the BLE stack.
  179. */
  180. static void on_loc_speed_cccd_write(ble_lns_t * p_lns,
  181. ble_gatts_evt_write_t const * p_evt_write)
  182. {
  183. if (p_evt_write->len == BLE_CCCD_VALUE_LEN)
  184. {
  185. // CCCD written, update notification state
  186. p_lns->is_loc_speed_notification_enabled = ble_srv_is_notification_enabled(p_evt_write->data);
  187. if (p_lns->evt_handler != NULL)
  188. {
  189. ble_lns_evt_t evt;
  190. if (p_lns->is_loc_speed_notification_enabled)
  191. {
  192. evt.evt_type = BLE_LNS_LOC_SPEED_EVT_NOTIFICATION_ENABLED;
  193. }
  194. else
  195. {
  196. evt.evt_type = BLE_LNS_LOC_SPEED_EVT_NOTIFICATION_DISABLED;
  197. }
  198. p_lns->evt_handler(p_lns, &evt);
  199. }
  200. }
  201. }
  202. /**@brief Handle write events to the navigation cccd.
  203. *
  204. * @param[in] p_lns Location and Navigation Service structure.
  205. * @param[in] p_evt_write Write event received from the BLE stack.
  206. */
  207. static void on_nav_cccd_write(ble_lns_t * p_lns,
  208. ble_gatts_evt_write_t const * p_evt_write)
  209. {
  210. if (p_evt_write->len == BLE_CCCD_VALUE_LEN)
  211. {
  212. p_lns->is_nav_notification_enabled = ble_srv_is_notification_enabled(p_evt_write->data);
  213. if (p_lns->evt_handler != NULL)
  214. {
  215. ble_lns_evt_t evt;
  216. if (p_lns->is_nav_notification_enabled)
  217. {
  218. evt.evt_type = BLE_LNS_NAVIGATION_EVT_NOTIFICATION_ENABLED;
  219. }
  220. else
  221. {
  222. evt.evt_type = BLE_LNS_NAVIGATION_EVT_NOTIFICATION_DISABLED;
  223. }
  224. p_lns->evt_handler(p_lns, &evt);
  225. }
  226. }
  227. }
  228. /**@brief Write event handler.
  229. *
  230. * @param[in] p_lns Location and Navigation Service structure.
  231. * @param[in] p_ble_evt Event received from the BLE stack.
  232. */
  233. static void on_write(ble_lns_t * p_lns, ble_evt_t const * p_ble_evt)
  234. {
  235. if (p_lns->conn_handle != p_ble_evt->evt.gatts_evt.conn_handle)
  236. {
  237. return;
  238. }
  239. ble_gatts_evt_write_t const * p_evt_write = &p_ble_evt->evt.gatts_evt.params.write;
  240. if (p_evt_write->handle == p_lns->ctrlpt_handles.cccd_handle)
  241. {
  242. on_ctrl_pt_cccd_write(p_lns, p_evt_write);
  243. }
  244. else if (p_evt_write->handle == p_lns->loc_speed_handles.cccd_handle)
  245. {
  246. on_loc_speed_cccd_write(p_lns, p_evt_write);
  247. }
  248. else if (p_evt_write->handle == p_lns->navigation_handles.cccd_handle)
  249. {
  250. on_nav_cccd_write(p_lns, p_evt_write);
  251. }
  252. }
  253. /**@brief Tx Complete event handler. This is used to retry sending a packet.
  254. *
  255. * @details Tx Complete event handler.
  256. * Handles WRITE events from the BLE stack and if an indication was pending try sending it
  257. * again.
  258. *
  259. * @param[in] p_lns Location navigation structure.
  260. */
  261. static void on_tx_complete(ble_lns_t * p_lns)
  262. {
  263. notification_buffer_process(p_lns);
  264. }
  265. /**@brief Encode position quality.
  266. *
  267. * @param[in] p_lns Location and Navigation Service structure.
  268. * @param[in] p_pos_qual Position quality data to be encoded.
  269. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  270. *
  271. * @return Size of encoded data.
  272. */
  273. static uint8_t pos_qual_encode(ble_lns_t const * p_lns,
  274. ble_lns_pos_quality_t const * p_pos_qual,
  275. uint8_t * p_encoded_buffer)
  276. {
  277. uint16_t flags = 0;
  278. uint8_t len = 2; // flags are added at last
  279. flags |= ((uint16_t)p_pos_qual->position_status << 7) & POS_QUAL_FLAG_POSITION_STATUS;
  280. if (p_pos_qual->number_of_satellites_in_solution_present)
  281. {
  282. flags |= POS_QUAL_FLAG_NUM_SATS_IN_SOLUTION_PRESENT;
  283. p_encoded_buffer[len++] = p_pos_qual->number_of_satellites_in_solution;
  284. }
  285. if (p_pos_qual->number_of_satellites_in_view_present)
  286. {
  287. flags |= POS_QUAL_FLAG_NUM_SATS_IN_VIEW_PRESENT;
  288. p_encoded_buffer[len++] = p_pos_qual->number_of_satellites_in_view;
  289. }
  290. if (p_pos_qual->time_to_first_fix_present)
  291. {
  292. flags |= POS_QUAL_FLAG_TIME_TO_FIRST_FIX_PRESESNT;
  293. len += uint16_encode(p_pos_qual->time_to_first_fix, &p_encoded_buffer[len]);
  294. }
  295. if (p_pos_qual->ehpe_present)
  296. {
  297. flags |= POS_QUAL_FLAG_EHPE_PRESENT;
  298. len += uint32_encode(p_pos_qual->ehpe, &p_encoded_buffer[len]);
  299. }
  300. if (p_pos_qual->evpe_present)
  301. {
  302. flags |= POS_QUAL_FLAG_EVPE_PRESENT;
  303. len += uint32_encode(p_pos_qual->evpe, &p_encoded_buffer[len]);
  304. }
  305. if (p_pos_qual->hdop_present)
  306. {
  307. flags |= POS_QUAL_FLAG_HDOP_PRESENT;
  308. p_encoded_buffer[len++] = p_pos_qual->hdop;
  309. }
  310. if (p_pos_qual->vdop_present)
  311. {
  312. flags |= POS_QUAL_FLAG_VDOP_PRESENT;
  313. p_encoded_buffer[len++] = p_pos_qual->vdop;
  314. }
  315. // Flags field
  316. uint16_encode(flags, &p_encoded_buffer[0]); //lint !e534 "Ignoring return value of function"
  317. return len;
  318. }
  319. /**@brief Encode Location and Speed data packet 1
  320. *
  321. * @param[in] p_lns Location and Navigation Service structure.
  322. * @param[in] p_loc_speed Location and Speed data to be encoded.
  323. * @param[out] p_encoded_buffer Pointer to buffer buffer where encoded data will be written.
  324. *
  325. * @return Size of encoded data.
  326. *
  327. */
  328. static uint8_t loc_speed_encode_packet1(ble_lns_t const * p_lns,
  329. ble_lns_loc_speed_t const * p_loc_speed,
  330. uint8_t * p_encoded_buffer)
  331. {
  332. uint16_t flags = 0;
  333. uint8_t len = 2;
  334. ble_lncp_mask_t const mask = ble_lncp_mask_get(&p_lns->ctrl_pt);
  335. // Instantaneous Speed
  336. if (p_lns->available_features & BLE_LNS_FEATURE_INSTANT_SPEED_SUPPORTED)
  337. {
  338. if (p_loc_speed->instant_speed_present && !mask.instantaneous_speed)
  339. {
  340. flags |= LOC_SPEED_FLAG_INSTANT_SPEED_PRESENT;
  341. flags |= ((uint16_t)p_loc_speed->data_format<<9) & LOC_SPEED_FLAG_SPEED_AND_DIST_FORMAT;
  342. len += uint16_encode(p_loc_speed->instant_speed, &p_encoded_buffer[len]);
  343. }
  344. }
  345. // Total Distance
  346. if (p_lns->available_features & BLE_LNS_FEATURE_TOTAL_DISTANCE_SUPPORTED)
  347. {
  348. if (p_loc_speed->total_distance_present && !mask.total_distance)
  349. {
  350. uint32_t const total_distance = ble_lncp_total_distance_get(&p_lns->ctrl_pt);
  351. flags |= LOC_SPEED_FLAG_TOTAL_DISTANCE_PRESENT;
  352. len += uint24_encode(total_distance, &p_encoded_buffer[len]);
  353. }
  354. }
  355. // Location
  356. if (p_lns->available_features & BLE_LNS_FEATURE_LOCATION_SUPPORTED)
  357. {
  358. if (p_loc_speed->location_present && !mask.location)
  359. {
  360. flags |= LOC_SPEED_FLAG_LOCATION_PRESENT;
  361. flags |= ((uint16_t)p_loc_speed->position_status <<7) & LOC_SPEED_FLAG_POSITION_STATUS;
  362. len += uint32_encode(p_loc_speed->latitude, &p_encoded_buffer[len]);
  363. len += uint32_encode(p_loc_speed->longitude, &p_encoded_buffer[len]);
  364. }
  365. }
  366. // Flags field
  367. uint16_encode(flags, &p_encoded_buffer[0]); //lint !e534 "Ignoring return value of function"
  368. return len;
  369. }
  370. /**@brief Encode Location and Speed data packet 2
  371. *
  372. * @param[in] p_lns Location and Navigation Service structure.
  373. * @param[in] p_loc_speed Location and Speed data to be encoded.
  374. * @param[out] p_encoded_buffer Pointer to buffer buffer where encoded data will be written.
  375. *
  376. * @return Size of encoded data.
  377. *
  378. */
  379. static uint8_t loc_speed_encode_packet2(ble_lns_t const * p_lns,
  380. ble_lns_loc_speed_t const * p_loc_speed,
  381. uint8_t * p_encoded_buffer)
  382. {
  383. uint16_t flags = 0;
  384. uint8_t len = 2;
  385. ble_lncp_mask_t const mask = ble_lncp_mask_get(&p_lns->ctrl_pt);
  386. // Elevation
  387. if (p_lns->available_features & BLE_LNS_FEATURE_ELEVATION_SUPPORTED)
  388. {
  389. if (p_loc_speed->elevation_present && !mask.elevation)
  390. {
  391. uint32_t const elevation = ble_lncp_elevation_get(&p_lns->ctrl_pt);
  392. flags |= LOC_SPEED_FLAG_ELEVATION_PRESENT;
  393. flags |= ((uint16_t) p_loc_speed->elevation_source << 10) & LOC_SPEED_FLAG_ELEVATION_SOURCE;
  394. len += uint24_encode(elevation, &p_encoded_buffer[len]);
  395. }
  396. }
  397. // Heading
  398. if (p_lns->available_features & BLE_LNS_FEATURE_HEADING_SUPPORTED)
  399. {
  400. if (p_loc_speed->heading_present && !mask.heading)
  401. {
  402. flags |= LOC_SPEED_FLAG_HEADING_PRESENT;
  403. flags |= ((uint16_t) p_loc_speed->heading_source << 12) & LOC_SPEED_FLAG_HEADING_SOURCE;
  404. len += uint16_encode(p_loc_speed->heading, &p_encoded_buffer[len]);
  405. }
  406. }
  407. // Rolling Time
  408. if (p_lns->available_features & BLE_LNS_FEATURE_ROLLING_TIME_SUPPORTED)
  409. {
  410. if ((p_loc_speed->rolling_time_present && !mask.rolling_time))
  411. {
  412. flags |= LOC_SPEED_FLAG_ROLLING_TIME_PRESENT;
  413. p_encoded_buffer[len++] = p_loc_speed->rolling_time;
  414. }
  415. }
  416. // UTC Time
  417. if (p_lns->available_features & BLE_LNS_FEATURE_UTC_TIME_SUPPORTED)
  418. {
  419. if ((p_loc_speed->utc_time_time_present && !mask.utc_time))
  420. {
  421. flags |= LOC_SPEED_FLAG_UTC_TIME_PRESENT;
  422. len += ble_date_time_encode(&p_loc_speed->utc_time, &p_encoded_buffer[len]);
  423. }
  424. }
  425. // Flags field
  426. uint16_encode(flags, &p_encoded_buffer[0]); //lint !e534 "Ignoring return value of function"
  427. return len;
  428. }
  429. /**@brief Encode Navigation data.
  430. *
  431. * @param[in] p_lns Location and Navigation Service structure.
  432. * @param[in] p_navigation Navigation data to be encoded.
  433. * @param[out] p_encoded_buffer Buffer where the encoded data will be written.
  434. *
  435. * @return Size of encoded data.
  436. */
  437. static uint8_t navigation_encode(ble_lns_t const * p_lns,
  438. ble_lns_navigation_t const * p_navigation,
  439. uint8_t * p_encoded_buffer)
  440. {
  441. uint16_t flags = 0;
  442. uint8_t len = 2;
  443. // Bearing
  444. len += uint16_encode(p_navigation->bearing, &p_encoded_buffer[len]);
  445. // Heading
  446. len += uint16_encode(p_navigation->heading, &p_encoded_buffer[len]);
  447. // Remaining Distance
  448. if (p_lns->available_features & BLE_LNS_FEATURE_REMAINING_DISTANCE_SUPPORTED)
  449. {
  450. if (p_navigation->remaining_dist_present)
  451. {
  452. flags |= NAV_FLAG_REMAINING_DIST_PRESENT;
  453. p_encoded_buffer[len++] = ((p_navigation->remaining_distance >> 0) & 0xFF);
  454. p_encoded_buffer[len++] = ((p_navigation->remaining_distance >> 8) & 0xFF);
  455. p_encoded_buffer[len++] = ((p_navigation->remaining_distance >> 16) & 0xFF);
  456. }
  457. }
  458. // Remaining Vertical Distance
  459. if (p_lns->available_features & BLE_LNS_FEATURE_REMAINING_VERT_DISTANCE_SUPPORTED)
  460. {
  461. if (p_navigation->remaining_vert_dist_present)
  462. {
  463. flags |= NAV_FLAG_REAMINGING_VERT_DIST_PRESESNT;
  464. p_encoded_buffer[len++] = ((p_navigation->remaining_vert_distance >> 0) & 0xFF);
  465. p_encoded_buffer[len++] = ((p_navigation->remaining_vert_distance >> 8) & 0xFF);
  466. p_encoded_buffer[len++] = ((p_navigation->remaining_vert_distance >> 16) & 0xFF);
  467. }
  468. }
  469. // Estimated Time of Arrival
  470. if (p_lns->available_features & BLE_LNS_FEATURE_EST_TIME_OF_ARRIVAL_SUPPORTED)
  471. {
  472. if (p_navigation->eta_present)
  473. {
  474. flags |= NAV_FLAG_ETA_PRESENT;
  475. len += ble_date_time_encode(&p_navigation->eta, &p_encoded_buffer[len]);
  476. }
  477. }
  478. flags |= ((uint16_t)p_navigation->position_status <<3) & NAV_FLAG_POSITION_STATUS;
  479. flags |= ((uint16_t)p_navigation->heading_source <<5) & NAV_FLAG_HEADING_SOURCE;
  480. flags |= ((uint16_t)p_navigation->navigation_indicator_type<<6)& NAV_FLAG_NAVIGATION_INDICATOR_TYPE;
  481. flags |= ((uint16_t)p_navigation->waypoint_reached <<7)& NAV_FLAG_WAYPOINT_REACHED;
  482. flags |= ((uint16_t)p_navigation->destination_reached <<8)& NAV_FLAG_DESTINATION_REACHED;
  483. // Flags field
  484. uint16_encode(flags, &p_encoded_buffer[0]); //lint !e534 "Ignoring return value of function"
  485. return len;
  486. }
  487. /**@brief Add Location and Navigation Feature characteristic.
  488. *
  489. * @param[in] p_lns Location and Navigation Service structure.
  490. * @param[in] p_lns_init Information needed to initialize the service.
  491. *
  492. * @return NRF_SUCCESS on success, otherwise an error code.
  493. */
  494. static ret_code_t loc_and_nav_feature_char_add(ble_lns_t * p_lns, ble_lns_init_t const * p_lns_init)
  495. {
  496. uint8_t init_value_encoded[sizeof(uint32_t)];
  497. uint8_t len;
  498. ble_add_char_params_t add_char_params;
  499. len = uint32_encode(p_lns_init->available_features, init_value_encoded);
  500. memset(&add_char_params, 0, sizeof(add_char_params));
  501. add_char_params.uuid = BLE_UUID_LN_FEATURE_CHAR;
  502. add_char_params.max_len = len;
  503. add_char_params.init_len = len;
  504. add_char_params.p_init_value = &init_value_encoded[0];
  505. add_char_params.char_props.read = true;
  506. add_char_params.read_access = p_lns_init->loc_nav_feature_security_req_read_perm;
  507. return characteristic_add(p_lns->service_handle,
  508. &add_char_params,
  509. &p_lns->feature_handles);
  510. }
  511. /**@brief Add Location and Speed characteristic.
  512. *
  513. * @param[in] p_lns Location and Navigation Service structure.
  514. * @param[in] p_lns_init Information needed to initialize the service.
  515. *
  516. * @return NRF_SUCCESS on success, otherwise an error code.
  517. */
  518. static ret_code_t loc_speed_char_add(ble_lns_t * p_lns, ble_lns_init_t const * p_lns_init)
  519. {
  520. uint8_t encoded_initial_loc_speed1[BLE_GATT_ATT_MTU_DEFAULT];
  521. uint8_t len;
  522. ble_add_char_params_t add_char_params;
  523. len = loc_speed_encode_packet1(p_lns, p_lns_init->p_location_speed, &encoded_initial_loc_speed1[0]);
  524. memset(&add_char_params, 0, sizeof(add_char_params));
  525. add_char_params.uuid = BLE_UUID_LN_LOCATION_AND_SPEED_CHAR;
  526. add_char_params.max_len = BLE_GATT_ATT_MTU_DEFAULT ;
  527. add_char_params.init_len = len;
  528. add_char_params.p_init_value = &encoded_initial_loc_speed1[0];
  529. add_char_params.is_var_len = true;
  530. add_char_params.char_props.notify = true;
  531. add_char_params.cccd_write_access = p_lns_init->loc_speed_security_req_cccd_write_perm;
  532. return characteristic_add(p_lns->service_handle,
  533. &add_char_params,
  534. &p_lns->loc_speed_handles);
  535. }
  536. /**@brief Add Location and Navigation position quality characteristic.
  537. *
  538. * @param[in] p_lns Location and Navigation Service structure.
  539. * @param[in] p_lns_init Information needed to initialize the service.
  540. *
  541. * @return NRF_SUCCESS on success, otherwise an error code.
  542. */
  543. static ret_code_t pos_quality_char_add(ble_lns_t * p_lns, ble_lns_init_t const * p_lns_init)
  544. {
  545. uint8_t len;
  546. uint8_t init_value_encoded[BLE_GATT_ATT_MTU_DEFAULT];
  547. ble_add_char_params_t add_char_params;
  548. len = pos_qual_encode(p_lns, p_lns_init->p_position_quality, init_value_encoded);
  549. memset(&add_char_params, 0, sizeof(add_char_params));
  550. add_char_params.uuid = BLE_UUID_LN_POSITION_QUALITY_CHAR;
  551. add_char_params.max_len = BLE_GATT_ATT_MTU_DEFAULT ;
  552. add_char_params.init_len = len;
  553. add_char_params.p_init_value = init_value_encoded;
  554. add_char_params.char_props.read = true;
  555. add_char_params.read_access = p_lns_init->position_quality_security_req_read_perm;
  556. return characteristic_add(p_lns->service_handle,
  557. &add_char_params,
  558. &p_lns->pos_qual_handles);
  559. }
  560. /**@brief Add Navigation characteristic.
  561. *
  562. * @param[in] p_lns Location and Navigation Service structure.
  563. * @param[in] p_lns_init Information needed to initialize the service.
  564. *
  565. * @return NRF_SUCCESS on success, otherwise an error code.
  566. */
  567. static ret_code_t navigation_char_add(ble_lns_t * p_lns, ble_lns_init_t const * p_lns_init)
  568. {
  569. ble_add_char_params_t add_char_params;
  570. memset(&add_char_params, 0, sizeof(add_char_params));
  571. add_char_params.uuid = BLE_UUID_LN_NAVIGATION_CHAR;
  572. add_char_params.max_len = BLE_LNS_NAV_MAX_LEN;
  573. add_char_params.init_len = 0;
  574. add_char_params.p_init_value = NULL;
  575. add_char_params.char_props.notify = true;
  576. add_char_params.cccd_write_access = p_lns_init->navigation_security_req_cccd_write_perm;
  577. return characteristic_add(p_lns->service_handle,
  578. &add_char_params,
  579. &p_lns->navigation_handles);
  580. }
  581. /** @brief Check if there is a mismatch in initialization parameters.
  582. *
  583. * @details It is possible to give an input which has an internal mismatch. Such a mismatch can arise in two different ways.
  584. * One possibility is a mismatch between the characteristic present indicators and the available features specified.
  585. * The other mismatch arises when no pointer to the characteristic data structure is specified.
  586. *
  587. * @param[in] p_lns_init The init structure which will be checked
  588. *
  589. * @return false if there is no mismatch. true if there is a mismatch
  590. */
  591. static bool init_param_mismatch_present(ble_lns_init_t const * p_lns_init)
  592. {
  593. if (p_lns_init->is_position_quality_present == false)
  594. {
  595. if (p_lns_init->available_features &
  596. (BLE_LNS_FEATURE_NUM_SATS_IN_SOLUTION_SUPPORTED |
  597. BLE_LNS_FEATURE_NUM_SATS_IN_VIEW_SUPPORTED |
  598. BLE_LNS_FEATURE_TIME_TO_FIRST_FIX_SUPPORTED |
  599. BLE_LNS_FEATURE_EST_HORZ_POS_ERROR_SUPPORTED |
  600. BLE_LNS_FEATURE_EST_VERT_POS_ERROR_SUPPORTED |
  601. BLE_LNS_FEATURE_HORZ_DILUTION_OF_PRECISION_SUPPORTED |
  602. BLE_LNS_FEATURE_VERT_DILUTION_OF_PRECISION_SUPPORTED)
  603. )
  604. {
  605. return true;
  606. }
  607. if (p_lns_init->p_position_quality != NULL)
  608. {
  609. return true;
  610. }
  611. }
  612. else if (p_lns_init->is_position_quality_present == true)
  613. {
  614. if (p_lns_init->p_position_quality == NULL)
  615. {
  616. return true;
  617. }
  618. }
  619. if (p_lns_init->is_control_point_present == false)
  620. {
  621. if (p_lns_init->available_features &
  622. (BLE_LNS_FEATURE_LOC_AND_SPEED_CONTENT_MASKING_SUPPORTED |
  623. BLE_LNS_FEATURE_FIX_RATE_SETTING_SUPPORTED |
  624. BLE_LNS_FEATURE_ELEVATION_SETTING_SUPPORTED)
  625. )
  626. {
  627. return true;
  628. }
  629. }
  630. if (p_lns_init->is_navigation_present == false)
  631. {
  632. if (p_lns_init->available_features &
  633. (BLE_LNS_FEATURE_REMAINING_DISTANCE_SUPPORTED |
  634. BLE_LNS_FEATURE_REMAINING_VERT_DISTANCE_SUPPORTED |
  635. BLE_LNS_FEATURE_EST_TIME_OF_ARRIVAL_SUPPORTED)
  636. )
  637. {
  638. return true;
  639. }
  640. if (p_lns_init->p_navigation != NULL)
  641. {
  642. return true;
  643. }
  644. }
  645. else if (p_lns_init->is_navigation_present == true)
  646. {
  647. if (p_lns_init->p_navigation == NULL)
  648. {
  649. return true;
  650. }
  651. }
  652. // location and speed must always be specified
  653. if (p_lns_init->p_location_speed == NULL)
  654. {
  655. return true;
  656. }
  657. return false;
  658. }
  659. void ble_lns_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context)
  660. {
  661. VERIFY_PARAM_NOT_NULL_VOID(p_context);
  662. VERIFY_PARAM_NOT_NULL_VOID(p_ble_evt);
  663. ble_lns_t * p_lns = (ble_lns_t *)p_context;
  664. ble_lncp_on_ble_evt(&p_lns->ctrl_pt, p_ble_evt);
  665. switch (p_ble_evt->header.evt_id)
  666. {
  667. case BLE_GAP_EVT_CONNECTED:
  668. on_connect(p_lns, p_ble_evt);
  669. break;
  670. case BLE_GAP_EVT_DISCONNECTED:
  671. on_disconnect(p_lns, p_ble_evt);
  672. break;
  673. case BLE_GATTS_EVT_WRITE:
  674. on_write(p_lns, p_ble_evt);
  675. break;
  676. case BLE_GATTS_EVT_HVN_TX_COMPLETE:
  677. on_tx_complete(p_lns);
  678. break;
  679. default:
  680. // no implementation
  681. break;
  682. }
  683. }
  684. ret_code_t ble_lns_init(ble_lns_t * p_lns, ble_lns_init_t const * p_lns_init)
  685. {
  686. VERIFY_PARAM_NOT_NULL(p_lns);
  687. VERIFY_PARAM_NOT_NULL(p_lns_init);
  688. VERIFY_PARAM_NOT_NULL(p_lns_init->p_gatt_queue);
  689. if (init_param_mismatch_present(p_lns_init) == true)
  690. {
  691. return NRF_ERROR_INVALID_PARAM;
  692. }
  693. uint32_t err_code;
  694. ble_uuid_t service_uuid;
  695. ble_lncp_init_t lncp_init;
  696. // Initialize service structure
  697. p_lns->evt_handler = p_lns_init->evt_handler;
  698. p_lns->error_handler = p_lns_init->error_handler;
  699. p_lns->conn_handle = BLE_CONN_HANDLE_INVALID;
  700. p_lns->available_features = p_lns_init->available_features;
  701. p_lns->is_navigation_present = p_lns_init->is_navigation_present;
  702. p_lns->p_gatt_queue = p_lns_init->p_gatt_queue;
  703. // clear pending notifications
  704. p_lns->pending_loc_speed_notifications[0].is_pending = false;
  705. p_lns->pending_loc_speed_notifications[1].is_pending = false;
  706. p_lns->pending_navigation_notification.is_pending = false;
  707. p_lns->p_location_speed = p_lns_init->p_location_speed;
  708. p_lns->p_position_quality = p_lns_init->p_position_quality;
  709. p_lns->p_navigation = p_lns_init->p_navigation;
  710. p_lns->is_loc_speed_notification_enabled = false;
  711. p_lns->is_nav_notification_enabled = false;
  712. ble_ln_db_init();
  713. // Add service
  714. BLE_UUID_BLE_ASSIGN(service_uuid, BLE_UUID_LOCATION_AND_NAVIGATION_SERVICE);
  715. err_code = sd_ble_gatts_service_add(BLE_GATTS_SRVC_TYPE_PRIMARY, &service_uuid, &p_lns->service_handle);
  716. VERIFY_SUCCESS(err_code);
  717. // Add location and navigation feature characteristic
  718. err_code = loc_and_nav_feature_char_add(p_lns, p_lns_init);
  719. VERIFY_SUCCESS(err_code);
  720. // Add location and speed characteristic
  721. err_code = loc_speed_char_add(p_lns, p_lns_init);
  722. VERIFY_SUCCESS(err_code);
  723. if (p_lns_init->is_position_quality_present)
  724. {
  725. // Add Position quality characteristic
  726. err_code = pos_quality_char_add(p_lns, p_lns_init);
  727. VERIFY_SUCCESS(err_code);
  728. }
  729. else
  730. {
  731. p_lns->pos_qual_handles.cccd_handle = BLE_GATT_HANDLE_INVALID;
  732. p_lns->pos_qual_handles.sccd_handle = BLE_GATT_HANDLE_INVALID;
  733. p_lns->pos_qual_handles.user_desc_handle = BLE_GATT_HANDLE_INVALID;
  734. p_lns->pos_qual_handles.value_handle = BLE_GATT_HANDLE_INVALID;
  735. }
  736. if (p_lns_init->is_navigation_present)
  737. {
  738. // Add navigation characteristic
  739. err_code = navigation_char_add(p_lns, p_lns_init);
  740. VERIFY_SUCCESS(err_code);
  741. }
  742. else
  743. {
  744. p_lns->navigation_handles.cccd_handle = BLE_GATT_HANDLE_INVALID;
  745. p_lns->navigation_handles.sccd_handle = BLE_GATT_HANDLE_INVALID;
  746. p_lns->navigation_handles.user_desc_handle = BLE_GATT_HANDLE_INVALID;
  747. p_lns->navigation_handles.value_handle = BLE_GATT_HANDLE_INVALID;
  748. }
  749. if (p_lns_init->is_control_point_present)
  750. {
  751. lncp_init.error_handler = p_lns_init->error_handler;
  752. lncp_init.evt_handler = p_lns_init->lncp_evt_handler;
  753. lncp_init.write_perm = p_lns_init->ctrl_point_security_req_write_perm;
  754. lncp_init.cccd_write_perm = p_lns_init->ctrl_point_security_req_cccd_write_perm;
  755. lncp_init.available_features = p_lns_init->available_features;
  756. lncp_init.is_position_quality_present = p_lns_init->is_position_quality_present;
  757. lncp_init.is_navigation_present = p_lns_init->is_navigation_present;
  758. lncp_init.p_gatt_queue = p_lns_init->p_gatt_queue;
  759. lncp_init.total_distance = p_lns_init->p_location_speed->total_distance;
  760. lncp_init.elevation = p_lns_init->p_location_speed->elevation;
  761. lncp_init.service_handle = p_lns->service_handle;
  762. lncp_init.navigation_handles = p_lns->navigation_handles;
  763. // Add control pointer characteristic
  764. err_code = ble_lncp_init(&p_lns->ctrl_pt, &lncp_init);
  765. VERIFY_SUCCESS(err_code);
  766. memcpy(&p_lns->ctrlpt_handles, &p_lns->ctrl_pt.ctrlpt_handles, sizeof(ble_gatts_char_handles_t));
  767. }
  768. NRF_LOG_DEBUG("Initialized");
  769. return NRF_SUCCESS;
  770. }
  771. ret_code_t ble_lns_loc_speed_send(ble_lns_t * p_lns)
  772. {
  773. VERIFY_PARAM_NOT_NULL(p_lns);
  774. if (p_lns->conn_handle == BLE_CONN_HANDLE_INVALID)
  775. {
  776. return NRF_ERROR_INVALID_STATE;
  777. }
  778. if (!p_lns->is_loc_speed_notification_enabled)
  779. {
  780. return NRF_ERROR_INVALID_STATE;
  781. }
  782. notification_t * notif1 = &p_lns->pending_loc_speed_notifications[0];
  783. notification_t * notif2 = &p_lns->pending_loc_speed_notifications[1];
  784. // clear previous unsent data. Previous data is invalid.
  785. notif1->is_pending = false;
  786. notif2->is_pending = false;
  787. // check if it is necessary to send packet 1
  788. if (p_lns->available_features & (BLE_LNS_FEATURE_INSTANT_SPEED_SUPPORTED
  789. | BLE_LNS_FEATURE_TOTAL_DISTANCE_SUPPORTED
  790. | BLE_LNS_FEATURE_LOCATION_SUPPORTED))
  791. {
  792. // encode
  793. notif1->len = loc_speed_encode_packet1(p_lns, p_lns->p_location_speed, &notif1->data[0]);
  794. notif1->handle = p_lns->loc_speed_handles.value_handle;
  795. notif1->is_pending = true;
  796. // send
  797. notification_buffer_process(p_lns);
  798. }
  799. // check if it is necessary to send packet 2
  800. if (p_lns->available_features & (BLE_LNS_FEATURE_ELEVATION_SUPPORTED
  801. | BLE_LNS_FEATURE_HEADING_SUPPORTED
  802. | BLE_LNS_FEATURE_ROLLING_TIME_SUPPORTED
  803. | BLE_LNS_FEATURE_UTC_TIME_SUPPORTED))
  804. {
  805. notif2->len = loc_speed_encode_packet2(p_lns, p_lns->p_location_speed, &notif2->data[0]);
  806. notif2->handle = p_lns->loc_speed_handles.value_handle;
  807. notif2->is_pending = true;
  808. // send
  809. notification_buffer_process(p_lns);
  810. }
  811. return NRF_SUCCESS;
  812. }
  813. ret_code_t ble_lns_navigation_send(ble_lns_t * p_lns)
  814. {
  815. VERIFY_PARAM_NOT_NULL(p_lns);
  816. if (p_lns->conn_handle == BLE_CONN_HANDLE_INVALID)
  817. {
  818. return NRF_ERROR_INVALID_STATE;
  819. }
  820. notification_t * notif = &p_lns->pending_navigation_notification;
  821. // clear previous unsent data. Previous data is invalid.
  822. notif->is_pending = false;
  823. if (!p_lns->is_navigation_present)
  824. {
  825. return NRF_ERROR_NOT_SUPPORTED;
  826. }
  827. if (!p_lns->is_nav_notification_enabled)
  828. {
  829. return NRF_ERROR_INVALID_STATE;
  830. }
  831. if (!ble_lncp_is_navigation_running(&p_lns->ctrl_pt))
  832. {
  833. return NRF_ERROR_INVALID_STATE;
  834. }
  835. notif->len = navigation_encode(p_lns, p_lns->p_navigation, &notif->data[0]);
  836. notif->handle = p_lns->navigation_handles.value_handle;
  837. notif->is_pending = true;
  838. notification_buffer_process(p_lns);
  839. return NRF_SUCCESS;
  840. }
  841. ret_code_t ble_lns_add_route(ble_lns_t * p_lns, ble_lns_route_t * p_route)
  842. {
  843. VERIFY_PARAM_NOT_NULL(p_lns);
  844. VERIFY_PARAM_NOT_NULL(p_route);
  845. if (p_lns->is_navigation_present == false)
  846. {
  847. return NRF_ERROR_NOT_SUPPORTED;
  848. }
  849. return ble_ln_db_record_add(p_route);
  850. }
  851. ret_code_t ble_lns_remove_route(ble_lns_t * p_lns, uint16_t route_id)
  852. {
  853. VERIFY_PARAM_NOT_NULL(p_lns);
  854. if (p_lns->is_navigation_present == false)
  855. {
  856. return NRF_ERROR_NOT_SUPPORTED;
  857. }
  858. return ble_ln_db_record_delete(route_id);
  859. }