-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcom_utils.c
328 lines (263 loc) · 7.13 KB
/
com_utils.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
#include "com_utils.h"
#include "corl8_utils.h"
#include "crc_utils.h"
#include "slcp_ack_cmds.h"
#include "slcp_sensor_cmds.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
uchar* makeSLCPFrame(uchar *data, uchar dataLength)
{
if (!data) {
fprintf(stderr, "No data passed into makeFrame function!\n");
return NULL;
}
if (!dataLength) {
fprintf(stderr, "No dataLength passed into makeFrame function!\n");
return NULL;
}
uchar *frame, *chksum, *payload;
payload = data;
int frameLength = dataLength
+ LENGTH_SLCP_HEADER
+ LENGTH_SLCP_LENGTH
+ LENGTH_SLCP_CHKSUM;
chksum = crc8_frame(data, dataLength);
if (chksum == NULL) {
fprintf(stderr, "CRC8 function returned NULL!\n");
return NULL;
}
frame = (uchar *) malloc(sizeof(uchar) * frameLength);
frame[0] = SLCP_HEADER_BEGIN;
frame[1] = SLCP_HEADER_END;
frame[2] = dataLength;
memcpy(&(frame[3]), payload, dataLength);
frame[frameLength - 1] = *chksum;
return (uchar *) frame;
}
uchar* makeQueryFrame(uchar cmd)
{
// TODO: should check to make sure that cmd is a QUERY cmd
return makeSLCPFrame(&cmd, 1);
}
uchar* sendSLCPFrame(uchar *payload)
{
uchar *ret = 0;
uchar cmd;
if (!payload) {
fprintf(stderr, "sendFrame was passed a NULL payload!\n");
return NULL;
}
cmd = payload[0];
switch (cmd) {
// Don't send commands that we should only be receiving
case CMD_PONG:
case CMD_L8_VOLTAGE_RESPONSE:
case CMD_L8_TEMP_RESPONSE:
case CMD_L8_ACC_RESPONSE:
case CMD_L8_UID_RESPONSE:
case CMD_L8_AMBIENT_RESPONSE:
case CMD_L8_PROX_RESPONSE:
case CMD_L8_VERSIONS_RESPONSE:
case CMD_L8_BUTTON_RESPONSE:
case CMD_L8_MIC_RESPONSE:
case CMD_L8_VBUS_RESPONSE:
case CMD_L8_MCUTEMP_RESPONSE:
case CMD_L8_STORE_L8Y_RESPONSE:
case CMD_L8_READ_L8Y_RESPONSE:
case CMD_L8_STORE_FRAME_RESPONSE:
case CMD_L8_READ_FRAME_RESPONSE:
case CMD_L8_BATCHG_RESPONSE:
case CMD_L8_STORE_ANIM_RESPONSE:
case CMD_L8_READ_ANIM_RESPONSE:
case CMD_L8_ORIENTATION_RESPONSE:
case CMD_L8_NUML8IES_RESPONSE:
case CMD_L8_NUMANIMS_RESPONSE:
case CMD_L8_NUMFRAMES_RESPONSE:
case CMD_L8_NOTIFAPP_RESPONSE:
case CMD_L8_NOTIFAPPS_NUM_RESPONSE:
case CMD_L8_FRAMEGRAB_RESPONSE:
case CMD_L8_SENSORS_THRESHOLDS_RESPONSE:
case CMD_L8_NOTIFAPPS_SILENCE_RESPONSE:
fprintf(stderr, "sendFrame shouldn't be sending a RESPONSE command!\n");
*ret = 1;
break;
default:
*ret = 0;
break;
}
return ret;
}
uchar* readSLCPFrame(uchar *frame)
{
uchar payloadLength;
uchar *chksum, *payload;
if ((frame[0] != SLCP_HEADER_BEGIN) || (frame[1] != SLCP_HEADER_END)) {
fprintf(stderr, "readFrame received a frame with an invalid header!\n");
return NULL;
}
payloadLength = frame[2];
if (payloadLength == 0) {
fprintf(stderr, "readFrame received a frame with an 0 length payload!\n");
return NULL;
}
chksum = crc8_frame(&(frame[3]), payloadLength);
if (chksum == NULL) {
fprintf(stderr, "CRC8 function returned NULL!\n");
return NULL;
} else if (*chksum != frame[2 + payloadLength]) {
fprintf(stderr, "CRC8 of received frame doesn't match calculated CRC8 in readFrame!\n");
return NULL;
}
payload = (uchar *) malloc(sizeof(uchar) * payloadLength);
memcpy(payload, &(frame[3]), payloadLength);
handleReceivedPayload(payload, payloadLength);
return (uchar *) 0;
}
uchar* handleReceivedPayload(uchar *payload, uchar payloadLength)
{
uchar cmd, dataLength;
uchar *data;
uchar *ret = 0;
cmd = payload[0];
data = &(payload[1]);
dataLength = payloadLength - 1;
switch (cmd) {
case CMD_L8_VOLTAGE_RESPONSE:
if (dataLength != CMD_L8_TEMP_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseVoltage(data);
}
break;
case CMD_L8_TEMP_RESPONSE:
if (dataLength != CMD_L8_TEMP_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseTemp(data);
}
break;
case CMD_L8_ACC_RESPONSE:
if (dataLength != CMD_L8_ACC_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseAccel(data);
}
break;
case CMD_L8_UID_RESPONSE:
if (dataLength != CMD_L8_UID_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseUID(data);
}
break;
case CMD_L8_AMBIENT_RESPONSE:
if (dataLength != CMD_L8_AMBIENT_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseAmbient(data);
}
break;
case CMD_L8_PROX_RESPONSE:
if (dataLength != CMD_L8_PROX_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseProximity(data);
}
break;
case CMD_L8_VERSIONS_RESPONSE:
if (dataLength != CMD_L8_VERSIONS_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseVersions(data);
}
break;
case CMD_L8_BUTTON_RESPONSE:
if (dataLength != CMD_L8_BUTTON_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseButton(data);
}
break;
case CMD_L8_MIC_RESPONSE:
if (dataLength != CMD_L8_MIC_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseMic(data);
}
break;
case CMD_L8_VBUS_RESPONSE:
if (dataLength != CMD_L8_VBUS_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseVBUS(data);
}
break;
case CMD_L8_MCUTEMP_RESPONSE:
if (dataLength != CMD_L8_MCUTEMP_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseMCUTemp(data);
}
break;
case CMD_L8_STORE_L8Y_RESPONSE:
case CMD_L8_READ_L8Y_RESPONSE:
case CMD_L8_STORE_FRAME_RESPONSE:
case CMD_L8_READ_FRAME_RESPONSE:
case CMD_L8_BATCHG_RESPONSE:
if (dataLength != CMD_L8_BATCHG_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseBatCharge(data);
}
break;
case CMD_L8_STORE_ANIM_RESPONSE:
case CMD_L8_READ_ANIM_RESPONSE:
case CMD_L8_ORIENTATION_RESPONSE:
if (dataLength != CMD_L8_ORIENTATION_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseOrientation(data);
}
break;
case CMD_L8_NUML8IES_RESPONSE:
case CMD_L8_NUMANIMS_RESPONSE:
case CMD_L8_NUMFRAMES_RESPONSE:
case CMD_L8_NOTIFAPP_RESPONSE:
case CMD_L8_NOTIFAPPS_NUM_RESPONSE:
case CMD_L8_FRAMEGRAB_RESPONSE:
case CMD_L8_SENSORS_THRESHOLDS_RESPONSE:
if (dataLength != CMD_L8_SENSORS_THRESHOLDS_RESPONSE_LENGTH) {
*ret = 1;
} else {
ret = responseSensorThresholds(data);
}
break;
case CMD_L8_NOTIFAPPS_SILENCE_RESPONSE:
break;
case CMD_ERR:
if (dataLength != CMD_ACK_LENGTH) {
*ret = 1;
} else {
ret = handleCMDERR(data);
}
break;
case CMD_OK:
if (dataLength != CMD_ACK_LENGTH) {
*ret = 1;
} else {
ret = handleCMDOK(data);
}
break;
case CMD_PONG:
if (dataLength != 0) {
*ret = 1;
} else {
ret = handleCMDPONG();
}
break;
default:
break;
}
return ret;
}