Subversion Repositories NedoOS

Rev

Rev 2427 | 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.   case 98:
  684.     sprintf(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:%u/start:%lu/order:date,desc%s", limit, startPos, userAgent);
  685.     break;
  686.   case 1:
  687.     sprintf(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=%s;zxPictureType=standard%s", minRating, userAgent);
  688.     break;
  689.   case 99: // GET /jsonElementData/elementId:182798
  690.     sprintf(netbuf, "GET /jsonElementData/elementId:%lu%s", startPos, userAgent);
  691.     break;
  692.   }
  693.  
  694.   switch (netDriver)
  695.   {
  696.   case 0:
  697.     result = fillPictureNet();
  698.     break;
  699.   case 1:
  700.     result = fillPictureEsp();
  701.     break;
  702.   }
  703.  
  704.   if (!result)
  705.   {
  706.     return -1;
  707.   }
  708.  
  709.   count1 = strstr(picture, "responseStatus\":\"success");
  710.   if (count1 == NULL)
  711.   {
  712.     return -1;
  713.   }
  714.  
  715.   count1 = strstr(picture, "\"id\":");
  716.   if (count1 == NULL)
  717.   {
  718.     parseJson("\"totalAmount\":");
  719.  
  720.     if (atol(netbuf) == 0)
  721.     {
  722.       return -3;
  723.     }
  724.  
  725.     if (netbuf[0] != '-')
  726.     {
  727.       return -4;
  728.     }
  729.     return -2;
  730.   }
  731.   netbuf[0] = 0;
  732.  
  733.   switch (queryNum)
  734.   {
  735.   case 0:
  736.   case 1:
  737.     parseJson("\"id\":");
  738.     curFileStruct.picId = atol(netbuf);
  739.     parseJson(",\"title\":\"");
  740.     convert866();
  741.     strcpy(curFileStruct.picName, netbuf);
  742.     tSize = sizeof(curFileStruct.picName);
  743.     stringRepair(curFileStruct.picName, tSize);
  744.  
  745.     parseJson(",\"type\":\"");
  746.     strcpy(curFileStruct.picType, netbuf);
  747.     parseJson("\"rating\":\"");
  748.     strcpy(curFileStruct.picRating, netbuf);
  749.     parseJson("\"year\":");
  750.     curFileStruct.picYear = atoi(netbuf);
  751.     parseJson("\"totalAmount\":");
  752.     curFileStruct.totalAmount = atol(netbuf);
  753.     parseJson("\"authorIds\":[");
  754.     strcpy(curFileStruct.authorIds, netbuf);
  755.     parseJson(",\"description\":\"");
  756.  
  757.     if (netbuf[0] != '-')
  758.     {
  759.       curFileStruct.hasDescription = true;
  760.     }
  761.     else
  762.     {
  763.       curFileStruct.hasDescription = false;
  764.     }
  765.     break;
  766.  
  767.   case 98:
  768.     parseJson(",\"description\":\"");
  769.     convert866();
  770.     break;
  771.  
  772.   case 99: // Author info
  773.     parseJson(",\"title\":\"");
  774.     convert866();
  775.     strcpy(curFileStruct.authorTitle, netbuf);
  776.     parseJson(",\"realName\":\"");
  777.     convert866();
  778.     strcpy(curFileStruct.authorRealName, netbuf);
  779.     break;
  780.   }
  781.   return curFileStruct.picId;
  782. }
  783.  
  784. void showDescription(unsigned long counter)
  785. {
  786.   processJson(counter, 1, 98);
  787.   puts(netbuf);
  788. }
  789.  
  790. void printData(void)
  791. {
  792.   OS_SETGFX(0x86);
  793.   OS_CLS(0);
  794.   OS_SETCOLOR(70);
  795.   printf(" #: ");
  796.   OS_SETCOLOR(71);
  797.   printf("%lu", count);
  798.   OS_SETCOLOR(70);
  799.   printf(" ID: ");
  800.   OS_SETCOLOR(71);
  801.   printf("%ld ", curFileStruct.picId);
  802.   OS_SETCOLOR(70);
  803.   printf(" Total Pics: ");
  804.   OS_SETCOLOR(71);
  805.   printf("%lu\r\n", curFileStruct.totalAmount);
  806.   OS_SETCOLOR(70);
  807.   printf(" Author: ");
  808.   OS_SETCOLOR(69);
  809.   printf("%s\r\n", curFileStruct.authorTitle);
  810.   OS_SETCOLOR(70);
  811.   printf(" TITLE: ");
  812.   OS_SETCOLOR(67);
  813.   printf("%s\r\n", curFileStruct.picName);
  814.   OS_SETCOLOR(70);
  815.   printf(" RATING: ");
  816.   OS_SETCOLOR(71);
  817.   printf("%s", curFileStruct.picRating);
  818.   OS_SETCOLOR(70);
  819.   printf(" YEAR: ");
  820.   OS_SETCOLOR(71);
  821.   printf("%u\r\n", curFileStruct.picYear);
  822.   OS_SETCOLOR(70);
  823.   printf(" AuthorsIDs ");
  824.   OS_SETCOLOR(71);
  825.   printf("%s", curFileStruct.authorIds);
  826.   OS_SETCOLOR(70);
  827.   printf(" Real name: ");
  828.   OS_SETCOLOR(71);
  829.   printf("%s\r\n", curFileStruct.authorRealName);
  830.   OS_SETCOLOR(70);
  831.   printf(" Description: ");
  832.   OS_SETCOLOR(71);
  833.  
  834.   if (showDesc)
  835.   {
  836.     if (curFileStruct.hasDescription == true)
  837.     {
  838.       showDescription(count);
  839.     }
  840.     else
  841.     {
  842.       printf("none");
  843.     }
  844.   }
  845.   else
  846.   {
  847.     printf("disabled");
  848.   }
  849.  
  850.   OS_SETCOLOR(69);
  851.   printf("\r\n");
  852.   printf("\r\n");
  853.  
  854.   OS_SETCOLOR(70);
  855.  
  856.   OS_SETCOLOR(70);
  857.   printf(" Query: ");
  858.   OS_SETCOLOR(71);
  859.   if (randomPic)
  860.   {
  861.     printf("Random pic with %s+ rating\r\n", minRating);
  862.   }
  863.   else
  864.   {
  865.     puts("Sequental from newest");
  866.   }
  867.   OS_SETCOLOR(70);
  868.   printf(" Mode : ");
  869.   OS_SETCOLOR(71);
  870.  
  871.   if (slideShow)
  872.   {
  873.     printf("Slide-show, %u ints \r\n", slideShowTime);
  874.   }
  875.   else
  876.   {
  877.     puts("Manual show");
  878.   }
  879.  
  880.   // YIELD();
  881. }
  882.  
  883. unsigned char inputBox(struct window w, const char *prefilled)
  884. {
  885.   unsigned char wcount, tempx, tittleStart;
  886.   unsigned char byte, counter;
  887.   w.h++;
  888.   OS_SETXY(w.x, w.y - 1);
  889.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  890.   OS_SETXY(w.x, w.y);
  891.   OS_SETCOLOR(w.text);
  892.   putchar(201);
  893.   for (wcount = 0; wcount < w.w; wcount++)
  894.   {
  895.     putchar(205);
  896.   }
  897.   putchar(187);
  898.   OS_SETXY(w.x, w.y + w.h);
  899.   putchar(200);
  900.   for (wcount = 0; wcount < w.w; wcount++)
  901.   {
  902.     putchar(205);
  903.   }
  904.   putchar(188);
  905.  
  906.   tempx = w.x + w.w + 1;
  907.   for (wcount = 1; wcount < w.h; wcount++)
  908.   {
  909.     OS_SETXY(w.x, w.y + wcount);
  910.     putchar(186);
  911.     OS_SETXY(tempx, w.y + wcount);
  912.     putchar(186);
  913.   }
  914.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  915.   OS_SETXY(tittleStart, w.y);
  916.   printf("[%s]", w.tittle);
  917.   OS_SETXY(w.x + 1, w.y + 1);
  918.   OS_SETCOLOR(w.back);
  919.   putchar(219);
  920.  
  921.   cmd[0] = 0;
  922.  
  923.   counter = strlen(prefilled);
  924.   if (counter != 0)
  925.   {
  926.     strcpy(cmd, prefilled);
  927.     goto skipKeys;
  928.   }
  929.  
  930.   do
  931.   {
  932.     byte = OS_GETKEY();
  933.     if (byte != 0)
  934.     {
  935.       switch (byte)
  936.       {
  937.       case 0x08:
  938.         if (counter > 0)
  939.         {
  940.           counter--;
  941.           cmd[counter] = 0;
  942.         }
  943.         break;
  944.       case 0x0d:
  945.  
  946.         if (counter == 0)
  947.         {
  948.           return false;
  949.         }
  950.         else
  951.         {
  952.           return true;
  953.         }
  954.  
  955.       case 31:
  956.         break;
  957.       case 250:
  958.         break;
  959.       case 249:
  960.         break;
  961.       case 248:
  962.         break;
  963.       case 251: // Right
  964.         break;
  965.       case 252: // Del
  966.         OS_SETXY(w.x + 1, w.y + 1);
  967.         spaces(counter + 1);
  968.         cmd[0] = 0;
  969.         counter = 0;
  970.         break;
  971.       case 27:
  972.         cmd[0] = 0;
  973.         return false;
  974.       default:
  975.         if (counter < w.w - 1)
  976.         {
  977.           cmd[counter] = byte;
  978.           counter++;
  979.           cmd[counter] = 0;
  980.         }
  981.         break;
  982.       }
  983.     skipKeys:
  984.       OS_SETXY(w.x + 1, w.y + 1);
  985.       printf("%s", cmd);
  986.       putchar(219);
  987.       if (byte == 0x08)
  988.       {
  989.         putchar(' ');
  990.       }
  991.     }
  992.     YIELD();
  993.   } while (42);
  994.   return false;
  995. }
  996.  
  997. void safeKeys(unsigned char keypress)
  998. {
  999.   switch (keypress & 0xdf)
  1000.   {
  1001.   case 27:
  1002.     quit();
  1003.     break;
  1004.   case 'J':
  1005.     curWin.w = 13;
  1006.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1007.     curWin.y = 11;
  1008.     curWin.h = 1;
  1009.     curWin.text = 103;
  1010.     curWin.back = 103;
  1011.     strcpy(curWin.tittle, "# of pic:");
  1012.     if (inputBox(curWin, ""))
  1013.     {
  1014.       sscanf(cmd, "%lu", &count);
  1015.     }
  1016.     break;
  1017.   case 'T':
  1018.     curWin.w = 20;
  1019.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1020.     curWin.y = 11;
  1021.     curWin.h = 1;
  1022.     curWin.text = 103;
  1023.     curWin.back = 103;
  1024.     strcpy(curWin.tittle, "Slide time(ints)");
  1025.     if (inputBox(curWin, ""))
  1026.     {
  1027.       sscanf(cmd, "%u", &slideShowTime);
  1028.       if (slideShowTime == 0)
  1029.       {
  1030.         slideShowTime = 1;
  1031.       }
  1032.       OS_CLS(0);
  1033.       OS_SETCOLOR(70);
  1034.       printf("Slide duration set to %u ints.", slideShowTime);
  1035.       delayLong(500);
  1036.       OS_CLS(0);
  1037.     }
  1038.     break;
  1039.   case 'V':
  1040.     verbose = !verbose;
  1041.     break;
  1042.   case 'H':
  1043.     printHelp();
  1044.     break;
  1045.   case 'R':
  1046.     randomPic = !randomPic;
  1047.     OS_SETCOLOR(70);
  1048.     if (verbose)
  1049.     {
  1050.       if (randomPic == 1)
  1051.       {
  1052.         printf("    Random mode enabled...\r\n");
  1053.         count = 0;
  1054.         // delayLong(500);
  1055.       }
  1056.       else
  1057.       {
  1058.         printf("    Sequental mode enabled...\r\n");
  1059.         count = 0;
  1060.         // delayLong(500);
  1061.       }
  1062.     }
  1063.     break;
  1064.   case 'A':
  1065.     slideShow = !slideShow;
  1066.     OS_SETCOLOR(70);
  1067.     if (slideShow == 1)
  1068.     {
  1069.       if (verbose == 1)
  1070.         printf("    SlideShow mode enabled...\r\n\r\n");
  1071.       slideShowTime = 150;
  1072.       delayLong(500);
  1073.     }
  1074.     else
  1075.     {
  1076.       if (verbose == 1)
  1077.         printf("    Manual mode enabled...\r\n\r\n");
  1078.       slideShowTime = 0;
  1079.       delayLong(500);
  1080.     }
  1081.     break;
  1082.   case 'D':
  1083.     netDriver = !netDriver;
  1084.     OS_SETCOLOR(70);
  1085.     if (netDriver == 1)
  1086.     {
  1087.       printf("    ESP-COM mode enabled...\r\n");
  1088.       OS_GETPATH((unsigned int)&curPath);
  1089.       loadEspConfig();
  1090.       OS_CHDIR(curPath);
  1091.       uart_init(divider);
  1092.  
  1093.       espReBoot();
  1094.     }
  1095.     else
  1096.     {
  1097.       if (verbose == 1)
  1098.         printf("    NedoNET mode enabled...");
  1099.       delayLong(500);
  1100.     }
  1101.     break;
  1102.   case 'M':
  1103.     curWin.w = 22;
  1104.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1105.     curWin.y = 1;
  1106.     curWin.h = 1;
  1107.     curWin.text = 103;
  1108.     curWin.back = 103;
  1109.     strcpy(curWin.tittle, "Minimal rating:");
  1110.  
  1111.     if (inputBox(curWin, ""))
  1112.     {
  1113.       char counter;
  1114.       for (counter = 0; counter < strlen(cmd); counter++)
  1115.       {
  1116.         if ((((cmd[counter] < '0') || (cmd[counter] > '9'))) && cmd[counter] != '.')
  1117.         {
  1118.           counter = 0;
  1119.           break;
  1120.         }
  1121.       }
  1122.       if (counter != 0)
  1123.       {
  1124.         strncpy(minRating, cmd, 5);
  1125.         count = 0;
  1126.       }
  1127.     }
  1128.   default:
  1129.     break;
  1130.   }
  1131. }
  1132.  
  1133. char readParamFromIni(void)
  1134. {
  1135.   FILE *fpini;
  1136.   unsigned char *count1;
  1137.   const char currentNetwork[] = "currentNetwork";
  1138.   unsigned char curNet = 0;
  1139.  
  1140.   OS_GETPATH((unsigned int)&curPath);
  1141.  
  1142.   OS_SETSYSDRV();
  1143.   OS_CHDIR("/");
  1144.   OS_CHDIR("ini");
  1145.  
  1146.   fpini = OS_OPENHANDLE("network.ini", 0x80);
  1147.   if (((int)fpini) & 0xff)
  1148.   {
  1149.     OS_CHDIR(curPath);
  1150.     clearStatus();
  1151.     printf("network.ini not found.\r\n");
  1152.     getchar();
  1153.     return false;
  1154.   }
  1155.  
  1156.   OS_READHANDLE(netbuf, fpini, sizeof(netbuf) - 1);
  1157.   OS_CLOSEHANDLE(fpini);
  1158.  
  1159.   count1 = strstr(netbuf, currentNetwork);
  1160.   if (count1 != NULL)
  1161.   {
  1162.     sscanf(count1 + strlen(currentNetwork) + 1, "%u", &curNet);
  1163.   }
  1164.  
  1165.   OS_CHDIR(curPath);
  1166.   return curNet;
  1167. }
  1168.  
  1169. void init(void)
  1170. {
  1171.   count = 0;
  1172.   verbose = 1;
  1173.   showDesc = false;
  1174.   randomPic = 0;
  1175.   slideShow = 0;
  1176.   strcpy(minRating, "4.1");
  1177.   targetadr.family = AF_INET;
  1178.   targetadr.porth = 00;
  1179.   targetadr.portl = 80;
  1180.   targetadr.b1 = 217; // D9
  1181.   targetadr.b2 = 146; // 92
  1182.   targetadr.b3 = 69;  // 45
  1183.   targetadr.b4 = 13;  // 0D
  1184.  
  1185.   OS_SETSYSDRV();
  1186.   OS_MKDIR("../downloads");        // Create if not exist
  1187.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  1188.   OS_CHDIR("../downloads/getpic");
  1189.  
  1190.   netDriver = readParamFromIni();
  1191.  
  1192.   if (netDriver == 0)
  1193.   {
  1194.     verbose = 0;
  1195.     get_dns();
  1196.     clearStatus();
  1197.     dnsResolve("zxart.ee");
  1198.   }
  1199.  
  1200.   if (netDriver == 1)
  1201.   {
  1202.     OS_GETPATH((unsigned int)&curPath);
  1203.     loadEspConfig();
  1204.     OS_CHDIR(curPath);
  1205.     uart_init(divider);
  1206.     if (!espReBoot())
  1207.     {
  1208.       puts("Error rebooting ESP!. Press any key to continue.");
  1209.       writeLog("Error rebooting ESP!. Continue.", "main           ");
  1210.       getchar();
  1211.     }
  1212.     writeLog("GetPic Started & Inited.", "main           ");
  1213.   }
  1214. }
  1215.  
  1216. void viewScreen6912c(unsigned int bufAdr)
  1217. {
  1218.   OS_CLS(0);
  1219.   OS_SETBORDER(0);
  1220.   SETPG32KHIGH(OS_GETSCR0() >> 8);
  1221.   memcpy((unsigned char *)(0xc000), (unsigned char *)(bufAdr), 6912);
  1222.   OS_SETGFX(0x83);
  1223.   return;
  1224. }
  1225.  
  1226. C_task main(void)
  1227. {
  1228.   long iddqd, idkfa;
  1229.   char result;
  1230.   OS_SETGFX(0x86);
  1231.   OS_CLS(0);
  1232.  
  1233.   init();
  1234.  
  1235.   OS_HIDEFROMPARENT();
  1236.  
  1237.   printHelp();
  1238.   safeKeys(keypress);
  1239.  
  1240. start:
  1241.  
  1242.   keypress = 0;
  1243.  
  1244.   if (count > curFileStruct.totalAmount - 1)
  1245.   {
  1246.     count = 0;
  1247.   }
  1248.  
  1249.   switch (randomPic)
  1250.   {
  1251.   case 0:
  1252.  
  1253.     iddqd = processJson(count, 1, 0);
  1254.     break;
  1255.   case 1:
  1256.     iddqd = processJson(0, 1, 1);
  1257.     break;
  1258.   }
  1259.  
  1260.   OS_SETCOLOR(70);
  1261.  
  1262.   switch (iddqd)
  1263.   {
  1264.   case -3: // return 0 pictures
  1265.     OS_SETGFX(0x86);
  1266.     strcpy(minRating, "1.0");
  1267.     printf("[%u]No picture is returned in query. Minimal rating is set to %s\r\n", curFileStruct.httpErr, minRating);
  1268.     writeLog("[-3]No picture is returned in query. minRating=1.0", "main           ");
  1269.     delayLongKey(2000);
  1270.  
  1271.     goto start;
  1272.   case -4: // return xxxx picture, but empty body.
  1273.     OS_SETGFX(0x86);
  1274.     printf("[%u]Empty body is returned. Next picture(%lu)...\r\n", curFileStruct.httpErr, count);
  1275.     writeLog("[-4]Empty body is returned. Next picture.", "main           ");
  1276.     count++;
  1277.     delayLongKey(2000);
  1278.     goto start;
  1279.   case -1: // return HTTP error != 200
  1280.     OS_SETGFX(0x86);
  1281.     printf("[%u]Error getting pic info. Next picture(%lu)...\r\n", curFileStruct.httpErr, count);
  1282.     writeLog("[-1]Error getting pic info. Next picture.", "main           ");
  1283.     count++;
  1284.     delayLongKey(2000);
  1285.     goto start;
  1286.   }
  1287.   if (verbose)
  1288.   {
  1289.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  1290.     if (idkfa < 0)
  1291.     {
  1292.       OS_SETGFX(0x86);
  1293.       printf("[%u]Error can't parse authorIds(%s).\r\n", curFileStruct.httpErr, curFileStruct.authorIds);
  1294.       strcpy(curFileStruct.authorTitle, "ErrorGet");
  1295.       strcpy(curFileStruct.authorRealName, "Error Getting Name");
  1296.     }
  1297.     printData();
  1298.   }
  1299.  
  1300.   if (strcmp(curFileStruct.picType, "standard") != 0)
  1301.   {
  1302.     OS_SETGFX(0x86);
  1303.     printf("[%u]Error format '%s' not supported. Next picture.\n\r", curFileStruct.httpErr, curFileStruct.picType);
  1304.     count++;
  1305.     delayLongKey(2000);
  1306.     goto start;
  1307.   }
  1308.   sprintf(netbuf, "GET /file/id:%ld%s", iddqd, userAgent);
  1309.   switch (netDriver)
  1310.   {
  1311.   case 0:
  1312.     result = fillPictureNet();
  1313.     break;
  1314.   case 1:
  1315.     result = fillPictureEsp();
  1316.     break;
  1317.   }
  1318.  
  1319.   if (!result) // return HTTP error != 200
  1320.   {
  1321.     OS_SETGFX(0x86);
  1322.     printf("[%u]Error getting pic. Next picture. Incorrect format?\r\n", curFileStruct.httpErr);
  1323.     count++;
  1324.     delayLongKey(2000);
  1325.     goto start;
  1326.   }
  1327.  
  1328.   viewScreen6912c((unsigned int)&picture);
  1329.  
  1330.   if (slideShowTime != 0)
  1331.   {
  1332.     keypress = delayLongKey(slideShowTime * 20);
  1333.   }
  1334.   else
  1335.   {
  1336.     do
  1337.     {
  1338.       YIELD();
  1339.       keypress = OS_GETKEY();
  1340.     } while (keypress == 0);
  1341.   }
  1342.  
  1343.   // OS_SETGFX(0x86);
  1344.  
  1345.   ////// Keys for pictures
  1346.  
  1347.   switch (keypress & 0xdf)
  1348.   {
  1349.   case 'S':
  1350.     OS_SETGFX(0x86);
  1351.     if (!verbose)
  1352.     {
  1353.       idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  1354.       if (idkfa < 0)
  1355.       {
  1356.         printf("[%u]Error can't parse authorIds(%s). Next picture.\r\n", curFileStruct.httpErr, curFileStruct.authorIds);
  1357.         strcpy(curFileStruct.authorTitle, "ErrorGet");
  1358.         strcpy(curFileStruct.authorRealName, "Error Getting Name");
  1359.       }
  1360.     }
  1361.  
  1362.     printf("Saving ");
  1363.  
  1364.     savePic(iddqd);
  1365.     puts("O.K.");
  1366.     count++;
  1367.     break;
  1368.   case 'B':
  1369.   case 216:
  1370.     if (count > 0)
  1371.     {
  1372.       count--;
  1373.     }
  1374.     break;
  1375.   case 0: //' '
  1376.   case 219:
  1377.     count++;
  1378.     break;
  1379.   case 'I':
  1380.     printData();
  1381.     while (OS_GETKEY() == 0)
  1382.     {
  1383.       YIELD();
  1384.     }
  1385.     break;
  1386.   case 'O':
  1387.     showDesc = !showDesc;
  1388.     break;
  1389.  
  1390.   default:
  1391.     OS_SETGFX(0x86);
  1392.     safeKeys(keypress);
  1393.     break;
  1394.   }
  1395.   goto start;
  1396. }
  1397.