Subversion Repositories NedoOS

Rev

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

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