Subversion Repositories NedoOS

Rev

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