?login_element?

Subversion Repositories NedoOS

Rev

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

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  12. #define COMMANDLINE 0x0080
  13. unsigned char ver[] = "1.9";
  14. unsigned char queryType[64];
  15. unsigned char netbuf[1452];
  16. unsigned char dataBuffer[6096];
  17. unsigned char crlf[2] = {13, 10};
  18. unsigned char formats[4][4] = {"pt3", "pt2", "tfc", "ts"};
  19. unsigned char status, key, curFormat;
  20. struct sockaddr_in targetadr;
  21. struct readstructure readStruct;
  22. unsigned long contLen;
  23. long count;
  24. unsigned char saveFlag, saveBak, logFlag, rptFlag;
  25. union APP_PAGES main_pg;
  26. union APP_PAGES player_pg;
  27. extern void dns_resolve(void);
  28.  
  29. struct fileStruct
  30. {
  31.   long picId;
  32.   unsigned long fileSize;
  33.   unsigned int picYear;
  34.   unsigned long totalAmount;
  35.   unsigned int curPos;
  36.   unsigned int startBar;
  37.   unsigned int trackInSeconds;
  38.   unsigned char time[16];
  39.   unsigned char picRating[8];
  40.   unsigned char trackName[256];
  41.   unsigned char fileName[256];
  42.   unsigned char authorIds[64];
  43.   unsigned char authorTitle[64];
  44.   unsigned char authorRealName[64];
  45.   unsigned char afn[64];
  46.   unsigned char tfn[64];
  47.   unsigned char fileName2[256];
  48. } curFileStruct;
  49.  
  50. void delay(unsigned long counter)
  51. {
  52.   unsigned long start, finish;
  53.   counter = counter / 20;
  54.   if (counter < 1)
  55.   {
  56.     counter = 1;
  57.   }
  58.   start = time();
  59.   finish = start + counter;
  60.  
  61.   while (start < finish)
  62.   {
  63.     start = time();
  64.     YIELD();
  65.   }
  66. }
  67.  
  68. void clearStatus(void)
  69. {
  70.   AT(1, 25);
  71.   printf("                                                                               \r");
  72. }
  73.  
  74. void printProgress(unsigned char type)
  75. {
  76.   unsigned char bar, minutes, seconds;
  77.   unsigned char *position;
  78.   long barLenght;
  79.   int timer;
  80.   switch (type)
  81.   {
  82.   case 0: // print empty bar
  83.     AT(6, 11);
  84.     ATRIB(93);
  85.     printf("%02u:%02u", 0, 0);
  86.     AT(15, 11);
  87.     ATRIB(97);
  88.     for (bar = 0; bar < 50; bar++)
  89.     {
  90.       putchar(176);
  91.     }
  92.     putchar(' ');
  93.     putchar(' ');
  94.     minutes = atoi(curFileStruct.time);
  95.     position = (strstr(curFileStruct.time, ":")) + 1;
  96.     seconds = atoi(position);
  97.     curFileStruct.trackInSeconds = minutes * 60 + seconds;
  98.     curFileStruct.curPos = 0;
  99.     curFileStruct.startBar = 0;
  100.     break;
  101.   case 1: // print progress bar
  102.  
  103.     AT(6, 11);
  104.     ATRIB(93);
  105.     timer = floor(curFileStruct.curPos / 60);
  106.     printf("%02u:%02u", timer, (curFileStruct.curPos - (timer * 60)));
  107.  
  108.     barLenght = (curFileStruct.curPos * 50 / curFileStruct.trackInSeconds);
  109.     if (barLenght > 49)
  110.     {
  111.       barLenght = 50;
  112.     }
  113.     AT(15 + curFileStruct.startBar, 11);
  114.     ATRIB(97);
  115.     for (bar = 0; bar < barLenght - curFileStruct.startBar; bar++)
  116.     {
  117.       putchar(178);
  118.     }
  119.     AT(1, 1);
  120.     curFileStruct.startBar = bar;
  121.     break;
  122.   case 2: // print full bar
  123.     AT(15, 11);
  124.     ATRIB(97);
  125.     for (bar = 0; bar < 50; bar++)
  126.     {
  127.       putchar(178);
  128.     }
  129.     break;
  130.   }
  131. }
  132. void errorPrint(unsigned int error)
  133. {
  134.   switch (error)
  135.   {
  136.   case 2:
  137.     printf("02 SHUT_RDWR");
  138.     break;
  139.   case 4:
  140.     printf("04 ERR_INTR");
  141.     break;
  142.   case 23:
  143.     printf("23 ERR_NFILE");
  144.     break;
  145.   case 35:
  146.     printf("35 ERR_EAGAIN");
  147.     break;
  148.   case 37:
  149.     printf("37 ERR_ALREADY");
  150.     break;
  151.   case 38:
  152.     printf("38 ERR_NOTSOCK");
  153.     break;
  154.   case 40:
  155.     printf("40 ERR_EMSGSIZE");
  156.     break;
  157.   case 41:
  158.     printf("41 ERR_PROTOTYPE");
  159.     break;
  160.   case 47:
  161.     printf("47 ERR_AFNOSUPPORT");
  162.     break;
  163.   case 53:
  164.     printf("53 ERR_ECONNABORTED");
  165.     break;
  166.   case 54:
  167.     printf("54 ERR_CONNRESET");
  168.     break;
  169.   case 57:
  170.     printf("57 ERR_NOTCONN");
  171.     break;
  172.   case 65:
  173.     printf("65 ERR_HOSTUNREACH");
  174.     break;
  175.   default:
  176.     printf("%u UNKNOWN ERROR", error);
  177.     break;
  178.   }
  179.   YIELD();
  180. }
  181.  
  182. unsigned char OpenSock(unsigned char family, unsigned char protocol)
  183. {
  184.   unsigned char socket;
  185.   unsigned int todo;
  186.   todo = OS_NETSOCKET((family << 8) + protocol);
  187.   if (todo > 32767)
  188.   {
  189.     clearStatus();
  190.     printf("OS_NETSOCKET: ");
  191.     errorPrint(todo & 255);
  192.     exit(0);
  193.   }
  194.   else
  195.   {
  196.     socket = ((todo & 65280) >> 8);
  197.     if (logFlag)
  198.     {
  199.       clearStatus();
  200.       printf("OS_NETSOCKET: Socket #%d created.", socket);
  201.     }
  202.   }
  203.   return socket;
  204. }
  205.  
  206. unsigned int netShutDown(unsigned char socket, unsigned char type)
  207. {
  208.   unsigned int todo;
  209.   todo = OS_NETSHUTDOWN(socket, type);
  210.   if (todo > 32767)
  211.   {
  212.     clearStatus();
  213.     printf("OS_NETSHUTDOWN: ");
  214.     errorPrint(todo & 255);
  215.     return 255;
  216.   }
  217.   else
  218.   {
  219.     if (logFlag)
  220.     {
  221.       clearStatus();
  222.       printf("OS_NETSHUTDOWN: Socket #%u closed.", socket);
  223.     }
  224.   }
  225.   return 0;
  226. }
  227.  
  228. unsigned char netConnect(unsigned char socket)
  229. {
  230.   unsigned int todo, retry = 10;
  231.  
  232.   targetadr.family = AF_INET;
  233.   targetadr.porth = 00;
  234.   targetadr.portl = 80;
  235.   targetadr.b1 = 217;
  236.   targetadr.b2 = 146;
  237.   targetadr.b3 = 69;
  238.   targetadr.b4 = 13;
  239.   while (retry > 0)
  240.   {
  241.     todo = OS_NETCONNECT(socket, &targetadr);
  242.  
  243.     if (todo > 32767)
  244.     {
  245.       retry--;
  246.       clearStatus();
  247.       printf("OS_NETCONNECT [ERROR:");
  248.       errorPrint(todo & 255);
  249.       printf("] [Retry:%u]", retry);
  250.       YIELD();
  251.       netShutDown(socket, 0);
  252.       socket = OpenSock(AF_INET, SOCK_STREAM);
  253.     }
  254.     else
  255.     {
  256.       if (logFlag)
  257.       {
  258.         clearStatus();
  259.         printf("OS_NETCONNECT: connected , %u", (todo & 255));
  260.       }
  261.       return 1;
  262.     }
  263.   }
  264.   getchar();
  265.   exit(0);
  266.   return 0;
  267. }
  268.  
  269. unsigned int tcpRead(unsigned char socket)
  270. {
  271.   unsigned char retry = 20;
  272.   unsigned int err, todo;
  273.   readStruct.socket = socket;
  274.   readStruct.BufAdr = (unsigned int)&netbuf;
  275.   readStruct.bufsize = sizeof(netbuf);
  276.   readStruct.protocol = SOCK_STREAM;
  277.  
  278. wizread:
  279.   todo = OS_WIZNETREAD(&readStruct);
  280.   if (todo > 32767)
  281.   {
  282.     if (retry == 0)
  283.     {
  284.       err = todo & 255;
  285.       clearStatus();
  286.       printf("OS_WIZNETREAD: ");
  287.       errorPrint(err);
  288.       if (err == ERR_EAGAIN)
  289.       {
  290.         return 0;
  291.       }
  292.       exit(0);
  293.     }
  294.     retry--;
  295.     if (logFlag)
  296.     {
  297.       AT(54, 25);
  298.       printf("OS_WIZNETREAD: retry %u   ", retry);
  299.     }
  300.     delay(200);
  301.     goto wizread;
  302.   }
  303.   if (logFlag)
  304.   {
  305.     clearStatus();
  306.     printf("OS_WIZNETREAD: %u bytes read.", todo);
  307.   }
  308.   return todo;
  309. }
  310.  
  311. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  312. {
  313.   unsigned int i, j;
  314.   unsigned int lenC, lenS;
  315.  
  316.   for (lenC = 0; c[lenC]; lenC++)
  317.     ;
  318.   for (lenS = 0; s[lenS]; lenS++)
  319.     ;
  320.  
  321.   for (i = startPos; i <= lenS - lenC; i++)
  322.   {
  323.     for (j = 0; s[i + j] == c[j]; j++)
  324.       ;
  325.  
  326.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  327.       return i;
  328.     if (j == lenC)
  329.       if (n - 1)
  330.         n--;
  331.       else
  332.         return i;
  333.   }
  334.   return -1;
  335. }
  336.  
  337. char *str_replace(char *dst, int num, const char *str,
  338.                   const char *orig, const char *rep)
  339. {
  340.   const char *ptr;
  341.   size_t len1 = strlen(orig);
  342.   size_t len2 = strlen(rep);
  343.   char *tmp = dst;
  344.  
  345.   num -= 1;
  346.   while ((ptr = strstr(str, orig)) != NULL)
  347.   {
  348.     num -= (ptr - str) + len2;
  349.     if (num < 1)
  350.       break;
  351.  
  352.     strncpy(dst, str, (size_t)(ptr - str));
  353.     dst += ptr - str;
  354.     strncpy(dst, rep, len2);
  355.     dst += len2;
  356.     str = ptr + len1;
  357.   }
  358.  
  359.   for (; (*dst = *str) && (num > 0); --num)
  360.   {
  361.     ++dst;
  362.     ++str;
  363.   }
  364.   return tmp;
  365. }
  366.  
  367. const char *parseJson(unsigned char *property)
  368. {
  369.   unsigned int w, lng, lngp1, findEnd, listPos;
  370.   unsigned char terminator;
  371.   int n;
  372.   n = -1;
  373.   netbuf[0] = '\0';
  374.   n = pos(dataBuffer, property, 1, 0);
  375.   if (n == -1)
  376.   {
  377.     strcpy(netbuf, "0\0");
  378.     return netbuf;
  379.   }
  380.   lng = n - 1 + strlen(property);
  381.   if (dataBuffer[lng] == ':')
  382.   {
  383.     terminator = '\0';
  384.   }
  385.   if (dataBuffer[lng] == '\"')
  386.   {
  387.     terminator = '\"';
  388.   }
  389.   if (dataBuffer[lng] == '[')
  390.   {
  391.     terminator = ']';
  392.   }
  393.  
  394.   findEnd = 1;
  395.   lngp1 = lng + 1;
  396.  
  397.   while (42)
  398.   {
  399.  
  400.     if ((dataBuffer[lngp1 + findEnd] == ','))
  401.     {
  402.       if (terminator == '\0')
  403.       {
  404.         break;
  405.       }
  406.       if ((dataBuffer[lng + findEnd] == terminator))
  407.       {
  408.         findEnd--;
  409.         break;
  410.       }
  411.     }
  412.     findEnd++;
  413.   }
  414.   listPos = 0;
  415.   for (w = lngp1; w < findEnd + lngp1; w++)
  416.   {
  417.     netbuf[listPos] = dataBuffer[w];
  418.     listPos++;
  419.   }
  420.   netbuf[listPos] = '\0';
  421.   return netbuf;
  422. }
  423. void convert866(void)
  424. {
  425.   unsigned int lng, targetPos, w, q = 0;
  426.   unsigned char buffer[8], one, two;
  427.   unsigned int decVal;
  428.   lng = strlen(netbuf);
  429.   targetPos = lng + 1;
  430.  
  431.   while (q < lng)
  432.   {
  433.     one = netbuf[q];
  434.     two = netbuf[q + 1];
  435.     if (one == 92 && two == 117)
  436.     {
  437.       q = q + 2;
  438.       for (w = 0; w < 4; w++)
  439.       {
  440.         buffer[w] = netbuf[q + w];
  441.       }
  442.       q = q + 4;
  443.       buffer[4] = '\0';
  444.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  445.  
  446.       if (decVal < 1088)
  447.       {
  448.         decVal = decVal - 912;
  449.       }
  450.       if (decVal > 1087)
  451.       {
  452.         decVal = decVal - 864;
  453.       }
  454.       if (decVal == 1025)
  455.       {
  456.         decVal = 240;
  457.       }
  458.       if (decVal == 1105)
  459.       {
  460.         decVal = 241;
  461.       }
  462.       netbuf[targetPos] = decVal;
  463.     }
  464.     else
  465.     {
  466.       netbuf[targetPos] = netbuf[q];
  467.       q++;
  468.     }
  469.     targetPos++;
  470.   }
  471.   netbuf[targetPos] = '\0';
  472.  
  473.   for (w = lng + 1; w < targetPos + 1; w++)
  474.   {
  475.     netbuf[w - lng - 1] = netbuf[w];
  476.   }
  477. }
  478.  
  479. unsigned int cutHeader(unsigned int todo)
  480. {
  481.   unsigned int q, headlng;
  482.   unsigned char *count;
  483.   count = strstr(netbuf, "Content-Length:");
  484.   if (count == NULL)
  485.   {
  486.     clearStatus();
  487.     printf("Content-Length:  not found.");
  488.     contLen = 0;
  489.   }
  490.   else
  491.   {
  492.     contLen = atol(count + 15);
  493.     curFileStruct.fileSize = contLen;
  494.     //  printf("=> Dlinna  soderzhimogo = %lu \n\r", curFileStruct.fileSize);
  495.   }
  496.  
  497.   count = strstr(netbuf, "\r\n\r\n");
  498.   headlng = ((unsigned int)count - (unsigned int)netbuf + 4);
  499.   q = todo - headlng;
  500.   memcpy(&netbuf, count + 4, q);
  501.   return q;
  502. }
  503.  
  504. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  505. {
  506.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  507.   str_replace(pfn, tfnSize, pfn, "/", "_");
  508.   str_replace(pfn, tfnSize, pfn, ":", "_");
  509.   str_replace(pfn, tfnSize, pfn, "*", "_");
  510.   str_replace(pfn, tfnSize, pfn, "?", "_");
  511.   str_replace(pfn, tfnSize, pfn, "<", "_");
  512.   str_replace(pfn, tfnSize, pfn, ">", "_");
  513.   str_replace(pfn, tfnSize, pfn, "|", "_");
  514.   str_replace(pfn, tfnSize, pfn, " ", "_");
  515.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  516.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  517.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  518.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  519.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  520.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  521. }
  522.  
  523. void stringRepair(unsigned char *pfn, unsigned int tSize)
  524. {
  525.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  526.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  527.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  528.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  529.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  530.   str_replace(pfn, tSize, pfn, "\\/", "/");
  531. }
  532.  
  533. unsigned char saveBuf(unsigned long fileId, unsigned char operation, unsigned int sizeOfBuf)
  534. {
  535.   FILE *fp2;
  536.   unsigned long fileSize;
  537.   unsigned char afnSize, tfnSize;
  538.   unsigned char fileIdChar[10];
  539.  
  540.   if (operation == 00)
  541.   {
  542.  
  543.     if (saveFlag == 0)
  544.     {
  545.       sprintf(curFileStruct.fileName, "temp.%s", formats[curFormat]);
  546.     }
  547.     else
  548.     {
  549.       afnSize = sizeof(curFileStruct.afn) - 1;
  550.       tfnSize = sizeof(curFileStruct.tfn) - 1;
  551.  
  552.       strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  553.       nameRepair(curFileStruct.afn, afnSize);
  554.       strcpy(curFileStruct.tfn, curFileStruct.trackName);
  555.       nameRepair(curFileStruct.tfn, tfnSize);
  556.       sprintf(curFileStruct.fileName, "%s-%s.%s", curFileStruct.afn, curFileStruct.tfn, formats[curFormat]);
  557.  
  558.       if (strlen(curFileStruct.fileName) > 63)
  559.       {
  560.         sprintf(fileIdChar, "-%ld", fileId);
  561.         str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  562.         curFileStruct.fileName[50] = '\0';
  563.         strcat(curFileStruct.fileName, fileIdChar);
  564.         strcat(curFileStruct.fileName, formats[curFormat]);
  565.       }
  566.     }
  567.     OS_SETSYSDRV();
  568.     OS_MKDIR("../downloads/radio"); // Create if not exist
  569.     OS_CHDIR("../downloads/radio");
  570.     fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  571.     if (((int)fp2) & 0xff)
  572.     {
  573.       clearStatus();
  574.       printf(curFileStruct.fileName);
  575.       printf(" creating error. Check for  downloads\\radio folder.");
  576.       getchar();
  577.       exit(0);
  578.     }
  579.     OS_CLOSEHANDLE(fp2);
  580.     return 0;
  581.   }
  582.  
  583.   if (operation == 01)
  584.   {
  585.     fp2 = OS_OPENHANDLE(curFileStruct.fileName, 0x80);
  586.     if (((int)fp2) & 0xff)
  587.     {
  588.  
  589.       clearStatus();
  590.       printf(curFileStruct.fileName);
  591.       printf(" opening error.");
  592.       exit(0);
  593.     }
  594.     fileSize = OS_GETFILESIZE(fp2);
  595.     OS_SEEKHANDLE(fp2, fileSize);
  596.     OS_WRITEHANDLE(netbuf, fp2, sizeOfBuf);
  597.     OS_CLOSEHANDLE(fp2);
  598.     return 0;
  599.   }
  600.  
  601.   if (operation == 02)
  602.   {
  603.     OS_CLOSEHANDLE(fp2);
  604.     return 0;
  605.   }
  606.  
  607.   return 0;
  608. }
  609.  
  610. void getData(unsigned char socket)
  611. {
  612.   unsigned int todo, w, bPos, headskip;
  613.  
  614.   headskip = 0;
  615.   bPos = 0;
  616.   while (1)
  617.   {
  618.     todo = tcpRead(socket);
  619.     if (todo == 0)
  620.     {
  621.       break;
  622.     }
  623.     if (headskip == 0)
  624.     {
  625.       headskip = 1;
  626.       todo = cutHeader(todo);
  627.     }
  628.  
  629.     if (bPos + todo > sizeof(dataBuffer))
  630.     {
  631.       clearStatus();
  632.       printf("dataBuffer overrun...");
  633.       break;
  634.     }
  635.  
  636.     for (w = 0; w < todo; w++)
  637.     {
  638.       dataBuffer[w + bPos] = netbuf[w];
  639.     }
  640.     bPos = bPos + todo;
  641.     if (bPos == contLen)
  642.     {
  643.       // dataBuffer[todo + bPos + 1] = '\0';
  644.       break;
  645.     }
  646.   }
  647.   netShutDown(socket, 1);
  648. }
  649.  
  650. unsigned int tcpSend(unsigned char socket, unsigned int messageadr, unsigned int size)
  651. {
  652.   unsigned char retry = 20;
  653.   unsigned int todo;
  654.   readStruct.socket = socket;
  655.   readStruct.BufAdr = messageadr;
  656.   readStruct.bufsize = size;
  657.   readStruct.protocol = SOCK_STREAM;
  658.  
  659. wizwrite:
  660.   todo = OS_WIZNETWRITE(&readStruct);
  661.   if (todo > 32767)
  662.   {
  663.     clearStatus();
  664.     printf("OS_WIZNETWRITE: ");
  665.     errorPrint(todo & 255);
  666.     if (retry == 0)
  667.     {
  668.       exit(0);
  669.     }
  670.     retry--;
  671.     delay(150);
  672.     goto wizwrite;
  673.   }
  674.   else
  675.   {
  676.     if (logFlag)
  677.     {
  678.       clearStatus();
  679.       printf("OS_WIZNETWRITE: %u bytes written.", todo);
  680.     }
  681.   }
  682.   return todo;
  683. }
  684.  
  685. unsigned long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  686. {
  687.   FILE *fp3;
  688.   unsigned int retry, tSize;
  689.   unsigned int todo;
  690.   unsigned char buffer[] = "000000000";
  691.   unsigned char *count, socket;
  692.   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";
  693.   unsigned char userQuery[256] = "/api/export:zxMusic/limit:10/filter:zxMusicId=44816";
  694.   clearStatus();
  695.   printf("Getting data(%u)...", queryNum);
  696.  
  697.   switch (queryNum)
  698.   {
  699.   case 0: // GET /api/export:zxMusic/limit:1/start:1/filter:zxMusicFormat=pt3/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  700.     strcpy(netbuf, "GET /api/export:zxMusic/limit:");
  701.     sprintf(buffer, "%u", limit);
  702.     strcat(netbuf, buffer);
  703.     strcat(netbuf, "/start:");
  704.     sprintf(buffer, "%lu", startPos);
  705.     strcat(netbuf, buffer);
  706.     strcat(netbuf, "/filter:zxMusicFormat=");
  707.     strcat(netbuf, formats[curFormat]);
  708.     strcat(netbuf, "/order:date,desc");
  709.     strcat(netbuf, userAgent);
  710.     break;
  711.   case 1: // GET /api/types:zxMusic/export:zxMusic/language:eng/limit:1/start:0/order:votes,rand/filter:zxMusicMinRating=4;
  712.     startPos = 0;
  713.     strcpy(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:");
  714.     sprintf(buffer, "%u", limit);
  715.     strcat(netbuf, buffer);
  716.     strcat(netbuf, "/start:");
  717.     sprintf(buffer, "%lu", startPos);
  718.     strcat(netbuf, buffer);
  719.     strcat(netbuf, "/order:votes,rand/filter:zxMusicMinRating=4;zxMusicFormat=");
  720.     strcat(netbuf, formats[curFormat]);
  721.     strcat(netbuf, userAgent);
  722.     break;
  723.   case 2: // GET /api/types:zxMusic/export:zxMusic/language:eng/limit:1/start:0/order:rand/filter:zxMusicFormat=PT3 HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  724.     startPos = 0;
  725.     strcpy(netbuf, "GET /api/types:zxMusic/export:zxMusic/language:eng/limit:");
  726.     sprintf(buffer, "%u", limit);
  727.     strcat(netbuf, buffer);
  728.     strcat(netbuf, "/start:");
  729.     sprintf(buffer, "%lu", startPos);
  730.     strcat(netbuf, buffer);
  731.     strcat(netbuf, "/order:rand/filter:zxMusicFormat=");
  732.     strcat(netbuf, formats[curFormat]);
  733.     strcat(netbuf, userAgent);
  734.     break;
  735.  
  736.   case 3: // GET /api/export:zxMusic/limit:1/start:1/filter:zxMusicFormat=pt3;authorId=7744/order:date,desc HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)
  737.  
  738.     fp3 = OS_OPENHANDLE("radio/user.que", 0x80);
  739.     if (((int)fp3) & 0xff)
  740.     {
  741.       fp3 = OS_CREATEHANDLE("radio/user.que", 0x80);
  742.       OS_WRITEHANDLE(userQuery, fp3, sizeof(userQuery));
  743.       OS_CLOSEHANDLE(fp3);
  744.       fp3 = OS_OPENHANDLE("radio/user.que", 0x80);
  745.     }
  746.     OS_READHANDLE(userQuery, fp3, sizeof(userQuery));
  747.     OS_CLOSEHANDLE(fp3);
  748.  
  749.     strcpy(netbuf, "GET /api/limit:");
  750.     sprintf(buffer, "%u", limit);
  751.     strcat(netbuf, buffer);
  752.     strcat(netbuf, "/start:");
  753.     sprintf(buffer, "%lu", startPos);
  754.     strcat(netbuf, buffer);
  755.     strcat(netbuf, userQuery);
  756.     strcat(netbuf, userAgent);
  757.     break;
  758.  
  759.   case 99: // GET /jsonElementData/elementId:182798
  760.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  761.     sprintf(buffer, "%lu", startPos);
  762.     strcat(netbuf, buffer);
  763.     strcat(netbuf, userAgent);
  764.     break;
  765.   }
  766.  
  767.   retry = 10;
  768. rejson:
  769.   socket = OpenSock(AF_INET, SOCK_STREAM);
  770.   netConnect(socket);
  771.  
  772.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  773.  
  774.   getData(socket);
  775.  
  776.   clearStatus();
  777.   printf("Processing data (%u)...", queryNum);
  778.  
  779.   count = strstr(dataBuffer, "responseStatus\":\"success");
  780.   if (count == NULL)
  781.   {
  782.     clearStatus();
  783.     printf("BAD JSON, NO responseStatus: success. (%u)", retry);
  784.     retry--;
  785.     YIELD();
  786.     if (retry > 0)
  787.     {
  788.       netShutDown(socket, 1);
  789.       goto rejson;
  790.     }
  791.     return -1;
  792.   }
  793.  
  794.   count = strstr(dataBuffer, "\"id\":");
  795.   if (count == NULL)
  796.   {
  797.     clearStatus();
  798.     printf("BAD JSON: not ID query = %u startPos = %lu", queryNum, startPos);
  799.     return -2;
  800.   }
  801.   if (queryNum < 4)
  802.   {
  803.     netbuf[0] = '\0';
  804.     parseJson("\"id\":");
  805.     curFileStruct.picId = atol(netbuf);
  806.     parseJson(",\"title\":\"");
  807.     convert866();
  808.     strcpy(curFileStruct.trackName, netbuf);
  809.  
  810.     tSize = sizeof(curFileStruct.trackName);
  811.     stringRepair(curFileStruct.trackName, tSize);
  812.  
  813.     parseJson("\"rating\":\"");
  814.     strcpy(curFileStruct.picRating, netbuf);
  815.     parseJson("\"year\":\"");
  816.     curFileStruct.picYear = atoi(netbuf);
  817.     parseJson("\"totalAmount\":");
  818.     curFileStruct.totalAmount = atol(netbuf);
  819.     parseJson("\"time\":\"");
  820.     strcpy(curFileStruct.time, netbuf);
  821.     parseJson("\"authorIds\":[");
  822.     strcpy(curFileStruct.authorIds, netbuf);
  823.     parseJson("\"authorIds\":[");
  824.     strcpy(curFileStruct.fileName2, netbuf);
  825.   }
  826.   if (queryNum == 99)
  827.   {
  828.     parseJson(",\"title\":\"");
  829.     convert866();
  830.     strcpy(curFileStruct.authorTitle, netbuf);
  831.     parseJson(",\"realName\":\"");
  832.     convert866();
  833.     strcpy(curFileStruct.authorRealName, netbuf);
  834.   }
  835.   return curFileStruct.picId;
  836. }
  837.  
  838. unsigned char getTrack(unsigned long fileId)
  839. {
  840.   unsigned int todo;
  841.   unsigned char cmdlist1[] = "GET /file/id:";
  842.   unsigned char cmdlist2[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; Radio)\r\n\r\n\0";
  843.   unsigned char buffer[] = "0000000000";
  844.   unsigned char socket;
  845.   unsigned int headskip;
  846.   unsigned long bytecount;
  847.   clearStatus();
  848.   printf("Getting track...");
  849.  
  850.   socket = OpenSock(AF_INET, SOCK_STREAM);
  851.   todo = netConnect(socket);
  852.   sprintf(buffer, "%lu", fileId);
  853.   strcpy(netbuf, cmdlist1);
  854.   strcat(netbuf, buffer);
  855.   strcat(netbuf, cmdlist2);
  856.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  857.  
  858.   headskip = 0;
  859.   bytecount = 255;
  860.   saveBuf(curFileStruct.picId, 00, 0);
  861.   while (bytecount != 0)
  862.   {
  863.     todo = tcpRead(socket);
  864.     if (todo == 0)
  865.     {
  866.       break;
  867.     }
  868.     if (headskip == 0)
  869.     {
  870.       headskip = 1;
  871.       todo = cutHeader(todo);
  872.       bytecount = contLen;
  873.     }
  874.     saveBuf(curFileStruct.picId, 01, todo);
  875.     bytecount = bytecount - todo;
  876.   }
  877.   netShutDown(socket, 0);
  878.   return 0;
  879. }
  880.  
  881. unsigned char runPlayer(void)
  882. {
  883.   FILE *fp2;
  884.   unsigned char fileName[] = "radio/player.ovl";
  885.   unsigned char appCmd[128] = "player.com ";
  886.   unsigned char curPath[128];
  887.   unsigned long playerSize, loaded, loop;
  888.   unsigned char pgbak;
  889.  
  890.   clearStatus();
  891.   printf("Running player...");
  892.  
  893.   strcat(appCmd, curFileStruct.fileName);
  894.   player_pg.l = OS_GETMAINPAGES();
  895.   pgbak = main_pg.pgs.window_3;
  896.   loaded = 0;
  897.   OS_GETPATH((unsigned int)&curPath);
  898.   OS_SETSYSDRV();
  899.   fp2 = OS_OPENHANDLE(fileName, 0x80);
  900.   if (((int)fp2) & 0xff)
  901.   {
  902.     clearStatus();
  903.     printf("%s", fileName);
  904.     printf(" not found.");
  905.     exit(0);
  906.   }
  907.   playerSize = OS_GETFILESIZE(fp2);
  908.   OS_CHDIR(curPath);
  909.   OS_NEWAPP((unsigned int)&player_pg);
  910.   SETPG32KHIGH(player_pg.pgs.window_3);
  911.   memcpy((char *)(0xC080), &appCmd, sizeof(appCmd));
  912.   for (loop = 0; loop < playerSize; loop = loop + loaded)
  913.   {
  914.     loaded = OS_READHANDLE(dataBuffer, fp2, sizeof(dataBuffer));
  915.     memcpy((char *)(0xC100 + loop), &dataBuffer, loaded);
  916.   }
  917.   OS_CLOSEHANDLE(fp2);
  918.   SETPG32KHIGH(pgbak);
  919.   OS_RUNAPP(player_pg.pgs.pId);
  920.   return player_pg.pgs.pId;
  921. }
  922.  
  923. long trackSelector(unsigned char mode)
  924. {
  925.   switch (mode)
  926.   {
  927.   case 0: // Next track
  928.     count++;
  929.     if (count > curFileStruct.totalAmount - 1)
  930.     {
  931.       count = 0;
  932.     }
  933.     break;
  934.   case 1: // Prev. track
  935.     count--;
  936.     if (count < 0)
  937.     {
  938.       count = curFileStruct.totalAmount - 1;
  939.     }
  940.     break;
  941.   }
  942.   return count;
  943. }
  944.  
  945. void printStatus(void)
  946. {
  947.   AT(1, 9);
  948.   ATRIB(93);
  949.   printf(" [Q]Query : ");
  950.   ATRIB(97);
  951.   printf("%s", queryType);
  952.   printf("  ");
  953.   AT(1, 24);
  954.   ATRIB(45);
  955.   printf("                                                                                ");
  956.   AT(2, 24);
  957.   ATRIB(93);
  958.   printf(" [F]Format: ");
  959.   ATRIB(97);
  960.   printf("%s", formats[curFormat]);
  961.   ATRIB(93);
  962.   printf(" [K]Keep files: ");
  963.   ATRIB(97);
  964.   printf("%u", saveFlag);
  965.   ATRIB(93);
  966.   printf(" [R]Repeat: ");
  967.   ATRIB(97);
  968.   printf("%u", rptFlag);
  969.   ATRIB(93);
  970.   printf(" [J]Jump to ");
  971.   printf(" [E]Exit        [%s]\r\n", ver);
  972.  
  973.   ATRIB(97);
  974.   ATRIB(40);
  975. }
  976.  
  977. void printInfo(void)
  978. {
  979.   BOX(30, 2, 50, 6, 40);
  980.   AT(1, 2);
  981.   ATRIB(97);
  982.   ATRIB(93);
  983.   printf(" #: ");
  984.   ATRIB(97);
  985.   printf("%lu", count);
  986.   ATRIB(93);
  987.   printf(" ID: ");
  988.   ATRIB(97);
  989.   printf("%lu", curFileStruct.picId);
  990.   ATRIB(93);
  991.   printf(" Total Tracks: ");
  992.   ATRIB(97);
  993.   printf("%lu", curFileStruct.totalAmount);
  994.   printf(" \r\n");
  995.   ATRIB(93);
  996.   printf(" RATING: ");
  997.   ATRIB(97);
  998.   printf("%s", curFileStruct.picRating);
  999.   ATRIB(93);
  1000.   printf(" YEAR: ");
  1001.   ATRIB(97);
  1002.   printf("%u", curFileStruct.picYear);
  1003.   ATRIB(93);
  1004.   printf(" DURATION: ");
  1005.   ATRIB(97);
  1006.   printf("%s", curFileStruct.time);
  1007.   printf(" \r\n\r\n");
  1008.   ATRIB(93);
  1009.   printf(" AuthorsIDs ");
  1010.   ATRIB(97);
  1011.   printf("%s", curFileStruct.authorIds);
  1012.   ATRIB(93);
  1013.   printf(" Author: ");
  1014.   ATRIB(97);
  1015.   printf("%s", curFileStruct.authorTitle);
  1016.   ATRIB(93);
  1017.   printf(" Real name: ");
  1018.   ATRIB(97);
  1019.   printf("%s", curFileStruct.authorRealName);
  1020.   printf(" \r\n\r\n");
  1021.   ATRIB(96);
  1022.   printf("                                                                           \r");
  1023.   printf("   TITLE: %s\r\n", curFileStruct.trackName);
  1024. }
  1025.  
  1026. void printHelp(void)
  1027. {
  1028.   AT(1, 13);
  1029.   ATRIB(97);
  1030.   printf(" [E] or [ESC] Exit to OS           \r\n");
  1031.   printf(" [B] or [<--] Previous track       \r\n");
  1032.   printf(" [ ] or [-->] Next track    \r\n");
  1033.   printf(" [S]          Stop player          \r\n");
  1034.   printf(" [K]          Toggle saving tracks \r\n");
  1035.   printf(" [Q]          Select Query type    \r\n");
  1036.   printf(" [D]          Download track       \r\n");
  1037.   printf(" [R]          Repeat track mode    \r\n");
  1038.   printf(" [J]          Jump to NNNN file from newest  \r\n");
  1039.   printf(" [F]          Change tracks format to play   \r\n");
  1040.   printf(" [L]          Toggle operation logging       \r\n");
  1041. }
  1042.  
  1043. unsigned char testPlayer(void)
  1044. {
  1045.   union APP_PAGES player2_pg;
  1046.   player2_pg.l = OS_GETAPPMAINPAGES(player_pg.pgs.pId);
  1047.  
  1048.   if (errno == 0)
  1049.   {
  1050.     return 1;
  1051.   }
  1052.   else
  1053.   {
  1054.     return 0;
  1055.   }
  1056. }
  1057.  
  1058. C_task main(void)
  1059. {
  1060.   unsigned char errn, keypress, queryNum, pId, alive, changedFormat;
  1061.   long iddqd, idkfa, ipadress;
  1062.   unsigned long curTimer, startTimer, oldTimer;
  1063.   os_initstdio();
  1064.   srand(time());
  1065.   count = 0;
  1066.   saveFlag = 0;
  1067.   logFlag = 0;
  1068.   queryNum = 0;
  1069.   curFormat = 0;
  1070.   changedFormat = 0;
  1071.   rptFlag = 0;
  1072.   strcpy(queryType, "from newest to oldest");
  1073.  
  1074.   BOX(1, 1, 80, 25, 40);
  1075.   AT(1, 1);
  1076.   ATRIB(97);
  1077.   ATRIB(45);
  1078.   printf("                           ZXART.EE radio for nedoNET                           \n\r");
  1079.   AT(1, 24);
  1080.   printf("  [L]Enable logging(press on startup)                                          ");
  1081.  
  1082.   ATRIB(33);
  1083.   ATRIB(40);
  1084.  
  1085.   /*
  1086.     ipadress = OS_DNSRESOLVE("zxart.ee");
  1087.     printf("\n\r  OS_DNSRESOLVE =  %lu \n\r", ipadress);
  1088.     printf("------------------------\n\r");
  1089.     printf("OS_GETPATH = %s\r\n", curPath);
  1090.   */
  1091.  
  1092.   keypress = _low_level_get();
  1093.   if (keypress == 'l' || keypress == 'L')
  1094.   {
  1095.     logFlag = 1;
  1096.     clearStatus();
  1097.     printf("Logging enabled");
  1098.     getchar();
  1099.   }
  1100.  
  1101. start:
  1102.   OS_SETSYSDRV();
  1103.   printHelp();
  1104.   curFileStruct.fileSize = 0;
  1105.  
  1106.   iddqd = processJson(count, 1, queryNum); // Query for track info
  1107.   if (iddqd < 0)
  1108.   {
  1109.     {
  1110.       clearStatus();
  1111.       printf("Error getting track info, next please(%ld)...", iddqd);
  1112.       count = trackSelector(0);
  1113.       goto start;
  1114.     }
  1115.   }
  1116.  
  1117.   idkfa = processJson(atol(curFileStruct.authorIds), 0, 99); // Query for AuthorID
  1118.  
  1119.   if (idkfa < 0)
  1120.   {
  1121.     clearStatus();
  1122.     printf("Error getting author %lu", atol(curFileStruct.authorIds));
  1123.     strcpy(curFileStruct.authorTitle, " Error getting Tittle ");
  1124.     // strcpy(curFileStruct.authorRealName, " \0");
  1125.   }
  1126. replay:
  1127.   errn = getTrack(iddqd); // Downloading the track
  1128. resume:
  1129.   startTimer = time();
  1130.   printProgress(0);
  1131.   pId = runPlayer(); // Start thr Player!
  1132.   printStatus();
  1133.   printInfo();
  1134. rekey:
  1135.   YIELD();
  1136.   keypress = _low_level_get();
  1137.   if (keypress != 0)
  1138.   {
  1139.     if (keypress == 27 || keypress == 'e' || keypress == 'E')
  1140.     {
  1141.       OS_DROPAPP(pId);
  1142.       BOX(1, 1, 80, 25, 40);
  1143.       AT(1, 1);
  1144.       printf("Good bye...\r\n");
  1145.       ATRIB(37);
  1146.       ATRIB(40);
  1147.       exit(0);
  1148.     }
  1149.     if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1150.     {
  1151.       changedFormat = 0;
  1152.       OS_DROPAPP(pId);
  1153.       clearStatus();
  1154.       printf("Player stopped...");
  1155.       count = trackSelector(1);
  1156.       goto start;
  1157.     }
  1158.  
  1159.     if (keypress == 251 || keypress == 32 || keypress == 'n' || keypress == 'N')
  1160.     {
  1161.       changedFormat = 0;
  1162.       OS_DROPAPP(pId);
  1163.       clearStatus();
  1164.       printf("Player stopped...");
  1165.       count = trackSelector(0);
  1166.       goto start;
  1167.     }
  1168.  
  1169.     if (keypress == 'k' || keypress == 'K')
  1170.     {
  1171.       OS_DROPAPP(pId);
  1172.       clearStatus();
  1173.       printf("Player stopped...");
  1174.       saveFlag = !saveFlag;
  1175.       printStatus();
  1176.       goto replay;
  1177.     }
  1178.  
  1179.     if (keypress == 'q' || keypress == 'Q')
  1180.     {
  1181.       OS_DROPAPP(pId);
  1182.       clearStatus();
  1183.       printf("Player stopped...");
  1184.       queryNum++;
  1185.       if (queryNum > 3)
  1186.       {
  1187.         queryNum = 0;
  1188.       }
  1189.       switch (queryNum)
  1190.       {
  1191.       case 0:
  1192.         strcpy(queryType, "from newest to oldest                   ");
  1193.         break;
  1194.       case 1:
  1195.         strcpy(queryType, "Random best and most voted tracks       ");
  1196.         break;
  1197.       case 2:
  1198.         strcpy(queryType, "Random play                             ");
  1199.         break;
  1200.       case 3:
  1201.         strcpy(queryType, "User defined query from \"user.que\"     ");
  1202.         break;
  1203.       }
  1204.       count = 0;
  1205.       printStatus();
  1206.       goto start;
  1207.     }
  1208.  
  1209.     if (keypress == 'j' || keypress == 'J')
  1210.     {
  1211.       AT(1, 7);
  1212.       printf("                                                                      \r");
  1213.       printf("Jump to track:");
  1214.       scanf("%lu", &count);
  1215.       OS_DROPAPP(pId);
  1216.       if (count > curFileStruct.totalAmount - 1)
  1217.       {
  1218.         count = curFileStruct.totalAmount - 1;
  1219.       }
  1220.       goto start;
  1221.     }
  1222.  
  1223.     if (keypress == 'f' || keypress == 'F')
  1224.     {
  1225.       OS_DROPAPP(pId);
  1226.       clearStatus();
  1227.       printf("Player stopped...");
  1228.       curFormat++;
  1229.       count = -1;
  1230.       if (curFormat > 3)
  1231.       {
  1232.         curFormat = 0;
  1233.       }
  1234.       changedFormat = 1;
  1235.       curFileStruct.totalAmount = 1;
  1236.       printStatus();
  1237.       printProgress(0);
  1238.       BOX(1, 2, 80, 6, 40);
  1239.       goto rekey;
  1240.     }
  1241.  
  1242.     if (keypress == 'l' || keypress == 'L')
  1243.     {
  1244.       logFlag = !logFlag;
  1245.       clearStatus();
  1246.       printf("Logging: %u", logFlag);
  1247.     }
  1248.  
  1249.     if (keypress == 's' || keypress == 'S')
  1250.     {
  1251.       OS_DROPAPP(pId);
  1252.       clearStatus();
  1253.       printf("Player stopped...");
  1254.       printProgress(0);
  1255.       getchar();
  1256.       goto resume;
  1257.     }
  1258.     if (keypress == 'r' || keypress == 'R')
  1259.     {
  1260.       rptFlag = !rptFlag;
  1261.       clearStatus();
  1262.       printStatus();
  1263.       goto rekey;
  1264.     }
  1265.     if (keypress == 'd' || keypress == 'D')
  1266.     {
  1267.       saveBak = saveFlag;
  1268.       saveFlag = 1;
  1269.       errn = getTrack(iddqd); // Downloading the track
  1270.       saveFlag = saveBak;
  1271.       clearStatus();
  1272.       printf("File saved: [%s]...", curFileStruct.fileName);
  1273.       goto rekey;
  1274.     }
  1275.   }
  1276.  
  1277.   curTimer = time();
  1278.   curFileStruct.curPos = (curTimer - startTimer) / 50;
  1279.   alive = testPlayer();
  1280.  
  1281.   if (alive == 0 && !changedFormat)
  1282.   {
  1283.     if (rptFlag == 1)
  1284.     {
  1285.       goto resume;
  1286.     }
  1287.     printProgress(2);
  1288.     count = trackSelector(0);
  1289.     goto start;
  1290.   }
  1291.  
  1292.   if (alive == 1 && ((curTimer - oldTimer) > 49))
  1293.   {
  1294.     printProgress(1);
  1295.     oldTimer = curTimer;
  1296.   }
  1297.  
  1298.   YIELD();
  1299.   YIELD();
  1300.   goto rekey;
  1301. }
  1302.