Subversion Repositories seema-scanner

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 jakw 1
/*****************************************************************************
2
**             TEXAS INSTRUMENTS PROPRIETARY INFORMATION
3
**
4
**  (c) Copyright, Texas Instruments Incorporated, 2012-2013.
5
**      All Rights Reserved.
6
**
7
**  Property of Texas Instruments Incorporated. Restricted Rights -
8
**  Use, duplication, or disclosure is subject to restrictions set
9
**  forth in TI's program license agreement and associated documentation.
10
******************************************************************************/
11
/**
12
*
13
* @file    lcr_packetizer.c
14
*
15
* @brief	This file prepares and decodes the packet data for transaction with
16
*			with DM365 over TCP link
17
**/
18
/*****************************************************************************/
19
 
20
 
21
#include "common.h"
22
#include "error.h"
23
#include "tcp_client.h"
24
#include "lcr_packetizer.h"
25
 
26
 
27
static uint8 packetBuffer[HEADER_SIZE + MAX_PACKET_SIZE + CHECKSUM_SIZE];
28
static uint8 * const packetData = packetBuffer + HEADER_SIZE;
29
static uint8 LCR_PacketType;
30
static uint8 contFlag;
31
static uint8 recvFlag;
32
static uint16 commandId;
33
static uint16 dataLength;
34
static uint16 parseIndex;
35
static int LCR_PKT_Socket;
36
 
37
static uint8 LCR_CMD_PKT_CalcChecksum(void);
38
 
39
 
40
 
41
/**********************************************************/
42
/* Low level functions for transfer of data over TCP link */
43
/**********************************************************/
44
 
45
/* Connects to the DLP LightCrafter(TM) Hardware */
46
ErrorCode_t LCR_CMD_PKT_ConnectToLCR(void)
47
{
48
	/* Open tcp connection with LCr */
49
	LCR_PKT_Socket = TCP_Connect(LCR_CMD_IP, LCR_CMD_PORT);
50
 
51
	if(LCR_PKT_Socket < 0)
52
		return FAIL;
53
 
54
	return SUCCESS;
55
}
56
 
57
/*Close the TCP Connection*/
58
ErrorCode_t LCR_CMD_PKT_DisconnectLCR(void)
59
{
60
	TCP_Disconnect(LCR_PKT_Socket);
61
	return SUCCESS;
62
}
63
 
64
/**********************************************************/
65
/* Low level functions for transfer of data over TCP link */
66
/**********************************************************/
67
 
68
int LCR_CMD_ReadData(uint8 *data, uint32 size)
69
{
70
	return TCP_Receive(LCR_PKT_Socket, data, size);
71
}
72
 
73
int LCR_CMD_WriteData(uint8 *data, uint32 size)
74
{
75
	return TCP_Send(LCR_PKT_Socket, data, size);
76
}
77
 
78
/*****************************/
79
/* Packet Creation functions */
80
/*****************************/
81
 
82
/* Initialize the read/write command */
83
int LCR_CMD_PKT_CommandInit(LCR_CommandType_t cmdType, uint16 cmdId)
84
{
85
	if(cmdType == LCR_CMD_PKT_TYPE_WRITE)
86
		LCR_PacketType = PKT_TYPE_WRITE;
87
	else if(cmdType == LCR_CMD_PKT_TYPE_READ)
88
		LCR_PacketType = PKT_TYPE_READ;
89
	else
90
		return -1;
91
 
92
	contFlag = 0;
93
	commandId = cmdId;
94
	dataLength = 0;
95
 
96
	return 0;
97
}
98
 
99
/* Add raw data to the command */
100
int LCR_CMD_PKT_PutData(uint8 *data, unsigned long int size)
101
{
102
	if(data == NULL)
103
		return -1;
104
 
105
	while(size)
106
	{
107
		int  copySize = size;
108
		if(dataLength == MAX_PACKET_SIZE)
109
		{
110
			if(LCR_CMD_PKT_SendPacket(1))
111
				return -1;
112
			dataLength = 0;
113
		}
114
 
115
		if(dataLength + copySize > MAX_PACKET_SIZE)
116
		{
117
			copySize = MAX_PACKET_SIZE - dataLength;
118
		}
119
 
120
		memcpy(packetData + dataLength, data, copySize);
121
		dataLength += copySize;
122
		size -= copySize;
123
	}
124
 
125
	return 0;
126
}
127
 
128
/* Add the initiger data to the command */
129
int LCR_CMD_PKT_PutInt(uint32 value, unsigned int size)
130
{
131
	uint8 data[4];
132
	unsigned int i;
133
 
134
	if(size > 4)
135
		return -1;
136
 
137
	for(i = 0; i < size; i++)
138
	{
139
		data[i] = (value & 0xFF);
140
		value >>= 8;
141
	}
142
 
143
	if(LCR_CMD_PKT_PutData(data, size))
144
		return -1;
145
 
146
	return 0;
147
}
148
 
