ble_ln_cp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  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_sdk_srv_lncp Location and Navigation Service Control Point
  43. * @{
  44. * @ingroup ble_sdk_srv
  45. * @brief Location and Navigation Service Control Point module
  46. *
  47. * @details This module implements the Location and Navigation Service Control Point behavior.
  48. */
  49. #ifndef BLE_LN_CTRLPT_H__
  50. #define BLE_LN_CTRLPT_H__
  51. #include "ble_srv_common.h"
  52. #include "sdk_common.h"
  53. #include "nrf_ble_gq.h"
  54. #ifdef __cplusplus
  55. extern "C" {
  56. #endif
  57. #define BLE_LNS_MAX_ROUTE_NAME_LEN BLE_GATT_ATT_MTU_DEFAULT - 5 /**< The maximum length of length of a route name. */
  58. #define MAX_CTRL_POINT_RESP_PARAM_LEN BLE_LNS_MAX_ROUTE_NAME_LEN + 3 /**< Maximum length of a control point response. */
  59. typedef struct ble_lncp_s ble_lncp_t;
  60. /** @brief Location and Navigation event type. This list defines the possible events types from the Location and Navigation Service. */
  61. typedef enum
  62. {
  63. LNCP_EVT_ELEVATION_SET, /**< Location and Navigation elevation was set. */
  64. LNCP_EVT_FIX_RATE_SET, /**< Fix rate was set. */
  65. LNCP_EVT_ROUTE_SELECTED, /**< A route was selected. */
  66. LNCP_EVT_NAV_COMMAND, /**< A navigation command was issued. */
  67. LNCP_EVT_MASK_SET, /**< Location and Speed feature mask was set. */
  68. LNCP_EVT_TOTAL_DISTANCE_SET /**< Location and Navigation total distance was set. */
  69. } ble_lncp_evt_type_t;
  70. /** @brief Navigation commands. These commands can be sent to the control point and returned by an event callback. */
  71. typedef enum
  72. {
  73. LNCP_CMD_NAV_STOP = 0x00, /**< When received, is_navigation_running in @ref ble_lns_s will be set to false. */
  74. LNCP_CMD_NAV_START = 0x01, /**< When received, is_navigation_running in @ref ble_lns_s will be set to true. */
  75. LNCP_CMD_NAV_PAUSE = 0x02, /**< When received, is_navigation_running in @ref ble_lns_s will be set to false. */
  76. LNCP_CMD_NAV_CONTINUE = 0x03, /**< When received, is_navigation_running in @ref ble_lns_s will be set to true. */
  77. LNCP_CMD_NAV_SKIP_WAYPOINT = 0x04, /**< When received, is_navigation_running in @ref ble_lns_s will not be affected. */
  78. LNCP_CMD_NAV_NEAREST = 0x05, /**< When received, is_navigation_running in @ref ble_lns_s will be set to true. */
  79. } ble_lncp_nav_cmd_t;
  80. #define LNCP_NAV_CMD_MAX 0x05
  81. #define LNCP_NAV_CMD_LEN (OPCODE_LENGTH + 1)
  82. #if defined(__CC_ARM)
  83. #pragma push
  84. #pragma anon_unions
  85. #elif defined(__ICCARM__)
  86. #pragma language=extended
  87. #elif defined(__GNUC__)
  88. /* anonymous unions are enabled by default */
  89. #endif
  90. /** @brief A mask can be used to temporarily enable and disable features of the Location and Speed characteristic.*/
  91. typedef union
  92. {
  93. uint8_t flags;
  94. struct
  95. {
  96. uint8_t instantaneous_speed :1;
  97. uint8_t total_distance :1;
  98. uint8_t location :1;
  99. uint8_t elevation :1;
  100. uint8_t heading :1;
  101. uint8_t rolling_time :1;
  102. uint8_t utc_time :1;
  103. };
  104. } ble_lncp_mask_t;
  105. #if defined(__CC_ARM)
  106. #pragma pop
  107. #elif defined(__ICCARM__)
  108. /* leave anonymous unions enabled */
  109. #elif defined(__GNUC__)
  110. /* anonymous unions are enabled by default */
  111. #endif
  112. typedef struct
  113. {
  114. ble_lncp_evt_type_t evt_type;
  115. union
  116. {
  117. ble_lncp_mask_t mask;
  118. ble_lncp_nav_cmd_t nav_cmd;
  119. uint32_t total_distance;
  120. uint8_t fix_rate;
  121. uint16_t selected_route;
  122. uint32_t elevation;
  123. } params;
  124. } ble_lncp_evt_t;
  125. // Location and Navigation Control Point response values
  126. typedef enum
  127. {
  128. LNCP_RSP_RESERVED = 0x00, /**< Reserved for future use. */
  129. LNCP_RSP_SUCCESS = 0x01, /**< Success. */
  130. LNCP_RSP_OP_CODE_NOT_SUPPORTED = 0x02, /**< Op Code not supported. */
  131. LNCP_RSP_INVALID_PARAMETER = 0x03, /**< Invalid Parameter. */
  132. LNCP_RSP_OPERATION_FAILED = 0x04, /**< Operation Failed. */
  133. } ble_lncp_rsp_code_t;
  134. typedef ble_lncp_rsp_code_t (*ble_lncp_evt_handler_t) (ble_lncp_t const * p_lncp, ble_lncp_evt_t const * p_evt);
  135. // Location and Navigation Control Point Op Code values
  136. typedef enum
  137. {
  138. LNCP_OP_RESERVED = 0x00, /**< Reserved for future use. */
  139. LNCP_OP_SET_CUMULATIVE_VALUE = 0x01, /**< Set Cumulative Value. */
  140. LNCP_OP_MASK_LOC_SPEED_CONTENT = 0x02, /**< Mask Location and Speed Characteristic Content. */
  141. LNCP_OP_NAV_CONTROL = 0x03, /**< Navigation Control. */
  142. LNCP_OP_REQ_NUM_ROUTES = 0x04, /**< Request Number of Routes. */
  143. LNCP_OP_REQ_NAME_OF_ROUTE = 0x05, /**< Request Name of Route. */
  144. LNCP_OP_SELECT_ROUTE = 0x06, /**< Select Route. */
  145. LNCP_OP_SET_FIX_RATE = 0x07, /**< Set Fix Rate. */
  146. LNCP_OP_SET_ELEVATION = 0x08, /**< Set Elevation. */
  147. LNCP_OP_RESPONSE_CODE = 0x20 /**< Response code. */
  148. } ble_lncp_op_code_t;
  149. /** @brief Information included in a control point write response indication. */
  150. typedef struct
  151. {
  152. ble_lncp_op_code_t op_code; /**< Opcode of the control point write action. */
  153. ble_lncp_rsp_code_t rsp_code; /**< Response code of the control point write action. */
  154. uint8_t rsp_param_len;
  155. uint8_t rsp_param[MAX_CTRL_POINT_RESP_PARAM_LEN];
  156. } ble_lncp_rsp_t;
  157. typedef struct
  158. {
  159. uint16_t service_handle;
  160. ble_lncp_evt_handler_t evt_handler;
  161. ble_srv_error_handler_t error_handler;
  162. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT queue instance. */
  163. uint32_t available_features; /**< Value of the LN feature. */
  164. bool is_position_quality_present; /**< If set to true, the position quality characteristic will be added. Else not. */
  165. bool is_control_point_present; /**< If set to true, the control point characteristic will be added. Else not. */
  166. bool is_navigation_present; /**< If set to true, the navigation characteristic will be added. Else not. */
  167. ble_gatts_char_handles_t navigation_handles;
  168. uint32_t total_distance;
  169. uint32_t elevation;
  170. security_req_t write_perm;
  171. security_req_t cccd_write_perm;
  172. } ble_lncp_init_t;
  173. struct ble_lncp_s
  174. {
  175. uint16_t conn_handle;
  176. uint16_t service_handle;
  177. ble_gatts_char_handles_t ctrlpt_handles;
  178. ble_gatts_char_handles_t navigation_handles;
  179. ble_lncp_evt_handler_t evt_handler;
  180. ble_srv_error_handler_t error_handler;
  181. ble_lncp_rsp_t pending_rsp;
  182. nrf_ble_gq_t * p_gatt_queue; /**< Pointer to BLE GATT queue instance. */
  183. ble_lncp_mask_t mask;
  184. uint32_t total_distance;
  185. uint32_t elevation;
  186. uint8_t fix_rate;
  187. uint16_t selected_route;
  188. uint32_t available_features; /**< Value of the LN feature. */
  189. bool is_position_quality_present; /**< If set to true, the position quality characteristic will be added. Else not. */
  190. bool is_control_point_present; /**< If set to true, the control point characteristic will be added. Else not. */
  191. bool is_navigation_present; /**< If set to true, the navigation characteristic will be added. Else not. */
  192. bool is_navigation_running; /**< This variable can be set using the control point. Must be true to be able to send navigation updates. */
  193. bool is_ctrlpt_indication_enabled; /**< True if indication is enabled on the Control Point characteristic. */
  194. bool is_nav_notification_enabled; /**< True if notification is enabled on the Navigation characteristic. */
  195. bool is_indication_pending; /**< True if Control Point indication is pending. */
  196. };
  197. void ble_lncp_on_ble_evt(ble_lncp_t * p_lncp, ble_evt_t const * p_ble_evt);
  198. uint32_t ble_lncp_total_distance_get(ble_lncp_t const * p_lncp);
  199. uint32_t ble_lncp_elevation_get(ble_lncp_t const * p_lncp);
  200. ble_lncp_mask_t ble_lncp_mask_get(ble_lncp_t const * p_lncp);
  201. bool ble_lncp_is_navigation_running(ble_lncp_t const * p_lncp);
  202. ret_code_t ble_lncp_init(ble_lncp_t * p_lncp, ble_lncp_init_t const * p_lncp_init);
  203. #ifdef __cplusplus
  204. }
  205. #endif
  206. #endif //BLE_LN_CTRLPT_H__
  207. /** @} */