Subversion Repositories NedoOS

Rev

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

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