ble_lns.h 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  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. /** @file
  41. *
  42. * @defgroup ble_lns Location and Navigation Service
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Location and Navigation Service module.
  46. *
  47. * @details This module implements the Location and Navigation Service with the Location and Speed, Position
  48. * Quality, Feature, Control Point, and Navigation characteristics.
  49. *
  50. * If an event handler is supplied by the application, the Location and Navigation Service will
  51. * generate Location and Navigation Service events to the application.
  52. *
  53. * @note The application must register this module as BLE event observer using the
  54. * NRF_SDH_BLE_OBSERVER macro. Example:
  55. * @code
  56. * ble_lns_t instance;
  57. * NRF_SDH_BLE_OBSERVER(anything, BLE_LNS_BLE_OBSERVER_PRIO,
  58. * ble_lns_on_ble_evt, &instance);
  59. * @endcode
  60. */
  61. #ifndef BLE_LNS_H__
  62. #define BLE_LNS_H__
  63. #include "ble_srv_common.h"
  64. #include "ble_date_time.h"
  65. #include "ble_ln_common.h"
  66. #include "ble_ln_cp.h"
  67. #include "sdk_common.h"
  68. #include "nrf_sdh_ble.h"
  69. #include "nrf_ble_gq.h"
  70. #ifdef __cplusplus
  71. extern "C" {
  72. #endif
  73. /**@ Macro for defining a ble_lns instance.
  74. *
  75. * @param _name Name of the instance.
  76. * @hideinitializer
  77. */
  78. #define BLE_LNS_DEF(_name) \
  79. static ble_lns_t _name; \
  80. NRF_SDH_BLE_OBSERVER(_name ## _obs, \
  81. BLE_LNS_BLE_OBSERVER_PRIO, \
  82. ble_lns_on_ble_evt, &_name)
  83. /**@brief Location and Navigation event type. This list defines the possible events types from the Location and Navigation Service. */
  84. typedef enum
  85. {
  86. BLE_LNS_CTRLPT_EVT_INDICATION_ENABLED, /**< Control Point value indication was enabled. */
  87. BLE_LNS_CTRLPT_EVT_INDICATION_DISABLED, /**< Control Point value indication was disabled. */
  88. BLE_LNS_LOC_SPEED_EVT_NOTIFICATION_ENABLED, /**< Location and Speed value notification was enabled. */
  89. BLE_LNS_LOC_SPEED_EVT_NOTIFICATION_DISABLED, /**< Location and Speed value notification was disabled. */
  90. BLE_LNS_NAVIGATION_EVT_NOTIFICATION_ENABLED, /**< Navigation value notification was enabled. */
  91. BLE_LNS_NAVIGATION_EVT_NOTIFICATION_DISABLED, /**< Navigation value notification was disabled. */
  92. } ble_lns_evt_type_t;
  93. /**@brief Location and Navigation event structure. When an event occurs, the data structures of the module are automatically updated. */
  94. typedef struct
  95. {
  96. ble_lns_evt_type_t evt_type;
  97. } ble_lns_evt_t;
  98. // Forward declarations of the ble_lns types.
  99. typedef struct ble_lns_init_s ble_lns_init_t;
  100. typedef struct ble_lns_s ble_lns_t;
  101. typedef struct ble_lns_loc_speed_s ble_lns_loc_speed_t;
  102. typedef struct ble_lns_pos_quality_s ble_lns_pos_quality_t;
  103. typedef struct ble_lns_navigation_s ble_lns_navigation_t;
  104. typedef struct
  105. {
  106. bool is_pending;
  107. uint16_t handle;
  108. uint16_t len;
  109. uint8_t data[BLE_GATT_ATT_MTU_DEFAULT];
  110. } notification_t;
  111. /**@brief Location and Navigation Service event handler type. */
  112. typedef void (*ble_lns_evt_handler_t) (ble_lns_t const * p_lns, ble_lns_evt_t const * p_evt);
  113. /**@brief Location and Navigation Service init structure. This structure contains all options
  114. * and data needed to initialize the service.
  115. */
  116. struct ble_lns_init_s
  117. {
  118. ble_lns_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Location and Navigation Service. */
  119. ble_lncp_evt_handler_t lncp_evt_handler;
  120. ble_srv_error_handler_t error_handler; /**< Errors will be sent back to this function. */
  121. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT queue instance. */
  122. bool is_position_quality_present; /**< If set to true, the position quality characteristic will be added. Else not. */
  123. bool is_control_point_present; /**< If set to true, the control point characteristic will be added. Else not. */
  124. bool is_navigation_present; /**< If set to true, the navigation characteristic will be added. Else not. */
  125. security_req_t loc_nav_feature_security_req_read_perm; /**< Read security level of the LN Feature characteristic. */
  126. security_req_t loc_speed_security_req_cccd_write_perm; /**< CCCD write security level of the Write Location and Speed characteristic. */
  127. security_req_t position_quality_security_req_read_perm; /**< Read security level of the Position Quality characteristic. */
  128. security_req_t navigation_security_req_cccd_write_perm; /**< CCCD write security level of the Navigation characteristic. */
  129. security_req_t ctrl_point_security_req_write_perm; /**< Read security level of the LN Control Point characteristic. */
  130. security_req_t ctrl_point_security_req_cccd_write_perm; /**< CCCD write security level of the LN Control Point characteristic. */
  131. uint32_t available_features; /**< Value of the LN feature. */
  132. ble_lns_loc_speed_t * p_location_speed; /**< Initial Location and Speed. */
  133. ble_lns_pos_quality_t * p_position_quality; /**< Initial Position Quality. */
  134. ble_lns_navigation_t * p_navigation; /**< Initial Navigation data structure. */
  135. };
  136. /**@brief Definition of a navigation route.*/
  137. typedef struct
  138. {
  139. uint16_t route_id;
  140. uint8_t route_name[BLE_LNS_MAX_ROUTE_NAME_LEN];
  141. } ble_lns_route_t;
  142. /**@brief Location and Navigation Service structure. This structure contains various status information for the service. */
  143. struct ble_lns_s
  144. {
  145. ble_lns_evt_handler_t evt_handler; /**< Event handler to be called for handling events in the Location and Navigation Service. */
  146. ble_srv_error_handler_t error_handler; /**< Error handler. */
  147. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT queue instance. */
  148. bool is_navigation_present; /**< If set to true, the navigation characteristic is present. Else not. */
  149. uint16_t conn_handle; /**< Handle of the current connection (as provided by the BLE stack; BLE_CONN_HANDLE_INVALID if not in a connection). */
  150. uint16_t service_handle; /**< Handle of Location and Navigation Service (as provided by the BLE stack). */
  151. ble_gatts_char_handles_t loc_speed_handles; /**< Handles related to the Location and Speed characteristic. */
  152. ble_gatts_char_handles_t feature_handles; /**< Handles related to the Location and Navigation Feature characteristic. */
  153. ble_gatts_char_handles_t navigation_handles; /**< Handles related to the Navigation characteristic. */
  154. ble_gatts_char_handles_t pos_qual_handles; /**< Handles related to the Position Quality characteristic. */
  155. ble_gatts_char_handles_t ctrlpt_handles;
  156. uint32_t available_features; /**< Value of Location and Navigation feature. */
  157. bool is_loc_speed_notification_enabled; /**< True if notification is enabled on the Location and Speed characteristic. */
  158. bool is_nav_notification_enabled; /**< True if notification is enabled on the Navigation characteristic. */
  159. notification_t pending_loc_speed_notifications[2]; /**< This buffer holds location and speed notifications. */
  160. notification_t pending_navigation_notification; /**< This buffer holds navigation notifications. */
  161. ble_lns_loc_speed_t * p_location_speed; /**< Location and Speed. */
  162. ble_lns_pos_quality_t * p_position_quality; /**< Position measurement quality. */
  163. ble_lns_navigation_t * p_navigation; /**< Navigation data structure. */
  164. ble_lncp_t ctrl_pt;
  165. };
  166. /**@brief Position status. This enumeration defines how to interpret the position data. */
  167. typedef enum
  168. {
  169. BLE_LNS_NO_POSITION = 0,
  170. BLE_LNS_POSITION_OK = 1,
  171. BLE_LNS_ESTIMATED = 2,
  172. BLE_LNS_LAST_KNOWN_POSITION = 3
  173. } ble_lns_pos_status_type_t;
  174. /**@brief The format of the position and speed measurements. */
  175. typedef enum
  176. {
  177. BLE_LNS_SPEED_DISTANCE_FORMAT_2D = 0,
  178. BLE_LNS_SPEED_DISTANCE_FORMAT_3D = 1
  179. } ble_lns_speed_distance_format_t;
  180. /**@brief Elevation source. */
  181. typedef enum
  182. {
  183. BLE_LNS_ELEV_SOURCE_POSITIONING_SYSTEM = 0,
  184. BLE_LNS_ELEV_SOURCE_BAROMETRIC = 1,
  185. BLE_LNS_ELEV_SOURCE_DATABASE_SERVICE = 2,
  186. BLE_LNS_ELEV_SOURCE_OTHER = 3
  187. } ble_lns_elevation_source_t;
  188. /**@brief Heading source. */
  189. typedef enum
  190. {
  191. BLE_LNS_HEADING_SOURCE_MOVEMENT = 0,
  192. BLE_LNS_HEADING_SOURCE_COMPASS = 1
  193. } ble_lns_heading_source_t;
  194. /**@brief Location and Speed data structure. */
  195. struct ble_lns_loc_speed_s
  196. {
  197. bool instant_speed_present; /**< Instantaneous Speed present (0=not present, 1=present). */
  198. bool total_distance_present; /**< Total Distance present (0=not present, 1=present). */
  199. bool location_present; /**< Location present (0=not present, 1=present). */
  200. bool elevation_present; /**< Elevation present (0=not present, 1=present). */
  201. bool heading_present; /**< Heading present (0=not present, 1=present). */
  202. bool rolling_time_present; /**< Rolling Time present (0=not present, 1=present). */
  203. bool utc_time_time_present; /**< UTC Time present (0=not present, 1=present). */
  204. ble_lns_pos_status_type_t position_status; /**< Status of current position */
  205. ble_lns_speed_distance_format_t data_format; /**< Format of data (either 2D or 3D). */
  206. ble_lns_elevation_source_t elevation_source; /**< Source of the elevation measurement. */
  207. ble_lns_heading_source_t heading_source; /**< Source of the heading measurement. */
  208. uint16_t instant_speed; /**< Instantaneous Speed (1/10 meter per sec). */
  209. uint32_t total_distance; /**< Total Distance (meters), size=24 bits. */
  210. int32_t latitude; /**< Latitude (10e-7 degrees). */
  211. int32_t longitude; /**< Longitude (10e-7 degrees). */
  212. int32_t elevation; /**< Elevation (1/100 meters), size=24 bits. */
  213. uint16_t heading; /**< Heading (1/100 degrees). */
  214. uint8_t rolling_time; /**< Rolling Time (seconds). */
  215. ble_date_time_t utc_time; /**< UTC Time. */
  216. };
  217. /**@brief Position quality structure. */
  218. struct ble_lns_pos_quality_s
  219. {
  220. bool number_of_satellites_in_solution_present; /**< The number of satellites present in solution (0=not present, 1=present). */
  221. bool number_of_satellites_in_view_present; /**< The number of satellites present in solution (0=not present, 1=present). */
  222. bool time_to_first_fix_present; /**< Time to the first position fix present (0=not present, 1=present). */
  223. bool ehpe_present; /**< Error in horizontal position estimate present (0=not present, 1=present). */
  224. bool evpe_present; /**< Error in vertical position estimate present (0=not present, 1=present). */
  225. bool hdop_present; /**< Horizontal dilution of precision present (0=not present, 1=present). */
  226. bool vdop_present; /**< Vertical dilution of precision present (0=not present, 1=present). */
  227. ble_lns_pos_status_type_t position_status; /**< Status of last measured position. */
  228. uint8_t number_of_satellites_in_solution; /**< The number of satellites in solution (unitless, with a resolution of 1). */
  229. uint8_t number_of_satellites_in_view; /**< The number of satellites in view (unitless, with a resolution of 1). */
  230. uint16_t time_to_first_fix; /**< Time to the first position fix (seconds, with a resolution of 1/10). */
  231. uint32_t ehpe; /**< Error in horizontal position estimate (meters, with a resolution of 1/100). */
  232. uint32_t evpe; /**< Error in vertical position estimate (meters, with a resolution of 1/100). */
  233. uint8_t hdop; /**< Horizontal dilution of precision (unitless, with a resolution of 2/10). */
  234. uint8_t vdop; /**< Vertical dilution of precision (unitless, with a resolution of 2/10). */
  235. };
  236. /**@brief Navigation indicator type. */
  237. typedef enum
  238. {
  239. BLE_LNS_NAV_TO_WAYPOINT = 0,
  240. BLE_LNS_NAV_TO_DESTINATION = 1
  241. } ble_lns_nav_indicator_type_t;
  242. /**@brief Navigation data structure. */
  243. struct ble_lns_navigation_s
  244. {
  245. bool remaining_dist_present; /**< Remaining Distance present (0=not present, 1=present). */
  246. bool remaining_vert_dist_present; /**< Remaining Vertical Distance present (0=not present, 1=present). */
  247. bool eta_present; /**< Estimated Time of Arrival present (0=not present, 1=present). */
  248. ble_lns_pos_status_type_t position_status; /**< Status of last measured position. */
  249. ble_lns_heading_source_t heading_source; /**< Source of the heading measurement. */
  250. ble_lns_nav_indicator_type_t navigation_indicator_type; /**< Navigation indicator type. */
  251. bool waypoint_reached; /**< Waypoint Reached (0=not reached, 1=reached). */
  252. bool destination_reached; /**< Destination Reached (0=not reached, 1=reached). */
  253. uint16_t bearing; /**< Bearing (1/100 degrees).*/
  254. uint16_t heading; /**< Heading (1/100 degrees), size=24 bit. */
  255. uint32_t remaining_distance; /**< Remaining Distance (1/10 meters), size=24 bit. */
  256. int32_t remaining_vert_distance; /**< Remaining Vertical Distance (1/100 meters), size=24 bit. */
  257. ble_date_time_t eta; /**< Estimated Time of Arrival. */
  258. };
  259. /**@brief Function for initializing the Location and Navigation Service.
  260. *
  261. * @param[out] p_lns Location and Navigation Service structure. This structure must be supplied by
  262. * the application. It is initialized by this function, and will later
  263. * be used to identify this particular service instance.
  264. * @param[in] p_lns_init Information needed to initialize the service.
  265. *
  266. * @retval NRF_SUCCESS If the service was initialized successfully.
  267. * @retval NRF_ERROR_NULL If a NULL parameter was provided.
  268. * @retval NRF_INVALID_PARAMS If there is an inconsistency in the initialization structure.
  269. * @return Otherwise, an error code from either sd_ble_gatts_service_add() or sd_ble_gatts_characteristic_add() is returned.
  270. */
  271. ret_code_t ble_lns_init(ble_lns_t * p_lns, ble_lns_init_t const * p_lns_init);
  272. /**@brief Function for handling Location and Navigation Service BLE stack events.
  273. *
  274. * @details This function handles all events from the BLE stack that are of interest to the
  275. * Location and Navigation Service.
  276. *
  277. * @note The function returns when a NULL parameter is provided.
  278. *
  279. * @param[in] p_ble_evt Event received from the BLE stack.
  280. * @param[in] p_context Location and Navigation Service structure.
  281. */
  282. void ble_lns_on_ble_evt(ble_evt_t const * p_ble_evt, void * p_context);
  283. /**@brief Function for sending location and speed data if notification has been enabled.
  284. *
  285. * @details The application calls this function after having performed a location and speed determination.
  286. * If notification has been enabled, the location and speed data is encoded and sent to
  287. * the client.
  288. *
  289. * @param[in] p_lns Location and Navigation Service structure holding the location and speed data.
  290. *
  291. * @retval NRF_SUCCESS If the data was sent successfully.
  292. * @retval NRF_ERROR_NULL If a NULL parameter was provided.
  293. * @retval NRF_ERROR_INVALID_STATE If notification is disabled.
  294. */
  295. ret_code_t ble_lns_loc_speed_send(ble_lns_t * p_lns);
  296. /**@brief Function for sending navigation data if notification has been enabled.
  297. *
  298. * @details The application calls this function after having performed a navigation determination.
  299. * If notification has been enabled, the navigation data is encoded and sent to
  300. * the client.
  301. *
  302. * @param[in] p_lns Location and Navigation Service structure holding the navigation data.
  303. *
  304. * @retval NRF_SUCCESS If the data was sent successfully.
  305. * @retval NRF_ERROR_NULL If a NULL parameter was provided.
  306. * @retval NRF_ERROR_NOT_SUPPORTED If the navigation characteristic is absent.
  307. * @retval NRF_ERROR_INVALID_STATE If navigation is not running or notification is disabled.
  308. */
  309. ret_code_t ble_lns_navigation_send(ble_lns_t * p_lns);
  310. /**@brief Function for adding a route to the Location and Navigation Service.
  311. *
  312. * @param[in] p_lns Location and Navigation Service structure.
  313. * @param[in,out] p_route The new route to be added. The route ID is updated.
  314. *
  315. * @retval NRF_SUCCESS If the route was added successfully.
  316. * @retval NRF_ERROR_NULL If a NULL parameter was provided.
  317. * @retval NRF_ERROR_NOT_SUPPORTED If the navigation characteristic is absent.
  318. * @retval NRF_ERROR_NO_MEM If there is no memory left.
  319. * @retval NRF_ERROR_INTERNAL If there is an inconsistency in the routes table.
  320. */
  321. ret_code_t ble_lns_add_route(ble_lns_t * p_lns, ble_lns_route_t * p_route);
  322. /**@brief Function for removing a route from the Location and Navigation Service.
  323. *
  324. * @param[in] p_lns Location and Navigation Service structure.
  325. * @param[in] route_id The ID of the route to be removed.
  326. *
  327. * @retval NRF_SUCCESS If the route was removed successfully.
  328. * @retval NRF_ERROR_NULL If a NULL parameter was provided.
  329. * @retval NRF_ERROR_NOT_SUPPORTED If the navigation characteristic is absent.
  330. * @retval NRF_INVALID_PARAM If the route ID does not exist.
  331. */
  332. ret_code_t ble_lns_remove_route(ble_lns_t * p_lns, uint16_t route_id);
  333. #ifdef __cplusplus
  334. }
  335. #endif
  336. #endif // BLE_LNS_H__
  337. /** @} */