bluetooth.h 69 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183
  1. /** @file
  2. * @brief Bluetooth subsystem core APIs.
  3. */
  4. /*
  5. * Copyright (c) 2017 Nordic Semiconductor ASA
  6. * Copyright (c) 2015-2016 Intel Corporation
  7. *
  8. * SPDX-License-Identifier: Apache-2.0
  9. */
  10. #ifndef ZEPHYR_INCLUDE_BLUETOOTH_BLUETOOTH_H_
  11. #define ZEPHYR_INCLUDE_BLUETOOTH_BLUETOOTH_H_
  12. /**
  13. * @brief Bluetooth APIs
  14. * @defgroup bluetooth Bluetooth APIs
  15. * @{
  16. */
  17. #include <stdbool.h>
  18. #include <string.h>
  19. #include <sys/util.h>
  20. #include <net/buf.h>
  21. #include <bluetooth/gap.h>
  22. #include <bluetooth/addr.h>
  23. #include <bluetooth/crypto.h>
  24. #ifdef __cplusplus
  25. extern "C" {
  26. #endif
  27. /**
  28. * @brief Generic Access Profile
  29. * @defgroup bt_gap Generic Access Profile
  30. * @ingroup bluetooth
  31. * @{
  32. */
  33. /**
  34. * @def BT_ID_DEFAULT
  35. *
  36. * Convenience macro for specifying the default identity. This helps
  37. * make the code more readable, especially when only one identity is
  38. * supported.
  39. */
  40. #define BT_ID_DEFAULT 0
  41. /** Opaque type representing an advertiser. */
  42. struct bt_le_ext_adv;
  43. /** Opaque type representing an periodic advertising sync. */
  44. struct bt_le_per_adv_sync;
  45. /* Don't require everyone to include conn.h */
  46. struct bt_conn;
  47. /* Don't require everyone to include iso.h */
  48. struct bt_iso_biginfo;
  49. /* Don't require everyone to include direction.h */
  50. struct bt_df_per_adv_sync_iq_samples_report;
  51. struct bt_le_ext_adv_sent_info {
  52. /** The number of advertising events completed. */
  53. uint8_t num_sent;
  54. };
  55. struct bt_le_ext_adv_connected_info {
  56. /** Connection object of the new connection */
  57. struct bt_conn *conn;
  58. };
  59. struct bt_le_ext_adv_scanned_info {
  60. /** Active scanner LE address and type */
  61. bt_addr_le_t *addr;
  62. };
  63. struct bt_le_ext_adv_cb {
  64. /**
  65. * @brief The advertising set has finished sending adv data.
  66. *
  67. * This callback notifies the application that the advertising set has
  68. * finished sending advertising data.
  69. * The advertising set can either have been stopped by a timeout or
  70. * because the specified number of advertising events has been reached.
  71. *
  72. * @param adv The advertising set object.
  73. * @param info Information about the sent event.
  74. */
  75. void (*sent)(struct bt_le_ext_adv *adv,
  76. struct bt_le_ext_adv_sent_info *info);
  77. /**
  78. * @brief The advertising set has accepted a new connection.
  79. *
  80. * This callback notifies the application that the advertising set has
  81. * accepted a new connection.
  82. *
  83. * @param adv The advertising set object.
  84. * @param info Information about the connected event.
  85. */
  86. void (*connected)(struct bt_le_ext_adv *adv,
  87. struct bt_le_ext_adv_connected_info *info);
  88. /**
  89. * @brief The advertising set has sent scan response data.
  90. *
  91. * This callback notifies the application that the advertising set has
  92. * has received a Scan Request packet, and has sent a Scan Response
  93. * packet.
  94. *
  95. * @param adv The advertising set object.
  96. * @param addr Information about the scanned event.
  97. */
  98. void (*scanned)(struct bt_le_ext_adv *adv,
  99. struct bt_le_ext_adv_scanned_info *info);
  100. };
  101. /**
  102. * @typedef bt_ready_cb_t
  103. * @brief Callback for notifying that Bluetooth has been enabled.
  104. *
  105. * @param err zero on success or (negative) error code otherwise.
  106. */
  107. typedef void (*bt_ready_cb_t)(int err);
  108. /**
  109. * @brief Enable Bluetooth
  110. *
  111. * Enable Bluetooth. Must be the called before any calls that
  112. * require communication with the local Bluetooth hardware.
  113. *
  114. * When @kconfig{CONFIG_BT_SETTINGS} has been enabled and the application is not
  115. * managing identities of the stack itself then the application must call
  116. * @ref settings_load() before the stack is fully enabled.
  117. * See @ref bt_id_create() for more information.
  118. *
  119. * @param cb Callback to notify completion or NULL to perform the
  120. * enabling synchronously.
  121. *
  122. * @return Zero on success or (negative) error code otherwise.
  123. */
  124. int bt_enable(bt_ready_cb_t cb);
  125. /**
  126. * @brief Set Bluetooth Device Name
  127. *
  128. * Set Bluetooth GAP Device Name.
  129. *
  130. * When advertising with device name in the advertising data the name should
  131. * be updated by calling @ref bt_le_adv_update_data or
  132. * @ref bt_le_ext_adv_set_data.
  133. *
  134. * @param name New name
  135. *
  136. * @return Zero on success or (negative) error code otherwise.
  137. */
  138. int bt_set_name(const char *name);
  139. /**
  140. * @brief Get Bluetooth Device Name
  141. *
  142. * Get Bluetooth GAP Device Name.
  143. *
  144. * @return Bluetooth Device Name
  145. */
  146. const char *bt_get_name(void);
  147. /**
  148. * @brief Get the currently configured identities.
  149. *
  150. * Returns an array of the currently configured identity addresses. To
  151. * make sure all available identities can be retrieved, the number of
  152. * elements in the @a addrs array should be CONFIG_BT_ID_MAX. The identity
  153. * identifier that some APIs expect (such as advertising parameters) is
  154. * simply the index of the identity in the @a addrs array.
  155. *
  156. * If @a addrs is passed as NULL, then returned @a count contains the
  157. * count of all available identities that can be retrieved with a
  158. * subsequent call to this function with non-NULL @a addrs parameter.
  159. *
  160. * @note Deleted identities may show up as BT_LE_ADDR_ANY in the returned
  161. * array.
  162. *
  163. * @param addrs Array where to store the configured identities.
  164. * @param count Should be initialized to the array size. Once the function
  165. * returns it will contain the number of returned identities.
  166. */
  167. void bt_id_get(bt_addr_le_t *addrs, size_t *count);
  168. /**
  169. * @brief Create a new identity.
  170. *
  171. * Create a new identity using the given address and IRK. This function
  172. * can be called before calling bt_enable(), in which case it can be used
  173. * to override the controller's public address (in case it has one). However,
  174. * the new identity will only be stored persistently in flash when this API
  175. * is used after bt_enable(). The reason is that the persistent settings
  176. * are loaded after bt_enable() and would therefore cause potential conflicts
  177. * with the stack blindly overwriting what's stored in flash. The identity
  178. * will also not be written to flash in case a pre-defined address is
  179. * provided, since in such a situation the app clearly has some place it got
  180. * the address from and will be able to repeat the procedure on every power
  181. * cycle, i.e. it would be redundant to also store the information in flash.
  182. *
  183. * Generating random static address or random IRK is not supported when calling
  184. * this function before bt_enable().
  185. *
  186. * If the application wants to have the stack randomly generate identities
  187. * and store them in flash for later recovery, the way to do it would be
  188. * to first initialize the stack (using bt_enable), then call settings_load(),
  189. * and after that check with bt_id_get() how many identities were recovered.
  190. * If an insufficient amount of identities were recovered the app may then
  191. * call bt_id_create() to create new ones.
  192. *
  193. * @param addr Address to use for the new identity. If NULL or initialized
  194. * to BT_ADDR_LE_ANY the stack will generate a new random
  195. * static address for the identity and copy it to the given
  196. * parameter upon return from this function (in case the
  197. * parameter was non-NULL).
  198. * @param irk Identity Resolving Key (16 bytes) to be used with this
  199. * identity. If set to all zeroes or NULL, the stack will
  200. * generate a random IRK for the identity and copy it back
  201. * to the parameter upon return from this function (in case
  202. * the parameter was non-NULL). If privacy
  203. * @kconfig{CONFIG_BT_PRIVACY} is not enabled this parameter must
  204. * be NULL.
  205. *
  206. * @return Identity identifier (>= 0) in case of success, or a negative
  207. * error code on failure.
  208. */
  209. int bt_id_create(bt_addr_le_t *addr, uint8_t *irk);
  210. /**
  211. * @brief Reset/reclaim an identity for reuse.
  212. *
  213. * The semantics of the @a addr and @a irk parameters of this function
  214. * are the same as with bt_id_create(). The difference is the first
  215. * @a id parameter that needs to be an existing identity (if it doesn't
  216. * exist this function will return an error). When given an existing
  217. * identity this function will disconnect any connections created using it,
  218. * remove any pairing keys or other data associated with it, and then create
  219. * a new identity in the same slot, based on the @a addr and @a irk
  220. * parameters.
  221. *
  222. * @note the default identity (BT_ID_DEFAULT) cannot be reset, i.e. this
  223. * API will return an error if asked to do that.
  224. *
  225. * @param id Existing identity identifier.
  226. * @param addr Address to use for the new identity. If NULL or initialized
  227. * to BT_ADDR_LE_ANY the stack will generate a new static
  228. * random address for the identity and copy it to the given
  229. * parameter upon return from this function (in case the
  230. * parameter was non-NULL).
  231. * @param irk Identity Resolving Key (16 bytes) to be used with this
  232. * identity. If set to all zeroes or NULL, the stack will
  233. * generate a random IRK for the identity and copy it back
  234. * to the parameter upon return from this function (in case
  235. * the parameter was non-NULL). If privacy
  236. * @kconfig{CONFIG_BT_PRIVACY} is not enabled this parameter must
  237. * be NULL.
  238. *
  239. * @return Identity identifier (>= 0) in case of success, or a negative
  240. * error code on failure.
  241. */
  242. int bt_id_reset(uint8_t id, bt_addr_le_t *addr, uint8_t *irk);
  243. /**
  244. * @brief Delete an identity.
  245. *
  246. * When given a valid identity this function will disconnect any connections
  247. * created using it, remove any pairing keys or other data associated with
  248. * it, and then flag is as deleted, so that it can not be used for any
  249. * operations. To take back into use the slot the identity was occupying the
  250. * bt_id_reset() API needs to be used.
  251. *
  252. * @note the default identity (BT_ID_DEFAULT) cannot be deleted, i.e. this
  253. * API will return an error if asked to do that.
  254. *
  255. * @param id Existing identity identifier.
  256. *
  257. * @return 0 in case of success, or a negative error code on failure.
  258. */
  259. int bt_id_delete(uint8_t id);
  260. /**
  261. * @brief Bluetooth data.
  262. *
  263. * Description of different data types that can be encoded into
  264. * advertising data. Used to form arrays that are passed to the
  265. * bt_le_adv_start() function.
  266. */
  267. struct bt_data {
  268. uint8_t type;
  269. uint8_t data_len;
  270. const uint8_t *data;
  271. };
  272. /**
  273. * @brief Helper to declare elements of bt_data arrays
  274. *
  275. * This macro is mainly for creating an array of struct bt_data
  276. * elements which is then passed to e.g. @ref bt_le_adv_start().
  277. *
  278. * @param _type Type of advertising data field
  279. * @param _data Pointer to the data field payload
  280. * @param _data_len Number of bytes behind the _data pointer
  281. */
  282. #define BT_DATA(_type, _data, _data_len) \
  283. { \
  284. .type = (_type), \
  285. .data_len = (_data_len), \
  286. .data = (const uint8_t *)(_data), \
  287. }
  288. /**
  289. * @brief Helper to declare elements of bt_data arrays
  290. *
  291. * This macro is mainly for creating an array of struct bt_data
  292. * elements which is then passed to e.g. @ref bt_le_adv_start().
  293. *
  294. * @param _type Type of advertising data field
  295. * @param _bytes Variable number of single-byte parameters
  296. */
  297. #define BT_DATA_BYTES(_type, _bytes...) \
  298. BT_DATA(_type, ((uint8_t []) { _bytes }), \
  299. sizeof((uint8_t []) { _bytes }))
  300. /** Advertising options */
  301. enum {
  302. /** Convenience value when no options are specified. */
  303. BT_LE_ADV_OPT_NONE = 0,
  304. /**
  305. * @brief Advertise as connectable.
  306. *
  307. * Advertise as connectable. If not connectable then the type of
  308. * advertising is determined by providing scan response data.
  309. * The advertiser address is determined by the type of advertising
  310. * and/or enabling privacy @kconfig{CONFIG_BT_PRIVACY}.
  311. */
  312. BT_LE_ADV_OPT_CONNECTABLE = BIT(0),
  313. /**
  314. * @brief Advertise one time.
  315. *
  316. * Don't try to resume connectable advertising after a connection.
  317. * This option is only meaningful when used together with
  318. * BT_LE_ADV_OPT_CONNECTABLE. If set the advertising will be stopped
  319. * when bt_le_adv_stop() is called or when an incoming (peripheral)
  320. * connection happens. If this option is not set the stack will
  321. * take care of keeping advertising enabled even as connections
  322. * occur.
  323. * If Advertising directed or the advertiser was started with
  324. * @ref bt_le_ext_adv_start then this behavior is the default behavior
  325. * and this flag has no effect.
  326. */
  327. BT_LE_ADV_OPT_ONE_TIME = BIT(1),
  328. /**
  329. * @brief Advertise using identity address.
  330. *
  331. * Advertise using the identity address as the advertiser address.
  332. * @warning This will compromise the privacy of the device, so care
  333. * must be taken when using this option.
  334. * @note The address used for advertising will not be the same as
  335. * returned by @ref bt_le_oob_get_local, instead @ref bt_id_get
  336. * should be used to get the LE address.
  337. */
  338. BT_LE_ADV_OPT_USE_IDENTITY = BIT(2),
  339. /** Advertise using GAP device name.
  340. *
  341. * Include the GAP device name automatically when advertising.
  342. * By default the GAP device name is put at the end of the scan
  343. * response data.
  344. * When advertising using @ref BT_LE_ADV_OPT_EXT_ADV and not
  345. * @ref BT_LE_ADV_OPT_SCANNABLE then it will be put at the end of the
  346. * advertising data.
  347. * If the GAP device name does not fit into advertising data it will be
  348. * converted to a shortened name if possible.
  349. * @ref BT_LE_ADV_OPT_FORCE_NAME_IN_AD can be used to force the device
  350. * name to appear in the advertising data of an advert with scan
  351. * response data.
  352. *
  353. * The application can set the device name itself by including the
  354. * following in the advertising data.
  355. * @code
  356. * BT_DATA(BT_DATA_NAME_COMPLETE, name, sizeof(name) - 1)
  357. * @endcode
  358. */
  359. BT_LE_ADV_OPT_USE_NAME = BIT(3),
  360. /**
  361. * @brief Low duty cycle directed advertising.
  362. *
  363. * Use low duty directed advertising mode, otherwise high duty mode
  364. * will be used.
  365. */
  366. BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY = BIT(4),
  367. /**
  368. * @brief Directed advertising to privacy-enabled peer.
  369. *
  370. * Enable use of Resolvable Private Address (RPA) as the target address
  371. * in directed advertisements.
  372. * This is required if the remote device is privacy-enabled and
  373. * supports address resolution of the target address in directed
  374. * advertisement.
  375. * It is the responsibility of the application to check that the remote
  376. * device supports address resolution of directed advertisements by
  377. * reading its Central Address Resolution characteristic.
  378. */
  379. BT_LE_ADV_OPT_DIR_ADDR_RPA = BIT(5),
  380. /** Use filter accept list to filter devices that can request scan
  381. * response data.
  382. */
  383. BT_LE_ADV_OPT_FILTER_SCAN_REQ = BIT(6),
  384. /** Use filter accept list to filter devices that can connect. */
  385. BT_LE_ADV_OPT_FILTER_CONN = BIT(7),
  386. /** Notify the application when a scan response data has been sent to an
  387. * active scanner.
  388. */
  389. BT_LE_ADV_OPT_NOTIFY_SCAN_REQ = BIT(8),
  390. /**
  391. * @brief Support scan response data.
  392. *
  393. * When used together with @ref BT_LE_ADV_OPT_EXT_ADV then this option
  394. * cannot be used together with the @ref BT_LE_ADV_OPT_CONNECTABLE
  395. * option.
  396. * When used together with @ref BT_LE_ADV_OPT_EXT_ADV then scan
  397. * response data must be set.
  398. */
  399. BT_LE_ADV_OPT_SCANNABLE = BIT(9),
  400. /**
  401. * @brief Advertise with extended advertising.
  402. *
  403. * This options enables extended advertising in the advertising set.
  404. * In extended advertising the advertising set will send a small header
  405. * packet on the three primary advertising channels. This small header
  406. * points to the advertising data packet that will be sent on one of
  407. * the 37 secondary advertising channels.
  408. * The advertiser will send primary advertising on LE 1M PHY, and
  409. * secondary advertising on LE 2M PHY.
  410. * Connections will be established on LE 2M PHY.
  411. *
  412. * Without this option the advertiser will send advertising data on the
  413. * three primary advertising channels.
  414. *
  415. * @note Enabling this option requires extended advertising support in
  416. * the peer devices scanning for advertisement packets.
  417. */
  418. BT_LE_ADV_OPT_EXT_ADV = BIT(10),
  419. /**
  420. * @brief Disable use of LE 2M PHY on the secondary advertising
  421. * channel.
  422. *
  423. * Disabling the use of LE 2M PHY could be necessary if scanners don't
  424. * support the LE 2M PHY.
  425. * The advertiser will send primary advertising on LE 1M PHY, and
  426. * secondary advertising on LE 1M PHY.
  427. * Connections will be established on LE 1M PHY.
  428. *
  429. * @note Cannot be set if BT_LE_ADV_OPT_CODED is set.
  430. *
  431. * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV.
  432. */
  433. BT_LE_ADV_OPT_NO_2M = BIT(11),
  434. /**
  435. * @brief Advertise on the LE Coded PHY (Long Range).
  436. *
  437. * The advertiser will send both primary and secondary advertising
  438. * on the LE Coded PHY. This gives the advertiser increased range with
  439. * the trade-off of lower data rate and higher power consumption.
  440. * Connections will be established on LE Coded PHY.
  441. *
  442. * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV
  443. */
  444. BT_LE_ADV_OPT_CODED = BIT(12),
  445. /**
  446. * @brief Advertise without a device address (identity or RPA).
  447. *
  448. * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV
  449. */
  450. BT_LE_ADV_OPT_ANONYMOUS = BIT(13),
  451. /**
  452. * @brief Advertise with transmit power.
  453. *
  454. * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV
  455. */
  456. BT_LE_ADV_OPT_USE_TX_POWER = BIT(14),
  457. /** Disable advertising on channel index 37. */
  458. BT_LE_ADV_OPT_DISABLE_CHAN_37 = BIT(15),
  459. /** Disable advertising on channel index 38. */
  460. BT_LE_ADV_OPT_DISABLE_CHAN_38 = BIT(16),
  461. /** Disable advertising on channel index 39. */
  462. BT_LE_ADV_OPT_DISABLE_CHAN_39 = BIT(17),
  463. /**
  464. * @brief Put GAP device name into advert data
  465. *
  466. * Will place the GAP device name into the advertising data rather
  467. * than the scan response data.
  468. *
  469. * @note Requires @ref BT_LE_ADV_OPT_USE_NAME
  470. */
  471. BT_LE_ADV_OPT_FORCE_NAME_IN_AD = BIT(18),
  472. };
  473. /** LE Advertising Parameters. */
  474. struct bt_le_adv_param {
  475. /**
  476. * @brief Local identity.
  477. *
  478. * @note When extended advertising @kconfig{CONFIG_BT_EXT_ADV} is not
  479. * enabled or not supported by the controller it is not possible
  480. * to scan and advertise simultaneously using two different
  481. * random addresses.
  482. */
  483. uint8_t id;
  484. /**
  485. * @brief Advertising Set Identifier, valid range 0x00 - 0x0f.
  486. *
  487. * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV
  488. **/
  489. uint8_t sid;
  490. /**
  491. * @brief Secondary channel maximum skip count.
  492. *
  493. * Maximum advertising events the advertiser can skip before it must
  494. * send advertising data on the secondary advertising channel.
  495. *
  496. * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV
  497. */
  498. uint8_t secondary_max_skip;
  499. /** Bit-field of advertising options */
  500. uint32_t options;
  501. /** Minimum Advertising Interval (N * 0.625 milliseconds)
  502. * Minimum Advertising Interval shall be less than or equal to the
  503. * Maximum Advertising Interval. The Minimum Advertising Interval and
  504. * Maximum Advertising Interval should not be the same value (as stated
  505. * in Bluetooth Core Spec 5.2, section 7.8.5)
  506. * Range: 0x0020 to 0x4000
  507. */
  508. uint32_t interval_min;
  509. /** Maximum Advertising Interval (N * 0.625 milliseconds)
  510. * Minimum Advertising Interval shall be less than or equal to the
  511. * Maximum Advertising Interval. The Minimum Advertising Interval and
  512. * Maximum Advertising Interval should not be the same value (as stated
  513. * in Bluetooth Core Spec 5.2, section 7.8.5)
  514. * Range: 0x0020 to 0x4000
  515. */
  516. uint32_t interval_max;
  517. /**
  518. * @brief Directed advertising to peer
  519. *
  520. * When this parameter is set the advertiser will send directed
  521. * advertising to the remote device.
  522. *
  523. * The advertising type will either be high duty cycle, or low duty
  524. * cycle if the BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY option is enabled.
  525. * When using @ref BT_LE_ADV_OPT_EXT_ADV then only low duty cycle is
  526. * allowed.
  527. *
  528. * In case of connectable high duty cycle if the connection could not
  529. * be established within the timeout the connected() callback will be
  530. * called with the status set to @ref BT_HCI_ERR_ADV_TIMEOUT.
  531. */
  532. const bt_addr_le_t *peer;
  533. };
  534. /** Periodic Advertising options */
  535. enum {
  536. /** Convenience value when no options are specified. */
  537. BT_LE_PER_ADV_OPT_NONE = 0,
  538. /**
  539. * @brief Advertise with transmit power.
  540. *
  541. * @note Requires @ref BT_LE_ADV_OPT_EXT_ADV
  542. */
  543. BT_LE_PER_ADV_OPT_USE_TX_POWER = BIT(1),
  544. };
  545. struct bt_le_per_adv_param {
  546. /**
  547. * @brief Minimum Periodic Advertising Interval (N * 1.25 ms)
  548. *
  549. * Shall be greater or equal to BT_GAP_PER_ADV_MIN_INTERVAL and
  550. * less or equal to interval_max.
  551. */
  552. uint16_t interval_min;
  553. /**
  554. * @brief Maximum Periodic Advertising Interval (N * 1.25 ms)
  555. *
  556. * Shall be less or equal to BT_GAP_PER_ADV_MAX_INTERVAL and
  557. * greater or equal to interval_min.
  558. */
  559. uint16_t interval_max;
  560. /** Bit-field of periodic advertising options */
  561. uint32_t options;
  562. };
  563. /**
  564. * @brief Initialize advertising parameters
  565. *
  566. * @param _options Advertising Options
  567. * @param _int_min Minimum advertising interval
  568. * @param _int_max Maximum advertising interval
  569. * @param _peer Peer address, set to NULL for undirected advertising or
  570. * address of peer for directed advertising.
  571. */
  572. #define BT_LE_ADV_PARAM_INIT(_options, _int_min, _int_max, _peer) \
  573. { \
  574. .id = BT_ID_DEFAULT, \
  575. .sid = 0, \
  576. .secondary_max_skip = 0, \
  577. .options = (_options), \
  578. .interval_min = (_int_min), \
  579. .interval_max = (_int_max), \
  580. .peer = (_peer), \
  581. }
  582. /**
  583. * @brief Helper to declare advertising parameters inline
  584. *
  585. * @param _options Advertising Options
  586. * @param _int_min Minimum advertising interval
  587. * @param _int_max Maximum advertising interval
  588. * @param _peer Peer address, set to NULL for undirected advertising or
  589. * address of peer for directed advertising.
  590. */
  591. #define BT_LE_ADV_PARAM(_options, _int_min, _int_max, _peer) \
  592. ((struct bt_le_adv_param[]) { \
  593. BT_LE_ADV_PARAM_INIT(_options, _int_min, _int_max, _peer) \
  594. })
  595. #define BT_LE_ADV_CONN_DIR(_peer) BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \
  596. BT_LE_ADV_OPT_ONE_TIME, 0, 0,\
  597. _peer)
  598. #define BT_LE_ADV_CONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE, \
  599. BT_GAP_ADV_FAST_INT_MIN_2, \
  600. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  601. #define BT_LE_ADV_CONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \
  602. BT_LE_ADV_OPT_USE_NAME, \
  603. BT_GAP_ADV_FAST_INT_MIN_2, \
  604. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  605. #define BT_LE_ADV_CONN_NAME_AD BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | \
  606. BT_LE_ADV_OPT_USE_NAME | \
  607. BT_LE_ADV_OPT_FORCE_NAME_IN_AD, \
  608. BT_GAP_ADV_FAST_INT_MIN_2, \
  609. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  610. #define BT_LE_ADV_CONN_DIR_LOW_DUTY(_peer) \
  611. BT_LE_ADV_PARAM(BT_LE_ADV_OPT_CONNECTABLE | BT_LE_ADV_OPT_ONE_TIME | \
  612. BT_LE_ADV_OPT_DIR_MODE_LOW_DUTY, \
  613. BT_GAP_ADV_FAST_INT_MIN_2, BT_GAP_ADV_FAST_INT_MAX_2, \
  614. _peer)
  615. /** Non-connectable advertising with private address */
  616. #define BT_LE_ADV_NCONN BT_LE_ADV_PARAM(0, BT_GAP_ADV_FAST_INT_MIN_2, \
  617. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  618. /** Non-connectable advertising with @ref BT_LE_ADV_OPT_USE_NAME */
  619. #define BT_LE_ADV_NCONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_NAME, \
  620. BT_GAP_ADV_FAST_INT_MIN_2, \
  621. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  622. /** Non-connectable advertising with @ref BT_LE_ADV_OPT_USE_IDENTITY */
  623. #define BT_LE_ADV_NCONN_IDENTITY BT_LE_ADV_PARAM(BT_LE_ADV_OPT_USE_IDENTITY, \
  624. BT_GAP_ADV_FAST_INT_MIN_2, \
  625. BT_GAP_ADV_FAST_INT_MAX_2, \
  626. NULL)
  627. /** Connectable extended advertising with @ref BT_LE_ADV_OPT_USE_NAME */
  628. #define BT_LE_EXT_ADV_CONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \
  629. BT_LE_ADV_OPT_CONNECTABLE | \
  630. BT_LE_ADV_OPT_USE_NAME, \
  631. BT_GAP_ADV_FAST_INT_MIN_2, \
  632. BT_GAP_ADV_FAST_INT_MAX_2, \
  633. NULL)
  634. /** Scannable extended advertising with @ref BT_LE_ADV_OPT_USE_NAME */
  635. #define BT_LE_EXT_ADV_SCAN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \
  636. BT_LE_ADV_OPT_SCANNABLE | \
  637. BT_LE_ADV_OPT_USE_NAME, \
  638. BT_GAP_ADV_FAST_INT_MIN_2, \
  639. BT_GAP_ADV_FAST_INT_MAX_2, \
  640. NULL)
  641. /** Non-connectable extended advertising with private address */
  642. #define BT_LE_EXT_ADV_NCONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV, \
  643. BT_GAP_ADV_FAST_INT_MIN_2, \
  644. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  645. /** Non-connectable extended advertising with @ref BT_LE_ADV_OPT_USE_NAME */
  646. #define BT_LE_EXT_ADV_NCONN_NAME BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \
  647. BT_LE_ADV_OPT_USE_NAME, \
  648. BT_GAP_ADV_FAST_INT_MIN_2, \
  649. BT_GAP_ADV_FAST_INT_MAX_2, \
  650. NULL)
  651. /** Non-connectable extended advertising with @ref BT_LE_ADV_OPT_USE_IDENTITY */
  652. #define BT_LE_EXT_ADV_NCONN_IDENTITY \
  653. BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \
  654. BT_LE_ADV_OPT_USE_IDENTITY, \
  655. BT_GAP_ADV_FAST_INT_MIN_2, \
  656. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  657. /** Non-connectable extended advertising on coded PHY with private address */
  658. #define BT_LE_EXT_ADV_CODED_NCONN BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | \
  659. BT_LE_ADV_OPT_CODED, \
  660. BT_GAP_ADV_FAST_INT_MIN_2, \
  661. BT_GAP_ADV_FAST_INT_MAX_2, \
  662. NULL)
  663. /** Non-connectable extended advertising on coded PHY with
  664. * @ref BT_LE_ADV_OPT_USE_NAME
  665. */
  666. #define BT_LE_EXT_ADV_CODED_NCONN_NAME \
  667. BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED | \
  668. BT_LE_ADV_OPT_USE_NAME, \
  669. BT_GAP_ADV_FAST_INT_MIN_2, \
  670. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  671. /** Non-connectable extended advertising on coded PHY with
  672. * @ref BT_LE_ADV_OPT_USE_IDENTITY
  673. */
  674. #define BT_LE_EXT_ADV_CODED_NCONN_IDENTITY \
  675. BT_LE_ADV_PARAM(BT_LE_ADV_OPT_EXT_ADV | BT_LE_ADV_OPT_CODED | \
  676. BT_LE_ADV_OPT_USE_IDENTITY, \
  677. BT_GAP_ADV_FAST_INT_MIN_2, \
  678. BT_GAP_ADV_FAST_INT_MAX_2, NULL)
  679. /**
  680. * Helper to initialize extended advertising start parameters inline
  681. *
  682. * @param _timeout Advertiser timeout
  683. * @param _n_evts Number of advertising events
  684. */
  685. #define BT_LE_EXT_ADV_START_PARAM_INIT(_timeout, _n_evts) \
  686. { \
  687. .timeout = (_timeout), \
  688. .num_events = (_n_evts), \
  689. }
  690. /**
  691. * Helper to declare extended advertising start parameters inline
  692. *
  693. * @param _timeout Advertiser timeout
  694. * @param _n_evts Number of advertising events
  695. */
  696. #define BT_LE_EXT_ADV_START_PARAM(_timeout, _n_evts) \
  697. ((struct bt_le_ext_adv_start_param[]) { \
  698. BT_LE_EXT_ADV_START_PARAM_INIT((_timeout), (_n_evts)) \
  699. })
  700. #define BT_LE_EXT_ADV_START_DEFAULT BT_LE_EXT_ADV_START_PARAM(0, 0)
  701. /**
  702. * Helper to declare periodic advertising parameters inline
  703. *
  704. * @param _int_min Minimum periodic advertising interval
  705. * @param _int_max Maximum periodic advertising interval
  706. * @param _options Periodic advertising properties bitfield.
  707. */
  708. #define BT_LE_PER_ADV_PARAM_INIT(_int_min, _int_max, _options) \
  709. { \
  710. .interval_min = (_int_min), \
  711. .interval_max = (_int_max), \
  712. .options = (_options), \
  713. }
  714. /**
  715. * Helper to declare periodic advertising parameters inline
  716. *
  717. * @param _int_min Minimum periodic advertising interval
  718. * @param _int_max Maximum periodic advertising interval
  719. * @param _options Periodic advertising properties bitfield.
  720. */
  721. #define BT_LE_PER_ADV_PARAM(_int_min, _int_max, _options) \
  722. ((struct bt_le_per_adv_param[]) { \
  723. BT_LE_PER_ADV_PARAM_INIT(_int_min, _int_max, _options) \
  724. })
  725. #define BT_LE_PER_ADV_DEFAULT BT_LE_PER_ADV_PARAM(BT_GAP_PER_ADV_SLOW_INT_MIN, \
  726. BT_GAP_PER_ADV_SLOW_INT_MAX, \
  727. BT_LE_PER_ADV_OPT_NONE)
  728. /**
  729. * @brief Start advertising
  730. *
  731. * Set advertisement data, scan response data, advertisement parameters
  732. * and start advertising.
  733. *
  734. * When the advertisement parameter peer address has been set the advertising
  735. * will be directed to the peer. In this case advertisement data and scan
  736. * response data parameters are ignored. If the mode is high duty cycle
  737. * the timeout will be @ref BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT.
  738. *
  739. * @param param Advertising parameters.
  740. * @param ad Data to be used in advertisement packets.
  741. * @param ad_len Number of elements in ad
  742. * @param sd Data to be used in scan response packets.
  743. * @param sd_len Number of elements in sd
  744. *
  745. * @return Zero on success or (negative) error code otherwise.
  746. * @return -ENOMEM No free connection objects available for connectable
  747. * advertiser.
  748. * @return -ECONNREFUSED When connectable advertising is requested and there
  749. * is already maximum number of connections established
  750. * in the controller.
  751. * This error code is only guaranteed when using Zephyr
  752. * controller, for other controllers code returned in
  753. * this case may be -EIO.
  754. */
  755. int bt_le_adv_start(const struct bt_le_adv_param *param,
  756. const struct bt_data *ad, size_t ad_len,
  757. const struct bt_data *sd, size_t sd_len);
  758. /**
  759. * @brief Update advertising
  760. *
  761. * Update advertisement and scan response data.
  762. *
  763. * @param ad Data to be used in advertisement packets.
  764. * @param ad_len Number of elements in ad
  765. * @param sd Data to be used in scan response packets.
  766. * @param sd_len Number of elements in sd
  767. *
  768. * @return Zero on success or (negative) error code otherwise.
  769. */
  770. int bt_le_adv_update_data(const struct bt_data *ad, size_t ad_len,
  771. const struct bt_data *sd, size_t sd_len);
  772. /**
  773. * @brief Stop advertising
  774. *
  775. * Stops ongoing advertising.
  776. *
  777. * @return Zero on success or (negative) error code otherwise.
  778. */
  779. int bt_le_adv_stop(void);
  780. /**
  781. * @brief Create advertising set.
  782. *
  783. * Create a new advertising set and set advertising parameters.
  784. * Advertising parameters can be updated with @ref bt_le_ext_adv_update_param.
  785. *
  786. * @param[in] param Advertising parameters.
  787. * @param[in] cb Callback struct to notify about advertiser activity. Can be
  788. * NULL. Must point to valid memory during the lifetime of the
  789. * advertising set.
  790. * @param[out] adv Valid advertising set object on success.
  791. *
  792. * @return Zero on success or (negative) error code otherwise.
  793. */
  794. int bt_le_ext_adv_create(const struct bt_le_adv_param *param,
  795. const struct bt_le_ext_adv_cb *cb,
  796. struct bt_le_ext_adv **adv);
  797. struct bt_le_ext_adv_start_param {
  798. /**
  799. * @brief Advertiser timeout (N * 10 ms).
  800. *
  801. * Application will be notified by the advertiser sent callback.
  802. * Set to zero for no timeout.
  803. *
  804. * When using high duty cycle directed connectable advertising then
  805. * this parameters must be set to a non-zero value less than or equal
  806. * to the maximum of @ref BT_GAP_ADV_HIGH_DUTY_CYCLE_MAX_TIMEOUT.
  807. *
  808. * If privacy @kconfig{CONFIG_BT_PRIVACY} is enabled then the timeout
  809. * must be less than @kconfig{CONFIG_BT_RPA_TIMEOUT}.
  810. */
  811. uint16_t timeout;
  812. /**
  813. * @brief Number of advertising events.
  814. *
  815. * Application will be notified by the advertiser sent callback.
  816. * Set to zero for no limit.
  817. */
  818. uint8_t num_events;
  819. };
  820. /**
  821. * @brief Start advertising with the given advertising set
  822. *
  823. * If the advertiser is limited by either the timeout or number of advertising
  824. * events the application will be notified by the advertiser sent callback once
  825. * the limit is reached.
  826. * If the advertiser is limited by both the timeout and the number of
  827. * advertising events then the limit that is reached first will stop the
  828. * advertiser.
  829. *
  830. * @param adv Advertising set object.
  831. * @param param Advertise start parameters.
  832. */
  833. int bt_le_ext_adv_start(struct bt_le_ext_adv *adv,
  834. struct bt_le_ext_adv_start_param *param);
  835. /**
  836. * @brief Stop advertising with the given advertising set
  837. *
  838. * Stop advertising with a specific advertising set. When using this function
  839. * the advertising sent callback will not be called.
  840. *
  841. * @param adv Advertising set object.
  842. *
  843. * @return Zero on success or (negative) error code otherwise.
  844. */
  845. int bt_le_ext_adv_stop(struct bt_le_ext_adv *adv);
  846. /**
  847. * @brief Set an advertising set's advertising or scan response data.
  848. *
  849. * Set advertisement data or scan response data. If the advertising set is
  850. * currently advertising then the advertising data will be updated in
  851. * subsequent advertising events.
  852. *
  853. * When both @ref BT_LE_ADV_OPT_EXT_ADV and @ref BT_LE_ADV_OPT_SCANNABLE are
  854. * enabled then advertising data is ignored.
  855. * When @ref BT_LE_ADV_OPT_SCANNABLE is not enabled then scan response data is
  856. * ignored.
  857. *
  858. * If the advertising set has been configured to send advertising data on the
  859. * primary advertising channels then the maximum data length is
  860. * @ref BT_GAP_ADV_MAX_ADV_DATA_LEN bytes.
  861. * If the advertising set has been configured for extended advertising,
  862. * then the maximum data length is defined by the controller with the maximum
  863. * possible of @ref BT_GAP_ADV_MAX_EXT_ADV_DATA_LEN bytes.
  864. *
  865. * @note Not all scanners support extended data length advertising data.
  866. *
  867. * @note When updating the advertising data while advertising the advertising
  868. * data and scan response data length must be smaller or equal to what
  869. * can be fit in a single advertising packet. Otherwise the
  870. * advertiser must be stopped.
  871. *
  872. * @param adv Advertising set object.
  873. * @param ad Data to be used in advertisement packets.
  874. * @param ad_len Number of elements in ad
  875. * @param sd Data to be used in scan response packets.
  876. * @param sd_len Number of elements in sd
  877. *
  878. * @return Zero on success or (negative) error code otherwise.
  879. */
  880. int bt_le_ext_adv_set_data(struct bt_le_ext_adv *adv,
  881. const struct bt_data *ad, size_t ad_len,
  882. const struct bt_data *sd, size_t sd_len);
  883. /**
  884. * @brief Update advertising parameters.
  885. *
  886. * Update the advertising parameters. The function will return an error if the
  887. * advertiser set is currently advertising. Stop the advertising set before
  888. * calling this function.
  889. *
  890. * @note When changing the option @ref BT_LE_ADV_OPT_USE_NAME then
  891. * @ref bt_le_ext_adv_set_data needs to be called in order to update the
  892. * advertising data and scan response data.
  893. *
  894. * @param adv Advertising set object.
  895. * @param param Advertising parameters.
  896. *
  897. * @return Zero on success or (negative) error code otherwise.
  898. */
  899. int bt_le_ext_adv_update_param(struct bt_le_ext_adv *adv,
  900. const struct bt_le_adv_param *param);
  901. /**
  902. * @brief Delete advertising set.
  903. *
  904. * Delete advertising set. This will free up the advertising set and make it
  905. * possible to create a new advertising set.
  906. *
  907. * @return Zero on success or (negative) error code otherwise.
  908. */
  909. int bt_le_ext_adv_delete(struct bt_le_ext_adv *adv);
  910. /**
  911. * @brief Get array index of an advertising set.
  912. *
  913. * This function is used to map bt_adv to index of an array of
  914. * advertising sets. The array has CONFIG_BT_EXT_ADV_MAX_ADV_SET elements.
  915. *
  916. * @param adv Advertising set.
  917. *
  918. * @return Index of the advertising set object.
  919. * The range of the returned value is 0..CONFIG_BT_EXT_ADV_MAX_ADV_SET-1
  920. */
  921. uint8_t bt_le_ext_adv_get_index(struct bt_le_ext_adv *adv);
  922. /** @brief Advertising set info structure. */
  923. struct bt_le_ext_adv_info {
  924. /* Local identity */
  925. uint8_t id;
  926. /** Currently selected Transmit Power (dBM). */
  927. int8_t tx_power;
  928. };
  929. /**
  930. * @brief Get advertising set info
  931. *
  932. * @param adv Advertising set object
  933. * @param info Advertising set info object
  934. *
  935. * @return Zero on success or (negative) error code on failure.
  936. */
  937. int bt_le_ext_adv_get_info(const struct bt_le_ext_adv *adv,
  938. struct bt_le_ext_adv_info *info);
  939. /**
  940. * @typedef bt_le_scan_cb_t
  941. * @brief Callback type for reporting LE scan results.
  942. *
  943. * A function of this type is given to the bt_le_scan_start() function
  944. * and will be called for any discovered LE device.
  945. *
  946. * @param addr Advertiser LE address and type.
  947. * @param rssi Strength of advertiser signal.
  948. * @param adv_type Type of advertising response from advertiser.
  949. * @param buf Buffer containing advertiser data.
  950. */
  951. typedef void bt_le_scan_cb_t(const bt_addr_le_t *addr, int8_t rssi,
  952. uint8_t adv_type, struct net_buf_simple *buf);
  953. /**
  954. * @brief Set or update the periodic advertising parameters.
  955. *
  956. * The periodic advertising parameters can only be set or updated on an
  957. * extended advertisement set which is neither scannable, connectable nor
  958. * anonymous.
  959. *
  960. * @param adv Advertising set object.
  961. * @param param Advertising parameters.
  962. *
  963. * @return Zero on success or (negative) error code otherwise.
  964. */
  965. int bt_le_per_adv_set_param(struct bt_le_ext_adv *adv,
  966. const struct bt_le_per_adv_param *param);
  967. /**
  968. * @brief Set or update the periodic advertising data.
  969. *
  970. * The periodic advertisement data can only be set or updated on an
  971. * extended advertisement set which is neither scannable, connectable nor
  972. * anonymous.
  973. *
  974. * @param adv Advertising set object.
  975. * @param ad Advertising data.
  976. * @param ad_len Advertising data length.
  977. *
  978. * @return Zero on success or (negative) error code otherwise.
  979. */
  980. int bt_le_per_adv_set_data(const struct bt_le_ext_adv *adv,
  981. const struct bt_data *ad, size_t ad_len);
  982. /**
  983. * @brief Starts periodic advertising.
  984. *
  985. * Enabling the periodic advertising can be done independently of extended
  986. * advertising, but both periodic advertising and extended advertising
  987. * shall be enabled before any periodic advertising data is sent. The
  988. * periodic advertising and extended advertising can be enabled in any order.
  989. *
  990. * Once periodic advertising has been enabled, it will continue advertising
  991. * until @ref bt_le_per_adv_stop() has been called, or if the advertising set
  992. * is deleted by @ref bt_le_ext_adv_delete(). Calling @ref bt_le_ext_adv_stop()
  993. * will not stop the periodic advertising.
  994. *
  995. * @param adv Advertising set object.
  996. *
  997. * @return Zero on success or (negative) error code otherwise.
  998. */
  999. int bt_le_per_adv_start(struct bt_le_ext_adv *adv);
  1000. /**
  1001. * @brief Stops periodic advertising.
  1002. *
  1003. * Disabling the periodic advertising can be done independently of extended
  1004. * advertising. Disabling periodic advertising will not disable extended
  1005. * advertising.
  1006. *
  1007. * @param adv Advertising set object.
  1008. *
  1009. * @return Zero on success or (negative) error code otherwise.
  1010. */
  1011. int bt_le_per_adv_stop(struct bt_le_ext_adv *adv);
  1012. struct bt_le_per_adv_sync_synced_info {
  1013. /** Advertiser LE address and type. */
  1014. const bt_addr_le_t *addr;
  1015. /** Advertiser SID */
  1016. uint8_t sid;
  1017. /** Periodic advertising interval (N * 1.25 ms) */
  1018. uint16_t interval;
  1019. /** Advertiser PHY */
  1020. uint8_t phy;
  1021. /** True if receiving periodic advertisements, false otherwise. */
  1022. bool recv_enabled;
  1023. /**
  1024. * @brief Service Data provided by the peer when sync is transferred
  1025. *
  1026. * Will always be 0 when the sync is locally created.
  1027. */
  1028. uint16_t service_data;
  1029. /**
  1030. * @brief Peer that transferred the periodic advertising sync
  1031. *
  1032. * Will always be 0 when the sync is locally created.
  1033. *
  1034. */
  1035. struct bt_conn *conn;
  1036. };
  1037. struct bt_le_per_adv_sync_term_info {
  1038. /** Advertiser LE address and type. */
  1039. const bt_addr_le_t *addr;
  1040. /** Advertiser SID */
  1041. uint8_t sid;
  1042. };
  1043. struct bt_le_per_adv_sync_recv_info {
  1044. /** Advertiser LE address and type. */
  1045. const bt_addr_le_t *addr;
  1046. /** Advertiser SID */
  1047. uint8_t sid;
  1048. /** The TX power of the advertisement. */
  1049. int8_t tx_power;
  1050. /** The RSSI of the advertisement excluding any CTE. */
  1051. int8_t rssi;
  1052. /** The Constant Tone Extension (CTE) of the advertisement (@ref bt_df_cte_type) */
  1053. uint8_t cte_type;
  1054. };
  1055. struct bt_le_per_adv_sync_state_info {
  1056. /** True if receiving periodic advertisements, false otherwise. */
  1057. bool recv_enabled;
  1058. };
  1059. struct bt_le_per_adv_sync_cb {
  1060. /**
  1061. * @brief The periodic advertising has been successfully synced.
  1062. *
  1063. * This callback notifies the application that the periodic advertising
  1064. * set has been successfully synced, and will now start to
  1065. * receive periodic advertising reports.
  1066. *
  1067. * @param sync The periodic advertising sync object.
  1068. * @param info Information about the sync event.
  1069. */
  1070. void (*synced)(struct bt_le_per_adv_sync *sync,
  1071. struct bt_le_per_adv_sync_synced_info *info);
  1072. /**
  1073. * @brief The periodic advertising sync has been terminated.
  1074. *
  1075. * This callback notifies the application that the periodic advertising
  1076. * sync has been terminated, either by local request, remote request or
  1077. * because due to missing data, e.g. by being out of range or sync.
  1078. *
  1079. * @param sync The periodic advertising sync object.
  1080. */
  1081. void (*term)(struct bt_le_per_adv_sync *sync,
  1082. const struct bt_le_per_adv_sync_term_info *info);
  1083. /**
  1084. * @brief Periodic advertising data received.
  1085. *
  1086. * This callback notifies the application of an periodic advertising
  1087. * report.
  1088. *
  1089. * @param sync The advertising set object.
  1090. * @param info Information about the periodic advertising event.
  1091. * @param buf Buffer containing the periodic advertising data.
  1092. */
  1093. void (*recv)(struct bt_le_per_adv_sync *sync,
  1094. const struct bt_le_per_adv_sync_recv_info *info,
  1095. struct net_buf_simple *buf);
  1096. /**
  1097. * @brief The periodic advertising sync state has changed.
  1098. *
  1099. * This callback notifies the application about changes to the sync
  1100. * state. Initialize sync and termination is handled by their individual
  1101. * callbacks, and won't be notified here.
  1102. *
  1103. * @param sync The periodic advertising sync object.
  1104. * @param info Information about the state change.
  1105. */
  1106. void (*state_changed)(struct bt_le_per_adv_sync *sync,
  1107. const struct bt_le_per_adv_sync_state_info *info);
  1108. /**
  1109. * @brief BIGInfo advertising report received.
  1110. *
  1111. * This callback notifies the application of a BIGInfo advertising report.
  1112. * This is received if the advertiser is broadcasting isochronous streams in a BIG.
  1113. * See iso.h for more information.
  1114. *
  1115. * @param sync The advertising set object.
  1116. * @param biginfo The BIGInfo report.
  1117. */
  1118. void (*biginfo)(struct bt_le_per_adv_sync *sync, const struct bt_iso_biginfo *biginfo);
  1119. /**
  1120. * @brief Callback for IQ samples report collected when sampling
  1121. * CTE received with periodic advertising PDU.
  1122. *
  1123. * @param sync The periodic advertising sync object.
  1124. * @param info Information about the sync event.
  1125. */
  1126. void (*cte_report_cb)(struct bt_le_per_adv_sync *sync,
  1127. struct bt_df_per_adv_sync_iq_samples_report const *info);
  1128. sys_snode_t node;
  1129. };
  1130. /** Periodic advertising sync options */
  1131. enum {
  1132. /** Convenience value when no options are specified. */
  1133. BT_LE_PER_ADV_SYNC_OPT_NONE = 0,
  1134. /**
  1135. * @brief Use the periodic advertising list to sync with advertiser
  1136. *
  1137. * When this option is set, the address and SID of the parameters
  1138. * are ignored.
  1139. */
  1140. BT_LE_PER_ADV_SYNC_OPT_USE_PER_ADV_LIST = BIT(0),
  1141. /**
  1142. * @brief Disables periodic advertising reports
  1143. *
  1144. * No advertisement reports will be handled until enabled.
  1145. */
  1146. BT_LE_PER_ADV_SYNC_OPT_REPORTING_INITIALLY_DISABLED = BIT(1),
  1147. /** Sync with Angle of Arrival (AoA) constant tone extension */
  1148. BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOA = BIT(2),
  1149. /** Sync with Angle of Departure (AoD) 1 us constant tone extension */
  1150. BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_1US = BIT(3),
  1151. /** Sync with Angle of Departure (AoD) 2 us constant tone extension */
  1152. BT_LE_PER_ADV_SYNC_OPT_DONT_SYNC_AOD_2US = BIT(4),
  1153. /** Do not sync to packets without a constant tone extension */
  1154. BT_LE_PER_ADV_SYNC_OPT_SYNC_ONLY_CONST_TONE_EXT = BIT(5),
  1155. };
  1156. struct bt_le_per_adv_sync_param {
  1157. /**
  1158. * @brief Periodic Advertiser Address
  1159. *
  1160. * Only valid if not using the periodic advertising list
  1161. */
  1162. bt_addr_le_t addr;
  1163. /**
  1164. * @brief Advertiser SID
  1165. *
  1166. * Only valid if not using the periodic advertising list
  1167. */
  1168. uint8_t sid;
  1169. /** Bit-field of periodic advertising sync options. */
  1170. uint32_t options;
  1171. /**
  1172. * @brief Maximum event skip
  1173. *
  1174. * Maximum number of periodic advertising events that can be
  1175. * skipped after a successful receive
  1176. */
  1177. uint16_t skip;
  1178. /**
  1179. * @brief Synchronization timeout (N * 10 ms)
  1180. *
  1181. * Synchronization timeout for the periodic advertising sync.
  1182. * Range 0x000A to 0x4000 (100 ms to 163840 ms)
  1183. */
  1184. uint16_t timeout;
  1185. };
  1186. /**
  1187. * @brief Get array index of an periodic advertising sync object.
  1188. *
  1189. * This function is get the index of an array of periodic advertising sync
  1190. * objects. The array has CONFIG_BT_PER_ADV_SYNC_MAX elements.
  1191. *
  1192. * @param per_adv_sync The periodic advertising sync object.
  1193. *
  1194. * @return Index of the periodic advertising sync object.
  1195. * The range of the returned value is 0..CONFIG_BT_PER_ADV_SYNC_MAX-1
  1196. */
  1197. uint8_t bt_le_per_adv_sync_get_index(struct bt_le_per_adv_sync *per_adv_sync);
  1198. /** @brief Advertising set info structure. */
  1199. struct bt_le_per_adv_sync_info {
  1200. /** Periodic Advertiser Address */
  1201. bt_addr_le_t addr;
  1202. /** Advertiser SID */
  1203. uint8_t sid;
  1204. /** Periodic advertising interval (N * 1.25 ms) */
  1205. uint16_t interval;
  1206. /** Advertiser PHY */
  1207. uint8_t phy;
  1208. };
  1209. /**
  1210. * @brief Get periodic adv sync information.
  1211. *
  1212. * @param per_adv_sync Periodic advertising sync object.
  1213. * @param info Periodic advertising sync info object
  1214. *
  1215. * @return Zero on success or (negative) error code on failure.
  1216. */
  1217. int bt_le_per_adv_sync_get_info(struct bt_le_per_adv_sync *per_adv_sync,
  1218. struct bt_le_per_adv_sync_info *info);
  1219. /**
  1220. * @brief Look up an existing periodic advertising sync object by advertiser address.
  1221. *
  1222. * @param adv_addr Advertiser address.
  1223. * @param sid The advertising set ID.
  1224. *
  1225. * @return Periodic advertising sync object or NULL if not found.
  1226. */
  1227. struct bt_le_per_adv_sync *bt_le_per_adv_sync_lookup_addr(const bt_addr_le_t *adv_addr,
  1228. uint8_t sid);
  1229. /**
  1230. * @brief Create a periodic advertising sync object.
  1231. *
  1232. * Create a periodic advertising sync object that can try to synchronize
  1233. * to periodic advertising reports from an advertiser. Scan shall either be
  1234. * disabled or extended scan shall be enabled.
  1235. *
  1236. * @param[in] param Periodic advertising sync parameters.
  1237. * @param[out] out_sync Periodic advertising sync object on.
  1238. *
  1239. * @return Zero on success or (negative) error code otherwise.
  1240. */
  1241. int bt_le_per_adv_sync_create(const struct bt_le_per_adv_sync_param *param,
  1242. struct bt_le_per_adv_sync **out_sync);
  1243. /**
  1244. * @brief Delete periodic advertising sync.
  1245. *
  1246. * Delete the periodic advertising sync object. Can be called regardless of the
  1247. * state of the sync. If the syncing is currently syncing, the syncing is
  1248. * cancelled. If the sync has been established, it is terminated. The
  1249. * periodic advertising sync object will be invalidated afterwards.
  1250. *
  1251. * If the state of the sync object is syncing, then a new periodic advertising
  1252. * sync object may not be created until the controller has finished canceling
  1253. * this object.
  1254. *
  1255. * @param per_adv_sync The periodic advertising sync object.
  1256. *
  1257. * @return Zero on success or (negative) error code otherwise.
  1258. */
  1259. int bt_le_per_adv_sync_delete(struct bt_le_per_adv_sync *per_adv_sync);
  1260. /**
  1261. * @brief Register periodic advertising sync callbacks.
  1262. *
  1263. * Adds the callback structure to the list of callback structures for periodic
  1264. * adverising syncs.
  1265. *
  1266. * This callback will be called for all periodic advertising sync activity,
  1267. * such as synced, terminated and when data is received.
  1268. *
  1269. * @param cb Callback struct. Must point to memory that remains valid.
  1270. */
  1271. void bt_le_per_adv_sync_cb_register(struct bt_le_per_adv_sync_cb *cb);
  1272. /**
  1273. * @brief Enables receiving periodic advertising reports for a sync.
  1274. *
  1275. * If the sync is already receiving the reports, -EALREADY is returned.
  1276. *
  1277. * @param per_adv_sync The periodic advertising sync object.
  1278. *
  1279. * @return Zero on success or (negative) error code otherwise.
  1280. */
  1281. int bt_le_per_adv_sync_recv_enable(struct bt_le_per_adv_sync *per_adv_sync);
  1282. /**
  1283. * @brief Disables receiving periodic advertising reports for a sync.
  1284. *
  1285. * If the sync report receiving is already disabled, -EALREADY is returned.
  1286. *
  1287. * @param per_adv_sync The periodic advertising sync object.
  1288. *
  1289. * @return Zero on success or (negative) error code otherwise.
  1290. */
  1291. int bt_le_per_adv_sync_recv_disable(struct bt_le_per_adv_sync *per_adv_sync);
  1292. /** Periodic Advertising Sync Transfer options */
  1293. enum {
  1294. /** Convenience value when no options are specified. */
  1295. BT_LE_PER_ADV_SYNC_TRANSFER_OPT_NONE = 0,
  1296. /**
  1297. * @brief No Angle of Arrival (AoA)
  1298. *
  1299. * Do not sync with Angle of Arrival (AoA) constant tone extension
  1300. **/
  1301. BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOA = BIT(0),
  1302. /**
  1303. * @brief No Angle of Departure (AoD) 1 us
  1304. *
  1305. * Do not sync with Angle of Departure (AoD) 1 us
  1306. * constant tone extension
  1307. */
  1308. BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_1US = BIT(1),
  1309. /**
  1310. * @brief No Angle of Departure (AoD) 2
  1311. *
  1312. * Do not sync with Angle of Departure (AoD) 2 us
  1313. * constant tone extension
  1314. */
  1315. BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_NO_AOD_2US = BIT(2),
  1316. /** Only sync to packets with constant tone extension */
  1317. BT_LE_PER_ADV_SYNC_TRANSFER_OPT_SYNC_ONLY_CTE = BIT(3),
  1318. };
  1319. struct bt_le_per_adv_sync_transfer_param {
  1320. /**
  1321. * @brief Maximum event skip
  1322. *
  1323. * The number of periodic advertising packets that can be skipped
  1324. * after a successful receive.
  1325. */
  1326. uint16_t skip;
  1327. /**
  1328. * @brief Synchronization timeout (N * 10 ms)
  1329. *
  1330. * Synchronization timeout for the periodic advertising sync.
  1331. * Range 0x000A to 0x4000 (100 ms to 163840 ms)
  1332. */
  1333. uint16_t timeout;
  1334. /** Periodic Advertising Sync Transfer options */
  1335. uint32_t options;
  1336. };
  1337. /**
  1338. * @brief Transfer the periodic advertising sync information to a peer device.
  1339. *
  1340. * This will allow another device to quickly synchronize to the same periodic
  1341. * advertising train that this device is currently synced to.
  1342. *
  1343. * @param per_adv_sync The periodic advertising sync to transfer.
  1344. * @param conn The peer device that will receive the sync information.
  1345. * @param service_data Application service data provided to the remote host.
  1346. *
  1347. * @return Zero on success or (negative) error code otherwise.
  1348. */
  1349. int bt_le_per_adv_sync_transfer(const struct bt_le_per_adv_sync *per_adv_sync,
  1350. const struct bt_conn *conn,
  1351. uint16_t service_data);
  1352. /**
  1353. * @brief Transfer the information about a periodic advertising set.
  1354. *
  1355. * This will allow another device to quickly synchronize to periodic
  1356. * advertising set from this device.
  1357. *
  1358. * @param adv The periodic advertising set to transfer info of.
  1359. * @param conn The peer device that will receive the information.
  1360. * @param service_data Application service data provided to the remote host.
  1361. *
  1362. * @return Zero on success or (negative) error code otherwise.
  1363. */
  1364. int bt_le_per_adv_set_info_transfer(const struct bt_le_ext_adv *adv,
  1365. const struct bt_conn *conn,
  1366. uint16_t service_data);
  1367. /**
  1368. * @brief Subscribe to periodic advertising sync transfers (PASTs).
  1369. *
  1370. * Sets the parameters and allow other devices to transfer periodic advertising
  1371. * syncs.
  1372. *
  1373. * @param conn The connection to set the parameters for. If NULL default
  1374. * parameters for all connections will be set. Parameters set
  1375. * for specific connection will always have precedence.
  1376. * @param param The periodic advertising sync transfer parameters.
  1377. *
  1378. * @return Zero on success or (negative) error code otherwise.
  1379. */
  1380. int bt_le_per_adv_sync_transfer_subscribe(
  1381. const struct bt_conn *conn,
  1382. const struct bt_le_per_adv_sync_transfer_param *param);
  1383. /**
  1384. * @brief Unsubscribe from periodic advertising sync transfers (PASTs).
  1385. *
  1386. * Remove the parameters that allow other devices to transfer periodic
  1387. * advertising syncs.
  1388. *
  1389. * @param conn The connection to remove the parameters for. If NULL default
  1390. * parameters for all connections will be removed. Unsubscribing
  1391. * for a specific device, will still allow other devices to
  1392. * transfer periodic advertising syncs.
  1393. *
  1394. * @return Zero on success or (negative) error code otherwise.
  1395. */
  1396. int bt_le_per_adv_sync_transfer_unsubscribe(const struct bt_conn *conn);
  1397. /**
  1398. * @brief Add a device to the periodic advertising list.
  1399. *
  1400. * Add peer device LE address to the periodic advertising list. This will make
  1401. * it possibly to automatically create a periodic advertising sync to this
  1402. * device.
  1403. *
  1404. * @param addr Bluetooth LE identity address.
  1405. * @param sid The advertising set ID. This value is obtained from the
  1406. * @ref bt_le_scan_recv_info in the scan callback.
  1407. *
  1408. * @return Zero on success or (negative) error code otherwise.
  1409. */
  1410. int bt_le_per_adv_list_add(const bt_addr_le_t *addr, uint8_t sid);
  1411. /**
  1412. * @brief Remove a device from the periodic advertising list.
  1413. *
  1414. * Removes peer device LE address from the periodic advertising list.
  1415. *
  1416. * @param addr Bluetooth LE identity address.
  1417. * @param sid The advertising set ID. This value is obtained from the
  1418. * @ref bt_le_scan_recv_info in the scan callback.
  1419. *
  1420. * @return Zero on success or (negative) error code otherwise.
  1421. */
  1422. int bt_le_per_adv_list_remove(const bt_addr_le_t *addr, uint8_t sid);
  1423. /**
  1424. * @brief Clear the periodic advertising list.
  1425. *
  1426. * Clears the entire periodic advertising list.
  1427. *
  1428. * @return Zero on success or (negative) error code otherwise.
  1429. */
  1430. int bt_le_per_adv_list_clear(void);
  1431. enum {
  1432. /** Convenience value when no options are specified. */
  1433. BT_LE_SCAN_OPT_NONE = 0,
  1434. /** Filter duplicates. */
  1435. BT_LE_SCAN_OPT_FILTER_DUPLICATE = BIT(0),
  1436. /** Filter using filter accept list. */
  1437. BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST = BIT(1),
  1438. /** Enable scan on coded PHY (Long Range).*/
  1439. BT_LE_SCAN_OPT_CODED = BIT(2),
  1440. /**
  1441. * @brief Disable scan on 1M phy.
  1442. *
  1443. * @note Requires @ref BT_LE_SCAN_OPT_CODED.
  1444. */
  1445. BT_LE_SCAN_OPT_NO_1M = BIT(3),
  1446. };
  1447. #define BT_LE_SCAN_OPT_FILTER_WHITELIST __DEPRECATED_MACRO BT_LE_SCAN_OPT_FILTER_ACCEPT_LIST
  1448. enum {
  1449. /** Scan without requesting additional information from advertisers. */
  1450. BT_LE_SCAN_TYPE_PASSIVE = 0x00,
  1451. /** Scan and request additional information from advertisers. */
  1452. BT_LE_SCAN_TYPE_ACTIVE = 0x01,
  1453. };
  1454. /** LE scan parameters */
  1455. struct bt_le_scan_param {
  1456. /** Scan type (BT_LE_SCAN_TYPE_ACTIVE or BT_LE_SCAN_TYPE_PASSIVE) */
  1457. uint8_t type;
  1458. /** Bit-field of scanning options. */
  1459. uint32_t options;
  1460. /** Scan interval (N * 0.625 ms) */
  1461. uint16_t interval;
  1462. /** Scan window (N * 0.625 ms) */
  1463. uint16_t window;
  1464. /**
  1465. * @brief Scan timeout (N * 10 ms)
  1466. *
  1467. * Application will be notified by the scan timeout callback.
  1468. * Set zero to disable timeout.
  1469. */
  1470. uint16_t timeout;
  1471. /**
  1472. * @brief Scan interval LE Coded PHY (N * 0.625 MS)
  1473. *
  1474. * Set zero to use same as LE 1M PHY scan interval.
  1475. */
  1476. uint16_t interval_coded;
  1477. /**
  1478. * @brief Scan window LE Coded PHY (N * 0.625 MS)
  1479. *
  1480. * Set zero to use same as LE 1M PHY scan window.
  1481. */
  1482. uint16_t window_coded;
  1483. };
  1484. /** LE advertisement packet information */
  1485. struct bt_le_scan_recv_info {
  1486. /**
  1487. * @brief Advertiser LE address and type.
  1488. *
  1489. * If advertiser is anonymous then this address will be
  1490. * @ref BT_ADDR_LE_ANY.
  1491. */
  1492. const bt_addr_le_t *addr;
  1493. /** Advertising Set Identifier. */
  1494. uint8_t sid;
  1495. /** Strength of advertiser signal. */
  1496. int8_t rssi;
  1497. /** Transmit power of the advertiser. */
  1498. int8_t tx_power;
  1499. /** Advertising packet type. */
  1500. uint8_t adv_type;
  1501. /** Advertising packet properties. */
  1502. uint16_t adv_props;
  1503. /**
  1504. * @brief Periodic advertising interval.
  1505. *
  1506. * If 0 there is no periodic advertising.
  1507. */
  1508. uint16_t interval;
  1509. /** Primary advertising channel PHY. */
  1510. uint8_t primary_phy;
  1511. /** Secondary advertising channel PHY. */
  1512. uint8_t secondary_phy;
  1513. };
  1514. /** Listener context for (LE) scanning. */
  1515. struct bt_le_scan_cb {
  1516. /**
  1517. * @brief Advertisement packet received callback.
  1518. *
  1519. * @param info Advertiser packet information.
  1520. * @param buf Buffer containing advertiser data.
  1521. */
  1522. void (*recv)(const struct bt_le_scan_recv_info *info,
  1523. struct net_buf_simple *buf);
  1524. /** @brief The scanner has stopped scanning after scan timeout. */
  1525. void (*timeout)(void);
  1526. sys_snode_t node;
  1527. };
  1528. /**
  1529. * @brief Initialize scan parameters
  1530. *
  1531. * @param _type Scan Type, BT_LE_SCAN_TYPE_ACTIVE or
  1532. * BT_LE_SCAN_TYPE_PASSIVE.
  1533. * @param _options Scan options
  1534. * @param _interval Scan Interval (N * 0.625 ms)
  1535. * @param _window Scan Window (N * 0.625 ms)
  1536. */
  1537. #define BT_LE_SCAN_PARAM_INIT(_type, _options, _interval, _window) \
  1538. { \
  1539. .type = (_type), \
  1540. .options = (_options), \
  1541. .interval = (_interval), \
  1542. .window = (_window), \
  1543. .timeout = 0, \
  1544. .interval_coded = 0, \
  1545. .window_coded = 0, \
  1546. }
  1547. /**
  1548. * @brief Helper to declare scan parameters inline
  1549. *
  1550. * @param _type Scan Type, BT_LE_SCAN_TYPE_ACTIVE or
  1551. * BT_LE_SCAN_TYPE_PASSIVE.
  1552. * @param _options Scan options
  1553. * @param _interval Scan Interval (N * 0.625 ms)
  1554. * @param _window Scan Window (N * 0.625 ms)
  1555. */
  1556. #define BT_LE_SCAN_PARAM(_type, _options, _interval, _window) \
  1557. ((struct bt_le_scan_param[]) { \
  1558. BT_LE_SCAN_PARAM_INIT(_type, _options, _interval, _window) \
  1559. })
  1560. /**
  1561. * @brief Helper macro to enable active scanning to discover new devices.
  1562. */
  1563. #define BT_LE_SCAN_ACTIVE BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_ACTIVE, \
  1564. BT_LE_SCAN_OPT_FILTER_DUPLICATE, \
  1565. BT_GAP_SCAN_FAST_INTERVAL, \
  1566. BT_GAP_SCAN_FAST_WINDOW)
  1567. /**
  1568. * @brief Helper macro to enable passive scanning to discover new devices.
  1569. *
  1570. * This macro should be used if information required for device identification
  1571. * (e.g., UUID) are known to be placed in Advertising Data.
  1572. */
  1573. #define BT_LE_SCAN_PASSIVE BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_PASSIVE, \
  1574. BT_LE_SCAN_OPT_FILTER_DUPLICATE, \
  1575. BT_GAP_SCAN_FAST_INTERVAL, \
  1576. BT_GAP_SCAN_FAST_WINDOW)
  1577. /**
  1578. * @brief Helper macro to enable active scanning to discover new devices.
  1579. * Include scanning on Coded PHY in addition to 1M PHY.
  1580. */
  1581. #define BT_LE_SCAN_CODED_ACTIVE \
  1582. BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_ACTIVE, \
  1583. BT_LE_SCAN_OPT_CODED | \
  1584. BT_LE_SCAN_OPT_FILTER_DUPLICATE, \
  1585. BT_GAP_SCAN_FAST_INTERVAL, \
  1586. BT_GAP_SCAN_FAST_WINDOW)
  1587. /**
  1588. * @brief Helper macro to enable passive scanning to discover new devices.
  1589. * Include scanning on Coded PHY in addition to 1M PHY.
  1590. *
  1591. * This macro should be used if information required for device identification
  1592. * (e.g., UUID) are known to be placed in Advertising Data.
  1593. */
  1594. #define BT_LE_SCAN_CODED_PASSIVE \
  1595. BT_LE_SCAN_PARAM(BT_LE_SCAN_TYPE_PASSIVE, \
  1596. BT_LE_SCAN_OPT_CODED | \
  1597. BT_LE_SCAN_OPT_FILTER_DUPLICATE, \
  1598. BT_GAP_SCAN_FAST_INTERVAL, \
  1599. BT_GAP_SCAN_FAST_WINDOW)
  1600. /**
  1601. * @brief Start (LE) scanning
  1602. *
  1603. * Start LE scanning with given parameters and provide results through
  1604. * the specified callback.
  1605. *
  1606. * @note The LE scanner by default does not use the Identity Address of the
  1607. * local device when @kconfig{CONFIG_BT_PRIVACY} is disabled. This is to
  1608. * prevent the active scanner from disclosing the identity information
  1609. * when requesting additional information from advertisers.
  1610. * In order to enable directed advertiser reports then
  1611. * @kconfig{CONFIG_BT_SCAN_WITH_IDENTITY} must be enabled.
  1612. *
  1613. * @param param Scan parameters.
  1614. * @param cb Callback to notify scan results. May be NULL if callback
  1615. * registration through @ref bt_le_scan_cb_register is preferred.
  1616. *
  1617. * @return Zero on success or error code otherwise, positive in case of
  1618. * protocol error or negative (POSIX) in case of stack internal error.
  1619. */
  1620. int bt_le_scan_start(const struct bt_le_scan_param *param, bt_le_scan_cb_t cb);
  1621. /**
  1622. * @brief Stop (LE) scanning.
  1623. *
  1624. * Stops ongoing LE scanning.
  1625. *
  1626. * @return Zero on success or error code otherwise, positive in case of
  1627. * protocol error or negative (POSIX) in case of stack internal error.
  1628. */
  1629. int bt_le_scan_stop(void);
  1630. /**
  1631. * @brief Register scanner packet callbacks.
  1632. *
  1633. * Adds the callback structure to the list of callback structures that monitors
  1634. * scanner activity.
  1635. *
  1636. * This callback will be called for all scanner activity, regardless of what
  1637. * API was used to start the scanner.
  1638. *
  1639. * @param cb Callback struct. Must point to memory that remains valid.
  1640. */
  1641. void bt_le_scan_cb_register(struct bt_le_scan_cb *cb);
  1642. /**
  1643. * @brief Unregister scanner packet callbacks.
  1644. *
  1645. * Remove the callback structure from the list of scanner callbacks.
  1646. *
  1647. * @param cb Callback struct. Must point to memory that remains valid.
  1648. */
  1649. void bt_le_scan_cb_unregister(struct bt_le_scan_cb *cb);
  1650. /**
  1651. * @brief Add device (LE) to filter accept list.
  1652. *
  1653. * Add peer device LE address to the filter accept list.
  1654. *
  1655. * @note The filter accept list cannot be modified when an LE role is using
  1656. * the filter accept list, i.e advertiser or scanner using a filter accept list
  1657. * or automatic connecting to devices using filter accept list.
  1658. *
  1659. * @param addr Bluetooth LE identity address.
  1660. *
  1661. * @return Zero on success or error code otherwise, positive in case of
  1662. * protocol error or negative (POSIX) in case of stack internal error.
  1663. */
  1664. int bt_le_filter_accept_list_add(const bt_addr_le_t *addr);
  1665. __deprecated
  1666. static inline int bt_le_whitelist_add(const bt_addr_le_t *addr)
  1667. {
  1668. return bt_le_filter_accept_list_add(addr);
  1669. }
  1670. /**
  1671. * @brief Remove device (LE) from filter accept list.
  1672. *
  1673. * Remove peer device LE address from the filter accept list.
  1674. *
  1675. * @note The filter accept list cannot be modified when an LE role is using
  1676. * the filter accept list, i.e advertiser or scanner using a filter accept list
  1677. * or automatic connecting to devices using filter accept list.
  1678. *
  1679. * @param addr Bluetooth LE identity address.
  1680. *
  1681. * @return Zero on success or error code otherwise, positive in case of
  1682. * protocol error or negative (POSIX) in case of stack internal error.
  1683. */
  1684. int bt_le_filter_accept_list_remove(const bt_addr_le_t *addr);
  1685. __deprecated
  1686. static inline int bt_le_whitelist_rem(const bt_addr_le_t *addr)
  1687. {
  1688. return bt_le_filter_accept_list_remove(addr);
  1689. }
  1690. /**
  1691. * @brief Clear filter accept list.
  1692. *
  1693. * Clear all devices from the filter accept list.
  1694. *
  1695. * @note The filter accept list cannot be modified when an LE role is using
  1696. * the filter accept list, i.e advertiser or scanner using a filter accept
  1697. * list or automatic connecting to devices using filter accept list.
  1698. *
  1699. * @return Zero on success or error code otherwise, positive in case of
  1700. * protocol error or negative (POSIX) in case of stack internal error.
  1701. */
  1702. int bt_le_filter_accept_list_clear(void);
  1703. __deprecated
  1704. static inline int bt_le_whitelist_clear(void)
  1705. {
  1706. return bt_le_filter_accept_list_clear();
  1707. }
  1708. /**
  1709. * @brief Set (LE) channel map.
  1710. *
  1711. * @param chan_map Channel map.
  1712. *
  1713. * @return Zero on success or error code otherwise, positive in case of
  1714. * protocol error or negative (POSIX) in case of stack internal error.
  1715. */
  1716. int bt_le_set_chan_map(uint8_t chan_map[5]);
  1717. /**
  1718. * @brief Helper for parsing advertising (or EIR or OOB) data.
  1719. *
  1720. * A helper for parsing the basic data types used for Extended Inquiry
  1721. * Response (EIR), Advertising Data (AD), and OOB data blocks. The most
  1722. * common scenario is to call this helper on the advertising data
  1723. * received in the callback that was given to bt_le_scan_start().
  1724. *
  1725. * @param ad Advertising data as given to the bt_le_scan_cb_t callback.
  1726. * @param func Callback function which will be called for each element
  1727. * that's found in the data. The callback should return
  1728. * true to continue parsing, or false to stop parsing.
  1729. * @param user_data User data to be passed to the callback.
  1730. */
  1731. void bt_data_parse(struct net_buf_simple *ad,
  1732. bool (*func)(struct bt_data *data, void *user_data),
  1733. void *user_data);
  1734. /** LE Secure Connections pairing Out of Band data. */
  1735. struct bt_le_oob_sc_data {
  1736. /** Random Number. */
  1737. uint8_t r[16];
  1738. /** Confirm Value. */
  1739. uint8_t c[16];
  1740. };
  1741. /** LE Out of Band information. */
  1742. struct bt_le_oob {
  1743. /** LE address. If privacy is enabled this is a Resolvable Private
  1744. * Address.
  1745. */
  1746. bt_addr_le_t addr;
  1747. /** LE Secure Connections pairing Out of Band data. */
  1748. struct bt_le_oob_sc_data le_sc_data;
  1749. };
  1750. /**
  1751. * @brief Get local LE Out of Band (OOB) information.
  1752. *
  1753. * This function allows to get local information that are useful for
  1754. * Out of Band pairing or connection creation.
  1755. *
  1756. * If privacy @kconfig{CONFIG_BT_PRIVACY} is enabled this will result in
  1757. * generating new Resolvable Private Address (RPA) that is valid for
  1758. * @kconfig{CONFIG_BT_RPA_TIMEOUT} seconds. This address will be used for
  1759. * advertising started by @ref bt_le_adv_start, active scanning and
  1760. * connection creation.
  1761. *
  1762. * @note If privacy is enabled the RPA cannot be refreshed in the following
  1763. * cases:
  1764. * - Creating a connection in progress, wait for the connected callback.
  1765. * In addition when extended advertising @kconfig{CONFIG_BT_EXT_ADV} is
  1766. * not enabled or not supported by the controller:
  1767. * - Advertiser is enabled using a Random Static Identity Address for a
  1768. * different local identity.
  1769. * - The local identity conflicts with the local identity used by other
  1770. * roles.
  1771. *
  1772. * @param[in] id Local identity, in most cases BT_ID_DEFAULT.
  1773. * @param[out] oob LE OOB information
  1774. *
  1775. * @return Zero on success or error code otherwise, positive in case of
  1776. * protocol error or negative (POSIX) in case of stack internal error.
  1777. */
  1778. int bt_le_oob_get_local(uint8_t id, struct bt_le_oob *oob);
  1779. /**
  1780. * @brief Get local LE Out of Band (OOB) information.
  1781. *
  1782. * This function allows to get local information that are useful for
  1783. * Out of Band pairing or connection creation.
  1784. *
  1785. * If privacy @kconfig{CONFIG_BT_PRIVACY} is enabled this will result in
  1786. * generating new Resolvable Private Address (RPA) that is valid for
  1787. * @kconfig{CONFIG_BT_RPA_TIMEOUT} seconds. This address will be used by the
  1788. * advertising set.
  1789. *
  1790. * @note When generating OOB information for multiple advertising set all
  1791. * OOB information needs to be generated at the same time.
  1792. *
  1793. * @note If privacy is enabled the RPA cannot be refreshed in the following
  1794. * cases:
  1795. * - Creating a connection in progress, wait for the connected callback.
  1796. *
  1797. * @param[in] adv The advertising set object
  1798. * @param[out] oob LE OOB information
  1799. *
  1800. * @return Zero on success or error code otherwise, positive in case
  1801. * of protocol error or negative (POSIX) in case of stack internal error.
  1802. */
  1803. int bt_le_ext_adv_oob_get_local(struct bt_le_ext_adv *adv,
  1804. struct bt_le_oob *oob);
  1805. /** @brief BR/EDR discovery result structure */
  1806. struct bt_br_discovery_result {
  1807. /** private */
  1808. uint8_t _priv[4];
  1809. /** Remote device address */
  1810. bt_addr_t addr;
  1811. /** RSSI from inquiry */
  1812. int8_t rssi;
  1813. /** Class of Device */
  1814. uint8_t cod[3];
  1815. /** Extended Inquiry Response */
  1816. uint8_t eir[240];
  1817. };
  1818. /**
  1819. * @typedef bt_br_discovery_cb_t
  1820. * @brief Callback type for reporting BR/EDR discovery (inquiry)
  1821. * results.
  1822. *
  1823. * A callback of this type is given to the bt_br_discovery_start()
  1824. * function and will be called at the end of the discovery with
  1825. * information about found devices populated in the results array.
  1826. *
  1827. * @param results Storage used for discovery results
  1828. * @param count Number of valid discovery results.
  1829. */
  1830. typedef void bt_br_discovery_cb_t(struct bt_br_discovery_result *results,
  1831. size_t count);
  1832. /** BR/EDR discovery parameters */
  1833. struct bt_br_discovery_param {
  1834. /** Maximum length of the discovery in units of 1.28 seconds.
  1835. * Valid range is 0x01 - 0x30.
  1836. */
  1837. uint8_t length;
  1838. /** True if limited discovery procedure is to be used. */
  1839. bool limited;
  1840. };
  1841. /**
  1842. * @brief Start BR/EDR discovery
  1843. *
  1844. * Start BR/EDR discovery (inquiry) and provide results through the specified
  1845. * callback. When bt_br_discovery_cb_t is called it indicates that discovery
  1846. * has completed. If more inquiry results were received during session than
  1847. * fits in provided result storage, only ones with highest RSSI will be
  1848. * reported.
  1849. *
  1850. * @param param Discovery parameters.
  1851. * @param results Storage for discovery results.
  1852. * @param count Number of results in storage. Valid range: 1-255.
  1853. * @param cb Callback to notify discovery results.
  1854. *
  1855. * @return Zero on success or error code otherwise, positive in case
  1856. * of protocol error or negative (POSIX) in case of stack internal error
  1857. */
  1858. int bt_br_discovery_start(const struct bt_br_discovery_param *param,
  1859. struct bt_br_discovery_result *results, size_t count,
  1860. bt_br_discovery_cb_t cb);
  1861. /**
  1862. * @brief Stop BR/EDR discovery.
  1863. *
  1864. * Stops ongoing BR/EDR discovery. If discovery was stopped by this call
  1865. * results won't be reported
  1866. *
  1867. * @return Zero on success or error code otherwise, positive in case of
  1868. * protocol error or negative (POSIX) in case of stack internal error.
  1869. */
  1870. int bt_br_discovery_stop(void);
  1871. struct bt_br_oob {
  1872. /** BR/EDR address. */
  1873. bt_addr_t addr;
  1874. };
  1875. /**
  1876. * @brief Get BR/EDR local Out Of Band information
  1877. *
  1878. * This function allows to get local controller information that are useful
  1879. * for Out Of Band pairing or connection creation process.
  1880. *
  1881. * @param oob Out Of Band information
  1882. */
  1883. int bt_br_oob_get_local(struct bt_br_oob *oob);
  1884. /**
  1885. * @brief Enable/disable set controller in discoverable state.
  1886. *
  1887. * Allows make local controller to listen on INQUIRY SCAN channel and responds
  1888. * to devices making general inquiry. To enable this state it's mandatory
  1889. * to first be in connectable state.
  1890. *
  1891. * @param enable Value allowing/disallowing controller to become discoverable.
  1892. *
  1893. * @return Negative if fail set to requested state or requested state has been
  1894. * already set. Zero if done successfully.
  1895. */
  1896. int bt_br_set_discoverable(bool enable);
  1897. /**
  1898. * @brief Enable/disable set controller in connectable state.
  1899. *
  1900. * Allows make local controller to be connectable. It means the controller
  1901. * start listen to devices requests on PAGE SCAN channel. If disabled also
  1902. * resets discoverability if was set.
  1903. *
  1904. * @param enable Value allowing/disallowing controller to be connectable.
  1905. *
  1906. * @return Negative if fail set to requested state or requested state has been
  1907. * already set. Zero if done successfully.
  1908. */
  1909. int bt_br_set_connectable(bool enable);
  1910. /**
  1911. * @brief Clear pairing information.
  1912. *
  1913. * @param id Local identity (mostly just BT_ID_DEFAULT).
  1914. * @param addr Remote address, NULL or BT_ADDR_LE_ANY to clear all remote
  1915. * devices.
  1916. *
  1917. * @return 0 on success or negative error value on failure.
  1918. */
  1919. int bt_unpair(uint8_t id, const bt_addr_le_t *addr);
  1920. /** Information about a bond with a remote device. */
  1921. struct bt_bond_info {
  1922. /** Address of the remote device. */
  1923. bt_addr_le_t addr;
  1924. };
  1925. /**
  1926. * @brief Iterate through all existing bonds.
  1927. *
  1928. * @param id Local identity (mostly just BT_ID_DEFAULT).
  1929. * @param func Function to call for each bond.
  1930. * @param user_data Data to pass to the callback function.
  1931. */
  1932. void bt_foreach_bond(uint8_t id, void (*func)(const struct bt_bond_info *info,
  1933. void *user_data),
  1934. void *user_data);
  1935. /**
  1936. * @}
  1937. */
  1938. #ifdef __cplusplus
  1939. }
  1940. #endif
  1941. /**
  1942. * @}
  1943. */
  1944. #endif /* ZEPHYR_INCLUDE_BLUETOOTH_BLUETOOTH_H_ */