Subversion Repositories NedoOS

Rev

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