?login_element?

Subversion Repositories NedoOS

Rev

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