Subversion Repositories NedoOS

Rev

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