?login_element?

Subversion Repositories NedoOS

Rev

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