-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlcr_packetizer.c
509 lines (411 loc) · 9.38 KB
/
lcr_packetizer.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
/*****************************************************************************
** TEXAS INSTRUMENTS PROPRIETARY INFORMATION
**
** (c) Copyright, Texas Instruments Incorporated, 2012-2013.
** All Rights Reserved.
**
** Property of Texas Instruments Incorporated. Restricted Rights -
** Use, duplication, or disclosure is subject to restrictions set
** forth in TI's program license agreement and associated documentation.
******************************************************************************/
/**
*
* @file lcr_packetizer.c
*
* @brief This file prepares and decodes the packet data for transaction with
* with DM365 over TCP link
**/
/*****************************************************************************/
#include "common.h"
#include "error.h"
#include "tcp_client.h"
#include "lcr_packetizer.h"
static uint8 packetBuffer[HEADER_SIZE + MAX_PACKET_SIZE + CHECKSUM_SIZE];
static uint8 * const packetData = packetBuffer + HEADER_SIZE;
static uint8 LCR_PacketType;
static uint8 contFlag;
static uint8 recvFlag;
static uint16 commandId;
static uint16 dataLength;
static uint16 parseIndex;
static int LCR_PKT_Socket;
static uint8 LCR_CMD_PKT_CalcChecksum(void);
/**********************************************************/
/* Low level functions for transfer of data over TCP link */
/**********************************************************/
/* Connects to the DLP LightCrafter(TM) Hardware */
ErrorCode_t LCR_CMD_PKT_ConnectToLCR(void)
{
/* Open tcp connection with LCr */
LCR_PKT_Socket = TCP_Connect(LCR_CMD_IP, LCR_CMD_PORT);
if(LCR_PKT_Socket < 0)
return FAIL;
return SUCCESS;
}
/*Close the TCP Connection*/
ErrorCode_t LCR_CMD_PKT_DisconnectLCR(void)
{
TCP_Disconnect(LCR_PKT_Socket);
return SUCCESS;
}
/**********************************************************/
/* Low level functions for transfer of data over TCP link */
/**********************************************************/
int LCR_CMD_ReadData(uint8 *data, uint32 size)
{
return TCP_Receive(LCR_PKT_Socket, data, size);
}
int LCR_CMD_WriteData(uint8 *data, uint32 size)
{
return TCP_Send(LCR_PKT_Socket, data, size);
}
/*****************************/
/* Packet Creation functions */
/*****************************/
/* Initialize the read/write command */
int LCR_CMD_PKT_CommandInit(LCR_CommandType_t cmdType, uint16 cmdId)
{
if(cmdType == LCR_CMD_PKT_TYPE_WRITE)
LCR_PacketType = PKT_TYPE_WRITE;
else if(cmdType == LCR_CMD_PKT_TYPE_READ)
LCR_PacketType = PKT_TYPE_READ;
else
return -1;
contFlag = 0;
commandId = cmdId;
dataLength = 0;
return 0;
}
/* Add raw data to the command */
int LCR_CMD_PKT_PutData(uint8 *data, unsigned long int size)
{
if(data == NULL)
return -1;
while(size)
{
int copySize = size;
if(dataLength == MAX_PACKET_SIZE)
{
if(LCR_CMD_PKT_SendPacket(1))
return -1;
dataLength = 0;
}
if(dataLength + copySize > MAX_PACKET_SIZE)
{
copySize = MAX_PACKET_SIZE - dataLength;
}
memcpy(packetData + dataLength, data, copySize);
dataLength += copySize;
size -= copySize;
}
return 0;
}
/* Add the initiger data to the command */
int LCR_CMD_PKT_PutInt(uint32 value, unsigned int size)
{
uint8 data[4];
unsigned int i;
if(size > 4)
return -1;
for(i = 0; i < size; i++)
{
data[i] = (value & 0xFF);
value >>= 8;
}
if(LCR_CMD_PKT_PutData(data, size))
return -1;
return 0;
}
/* Add the file content to the command */
int LCR_CMD_PKT_PutFile(char const *fileName)
{
int error = 0;
FILE *fp;
int copySize;
fp = fopen(fileName, "rb");
if(fp == NULL)
return -1;
while(!feof(fp))
{
if(dataLength == MAX_PACKET_SIZE)
{
if(LCR_CMD_PKT_SendPacket(1))
{
error = -1;
break;
}
}
copySize = MAX_PACKET_SIZE - dataLength;
copySize = fread(packetData + dataLength,
1, copySize, fp);
if(copySize <= 0)
break;
dataLength += copySize;
}
fclose(fp);
return error;
}
/*Copied returened data into the <fileName>*/
int LCR_CMD_PKT_GetFile(char const *fileName,uint32 size)
{
FILE *fp;
int ret = 0;
uint32 remSize = size;
if(packetBuffer[0] != PKT_TYPE_READ_RESP)
{
return -1;
}
fp = fopen(fileName, "wb");
if(fp == NULL)
{
return -1;
}
if(remSize == 0)
remSize = MAX_PACKET_SIZE;
while(remSize)
{
unsigned int copySize = dataLength - parseIndex;
if(copySize == 0)
{
if(LCR_CMD_PKT_ReceivePacket(0))
{
if(size != 0)
ret = -1;
break;
}
copySize = dataLength - parseIndex;
}
if(copySize >= remSize)
{
copySize = remSize;
}
if(fwrite(packetData + parseIndex, 1, copySize, fp) != copySize)
{
ret = -1;
break;
}
parseIndex += copySize;
if(size != 0)
remSize -= copySize;
}
fclose(fp);
return ret;
}
/* Get raw data from the received packet */
int LCR_CMD_PKT_GetData(uint8 *data, unsigned long int size)
{
if(packetBuffer[0] != PKT_TYPE_READ_RESP)
{
return -1;
}
while(size)
{
unsigned int copySize = dataLength - parseIndex;
if(copySize == 0)
{
if(LCR_CMD_PKT_ReceivePacket(0))
return -1;
copySize = dataLength - parseIndex;
}
if(copySize >= size)
{
copySize = size;
}
memcpy(data, packetData + parseIndex, copySize);
parseIndex += copySize;
size -= copySize;
data += copySize;
}
return 0;
}
/* Get integer data from the received packet */
uint32 LCR_CMD_PKT_GetInt(unsigned int size)
{
uint32 value = 0;
uint8 data[4];
unsigned int i;
if(size > 4)
return 0;
if(LCR_CMD_PKT_GetData(data, size))
return 0;
for(i = 0; i < size; i++)
{
value |= data[i] << (i * 8);
}
return value;
}
/* Calculate the packet checksum to be added at the end */
uint8 LCR_CMD_PKT_CalcChecksum(void)
{
int i;
int sum = 0;
for(i = 0; i < dataLength + HEADER_SIZE; i++)
{
sum += packetBuffer[i];
}
return (uint8)(sum & 0xFF);
}
/***********************************************************/
/* Sending and receiving the Packets after packet creation */
/***********************************************************/
/* Receive one packet of the responce */
int LCR_CMD_PKT_ReceivePacket(BOOL firstPkt)
{
unsigned long int mask;
int i;
dataLength = 0;
parseIndex = 0;
if(firstPkt == 0)
{
if(recvFlag == 0 || recvFlag == 3)
{
return -1;
}
}
if(LCR_CMD_ReadData(packetBuffer, HEADER_SIZE))
{
return -1;
}
dataLength = packetBuffer[4] | packetBuffer[5] << 8;
if(LCR_CMD_ReadData(packetData, dataLength + 1))
{
return -1;
}
if(packetData[dataLength] != LCR_CMD_PKT_CalcChecksum())
{
printf("ERROR: Checksum failed in the commands response packet!!!\n");
return -1;
}
if(packetBuffer[0] != LCR_PacketType + 1)
{
//Determine the response packet
switch(packetBuffer[0])
{
case 0x00:
printf("INFO: Command NOT executed; DM365 is BUSY\n");
break;
case 0x01:
printf("INFO: Command packet has ERROR in it\n");
//Set flag based on the error #
mask = 0;
i = 0;
while(dataLength--)
{
if(packetBuffer[(6+i)] != 0)
{
mask |= (1<<(packetBuffer[(6+i)] - 1));
}
i++;
}
//print each error
if((mask & 0x01) == 0x01)
{
printf("COMMAND PACKET ERROR: Commmand Failed!!!\n");
}
else if((mask & 0x02) == 0x02)
{
printf("COMMAND PACKET ERROR: Unsupported Command!!!\n");
}
else if((mask & 0x04) == 0x04)
{
printf("COMMAND PACKET ERROR: Invalid Parameter!!!\n");
}
else if((mask & 0x08) == 0x08)
{
printf("COMMAND PACKET ERROR: Out Of Resource!!!\n");
}
else if((mask & 0x10) == 0x10)
{
printf("COMMAND PACKET ERROR: Device Failed!!!\n");
}
else if((mask & 0x20) == 0x20)
{
printf("COMMAND PACKET ERROR: Device Busy!!!\n");
}
else if((mask & 0x40) == 0x40)
{
printf("COMMAND PACKET ERROR: Not Initialized!!!\n");
}
else if((mask & 0x80) == 0x80)
{
printf("COMMAND PACKET ERROR: Not Found!!!\n");
}
else if((mask & 0x100) == 0x100)
{
printf("COMMAND PACKET ERROR: Checksum Error!!!\n");
}
else if((mask & 0x200) == 0x200)
{
printf("COMMAND PACKET ERROR: Packet Format Error!!!\n");
}
else if((mask & 0x400) == 0x400)
{
printf("COMMAND PACKET ERROR: Command Continuation Error!!!\n");
}
else
{
printf("COMMAND PACKET ERROR: DM365 returned undocumented Error!!!\n");
}
break;
case 0x02:
case 0x04:
printf("COMMAND PACKET ERROR: DM365 sent back command type as Write = [0x02] or Read = [0x04] command!!!\n");
break;
default:
printf("ERROR: Unkknown packet type information received in response packet!!!\n");
break;
}
return -1;
}
recvFlag = packetBuffer[3];
if(firstPkt != (recvFlag == 0 || recvFlag == 1))
{
return -1;
}
if(recvFlag == 3)
{
/* Command SUCCESS */
}
return 0;
}
/* Send the command paket just initialized */
int LCR_CMD_PKT_SendPacket(BOOL more)
{
uint8 flag;
packetBuffer[0] = LCR_PacketType;
packetBuffer[1] = (commandId >> 8) & 0xFF;
packetBuffer[2] = (commandId) & 0xFF;
if(contFlag)
{
if(more)
flag = 2;
else
flag = 3;
}
else
{
if(more)
flag = 1;
else
flag = 0;
}
contFlag = more;
packetBuffer[3] = flag;
packetBuffer[4] = dataLength & 0xFF;
packetBuffer[5] = (dataLength >> 8) & 0xFF;
packetData[dataLength] = LCR_CMD_PKT_CalcChecksum();
int length = dataLength + HEADER_SIZE + CHECKSUM_SIZE;
if(LCR_CMD_WriteData(packetBuffer, length))
return -1;
if(LCR_CMD_PKT_ReceivePacket(1))
return -1;
if(more == 0 && recvFlag == 0)
{
/* SUCCESS */
}
return 0;
}
int LCR_CMD_PKT_SendCommand(void)
{
return LCR_CMD_PKT_SendPacket(0);
}