149
/* Add the file content to the command */
150
int LCR_CMD_PKT_PutFile(char const *fileName)
151
{
152
	int error = 0;
153
	FILE *fp;
154
	int copySize;
155
 
156
	fp = fopen(fileName, "rb");
157
	if(fp == NULL)
158
		return -1;
159
 
160
	while(!feof(fp))
161
	{
162
		if(dataLength == MAX_PACKET_SIZE)
163
		{
164
			if(LCR_CMD_PKT_SendPacket(1))
165
			{
166
				error = -1;
167
				break;
168
			}
169
		}
170
 
171
		copySize = MAX_PACKET_SIZE - dataLength;
172
 
173
		copySize = fread(packetData + dataLength,
174
			1, copySize, fp);
175
		if(copySize <= 0)
176
			break;
177
 
178
		dataLength += copySize;
179
	}
180
 
181
	fclose(fp);
182
 
183
	return error;
184
}
185
 
186
/*Copied returened data into the <fileName>*/
187
int LCR_CMD_PKT_GetFile(char const *fileName,uint32 size)
188
{
189
	FILE *fp;
190
	int ret = 0;
191
	uint32 remSize = size;
192
 
193
	if(packetBuffer[0] != PKT_TYPE_READ_RESP)
194
	{
195
		return -1;
196
	}
197
 
198
	fp = fopen(fileName, "wb");
199
	if(fp == NULL)
200
	{
201
		return -1;
202
	}
203
 
204
	if(remSize == 0)
205
		remSize = MAX_PACKET_SIZE;
206
 
207
	while(remSize)
208
	{
209
		unsigned int copySize = dataLength - parseIndex;
210
 
211
		if(copySize == 0)
212
		{
213
			if(LCR_CMD_PKT_ReceivePacket(0))
214
			{
215
				if(size != 0)
216
					ret = -1;
217
				break;
218
			}
219
 
220
			copySize = dataLength - parseIndex;
221
		}
222
 
223
		if(copySize >= remSize)
224
		{
225
			copySize = remSize;
226
		}
227
 
228
		if(fwrite(packetData + parseIndex, 1, copySize, fp) != copySize)
229
		{
230
			ret = -1;
231
			break;
232
		}
233
 
234
		parseIndex += copySize;
235
 
236
		if(size != 0)
237
			remSize -= copySize;
238
	}
239
 
240
	fclose(fp);
241
 
242
	return ret;
243
}
244
 
245
 
246
/* Get raw data from the received packet */
247
int LCR_CMD_PKT_GetData(uint8 *data, unsigned long int size)
248
{
249
	if(packetBuffer[0] != PKT_TYPE_READ_RESP)
250
	{
251
		return -1;
252
	}
253
 
254
	while(size)
255
	{
256
		unsigned int copySize = dataLength - parseIndex;
257
 
258
		if(copySize == 0)
259
		{
260
			if(LCR_CMD_PKT_ReceivePacket(0))
261
				return -1;
262
 
263
			copySize = dataLength - parseIndex;
264
		}
265
 
266
		if(copySize >= size)
267
		{
268
			copySize = size;
269
		}
270
 
271
		memcpy(data, packetData + parseIndex, copySize);
272
		parseIndex += copySize;
273
		size -= copySize;
274
		data += copySize;
275
	}
276
 
277
	return 0;
278
}
279
 
280
/* Get integer data from the received packet */
281
uint32 LCR_CMD_PKT_GetInt(unsigned int size)
282
{
283
	uint32 value = 0;
284
	uint8 data[4];
285
	unsigned int i;
286
 
287
	if(size > 4)
288
		return 0;
289
 
290
	if(LCR_CMD_PKT_GetData(data, size))
291
		return 0;
292
 
293
	for(i = 0; i < size; i++)
294
	{
295
		value |= data[i] << (i * 8);
296
	}
297
 
298
	return value;
299
}
300
 
301
/* Calculate the packet checksum to be added at the end */
302
uint8 LCR_CMD_PKT_CalcChecksum(void)
303
{
304
	int i;
305
	int sum = 0;
306
 
307
	for(i = 0; i < dataLength + HEADER_SIZE; i++)
308
	{
309
		sum += packetBuffer[i];
310
	}
311
	return (uint8)(sum & 0xFF);
312
}
313
 
314
/***********************************************************/
315
/* Sending and receiving the Packets after packet creation */
316
/***********************************************************/
317
 
