LMS 2012
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
c_com.c
Go to the documentation of this file.
1 /*
2  * LEGO® MINDSTORMS EV3
3  *
4  * Copyright (C) 2010-2013 The LEGO Group
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19  */
20 
21 
41 #include "c_com.h"
42 #include "lms2012.h"
43 #include "c_bt.h"
44 #include "c_wifi.h"
45 #include "c_daisy.h"
46 #include "c_md5.h"
47 #include "c_i2c.h"
48 #include "c_output.h"
49 #include "c_memory.h"
50 
51 #ifdef DEBUG_C_COM
52 #define DEBUG
53 #endif
54 
55 #if (HARDWARE != SIMULATION)
56  #include <stdlib.h>
57  #include <stdio.h>
58  #include <sys/types.h>
59  #include <sys/stat.h>
60  #include <fcntl.h>
61  #include <unistd.h>
62  #include <string.h>
63  #include <errno.h>
64  #include <dirent.h>
66 #else
67 
69 
70 void setComInstance(COM_GLOBALS * _Instance)
71 {
72  gComInstance= _Instance;
73 }
74 
76 {
77  return gComInstance;
78 }
79 #endif
80 
81 #define USB_CABLE_DETECT_RATE 15000 // ?? Approx 5 sec. on a good day. Cable detection is a NON-critical
82  // event
83 UWORD UsbConUpdate = 0; // Timing of USB device side cable detection;
85 
86 UWORD cComReadBuffer(UBYTE *pBuffer,UWORD Size);
87 UWORD cComWriteBuffer(UBYTE *pBuffer,UWORD Size);
88 UBYTE cComFindMailbox(UBYTE *pName, UBYTE *pNo);
89 
90 void cComSetMusbHdrcMode(void);
91 
92 #ifdef DEBUG
93  void cComPrintTxMsg(TXBUF *pTxBuf);
94 #endif
95 
96 
97 #define SIZEOFFILESIZE 8
98 
99 enum
100 {
102 };
103 
104 RESULT cComInit(void)
105 {
106  RESULT Result = FAIL;
107  UWORD TmpFileHandle;
108  UBYTE Cnt;
109  FILE *File;
110 
111 
112 #if (HARDWARE != SIMULATION)
113 
114  ComInstance.CommandReady = 0;
115  ComInstance.Cmdfd = open(COM_CMD_DEVICE_NAME, O_RDWR, 0666);
116 
117  if (ComInstance.Cmdfd >= 0)
118  {
119  memset(ComInstance.TxBuf[0].Buf,0,sizeof(ComInstance.TxBuf[0].Buf));
120 
121  Result = OK;
122  }
123  for (TmpFileHandle = 0;TmpFileHandle < MAX_FILE_HANDLES;TmpFileHandle++)
124  {
125  ComInstance.Files[TmpFileHandle].State = FS_IDLE;
126  ComInstance.Files[TmpFileHandle].Name[0] = 0;
127  ComInstance.Files[TmpFileHandle].File = -1;
128  }
129 
130  for(Cnt = 0; Cnt < NO_OF_CHS; Cnt++)
131  {
132  ComInstance.TxBuf[Cnt].BufSize = 1024;
133  ComInstance.TxBuf[Cnt].Writing = 0;
134  ComInstance.RxBuf[Cnt].BufSize = 1024;
135  ComInstance.RxBuf[Cnt].State = RXIDLE;
136  }
137 
138  ComInstance.ReadChannel[0] = cComReadBuffer;
139  ComInstance.ReadChannel[1] = NULL;
140  ComInstance.ReadChannel[2] = cBtReadCh0;
141  ComInstance.ReadChannel[3] = cBtReadCh1;
142  ComInstance.ReadChannel[4] = cBtReadCh2;
143  ComInstance.ReadChannel[5] = cBtReadCh3;
144  ComInstance.ReadChannel[6] = cBtReadCh4;
145  ComInstance.ReadChannel[7] = cBtReadCh5;
146  ComInstance.ReadChannel[8] = cBtReadCh6;
147  ComInstance.ReadChannel[9] = cBtReadCh7;
148  ComInstance.ReadChannel[10] = cWiFiReadTcp;
149 
150  ComInstance.WriteChannel[0] = cComWriteBuffer;
151  ComInstance.WriteChannel[1] = NULL;
152  ComInstance.WriteChannel[2] = cBtDevWriteBuf;
153  ComInstance.WriteChannel[3] = cBtDevWriteBuf1;
154  ComInstance.WriteChannel[4] = cBtDevWriteBuf2;
155  ComInstance.WriteChannel[5] = cBtDevWriteBuf3;
156  ComInstance.WriteChannel[6] = cBtDevWriteBuf4;
157  ComInstance.WriteChannel[7] = cBtDevWriteBuf5;
158  ComInstance.WriteChannel[8] = cBtDevWriteBuf6;
159  ComInstance.WriteChannel[9] = cBtDevWriteBuf7;
160  ComInstance.WriteChannel[10] = cWiFiWriteTcp;
161 
162  for(Cnt = 0; Cnt < NO_OF_MAILBOXES; Cnt++)
163  {
164  ComInstance.MailBox[Cnt].Status = FAIL;
165  ComInstance.MailBox[Cnt].DataSize = 0;
166  ComInstance.MailBox[Cnt].ReadCnt = 0;
167  ComInstance.MailBox[Cnt].WriteCnt = 0;
168  ComInstance.MailBox[Cnt].Name[0] = 0;
169  }
170 
171  ComInstance.ComResult = OK;
172 
173  ComInstance.BrickName[0] = 0;
174  File = fopen("./settings/BrickName","r");
175  if (File != NULL)
176  {
177 
178  fgets((char*)&(ComInstance.BrickName[0]), (int)vmBRICKNAMESIZE, File);
179  fclose (File);
180  }
181 
182  // USB cable stuff init
183  UsbConUpdate = 0; // Reset timing of USB device side cable detection;
184  cComUsbDeviceConnected = FALSE; // Until we believe in something else ;-)
186 
187  BtInit((char*)&(ComInstance.BrickName[0]));
188  cDaisyInit();
189  cWiFiInit();
190 
191  ComInstance.VmReady = 1;
192  ComInstance.ReplyStatus = 0;
193 
194  #else
195  return OK;
196  #endif
197 
198  return (Result);
199 
200 }
201 
202 
203 RESULT cComOpen(void)
204 {
205  RESULT Result = FAIL;
206 
207  Result = OK;
208 
209  return (Result);
210 }
211 
212 
213 RESULT cComClose(void)
214 {
215  RESULT Result = FAIL;
216  UBYTE Cnt;
217 
218  for(Cnt = 0; Cnt < NO_OF_MAILBOXES; Cnt++)
219  {
220  ComInstance.MailBox[Cnt].Status = FAIL;
221  ComInstance.MailBox[Cnt].DataSize = 0;
222  ComInstance.MailBox[Cnt].ReadCnt = 0;
223  ComInstance.MailBox[Cnt].WriteCnt = 0;
224  ComInstance.MailBox[Cnt].Name[0] = 0;
225  }
226 
227  Result = OK;
228 
229  return (Result);
230 }
231 
232 
233 RESULT cComExit(void)
234 {
235  RESULT Result = FAIL;
236 
237  close(ComInstance.Cmdfd);
238 
239  Result = OK;
240 
241  cWiFiExit();
242  BtExit();
243 
244  return (Result);
245 }
246 
247 // DAISY chain
248 // Write type data to chain
249 RESULT cComSetDeviceInfo(DATA8 Length,UBYTE *pInfo)
250 {
251  return cDaisySetDeviceInfo(Length, pInfo);
252  //return FAIL;
253 }
254 
255 // Read type data from chain
256 RESULT cComGetDeviceInfo(DATA8 Length,UBYTE *pInfo)
257 {
258  return cDaisyGetDeviceInfo(Length, pInfo);
259 }
260 
261 // Write mode to chain
262 RESULT cComSetDeviceType(DATA8 Layer,DATA8 Port,DATA8 Type,DATA8 Mode)
263 {
264  return cDaisySetDeviceType(Layer, Port, Type, Mode);
265 }
266 
267 // Read device data from chain
268 RESULT cComGetDeviceData(DATA8 Layer,DATA8 Port,DATA8 Length,DATA8 *pType,DATA8 *pMode,DATA8 *pData)
269 {
270  return cDaisyGetDownstreamData(Layer, Port, Length, pType, pMode, pData);
271 }
272 
274 {
275  // Force OTG into mode
276  char Musb_Cmd[64];
277  // Build the command string for setting the OTG mode
278  strcpy(Musb_Cmd, "echo otg > /sys/devices/platform/musb_hdrc/mode");
279  system(Musb_Cmd);
280 }
281 
283 {
284  // Get mode from MUSB_HDRC
285  UBYTE Result = FALSE;
286  char buffer[21];
287  FILE *f;
288 
289  f = fopen("/sys/devices/platform/musb_hdrc/mode", "r");
290  if(f)
291  {
292  fread(buffer, 20, 1, f);
293  fclose(f);
294  }
295 
296  //#define DEBUG
297  #undef DEBUG
298  #ifdef DEBUG
299  printf("BUFFER = %s\n\r", buffer);
300  #endif
301 
302  if(strstr(buffer, "b_peripheral") != 0)
303  Result = TRUE;
304 
305  //#define DEBUG
306  #undef DEBUG
307  #ifdef DEBUG
308  if(Result == TRUE)
309  printf("CABLE connected\n\r");
310  else
311  printf("CABLE dis-connected :-(\n\r");
312  #endif
313  // Just to be sure - we disables further prinf "polution"
314  #undef DEBUG
315 
316  return Result;
317 }
318 
319 #ifdef DEBUG
320 void cComShow(UBYTE *pB)
321 {
322  UWORD Lng;
323 
324  Lng = (UWORD)pB[0];
325  Lng += (UWORD)pB[1] << 8;
326 
327  printf("[%02X%02X",pB[0],pB[1]);
328  pB++;
329  pB++;
330 
331  while (Lng)
332  {
333  printf("%02X",*pB);
334  pB++;
335  Lng--;
336  }
337  printf("]\r\n");
338 }
339 
340 #endif
341 
342 
343 #ifdef DEBUG
344  void cComPrintTxMsg(TXBUF *pTxBuf)
345  {
346  ULONG Cnt, Cnt2;
347  COMRPL *pComRpl;
348 
349  pComRpl = (COMRPL*) pTxBuf->Buf;
350 
351  printf("Tx Buf content: \r\n");
352  for(Cnt = 0; ((Cnt < ((*pComRpl).CmdSize + 2)) && (Cnt < 1024)); Cnt++)
353  {
354  for(Cnt2 = Cnt; Cnt2 < (Cnt + 16); Cnt2++)
355  {
356  printf("0x%02X, ",((UBYTE*)(&((*pComRpl).CmdSize)))[Cnt2]);
357  }
358  Cnt = (Cnt2-1);
359  printf("\r\n");
360  }
361  printf("\r\n");
362  }
363 #endif
364 
365 
366 
368 {
369  UWORD Length = 0;
370 #if (HARDWARE != SIMULATION)
371 
372  struct timeval Cmdtv;
373  fd_set Cmdfds;
374 
375  Cmdtv.tv_sec = 0;
376  Cmdtv.tv_usec = 0;
377  FD_ZERO(&Cmdfds);
378  FD_SET(ComInstance.Cmdfd, &Cmdfds);
379  if (select(ComInstance.Cmdfd + 1, &Cmdfds, NULL, NULL, &Cmdtv))
380  {
381  Length = read(ComInstance.Cmdfd,pBuffer,(size_t)Size);
382 
383  #undef DEBUG
384  //#define DEBUG
385  #ifdef DEBUG
386  printf("cComReadBuffer Length = %d\r\n", Length);
387  #endif
388 
389  }
390 #endif
391  return (Length);
392 }
393 
394 
396 {
397  UWORD Length = 0;
398 #if (HARDWARE != SIMULATION)
399 
400 
401 
402  if(FULL_SPEED == cDaisyGetUsbUpStreamSpeed())
403  {
404  Length = write(ComInstance.Cmdfd,pBuffer,64);
405  }
406  else
407  Length = write(ComInstance.Cmdfd,pBuffer,1024);
408 
409  #undef DEBUG
410  //#define DEBUG
411  #ifdef DEBUG
412  printf("cComWriteBuffer %d\n\r", Length);
413  #endif
414 
415 #endif
416  return (Length);
417 }
418 
419 
421 {
422  UBYTE Result = 0;
423  COMCMD *pComCmd;
424  COMRPL *pComRpl;
425  DIRCMD *pDirCmd;
426  CMDSIZE CmdSize;
427  CMDSIZE HeadSize;
428  UWORD Tmp;
429  UWORD Globals;
430  UWORD Locals;
431  IMGHEAD *pImgHead;
432  OBJHEAD *pObjHead;
433  CMDSIZE Length;
434 
435  ComInstance.VmReady = 0;
436  pComCmd = (COMCMD*)pBuffer;
437  pDirCmd = (DIRCMD*)(*pComCmd).PayLoad;
438 
439  CmdSize = (*pComCmd).CmdSize;
440  HeadSize = ((*pDirCmd).Code - pBuffer) - sizeof(CMDSIZE);
441  Length = CmdSize - HeadSize;
442 
443  pComRpl = (COMRPL*)pReply;
444  (*pComRpl).CmdSize = 3;
445  (*pComRpl).MsgCnt = (*pComCmd).MsgCnt;
446  (*pComRpl).Cmd = DIRECT_REPLY_ERROR;
447 
448  if ((CmdSize > HeadSize) && ((CmdSize - HeadSize) < (sizeof(ComInstance.Image) - (sizeof(IMGHEAD) + sizeof(OBJHEAD)))))
449  {
450 
451  Tmp = (UWORD)(*pDirCmd).Globals + ((UWORD)(*pDirCmd).Locals << 8);
452 
453  Globals = (Tmp & 0x3FF);
454  Locals = ((Tmp >> 10) & 0x3F);
455 
456  if ((Locals <= MAX_COMMAND_LOCALS) && (Globals <= MAX_COMMAND_GLOBALS))
457  {
458 
459  pImgHead = (IMGHEAD*)ComInstance.Image;
460  pObjHead = (OBJHEAD*)(ComInstance.Image + sizeof(IMGHEAD));
461 
462  (*pImgHead).Sign[0] = 'l';
463  (*pImgHead).Sign[1] = 'e';
464  (*pImgHead).Sign[2] = 'g';
465  (*pImgHead).Sign[3] = 'o';
466  (*pImgHead).VersionInfo = (UWORD)(VERS * 100.0);
467  (*pImgHead).NumberOfObjects = (OBJID)1;
468  (*pImgHead).GlobalBytes = (GBINDEX)Globals;
469 
470  (*pObjHead).OffsetToInstructions = (IP)(sizeof(IMGHEAD) + sizeof(OBJHEAD));
471  (*pObjHead).OwnerObjectId = 0;
472  (*pObjHead).TriggerCount = 0;
473  (*pObjHead).LocalBytes = (OBJID)Locals;
474 
475  memcpy(&ComInstance.Image[sizeof(IMGHEAD) + sizeof(OBJHEAD)],(*pDirCmd).Code,Length);
476  Length += sizeof(IMGHEAD) + sizeof(OBJHEAD);
477 
478  ComInstance.Image[Length] = opOBJECT_END;
479  (*pImgHead).ImageSize = Length;
480 
481  //#define DEBUG
482  #undef DEBUG
483  #ifdef DEBUG
484  printf("\r\n");
485  for (Tmp = 0;Tmp <= Length;Tmp++)
486  {
487  printf("%02X ",ComInstance.Image[Tmp]);
488  if ((Tmp & 0x0F) == 0x0F)
489  {
490  printf("\r\n");
491  }
492  }
493  printf("\r\n");
494  #endif
495 
496  Result = 1;
497  }
498  else
499  {
500  (*pComRpl).Cmd = DIRECT_REPLY_ERROR;
501  }
502  }
503  else
504  {
505  (*pComRpl).Cmd = DIRECT_REPLY_ERROR;
506  }
507 
508  return (Result);
509 }
510 
511 
513 {
514  if (*pHandle >= MIN_HANDLE)
515  {
516  close(*pHandle);
517  *pHandle = -1;
518  }
519 }
520 
521 
523 {
524  UBYTE RtnVal = TRUE;
525 
526  if (0 != ComInstance.Files[Handle].Name[0])
527  {
528  if ((Handle >= 0) && (Handle < MAX_FILE_HANDLES))
529  {
530  ComInstance.Files[Handle].Name[0] = 0;
531  }
532  }
533  else
534  {
535  // Handle is unused
536  RtnVal = FALSE;
537  }
538  return(RtnVal);
539 }
540 
541 
542 DATA8 cComGetHandle(char *pName)
543 {
544  DATA8 Result = 0;
545 
546  while ((ComInstance.Files[Result].Name[0]) && (Result < MAX_FILE_HANDLES))
547  {
548  Result++;
549  }
550 
551  if (Result < MAX_FILE_HANDLES)
552  {
553  if (OK == cMemoryCheckFilename(pName, NULL, NULL, NULL))
554  {
555  sprintf(ComInstance.Files[Result].Name, "%s", (char*)pName);
556  }
557  else
558  {
559  Result = -1;
560  }
561  }
562  else
563  {
564  Result = -1;
565  }
566  return (Result);
567 }
568 
569 
571 {
572  UBYTE RtnVal = 0;
573  struct dirent *pDirPtr;
574 
575  pDirPtr = readdir(pDir);
576  while((NULL != pDirPtr) && (DT_REG != pDirPtr->d_type))
577  {
578  pDirPtr = readdir(pDir);
579  }
580 
581  if(NULL != pDirPtr)
582  {
583  snprintf((char*)pName, FILENAME_SIZE,"%s",pDirPtr->d_name);
584  RtnVal = 1;
585  }
586  else
587  {
588  closedir(pDir);
589  }
590 
591  return(RtnVal);
592 }
593 
594 
595 void cComCreateBeginDl( TXBUF *pTxBuf, UBYTE *pName)
596 {
597  UWORD Index;
598  UWORD ReadBytes;
599  BEGIN_DL *pDlMsg;
600  SBYTE FileHandle;
601  char TmpFileName[vmFILENAMESIZE];
602 
603  FileHandle = -1;
604  pDlMsg = (BEGIN_DL*) &(pTxBuf->Buf[0]);
605 
606  if ((strlen((char *)pTxBuf->Folder) + strlen((char *)pName) + 1) <= vmFILENAMESIZE)
607  {
608  sprintf(TmpFileName, "%s", (char*)pTxBuf->Folder);
609  strcat(TmpFileName, (char*)pName);
610 
611  FileHandle = cComGetHandle(TmpFileName);
612  }
613 
614  if (-1 != FileHandle)
615  {
616 
617  pTxBuf->pFile = (FIL*)&(ComInstance.Files[FileHandle]);
618  pTxBuf->pFile->File = open(pTxBuf->pFile->Name,O_RDONLY,0x444);
619 
620  // Get file length
621  pTxBuf->pFile->Size = lseek(pTxBuf->pFile->File, 0L, SEEK_END);
622  lseek(pTxBuf->pFile->File, 0L, SEEK_SET);
623 
624  pDlMsg->CmdSize = 0x00; // Msg Len
625  pDlMsg->MsgCount = 0x00; // Msg Count
626  pDlMsg->CmdType = SYSTEM_COMMAND_REPLY; // Cmd Type
627  pDlMsg->Cmd = BEGIN_DOWNLOAD; // Cmd
628 
629  pDlMsg->FileSizeLsb = (UBYTE)(pTxBuf->pFile->Size); // File Size Lsb
630  pDlMsg->FileSizeNsb1 = (UBYTE)(pTxBuf->pFile->Size >> 8); // File Size
631  pDlMsg->FileSizeNsb2 = (UBYTE)(pTxBuf->pFile->Size >> 16); // File Size
632  pDlMsg->FileSizeMsb = (UBYTE)(pTxBuf->pFile->Size >> 24); // File Size Msb
633 
634  Index = strlen((char*)pTxBuf->pFile->Name) + 1;
635  snprintf((char*)(&(pTxBuf->Buf[SIZEOF_BEGINDL])), Index, "%s", (char*)pTxBuf->pFile->Name);
636 
637  Index += SIZEOF_BEGINDL;
638 
639  // Find the number of bytes to go into this message
640  if ((MAX_MSG_SIZE - Index) < pTxBuf->pFile->Size)
641  {
642  // Msg cannot hold complete file
643  pTxBuf->Buf[0] = (UBYTE)(MAX_MSG_SIZE - 2);
644  pTxBuf->Buf[1] = (UBYTE)((MAX_MSG_SIZE - 2) >> 8);
645 
646  ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[Index]), (pTxBuf->BufSize - Index));
647  pTxBuf->BlockLen = pTxBuf->BufSize;
648 
650  pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
651  pTxBuf->pFile->Pointer = ReadBytes; // No of bytes read from file
652  pTxBuf->MsgLen = (pDlMsg->CmdSize - Index) + 2; // Index = full header size
653  pTxBuf->Writing = 1;
654  }
655  else
656  {
657  // Msg can hold complete file
658  pTxBuf->Buf[0] = (UBYTE) (pTxBuf->pFile->Size + Index - 2);
659  pTxBuf->Buf[1] = (UBYTE)((pTxBuf->pFile->Size + Index - 2) >> 8);
660 
661  if ((pTxBuf->BufSize - Index) < pTxBuf->pFile->Size)
662  {
663  // Complete msg exceeds buffer size
664  ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[Index]), (pTxBuf->BufSize - Index));
665  pTxBuf->BlockLen = pTxBuf->BufSize;
666  }
667  else
668  {
669  // Complete msg can fit in buffer
670  ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[Index]), pTxBuf->pFile->Size);
671  pTxBuf->BlockLen = pTxBuf->pFile->Size + Index;
672 
673  // Close handles
674  cComCloseFileHandle(&(pTxBuf->pFile->File));
675  cComFreeHandle(FileHandle);
676  }
677 
679  pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
680  pTxBuf->pFile->Pointer = ReadBytes; // No of bytes read from file
681  pTxBuf->MsgLen = (pDlMsg->CmdSize - Index) + 2; // Index = full header size
682  pTxBuf->Writing = 1;
683  }
684  }
685 }
686 
687 void cComCreateContinueDl(RXBUF *pRxBuf, TXBUF *pTxBuf)
688 {
689  UWORD ReadBytes;
690  CONTINUE_DL *pContinueDl;
691  RPLY_BEGIN_DL *pRplyBeginDl;
692 
693  pRplyBeginDl = (RPLY_BEGIN_DL*)&(pRxBuf->Buf[0]);
694  pContinueDl = (CONTINUE_DL*)&(pTxBuf->Buf[0]);
695 
696  pContinueDl->CmdSize = SIZEOF_CONTINUEDL - sizeof(CMDSIZE);
697  pContinueDl->MsgCount = (pRplyBeginDl->MsgCount)++;
698  pContinueDl->CmdType = SYSTEM_COMMAND_REPLY;
699  pContinueDl->Cmd = CONTINUE_DOWNLOAD;
700  pContinueDl->Handle = pTxBuf->RemoteFileHandle;
701 
702  if ((MAX_MSG_SIZE - SIZEOF_CONTINUEDL) < (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer))
703  {
704  // Msg cannot hold complete file
705  ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[SIZEOF_CONTINUEDL]), (pTxBuf->BufSize - SIZEOF_CONTINUEDL));
706  pTxBuf->BlockLen = pTxBuf->BufSize;
707 
708  pContinueDl->CmdSize += ReadBytes;
710  pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
711  pTxBuf->pFile->Pointer += ReadBytes; // No of bytes read from file
712  pTxBuf->MsgLen = (pContinueDl->CmdSize - SIZEOF_CONTINUEDL) + 2; // Index = full header size
713  pTxBuf->Writing = 1;
714  }
715  else
716  {
717  // Msg can hold complete file
718  if ((pTxBuf->BufSize - SIZEOF_CONTINUEDL) < (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer))
719  {
720  // Complete msg exceeds buffer size
721  ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[SIZEOF_CONTINUEDL]), (pTxBuf->BufSize - SIZEOF_CONTINUEDL));
722  }
723  else
724  {
725  // Complete msg can fit in buffer
726  ReadBytes = read(pTxBuf->pFile->File, &(pTxBuf->Buf[SIZEOF_CONTINUEDL]), (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer));
727 
728  // Close handles
729  cComCloseFileHandle(&(pTxBuf->pFile->File));
730  cComFreeHandle(pTxBuf->FileHandle);
731  }
732 
733  pTxBuf->BlockLen = ReadBytes + SIZEOF_CONTINUEDL;
734  pContinueDl->CmdSize += ReadBytes;
736  pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
737  pTxBuf->pFile->Pointer += ReadBytes; // No of bytes read from file
738  pTxBuf->MsgLen = (pContinueDl->CmdSize - SIZEOF_CONTINUEDL) + 2; // Index = full header size
739  pTxBuf->Writing = 1;
740  }
741 }
742 
743 
744 void cComSystemReply(RXBUF *pRxBuf, TXBUF *pTxBuf)
745 {
746  COMCMD *pComCmd;
747  SYSCMDC *pSysCmdC;
748  CMDSIZE CmdSize;
749  UBYTE FileName[MAX_FILENAME_SIZE];
750 
751  pComCmd = (COMCMD*)pRxBuf->Buf;
752  pSysCmdC = (SYSCMDC*)(*pComCmd).PayLoad;
753  CmdSize = (*pComCmd).CmdSize;
754 
755  switch ((*pSysCmdC).Sys)
756  {
757  case BEGIN_DOWNLOAD:
758  {
759  // This is the reply from the begin download command
760  // This is part of the file transfer sequence
761  // Check for single file or Folder transfer
762 
763  RPLY_BEGIN_DL *pRplyBeginDl;
764 
765  pRplyBeginDl = (RPLY_BEGIN_DL*)&(pRxBuf->Buf[0]);
766 
767  if ((SUCCESS == pRplyBeginDl->Status) || (END_OF_FILE == pRplyBeginDl->Status))
768  {
769  pTxBuf->RemoteFileHandle = pRplyBeginDl->Handle;
770 
772  {
773  // Issue continue write as file is not
774  // completely downloaded
775  cComCreateContinueDl(pRxBuf, pTxBuf);
776  }
777  else
778  {
779  if (FILE_COMPLETE_WAIT_FOR_REPLY == pTxBuf->SubState)
780  {
781  // Complete file downloaded check for more files
782  // to download
783  if (TXFOLDER == pTxBuf->State)
784  {
785  if (cComGetNxtFile(pTxBuf->pDir, FileName))
786  {
787  cComCreateBeginDl(pTxBuf, FileName);
788  }
789  else
790  {
791  // No More files
792  pTxBuf->State = TXIDLE;
793  pTxBuf->SubState = SUBSTATE_IDLE;
794  ComInstance.ComResult = OK;
795  }
796  }
797  else
798  {
799  // Only one file to send
800  pTxBuf->State = TXIDLE;
801  pTxBuf->SubState = SUBSTATE_IDLE;
802  ComInstance.ComResult = OK;
803  }
804  }
805  }
806  }
807  }
808  break;
809 
810  case CONTINUE_DOWNLOAD:
811  {
812 
813  RPLY_CONTINUE_DL *pRplyContinueDl;
814  pRplyContinueDl = (RPLY_CONTINUE_DL*)&(pRxBuf->Buf[0]);
815 
816  if ((SUCCESS == pRplyContinueDl->Status) || (END_OF_FILE == pRplyContinueDl->Status))
817  {
819  {
820  // Issue continue write as file is not
821  // completely downloaded
822  cComCreateContinueDl(pRxBuf, pTxBuf);
823  }
824  else
825  {
826  if (FILE_COMPLETE_WAIT_FOR_REPLY == pTxBuf->SubState)
827  {
828  // Complete file downloaded check for more files
829  // to download
830  if (TXFOLDER == pTxBuf->State)
831  {
832  if (cComGetNxtFile(pTxBuf->pDir, FileName))
833  {
834  cComCreateBeginDl(pTxBuf, FileName);
835  }
836  else
837  {
838  pTxBuf->State = TXIDLE;
839  pTxBuf->SubState = SUBSTATE_IDLE;
840  ComInstance.ComResult = OK;
841  }
842  }
843  else
844  {
845  pTxBuf->State = TXIDLE;
846  pTxBuf->SubState = SUBSTATE_IDLE;
847  ComInstance.ComResult = OK;
848  }
849  }
850  }
851  }
852  }
853  break;
854 
855  default:
856  {
857  }
858  break;
859  }
860 }
861 
862 
863 void cComGetNameFromScandirList(struct dirent *NameList, char *pBuffer, ULONG *pNameLen, UBYTE *pFolder)
864 {
865  char FileName[MD5LEN + 1 + FILENAMESIZE];
866  struct stat FileInfo;
867  ULONG Md5Sum[4];
868 
869  *pNameLen = 0;
870 
871  //If type is a directory the add "/" at the end
872  if ((DT_LNK == NameList->d_type) || (DT_DIR == NameList->d_type))
873  {
874  strncpy(pBuffer, NameList->d_name, FILENAMESIZE); // Need to copy to tmp var to be able to add "/" for folders
875  *pNameLen = strlen(NameList->d_name); // + 1 is the new line character
876 
877  strcat(pBuffer,"/");
878  (*pNameLen)++;
879 
880  strcat(pBuffer,"\n");
881  (*pNameLen)++;
882 
883  }
884  else
885  {
886 
887  if (DT_REG == NameList->d_type)
888  {
889  /* If it is a file then add 16 bytes MD5SUM + space */
890  strcpy(FileName, (char*)pFolder);
891  strcat(FileName, "/");
892  strcat(FileName,NameList->d_name);
893 
894  /* Get the MD5sum and put in the buffer */
895  md5_file(FileName, 0, (unsigned char *)Md5Sum);
896  *pNameLen = sprintf(pBuffer, "%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X%02X ",
897  ((UBYTE*)Md5Sum)[0] , ((UBYTE*)Md5Sum)[1] , ((UBYTE*)Md5Sum)[2] , ((UBYTE*)Md5Sum)[3] ,
898  ((UBYTE*)Md5Sum)[4] , ((UBYTE*)Md5Sum)[5] , ((UBYTE*)Md5Sum)[6] , ((UBYTE*)Md5Sum)[7] ,
899  ((UBYTE*)Md5Sum)[8] , ((UBYTE*)Md5Sum)[9] , ((UBYTE*)Md5Sum)[10], ((UBYTE*)Md5Sum)[11],
900  ((UBYTE*)Md5Sum)[12], ((UBYTE*)Md5Sum)[13], ((UBYTE*)Md5Sum)[14], ((UBYTE*)Md5Sum)[15]);
901 
902  /* Insert file size */
903  stat(FileName,&FileInfo);
904  *pNameLen += sprintf(&(pBuffer[MD5LEN + 1]), "%08X ",(ULONG)FileInfo.st_size);
905 
906  /* Insert Filename */
907  strcat(pBuffer, NameList->d_name);
908  *pNameLen += strlen(NameList->d_name);
909 
910  strcat(pBuffer,"\n");
911  (*pNameLen)++;
912  }
913  }
914 }
915 
916 
917 UBYTE cComCheckForSpace(char *pFullName, ULONG Size)
918 {
919  UBYTE RtnVal;
920  DATA32 TotalSize;
921  DATA32 FreeSize;
922  DATA8 Changed;
923 
924  RtnVal = FAIL;
925 
926  //Make it KB and round up
927  if (Size & (ULONG)(KB-1))
928  {
929  Size /= KB;
930  Size += 1;
931  }
932  else
933  {
934  Size /= KB;
935  }
936 
937  if (strstr(pFullName, SDCARD_FOLDER) != NULL)
938  {
939  if (CheckSdcard(&Changed, &TotalSize, &FreeSize, 1))
940  {
941  if (FreeSize >= Size)
942  {
943  RtnVal = OK;
944  }
945  }
946  }
947  else
948  {
949  if (strstr(pFullName, USBSTICK_FOLDER) != NULL)
950  {
951  if (CheckUsbstick(&Changed, &TotalSize, &FreeSize, 1))
952  {
953  if (FreeSize >= Size)
954  {
955  RtnVal = OK;
956  }
957  }
958  }
959  else
960  {
961  cMemoryGetUsage(&TotalSize, &FreeSize, 1);
962  if (FreeSize >= Size + 300)
963  {
964  RtnVal = OK;
965  }
966  }
967  }
968  return(RtnVal);
969 }
970 
971 
1037 void cComSystemCommand(RXBUF *pRxBuf, TXBUF *pTxBuf)
1038 {
1039  COMCMD *pComCmd;
1040  SYSCMDC *pSysCmdC;
1041  CMDSIZE CmdSize;
1042  int Tmp;
1043  char Folder[60];
1044  ULONG BytesToWrite;
1045  DATA8 FileHandle;
1046 
1047  pComCmd = (COMCMD*)pRxBuf->Buf;
1048  pSysCmdC = (SYSCMDC*)(*pComCmd).PayLoad;
1049  CmdSize = (*pComCmd).CmdSize;
1050 
1051  switch ((*pSysCmdC).Sys)
1052  {
1053  case BEGIN_DOWNLOAD :
1054  {
1055  BEGIN_DL *pBeginDl;
1056  RPLY_BEGIN_DL *pReplyDl;
1057  ULONG MsgHeaderSize;
1058 
1059  //Setup pointers
1060  pBeginDl = (BEGIN_DL*)pRxBuf->Buf;
1061  pReplyDl = (RPLY_BEGIN_DL*)pTxBuf->Buf;
1062 
1063  // Get file handle
1064  FileHandle = cComGetHandle((char*)pBeginDl->Path);
1065  pRxBuf->pFile = &(ComInstance.Files[FileHandle]);
1066 
1067  // Fill the reply
1068  pReplyDl->CmdSize = SIZEOF_RPLYBEGINDL - sizeof(CMDSIZE);
1069  pReplyDl->MsgCount = pBeginDl->MsgCount;
1070  pReplyDl->CmdType = SYSTEM_REPLY;
1071  pReplyDl->Cmd = BEGIN_DOWNLOAD;
1072  pReplyDl->Status = SUCCESS;
1073  pReplyDl->Handle = FileHandle;
1074 
1075  if (FileHandle >= 0)
1076  {
1077  pRxBuf->pFile->Size = (ULONG)(pBeginDl->FileSizeLsb);
1078  pRxBuf->pFile->Size += (ULONG)(pBeginDl->FileSizeNsb1) << 8;
1079  pRxBuf->pFile->Size += (ULONG)(pBeginDl->FileSizeNsb2) << 16;
1080  pRxBuf->pFile->Size += (ULONG)(pBeginDl->FileSizeMsb) << 24;
1081 
1082  if (OK == cComCheckForSpace(&(ComInstance.Files[FileHandle].Name[0]), pRxBuf->pFile->Size))
1083  {
1084 
1085  pRxBuf->pFile->Length = (ULONG)0;
1086  pRxBuf->pFile->Pointer = (ULONG)0;
1087 
1088  Tmp = 0;
1089  while ((ComInstance.Files[FileHandle].Name[Tmp]) && (Tmp < (MAX_FILENAME_SIZE - 1)))
1090  {
1091  Folder[Tmp] = ComInstance.Files[FileHandle].Name[Tmp];
1092  if (Folder[Tmp] == '/')
1093  {
1094  Folder[Tmp + 1] = 0;
1095  if (strcmp("~/",Folder) != 0)
1096  {
1097  if (mkdir(Folder,S_IRWXU | S_IRWXG | S_IRWXO) == 0)
1098  {
1099  chmod(Folder,S_IRWXU | S_IRWXG | S_IRWXO);
1100  #ifdef DEBUG
1101  printf("Folder %s created\r\n",Folder);
1102  #endif
1103  }
1104  else
1105  {
1106  #ifdef DEBUG
1107  printf("Folder %s not created (%s)\r\n",Folder,strerror(errno));
1108  #endif
1109  }
1110  }
1111  }
1112  Tmp++;
1113  }
1114 
1115  pRxBuf->pFile->File = open(pRxBuf->pFile->Name, O_CREAT | O_WRONLY | O_TRUNC | O_SYNC, 0x666);
1116 
1117  if (pRxBuf->pFile->File >= 0)
1118  {
1119  MsgHeaderSize = (strlen((char*)pBeginDl->Path) + 1 + SIZEOF_BEGINDL); // +1 = zero termination
1120  pRxBuf->MsgLen = CmdSize + sizeof(CMDSIZE) - MsgHeaderSize;
1121 
1122  if (CmdSize <= (pRxBuf->BufSize - sizeof(CMDSIZE)))
1123  {
1124  // All data included in this buffer
1125  BytesToWrite = pRxBuf->MsgLen;
1126  pRxBuf->State = RXIDLE;
1127  }
1128  else
1129  {
1130  // Rx buffer must be read more that one to receive the complete message
1131  BytesToWrite = pRxBuf->BufSize - MsgHeaderSize;
1132  pRxBuf->State = RXFILEDL;
1133  }
1134  pTxBuf->BlockLen = SIZEOF_RPLYBEGINDL;
1135 
1136  if (BytesToWrite > (ComInstance.Files[FileHandle].Size))
1137  {
1138  // If Bytes to write into the file is bigger than file size -> Error message
1139  pReplyDl->Status = SIZE_ERROR;
1140  BytesToWrite = ComInstance.Files[FileHandle].Size;
1141  }
1142 
1143  write(pRxBuf->pFile->File, &(pRxBuf->Buf[MsgHeaderSize]), (size_t)BytesToWrite);
1144  ComInstance.Files[FileHandle].Length += (ULONG)BytesToWrite;
1145  pRxBuf->RxBytes = (ULONG)BytesToWrite;
1146  pRxBuf->pFile->Pointer = (ULONG)BytesToWrite;
1147 
1148  if (pRxBuf->pFile->Pointer >= pRxBuf->pFile->Size)
1149  {
1150  cComCloseFileHandle(&(pRxBuf->pFile->File));
1151  chmod(ComInstance.Files[FileHandle].Name,S_IRWXU | S_IRWXG | S_IRWXO);
1152  cComFreeHandle(FileHandle);
1153 
1154  pReplyDl->Status = END_OF_FILE;
1155  pRxBuf->State = RXIDLE;
1156  }
1157  }
1158  else
1159  {
1160  // Error in opening file
1161  #ifdef DEBUG
1162  printf("File %s not created\r\n",ComInstance.Files[FileHandle].Name);
1163  #endif
1164  cComFreeHandle(FileHandle);
1165  pReplyDl->CmdType = SYSTEM_REPLY_ERROR;
1166  pReplyDl->Status = UNKNOWN_HANDLE;
1167  pReplyDl->Handle = -1;
1168  pTxBuf->BlockLen = SIZEOF_RPLYBEGINDL;
1169 
1170  }
1171  }
1172  else
1173  {
1174  //Not enough space for the file
1175  #ifdef DEBUG
1176  printf("File %s is too big\r\n",ComInstance.Files[FileHandle].Name);
1177  #endif
1178 
1179  cComFreeHandle(FileHandle);
1180  pReplyDl->CmdType = SYSTEM_REPLY_ERROR;
1181  pReplyDl->Status = SIZE_ERROR;
1182  pReplyDl->Handle = -1;
1183  pTxBuf->BlockLen = SIZEOF_RPLYBEGINDL;
1184  }
1185  }
1186  else
1187  {
1188  // Error in getting a handle
1189  pReplyDl->CmdType = SYSTEM_REPLY_ERROR;
1190  pReplyDl->Status = NO_HANDLES_AVAILABLE;
1191  pReplyDl->Handle = -1;
1192  pTxBuf->BlockLen = SIZEOF_RPLYBEGINDL;
1193 
1194  #ifdef DEBUG
1195  printf("No more handles\r\n");
1196  #endif
1197  }
1198  }
1199  break;
1200 
1201  case CONTINUE_DOWNLOAD :
1202  {
1203  CONTINUE_DL *pContiDl;
1204  RPLY_CONTINUE_DL *pRplyContiDl;
1205  ULONG BytesToWrite;
1206 
1207  //Setup pointers
1208  pContiDl = (CONTINUE_DL*)pComCmd;
1209  pRplyContiDl = (RPLY_CONTINUE_DL*)pTxBuf->Buf;
1210 
1211  // Get handle
1212  FileHandle = pContiDl->Handle;
1213 
1214  // Fill the reply
1215  pRplyContiDl->CmdSize = SIZEOF_RPLYCONTINUEDL - sizeof(CMDSIZE);
1216  pRplyContiDl->MsgCount = pContiDl->MsgCount;
1217  pRplyContiDl->CmdType = SYSTEM_REPLY;
1218  pRplyContiDl->Cmd = CONTINUE_DOWNLOAD;
1219  pRplyContiDl->Status = SUCCESS;
1220  pRplyContiDl->Handle = FileHandle;
1221 
1222  if ((FileHandle >= 0) && (ComInstance.Files[FileHandle].Name[0]))
1223  {
1224 
1225  pRxBuf->FileHandle = FileHandle;
1226  pRxBuf->pFile = &(ComInstance.Files[FileHandle]);
1227 
1228  if (pRxBuf->pFile->File >= 0)
1229  {
1230 
1231  pRxBuf->MsgLen = (pContiDl->CmdSize + sizeof(CMDSIZE)) - SIZEOF_CONTINUEDL;
1232  pRxBuf->RxBytes = 0;
1233 
1234  if (pContiDl->CmdSize <= (pRxBuf->BufSize - sizeof(CMDSIZE)))
1235  {
1236  // All data included in this buffer
1237  BytesToWrite = pRxBuf->MsgLen;
1238  pRxBuf->State = RXIDLE;
1239  }
1240  else
1241  {
1242  // Rx buffer must be read more that one to receive the complete message
1243  BytesToWrite = pRxBuf->BufSize - SIZEOF_CONTINUEDL;
1244  pRxBuf->State = RXFILEDL;
1245  }
1246  pTxBuf->BlockLen = SIZEOF_RPLYCONTINUEDL;
1247 
1248  if ((pRxBuf->pFile->Pointer + pRxBuf->MsgLen) > pRxBuf->pFile->Size)
1249  {
1250  BytesToWrite = pRxBuf->pFile->Size - pRxBuf->pFile->Pointer;
1251 
1252  #ifdef DEBUG
1253  printf("File size limited\r\n");
1254  #endif
1255  }
1256 
1257  write(ComInstance.Files[FileHandle].File, (pContiDl->PayLoad), (size_t)BytesToWrite);
1258  pRxBuf->pFile->Pointer += BytesToWrite;
1259  pRxBuf->RxBytes = BytesToWrite;
1260 
1261  #ifdef DEBUG
1262  printf("Size %8lu - Loaded %8lu\r\n",(unsigned long)ComInstance.Files[FileHandle].Size,(unsigned long)ComInstance.Files[FileHandle].Length);
1263  #endif
1264 
1265  if (pRxBuf->pFile->Pointer >= ComInstance.Files[FileHandle].Size)
1266  {
1267  #ifdef DEBUG
1268  printf("%s %lu bytes downloaded\r\n",ComInstance.Files[FileHandle].Name,(unsigned long)ComInstance.Files[FileHandle].Length);
1269  #endif
1270 
1271  cComCloseFileHandle(&(ComInstance.Files[FileHandle].File));
1272  chmod(ComInstance.Files[FileHandle].Name,S_IRWXU | S_IRWXG | S_IRWXO);
1273  cComFreeHandle(FileHandle);
1274  pRplyContiDl->Status = END_OF_FILE;
1275  }
1276  }
1277  else
1278  {
1279  // Illegal file
1280  pRplyContiDl->CmdType = SYSTEM_REPLY_ERROR;
1281  pRplyContiDl->Status = UNKNOWN_ERROR;
1282  pRplyContiDl->Handle = -1;
1283  pTxBuf->BlockLen = SIZEOF_RPLYCONTINUEDL;
1284  cComFreeHandle(FileHandle);
1285 
1286  #ifdef DEBUG
1287  printf("Data not appended\r\n");
1288  #endif
1289  }
1290  }
1291  else
1292  {
1293  // Illegal file handle
1294  pRplyContiDl->CmdType = SYSTEM_REPLY_ERROR;
1295  pRplyContiDl->Status = UNKNOWN_HANDLE;
1296  pRplyContiDl->Handle = -1;
1297  pTxBuf->BlockLen = SIZEOF_RPLYCONTINUEDL;
1298 
1299  #ifdef DEBUG
1300  printf("Invalid handle\r\n");
1301  #endif
1302  }
1303  }
1304  break;
1305 
1306  case BEGIN_UPLOAD:
1307  {
1308  ULONG BytesToRead;
1309  ULONG ReadBytes;
1310 
1311  BEGIN_READ *pBeginRead;;
1312  RPLY_BEGIN_READ *pReplyBeginRead;
1313 
1314  //Setup pointers
1315  pBeginRead = (BEGIN_READ*)pRxBuf->Buf;
1316  pReplyBeginRead = (RPLY_BEGIN_READ*)pTxBuf->Buf;
1317 
1318  FileHandle = cComGetHandle((char*)pBeginRead->Path);
1319 
1320  pTxBuf->pFile = &ComInstance.Files[FileHandle]; // Insert the file pointer into the ch struct
1321  pTxBuf->FileHandle = FileHandle; // Also save the File handle number
1322 
1323  pReplyBeginRead->CmdSize = SIZEOF_RPLYBEGINREAD - sizeof(CMDSIZE);
1324  pReplyBeginRead->MsgCount = pBeginRead->MsgCount;
1325  pReplyBeginRead->CmdType = SYSTEM_REPLY;
1326  pReplyBeginRead->Cmd = BEGIN_UPLOAD;
1327  pReplyBeginRead->Status = SUCCESS;
1328  pReplyBeginRead->Handle = FileHandle;
1329 
1330  if (FileHandle >= 0)
1331  {
1332  BytesToRead = (ULONG)(pBeginRead->BytesToReadLsb);
1333  BytesToRead += (ULONG)(pBeginRead->BytesToReadMsb) << 8;
1334 
1335  pTxBuf->pFile->File = open(pTxBuf->pFile->Name,O_RDONLY,0x444);
1336 
1337  if (pTxBuf->pFile->File >= 0)
1338  {
1339  // Get file length
1340  pTxBuf->pFile->Size = lseek(pTxBuf->pFile->File, 0L, SEEK_END);
1341  lseek(pTxBuf->pFile->File, 0L, SEEK_SET);
1342 
1343  pTxBuf->MsgLen = BytesToRead;
1344 
1345  if (BytesToRead > pTxBuf->pFile->Size)
1346  {
1347  // if message length > filesize then crop message
1348  pTxBuf->MsgLen = pTxBuf->pFile->Size;
1349  }
1350 
1351  if ((pTxBuf->MsgLen + SIZEOF_RPLYBEGINREAD) <= pTxBuf->BufSize)
1352  {
1353  // Read all requested bytes as they can fit into the buffer
1354  ReadBytes = read(pTxBuf->pFile->File, pReplyBeginRead->PayLoad, pTxBuf->MsgLen);
1355  pTxBuf->BlockLen = pTxBuf->MsgLen + SIZEOF_RPLYBEGINREAD;
1356  pTxBuf->State = TXIDLE;
1357  }
1358  else
1359  {
1360  // Read only up to full buffer size
1361  ReadBytes = read(pTxBuf->pFile->File, pReplyBeginRead->PayLoad, (pTxBuf->BufSize - SIZEOF_RPLYBEGINREAD));
1362  pTxBuf->BlockLen = pTxBuf->BufSize - SIZEOF_RPLYBEGINREAD;
1363  pTxBuf->State = TXFILEUPLOAD;
1364  }
1365 
1366  pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
1367  pTxBuf->pFile->Pointer = ReadBytes; // No of bytes read from file
1368 
1369  #ifdef DEBUG
1370  printf("Bytes to read %d bytes read %d\r\n",BytesToRead, ReadBytes);
1371  printf("FileSize: %d \r\n",pTxBuf->pFile->Size);
1372  #endif
1373 
1374  pReplyBeginRead->CmdSize += (CMDSIZE)pTxBuf->MsgLen;
1375  pReplyBeginRead->FileSizeLsb = (UBYTE)(pTxBuf->pFile->Size);
1376  pReplyBeginRead->FileSizeNsb1 = (UBYTE)(pTxBuf->pFile->Size >> 8);
1377  pReplyBeginRead->FileSizeNsb2 = (UBYTE)(pTxBuf->pFile->Size >> 16);
1378  pReplyBeginRead->FileSizeMsb = (UBYTE)(pTxBuf->pFile->Size >> 24);
1379 
1380  if (pTxBuf->pFile->Pointer >= pTxBuf->pFile->Size)
1381  {
1382  #ifdef DEBUG
1383  printf("%s %lu bytes UpLoaded\r\n",pTxBuf->pFile->Name,(unsigned long)pTxBuf->pFile->Length);
1384  #endif
1385 
1386  pReplyBeginRead->Status = END_OF_FILE;
1387  cComCloseFileHandle(&(pTxBuf->pFile->File));
1388  cComFreeHandle(FileHandle);
1389  }
1390 
1391  #ifdef DEBUG
1392  cComPrintTxMsg(pTxBuf);
1393  #endif
1394 
1395  }
1396  else
1397  {
1398  pReplyBeginRead->CmdType = SYSTEM_REPLY_ERROR;
1399  pReplyBeginRead->Status = UNKNOWN_HANDLE;
1400  pReplyBeginRead->Handle = -1;
1401  pTxBuf->BlockLen = SIZEOF_RPLYBEGINREAD;
1402  cComFreeHandle(FileHandle);
1403 
1404  #ifdef DEBUG
1405  printf("File %s is not present \r\n",ComInstance.Files[FileHandle].Name);
1406  #endif
1407  }
1408  }
1409  else
1410  {
1411  pReplyBeginRead->CmdType = SYSTEM_REPLY_ERROR;
1412  pReplyBeginRead->Status = NO_HANDLES_AVAILABLE;
1413  pReplyBeginRead->Handle = -1;
1414  pTxBuf->BlockLen = SIZEOF_RPLYBEGINREAD;
1415 
1416  #ifdef DEBUG
1417  printf("No more handles\r\n");
1418  #endif
1419  }
1420 
1421  }
1422  break;
1423 
1424  case CONTINUE_UPLOAD:
1425  {
1426  ULONG BytesToRead;
1427  ULONG ReadBytes;
1428 
1429  CONTINUE_READ *pContinueRead;
1430  RPLY_CONTINUE_READ *pReplyContinueRead;
1431 
1432  //Setup pointers
1433  pContinueRead = (CONTINUE_READ*)pRxBuf->Buf;
1434  pReplyContinueRead = (RPLY_CONTINUE_READ*)pTxBuf->Buf;
1435 
1436  FileHandle = pContinueRead->Handle;
1437 
1438  /* Fill out the default settings */
1439  pReplyContinueRead->CmdSize = SIZEOF_RPLYCONTINUEREAD - sizeof(CMDSIZE);
1440  pReplyContinueRead->MsgCount = pContinueRead->MsgCount;
1441  pReplyContinueRead->CmdType = SYSTEM_REPLY;
1442  pReplyContinueRead->Cmd = CONTINUE_UPLOAD;
1443  pReplyContinueRead->Status = SUCCESS;
1444  pReplyContinueRead->Handle = FileHandle;
1445 
1446  if ((FileHandle >= 0) && (pTxBuf->pFile->Name[0]))
1447  {
1448 
1449  if (pTxBuf->pFile->File >= 0)
1450  {
1451  BytesToRead = (ULONG)(pContinueRead->BytesToReadLsb);
1452  BytesToRead += (ULONG)(pContinueRead->BytesToReadMsb) << 8;
1453 
1454  // If host is asking for more bytes than remaining file size then
1455  // message length needs to adjusted accordingly
1456  if ((pTxBuf->pFile->Size - pTxBuf->pFile->Pointer) > BytesToRead)
1457  {
1458  pTxBuf->MsgLen = BytesToRead;
1459  }
1460  else
1461  {
1462  pTxBuf->MsgLen = (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer);
1463  BytesToRead = pTxBuf->MsgLen;
1464  }
1465 
1466  if ((BytesToRead + SIZEOF_RPLYCONTINUEREAD) <= pTxBuf->BufSize)
1467  {
1468  // Read all requested bytes as they can fit into the buffer
1469  ReadBytes = read(pTxBuf->pFile->File,pReplyContinueRead->PayLoad,(size_t)BytesToRead);
1470  pTxBuf->BlockLen = BytesToRead + SIZEOF_RPLYCONTINUEREAD;
1471  pTxBuf->State = TXIDLE;
1472  }
1473  else
1474  {
1475  ReadBytes = read(pTxBuf->pFile->File,pReplyContinueRead->PayLoad,(size_t)(pTxBuf->BufSize - SIZEOF_RPLYCONTINUEREAD));
1476  pTxBuf->BlockLen = pTxBuf->BufSize - SIZEOF_RPLYCONTINUEREAD;
1477  pTxBuf->State = TXFILEUPLOAD;
1478  }
1479 
1480  pTxBuf->SendBytes = (ULONG)ReadBytes;
1481  pTxBuf->pFile->Pointer += (ULONG)ReadBytes;
1482  pReplyContinueRead->CmdSize += pTxBuf->MsgLen;
1483 
1484  #ifdef DEBUG
1485  cComPrintTxMsg(pTxBuf);
1486  #endif
1487 
1488  #ifdef DEBUG
1489  printf("Size %8lu - Loaded %8lu\r\n",(unsigned long)pTxBuf->pFile->Size,(unsigned long)pTxBuf->pFile->Length);
1490  #endif
1491 
1492  if (ComInstance.Files[FileHandle].Pointer >= ComInstance.Files[FileHandle].Size)
1493  {
1494  #ifdef DEBUG
1495  printf("%s %lu bytes UpLoaded\r\n",ComInstance.Files[FileHandle].Name,(unsigned long)pTxBuf->pFile->Length);
1496  #endif
1497 
1498  pReplyContinueRead->Status = END_OF_FILE;
1499  cComCloseFileHandle(&(pTxBuf->pFile->File));
1500  cComFreeHandle(FileHandle);
1501  }
1502  }
1503  else
1504  {
1505  pReplyContinueRead->CmdType = SYSTEM_REPLY_ERROR;
1506  pReplyContinueRead->Status = HANDLE_NOT_READY;
1507  pReplyContinueRead->Handle = -1;
1509  cComFreeHandle(FileHandle);
1510  #ifdef DEBUG
1511  printf("Data not read\r\n");
1512  #endif
1513  }
1514  }
1515  else
1516  {
1517  pReplyContinueRead->CmdType = SYSTEM_REPLY_ERROR;
1518  pReplyContinueRead->Status = UNKNOWN_HANDLE;
1519  pReplyContinueRead->Handle = -1;
1521  #ifdef DEBUG
1522  printf("Invalid handle\r\n");
1523  #endif
1524  }
1525  }
1526  break;
1527 
1528  case BEGIN_GETFILE:
1529  {
1530 
1531  ULONG BytesToRead;
1532  ULONG ReadBytes;
1533  BEGIN_GET_FILE *pBeginGetFile;;
1534  RPLY_BEGIN_GET_FILE *pReplyBeginGetFile;
1535 
1536  //Setup pointers
1537  pBeginGetFile = (BEGIN_GET_FILE*)pRxBuf->Buf;
1538  pReplyBeginGetFile = (RPLY_BEGIN_GET_FILE*)pTxBuf->Buf;
1539 
1540  FileHandle = cComGetHandle((char*)pBeginGetFile->Path);
1541  pTxBuf->pFile = &ComInstance.Files[FileHandle]; // Insert the file pointer into the ch struct
1542  pTxBuf->FileHandle = FileHandle; // Also save the File handle number
1543 
1544  // Fill out the reply
1545  pReplyBeginGetFile->CmdSize = SIZEOF_RPLYBEGINGETFILE - sizeof(CMDSIZE);
1546  pReplyBeginGetFile->MsgCount = pBeginGetFile->MsgCount;
1547  pReplyBeginGetFile->CmdType = SYSTEM_REPLY;
1548  pReplyBeginGetFile->Cmd = BEGIN_GETFILE;
1549  pReplyBeginGetFile->Handle = FileHandle;
1550  pReplyBeginGetFile->Status = SUCCESS;
1551 
1552  if (FileHandle >= 0)
1553  {
1554  /* How many bytes to be returned the in the reply for BEGIN_UPLOAD */
1555  /* Should actually only be 2 bytes as only we can hold into 2 length */
1556  /* bytes in the protocol */
1557  BytesToRead = (ULONG)(pBeginGetFile->BytesToReadLsb);
1558  BytesToRead += (ULONG)(pBeginGetFile->BytesToReadMsb) << 8;
1559 
1560  #ifdef DEBUG
1561  printf("File to get: %s \r\n",ComInstance.Files[FileHandle].Name);
1562  #endif
1563 
1564  pTxBuf->pFile->File = open(pTxBuf->pFile->Name,O_RDONLY,0x444);
1565  if (pTxBuf->pFile->File >= 0)
1566  {
1567  // Get file length
1568  pTxBuf->pFile->Size = lseek(pTxBuf->pFile->File, 0L, SEEK_END);
1569  lseek(pTxBuf->pFile->File, 0L, SEEK_SET);
1570 
1571  pTxBuf->MsgLen = BytesToRead;
1572  if (BytesToRead > pTxBuf->pFile->Size)
1573  {
1574  // if message length > filesize then crop message
1575  pTxBuf->MsgLen = pTxBuf->pFile->Size;
1576  }
1577 
1578  if ((pTxBuf->MsgLen + SIZEOF_RPLYBEGINGETFILE) <= pTxBuf->BufSize)
1579  {
1580  // Read all requested bytes as they can fit into the buffer
1581  ReadBytes = read(pTxBuf->pFile->File, pReplyBeginGetFile->PayLoad, pTxBuf->MsgLen);
1582  pTxBuf->BlockLen = ReadBytes + SIZEOF_RPLYBEGINGETFILE;
1583  pTxBuf->State = TXIDLE;
1584  }
1585  else
1586  {
1587  // Read only up to full buffer size
1588  ReadBytes = read(pTxBuf->pFile->File, pReplyBeginGetFile->PayLoad, (pTxBuf->BufSize - SIZEOF_RPLYBEGINGETFILE));
1589  pTxBuf->BlockLen = pTxBuf->BufSize - SIZEOF_RPLYBEGINGETFILE;
1590  pTxBuf->State = TXGETFILE;
1591  }
1592 
1593  pTxBuf->SendBytes = ReadBytes; // No of bytes send in message
1594  pTxBuf->pFile->Pointer = ReadBytes; // No of bytes read from file
1595 
1596  #ifdef DEBUG
1597  printf("Bytes to read %d vs. bytes read %d\r\n",BytesToRead, ReadBytes);
1598  printf("FileSize: %d \r\n",pTxBuf->pFile->Size);
1599  #endif
1600 
1601  pReplyBeginGetFile->CmdSize += pTxBuf->MsgLen;
1602  pReplyBeginGetFile->FileSizeLsb = (UBYTE)pTxBuf->pFile->Size;
1603  pReplyBeginGetFile->FileSizeNsb1 = (UBYTE)(pTxBuf->pFile->Size >> 8);
1604  pReplyBeginGetFile->FileSizeNsb2 = (UBYTE)(pTxBuf->pFile->Size >> 16);
1605  pReplyBeginGetFile->FileSizeMsb = (UBYTE)(pTxBuf->pFile->Size >> 24);
1606 
1607  if (pTxBuf->pFile->Pointer >= pTxBuf->pFile->Size)
1608  {
1609  #ifdef DEBUG
1610  printf("%s %lu bytes UpLoaded\r\n",pTxBuf->pFile->Name,(unsigned long)pTxBuf->pFile->Length);
1611  #endif
1612 
1613  // If last bytes has been returned and file is not open for writing
1614  if (FAIL == cMemoryCheckOpenWrite(pTxBuf->pFile->Name))
1615  {
1616  pReplyBeginGetFile->Status = END_OF_FILE;
1617  cComCloseFileHandle(&(pTxBuf->pFile->File));
1618  cComFreeHandle(FileHandle);
1619  }
1620  }
1621 
1622  #ifdef DEBUG
1623  cComPrintTxMsg(pTxBuf);
1624  #endif
1625  }
1626  else
1627  {
1628  pReplyBeginGetFile->CmdType = SYSTEM_REPLY_ERROR;
1629  pReplyBeginGetFile->Status = HANDLE_NOT_READY;
1630  pReplyBeginGetFile->Handle = -1;
1632  cComFreeHandle(FileHandle);
1633 
1634  #ifdef DEBUG
1635  printf("File %s is not present \r\n",ComInstance.Files[FileHandle].Name);
1636  #endif
1637  }
1638  }
1639  else
1640  {
1641  pReplyBeginGetFile->CmdType = SYSTEM_REPLY_ERROR;
1642  pReplyBeginGetFile->Status = UNKNOWN_HANDLE;
1643  pReplyBeginGetFile->Handle = -1;
1645 
1646  #ifdef DEBUG
1647  printf("No more handles\r\n");
1648  #endif
1649  }
1650  }
1651  break;
1652 
1653  case CONTINUE_GETFILE:
1654  {
1655  ULONG BytesToRead;
1656  ULONG ReadBytes;
1657  CONTINUE_GET_FILE *pContinueGetFile;
1658  RPLY_CONTINUE_GET_FILE *pReplyContinueGetFile;
1659 
1660  //Setup pointers
1661  pContinueGetFile = (CONTINUE_GET_FILE*)pRxBuf->Buf;
1662  pReplyContinueGetFile = (RPLY_CONTINUE_GET_FILE*)pTxBuf->Buf;
1663 
1664  FileHandle = pContinueGetFile->Handle;
1665 
1666  /* Fill out the default settings */
1667  pReplyContinueGetFile->CmdSize = SIZEOF_RPLYCONTINUEGETFILE - sizeof(CMDSIZE);
1668  pReplyContinueGetFile->MsgCount = pContinueGetFile->MsgCount;
1669  pReplyContinueGetFile->CmdType = SYSTEM_REPLY;
1670  pReplyContinueGetFile->Cmd = CONTINUE_GETFILE;
1671  pReplyContinueGetFile->Status = SUCCESS;
1672  pReplyContinueGetFile->Handle = FileHandle;
1673 
1674  if ((FileHandle >= 0) && (pTxBuf->pFile->Name[0]))
1675  {
1676 
1677  if (pTxBuf->pFile->File >= 0)
1678  {
1679 
1680  BytesToRead = (ULONG)(pContinueGetFile->BytesToReadLsb);
1681  BytesToRead += (ULONG)(pContinueGetFile->BytesToReadMsb) << 8;
1682 
1683  // Get new file length: Set pointer to 0 -> find end -> set where to read from
1684  lseek(pTxBuf->pFile->File, 0L, SEEK_SET);
1685  pTxBuf->pFile->Size = lseek(pTxBuf->pFile->File, 0L, SEEK_END);
1686  lseek(pTxBuf->pFile->File, pTxBuf->pFile->Pointer, SEEK_SET);
1687 
1688  // If host is asking for more bytes than remaining file size then
1689  // message length needs to adjusted accordingly
1690  if ((pTxBuf->pFile->Size - pTxBuf->pFile->Pointer) > BytesToRead)
1691  {
1692  pTxBuf->MsgLen = BytesToRead;
1693  }
1694  else
1695  {
1696  pTxBuf->MsgLen = (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer);
1697  BytesToRead = pTxBuf->MsgLen;
1698  }
1699 
1700  if ((BytesToRead + SIZEOF_RPLYCONTINUEGETFILE) <= pTxBuf->BufSize)
1701  {
1702  // Read all requested bytes as they can fit into the buffer
1703  ReadBytes = read(pTxBuf->pFile->File,pReplyContinueGetFile->PayLoad,(size_t)BytesToRead);
1704  pTxBuf->BlockLen = ReadBytes + SIZEOF_RPLYCONTINUEGETFILE;
1705  pTxBuf->State = TXIDLE;
1706  }
1707  else
1708  {
1709  ReadBytes = read(pTxBuf->pFile->File,pReplyContinueGetFile->PayLoad,(size_t)(pTxBuf->BufSize - SIZEOF_RPLYCONTINUEGETFILE));
1710  pTxBuf->BlockLen = ReadBytes + SIZEOF_RPLYCONTINUEGETFILE;
1711  pTxBuf->State = TXGETFILE;
1712  }
1713 
1714  pTxBuf->SendBytes = (ULONG)ReadBytes;
1715  pTxBuf->pFile->Pointer += (ULONG)ReadBytes;
1716 
1717  pReplyContinueGetFile->CmdSize += pTxBuf->MsgLen;
1718  pReplyContinueGetFile->FileSizeLsb = (UBYTE)(pTxBuf->pFile->Size);
1719  pReplyContinueGetFile->FileSizeNsb1 = (UBYTE)(pTxBuf->pFile->Size >> 8);
1720  pReplyContinueGetFile->FileSizeNsb2 = (UBYTE)(pTxBuf->pFile->Size >> 16);
1721  pReplyContinueGetFile->FileSizeMsb = (UBYTE)(pTxBuf->pFile->Size >> 24);
1722 
1723  if (pTxBuf->pFile->Pointer >= pTxBuf->pFile->Size)
1724  {
1725  #ifdef DEBUG
1726  printf("%s %lu bytes UpLoaded\r\n",pTxBuf->pFile->Name,(unsigned long)pTxBuf->pFile->Length);
1727  #endif
1728 
1729  // If last bytes has been returned and file is not open for writing
1730  if (FAIL == cMemoryCheckOpenWrite(pTxBuf->pFile->Name))
1731  {
1732  pReplyContinueGetFile->Status = END_OF_FILE;
1733  cComCloseFileHandle(&(pTxBuf->pFile->File));
1734  cComFreeHandle(FileHandle);
1735  }
1736  }
1737 
1738  #ifdef DEBUG
1739  printf("Size %8lu - Loaded %8lu\r\n",(unsigned long)pTxBuf->pFile->Size,(unsigned long)pTxBuf->pFile->Length);
1740  #endif
1741 
1742  #ifdef DEBUG
1743  cComPrintTxMsg(pTxBuf);
1744  #endif
1745 
1746  }
1747  else
1748  {
1749  pReplyContinueGetFile->CmdType = SYSTEM_REPLY_ERROR;
1750  pReplyContinueGetFile->Status = HANDLE_NOT_READY;
1751  pReplyContinueGetFile->Handle = -1;
1753  cComFreeHandle(FileHandle);
1754 
1755  #ifdef DEBUG
1756  printf("Data not read\r\n");
1757  #endif
1758  }
1759  }
1760  else
1761  {
1762  pReplyContinueGetFile->CmdType = SYSTEM_REPLY_ERROR;
1763  pReplyContinueGetFile->Status = UNKNOWN_HANDLE;
1764  pReplyContinueGetFile->Handle = -1;
1766 
1767  #ifdef DEBUG
1768  printf("Invalid handle\r\n");
1769  #endif
1770  }
1771  }
1772  break;
1773 
1774  case LIST_FILES:
1775  {
1776  ULONG BytesToRead;
1777  ULONG Len;
1778  char TmpFileName[MD5LEN + 1 + SIZEOFFILESIZE + 1+ FILENAMESIZE];
1779  BEGIN_LIST *pBeginList;
1780  RPLY_BEGIN_LIST *pReplyBeginList;
1781 
1782  //Setup pointers
1783  pBeginList = (BEGIN_LIST*)pRxBuf->Buf;
1784  pReplyBeginList = (RPLY_BEGIN_LIST*)pTxBuf->Buf;
1785 
1786  FileHandle = cComGetHandle("ListFileHandle");
1787  pTxBuf->pFile = &ComInstance.Files[FileHandle]; // Insert the file pointer into the ch struct
1788  pTxBuf->FileHandle = FileHandle; // Also save the File handle number
1789 
1790  pReplyBeginList->CmdSize = SIZEOF_RPLYBEGINLIST - sizeof(CMDSIZE);
1791  pReplyBeginList->MsgCount = pBeginList->MsgCount;
1792  pReplyBeginList->CmdType = SYSTEM_REPLY;
1793  pReplyBeginList->Cmd = LIST_FILES;
1794  pReplyBeginList->Status = SUCCESS;
1795  pReplyBeginList->Handle = FileHandle;
1796  pTxBuf->MsgLen = 0;
1797 
1798  if (0 <= FileHandle)
1799  {
1800  BytesToRead = (ULONG)pBeginList->BytesToReadLsb;
1801  BytesToRead += (ULONG)pBeginList->BytesToReadMsb << 8;
1802 
1803  snprintf((char*)pTxBuf->Folder, FILENAMESIZE, "%s",(char*)pBeginList->Path);
1804  pTxBuf->pFile->File = scandir((char*)pTxBuf->Folder, &(pTxBuf->pFile->namelist), 0, NULL);
1805 
1806  if (pTxBuf->pFile->File <= 0)
1807  {
1808  if (pTxBuf->pFile->File == 0)
1809  {
1810  // here if no files found, equal to error as "./" and "../" normally
1811  // always found
1812  pReplyBeginList->ListSizeLsb = 0;
1813  pReplyBeginList->ListSizeNsb1 = 0;
1814  pReplyBeginList->ListSizeNsb2 = 0;
1815  pReplyBeginList->ListSizeMsb = 0;
1816  pReplyBeginList->Handle = -1;
1817 
1818  pTxBuf->BlockLen = SIZEOF_RPLYBEGINLIST;
1819  cComFreeHandle(FileHandle);
1820  }
1821  else
1822  {
1823  // If error occurs
1824  pReplyBeginList->CmdType = SYSTEM_REPLY_ERROR;
1825  pReplyBeginList->Status = UNKNOWN_ERROR;
1826  pReplyBeginList->ListSizeLsb = 0;
1827  pReplyBeginList->ListSizeNsb1 = 0;
1828  pReplyBeginList->ListSizeNsb2 = 0;
1829  pReplyBeginList->ListSizeMsb = 0;
1830  pReplyBeginList->Handle = -1;
1831 
1832  pTxBuf->BlockLen = SIZEOF_RPLYBEGINLIST;
1833  cComFreeHandle(FileHandle);
1834  }
1835  }
1836  else
1837  {
1838  SLONG TmpN;
1839  ULONG NameLen;
1840  ULONG BytesToSend;
1841  UBYTE Repeat;
1842  UBYTE *pDstAdr;
1843  UBYTE *pSrcAdr;
1844 
1845  TmpN = pTxBuf->pFile->File; // Make a copy of number of entries
1846  Len = 0;
1847 
1848  // Start by calculating the length of the entire list
1849  while(TmpN--)
1850  {
1851  if ((DT_REG == pTxBuf->pFile->namelist[TmpN]->d_type) || (DT_DIR == pTxBuf->pFile->namelist[TmpN]->d_type) || (DT_LNK == pTxBuf->pFile->namelist[TmpN]->d_type))
1852  {
1853  Len += strlen(pTxBuf->pFile->namelist[TmpN]->d_name);
1854  if (DT_REG != pTxBuf->pFile->namelist[TmpN]->d_type)
1855  {
1856  // Make room room for ending "/"
1857  Len++;
1858  }
1859  else
1860  {
1861  // Make room for md5sum + space + Filelength (fixed to 8 chars) + space
1862  Len += MD5LEN + 1 + SIZEOFFILESIZE + 1;
1863  }
1864 
1865  // Add one new line character per file/folder/link -name
1866  Len++;
1867  }
1868  }
1869 
1870  pTxBuf->pFile->Size = Len;
1871 
1872  // Total list length has been calculated
1873  pReplyBeginList->ListSizeLsb = (UBYTE) Len;
1874  pReplyBeginList->ListSizeNsb1 = (UBYTE)(Len >> 8);
1875  pReplyBeginList->ListSizeNsb2 = (UBYTE)(Len >> 16);
1876  pReplyBeginList->ListSizeMsb = (UBYTE)(Len >> 24);
1877 
1878  pTxBuf->MsgLen = BytesToRead;
1879  pTxBuf->SendBytes = 0;
1880  pTxBuf->pFile->Pointer = 0;
1881  pTxBuf->pFile->Length = 0;
1882 
1883  if (BytesToRead > Len)
1884  {
1885  // if message length > file size then crop message
1886  pTxBuf->MsgLen = Len;
1887  pReplyBeginList->Status = END_OF_FILE; // Complete file in first message
1888  }
1889 
1890  TmpN = pTxBuf->pFile->File ; // Make a copy of number of entries
1891 
1892  if ((pTxBuf->MsgLen + SIZEOF_RPLYBEGINLIST) <= pTxBuf->BufSize)
1893  {
1894  //All requested bytes can be inside the buffer
1895  BytesToSend = pTxBuf->MsgLen;
1896  pTxBuf->BlockLen = pTxBuf->MsgLen + SIZEOF_RPLYBEGINLIST;
1897  pTxBuf->State = TXIDLE;
1898  }
1899  else
1900  {
1901  BytesToSend = (pTxBuf->BufSize - SIZEOF_RPLYBEGINLIST);
1902  pTxBuf->BlockLen = pTxBuf->BufSize;
1903  pTxBuf->State = TXLISTFILES;
1904  }
1905 
1906  Repeat = 1;
1907  Len = 0;
1908  pDstAdr = pReplyBeginList->PayLoad;
1909  while(Repeat)
1910  {
1911  TmpN--;
1912 
1913  cComGetNameFromScandirList((pTxBuf->pFile->namelist[TmpN]), (char *)TmpFileName, &NameLen, (UBYTE *)pTxBuf->Folder);
1914 
1915  if (0 != NameLen)
1916  {
1917  if ((NameLen + Len) <= BytesToSend) // Does the next name fit into the buffer?
1918  {
1919 
1920  pSrcAdr = (UBYTE*)(TmpFileName);
1921  while (*pSrcAdr) *pDstAdr++ = *pSrcAdr++;
1922  Len += NameLen;
1923 
1924  free(pTxBuf->pFile->namelist[TmpN]);
1925 
1926  if (BytesToSend == Len)
1927  {
1928  // buffer is filled up now exit
1929  pTxBuf->pFile->Length = 0;
1930  pTxBuf->pFile->File = TmpN;
1931  Repeat = 0;
1932  }
1933  }
1934  else
1935  {
1936  // No - Now fill up to exact size
1937  pTxBuf->pFile->Length = (BytesToSend - Len);
1938  memcpy((char*)pDstAdr, TmpFileName, pTxBuf->pFile->Length);
1939  Len += pTxBuf->pFile->Length;
1940  pTxBuf->pFile->File = TmpN; // Adjust Iteration number
1941  Repeat = 0;
1942  }
1943  }
1944  }
1945 
1946  // Update pointers
1947  pTxBuf->SendBytes = Len;
1948  pTxBuf->pFile->Pointer = Len;
1949 
1950  if (pTxBuf->pFile->Pointer >= pTxBuf->pFile->Size)
1951  {
1952  #ifdef DEBUG
1953  printf("Complete list of %lu Bytes uploaded \r\n",(unsigned long)pTxBuf->pFile->Length);
1954  #endif
1955 
1956  pReplyBeginList->Status = END_OF_FILE;
1957  cComFreeHandle(FileHandle);
1958  }
1959  }
1960  }
1961  else
1962  {
1963  // No more handle or illegal file name
1964  pReplyBeginList->CmdType = SYSTEM_REPLY_ERROR;
1965  pReplyBeginList->Status = UNKNOWN_ERROR;
1966  pReplyBeginList->ListSizeLsb = 0;
1967  pReplyBeginList->ListSizeNsb1 = 0;
1968  pReplyBeginList->ListSizeNsb2 = 0;
1969  pReplyBeginList->ListSizeMsb = 0;
1970  pReplyBeginList->Handle = -1;
1971 
1972  pTxBuf->BlockLen = SIZEOF_RPLYBEGINLIST;
1973  }
1974  pReplyBeginList->CmdSize += pTxBuf->MsgLen;
1975 
1976  #ifdef DEBUG
1977  cComPrintTxMsg(pTxBuf);
1978  #endif
1979 
1980  }
1981  break;
1982 
1983  case CONTINUE_LIST_FILES:
1984  {
1985  SLONG TmpN;
1986  ULONG BytesToRead;
1987  ULONG Len;
1988  ULONG BytesToSend;
1989  ULONG NameLen;
1990  ULONG RemCharCnt;
1991  UBYTE Repeat;
1992  char TmpFileName[FILENAMESIZE];
1993 
1994  CONTINUE_LIST *pContinueList;
1995  RPLY_CONTINUE_LIST *pReplyContinueList;
1996 
1997  //Setup pointers
1998  pContinueList = (CONTINUE_LIST*)pRxBuf->Buf;
1999  pReplyContinueList = (RPLY_CONTINUE_LIST*)pTxBuf->Buf;
2000 
2001  pReplyContinueList->CmdSize = SIZEOF_RPLYCONTINUELIST - sizeof(CMDSIZE);
2002  pReplyContinueList->MsgCount = pContinueList->MsgCount;
2003  pReplyContinueList->CmdType = SYSTEM_REPLY;
2004  pReplyContinueList->Cmd = CONTINUE_LIST_FILES;
2005  pReplyContinueList->Handle = pContinueList->Handle;
2006  pReplyContinueList->Status = SUCCESS;
2007 
2008  BytesToRead = (ULONG)pContinueList->BytesToReadLsb;
2009  BytesToRead += (ULONG)pContinueList->BytesToReadMsb << 8;
2010 
2011  if (((pTxBuf->pFile->File == 0) && (0 > pTxBuf->pFile->Length)) || (pTxBuf->pFile->File < 0))
2012  {
2013  // here if nothing is to be returned
2014  pReplyContinueList->CmdType = SYSTEM_REPLY_ERROR;
2015  pReplyContinueList->Status = UNKNOWN_ERROR;
2016  pReplyContinueList->Handle = -1;
2017 
2018  pTxBuf->MsgLen = 0;
2020  cComFreeHandle(FileHandle);
2021  }
2022  else
2023  {
2024  TmpN = pTxBuf->pFile->File; // Make a copy of number of entries
2025 
2026  pTxBuf->MsgLen = BytesToRead;
2027  pTxBuf->SendBytes = 0;
2028 
2029  if (BytesToRead >= (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer))
2030  {
2031  // if message length > file size then crop message
2032  pTxBuf->MsgLen = (pTxBuf->pFile->Size - pTxBuf->pFile->Pointer);
2033  pReplyContinueList->Status = END_OF_FILE; // Remaining file included in this message
2034  }
2035 
2036  if ((pTxBuf->MsgLen + SIZEOF_RPLYCONTINUELIST) <= pTxBuf->BufSize)
2037  {
2038  BytesToSend = pTxBuf->MsgLen;
2039  pTxBuf->BlockLen = pTxBuf->MsgLen + SIZEOF_RPLYCONTINUELIST;
2040  pTxBuf->State = TXIDLE;
2041  }
2042  else
2043  {
2044  BytesToSend = pTxBuf->BufSize - SIZEOF_RPLYCONTINUELIST;
2045  pTxBuf->BlockLen = pTxBuf->BufSize;
2046  pTxBuf->State = TXLISTFILES;
2047  }
2048 
2049  Len = 0;
2050  Repeat = 1;
2051  if (pTxBuf->pFile->Length)
2052  {
2053 
2054  // Only here if filename has been divided in 2 pieces
2055  cComGetNameFromScandirList((pTxBuf->pFile->namelist[TmpN]), (char *)TmpFileName, &NameLen, (UBYTE *)pTxBuf->Folder);
2056 
2057  if (0 != NameLen)
2058  {
2059  // First transfer the remaining part of the last filename
2060  RemCharCnt = NameLen - pTxBuf->pFile->Length;
2061 
2062  if (RemCharCnt <= BytesToSend)
2063  {
2064  // this will fit into the message length
2065  memcpy((char*)(&(pReplyContinueList->PayLoad[Len])), &(TmpFileName[pTxBuf->pFile->Length]), RemCharCnt);
2066  Len += RemCharCnt;
2067 
2068  free(pTxBuf->pFile->namelist[TmpN]);
2069 
2070  if (RemCharCnt == BytesToSend)
2071  {
2072  //if If all bytes is already occupied not more to go
2073  //for this message
2074  Repeat = 0;
2075  pTxBuf->pFile->Length = 0;
2076  }
2077  else
2078  {
2079  Repeat = 1;
2080  }
2081  }
2082  else
2083  {
2084  // This is the rare condition if remaining msg len and buf size are almost equal
2085  memcpy((char*)(&(pReplyContinueList->PayLoad[Len])), &(TmpFileName[pTxBuf->pFile->Length]), BytesToSend);
2086  Len += BytesToSend;
2087 
2088  pTxBuf->pFile->File = TmpN; // Adjust Iteration number
2089  pTxBuf->pFile->Length += Len;
2090  Repeat = 0;
2091  }
2092  }
2093  }
2094  if (TmpN)
2095  {
2096  while(Repeat)
2097  {
2098  TmpN--;
2099 
2100  cComGetNameFromScandirList((pTxBuf->pFile->namelist[TmpN]), (char *)TmpFileName, &NameLen, (UBYTE *)pTxBuf->Folder);
2101 
2102  if ((NameLen + Len) <= BytesToSend) // Does the next name fit into the buffer?
2103  {
2104  memcpy((char*)(&(pReplyContinueList->PayLoad[Len])), TmpFileName, NameLen);
2105  Len += NameLen;
2106 
2107  free(pTxBuf->pFile->namelist[TmpN]);
2108 
2109  if (BytesToSend == Len)
2110  {
2111  // buffer is filled up now exit
2112  pTxBuf->pFile->Length = 0;
2113  pTxBuf->pFile->File = TmpN;
2114  Repeat = 0;
2115  }
2116  }
2117  else
2118  {
2119  // Now fill up to complete buffer size
2120  pTxBuf->pFile->Length = (BytesToSend - Len);
2121  memcpy((char*)(&(pReplyContinueList->PayLoad[Len])), TmpFileName, pTxBuf->pFile->Length);
2122  Len += pTxBuf->pFile->Length;
2123  pTxBuf->pFile->File = TmpN; // Adjust Iteration number
2124  Repeat = 0;
2125  }
2126  }
2127  }
2128  }
2129 
2130  // Update pointers
2131  pTxBuf->SendBytes = Len;
2132  pTxBuf->pFile->Pointer += Len;
2133 
2134  if (pTxBuf->pFile->Pointer >= pTxBuf->pFile->Size)
2135  {
2136  #ifdef DEBUG
2137  printf("Complete list of %lu Bytes uploaded \r\n",(unsigned long)pTxBuf->pFile->Length);
2138  #endif
2139 
2140  pReplyContinueList->Status = END_OF_FILE;
2141  cComFreeHandle(pContinueList->Handle);
2142  }
2143 
2144  pReplyContinueList->CmdSize += pTxBuf->MsgLen;
2145 
2146  #ifdef DEBUG
2147  cComPrintTxMsg(pTxBuf);
2148  #endif
2149  }
2150  break;
2151 
2152  case CLOSE_FILEHANDLE:
2153  {
2154  CLOSE_HANDLE *pCloseHandle;
2155  RPLY_CLOSE_HANDLE *pReplyCloseHandle;
2156 
2157  //Setup pointers
2158  pCloseHandle = (CLOSE_HANDLE*)pRxBuf->Buf;
2159  pReplyCloseHandle = (RPLY_CLOSE_HANDLE*)pTxBuf->Buf;
2160 
2161  FileHandle = pCloseHandle->Handle;
2162 
2163  #ifdef DEBUG
2164  printf("FileHandle to close = %d, Linux Handle = %d\r\n",FileHandle, ComInstance.Files[FileHandle].File);
2165  #endif
2166 
2167  pReplyCloseHandle->CmdSize = SIZEOF_RPLYCLOSEHANDLE - sizeof(CMDSIZE);
2168  pReplyCloseHandle->MsgCount = pCloseHandle->MsgCount;
2169  pReplyCloseHandle->CmdType = SYSTEM_REPLY;
2170  pReplyCloseHandle->Cmd = CLOSE_FILEHANDLE;
2171  pReplyCloseHandle->Handle = pCloseHandle->Handle;
2172  pReplyCloseHandle->Status = SUCCESS;
2173 
2174  if (TRUE == cComFreeHandle(FileHandle))
2175  {
2176  cComCloseFileHandle(&(ComInstance.Files[FileHandle].File));
2177  }
2178  else
2179  {
2180  pReplyCloseHandle->CmdType = SYSTEM_REPLY_ERROR;
2181  pReplyCloseHandle->Status = UNKNOWN_HANDLE;
2182  }
2183  pTxBuf->BlockLen = SIZEOF_RPLYCLOSEHANDLE;
2184 
2185  #ifdef DEBUG
2186  cComPrintTxMsg(pTxBuf);
2187  #endif
2188 
2189  }
2190  break;
2191 
2192  case CREATE_DIR:
2193  {
2194  MAKE_DIR *pMakeDir;
2195  RPLY_MAKE_DIR *pReplyMakeDir;
2196  char Folder[sizeof(ComInstance.Files[FileHandle].Name)];
2197 
2198  //Setup pointers
2199  pMakeDir = (MAKE_DIR*)pRxBuf->Buf;
2200  pReplyMakeDir = (RPLY_MAKE_DIR*)pTxBuf->Buf;
2201 
2202  pReplyMakeDir->CmdSize = SIZEOF_RPLYMAKEDIR - sizeof(CMDSIZE);
2203  pReplyMakeDir->MsgCount = pMakeDir->MsgCount;
2204  pReplyMakeDir->CmdType = SYSTEM_REPLY;
2205  pReplyMakeDir->Cmd = CREATE_DIR;
2206  pReplyMakeDir->Status = SUCCESS;
2207 
2208  snprintf(Folder,sizeof(Folder),"%s",(char*)(pMakeDir->Dir));
2209 
2210  if (0 == mkdir(Folder,S_IRWXU | S_IRWXG | S_IRWXO))
2211  {
2212  chmod(Folder,S_IRWXU | S_IRWXG | S_IRWXO);
2213  #ifdef DEBUG
2214  printf("Folder %s created\r\n",Folder);
2215  #endif
2216  SetUiUpdate();
2217  }
2218  else
2219  {
2220  pReplyMakeDir->CmdType = SYSTEM_REPLY_ERROR;
2221  pReplyMakeDir->Status = NO_PERMISSION;
2222 
2223  #ifdef DEBUG
2224  printf("Folder %s not created (%s)\r\n",Folder,strerror(errno));
2225  #endif
2226  }
2227  pTxBuf->BlockLen = SIZEOF_RPLYMAKEDIR;
2228  }
2229  break;
2230 
2231  case DELETE_FILE:
2232  {
2233  REMOVE_FILE *pRemove;
2234  RPLY_REMOVE_FILE *pReplyRemove;
2235  char Name[60];
2236 
2237  //Setup pointers
2238  pRemove = (REMOVE_FILE*)pRxBuf->Buf;
2239  pReplyRemove = (RPLY_REMOVE_FILE*)pTxBuf->Buf;
2240 
2241  pReplyRemove->CmdSize = SIZEOF_RPLYREMOVEFILE - sizeof(CMDSIZE);
2242  pReplyRemove->MsgCount = pRemove->MsgCount;
2243  pReplyRemove->CmdType = SYSTEM_REPLY;
2244  pReplyRemove->Cmd = DELETE_FILE;
2245  pReplyRemove->Status = SUCCESS;
2246 
2247  snprintf(Name, 60,"%s", (char*)(pRemove->Name));
2248 
2249  #ifdef DEBUG
2250  printf("File to delete %s\r\n", Name);
2251  #endif
2252 
2253  if (0 == remove(Name))
2254  {
2255  SetUiUpdate();
2256  }
2257  else
2258  {
2259  pReplyRemove->CmdType = SYSTEM_REPLY_ERROR;
2260  pReplyRemove->Status = NO_PERMISSION;
2261 
2262  #ifdef DEBUG
2263  printf("Folder %s not deleted (%s)\r\n",Folder,strerror(errno));
2264  #endif
2265  }
2266  pTxBuf->BlockLen = SIZEOF_RPLYREMOVEFILE;
2267  }
2268  break;
2269 
2270  case LIST_OPEN_HANDLES:
2271  {
2272  UBYTE HCnt1, HCnt2;
2273 
2274  LIST_HANDLES *pListHandles;
2275  RPLY_LIST_HANDLES *pReplyListHandles;
2276 
2277  //Setup pointers
2278  pListHandles = (LIST_HANDLES*)pRxBuf->Buf;
2279  pReplyListHandles = (RPLY_LIST_HANDLES*)pTxBuf->Buf;
2280 
2281  pReplyListHandles->CmdSize = SIZEOF_RPLYLISTHANDLES - sizeof(CMDSIZE);
2282  pReplyListHandles->MsgCount = pListHandles->MsgCount;
2283  pReplyListHandles->CmdType = SYSTEM_REPLY;
2284  pReplyListHandles->Cmd = LIST_OPEN_HANDLES;
2285  pReplyListHandles->Status = SUCCESS;
2286 
2287  for (HCnt1 = 0; HCnt1 < ((MAX_FILE_HANDLES/8) + 1); HCnt1++)
2288  {
2289 
2290  pReplyListHandles->PayLoad[HCnt1 + 2] = 0;
2291 
2292  for(HCnt2 = 0; HCnt2 < 8; HCnt2++)
2293  {
2294 
2295  if (0 != ComInstance.Files[HCnt2 * HCnt1].State)
2296  { // Filehandle is in use
2297 
2298  pReplyListHandles->PayLoad[HCnt1 + 2] |= (0x01 << HCnt2) ;
2299  }
2300  }
2301  }
2302  pReplyListHandles->CmdSize += HCnt1;
2303  pTxBuf->BlockLen = SIZEOF_RPLYLISTHANDLES + HCnt1;
2304  }
2305  break;
2306 
2307  case WRITEMAILBOX:
2308  {
2309  UBYTE No;
2310  UWORD PayloadSize;
2311  WRITE_MAILBOX *pWriteMailbox;
2312  WRITE_MAILBOX_PAYLOAD *pWriteMailboxPayload;
2313 
2314  pWriteMailbox = (WRITE_MAILBOX*)pRxBuf->Buf;
2315 
2316  if(1 == cComFindMailbox(&(pWriteMailbox->Name[0]), &No))
2317  {
2318  pWriteMailboxPayload = (WRITE_MAILBOX_PAYLOAD*)&(pWriteMailbox->Name[(pWriteMailbox->NameSize)]);
2319  PayloadSize = (UWORD)(pWriteMailboxPayload->SizeLsb);
2320  PayloadSize += ((UWORD)(pWriteMailboxPayload->SizeMsb)) << 8;
2321  memcpy(ComInstance.MailBox[No].Content, pWriteMailboxPayload->Payload, PayloadSize);
2322  ComInstance.MailBox[No].DataSize = PayloadSize;
2323  ComInstance.MailBox[No].WriteCnt++;
2324  }
2325  }
2326  break;
2327 
2328  case BLUETOOTHPIN:
2329  {
2330  // Both MAC and Pin are zero terminated string type
2331  UBYTE BtAddr[vmBTADRSIZE];
2332  UBYTE Pin[vmBTPASSKEYSIZE];
2333  UBYTE PinSize;
2334  BLUETOOTH_PIN *pBtPin;
2335  RPLY_BLUETOOTH_PIN *pReplyBtPin;
2336 
2337  pBtPin = (BLUETOOTH_PIN*)pRxBuf->Buf;
2338  pReplyBtPin = (RPLY_BLUETOOTH_PIN*)pTxBuf->Buf;
2339 
2340  snprintf((char*)BtAddr, pBtPin->MacSize, "%s", (char*)pBtPin->Mac);
2341  PinSize = pBtPin->PinSize;
2342  snprintf((char*)Pin, PinSize, "%s", (char*)pBtPin->Pin);
2343 
2344  // This command can for safety reasons only be handled by USB
2345  if (USBDEV == ComInstance.ActiveComCh)
2346  {
2347  cBtSetTrustedDev(BtAddr, Pin, PinSize);
2348  pReplyBtPin->Status = SUCCESS;
2349  }
2350  else
2351  {
2352  pReplyBtPin->Status = ILLEGAL_CONNECTION;
2353  }
2354 
2355  pReplyBtPin->CmdSize = 0x00;
2356  pReplyBtPin->MsgCount = pBtPin->MsgCount;
2357  pReplyBtPin->CmdType = SYSTEM_REPLY;
2358  pReplyBtPin->Cmd = BLUETOOTHPIN;
2359  pReplyBtPin->MacSize = vmBTADRSIZE;
2360 
2361  cBtGetId(pReplyBtPin->Mac, vmBTADRSIZE);
2362  pReplyBtPin->PinSize = PinSize;
2363  memcpy(pReplyBtPin->Pin, pBtPin->Pin, PinSize);
2364 
2365  pReplyBtPin->CmdSize = (SIZEOF_RPLYBLUETOOTHPIN + pReplyBtPin->MacSize + pReplyBtPin->PinSize + sizeof(pReplyBtPin->MacSize) + sizeof(pReplyBtPin->PinSize)) - sizeof(CMDSIZE);
2366  pTxBuf->BlockLen = (pReplyBtPin->CmdSize) + sizeof(CMDSIZE);
2367  }
2368  break;
2369 
2370  case ENTERFWUPDATE:
2371  {
2372  ULONG UpdateFile;
2373  UBYTE Dummy;
2374 
2375  if (USBDEV == ComInstance.ActiveComCh)
2376  {
2377  UpdateFile = open(UPDATE_DEVICE_NAME,O_RDWR);
2378 
2379  if (UpdateFile >= 0)
2380  {
2381  write(UpdateFile,&Dummy,1);
2382  close(UpdateFile);
2383  system("reboot -d -f -i");
2384  }
2385  }
2386  }
2387  break;
2388 
2389  case SETBUNDLEID:
2390  {
2391  BUNDLE_ID *pBundleId;
2392  RPLY_BUNDLE_ID *pReplyBundleId;
2393 
2394  pBundleId = (BUNDLE_ID*)pRxBuf->Buf;
2395  pReplyBundleId = (RPLY_BUNDLE_ID*)pTxBuf->Buf;
2396 
2397  pReplyBundleId->CmdSize = 0x05;
2398  pReplyBundleId->MsgCount = pBundleId->MsgCount;
2399  pReplyBundleId->Cmd = SETBUNDLEID;
2400 
2401  if (TRUE == cBtSetBundleId(pBundleId->BundleId))
2402  {
2403  // Success
2404  pReplyBundleId->CmdType = SYSTEM_REPLY;
2405  pReplyBundleId->Status = SUCCESS;
2406  }
2407  else
2408  {
2409  // Error
2410  pReplyBundleId->CmdType = SYSTEM_REPLY_ERROR;
2411  pReplyBundleId->Status = SIZE_ERROR;
2412  }
2413  pTxBuf->BlockLen = SIZEOF_RPLYBUNDLEID;
2414  }
2415  break;
2416 
2417  case SETBUNDLESEEDID:
2418  {
2419  BUNDLE_SEED_ID *pBundleSeedId;
2420  RPLY_BUNDLE_SEED_ID *pReplyBundleSeedId;
2421 
2422  pBundleSeedId = (BUNDLE_SEED_ID*)pRxBuf->Buf;
2423  pReplyBundleSeedId = (RPLY_BUNDLE_SEED_ID*)pTxBuf->Buf;
2424 
2425  pReplyBundleSeedId->CmdSize = 0x05;
2426  pReplyBundleSeedId->MsgCount = pBundleSeedId->MsgCount;
2427  pReplyBundleSeedId->Cmd = SETBUNDLESEEDID;
2428 
2429  if (TRUE == cBtSetBundleSeedId(pBundleSeedId->BundleSeedId))
2430  {
2431  // Success
2432  pReplyBundleSeedId->CmdType = SYSTEM_REPLY;
2433  pReplyBundleSeedId->Status = SUCCESS;
2434  }
2435  else
2436  {
2437  // Error
2438  pReplyBundleSeedId->CmdType = SYSTEM_REPLY_ERROR;
2439  pReplyBundleSeedId->Status = SIZE_ERROR;
2440  }
2442  }
2443  break;
2444  }
2445 }
2446 
2447 
2448 void cComUpdate(void)
2449 {
2450  COMCMD *pComCmd;
2451  IMGHEAD *pImgHead;
2452  TXBUF *pTxBuf;
2453  RXBUF *pRxBuf;
2454  UBYTE ChNo;
2455  UWORD BytesRead;
2456 
2457  UBYTE ThisMagicCookie;
2458  UBYTE HelperByte;
2459  uint Iterator;
2460  UBYTE MotorBusySignal;
2461  UBYTE MotorBusySignalPointer;
2462 
2463  ChNo = 0;
2464 
2465  cDaisyControl(); // Keep the HOST part going
2466 
2467  for(ChNo = 0; ChNo < NO_OF_CHS; ChNo++)
2468  {
2469 
2470  pTxBuf = &(ComInstance.TxBuf[ChNo]);
2471  pRxBuf = &(ComInstance.RxBuf[ChNo]);
2472  BytesRead = 0;
2473 
2474  if (!pTxBuf->Writing)
2475  {
2476  if(NULL != ComInstance.ReadChannel[ChNo])
2477  {
2478  if(ComInstance.VmReady == 1)
2479  {
2480  BytesRead = ComInstance.ReadChannel[ChNo](pRxBuf->Buf, pRxBuf->BufSize);
2481  }
2482  }
2483 
2484  #undef DEBUG
2485  //#define DEBUG
2486  #ifdef DEBUG
2487  // start DEBUG
2488  if(ChNo == USBDEV)
2489  {
2490  printf("Writing NOT set @ USBDEV - BytesRead = %d\r\n", BytesRead);
2491  }
2492  // end DEBUG
2493  #endif
2494 
2495 
2496  if(BytesRead)
2497  {
2498  // Temporary fix until full implementation of com channels is ready
2499  ComInstance.ActiveComCh = ChNo;
2500 
2501  #ifdef DEBUG
2502  // cComShow(ComInstance.UsbCmdInRep);
2503  #endif
2504 
2505  if (RXIDLE == pRxBuf->State)
2506  {
2507  // Not file down-loading
2508  memset(pTxBuf->Buf,0,sizeof(pTxBuf->Buf));
2509 
2510  pComCmd = (COMCMD*)pRxBuf->Buf;
2511 
2512  if ((*pComCmd).CmdSize)
2513  {
2514  // message received
2515  switch ((*pComCmd).Cmd)
2516  {
2517  // NEW MOTOR/DAISY
2519  #undef DEBUG
2520  //#define DEBUG
2521  #ifdef DEBUG
2522  printf("Did we reach a *BUSY* DIRECT_COMMAND_NO_REPLY pComCmd.Size = %d\n\r", ((*pComCmd).CmdSize));
2523  #endif
2524 
2525  Iterator = ((*pComCmd).CmdSize) - 7; // Incl. LEN bytes
2526 
2527  HelperByte = 0x00;
2528 
2529  for(Iterator = (((*pComCmd).CmdSize) - 7); Iterator < (((*pComCmd).CmdSize) - 3); Iterator++ )
2530  {
2531  HelperByte |= ((*pComCmd).PayLoad[Iterator] & 0x03);
2532  }
2533 
2534  cDaisySetOwnLayer(HelperByte);
2535 
2536  // Setup the Cookie stuff get one by one and store
2537 
2538  HelperByte = 0; // used as Index
2539 
2540  // New MotorSignal
2541  MotorBusySignal = 0;
2542  MotorBusySignalPointer = 1;
2543 
2544  for(Iterator = (((*pComCmd).CmdSize) - 7); Iterator < (((*pComCmd).CmdSize) - 3); Iterator++ )
2545  {
2546  #undef DEBUG
2547  //#define DEBUG
2548  #ifdef DEBUG
2549  printf("Iterator = %d\n\r", Iterator);
2550  #endif
2551 
2552  ThisMagicCookie = (*pComCmd).PayLoad[Iterator];
2553 
2554  // New MotorSignal
2555  if(ThisMagicCookie & 0x80) // BUSY signalled
2556  {
2557  MotorBusySignal = (UBYTE)(MotorBusySignal | MotorBusySignalPointer);
2558  }
2559 
2560  // New MotorSignal
2561  MotorBusySignalPointer <<= 1;
2562 
2563  #undef DEBUG
2564  //#define DEBUG
2565  #ifdef DEBUG
2566  printf("ThisMagicCookie = %d\n\r", ThisMagicCookie);
2567  #endif
2568 
2569  cDaisySetBusyFlags(cDaisyGetOwnLayer(), HelperByte, ThisMagicCookie);
2570  HelperByte++;
2571  }
2572 
2573  ResetDelayCounter(MotorBusySignal);
2574 
2575  #undef DEBUG
2576  //#define DEBUG
2577  #ifdef DEBUG
2578  printf("cMotorSetBusyFlags(%X)\n\r", MotorBusySignal);
2579  #endif
2580 
2581  // New MotorSignal
2582  cMotorSetBusyFlags(MotorBusySignal);
2583 
2584  // Adjust length
2585 
2586  ((*pComCmd).CmdSize) -= 4; // NO VM use of cookies (or sweets ;-))
2587 
2588  // Now as "normal" direct command with NO reply
2589 
2590  ComInstance.CommandReady = cComDirectCommand(pRxBuf->Buf,pTxBuf->Buf);
2591  }
2592  break;
2593 
2594  case DIRECT_COMMAND_REPLY :
2595  {
2596  // direct command
2597 
2598  #undef DEBUG
2599  //#define DEBUG
2600  #ifdef DEBUG
2601  printf("Did we reach a DIRECT_COMMAND_REPLY\n\r");
2602  #endif
2603 
2604  if(0 == ComInstance.ReplyStatus)
2605  {
2606  // If ReplyStstus = 0 then no commands is currently being
2607  // processed -> new command can be processed
2608  ComInstance.ReplyStatus |= DIR_CMD_REPLY;
2609  ComInstance.CommandReady = cComDirectCommand(pRxBuf->Buf,pTxBuf->Buf);
2610  if (!ComInstance.CommandReady)
2611  {
2612  // some error
2613  pTxBuf->Writing = 1;
2614  ComInstance.ReplyStatus = 0;
2615  }
2616  }
2617  else
2618  {
2619  // Else VM is currently processing direct commands
2620  // send error command, only if a DIR_CMD_NOREPLY
2621  // is being executed. Not if a DIR_CMD_NOREPLY is
2622  // being executed as replies can collide.
2623  if(DIR_CMD_NOREPLY & ComInstance.ReplyStatus)
2624  {
2625  COMCMD *pComCmd;
2626  COMRPL *pComRpl;
2627 
2628  pComCmd = (COMCMD*)pRxBuf->Buf;
2629  pComRpl = (COMRPL*)pTxBuf->Buf;
2630 
2631  (*pComRpl).CmdSize = 3;
2632  (*pComRpl).MsgCnt = (*pComCmd).MsgCnt;
2633  (*pComRpl).Cmd = DIRECT_REPLY_ERROR;
2634 
2635  pTxBuf->Writing = 1;
2636  }
2637  }
2638  }
2639  break;
2640 
2641  case DIRECT_COMMAND_NO_REPLY :
2642  {
2643  // direct command
2644 
2645  #undef DEBUG
2646  //#define DEBUG
2647  #ifdef DEBUG
2648  printf("Did we reach a DIRECT_COMMAND_NO_REPLY\n\r");
2649  #endif
2650 
2651  //Do not reply even if error
2652  if(0 == ComInstance.ReplyStatus)
2653  {
2654  // If ReplyStstus = 0 then no commands is currently being
2655  // processed -> new command can be processed
2656  ComInstance.ReplyStatus |= DIR_CMD_NOREPLY;
2657  ComInstance.CommandReady = cComDirectCommand(pRxBuf->Buf,pTxBuf->Buf);
2658  }
2659  }
2660  break;
2661 
2662  case SYSTEM_COMMAND_REPLY :
2663  {
2664  if(0 == ComInstance.ReplyStatus)
2665  {
2666  ComInstance.ReplyStatus |= SYS_CMD_REPLY;
2667  cComSystemCommand(pRxBuf,pTxBuf);
2668  if (RXFILEDL != pRxBuf->State)
2669  {
2670  // Only here if command has been completed
2671  pTxBuf->Writing = 1;
2672  ComInstance.ReplyStatus = 0;
2673  }
2674  }
2675  }
2676  break;
2677 
2678  case SYSTEM_COMMAND_NO_REPLY :
2679  {
2680  if(0 == ComInstance.ReplyStatus)
2681  {
2682  ComInstance.ReplyStatus |= SYS_CMD_NOREPLY;
2683  cComSystemCommand(pRxBuf,pTxBuf);
2684  if (RXFILEDL != pRxBuf->State)
2685  {
2686  // Only here if command has been completed
2687  ComInstance.ReplyStatus = 0;
2688  }
2689  }
2690  }
2691  break;
2692 
2693  case SYSTEM_REPLY:
2694  {
2695  cComSystemReply(pRxBuf, pTxBuf);
2696  }
2697  break;
2698 
2699  case SYSTEM_REPLY_ERROR:
2700  {
2701  #ifdef DEBUG
2702  printf("\r\nsystem reply error\r\n");
2703  #endif
2704  }
2705  break;
2706 
2707  case DAISY_COMMAND_REPLY:
2708  {
2709  if(ChNo == USBDEV)
2710  {
2711  #undef DEBUG
2712  //#define DEBUG
2713  #ifdef DEBUG
2714  printf("Did we reach c_COM @ DAISY_COMMAND_REPLY?\n\r");
2715  #endif
2716 
2717  cDaisyCmd(pRxBuf, pTxBuf);
2718 
2719  }
2720  else
2721  {
2722  // Some ERROR handling
2723  }
2724  }
2725  break;
2726 
2728  {
2729 
2730  #undef DEBUG
2731  //#define DEBUG
2732  #ifdef DEBUG
2733  printf("Did we reach c_COM @ DAISY_COMMAND_NO_REPLY?\n\r");
2734  #endif
2735 
2736  // A Daisy command without any reply
2737  if(ChNo == USBDEV)
2738  {
2739  // Do something
2740  cDaisyCmd(pRxBuf, pTxBuf);
2741  }
2742  else
2743  {
2744  // Some ERROR handling
2745  }
2746  }
2747  break;
2748 
2749  default :
2750  {
2751  }
2752  break;
2753 
2754  }
2755  }
2756  else
2757  { // poll received
2758  // send response
2759 
2760  pImgHead = (IMGHEAD*)ComInstance.Image;
2761  pComCmd = (COMCMD*)pTxBuf->Buf;
2762 
2763  (*pComCmd).CmdSize = (CMDSIZE)(*pImgHead).GlobalBytes + 1;
2764  (*pComCmd).Cmd = DIRECT_REPLY;
2765  memcpy((*pComCmd).PayLoad,ComInstance.Globals,(*pImgHead).GlobalBytes);
2766 
2767  pTxBuf->Writing = 1;
2768  }
2769  }
2770  else
2771  { // in the middle of a write file command
2772  ULONG RemBytes;
2773  ULONG BytesToWrite;
2774 
2775  RemBytes = pRxBuf->MsgLen - pRxBuf->RxBytes;
2776 
2777  if (RemBytes <= pRxBuf->BufSize)
2778  {
2779  // Remaining bytes to write
2780  BytesToWrite = RemBytes;
2781 
2782  // Send the reply if requested
2783  if (ComInstance.ReplyStatus & SYS_CMD_REPLY)
2784  {
2785  pTxBuf->Writing = 1;
2786  }
2787 
2788  // Clear to receive next msg header
2789  pRxBuf->State = RXIDLE;
2790  ComInstance.ReplyStatus = 0;
2791  }
2792  else
2793  {
2794  BytesToWrite = pRxBuf->BufSize;
2795  }
2796 
2797  write(pRxBuf->pFile->File, pRxBuf->Buf, (size_t)BytesToWrite);
2798  pRxBuf->pFile->Pointer += (ULONG)BytesToWrite;
2799  pRxBuf->RxBytes += (ULONG)BytesToWrite;
2800 
2801  if (pRxBuf->pFile->Pointer >= pRxBuf->pFile->Size)
2802  {
2803  cComCloseFileHandle(&(pRxBuf->pFile->File));
2804  chmod(pRxBuf->pFile->Name, S_IRWXU | S_IRWXG | S_IRWXO);
2805  cComFreeHandle(pRxBuf->FileHandle);
2806  }
2807  }
2808  }
2809  }
2810  cComTxUpdate(ChNo);
2811 
2812  // Time for USB unplug detect?
2813  UsbConUpdate++;
2815  {
2816  //#define DEBUG
2817  #undef DEBUG
2818  #ifdef DEBUG
2819  printf("ready to check\n\r");
2820  #endif
2821  UsbConUpdate = 0;
2823  }
2824 
2825  }
2826 
2827  BtUpdate();
2828  cWiFiControl();
2829 }
2830 
2832  {
2833  // Returns true if the USB port is connected
2834  return cComUsbDeviceConnected;
2835  }
2836 
2838 {
2839  TXBUF *pTxBuf;
2840  ULONG ReadBytes;
2841 
2842  pTxBuf = &(ComInstance.TxBuf[ChNo]);
2843 
2844  if((OK == cDaisyChained()) && (ChNo == USBDEV)) // We're part of a chain && USBDEV
2845  {
2846  // Do "special handling" - I.e. no conflict between pushed DaisyData and non-syncronized
2847  // returns from Commands (answers/errors).
2848 
2849  //#define DEBUG
2850  #undef DEBUG
2851  #ifdef DEBUG
2852  printf("\n\r001\n\r");
2853  #endif
2854 
2855  if(GetDaisyPushCounter() == DAISY_PUSH_NOT_UNLOCKED) // It's the very first
2856  { // (or one of the first ;-)) transmission(s)
2857  //#define DEBUG
2858  #undef DEBUG
2859  #ifdef DEBUG
2860  printf("Not unlocked 001\n\r");
2861  #endif
2862 
2863  if (pTxBuf->Writing) // Anything Pending?
2864  {
2865  //#define DEBUG
2866  #undef DEBUG
2867  #ifdef DEBUG
2868  printf("Not unlocked 002\n\r");
2869  #endif
2870 
2871  if(NULL != ComInstance.WriteChannel[ChNo]) // Valid channel?
2872  {
2873  //#define DEBUG
2874  #undef DEBUG
2875  #ifdef DEBUG
2876  printf("Not unlocked 003\n\r");
2877  #endif
2878 
2879  if((ComInstance.WriteChannel[USBDEV](pTxBuf->Buf, pTxBuf->BlockLen)) != 0)
2880  {
2881  //#define DEBUG
2882  #undef DEBUG
2883  #ifdef DEBUG
2884  printf("Not (OR should be) unlocked 004\n\r");
2885  #endif
2886 
2887  pTxBuf->Writing = 0;
2888 
2889  ResetDaisyPushCounter(); // Ready for normal run
2890 
2891  }
2892  }
2893  }
2894  }
2895  else
2896  {
2897  // We're unlocked
2898 
2899  if(GetDaisyPushCounter() == 0) // It's a NON DaisyChain time-slice
2900  {
2901  //#define DEBUG
2902  #undef DEBUG
2903  #ifdef DEBUG
2904  printf("Unlocked 001\n\r");
2905  #endif
2906 
2907  if (pTxBuf->Writing)
2908  {
2909  //#define DEBUG
2910  #undef DEBUG
2911  #ifdef DEBUG
2912  printf("Unlocked 002\n\r");
2913  #endif
2914 
2915  if(NULL != ComInstance.WriteChannel[ChNo])
2916  {
2917  //#define DEBUG
2918  #undef DEBUG
2919  #ifdef DEBUG
2920  printf("Unlocked 003\n\r");
2921  #endif
2922 
2923  if(ComInstance.WriteChannel[ChNo](pTxBuf->Buf, pTxBuf->BlockLen))
2924  {
2925  //#define DEBUG
2926  #undef DEBUG
2927  #ifdef DEBUG
2928  printf("Unlocked 004\n\r");
2929  #endif
2930 
2931  pTxBuf->Writing = 0;
2932  ResetDaisyPushCounter(); // Done/or we'll wait - we can allow more Daisy stuff
2933  }
2934 
2935  }
2936  }
2937  else
2938  {
2939  //#define DEBUG
2940  #undef DEBUG
2941  #ifdef DEBUG
2942  printf("Unlocked 005\n\r");
2943  #endif
2944 
2945  ResetDaisyPushCounter(); // Skip this "master" slice - use time with more benefit ;-)
2946  }
2947  }
2948  else
2949  {
2950  // We have a prioritised Daisy transfer/time-slice
2951  // DaisyPushCounter == either 3, 2 or 1
2952 
2953  if(NULL != ComInstance.WriteChannel[USBDEV])
2954  {
2955  UBYTE *pData; // Pointer to dedicated Daisy Upstream Buffer (INFO or Data)
2956  UWORD Len = 0;
2957 
2958  Len = cDaisyData(&pData);
2959 
2960  //#define DEBUG
2961  #undef DEBUG
2962  #ifdef DEBUG
2963  printf("Daisy Len = %d, Counter = %d\n\r", Len, GetDaisyPushCounter());
2964  #endif
2965 
2966  if(Len > 0)
2967  {
2968 
2969  if((ComInstance.WriteChannel[USBDEV](pData, Len)) != 0)
2970  {
2971  //#define DEBUG
2972  #undef DEBUG
2973  #ifdef DEBUG
2974  printf("Daisy OK tx%d\n\r", GetDaisyPushCounter());
2975  #endif
2976 
2978  cDaisyPushUpStream(); // Flood upward
2979  cDaisyPrepareNext(); // Ready for the next sensor in the array
2980  }
2981  else
2982  {
2983  //#define DEBUG
2984  #undef DEBUG
2985  #ifdef DEBUG
2986  printf("Daisy FAIL in txing %d\n\r", GetDaisyPushCounter()); // TX upstream called
2987  #endif
2988  }
2989  }
2990 
2991  }
2992  }
2993  }
2994  }
2995  else
2996  {
2997  if (pTxBuf->Writing)
2998  {
2999  //#define DEBUG
3000  #undef DEBUG
3001  #ifdef DEBUG
3002  printf("007\n\r");
3003  #endif
3004 
3005  if(NULL != ComInstance.WriteChannel[ChNo])
3006  {
3007  //#define DEBUG
3008  #undef DEBUG
3009  #ifdef DEBUG
3010  printf("Tx Writing true in the bottom ChNo = %d - PushCounter = %d\n\r", ChNo, GetDaisyPushCounter());
3011  #endif
3012 
3013  if(ComInstance.WriteChannel[ChNo](pTxBuf->Buf, pTxBuf->BlockLen))
3014  {
3015  //#define DEBUG
3016  #undef DEBUG
3017  #ifdef DEBUG
3018  printf("008\n\r");
3019  #endif
3020 
3021  pTxBuf->Writing = 0;
3022  }
3023  }
3024 
3025  }
3026  }
3027 
3028  if (0 == pTxBuf->Writing)
3029  {
3030  // Tx buffer needs to be empty to fill new data into it....
3031  switch(pTxBuf->State)
3032  {
3033  case TXFILEUPLOAD:
3034  {
3035  ULONG MsgLeft;
3036 
3037  MsgLeft = pTxBuf->MsgLen - pTxBuf->SendBytes;
3038 
3039  if( MsgLeft > pTxBuf->BufSize)
3040  {
3041  ReadBytes = read(pTxBuf->pFile->File ,pTxBuf->Buf,(size_t)pTxBuf->BufSize);
3042  pTxBuf->pFile->Pointer += ReadBytes;
3043  pTxBuf->SendBytes += ReadBytes;
3044  pTxBuf->State = TXFILEUPLOAD;
3045  }
3046  else
3047  {
3048  ReadBytes = read(pTxBuf->pFile->File ,pTxBuf->Buf,(size_t)MsgLeft);
3049  pTxBuf->pFile->Pointer += ReadBytes;
3050  pTxBuf->SendBytes += ReadBytes;
3051 
3052  if (pTxBuf->MsgLen == pTxBuf->SendBytes)
3053  {
3054  pTxBuf->State = TXIDLE;
3055 
3056  if (pTxBuf->pFile->Pointer >= pTxBuf->pFile->Size)
3057  { //All Bytes has been read in the file - close handles (it is not GetFile command)
3058 
3059  #ifdef DEBUG
3060  printf("%s %lu bytes UpLoaded\r\n",pTxBuf->pFile->Name,(unsigned long)pTxBuf->pFile->Length);
3061  #endif
3062 
3063  cComCloseFileHandle(&(pTxBuf->pFile->File));
3064  cComFreeHandle(pTxBuf->FileHandle);
3065  }
3066  }
3067  }
3068  if (ReadBytes)
3069  {
3070  pTxBuf->Writing = 1;
3071  }
3072  }
3073  break;
3074 
3075  case TXGETFILE:
3076  {
3077  ULONG MsgLeft;
3078 
3079  MsgLeft = pTxBuf->MsgLen - pTxBuf->SendBytes;
3080 
3081  if( MsgLeft > pTxBuf->BufSize)
3082  {
3083  ReadBytes = read(pTxBuf->pFile->File ,pTxBuf->Buf,(size_t)pTxBuf->BufSize);
3084  pTxBuf->pFile->Pointer += ReadBytes;
3085  pTxBuf->SendBytes += ReadBytes;
3086  pTxBuf->State = TXGETFILE;
3087  }
3088  else
3089  {
3090  ReadBytes = read(pTxBuf->pFile->File ,pTxBuf->Buf,(size_t)MsgLeft);
3091  pTxBuf->pFile->Pointer += ReadBytes;
3092  pTxBuf->SendBytes += ReadBytes;
3093 
3094  if (pTxBuf->MsgLen == pTxBuf->SendBytes)
3095  {
3096  pTxBuf->State = TXIDLE;
3097 
3098  if (pTxBuf->pFile->Pointer >= pTxBuf->pFile->Size)
3099  {
3100 
3101  #ifdef DEBUG
3102  printf("%s %lu bytes UpLoaded\r\n",pTxBuf->pFile->Name,(unsigned long)pTxBuf->pFile->Length);
3103  #endif
3104  }
3105  }
3106  }
3107  if (ReadBytes)
3108  {
3109  pTxBuf->Writing = 1;
3110  }
3111  }
3112  break;
3113 
3114  case TXLISTFILES:
3115  {
3116  ULONG TmpN;
3117  ULONG Len;
3118  ULONG NameLen;
3119  ULONG RemCharCnt;
3120  ULONG BytesToSend;
3121  UBYTE Repeat;
3122  char TmpFileName[FILENAMESIZE];
3123 
3124  TmpN = pTxBuf->pFile->File;
3125  Len = 0;
3126  Repeat = 1;
3127 
3128  if ((pTxBuf->MsgLen - pTxBuf->SendBytes) <= pTxBuf->BufSize)
3129  {
3130  //All requested bytes can be inside the buffer
3131  BytesToSend = (pTxBuf->MsgLen - pTxBuf->SendBytes);
3132  pTxBuf->State = TXIDLE;
3133  }
3134  else
3135  {
3136  BytesToSend = pTxBuf->BufSize;
3137  pTxBuf->State = TXLISTFILES;
3138  }
3139  pTxBuf->BlockLen = BytesToSend;
3140 
3141  if (pTxBuf->pFile->Length)
3142  {
3143  // Only here if filename has been divided in 2 pieces
3144  // First transfer the remaining part of the last filename
3145  cComGetNameFromScandirList((pTxBuf->pFile->namelist[TmpN]), (char *)TmpFileName, &NameLen, (UBYTE *)pTxBuf->Folder);
3146  RemCharCnt = NameLen - pTxBuf->pFile->Length;
3147 
3148  if (RemCharCnt <= BytesToSend)
3149  {
3150  // this will fit into the message length
3151  memcpy((char*)(&(pTxBuf->Buf[Len])), &(TmpFileName[pTxBuf->pFile->Length]), RemCharCnt);
3152  Len += RemCharCnt;
3153 
3154  free(pTxBuf->pFile->namelist[TmpN]);
3155 
3156  if (RemCharCnt == BytesToSend)
3157  {
3158  //if If all bytes is already occupied not more to go right now
3159  Repeat = 0;
3160  pTxBuf->pFile->Length = 0;
3161  }
3162  else
3163  {
3164  Repeat = 1;
3165  }
3166  }
3167  else
3168  {
3169  // This is the rare condition if remaining msg len and buf size are almost equal
3170  memcpy((char*)(&(pTxBuf->Buf[Len])), &(TmpFileName[pTxBuf->pFile->Length]), BytesToSend);
3171  Len += BytesToSend;
3172  pTxBuf->pFile->File = TmpN; // Adjust Iteration number
3173  pTxBuf->pFile->Length += Len;
3174  Repeat = 0;
3175  }
3176  }
3177 
3178  while(Repeat)
3179  {
3180 
3181  TmpN--;
3182 
3183  cComGetNameFromScandirList((pTxBuf->pFile->namelist[TmpN]), (char *)TmpFileName, &NameLen, (UBYTE *)pTxBuf->Folder);
3184 
3185  if ((NameLen + Len) <= BytesToSend) // Does the next name fit into the buffer?
3186  {
3187  memcpy((char*)(&(pTxBuf->Buf[Len])), TmpFileName, NameLen);
3188  Len += NameLen;
3189 
3190  #ifdef DEBUG
3191  printf("List entry no = %d; File name = %s \r\n", TmpN, pTxBuf->pFile->namelist[TmpN]->d_name);
3192  #endif
3193 
3194  free(pTxBuf->pFile->namelist[TmpN]);
3195 
3196  if (BytesToSend == Len)
3197  { // buffer is filled up now exit
3198 
3199  pTxBuf->pFile->Length = 0;
3200  pTxBuf->pFile->File = TmpN;
3201  Repeat = 0;
3202  }
3203  }
3204  else
3205  {
3206  // No, now fill up to complete buffer size
3207  ULONG RemCnt;
3208 
3209  RemCnt = BytesToSend - Len;
3210  memcpy((char*)(&(pTxBuf->Buf[Len])), TmpFileName, RemCnt);
3211  Len += RemCnt;
3212  pTxBuf->pFile->Length = RemCnt;
3213  pTxBuf->pFile->File = TmpN;
3214  Repeat = 0;
3215  }
3216  }
3217 
3218  // Update pointers
3219  pTxBuf->pFile->Pointer += Len;
3220  pTxBuf->SendBytes += Len;
3221 
3222  if (pTxBuf->pFile->Pointer == pTxBuf->pFile->Size)
3223  {
3224  // Complete list has been tx'ed
3225  free(pTxBuf->pFile->namelist);
3226  cComFreeHandle(pTxBuf->FileHandle);
3227  }
3228 
3229  pTxBuf->Writing = 1;
3230  }
3231  break;
3232 
3233  default:
3234  {
3235  // this is idle state
3236  }
3237  break;
3238  }
3239  }
3240 }
3241 
3242 
3244 {
3245  UBYTE RtnVal = 0;
3246  UBYTE Index;
3247 
3248  Index = 0;
3249  while((0 != strcmp((char*)pName,(char*)ComInstance.MailBox[Index].Name)) && (Index < NO_OF_MAILBOXES))
3250  {
3251  Index++;
3252  }
3253 
3254  if (Index < NO_OF_MAILBOXES)
3255  {
3256  RtnVal = 1;
3257  *pNo = Index;
3258  }
3259  return(RtnVal);
3260 }
3261 
3262 
3263 //******* BYTE CODE SNIPPETS **************************************************
3264 
3265 
3281 void cComReady(void)
3282 {
3283  DSPSTAT DspStat = NOBREAK;
3284  DATA8 Hardware;
3285  DATA8 *pName;
3286  IP TmpIp;
3287  UBYTE Status;
3288  UBYTE ChNos;
3289  UBYTE ChNoArr[NO_OF_BT_CHS];
3290 
3291 
3292  TmpIp = GetObjectIp();
3293  Hardware = *(DATA8*)PrimParPointer();
3294  pName = (DATA8*)PrimParPointer();
3295 
3296  if(0 == pName[0])
3297  {
3298  //Checking if own bt adapter is busy
3299  if (OK == ComInstance.ComResult)
3300  {
3301  Status = cBtGetHciBusyFlag();
3302  }
3303  else
3304  {
3305  Status = ComInstance.ComResult;
3306  }
3307 
3308  if (BUSY == Status)
3309  {
3310  DspStat = BUSYBREAK;
3311  }
3312  }
3313  else
3314  {
3315  ChNos = cBtGetChNo((UBYTE*)pName, ChNoArr);
3316  if(ChNos)
3317  {
3318  if (1 == ComInstance.TxBuf[ChNoArr[ChNos - 1] + BTSLAVE].Writing)
3319  {
3320  DspStat = BUSYBREAK;
3321  }
3322  }
3323  }
3324 
3325  if (DspStat == BUSYBREAK)
3326  {
3327  // Rewind IP
3328  SetObjectIp(TmpIp - 1);
3329  }
3330  SetDispatchStatus(DspStat);
3331 }
3332 
3333 
3350 void cComTest(void)
3351 {
3352  DSPSTAT DspStat = FAILBREAK;
3353  DATA8 Busy = 0;
3354  DATA8 Hardware;
3355  DATA8 *pName;
3356  UBYTE Status;
3357  UBYTE ChNos;
3358  UBYTE ChNoArr[NO_OF_BT_CHS];
3359 
3360  Hardware = *(DATA8*)PrimParPointer();
3361  pName = (DATA8*)PrimParPointer();
3362 
3363  if(0 == pName[0])
3364  {
3365  //Checking if own bt adapter is busy
3366  if (OK == ComInstance.ComResult)
3367  {
3368  Status = cBtGetHciBusyFlag();
3369  }
3370  else
3371  {
3372  Status = ComInstance.ComResult;
3373  }
3374  if (BUSY == Status)
3375  {
3376  Busy = 1;
3377  }
3378  }
3379  else
3380  {
3381  ChNos = cBtGetChNo((UBYTE*)pName, ChNoArr);
3382  if(ChNos)
3383  {
3384  if (1 == ComInstance.TxBuf[ChNoArr[ChNos - 1] + BTSLAVE].Writing)
3385  {
3386  Busy = 1;
3387  }
3388  }
3389  }
3390 
3391  *(DATA8*)PrimParPointer() = Busy;
3392  DspStat = NOBREAK;
3393  SetDispatchStatus(DspStat);
3394 }
3395 
3396 
3416 void cComRead(void)
3417 {
3418  DATA8 Cmd;
3419  DATA32 pImage;
3420  DATA32 pGlobal;
3421  DATA8 Flag;
3422 
3423  if (ComInstance.Cmdfd >= 0)
3424  {
3425  // Moved To lms2012.c
3426  //cComUpdate();
3427  }
3428 
3429  Cmd = *(DATA8*)PrimParPointer();
3430 
3431  switch (Cmd)
3432  { // Function
3433 
3434  case COMMAND :
3435  {
3436  // pImage used as temp var
3437  pImage = *(DATA32*)PrimParPointer();
3438 
3439  pImage = (DATA32)ComInstance.Image;
3440  pGlobal = (DATA32)ComInstance.Globals;
3441 
3442  Flag = ComInstance.CommandReady;
3443  ComInstance.CommandReady = 0;
3444 
3445  *(DATA32*)PrimParPointer() = pImage;
3446  *(DATA32*)PrimParPointer() = pGlobal;
3447  *(DATA8*)PrimParPointer() = Flag;
3448 
3449  }
3450  break;
3451  }
3452 }
3453 
3454 
3472 void cComWrite(void)
3473 {
3474  DATA8 Cmd;
3475  DATA8 Status;
3476  DATA32 pImage;
3477  DATA32 pGlobal;
3478  COMCMD *pComCmd;
3479  IMGHEAD *pImgHead;
3480 
3481  Cmd = *(DATA8*)PrimParPointer();
3482 
3483  switch (Cmd)
3484  { // Function
3485 
3486  case REPLY :
3487  {
3488  ComInstance.VmReady = 1;
3489  pImage = *(DATA32*)PrimParPointer();
3490  pGlobal = *(DATA32*)PrimParPointer();
3491  Status = *(DATA8*)PrimParPointer();
3492  pImgHead = (IMGHEAD*)pImage;
3493 
3494  if (DIR_CMD_REPLY & ComInstance.ReplyStatus)
3495  {
3496  pComCmd = (COMCMD*)ComInstance.TxBuf[ComInstance.ActiveComCh].Buf;
3497  (*pComCmd).CmdSize += (CMDSIZE)(*pImgHead).GlobalBytes; // Has been pre filled with the default length
3498 
3499  if (OK == Status)
3500  {
3501  (*pComCmd).Cmd = DIRECT_REPLY;
3502  }
3503  else
3504  {
3505  (*pComCmd).Cmd = DIRECT_REPLY_ERROR;
3506  }
3507 
3508  memcpy((*pComCmd).PayLoad,(UBYTE*)pGlobal,(*pImgHead).GlobalBytes);
3509 
3510  ComInstance.TxBuf[ComInstance.ActiveComCh].Writing = 1;
3511  ComInstance.TxBuf[ComInstance.ActiveComCh].BlockLen = (*pComCmd).CmdSize + sizeof(CMDSIZE);
3512  }
3513 
3514  //Clear ReplyStatus both for DIR_CMD_REPLY and DIR_CMD_NOREPLY
3515  ComInstance.ReplyStatus = 0;
3516  }
3517  break;
3518  }
3519 }
3520 
3521 
3522 void cComReadData(void)
3523 {
3524  DATA8 Hardware;
3525  DATA8 *pName;
3526  DATA8 Size;
3527  DATA8 *pData;
3528 
3529  Hardware = *(DATA8*)PrimParPointer();
3530  pName = (DATA8*)PrimParPointer();
3531  Size = *(DATA8*)PrimParPointer();
3532  pData = (DATA8*)PrimParPointer();
3533 }
3534 
3535 
3536 void cComWriteData(void)
3537 {
3538  DATA8 Hardware;
3539  DATA8 *pName;
3540  DATA8 Size;
3541  DATA8 *pData;
3542 
3543  Hardware = *(DATA8*)PrimParPointer();
3544  pName = (DATA8*)PrimParPointer();
3545  Size = *(DATA8*)PrimParPointer();
3546  pData = (DATA8*)PrimParPointer();
3547 }
3548 
3549 
3574 {
3575  DSPSTAT DspStat = FAILBREAK;
3576  DATA8 No;
3577  DATA8 *pBoxName;
3578  DATA8 Type;
3579  DATA8 Values;
3580  DATA8 FifoSize;
3581 
3582  No = *(DATA8*)PrimParPointer();
3583  pBoxName = (DATA8*)PrimParPointer();
3584  Type = *(DATA8*)PrimParPointer();
3585  FifoSize = *(DATA8*)PrimParPointer();
3586  Values = *(DATA8*)PrimParPointer();
3587 
3588  if (OK != ComInstance.MailBox[No].Status)
3589  {
3590  snprintf((char*)(&(ComInstance.MailBox[No].Name[0])), 50,"%s",(char*)pBoxName);
3591  memset(ComInstance.MailBox[No].Content, 0, MAILBOX_CONTENT_SIZE);
3592  ComInstance.MailBox[No].Type = Type;
3593  ComInstance.MailBox[No].Status = OK;
3594  ComInstance.MailBox[No].ReadCnt = 0;
3595  ComInstance.MailBox[No].WriteCnt = 0;
3596  DspStat = NOBREAK;
3597  }
3598  SetDispatchStatus(DspStat);
3599 }
3600 
3601 
3629 {
3630  DSPSTAT DspStat = FAILBREAK;
3631  DATA8 *pBrickName;
3632  DATA8 Hardware;
3633  DATA8 *pBoxName;
3634  DATA8 Type;
3635  DATA8 Values;
3636  DATA32 Payload[(MAILBOX_CONTENT_SIZE/4) + 1];
3637  UBYTE ChNos;
3638  UBYTE ComChNo;
3639  UBYTE ChNoArr[NO_OF_BT_CHS];
3640  UBYTE Cnt;
3641  UWORD PayloadSize = 0;
3642 
3643  WRITE_MAILBOX *pComMbx;
3644  WRITE_MAILBOX_PAYLOAD *pComMbxPayload;
3645 
3646  pBrickName = (DATA8*)PrimParPointer();
3647  Hardware = *(DATA8*)PrimParPointer();
3648  pBoxName = (DATA8*)PrimParPointer();
3649  Type = *(DATA8*)PrimParPointer();
3650  Values = *(DATA8*)PrimParPointer();
3651 
3652  // It is needed that all parameters are pop'ed off regardless
3653  for(Cnt = 0; Cnt < Values; Cnt++)
3654  {
3655  switch(Type)
3656  {
3657  case DATA_8:
3658  {
3659  ((DATA8*)(Payload))[Cnt] = *(DATA8*)PrimParPointer();
3660  PayloadSize++;
3661  }
3662  break;
3663  case DATA_16:
3664  {
3665  ((DATA16*)(Payload))[Cnt] = *(DATA16*)PrimParPointer();
3666  PayloadSize += 2;
3667  }
3668  break;
3669  case DATA_32:
3670  {
3671  ((DATA32*)(Payload))[Cnt] = *(DATA32*)PrimParPointer();
3672  PayloadSize += 4;
3673  }
3674  break;
3675  case DATA_F:
3676  {
3677  ((DATAF*)(Payload))[Cnt] = *(DATAF*)PrimParPointer();
3678  PayloadSize += 4;
3679  }
3680  break;
3681  case DATA_S:
3682  {
3683  // Supports only one string
3684  DATA8 *pName;
3685 
3686  pName = (DATA8*)PrimParPointer();
3687 
3688  PayloadSize = snprintf((char*)&Payload[0], MAILBOX_CONTENT_SIZE, "%s",(char*)pName);
3689  PayloadSize++; // Include zero termination
3690  }
3691  break;
3692  case DATA_A:
3693  {
3694  DESCR *pDescr;
3695  PRGID TmpPrgId;
3696  DATA32 Size;
3697  void *pTmp;
3698  HANDLER TmpHandle;
3699 
3700  TmpPrgId = CurrentProgramId();
3701  TmpHandle = *(HANDLER*)PrimParPointer();
3702 
3703  if (OK == cMemoryGetPointer(TmpPrgId,TmpHandle,&pTmp))
3704  {
3705  pDescr = (DESCR*)pTmp;
3706  Size = (pDescr->Elements);
3707  }
3708  else
3709  {
3710  Size = 0;
3711  }
3712 
3713  if (MAILBOX_CONTENT_SIZE < Size)
3714  {
3715  //Truncate if larger than buffer size
3716  Size = MAILBOX_CONTENT_SIZE;
3717  }
3718 
3719  memcpy((char*)&Payload[0],(DATA8*)(pDescr->pArray), Size);
3720  PayloadSize = Size;
3721  }
3722  break;
3723  }
3724  }
3725 
3726  ChNos = cBtGetChNo((UBYTE*)pBrickName, ChNoArr);
3727 
3728  for(Cnt = 0; Cnt < ChNos; Cnt++)
3729  {
3730  ComChNo = ChNoArr[Cnt] + BTSLAVE; // Ch nos offset from BT module
3731 
3732  // Valid channel found
3733  if ((0 == ComInstance.TxBuf[ComChNo].Writing) && (TXIDLE == ComInstance.TxBuf[ComChNo].State))
3734  {
3735  // Buffer is empty
3736  pComMbx = (WRITE_MAILBOX*)ComInstance.TxBuf[ComChNo].Buf;
3737 
3738  // First part of message
3739  (*pComMbx).CmdSize = SIZEOF_WRITEMAILBOX - sizeof(CMDSIZE);
3740  (*pComMbx).MsgCount = 1;
3741  (*pComMbx).CmdType = SYSTEM_COMMAND_NO_REPLY;
3742  (*pComMbx).Cmd = WRITEMAILBOX;
3743  (*pComMbx).NameSize = strlen((char*)pBoxName) + 1;
3744  snprintf((char*)(*pComMbx).Name,(*pComMbx).NameSize,"%s",(char*)pBoxName);
3745 
3746  (*pComMbx).CmdSize += (*pComMbx).NameSize;
3747 
3748  // Payload part of message
3749  pComMbxPayload = (WRITE_MAILBOX_PAYLOAD*) &(ComInstance.TxBuf[ComChNo].Buf[(*pComMbx).CmdSize + sizeof(CMDSIZE)]);
3750  (*pComMbxPayload).SizeLsb = (UBYTE) (PayloadSize & 0x00FF);
3751  (*pComMbxPayload).SizeMsb = (UBYTE)((PayloadSize >> 8) & 0x00FF);
3752  memcpy((*pComMbxPayload).Payload, Payload, PayloadSize);
3753  (*pComMbx).CmdSize += (PayloadSize + SIZEOF_WRITETOMAILBOXPAYLOAD);
3754 
3755  ComInstance.TxBuf[ComChNo].BlockLen = (*pComMbx).CmdSize + sizeof(CMDSIZE);
3756  ComInstance.TxBuf[ComChNo].Writing = 1;
3757  }
3758  }
3759 
3760  DspStat = NOBREAK;
3761  SetDispatchStatus(DspStat);
3762 }
3763 
3782 {
3783  DSPSTAT DspStat = FAILBREAK;
3784  DATA8 No;
3785  DATA8 Values;
3786  DATA16 Len;
3787  UBYTE Cnt;
3788 
3789  No = *(DATA8*)PrimParPointer();
3790  Len = *(DATA16*)PrimParPointer();
3791  Values = *(DATA8*)PrimParPointer();
3792 
3793  if (OK == ComInstance.MailBox[No].Status)
3794  {
3795  for(Cnt = 0; Cnt < Values; Cnt++)
3796  {
3797  switch(ComInstance.MailBox[No].Type)
3798  {
3799  case DATA_8:
3800  {
3801  *(DATA8*)PrimParPointer() = ((DATA8*)(ComInstance.MailBox[No].Content))[Cnt];
3802  }
3803  break;
3804  case DATA_16:
3805  {
3806  *(DATA16*)PrimParPointer() = ((DATA16*)(ComInstance.MailBox[No].Content))[Cnt];
3807  }
3808  break;
3809  case DATA_32:
3810  {
3811  *(DATA32*)PrimParPointer() = ((DATA32*)(ComInstance.MailBox[No].Content))[Cnt];
3812  }
3813  break;
3814  case DATA_F:
3815  {
3816  *(DATAF*)PrimParPointer() = ((DATAF*)(ComInstance.MailBox[No].Content))[Cnt];
3817  }
3818  break;
3819  case DATA_S:
3820  {
3821  // Supports only one string
3822  DATA8 *pData;
3823  DATA32 Data32;
3824 
3825  pData = (DATA8*)PrimParPointer();
3826 
3827  if (VMInstance.Handle >= 0)
3828  {
3829 
3830  Data32 = ComInstance.MailBox[No].DataSize;
3831  if (Data32 > MIN_ARRAY_ELEMENTS)
3832  {
3833  pData = (DATA8*)VmMemoryResize(VMInstance.Handle, Data32);
3834  }
3835  }
3836 
3837  if (pData != NULL)
3838  {
3839  snprintf((char*)pData, MAILBOX_CONTENT_SIZE, "%s",(char*)ComInstance.MailBox[No].Content);
3840  }
3841  }
3842  break;
3843  case DATA_A:
3844  {
3845  HANDLER TmpHandle;
3846  DATA8 *pData;
3847  DATA32 Data32;
3848 
3849  TmpHandle = *(HANDLER*)PrimParPointer();
3850  Data32 = ComInstance.MailBox[No].DataSize;
3851 
3852  if (Data32 > MIN_ARRAY_ELEMENTS)
3853  {
3854  pData = (DATA8*)VmMemoryResize(TmpHandle, Data32);
3855  }
3856 
3857  if (NULL != pData)
3858  {
3859  memcpy(pData, (char*)&((DATA8*)(ComInstance.MailBox[No].Content))[Cnt], Data32);
3860  }
3861  ComInstance.MailBox[No].DataSize = 0;
3862  }
3863  break;
3864  }
3865  }
3866 
3867  if (ComInstance.MailBox[No].WriteCnt != ComInstance.MailBox[No].ReadCnt)
3868  {
3869  ComInstance.MailBox[No].ReadCnt++;
3870  }
3871  }
3872 
3873  DspStat = NOBREAK;
3874  SetDispatchStatus(DspStat);
3875 }
3876 
3877 
3892 {
3893  DSPSTAT DspStat = FAILBREAK;
3894  DATA8 Busy = 0;
3895  DATA8 No;
3896 
3897  No = *(DATA8*)PrimParPointer();
3898 
3899  if (ComInstance.MailBox[No].WriteCnt == ComInstance.MailBox[No].ReadCnt)
3900  {
3901  Busy = 1;
3902  }
3903 
3904  *(DATA8*)PrimParPointer() = Busy;
3905 
3906  DspStat = NOBREAK;
3907  SetDispatchStatus(DspStat);
3908 }
3909 
3910 
3924 {
3925  DSPSTAT DspStat = NOBREAK;
3926  DATA8 No;
3927  IP TmpIp;
3928 
3929  TmpIp = GetObjectIp();
3930  No = *(DATA8*)PrimParPointer();
3931 
3932  if (ComInstance.MailBox[No].WriteCnt == ComInstance.MailBox[No].ReadCnt)
3933  {
3934  DspStat = BUSYBREAK;;
3935  }
3936 
3937  if (DspStat == BUSYBREAK)
3938  { // Rewind IP
3939 
3940  SetObjectIp(TmpIp - 1);
3941  }
3942  SetDispatchStatus(DspStat);
3943 }
3944 
3945 
3959 {
3960  DSPSTAT DspStat = NOBREAK;
3961  DATA8 No;
3962 
3963  No = *(DATA8*)PrimParPointer();
3964 
3965  ComInstance.MailBox[No].Status = FAIL;
3966  SetDispatchStatus(DspStat);
3967 }
3968 
3969 
3970  void cComWriteFile(void)
3971 {
3972  TXBUF *pTxBuf;
3973  DSPSTAT DspStat = FAILBREAK;
3974 
3975  DATA8 Hardware;
3976  DATA8 *pDeviceName;
3977  DATA8 *pFileName;
3978  DATA8 FileType;
3979 
3980  Hardware = *(DATA8*)PrimParPointer();
3981  pDeviceName = (DATA8*)PrimParPointer();
3982  pFileName = (DATA8*)PrimParPointer();
3983  FileType = *(DATA8*)PrimParPointer();
3984 
3985  #ifdef DEBUG
3986  printf("%d [%s] ->> %d [%s]\r\n",FileType,(char*)pFileName,Hardware,(char*)pDeviceName);
3987  #endif
3988 
3989  switch(Hardware)
3990  {
3991  case HW_USB:
3992  {
3993  }
3994  break;
3995 
3996  case HW_BT:
3997  {
3998  UBYTE ChNo[NO_OF_BT_CHS];
3999  UBYTE pName[MAX_FILENAME_SIZE];
4000  BEGIN_DL *pDlMsg;
4001 
4002  cBtGetChNo((UBYTE*)pDeviceName, ChNo);
4003 
4004  ChNo[0] += BTSLAVE; // Add Com module offset
4005  pTxBuf = &(ComInstance.TxBuf[ChNo[0]]);
4006  pDlMsg = (BEGIN_DL*)(&(pTxBuf->Buf[0]));
4007 
4008  if (TYPE_FOLDER == FileType)
4009  {
4010  // Sending folder
4011  pTxBuf->State = TXFOLDER;
4012 
4013  // Make copy of Foldername including a "/" at the end
4014  snprintf((char*)pTxBuf->Folder, MAX_FILENAME_SIZE, "%s",pFileName);
4015  strcat((char*)pTxBuf->Folder, "/");
4016 
4017  pTxBuf->pDir = (DIR*)opendir((char*)pFileName);
4018 
4019  if (cComGetNxtFile(pTxBuf->pDir, pName))
4020  {
4021  // File has been located
4022  ComInstance.ComResult = BUSY;
4023  cComCreateBeginDl(pTxBuf, pName);
4024  }
4025  else
4026  {
4027  // No files in directory
4028  ComInstance.ComResult = OK;
4029  }
4030  DspStat = NOBREAK;
4031  }
4032  else
4033  {
4034  // Sending single file
4035  pTxBuf->State = TXFILE;
4036  pTxBuf->Folder[0] = 0; // Make sure that folder is empty
4037  ComInstance.ComResult = BUSY;
4038  cComCreateBeginDl(pTxBuf, (UBYTE*)pFileName);
4039  DspStat = NOBREAK;
4040  }
4041  }
4042  break;
4043 
4044  case HW_WIFI:
4045  {
4046  }
4047  break;
4048  }
4049  SetDispatchStatus(DspStat);
4050 }
4051 
4052 
4053 //static DATA8 BtParred = 0;
4054 
4208 void cComGet(void)
4209 {
4210  DSPSTAT DspStat = FAILBREAK;
4211  DATA8 Cmd;
4212  DATA8 Hardware;
4213  DATA8 OnOff;
4214  DATA8 Mode2;
4215  DATA8 Item;
4216  DATA8 Status;
4217  DATA8 *pName;
4218  DATA8 Length;
4219  DATA8 *pPin;
4220  DATA8 Items;
4221  DATA8 Parred;
4222  DATA8 Connected;
4223  DATA8 Visible;
4224  DATA8 Type;
4225  DATA8 *pMac;
4226  DATA8 *pIp;
4227 
4228  Cmd = *(DATA8*)PrimParPointer();
4229 
4230  switch (Cmd)
4231  { // Function
4232 
4233  case GET_MODE2 :
4234  {
4235  Hardware = *(DATA8*)PrimParPointer();
4236  Mode2 = 0;
4237 
4238  switch(Hardware)
4239  {
4240  case HW_BT:
4241  {
4242 
4243  if (FAIL != BtGetMode2((UBYTE*)&Mode2))
4244  {
4245  DspStat = NOBREAK;
4246  }
4247  }
4248  break;
4249  default:
4250  {
4251  }
4252  break;
4253  }
4254  *(DATA8*)PrimParPointer() = Mode2;
4255  }
4256  break;
4257 
4258  case GET_ON_OFF :
4259  {
4260  Hardware = *(DATA8*)PrimParPointer();
4261  OnOff = 0;
4262 
4263  switch(Hardware)
4264  {
4265  case HW_USB:
4266  {
4267  }
4268  break;
4269  case HW_BT:
4270  {
4271  if (FAIL != BtGetOnOff((UBYTE*)&OnOff))
4272  {
4273  DspStat = NOBREAK;
4274  }
4275  }
4276  break;
4277  case HW_WIFI:
4278  {
4279  if (OK == cWiFiGetOnStatus())
4280  {
4281  OnOff = 1;
4282  }
4283  DspStat = NOBREAK;
4284  }
4285  break;
4286  default:
4287  {
4288  }
4289  break;
4290  }
4291 
4292  *(DATA8*)PrimParPointer() = OnOff;
4293  }
4294  break;
4295 
4296  case GET_VISIBLE :
4297  {
4298  DATA8 Visible;
4299 
4300  Hardware = *(DATA8*)PrimParPointer();
4301 
4302  if (Hardware < HWTYPES)
4303  {
4304  // Fill in code here
4305  Visible = BtGetVisibility();
4306 
4307  DspStat = NOBREAK;
4308  }
4309  *(DATA8*)PrimParPointer() = Visible;
4310  }
4311  break;
4312 
4313  case GET_RESULT :
4314  {
4315  Hardware = *(DATA8*)PrimParPointer();
4316  Item = *(DATA8*)PrimParPointer();
4317  Status = 0;
4318 
4319  switch(Hardware)
4320  {
4321  case HW_USB:
4322  {
4323  }
4324  break;
4325  case HW_BT:
4326  {
4327  if (OK == ComInstance.ComResult)
4328  {
4329  Status = cBtGetHciBusyFlag();
4330  }
4331  else
4332  {
4333  Status = ComInstance.ComResult;
4334  }
4335  DspStat = NOBREAK;
4336  }
4337  break;
4338  case HW_WIFI:
4339  {
4340  Status = cWiFiGetStatus();
4341  DspStat = NOBREAK;
4342  }
4343  break;
4344  default:
4345  {
4346  }
4347  break;
4348  }
4349 
4350  *(DATA8*)PrimParPointer() = Status;
4351  }
4352  break;
4353 
4354  case GET_PIN :
4355  {
4356  Hardware = *(DATA8*)PrimParPointer();
4357  pName = (DATA8*)PrimParPointer();
4358  Length = *(DATA8*)PrimParPointer();
4359  pPin = (DATA8*)PrimParPointer();
4360 
4361  // Fill in code here
4362  snprintf((char*)pPin,Length, "%s","1234");
4363 
4364  DspStat = NOBREAK;
4365 
4366  }
4367  break;
4368 
4369  case CONNEC_ITEMS:
4370  {
4371  Hardware = *(DATA8*)PrimParPointer();
4372  Items = 0;
4373 
4374  switch(Hardware)
4375  {
4376  case HW_USB:
4377  {
4378  DspStat = NOBREAK;
4379  }
4380  break;
4381  case HW_BT:
4382  {
4383  Items = cBtGetNoOfConnListEntries();
4384  DspStat = NOBREAK;
4385  }
4386  break;
4387  case HW_WIFI:
4388  {
4389  DspStat = NOBREAK;
4390  }
4391  break;
4392  }
4393  *(DATA8*)PrimParPointer() = Items;
4394  }
4395  break;
4396 
4397  case CONNEC_ITEM:
4398  {
4399  Hardware = *(DATA8*)PrimParPointer();
4400  Item = *(DATA8*)PrimParPointer();
4401  Length = *(DATA8*)PrimParPointer();
4402  pName = (DATA8*)PrimParPointer();
4403 
4404  if (VMInstance.Handle >= 0)
4405  {
4406  if (-1 == Length)
4407  {
4408  Length = vmBRICKNAMESIZE;
4409  }
4410  pName = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4411  }
4412 
4413  switch(Hardware)
4414  {
4415  case HW_USB:
4416  {
4417  DspStat = NOBREAK;
4418  }
4419  break;
4420  case HW_BT:
4421  {
4422  if (NULL != pName)
4423  {
4424  cBtGetConnListEntry(Item, (UBYTE *)pName, Length, (UBYTE*) &Type);
4425  *(DATA8*)PrimParPointer() = Type;
4426  }
4427  DspStat = NOBREAK;
4428  }
4429  break;
4430  case HW_WIFI:
4431  {
4432  DspStat = NOBREAK;
4433  }
4434  break;
4435  }
4436  }
4437  break;
4438 
4439  case SEARCH_ITEMS :
4440  {
4441  Hardware = *(DATA8*)PrimParPointer();
4442  Items = 0;
4443 
4444  switch(Hardware)
4445  {
4446  case HW_USB:
4447  {
4448  }
4449  break;
4450  case HW_BT:
4451  {
4452  Items = cBtGetNoOfSearchListEntries();
4453  DspStat = NOBREAK;
4454  }
4455  break;
4456  case HW_WIFI:
4457  {
4458  Items = cWiFiGetApListSize();
4459  DspStat = NOBREAK;
4460  }
4461  break;
4462  }
4463  *(DATA8*)PrimParPointer() = Items;
4464  }
4465  break;
4466 
4467  case SEARCH_ITEM :
4468  {
4469  UBYTE Flags;
4470 
4471  Hardware = *(DATA8*)PrimParPointer();
4472  Item = *(DATA8*)PrimParPointer();
4473  Length = *(DATA8*)PrimParPointer();
4474  pName = (DATA8*)PrimParPointer();
4475 
4476  if (VMInstance.Handle >= 0)
4477  {
4478  if (-1 == Length)
4479  {
4480  Length = vmBRICKNAMESIZE;
4481  }
4482  pName = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4483  }
4484 
4485  switch(Hardware)
4486  {
4487  case HW_USB:
4488  {
4489  }
4490  break;
4491  case HW_BT:
4492  {
4493  Parred = 0;
4494  Connected = 0;
4495  Type = ICON_UNKNOWN;
4496  Visible = 1;
4497 
4498  if (NULL != pName)
4499  {
4500  cBtGetSearchListEntry(Item, &Connected, &Type, &Parred, (UBYTE*)pName, Length);
4501  }
4502 
4503  DspStat = NOBREAK;
4504  }
4505  break;
4506  case HW_WIFI:
4507  {
4508  Parred = 0;
4509  Connected = 0;
4510  Type = 0;
4511  Visible = 0;
4512 
4513  if (NULL != pName)
4514  {
4515  cWiFiGetName((char *)pName, (int)Item, Length);
4516  Flags = cWiFiGetFlags(Item);
4517 
4518  if (Flags & VISIBLE)
4519  {
4520  Visible = 1;
4521  }
4522  if (Flags & CONNECTED)
4523  {
4524  Connected = 1;
4525  }
4526  if (Flags & KNOWN)
4527  {
4528  Parred = 1;
4529  }
4530  if (Flags & WPA2)
4531  {
4532  Type = 1;
4533  }
4534  }
4535 
4536  DspStat = NOBREAK;
4537  }
4538  break;
4539  }
4540  *(DATA8*)PrimParPointer() = Parred;
4541  *(DATA8*)PrimParPointer() = Connected;
4542  *(DATA8*)PrimParPointer() = Type;
4543  *(DATA8*)PrimParPointer() = Visible;
4544  }
4545  break;
4546 
4547  case FAVOUR_ITEMS :
4548  {
4549  Hardware = *(DATA8*)PrimParPointer();
4550  Items = 0;
4551 
4552  switch(Hardware)
4553  {
4554  case HW_USB:
4555  {
4556  }
4557  break;
4558  case HW_BT:
4559  {
4560  Items = cBtGetNoOfDevListEntries();
4561  DspStat = NOBREAK;
4562  }
4563  break;
4564  case HW_WIFI:
4565  {
4566  }
4567  break;
4568  default:
4569  {
4570  }
4571  break;
4572  }
4573  *(DATA8*)PrimParPointer() = Items;
4574  }
4575  break;
4576 
4577  case FAVOUR_ITEM :
4578  {
4579  UBYTE Flags;
4580 
4581  Hardware = *(DATA8*)PrimParPointer();
4582  Item = *(DATA8*)PrimParPointer();
4583  Length = *(DATA8*)PrimParPointer();
4584  pName = (DATA8*)PrimParPointer();
4585 
4586  if (VMInstance.Handle >= 0)
4587  {
4588  if (-1 == Length)
4589  {
4590  Length = vmBRICKNAMESIZE;
4591  }
4592  pName = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4593  }
4594 
4595  switch(Hardware)
4596  {
4597  case HW_USB:
4598  {
4599  }
4600  break;
4601  case HW_BT:
4602  {
4603  Parred = 1; // Only in favour list if parred
4604  Connected = 0;
4605  Type = ICON_UNKNOWN;
4606 
4607  if (NULL != pName)
4608  {
4609  cBtGetDevListEntry(Item, &Connected, &Type,(UBYTE*)pName, Length);
4610  }
4611  DspStat = NOBREAK;
4612  }
4613  break;
4614  case HW_WIFI:
4615  {
4616  Parred = 0; // Only in favour list if parred
4617  Connected = 0;
4618  Type = 0;
4619 
4620  if (NULL != pName)
4621  {
4622  cWiFiGetName((char *)pName, (int)Item, Length);
4623  Flags = cWiFiGetFlags(Item);
4624 
4625  if (Flags & CONNECTED)
4626  {
4627  Connected = 1;
4628  }
4629  if (Flags & KNOWN)
4630  {
4631  Parred = 1;
4632  }
4633  if (Flags & WPA2)
4634  {
4635  Type = 1;
4636  }
4637  }
4638 
4639  DspStat = NOBREAK;
4640  }
4641  break;
4642  default:
4643  {
4644  }
4645  break;
4646  }
4647 
4648  *(DATA8*)PrimParPointer() = Parred;
4649  *(DATA8*)PrimParPointer() = Connected;
4650  *(DATA8*)PrimParPointer() = Type;
4651  }
4652  break;
4653 
4654  case GET_ID:
4655  {
4656  Hardware = *(DATA8*)PrimParPointer();
4657  Length = *(DATA8*)PrimParPointer();
4658  pName = (DATA8*)PrimParPointer();
4659 
4660  switch(Hardware)
4661  {
4662  case HW_USB:
4663  {
4664  }
4665  break;
4666  case HW_BT:
4667  {
4668  if (VMInstance.Handle >= 0)
4669  {
4670  if (-1 == Length)
4671  {
4672  Length = vmBTADRSIZE;
4673  }
4674  pName = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4675  }
4676 
4677  if (pName != NULL)
4678  {
4679  cBtGetId((UBYTE*)pName, Length);
4680  }
4681  DspStat = NOBREAK;
4682 
4683  }
4684  break;
4685  case HW_WIFI:
4686  {
4687  }
4688  break;
4689  default:
4690  {
4691  }
4692  break;
4693  }
4694 
4695  }
4696  break;
4697 
4698  case GET_BRICKNAME :
4699  {
4700  Length = *(DATA8*)PrimParPointer();
4701  pName = (DATA8*)PrimParPointer();
4702 
4703  if (VMInstance.Handle >= 0)
4704  {
4705  if (-1 == Length)
4706  {
4707  Length = vmBRICKNAMESIZE;
4708  }
4709  pName = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4710  }
4711 
4712  if (NULL != pName)
4713  {
4714  snprintf((char*)pName,Length,"%s",(char*)&(ComInstance.BrickName[0]));
4715  }
4716 
4717  DspStat = NOBREAK;
4718  }
4719  break;
4720 
4721  case GET_NETWORK:
4722  {
4723  UBYTE Flags;
4724  UBYTE MaxStrLen;
4725 
4726  Hardware = *(DATA8*)PrimParPointer();
4727  Length = *(DATA8*)PrimParPointer();
4728 
4729  // Lenght are the maximum length for all
4730  // 3 strings returned (pName, pMac and pIp)
4731  if ((vmBRICKNAMESIZE >= vmMACSIZE) && (vmBRICKNAMESIZE >= vmIPSIZE))
4732  {
4733  MaxStrLen = vmBRICKNAMESIZE;
4734  }
4735  else
4736  {
4737  if (vmMACSIZE >= vmIPSIZE)
4738  {
4739  MaxStrLen = vmMACSIZE;
4740  }
4741  else
4742  {
4743  MaxStrLen = vmIPSIZE;
4744  }
4745  }
4746 
4747  pName = (DATA8*)PrimParPointer();
4748  if (VMInstance.Handle >= 0)
4749  {
4750  if (-1 == Length)
4751  {
4752  Length = MaxStrLen;
4753  }
4754  pName = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4755  }
4756 
4757  pMac = (DATA8*)PrimParPointer();
4758  if (VMInstance.Handle >= 0)
4759  {
4760  if (-1 == Length)
4761  {
4762  Length = MaxStrLen;
4763  }
4764  pMac = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4765  }
4766 
4767  pIp = (DATA8*)PrimParPointer();
4768  if (VMInstance.Handle >= 0)
4769  {
4770  if (-1 == Length)
4771  {
4772  Length = MaxStrLen;
4773  }
4774  pIp = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4775  }
4776 
4777  switch(Hardware)
4778  {
4779  case HW_USB:
4780  {
4781  }
4782  break;
4783  case HW_BT:
4784  {
4785  }
4786  break;
4787  case HW_WIFI:
4788  {
4789  if ((NULL != pName) && (NULL != pMac) && (NULL != pIp))
4790  {
4791  if(OK == cWiFiGetOnStatus())
4792  {
4793  Flags = cWiFiGetFlags((int)0);
4794  if (Flags & CONNECTED)
4795  {
4796  cWiFiGetIpAddr((char*)pIp);
4797  cWiFiGetMyMacAddr((char*)pMac);
4798  cWiFiGetName((char*)pName, 0, Length);
4799  }
4800  else
4801  {
4802  cWiFiGetMyMacAddr((char*)pMac);
4803  snprintf((char*)pName, Length, "%s","NONE");
4804  snprintf((char*)pIp, 3, "%s","??");
4805  }
4806  }
4807  else
4808  {
4809  snprintf((char*)pMac, 3, "%s","??");
4810  snprintf((char*)pName, Length, "%s","NONE");
4811  snprintf((char*)pIp, 3, "%s", "??");
4812  }
4813  }
4814  }
4815  break;
4816  }
4817 
4818  DspStat = NOBREAK;
4819  }
4820  break;
4821 
4822  case GET_PRESENT:
4823  {
4824  Hardware = *(DATA8*)PrimParPointer();
4825  Status = 0;
4826 
4827  switch(Hardware)
4828  {
4829  case HW_USB:
4830  {
4831  Status = 1;
4832  }
4833  break;
4834  case HW_BT:
4835  {
4836  Status = 1;
4837  }
4838  break;
4839  case HW_WIFI:
4840  {
4841  if(OK == cWiFiKnownDongleAttached())
4842  {
4843  Status = 1;
4844  }
4845  }
4846  break;
4847  }
4848  *(DATA8*)PrimParPointer() = Status;
4849  DspStat = NOBREAK;
4850  }
4851  break;
4852 
4853  case GET_ENCRYPT:
4854  {
4855 
4856  Hardware = *(DATA8*)PrimParPointer();
4857  Item = *(DATA8*)PrimParPointer();
4858 
4859  Type = 0;
4860  switch(Hardware)
4861  {
4862  case HW_USB:
4863  {
4864  }
4865  break;
4866  case HW_BT:
4867  {
4868  }
4869  break;
4870  case HW_WIFI:
4871  {
4872  DspStat = NOBREAK;
4873  }
4874  break;
4875  }
4876 
4877  *(DATA8*)PrimParPointer() = Type;
4878  }
4879  break;
4880 
4881  case GET_INCOMING:
4882  {
4883  Hardware = *(DATA8*)PrimParPointer();
4884  Length = *(DATA8*)PrimParPointer();
4885  pName = (DATA8*)PrimParPointer();
4886 
4887  if (VMInstance.Handle >= 0)
4888  {
4889  if (-1 == Length)
4890  {
4891  Length = vmBRICKNAMESIZE;
4892  }
4893  pName = (DATA8*)VmMemoryResize(VMInstance.Handle,(DATA32)Length);
4894  }
4895 
4896  switch(Hardware)
4897  {
4898  case HW_USB:
4899  {
4900  }
4901  break;
4902  case HW_BT:
4903  {
4904  if (NULL != pName)
4905  {
4906  cBtGetIncoming((UBYTE*)pName,(UBYTE*)&Type, Length);
4907  }
4908 
4909  *(DATA8*)PrimParPointer() = Type;
4910  DspStat = NOBREAK;
4911  }
4912  break;
4913  case HW_WIFI:
4914  {
4915  }
4916  break;
4917  }
4918  }
4919  break;
4920 
4921  }
4922  SetDispatchStatus(DspStat);
4923 }
4924 
4925 
5045 void cComSet(void)
5046 {
5047  DSPSTAT DspStat = FAILBREAK;
5048  DATA8 Cmd;
5049  DATA8 Hardware;
5050  DATA8 OnOff;
5051  DATA8 Mode2;
5052  DATA8 Visible;
5053  DATA8 Search;
5054  DATA8 *pName;
5055  DATA8 *pPin;
5056  DATA8 Connection;
5057  DATA8 Item;
5058  DATA8 Type;
5059 
5060  Cmd = *(DATA8*)PrimParPointer();
5061 
5062  switch (Cmd)
5063  { // Function
5064  case SET_MODE2 :
5065  {
5066  Hardware = *(DATA8*)PrimParPointer();
5067  Mode2 = *(DATA8*)PrimParPointer();
5068 
5069  switch(Hardware)
5070  {
5071  case HW_BT:
5072  {
5073  if (FAIL != BtSetMode2(Mode2))
5074  {
5075  DspStat = NOBREAK;
5076  }
5077  }
5078  break;
5079  default:
5080  {
5081  }
5082  break;
5083  }
5084  }
5085  break;
5086 
5087  case SET_ON_OFF :
5088  {
5089  Hardware = *(DATA8*)PrimParPointer();
5090  OnOff = *(DATA8*)PrimParPointer();
5091 
5092  switch(Hardware)
5093  {
5094  case HW_USB:
5095  {
5096  }
5097  break;
5098  case HW_BT:
5099  {
5100  if(FAIL != BtSetOnOff(OnOff))
5101  {
5102  DspStat = NOBREAK;
5103  }
5104  }
5105  break;
5106  case HW_WIFI:
5107  {
5108  if (OnOff)
5109  {
5110  if (FAIL != cWiFiTurnOn())
5111  {
5112  DspStat = NOBREAK;
5113  }
5114  }
5115  else
5116  {
5117  if (FAIL != cWiFiTurnOff())
5118  {
5119  DspStat = NOBREAK;
5120  }
5121  }
5122  }
5123  break;
5124  default:
5125  {
5126  }
5127  break;
5128  }
5129  }
5130  break;
5131 
5132  case SET_VISIBLE :
5133  {
5134  Hardware = *(DATA8*)PrimParPointer();
5135  Visible = *(DATA8*)PrimParPointer();
5136  DspStat = NOBREAK;
5137 
5138  switch(Hardware)
5139  {
5140  case HW_USB:
5141  {
5142  }
5143  break;
5144  case HW_BT:
5145  {
5146  // Fill in code here
5147  if (FAIL != BtSetVisibility(Visible))
5148  {
5149  DspStat = NOBREAK;
5150  }
5151  }
5152  break;
5153  case HW_WIFI:
5154  {
5155  // N_A
5156  }
5157  break;
5158  default:
5159  {
5160  }
5161  break;
5162  }
5163  }
5164  break;
5165 
5166  case SET_SEARCH :
5167  {
5168  Hardware = *(DATA8*)PrimParPointer();
5169  Search = *(DATA8*)PrimParPointer();
5170 
5171  switch(Hardware)
5172  {
5173  case HW_USB:
5174  {
5175  }
5176  break;
5177  case HW_BT:
5178  {
5179  if (Search)
5180  {
5181  if (FAIL != BtStartScan())
5182  {
5183  DspStat = NOBREAK;
5184  }
5185  }
5186  else
5187  {
5188  if (FAIL != BtStopScan())
5189  {
5190  DspStat = NOBREAK;
5191  }
5192  }
5193  }
5194  break;
5195  case HW_WIFI:
5196  {
5197  if (Search)
5198  {
5199  if (FAIL != cWiFiScanForAPs())
5200  {
5201  DspStat = NOBREAK;
5202  }
5203  }
5204  else
5205  {
5206  //NA in Wifi
5207  }
5208  }
5209  break;
5210  default:
5211  {
5212  }
5213  break;
5214  }
5215  }
5216  break;
5217 
5218  case SET_PIN :
5219  {
5220  Hardware = *(DATA8*)PrimParPointer();
5221  pName = (DATA8*)PrimParPointer();
5222  pPin = (DATA8*)PrimParPointer();
5223 
5224  switch (Hardware)
5225  {
5226  case HW_USB:
5227  {
5228  }
5229  break;
5230  case HW_BT:
5231  {
5232  if (FAIL != cBtSetPin((UBYTE*)pPin))
5233  {
5234  DspStat = NOBREAK;
5235  }
5236  }
5237  break;
5238  case HW_WIFI:
5239  {
5240  #ifdef DEBUG
5241  printf("\n\rSSID = %s\r\n", pName);
5242  #endif
5243  if (OK == cWiFiGetIndexFromName((char*)pName, (UBYTE*)&Item))
5244  {
5245  #ifdef DEBUG
5246  printf("\r\nGot Index = %d\r\n", Item);
5247  printf("\r\nGot Index from name = %s\r\n", pName);
5248  #endif
5249  cWiFiMakePsk((char*)pName, (char*)pPin, (int)Item);
5250  #ifdef DEBUG
5251  printf("\r\nPSK made\r\n");
5252  #endif
5253  DspStat = NOBREAK;
5254  }
5255  }
5256  break;
5257  }
5258  }
5259  break;
5260 
5261  case SET_PASSKEY:
5262  {
5263  UBYTE Accept;
5264 
5265  Hardware = *(DATA8*)PrimParPointer();
5266  Accept = *(DATA8*)PrimParPointer();
5267 
5268  switch (Hardware)
5269  {
5270  case HW_BT:
5271  {
5272  if (FAIL != cBtSetPasskey(Accept))
5273  {
5274  DspStat = NOBREAK;
5275  }
5276  }
5277  break;
5278  default:
5279  {
5280  //Only used in bluetooth
5281  }
5282  break;
5283  }
5284  }
5285  break;
5286 
5287  case SET_CONNECTION :
5288  {
5289  Hardware = *(DATA8*)PrimParPointer();
5290  pName = (DATA8*)PrimParPointer();
5291  Connection = *(DATA8*)PrimParPointer();
5292 
5293  switch (Hardware)
5294  {
5295  case HW_USB:
5296  {
5297  }
5298  break;
5299  case HW_BT:
5300  {
5301  if (Connection)
5302  {
5303  if (FAIL != cBtConnect((UBYTE *)pName))
5304  {
5305  DspStat = NOBREAK;
5306  }
5307 
5308  }
5309  else
5310  {
5311  if (FAIL != cBtDisconnect((UBYTE *)pName))
5312  {
5313  DspStat = NOBREAK;
5314  }
5315  }
5316  }
5317  break;
5318  case HW_WIFI:
5319  {
5320  if (Connection)
5321  {
5322  if (OK == cWiFiGetIndexFromName((char*)pName, (UBYTE*)&Item))
5323  {
5324  //#define DEBUG
5325  #undef DEBUG
5326  #ifdef DEBUG
5327  printf("cWiFiConnect => index: %d, Name: %s\n\r", Item, pName);
5328  #endif
5329 
5330  cWiFiConnectToAp((int)Item);
5331 
5332  //#define DEBUG
5333  #undef DEBUG
5334  #ifdef DEBUG
5335  printf("We have tried to connect....\n\r");
5336  #endif
5337 
5338  DspStat = NOBREAK;
5339  }
5340  else
5341  {
5342  }
5343  }
5344  else
5345  {
5346  // Not implemented
5347  // implicite in connect
5348  }
5349  }
5350  break;
5351  }
5352  }
5353  break;
5354 
5355  case SET_BRICKNAME:
5356  {
5357  UBYTE Len;
5358  FILE *File;
5359  char nl[1];
5360 
5361  nl[0] = 0x0A; // Insert new line
5362 
5363  pName = (DATA8*)PrimParPointer();
5364 
5365  Len = strlen((char*)pName);
5366 
5367  if (OK == ValidateString(pName,vmCHARSET_NAME) && ((vmBRICKNAMESIZE - 1) > strlen((char*)pName)))
5368  {
5369  if (FAIL != cBtSetName((UBYTE*)pName, Len + 1))
5370  {
5371  File = fopen("./settings/BrickName","w");
5372  if (File != NULL)
5373  {
5374  fwrite(pName, 1, (int)Len + 1, File);
5375  fwrite(nl, 1, (int)1, File); // Insert new line
5376  fclose (File);
5377  }
5378  snprintf((char*)&(ComInstance.BrickName[0]), vmBRICKNAMESIZE, "%s",(char*)pName);
5379  sethostname((char*)pName, Len + 1);
5380  DspStat = NOBREAK;
5381  }
5382  }
5383  }
5384  break;
5385 
5386  case SET_MOVEUP:
5387  {
5388  Hardware = *(DATA8*)PrimParPointer();
5389  Item = *(DATA8*)PrimParPointer();
5390 
5391  switch(Hardware)
5392  {
5393  case HW_USB:
5394  {
5395  }
5396  break;
5397  case HW_BT:
5398  {
5399  }
5400  break;
5401  case HW_WIFI:
5402  {
5403  cWiFiMoveUpInList((int)Item);
5404  DspStat = NOBREAK;
5405  }
5406  break;
5407  }
5408  }
5409  break;
5410 
5411  case SET_MOVEDOWN:
5412  {
5413  Hardware = *(DATA8*)PrimParPointer();
5414  Item = *(DATA8*)PrimParPointer();
5415 
5416  switch(Hardware)
5417  {
5418  case HW_USB:
5419  {
5420  }
5421  break;
5422  case HW_BT:
5423  {
5424  }
5425  break;
5426  case HW_WIFI:
5427  {
5428  cWiFiMoveDownInList((int)Item);
5429  DspStat = NOBREAK;
5430  }
5431  break;
5432  }
5433  }
5434  break;
5435 
5436  case SET_ENCRYPT:
5437  {
5438  Hardware = *(DATA8*)PrimParPointer();
5439  pName = (DATA8*)PrimParPointer();
5440  Type = *(DATA8*)PrimParPointer();
5441 
5442  switch(Hardware)
5443  {
5444  case HW_USB:
5445  {
5446  }
5447  break;
5448  case HW_BT:
5449  {
5450  }
5451  break;
5452  case HW_WIFI:
5453  {
5454  if (Type)
5455  {
5456  //#define DEBUG
5457  #undef DEBUG
5458  #ifdef DEBUG
5459  printf("\r\nWPA encrypt called\r\n");
5460  #endif
5461 
5462  uint LocalIndex = 0;
5463  cWiFiGetIndexFromName((char*)pName, (UBYTE*)&LocalIndex);
5464  cWiFiSetEncryptToWpa2(LocalIndex);
5465  }
5466  else
5467  {
5468  //#define DEBUG
5469  #undef DEBUG
5470  #ifdef DEBUG
5471  printf("\r\nNONE encrypt called\r\n");
5472  #endif
5473 
5475  }
5476  DspStat = NOBREAK;
5477  }
5478  break;
5479  }
5480  }
5481  break;
5482 
5483  case SET_SSID:
5484  {
5485  UBYTE Index;
5486 
5487  Hardware = *(DATA8*)PrimParPointer();
5488  pName = (DATA8*)PrimParPointer();
5489 
5490  switch(Hardware)
5491  {
5492  case HW_USB:
5493  {
5494  }
5495  break;
5496  case HW_BT:
5497  {
5498  }
5499  break;
5500  case HW_WIFI:
5501  {
5502  Index = cWiFiGetApListSize();
5503  cWiFiSetName((char*)pName, (int)Index);
5505  DspStat = NOBREAK;
5506  }
5507  break;
5508  }
5509  }
5510 
5511  }
5512  SetDispatchStatus(DspStat);
5513 }
5514 
5515 
5516 void cComRemove(void)
5517 {
5518  DSPSTAT DspStat = FAILBREAK;
5519  DATA8 Hardware;
5520  DATA8 *pName;
5521  UBYTE LocalIndex;
5522 
5523  Hardware = *(DATA8*)PrimParPointer();
5524  pName = (DATA8*)PrimParPointer();
5525 
5526  switch(Hardware)
5527  {
5528  case HW_USB:
5529  {
5530  }
5531  break;
5532  case HW_BT:
5533  {
5534  if (FAIL != cBtDeleteFavourItem((UBYTE *)pName))
5535  {
5536  DspStat = NOBREAK;
5537  }
5538  }
5539  break;
5540  case HW_WIFI:
5541  {
5542  cWiFiGetIndexFromName((char*)pName, (UBYTE*)&LocalIndex);
5543 
5544  //#define DEBUG
5545  #undef DEBUG
5546  #ifdef DEBUG
5547  printf("Removing Index: %d\n\r", LocalIndex);
5548  #endif
5549 
5550  cWiFiDeleteAsKnown(LocalIndex); // We removes the (favorit) "*"
5551  DspStat = NOBREAK;
5552  }
5553  break;
5554  }
5555 }
5556 
5558 {
5559  return(cBtGetStatus());
5560 }
5561 
5562 
5564 {
5565  UBYTE Flags;
5566  UBYTE Status;
5567 
5568  Status = 0;
5569 
5570  if (OK == cWiFiGetOnStatus())
5571  {
5572  Status |= 0x03; // Wifi on + visible (always visible if on)
5573  Flags = cWiFiGetFlags((int)0);
5574  if (CONNECTED & Flags)
5575  {
5576  Status |= 0x04; // Wifi connected to AP
5577  }
5578  }
5579  return(Status);
5580 }
5581 
5582 
5583 void cComGetBrickName(DATA8 Length, DATA8 *pBrickName)
5584 {
5585  snprintf((char*)pBrickName,Length,"%s",(char*)&(ComInstance.BrickName[0]));
5586 }
5587 
5588 
5590 {
5591  return(cBtGetEvent());
5592 }
5593 
5594 //*****************************************************************************
MSGCNT MsgCount
Definition: c_com.h:1307
void BtUpdate(void)
Definition: c_bt.c:1572
UBYTE Status
Definition: c_com.h:1253
UWORD cBtSetBundleSeedId(UBYTE *pSeedId)
Definition: c_bt.c:4279
Definition: c_com.h:974
UWORD cBtReadCh2(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:749
UBYTE Path[]
Definition: c_com.h:1288
void SetDispatchStatus(DSPSTAT DspStat)
Set object (dispatch) status.
Definition: lms2012.c:256
UBYTE cBtSetName(UBYTE *pName, UBYTE Length)
Definition: c_bt.c:3651
void cComRead(void)
opCOM_READ byte code
Definition: c_com.c:3416
MSGCNT MsgCount
Definition: c_com.h:1080
UWORD cWiFiWriteTcp(UBYTE *Buffer, UWORD Length)
Definition: c_wifi.c:2757
#define SIZEOF_WRITEMAILBOX
Definition: c_com.h:1335
MSGCNT MsgCount
Definition: c_com.h:1120
UBYTE cComGetNxtFile(DIR *pDir, UBYTE *pName)
Definition: c_com.c:570
UBYTE cBtGetChNo(UBYTE *pName, UBYTE *pChNos)
Definition: c_bt.c:3680
void cComWriteMailBox(void)
opMAILBOX_WRITE byte code
Definition: c_com.c:3628
void cComSetMusbHdrcMode(void)
Definition: c_com.c:273
CMDSIZE CmdSize
Definition: c_com.h:1306
#define SIZEOF_BEGINDL
Definition: c_com.h:1291
MAILBOX MailBox[NO_OF_MAILBOXES]
Definition: c_com.h:1577
MSGCNT MsgCount
Definition: c_com.h:1361
CMDSIZE CmdSize
Definition: c_com.h:1384
UBYTE cBtConnect(UBYTE *pDevName)
Definition: c_bt.c:2276
DATA8 Changed[INPUTS][MAX_DEVICE_MODES]
Definition: d_uart_mod.c:1953
RESULT cWiFiGetStatus(void)
Definition: c_wifi.c:2601
Definition: lms2012.h:558
MSGCNT MsgCount
Definition: c_com.h:1260
#define snprintf
Definition: c_input.c:141
UWORD cBtDevWriteBuf6(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2652
UWORD(* WriteChannel[NO_OF_CHS])(UBYTE *, UWORD)
Definition: c_com.h:1570
#define SIZEOF_RPLYBEGINLIST
Definition: c_com.h:1062
CMDSIZE CmdSize
Definition: c_com.h:1269
UWORD(* ReadChannel[NO_OF_CHS])(UBYTE *, UWORD)
Definition: c_com.h:1569
UBYTE cBtDisconnect(UBYTE *pName)
Definition: c_bt.c:2415
UBYTE BtStartScan(void)
Definition: c_bt.c:646
signed char SBYTE
Basic Type used to symbolise 8 bit signed values.
Definition: lmstypes.h:33
UBYTE BytesToReadLsb
Definition: c_com.h:1151
UBYTE Cmd
Definition: c_com.h:1298
UBYTE cComUsbDeviceConnected
Definition: c_com.c:84
uint GetDaisyPushCounter(void)
Definition: c_daisy.c:510
void cDaisySetOwnLayer(UBYTE Layer)
Definition: c_daisy.c:204
ULONG Size
File size.
Definition: c_com.h:1507
UBYTE FileHandle
Definition: c_com.h:1524
UBYTE PayLoad[]
Definition: c_com.h:1060
UBYTE PayLoad[]
Definition: c_com.h:1213
RESULT cWiFiGetName(char *ApName, int Index, char Length)
Definition: c_wifi.c:1681
void ResetDelayCounter(UBYTE Pattern)
Definition: c_output.c:376
#define SIZEOF_RPLYBEGINGETFILE
Definition: c_com.h:1115
UWORD cBtDevWriteBuf2(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2592
void cWiFiMoveDownInList(int Index)
Definition: c_wifi.c:586
Break because of fail.
Definition: lms2012.h:674
void cComUpdate(void)
Definition: c_com.c:2448
DATA8 CheckUsbstick(DATA8 *pChanged, DATA32 *pTotal, DATA32 *pFree, DATA8 Force)
Definition: lms2012.c:1959
UBYTE RemoteFileHandle
Definition: c_com.h:1525
UBYTE BtSetOnOff(UBYTE On)
Definition: c_bt.c:2000
SWORD DATA16
VM Type for 2 byte signed value.
Definition: lmstypes.h:62
#define SIZEOF_RPLYCONTINUEDL
Definition: c_com.h:1324
UBYTE BundleSeedId[]
Definition: c_com.h:1398
UBYTE Path[]
Definition: c_com.h:1044
UBYTE Buf[1024]
Definition: c_com.h:1536
UWORD cBtReadCh3(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:771
UBYTE BtGetVisibility(void)
Definition: c_bt.c:2114
UBYTE Status
Definition: c_com.h:1549
UBYTE FileSizeMsb
Definition: c_com.h:1287
UBYTE Handle
Definition: c_com.h:1168
SLONG DATA32
VM Type for 4 byte signed value.
Definition: lmstypes.h:63
UBYTE FileSizeNsb1
Definition: c_com.h:1109
UBYTE cBtGetConnListEntry(UBYTE Item, UBYTE *pName, SBYTE Length, UBYTE *pType)
Definition: c_bt.c:3489
Definition: c_com.h:1503
void cMotorSetBusyFlags(UBYTE Flags)
Definition: c_output.c:406
UBYTE CmdType
Definition: c_com.h:1297
UBYTE BtSetMode2(UBYTE Mode2)
Definition: c_bt.c:1936
UBYTE Status
Definition: c_com.h:1299
UBYTE BytesToReadMsb
Definition: c_com.h:1072
UWORD OBJID
Object id type.
Definition: lmstypes.h:73
UBYTE ListSizeLsb
Definition: c_com.h:1055
UBYTE SubState
Definition: c_com.h:1527
#define SIZEOF_RPLYCONTINUEREAD
Definition: c_com.h:1195
MSGCNT MsgCount
Definition: c_com.h:1092
UBYTE ListSizeMsb
Definition: c_com.h:1058
#define Globals
UBYTE BytesToReadMsb
Definition: c_com.h:1043
UWORD cComReadBuffer(UBYTE *pBuffer, UWORD Size)
Definition: c_com.c:367
UBYTE MacSize
Definition: c_com.h:1351
#define KB
Definition: lms2012.h:553
CMDSIZE CmdSize
Definition: c_com.h:976
void SetObjectIp(IP Ip)
Set current instruction pointer.
Definition: lms2012.c:309
UWORD cBtReadCh5(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:815
ULONG Content[(MAILBOX_CONTENT_SIZE/4)+1]
Definition: c_com.h:1555
Definition: c_com.h:984
TXBUF TxBuf[NO_OF_CHS]
Definition: c_com.h:1574
UBYTE cComGetBtStatus(void)
Definition: c_com.c:5557
UWORD cBtDevWriteBuf7(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2667
UBYTE BytesToReadMsb
Definition: c_com.h:1152
UBYTE FileSizeLsb
Definition: c_com.h:1164
void cComCloseFileHandle(SLONG *pHandle)
Definition: c_com.c:512
UBYTE cBtGetHciBusyFlag(void)
Definition: c_bt.c:3433
ULONG RxBytes
Definition: c_com.h:1538
Definition: c_com.h:1422
MSGCNT MsgCount
Definition: c_com.h:1270
#define USB_CABLE_DETECT_RATE
Definition: c_com.c:81
UBYTE Mac[13]
Definition: c_com.h:1366
UBYTE CmdType
Definition: c_com.h:1231
IMGDATA Image[sizeof(IMGHEAD)+sizeof(OBJHEAD)+USB_CMD_IN_REP_SIZE-sizeof(DIRCMD)]
Definition: c_com.h:1564
#define SIZEOF_RPLYBLUETOOTHPIN
Definition: c_com.h:1370
void cWiFiControl(void)
Definition: c_wifi.c:3462
UBYTE ListSizeNsb1
Definition: c_com.h:1056
UWORD cBtDevWriteBuf3(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2607
Definition: c_com.h:964
void ResetDaisyPushCounter(void)
Definition: c_daisy.c:520
RESULT cComSetDeviceType(DATA8 Layer, DATA8 Port, DATA8 Type, DATA8 Mode)
Definition: c_com.c:262
DATA16 HANDLER
Memory list index.
Definition: lmstypes.h:85
RESULT cWiFiKnownDongleAttached(void)
Definition: c_wifi.c:1311
FIL * pFile
Definition: c_com.h:1523
UBYTE cComCheckForSpace(char *pFullName, ULONG Size)
Definition: c_com.c:917
#define MAX_MSG_SIZE
Definition: c_com.h:953
void * PrimParPointer(void)
Get next encoded parameter from byte code stream.
Definition: lms2012.c:694
Definition: lms2012.h:557
MSGCNT MsgCount
Definition: c_com.h:1348
RESULT cWiFiGetOnStatus(void)
Definition: c_wifi.c:3875
void cComReadData(void)
Definition: c_com.c:3522
#define DAISY_COMMAND_REPLY
Definition: c_daisy.h:60
UBYTE BtGetOnOff(UBYTE *On)
Definition: c_bt.c:2042
UBYTE Pin[7]
Definition: c_com.h:1368
UBYTE cBtGetDevListEntry(UBYTE Item, SBYTE *pConnected, SBYTE *pType, UBYTE *pName, SBYTE Length)
Definition: c_bt.c:3523
UBYTE Handle
Definition: c_com.h:1310
UBYTE cBtSetPin(UBYTE *pPin)
Definition: c_bt.c:4191
UBYTE BundleId[]
Definition: c_com.h:1378
IP GetObjectIp(void)
Get current instruction pointer.
Definition: lms2012.c:298
UBYTE CmdType
Definition: c_com.h:1052
Definition: c_wifi.h:139
CMDSIZE CmdSize
Definition: c_com.h:1317
MSGCNT MsgCount
Definition: c_com.h:1067
MSGCNT MsgCount
Definition: c_com.h:1051
RESULT cMemoryCheckFilename(char *pFilename, char *pPath, char *pName, char *pExt)
Definition: c_memory.c:552
UBYTE Name[]
Definition: c_com.h:1333
Definition: c_com.h:1416
UBYTE cComFindMailbox(UBYTE *pName, UBYTE *pNo)
Definition: c_com.c:3243
UBYTE CmdType
Definition: c_com.h:1319
UWORD cComWriteBuffer(UBYTE *pBuffer, UWORD Size)
Definition: c_com.c:395
MSGCNT MsgCount
Definition: c_com.h:1160
CMDSIZE CmdSize
Definition: c_com.h:1249
UBYTE Folder[vmFILENAMESIZE]
Definition: c_com.h:1530
DATA8 cDaisyGetUsbUpStreamSpeed(void)
Definition: c_daisy.c:960
#define DAISY_COMMAND_NO_REPLY
Definition: c_daisy.h:61
UBYTE NameSize
Definition: c_com.h:1332
Definition: c_com.c:101
#define SIZEOFFILESIZE
Definition: c_com.c:97
UBYTE PayLoad[]
Definition: c_com.h:1311
RESULT cComClose(void)
Definition: c_com.c:213
UBYTE FileSizeNsb1
Definition: c_com.h:1285
UBYTE Status
Definition: c_com.h:1054
RESULT cWiFiTurnOn(void)
Definition: c_wifi.c:3885
UBYTE cBtGetNoOfDevListEntries(void)
Definition: c_bt.c:3517
MSGCNT MsgCount
Definition: c_com.h:1188
RESULT cDaisyInit(void)
Definition: c_daisy.c:1212
UBYTE CmdType
Definition: c_com.h:1161
RESULT cDaisyGetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_daisy.c:1370
UBYTE PayLoad[]
Definition: c_com.h:1085
IMGDATA * IP
Instruction pointer type.
Definition: lmstypes.h:74
UBYTE cComDirectCommand(UBYTE *pBuffer, UBYTE *pReply)
Definition: c_com.c:420
UBYTE Mac[13]
Definition: c_com.h:1352
UBYTE Cmd
Definition: c_com.h:1309
UBYTE PayLoad[]
Definition: c_com.h:1169
CMDSIZE CmdSize
Definition: c_com.h:1229
CMDSIZE CmdSize
Definition: c_com.h:1404
CMDSIZE CmdSize
Definition: c_com.h:1159
UBYTE PayLoad[]
Definition: c_com.h:1193
UBYTE BytesToReadLsb
Definition: c_com.h:1071
void cDaisySetBusyFlags(UBYTE Layer, UBYTE Port, UBYTE MagicCookie)
Definition: c_daisy.c:391
unsigned int ULONG
Basic Type used to symbolise 32 bit unsigned values.
Definition: lmstypes.h:31
RESULT cComGetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_com.c:256
void cComSystemCommand(RXBUF *pRxBuf, TXBUF *pTxBuf)
cSystemCommand
Definition: c_com.c:1037
void cDaisyPrepareNext(void)
Definition: c_daisy.c:1643
RESULT cMemoryGetPointer(PRGID PrgId, HANDLER Handle, void **pMemory)
Definition: c_memory.c:252
UBYTE VmReady
Definition: c_com.h:1580
void cComRemove(void)
Definition: c_com.c:5516
MSGCNT MsgCount
Definition: c_com.h:1209
UBYTE FileSizeNsb2
Definition: c_com.h:1286
UWORD cBtReadCh0(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:706
void cComOpenMailBox(void)
opMAILBOX_OPEN byte code
Definition: c_com.c:3573
ULONG GBINDEX
GlobalBytes index type.
Definition: lmstypes.h:79
MSGCNT MsgCount
Definition: c_com.h:1039
COM_GLOBALS * gComInstance
Definition: c_com.c:68
#define SIZEOF_RPLYBUNDLEID
Definition: c_com.h:1390
UBYTE Status
Definition: c_com.h:1321
void cComReady(void)
opCOM_READY byte code
Definition: c_com.c:3281
UBYTE BrickName[vmBRICKNAMESIZE]
Definition: c_com.h:1585
#define MAILBOX_CONTENT_SIZE
Definition: c_com.h:955
void cBtSetTrustedDev(UBYTE *pBtAddr, UBYTE *pPin, UBYTE PinSize)
Definition: c_bt.c:4240
DATA32 Elements
Definition: c_memory.h:119
ULONG BufSize
Definition: c_com.h:1522
#define SIZEOF_RPLYLISTHANDLES
Definition: c_com.h:1215
void cWiFiDeleteAsKnown(int LocalIndex)
Definition: c_wifi.c:1112
DATA8 cComGetUsbStatus(void)
Definition: c_com.c:2831
UWORD cBtSetBundleId(UBYTE *pId)
Definition: c_bt.c:4265
UWORD cBtReadCh6(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:837
UBYTE cDaisyGetOwnLayer(void)
Definition: c_daisy.c:215
Definition: d_pwm.c:329
void cComCloseMailBox(void)
opMAILBOX_CLOSE byte code
Definition: c_com.c:3958
RESULT cWiFiInit(void)
Definition: c_wifi.c:4006
UWORD WriteCnt
Definition: c_com.h:1554
RESULT cWiFiGetIpAddr(char *IpAddress)
Definition: c_wifi.c:1197
UWORD cBtDevWriteBuf(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2547
CMDSIZE CmdSize
Definition: c_com.h:1103
UBYTE FileSizeLsb
Definition: c_com.h:1284
RESULT cWiFiExit(void)
Definition: c_wifi.c:3998
UBYTE PinSize
Definition: c_com.h:1353
UBYTE cBtDeleteFavourItem(UBYTE *pName)
Definition: c_bt.c:3556
ULONG SendBytes
Definition: c_com.h:1521
MSGCNT MsgCount
Definition: c_com.h:1385
MSGCNT MsgCount
Definition: c_com.h:1250
#define ComInstance
Definition: c_com.h:1591
RXBUF RxBuf[NO_OF_CHS]
Definition: c_com.h:1575
Definition: c_com.h:939
RESULT cWiFiSetName(char *ApName, int Index)
Definition: c_wifi.c:1699
#define SIZEOF_RPLYBUNDLESEEDID
Definition: c_com.h:1410
void cComWrite(void)
opCOM_WRITE byte code
Definition: c_com.c:3472
UWORD UsbConUpdate
Definition: c_com.c:83
CMDSIZE CmdSize
Definition: c_com.h:1050
UBYTE cBtGetStatus(void)
Definition: c_bt.c:3621
ULONG MsgLen
Definition: c_com.h:1519
UWORD cBtReadCh4(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:793
UWORD CMDSIZE
Definition: c_com.h:960
#define NO_OF_MAILBOXES
Definition: c_com.h:954
RESULT cWiFiConnectToAp(int Index)
Definition: c_wifi.c:2011
RESULT cMemoryCheckOpenWrite(char *pFileName)
Definition: c_memory.c:1199
RESULT cWiFiTurnOff(void)
Definition: c_wifi.c:3929
RESULT cComOpen(void)
Definition: c_com.c:203
UBYTE cComCheckUsbCable(void)
Definition: c_com.c:282
void cMemoryGetUsage(DATA32 *pTotal, DATA32 *pFree, DATA8 Force)
Definition: c_memory.c:104
MSGCNT MsgCount
Definition: c_com.h:1375
UBYTE State
Definition: c_com.h:1526
#define DIR_CMD_NO_REPLY_WITH_BUSY
Definition: c_com.h:933
FIL * pFile
Definition: c_com.h:1540
MSGCNT MsgCount
Definition: c_com.h:1148
UBYTE Handle
Definition: c_com.h:1300
RESULT cComSetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_com.c:249
CMDSIZE CmdSize
Definition: c_com.h:1295
UBYTE CmdType
Definition: c_com.h:1282
#define SIZEOF_CONTINUEDL
Definition: c_com.h:1313
UBYTE CmdType
Definition: c_com.h:1251
ULONG Pointer
Definition: c_com.h:1511
UBYTE Handle
Definition: c_com.h:1322
UBYTE Handle
Definition: c_com.h:1059
UBYTE Writing
Definition: c_com.h:1528
UBYTE Buf[1024]
Definition: c_com.h:1518
UWORD DataSize
Definition: c_com.h:1552
UBYTE BtStopScan(void)
Definition: c_bt.c:664
UBYTE FileSizeNsb1
Definition: c_com.h:1165
ULONG BlockLen
Definition: c_com.h:1520
UBYTE cBtGetSearchListEntry(UBYTE Item, SBYTE *pConnected, SBYTE *pType, SBYTE *pParred, UBYTE *pName, SBYTE Length)
Definition: c_bt.c:3542
void cBtGetId(UBYTE *pId, UBYTE Length)
Definition: c_bt.c:3645
MSGCNT MsgCount
Definition: c_com.h:1200
UWORD State
Definition: c_com.h:1512
UBYTE cBtGetNoOfConnListEntries(void)
Definition: c_bt.c:3483
UBYTE ComResult
Definition: c_com.h:1581
UBYTE Cmd
Definition: c_com.h:1387
UBYTE CommandReady
Definition: c_com.h:1567
CMDSIZE CmdSize
Definition: c_com.h:1208
RESULT cComExit(void)
Definition: c_com.c:233
void cDaisyControl(void)
Definition: c_daisy.c:2340
RESULT ValidateString(DATA8 *pString, DATA8 Set)
Definition: lms2012.c:5694
void cComTxUpdate(UBYTE ChNo)
Definition: c_com.c:2837
unsigned char UBYTE
Basic Type used to symbolise 8 bit unsigned values.
Definition: lmstypes.h:29
RESULT cDaisyGetDownstreamData(DATA8 Layer, DATA8 Sensor, DATA8 Length, DATA8 *pType, DATA8 *pMode, DATA8 *pData)
Definition: c_daisy.c:1670
UBYTE Status
Definition: c_com.h:1388
UBYTE ReplyStatus
Definition: c_com.h:1583
RESULT cComGetDeviceData(DATA8 Layer, DATA8 Port, DATA8 Length, DATA8 *pType, DATA8 *pMode, DATA8 *pData)
Definition: c_com.c:268
struct dirent ** namelist
Definition: c_com.h:1506
UBYTE PayLoad[]
Definition: c_com.h:1113
UBYTE ActiveComCh
Definition: c_com.h:1582
UWORD cBtReadCh1(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:727
UBYTE BytesToReadLsb
Definition: c_com.h:1042
UBYTE BytesToReadLsb
Definition: c_com.h:1180
HANDLER Handle
Definition: lms2012.h:1474
MSGCNT MsgCount
Definition: c_com.h:1230
UBYTE BytesToReadLsb
Definition: c_com.h:1095
#define SIZEOF_WRITETOMAILBOXPAYLOAD
Definition: c_com.h:1343
RESULT cDaisyChained(void)
Definition: c_daisy.c:1004
void cComCreateContinueDl(RXBUF *pRxBuf, TXBUF *pTxBuf)
Definition: c_com.c:687
UBYTE Name[50]
Definition: c_com.h:1548
Definition: c_com.h:1516
FIL Files[MAX_FILE_HANDLES]
Definition: c_com.h:1572
Definition: c_com.h:1534
FLOAT DATAF
VM Type for 4 byte floating point value.
Definition: lmstypes.h:64
UBYTE cComGetWifiStatus(void)
Definition: c_com.c:5563
RESULT cWiFiGetIndexFromName(char *Name, UBYTE *Index)
Definition: c_wifi.c:2152
UBYTE Cmd
Definition: c_com.h:1283
UBYTE cBtGetNoOfSearchListEntries(void)
Definition: c_bt.c:3536
UBYTE Path[]
Definition: c_com.h:1153
UBYTE FileHandle
Definition: c_com.h:1541
int md5_file(char *filename, int binary, unsigned char *md5_result)
Definition: c_md5.c:400
unsigned short UWORD
Basic Type used to symbolise 16 bit unsigned values.
Definition: lmstypes.h:30
UBYTE Status
Definition: c_com.h:1163
void SetUiUpdate(void)
Definition: lms2012.c:396
DATA8 cComGetHandle(char *pName)
Definition: c_com.c:542
UBYTE Globals[MAX_COMMAND_GLOBALS]
Definition: c_com.h:1566
void cComTestMailBox(void)
opMAILBOX_TEST byte code
Definition: c_com.c:3891
void cBtGetIncoming(UBYTE *pName, UBYTE *pCod, UBYTE Len)
Definition: c_bt.c:4184
void cComGetBrickName(DATA8 Length, DATA8 *pBrickName)
Definition: c_com.c:5583
CMDSIZE CmdSize
Definition: c_com.h:1187
UBYTE cBtSetPasskey(UBYTE Accept)
Definition: c_bt.c:4212
UBYTE Status
Definition: c_com.h:1233
void cComTest(void)
opCOM_TEST byte code
Definition: c_com.c:3350
UBYTE CmdType
Definition: c_com.h:1308
void cWiFiSetEncryptToNone(int Index)
Definition: c_wifi.c:1099
RESULT cDaisySetDeviceType(DATA8 Layer, DATA8 Port, DATA8 Type, DATA8 Mode)
Definition: c_daisy.c:1156
#define SIZEOF_RPLYMAKEDIR
Definition: c_com.h:1255
void BtExit(void)
Definition: c_bt.c:426
void cComWriteData(void)
Definition: c_com.c:3536
void cComSystemReply(RXBUF *pRxBuf, TXBUF *pTxBuf)
Definition: c_com.c:744
void cDaisyCmd(RXBUF *pRxBuf, TXBUF *pTxBuf)
Definition: c_daisy.c:722
DATA8 CheckSdcard(DATA8 *pChanged, DATA32 *pTotal, DATA32 *pFree, DATA8 Force)
Definition: lms2012.c:1855
MSGCNT MsgCount
Definition: c_com.h:1281
PRGID CurrentProgramId(void)
Get current program id.
Definition: lms2012.c:207
DIR * pDir
Definition: c_com.h:1529
int cWiFiGetApListSize(void)
Definition: c_wifi.c:2544
ULONG Length
Total down load length.
Definition: c_com.h:1510
UBYTE Cmd
Definition: c_com.h:1252
UBYTE Pin[7]
Definition: c_com.h:1354
RESULT cWiFiMakePsk(char *ApSsid, char *PassPhrase, int Index)
Definition: c_wifi.c:2078
CMDSIZE CmdSize
Definition: c_com.h:1079
void setComInstance(COM_GLOBALS *_Instance)
Definition: c_com.c:70
COM_GLOBALS * getComInstance()
Definition: c_com.c:75
void * VmMemoryResize(HANDLER Handle, DATA32 Elements)
Definition: lms2012.c:366
void cWiFiMoveUpInList(int Index)
Definition: c_wifi.c:465
unsigned char cWiFiGetFlags(int Index)
Definition: c_wifi.c:1726
UWORD PRGID
Program id type.
Definition: lmstypes.h:71
#define DAISY_PUSH_NOT_UNLOCKED
Definition: c_daisy.h:414
MSGCNT MsgCount
Definition: c_com.h:1296
RESULT cWiFiScanForAPs()
Definition: c_wifi.c:2477
RESULT cComInit(void)
Definition: c_com.c:104
UBYTE Type
Definition: c_com.h:1551
void cDaisyPushUpStream(void)
Definition: c_daisy.c:1442
UBYTE Handle
Definition: c_com.h:1179
#define SIZEOF_RPLYCLOSEHANDLE
Definition: c_com.h:1276
void cWiFiSetEncryptToWpa2(int Index)
Definition: c_wifi.c:1076
#define SIZEOF_RPLYREMOVEFILE
Definition: c_com.h:1235
SBYTE DATA8
VM Type for 1 byte signed value.
Definition: lmstypes.h:61
void cComReadyMailBox(void)
opMAILBOX_READY byte code
Definition: c_com.c:3923
RESULT cWiFiGetMyMacAddr(char *MacAddress)
Definition: c_wifi.c:1209
UWORD cBtDevWriteBuf4(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2622
UBYTE cBtGetEvent(void)
Definition: c_bt.c:4174
DATA8 pArray[]
Definition: c_memory.h:125
UWORD cWiFiReadTcp(UBYTE *Buffer, UWORD Length)
Definition: c_wifi.c:2824
void BtInit(char *pName)
Definition: c_bt.c:349
UBYTE BytesToReadLsb
Definition: c_com.h:1124
MSGCNT MsgCount
Definition: c_com.h:1395
char Name[vmFILENAMESIZE]
File name.
Definition: c_com.h:1505
void cComReadMailBox(void)
opMAILBOX_READ byte code
Definition: c_com.c:3781
UWORD cDaisyData(UBYTE **pData)
Definition: c_daisy.c:500
void cComGetNameFromScandirList(struct dirent *NameList, char *pBuffer, ULONG *pNameLen, UBYTE *pFolder)
Definition: c_com.c:863
void cComGet(void)
opCOM_GET byte code
Definition: c_com.c:4208
void cComSet(void)
opCOM_SET byte code
Definition: c_com.c:5045
UBYTE State
Definition: c_com.h:1542
UBYTE PayLoad[]
Definition: c_com.h:1027
UWORD cBtDevWriteBuf1(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2577
#define SIZEOF_RPLYCONTINUELIST
Definition: c_com.h:1087
RESULT cDaisySetDeviceInfo(DATA8 Length, UBYTE *pInfo)
Definition: c_daisy.c:1396
UBYTE BytesToReadMsb
Definition: c_com.h:1096
UBYTE FileSizeNsb2
Definition: c_com.h:1110
void cComCreateBeginDl(TXBUF *pTxBuf, UBYTE *pName)
Definition: c_com.c:595
Definition: c_com.h:1421
Definition: c_com.h:941
UBYTE BytesToReadMsb
Definition: c_com.h:1125
UBYTE Name[]
Definition: c_com.h:1223
#define USBSTICK_FOLDER
Definition: lms2012.h:216
UBYTE FileSizeNsb2
Definition: c_com.h:1166
void DecrementDaisyPushCounter(void)
Definition: c_daisy.c:525
UBYTE BtGetMode2(UBYTE *pMode2)
Definition: c_bt.c:1990
UBYTE BtSetVisibility(UBYTE State)
Definition: c_bt.c:2076
#define SIZEOF_RPLYCONTINUEGETFILE
Definition: c_com.h:1143
DSPSTAT
Definition: lms2012.h:665
ULONG BufSize
Definition: c_com.h:1539
UBYTE CmdType
Definition: c_com.h:1386
DATA8 cComGetEvent(void)
Definition: c_com.c:5589
void cComWriteFile(void)
Definition: c_com.c:3970
GLOBALS VMInstance
Definition: lms2012.c:131
UBYTE ListSizeNsb2
Definition: c_com.h:1057
void cWiFiIncApListSize(void)
Definition: c_wifi.c:2536
UWORD ReadCnt
Definition: c_com.h:1553
Break because of waiting for completion.
Definition: lms2012.h:671
UBYTE Path[]
Definition: c_com.h:1097
#define SDCARD_FOLDER
Definition: lms2012.h:215
Dispatcher running (looping)
Definition: lms2012.h:667
int Cmdfd
Definition: c_com.h:1579
MSGCNT MsgCount
Definition: c_com.h:1176
UBYTE BytesToReadMsb
Definition: c_com.h:1181
MSGCNT MsgCount
Definition: c_com.h:1318
UBYTE cComFreeHandle(DATA8 Handle)
Definition: c_com.c:522
#define MD5LEN
Definition: c_md5.h:25
Definition: c_wifi.h:140
ULONG MsgLen
Definition: c_com.h:1537
UBYTE Handle
Definition: c_com.h:1070
UBYTE FileSizeMsb
Definition: c_com.h:1167
#define SIZEOF_RPLYBEGINREAD
Definition: c_com.h:1171
CMDSIZE CmdSize
Definition: c_com.h:1280
IP OffsetToInstructions
Offset to instructions from image start.
Definition: lmstypes.h:169
signed int SLONG
Basic Type used to symbolise 32 bit signed values.
Definition: lmstypes.h:35
UWORD cBtDevWriteBuf5(UBYTE *pBuf, UWORD Size)
Definition: c_bt.c:2637
#define SIZEOF_RPLYBEGINDL
Definition: c_com.h:1302
#define NULL
UWORD cBtReadCh7(UBYTE *pBuf, UWORD Length)
Definition: c_bt.c:858
MSGCNT MsgCount
Definition: c_com.h:1220
CMDSIZE CmdSize
Definition: c_com.h:1360
UBYTE Handle
Definition: c_com.h:1263
int File
Definition: c_com.h:1508