-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathuart_aci.c
332 lines (290 loc) · 11.5 KB
/
uart_aci.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
/* Copyright (c) 2014, Nordic Semiconductor ASA
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
#include "uart_aci.h"
/**
Put the nRF8001 setup in the RAM of the nRF8001.
*/
#include "services.h"
/**
Include the services_lock.h to put the setup in the OTP memory of the nRF8001.
This would mean that the setup cannot be changed once put in.
However this removes the need to do the setup of the nRF8001 on every reset.
*/
#include "ble_uart.h"
char txt[20];
/*
Externally defined in uart_aci.h
*/
struct aci_state_t aci_state;
/*
Temporary buffers for sending ACI commands
*/
static hal_aci_evt_t aci_data;
/*
Timing change state variable
*/
static bool timing_change_done = false;
/*
Used to test the UART TX characteristic notification
*/
static uart_over_ble_t uart_over_ble;
/* Define how assert should function in the BLE library */
void __ble_assert(const char *file, uint16_t line)
{
PRINT("BLE UART ERROR");
PRINT(file);
PRINT(": ");
PRINT(line);
PRINT("\n");
while(1);
}
bool uart_process_control_point_rx(uint8_t *byte, uint8_t length)
{
bool status = false;
aci_ll_conn_params_t *conn_params;
if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_TX) )
{
PRINT_BASE(*byte, HEX);
switch(*byte)
{
/*
Queues a ACI Disconnect to the nRF8001 when this packet is received.
May cause some of the UART packets being sent to be dropped
*/
case UART_OVER_BLE_DISCONNECT:
/*
Parameters:
None
*/
lib_aci_disconnect(&aci_state, ACI_REASON_TERMINATE);
status = true;
break;
/*
Queues an ACI Change Timing to the nRF8001
*/
case UART_OVER_BLE_LINK_TIMING_REQ:
/*
Parameters:
Connection interval min: 2 bytes
Connection interval max: 2 bytes
Slave latency: 2 bytes
Timeout: 2 bytes
Same format as Peripheral Preferred Connection Parameters (See nRFgo studio -> nRF8001 Configuration -> GAP Settings
Refer to the ACI Change Timing Request in the nRF8001 Product Specifications
*/
conn_params = (aci_ll_conn_params_t *)(byte+1);
lib_aci_change_timing( conn_params->min_conn_interval,
conn_params->max_conn_interval,
conn_params->slave_latency,
conn_params->timeout_mult);
status = true;
break;
/*
Clears the RTS of the UART over BLE
*/
case UART_OVER_BLE_TRANSMIT_STOP:
/*
Parameters:
None
*/
uart_over_ble.uart_rts_local = false;
status = true;
break;
/*
Set the RTS of the UART over BLE
*/
case UART_OVER_BLE_TRANSMIT_OK:
/*
Parameters:
None
*/
uart_over_ble.uart_rts_local = true;
status = true;
break;
}
}
return status;
}
void aci_loop()
{
static bool setup_required = false;
uint8_t counter;
// We enter the if statement only when there is a ACI event available to be processed
if (lib_aci_event_get(&aci_state, &aci_data))
{
aci_evt_t * aci_evt;
aci_evt = &aci_data.evt;
switch(aci_evt->evt_opcode)
{
/**
As soon as you reset the nRF8001 you will get an ACI Device Started Event
*/
case ACI_EVT_DEVICE_STARTED:
{
aci_state.data_credit_total = aci_evt->params.device_started.credit_available;
switch(aci_evt->params.device_started.device_mode)
{
case ACI_DEVICE_SETUP:
/**
When the device is in the setup mode
*/
UART1_Write_Text(" Event device started: Setup ");
setup_required = true;
break;
case ACI_DEVICE_STANDBY:
UART1_Write_Text(" Evt Device Started: Standby ");
//Looking for an iPhone by sending radio advertisements
//When a device connects to us we will get an ACI_EVT_CONNECTED event from the nRF8001
if (aci_evt->params.device_started.hw_error)
{
Delay_ms(20); //Handle the HW error event correctly.
}
else
{
lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/);
UART1_Write_Text(" Advertising started : Tap Connect on the nRF UART app ");
}
break;
default:
break;
}
}
break; //ACI Device Started Event
case ACI_EVT_CMD_RSP:
//If an ACI command response event comes with an error -> stop
if (ACI_STATUS_SUCCESS != aci_evt->params.cmd_rsp.cmd_status)
{
//ACI ReadDynamicData and ACI WriteDynamicData will have status codes of
//TRANSACTION_CONTINUE and TRANSACTION_COMPLETE
//all other ACI commands will have status code of ACI_STATUS_SCUCCESS for a successful command
UART1_Write_Text("ACI Command ");
sprinti(txt,"%d",aci_evt->params.cmd_rsp.cmd_opcode);
UART1_Write_Text(txt);
UART1_Write_Text("Evt Cmd respone: Status ");
sprinti(txt,"%d",aci_evt->params.cmd_rsp.cmd_status);
UART1_Write_Text(txt);
}
if (ACI_CMD_GET_DEVICE_VERSION == aci_evt->params.cmd_rsp.cmd_opcode)
{
//Store the version and configuration information of the nRF8001 in the Hardware Revision String Characteristic
lib_aci_set_local_data(&aci_state, PIPE_DEVICE_INFORMATION_HARDWARE_REVISION_STRING_SET,
(uint8_t *)&(aci_evt->params.cmd_rsp.params.get_device_version), sizeof(aci_evt_cmd_rsp_params_get_device_version_t));
}
break;
case ACI_EVT_CONNECTED:
UART1_Write_Text(" Evt Connected ");
timing_change_done = false;
aci_state.data_credit_available = aci_state.data_credit_total;
/*
Get the device version of the nRF8001 and store it in the Hardware Revision String
*/
lib_aci_device_version();
break;
case ACI_EVT_PIPE_STATUS:
UART1_Write_Text(" Evt Pipe Status ");
if (lib_aci_is_pipe_available(&aci_state, PIPE_UART_OVER_BTLE_UART_TX_TX) && (false == timing_change_done))
{
char hello[]="hello android!";
lib_aci_change_timing_GAP_PPCP(); // change the timing on the link as specified in the nRFgo studio -> nRF8001 conf. -> GAP.
// Used to increase or decrease bandwidth
timing_change_done = true;
ble_uart_tx((uint8_t *)&hello[0], strlen(hello));
UART1_Write_Text(" Sending: ");
UART1_Write_Text(hello);
}
break;
case ACI_EVT_TIMING:
UART1_Write_Text(" Evt link connection interval changed ");
lib_aci_set_local_data(&aci_state,
PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET,
(uint8_t *)&(aci_evt->params.timing.conn_rf_interval), /* Byte aligned */
PIPE_UART_OVER_BTLE_UART_LINK_TIMING_CURRENT_SET_MAX_SIZE);
break;
case ACI_EVT_DISCONNECTED:
UART1_Write_Text(" Evt Disconnected/Advertising timed out ");
lib_aci_connect(0/* in seconds : 0 means forever */, 0x0050 /* advertising interval 50ms*/);
UART1_Write_Text(" Advertising started. Tap Connect on the nRF UART app ");
break;
case ACI_EVT_DATA_RECEIVED:
if (PIPE_UART_OVER_BTLE_UART_RX_RX == aci_evt->params.data_received.rx_data.pipe_number)
{
// Sending received data to the user callback function
ble_uart_rx(&aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2);
}
if (PIPE_UART_OVER_BTLE_UART_CONTROL_POINT_RX == aci_evt->params.data_received.rx_data.pipe_number)
{
uart_process_control_point_rx(&aci_evt->params.data_received.rx_data.aci_data[0], aci_evt->len - 2); //Subtract for Opcode and Pipe number
}
break;
case ACI_EVT_DATA_CREDIT:
aci_state.data_credit_available = aci_state.data_credit_available + aci_evt->params.data_credit.credit;
break;
case ACI_EVT_PIPE_ERROR:
//See the appendix in the nRF8001 Product Specification for details on the error codes
UART1_Write_Text(" ACI Evt Pipe Error: Pipe #: ");
sprinti(txt,"%d",aci_evt->params.pipe_error.pipe_number);
UART1_Write_Text(txt);
UART1_Write_Text(" Pipe Error Code: 0x");
sprinti(txt,"%d",aci_evt->params.pipe_error.error_code);
UART1_Write_Text(txt);
//Increment the credit available as the data packet was not sent.
//The pipe error also represents the Attribute protocol Error Response sent from the peer and that should not be counted
//for the credit.
if (ACI_STATUS_ERROR_PEER_ATT_ERROR != aci_evt->params.pipe_error.error_code)
{
aci_state.data_credit_available++;
}
break;
case ACI_EVT_HW_ERROR:
UART1_Write_Text(" HW error: ");
sprinti(txt,"%d",aci_evt->params.hw_error.line_num);
UART1_Write_Text(txt);
for(counter = 0; counter <= (aci_evt->len - 3); counter++)
{
PRINT(aci_evt->params.hw_error.file_name[counter]); //uint8_t file_name[20];
}
UART1_Write_Text("\n");
lib_aci_connect(0/* in seconds, 0 means forever */, 0x0050 /* advertising interval 50ms*/);
UART1_Write_Text(" Advertising started. Tap Connect on the nRF UART app ");
break;
default:
break;
}
}
else
{
//UART1_Write_Text("No ACI Events available");
// No event in the ACI Event queue and if there is no event in the ACI command queue the device can go to sleep
// The device can go to sleep now
// Wakeup from sleep from the RDYN line
}
/* setup_required is set to true when the device starts up and enters setup mode.
* It indicates that do_aci_setup() should be called. The flag should be cleared if
* do_aci_setup() returns ACI_STATUS_TRANSACTION_COMPLETE.
*/
if(setup_required)
{
if (SETUP_SUCCESS == do_aci_setup(&aci_state))
{
setup_required = false;
}
}
}