upm  0.5.1
Sensor/Actuator repository for libmraa (v0.9.1)
 All Data Structures Files Functions Variables Enumerations Enumerator Macros Groups Pages
lib_aci.h
Go to the documentation of this file.
1 /* Copyright (c) 2014, Nordic Semiconductor ASA
2  *
3  * Permission is hereby granted, free of charge, to any person obtaining a copy
4  * of this software and associated documentation files (the "Software"), to deal
5  * in the Software without restriction, including without limitation the rights
6  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7  * copies of the Software, and to permit persons to whom the Software is
8  * furnished to do so, subject to the following conditions:
9  *
10  * The above copyright notice and this permission notice shall be included in all
11  * copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19  * SOFTWARE.
20  */
21 
32 #ifndef LIB_ACI_H__
33 #define LIB_ACI_H__
34 
39 #ifdef __cplusplus
40 extern "C" {
41 #endif
42 
43 #include "hal_platform.h"
44 #include "hal_aci_tl.h"
45 #include "aci_queue.h"
46 #include "aci.h"
47 #include "aci_cmds.h"
48 #include "aci_evts.h"
49 
50 #define EVT_CMD_RESPONSE_MIN_LENGTH 3
51 
52 #define PIPES_ARRAY_SIZE ((ACI_DEVICE_MAX_PIPES + 7)/8)
53 
54 /* Same size as a hal_aci_data_t */
55 typedef struct {
56  uint8_t debug_byte;
57  aci_evt_t evt;
58 } _aci_packed_ hal_aci_evt_t;
59 
60 ACI_ASSERT_SIZE(hal_aci_evt_t, 34);
61 
62 typedef struct
63 {
64  uint8_t location;
65  aci_pipe_type_t pipe_type;
67 
68 typedef struct aci_setup_info_t
69 {
70  services_pipe_type_mapping_t *services_pipe_type_mapping;
71  uint8_t number_of_pipes;
72  hal_aci_data_t *setup_msgs;
73  uint8_t num_setup_msgs;
74 } aci_setup_info_t;
75 
76 
77 
78 // aci_struct that will contain
79 // total initial credits
80 // current credit
81 // current state of the aci (setup/standby/active/sleep)
82 // open remote pipe pending
83 // close remote pipe pending
84 // Current pipe available bitmap
85 // Current pipe closed bitmap
86 // Current connection interval, slave latency and link supervision timeout
87 // Current State of the the GATT client (Service Discovery status)
88 // Relationship of bond to peer address
89 typedef struct aci_state_t
90 {
91  aci_pins_t aci_pins; /* Pins on the MCU used to connect to the nRF8001 */
92  aci_setup_info_t aci_setup_info; /* Data structures that are created from nRFgo Studio */
93  uint8_t bonded; /* ( aci_bond_status_code_t ) Is the nRF8001 bonded to a peer device */
94  uint8_t data_credit_total; /* Total data credit available for the specific version of the nRF8001, total equals available when a link is established */
95  aci_device_operation_mode_t device_state; /* Operating mode of the nRF8001 */
96 
97  /* */
98 
99  /* Start : Variables that are valid only when in a connection */
100  uint8_t data_credit_available; /* Available data credits at a specific point of time, ACI_EVT_DATA_CREDIT updates the available credits */
101 
102  uint16_t connection_interval; /* Multiply by 1.25 to get the connection interval in milliseconds*/
103  uint16_t slave_latency; /* Number of consecutive connection intervals that the nRF8001 is not required to transmit. Use this to save power */
104  uint16_t supervision_timeout; /* Multiply by 10 to get the supervision timeout in milliseconds */
105 
106  uint8_t pipes_open_bitmap[PIPES_ARRAY_SIZE]; /* Bitmap -> pipes are open and can be used for sending data over the air */
107  uint8_t pipes_closed_bitmap[PIPES_ARRAY_SIZE]; /* Bitmap -> pipes are closed and cannot be used for sending data over the air */
108  bool confirmation_pending; /* Attribute protocol Handle Value confirmation is pending for a Handle Value Indication
109  (ACK is pending for a TX_ACK pipe) on local GATT Server*/
110  /* End : Variables that are valid only when in a connection */
111 
112 } aci_state_t;
113 
114 
115 
116 #define DISCONNECT_REASON_CX_TIMEOUT 0x08
117 #define DISCONNECT_REASON_CX_CLOSED_BY_PEER_DEVICE 0x13
118 #define DISCONNECT_REASON_POWER_LOSS 0x14
119 #define DISCONNECT_REASON_CX_CLOSED_BY_LOCAL_DEVICE 0x16
120 #define DISCONNECT_REASON_ADVERTISER_TIMEOUT 0x50
121 
122 
125 
130 void lib_aci_debug_print(bool enable);
131 
135 void lib_aci_pin_reset(void);
136 
144 void lib_aci_init(aci_state_t *aci_stat, bool debug);
145 
146 
150 uint8_t lib_aci_get_nb_available_credits(aci_state_t *aci_stat);
151 
155 uint16_t lib_aci_get_cx_interval_ms(aci_state_t *aci_stat);
156 
160 uint16_t lib_aci_get_cx_interval(aci_state_t *aci_stat);
161 
165 uint16_t lib_aci_get_slave_latency(aci_state_t *aci_stat);
166 
171 bool lib_aci_is_pipe_available(aci_state_t *aci_stat, uint8_t pipe);
172 
177 bool lib_aci_is_pipe_closed(aci_state_t *aci_stat, uint8_t pipe);
178 
182 bool lib_aci_is_discovery_finished(aci_state_t *aci_stat);
183 
184 
185 
187 
190 
197 bool lib_aci_sleep(void);
198 
203 bool lib_aci_radio_reset(void);
204 
209 bool lib_aci_direct_connect(void);
210 
215 bool lib_aci_device_version(void);
216 
221 bool lib_aci_get_address(void);
222 
228 bool lib_aci_get_temperature(void);
229 
234 bool lib_aci_get_battery_level(void);
235 
237 
240 
247 bool lib_aci_wakeup(void);
248 
250 
253 
264 bool lib_aci_test(aci_test_mode_change_t enter_exit_test_mode);
265 
271 bool lib_aci_set_tx_power(aci_device_output_power_t tx_power);
272 
279 bool lib_aci_connect(uint16_t run_timeout, uint16_t adv_interval);
280 
287 bool lib_aci_bond(uint16_t run_timeout, uint16_t adv_interval);
288 
294 bool lib_aci_disconnect(aci_state_t *aci_stat, aci_disconnect_reason_t reason);
295 
306 bool lib_aci_set_local_data(aci_state_t *aci_stat, uint8_t pipe, uint8_t *value, uint8_t size);
307 
317 bool lib_aci_broadcast(const uint16_t timeout, const uint16_t adv_interval);
318 
330 bool lib_aci_open_adv_pipe(const uint8_t pipe);
331 
332 
346 bool lib_aci_open_adv_pipes(const uint8_t * const adv_service_data_pipes);
347 
348 
350 
353 
354 
359 bool lib_aci_set_app_latency(uint16_t latency, aci_app_latency_mode_t latency_mode);
360 
366 bool lib_aci_open_remote_pipe(aci_state_t *aci_stat, uint8_t pipe);
367 
373 bool lib_aci_close_remote_pipe(aci_state_t *aci_stat, uint8_t pipe);
374 
384 bool lib_aci_send_data(uint8_t pipe, uint8_t *value, uint8_t size);
385 
394 bool lib_aci_request_data(aci_state_t *aci_stat, uint8_t pipe);
395 
409 bool lib_aci_change_timing(uint16_t minimun_cx_interval, uint16_t maximum_cx_interval, uint16_t slave_latency, uint16_t timeout);
410 
423 
431 bool lib_aci_send_ack(aci_state_t *aci_stat, const uint8_t pipe);
432 
440 bool lib_aci_send_nack(aci_state_t *aci_stat, const uint8_t pipe, const uint8_t error_code);
441 
452 bool lib_aci_read_dynamic_data(void);
453 
467 bool lib_aci_write_dynamic_data(uint8_t sequence_number, uint8_t* dynamic_data, uint8_t length);
469 
472 
479 bool lib_aci_bond_request(void);
480 
488 bool lib_aci_set_key(aci_key_type_t key_rsp_type, uint8_t *key, uint8_t len);
489 
491 
492 
493 
496 
504 bool lib_aci_echo_msg(uint8_t message_size, uint8_t *message_data);
505 
512 bool lib_aci_dtm_command(uint8_t dtm_command_msbyte, uint8_t dtm_command_lsbyte);
513 
521 bool lib_aci_event_get(aci_state_t *aci_stat, hal_aci_evt_t * aci_evt);
522 
530 bool lib_aci_event_peek(hal_aci_evt_t *p_aci_evt_data);
531 
535 void lib_aci_flush(void);
536 
541  bool lib_aci_event_queue_full(void);
542 
547  bool lib_aci_event_queue_empty(void);
548 
553  bool lib_aci_command_queue_full(void);
554 
559  bool lib_aci_command_queue_empty(void);
560 
562 
565 #ifdef __cplusplus
566 }
567 #endif
568 
569 #endif /* LIB_ACI_H__ */
bool lib_aci_broadcast(const uint16_t timeout, const uint16_t adv_interval)
Sends Broadcast message to the radio.
Definition: lib_aci.cpp:641
Definition: lib_aci.h:62
bool lib_aci_get_address(void)
Gets the device address.
Definition: lib_aci.cpp:362
bool lib_aci_change_timing(uint16_t minimun_cx_interval, uint16_t maximum_cx_interval, uint16_t slave_latency, uint16_t timeout)
Sends a L2CAP change connection parameters request.
Definition: lib_aci.cpp:437
bool lib_aci_device_version(void)
Gets the radio's version.
Definition: lib_aci.cpp:277
uint16_t lib_aci_get_cx_interval(aci_state_t *aci_stat)
Gets the connection interval in multiple of 1.25 ms.
Definition: lib_aci.cpp:223
void lib_aci_flush(void)
Flushes the events in the ACI command queues and ACI Event queue.
Definition: lib_aci.cpp:708
uint8_t lib_aci_get_nb_available_credits(aci_state_t *aci_stat)
Gets the number of currently available ACI credits.
Definition: lib_aci.cpp:205
bool lib_aci_bond(uint16_t run_timeout, uint16_t adv_interval)
Tries to bond with a peer device.
Definition: lib_aci.cpp:336
void lib_aci_pin_reset(void)
Function to pin reset the nRF8001.
Definition: lib_aci.cpp:719
bool lib_aci_set_key(aci_key_type_t key_rsp_type, uint8_t *key, uint8_t len)
Set the key requested by the 8001.
Definition: lib_aci.cpp:510
bool lib_aci_event_get(aci_state_t *aci_stat, hal_aci_evt_t *aci_evt)
Gets an ACI event from the ACI Event Queue.
Definition: lib_aci.cpp:551
bool lib_aci_read_dynamic_data(void)
Sends ReadDynamicData command to the host.
Definition: lib_aci.cpp:686
bool lib_aci_event_peek(hal_aci_evt_t *p_aci_evt_data)
Peeks an ACI event from the ACI Event Queue.
Definition: lib_aci.cpp:546
Definitions for the ACI (Application Control Interface) commands.
bool lib_aci_wakeup(void)
Wakes up the radio.
Definition: lib_aci.cpp:346
bool lib_aci_close_remote_pipe(aci_state_t *aci_stat, uint8_t pipe)
Closes a remote pipe.
Definition: lib_aci.cpp:483
uint16_t lib_aci_get_cx_interval_ms(aci_state_t *aci_stat)
Gets the connection interval in milliseconds.
Definition: lib_aci.cpp:210
bool lib_aci_direct_connect(void)
Radio starts directed advertising to bonded device.
Definition: lib_aci.cpp:270
bool lib_aci_command_queue_full(void)
Return full status of Command queue.
Definition: lib_aci.cpp:739
bool lib_aci_send_nack(aci_state_t *aci_stat, const uint8_t pipe, const uint8_t error_code)
Sends negative acknowledgement message to peer.
Definition: lib_aci.cpp:628
bool lib_aci_disconnect(aci_state_t *aci_stat, aci_disconnect_reason_t reason)
Disconnects from peer device.
Definition: lib_aci.cpp:311
HAL specific macros.
bool lib_aci_open_adv_pipes(const uint8_t *const adv_service_data_pipes)
Sends a command to the radio to set the pipes to be placed in Advertisement Service Data...
Definition: lib_aci.cpp:663
bool lib_aci_write_dynamic_data(uint8_t sequence_number, uint8_t *dynamic_data, uint8_t length)
Sends WriteDynamicData command to the host.
Definition: lib_aci.cpp:693
bool lib_aci_connect(uint16_t run_timeout, uint16_t adv_interval)
Tries to connect to a peer device.
Definition: lib_aci.cpp:301
bool lib_aci_dtm_command(uint8_t dtm_command_msbyte, uint8_t dtm_command_lsbyte)
Sends an DTM command.
Definition: lib_aci.cpp:699
bool lib_aci_change_timing_GAP_PPCP(void)
Sends a L2CAP change connection parameters request with the connection predefined preffered connectio...
Definition: lib_aci.cpp:449
bool lib_aci_test(aci_test_mode_change_t enter_exit_test_mode)
Sets the radio in test mode.
Definition: lib_aci.cpp:247
Interface for hal_aci_tl.
bool lib_aci_request_data(aci_state_t *aci_stat, uint8_t pipe)
Requests data from a given pipe.
Definition: lib_aci.cpp:410
bool lib_aci_is_discovery_finished(aci_state_t *aci_stat)
Checks if the discovery operation is finished.
Definition: lib_aci.cpp:96
bool lib_aci_bond_request(void)
Sends a SMP Security Request.
Definition: lib_aci.cpp:540
bool lib_aci_echo_msg(uint8_t message_size, uint8_t *message_data)
Sends an echo message.
Definition: lib_aci.cpp:520
bool lib_aci_radio_reset(void)
Resets the radio.
Definition: lib_aci.cpp:263
bool lib_aci_is_pipe_closed(aci_state_t *aci_stat, uint8_t pipe)
Checks if a given pipe is closed.
Definition: lib_aci.cpp:83
bool lib_aci_is_pipe_available(aci_state_t *aci_stat, uint8_t pipe)
Checks if a given pipe is available.
Definition: lib_aci.cpp:70
bool lib_aci_event_queue_full(void)
Return full status of the Event queue.
Definition: lib_aci.cpp:729
uint16_t lib_aci_get_slave_latency(aci_state_t *aci_stat)
Gets the current slave latency.
Definition: lib_aci.cpp:229
bool lib_aci_event_queue_empty(void)
Return empty status of the Event queue.
Definition: lib_aci.cpp:724
void lib_aci_debug_print(bool enable)
Function to enable printing of all ACI commands sent and ACI events received.
Definition: lib_aci.cpp:713
bool lib_aci_get_battery_level(void)
Gets the battery level.
Definition: lib_aci.cpp:376
bool lib_aci_set_app_latency(uint16_t latency, aci_app_latency_mode_t latency_mode)
Sets a given application latency.
Definition: lib_aci.cpp:235
bool lib_aci_send_ack(aci_state_t *aci_stat, const uint8_t pipe)
Sends acknowledgement message to peer.
Definition: lib_aci.cpp:616
bool lib_aci_set_tx_power(aci_device_output_power_t tx_power)
Sets the radio's TX power.
Definition: lib_aci.cpp:353
bool lib_aci_sleep(void)
Sets the radio in sleep mode.
Definition: lib_aci.cpp:256
bool lib_aci_set_local_data(aci_state_t *aci_stat, uint8_t pipe, uint8_t *value, uint8_t size)
Sets Local Data.
Definition: lib_aci.cpp:284
uint8_t location
Definition: lib_aci.h:64
bool lib_aci_send_data(uint8_t pipe, uint8_t *value, uint8_t size)
Sends data on a given pipe.
Definition: lib_aci.cpp:383
bool lib_aci_open_adv_pipe(const uint8_t pipe)
Sends a command to the radio to set the input pipe to be placed in Advertisement Service Data...
Definition: lib_aci.cpp:676
Structure for the ACI_CMD_SET_LOCAL_DATA ACI command parameters.
Definition: aci.h:271
bool lib_aci_open_remote_pipe(aci_state_t *aci_stat, uint8_t pipe)
Opens a remote pipe.
Definition: lib_aci.cpp:456
Definitions for the ACI (Application Control Interface) events.
Interface for buffer.
void lib_aci_init(aci_state_t *aci_stat, bool debug)
Initialization function.
Definition: lib_aci.cpp:179
bool lib_aci_get_temperature(void)
Gets the temperature.
Definition: lib_aci.cpp:369
bool lib_aci_command_queue_empty(void)
Return empty status of Command queue.
Definition: lib_aci.cpp:734