?login_element?

Subversion Repositories NedoOS

Rev

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