Subversion Repositories NedoOS

Rev

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