Subversion Repositories NedoOS

Rev

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