?login_element?

Subversion Repositories NedoOS

Rev

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

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