Subversion Repositories NedoOS

Rev

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

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