318
/* Receive one packet of the responce */
319
int LCR_CMD_PKT_ReceivePacket(BOOL firstPkt)
320
{
321
	unsigned long int mask;
322
	int i;
323
 
324
	dataLength = 0;
325
	parseIndex = 0;
326
 
327
	if(firstPkt == 0)
328
	{
329
		if(recvFlag == 0 ||  recvFlag == 3)
330
		{
331
			return -1;
332
		}
333
	}
334
 
335
	if(LCR_CMD_ReadData(packetBuffer, HEADER_SIZE))
336
	{
337
		return -1;
338
	}
339
 
340
	dataLength = packetBuffer[4] | packetBuffer[5] << 8;
341
 
342
	if(LCR_CMD_ReadData(packetData, dataLength + 1))
343
	{
344
		return -1;
345
	}
346
 
347
	if(packetData[dataLength] != LCR_CMD_PKT_CalcChecksum())
348
	{
349
		printf("ERROR: Checksum failed in the commands response packet!!!\n");
350
		return -1;
351
	}
352
 
353
	if(packetBuffer[0] != LCR_PacketType + 1)
354
	{
355
		//Determine the response packet 
356
		switch(packetBuffer[0])
357
		{
358
 
359
		case 0x00:
360
			printf("INFO: Command NOT executed; DM365 is BUSY\n");
361
			break;
362
 
363
		case 0x01:
364
			printf("INFO: Command packet has ERROR in it\n");
365
 
366
			//Set flag based on the error #
367
			mask = 0; 
368
			i = 0;
369
			while(dataLength--)
370
			{
371
				if(packetBuffer[(6+i)] != 0)
372
				{
373
					mask |= (1<<(packetBuffer[(6+i)] - 1));
374
				}
375
 
376
				i++;
377
			}
378
 
379
			//print each error 
380
			if((mask & 0x01) == 0x01)
381
			{
382
				printf("COMMAND PACKET ERROR: Commmand Failed!!!\n");
383
			}
384
			else if((mask & 0x02) == 0x02)
385
			{
386
				printf("COMMAND PACKET ERROR: Unsupported Command!!!\n");
387
			}
388
			else if((mask & 0x04) == 0x04)
389
			{
390
				printf("COMMAND PACKET ERROR: Invalid Parameter!!!\n");
391
			}
392
			else if((mask & 0x08) == 0x08)
393
			{
394
				printf("COMMAND PACKET ERROR: Out Of Resource!!!\n");
395
			}
396
			else if((mask & 0x10) == 0x10)
397
			{
398
				printf("COMMAND PACKET ERROR: Device Failed!!!\n");
399
			}
400
			else if((mask & 0x20) == 0x20)
401
			{
402
				printf("COMMAND PACKET ERROR: Device Busy!!!\n");
403
			}
404
			else if((mask & 0x40) == 0x40)
405
			{
406
				printf("COMMAND PACKET ERROR: Not Initialized!!!\n");
407
			}
408
			else if((mask & 0x80) == 0x80)
409
			{
410
				printf("COMMAND PACKET ERROR: Not Found!!!\n");
411
			}
412
			else if((mask & 0x100) == 0x100)
413
			{
414
				printf("COMMAND PACKET ERROR: Checksum Error!!!\n");
415
			}
416
			else if((mask & 0x200) == 0x200)
417
			{
418
				printf("COMMAND PACKET ERROR: Packet Format Error!!!\n");
419
			}
420
			else if((mask & 0x400) == 0x400)
421
			{
422
				printf("COMMAND PACKET ERROR: Command Continuation Error!!!\n");
423
			}
424
			else
425
			{
426
				printf("COMMAND PACKET ERROR: DM365 returned undocumented Error!!!\n");
427
			}
428
 
429
			break;
430
 
431
		case 0x02:
432
		case 0x04:
433
			printf("COMMAND PACKET ERROR: DM365 sent back command type as Write = [0x02] or Read = [0x04] command!!!\n");	
434
			break;
435
 
436
		default: 
437
			printf("ERROR: Unkknown packet type information received in response packet!!!\n");
438
			break;
439
		}
440
 
441
		return -1;
442
	}
443
 
444
	recvFlag = packetBuffer[3];
445
 
446
	if(firstPkt != (recvFlag == 0 || recvFlag == 1))
447
	{
448
		return -1;
449
	}
450
 
451
	if(recvFlag == 3)
452
	{
453
		/* Command SUCCESS */
454
	}
455
 
456
	return 0;
457
}
458
 
459
/* Send the command paket just initialized */
460
int LCR_CMD_PKT_SendPacket(BOOL more)
461
{
462
	uint8 flag;
463
 
464
	packetBuffer[0] = LCR_PacketType;
465
	packetBuffer[1] = (commandId >> 8) & 0xFF;
466
	packetBuffer[2] = (commandId) & 0xFF;
467
 
468
	if(contFlag)
469
	{
470
		if(more)
471
			flag = 2;
472
		else
473
			flag = 3;
474
	}
475
	else
476
	{
477
		if(more)
478
			flag = 1;
479
		else
480
			flag = 0;
481
	}
482
	contFlag = more;
483
 
484
	packetBuffer[3] = flag;
485
	packetBuffer[4] = dataLength & 0xFF;
486
	packetBuffer[5] = (dataLength >> 8) & 0xFF;
487
 
488
	packetData[dataLength] = LCR_CMD_PKT_CalcChecksum();
489
 
490
	int length = dataLength + HEADER_SIZE + CHECKSUM_SIZE;
491
 
492
	if(LCR_CMD_WriteData(packetBuffer, length))
493
		return -1;
494
 
495
	if(LCR_CMD_PKT_ReceivePacket(1))
496
		return -1;
497
 
498
	if(more == 0 && recvFlag == 0)
499
	{
500
		/* SUCCESS */
501
	}
502
 
503
	return 0;
504
}
505
 
506
int LCR_CMD_PKT_SendCommand(void)
507
{
508
	return LCR_CMD_PKT_SendPacket(0);
509
}