?login_element?

Subversion Repositories NedoOS

Rev

Rev 2041 | Rev 2054 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. #define COMMANDLINE 0x0080
  13.  
  14. unsigned int RBR_THR = 0xf8ef;
  15. unsigned int IER = 0xf9ef;
  16. unsigned int IIR_FCR = 0xfaef;
  17. unsigned int LCR = 0xfbef;
  18. unsigned int MCR = 0xfcef;
  19. unsigned int LSR = 0xfdef;
  20. unsigned int MSR = 0xfeef;
  21. unsigned int SR = 0xffef;
  22. unsigned int divider = 1;
  23. unsigned char comType = 0;
  24.  
  25. unsigned char ver[] = "2.1";
  26. unsigned char queryType[64];
  27. unsigned char netbuf[3048];
  28. unsigned char dataBuffer[6132];
  29. unsigned char crlf[2] = {13, 10};
  30. unsigned char formats[4][4] = {"pt3", "pt2", "tfc", "ts"};
  31. unsigned char status, key, curFormat;
  32. struct sockaddr_in targetadr;
  33. struct readstructure readStruct;
  34. unsigned long contLen;
  35. long count;
  36. unsigned char saveFlag, saveBak, logFlag, rptFlag, netDriver;
  37. union APP_PAGES main_pg;
  38. union APP_PAGES player_pg;
  39. unsigned int loaded;
  40. unsigned int headlng;
  41. unsigned char cmd[256];
  42. unsigned char link[512];
  43.  
  44. struct fileStruct
  45. {
  46.   long picId;
  47.   unsigned long fileSize;
  48.   unsigned int picYear;
  49.   unsigned long totalAmount;
  50.   unsigned int curPos;
  51.   unsigned int startBar;
  52.   unsigned int trackInSeconds;
  53.   unsigned char time[16];
  54.   unsigned char picRating[8];
  55.   unsigned char trackName[256];
  56.   unsigned char fileName[256];
  57.   unsigned char authorIds[64];
  58.   unsigned char authorTitle[64];
  59.   unsigned char authorRealName[64];
  60.   unsigned char afn[64];
  61.   unsigned char tfn[64];
  62.   unsigned char fileName2[256];
  63. } curFileStruct;
  64.  
  65. void delay(unsigned long counter)
  66. {
  67.   unsigned long start, finish;
  68.   counter = counter / 20;
  69.   if (counter < 1)
  70.   {
  71.     counter = 1;
  72.   }
  73.   start = time();
  74.   finish = start + counter;
  75.  
  76.   while (start < finish)
  77.   {
  78.     start = time();
  79.   }
  80. }
  81.  
  82. void clearStatus(void)
  83. {
  84.   AT(1, 25);
  85.   printf("                                                                               \r");
  86. }
  87.  
  88. void printProgress(unsigned char type)
  89. {
  90.   unsigned char bar, minutes, seconds;
  91.   unsigned char *position;
  92.   long barLenght;
  93.   int timer;
  94.   switch (type)
  95.   {
  96.   case 0: // print empty bar
  97.     AT(6, 11);
  98.     ATRIB(93);
  99.     printf("%02u:%02u", 0, 0);
  100.     AT(15, 11);
  101.     ATRIB(97);
  102.     for (bar = 0; bar < 50; bar++)
  103.     {
  104.       putchar(176);
  105.     }
  106.     putchar(' ');
  107.     putchar(' ');
  108.     minutes = atoi(curFileStruct.time);
  109.     position = (strstr(curFileStruct.time, ":")) + 1;
  110.     seconds = atoi(position);
  111.     curFileStruct.trackInSeconds = minutes * 60 + seconds;
  112.     curFileStruct.curPos = 0;
  113.     curFileStruct.startBar = 0;
  114.     break;
  115.   case 1: // print progress bar
  116.  
  117.     AT(6, 11);
  118.     ATRIB(93);
  119.     timer = floor(curFileStruct.curPos / 60);
  120.     printf("%02u:%02u", timer, (curFileStruct.curPos - (timer * 60)));
  121.  
  122.     barLenght = (curFileStruct.curPos * 50 / curFileStruct.trackInSeconds);
  123.     if (barLenght > 49)
  124.     {
  125.       barLenght = 50;
  126.     }
  127.     AT(15 + curFileStruct.startBar, 11);
  128.     ATRIB(97);
  129.     for (bar = 0; bar < barLenght - curFileStruct.startBar; bar++)
  130.     {
  131.       putchar(178);
  132.     }
  133.     AT(1, 1);
  134.     curFileStruct.startBar = bar;
  135.     break;
  136.   case 2: // print full bar
  137.     AT(15, 11);
  138.     ATRIB(97);
  139.     for (bar = 0; bar < 50; bar++)
  140.     {
  141.       putchar(178);
  142.     }
  143.     break;
  144.   }
  145. }
  146. void errorPrint(unsigned int error)
  147. {
  148.   switch (error)
  149.   {
  150.   case 2:
  151.     printf("02 SHUT_RDWR");
  152.     break;
  153.   case 4:
  154.     printf("04 ERR_INTR");
  155.     break;
  156.   case 23:
  157.     printf("23 ERR_NFILE");
  158.     break;
  159.   case 35:
  160.     printf("35 ERR_EAGAIN");
  161.     break;
  162.   case 37:
  163.     printf("37 ERR_ALREADY");
  164.     break;
  165.   case 38:
  166.     printf("38 ERR_NOTSOCK");
  167.     break;
  168.   case 40:
  169.     printf("40 ERR_EMSGSIZE");
  170.     break;
  171.   case 41:
  172.     printf("41 ERR_PROTOTYPE");
  173.     break;
  174.   case 47:
  175.     printf("47 ERR_AFNOSUPPORT");
  176.     break;
  177.   case 53:
  178.     printf("53 ERR_ECONNABORTED");
  179.     break;
  180.   case 54:
  181.     printf("54 ERR_CONNRESET");
  182.     break;
  183.   case 57:
  184.     printf("57 ERR_NOTCONN");
  185.     break;
  186.   case 65:
  187.     printf("65 ERR_HOSTUNREACH");
  188.     break;
  189.   default:
  190.     printf("%u UNKNOWN ERROR", error);
  191.     break;
  192.   }
  193.   YIELD();
  194. }
  195.  
  196. void printHelp(void)
  197. {
  198.   AT(1, 15);
  199.   ATRIB(97);
  200.   printf(" [<-] Previous track              [->] Next track\r\n");
  201.   printf(" [S]  Stop player                 [R]  Repeat track mode\r\n");
  202.   printf(" [K]  Toggle saving tracks        [D]  Download track\r\n");
  203.   printf(" [Q]  Select Query type           [F]  Select tracks format\r\n");
  204.   printf(" [I]  Interface ZXNETUSB/ESP32    [J]  Jump to NNNN file\r\n");
  205.   printf(" [L]  Toggle operation logging    [ESC] Exit to OS\r\n");
  206.   printf("                                                        \r\n");
  207. }
  208.  
  209. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  210. {
  211.   unsigned char socket;
  212.   unsigned int todo;
  213.   todo = OS_NETSOCKET((family << 8) + protocol);
  214.   if (todo > 32767)
  215.   {
  216.     clearStatus();
  217.     printf("OS_NETSOCKET: ");
  218.     errorPrint(todo & 255);
  219.     exit(0);
  220.   }
  221.   else
  222.   {
  223.     socket = ((todo & 65280) >> 8);
  224.     if (logFlag)
  225.     {
  226.       clearStatus();
  227.       printf("OS_NETSOCKET: Socket #%d created.", socket);
  228.     }
  229.   }
  230.   return socket;
  231. }
  232.  
  233. unsigned int netShutDown(unsigned char socket, unsigned char type)
  234. {
  235.   unsigned int todo;
  236.   todo = OS_NETSHUTDOWN(socket, type);
  237.   if (todo > 32767)
  238.   {
  239.     clearStatus();
  240.     printf("OS_NETSHUTDOWN: ");
  241.     errorPrint(todo & 255);
  242.     return 255;
  243.   }
  244.   else
  245.   {
  246.     if (logFlag)
  247.     {
  248.       clearStatus();
  249.       printf("OS_NETSHUTDOWN: Socket #%u closed.", socket);
  250.     }
  251.   }
  252.   return 0;
  253. }
  254.  
  255. unsigned char netConnect(unsigned char socket)
  256. {
  257.   unsigned int todo, retry = 10;
  258.  
  259.   targetadr.family = AF_INET;
  260.   targetadr.porth = 00;
  261.   targetadr.portl = 80;
  262.   targetadr.b1 = 217;
  263.   targetadr.b2 = 146;
  264.   targetadr.b3 = 69;
  265.   targetadr.b4 = 13;
  266.   while (retry > 0)
  267.   {
  268.     todo = OS_NETCONNECT(socket, &targetadr);
  269.  
  270.     if (todo > 32767)
  271.     {
  272.       retry--;
  273.       clearStatus();
  274.       printf("OS_NETCONNECT [ERROR:");
  275.       errorPrint(todo & 255);
  276.       printf("] [Retry:%u]", retry);
  277.       YIELD();
  278.       netShutDown(socket, 0);
  279.       socket = OpenSock(AF_INET, SOCK_STREAM);
  280.     }
  281.     else
  282.     {
  283.       if (logFlag)
  284.       {
  285.         clearStatus();
  286.         printf("OS_NETCONNECT: connected , %u", (todo & 255));
  287.       }
  288.       return 1;
  289.     }
  290.   }
  291.   getchar();
  292.   exit(0);
  293.   return 0;
  294. }
  295.  
  296. unsigned int tcpRead(unsigned char socket)
  297. {
  298.   unsigned char retry = 20;
  299.   unsigned int err, todo;
  300.   readStruct.socket = socket;
  301.   readStruct.BufAdr = (unsigned int)&netbuf;
  302.   readStruct.bufsize = sizeof(netbuf);
  303.   readStruct.protocol = SOCK_STREAM;
  304.  
  305. wizread:
  306.   todo = OS_WIZNETREAD(&readStruct);
  307.   if (todo > 32767)
  308.   {
  309.     if (retry == 0)
  310.     {
  311.       err = todo & 255;
  312.       clearStatus();
  313.       printf("OS_WIZNETREAD: ");
  314.       errorPrint(err);
  315.       if (err == ERR_EAGAIN)
  316.       {
  317.         return 0;
  318.       }
  319.       exit(0);
  320.     }
  321.     retry--;
  322.     if (logFlag)
  323.     {
  324.       AT(54, 25);
  325.       printf("OS_WIZNETREAD: retry %u   ", retry);
  326.     }
  327.     delay(200);
  328.     goto wizread;
  329.   }
  330.   if (logFlag)
  331.   {
  332.     clearStatus();
  333.     printf("OS_WIZNETREAD: %u bytes read.", todo);
  334.   }
  335.   return todo;
  336. }
  337.  
  338. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  339. {
  340.   unsigned int i, j;
  341.   unsigned int lenC, lenS;
  342.  
  343.   for (lenC = 0; c[lenC]; lenC++)
  344.     ;
  345.   for (lenS = 0; s[lenS]; lenS++)
  346.     ;
  347.  
  348.   for (i = startPos; i <= lenS - lenC; i++)
  349.   {
  350.     for (j = 0; s[i + j] == c[j]; j++)
  351.       ;
  352.  
  353.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  354.       return i;
  355.     if (j == lenC)
  356.       if (n - 1)
  357.         n--;
  358.       else
  359.         return i;
  360.   }
  361.   return -1;
  362. }
  363.  
  364. unsigned int cutHeader(unsigned int todo)
  365. {
  366.   unsigned int q, headlng;
  367.   unsigned char *count;
  368.   count = strstr(netbuf, "Content-Length:");
  369.   if (count == NULL)
  370.   {
  371.     clearStatus();
  372.     printf("Content-Length:  not found.");
  373.     contLen = 0;
  374.   }
  375.   else
  376.   {
  377.     contLen = atol(count + 15);
  378.     curFileStruct.fileSize = contLen;
  379.     //  printf("=> Dlinna  soderzhimogo = %lu \n\r", curFileStruct.fileSize);
  380.   }
  381.  
  382.   count = strstr(netbuf, "\r\n\r\n");
  383.   headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
  384.   q = todo - headlng;
  385.   memcpy(&netbuf, count + 4, q);
  386.   return q;
  387. }
  388.  
  389. ////////////////////////ESP32 PROCEDURES//////////////////////
  390. void uart_write(unsigned char data)
  391. {
  392.   while ((input(LSR) & 64) == 0)
  393.   {
  394.   }
  395.   output(RBR_THR, data);
  396. }
  397.  
  398. void uart_setrts(unsigned char mode)
  399. {
  400.   switch (mode)
  401.   {
  402.   case 1:
  403.     output(MCR, 2);
  404.     break;
  405.   case 0:
  406.     output(MCR, 0);
  407.     break;
  408.   default:
  409.     disable_interrupt();
  410.     output(MCR, 2);
  411.     output(MCR, 0);
  412.     enable_interrupt();
  413.   }
  414. }
  415.  
  416. void uart_init(unsigned char divisor)
  417. {
  418.   clearStatus();
  419.   printf("Initing UART [divider:%u]", divisor);
  420.   output(MCR, 0x00);        // Disable input
  421.   output(IIR_FCR, 0x87);    // Enable fifo 8 level, and clear it
  422.   output(LCR, 0x83);        // 8n1, DLAB=1
  423.   output(RBR_THR, divisor); // 115200 (divider 1-115200, 3 - 38400)
  424.   output(IER, 0x00);        // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  425.   output(LCR, 0x03);        // 8n1, DLAB=0
  426.   output(IER, 0x00);        // Disable int
  427.   output(MCR, 0x2f);        // Enable AFE
  428.   uart_setrts(0);
  429. }
  430.  
  431. unsigned char uart_hasByte(void)
  432. {
  433.   return (1 & input(LSR));
  434. }
  435.  
  436. unsigned char uart_read(void)
  437. {
  438.   uart_setrts(2);
  439.   return input(RBR_THR);
  440. }
  441.  
  442. unsigned char uart_readBlock(void)
  443. {
  444.   while (uart_hasByte() == 0)
  445.   {
  446.     uart_setrts(2);
  447.   }
  448.   return input(RBR_THR);
  449. }
  450.  
  451. void uart_flush(void)
  452. {
  453.   unsigned int count;
  454.   for (count = 0; count < 6000; count++)
  455.   {
  456.     uart_setrts(1);
  457.     uart_read();
  458.   }
  459.   clearStatus();
  460.   printf("Buffer cleared.");
  461. }
  462. void getdataEsp(unsigned int counted)
  463. {
  464.   unsigned int counter;
  465.   for (counter = 0; counter < counted; counter++)
  466.   {
  467.     netbuf[counter] = uart_readBlock();
  468.   }
  469.   netbuf[counter] = 0;
  470. }
  471.  
  472. void sendcommand(char *commandline)
  473. {
  474.   unsigned int count, cmdLen;
  475.   cmdLen = strlen(commandline);
  476.   for (count = 0; count < cmdLen; count++)
  477.   {
  478.     uart_write(commandline[count]);
  479.   }
  480.   uart_write('\r');
  481.   uart_write('\n');
  482.   // printf("Sended:[%s] \r\n", commandline);
  483. }
  484. unsigned char getAnswer(unsigned char skip)
  485. {
  486.   unsigned char readbyte;
  487.   unsigned int curPos = 0;
  488.   while (skip != 0)
  489.   {
  490.     uart_readBlock();
  491.     skip--;
  492.   }
  493.   while (42)
  494.   {
  495.     readbyte = uart_readBlock();
  496.     if (readbyte == 0x0a)
  497.     {
  498.       break;
  499.     }
  500.     netbuf[curPos] = readbyte;
  501.     curPos++;
  502.   }
  503.   netbuf[curPos - 1] = 0;
  504.   // printf("Answer:[%s]\r\n", netbuf);
  505.   return curPos;
  506. }
  507. void espReBoot(void)
  508. {
  509.   unsigned char byte;
  510.   uart_flush();
  511.   sendcommand("AT+RST");
  512.   clearStatus();
  513.   printf("Resetting ESP...");
  514.   do
  515.   {
  516.     byte = uart_read();
  517.   } while (byte != 'P'); // WIFI GOT IP
  518.   uart_readBlock();      // CR
  519.   uart_readBlock();      // LN
  520.   clearStatus();
  521.   printf("Reset complete.");
  522.  
  523.   sendcommand("ATE0");
  524.   do
  525.   {
  526.     byte = uart_read();
  527.   } while (byte != 'K'); // OK
  528.   // puts("Answer:[OK]");
  529.   uart_readBlock(); // CR
  530.   uart_readBlock(); // LN
  531.  
  532.   sendcommand("AT+CIPCLOSE");
  533.   getAnswer(2);
  534.   sendcommand("AT+CIPDINFO=0");
  535.   getAnswer(2);
  536.   sendcommand("AT+CIPMUX=0");
  537.   getAnswer(2);
  538.   sendcommand("AT+CIPSERVER=0");
  539.   getAnswer(2);
  540.   sendcommand("AT+CIPRECVMODE=1");
  541.   getAnswer(2);
  542. }
  543. unsigned int recvHead(void)
  544. {
  545.   unsigned char byte, dataRead = 0;
  546.   do
  547.   {
  548.     byte = uart_readBlock();
  549.     netbuf[dataRead] = byte;
  550.     dataRead++;
  551.   } while (byte != ',');
  552.   netbuf[dataRead] = 0;
  553.   loaded = atoi(netbuf + 13); // <actual_len>
  554.   return loaded;
  555. }
  556. // in netbuf data to send
  557. unsigned int fillDataBufferEsp(void)
  558. {
  559.   unsigned int packSize = 2000;
  560.   unsigned char link[512];
  561.   unsigned char sizeLink;
  562.   unsigned long toDownload, downloaded;
  563.   unsigned char byte;
  564.   unsigned int dataSize;
  565.   unsigned char skipHeader;
  566.   strcpy(link, netbuf);
  567.   strcat(link, "\r\n");
  568.   sizeLink = strlen(link);
  569.   sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  570.   getAnswer(2); // CONNECT
  571.   getAnswer(0); // OK
  572.   strcpy(cmd, "AT+CIPSEND=");
  573.   sprintf(netbuf, "%u", sizeLink + 2); // second CRLF in send command
  574.   strcat(cmd, netbuf);
  575.   sendcommand(cmd);
  576.   byte = 0;
  577.   while (byte != '>')
  578.   {
  579.     byte = uart_readBlock();
  580.     // putchar(byte);
  581.   }
  582.   sendcommand(link);
  583.   getAnswer(2); // Recv 132 bytes
  584.   getAnswer(2); // SEND OK
  585.   getAnswer(2); //+IPD,3872
  586.   skipHeader = 0;
  587.   downloaded = 0;
  588.   do
  589.   {
  590.     headlng = 0;
  591.     strcpy(netbuf, "AT+CIPRECVDATA=");
  592.     sprintf(link, "%u", packSize);
  593.     strcat(netbuf, link);
  594.     sendcommand(netbuf);
  595.     dataSize = recvHead();
  596.     getdataEsp(dataSize); // Requested size
  597.     if (skipHeader == 0)
  598.     {
  599.       dataSize = cutHeader(dataSize);
  600.       toDownload = contLen;
  601.       skipHeader = 1;
  602.     }
  603.     downloaded = downloaded + dataSize;
  604.     memcpy(dataBuffer + downloaded - dataSize, netbuf + headlng, dataSize);
  605.     toDownload = toDownload - dataSize;
  606.     getAnswer(2); // OK
  607.     if (toDownload > 0)
  608.     {
  609.       getAnswer(2); // +IPD,1824
  610.     }
  611.   } while (toDownload > 0);
  612.   sendcommand("AT+CIPCLOSE");
  613.   getAnswer(0); // CLOSED
  614.   return 0;
  615. }
  616. void loadEspConfig(void)
  617. {
  618.   unsigned char curParam[256];
  619.   unsigned char res;
  620.   FILE *espcom;
  621.   OS_SETSYSDRV();
  622.   OS_CHDIR("browser");
  623.   espcom = OS_OPENHANDLE("espcom.ini", 0x80);
  624.   if (((int)espcom) & 0xff)
  625.   {
  626.     clearStatus();
  627.     printf("mrfesp.ini opening error");
  628.     return;
  629.   }
  630.  
  631.   OS_READHANDLE(curParam, espcom, 256);
  632.  
  633.   res = sscanf(curParam, "%x %x %x %x %x %x %x %x %u", &RBR_THR, &IER, &IIR_FCR, &LCR, &MCR, &LSR, &MSR, &SR, &divider, &comType);
  634.  
  635.   BOX(1, 15, 80, 8, 40);
  636.   AT(1, 15);
  637.   puts("Config loaded:");
  638.   printf("     RBR_THR:0x%4x     IER    :0x%4x\r\n     IIR_FCR:0x%4x     LCR    :0x%4x\r\n", RBR_THR, IER, IIR_FCR, LCR);
  639.   printf("     MCR    :0x%4x     LSR    :0x%4x\r\n     MSR    :0x%4x     SR     :0x%4x\r\n", MCR, LSR, MSR, SR);
  640.   printf("     DIV    :%4u       TYPE   :%4u(", divider, comType);
  641.   switch (comType)
  642.   {
  643.   case 0:
  644.     puts("16550 like w/o AFC)");
  645.     break;
  646.   case 1:
  647.     puts("ATM Turbo 2+)");
  648.     break;
  649.   case 2:
  650.     puts("16550 with AFC)");
  651.   default:
  652.     puts("Unknown type)");
  653.     break;
  654.   }
  655. }
  656.  
  657. ////////////////////////ESP32 PROCEDURES//////////////////////
  658.  
  659. char *str_replace(char *dst, int num, const char *str,
  660.                   const char *orig, const char *rep)
  661. {
  662.   const char *ptr;
  663.   size_t len1 = strlen(orig);
  664.   size_t len2 = strlen(rep);
  665.   char *tmp = dst;
  666.  
  667.   num -= 1;
  668.   while ((ptr = strstr(str, orig)) != NULL)
  669.   {
  670.     num -= (ptr - str) + len2;
  671.     if (num < 1)
  672.       break;
  673.  
  674.     strncpy(dst, str, (size_t)(ptr - str));
  675.     dst += ptr - str;
  676.     strncpy(dst, rep, len2);
  677.     dst += len2;
  678.     str = ptr + len1;
  679.   }
  680.  
  681.   for (; (*dst = *str) && (num > 0); --num)
  682.   {
  683.     ++dst;
  684.     ++str;
  685.   }
  686.   return tmp;
  687. }
  688.  
  689. const char *parseJson(unsigned char *property)
  690. {
  691.   unsigned int w, lng, lngp1, findEnd, listPos;
  692.   unsigned char terminator;
  693.   int n;
  694.   n = -1;
  695.   netbuf[0] = '\0';
  696.   n = pos(dataBuffer, property, 1, 0);
  697.   if (n == -1)
  698.   {
  699.     strcpy(netbuf, "0\0");
  700.     return netbuf;
  701.   }
  702.   lng = n - 1 + strlen(property);
  703.   if (dataBuffer[lng] == ':')
  704.   {
  705.     terminator = '\0';
  706.   }
  707.   if (dataBuffer[lng] == '\"')
  708.   {
  709.     terminator = '\"';
  710.   }
  711.   if (dataBuffer[lng] == '[')
  712.   {
  713.     terminator = ']';
  714.   }
  715.  
  716.   findEnd = 1;
  717.   lngp1 = lng + 1;
  718.  
  719.   while (42)
  720.   {
  721.  
  722.     if ((dataBuffer[lngp1 + findEnd] == ','))
  723.     {
  724.       if (terminator == '\0')
  725.       {
  726.         break;
  727.       }
  728.       if ((dataBuffer[lng + findEnd] == terminator))
  729.       {
  730.         findEnd--;
  731.         break;
  732.       }
  733.     }
  734.     findEnd++;
  735.   }
  736.   listPos = 0;
  737.   for (w = lngp1; w < findEnd + lngp1; w++)
  738.   {
  739.     netbuf[listPos] = dataBuffer[w];
  740.     listPos++;
  741.   }
  742.   netbuf[listPos] = '\0';
  743.   return netbuf;
  744. }
  745. void convert866(void)
  746. {
  747.   unsigned int lng, targetPos, w, q = 0;
  748.   unsigned char buffer[8], one, two;
  749.   unsigned int decVal;
  750.   lng = strlen(netbuf);
  751.   targetPos = lng + 1;
  752.  
  753.   while (q < lng)
  754.   {
  755.     one = netbuf[q];
  756.     two = netbuf[q + 1];
  757.     if (one == 92 && two == 117)
  758.     {
  759.       q = q + 2;
  760.       for (w = 0; w < 4; w++)
  761.       {
  762.         buffer[w] = netbuf[q + w];
  763.       }
  764.       q = q + 4;
  765.       buffer[4] = '\0';
  766.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  767.  
  768.       if (decVal < 1088)
  769.       {
  770.         decVal = decVal - 912;
  771.       }
  772.       if (decVal > 1087)
  773.       {
  774.         decVal = decVal - 864;
  775.       }
  776.       if (decVal == 1025)
  777.       {
  778.         decVal = 240;
  779.       }
  780.       if (decVal == 1105)
  781.       {
  782.         decVal = 241;
  783.       }
  784.       netbuf[targetPos] = decVal;
  785.     }
  786.     else
  787.     {
  788.       netbuf[targetPos] = netbuf[q];
  789.       q++;
  790.     }
  791.     targetPos++;
  792.   }
  793.   netbuf[targetPos] = '\0';
  794.  
  795.   for (w = lng + 1; w < targetPos + 1; w++)
  796.   {
  797.     netbuf[w - lng - 1] = netbuf[w];
  798.   }
  799. }
  800.  
  801. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  802. {
  803.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  804.   str_replace(pfn, tfnSize, pfn, "/", "_");
  805.   str_replace(pfn, tfnSize, pfn, ":", "_");
  806.   str_replace(pfn, tfnSize, pfn, "*", "_");
  807.   str_replace(pfn, tfnSize, pfn, "?", "_");
  808.   str_replace(pfn, tfnSize, pfn, "<", "_");
  809.   str_replace(pfn, tfnSize, pfn, ">", "_");
  810.   str_replace(pfn, tfnSize, pfn, "|", "_");
  811.   str_replace(pfn, tfnSize, pfn, " ", "_");
  812.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  813.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  814.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  815.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  816.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  817.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  818. }
  819.  
  820. void stringRepair(unsigned char *pfn, unsigned int tSize)
  821. {
  822.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  823.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  824.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  825.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  826.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  827.   str_replace(pfn, tSize, pfn, "\\/", "/");
  828. }
  829.  
  830. void ncReplace(void)
  831. {
  832.   unsigned char len;
  833.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  834.   {
  835.     if (curFileStruct.afn[len] < ' ')
  836.     {
  837.       curFileStruct.afn[len] = '_';
  838.     }
  839.   }
  840.  
  841.   for (len = 0; len < strlen(curFileStruct.tfn); len++)
  842.   {
  843.     if (curFileStruct.tfn[len] < ' ')
  844.     {
  845.       curFileStruct.tfn[len] = '_';
  846.     }
  847.   }
  848. }
  849.  
  850. unsigned char saveBuf(unsigned long fileId, unsigned char operation, unsigned int sizeOfBuf)
  851. {
  852.   FILE *fp2;
  853.   unsigned long fileSize;
  854.   unsigned char afnSize, tfnSize;
  855.   unsigned char fileIdChar[10];
  856.  
  857.   if (operation == 00)
  858.   {
  859.  
  860.     if (saveFlag == 0)
  861.     {
  862.       sprintf(curFileStruct.fileName, "temp.%s", formats[curFormat]);
  863.     }
  864.     else
  865.     {
  866.       afnSize = sizeof(curFileStruct.afn) - 1;
  867.       tfnSize = sizeof(curFileStruct.tfn) - 1;
  868.  
  869.       strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  870.       nameRepair(curFileStruct.afn, afnSize);
  871.       strcpy(curFileStruct.tfn, curFileStruct.trackName);
  872.       nameRepair(curFileStruct.tfn, tfnSize);
  873.       sprintf(curFileStruct.fileName, "%s-%s.%s", curFileStruct.afn, curFileStruct.tfn, formats[curFormat]);
  874.       ncReplace();
  875.  
  876.       if (strlen(curFileStruct.fileName) > 63)
  877.       {
  878.         sprintf(fileIdChar, "-%ld", fileId);
  879.         str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  880.         curFileStruct.fileName[50] = '\0';
  881.         strcat(curFileStruct.fileName, fileIdChar);
  882.         strcat(curFileStruct.fileName, formats[curFormat]);
  883.       }
  884.     }
  885.     OS_SETSYSDRV();
  886.     OS_MKDIR("../downloads/radio"); // Create if not exist
  887.     OS_CHDIR("../downloads/radio");
  888.     fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  889.     if (((int)fp2) & 0xff)
  890.     {
  891.       clearStatus();
  892.       printf("%s creating error. Check for  downloads\\radio folder.", curFileStruct.fileName);
  893.       getchar();
  894.       exit(0);
  895.     }
  896.     OS_CLOSEHANDLE(fp2);
  897.     return 0;
  898.   }
  899.  
  900.   if (operation == 01)
  901.   {
  902.     fp2 = OS_OPENHANDLE(curFileStruct.fileName, 0x80);
  903.     if (((int)fp2) & 0xff)
  904.     {
  905.  
  906.       clearStatus();
  907.       printf("%s opening error.", curFileStruct.fileName);
  908.       exit(0);
  909.     }
  910.     fileSize = OS_GETFILESIZE(fp2);
  911.     OS_SEEKHANDLE(fp2, fileSize);
  912.     OS_WRITEHANDLE(netbuf, fp2, sizeOfBuf);
  913.     OS_CLOSEHANDLE(fp2);
  914.     return 0;
  915.   }
  916.  
  917.   if (operation == 02)
  918.   {
  919.     OS_CLOSEHANDLE(fp2);
  920.     return 0;
  921.   }
  922.  
  923.   return 0;
  924. }
  925.  
  926. void getData(unsigned char socket)
  927. {
  928.   unsigned int todo, w, bPos, skipHeader;
  929.  
  930.   skipHeader = 0;
  931.   bPos = 0;
  932.   while (1)
  933.   {
  934.     todo = tcpRead(socket);
  935.     if (todo == 0)
  936.     {
  937.       break;
  938.     }
  939.     if (skipHeader == 0)
  940.     {
  941.       skipHeader = 1;
  942.       todo = cutHeader(todo);
  943.     }
  944.  
  945.     if (bPos + todo > sizeof(dataBuffer))
  946.     {
  947.       clearStatus();
  948.       printf("dataBuffer overrun...");
  949.       break;
  950.     }
  951.  
  952.     for (w = 0; w < todo; w++)
  953.     {
  954.       dataBuffer[w + bPos] = netbuf[w];
  955.     }
  956.     bPos = bPos + todo;
  957.     if (bPos == contLen)
  958.     {
  959.       // dataBuffer[todo + bPos + 1] = '\0';
  960.       break;
  961.     }
  962.   }
  963.   netShutDown(socket, 1);
  964. }
  965.  
  966. unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
  967. {
  968.   unsigned char retry = 20;
  969.   unsigned int todo;
  970.   readStruct.socket = socket;
  971.   readStruct.BufAdr = messageadr;
  972.   readStruct.bufsize = size;
  973.   readStruct.protocol = SOCK_STREAM;
  974.  
  975. wizwrite:
  976.   todo = OS_WIZNETWRITE(&readStruct);
  977.   if (todo > 32767)
  978.   {
  979.     clearStatus();
  980.     printf("OS_WIZNETWRITE: ");
  981.     errorPrint(todo & 255);
  982.     if (retry == 0)
  983.     {
  984.       exit(0);
  985.     }
  986.     retry--;
  987.     delay(150);
  988.     goto wizwrite;
  989.   }
  990.   else
  991.   {
  992.     if (logFlag)
  993.     {
  994.       clearStatus();
  995.       printf("OS_WIZNETWRITE: %u bytes written.", todo);
  996.     }
  997.   }
  998.   return todo;
  999. }
  1000.  
  1001. unsigned long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  1002. {
  1003.   FILE *fp3;
  1004.   unsigned int retry, tSize;
  1005.   unsigned int todo;
  1006.   unsigned char buffer[] = "000000000";
  1007.   unsigned char *count, socket;
  1008.   unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; Radio)\r\n\r\n\0";
  1009.   unsigned char userQuery[256] = "/api/export:zxMusic/limit:10/filter:zxMusicId=44816";
  1010.   clearStatus();
  1011.   printf("Getting data(%u)...", queryNum);
  1012.  
  1013.   switch (queryNum)
  1014.   {
  1015.   case 0: // GET /api/export:zxMusic/limit:1/start:1/filter:zxMusicFormat=pt3/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  1016.     strcpy(netbuf, "GET /api/export:zxMusic/limit:");
  1017.     sprintf(buffer, "%u", limit);
  1018.     strcat(netbuf, buffer);
  1019.     strcat(netbuf, "/start:");
  1020.     sprintf(buffer, "%lu", startPos);
  1021.     strcat(netbuf, buffer);
  1022.     strcat(netbuf, "/filter:zxMusicFormat=");
  1023.     strcat(netbuf, formats[curFormat]);
  1024.     strcat(netbuf, "/order:date,desc");
  1025.     strcat(netbuf, userAgent);
  1026.     break;
  1027.   case 1: // GET /api/types:zxMusic/export:zxMusic/language:eng/limit:1/start:0/order:votes,rand/filter:zxMusicMinRating=4;
  1028.     startPos = 0;
  1029.     strcpy(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:");
  1030.     sprintf(buffer, "%u", limit);
  1031.     strcat(netbuf, buffer);
  1032.     strcat(netbuf, "/start:");
  1033.     sprintf(buffer, "%lu", startPos);
  1034.     strcat(netbuf, buffer);
  1035.     strcat(netbuf, "/order:votes,rand/filter:zxMusicMinRating=4;zxMusicFormat=");
  1036.     strcat(netbuf, formats[curFormat]);
  1037.     strcat(netbuf, userAgent);
  1038.     break;
  1039.   case 2: // GET /api/types:zxMusic/export:zxMusic/language:eng/limit:1/start:0/order:rand/filter:zxMusicFormat=PT3 HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  1040.     startPos = 0;
  1041.     strcpy(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:");
  1042.     sprintf(buffer, "%u", limit);
  1043.     strcat(netbuf, buffer);
  1044.     strcat(netbuf, "/start:");
  1045.     sprintf(buffer, "%lu", startPos);
  1046.     strcat(netbuf, buffer);
  1047.     strcat(netbuf, "/order:rand/filter:zxMusicFormat=");
  1048.     strcat(netbuf, formats[curFormat]);
  1049.     strcat(netbuf, userAgent);
  1050.     break;
  1051.  
  1052.   case 3: // GET /api/export:zxMusic/limit:1/start:1/filter:zxMusicFormat=pt3;authorId=7744/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  1053.  
  1054.     fp3 = OS_OPENHANDLE("radio/user.que", 0x80);
  1055.     if (((int)fp3) & 0xff)
  1056.     {
  1057.       fp3 = OS_CREATEHANDLE("radio/user.que", 0x80);
  1058.       OS_WRITEHANDLE(userQuery, fp3, sizeof(userQuery));
  1059.       OS_CLOSEHANDLE(fp3);
  1060.       fp3 = OS_OPENHANDLE("radio/user.que", 0x80);
  1061.     }
  1062.     OS_READHANDLE(userQuery, fp3, sizeof(userQuery));
  1063.     OS_CLOSEHANDLE(fp3);
  1064.  
  1065.     strcpy(netbuf, "GET /api/limit:");
  1066.     sprintf(buffer, "%u", limit);
  1067.     strcat(netbuf, buffer);
  1068.     strcat(netbuf, "/start:");
  1069.     sprintf(buffer, "%lu", startPos);
  1070.     strcat(netbuf, buffer);
  1071.     strcat(netbuf, userQuery);
  1072.     strcat(netbuf, userAgent);
  1073.     break;
  1074.  
  1075.   case 99: // GET /jsonElementData/elementId:182798
  1076.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  1077.     sprintf(buffer, "%lu", startPos);
  1078.     strcat(netbuf, buffer);
  1079.     strcat(netbuf, userAgent);
  1080.     break;
  1081.   }
  1082.  
  1083.   retry = 10;
  1084.  
  1085.   while (42)
  1086.   {
  1087.     if (netDriver == 0)
  1088.     {
  1089.       socket = OpenSock(AF_INET, SOCK_STREAM);
  1090.       netConnect(socket);
  1091.       todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  1092.       getData(socket);
  1093.       clearStatus();
  1094.       printf("Processing data (%u)...", queryNum);
  1095.     }
  1096.     else
  1097.     {
  1098.       fillDataBufferEsp();
  1099.     }
  1100.  
  1101.     count = strstr(dataBuffer, "responseStatus\":\"success");
  1102.     if (count == NULL)
  1103.     {
  1104.       retry--;
  1105.       clearStatus();
  1106.       printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u][Retry:%u] [Track:%lu]\r\n", queryNum, retry, startPos);
  1107.       YIELD();
  1108.       if (retry < 1)
  1109.       {
  1110.         return -1;
  1111.       }
  1112.     }
  1113.     else
  1114.     {
  1115.       break;
  1116.     }
  1117.   }
  1118.   count = strstr(dataBuffer, "\"id\":");
  1119.   if (count == NULL)
  1120.   {
  1121.     clearStatus();
  1122.     printf("BAD JSON: not ID query = %u startPos = %lu", queryNum, startPos);
  1123.     return -2;
  1124.   }
  1125.   if (queryNum < 4)
  1126.   {
  1127.     netbuf[0] = '\0';
  1128.     parseJson("\"id\":");
  1129.     curFileStruct.picId = atol(netbuf);
  1130.     parseJson(",\"title\":\"");
  1131.     convert866();
  1132.     strcpy(curFileStruct.trackName, netbuf);
  1133.  
  1134.     tSize = sizeof(curFileStruct.trackName);
  1135.     stringRepair(curFileStruct.trackName, tSize);
  1136.  
  1137.     parseJson("\"rating\":\"");
  1138.     strcpy(curFileStruct.picRating, netbuf);
  1139.     parseJson("\"year\":\"");
  1140.     curFileStruct.picYear = atoi(netbuf);
  1141.     parseJson("\"totalAmount\":");
  1142.     curFileStruct.totalAmount = atol(netbuf);
  1143.     parseJson("\"time\":\"");
  1144.     strcpy(curFileStruct.time, netbuf);
  1145.     parseJson("\"authorIds\":[");
  1146.     strcpy(curFileStruct.authorIds, netbuf);
  1147.     parseJson("\"authorIds\":[");
  1148.     strcpy(curFileStruct.fileName2, netbuf);
  1149.   }
  1150.   if (queryNum == 99)
  1151.   {
  1152.     parseJson(",\"title\":\"");
  1153.     convert866();
  1154.     strcpy(curFileStruct.authorTitle, netbuf);
  1155.     parseJson(",\"realName\":\"");
  1156.     convert866();
  1157.     strcpy(curFileStruct.authorRealName, netbuf);
  1158.   }
  1159.   return curFileStruct.picId;
  1160. }
  1161.  
  1162. unsigned char getTrack2(unsigned long fileId)
  1163. {
  1164.   unsigned int todo;
  1165.   unsigned char cmdlist1[] = "GET /file/id:";
  1166.   unsigned char cmdlist2[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; Radio)\r\n\r\n\0";
  1167.   unsigned char buffer[] = "0000000000";
  1168.   unsigned char socket;
  1169.   unsigned int skipHeader = 0;
  1170.   unsigned long bytecount;
  1171.   unsigned int packSize = 2000;
  1172.   unsigned char sizeLink;
  1173.   unsigned long toDownload, downloaded;
  1174.   unsigned char byte = 0;
  1175.   unsigned int dataSize;
  1176.  
  1177.   if (netDriver == 0)
  1178.   {
  1179.     clearStatus();
  1180.     printf("Getting track...");
  1181.  
  1182.     strcpy(netbuf, cmdlist1);
  1183.     sprintf(buffer, "%lu", fileId);
  1184.     strcat(netbuf, buffer);
  1185.     strcat(netbuf, cmdlist2);
  1186.  
  1187.     socket = OpenSock(AF_INET, SOCK_STREAM);
  1188.     todo = netConnect(socket);
  1189.     todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  1190.     saveBuf(curFileStruct.picId, 00, 0);
  1191.     do
  1192.     {
  1193.       todo = tcpRead(socket);
  1194.       if (todo == 0)
  1195.       {
  1196.         break;
  1197.       }
  1198.       if (skipHeader == 0)
  1199.       {
  1200.         skipHeader = 1;
  1201.         todo = cutHeader(todo);
  1202.         bytecount = contLen;
  1203.       }
  1204.       saveBuf(curFileStruct.picId, 01, todo);
  1205.       bytecount = bytecount - todo;
  1206.     } while (bytecount != 0);
  1207.     netShutDown(socket, 0);
  1208.   }
  1209.   else
  1210.   {
  1211.     clearStatus();
  1212.     printf("Getting track...");
  1213.  
  1214.     sprintf(buffer, "%lu", fileId);
  1215.     strcpy(netbuf, cmdlist1);
  1216.     strcat(netbuf, buffer);
  1217.     strcat(netbuf, cmdlist2);
  1218.     saveBuf(curFileStruct.picId, 00, 0);
  1219.  
  1220.     strcpy(link, netbuf);
  1221.     sizeLink = strlen(link);
  1222.     sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  1223.     getAnswer(2); // CONNECT
  1224.     getAnswer(0); // OK
  1225.  
  1226.     strcpy(netbuf, cmdlist1);
  1227.     sprintf(buffer, "%lu", fileId);
  1228.     strcat(netbuf, buffer);
  1229.     strcat(netbuf, cmdlist2);
  1230.     strcpy(cmd, "AT+CIPSEND=");
  1231.     sprintf(netbuf, "%u", sizeLink + 2); // second CRLF in send command
  1232.     strcat(cmd, netbuf);
  1233.     sendcommand(cmd);
  1234.     do
  1235.     {
  1236.       byte = uart_readBlock();
  1237.       // putchar(byte);
  1238.     } while (byte != '>');
  1239.  
  1240.     sendcommand(link);
  1241.     getAnswer(2); // Recv 132 bytes
  1242.     getAnswer(2); // SEND OK
  1243.     getAnswer(2); //+IPD,3872
  1244.     downloaded = 0;
  1245.     do
  1246.     {
  1247.       headlng = 0;
  1248.       strcpy(netbuf, "AT+CIPRECVDATA=");
  1249.       sprintf(link, "%u", packSize);
  1250.       strcat(netbuf, link);
  1251.       sendcommand(netbuf);
  1252.       dataSize = recvHead();
  1253.       getdataEsp(dataSize); // Requested size
  1254.       if (skipHeader == 0)
  1255.       {
  1256.         dataSize = cutHeader(dataSize);
  1257.         toDownload = contLen;
  1258.         skipHeader = 1;
  1259.       }
  1260.       downloaded = downloaded + dataSize;
  1261.       saveBuf(curFileStruct.picId, 01, dataSize);
  1262.       toDownload = toDownload - dataSize;
  1263.       getAnswer(2); // OK
  1264.       if (toDownload > 0)
  1265.       {
  1266.         getAnswer(2); // +IPD,1824
  1267.       }
  1268.     } while (toDownload > 0);
  1269.     sendcommand("AT+CIPCLOSE");
  1270.     getAnswer(0); // CLOSED
  1271.   }
  1272.   return 0;
  1273. }
  1274.  
  1275. unsigned char runPlayer(void)
  1276. {
  1277.   FILE *fp2;
  1278.   unsigned char fileName[] = "radio/player.ovl";
  1279.   unsigned char appCmd[128] = "player.com ";
  1280.   unsigned char curPath[128];
  1281.   unsigned long playerSize, loaded, loop;
  1282.   unsigned char pgbak;
  1283.  
  1284.   clearStatus();
  1285.   printf("Running player...");
  1286.  
  1287.   strcat(appCmd, curFileStruct.fileName);
  1288.   player_pg.l = OS_GETMAINPAGES();
  1289.   pgbak = main_pg.pgs.window_3;
  1290.   loaded = 0;
  1291.   OS_GETPATH((unsigned int)&curPath);
  1292.   OS_SETSYSDRV();
  1293.   fp2 = OS_OPENHANDLE(fileName, 0x80);
  1294.   if (((int)fp2) & 0xff)
  1295.   {
  1296.     clearStatus();
  1297.     printf("%s", fileName);
  1298.     printf(" not found.");
  1299.     exit(0);
  1300.   }
  1301.   playerSize = OS_GETFILESIZE(fp2);
  1302.   OS_CHDIR(curPath);
  1303.   OS_NEWAPP((unsigned int)&player_pg);
  1304.   SETPG32KHIGH(player_pg.pgs.window_3);
  1305.   memcpy((char *)(0xC080), &appCmd, sizeof(appCmd));
  1306.   for (loop = 0; loop < playerSize; loop = loop + loaded)
  1307.   {
  1308.     loaded = OS_READHANDLE(dataBuffer, fp2, sizeof(dataBuffer));
  1309.     memcpy((char *)(0xC100 + loop), &dataBuffer, loaded);
  1310.   }
  1311.   OS_CLOSEHANDLE(fp2);
  1312.   SETPG32KHIGH(pgbak);
  1313.   OS_RUNAPP(player_pg.pgs.pId);
  1314.   return player_pg.pgs.pId;
  1315. }
  1316.  
  1317. long trackSelector(unsigned char mode)
  1318. {
  1319.   switch (mode)
  1320.   {
  1321.   case 0: // Next track
  1322.     count++;
  1323.     if (count > curFileStruct.totalAmount - 1)
  1324.     {
  1325.       count = 0;
  1326.     }
  1327.     break;
  1328.   case 1: // Prev. track
  1329.     count--;
  1330.     if (count < 0)
  1331.     {
  1332.       count = curFileStruct.totalAmount - 1;
  1333.     }
  1334.     break;
  1335.   }
  1336.   return count;
  1337. }
  1338.  
  1339. void printStatus(void)
  1340. {
  1341.   AT(1, 9);
  1342.   ATRIB(93);
  1343.   printf(" [Q]Query : ");
  1344.   ATRIB(97);
  1345.   printf("%s", queryType);
  1346.   printf("  ");
  1347.   AT(1, 24);
  1348.   ATRIB(45);
  1349.   printf("                                                                                ");
  1350.   AT(2, 24);
  1351.   ATRIB(93);
  1352.   printf(" [F]Format: ");
  1353.   ATRIB(97);
  1354.   printf("%s", formats[curFormat]);
  1355.   ATRIB(93);
  1356.   printf(" [K]Keep files: ");
  1357.   ATRIB(97);
  1358.   printf("%u", saveFlag);
  1359.   ATRIB(93);
  1360.   printf(" [R]Repeat: ");
  1361.   ATRIB(97);
  1362.   printf("%u", rptFlag);
  1363.   ATRIB(93);
  1364.   printf(" [J]Jump to ");
  1365.   printf(" [E]Exit        [%s]", ver);
  1366.  
  1367.   ATRIB(97);
  1368.   ATRIB(40);
  1369. }
  1370.  
  1371. void printInfo(void)
  1372. {
  1373.   BOX(30, 2, 50, 6, 40);
  1374.   AT(1, 2);
  1375.   ATRIB(97);
  1376.   ATRIB(93);
  1377.   printf(" #: ");
  1378.   ATRIB(97);
  1379.   printf("%lu", count);
  1380.   ATRIB(93);
  1381.   printf(" ID: ");
  1382.   ATRIB(97);
  1383.   printf("%lu", curFileStruct.picId);
  1384.   ATRIB(93);
  1385.   printf(" Total Tracks: ");
  1386.   ATRIB(97);
  1387.   printf("%lu", curFileStruct.totalAmount);
  1388.   printf(" \r\n");
  1389.   ATRIB(93);
  1390.   printf(" RATING: ");
  1391.   ATRIB(97);
  1392.   printf("%s", curFileStruct.picRating);
  1393.   ATRIB(93);
  1394.   printf(" YEAR: ");
  1395.   ATRIB(97);
  1396.   printf("%u", curFileStruct.picYear);
  1397.   ATRIB(93);
  1398.   printf(" DURATION: ");
  1399.   ATRIB(97);
  1400.   printf("%s", curFileStruct.time);
  1401.   printf(" \r\n\r\n");
  1402.   ATRIB(93);
  1403.   printf(" AuthorsIDs ");
  1404.   ATRIB(97);
  1405.   printf("%s", curFileStruct.authorIds);
  1406.   ATRIB(93);
  1407.   printf(" Author: ");
  1408.   ATRIB(97);
  1409.   printf("%s", curFileStruct.authorTitle);
  1410.   ATRIB(93);
  1411.   printf(" Real name: ");
  1412.   ATRIB(97);
  1413.   printf("%s", curFileStruct.authorRealName);
  1414.   printf(" \r\n\r\n");
  1415.   ATRIB(96);
  1416.   printf("                                                                           \r");
  1417.   printf("   TITLE: %s\r\n", curFileStruct.trackName);
  1418. }
  1419.  
  1420. unsigned char testPlayer(void)
  1421. {
  1422.   union APP_PAGES player2_pg;
  1423.   player2_pg.l = OS_GETAPPMAINPAGES(player_pg.pgs.pId);
  1424.  
  1425.   if (errno == 0)
  1426.   {
  1427.     return 1;
  1428.   }
  1429.   else
  1430.   {
  1431.     return 0;
  1432.   }
  1433. }
  1434.  
  1435. C_task main(int argc, char *argv[])
  1436. {
  1437.   unsigned char errn, keypress, queryNum, pId, alive, changedFormat;
  1438.   long iddqd, idkfa, ipadress;
  1439.   unsigned long curTimer, startTimer, oldTimer;
  1440.   os_initstdio();
  1441.   srand(time());
  1442.  
  1443.   count = 0;
  1444.   saveFlag = 0;
  1445.   logFlag = 0;
  1446.   queryNum = 0;
  1447.   curFormat = 0;
  1448.   changedFormat = 0;
  1449.   rptFlag = 0;
  1450.   netDriver = 0;
  1451.  
  1452.   if (argc > 1)
  1453.   {
  1454.     if ((argv[1][0] == 'e') || (argv[1][0] == 'E'))
  1455.     {
  1456.       netDriver = 1;
  1457.       clearStatus();
  1458.       printf("    ESP32 mode enabled...");
  1459.       loadEspConfig();
  1460.       uart_init(divider);
  1461.       espReBoot();
  1462.       printHelp();
  1463.     }
  1464.   }
  1465.  
  1466.   strcpy(queryType, "from newest to oldest");
  1467.   BOX(1, 1, 80, 25, 40);
  1468.   AT(1, 1);
  1469.   ATRIB(97);
  1470.   ATRIB(45);
  1471.   printf("                           ZXART.EE radio for nedoNET                           \n\r");
  1472.   AT(1, 24);
  1473.   printf("  [L]Enable logging(press on startup)                                          ");
  1474.  
  1475.   ATRIB(33);
  1476.   ATRIB(40);
  1477.  
  1478.   keypress = _low_level_get();
  1479.   if (keypress == 'l' || keypress == 'L')
  1480.   {
  1481.     logFlag = 1;
  1482.     clearStatus();
  1483.     printf("Logging enabled");
  1484.     getchar();
  1485.   }
  1486.  
  1487. start:
  1488.   OS_SETSYSDRV();
  1489.   printHelp();
  1490.   curFileStruct.fileSize = 0;
  1491.  
  1492.   iddqd = processJson(count, 1, queryNum); // Query for track info
  1493.   if (iddqd < 0)
  1494.   {
  1495.     {
  1496.       clearStatus();
  1497.       printf("Error getting track info, next please(%ld)...", iddqd);
  1498.       count = trackSelector(0);
  1499.       goto start;
  1500.     }
  1501.   }
  1502.  
  1503.   idkfa = processJson(atol(curFileStruct.authorIds), 0, 99); // Query for AuthorID
  1504.  
  1505.   if (idkfa < 0)
  1506.   {
  1507.     clearStatus();
  1508.     printf("Error getting author %lu", atol(curFileStruct.authorIds));
  1509.     strcpy(curFileStruct.authorTitle, "-");
  1510.     strcpy(curFileStruct.authorRealName, "-");
  1511.   }
  1512. replay:
  1513.  
  1514.   errn = getTrack2(iddqd); // Downloading the track
  1515.  
  1516. resume:
  1517.   startTimer = time();
  1518.   printProgress(0);
  1519.   pId = runPlayer(); // Start thr Player!
  1520.   printStatus();
  1521.   printInfo();
  1522. rekey:
  1523.   keypress = _low_level_get();
  1524.   if (keypress != 0)
  1525.   {
  1526.     if (keypress == 27 || keypress == 'e' || keypress == 'E')
  1527.     {
  1528.       OS_DROPAPP(pId);
  1529.       BOX(1, 1, 80, 25, 40);
  1530.       AT(1, 1);
  1531.       printf("Good bye...\r\n");
  1532.       ATRIB(37);
  1533.       ATRIB(40);
  1534.       exit(0);
  1535.     }
  1536.     if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1537.     {
  1538.       changedFormat = 0;
  1539.       OS_DROPAPP(pId);
  1540.       clearStatus();
  1541.       printf("Player stopped...");
  1542.       count = trackSelector(1);
  1543.       goto start;
  1544.     }
  1545.  
  1546.     if (keypress == 251 || keypress == 32 || keypress == 'n' || keypress == 'N')
  1547.     {
  1548.       changedFormat = 0;
  1549.       OS_DROPAPP(pId);
  1550.       clearStatus();
  1551.       printf("Player stopped...");
  1552.       count = trackSelector(0);
  1553.       goto start;
  1554.     }
  1555.  
  1556.     if (keypress == 'k' || keypress == 'K')
  1557.     {
  1558.       OS_DROPAPP(pId);
  1559.       clearStatus();
  1560.       printf("Player stopped...");
  1561.       saveFlag = !saveFlag;
  1562.       printStatus();
  1563.       goto replay;
  1564.     }
  1565.  
  1566.     if (keypress == 'q' || keypress == 'Q')
  1567.     {
  1568.       OS_DROPAPP(pId);
  1569.       clearStatus();
  1570.       printf("Player stopped...");
  1571.       queryNum++;
  1572.       if (queryNum > 3)
  1573.       {
  1574.         queryNum = 0;
  1575.       }
  1576.       switch (queryNum)
  1577.       {
  1578.       case 0:
  1579.         strcpy(queryType, "from newest to oldest                   ");
  1580.         break;
  1581.       case 1:
  1582.         strcpy(queryType, "Random best and most voted tracks       ");
  1583.         break;
  1584.       case 2:
  1585.         strcpy(queryType, "Random play                             ");
  1586.         break;
  1587.       case 3:
  1588.         strcpy(queryType, "User defined query from \"user.que\"     ");
  1589.         break;
  1590.       }
  1591.       count = 0;
  1592.       printStatus();
  1593.       goto start;
  1594.     }
  1595.  
  1596.     if (keypress == 'j' || keypress == 'J')
  1597.     {
  1598.       AT(1, 7);
  1599.       printf("                                                                      \r");
  1600.       printf("Jump to track:");
  1601.       scanf("%lu", &count);
  1602.       OS_DROPAPP(pId);
  1603.       if (count > curFileStruct.totalAmount - 1)
  1604.       {
  1605.         count = curFileStruct.totalAmount - 1;
  1606.       }
  1607.       goto start;
  1608.     }
  1609.  
  1610.     if (keypress == 'f' || keypress == 'F')
  1611.     {
  1612.       OS_DROPAPP(pId);
  1613.       clearStatus();
  1614.       printf("Player stopped...");
  1615.       curFormat++;
  1616.       count = -1;
  1617.       if (curFormat > 3)
  1618.       {
  1619.         curFormat = 0;
  1620.       }
  1621.       changedFormat = 1;
  1622.       curFileStruct.totalAmount = 1;
  1623.       printStatus();
  1624.       printProgress(0);
  1625.       BOX(1, 2, 80, 6, 40);
  1626.       goto rekey;
  1627.     }
  1628.  
  1629.     if (keypress == 'l' || keypress == 'L')
  1630.     {
  1631.       logFlag = !logFlag;
  1632.       clearStatus();
  1633.       printf("Logging: %u", logFlag);
  1634.     }
  1635.  
  1636.     if (keypress == 's' || keypress == 'S')
  1637.     {
  1638.       OS_DROPAPP(pId);
  1639.       clearStatus();
  1640.       printf("Player stopped...");
  1641.       printProgress(0);
  1642.       getchar();
  1643.       goto resume;
  1644.     }
  1645.     if (keypress == 'r' || keypress == 'R')
  1646.     {
  1647.       rptFlag = !rptFlag;
  1648.       clearStatus();
  1649.       printStatus();
  1650.       goto rekey;
  1651.     }
  1652.     if (keypress == 'd' || keypress == 'D')
  1653.     {
  1654.       saveBak = saveFlag;
  1655.       saveFlag = 1;
  1656.  
  1657.       errn = getTrack2(iddqd); // Downloading the track
  1658.  
  1659.       saveFlag = saveBak;
  1660.       clearStatus();
  1661.       printf("File saved: [%s]...", curFileStruct.fileName);
  1662.       goto rekey;
  1663.     }
  1664.  
  1665.     if (keypress == 'i' || keypress == 'I')
  1666.     {
  1667.       netDriver = !netDriver;
  1668.       if (netDriver == 1)
  1669.       {
  1670.         clearStatus();
  1671.         printf("    ESP32 mode enabled...");
  1672.         loadEspConfig();
  1673.         uart_init(divider);
  1674.         espReBoot();
  1675.         printHelp();
  1676.       }
  1677.       else
  1678.       {
  1679.         clearStatus();
  1680.         printf("    ZXNETUSB mode enabled...");
  1681.       }
  1682.     }
  1683.   }
  1684.  
  1685.   curTimer = time();
  1686.   curFileStruct.curPos = (curTimer - startTimer) / 50;
  1687.   alive = testPlayer();
  1688.  
  1689.   if (alive == 0 && !changedFormat)
  1690.   {
  1691.     if (rptFlag == 1)
  1692.     {
  1693.       goto resume;
  1694.     }
  1695.     printProgress(2);
  1696.     count = trackSelector(0);
  1697.     goto start;
  1698.   }
  1699.  
  1700.   if (alive == 1 && ((curTimer - oldTimer) > 49))
  1701.   {
  1702.     printProgress(1);
  1703.     oldTimer = curTimer;
  1704.   }
  1705.   YIELD();
  1706.   goto rekey;
  1707. }
  1708.