-
Notifications
You must be signed in to change notification settings - Fork 178
/
c_com.c
5594 lines (4829 loc) · 160 KB
/
c_com.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
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* LEGO® MINDSTORMS EV3
*
* Copyright (C) 2010-2013 The LEGO Group
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*! \page CommunicationLibrary Communication Library
*
*- \subpage CommunicationLibraryDescription
*- \subpage CommunicationLibraryCodes
*/
/*! \page CommunicationLibraryDescription Description
*
*
*/
/*! \page CommunicationLibraryCodes Byte Code Summary
*
*
*/
#include "c_com.h"
#include "lms2012.h"
#include "c_bt.h"
#include "c_wifi.h"
#include "c_daisy.h"
#include "c_md5.h"
#include "c_i2c.h"
#include "c_output.h"
#include "c_memory.h"
#ifdef DEBUG_C_COM
#define DEBUG
#endif
#if (HARDWARE != SIMULATION)
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <dirent.h>
COM_GLOBALS ComInstance;
#else
COM_GLOBALS * gComInstance;
void setComInstance(COM_GLOBALS * _Instance)
{
gComInstance= _Instance;
}
COM_GLOBALS* getComInstance()
{
return gComInstance;
}
#endif
#define USB_CABLE_DETECT_RATE 15000 // ?? Approx 5 sec. on a good day. Cable detection is a NON-critical
// event
UWORD UsbConUpdate = 0; // Timing of USB device side cable detection;
UBYTE cComUsbDeviceConnected = FALSE;
UWORD cComReadBuffer(UBYTE *pBuffer,UWORD Size);
UWORD cComWriteBuffer(UBYTE *pBuffer,UWORD Size);
UBYTE cComFindMailbox(UBYTE *pName, UBYTE *pNo);
void cComSetMusbHdrcMode(void);
#ifdef DEBUG
void cComPrintTxMsg(TXBUF *pTxBuf);
#endif
#define SIZEOFFILESIZE 8
enum
{
FS_IDLE,
};
RESULT cComInit(void)
{
RESULT Result = FAIL;
UWORD TmpFileHandle;
UBYTE Cnt;
FILE *File;
#if (HARDWARE != SIMULATION)
ComInstance.CommandReady = 0;
ComInstance.Cmdfd = open(COM_CMD_DEVICE_NAME, O_RDWR, 0666);
if (ComInstance.Cmdfd >= 0)
{
memset(ComInstance.TxBuf[0].Buf,0,sizeof(ComInstance.TxBuf[0].Buf));
Result = OK;
}
for (TmpFileHandle = 0;TmpFileHandle < MAX_FILE_HANDLES;TmpFileHandle++)
{
ComInstance.Files[TmpFileHandle].State = FS_IDLE;
ComInstance.Files[TmpFileHandle].Name[0] = 0;
ComInstance.Files[TmpFileHandle].File = -1;
}
for(Cnt = 0; Cnt < NO_OF_CHS; Cnt++)
{
ComInstance.TxBuf[Cnt].BufSize = 1024;
ComInstance.TxBuf[Cnt].Writing = 0;
ComInstance.RxBuf[Cnt].BufSize = 1024;
ComInstance.RxBuf[Cnt].State = RXIDLE;
}
ComInstance.ReadChannel[0] = cComReadBuffer;
ComInstance.ReadChannel[1] = NULL;
ComInstance.ReadChannel[2] = cBtReadCh0;
ComInstance.ReadChannel[3] = cBtReadCh1;
ComInstance.ReadChannel[4] = cBtReadCh2;
ComInstance.ReadChannel[5] = cBtReadCh3;
ComInstance.ReadChannel[6] = cBtReadCh4;
ComInstance.ReadChannel[7] = cBtReadCh5;
ComInstance.ReadChannel[8] = cBtReadCh6;
ComInstance.ReadChannel[9] = cBtReadCh7;
ComInstance.ReadChannel[10] = cWiFiReadTcp;
ComInstance.WriteChannel[0] = cComWriteBuffer;
ComInstance.WriteChannel[1] = NULL;
ComInstance.WriteChannel[2] = cBtDevWriteBuf;
ComInstance.WriteChannel[3] = cBtDevWriteBuf1;
ComInstance.WriteChannel[4] = cBtDevWriteBuf2;
ComInstance.WriteChannel[5] = cBtDevWriteBuf3;
ComInstance.WriteChannel[6] = cBtDevWriteBuf4;
ComInstance.WriteChannel[7] = cBtDevWriteBuf5;
ComInstance.WriteChannel[8] = cBtDevWriteBuf6;
ComInstance.WriteChannel[9] = cBtDevWriteBuf7;
ComInstance.WriteChannel[10] = cWiFiWriteTcp;
for(Cnt = 0; Cnt < NO_OF_MAILBOXES; Cnt++)
{
ComInstance.MailBox[Cnt].Status = FAIL;
ComInstance.MailBox[Cnt].DataSize = 0;
ComInstance.MailBox[Cnt].ReadCnt = 0;
ComInstance.MailBox[Cnt].WriteCnt = 0;
ComInstance.MailBox[Cnt].Name[0] = 0;
}
ComInstance.ComResult = OK;
ComInstance.BrickName[0] = 0;
File = fopen("./settings/BrickName","r");
if (File != NULL)
{
fgets((char*)&(ComInstance.BrickName[0]), (int)vmBRICKNAMESIZE, File);
fclose (File);
}
// USB cable stuff init
UsbConUpdate = 0; // Reset timing of USB device side cable detection;
cComUsbDeviceConnected = FALSE; // Until we believe in something else ;-)
cComSetMusbHdrcMode();
BtInit((char*)&(ComInstance.BrickName[0]));
cDaisyInit();
cWiFiInit();
ComInstance.VmReady = 1;
ComInstance.ReplyStatus = 0;
#else
return OK;
#endif
return (Result);
}
RESULT cComOpen(void)
{
RESULT Result = FAIL;
Result = OK;
return (Result);
}
RESULT cComClose(void)
{
RESULT Result = FAIL;
UBYTE Cnt;
for(Cnt = 0; Cnt < NO_OF_MAILBOXES; Cnt++)
{
ComInstance.MailBox[Cnt].Status = FAIL;
ComInstance.MailBox[Cnt].DataSize = 0;
ComInstance.MailBox[Cnt].ReadCnt = 0;
ComInstance.MailBox[Cnt].WriteCnt = 0;
ComInstance.MailBox[Cnt].Name[0] = 0;
}
Result = OK;
return (Result);
}
RESULT cComExit(void)
{
RESULT Result = FAIL;
close(ComInstance.Cmdfd);
Result = OK;
cWiFiExit();
BtExit();
return (Result);
}
// DAISY chain
// Write type data to chain
RESULT cComSetDeviceInfo(DATA8 Length,UBYTE *pInfo)
{
return cDaisySetDeviceInfo(Length, pInfo);
//return FAIL;
}
// Read type data from chain
RESULT cComGetDeviceInfo(DATA8 Length,UBYTE *pInfo)
{
return cDaisyGetDeviceInfo(Length, pInfo);
}
// Write mode to chain
RESULT cComSetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
{
return cDaisySetDeviceType(Layer, Port, Type, Mode);
}
// Read device data from chain
RESULT cComGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
{
return cDaisyGetDownstreamData(Layer, Port, Length, pType, pMode, pData);
}
void cComSetMusbHdrcMode(void)
{
// Force OTG into mode
char Musb_Cmd[64];
// Build the command string for setting the OTG mode
strcpy(Musb_Cmd, "echo otg > /sys/devices/platform/musb_hdrc/mode");
system(Musb_Cmd);
}
UBYTE cComCheckUsbCable(void)
{
// Get mode from MUSB_HDRC
UBYTE Result = FALSE;
char buffer[21];
FILE *f;
f = fopen("/sys/devices/platform/musb_hdrc/mode", "r");
if(f)
{
fread(buffer, 20, 1, f);
fclose(f);
}
//#define DEBUG
#undef DEBUG
#ifdef DEBUG
printf("BUFFER = %s\n\r", buffer);
#endif
if(strstr(buffer, "b_peripheral") != 0)
Result = TRUE;
//#define DEBUG
#undef DEBUG
#ifdef DEBUG
if(Result == TRUE)
printf("CABLE connected\n\r");
else
printf("CABLE dis-connected :-(\n\r");
#endif
// Just to be sure - we disables further prinf "polution"
#undef DEBUG
return Result;
}
#ifdef DEBUG
void cComShow(UBYTE *pB)
{
UWORD Lng;
Lng = (UWORD)pB[0];
Lng += (UWORD)pB[1] << 8;
printf("[%02X%02X",pB[0],pB[1]);
pB++;
pB++;
while (Lng)
{
printf("%02X",*pB);
pB++;
Lng--;
}
printf("]\r\n");
}
#endif
#ifdef DEBUG
void cComPrintTxMsg(TXBUF *pTxBuf)
{
ULONG Cnt, Cnt2;
COMRPL *pComRpl;
pComRpl = (COMRPL*) pTxBuf->Buf;
printf("Tx Buf content: \r\n");
for(Cnt = 0; ((Cnt < ((*pComRpl).CmdSize + 2)) && (Cnt < 1024)); Cnt++)
{
for(Cnt2 = Cnt; Cnt2 < (Cnt + 16); Cnt2++)
{
printf("0x%02X, ",((UBYTE*)(&((*pComRpl).CmdSize)))[Cnt2]);
}
Cnt = (Cnt2-1);
printf("\r\n");
}
printf("\r\n");
}
#endif
UWORD cComReadBuffer(UBYTE *pBuffer,UWORD Size)
{
UWORD Length = 0;
#if (HARDWARE != SIMULATION)
struct timeval Cmdtv;
fd_set Cmdfds;
Cmdtv.tv_sec = 0;
Cmdtv.tv_usec = 0;
FD_ZERO(&Cmdfds);
FD_SET(ComInstance.Cmdfd, &Cmdfds);
if (select(ComInstance.Cmdfd + 1, &Cmdfds, NULL, NULL, &Cmdtv))
{
Length = read(ComInstance.Cmdfd,pBuffer,(size_t)Size);
#undef DEBUG
//#define DEBUG
#ifdef DEBUG
printf("cComReadBuffer Length = %d\r\n", Length);
#endif
}
#endif
return (Length);
}
UWORD cComWriteBuffer(UBYTE *pBuffer,UWORD Size)
{
UWORD Length = 0;
#if (HARDWARE != SIMULATION)
if(FULL_SPEED == cDaisyGetUsbUpStreamSpeed())
{
Length = write(ComInstance.Cmdfd,pBuffer,64);
}
else
Length = write(ComInstance.Cmdfd,pBuffer,1024);
#undef DEBUG
//#define DEBUG
#ifdef DEBUG
printf("cComWriteBuffer %d\n\r", Length);
#endif
#endif
return (Length);
}
UBYTE cComDirectCommand(UBYTE *pBuffer,UBYTE *pReply)
{
UBYTE Result = 0;
COMCMD *pComCmd;
COMRPL *pComRpl;
DIRCMD *pDirCmd;
CMDSIZE CmdSize;
CMDSIZE HeadSize;
UWORD Tmp;
UWORD Globals;
UWORD Locals;
IMGHEAD *pImgHead;
OBJHEAD *pObjHead;
CMDSIZE Length;
ComInstance.VmReady = 0;
pComCmd = (COMCMD*)pBuffer;
pDirCmd = (DIRCMD*)(*pComCmd).PayLoad;
CmdSize = (*pComCmd).CmdSize;
HeadSize = ((*pDirCmd).Code - pBuffer) - sizeof(CMDSIZE);
Length = CmdSize - HeadSize;
pComRpl = (COMRPL*)pReply;
(*pComRpl).CmdSize = 3;
(*pComRpl).MsgCnt = (*pComCmd).MsgCnt;
(*pComRpl).Cmd = DIRECT_REPLY_ERROR;
if ((CmdSize > HeadSize) && ((CmdSize - HeadSize) < (sizeof(ComInstance.Image) - (sizeof(IMGHEAD) + sizeof(OBJHEAD)))))
{
Tmp = (UWORD)(*pDirCmd).Globals + ((UWORD)(*pDirCmd).Locals << 8);
Globals = (Tmp & 0x3FF);
Locals = ((Tmp >> 10) & 0x3F);
if ((Locals <= MAX_COMMAND_LOCALS) && (Globals <= MAX_COMMAND_GLOBALS))
{
pImgHead = (IMGHEAD*)ComInstance.Image;
pObjHead = (OBJHEAD*)(ComInstance.Image + sizeof(IMGHEAD));
(*pImgHead).Sign[0] = 'l';
(*pImgHead).Sign[1] = 'e';
(*pImgHead).Sign[2] = 'g';
(*pImgHead).Sign[3] = 'o';
(*pImgHead).VersionInfo = (UWORD)(VERS * 100.0);
(*pImgHead).NumberOfObjects = (OBJID)1;
(*pImgHead).GlobalBytes = (GBINDEX)Globals;
(*pObjHead).OffsetToInstructions = (IP)(sizeof(IMGHEAD) + sizeof(OBJHEAD));
(*pObjHead).OwnerObjectId = 0;
(*pObjHead).TriggerCount = 0;
(*pObjHead).LocalBytes = (OBJID)Locals;
memcpy(&ComInstance.Image[sizeof(IMGHEAD) + sizeof(OBJHEAD)],(*pDirCmd).Code,Length);
Length += sizeof(IMGHEAD) + sizeof(OBJHEAD);
ComInstance.Image[Length] = opOBJECT_END;
(*pImgHead).ImageSize = Length;
//#define DEBUG
#undef DEBUG
#ifdef DEBUG
printf("\r\n");
for (Tmp = 0;Tmp <= Length;Tmp++)
{
printf("%02X ",ComInstance.Image[Tmp]);
if ((Tmp & 0x0F) == 0x0F)
{
printf("\r\n");
}
}
printf("\r\n");
#endif
Result = 1;
}
else
{
(*pComRpl).Cmd = DIRECT_REPLY_ERROR;
}
}
else
{
(*pComRpl).Cmd = DIRECT_REPLY_ERROR;
}
return (Result);
}
void cComCloseFileHandle(SLONG *pHandle)
{
if (*pHandle >= MIN_HANDLE)
{
close(*pHandle);
*pHandle = -1;
}
}
UBYTE cComFreeHandle(DATA8 Handle)
{
UBYTE RtnVal = TRUE;
if (0 != ComInstance.Files[Handle].Name[0])
{
if ((Handle >= 0) && (Handle < MAX_FILE_HANDLES))
{
ComInstance.Files[Handle].Name[0] = 0;
}
}
else
{
// Handle is unused
RtnVal = FALSE;
}
return(RtnVal);
}
DATA8 cComGetHandle(char *pName)
{
DATA8 Result = 0;
while ((ComInstance.Files[Result].Name[0]) && (Result < MAX_FILE_HANDLES))
{
Result++;
}
if (Result < MAX_FILE_HANDLES)
{
if (OK == cMemoryCheckFilename(pName, NULL, NULL, NULL))
{
sprintf(ComInstance.Files[Result].Name, "%s", (char*)pName);
}
else
{
Result = -1;
}
}
else
{
Result = -1;
}
return (Result);
}
UBYTE cComGetNxtFile(DIR *pDir, UBYTE *pName)
{
UBYTE RtnVal = 0;
struct dirent *pDirPtr;
pDirPtr = readdir(pDir);
while((NULL != pDirPtr) && (DT_REG != pDirPtr->d_type))
{
pDirPtr = readdir(pDir);
}
if(NULL != pDirPtr)
{
snprintf((char*)pName, FILENAME_SIZE,"%s",pDirPtr->d_name);
RtnVal = 1;
}
else
{
closedir(pDir);
}
return(RtnVal);
}
void cComCreateBeginDl( TXBUF *pTxBuf, UBYTE *pName)
{
UWORD Index;
UWORD ReadBytes;
BEGIN_DL *pDlMsg;
SBYTE FileHandle;
char TmpFileName[vmFILENAMESIZE];
FileHandle = -1;
pDlMsg = (BEGIN_DL*) &(pTxBuf->Buf[0]);
if ((strlen((char *)pTxBuf->Folder) + strlen((char *)pName) + 1) <= vmFILENAMESIZE)
{
sprintf(TmpFileName, "%s", (char*)pTxBuf->Folder);
strcat(TmpFileName, (char*)pName);
FileHandle = cComGetHandle(TmpFileName);
}
if (-1 != FileHandle)
{
pTxBuf->pFile = (FIL*)&(ComInstance.Files[FileHandle]);
pTxBuf->pFile->File = open(pTxBuf->pFile->Name,O_RDONLY,0x444);
// Get file length
pTxBuf->pFile->Size = lseek(pTxBuf->pFile->File, 0L, SEEK_END);
lseek(pTxBuf->pFile->File, 0L, SEEK_SET);
pDlMsg->CmdSize = 0x00; // Msg Len
pDlMsg->MsgCount = 0x00; // Msg Count
pDlMsg->CmdType = SYSTEM_COMMAND_REPLY; // Cmd Type
pDlMsg->Cmd = BEGIN_DOWNLOAD; // Cmd
pDlMsg->FileSizeLsb = (UBYTE)(pTxBuf->pFile->Size); // File Size Lsb
pDlMsg->FileSizeNsb1 = (UBYTE)(pTxBuf->pFile->Size >> 8); // File Size
pDlMsg->FileSizeNsb2 = (UBYTE)(pTxBuf->pFile->Size >> 16); // File Size
pDlMsg->FileSizeMsb = (UBYTE)(pTxBuf->pFile->Size >> 24); // File Size Msb
Index = strlen((char*)pTxBuf->pFile->Name) + 1;
snprintf((char*)(&(pTxBuf->Buf[SIZEOF_BEGINDL])), Index, "%s", (char*)pTxBuf->pFile->Name);
Index += SIZEOF_BEGINDL;
// Find the number of bytes to go into this message
if ((MAX_MSG_SIZE - Index) < pTxBuf->pFile->Size)
{
// Msg cannot hold complete file
pTxBuf->Buf[0] = (UBYTE)(MAX_MSG_SIZE - 2);
pTxBuf->Buf[1] = (UBYTE)((MAX_MSG_SIZE - 2) >> 8);
ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[Index]), (pTxBuf->BufSize - Index));
pTxBuf->BlockLen = pTxBuf->BufSize;
pTxBuf->SubState = FILE_IN_PROGRESS_WAIT_FOR_REPLY;
pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
pTxBuf->pFile->Pointer = ReadBytes; // No of bytes read from file
pTxBuf->MsgLen = (pDlMsg->CmdSize - Index) + 2; // Index = full header size
pTxBuf->Writing = 1;
}
else
{
// Msg can hold complete file
pTxBuf->Buf[0] = (UBYTE) (pTxBuf->pFile->Size + Index - 2);
pTxBuf->Buf[1] = (UBYTE)((pTxBuf->pFile->Size + Index - 2) >> 8);
if ((pTxBuf->BufSize - Index) < pTxBuf->pFile->Size)
{
// Complete msg exceeds buffer size
ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[Index]), (pTxBuf->BufSize - Index));
pTxBuf->BlockLen = pTxBuf->BufSize;
}
else
{
// Complete msg can fit in buffer
ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[Index]), pTxBuf->pFile->Size);
pTxBuf->BlockLen = pTxBuf->pFile->Size + Index;
// Close handles
cComCloseFileHandle(&(pTxBuf->pFile->File));
cComFreeHandle(FileHandle);
}
pTxBuf->SubState = FILE_COMPLETE_WAIT_FOR_REPLY;
pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
pTxBuf->pFile->Pointer = ReadBytes; // No of bytes read from file
pTxBuf->MsgLen = (pDlMsg->CmdSize - Index) + 2; // Index = full header size
pTxBuf->Writing = 1;
}
}
}
void cComCreateContinueDl(RXBUF *pRxBuf, TXBUF *pTxBuf)
{
UWORD ReadBytes;
CONTINUE_DL *pContinueDl;
RPLY_BEGIN_DL *pRplyBeginDl;
pRplyBeginDl = (RPLY_BEGIN_DL*)&(pRxBuf->Buf[0]);
pContinueDl = (CONTINUE_DL*)&(pTxBuf->Buf[0]);
pContinueDl->CmdSize = SIZEOF_CONTINUEDL - sizeof(CMDSIZE);
pContinueDl->MsgCount = (pRplyBeginDl->MsgCount)++;
pContinueDl->CmdType = SYSTEM_COMMAND_REPLY;
pContinueDl->Cmd = CONTINUE_DOWNLOAD;
pContinueDl->Handle = pTxBuf->RemoteFileHandle;
if ((MAX_MSG_SIZE - SIZEOF_CONTINUEDL) < (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer))
{
// Msg cannot hold complete file
ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[SIZEOF_CONTINUEDL]), (pTxBuf->BufSize - SIZEOF_CONTINUEDL));
pTxBuf->BlockLen = pTxBuf->BufSize;
pContinueDl->CmdSize += ReadBytes;
pTxBuf->SubState = FILE_IN_PROGRESS_WAIT_FOR_REPLY;
pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
pTxBuf->pFile->Pointer += ReadBytes; // No of bytes read from file
pTxBuf->MsgLen = (pContinueDl->CmdSize - SIZEOF_CONTINUEDL) + 2; // Index = full header size
pTxBuf->Writing = 1;
}
else
{
// Msg can hold complete file
if ((pTxBuf->BufSize - SIZEOF_CONTINUEDL) < (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer))
{
// Complete msg exceeds buffer size
ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[SIZEOF_CONTINUEDL]), (pTxBuf->BufSize - SIZEOF_CONTINUEDL));
}
else
{
// Complete msg can fit in buffer
ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[SIZEOF_CONTINUEDL]), (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer));
// Close handles
cComCloseFileHandle(&(pTxBuf->pFile->File));
cComFreeHandle(pTxBuf->FileHandle);
}
pTxBuf->BlockLen = ReadBytes + SIZEOF_CONTINUEDL;
pContinueDl->CmdSize += ReadBytes;
pTxBuf->SubState = FILE_COMPLETE_WAIT_FOR_REPLY;
pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
pTxBuf->pFile->Pointer += ReadBytes; // No of bytes read from file
pTxBuf->MsgLen = (pContinueDl->CmdSize - SIZEOF_CONTINUEDL) + 2; // Index = full header size
pTxBuf->Writing = 1;
}
}
void cComSystemReply(RXBUF *pRxBuf, TXBUF *pTxBuf)
{
COMCMD *pComCmd;
SYSCMDC *pSysCmdC;
CMDSIZE CmdSize;
UBYTE FileName[MAX_FILENAME_SIZE];
pComCmd = (COMCMD*)pRxBuf->Buf;
pSysCmdC = (SYSCMDC*)(*pComCmd).PayLoad;
CmdSize = (*pComCmd).CmdSize;
switch ((*pSysCmdC).Sys)
{
case BEGIN_DOWNLOAD:
{
// This is the reply from the begin download command
// This is part of the file transfer sequence
// Check for single file or Folder transfer
RPLY_BEGIN_DL *pRplyBeginDl;
pRplyBeginDl = (RPLY_BEGIN_DL*)&(pRxBuf->Buf[0]);
if ((SUCCESS == pRplyBeginDl->Status) || (END_OF_FILE == pRplyBeginDl->Status))
{
pTxBuf->RemoteFileHandle = pRplyBeginDl->Handle;
if (FILE_IN_PROGRESS_WAIT_FOR_REPLY == pTxBuf->SubState)
{
// Issue continue write as file is not
// completely downloaded
cComCreateContinueDl(pRxBuf, pTxBuf);
}
else
{
if (FILE_COMPLETE_WAIT_FOR_REPLY == pTxBuf->SubState)
{
// Complete file downloaded check for more files
// to download
if (TXFOLDER == pTxBuf->State)
{
if (cComGetNxtFile(pTxBuf->pDir, FileName))
{
cComCreateBeginDl(pTxBuf, FileName);
}
else
{
// No More files
pTxBuf->State = TXIDLE;
pTxBuf->SubState = SUBSTATE_IDLE;
ComInstance.ComResult = OK;
}
}
else
{
// Only one file to send
pTxBuf->State = TXIDLE;
pTxBuf->SubState = SUBSTATE_IDLE;
ComInstance.ComResult = OK;
}
}
}
}
}
break;
case CONTINUE_DOWNLOAD:
{
RPLY_CONTINUE_DL *pRplyContinueDl;
pRplyContinueDl = (RPLY_CONTINUE_DL*)&(pRxBuf->Buf[0]);
if ((SUCCESS == pRplyContinueDl->Status) || (END_OF_FILE == pRplyContinueDl->Status))
{
if (FILE_IN_PROGRESS_WAIT_FOR_REPLY == pTxBuf->SubState)
{
// Issue continue write as file is not
// completely downloaded
cComCreateContinueDl(pRxBuf, pTxBuf);
}
else
{
if (FILE_COMPLETE_WAIT_FOR_REPLY == pTxBuf->SubState)
{
// Complete file downloaded check for more files
// to download
if (TXFOLDER == pTxBuf->State)
{
if (cComGetNxtFile(pTxBuf->pDir, FileName))
{
cComCreateBeginDl(pTxBuf, FileName);
}
else
{
pTxBuf->State = TXIDLE;
pTxBuf->SubState = SUBSTATE_IDLE;
ComInstance.ComResult = OK;
}
}
else
{
pTxBuf->State = TXIDLE;
pTxBuf->SubState = SUBSTATE_IDLE;
ComInstance.ComResult = OK;
}
}
}
}
}
break;
default:
{
}
break;
}
}
void cComGetNameFromScandirList(struct dirent *NameList, char *pBuffer, ULONG *pNameLen, UBYTE *pFolder)
{
char FileName[MD5LEN + 1 + FILENAMESIZE];
struct stat FileInfo;
ULONG Md5Sum[4];
*pNameLen = 0;
//If type is a directory the add "/" at the end
if ((DT_LNK == NameList->d_type) || (DT_DIR == NameList->d_type))
{
strncpy(pBuffer, NameList->d_name, FILENAMESIZE); // Need to copy to tmp var to be able to add "/" for folders
*pNameLen = strlen(NameList->d_name); // + 1 is the new line character
strcat(pBuffer,"/");
(*pNameLen)++;
strcat(pBuffer,"\n");
(*pNameLen)++;
}
else
{
if (DT_REG == NameList->d_type)
{
/* If it is a file then add 16 bytes MD5SUM + space */
strcpy(FileName, (char*)pFolder);
strcat(FileName, "/");
strcat(FileName,NameList->d_name);
/* Get the MD5sum and put in the buffer */
md5_file(FileName, 0, (unsigned char *)Md5Sum);
*pNameLen = sprintf(pBuffer, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X ",
((UBYTE*)Md5Sum)[0] , ((UBYTE*)Md5Sum)[1] , ((UBYTE*)Md5Sum)[2] , ((UBYTE*)Md5Sum)[3] ,
((UBYTE*)Md5Sum)[4] , ((UBYTE*)Md5Sum)[5] , ((UBYTE*)Md5Sum)[6] , ((UBYTE*)Md5Sum)[7] ,
((UBYTE*)Md5Sum)[8] , ((UBYTE*)Md5Sum)[9] , ((UBYTE*)Md5Sum)[10], ((UBYTE*)Md5Sum)[11],
((UBYTE*)Md5Sum)[12], ((UBYTE*)Md5Sum)[13], ((UBYTE*)Md5Sum)[14], ((UBYTE*)Md5Sum)[15]);
/* Insert file size */
stat(FileName,&FileInfo);
*pNameLen += sprintf(&(pBuffer[MD5LEN + 1]), "%08X ",(ULONG)FileInfo.st_size);
/* Insert Filename */
strcat(pBuffer, NameList->d_name);
*pNameLen += strlen(NameList->d_name);
strcat(pBuffer,"\n");
(*pNameLen)++;
}
}
}
UBYTE cComCheckForSpace(char *pFullName, ULONG Size)
{
UBYTE RtnVal;
DATA32 TotalSize;
DATA32 FreeSize;
DATA8 Changed;
RtnVal = FAIL;
//Make it KB and round up
if (Size & (ULONG)(KB-1))
{
Size /= KB;
Size += 1;
}
else
{
Size /= KB;
}
if (strstr(pFullName, SDCARD_FOLDER) != NULL)
{
if (CheckSdcard(&Changed, &TotalSize, &FreeSize, 1))
{
if (FreeSize >= Size)
{
RtnVal = OK;
}
}
}
else
{
if (strstr(pFullName, USBSTICK_FOLDER) != NULL)
{
if (CheckUsbstick(&Changed, &TotalSize, &FreeSize, 1))
{
if (FreeSize >= Size)
{
RtnVal = OK;
}
}
}
else
{
cMemoryGetUsage(&TotalSize, &FreeSize, 1);
if (FreeSize >= Size + 300)
{
RtnVal = OK;
}
}
}
return(RtnVal);
}
/*! \page ComModule
*
* <hr size="1"/>
* <b> write </b>
*/
/*! \brief cSystemCommand
*
*
* cComSystemCommand
* -----------------
*
*
*
* One message at a time principles
* --------------------------------
*
* - The system is only able to process one command at a time (both SYS and DIR commands).
* - ComInstance.ReplyStatus holds the information about command processing.
* - Writing flag indicated that the reply is ready to be transmitted
* - Direct commands are commands to be interpreted by VM - Direct commands are not interpreted until process is
* returned to the VM
* - System commands are usually processed when received except for large messages (messages larger that buffersize)
*
* Direct command -> When received -> if reply required -> ComInstance.ReplyStatus = DIR_CMD_REPLY
* |
* ------------> if no reply required -> ComInstance.ReplyStatus = DIR_CMD_NOREPLY
*
* VM reply to direct command -> if (ComInstance.ReplyStatus = DIR_CMD_REPLY) -> pTxBuf->Writing = 1
* (VM always replies, after interp.) ComInstance.ReplyStatus = 0