?login_element?

Subversion Repositories NedoOS

Rev

Rev 2046 | Rev 2050 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <oscalls.h>
  6. #include <osfs.h>
  7. #include <intrz80.h>
  8. #include <ctype.h>
  9. #include <tcp.h>
  10. #include <graphic.h>
  11. #include <terminal.c>
  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 char comType = 0;
  23. unsigned int espType = 32;
  24.  
  25. struct fileStruct
  26. {
  27.   long picId;
  28.   unsigned int picYear;
  29.   unsigned long totalAmount;
  30.   unsigned char picRating[8];
  31.   unsigned char picName[256];
  32.   unsigned char picType[64];
  33.   unsigned char authorIds[64];
  34.   unsigned char authorTitle[64];
  35.   unsigned char authorRealName[64];
  36.   unsigned char afn[128];
  37.   unsigned char pfn[128];
  38.   unsigned char fileName[128];
  39. } curFileStruct;
  40.  
  41. unsigned char ver[] = "2.6";
  42. unsigned char netbuf[4096];
  43. unsigned char picture[16384];
  44. unsigned char crlf[2] = {13, 10};
  45. unsigned char keypress, verbose, randomPic, slideShow, netDriver;
  46. struct sockaddr_in targetadr;
  47. struct readstructure readStruct;
  48. unsigned long contLen;
  49. unsigned long count;
  50. unsigned int headlng;
  51. unsigned int slideShowTime = 0;
  52. unsigned int loaded;
  53.  
  54. struct packetStruct
  55. {
  56.   unsigned long contLen;
  57.   unsigned int headlng;
  58. } pack;
  59.  
  60. void emptyKeys(void)
  61. {
  62.   unsigned char loop, key;
  63.   do
  64.   {
  65.     key = _low_level_get();
  66.     if (loop > 64)
  67.     {
  68.       break;
  69.     }
  70.     loop++;
  71.   } while (key != 0);
  72. }
  73.  
  74. void printHelp(void)
  75. {
  76.   ATRIB(95);
  77.   printf("   GETPIC [%s] zxart.ee picture viewer for nedoNET\n\r", ver);
  78.   ATRIB(33);
  79.   ATRIB(40);
  80.   printf("-------------------------------------------------------\n\r");
  81.   printf(" Управление:\n\r");
  82.   printf(" 'ESC' - выход из программы;\n\r");
  83.   printf(" '<-' или 'B' к последним картинкам;\n\r");
  84.   printf(" '->' или 'Пробел' к более старым картинкам\n\r");
  85.   printf(" 'J' Прыжок на  указанную по счету картинку\n\r");
  86.   printf(" 'I' Просмотр экрана информации о картинках\n\r");
  87.   printf(" 'S' Сохранить картинку на диск в текущую папку\n\r");
  88.   printf(" 'V' не выводить информацию об авторах\n\r");
  89.   printf(" 'R' переход в режим  случайная картинка с рейтингом 4+\n\r");
  90.   printf(" 'A' переход в режим  слайд-шоу\n\r");
  91.   printf(" 'D' Переключение режима ZXNETUSB/ESP32\n\r");
  92.   printf(" 'H' Данная справочная информация\n\r");
  93.   printf("-----------------Нажмите любую кнопку------------------\n\r");
  94.   ATRIB(93);
  95.   keypress = getchar();
  96. }
  97. void delay(unsigned long counter)
  98. {
  99.   unsigned long start, finish;
  100.   counter = counter / 20;
  101.   if (counter < 1)
  102.   {
  103.     counter = 1;
  104.   }
  105.   start = time();
  106.   finish = start + counter;
  107.  
  108.   while (start < finish)
  109.   {
  110.     start = time();
  111.   }
  112. }
  113.  
  114. unsigned int httpError(void)
  115. {
  116.   unsigned char *httpRes;
  117.   unsigned int httpErr;
  118.   httpRes = strstr(netbuf, "HTTP/1.1 ");
  119.  
  120.   if (httpRes != NULL)
  121.   {
  122.     httpErr = atol(httpRes + 9);
  123.   }
  124.   else
  125.   {
  126.     httpErr = 0;
  127.   }
  128.   return httpErr;
  129. }
  130.  
  131. void errorPrint(unsigned int error)
  132. {
  133.   switch (error)
  134.   {
  135.   case 2:
  136.     printf("02 SHUT_RDWR");
  137.     break;
  138.   case 4:
  139.     printf("04 ERR_INTR");
  140.     break;
  141.   case 23:
  142.     printf("23 ERR_NFILE");
  143.     break;
  144.   case 35:
  145.     printf("35 ERR_EAGAIN");
  146.     break;
  147.   case 37:
  148.     printf("37 ERR_ALREADY");
  149.     break;
  150.   case 38:
  151.     printf("38 ERR_NOTSOCK");
  152.     break;
  153.   case 40:
  154.     printf("40 ERR_EMSGSIZE");
  155.     break;
  156.   case 41:
  157.     printf("41 ERR_PROTOTYPE");
  158.     break;
  159.   case 47:
  160.     printf("47 ERR_AFNOSUPPORT");
  161.     break;
  162.   case 53:
  163.     printf("53 ERR_ECONNABORTED");
  164.     break;
  165.   case 54:
  166.     printf("54 ERR_CONNRESET");
  167.     break;
  168.   case 57:
  169.     printf("57 ERR_NOTCONN");
  170.     break;
  171.   case 65:
  172.     printf("65 ERR_HOSTUNREACH");
  173.     break;
  174.   default:
  175.     printf("%u UNKNOWN ERROR", error);
  176.     break;
  177.   }
  178. }
  179.  
  180. signed char OpenSock(unsigned char family, unsigned char protocol)
  181. {
  182.   signed char socket;
  183.   unsigned int todo;
  184.   todo = OS_NETSOCKET((family << 8) + protocol);
  185.   if (todo > 32767)
  186.   {
  187.     printf("OS_NETSOCKET: [ERROR:");
  188.     errorPrint(todo & 255);
  189.     printf("] Press any key.");
  190.     getchar();
  191.     exit(0);
  192.   }
  193.   else
  194.   {
  195.     socket = ((todo & 65280) >> 8);
  196.     // printf("OS_NETSOCKET: Socket #%d created\n\r", socket);
  197.   }
  198.   return socket;
  199. }
  200.  
  201. signed char netShutDown(signed char socket, unsigned char type)
  202. {
  203.   unsigned int todo;
  204.   todo = OS_NETSHUTDOWN(socket, type);
  205.   if (todo > 32767)
  206.   {
  207.     printf("OS_NETSHUTDOWN: [ERROR:");
  208.     errorPrint(todo & 255);
  209.     printf("] Press any key.");
  210.     return -1;
  211.   }
  212.   else
  213.   {
  214.     // printf("Socket #%d closed.\n\r", socket);
  215.   }
  216.   return 1;
  217. }
  218.  
  219. unsigned char netConnect(signed char socket)
  220. {
  221.   unsigned int todo, retry = 10;
  222.  
  223.   targetadr.family = AF_INET;
  224.   targetadr.porth = 00;
  225.   targetadr.portl = 80;
  226.   targetadr.b1 = 217;
  227.   targetadr.b2 = 146;
  228.   targetadr.b3 = 69;
  229.   targetadr.b4 = 13;
  230.   while (retry != 0)
  231.   {
  232.     todo = OS_NETCONNECT(socket, &targetadr);
  233.  
  234.     if (todo > 32767)
  235.     {
  236.       retry--;
  237.       printf("OS_NETCONNECT [ERROR:");
  238.       errorPrint(todo & 255);
  239.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  240.       YIELD();
  241.       netShutDown(socket, 0);
  242.       socket = OpenSock(AF_INET, SOCK_STREAM);
  243.     }
  244.     else
  245.     {
  246.       // printf("OS_NETCONNECT: connection successful, %u\n\r", (todo & 255));
  247.       return 1;
  248.     }
  249.   }
  250.   getchar();
  251.   exit(0);
  252.   return 0;
  253. }
  254.  
  255. unsigned int tcpSend(signed char socket, unsigned int messageadr, unsigned int size)
  256. {
  257.   unsigned char retry = 10;
  258.   unsigned int todo;
  259.   readStruct.socket = socket;
  260.   readStruct.BufAdr = messageadr;
  261.   readStruct.bufsize = size;
  262.   readStruct.protocol = SOCK_STREAM;
  263.   while (retry > 0)
  264.   {
  265.     todo = OS_WIZNETWRITE(&readStruct);
  266.     if (todo > 32767)
  267.     {
  268.       printf("OS_WIZNETWRITE: [ERROR:");
  269.       errorPrint(todo & 255);
  270.       printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  271.       YIELD();
  272.       retry--;
  273.     }
  274.     else
  275.     {
  276.       // printf("OS_WIZNETWRITE: %u bytes written. \n\r", todo);
  277.       return todo;
  278.     }
  279.   }
  280.  
  281.   getchar();
  282.   exit(0);
  283.   return todo;
  284. }
  285.  
  286. unsigned int tcpRead(signed char socket)
  287. {
  288.   unsigned char retry = 10;
  289.   unsigned int todo;
  290.  
  291.   readStruct.socket = socket;
  292.   readStruct.BufAdr = (unsigned int)&netbuf;
  293.   readStruct.bufsize = sizeof(netbuf);
  294.   readStruct.protocol = SOCK_STREAM;
  295.  
  296.   while (retry > 0)
  297.   {
  298.     todo = OS_WIZNETREAD(&readStruct);
  299.  
  300.     if (todo > 32767)
  301.     {
  302.       if ((todo & 255) != ERR_EAGAIN)
  303.       {
  304.         printf("OS_WIZNETREAD: [ERROR:");
  305.         errorPrint(todo & 255);
  306.         printf("] [Retry:%u] [Pic:%lu]\r\n", retry, count);
  307.         YIELD();
  308.         retry--;
  309.       }
  310.     }
  311.     else
  312.     {
  313.       // printf("OS_WIZNETREAD: %u bytes read. \n\r", todo);
  314.       return todo;
  315.     }
  316.   }
  317.   getchar();
  318.   exit(0);
  319.   return todo;
  320. }
  321. unsigned int cutHeader(unsigned int todo)
  322. {
  323.   unsigned int recAmount, err;
  324.   unsigned char *count1;
  325.  
  326.   err = httpError();
  327.   if (err != 200)
  328.   {
  329.     printf("\r\nHTTP response:[%u]\r\n", err);
  330.     printf("^^^^^^^^^^^^^^^^^^^^^\r\n");
  331.     puts(netbuf);
  332.     getchar();
  333.   }
  334.   count1 = strstr(netbuf, "Content-Length:");
  335.   if (count1 == NULL)
  336.   {
  337.     printf("contLen  not found \r\n");
  338.     contLen = 0;
  339.   }
  340.   else
  341.   {
  342.     contLen = atol(count1 + 15);
  343.     // printf("Content-Length: %lu \n\r", contLen);
  344.   }
  345.  
  346.   count1 = strstr(netbuf, "\r\n\r\n");
  347.   if (count1 == NULL)
  348.   {
  349.     printf("header not found\r\n");
  350.   }
  351.   else
  352.   {
  353.     headlng = ((unsigned int)count1 - (unsigned int)netbuf + 4);
  354.     recAmount = todo - headlng;
  355.     // printf("header %u bytes\r\n", headlng);
  356.   }
  357.   return recAmount;
  358. }
  359.  
  360. ////////////////////////ESP32 PROCEDURES//////////////////////
  361. void uart_write(unsigned char data)
  362. {
  363.   while ((input(LSR) & 64) == 0)
  364.   {
  365.   }
  366.   output(RBR_THR, data);
  367. }
  368.  
  369. void uart_setrts(unsigned char mode)
  370. {
  371.   switch (mode)
  372.   {
  373.   case 1:
  374.     output(MCR, 2);
  375.     break;
  376.   case 0:
  377.     output(MCR, 0);
  378.     break;
  379.   default:
  380.     disable_interrupt();
  381.     output(MCR, 2);
  382.     output(MCR, 0);
  383.     enable_interrupt();
  384.   }
  385. }
  386.  
  387. void uart_init(unsigned char divisor)
  388. {
  389.   printf("\r\n Initing UART [divider:%u]\r\n", divisor);
  390.   output(MCR, 0x00);        // Disable input
  391.   output(IIR_FCR, 0x87);    // Enable fifo 8 level, and clear it
  392.   output(LCR, 0x83);        // 8n1, DLAB=1
  393.   output(RBR_THR, divisor); // 115200 (divider 1-115200, 3 - 38400)
  394.   output(IER, 0x00);        // (divider 0). Divider is 16 bit, so we get (#0002 divider)
  395.   output(LCR, 0x03);        // 8n1, DLAB=0
  396.   output(IER, 0x00);        // Disable int
  397.   output(MCR, 0x2f);        // Enable AFE
  398.   uart_setrts(0);
  399. }
  400.  
  401. unsigned char uart_hasByte(void)
  402. {
  403.   return (1 & input(LSR));
  404. }
  405.  
  406. unsigned char uart_read(void)
  407. {
  408.   uart_setrts(2);
  409.   return input(RBR_THR);
  410. }
  411.  
  412. unsigned char uart_readBlock(void)
  413. {
  414.   while (uart_hasByte() == 0)
  415.   {
  416.     uart_setrts(2);
  417.   }
  418.   return input(RBR_THR);
  419. }
  420.  
  421. void uart_flush(void)
  422. {
  423.   unsigned int count;
  424.   for (count = 0; count < 6000; count++)
  425.   {
  426.     uart_setrts(1);
  427.     uart_read();
  428.   }
  429.   printf("\r\nBuffer cleared.\r\n");
  430. }
  431. void getdata(unsigned int counted)
  432. {
  433.   unsigned int counter;
  434.   for (counter = 0; counter < counted; counter++)
  435.   {
  436.     netbuf[counter] = uart_readBlock();
  437.   }
  438.   netbuf[counter] = 0;
  439. }
  440.  
  441. void sendcommand(char *commandline)
  442. {
  443.   unsigned int count, cmdLen;
  444.   cmdLen = strlen(commandline);
  445.   for (count = 0; count < cmdLen; count++)
  446.   {
  447.     uart_write(commandline[count]);
  448.   }
  449.   uart_write('\r');
  450.   uart_write('\n');
  451.   //printf("Sended:[%s] \r\n", commandline);
  452. }
  453. unsigned char getAnswer(unsigned char skip)
  454. {
  455.   unsigned char readbyte;
  456.   unsigned int curPos = 0;
  457.   while (skip != 0)
  458.   {
  459.     uart_readBlock();
  460.     skip--;
  461.   }
  462.   while (42)
  463.   {
  464.     readbyte = uart_readBlock();
  465.     if (readbyte == 0x0a)
  466.     {
  467.       break;
  468.     }
  469.     netbuf[curPos] = readbyte;
  470.     curPos++;
  471.   }
  472.   netbuf[curPos - 1] = 0;
  473.   //printf("Answer:[%s]\r\n", netbuf);
  474.   //getchar();
  475.   return curPos;
  476. }
  477. void espReBoot(void)
  478. {
  479.   unsigned char byte, count;
  480.   const unsigned char gotWiFi[] = "WIFI GOT IP";
  481.   uart_flush();
  482.   sendcommand("AT+RST");
  483.   printf("Resetting ESP...");
  484.   do
  485.   {
  486.     byte = uart_readBlock();
  487.     if (byte == gotWiFi[count])
  488.     {
  489.       count++;
  490.     }
  491.     else
  492.     {
  493.       count = 0;
  494.     }
  495.   } while (count < strlen(gotWiFi));
  496.   uart_readBlock(); // CR
  497.   uart_readBlock(); // LN
  498.   puts("Reset complete.\r\n");
  499.  
  500.   sendcommand("ATE0");
  501.   do
  502.   {
  503.     byte = uart_read();
  504.   } while (byte != 'K'); // OK
  505.   // puts("Answer:[OK]");
  506.   uart_readBlock(); // CR
  507.   uart_readBlock(); // LN
  508.  
  509.   sendcommand("AT+CIPCLOSE");
  510.   getAnswer(2);
  511.   sendcommand("AT+CIPDINFO=0");
  512.   getAnswer(2);
  513.   sendcommand("AT+CIPMUX=0");
  514.   getAnswer(2);
  515.   sendcommand("AT+CIPSERVER=0");
  516.   getAnswer(2);
  517.   sendcommand("AT+CIPRECVMODE=0");
  518.   getAnswer(2);
  519. }
  520. unsigned int recvHead(void)
  521. {
  522.   unsigned char byte, dataRead = 0;
  523.  
  524.   do
  525.   {
  526.     byte = uart_readBlock();
  527.     netbuf[dataRead] = byte;
  528.     dataRead++;
  529.   } while (byte != ':');
  530.   netbuf[dataRead] = 0;
  531.   loaded = atoi(netbuf + 5); // <actual_len>
  532.   // printf("\r\n loaded %u\r\n", loaded);
  533.   return loaded;
  534. }
  535.  
  536. // in netbuf data to send
  537. unsigned int fillPictureEsp(void)
  538. {
  539.   unsigned int packSize = 2000;
  540.   unsigned char cmd[256];
  541.   unsigned char link[512];
  542.   unsigned char sizeLink;
  543.   unsigned long toDownload, downloaded;
  544.   unsigned char byte;
  545.   unsigned int dataSize;
  546.   unsigned char skipHeader;
  547.   strcpy(link, netbuf);
  548.   // strcat(link, "\r\n");
  549.   sizeLink = strlen(link);
  550.   sendcommand("AT+CIPSTART=\"TCP\",\"zxart.ee\",80");
  551.  
  552.   getAnswer(0); // CONNECT
  553.   getAnswer(2); // OK
  554.  
  555.   strcpy(cmd, "AT+CIPSEND=");
  556.   sprintf(netbuf, "%u", sizeLink + 2); // second CRLF in send command
  557.   strcat(cmd, netbuf);
  558.   sendcommand(cmd);
  559.   getAnswer(2); // OK - ESP8266
  560.   byte = 0;
  561.   while (byte != '>')
  562.   {
  563.     byte = uart_readBlock();
  564.     // putchar(byte);
  565.   }
  566.   sendcommand(link);
  567.  
  568.   if (espType == 32)
  569.   {
  570.     getAnswer(2); // Recv 132 bytes
  571.     getAnswer(2); // SEND OK
  572.     getAnswer(0); // CRLF
  573.   }
  574.   else
  575.   {
  576.     getAnswer(3); // Recv 132 bytes
  577.     getAnswer(2); // SEND OK
  578.     getAnswer(0); // CRLF
  579.   }
  580.  
  581.   skipHeader = 0;
  582.   downloaded = 0;
  583.  
  584.   do
  585.   {
  586.     headlng = 0;
  587.     dataSize = recvHead();
  588.     getdata(dataSize); // Requested size
  589.     if (skipHeader == 0)
  590.     {
  591.       dataSize = cutHeader(dataSize);
  592.       toDownload = contLen;
  593.       skipHeader = 1;
  594.     }
  595.     downloaded = downloaded + dataSize;
  596.     memcpy(picture + downloaded - dataSize, netbuf + headlng, dataSize);
  597.     toDownload = toDownload - dataSize;
  598.     if (toDownload > 0)
  599.     {
  600.       if (espType == 32)
  601.       {
  602.         getAnswer(2);
  603.       }
  604.       else
  605.       {
  606.         getAnswer(0); // CRLF
  607.       }
  608.     }
  609.   } while (toDownload > 0);
  610.   sendcommand("AT+CIPCLOSE");
  611.  
  612.   if (espType == 32)
  613.   {
  614.     getAnswer(2);
  615.   } // CLOSED
  616.   else
  617.   {
  618.     getAnswer(0);
  619.   } // CLOSED
  620.  
  621.   getAnswer(2); // OK
  622.   return 0;
  623. }
  624. unsigned char getPicEsp(unsigned long fileId)
  625. {
  626.   unsigned char buffer[] = "0000000000";
  627.   netbuf[0] = '\0';
  628.   sprintf(buffer, "%lu", fileId);
  629.   strcat(netbuf, "GET /file/id:");
  630.   strcat(netbuf, buffer);
  631.   strcat(netbuf, " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0");
  632.   fillPictureEsp();
  633.   return 0;
  634. }
  635.  
  636. void loadEspConfig(void)
  637. {
  638.   unsigned char curParam[256];
  639.   unsigned char res;
  640.   FILE *espcom;
  641.   OS_SETSYSDRV();
  642.   OS_CHDIR("browser");
  643.   espcom = OS_OPENHANDLE("espcom.ini", 0x80);
  644.   if (((int)espcom) & 0xff)
  645.   {
  646.     printf("mrfesp.ini opening error\r\n");
  647.     return;
  648.   }
  649.  
  650.   OS_READHANDLE(curParam, espcom, 256);
  651.  
  652.   res = sscanf(curParam, "%x %x %x %x %x %x %x %x %u %u %u", &RBR_THR, &IER, &IIR_FCR, &LCR, &MCR, &LSR, &MSR, &SR, &divider, &comType, &espType);
  653.   puts("Config loaded:");
  654.   printf("     RBR_THR:0x%4x\r\n     IER    :0x%4x\r\n     IIR_FCR:0x%4x\r\n     LCR    :0x%4x\r\n", RBR_THR, IER, IIR_FCR, LCR);
  655.   printf("     MCR    :0x%4x\r\n     LSR    :0x%4x\r\n     MSR    :0x%4x\r\n     SR     :0x%4x\r\n", MCR, LSR, MSR, SR);
  656.   printf("     DIVIDER:  %4u\r\n     TYPE   :  %4u\r\n     ESP    : %u\r\n(", divider, comType, espType);
  657.  
  658.   switch (comType)
  659.   {
  660.   case 0:
  661.     puts("16550 like w/o AFC)");
  662.     break;
  663.   case 1:
  664.     puts("ATM Turbo 2+)");
  665.     break;
  666.   case 2:
  667.     puts("16550 with AFC)");
  668.   default:
  669.     puts("Unknown type)");
  670.     break;
  671.   }
  672. }
  673.  
  674. ////////////////////////ESP32 PROCEDURES//////////////////////
  675.  
  676. char *str_replace(char *dst, int num, const char *str,
  677.                   const char *orig, const char *rep)
  678. {
  679.   const char *ptr;
  680.   size_t len1 = strlen(orig);
  681.   size_t len2 = strlen(rep);
  682.   char *tmp = dst;
  683.  
  684.   num -= 1;
  685.   while ((ptr = strstr(str, orig)) != NULL)
  686.   {
  687.     num -= (ptr - str) + len2;
  688.     if (num < 1)
  689.       break;
  690.  
  691.     strncpy(dst, str, (size_t)(ptr - str));
  692.     dst += ptr - str;
  693.     strncpy(dst, rep, len2);
  694.     dst += len2;
  695.     str = ptr + len1;
  696.   }
  697.  
  698.   for (; (*dst = *str) && (num > 0); --num)
  699.   {
  700.     ++dst;
  701.     ++str;
  702.   }
  703.   return tmp;
  704. }
  705.  
  706. void fillPicture(signed char socket)
  707. {
  708.   unsigned int todo, w, pPos, headskip;
  709.   headskip = 0;
  710.   pPos = 0;
  711.   while (42)
  712.   {
  713.     headlng = 0;
  714.     todo = tcpRead(socket);
  715.     if (todo == 0)
  716.     {
  717.       break;
  718.     }
  719.     if (headskip == 0)
  720.     {
  721.       headskip = 1;
  722.       todo = cutHeader(todo);
  723.     }
  724.  
  725.     if (pPos + todo > sizeof(picture))
  726.     {
  727.       printf("dataBuffer overrun... %u reached \n\r", pPos + todo);
  728.       break;
  729.     }
  730.     for (w = 0; w < todo; w++)
  731.     {
  732.       picture[w + pPos] = netbuf[w + headlng];
  733.     }
  734.  
  735.     pPos = pPos + todo;
  736.     if (pPos == contLen)
  737.     {
  738.       break;
  739.     }
  740.   }
  741.   netShutDown(socket, 0);
  742. }
  743. void nameRepair(unsigned char *pfn, unsigned int tfnSize)
  744. {
  745.  
  746.   str_replace(pfn, tfnSize, pfn, "\\", "_");
  747.   str_replace(pfn, tfnSize, pfn, "/", "_");
  748.   str_replace(pfn, tfnSize, pfn, ":", "_");
  749.   str_replace(pfn, tfnSize, pfn, "*", "_");
  750.   str_replace(pfn, tfnSize, pfn, "?", "_");
  751.   str_replace(pfn, tfnSize, pfn, "<", "_");
  752.   str_replace(pfn, tfnSize, pfn, ">", "_");
  753.   str_replace(pfn, tfnSize, pfn, "|", "_");
  754.   str_replace(pfn, tfnSize, pfn, " ", "_");
  755.   str_replace(pfn, tfnSize, pfn, "&#039;", "'");
  756.   str_replace(pfn, tfnSize, pfn, "&amp;", "&");
  757.   str_replace(pfn, tfnSize, pfn, "&quot;", "'");
  758.   str_replace(pfn, tfnSize, pfn, "&gt;", ")");
  759.   str_replace(pfn, tfnSize, pfn, "&lt;", "(");
  760.   str_replace(pfn, tfnSize, pfn, "\"", "'");
  761. }
  762.  
  763. void stringRepair(unsigned char *pfn, unsigned int tSize)
  764. {
  765.   str_replace(pfn, tSize, pfn, "&#039;", "'");
  766.   str_replace(pfn, tSize, pfn, "&amp;", "&");
  767.   str_replace(pfn, tSize, pfn, "&gt;", ">");
  768.   str_replace(pfn, tSize, pfn, "&lt;", "<");
  769.   str_replace(pfn, tSize, pfn, "&quot;", "\"");
  770.   str_replace(pfn, tSize, pfn, "\\/", "/");
  771. }
  772.  
  773. unsigned char getPic(unsigned long fileId)
  774. {
  775.   unsigned int todo;
  776.   unsigned char buffer[] = "0000000000";
  777.   signed char socket;
  778.   socket = OpenSock(AF_INET, SOCK_STREAM);
  779.   todo = netConnect(socket);
  780.   sprintf(buffer, "%lu", fileId);
  781.   strcpy(netbuf, "GET /file/id:");
  782.   strcat(netbuf, buffer);
  783.   strcat(netbuf, " HTTP/1.1\r\nHost: zxart.ee\r\nUser-Agent: User-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS)\r\n\r\n\0");
  784.   todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  785.   fillPicture(socket);
  786.   return 0;
  787. }
  788.  
  789. void ncReplace(void)
  790. {
  791.   unsigned char len;
  792.   for (len = 0; len < strlen(curFileStruct.afn); len++)
  793.   {
  794.     if ((curFileStruct.afn[len] < ' ') || (curFileStruct.afn[len] > 0xef) || (curFileStruct.afn[len] > 0x7e && curFileStruct.afn[len] < 0xb0))
  795.     {
  796.       curFileStruct.afn[len] = '_';
  797.     }
  798.   }
  799.  
  800.   for (len = 0; len < strlen(curFileStruct.pfn); len++)
  801.   {
  802.     if ((curFileStruct.pfn[len] < ' ') || (curFileStruct.pfn[len] > 0xef) || (curFileStruct.pfn[len] > 0x7e && curFileStruct.pfn[len] < 0xb0))
  803.     {
  804.       curFileStruct.pfn[len] = '_';
  805.     }
  806.   }
  807. }
  808.  
  809. unsigned char savePic(unsigned long fileId)
  810. {
  811.   FILE *fp2;
  812.   unsigned char afnSize, tfnSize;
  813.   unsigned char fileIdChar[10];
  814.  
  815.   afnSize = sizeof(curFileStruct.afn) - 1;
  816.   tfnSize = sizeof(curFileStruct.pfn) - 1;
  817.  
  818.   strcpy(curFileStruct.afn, curFileStruct.authorTitle);
  819.   nameRepair(curFileStruct.afn, afnSize);
  820.  
  821.   strcpy(curFileStruct.pfn, curFileStruct.picName);
  822.   nameRepair(curFileStruct.pfn, tfnSize);
  823.   ncReplace();
  824.  
  825.   sprintf(curFileStruct.fileName, "%s-%s-%ld.scr", curFileStruct.afn, curFileStruct.pfn, fileId);
  826.   if (strlen(curFileStruct.fileName) > 62)
  827.   {
  828.     sprintf(fileIdChar, "-%ld", fileId);
  829.     str_replace(curFileStruct.fileName, sizeof(curFileStruct.fileName) - 1, curFileStruct.fileName, fileIdChar, "");
  830.     curFileStruct.fileName[50] = '\0';
  831.     strcat(curFileStruct.fileName, fileIdChar);
  832.     strcat(curFileStruct.fileName, ".scr");
  833.   }
  834.   OS_SETSYSDRV();
  835.   OS_MKDIR("../downloads");        // Create if not exist
  836.   OS_MKDIR("../downloads/getpic"); // Create if not exist
  837.   OS_CHDIR("../downloads/getpic");
  838.   fp2 = OS_CREATEHANDLE(curFileStruct.fileName, 0x80);
  839.   if (((int)fp2) & 0xff)
  840.   {
  841.     printf("%s creating error\r\n", curFileStruct.fileName);
  842.     getchar();
  843.     exit(0);
  844.   }
  845.   OS_WRITEHANDLE(picture, fp2, 6912);
  846.   OS_CLOSEHANDLE(fp2);
  847.   return 0;
  848. }
  849.  
  850. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  851. {
  852.   unsigned int i, j;
  853.   unsigned int lenC, lenS;
  854.  
  855.   for (lenC = 0; c[lenC]; lenC++)
  856.     ;
  857.   for (lenS = 0; s[lenS]; lenS++)
  858.     ;
  859.  
  860.   for (i = startPos; i <= lenS - lenC; i++)
  861.   {
  862.     for (j = 0; s[i + j] == c[j]; j++)
  863.       ;
  864.  
  865.     if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  866.       return i;
  867.     if (j == lenC)
  868.       if (n - 1)
  869.         n--;
  870.       else
  871.         return i;
  872.   }
  873.   return -1;
  874. }
  875.  
  876. const char *parseJson(unsigned char *property)
  877. {
  878.   unsigned int w, lng, lngp1, findEnd, listPos;
  879.   unsigned char terminator;
  880.   int n;
  881.   n = -1;
  882.   // netbuf[0] = '\0';
  883.   n = pos(picture, property, 1, 0);
  884.   if (n == -1)
  885.   {
  886.     strcpy(netbuf, "-");
  887.     // printf("Property %s not found", property);
  888.     return netbuf;
  889.   }
  890.   lng = n - 1 + strlen(property);
  891.   if (picture[lng] == ':')
  892.   {
  893.     terminator = '\0';
  894.   }
  895.   if (picture[lng] == '\"')
  896.   {
  897.     terminator = '\"';
  898.   }
  899.   if (picture[lng] == '[')
  900.   {
  901.     terminator = ']';
  902.   }
  903.  
  904.   findEnd = 1;
  905.   lngp1 = lng + 1;
  906.  
  907.   while (42)
  908.   {
  909.  
  910.     if ((picture[lngp1 + findEnd] == ','))
  911.     {
  912.       if (terminator == '\0')
  913.       {
  914.         break;
  915.       }
  916.       if ((picture[lng + findEnd] == terminator))
  917.       {
  918.         findEnd--;
  919.         break;
  920.       }
  921.     }
  922.     findEnd++;
  923.   }
  924.   listPos = 0;
  925.   for (w = lngp1; w < findEnd + lngp1; w++)
  926.   {
  927.     netbuf[listPos] = picture[w];
  928.     listPos++;
  929.   }
  930.   netbuf[listPos] = '\0';
  931.   return netbuf;
  932. }
  933. void convert866(void)
  934. {
  935.   unsigned int lng, targetPos, w, q = 0;
  936.   unsigned char buffer[8], one, two;
  937.   unsigned int decVal;
  938.   lng = strlen(netbuf);
  939.   targetPos = lng + 1;
  940.  
  941.   while (q < lng)
  942.   {
  943.     one = netbuf[q];
  944.     two = netbuf[q + 1];
  945.     if (one == 92 && two == 117)
  946.     {
  947.       q = q + 2;
  948.       for (w = 0; w < 4; w++)
  949.       {
  950.         buffer[w] = netbuf[q + w];
  951.       }
  952.       q = q + 4;
  953.       buffer[4] = '\0';
  954.       decVal = (unsigned int)strtol(buffer, NULL, 16);
  955.  
  956.       if (decVal < 1088)
  957.       {
  958.         decVal = decVal - 912;
  959.       }
  960.       if (decVal > 1087)
  961.       {
  962.         decVal = decVal - 864;
  963.       }
  964.       if (decVal == 1025)
  965.       {
  966.         decVal = 240;
  967.       }
  968.       if (decVal == 1105)
  969.       {
  970.         decVal = 241;
  971.       }
  972.  
  973.       netbuf[targetPos] = decVal;
  974.     }
  975.     else
  976.     {
  977.       netbuf[targetPos] = netbuf[q];
  978.       q++;
  979.     }
  980.     targetPos++;
  981.   }
  982.   netbuf[targetPos] = 0;
  983.  
  984.   for (w = lng + 1; w < targetPos + 1; w++)
  985.   {
  986.     netbuf[w - lng - 1] = netbuf[w];
  987.   }
  988. }
  989.  
  990. long processJson(unsigned long startPos, unsigned char limit, unsigned char queryNum)
  991. {
  992.   unsigned int retry, tSize;
  993.   unsigned int todo;
  994.   unsigned char buffer[] = "000000000";
  995.   unsigned char *count1, socket;
  996.   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";
  997.  
  998.   switch (queryNum)
  999.   {
  1000.   case 0:
  1001.     strcpy(netbuf, "GET /api/export:zxPicture/filter:zxPictureType=standard/limit:");
  1002.     sprintf(buffer, "%u", limit);
  1003.     strcat(netbuf, buffer);
  1004.     strcat(netbuf, "/start:");
  1005.     sprintf(buffer, "%lu", startPos);
  1006.     strcat(netbuf, buffer);
  1007.     strcat(netbuf, "/order:date,desc");
  1008.     strcat(netbuf, userAgent);
  1009.     break;
  1010.  
  1011.   case 1:
  1012.     strcpy(netbuf, "GET /api/types:zxPicture/export:zxPicture/language:eng/start:0/limit:1/order:rand/filter:zxPictureMinRating=4;zxPictureType=standard");
  1013.     strcat(netbuf, userAgent);
  1014.     break;
  1015.  
  1016.   case 3: // /api/export:author/filter:authorId=2202
  1017.     strcpy(netbuf, "GET /api/export:author/filter:authorId=");
  1018.     sprintf(buffer, "%lu", startPos);
  1019.     strcat(netbuf, buffer);
  1020.     strcat(netbuf, userAgent);
  1021.     break;
  1022.  
  1023.   case 99: // GET /jsonElementData/elementId:182798
  1024.     strcpy(netbuf, "GET /jsonElementData/elementId:");
  1025.     sprintf(buffer, "%lu", startPos);
  1026.     strcat(netbuf, buffer);
  1027.     strcat(netbuf, userAgent);
  1028.     break;
  1029.   }
  1030.   retry = 10;
  1031.   while (42)
  1032.   {
  1033.     if (netDriver == 0)
  1034.     {
  1035.       socket = OpenSock(AF_INET, SOCK_STREAM);
  1036.       netConnect(socket);
  1037.       todo = tcpSend(socket, (unsigned int)&netbuf, strlen(netbuf));
  1038.       fillPicture(socket);
  1039.     }
  1040.     else
  1041.     {
  1042.       fillPictureEsp();
  1043.     }
  1044.     count1 = strstr(picture, "responseStatus\":\"success");
  1045.     if (count1 == NULL)
  1046.     {
  1047.       retry--;
  1048.       ATRIB(91);
  1049.       printf("PROCESS JSON: [ERROR: Bad responseStatus.] [Query:%u][Retry:%u] [Pic:%lu]\r\n", queryNum, retry, startPos);
  1050.       YIELD();
  1051.       puts(netbuf);
  1052.       getchar();
  1053.       if (retry < 1)
  1054.       {
  1055.         return -1;
  1056.       }
  1057.     }
  1058.     else
  1059.     {
  1060.       break;
  1061.     }
  1062.   }
  1063.  
  1064.   count1 = strstr(picture, "\"id\":");
  1065.   if (count1 == NULL)
  1066.   {
  1067.     ATRIB(91);
  1068.     printf("PROCESS JSON: [ERROR: ID not found.] [Query:%u][Pic:%lu]\r\n", queryNum, startPos);
  1069.     YIELD();
  1070.     return -2;
  1071.   }
  1072.  
  1073.   netbuf[0] = 0;
  1074.   if (queryNum < 3)
  1075.   {
  1076.     parseJson("\"id\":");
  1077.     curFileStruct.picId = atol(netbuf);
  1078.     parseJson(",\"title\":\"");
  1079.     convert866();
  1080.     strcpy(curFileStruct.picName, netbuf);
  1081.  
  1082.     tSize = sizeof(curFileStruct.picName);
  1083.     stringRepair(curFileStruct.picName, tSize);
  1084.  
  1085.     parseJson(",\"type\":\"");
  1086.     strcpy(curFileStruct.picType, netbuf);
  1087.     parseJson("\"rating\":\"");
  1088.     strcpy(curFileStruct.picRating, netbuf);
  1089.     parseJson("\"year\":\"");
  1090.     curFileStruct.picYear = atoi(netbuf);
  1091.     parseJson("\"totalAmount\":");
  1092.     curFileStruct.totalAmount = atol(netbuf);
  1093.     parseJson("\"authorIds\":[");
  1094.     strcpy(curFileStruct.authorIds, netbuf);
  1095.   }
  1096.   if (queryNum == 99)
  1097.   {
  1098.     parseJson(",\"title\":\"");
  1099.     convert866();
  1100.     strcpy(curFileStruct.authorTitle, netbuf);
  1101.     parseJson(",\"realName\":\"");
  1102.     convert866();
  1103.     strcpy(curFileStruct.authorRealName, netbuf);
  1104.   }
  1105.   return curFileStruct.picId;
  1106. }
  1107.  
  1108. void printData(void)
  1109. {
  1110.   ATRIB(93);
  1111.   printf(" #: ");
  1112.   ATRIB(97);
  1113.   printf("%lu", count);
  1114.   ATRIB(93);
  1115.   printf(" ID: ");
  1116.   ATRIB(97);
  1117.   printf("%lu ", curFileStruct.picId);
  1118.   ATRIB(93);
  1119.   printf(" Total Pics: ");
  1120.   ATRIB(97);
  1121.   printf("%lu\r\n", curFileStruct.totalAmount);
  1122.   ATRIB(93);
  1123.   printf(" Author: ");
  1124.   ATRIB(96);
  1125.   printf("%s\r\n", curFileStruct.authorTitle);
  1126.   ATRIB(93);
  1127.   printf(" TITLE: ");
  1128.   ATRIB(95);
  1129.   printf("%s\r\n", curFileStruct.picName);
  1130.   ATRIB(93);
  1131.   printf(" RATING: ");
  1132.   ATRIB(97);
  1133.   printf("%s", curFileStruct.picRating);
  1134.   ATRIB(93);
  1135.   printf(" YEAR: ");
  1136.   ATRIB(97);
  1137.   printf("%u\r\n", curFileStruct.picYear);
  1138.   ATRIB(93);
  1139.   printf(" AuthorsIDs ");
  1140.   ATRIB(97);
  1141.   printf("%s", curFileStruct.authorIds);
  1142.   ATRIB(93);
  1143.   printf(" Real name: ");
  1144.   ATRIB(97);
  1145.   printf("%s\r\n", curFileStruct.authorRealName);
  1146.   ATRIB(96);
  1147.   printf("\r\n");
  1148.   YIELD();
  1149. }
  1150. void safeKeys(unsigned char keypress)
  1151. {
  1152.   if (keypress == 27)
  1153.   {
  1154.     printf("Good bye...\r\n");
  1155.     ATRIB(37);
  1156.     ATRIB(40);
  1157.     exit(0);
  1158.   }
  1159.  
  1160.   if (keypress == 'j' || keypress == 'J')
  1161.   {
  1162.     printf("Jump to picture:");
  1163.     scanf("%lu", &count);
  1164.     if (count > curFileStruct.totalAmount - 1)
  1165.     {
  1166.       count = curFileStruct.totalAmount - 1;
  1167.     }
  1168.   }
  1169.  
  1170.   if (keypress == 'v' || keypress == 'V')
  1171.   {
  1172.     verbose = !verbose;
  1173.  
  1174.     if (verbose == 0)
  1175.     {
  1176.       BOX(1, 1, 80, 25, 40);
  1177.       AT(1, 1);
  1178.     }
  1179.   }
  1180.  
  1181.   if (keypress == 'h' || keypress == 'H')
  1182.   {
  1183.     printHelp();
  1184.   }
  1185.  
  1186.   if (keypress == 'r' || keypress == 'R')
  1187.   {
  1188.     randomPic = !randomPic;
  1189.  
  1190.     if (verbose == 1)
  1191.     {
  1192.       if (randomPic == 1)
  1193.       {
  1194.         printf("    Random mode enabled...\r\n");
  1195.       }
  1196.       else
  1197.       {
  1198.         printf("    Sequental mode enabled...\r\n");
  1199.       }
  1200.     }
  1201.   }
  1202.   if (keypress == 'a' || keypress == 'A')
  1203.   {
  1204.     slideShow = !slideShow;
  1205.     if (slideShow == 1)
  1206.     {
  1207.       if (verbose == 1)
  1208.         printf("    SlideShow mode enabled...\r\n\r\n");
  1209.       slideShowTime = 250;
  1210.     }
  1211.     else
  1212.     {
  1213.       if (verbose == 1)
  1214.         printf("    Manual mode enabled...\r\n\r\n");
  1215.       slideShowTime = 0;
  1216.     }
  1217.   }
  1218.  
  1219.   if (keypress == 'd' || keypress == 'D')
  1220.   {
  1221.     netDriver = !netDriver;
  1222.     if (netDriver == 1)
  1223.     {
  1224.       printf("    ESP32 mode enabled...\r\n\r\n");
  1225.       loadEspConfig();
  1226.       uart_init(divider);
  1227.       espReBoot();
  1228.     }
  1229.  
  1230.     else
  1231.     {
  1232.       if (verbose == 1)
  1233.         printf("    ZXNETUSB mode enabled...\r\n\r\n");
  1234.     }
  1235.   }
  1236. }
  1237.  
  1238. C_task main(void)
  1239. {
  1240.   unsigned char errno;
  1241.   unsigned long ipadress;
  1242.   long iddqd, idkfa;
  1243.  
  1244.   os_initstdio();
  1245.  
  1246.   count = 0;
  1247.   verbose = 1;
  1248.   randomPic = 0;
  1249.   slideShow = 0;
  1250.   netDriver = 0;
  1251.  
  1252.   BOX(1, 1, 80, 25, 40);
  1253.   AT(1, 1);
  1254.   printHelp();
  1255.   safeKeys(keypress);
  1256.  
  1257. start:
  1258.   keypress = 0;
  1259.   switch (randomPic)
  1260.   {
  1261.   case 0:
  1262.     iddqd = processJson(count, 1, 0);
  1263.     break;
  1264.   case 1:
  1265.     iddqd = processJson(0, 1, 1);
  1266.     break;
  1267.   }
  1268.  
  1269.   if (iddqd < 0)
  1270.   {
  1271.     count++;
  1272.     goto start;
  1273.   }
  1274.  
  1275.   if (verbose == 1)
  1276.   {
  1277.     idkfa = processJson(atol(curFileStruct.authorIds), 0, 99);
  1278.  
  1279.     if (idkfa < 0)
  1280.     {
  1281.       printf(" Cant parse curFileStruct.authorIds = %s \r\n\r\n", curFileStruct.authorIds);
  1282.       count++;
  1283.       goto start;
  1284.     }
  1285.  
  1286.     printData();
  1287.   }
  1288.   else
  1289.   {
  1290.     // ATRIB(97);
  1291.     // printf(" Getting picture...\r\n");
  1292.   }
  1293.  
  1294.   if (strcmp(curFileStruct.picType, "standard") != 0)
  1295.   {
  1296.     printf("  >>Format '%s' not supported, skipped \n\r", curFileStruct.picType);
  1297.     count++;
  1298.     goto start;
  1299.   }
  1300.  
  1301.   if (netDriver == 0)
  1302.   {
  1303.     errno = getPic(iddqd);
  1304.   }
  1305.   else
  1306.   {
  1307.     errno = getPicEsp(iddqd);
  1308.   }
  1309.  
  1310. review:
  1311.   keypress = viewScreen6912((unsigned int)&picture, slideShowTime);
  1312.   emptyKeys();
  1313.  
  1314.   ///// Keys only for pictures
  1315.   if (keypress == 's' || keypress == 'S')
  1316.   {
  1317.     savePic(iddqd);
  1318.     if (verbose == 1)
  1319.       printf("        ID:%lu    TITLE:%s  SAVED\r\n\r\n", curFileStruct.picId, curFileStruct.picName);
  1320.     count++;
  1321.   }
  1322.  
  1323.   if (keypress == 248 || keypress == 'b' || keypress == 'B')
  1324.   {
  1325.     if (count > 0)
  1326.     {
  1327.       count--;
  1328.     }
  1329.   }
  1330.   if (keypress == 251 || keypress == 32)
  1331.   {
  1332.     count++;
  1333.     goto start;
  1334.   }
  1335.   if (keypress == 'i' || keypress == 'I')
  1336.   {
  1337.     delay(100);
  1338.     getchar();
  1339.     goto review;
  1340.   }
  1341.   safeKeys(keypress);
  1342.   goto start;
  1343. }
  1344.