Subversion Repositories NedoOS

Rev

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