Subversion Repositories NedoOS

Rev

Rev 2330 | Blame | Compare with Previous | Last modification | View Log | Download

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <intrz80.h>
  4. #include <stdlib.h>
  5. #include <oscalls.h>
  6. #include <../common/terminal.c>
  7. #include <tcp.h>
  8. #include <osfs.h>
  9. #include <graphic.h>
  10. #include <ctype.h>
  11. #include <math.h>
  12. //
  13. #define true 1
  14. #define false 0
  15. #define screenHeight 23
  16. #define screenWidth 80
  17.  
  18. unsigned int RBR_THR = 0xf8ef;
  19. unsigned int IER = 0xf9ef;
  20. unsigned int IIR_FCR = 0xfaef;
  21. unsigned int LCR = 0xfbef;
  22. unsigned int MCR = 0xfcef;
  23. unsigned int LSR = 0xfdef;
  24. unsigned int MSR = 0xfeef;
  25. unsigned int SR = 0xffef;
  26. unsigned int divider = 1;
  27. unsigned char comType = 0;
  28. unsigned int espType = 32;
  29. unsigned char netDriver = 0;
  30. unsigned char curHost;
  31. unsigned long contLen;
  32. unsigned int httpErr;
  33.  
  34. unsigned char uVer[] = "0.3";
  35. unsigned char curPath[128];
  36. unsigned char cmd[256];
  37. unsigned char search[256];
  38. unsigned char crlf[2] = {13, 10};
  39. const unsigned char gotWiFi[] = "WIFI GOT IP";
  40. char hosts[3][32] = {"next.zxart.ee", "zxdb.remysharp.com", "hood.speccy.cz"};
  41. unsigned char userAgent1[] = " HTTP/1.1\r\nHost: ";
  42. unsigned char userAgent2[] = "\r\nUser-Agent: Mozilla/4.0 (compatible; MSIE5.01; NedoOS; ZXDB)\r\n\r\n\0";
  43. unsigned char netbuf[8192];
  44. unsigned char buf[16384];
  45. struct sockaddr_in targetadr;
  46. struct readstructure readStruct;
  47. struct sockaddr_in dnsaddress;
  48.  
  49. struct window
  50. {
  51.         unsigned char x;
  52.         unsigned char y;
  53.         unsigned char w;
  54.         unsigned char h;
  55.         unsigned char text;
  56.         unsigned char back;
  57.         unsigned char tittle[80];
  58. } curWin;
  59.  
  60. struct time
  61. {
  62.         unsigned int hours;
  63.         unsigned int minutes;
  64.         unsigned char oldMinutes;
  65.  
  66. } clock;
  67.  
  68. struct linkStruct
  69. {
  70.         unsigned char host[128];
  71.         unsigned char path[512];
  72.         unsigned int port;
  73.         unsigned char hasName;
  74.         unsigned char fname[256];
  75. } link;
  76.  
  77. struct line
  78. {
  79.         unsigned long id;
  80.         unsigned char name[512];
  81.         unsigned char file[512];
  82.         unsigned char ext[5];
  83.         unsigned long size;
  84.         unsigned char option;
  85.         unsigned int year;
  86. } table[12];
  87.  
  88. struct limit
  89. {
  90.         int first;
  91.         int second;
  92.         int total;
  93.         int curline;
  94.         int curOpt;
  95.         int headLng;
  96. } limiter;
  97.  
  98. void spaces(unsigned char number)
  99. {
  100.         while (number > 0)
  101.         {
  102.                 putchar(' ');
  103.                 number--;
  104.         }
  105. }
  106.  
  107. void waitKey(void)
  108. {
  109.         do
  110.         {
  111.                 YIELD();
  112.         } while (OS_GETKEY() == 0);
  113. }
  114.  
  115. void clearStatus(void)
  116. {
  117.         OS_SETCOLOR(5);
  118.         OS_SETXY(0, 24);
  119.         spaces(79);
  120.         putchar('\r');
  121. }
  122.  
  123. void quit(void)
  124. {
  125.         OS_CLS(0);
  126.         OS_SETGFX(-1);
  127.         exit(0);
  128. }
  129.  
  130. void printTable(void)
  131. {
  132.         unsigned int cycle;
  133.  
  134.         for (cycle = 1; cycle < 256; cycle++)
  135.         {
  136.                 OS_SETCOLOR(7);
  137.                 printf("%03u:", cycle);
  138.                 OS_SETCOLOR(71);
  139.                 putchar(cycle);
  140.                 OS_SETCOLOR(7);
  141.                 printf(" ");
  142.                 if (cycle % 12 == 0)
  143.                 {
  144.                         printf("\r\n");
  145.                 }
  146.         }
  147. }
  148.  
  149. void delay(unsigned long counter)
  150. {
  151.         unsigned long start, finish;
  152.         counter = counter / 20;
  153.         if (counter < 1)
  154.         {
  155.                 counter = 1;
  156.         }
  157.         start = time();
  158.         finish = start + counter;
  159.  
  160.         while (start < finish)
  161.         {
  162.                 start = time();
  163.         }
  164. }
  165.  
  166. ///////////////////////////
  167. #include <../common/esp-com.c>
  168. #include <../common/network.c>
  169. //////////////////////////
  170.  
  171. void clearNetbuf(void)
  172. {
  173.         unsigned int counter;
  174.         for (counter = 0; counter < sizeof(netbuf); counter++)
  175.         {
  176.                 netbuf[counter] = 0;
  177.         }
  178. }
  179.  
  180. int testOperation2(const char *process, int socket)
  181. {
  182.         if (socket < 0)
  183.         {
  184.                 printf("%s: [ERROR:", process);
  185.                 errorPrint(-socket);
  186.                 printf("]\r\n");
  187.                 YIELD();
  188.                 return -socket;
  189.         }
  190.         return 1;
  191. }
  192.  
  193. unsigned char saveBuf(unsigned char *fileNamePtr, unsigned char operation, unsigned int sizeOfBuf)
  194. {
  195.         FILE *fp2;
  196.  
  197.         switch (operation)
  198.         {
  199.         case 00:
  200.                 fp2 = OS_CREATEHANDLE(fileNamePtr, 0x80);
  201.                 if (((int)fp2) & 0xff)
  202.                 {
  203.                         clearStatus();
  204.                         printf("%s  creating error.", fileNamePtr);
  205.                         getchar();
  206.                         exit(0);
  207.                 }
  208.                 OS_CLOSEHANDLE(fp2);
  209.                 break;
  210.         case 01:
  211.                 fp2 = OS_OPENHANDLE(fileNamePtr, 0x80);
  212.                 if (((int)fp2) & 0xff)
  213.                 {
  214.                         clearStatus();
  215.                         printf("%s opening error.\r\n ", fileNamePtr);
  216.                         getchar();
  217.                         exit(0);
  218.                 }
  219.                 OS_SEEKHANDLE(fp2, OS_GETFILESIZE(fp2));
  220.                 OS_WRITEHANDLE(netbuf + limiter.headLng, fp2, sizeOfBuf);
  221.                 OS_CLOSEHANDLE(fp2);
  222.                 break;
  223.         case 02:
  224.                 OS_CLOSEHANDLE(fp2);
  225.                 break;
  226.         default:
  227.                 break;
  228.         }
  229.  
  230.         return 0;
  231. }
  232.  
  233. void drawClock(void)
  234. {
  235.         unsigned long dosTime;
  236.         dosTime = OS_GETTIME();
  237.         clock.hours = dosTime >> 11 & 31;        // 0b00011111
  238.         clock.minutes = (dosTime >> 5) & 63; // 0b00111111
  239.  
  240.         if (clock.minutes != clock.oldMinutes)
  241.         {
  242.                 clock.oldMinutes = clock.minutes;
  243.                 OS_SETCOLOR(103);
  244.                 OS_SETXY(73, 0);
  245.                 printf("[%02u:%02u]", clock.hours, clock.minutes);
  246.         }
  247. }
  248.  
  249. char readParamFromIni(void)
  250. {
  251.         FILE *fpini;
  252.         unsigned char *count1;
  253.         const char currentNetwork[] = "currentNetwork";
  254.         unsigned char curNet = 0;
  255.  
  256.         OS_GETPATH((unsigned int)&curPath);
  257.         OS_SETSYSDRV();
  258.         OS_CHDIR("/");
  259.         OS_CHDIR("ini");
  260.  
  261.         fpini = OS_OPENHANDLE("network.ini", 0x80);
  262.         if (((int)fpini) & 0xff)
  263.         {
  264.                 clearStatus();
  265.                 printf("network.ini not found.\r\n");
  266.                 getchar();
  267.                 return false;
  268.         }
  269.  
  270.         OS_READHANDLE(netbuf, fpini, sizeof(netbuf) - 1);
  271.         OS_CLOSEHANDLE(fpini);
  272.  
  273.         count1 = strstr(netbuf, currentNetwork);
  274.         if (count1 != NULL)
  275.         {
  276.                 sscanf(count1 + strlen(currentNetwork) + 1, "%u", &curNet);
  277.         }
  278.  
  279.         OS_CHDIR(curPath);
  280.         return curNet;
  281. }
  282.  
  283. void init(void)
  284. {
  285.         targetadr.family = AF_INET;
  286.         targetadr.porth = 00;
  287.         targetadr.portl = 80;
  288.         targetadr.b1 = 0;
  289.         targetadr.b2 = 0;
  290.         targetadr.b3 = 0;
  291.         targetadr.b4 = 0;
  292.         curHost = 0;
  293.         link.port = 80;
  294.         link.hasName = false;
  295.         limiter.curOpt = 1;
  296.         get_dns();
  297.         netDriver = readParamFromIni();
  298.         if (netDriver == 1)
  299.         {
  300.                 loadEspConfig();
  301.                 uart_init(divider);
  302.                 espReBoot();
  303.         }
  304.         OS_SETSYSDRV();
  305.         OS_MKDIR("../downloads");          // Create if not exist
  306.         OS_MKDIR("../downloads/zxdb"); // Create if not exist
  307.         OS_CHDIR("../downloads/zxdb");
  308.  
  309.         strcpy(link.host, hosts[curHost]);
  310.         clock.oldMinutes = 255;
  311. }
  312.  
  313. void errorBox(struct window w, const char *message)
  314. {
  315.         unsigned char wcount, tempx, tittleStart;
  316.  
  317.         w.h++;
  318.         OS_SETXY(w.x, w.y - 1);
  319.         BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  320.         OS_SETXY(w.x, w.y);
  321.         OS_SETCOLOR(w.text);
  322.         putchar(201);
  323.         for (wcount = 0; wcount < w.w; wcount++)
  324.         {
  325.                 putchar(205);
  326.         }
  327.         putchar(187);
  328.         OS_SETXY(w.x, w.y + w.h);
  329.         putchar(200);
  330.         for (wcount = 0; wcount < w.w; wcount++)
  331.         {
  332.                 putchar(205);
  333.         }
  334.         putchar(188);
  335.  
  336.         tempx = w.x + w.w + 1;
  337.         for (wcount = 1; wcount < w.h; wcount++)
  338.         {
  339.                 OS_SETXY(w.x, w.y + wcount);
  340.                 putchar(186);
  341.                 OS_SETXY(tempx, w.y + wcount);
  342.                 putchar(186);
  343.         }
  344.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  345.         OS_SETXY(tittleStart, w.y);
  346.         printf("[%s]", w.tittle);
  347.         OS_SETXY(w.x + 1, w.y + 1);
  348.         OS_SETCOLOR(w.back);
  349.         tittleStart = w.x + (w.w / 2) - (strlen(message) / 2);
  350.         OS_SETXY(tittleStart, w.y + 1);
  351.         printf("%s", message);
  352. }
  353.  
  354. void simpleBox(struct window w)
  355. {
  356.         unsigned char wcount, tempx;
  357.         w.h = w.h - 2;
  358.         OS_SETXY(w.x, w.y);
  359.         BDBOX(w.x, w.y + 1, w.w, w.h, w.back, 32);
  360.         w.w = w.w - 2;
  361.         OS_SETXY(w.x, w.y);
  362.         OS_SETCOLOR(w.text);
  363.         putchar(201);
  364.         for (wcount = 0; wcount < w.w; wcount++)
  365.         {
  366.                 putchar(205);
  367.         }
  368.         putchar(187);
  369.  
  370.         OS_SETXY(w.x, w.y + w.h);
  371.         putchar(200);
  372.         for (wcount = 0; wcount < w.w; wcount++)
  373.         {
  374.                 putchar(205);
  375.         }
  376.         putchar(188);
  377.         tempx = w.x + w.w + 1;
  378.         for (wcount = 1; wcount < w.h; wcount++)
  379.         {
  380.                 OS_SETXY(w.x, w.y + wcount);
  381.                 putchar(186);
  382.                 OS_SETXY(tempx, w.y + wcount);
  383.                 putchar(186);
  384.         }
  385. }
  386.  
  387. unsigned char inputBox(struct window w, const char *prefilled)
  388. {
  389.         unsigned char wcount, tempx, tittleStart;
  390.         unsigned char byte, counter;
  391.         w.h++;
  392.         OS_SETXY(w.x, w.y - 1);
  393.         BDBOX(w.x, w.y, w.w + 1, w.h, w.back, 32);
  394.         OS_SETXY(w.x, w.y);
  395.         OS_SETCOLOR(w.text);
  396.         putchar(201);
  397.         for (wcount = 0; wcount < w.w; wcount++)
  398.         {
  399.                 putchar(205);
  400.         }
  401.         putchar(187);
  402.         OS_SETXY(w.x, w.y + w.h);
  403.         putchar(200);
  404.         for (wcount = 0; wcount < w.w; wcount++)
  405.         {
  406.                 putchar(205);
  407.         }
  408.         putchar(188);
  409.  
  410.         tempx = w.x + w.w + 1;
  411.         for (wcount = 1; wcount < w.h; wcount++)
  412.         {
  413.                 OS_SETXY(w.x, w.y + wcount);
  414.                 putchar(186);
  415.                 OS_SETXY(tempx, w.y + wcount);
  416.                 putchar(186);
  417.         }
  418.         tittleStart = w.x + (w.w / 2) - (strlen(w.tittle) / 2);
  419.         OS_SETXY(tittleStart, w.y);
  420.         printf("[%s]", w.tittle);
  421.         OS_SETXY(w.x + 1, w.y + 1);
  422.         OS_SETCOLOR(w.back);
  423.         putchar(219);
  424.  
  425.         cmd[0] = 0;
  426.  
  427.         counter = strlen(prefilled);
  428.         if (counter != 0)
  429.         {
  430.                 strcpy(cmd, prefilled);
  431.                 goto skipKeys;
  432.         }
  433.  
  434.         do
  435.         {
  436.                 byte = OS_GETKEY();
  437.                 if (byte != 0)
  438.                 {
  439.                         switch (byte)
  440.                         {
  441.                         case 0x08:
  442.                                 if (counter > 0)
  443.                                 {
  444.                                         counter--;
  445.                                         cmd[counter] = 0;
  446.                                 }
  447.                                 break;
  448.                         case 0x0d:
  449.  
  450.                                 if (counter == 0)
  451.                                 {
  452.                                         return false;
  453.                                 }
  454.                                 else
  455.                                 {
  456.                                         return true;
  457.                                 }
  458.  
  459.                         case 31:
  460.                                 break;
  461.                         case 250:
  462.                                 break;
  463.                         case 249:
  464.                                 break;
  465.                         case 248:
  466.                                 break;
  467.                         case 251: // Right
  468.                                 break;
  469.                         case 252: // Del
  470.                                 OS_SETXY(w.x + 1, w.y + 1);
  471.                                 spaces(counter + 1);
  472.                                 cmd[0] = 0;
  473.                                 counter = 0;
  474.                                 break;
  475.                         case 27:
  476.                                 cmd[0] = 0;
  477.                                 return false;
  478.                         default:
  479.                                 if (counter < w.w - 1)
  480.                                 {
  481.                                         cmd[counter] = byte;
  482.                                         counter++;
  483.                                         cmd[counter] = 0;
  484.                                 }
  485.                                 break;
  486.                         }
  487.                 skipKeys:
  488.                         OS_SETXY(w.x + 1, w.y + 1);
  489.                         printf("%s", cmd);
  490.                         putchar(219);
  491.                         if (byte == 0x08)
  492.                         {
  493.                                 putchar(' ');
  494.                         }
  495.                 }
  496.                 YIELD();
  497.         } while (42);
  498.         return false;
  499. }
  500.  
  501. void mainWinDraw(void)
  502. {
  503.         clock.oldMinutes = 255;
  504.         OS_SETCOLOR(103);
  505.         OS_SETXY(0, 0);
  506.         spaces(79);
  507.         OS_SETXY(0, 0);
  508.         printf(" ZXDB downloader [%s]", uVer);
  509.         OS_SETXY(38 - strlen(link.host) / 2, 0);
  510.         printf("[");
  511.         OS_SETCOLOR(102);
  512.         printf("%s", link.host);
  513.         OS_SETCOLOR(103);
  514.         printf("]");
  515.         drawClock();
  516.         OS_SETXY(0, 23);
  517.         OS_SETCOLOR(71);
  518.         printf("[");
  519.         OS_SETCOLOR(87);
  520.         printf("H");
  521.         OS_SETCOLOR(71);
  522.         printf("]change host   [");
  523.         OS_SETCOLOR(87);
  524.         printf("S");
  525.  
  526.         OS_SETCOLOR(71);
  527.         printf("]Search item   [");
  528.         OS_SETCOLOR(87);
  529.         printf("ENTER");
  530.         OS_SETCOLOR(71);
  531.         printf("]Download item   [");
  532.         printf("ESC");
  533.         OS_SETCOLOR(71);
  534.         printf("]Exit   ");
  535.  
  536.         curWin.x = 0;
  537.         curWin.y = 1;
  538.         curWin.w = 80;
  539.         curWin.h = 23;
  540.         curWin.text = 207;
  541.         curWin.back = 207;
  542.         simpleBox(curWin);
  543.         OS_SETXY(38 - strlen(search) / 2, 1);
  544.         putchar('[');
  545.         OS_SETCOLOR(71);
  546.         printf("%s", search);
  547.         OS_SETCOLOR(curWin.text);
  548.         putchar(']');
  549.         clearStatus();
  550. }
  551.  
  552. int pos(unsigned char *s, unsigned char *c, unsigned int n, unsigned int startPos)
  553. {
  554.         unsigned int i, j;
  555.         unsigned int lenC, lenS;
  556.  
  557.         for (lenC = 0; c[lenC]; lenC++)
  558.                 ;
  559.         for (lenS = 0; s[lenS]; lenS++)
  560.                 ;
  561.  
  562.         for (i = startPos; i <= lenS - lenC; i++)
  563.         {
  564.                 for (j = 0; s[i + j] == c[j]; j++)
  565.                         ;
  566.  
  567.                 if (j - lenC == 1 && i == lenS - lenC && !(n - 1))
  568.                         return i;
  569.                 if (j == lenC)
  570.                         if (n - 1)
  571.                                 n--;
  572.                         else
  573.                                 return i;
  574.         }
  575.         return -1;
  576. }
  577.  
  578. void squeeze(char s[], int c)
  579. {
  580.         int i, j;
  581.  
  582.         for (i = j = 0; s[i] != '\0'; i++)
  583.                 if (s[i] != c)
  584.                         s[j++] = s[i];
  585.         s[j] = '\0';
  586. }
  587.  
  588. char *insert_string(const char *original, const char *to_insert, unsigned int position)
  589. {
  590.         unsigned int original_len = strlen(original);
  591.         unsigned int insert_len = strlen(to_insert);
  592.         unsigned int new_len = original_len + insert_len;
  593.  
  594.         char *new_string = (char *)malloc(new_len + 1); // +1 для \0
  595.         if (new_string == NULL)
  596.         {
  597.                 return NULL; // Обработка ошибки выделения памяти
  598.         }
  599.  
  600.         // Копирование части исходной строки до позиции вставки
  601.         strncpy(new_string, original, position);
  602.         new_string[position] = '\0';
  603.  
  604.         // Вставка строки
  605.         strcat(new_string, to_insert);
  606.  
  607.         // Добавление оставшейся части исходной строки
  608.         strcat(new_string, original + position);
  609.  
  610.         return new_string;
  611. }
  612.  
  613. int cutHeader(void)
  614. {
  615.         unsigned char *count1;
  616.         int counter;
  617.  
  618.         httpErr = httpError();
  619.         if (httpError() != 200)
  620.         {
  621.                 clearStatus();
  622.                 printf("HTTP response:[%u]", httpErr);
  623.                 return 0;
  624.         }
  625.         count1 = strstr(netbuf, "Content-Length:");
  626.         if (count1 == NULL)
  627.         {
  628.                 clearStatus();
  629.                 printf("contLen not found");
  630.                 contLen = 0;
  631.                 httpErr = 999; // bad kostil
  632.                 return 0;
  633.         }
  634.         contLen = atol(count1 + 15);
  635.         // printf("Content-Length: %lu \n\r", contLen);
  636.  
  637.         count1 = strstr(netbuf, "Content-Disposition: attachment; filename="); // 42
  638.                                                                                                                                                    // Content-Disposition: attachment; filename="WED_PRO.TRD"
  639.         if (count1 != NULL)
  640.         {
  641.                 strncpy(link.fname, count1 + 43, 64);
  642.                 strcat(link.fname, "\0");
  643.                 counter = 0;
  644.                 while (link.fname[counter] != '\"')
  645.                 {
  646.                         counter++;
  647.                 }
  648.                 link.fname[counter] = 0;
  649.                 link.hasName = true;
  650.         }
  651.         else
  652.         {
  653.                 strncpy(link.fname, table[limiter.curline].file, 57);
  654.                 strcat(link.fname, ".");
  655.                 strcat(link.fname, table[limiter.curline].ext);
  656.  
  657.                 counter = strlen(link.fname);
  658.                 while (counter != 0)
  659.                 {
  660.                         counter--;
  661.                         if (link.fname[counter] == '.' && table[limiter.curline].option > 1)
  662.                         {
  663.                                 const char *new_string;
  664.                                 char temp[65];
  665.                                 sprintf(temp, "-%02d", limiter.curOpt);
  666.                                 new_string = insert_string(link.fname, temp, counter);
  667.                                 strcpy(link.fname, new_string);
  668.                         }
  669.                 }
  670.                 link.hasName = false;
  671.         }
  672.  
  673.         count1 = strstr(netbuf, "\r\n\r\n");
  674.         if (count1 == NULL)
  675.         {
  676.                 clearStatus();
  677.                 printf("end of header not found\r\n");
  678.         }
  679.         else
  680.         {
  681.                 // printf("header %u bytes\r\n", ((unsigned int)count1 - (unsigned int)netbuf + 4));
  682.         }
  683.         return ((unsigned int)count1 - (unsigned int)netbuf + 4);
  684. }
  685.  
  686. void downDialog(void)
  687. {
  688.         unsigned int nameLong;
  689.  
  690.         mainWinDraw();
  691.  
  692.         nameLong = strlen(link.fname);
  693.         if (nameLong < 21)
  694.         {
  695.                 curWin.w = 23;
  696.         }
  697.         else
  698.         {
  699.                 curWin.w = nameLong + 4;
  700.         }
  701.         curWin.x = 39 - curWin.w / 2;
  702.         curWin.y = 10;
  703.         curWin.h = 4;
  704.         curWin.text = 223;
  705.         curWin.back = 223;
  706.         simpleBox(curWin);
  707.         OS_SETXY(38 - nameLong / 2, curWin.y);
  708.         printf("[%s]", link.fname);
  709. }
  710.  
  711. char getFileEsp(void)
  712. {
  713.         int todo;
  714.         unsigned char byte, firstPacket;
  715.         unsigned long downloaded = 0;
  716.         unsigned int count, fileSize1, down;
  717.         const unsigned char sendOk[] = "SEND OK";
  718.  
  719.         sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%u", link.host, link.port);
  720.         sendcommand(cmd);
  721.  
  722.         do
  723.         {
  724.                 getAnswer2(); // CONNECT or ERROR or link is not valid
  725.  
  726.                 if (strstr(netbuf, "CONNECT") != NULL)
  727.                 {
  728.                         break;
  729.                 }
  730.                 else
  731.                 {
  732.                         if (strstr(netbuf, "ERROR") != NULL)
  733.                         {
  734.                                 return false;
  735.                         }
  736.                 }
  737.         } while (42); // Try until endo of the days recieve CONNECT or ERROR
  738.  
  739.         getAnswer2(); // OK
  740.  
  741.         sprintf(cmd, "AT+CIPSEND=%u", strlen(link.path) + 2);
  742.         sendcommand(cmd);
  743.         getAnswer2();
  744.  
  745.         do
  746.         {
  747.                 byte = uart_readBlock();
  748.         } while (byte != '>');
  749.  
  750.         // sendcommandNrn(link.path);
  751.         sendcommand(link.path);
  752.  
  753.         count = 0;
  754.         do
  755.         {
  756.                 byte = uart_readBlock();
  757.                 if (byte == sendOk[count])
  758.                 {
  759.                         count++;
  760.                         // putchar(byte);
  761.                 }
  762.                 else
  763.                 {
  764.                         count = 0;
  765.                 }
  766.         } while (count < strlen(sendOk));
  767.  
  768.         uart_readBlock(); // CR
  769.         uart_readBlock(); // LF
  770.  
  771.         firstPacket = true;
  772.         do
  773.         {
  774.                 unsigned char temp[64];
  775.                 limiter.headLng = 0;
  776.                 todo = recvHead();
  777.                 getdataEsp(todo); // Requested size
  778.  
  779.                 if (firstPacket)
  780.                 {
  781.                         firstPacket = false;
  782.                         limiter.headLng = cutHeader();
  783.                         todo = todo - limiter.headLng;
  784.                         fileSize1 = contLen / 1024;
  785.  
  786.                         if (!link.hasName)
  787.                         {
  788.                                 curWin.w = 66;
  789.                                 curWin.x = 39 - curWin.w / 2;
  790.                                 curWin.y = 9;
  791.                                 curWin.h = 1;
  792.                                 curWin.text = 103;
  793.                                 curWin.back = 103;
  794.  
  795.                                 strcpy(curWin.tittle, "Введите имя файла");
  796.                                 if (inputBox(curWin, link.fname))
  797.                                 {
  798.                                         strncpy(link.fname, cmd, 64);
  799.                                         strcat(link.fname, "\0");
  800.                                 }
  801.                         }
  802.                         if (httpErr != 200)
  803.                         {
  804.                                 sendcommand("AT+CIPCLOSE");
  805.                                 getAnswer2(); // CLOSED
  806.                                 getAnswer2(); // OK
  807.                                 mainWinDraw();
  808.                                 return false;
  809.                         }
  810.                         downDialog();
  811.                         saveBuf(link.fname, 00, 0);
  812.                 }
  813.  
  814.                 downloaded = downloaded + todo;
  815.                 down = downloaded / 1024;
  816.                 OS_SETCOLOR(223);
  817.                 sprintf(temp, "%4u  of %4u kb", down, fileSize1);
  818.                 OS_SETXY(38 - strlen(temp) / 2, 11);
  819.                 puts(temp);
  820.                 saveBuf(link.fname, 01, todo);
  821.                 drawClock();
  822.         } while (downloaded < contLen);
  823.         sendcommand("AT+CIPCLOSE");
  824.         getAnswer2(); // CLOSED
  825.         getAnswer2(); // OK
  826.         mainWinDraw();
  827.         return true;
  828. }
  829.  
  830. char getFileNet(void)
  831. {
  832.         int todo, socket;
  833.         char firstPacket;
  834.  
  835.         unsigned int fileSize1;
  836.         unsigned long downloaded = 0;
  837.         unsigned int down;
  838.  
  839.         socket = OpenSock(AF_INET, SOCK_STREAM);
  840.         if (testOperation2("OS_NETSOCKET", socket) != 1)
  841.         {
  842.                 getchar();
  843.                 quit();
  844.         }
  845.  
  846.         todo = netConnect(socket, 1);
  847.         if (testOperation2("OS_NETCONNECT", todo) != 1)
  848.         {
  849.                 getchar();
  850.                 quit();
  851.         }
  852.  
  853.         todo = tcpSend(socket, (unsigned int)&link.path, strlen(link.path), 1);
  854.         if (testOperation2("OS_WIZNETWRITE", todo) != 1)
  855.         {
  856.                 getchar();
  857.                 quit();
  858.         }
  859.  
  860.         firstPacket = true;
  861.         do
  862.         {
  863.                 unsigned char temp[64];
  864.                 limiter.headLng = 0;
  865.                 todo = tcpRead(socket, 1);
  866.                 testOperation("OS_WIZNETREAD", todo);
  867.                 if (todo == 0)
  868.                 {
  869.                         break;
  870.                 }
  871.                 if (firstPacket)
  872.                 {
  873.                         firstPacket = false;
  874.                         limiter.headLng = cutHeader();
  875.                         todo = todo - limiter.headLng;
  876.                         fileSize1 = contLen / 1024;
  877.  
  878.                         if (!link.hasName)
  879.                         {
  880.                                 curWin.w = 66;
  881.                                 curWin.x = 39 - curWin.w / 2;
  882.                                 curWin.y = 9;
  883.                                 curWin.h = 1;
  884.                                 curWin.text = 103;
  885.                                 curWin.back = 103;
  886.  
  887.                                 strcpy(curWin.tittle, "Введите имя файла");
  888.                                 if (inputBox(curWin, link.fname))
  889.                                 {
  890.                                         strncpy(link.fname, cmd, 64);
  891.                                         strcat(link.fname, "\0");
  892.                                 }
  893.                         }
  894.                         if (httpErr != 200)
  895.                         {
  896.                                 netShutDown(socket, 0);
  897.                                 mainWinDraw();
  898.                                 return false;
  899.                         }
  900.                         downDialog();
  901.                         saveBuf(link.fname, 00, 0);
  902.                 }
  903.                 downloaded = downloaded + todo;
  904.                 down = downloaded / 1024;
  905.                 OS_SETCOLOR(223);
  906.                 sprintf(temp, "%4u  of %4u kb", down, fileSize1);
  907.                 OS_SETXY(38 - strlen(temp) / 2, 11);
  908.                 puts(temp);
  909.                 saveBuf(link.fname, 01, todo);
  910.                 drawClock();
  911.         } while (downloaded < contLen);
  912.  
  913.         netShutDown(socket, 0);
  914.  
  915.         if (downloaded != contLen)
  916.         {
  917.                 puts("File download error!");
  918.                 puts("File download error!");
  919.                 puts("File download error!");
  920.                 puts("File download error!");
  921.                 waitKey();
  922.         }
  923.         mainWinDraw();
  924.         return true;
  925. }
  926.  
  927. char getFile(unsigned char number)
  928. {
  929.         int result = 0;
  930.         unsigned char option = 1;
  931.  
  932.         for (option = 1; option <= table[number].option; option++)
  933.         {
  934.                 sprintf(link.path, "GET /get/%lu/%u%s%s%s", table[number].id, option, userAgent1, link.host, userAgent2);
  935.                 limiter.curOpt = option;
  936.                 switch (netDriver)
  937.                 {
  938.                 case 0:
  939.                         result = getFileNet();
  940.                         break;
  941.                 case 1:
  942.                         result = getFileEsp();
  943.                         break;
  944.                 default:
  945.                         break;
  946.                 }
  947.         }
  948.         return result;
  949. }
  950.  
  951. char makeRequestEsp(void)
  952. {
  953.  
  954.         int todo;
  955.         unsigned char byte, firstPacket;
  956.         unsigned long downloaded = 0;
  957.         unsigned int count;
  958.         const unsigned char sendOk[] = "SEND OK";
  959.  
  960.         sprintf(cmd, "AT+CIPSTART=\"TCP\",\"%s\",%u", link.host, link.port);
  961.         sendcommand(cmd);
  962.  
  963.         do
  964.         {
  965.                 getAnswer2(); // CONNECT or ERROR or link is not valid
  966.  
  967.                 if (strstr(netbuf, "CONNECT") != NULL)
  968.                 {
  969.                         break;
  970.                 }
  971.                 else
  972.                 {
  973.                         if (strstr(netbuf, "ERROR") != NULL)
  974.                         {
  975.                                 return false;
  976.                         }
  977.                 }
  978.         } while (42); // Try until endo of the days recieve CONNECT or ERROR
  979.  
  980.         getAnswer2(); // OK
  981.  
  982.         sprintf(cmd, "AT+CIPSEND=%u", strlen(link.path) + 2);
  983.         sendcommand(cmd);
  984.         getAnswer2();
  985.  
  986.         do
  987.         {
  988.                 byte = uart_readBlock();
  989.         } while (byte != '>');
  990.  
  991.         // sendcommandNrn(link.path);
  992.         sendcommand(link.path);
  993.  
  994.         count = 0;
  995.         do
  996.         {
  997.                 byte = uart_readBlock();
  998.                 if (byte == sendOk[count])
  999.                 {
  1000.                         count++;
  1001.                         // putchar(byte);
  1002.                 }
  1003.                 else
  1004.                 {
  1005.                         count = 0;
  1006.                 }
  1007.         } while (count < strlen(sendOk));
  1008.  
  1009.         uart_readBlock(); // CR
  1010.         uart_readBlock(); // LF
  1011.  
  1012.         firstPacket = true;
  1013.         do
  1014.         {
  1015.                 limiter.headLng = 0;
  1016.                 todo = recvHead();
  1017.                 getdataEsp(todo); // Requested size
  1018.                 if (firstPacket)
  1019.                 {
  1020.                         firstPacket = false;
  1021.                         limiter.headLng = cutHeader();
  1022.                         todo = todo - limiter.headLng;
  1023.  
  1024.                         if (httpErr != 200)
  1025.                         {
  1026.                                 sendcommand("AT+CIPCLOSE");
  1027.                                 getAnswer2(); // CLOSED
  1028.                                 getAnswer2(); // OK
  1029.                                 return false;
  1030.                         }
  1031.                 }
  1032.                 if (downloaded + todo > sizeof(buf))
  1033.                 {
  1034.                         printf("dataBuffer overrun... %lu reached \n\r", downloaded + todo);
  1035.                         return false;
  1036.                 }
  1037.                 memcpy(buf + downloaded, netbuf + limiter.headLng, todo);
  1038.                 downloaded = downloaded + todo;
  1039.         } while (downloaded < contLen);
  1040.  
  1041.         sendcommand("AT+CIPCLOSE");
  1042.         getAnswer2(); // CLOSED
  1043.         getAnswer2(); // OK
  1044.         buf[downloaded + 1] = 0;
  1045.         return downloaded;
  1046. }
  1047.  
  1048. char makeRequestNet(void)
  1049. {
  1050.         int socket, todo;
  1051.         char firstPacket;
  1052.         unsigned long downloaded = 0;
  1053.  
  1054.         if (!dnsResolve(link.host))
  1055.         {
  1056.                 clearStatus();
  1057.                 printf("Ошибка определения адреса '%s'", link.host);
  1058.                 return false;
  1059.         }
  1060.  
  1061.         targetadr.porth = link.port >> 8;
  1062.         targetadr.portl = link.port;
  1063.         // clearStatus();
  1064.         // printf("Connecting to %u.%u.%u.%u:%u", targetadr.b1, targetadr.b2, targetadr.b3, targetadr.b4, targetadr.porth * 256 + targetadr.portl);
  1065.  
  1066.         socket = OpenSock(AF_INET, SOCK_STREAM);
  1067.         if (testOperation2("OS_NETSOCKET", socket) != 1)
  1068.         {
  1069.                 getchar();
  1070.                 quit();
  1071.         }
  1072.  
  1073.         todo = netConnect(socket, 1);
  1074.         if (testOperation2("OS_NETCONNECT", todo) != 1)
  1075.         {
  1076.                 getchar();
  1077.                 quit();
  1078.         }
  1079.         todo = tcpSend(socket, (unsigned int)&link.path, strlen(link.path), 1);
  1080.         if (testOperation2("OS_WIZNETWRITE", todo) != 1)
  1081.         {
  1082.                 getchar();
  1083.                 quit();
  1084.         }
  1085.         firstPacket = true;
  1086.         do
  1087.         {
  1088.                 limiter.headLng = 0;
  1089.                 todo = tcpRead(socket, 1);
  1090.                 testOperation("OS_WIZNETREAD", todo); // Quit if too many retries
  1091.  
  1092.                 if (firstPacket)
  1093.                 {
  1094.                         firstPacket = false;
  1095.                         limiter.headLng = cutHeader();
  1096.                         todo = todo - limiter.headLng;
  1097.  
  1098.                         if (httpErr != 200)
  1099.                         {
  1100.                                 netShutDown(socket, 0);
  1101.                                 return false;
  1102.                         }
  1103.                 }
  1104.  
  1105.                 if (downloaded + todo > sizeof(buf))
  1106.                 {
  1107.                         printf("dataBuffer overrun... %lu reached \n\r", downloaded + todo);
  1108.                         return false;
  1109.                 }
  1110.                 memcpy(buf + downloaded, netbuf + limiter.headLng, todo);
  1111.                 downloaded = downloaded + todo;
  1112.         } while (downloaded != contLen); // ref < лучше
  1113.  
  1114.         netShutDown(socket, 0);
  1115.         buf[downloaded + 1] = 0;
  1116.         return downloaded;
  1117. }
  1118.  
  1119. char makeRequest(const char *request)
  1120. {
  1121.         char result;
  1122.         unsigned int counter, len;
  1123.         char tempreq[256];
  1124.         strcpy(tempreq, request);
  1125.         len = strlen(tempreq);
  1126.         for (counter = 0; counter < len; counter++)
  1127.         {
  1128.                 if (tempreq[counter] == ' ' || tempreq[counter] == '-')
  1129.                 {
  1130.                         tempreq[counter] = '*';
  1131.                 }
  1132.         }
  1133.         sprintf(link.path, "GET /?s=%s%s%s%s", tempreq, userAgent1, link.host, userAgent2);
  1134.  
  1135.         switch (netDriver)
  1136.         {
  1137.         case 0:
  1138.                 result = makeRequestNet();
  1139.                 break;
  1140.         case 1:
  1141.                 result = makeRequestEsp();
  1142.                 break;
  1143.  
  1144.         default:
  1145.                 return false;
  1146.         }
  1147.  
  1148.         return result;
  1149. }
  1150.  
  1151. int findLimiters(int n)
  1152. {
  1153.         n = pos(buf, "^", 1, n + 1);
  1154.         if (n == -1)
  1155.         {
  1156.                 return -2;
  1157.         }
  1158.         limiter.first = n + 1;
  1159.         n = pos(buf, "^", 1, n + 1);
  1160.         if (n == -1)
  1161.         {
  1162.                 return -2;
  1163.         }
  1164.         limiter.second = n - 1;
  1165.         return n;
  1166. }
  1167. void fillTable(void)
  1168. {
  1169.         int counter = 0;
  1170.         limiter.second = -1;
  1171.         limiter.total = 0;
  1172.         do
  1173.         {
  1174.                 if (findLimiters(limiter.second) != -2)
  1175.                 {
  1176.                         table[counter].id = atol(buf + limiter.first);
  1177.                         limiter.total++;
  1178.                 }
  1179.  
  1180.                 if (findLimiters(limiter.second) != -2)
  1181.                 {
  1182.                         strncpy(table[counter].name, buf + limiter.first, limiter.second - limiter.first + 1);
  1183.                         strcat(table[counter].name, "\0");
  1184.                         table[counter].name[limiter.second - limiter.first + 1] = 0;
  1185.                 }
  1186.                 if (findLimiters(limiter.second) != -2)
  1187.                 {
  1188.                         strncpy(table[counter].file, buf + limiter.first, limiter.second - limiter.first - 3);
  1189.                         strcat(table[counter].file, "\0");
  1190.                         table[counter].file[limiter.second - limiter.first - 3] = 0;
  1191.                         strncpy(table[counter].ext, buf + limiter.second - 2, 3);
  1192.                         strcat(table[counter].ext, "\0");
  1193.                 }
  1194.  
  1195.                 if (findLimiters(limiter.second) != -2)
  1196.                 {
  1197.                         table[counter].size = atol(buf + limiter.first);
  1198.                 }
  1199.  
  1200.                 if (findLimiters(limiter.second) != -2)
  1201.                 {
  1202.                         table[counter].option = atoi(buf + limiter.first);
  1203.                 }
  1204.  
  1205.                 if (findLimiters(limiter.second) != -2)
  1206.                 {
  1207.                         table[counter].year = atoi(buf + limiter.first);
  1208.                 }
  1209.  
  1210.                 limiter.second++;
  1211.                 counter++;
  1212.         } while (counter < 12);
  1213. }
  1214.  
  1215. void renderResult(char currentLine)
  1216. {
  1217.         int counter, line = 2;
  1218.  
  1219.         for (counter = 0; counter < limiter.total; counter++)
  1220.         {
  1221.                 // printf("[%lu] ", table[counter].id);
  1222.  
  1223.                 if (counter == currentLine)
  1224.                 {
  1225.                         OS_SETCOLOR(121);
  1226.                         OS_SETXY(2, line);
  1227.                         spaces(76);
  1228.                         OS_SETXY(2, line + 1);
  1229.                         spaces(76);
  1230.                 }
  1231.                 else
  1232.                 {
  1233.                         if (counter % 2 == 0)
  1234.                         {
  1235.                                 OS_SETCOLOR(206);
  1236.                         }
  1237.                         else
  1238.                         {
  1239.                                 OS_SETCOLOR(207);
  1240.                         }
  1241.                 }
  1242.                 OS_SETXY(2, line);
  1243.                 spaces(76);
  1244.                 OS_SETXY(2, line);
  1245.                 printf("%s ", table[counter].name);
  1246.                 OS_SETXY(48, line);
  1247.                 printf("%s  %6lu  %u  %4u", table[counter].ext, table[counter].size, table[counter].option, table[counter].year);
  1248.                 line++;
  1249.                 OS_SETXY(2, line);
  1250.                 spaces(76);
  1251.                 OS_SETXY(5, line);
  1252.                 printf("%s ", table[counter].file);
  1253.                 line++;
  1254.         }
  1255. }
  1256.  
  1257. char getKey(void)
  1258. {
  1259.         char key;
  1260.         key = OS_GETKEY();
  1261.         switch (key)
  1262.         {
  1263.         case 27: // escape - exit
  1264.                 OS_CLS(0);
  1265.                 OS_SETGFX(-1);
  1266.                 exit(0);
  1267.                 break;
  1268.         case 's':
  1269.         case 'ы':
  1270.         case 'Ы':
  1271.         case 'S':
  1272.         fuckingoto:
  1273.                 limiter.total = 0;
  1274.                 limiter.curline = 0;
  1275.                 curWin.w = 40;
  1276.                 curWin.x = 80 / 2 - curWin.w / 2 - 1;
  1277.                 curWin.y = 10;
  1278.                 curWin.h = 1;
  1279.                 curWin.text = 103;
  1280.                 curWin.back = 103;
  1281.                 strcpy(curWin.tittle, "Введите поисковый запрос");
  1282.  
  1283.                 if (inputBox(curWin, ""))
  1284.                 {
  1285.                         strcpy(search, cmd);
  1286.                 }
  1287.                 else
  1288.                 {
  1289.                         mainWinDraw();
  1290.                         OS_SETXY(32, 11);
  1291.                         OS_SETCOLOR(206);
  1292.                         puts("No results found");
  1293.                         limiter.total = 0;
  1294.                         limiter.curline = 0;
  1295.                         break;
  1296.                 }
  1297.                 mainWinDraw();
  1298.  
  1299.                 if (makeRequest(search) < 2)
  1300.                 {
  1301.                         OS_SETXY(32, 11);
  1302.                         OS_SETCOLOR(206);
  1303.                         puts("No results found");
  1304.                         limiter.total = 0;
  1305.                         limiter.curline = 0;
  1306.                         return key;
  1307.                 }
  1308.  
  1309.                 OS_SETXY(1, 2);
  1310.                 OS_SETCOLOR(206);
  1311.                 fillTable();
  1312.                 break;
  1313.         case 'h':
  1314.         case 'H':
  1315.         case 'р':
  1316.         case 'Р':
  1317.                 limiter.total = 0;
  1318.                 limiter.curline = 0;
  1319.                 curHost++;
  1320.                 if (curHost > 1)
  1321.                 {
  1322.                         curHost = 0;
  1323.                 }
  1324.                 strcpy(link.host, hosts[curHost]);
  1325.                 mainWinDraw();
  1326.                 break;
  1327.         case 'q':
  1328.         case 'Q':
  1329.         case 'Й':
  1330.         case 'й':
  1331.         case 250:
  1332.                 if (limiter.total != 0)
  1333.                 {
  1334.                         if (limiter.curline < 1)
  1335.                         {
  1336.                                 limiter.curline = limiter.total - 1;
  1337.                         }
  1338.                         else
  1339.                         {
  1340.                                 limiter.curline--;
  1341.                         }
  1342.                 }
  1343.                 break;
  1344.         case 'a':
  1345.         case 'A':
  1346.         case 'ф':
  1347.         case 'Ф':
  1348.         case 249:
  1349.                 if (limiter.total != 0)
  1350.                 {
  1351.                         if (limiter.curline > limiter.total - 2)
  1352.                         {
  1353.                                 limiter.curline = 0;
  1354.                         }
  1355.                         else
  1356.                         {
  1357.                                 limiter.curline++;
  1358.                         }
  1359.                 }
  1360.                 break;
  1361.         case 13:
  1362.                 if (limiter.total != 0)
  1363.                 {
  1364.                         getFile(limiter.curline);
  1365.                 }
  1366.                 else
  1367.                 {
  1368.                         goto fuckingoto;
  1369.                 }
  1370.                 break;
  1371.         default:
  1372.                 break;
  1373.         }
  1374.  
  1375.         if (key != 0)
  1376.         {
  1377.                 renderResult(limiter.curline);
  1378.         }
  1379.         else
  1380.         {
  1381.                 YIELD();
  1382.         }
  1383.         return key;
  1384. }
  1385.  
  1386. C_task main(int argc, const char *argv[])
  1387. {
  1388.         OS_HIDEFROMPARENT();
  1389.         OS_SETGFX(0x86);
  1390.         OS_CLS(0);
  1391.         OS_SETSYSDRV();
  1392.         init();
  1393.         // printTable();
  1394.         //  waitKey();
  1395.         OS_CLS(0);
  1396.         mainWinDraw();
  1397.  
  1398.         do
  1399.         {
  1400.                 getKey();
  1401.         } while (42);
  1402. }
  1403.