/
BtStack.c
282 lines (250 loc) · 5.32 KB
/
BtStack.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
/**
* \file BtStack.c
* \brief Implements btStack service
* \author George Xian
* \version 0.1
* \date 2014-11-30
*/
#include "BtStack.h"
#include <xdc/runtime/Error.h>
#include <ti/sysbios/knl/Task.h>
#include <xdc/runtime/System.h>
#include <string.h>
#include "Board.h"
#define DEFAULT_RX_PRIORITY 10 //! Default priority of reception task
#define DEFAULT_RX_STACK 2048 //! Default stack size of reception task
#define DEFAULT_UART_BAUD 115200 //! Default baud rate for UART
static Bool hasStart = FALSE; //! Task started status
static Task_Handle rxTask = NULL; //! Handle to the reception task
static int8_t rxPriority = DEFAULT_RX_PRIORITY; //! Priority of reception task
static uint16_t rxStackSize = DEFAULT_RX_STACK; //! Stack size of reception task
static BtStack_Callback rxCallback = NULL; //! Function to call on receive event
static uint32_t uartBaud = DEFAULT_UART_BAUD; //! Baud rate to initiate UART peripheral to
/**
* \brief Function executed by the reception task
*/
void rxFxn(UArg unused0, UArg unused1);
int8_t BtStack_start(void)
{
if (rxTask != NULL)
{
// service already started
return -1;
}
// creating the task
Task_Params params;
Task_Params_init(¶ms);
params.instance->name = "btStack::rx";
params.priority = rxPriority;
params.stackSize = rxStackSize;
Error_Block eb;
Error_init(&eb);
rxTask = Task_create((Task_FuncPtr) rxFxn, ¶ms, &eb);
if (rxTask == NULL)
{
return -2;
}
else
{
hasStart = TRUE;
return 0;
}
}
int8_t BtStack_stop(void)
{
Task_delete(&rxTask);
hasStart = FALSE;
return 0;
}
Bool BtStack_hasStarted(void)
{
return hasStart;
}
Bool BtStack_hasCallback(void)
{
if (rxCallback == NULL)
{
return FALSE;
}
else
{
return TRUE;
}
}
int8_t BtStack_attachCallback(BtStack_Callback callback)
{
if (BtStack_hasCallback())
{
return -1;
}
else
{
rxCallback = callback;
return 0;
}
}
int8_t BtStack_removeCallback(void)
{
if (BtStack_hasCallback())
{
rxCallback = NULL;
return 0;
}
else
{
return -1;
}
}
int8_t BtStack_push(const BtStack_Frame* frame)
{
// special character declarations
const char escapedEnd[] = {SLIP_ESC, SLIP_ESC_END}; // escaped 0xC0
const char escapedEsc[] = {SLIP_ESC, SLIP_ESC_ESC}; // escaped 0xDB
char sendStream[KFP_WORST_SIZE];
uint8_t sentChar = 0;
// append start character
sendStream[0] = SLIP_END;
sentChar++;
// iterate through frame adding ESC characters where necessary
uint8_t i;
for (i=0; i<KFP_FRAME_SIZE-2; i++)
{
switch(frame->b8[i])
{
case(SLIP_END):
// escape END character
strncat(sendStream, escapedEnd, 2);
sentChar+=2;
break;
case(SLIP_ESC):
// escape ESC character
strncat(sendStream, escapedEsc, 2);
sentChar+=2;
break;
default:
sendStream[sentChar] = frame->b8[i];
sentChar++;
}
}
// append end character
sendStream[sentChar] = SLIP_END;
sentChar++;
// prepare UART socket
UART_Handle s;
UART_Params params;
UART_Params_init(¶ms);
params.baudRate = uartBaud;
params.writeMode = UART_MODE_BLOCKING;
params.writeDataMode = UART_DATA_BINARY;
params.readDataMode = UART_DATA_BINARY;
params.readReturnMode = UART_RETURN_FULL;
params.readEcho = UART_ECHO_OFF;
s = UART_open(Board_BT1, ¶ms);
// write to socket and close once complete
int8_t ret = UART_write(s, sendStream, sentChar);
UART_close(s);
return ret;
}
void BtStack_framePrint(const BtStack_Frame* frame, KfpPrintFormat format)
{
System_printf("ID =");
uint8_t i;
for (i=0; i<4; i++)
{
System_printf(" %u", frame->id.b8[i]);
}
System_printf(" Data =");
for (i=0; i<8; i++)
{
if (format == KFPPRINTFORMAT_ASCII)
{
System_printf("%c", frame->payload.b8[i]);
}
else if (format == KFPPRINTFORMAT_HEX)
{
System_printf("%x", frame->payload.b8[i]);
}
}
System_flush();
}
void rxFxn(UArg param0, UArg param1)
{
// Declare state variables
Bool inFrame = FALSE;
uint8_t frIndex = 0;
// Buffers
char tempRx[1];
BtStack_Frame tempFr;
while(TRUE)
{
// open socket
UART_Handle s;
UART_Params params;
UART_Params_init(¶ms);
params.baudRate = uartBaud;
params.writeDataMode = UART_DATA_BINARY;
params.readMode = UART_MODE_BLOCKING;
params.readDataMode = UART_DATA_BINARY;
params.readReturnMode = UART_RETURN_FULL;
params.readEcho = UART_ECHO_OFF;
s = UART_open(Board_BT1, ¶ms);
// read UART buffer and decode
UART_read(s, tempRx, 1);
switch(tempRx[0])
{
case(SLIP_END):
if (inFrame)
{
if (frIndex == (KFP_FRAME_SIZE-2))
{
// end of frame, call callback to interpret it
if (rxCallback != NULL)
{
rxCallback(&tempFr);
}
}
// ignore corrupt frames
frIndex = 0;
inFrame = FALSE;
break;
}
else
{
inFrame = TRUE; // start new frame
frIndex = 0;
break;
}
case(SLIP_ESC):
if (inFrame)
{
UART_read(s, tempRx, 1);
switch(tempRx[0])
{
case(SLIP_ESC_END):
tempFr.b8[frIndex] = SLIP_END;
frIndex++;
break;
case(SLIP_ESC_ESC):
tempFr.b8[frIndex] = SLIP_ESC;
frIndex++;
break;
default:
break; // invalid post ESC character
}
break;
}
else
{
break; // ignore corrupt frames
}
default:
if (inFrame)
{
// standard character store
tempFr.b8[frIndex] = tempRx[0];
frIndex++;
break;
}
}
}
}