Subversion Repositories NedoOS

Rev

Rev 2337 | 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 <graphic.h>
  8. #include <../common/terminal.c>
  9. #include <tcp.h>
  10. //
  11. #define true 1
  12. #define false 0
  13.  
  14. unsigned int RBR_THR = 0xf8ef;
  15. unsigned int IER = 0xf9ef;
  16. unsigned int IIR_FCR = 0xfaef;
  17. unsigned int LCR = 0xfbef;
  18. unsigned int MCR = 0xfcef;
  19. unsigned int LSR = 0xfdef;
  20. unsigned int MSR = 0xfeef;
  21. unsigned int SR = 0xffef;
  22. unsigned int divider = 1;
  23. unsigned char comType = 0;
  24. unsigned int espType = 32;
  25.  
  26. unsigned char picture[15000];
  27. unsigned char netbuf[6912];
  28. unsigned char curPath[128];
  29.  
  30. unsigned char minRating[] = "0000000000";
  31. struct fileStruct
  32. {
  33.   long picId;
  34.   unsigned int picYear;
  35.   unsigned long totalAmount;
  36.   unsigned int httpErr;
  37.   unsigned char picRating[8];
  38.   unsigned char picName[256];
  39.   unsigned char picType[64];
  40.   unsigned char authorIds[64];
  41.   unsigned char authorTitle[64];
  42.   unsigned char authorRealName[64];
  43.   unsigned char afn[128];
  44.   unsigned char pfn[128];
  45.   unsigned char fileName[128];
  46. } curFileStruct;
  47.  
  48. struct window
  49. {
  50.   unsigned char x;
  51.   unsigned char y;
  52.   unsigned char w;
  53.   unsigned char h;
  54.   unsigned char text;
  55.   unsigned char back;
  56.   unsigned char tittle[80];
  57. } curWin;
  58.  
  59. struct sockaddr_in dnsaddress;
  60. struct sockaddr_in targetadr;
  61. struct readstructure readStruct;
  62.  
  63. unsigned char ver[] = "4.4";
  64. const unsigned char sendOk[] = "SEND OK";
  65. const unsigned char gotWiFi[] = "WIFI GOT IP";
  66. unsigned char buffer[] = "0000000000";
  67. 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";
  68. unsigned char zxart[] = "zxart.ee";
  69. unsigned char keypress, verbose, randomPic, slideShow, netDriver;
  70.  
  71. unsigned long contLen;
  72. unsigned long count = 0;
  73. unsigned int headlng;
  74. unsigned int slideShowTime = 0;
  75. unsigned int loaded;
  76.  
  77. unsigned char crlf[2] = {13, 10};
  78. unsigned char cmd[512];
  79. unsigned char link[512];
  80. unsigned char fileIdChar[10];
  81.  
  82. void clearStatus(void)
  83. {
  84. }
  85.  
  86. void emptyKeys(void)
  87. {
  88.   unsigned char loop = 0, key;
  89.   do
  90.   {
  91.     key = OS_GETKEY();
  92.     if (loop > 64)
  93.     {
  94.       break;
  95.     }
  96.     loop++;
  97.   } while (key != 0);
  98. }
  99.  
  100. unsigned char delayLongKey(unsigned long counter)
  101. {
  102.   unsigned long start, finish, key;
  103.   counter = counter / 20;
  104.   if (counter < 1)
  105.   {
  106.     counter = 1;
  107.   }
  108.   start = time();
  109.   finish = start + counter;
  110.  
  111.   while (start < finish)
  112.   {
  113.     start = time();
  114.     key = OS_GETKEY();
  115.     if (key != 0)
  116.     {
  117.       return key;
  118.     }
  119.     YIELD();
  120.   }
  121.   return 32;
  122. }
  123.  
  124. void spaces(unsigned char number)
  125. {
  126.   while (number > 0)
  127.   {
  128.     putchar(' ');
  129.     number--;
  130.   }
  131. }
  132. void quit(void)
  133. {
  134.   OS_CLS(0);
  135.   OS_SETGFX(-1);
  136.   exit(0);
  137. }
  138. /*
  139. void infoBox(struct window w, const char *message)
  140. {
  141.   unsigned char wcount, tempx, tittleStart;
  142.  
  143.   w.h++;
  144.   OS_SETXY(w.x, w.y - 1);
  145.   BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  146.   OS_SETXY(w.x, w.y);
  147.   OS_SETCOLOR(w.text);
  148.   putchar(201);
  149.   for (wcount = 0; wcount < w.w; wcount++)
  150.   {
  151.     putchar(205);
  152.   }
  153.   putchar(187);
  154.   OS_SETXY(w.x, w.y + w.h);
  155.   putchar(200);
  156.   for (wcount = 0; wcount < w.w; wcount++)
  157.   {
  158.     putchar(205);
  159.   }
  160.   putchar(188);
  161.  
  162.   tempx = w.x + w.w + 1;
  163.   for (wcount = 1; wcount < w.h; wcount++)
  164.   {
  165.     OS_SETXY(w.x, w.y + wcount);
  166.     putchar(186);
  167.     OS_SETXY(tempx, w.y + wcount);
  168.     putchar(186);
  169.   }
  170.   tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  171.   OS_SETXY(tittleStart, w.y);
  172.   printf("[%s]", w.tittle);
  173.  
  174.   OS_SETXY(w.x + 1, w.y + 1);
  175.   OS_SETCOLOR(w.back);
  176.   tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  177.   OS_SETXY(tittleStart, w.y + 1);
  178.   printf("%s", message);
  179. }
  180. */
  181. void printHelp(void)
  182. {
  183.   OS_SETCOLOR(67);
  184.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  185.   OS_SETCOLOR(6);
  186.   printf("-------------------------------------------------------\n\r");
  187.   printf(" Управление:\n\r");
  188.   printf(" 'ESC' - выход из программы;\n\r");
  189.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  190.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  191.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  192.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  193.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  194.   printf(" 'V' не выводить информацию об авторах\n\r");
  195.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  196.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  197.   printf(" 'D' Переключение режима ZXNETUSB/ESP-COM\n\r");
  198.   printf(" 'T' Продолжительность одного слайда в int-ах \n\r");
  199.   printf(" 'M' Минимальный рейтинг для случайного воспроизведения. \n\r");
  200.   printf(" 'H' Данная справочная информация\n\r");
  201.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  202.   OS_SETCOLOR(70);
  203.   keypress = getchar();
  204.   OS_CLS(0);
  205. }
  206.  
  207. void delay(unsigned long counter)
  208. {
  209.   unsigned long start, finish;
  210.   counter = counter / 20;
  211.   if (counter < 1)
  212.   {
  213.     counter = 1;
  214.   }
  215.   start = time();
  216.   finish = start + counter;
  217.  
  218.   while (start < finish)
  219.   {
  220.     start = time();
  221.   }
  222. }
  223.  
  224. ///////////////////////////
  225. #include <../common/esp-com.c>
  226. #include <../common/network.c>
  227. //////////////////////////
  228.  
  229. int testOperation2(const char *process, int socket)
  230. {
  231.   if (socket < 0)
  232.   {
  233.     printf("%s: [ERROR:", process);
  234.     errorPrint(-socket);
  235.     printf("]\r\n");
  236.     YIELD();
  237.     return -socket;
  238.   }
  239.   return 1;
  240. }
  241.  
  242. int cutHeader(unsigned int todo)
  243. {
  244.   unsigned char *count1;
  245.  
  246.   curFileStruct.httpErr = httpError();
  247.   if (curFileStruct.httpErr != 200)
  248.   {
  249.     clearStatus();
  250.     printf("HTTP response:[%u]", curFileStruct.httpErr);
  251.     return 0;
  252.   }
  253.   count1 = strstr(netbuf, "Content-Length:");
  254.   if (count1 == NULL)
  255.   {
  256.     clearStatus();
  257.     printf("contLen not found");
  258.     contLen = 0;
  259.     curFileStruct.httpErr = 999; // bad kostil
  260.     return 0;
  261.   }
  262.   contLen = atol(count1 + 15);
  263.   // printf("Content-Length: %lu \n\r", contLen);
  264.  
  265.   count1 = strstr(netbuf, "\r\n\r\n");
  266.   if (count1 == NULL)
  267.   {
  268.     clearStatus();
  269.     printf("end of header not found\r\n");
  270.   }
  271.   else
  272.   {
  273.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  274.     // printf("header %u bytes\r\n", headlng);
  275.   }
  276.   return todo - headlng;
  277. }
  278.  
  279. char *str_replace(char *dst, int num, const char *str, const char *orig, const char *rep)
  280. {
  281.   const char *ptr;
  282.   size_t len1 = strlen(orig);
  283.   size_t len2 = strlen(rep);
  284.   char *tmp = dst;
  285.  
  286.   num -= 1;
  287.   while ((ptr = strstr(str, orig)) != NULL)
  288.   {
  289.     num -= (ptr - str) + len2;
  290.     if (num < 1)
  291.       break;
  292.  
  293.     strncpy(dst, str, (size_t)(ptr - str));
  294.     dst += ptr - str;
  295.     strncpy(dst, rep, len2);
  296.     dst += len2;
  297.     str = ptr + len1;
  298.   }
  299.  
  300.   for (; (*dst = *str) && (num > 0); --num)
  301.   {
  302.     ++dst;
  303.     ++str;
  304.   }
  305.   return tmp;
  306. }
  307.  
  308. char fillPictureEsp(void)
  309. {
  310.   unsigned char sizeLink;
  311.   unsigned long downloaded;
  312.   unsigned char byte, countl;
  313.   unsigned int todo;
  314.   const unsigned char *count1;
  315.   unsigned char firstPacket;
  316.   strcpy(link, netbuf);
  317.   sizeLink = strlen(link);
  318.   do
  319.   {
  320.     sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  321.     getAnswer2(); // CONNECT or ERROR or link is not valid
  322.     count1 = strstr(netbuf, "CONNECT");
  323.   } while (count1 == NULL);
  324.  
  325.   getAnswer2();                                   // OK
  326.   sprintf(netbuf, "AT+CIPSEND=%u", sizeLink + 2); // second CRLF in send command
  327.   sendcommand(netbuf);
  328.   getAnswer2();
  329.   do
  330.   {
  331.     byte = uart_readBlock();
  332.     // putchar(byte);
  333.   } while (byte != '>');
  334.   sendcommand(link);
  335.   countl = 0;
  336.   do
  337.   {
  338.     byte = uart_readBlock();
  339.     if (byte == sendOk[countl])
  340.     {
  341.       countl++;
  342.     }
  343.     else
  344.     {
  345.       countl = 0;
  346.     }
  347.   } while (countl < strlen(sendOk));
  348.   uart_readBlock(); // CR
  349.   uart_readBlock(); // LF
  350.   downloaded = 0;
  351.   firstPacket = true;
  352.   do
  353.   {
  354.     headlng = 0;
  355.     todo = recvHead();
  356.     getdataEsp(todo); // Requested size
  357.     if (firstPacket)
  358.     {
  359.       todo = cutHeader(todo);
  360.       firstPacket = false;
  361.       if (curFileStruct.httpErr != 200)
  362.       {
  363.         sendcommand("AT+CIPCLOSE");
  364.         getAnswer2(); // CLOSED
  365.         getAnswer2(); // OK
  366.         return false;
  367.       }
  368.     }
  369.  
  370.     if (downloaded + todo > sizeof(picture))
  371.     {
  372.       printf("dataBuffer overrun... %u reached \n\r", downloaded + todo);
  373.       return false;
  374.     }
  375.     memcpy(picture + downloaded, netbuf + headlng, todo);
  376.     downloaded = downloaded + todo;
  377.   } while (downloaded < contLen);
  378.   sendcommand("AT+CIPCLOSE");
  379.   getAnswer2(); // CLOSED or ERROR
  380.   count1 = strstr(netbuf, "CLOSED");
  381.   if (count1 != NULL)
  382.   {
  383.     getAnswer2(); // OK
  384.   }
  385.   return true;
  386. }
  387.  
  388. char fillPictureNet(void)
  389. {
  390.   int todo;
  391.   unsigned int downloaded = 0;
  392.   unsigned char firstPacket;
  393.   char socket, retry;
  394.   picture[0] = 0;
  395.   retry = 3;
  396.   socket = OpenSock(AF_INET, SOCK_STREAM);
  397.   if (testOperation2("OS_NETSOCKET", socket) != 1)
  398.   {
  399.     getchar();
  400.     quit();
  401.   }
  402.   todo = netConnect(socket, retry);
  403.   if (testOperation2("OS_NETCONNECT", todo) != 1)
  404.   {
  405.     getchar();
  406.     quit();
  407.   }
  408.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf), retry);
  409.   if (testOperation2("OS_WIZNETWRITE", todo) != 1)
  410.   {
  411.     getchar();
  412.     quit();
  413.   }
  414.   firstPacket = true;
  415.   do
  416.   {
  417.     headlng = 0;
  418.     todo = tcpRead(socket, retry);
  419.     testOperation("OS_WIZNETREAD", todo); // Quit if too many retries
  420.  
  421.     if (firstPacket)
  422.     {
  423.       todo = cutHeader(todo);
  424.       firstPacket = false;
  425.       if (curFileStruct.httpErr != 200)
  426.       {
  427.         netShutDown(socket, 0);
  428.         return false;
  429.       }
  430.     }
  431.  
  432.     if (downloaded + todo > sizeof(picture))
  433.     {
  434.       printf("dataBuffer overrun... %u reached \n\r", downloaded + todo);
  435.       return false;
  436.     }
  437.     memcpy(picture + downloaded, netbuf + headlng, todo);
  438.     downloaded = downloaded + todo;
  439.   } while (downloaded != contLen);
  440.  
  441.   netShutDown(socket, 0);
  442.   picture[downloaded + 1] = 0;
  443.   return true;
  444. }
  445.  
  446. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  447. {
  448.  
  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, " ", "_");
  458.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  459.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  460.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  461.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  462.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  463.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  464. }
  465.  
  466. void stringRepair(unsigned char *pfn, unsigned int tSize)
  467. {
  468.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  469.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  470.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  471.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  472.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  473.   str_replace(pfn, tSize, pfn, "\\/", "/");
  474. }
  475.  
  476. void ncReplace(void)
  477. {
  478.   unsigned char len;
  479.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  480.   {
  481.     if ((curFileStruct.afn[len] < ' ') || (curFileStruct.afn[len] > 0xf1) || (curFileStruct.afn[len] > 0xb0 && curFileStruct.afn[len] < 0xdf))
  482.     {
  483.       curFileStruct.afn[len] = '_';
  484.     }
  485.   }
  486.  
  487.   for (len = 0; len < strlen(curFileStruct.pfn); len++)
  488.   {
  489.     if ((curFileStruct.pfn[len] < ' ') || (curFileStruct.pfn[len] > 0xef) || (curFileStruct.pfn[len] > 0xb0 && curFileStruct.pfn[len] < 0xdf))
  490.     {
  491.       curFileStruct.pfn[len] = '_';
  492.     }
  493.   }
  494. }
  495.  
  496. unsigned char savePic(unsigned long fileId)
  497. {
  498.   FILE *fp2;
  499.   unsigned char afnSize, tfnSize;
  500.  
  501.   afnSize = sizeof(curFileStruct.afn) - 1;
  502.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  503.  
  504.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  505.   nameRepair(curFileStruct.afn, afnSize);
  506.  
  507.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  508.   nameRepair(curFileStruct.pfn, tfnSize);
  509.  
  510.   ncReplace();
  511.  
  512.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  513.   if (strlen(curFileStruct.fileName) > 62)
  514.   {
  515.     sprintf(fileIdChar, "-%ld", fileId);
  516.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  517.     curFileStruct.fileName[50] = '\0';
  518.     strcat(curFileStruct.fileName, fileIdChar);
  519.     strcat(curFileStruct.fileName, ".scr");
  520.   }
  521.   OS_SETSYSDRV();
  522.   OS_MKDIR("../downloads");        // Create if not exist
  523.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  524.   OS_CHDIR("../downloads/getpic");
  525.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  526.   if (((int)fp2) & 0xff)
  527.   {
  528.     printf("%s creating error\r\n", curFileStruct.fileName);
  529.     getchar();
  530.     quit();
  531.   }
  532.   OS_WRITEHANDLE(picture, fp2, 6912);
  533.   OS_CLOSEHANDLE(fp2);
  534.   return 0;
  535. }
  536.  
  537. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  538. {
  539.   unsigned int i, j;
  540.   unsigned int lenC, lenS;
  541.  
  542.   for (lenC = 0; c[lenC]; lenC++)
  543.     ;
  544.   for (lenS = 0; s[lenS]; lenS++)
  545.     ;
  546.  
  547.   for (i = startPos; i <= lenS - lenC; i++)
  548.   {
  549.     for (j = 0; s[i + j] == c[j]; j++)
  550.       ;
  551.  
  552.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  553.       return i;
  554.     if (j == lenC)
  555.       if (n - 1)
  556.         n--;
  557.       else
  558.         return i;
  559.   }
  560.   return -1;
  561. }
  562.  
  563. const char *parseJson(unsigned char *property)
  564. {
  565.   unsigned int w, lng, lngp1, findEnd, listPos;
  566.   unsigned char terminator;
  567.   int n;
  568.   // n = -1;
  569.   //  netbuf[0] = '\0';
  570.   n = pos(picture, property, 1, 0);
  571.   if (n == -1)
  572.   {
  573.     strcpy(netbuf, "-");
  574.     // printf("Property %s not found", property);
  575.     return netbuf;
  576.   }
  577.   lng = n - 1 + strlen(property);
  578.   if (picture[lng] == ':')
  579.   {
  580.     terminator = 0;
  581.   }
  582.   if (picture[lng] == '\"')
  583.   {
  584.     terminator = '\"';
  585.   }
  586.   if (picture[lng] == '[')
  587.   {
  588.     terminator = ']';
  589.   }
  590.  
  591.   findEnd = 1;
  592.   lngp1 = lng + 1;
  593.  
  594.   while (42)
  595.   {
  596.  
  597.     if ((picture[lngp1 + findEnd] == ','))
  598.     {
  599.       if (terminator == 0)
  600.       {
  601.         break;
  602.       }
  603.       if ((picture[lng + findEnd] == terminator))
  604.       {
  605.         findEnd--;
  606.         break;
  607.       }
  608.     }
  609.     findEnd++;
  610.   }
  611.   listPos = 0;
  612.   for (w = lngp1; w < findEnd + lngp1; w++)
  613.   {
  614.     netbuf[listPos] = picture[w];
  615.     listPos++;
  616.   }
  617.   netbuf[listPos] = 0;
  618.   return netbuf;
  619. }
  620.  
  621. void convert866(void)
  622. {
  623.   unsigned int lng, targetPos, w, q = 0;
  624.   unsigned char bufferl[8], one, two;
  625.   unsigned int decVal;
  626.   lng = strlen(netbuf);
  627.   targetPos = lng + 1;
  628.  
  629.   while (q < lng)
  630.   {
  631.     one = netbuf[q];
  632.     two = netbuf[q + 1];
  633.     if (one == 92 && two == 117)
  634.     {
  635.       q = q + 2;
  636.       for (w = 0; w < 4; w++)
  637.       {
  638.         bufferl[w] = netbuf[q + w];
  639.       }
  640.       q = q + 4;
  641.       bufferl[4] = '\0';
  642.       decVal = (unsigned int)strtol(bufferl, NULL, 16);
  643.  
  644.       if (decVal < 1088)
  645.       {
  646.         decVal = decVal - 912;
  647.       }
  648.       if (decVal > 1087)
  649.       {
  650.         decVal = decVal - 864;
  651.       }
  652.       if (decVal == 1025)
  653.       {
  654.         decVal = 240;
  655.       }
  656.       if (decVal == 1105)
  657.       {
  658.         decVal = 241;
  659.       }
  660.  
  661.       netbuf[targetPos] = decVal;
  662.     }
  663.     else
  664.     {
  665.       netbuf[targetPos] = netbuf[q];
  666.       q++;
  667.     }
  668.     targetPos++;
  669.   }
  670.   netbuf[targetPos] = 0;
  671.  
  672.   for (w = lng + 1; w < targetPos + 1; w++)
  673.   {
  674.     netbuf[w - lng - 1] = netbuf[w];
  675.   }
  676. }
  677.  
  678. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  679. {
  680.   unsigned int tSize;
  681.   const unsigned char *count1;
  682.   unsigned char result;
  683.   switch (queryNum)
  684.   {
  685.   case 0:
  686.     sprintf(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:%u/start:%lu/order:date,desc%s", limit, startPos, userAgent);
  687.     break;
  688.   case 1:
  689.     sprintf(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=%s;zxPictureType=standard%s", minRating, userAgent);
  690.     break;
  691.   case 99: // GET /jsonElementData/elementId:182798
  692.     sprintf(netbuf, "GET /jsonElementData/elementId:%lu%s", startPos, userAgent);
  693.     break;
  694.   }
  695.  
  696.   switch (netDriver)
  697.   {
  698.   case 0:
  699.     result = fillPictureNet();
  700.     break;
  701.   case 1:
  702.     result = fillPictureEsp();
  703.     break;
  704.   }
  705.  
  706.   if (!result)
  707.   {
  708.     return -1;
  709.   }
  710.  
  711.   count1 = strstr(picture, "responseStatus\":\"success");
  712.   if (count1 == NULL)
  713.   {
  714.     OS_CLS(0);
  715.     OS_SETCOLOR(66);
  716.     puts("Picture[]:");
  717.     puts(picture);
  718.     puts("---------------");
  719.     printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  720.     YIELD();
  721.     getchar();
  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.     OS_CLS(0);
  740.     OS_SETCOLOR(66);
  741.     puts("Picture[]:");
  742.     puts(picture);
  743.     puts("---------------");
  744.     printf("PROCESS JSON: [ERROR: ID not found.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  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("%lu ", 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)
  973.   {
  974.   case 27:
  975.     OS_SETCOLOR(70);
  976.     printf("Good bye...\r\n");
  977.     delayLong(500);
  978.     quit();
  979.     break;
  980.   case 'j':
  981.   case 'J':
  982.     curWin.w = 13;
  983.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  984.     curWin.y = 11;
  985.     curWin.h = 1;
  986.     curWin.text = 103;
  987.     curWin.back = 103;
  988.     strcpy(curWin.tittle, "# of pic:");
  989.     if (inputBox(curWin, ""))
  990.     {
  991.       sscanf(cmd, "%lu", &count);
  992.       if (count > curFileStruct.totalAmount - 1)
  993.       {
  994.         count = curFileStruct.totalAmount - 1;
  995.       }
  996.     }
  997.     break;
  998.   case 't':
  999.   case 'T':
  1000.     curWin.w = 20;
  1001.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1002.     curWin.y = 11;
  1003.     curWin.h = 1;
  1004.     curWin.text = 103;
  1005.     curWin.back = 103;
  1006.     strcpy(curWin.tittle, "Slide time(ints)");
  1007.     if (inputBox(curWin, ""))
  1008.     {
  1009.       sscanf(cmd, "%u", &slideShowTime);
  1010.       OS_CLS(0);
  1011.       OS_SETCOLOR(70);
  1012.       printf("Slide duration set to %u ints.", slideShowTime);
  1013.       delayLong(500);
  1014.     }
  1015.     break;
  1016.   case 'v':
  1017.   case 'V':
  1018.     verbose = !verbose;
  1019.  
  1020.     if (verbose == 0)
  1021.     {
  1022.       BOX(1, 1, 80, 25, 40, ' ');
  1023.       AT(1, 1);
  1024.     }
  1025.     break;
  1026.   case 'h':
  1027.   case 'H':
  1028.     printHelp();
  1029.     break;
  1030.   case 'r':
  1031.   case 'R':
  1032.     randomPic = !randomPic;
  1033.     OS_SETCOLOR(70);
  1034.     if (verbose == 1)
  1035.     {
  1036.       if (randomPic == 1)
  1037.       {
  1038.         printf("    Random mode enabled...\r\n");
  1039.         count = 0;
  1040.         delayLong(500);
  1041.       }
  1042.       else
  1043.       {
  1044.         printf("    Sequental mode enabled...\r\n");
  1045.         count = 0;
  1046.         delayLong(500);
  1047.       }
  1048.     }
  1049.     break;
  1050.   case 'a':
  1051.   case 'A':
  1052.     slideShow = !slideShow;
  1053.     OS_SETCOLOR(70);
  1054.     if (slideShow == 1)
  1055.     {
  1056.       if (verbose == 1)
  1057.         printf("    SlideShow mode enabled...\r\n\r\n");
  1058.       slideShowTime = 150;
  1059.       delayLong(500);
  1060.     }
  1061.     else
  1062.     {
  1063.       if (verbose == 1)
  1064.         printf("    Manual mode enabled...\r\n\r\n");
  1065.       slideShowTime = 0;
  1066.       delayLong(500);
  1067.     }
  1068.     break;
  1069.   case 'd':
  1070.   case 'D':
  1071.     netDriver = !netDriver;
  1072.     OS_SETCOLOR(70);
  1073.     if (netDriver == 1)
  1074.     {
  1075.       printf("    ESP-COM mode enabled...\r\n");
  1076.       loadEspConfig();
  1077.       uart_init(divider);
  1078.       espReBoot();
  1079.       printf("    ESP-COM inited...\r\n");
  1080.       delayLong(500);
  1081.     }
  1082.     else
  1083.     {
  1084.       if (verbose == 1)
  1085.         printf("    NedoNET mode enabled...");
  1086.       delayLong(500);
  1087.     }
  1088.     break;
  1089.   case 'm':
  1090.   case 'M':
  1091.     curWin.w = 22;
  1092.     curWin.x = 80 / 2 - curWin.w / 2 - 2;
  1093.     curWin.y = 1;
  1094.     curWin.h = 1;
  1095.     curWin.text = 103;
  1096.     curWin.back = 103;
  1097.     strcpy(curWin.tittle, "Минимальная оценка:");
  1098.  
  1099.     if (inputBox(curWin, ""))
  1100.     {
  1101.       char counter;
  1102.       for (counter = 0; counter < strlen(cmd); counter++)
  1103.       {
  1104.         if ((((cmd[counter] < '0') || (cmd[counter] > '9'))) && cmd[counter] != '.')
  1105.         {
  1106.           counter = 0;
  1107.           break;
  1108.         }
  1109.       }
  1110.       if (counter != 0)
  1111.       {
  1112.         strncpy(minRating, cmd, 5);
  1113.         count = 0;
  1114.       }
  1115.     }
  1116.   default:
  1117.     break;
  1118.   }
  1119. }
  1120.  
  1121. char readParamFromIni(void)
  1122. {
  1123.   FILE *fpini;
  1124.   unsigned char *count1;
  1125.   const char currentNetwork[] = "currentNetwork";
  1126.   unsigned char curNet = 0;
  1127.  
  1128.   OS_GETPATH((unsigned int)&curPath);
  1129.   OS_SETSYSDRV();
  1130.   OS_CHDIR("/");
  1131.   OS_CHDIR("ini");
  1132.  
  1133.   fpini = OS_OPENHANDLE("network.ini", 0x80);
  1134.   if (((int)fpini) & 0xff)
  1135.   {
  1136.     clearStatus();
  1137.     printf("network.ini not found.\r\n");
  1138.     getchar();
  1139.     return false;
  1140.   }
  1141.  
  1142.   OS_READHANDLE(netbuf, fpini, sizeof(netbuf) - 1);
  1143.   OS_CLOSEHANDLE(fpini);
  1144.  
  1145.   count1 = strstr(netbuf, currentNetwork);
  1146.   if (count1 != NULL)
  1147.   {
  1148.     sscanf(count1 + strlen(currentNetwork) + 1, "%u", &curNet);
  1149.   }
  1150.  
  1151.   OS_CHDIR(curPath);
  1152.   return curNet;
  1153. }
  1154.  
  1155. void init(void)
  1156. {
  1157.   OS_SETSYSDRV();
  1158.   OS_MKDIR("../downloads");        // Create if not exist
  1159.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  1160.   OS_CHDIR("../downloads/getpic");
  1161.  
  1162.   count = 0;
  1163.   verbose = 1;
  1164.   randomPic = 0;
  1165.   slideShow = 0;
  1166.   strcpy(minRating, "4.0");
  1167.  
  1168.   targetadr.family = AF_INET;
  1169.   targetadr.porth = 00;
  1170.   targetadr.portl = 80;
  1171.   targetadr.b1 = 217; // D9
  1172.   targetadr.b2 = 146; // 92
  1173.   targetadr.b3 = 69;  // 45
  1174.   targetadr.b4 = 13;  // 0D
  1175.  
  1176.   netDriver = readParamFromIni();
  1177.  
  1178.   if (netDriver == 0)
  1179.   {
  1180.     get_dns();
  1181.     clearStatus();
  1182.     dnsResolve("zxart.ee");
  1183.   }
  1184.  
  1185.   if (netDriver == 1)
  1186.   {
  1187.     loadEspConfig();
  1188.     uart_init(divider);
  1189.     espReBoot();
  1190.     OS_CLS(0);
  1191.   }
  1192. }
  1193.  
  1194. unsigned char viewScreen6912c(unsigned int bufAdr)
  1195. {
  1196.   unsigned char key;
  1197.   OS_SETBORDER(0);
  1198.   OS_SETGFX(0x83);
  1199.   SETPG32KHIGH(OS_GETSCR0() >> 8);
  1200.   memcpy((unsigned char *)(0xc000), (unsigned char *)(bufAdr), 6912);
  1201.  
  1202.   if (slideShowTime != 0)
  1203.   {
  1204.     key = delayLongKey(slideShowTime * 20);
  1205.   }
  1206.   else
  1207.   {
  1208.     key = getchar();
  1209.   }
  1210.   OS_SETGFX(0x86);
  1211.   return key;
  1212. }
  1213.  
  1214. C_task main(void)
  1215. {
  1216.   long iddqd, idkfa;
  1217.   char result;
  1218.  
  1219.   OS_HIDEFROMPARENT();
  1220.   OS_SETGFX(0x86);
  1221.   OS_CLS(0);
  1222.  
  1223.   init();
  1224.  
  1225.   printHelp();
  1226.   safeKeys(keypress);
  1227.  
  1228. start:
  1229.   keypress = 0;
  1230.   switch (randomPic)
  1231.   {
  1232.   case 0:
  1233.  
  1234.     iddqd = processJson(count, 1, 0);
  1235.     break;
  1236.   case 1:
  1237.     iddqd = processJson(0, 1, 1);
  1238.     break;
  1239.   }
  1240.  
  1241.   OS_SETCOLOR(70);
  1242.  
  1243.   switch (iddqd)
  1244.   {
  1245.   case -3: // return 0 pictures
  1246.     strcpy(minRating, "1.0");
  1247.     printf("[%u]No picture is returned in query. Minimal rating is set to %s\r\n", curFileStruct.httpErr, minRating);
  1248.     delayLong(500);
  1249.     goto start;
  1250.   case -4: // return xxxx picture, but empty body.
  1251.     printf("[%u]Empty body is returned. Next picture, please.(%ld)...\r\n", curFileStruct.httpErr, iddqd);
  1252.     delayLong(500);
  1253.     count++;
  1254.     goto start;
  1255.   case -1: // return HTTP error != 200
  1256.     printf("[%u]Error getting pic info. Next picture, please(%ld)...\r\n", curFileStruct.httpErr, iddqd);
  1257.     count++;
  1258.     delayLong(500);
  1259.     goto start;
  1260.   }
  1261.  
  1262.   if (verbose == 1)
  1263.   {
  1264.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  1265.     if (idkfa < 0)
  1266.     {
  1267.       printf("[%u]Error can't parse authorIds(%s). Next picture, please...\r\n", curFileStruct.httpErr, curFileStruct.authorIds);
  1268.       count++;
  1269.       delayLong(500);
  1270.       goto start;
  1271.     }
  1272.   }
  1273.   if (strcmp(curFileStruct.picType, "standard") != 0)
  1274.   {
  1275.     printf("[%u]Error format '%s' not supported. Next picture, please.\n\r", curFileStruct.httpErr, curFileStruct.picType);
  1276.     delayLong(500);
  1277.     count++;
  1278.     goto start;
  1279.   }
  1280.   sprintf(netbuf, "GET /file/id:%ld%s", iddqd, userAgent);
  1281.   switch (netDriver)
  1282.   {
  1283.   case 0:
  1284.     result = fillPictureNet();
  1285.     break;
  1286.   case 1:
  1287.     result = fillPictureEsp();
  1288.     break;
  1289.   }
  1290.  
  1291.   if (result == -1) // return HTTP error != 200
  1292.   {
  1293.     printf("[%u]Error getting pic. Next picture, please...\r\n", curFileStruct.httpErr);
  1294.     count++;
  1295.     delayLong(500);
  1296.     goto start;
  1297.   }
  1298.  
  1299. review:
  1300.   OS_CLS(0);
  1301.   YIELD();
  1302.   // keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  1303.   keypress = viewScreen6912c((unsigned int)&picture);
  1304.   emptyKeys();
  1305.  
  1306.   ///// Keys only for pictures
  1307.   if (keypress == 's' || keypress == 'S')
  1308.   {
  1309.     savePic(iddqd);
  1310.     printf("%s saved.", curFileStruct.fileName);
  1311.     delayLong(500);
  1312.     count++;
  1313.   }
  1314.  
  1315.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1316.   {
  1317.     if (count > 0)
  1318.     {
  1319.       count--;
  1320.     }
  1321.   }
  1322.   if (keypress == 251 || keypress == 32)
  1323.   {
  1324.     count++;
  1325.     goto start;
  1326.   }
  1327.   if (keypress == 'i' || keypress == 'I')
  1328.   {
  1329.     printData();
  1330.     while (OS_GETKEY() == 0)
  1331.     {
  1332.     }
  1333.     goto review;
  1334.   }
  1335.   safeKeys(keypress);
  1336.   goto start;
  1337. }
  1338.