ble.h 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685
  1. /*
  2. * Copyright (c) 2012 - 2018, Nordic Semiconductor ASA
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice, this
  9. * list of conditions and the following disclaimer.
  10. *
  11. * 2. Redistributions in binary form, except as embedded into a Nordic
  12. * Semiconductor ASA integrated circuit in a product or a software update for
  13. * such product, must reproduce the above copyright notice, this list of
  14. * conditions and the following disclaimer in the documentation and/or other
  15. * materials provided with the distribution.
  16. *
  17. * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * 4. This software, with or without modification, must only be used with a
  22. * Nordic Semiconductor ASA integrated circuit.
  23. *
  24. * 5. Any software provided in binary form under this license must not be reverse
  25. * engineered, decompiled, modified and/or disassembled.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
  28. * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  29. * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
  31. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  32. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
  33. * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  34. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  35. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  36. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. */
  38. /**
  39. @addtogroup BLE_COMMON BLE SoftDevice Common
  40. @{
  41. @defgroup ble_api Events, type definitions and API calls
  42. @{
  43. @brief Module independent events, type definitions and API calls for the BLE SoftDevice.
  44. */
  45. #ifndef BLE_H__
  46. #define BLE_H__
  47. #include <stdint.h>
  48. #include "nrf_svc.h"
  49. #include "nrf_error.h"
  50. #include "ble_err.h"
  51. #include "ble_gap.h"
  52. #include "ble_l2cap.h"
  53. #include "ble_gatt.h"
  54. #include "ble_gattc.h"
  55. #include "ble_gatts.h"
  56. #ifdef __cplusplus
  57. extern "C" {
  58. #endif
  59. /** @addtogroup BLE_COMMON_ENUMERATIONS Enumerations
  60. * @{ */
  61. /**
  62. * @brief Common API SVC numbers.
  63. */
  64. enum BLE_COMMON_SVCS
  65. {
  66. SD_BLE_ENABLE = BLE_SVC_BASE, /**< Enable and initialize the BLE stack */
  67. SD_BLE_EVT_GET, /**< Get an event from the pending events queue. */
  68. SD_BLE_UUID_VS_ADD, /**< Add a Vendor Specific base UUID. */
  69. SD_BLE_UUID_DECODE, /**< Decode UUID bytes. */
  70. SD_BLE_UUID_ENCODE, /**< Encode UUID bytes. */
  71. SD_BLE_VERSION_GET, /**< Get the local version information (company ID, Link Layer Version, Link Layer Subversion). */
  72. SD_BLE_USER_MEM_REPLY, /**< User Memory Reply. */
  73. SD_BLE_OPT_SET, /**< Set a BLE option. */
  74. SD_BLE_OPT_GET, /**< Get a BLE option. */
  75. SD_BLE_CFG_SET, /**< Add a configuration to the BLE stack. */
  76. SD_BLE_UUID_VS_REMOVE, /**< Remove a Vendor Specific base UUID. */
  77. };
  78. /**
  79. * @brief BLE Module Independent Event IDs.
  80. */
  81. enum BLE_COMMON_EVTS
  82. {
  83. BLE_EVT_USER_MEM_REQUEST = BLE_EVT_BASE + 0, /**< User Memory request. @ref ble_evt_user_mem_request_t */
  84. BLE_EVT_USER_MEM_RELEASE = BLE_EVT_BASE + 1, /**< User Memory release. @ref ble_evt_user_mem_release_t */
  85. };
  86. /**@brief BLE Connection Configuration IDs.
  87. *
  88. * IDs that uniquely identify a connection configuration.
  89. */
  90. enum BLE_CONN_CFGS
  91. {
  92. BLE_CONN_CFG_GAP = BLE_CONN_CFG_BASE + 0, /**< BLE GAP specific connection configuration. */
  93. BLE_CONN_CFG_GATTC = BLE_CONN_CFG_BASE + 1, /**< BLE GATTC specific connection configuration. */
  94. BLE_CONN_CFG_GATTS = BLE_CONN_CFG_BASE + 2, /**< BLE GATTS specific connection configuration. */
  95. BLE_CONN_CFG_GATT = BLE_CONN_CFG_BASE + 3, /**< BLE GATT specific connection configuration. */
  96. BLE_CONN_CFG_L2CAP = BLE_CONN_CFG_BASE + 4, /**< BLE L2CAP specific connection configuration. */
  97. };
  98. /**@brief BLE Common Configuration IDs.
  99. *
  100. * IDs that uniquely identify a common configuration.
  101. */
  102. enum BLE_COMMON_CFGS
  103. {
  104. BLE_COMMON_CFG_VS_UUID = BLE_CFG_BASE, /**< Vendor specific base UUID configuration */
  105. };
  106. /**@brief Common Option IDs.
  107. * IDs that uniquely identify a common option.
  108. */
  109. enum BLE_COMMON_OPTS
  110. {
  111. BLE_COMMON_OPT_PA_LNA = BLE_OPT_BASE + 0, /**< PA and LNA options */
  112. BLE_COMMON_OPT_CONN_EVT_EXT = BLE_OPT_BASE + 1, /**< Extended connection events option */
  113. BLE_COMMON_OPT_EXTENDED_RC_CAL = BLE_OPT_BASE + 2, /**< Extended RC calibration option */
  114. BLE_COMMON_OPT_ADV_SCHED_CFG = BLE_OPT_BASE + 3, /**< Advertiser role scheduling configuration option */
  115. };
  116. /** @} */
  117. /** @addtogroup BLE_COMMON_DEFINES Defines
  118. * @{ */
  119. /** @brief Required pointer alignment for BLE Events.
  120. */
  121. #define BLE_EVT_PTR_ALIGNMENT 4
  122. /** @brief Leaves the maximum of the two arguments.
  123. */
  124. #define BLE_MAX(a, b) ((a) < (b) ? (b) : (a))
  125. /** @brief Maximum possible length for BLE Events.
  126. * @note The highest value used for @ref ble_gatt_conn_cfg_t::att_mtu in any connection configuration shall be used as a parameter.
  127. * If that value has not been configured for any connections then @ref BLE_GATT_ATT_MTU_DEFAULT must be used instead.
  128. */
  129. #define BLE_EVT_LEN_MAX(ATT_MTU) ( \
  130. offsetof(ble_evt_t, evt.gattc_evt.params.prim_srvc_disc_rsp.services) + ((ATT_MTU) - 1) / 4 * sizeof(ble_gattc_service_t) \
  131. )
  132. /** @defgroup ADV_SCHED_CFG Advertiser Role Scheduling Configuration
  133. * @{ */
  134. #define ADV_SCHED_CFG_DEFAULT 0 /**< Default advertiser role scheduling configuration. */
  135. #define ADV_SCHED_CFG_IMPROVED 1 /**< Improved advertiser role scheduling configuration in which the housekeeping time is reduced. */
  136. /** @} */
  137. /** @defgroup BLE_USER_MEM_TYPES User Memory Types
  138. * @{ */
  139. #define BLE_USER_MEM_TYPE_INVALID 0x00 /**< Invalid User Memory Types. */
  140. #define BLE_USER_MEM_TYPE_GATTS_QUEUED_WRITES 0x01 /**< User Memory for GATTS queued writes. */
  141. /** @} */
  142. /** @defgroup BLE_UUID_VS_COUNTS Vendor Specific base UUID counts
  143. * @{
  144. */
  145. #define BLE_UUID_VS_COUNT_DEFAULT 10 /**< Default VS UUID count. */
  146. #define BLE_UUID_VS_COUNT_MAX 254 /**< Maximum VS UUID count. */
  147. /** @} */
  148. /** @defgroup BLE_COMMON_CFG_DEFAULTS Configuration defaults.
  149. * @{
  150. */
  151. #define BLE_CONN_CFG_TAG_DEFAULT 0 /**< Default configuration tag, SoftDevice default connection configuration. */
  152. /** @} */
  153. /** @} */
  154. /** @addtogroup BLE_COMMON_STRUCTURES Structures
  155. * @{ */
  156. /**@brief User Memory Block. */
  157. typedef struct
  158. {
  159. uint8_t *p_mem; /**< Pointer to the start of the user memory block. */
  160. uint16_t len; /**< Length in bytes of the user memory block. */
  161. } ble_user_mem_block_t;
  162. /**@brief Event structure for @ref BLE_EVT_USER_MEM_REQUEST. */
  163. typedef struct
  164. {
  165. uint8_t type; /**< User memory type, see @ref BLE_USER_MEM_TYPES. */
  166. } ble_evt_user_mem_request_t;
  167. /**@brief Event structure for @ref BLE_EVT_USER_MEM_RELEASE. */
  168. typedef struct
  169. {
  170. uint8_t type; /**< User memory type, see @ref BLE_USER_MEM_TYPES. */
  171. ble_user_mem_block_t mem_block; /**< User memory block */
  172. } ble_evt_user_mem_release_t;
  173. /**@brief Event structure for events not associated with a specific function module. */
  174. typedef struct
  175. {
  176. uint16_t conn_handle; /**< Connection Handle on which this event occurred. */
  177. union
  178. {
  179. ble_evt_user_mem_request_t user_mem_request; /**< User Memory Request Event Parameters. */
  180. ble_evt_user_mem_release_t user_mem_release; /**< User Memory Release Event Parameters. */
  181. } params; /**< Event parameter union. */
  182. } ble_common_evt_t;
  183. /**@brief BLE Event header. */
  184. typedef struct
  185. {
  186. uint16_t evt_id; /**< Value from a BLE_<module>_EVT series. */
  187. uint16_t evt_len; /**< Length in octets including this header. */
  188. } ble_evt_hdr_t;
  189. /**@brief Common BLE Event type, wrapping the module specific event reports. */
  190. typedef struct
  191. {
  192. ble_evt_hdr_t header; /**< Event header. */
  193. union
  194. {
  195. ble_common_evt_t common_evt; /**< Common Event, evt_id in BLE_EVT_* series. */
  196. ble_gap_evt_t gap_evt; /**< GAP originated event, evt_id in BLE_GAP_EVT_* series. */
  197. ble_gattc_evt_t gattc_evt; /**< GATT client originated event, evt_id in BLE_GATTC_EVT* series. */
  198. ble_gatts_evt_t gatts_evt; /**< GATT server originated event, evt_id in BLE_GATTS_EVT* series. */
  199. ble_l2cap_evt_t l2cap_evt; /**< L2CAP originated event, evt_id in BLE_L2CAP_EVT* series. */
  200. } evt; /**< Event union. */
  201. } ble_evt_t;
  202. /**
  203. * @brief Version Information.
  204. */
  205. typedef struct
  206. {
  207. uint8_t version_number; /**< Link Layer Version number. See https://www.bluetooth.org/en-us/specification/assigned-numbers/link-layer for assigned values. */
  208. uint16_t company_id; /**< Company ID, Nordic Semiconductor's company ID is 89 (0x0059) (https://www.bluetooth.org/apps/content/Default.aspx?doc_id=49708). */
  209. uint16_t subversion_number; /**< Link Layer Sub Version number, corresponds to the SoftDevice Config ID or Firmware ID (FWID). */
  210. } ble_version_t;
  211. /**
  212. * @brief Configuration parameters for the PA and LNA.
  213. */
  214. typedef struct
  215. {
  216. uint8_t enable :1; /**< Enable toggling for this amplifier */
  217. uint8_t active_high :1; /**< Set the pin to be active high */
  218. uint8_t gpio_pin :6; /**< The GPIO pin to toggle for this amplifier */
  219. } ble_pa_lna_cfg_t;
  220. /**
  221. * @brief PA & LNA GPIO toggle configuration
  222. *
  223. * This option configures the SoftDevice to toggle pins when the radio is active for use with a power amplifier and/or
  224. * a low noise amplifier.
  225. *
  226. * Toggling the pins is achieved by using two PPI channels and a GPIOTE channel. The hardware channel IDs are provided
  227. * by the application and should be regarded as reserved as long as any PA/LNA toggling is enabled.
  228. *
  229. * @note @ref sd_ble_opt_get is not supported for this option.
  230. * @note Setting this option while the radio is in use (i.e. any of the roles are active) may have undefined consequences
  231. * and must be avoided by the application.
  232. */
  233. typedef struct
  234. {
  235. ble_pa_lna_cfg_t pa_cfg; /**< Power Amplifier configuration */
  236. ble_pa_lna_cfg_t lna_cfg; /**< Low Noise Amplifier configuration */
  237. uint8_t ppi_ch_id_set; /**< PPI channel used for radio pin setting */
  238. uint8_t ppi_ch_id_clr; /**< PPI channel used for radio pin clearing */
  239. uint8_t gpiote_ch_id; /**< GPIOTE channel used for radio pin toggling */
  240. } ble_common_opt_pa_lna_t;
  241. /**
  242. * @brief Configuration of extended BLE connection events.
  243. *
  244. * When enabled the SoftDevice will dynamically extend the connection event when possible.
  245. *
  246. * The connection event length is controlled by the connection configuration as set by @ref ble_gap_conn_cfg_t::event_length.
  247. * The connection event can be extended if there is time to send another packet pair before the start of the next connection interval,
  248. * and if there are no conflicts with other BLE roles requesting radio time.
  249. *
  250. * @note @ref sd_ble_opt_get is not supported for this option.
  251. */
  252. typedef struct
  253. {
  254. uint8_t enable : 1; /**< Enable extended BLE connection events, disabled by default. */
  255. } ble_common_opt_conn_evt_ext_t;
  256. /**
  257. * @brief Enable/disable extended RC calibration.
  258. *
  259. * If extended RC calibration is enabled and the internal RC oscillator (@ref NRF_CLOCK_LF_SRC_RC) is used as the SoftDevice
  260. * LFCLK source, the SoftDevice as a peripheral will by default try to increase the receive window if two consecutive packets
  261. * are not received. If it turns out that the packets were not received due to clock drift, the RC calibration is started.
  262. * This calibration comes in addition to the periodic calibration that is configured by @ref sd_softdevice_enable(). When
  263. * using only peripheral connections, the periodic calibration can therefore be configured with a much longer interval as the
  264. * peripheral will be able to detect and adjust automatically to clock drift, and calibrate on demand.
  265. *
  266. * If extended RC calibration is disabled and the internal RC oscillator is used as the SoftDevice LFCLK source, the
  267. * RC oscillator is calibrated periodically as configured by @ref sd_softdevice_enable().
  268. *
  269. * @note @ref sd_ble_opt_get is not supported for this option.
  270. */
  271. typedef struct
  272. {
  273. uint8_t enable : 1; /**< Enable extended RC calibration, enabled by default. */
  274. } ble_common_opt_extended_rc_cal_t;
  275. /**
  276. * @brief Configuration of BLE advertiser role scheduling.
  277. *
  278. * @note @ref sd_ble_opt_get is not supported for this option.
  279. */
  280. typedef struct
  281. {
  282. uint8_t sched_cfg; /**< See @ref ADV_SCHED_CFG. */
  283. } ble_common_opt_adv_sched_cfg_t;
  284. /**@brief Option structure for common options. */
  285. typedef union
  286. {
  287. ble_common_opt_pa_lna_t pa_lna; /**< Parameters for controlling PA and LNA pin toggling. */
  288. ble_common_opt_conn_evt_ext_t conn_evt_ext; /**< Parameters for enabling extended connection events. */
  289. ble_common_opt_extended_rc_cal_t extended_rc_cal; /**< Parameters for enabling extended RC calibration. */
  290. ble_common_opt_adv_sched_cfg_t adv_sched_cfg; /**< Parameters for configuring advertiser role scheduling. */
  291. } ble_common_opt_t;
  292. /**@brief Common BLE Option type, wrapping the module specific options. */
  293. typedef union
  294. {
  295. ble_common_opt_t common_opt; /**< COMMON options, opt_id in @ref BLE_COMMON_OPTS series. */
  296. ble_gap_opt_t gap_opt; /**< GAP option, opt_id in @ref BLE_GAP_OPTS series. */
  297. } ble_opt_t;
  298. /**@brief BLE connection configuration type, wrapping the module specific configurations, set with
  299. * @ref sd_ble_cfg_set.
  300. *
  301. * @note Connection configurations don't have to be set.
  302. * In the case that no configurations has been set, or fewer connection configurations has been set than enabled connections,
  303. * the default connection configuration will be automatically added for the remaining connections.
  304. * When creating connections with the default configuration, @ref BLE_CONN_CFG_TAG_DEFAULT should be used in
  305. * place of @ref ble_conn_cfg_t::conn_cfg_tag.
  306. *
  307. * @sa sd_ble_gap_adv_start()
  308. * @sa sd_ble_gap_connect()
  309. *
  310. * @mscs
  311. * @mmsc{@ref BLE_CONN_CFG}
  312. * @endmscs
  313. */
  314. typedef struct
  315. {
  316. uint8_t conn_cfg_tag; /**< The application chosen tag it can use with the
  317. @ref sd_ble_gap_adv_start() and @ref sd_ble_gap_connect() calls
  318. to select this configuration when creating a connection.
  319. Must be different for all connection configurations added and not @ref BLE_CONN_CFG_TAG_DEFAULT. */
  320. union {
  321. ble_gap_conn_cfg_t gap_conn_cfg; /**< GAP connection configuration, cfg_id is @ref BLE_CONN_CFG_GAP. */
  322. ble_gattc_conn_cfg_t gattc_conn_cfg; /**< GATTC connection configuration, cfg_id is @ref BLE_CONN_CFG_GATTC. */
  323. ble_gatts_conn_cfg_t gatts_conn_cfg; /**< GATTS connection configuration, cfg_id is @ref BLE_CONN_CFG_GATTS. */
  324. ble_gatt_conn_cfg_t gatt_conn_cfg; /**< GATT connection configuration, cfg_id is @ref BLE_CONN_CFG_GATT. */
  325. ble_l2cap_conn_cfg_t l2cap_conn_cfg; /**< L2CAP connection configuration, cfg_id is @ref BLE_CONN_CFG_L2CAP. */
  326. } params; /**< Connection configuration union. */
  327. } ble_conn_cfg_t;
  328. /**
  329. * @brief Configuration of Vendor Specific base UUIDs, set with @ref sd_ble_cfg_set.
  330. *
  331. * @retval ::NRF_ERROR_INVALID_PARAM Too many UUIDs configured.
  332. */
  333. typedef struct
  334. {
  335. uint8_t vs_uuid_count; /**< Number of 128-bit Vendor Specific base UUID bases to allocate memory for.
  336. Default value is @ref BLE_UUID_VS_COUNT_DEFAULT. Maximum value is
  337. @ref BLE_UUID_VS_COUNT_MAX. */
  338. } ble_common_cfg_vs_uuid_t;
  339. /**@brief Common BLE Configuration type, wrapping the common configurations. */
  340. typedef union
  341. {
  342. ble_common_cfg_vs_uuid_t vs_uuid_cfg; /**< Vendor Specific base UUID configuration, cfg_id is @ref BLE_COMMON_CFG_VS_UUID. */
  343. } ble_common_cfg_t;
  344. /**@brief BLE Configuration type, wrapping the module specific configurations. */
  345. typedef union
  346. {
  347. ble_conn_cfg_t conn_cfg; /**< Connection specific configurations, cfg_id in @ref BLE_CONN_CFGS series. */
  348. ble_common_cfg_t common_cfg; /**< Global common configurations, cfg_id in @ref BLE_COMMON_CFGS series. */
  349. ble_gap_cfg_t gap_cfg; /**< Global GAP configurations, cfg_id in @ref BLE_GAP_CFGS series. */
  350. ble_gatts_cfg_t gatts_cfg; /**< Global GATTS configuration, cfg_id in @ref BLE_GATTS_CFGS series. */
  351. } ble_cfg_t;
  352. /** @} */
  353. /** @addtogroup BLE_COMMON_FUNCTIONS Functions
  354. * @{ */
  355. /**@brief Enable the BLE stack
  356. *
  357. * @param[in, out] p_app_ram_base Pointer to a variable containing the start address of the
  358. * application RAM region (APP_RAM_BASE). On return, this will
  359. * contain the minimum start address of the application RAM region
  360. * required by the SoftDevice for this configuration.
  361. *
  362. * @note The memory requirement for a specific configuration will not increase between SoftDevices
  363. * with the same major version number.
  364. *
  365. * @note At runtime the IC's RAM is split into 2 regions: The SoftDevice RAM region is located
  366. * between 0x20000000 and APP_RAM_BASE-1 and the application's RAM region is located between
  367. * APP_RAM_BASE and the start of the call stack.
  368. *
  369. * @details This call initializes the BLE stack, no BLE related function other than @ref
  370. * sd_ble_cfg_set can be called before this one.
  371. *
  372. * @mscs
  373. * @mmsc{@ref BLE_COMMON_ENABLE}
  374. * @endmscs
  375. *
  376. * @retval ::NRF_SUCCESS The BLE stack has been initialized successfully.
  377. * @retval ::NRF_ERROR_INVALID_STATE The BLE stack had already been initialized and cannot be reinitialized.
  378. * @retval ::NRF_ERROR_INVALID_ADDR Invalid or not sufficiently aligned pointer supplied.
  379. * @retval ::NRF_ERROR_NO_MEM One or more of the following is true:
  380. * - The amount of memory assigned to the SoftDevice by *p_app_ram_base is not
  381. * large enough to fit this configuration's memory requirement. Check *p_app_ram_base
  382. * and set the start address of the application RAM region accordingly.
  383. * - Dynamic part of the SoftDevice RAM region is larger then 64 kB which
  384. * is currently not supported.
  385. * @retval ::NRF_ERROR_RESOURCES The total number of L2CAP Channels configured using @ref sd_ble_cfg_set is too large.
  386. */
  387. SVCALL(SD_BLE_ENABLE, uint32_t, sd_ble_enable(uint32_t * p_app_ram_base));
  388. /**@brief Add configurations for the BLE stack
  389. *
  390. * @param[in] cfg_id Config ID, see @ref BLE_CONN_CFGS, @ref BLE_COMMON_CFGS, @ref
  391. * BLE_GAP_CFGS or @ref BLE_GATTS_CFGS.
  392. * @param[in] p_cfg Pointer to a ble_cfg_t structure containing the configuration value.
  393. * @param[in] app_ram_base The start address of the application RAM region (APP_RAM_BASE).
  394. * See @ref sd_ble_enable for details about APP_RAM_BASE.
  395. *
  396. * @note The memory requirement for a specific configuration will not increase between SoftDevices
  397. * with the same major version number.
  398. *
  399. * @note If a configuration is set more than once, the last one set is the one that takes effect on
  400. * @ref sd_ble_enable.
  401. *
  402. * @note Any part of the BLE stack that is NOT configured with @ref sd_ble_cfg_set will have default
  403. * configuration.
  404. *
  405. * @note @ref sd_ble_cfg_set may be called at any time when the SoftDevice is enabled (see @ref
  406. * sd_softdevice_enable) while the BLE part of the SoftDevice is not enabled (see @ref
  407. * sd_ble_enable).
  408. *
  409. * @note Error codes for the configurations are described in the configuration structs.
  410. *
  411. * @mscs
  412. * @mmsc{@ref BLE_COMMON_ENABLE}
  413. * @endmscs
  414. *
  415. * @retval ::NRF_SUCCESS The configuration has been added successfully.
  416. * @retval ::NRF_ERROR_INVALID_STATE The BLE stack had already been initialized.
  417. * @retval ::NRF_ERROR_INVALID_ADDR Invalid or not sufficiently aligned pointer supplied.
  418. * @retval ::NRF_ERROR_INVALID_PARAM Invalid cfg_id supplied.
  419. * @retval ::NRF_ERROR_NO_MEM The amount of memory assigned to the SoftDevice by app_ram_base is not
  420. * large enough to fit this configuration's memory requirement.
  421. */
  422. SVCALL(SD_BLE_CFG_SET, uint32_t, sd_ble_cfg_set(uint32_t cfg_id, ble_cfg_t const * p_cfg, uint32_t app_ram_base));
  423. /**@brief Get an event from the pending events queue.
  424. *
  425. * @param[out] p_dest Pointer to buffer to be filled in with an event, or NULL to retrieve the event length.
  426. * This buffer <b>must be aligned to the extend defined by @ref BLE_EVT_PTR_ALIGNMENT</b>.
  427. * The buffer should be interpreted as a @ref ble_evt_t struct.
  428. * @param[in, out] p_len Pointer the length of the buffer, on return it is filled with the event length.
  429. *
  430. * @details This call allows the application to pull a BLE event from the BLE stack. The application is signaled that
  431. * an event is available from the BLE stack by the triggering of the SD_EVT_IRQn interrupt.
  432. * The application is free to choose whether to call this function from thread mode (main context) or directly from the
  433. * Interrupt Service Routine that maps to SD_EVT_IRQn. In any case however, and because the BLE stack runs at a higher
  434. * priority than the application, this function should be called in a loop (until @ref NRF_ERROR_NOT_FOUND is returned)
  435. * every time SD_EVT_IRQn is raised to ensure that all available events are pulled from the BLE stack. Failure to do so
  436. * could potentially leave events in the internal queue without the application being aware of this fact.
  437. *
  438. * Sizing the p_dest buffer is equally important, since the application needs to provide all the memory necessary for the event to
  439. * be copied into application memory. If the buffer provided is not large enough to fit the entire contents of the event,
  440. * @ref NRF_ERROR_DATA_SIZE will be returned and the application can then call again with a larger buffer size.
  441. * The maximum possible event length is defined by @ref BLE_EVT_LEN_MAX. The application may also "peek" the event length
  442. * by providing p_dest as a NULL pointer and inspecting the value of *p_len upon return:
  443. *
  444. * \code
  445. * uint16_t len;
  446. * errcode = sd_ble_evt_get(NULL, &len);
  447. * \endcode
  448. *
  449. * @mscs
  450. * @mmsc{@ref BLE_COMMON_IRQ_EVT_MSC}
  451. * @mmsc{@ref BLE_COMMON_THREAD_EVT_MSC}
  452. * @endmscs
  453. *
  454. * @retval ::NRF_SUCCESS Event pulled and stored into the supplied buffer.
  455. * @retval ::NRF_ERROR_INVALID_ADDR Invalid or not sufficiently aligned pointer supplied.
  456. * @retval ::NRF_ERROR_NOT_FOUND No events ready to be pulled.
  457. * @retval ::NRF_ERROR_DATA_SIZE Event ready but could not fit into the supplied buffer.
  458. */
  459. SVCALL(SD_BLE_EVT_GET, uint32_t, sd_ble_evt_get(uint8_t *p_dest, uint16_t *p_len));
  460. /**@brief Add a Vendor Specific base UUID.
  461. *
  462. * @details This call enables the application to add a Vendor Specific base UUID to the BLE stack's table, for later
  463. * use with all other modules and APIs. This then allows the application to use the shorter, 24-bit @ref ble_uuid_t
  464. * format when dealing with both 16-bit and 128-bit UUIDs without having to check for lengths and having split code
  465. * paths. This is accomplished by extending the grouping mechanism that the Bluetooth SIG standard base UUID uses
  466. * for all other 128-bit UUIDs. The type field in the @ref ble_uuid_t structure is an index (relative to
  467. * @ref BLE_UUID_TYPE_VENDOR_BEGIN) to the table populated by multiple calls to this function, and the UUID field
  468. * in the same structure contains the 2 bytes at indexes 12 and 13. The number of possible 128-bit UUIDs available to
  469. * the application is therefore the number of Vendor Specific UUIDs added with the help of this function times 65536,
  470. * although restricted to modifying bytes 12 and 13 for each of the entries in the supplied array.
  471. *
  472. * @note Bytes 12 and 13 of the provided UUID will not be used internally, since those are always replaced by
  473. * the 16-bit uuid field in @ref ble_uuid_t.
  474. *
  475. * @note If a UUID is already present in the BLE stack's internal table, the corresponding index will be returned in
  476. * p_uuid_type along with an @ref NRF_SUCCESS error code.
  477. *
  478. * @param[in] p_vs_uuid Pointer to a 16-octet (128-bit) little endian Vendor Specific base UUID disregarding
  479. * bytes 12 and 13.
  480. * @param[out] p_uuid_type Pointer to a uint8_t where the type field in @ref ble_uuid_t corresponding to this UUID will be stored.
  481. *
  482. * @retval ::NRF_SUCCESS Successfully added the Vendor Specific base UUID.
  483. * @retval ::NRF_ERROR_INVALID_ADDR If p_vs_uuid or p_uuid_type is NULL or invalid.
  484. * @retval ::NRF_ERROR_NO_MEM If there are no more free slots for VS UUIDs.
  485. */
  486. SVCALL(SD_BLE_UUID_VS_ADD, uint32_t, sd_ble_uuid_vs_add(ble_uuid128_t const *p_vs_uuid, uint8_t *p_uuid_type));
  487. /**@brief Remove a Vendor Specific base UUID.
  488. *
  489. * @details This call removes a Vendor Specific base UUID that has been added with @ref sd_ble_uuid_vs_add. This function allows
  490. * the application to reuse memory allocated for Vendor Specific base UUIDs.
  491. *
  492. * @note Currently this function can only be called with a p_uuid_type set to @ref BLE_UUID_TYPE_UNKNOWN or the last added UUID type.
  493. *
  494. * @param[in] p_uuid_type Pointer to a uint8_t where the type field in @ref ble_uuid_t::type corresponds to the UUID type that
  495. * shall be removed. If the type is set to @ref BLE_UUID_TYPE_UNKNOWN, or the pointer is NULL, the last
  496. * Vendor Specific base UUID will be removed.
  497. * @param[out] p_uuid_type Pointer to a uint8_t where the type field in @ref ble_uuid_t corresponds to the UUID type that was
  498. * removed. If function returns with a failure, it contains the last type that is in use by the ATT Server.
  499. *
  500. * @retval ::NRF_SUCCESS Successfully removed the Vendor Specific base UUID.
  501. * @retval ::NRF_ERROR_INVALID_ADDR If p_uuid_type is invalid.
  502. * @retval ::NRF_ERROR_INVALID_PARAM If p_uuid_type points to a non-valid UUID type.
  503. * @retval ::NRF_ERROR_FORBIDDEN If the Vendor Specific base UUID is in use by the ATT Server.
  504. */
  505. SVCALL(SD_BLE_UUID_VS_REMOVE, uint32_t, sd_ble_uuid_vs_remove(uint8_t *p_uuid_type));
  506. /** @brief Decode little endian raw UUID bytes (16-bit or 128-bit) into a 24 bit @ref ble_uuid_t structure.
  507. *
  508. * @details The raw UUID bytes excluding bytes 12 and 13 (i.e. bytes 0-11 and 14-15) of p_uuid_le are compared
  509. * to the corresponding ones in each entry of the table of Vendor Specific base UUIDs populated with @ref sd_ble_uuid_vs_add
  510. * to look for a match. If there is such a match, bytes 12 and 13 are returned as p_uuid->uuid and the index
  511. * relative to @ref BLE_UUID_TYPE_VENDOR_BEGIN as p_uuid->type.
  512. *
  513. * @note If the UUID length supplied is 2, then the type set by this call will always be @ref BLE_UUID_TYPE_BLE.
  514. *
  515. * @param[in] uuid_le_len Length in bytes of the buffer pointed to by p_uuid_le (must be 2 or 16 bytes).
  516. * @param[in] p_uuid_le Pointer pointing to little endian raw UUID bytes.
  517. * @param[out] p_uuid Pointer to a @ref ble_uuid_t structure to be filled in.
  518. *
  519. * @retval ::NRF_SUCCESS Successfully decoded into the @ref ble_uuid_t structure.
  520. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  521. * @retval ::NRF_ERROR_INVALID_LENGTH Invalid UUID length.
  522. * @retval ::NRF_ERROR_NOT_FOUND For a 128-bit UUID, no match in the populated table of UUIDs.
  523. */
  524. SVCALL(SD_BLE_UUID_DECODE, uint32_t, sd_ble_uuid_decode(uint8_t uuid_le_len, uint8_t const *p_uuid_le, ble_uuid_t *p_uuid));
  525. /** @brief Encode a @ref ble_uuid_t structure into little endian raw UUID bytes (16-bit or 128-bit).
  526. *
  527. * @note The pointer to the destination buffer p_uuid_le may be NULL, in which case only the validity and size of p_uuid is computed.
  528. *
  529. * @param[in] p_uuid Pointer to a @ref ble_uuid_t structure that will be encoded into bytes.
  530. * @param[out] p_uuid_le_len Pointer to a uint8_t that will be filled with the encoded length (2 or 16 bytes).
  531. * @param[out] p_uuid_le Pointer to a buffer where the little endian raw UUID bytes (2 or 16) will be stored.
  532. *
  533. * @retval ::NRF_SUCCESS Successfully encoded into the buffer.
  534. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  535. * @retval ::NRF_ERROR_INVALID_PARAM Invalid UUID type.
  536. */
  537. SVCALL(SD_BLE_UUID_ENCODE, uint32_t, sd_ble_uuid_encode(ble_uuid_t const *p_uuid, uint8_t *p_uuid_le_len, uint8_t *p_uuid_le));
  538. /**@brief Get Version Information.
  539. *
  540. * @details This call allows the application to get the BLE stack version information.
  541. *
  542. * @param[out] p_version Pointer to a ble_version_t structure to be filled in.
  543. *
  544. * @retval ::NRF_SUCCESS Version information stored successfully.
  545. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  546. * @retval ::NRF_ERROR_BUSY The BLE stack is busy (typically doing a locally-initiated disconnection procedure).
  547. */
  548. SVCALL(SD_BLE_VERSION_GET, uint32_t, sd_ble_version_get(ble_version_t *p_version));
  549. /**@brief Provide a user memory block.
  550. *
  551. * @note This call can only be used as a response to a @ref BLE_EVT_USER_MEM_REQUEST event issued to the application.
  552. *
  553. * @param[in] conn_handle Connection handle.
  554. * @param[in] p_block Pointer to a user memory block structure or NULL if memory is managed by the application.
  555. *
  556. * @mscs
  557. * @mmsc{@ref BLE_GATTS_QUEUED_WRITE_PEER_CANCEL_MSC}
  558. * @mmsc{@ref BLE_GATTS_QUEUED_WRITE_NOBUF_AUTH_MSC}
  559. * @mmsc{@ref BLE_GATTS_QUEUED_WRITE_NOBUF_NOAUTH_MSC}
  560. * @mmsc{@ref BLE_GATTS_QUEUED_WRITE_BUF_AUTH_MSC}
  561. * @mmsc{@ref BLE_GATTS_QUEUED_WRITE_BUF_NOAUTH_MSC}
  562. * @mmsc{@ref BLE_GATTS_QUEUED_WRITE_QUEUE_FULL_MSC}
  563. * @endmscs
  564. *
  565. * @retval ::NRF_SUCCESS Successfully queued a response to the peer.
  566. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  567. * @retval ::NRF_ERROR_BUSY The stack is busy, process pending events and retry.
  568. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  569. * @retval ::NRF_ERROR_INVALID_LENGTH Invalid user memory block length supplied.
  570. * @retval ::NRF_ERROR_INVALID_STATE Invalid Connection state or no user memory request pending.
  571. */
  572. SVCALL(SD_BLE_USER_MEM_REPLY, uint32_t, sd_ble_user_mem_reply(uint16_t conn_handle, ble_user_mem_block_t const *p_block));
  573. /**@brief Set a BLE option.
  574. *
  575. * @details This call allows the application to set the value of an option.
  576. *
  577. * @mscs
  578. * @mmsc{@ref BLE_GAP_PERIPH_BONDING_STATIC_PK_MSC}
  579. * @endmscs
  580. *
  581. * @param[in] opt_id Option ID, see @ref BLE_COMMON_OPTS and @ref BLE_GAP_OPTS.
  582. * @param[in] p_opt Pointer to a ble_opt_t structure containing the option value.
  583. *
  584. * @retval ::NRF_SUCCESS Option set successfully.
  585. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  586. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  587. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, check parameter limits and constraints.
  588. * @retval ::NRF_ERROR_INVALID_STATE Unable to set the parameter at this time.
  589. * @retval ::NRF_ERROR_BUSY The BLE stack is busy or the previous procedure has not completed.
  590. */
  591. SVCALL(SD_BLE_OPT_SET, uint32_t, sd_ble_opt_set(uint32_t opt_id, ble_opt_t const *p_opt));
  592. /**@brief Get a BLE option.
  593. *
  594. * @details This call allows the application to retrieve the value of an option.
  595. *
  596. * @param[in] opt_id Option ID, see @ref BLE_COMMON_OPTS and @ref BLE_GAP_OPTS.
  597. * @param[out] p_opt Pointer to a ble_opt_t structure to be filled in.
  598. *
  599. * @retval ::NRF_SUCCESS Option retrieved successfully.
  600. * @retval ::NRF_ERROR_INVALID_ADDR Invalid pointer supplied.
  601. * @retval ::BLE_ERROR_INVALID_CONN_HANDLE Invalid Connection Handle.
  602. * @retval ::NRF_ERROR_INVALID_PARAM Invalid parameter(s) supplied, check parameter limits and constraints.
  603. * @retval ::NRF_ERROR_INVALID_STATE Unable to retrieve the parameter at this time.
  604. * @retval ::NRF_ERROR_BUSY The BLE stack is busy or the previous procedure has not completed.
  605. * @retval ::NRF_ERROR_NOT_SUPPORTED This option is not supported.
  606. *
  607. */
  608. SVCALL(SD_BLE_OPT_GET, uint32_t, sd_ble_opt_get(uint32_t opt_id, ble_opt_t *p_opt));
  609. /** @} */
  610. #ifdef __cplusplus
  611. }
  612. #endif
  613. #endif /* BLE_H__ */
  614. /**
  615. @}
  616. @}
  617. */