Subversion Repositories NedoOS

Rev

Rev 2275 | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <oscalls.h>
  5. #include <osfs.h>
  6. #include <intrz80.h>
  7. //#include <graphic.h>
  8. #include <../common/terminal.c>
  9. #include <tcp.h>
  10. //
  11. #define true 1
  12. #define false 0
  13.  
  14. unsigned int RBR_THR = 0xf8ef;
  15. unsigned int IER = 0xf9ef;
  16. unsigned int IIR_FCR = 0xfaef;
  17. unsigned int LCR = 0xfbef;
  18. unsigned int MCR = 0xfcef;
  19. unsigned int LSR = 0xfdef;
  20. unsigned int MSR = 0xfeef;
  21. unsigned int SR = 0xffef;
  22. unsigned int divider = 1;
  23. unsigned char comType = 0;
  24. unsigned int espType = 32;
  25.  
  26. unsigned char picture[15000];
  27. unsigned char netbuf[6912];
  28.  
  29. unsigned char minRating[] = "0000000000";
  30. struct fileStruct
  31. {
  32.   long picId;
  33.   unsigned int picYear;
  34.   unsigned long totalAmount;
  35.   unsigned int httpErr;
  36.   unsigned char picRating[8];
  37.   unsigned char picName[256];
  38.   unsigned char picType[64];
  39.   unsigned char authorIds[64];
  40.   unsigned char authorTitle[64];
  41.   unsigned char authorRealName[64];
  42.   unsigned char afn[128];
  43.   unsigned char pfn[128];
  44.   unsigned char fileName[128];
  45. } curFileStruct;
  46.  
  47. struct window
  48. {
  49.   unsigned char x;
  50.   unsigned char y;
  51.   unsigned char w;
  52.   unsigned char h;
  53.   unsigned char text;
  54.   unsigned char back;
  55.   unsigned char tittle[80];
  56. } curWin;
  57.  
  58. struct sockaddr_in dnsaddress;
  59. struct sockaddr_in targetadr;
  60. struct readstructure readStruct;
  61.  
  62. unsigned char ver[] = "4.2";
  63. const unsigned char sendOk[] = "SEND OK";
  64. const unsigned char gotWiFi[] = "WIFI GOT IP";
  65. unsigned char buffer[] = "0000000000";
  66. unsigned char userAgent[] = " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; GetPic)\r\n\r\n\0";
  67. unsigned char zxart[] = "zxart.ee";
  68. unsigned char keypress, verbose, randomPic, slideShow, netDriver;
  69.  
  70. unsigned long contLen;
  71. unsigned long count = 0;
  72. unsigned int headlng;
  73. unsigned int slideShowTime = 0;
  74. unsigned int loaded;
  75.  
  76. unsigned char crlf[2] = {13, 10};
  77. unsigned char cmd[512];
  78. unsigned char link[512];
  79. unsigned char fileIdChar[10];
  80.  
  81. void clearStatus(void)
  82. {
  83. }
  84.  
  85. void emptyKeys(void)
  86. {
  87.   unsigned char loop = 0, key;
  88.   do
  89.   {
  90.     key = OS_GETKEY();
  91.     if (loop > 64)
  92.     {
  93.       break;
  94.     }
  95.     loop++;
  96.   } while (key != 0);
  97. }
  98.  
  99. unsigned char delayLongKey(unsigned long counter)
  100. {
  101.   unsigned long start, finish, key;
  102.   counter = counter / 20;
  103.   if (counter < 1)
  104.   {
  105.     counter = 1;
  106.   }
  107.   start = time();
  108.   finish = start + counter;
  109.  
  110.   while (start < finish)
  111.   {
  112.     start = time();
  113.     key = OS_GETKEY();
  114.     if (key != 0)
  115.     {
  116.       return key;
  117.     }
  118.     YIELD();
  119.   }
  120.   return 32;
  121. }
  122.  
  123. void spaces(unsigned char number)
  124. {
  125.   while (number > 0)
  126.   {
  127.     putchar(' ');
  128.     number--;
  129.   }
  130. }
  131. void quit(void)
  132. {
  133.   OS_CLS(0);
  134.   OS_SETGFX(-1);
  135.   exit(0);
  136. }
  137. /*
  138. void infoBox(struct window w, const char *message)
  139. {
  140.   unsigned char wcount, tempx, tittleStart;
  141.  
  142.   w.h++;
  143.   OS_SETXY(w.x, w.y - 1);
  144.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  145.   OS_SETXY(w.x, w.y);
  146.   OS_SETCOLOR(w.text);
  147.   putchar(201);
  148.   for (wcount = 0; wcount < w.w; wcount++)
  149.   {
  150.     putchar(205);
  151.   }
  152.   putchar(187);
  153.   OS_SETXY(w.x, w.y + w.h);
  154.   putchar(200);
  155.   for (wcount = 0; wcount < w.w; wcount++)
  156.   {
  157.     putchar(205);
  158.   }
  159.   putchar(188);
  160.  
  161.   tempx = w.x + w.w + 1;
  162.   for (wcount = 1; wcount < w.h; wcount++)
  163.   {
  164.     OS_SETXY(w.x, w.y + wcount);
  165.     putchar(186);
  166.     OS_SETXY(tempx, w.y + wcount);
  167.     putchar(186);
  168.   }
  169.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  170.   OS_SETXY(tittleStart, w.y);
  171.   printf("[%s]", w.tittle);
  172.  
  173.   OS_SETXY(w.x + 1, w.y + 1);
  174.   OS_SETCOLOR(w.back);
  175.   tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  176.   OS_SETXY(tittleStart, w.y + 1);
  177.   printf("%s", message);
  178. }
  179. */
  180. void printHelp(void)
  181. {
  182.   OS_SETCOLOR(67);
  183.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  184.   OS_SETCOLOR(6);
  185.   printf("-------------------------------------------------------\n\r");
  186.   printf(" Управление:\n\r");
  187.   printf(" 'ESC' - выход из программы;\n\r");
  188.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  189.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  190.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  191.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  192.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  193.   printf(" 'V' не выводить информацию об авторах\n\r");
  194.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  195.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  196.   printf(" 'D' Переключение режима ZXNETUSB/ESP-COM\n\r");
  197.   printf(" 'T' Продолжительность одного слайда в int-ах \n\r");
  198.   printf(" 'M' Минимальный рейтинг для случайного воспроизведения. \n\r");
  199.   printf(" 'H' Данная справочная информация\n\r");
  200.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  201.   OS_SETCOLOR(70);
  202.   keypress = getchar();
  203.   OS_CLS(0);
  204. }
  205.  
  206. void delay(unsigned long counter)
  207. {
  208.   unsigned long start, finish;
  209.   counter = counter / 20;
  210.   if (counter < 1)
  211.   {
  212.     counter = 1;
  213.   }
  214.   start = time();
  215.   finish = start + counter;
  216.  
  217.   while (start < finish)
  218.   {
  219.     start = time();
  220.   }
  221. }
  222.  
  223. ///////////////////////////
  224. #include <../common/esp-com.c>
  225. #include <../common/network.c>
  226. //////////////////////////
  227.  
  228. int testOperation2(const char *process, int socket)
  229. {
  230.   if (socket < 0)
  231.   {
  232.     printf("%s: [ERROR:", process);
  233.     errorPrint(-socket);
  234.     printf("]\r\n");
  235.     YIELD();
  236.     return -socket;
  237.   }
  238.   return 1;
  239. }
  240.  
  241. int cutHeader(unsigned int todo)
  242. {
  243.   unsigned char *count1;
  244.  
  245.   curFileStruct.httpErr = httpError();
  246.   if (curFileStruct.httpErr != 200)
  247.   {
  248.     clearStatus();
  249.     printf("HTTP response:[%u]", curFileStruct.httpErr);
  250.     return 0;
  251.   }
  252.   count1 = strstr(netbuf, "Content-Length:");
  253.   if (count1 == NULL)
  254.   {
  255.     clearStatus();
  256.     printf("contLen not found");
  257.     contLen = 0;
  258.     curFileStruct.httpErr = 999; // bad kostil
  259.     return 0;
  260.   }
  261.   contLen = atol(count1 + 15);
  262.   // printf("Content-Length: %lu \n\r", contLen);
  263.  
  264.   count1 = strstr(netbuf, "\r\n\r\n");
  265.   if (count1 == NULL)
  266.   {
  267.     clearStatus();
  268.     printf("end of header not found\r\n");
  269.   }
  270.   else
  271.   {
  272.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  273.     // printf("header %u bytes\r\n", headlng);
  274.   }
  275.   return todo - headlng;
  276. }
  277.  
  278. char *str_replace(char *dst, int num, const char *str, const char *orig, const char *rep)
  279. {
  280.   const char *ptr;
  281.   size_t len1 = strlen(orig);
  282.   size_t len2 = strlen(rep);
  283.   char *tmp = dst;
  284.  
  285.   num -= 1;
  286.   while ((ptr = strstr(str, orig)) != NULL)
  287.   {
  288.     num -= (ptr - str) + len2;
  289.     if (num < 1)
  290.       break;
  291.  
  292.     strncpy(dst, str, (size_t)(ptr - str));
  293.     dst += ptr - str;
  294.     strncpy(dst, rep, len2);
  295.     dst += len2;
  296.     str = ptr + len1;
  297.   }
  298.  
  299.   for (; (*dst = *str) && (num > 0); --num)
  300.   {
  301.     ++dst;
  302.     ++str;
  303.   }
  304.   return tmp;
  305. }
  306.  
  307. char fillPictureEsp(void)
  308. {
  309.   unsigned char sizeLink;
  310.   unsigned long downloaded;
  311.   unsigned char byte, countl;
  312.   unsigned int todo;
  313.   const unsigned char *count1;
  314.   unsigned char firstPacket;
  315.   strcpy(link, netbuf);
  316.   sizeLink = strlen(link);
  317.   do
  318.   {
  319.     sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  320.     getAnswer2(); // CONNECT or ERROR or link is not valid
  321.     count1 = strstr(netbuf, "CONNECT");
  322.   } while (count1 == NULL);
  323.  
  324.   getAnswer2();                                   // OK
  325.   sprintf(netbuf, "AT+CIPSEND=%u", sizeLink + 2); // second CRLF in send command
  326.   sendcommand(netbuf);
  327.   getAnswer2();
  328.   do
  329.   {
  330.     byte = uart_readBlock();
  331.     // putchar(byte);
  332.   } while (byte != '>');
  333.   sendcommand(link);
  334.   countl = 0;
  335.   do
  336.   {
  337.     byte = uart_readBlock();
  338.     if (byte == sendOk[countl])
  339.     {
  340.       countl++;
  341.     }
  342.     else
  343.     {
  344.       countl = 0;
  345.     }
  346.   } while (countl < strlen(sendOk));
  347.   uart_readBlock(); // CR
  348.   uart_readBlock(); // LF
  349.   downloaded = 0;
  350.   firstPacket = true;
  351.   do
  352.   {
  353.     headlng = 0;
  354.     todo = recvHead();
  355.     getdataEsp(todo); // Requested size
  356.     if (firstPacket)
  357.     {
  358.       todo = cutHeader(todo);
  359.       firstPacket = false;
  360.       if (curFileStruct.httpErr != 200)
  361.       {
  362.         sendcommand("AT+CIPCLOSE");
  363.         getAnswer2(); // CLOSED
  364.         getAnswer2(); // OK
  365.         return false;
  366.       }
  367.     }
  368.  
  369.     if (downloaded + todo > sizeof(picture))
  370.     {
  371.       printf("dataBuffer overrun... %u reached \n\r", downloaded + todo);
  372.       return false;
  373.     }
  374.     memcpy(picture + downloaded, netbuf + headlng, todo);
  375.     downloaded = downloaded + todo;
  376.   } while (downloaded < contLen);
  377.   sendcommand("AT+CIPCLOSE");
  378.   getAnswer2(); // CLOSED or ERROR
  379.   count1 = strstr(netbuf, "CLOSED");
  380.   if (count1 != NULL)
  381.   {
  382.     getAnswer2(); // OK
  383.   }
  384.   return true;
  385. }
  386.  
  387. char fillPictureNet(void)
  388. {
  389.   int todo;
  390.   unsigned int downloaded = 0;
  391.   unsigned char firstPacket;
  392.   char socket, retry;
  393.   picture[0] = 0;
  394.   retry = 3;
  395.   socket = OpenSock(AF_INET, SOCK_STREAM);
  396.   if (testOperation2("OS_NETSOCKET", socket) != 1)
  397.   {
  398.     getchar();
  399.     quit();
  400.   }
  401.   todo = netConnect(socket, retry);
  402.   if (testOperation2("OS_NETCONNECT", todo) != 1)
  403.   {
  404.     getchar();
  405.     quit();
  406.   }
  407.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf), retry);
  408.   if (testOperation2("OS_WIZNETWRITE", todo) != 1)
  409.   {
  410.     getchar();
  411.     quit();
  412.   }
  413.   firstPacket = true;
  414.   do
  415.   {
  416.     headlng = 0;
  417.     todo = tcpRead(socket, retry);
  418.     testOperation("OS_WIZNETREAD", todo); // Quit if too many retries
  419.  
  420.     if (firstPacket)
  421.     {
  422.       todo = cutHeader(todo);
  423.       firstPacket = false;
  424.       if (curFileStruct.httpErr != 200)
  425.       {
  426.         netShutDown(socket, 0);
  427.         return false;
  428.       }
  429.     }
  430.  
  431.     if (downloaded + todo > sizeof(picture))
  432.     {
  433.       printf("dataBuffer overrun... %u reached \n\r", downloaded + todo);
  434.       return false;
  435.     }
  436.     memcpy(picture + downloaded, netbuf + headlng, todo);
  437.     downloaded = downloaded + todo;
  438.   } while (downloaded != contLen);
  439.  
  440.   netShutDown(socket, 0);
  441.   picture[downloaded + 1] = 0;
  442.   return true;
  443. }
  444.  
  445. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  446. {
  447.  
  448.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  449.   str_replace(pfn, tfnSize, pfn, "/", "_");
  450.   str_replace(pfn, tfnSize, pfn, ":", "_");
  451.   str_replace(pfn, tfnSize, pfn, "*", "_");
  452.   str_replace(pfn, tfnSize, pfn, "?", "_");
  453.   str_replace(pfn, tfnSize, pfn, "<", "_");
  454.   str_replace(pfn, tfnSize, pfn, ">", "_");
  455.   str_replace(pfn, tfnSize, pfn, "|", "_");
  456.   str_replace(pfn, tfnSize, pfn, " ", "_");
  457.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  458.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  459.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  460.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  461.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  462.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  463. }
  464.  
  465. void stringRepair(unsigned char *pfn, unsigned int tSize)
  466. {
  467.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  468.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  469.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  470.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  471.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  472.   str_replace(pfn, tSize, pfn, "\\/", "/");
  473. }
  474.  
  475. void ncReplace(void)
  476. {
  477.   unsigned char len;
  478.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  479.   {
  480.     if ((curFileStruct.afn[len] < ' ') || (curFileStruct.afn[len] > 0xef) || (curFileStruct.afn[len] > 0x7e && curFileStruct.afn[len] < 0xb0))
  481.     {
  482.       curFileStruct.afn[len] = '_';
  483.     }
  484.   }
  485.  
  486.   for (len = 0; len < strlen(curFileStruct.pfn); len++)
  487.   {
  488.     if ((curFileStruct.pfn[len] < ' ') || (curFileStruct.pfn[len] > 0xef) || (curFileStruct.pfn[len] > 0x7e && curFileStruct.pfn[len] < 0xb0))
  489.     {
  490.       curFileStruct.pfn[len] = '_';
  491.     }
  492.   }
  493. }
  494.  
  495. unsigned char savePic(unsigned long fileId)
  496. {
  497.   FILE *fp2;
  498.   unsigned char afnSize, tfnSize;
  499.  
  500.   afnSize = sizeof(curFileStruct.afn) - 1;
  501.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  502.  
  503.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  504.   nameRepair(curFileStruct.afn, afnSize);
  505.  
  506.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  507.   nameRepair(curFileStruct.pfn, tfnSize);
  508.   ncReplace();
  509.  
  510.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  511.   if (strlen(curFileStruct.fileName) > 62)
  512.   {
  513.     sprintf(fileIdChar, "-%ld", fileId);
  514.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  515.     curFileStruct.fileName[50] = '\0';
  516.     strcat(curFileStruct.fileName, fileIdChar);
  517.     strcat(curFileStruct.fileName, ".scr");
  518.   }
  519.   OS_SETSYSDRV();
  520.   OS_MKDIR("../downloads");        // Create if not exist
  521.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  522.   OS_CHDIR("../downloads/getpic");
  523.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  524.   if (((int)fp2) & 0xff)
  525.   {
  526.     printf("%s creating error\r\n", curFileStruct.fileName);
  527.     getchar();
  528.     quit();
  529.   }
  530.   OS_WRITEHANDLE(picture, fp2, 6912);
  531.   OS_CLOSEHANDLE(fp2);
  532.   return 0;
  533. }
  534.  
  535. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  536. {
  537.   unsigned int i, j;
  538.   unsigned int lenC, lenS;
  539.  
  540.   for (lenC = 0; c[lenC]; lenC++)
  541.     ;
  542.   for (lenS = 0; s[lenS]; lenS++)
  543.     ;
  544.  
  545.   for (i = startPos; i <= lenS - lenC; i++)
  546.   {
  547.     for (j = 0; s[i + j] == c[j]; j++)
  548.       ;
  549.  
  550.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  551.       return i;
  552.     if (j == lenC)
  553.       if (n - 1)
  554.         n--;
  555.       else
  556.         return i;
  557.   }
  558.   return -1;
  559. }
  560.  
  561. const char *parseJson(unsigned char *property)
  562. {
  563.   unsigned int w, lng, lngp1, findEnd, listPos;
  564.   unsigned char terminator;
  565.   int n;
  566.   // n = -1;
  567.   //  netbuf[0] = '\0';
  568.   n = pos(picture, property, 1, 0);
  569.   if (n == -1)
  570.   {
  571.     strcpy(netbuf, "-");
  572.     // printf("Property %s not found", property);
  573.     return netbuf;
  574.   }
  575.   lng = n - 1 + strlen(property);
  576.   if (picture[lng] == ':')
  577.   {
  578.     terminator = 0;
  579.   }
  580.   if (picture[lng] == '\"')
  581.   {
  582.     terminator = '\"';
  583.   }
  584.   if (picture[lng] == '[')
  585.   {
  586.     terminator = ']';
  587.   }
  588.  
  589.   findEnd = 1;
  590.   lngp1 = lng + 1;
  591.  
  592.   while (42)
  593.   {
  594.  
  595.     if ((picture[lngp1 + findEnd] == ','))
  596.     {
  597.       if (terminator == 0)
  598.       {
  599.         break;
  600.       }
  601.       if ((picture[lng + findEnd] == terminator))
  602.       {
  603.         findEnd--;
  604.         break;
  605.       }
  606.     }
  607.     findEnd++;
  608.   }
  609.   listPos = 0;
  610.   for (w = lngp1; w < findEnd + lngp1; w++)
  611.   {
  612.     netbuf[listPos] = picture[w];
  613.     listPos++;
  614.   }
  615.   netbuf[listPos] = 0;
  616.   return netbuf;
  617. }
  618.  
  619. void convert866(void)
  620. {
  621.   unsigned int lng, targetPos, w, q = 0;
  622.   unsigned char bufferl[8], one, two;
  623.   unsigned int decVal;
  624.   lng = strlen(netbuf);
  625.   targetPos = lng + 1;
  626.  
  627.   while (q < lng)
  628.   {
  629.     one = netbuf[q];
  630.     two = netbuf[q + 1];
  631.     if (one == 92 && two == 117)
  632.     {
  633.       q = q + 2;
  634.       for (w = 0; w < 4; w++)
  635.       {
  636.         bufferl[w] = netbuf[q + w];
  637.       }
  638.       q = q + 4;
  639.       bufferl[4] = '\0';
  640.       decVal = (unsigned int)strtol(bufferl, NULL, 16);
  641.  
  642.       if (decVal < 1088)
  643.       {
  644.         decVal = decVal - 912;
  645.       }
  646.       if (decVal > 1087)
  647.       {
  648.         decVal = decVal - 864;
  649.       }
  650.       if (decVal == 1025)
  651.       {
  652.         decVal = 240;
  653.       }
  654.       if (decVal == 1105)
  655.       {
  656.         decVal = 241;
  657.       }
  658.  
  659.       netbuf[targetPos] = decVal;
  660.     }
  661.     else
  662.     {
  663.       netbuf[targetPos] = netbuf[q];
  664.       q++;
  665.     }
  666.     targetPos++;
  667.   }
  668.   netbuf[targetPos] = 0;
  669.  
  670.   for (w = lng + 1; w < targetPos + 1; w++)
  671.   {
  672.     netbuf[w - lng - 1] = netbuf[w];
  673.   }
  674. }
  675.  
  676. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  677. {
  678.   unsigned int tSize;
  679.   const unsigned char *count1;
  680.   unsigned char result;
  681.   switch (queryNum)
  682.   {
  683.   case 0:
  684.     sprintf(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:%u/start:%lu/order:date,desc%s", limit, startPos, userAgent);
  685.     break;
  686.   case 1:
  687.     sprintf(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=%s;zxPictureType=standard%s", minRating, userAgent);
  688.     break;
  689.   case 99: // GET /jsonElementData/elementId:182798
  690.     sprintf(netbuf, "GET /jsonElementData/elementId:%lu%s", startPos, userAgent);
  691.     break;
  692.   }
  693.  
  694.   switch (netDriver)
  695.   {
  696.   case 0:
  697.     result = fillPictureNet();
  698.     break;
  699.   case 1:
  700.     result = fillPictureEsp();
  701.     break;
  702.   }
  703.  
  704.   if (!result)
  705.   {
  706.     return -1;
  707.   }
  708.  
  709.   count1 = strstr(picture, "responseStatus\":\"success");
  710.   if (count1 == NULL)
  711.   {
  712.     OS_CLS(0);
  713.     OS_SETCOLOR(66);
  714.     puts("Picture[]:");
  715.     puts(picture);
  716.     puts("---------------");
  717.     printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  718.     YIELD();
  719.     getchar();
  720.     return -1;
  721.   }
  722.  
  723.   count1 = strstr(picture, "\"id\":");
  724.   if (count1 == NULL)
  725.   {
  726.     parseJson("\"totalAmount\":");
  727.  
  728.     if (atol(netbuf) == 0)
  729.     {
  730.       return -3;
  731.     }
  732.  
  733.     if (netbuf[0] != '-')
  734.     {
  735.       return -4;
  736.     }
  737.     OS_CLS(0);
  738.     OS_SETCOLOR(66);
  739.     puts("Picture[]:");
  740.     puts(picture);
  741.     puts("---------------");
  742.     printf("PROCESS JSON: [ERROR: ID not found.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  743.     YIELD();
  744.     return -2;
  745.   }
  746.   netbuf[0] = 0;
  747.  
  748.   switch (queryNum)
  749.   {
  750.   case 0:
  751.   case 1:
  752.     parseJson("\"id\":");
  753.     curFileStruct.picId = atol(netbuf);
  754.     parseJson(",\"title\":\"");
  755.     convert866();
  756.     strcpy(curFileStruct.picName, netbuf);
  757.     tSize = sizeof(curFileStruct.picName);
  758.     stringRepair(curFileStruct.picName, tSize);
  759.  
  760.     parseJson(",\"type\":\"");
  761.     strcpy(curFileStruct.picType, netbuf);
  762.     parseJson("\"rating\":\"");
  763.     strcpy(curFileStruct.picRating, netbuf);
  764.     parseJson("\"year\":\"");
  765.     curFileStruct.picYear = atoi(netbuf);
  766.     parseJson("\"totalAmount\":");
  767.     curFileStruct.totalAmount = atol(netbuf);
  768.     parseJson("\"authorIds\":[");
  769.     strcpy(curFileStruct.authorIds, netbuf);
  770.     break;
  771.   case 99:
  772.     parseJson(",\"title\":\"");
  773.     convert866();
  774.     strcpy(curFileStruct.authorTitle, netbuf);
  775.     parseJson(",\"realName\":\"");
  776.     convert866();
  777.     strcpy(curFileStruct.authorRealName, netbuf);
  778.     break;
  779.   }
  780.   return curFileStruct.picId;
  781. }
  782.  
  783. void printData(void)
  784. {
  785.   OS_SETCOLOR(70);
  786.   printf(" #: ");
  787.   OS_SETCOLOR(71);
  788.   printf("%lu", count);
  789.   OS_SETCOLOR(70);
  790.   printf(" ID: ");
  791.   OS_SETCOLOR(71);
  792.   printf("%lu ", curFileStruct.picId);
  793.   OS_SETCOLOR(70);
  794.   printf(" Total Pics: ");
  795.   OS_SETCOLOR(71);
  796.   printf("%lu\r\n", curFileStruct.totalAmount);
  797.   OS_SETCOLOR(70);
  798.   printf(" Author: ");
  799.   OS_SETCOLOR(69);
  800.   printf("%s\r\n", curFileStruct.authorTitle);
  801.   OS_SETCOLOR(70);
  802.   printf(" TITLE: ");
  803.   OS_SETCOLOR(67);
  804.   printf("%s\r\n", curFileStruct.picName);
  805.   OS_SETCOLOR(70);
  806.   printf(" RATING: ");
  807.   OS_SETCOLOR(71);
  808.   printf("%s", curFileStruct.picRating);
  809.   OS_SETCOLOR(70);
  810.   printf(" YEAR: ");
  811.   OS_SETCOLOR(71);
  812.   printf("%u\r\n", curFileStruct.picYear);
  813.   OS_SETCOLOR(70);
  814.   printf(" AuthorsIDs ");
  815.   OS_SETCOLOR(71);
  816.   printf("%s", curFileStruct.authorIds);
  817.   OS_SETCOLOR(70);
  818.   printf(" Real name: ");
  819.   OS_SETCOLOR(71);
  820.   printf("%s\r\n", curFileStruct.authorRealName);
  821.   OS_SETCOLOR(69);
  822.   printf("\r\n");
  823.   printf("\r\n");
  824.  
  825.   OS_SETCOLOR(70);
  826.  
  827.   OS_SETCOLOR(70);
  828.   printf(" Query: ");
  829.   OS_SETCOLOR(71);
  830.   if (randomPic)
  831.   {
  832.     printf("Random pic with %s+ rating\r\n", minRating);
  833.   }
  834.   else
  835.   {
  836.     puts("Sequental from newest");
  837.   }
  838.   OS_SETCOLOR(70);
  839.   printf(" Mode : ");
  840.   OS_SETCOLOR(71);
  841.  
  842.   if (slideShow)
  843.   {
  844.     printf("Slide-show, %u ints \r\n", slideShowTime);
  845.   }
  846.   else
  847.   {
  848.     puts("Manual show");
  849.   }
  850.  
  851.   // YIELD();
  852. }
  853.  
  854. unsigned char inputBox(struct window w, const char *prefilled)
  855. {
  856.   unsigned char wcount, tempx, tittleStart;
  857.   unsigned char byte, counter;
  858.   w.h++;
  859.   OS_SETXY(w.x, w.y - 1);
  860.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  861.   OS_SETXY(w.x, w.y);
  862.   OS_SETCOLOR(w.text);
  863.   putchar(201);
  864.   for (wcount = 0; wcount < w.w; wcount++)
  865.   {
  866.     putchar(205);
  867.   }
  868.   putchar(187);
  869.   OS_SETXY(w.x, w.y + w.h);
  870.   putchar(200);
  871.   for (wcount = 0; wcount < w.w; wcount++)
  872.   {
  873.     putchar(205);
  874.   }
  875.   putchar(188);
  876.  
  877.   tempx = w.x + w.w + 1;
  878.   for (wcount = 1; wcount < w.h; wcount++)
  879.   {
  880.     OS_SETXY(w.x, w.y + wcount);
  881.     putchar(186);
  882.     OS_SETXY(tempx, w.y + wcount);
  883.     putchar(186);
  884.   }
  885.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  886.   OS_SETXY(tittleStart, w.y);
  887.   printf("[%s]", w.tittle);
  888.   OS_SETXY(w.x + 1, w.y + 1);
  889.   OS_SETCOLOR(w.back);
  890.   putchar(219);
  891.  
  892.   cmd[0] = 0;
  893.  
  894.   counter = strlen(prefilled);
  895.   if (counter != 0)
  896.   {
  897.     strcpy(cmd, prefilled);
  898.     goto skipKeys;
  899.   }
  900.  
  901.   do
  902.   {
  903.     byte = OS_GETKEY();
  904.     if (byte != 0)
  905.     {
  906.       switch (byte)
  907.       {
  908.       case 0x08:
  909.         if (counter > 0)
  910.         {
  911.           counter--;
  912.           cmd[counter] = 0;
  913.         }
  914.         break;
  915.       case 0x0d:
  916.  
  917.         if (counter == 0)
  918.         {
  919.           return false;
  920.         }
  921.         else
  922.         {
  923.           return true;
  924.         }
  925.  
  926.       case 31:
  927.         break;
  928.       case 250:
  929.         break;
  930.       case 249:
  931.         break;
  932.       case 248:
  933.         break;
  934.       case 251: // Right
  935.         break;
  936.       case 252: // Del
  937.         OS_SETXY(w.x + 1, w.y + 1);
  938.         spaces(counter + 1);
  939.         cmd[0] = 0;
  940.         counter = 0;
  941.         break;
  942.       case 27:
  943.         cmd[0] = 0;
  944.         return false;
  945.       default:
  946.         if (counter < w.w - 1)
  947.         {
  948.           cmd[counter] = byte;
  949.           counter++;
  950.           cmd[counter] = 0;
  951.         }
  952.         break;
  953.       }
  954.     skipKeys:
  955.       OS_SETXY(w.x + 1, w.y + 1);
  956.       printf("%s", cmd);
  957.       putchar(219);
  958.       if (byte == 0x08)
  959.       {
  960.         putchar(' ');
  961.       }
  962.     }
  963.     YIELD();
  964.   } while (42);
  965.   return false;
  966. }
  967.  
  968. void safeKeys(unsigned char keypress)
  969. {
  970.   switch (keypress)
  971.   {
  972.   case 27:
  973.     OS_SETCOLOR(70);
  974.     printf("Good bye...\r\n");
  975.     delayLong(500);
  976.     quit();
  977.     break;
  978.   case 'j':
  979.   case 'J':
  980.     curWin.w = 13;
  981.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  982.     curWin.y = 11;
  983.     curWin.h = 1;
  984.     curWin.text = 103;
  985.     curWin.back = 103;
  986.     strcpy(curWin.tittle, "# of pic:");
  987.     if (inputBox(curWin, ""))
  988.     {
  989.       sscanf(cmd, "%lu", &count);
  990.       if (count > curFileStruct.totalAmount - 1)
  991.       {
  992.         count = curFileStruct.totalAmount - 1;
  993.       }
  994.     }
  995.     break;
  996.   case 't':
  997.   case 'T':
  998.     curWin.w = 20;
  999.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1000.     curWin.y = 11;
  1001.     curWin.h = 1;
  1002.     curWin.text = 103;
  1003.     curWin.back = 103;
  1004.     strcpy(curWin.tittle, "Slide time(ints)");
  1005.     if (inputBox(curWin, ""))
  1006.     {
  1007.       sscanf(cmd, "%u", &slideShowTime);
  1008.       OS_CLS(0);
  1009.       OS_SETCOLOR(70);
  1010.       printf("Slide duration set to %u ints.", slideShowTime);
  1011.       delayLong(500);
  1012.     }
  1013.     break;
  1014.   case 'v':
  1015.   case 'V':
  1016.     verbose = !verbose;
  1017.  
  1018.     if (verbose == 0)
  1019.     {
  1020.       BOX(1, 1, 80, 25, 40, ' ');
  1021.       AT(1, 1);
  1022.     }
  1023.     break;
  1024.   case 'h':
  1025.   case 'H':
  1026.     printHelp();
  1027.     break;
  1028.   case 'r':
  1029.   case 'R':
  1030.     randomPic = !randomPic;
  1031.     OS_SETCOLOR(70);
  1032.     if (verbose == 1)
  1033.     {
  1034.       if (randomPic == 1)
  1035.       {
  1036.         printf("    Random mode enabled...\r\n");
  1037.         count = 0;
  1038.         delayLong(500);
  1039.       }
  1040.       else
  1041.       {
  1042.         printf("    Sequental mode enabled...\r\n");
  1043.         count = 0;
  1044.         delayLong(500);
  1045.       }
  1046.     }
  1047.     break;
  1048.   case 'a':
  1049.   case 'A':
  1050.     slideShow = !slideShow;
  1051.     OS_SETCOLOR(70);
  1052.     if (slideShow == 1)
  1053.     {
  1054.       if (verbose == 1)
  1055.         printf("    SlideShow mode enabled...\r\n\r\n");
  1056.       slideShowTime = 150;
  1057.       delayLong(500);
  1058.     }
  1059.     else
  1060.     {
  1061.       if (verbose == 1)
  1062.         printf("    Manual mode enabled...\r\n\r\n");
  1063.       slideShowTime = 0;
  1064.       delayLong(500);
  1065.     }
  1066.     break;
  1067.   case 'd':
  1068.   case 'D':
  1069.     netDriver = !netDriver;
  1070.     OS_SETCOLOR(70);
  1071.     if (netDriver == 1)
  1072.     {
  1073.       printf("    ESP-COM mode enabled...\r\n");
  1074.       loadEspConfig();
  1075.       uart_init(divider);
  1076.       espReBoot();
  1077.       printf("    ESP-COM inited...\r\n");
  1078.       delayLong(500);
  1079.     }
  1080.     else
  1081.     {
  1082.       if (verbose == 1)
  1083.         printf("    NedoNET mode enabled...");
  1084.       delayLong(500);
  1085.     }
  1086.     break;
  1087.   case 'm':
  1088.   case 'M':
  1089.     curWin.w = 22;
  1090.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1091.     curWin.y = 1;
  1092.     curWin.h = 1;
  1093.     curWin.text = 103;
  1094.     curWin.back = 103;
  1095.     strcpy(curWin.tittle, "Минимальная оценка:");
  1096.  
  1097.     if (inputBox(curWin, ""))
  1098.     {
  1099.       char counter;
  1100.       for (counter = 0; counter < strlen(cmd); counter++)
  1101.       {
  1102.         if ((((cmd[counter] < '0') || (cmd[counter] > '9'))) && cmd[counter] != '.')
  1103.         {
  1104.           counter = 0;
  1105.           break;
  1106.         }
  1107.       }
  1108.       if (counter != 0)
  1109.       {
  1110.         strncpy(minRating, cmd, 5);
  1111.         count = 0;
  1112.       }
  1113.     }
  1114.   default:
  1115.     break;
  1116.   }
  1117. }
  1118.  
  1119. void init(void)
  1120. {
  1121.   OS_SETSYSDRV();
  1122.   OS_MKDIR("../downloads");        // Create if not exist
  1123.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  1124.   OS_CHDIR("../downloads/getpic");
  1125.  
  1126.   count = 0;
  1127.   verbose = 1;
  1128.   randomPic = 0;
  1129.   slideShow = 0;
  1130.   netDriver = 0;
  1131.   strcpy(minRating, "4.0");
  1132.  
  1133.   targetadr.family = AF_INET;
  1134.   targetadr.porth = 00;
  1135.   targetadr.portl = 80;
  1136.   targetadr.b1 = 217; // D9
  1137.   targetadr.b2 = 146; // 92
  1138.   targetadr.b3 = 69;  // 45
  1139.   targetadr.b4 = 13;  // 0D
  1140. }
  1141.  
  1142. unsigned char viewScreen6912c(unsigned int bufAdr)
  1143. {
  1144.   unsigned char key;
  1145.   OS_SETBORDER(0);
  1146.   OS_SETGFX(0x83);
  1147.   SETPG32KHIGH(OS_GETSCR0() >> 8);
  1148.   memcpy((unsigned char *)(0xc000), (unsigned char *)(bufAdr), 6912);
  1149.  
  1150.   if (slideShowTime != 0)
  1151.   {
  1152.     key = delayLongKey(slideShowTime * 20);
  1153.   }
  1154.   else
  1155.   {
  1156.     key = getchar();
  1157.   }
  1158.   OS_SETGFX(0x86);
  1159.   return key;
  1160. }
  1161.  
  1162. C_task main(void)
  1163. {
  1164.   long iddqd, idkfa;
  1165.   char result;
  1166.  
  1167.   OS_HIDEFROMPARENT();
  1168.   OS_SETGFX(0x86);
  1169.   OS_CLS(0);
  1170.  
  1171.   init();
  1172.  
  1173.   printHelp();
  1174.   safeKeys(keypress);
  1175.  
  1176.   if (netDriver == 0)
  1177.   {
  1178.  
  1179.     get_dns();
  1180.     dnsResolve("zxart.ee");
  1181.     printf("\r\n");
  1182.   }
  1183.  
  1184. start:
  1185.   keypress = 0;
  1186.   switch (randomPic)
  1187.   {
  1188.   case 0:
  1189.  
  1190.     iddqd = processJson(count, 1, 0);
  1191.     break;
  1192.   case 1:
  1193.     iddqd = processJson(0, 1, 1);
  1194.     break;
  1195.   }
  1196.  
  1197.   OS_SETCOLOR(70);
  1198.  
  1199.   switch (iddqd)
  1200.   {
  1201.   case -3: // return 0 pictures
  1202.     strcpy(minRating, "1.0");
  1203.     printf("[%u]No picture is returned in query. Minimal rating is set to %s\r\n", curFileStruct.httpErr, minRating);
  1204.     delayLong(500);
  1205.     goto start;
  1206.   case -4: // return xxxx picture, but empty body.
  1207.     printf("[%u]Empty body is returned. Next picture, please.(%ld)...\r\n", curFileStruct.httpErr, iddqd);
  1208.     delayLong(500);
  1209.     count++;
  1210.     goto start;
  1211.   case -1: // return HTTP error != 200
  1212.     printf("[%u]Error getting pic info. Next picture, please(%ld)...\r\n", curFileStruct.httpErr, iddqd);
  1213.     count++;
  1214.     delayLong(500);
  1215.     goto start;
  1216.   }
  1217.  
  1218.   if (verbose == 1)
  1219.   {
  1220.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  1221.     if (idkfa < 0)
  1222.     {
  1223.       printf("[%u]Error can't parse authorIds(%s). Next picture, please...\r\n", curFileStruct.httpErr, curFileStruct.authorIds);
  1224.       count++;
  1225.       delayLong(500);
  1226.       goto start;
  1227.     }
  1228.   }
  1229.   if (strcmp(curFileStruct.picType, "standard") != 0)
  1230.   {
  1231.     printf("[%u]Error format '%s' not supported. Next picture, please.\n\r", curFileStruct.httpErr, curFileStruct.picType);
  1232.     delayLong(500);
  1233.     count++;
  1234.     goto start;
  1235.   }
  1236.   sprintf(netbuf, "GET /file/id:%ld%s", iddqd, userAgent);
  1237.   switch (netDriver)
  1238.   {
  1239.   case 0:
  1240.     result = fillPictureNet();
  1241.     break;
  1242.   case 1:
  1243.     result = fillPictureEsp();
  1244.     break;
  1245.   }
  1246.  
  1247.   if (result == -1) // return HTTP error != 200
  1248.   {
  1249.     printf("[%u]Error getting pic. Next picture, please...\r\n", curFileStruct.httpErr);
  1250.     count++;
  1251.     delayLong(500);
  1252.     goto start;
  1253.   }
  1254.  
  1255. review:
  1256.   OS_CLS(0);
  1257.   YIELD();
  1258.   // keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  1259.   keypress = viewScreen6912c((unsigned int)&picture);
  1260.   emptyKeys();
  1261.  
  1262.   ///// Keys only for pictures
  1263.   if (keypress == 's' || keypress == 'S')
  1264.   {
  1265.     savePic(iddqd);
  1266.     printf("%s saved.", curFileStruct.fileName);
  1267.     delayLong(500);
  1268.     count++;
  1269.   }
  1270.  
  1271.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1272.   {
  1273.     if (count > 0)
  1274.     {
  1275.       count--;
  1276.     }
  1277.   }
  1278.   if (keypress == 251 || keypress == 32)
  1279.   {
  1280.     count++;
  1281.     goto start;
  1282.   }
  1283.   if (keypress == 'i' || keypress == 'I')
  1284.   {
  1285.     printData();
  1286.     while (OS_GETKEY() == 0)
  1287.     {
  1288.     }
  1289.     goto review;
  1290.   }
  1291.   safeKeys(keypress);
  1292.   goto start;
  1293. }
  1294.