Subversion Repositories NedoOS

Rev

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