PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

508 lines
22KB

  1. /* Copyright (c) 2009 Nordic Semiconductor. All Rights Reserved.
  2. *
  3. * The information contained herein is property of Nordic Semiconductor ASA.
  4. * Terms and conditions of usage are described in detail in NORDIC
  5. * SEMICONDUCTOR STANDARD SOFTWARE LICENSE AGREEMENT.
  6. *
  7. * Licensees are granted free, non-transferable use of the information. NO
  8. * WARRENTY of ANY KIND is provided. This heading must NOT be removed from
  9. * the file.
  10. *
  11. * $LastChangedRevision$
  12. */
  13. /* Attention!
  14. * To maintain compliance with Nordic Semiconductor ASA’s Bluetooth profile
  15. * qualification listings, this section of source code must not be modified.
  16. */
  17. #ifndef LIB_ACI_H__
  18. #define LIB_ACI_H__
  19. /** @file
  20. * @brief ACI library
  21. */
  22. /** @addtogroup lib_aci
  23. @{
  24. @brief Library for the logical part of the Application Controller Interface (ACI)
  25. */
  26. #include "hal_platform.h"
  27. #include "hal/hal_aci_tl.h"
  28. #include "aci.h"
  29. #include "aci_cmds.h"
  30. #include "aci_evts.h"
  31. #define EVT_CMD_RESPONSE_MIN_LENGTH 3
  32. #define PIPES_ARRAY_SIZE ((ACI_DEVICE_MAX_PIPES + 7)/8)
  33. /* Same size as a hal_aci_data_t */
  34. typedef struct hal_aci_evt_t{
  35. uint8_t debug_byte;
  36. aci_evt_t evt;
  37. } __attribute__((packed)) hal_aci_evt_t;
  38. typedef struct
  39. {
  40. uint8_t location; /**< enum aci_pipe_store_t */
  41. aci_pipe_type_t pipe_type;
  42. } services_pipe_type_mapping_t;
  43. typedef struct aci_setup_info_t
  44. {
  45. services_pipe_type_mapping_t *services_pipe_type_mapping;
  46. uint8_t number_of_pipes;
  47. hal_aci_data_t *setup_msgs;
  48. uint8_t num_setup_msgs;
  49. } aci_setup_info_t;
  50. // aci_struct that will contain
  51. // total initial credits
  52. // current credit
  53. // current state of the aci (setup/standby/active/sleep)
  54. // open remote pipe pending
  55. // close remote pipe pending
  56. // Current pipe available bitmap
  57. // Current pipe closed bitmap
  58. // Current connection interval, slave latency and link supervision timeout
  59. // Current State of the the GATT client (Service Discovery status)
  60. // Relationship of bond to peer address
  61. typedef struct aci_state_t
  62. {
  63. aci_setup_info_t aci_setup_info; /* Data structures that are created from nRFgo Studio */
  64. uint8_t bonded; /* ( aci_bond_status_code_t ) Is the nRF8001 bonded to a peer device */
  65. uint8_t data_credit_total; /* Total data credit available for the specific version of the nRF8001, total equals available when a link is established */
  66. aci_device_operation_mode_t device_state; /* Operating mode of the nRF8001 */
  67. /* Start : Variables that are valid only when in a connection */
  68. uint8_t data_credit_available; /* Available data credits at a specific point of time, ACI_EVT_DATA_CREDIT updates the available credits */
  69. uint16_t connection_interval; /* Multiply by 1.25 to get the connection interval in milliseconds*/
  70. uint16_t slave_latency; /* Number of consecutive connection intervals that the nRF8001 is not required to transmit. Use this to save power */
  71. uint16_t supervision_timeout; /* Multiply by 10 to get the supervision timeout in milliseconds */
  72. uint8_t pipes_open_bitmap[PIPES_ARRAY_SIZE]; /* Bitmap -> pipes are open and can be used for sending data over the air */
  73. uint8_t pipes_closed_bitmap[PIPES_ARRAY_SIZE]; /* Bitmap -> pipes are closed and cannot be used for sending data over the air */
  74. bool confirmation_pending; /* Attribute protocol Handle Value confirmation is pending for a Handle Value Indication
  75. (ACK is pending for a TX_ACK pipe) on local GATT Server*/
  76. /* End : Variables that are valid only when in a connection */
  77. } aci_state_t;
  78. #define DISCONNECT_REASON_CX_TIMEOUT 0x08
  79. #define DISCONNECT_REASON_CX_CLOSED_BY_PEER_DEVICE 0x13
  80. #define DISCONNECT_REASON_POWER_LOSS 0x14
  81. #define DISCONNECT_REASON_CX_CLOSED_BY_LOCAL_DEVICE 0x16
  82. #define DISCONNECT_REASON_ADVERTISER_TIMEOUT 0x50
  83. /** @name Functions for library management */
  84. //@{
  85. /** @brief Function to enable printing of all ACI commands sent and ACI events received
  86. * @details This function shall be used to enable or disable the debug printing.
  87. Debug printing is disabled by default.
  88. */
  89. void lib_aci_debug_print(bool enable);
  90. /** @brief Initialization function.
  91. * @details This function shall be used to initialize/reset ACI Library and also Resets the nRF8001 by togging the reset pin of the nRF8001. This function will reset
  92. * all the variables locally used by ACI library to their respective default values.
  93. */
  94. void lib_aci_init(aci_state_t *aci_stat);
  95. /** @brief Gets the number of currently available ACI credits.
  96. * @return Number of ACI credits.
  97. */
  98. uint8_t lib_aci_get_nb_available_credits(aci_state_t *aci_stat);
  99. /** @brief Gets the connection interval in milliseconds.
  100. * @return Connection interval in milliseconds.
  101. */
  102. uint16_t lib_aci_get_cx_interval_ms(aci_state_t *aci_stat);
  103. /** @brief Gets the connection interval in multiple of 1.25&nbsp;ms.
  104. * @return Connection interval in multiple of 1.25&nbsp;ms.
  105. */
  106. uint16_t lib_aci_get_cx_interval(aci_state_t *aci_stat);
  107. /** @brief Gets the current slave latency.
  108. * @return Current slave latency.
  109. */
  110. uint16_t lib_aci_get_slave_latency(aci_state_t *aci_stat);
  111. /** @brief Checks if a given pipe is available.
  112. * @param pipe Pipe to check.
  113. * @return True if the pipe is available, otherwise false.
  114. */
  115. bool lib_aci_is_pipe_available(aci_state_t *aci_stat, uint8_t pipe);
  116. /** @brief Checks if a given pipe is closed.
  117. * @param pipe Pipe to check.
  118. * @return True if the pipe is closed, otherwise false.
  119. */
  120. bool lib_aci_is_pipe_closed(aci_state_t *aci_stat, uint8_t pipe);
  121. /** @brief Checks if the discovery operation is finished.
  122. * @return True if the discovery is finished.
  123. */
  124. bool lib_aci_is_discovery_finished(aci_state_t *aci_stat);
  125. //@}
  126. /** @name ACI Commands available in all modes */
  127. //@{
  128. /** @brief Sets the radio in sleep mode.
  129. * @details The function sends a @c sleep command to the radio.
  130. * If the radio is advertising or connected, it sends back an error, then use lib_aci_radio_reset
  131. * if advertising or disconnect if in a connection.
  132. * @return True if the transaction is successfully initiated.
  133. */
  134. bool lib_aci_sleep(void);
  135. /** @brief Resets the radio.
  136. * @details The function sends a @c BasebandReset command to the radio.
  137. * @return True if the transaction is successfully initiated.
  138. */
  139. bool lib_aci_radio_reset(void);
  140. /** @brief Radio starts directed advertising to bonded device.
  141. * @details The function sends a @c DirectedConnect command to the radio.
  142. * @return True if the transaction is successfully initiated.
  143. */
  144. bool lib_aci_direct_connect(void);
  145. /** @brief Gets the radio's version.
  146. * @details This function sends a @c GetDeviceVersion command.
  147. * @return True if the transaction is successfully initiated.
  148. */
  149. bool lib_aci_device_version(void);
  150. /** @brief Gets the device address.
  151. * @details This function sends a @c GetDeviceAddress command.
  152. * @return True if the transaction is successfully initiated.
  153. */
  154. bool lib_aci_get_address(void);
  155. /** @brief Gets the temperature.
  156. * @details This function sends a @c GetTemperature command. lib_aci
  157. * calls the @ref lib_aci_transaction_finished_hook() function when the temperature is received.
  158. * @return True if the transaction is successfully initiated.
  159. */
  160. bool lib_aci_get_temperature(void);
  161. /** @brief Gets the battery level.
  162. * @details This function sends a @c GetBatteryLevel command.
  163. * @return True if the transaction is successfully initiated.
  164. */
  165. bool lib_aci_get_battery_level(void);
  166. //@}
  167. /** @name ACI commands available in Sleep mode */
  168. //@{
  169. /** @brief Wakes up the radio.
  170. * @details This function sends a @c Wakeup command to wake up the radio from
  171. * sleep mode. When woken up the radio sends a @c DeviceStartedEvent and
  172. * a @c CommandResponseEvent.
  173. * @return True if the transaction is successfully initiated.
  174. */
  175. bool lib_aci_wakeup(void);
  176. //@}
  177. /** @name ACI commands available in Active mode */
  178. //@{
  179. /** @brief Sets the radio in test mode.
  180. * @details This function sends a @c Test command to the radio. There are two
  181. * Test modes available:
  182. * - UART: DTM commands are received over UART.
  183. * - ACI: DTM commands are received over ACI.
  184. * The same command is used to exit the test mode When receiving
  185. * a @c DeviceStartedEvent the radio has entered the new mode.
  186. * @param enter_exit_test_mode Enter a Test mode, or exit Test mode.
  187. * @return True if the transaction is successfully initiated.
  188. */
  189. bool lib_aci_test(aci_test_mode_change_t enter_exit_test_mode);
  190. /** @brief Sets the radio's TX power.
  191. * @details This function sends a @c SetTxPower command.
  192. * @param tx_power TX power to be used by the radio.
  193. * @return True if the transaction is successfully initiated.
  194. */
  195. bool lib_aci_set_tx_power(aci_device_output_power_t tx_power);
  196. /** @brief Tries to connect to a peer device.
  197. * @details This function sends a @c Connect command to the radio.
  198. * @param run_timeout Maximum advertising time in seconds (0 means infinite).
  199. * @param adv_interval Advertising interval (in multiple of 0.625&nbsp;ms).
  200. * @return True if the transaction is successfully initiated.
  201. */
  202. bool lib_aci_connect(uint16_t run_timeout, uint16_t adv_interval);
  203. /** @brief Tries to bond with a peer device.
  204. * @details This function sends a @c Bond command to the radio.
  205. * @param run_timeout Maximum advertising time in seconds (0 means infinite).
  206. * @param adv_interval Advertising interval (in multiple of 0.625&nbsp;ms).
  207. * @return True if the transaction is successfully initiated.
  208. */
  209. bool lib_aci_bond(uint16_t run_timeout, uint16_t adv_interval);
  210. /** @brief Disconnects from peer device.
  211. * @details This function sends a @c Disconnect command to the radio.
  212. * @param reason Reason for disconnecting.
  213. * @return True if the transaction is successfully initiated.
  214. */
  215. bool lib_aci_disconnect(aci_state_t *aci_stat, aci_disconnect_reason_t reason);
  216. /**@brief Sets Local Data.
  217. * @details
  218. * This command updates the value of the characteristic value or the characteristic descriptor stored locally on the device.
  219. * Can be called for all types of pipes as long as the data is stored locally.
  220. * @param ACI state structure
  221. * @param pipe Pipe number on which the data should be set.
  222. * @param value Pointer to the data to set.
  223. * @param size Size of the data to set.
  224. * @return True if the transaction is successfully initiated.
  225. */
  226. bool lib_aci_set_local_data(aci_state_t *aci_stat, uint8_t pipe, uint8_t *value, uint8_t size);
  227. /** @brief Sends Broadcast message to the radio.
  228. * @details The Broadcast message starts advertisement procedure
  229. * using the given interval with the intention of broadcasting data to a peer device.
  230. * @param timeout Time, in seconds, to advertise before exiting to standby mode (0 means infinite).
  231. * Valid values: 0 to 16383.
  232. * @param adv_interval Advertising interval (in multiple of 0.625&nbsp;ms).
  233. * Valid values: 160 to 16384 (which corresponds to an interval from 100 ms to 10.24 s).
  234. * @return True if the broadcast message is sent successfully to the radio.
  235. */
  236. bool lib_aci_broadcast(const uint16_t timeout, const uint16_t adv_interval);
  237. /** @name Open Advertising Pipes. */
  238. /** @brief Sends a command to the radio to set the input pipe to be placed in Advertisement Service Data.
  239. * @details This function sends a command to the radio that places the pipe in
  240. * advertisement service data. To start advertising service data, call this function before
  241. * Connecting, Broadcasting or Bonding to peer. The data that should be sent in the advertisement packets
  242. * must be set using the @c lib_aci_set_local_data function. This function can be called during
  243. * advertising to enable/disable broadcast pipes.
  244. * @param pipe The pipe that has to be placed in advertising service data.
  245. * @return True if the Open Adv Pipe message is sent successfully to the radio.
  246. */
  247. bool lib_aci_open_adv_pipe(const uint8_t pipe);
  248. /** @name Open Advertising Pipes */
  249. /** @brief Sends a command to the radio to set the pipes to be placed in Advertisement Service Data.
  250. * @details This function will send a command to the radio that will set the pipes to be placed in
  251. * advertisement Service Data. To start advertising service data, this function should be called before
  252. * Connecting, Broadcasting or Bonding to peer. This function can be called during
  253. * advertising to enable/disable broadcast pipes. Use this as an alternative to @ref lib_aci_open_adv_pipe
  254. * to avoid multiple function calls for placing multiple pipes in the adv data.
  255. * @param adv_service_data_pipes Pipe bitmap, where '1' indicates that the corresponding
  256. * Valid Values: 0000000000000000 to FEFFFFFFFFFFFF7F (See the ACI Pipe Status Evt bitmap in the nRF8001 datasheet
  257. * TX_BROADCAST pipe data is to be placed in Advertising Service Data fields
  258. * @return true if the Open Adv Pipe message was sent successfully to the radio.
  259. */
  260. bool lib_aci_open_adv_pipes(const uint8_t * const adv_service_data_pipes);
  261. //@}
  262. /** @name ACI commands available in Connected mode */
  263. //@{
  264. /** @brief Sets a given application latency.
  265. * @details This function sends a @c setApplicationLatency command.
  266. * @return True if the transaction is successfully initiated.
  267. */
  268. bool lib_aci_set_app_latency(uint16_t latency, aci_app_latency_mode_t latency_mode);
  269. /** @brief Opens a remote pipe.
  270. * @details This function sends an @c OpenRemotePipe command.
  271. * @param pipe Number of the pipe to open.
  272. * @return True if the transaction is successfully initiated.
  273. */
  274. bool lib_aci_open_remote_pipe(aci_state_t *aci_stat, uint8_t pipe);
  275. /** @brief Closes a remote pipe.
  276. * @details This function sends an @c CloseRemotePipe command.
  277. * @param pipe Pipe number to close.
  278. * @return True if the transaction is successfully initiated.
  279. */
  280. bool lib_aci_close_remote_pipe(aci_state_t *aci_stat, uint8_t pipe);
  281. /** @brief Sends data on a given pipe.
  282. * @details This function sends a @c SendData command with application data to
  283. * the radio. This function memorizes credit use, and checks that
  284. * enough credits are available.
  285. * @param pipe Pipe number on which the data should be sent.
  286. * @param value Pointer to the data to send.
  287. * @param size Size of the data to send.
  288. * @return True if the transaction is successfully initiated.
  289. */
  290. bool lib_aci_send_data(uint8_t pipe, uint8_t *value, uint8_t size);
  291. /** @brief Requests data from a given pipe.
  292. * @details This function sends a @c RequestData command to the radio. This
  293. * function memorizes credit uses, and check that enough credits are available.
  294. * After this command, the radio sends back either a @c DataReceivedEvent
  295. * or a @c PipeErrorEvent.
  296. * @param pipe Pipe number on which the data is requested.
  297. * @return True if the transaction is successfully initiated.
  298. */
  299. bool lib_aci_request_data(aci_state_t *aci_stat, uint8_t pipe);
  300. /** @brief Sends a L2CAP change connection parameters request.
  301. * @details This function sends a @c ChangeTiming command to the radio. This command triggers a "L2CAP change connection parameters" request
  302. * to the master. If the master rejects or accepts but doesn't change the connection parameters within
  303. * 30 seconds, a timing event with the unchanged connection parameters is sent by the radio.
  304. * If the request is accepted and the master changes connection parameters, a timing event with
  305. * the new connection parameters is sent by the radio.
  306. * If the master doesn't reply to the request within 60 seconds, the radio disconnects.
  307. * @param minimun_cx_interval Minimum connection interval requested, in multiple of 1.25&nbsp;ms.
  308. * @param maximum_cx_interval Maximum connection interval requested, in multiple of 1.25&nbsp;ms.
  309. * @param slave_latency requested slave latency.
  310. * @param timeout requested slave timeout, in multiple of 10&nbsp;ms.
  311. * @return True if the transaction is successfully initiated.
  312. */
  313. bool lib_aci_change_timing(uint16_t minimun_cx_interval, uint16_t maximum_cx_interval, uint16_t slave_latency, uint16_t timeout);
  314. /** @brief Sends a L2CAP change connection parameters request with the connection predefined preffered connection parameters.
  315. * @details This function sends a @c ChangeTiming command to the radio. This command triggers a "L2CAP change connection parameters" request
  316. * to the master. If the master rejects or accepts but doesn't change the connection parameters within
  317. * 30 seconds, a timing event with the unchanged connection parameters is sent by the radio.
  318. * If the request is accepted and the master changes connection parameters, a timing event with
  319. * the new connection parameters is sent by the radio.
  320. * If the master doesn't reply to the request within 60 seconds, the radio disconnects.
  321. * The timing parameters used are the Timing parameters in the GAP settings in the nRFgo Studio.
  322. * The Timing parameters as stored as the GAP Preferred Peripheral Connection Parameters.
  323. * @return True if the transaction is successfully initiated.
  324. */
  325. bool lib_aci_change_timing_GAP_PPCP(void);
  326. /** @brief Sends acknowledgement message to peer.
  327. * @details This function sends @c SendDataAck command to radio. The radio is expected
  328. * to send either Handle Value Confirmation or Write response depending
  329. * on whether the data is stored remotely or locally.
  330. * @param pipe Pipe number for which the acknowledgement is to be sent.
  331. * @return True if the ack was sent successfully. False otherwise.
  332. */
  333. bool lib_aci_send_ack(aci_state_t *aci_stat, const uint8_t pipe);
  334. /** @brief Sends negative acknowledgement message to peer.
  335. * @details This function sends @c SendDataNack command to radio. The radio is expected
  336. * to send Error Response to the peer.
  337. * @param pipe Pipe number for which the nack is to be sent.
  338. * @param error_code Error code to be sent in the NACk.
  339. * @return True if the nack was sent successfully. False otherwise.
  340. */
  341. bool lib_aci_send_nack(aci_state_t *aci_stat, const uint8_t pipe, const uint8_t error_code);
  342. /** @brief Sends ReadDynamicData command to the host.
  343. * @details This function sends @c ReadDynamicData command to host. The host is expected
  344. * to send @c CommandResponse back with the dynamic data. The application is expected to
  345. * call this function in a loop until all the dynamic data is read out from the host.
  346. * As long as there is dynamic data to be read from the host, the command response
  347. * for this message has its status field set to ACI_STATUS_TRANSACTION_CONTINUE (0x01).
  348. * The application may chose to store this read out data in a non-volatile memory location
  349. * and later chose to write it back using the function lib_aci_write_dynamic_data.
  350. * @return True if the command was sent successfully through the ACI. False otherwise.
  351. */
  352. bool lib_aci_read_dynamic_data(void);
  353. /** @brief Sends WriteDynamicData command to the host.
  354. * @details This function sends @c WriteDynamicData command to host. The host is expected
  355. * to send @c CommandResponse with the status of this operation. As long as the status field
  356. * in the @c CommandResponse is ACI_STATUS_TRANSACTION_CONTINUE (0x01), the hosts expects
  357. * more dynamic data to be written. This function should ideally be called in a cycle,
  358. * until all the stored dynamic data is sent to the host. This function should be
  359. * called with the dynamic data obtained from the response to a @c ReadDynamicData
  360. * (see @c lib_aci_read_dynamic_data) command.
  361. * @param sequence_number Sequence number of the dynamic data to be sent.
  362. * @param dynamic_data Pointer to the dynamic data.
  363. * @param length Length of the dynamic data.
  364. * @return True if the command was sent successfully through the ACI. False otherwise.
  365. */
  366. bool lib_aci_write_dynamic_data(uint8_t sequence_number, uint8_t* dynamic_data, uint8_t length);
  367. //@}
  368. /** @name ACI commands available while connected in Bond mode */
  369. //@{
  370. /** @brief Sends a SMP Security Request.
  371. * @details This function send a @c BondRequest command to the radio.
  372. * This command triggers a SMP Security Request to the master. If the
  373. * master rejects with a pairing failed or if the bond timer expires the connection is closed.
  374. * @return True if the transaction is successfully initiated.
  375. */
  376. bool lib_aci_bond_request(void);
  377. /** @brief Set the key requested by the 8001.
  378. * @details This function sends an @c SetKey command to the radio.
  379. * @param key_rsp_type Type of key.
  380. * @param key Pointer to the key to set.
  381. * @param len Length of the key.
  382. * @return True if the transaction is successfully initiated.
  383. */
  384. bool lib_aci_set_key(aci_key_type_t key_rsp_type, uint8_t *key, uint8_t len);
  385. //@}
  386. /** @name ACI commands available in Test mode */
  387. //@{
  388. /** @brief Sends an echo message
  389. * @details This function sends an @c Echo command to the radio. lib_aci
  390. * places the Echp ACI command in the ACI command queue
  391. * @param message_size Length of the data to send.
  392. * @param message_data Pointer to the data to send.
  393. * @return True if the transaction is successfully initiated.
  394. */
  395. bool lib_aci_echo_msg(uint8_t message_size, uint8_t *message_data);
  396. /** @brief Sends an DTM command
  397. * @details This function sends an @c DTM command to the radio.
  398. * @param dtm_command_msbyte Most significant byte of the DTM command.
  399. * @param dtm_command_lsbyte Least significant byte of the DTM command.
  400. * @return True if the transaction is successfully initiated.
  401. */
  402. bool lib_aci_dtm_command(uint8_t dtm_command_msbyte, uint8_t dtm_command_lsbyte);
  403. /** @brief Gets an ACI event from the ACI Event Queue
  404. * @details This function gets an ACI event from the ACI event queue.
  405. * The queue is updated by the SPI driver for the ACI running in the interrupt context
  406. * @param aci_stat pointer to the state of the ACI.
  407. * @param p_aci_data pointer to the ACI Event. The ACI Event received will be copied into this pointer.
  408. * @return True if an ACI Event was copied to the pointer.
  409. */
  410. bool lib_aci_event_get(aci_state_t *aci_stat, hal_aci_evt_t * aci_evt);
  411. /** @brief Flushes the events in the ACI command queues and ACI Event queue
  412. *
  413. */
  414. void lib_aci_flush(void);
  415. //@}
  416. /** @} */
  417. #endif /* LIB_ACI_H__ */