Subversion Repositories NedoOS

Rev

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

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <oscalls.h>
  4. #include <socket.h>
  5. #include <intrz80.h>
  6. #include <osfs.h>
  7. #include <stdlib.h>
  8. ///////
  9. #define true 1
  10. #define false 0
  11.  
  12. unsigned int RBR_THR = 0xf8ef;
  13. unsigned int IER = 0xf9ef;
  14. unsigned int IIR_FCR = 0xfaef;
  15. unsigned int LCR = 0xfbef;
  16. unsigned int MCR = 0xfcef;
  17. unsigned int LSR = 0xfdef;
  18. unsigned int MSR = 0xfeef;
  19. unsigned int SR = 0xffef;
  20. unsigned int divider = 1;
  21. unsigned int comType = 0;
  22. unsigned int espType = 32;
  23. unsigned int espRetry = 5;
  24. unsigned long factor, timerok;
  25. unsigned char cmd[512];
  26. const unsigned char sendOk[] = "SEND OK";
  27. const unsigned char gotWiFi[] = "WIFI GOT IP";
  28. const unsigned char timeUpdated[] = "+CIPSNTPTIME:";
  29. int GMT = 3;
  30. unsigned char is_atm;
  31. unsigned char netbuf[4 * 1024];
  32. struct sockaddr_in ntp_ia;
  33. union
  34. {
  35.         unsigned long ul;
  36.         unsigned char b[4];
  37. } secsUnix;
  38. unsigned int hour, minute, second, day, month, year, weekday;
  39. SOCKET s = 0;
  40. unsigned char inet = 0, espInet = 0;
  41. const unsigned char monthDays[12] =
  42.         {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  43. const unsigned char ntpnead[48] =
  44.         {
  45.                 0xdb,
  46.                 0x00,
  47.                 0x11,
  48.                 0xfa,
  49.                 0x00,
  50.                 0x00,
  51.                 0x00,
  52.                 0x00,
  53.                 0x00,
  54.                 0x01,
  55.                 0x03,
  56.                 0xfe,
  57. };
  58. unsigned char *defntp = "2.ru.pool.ntp.org";
  59. const unsigned char regaddr_ve[16] = {0x10, 0, 0x50, 0, 0x90, 0, 0, 0x12, 0x52, 0x92, 0, 0, 0, 0, 0, 0};
  60.  
  61. const unsigned char help[] = "\
  62. -H help\r\n\
  63. -T set time(-T17:59:38)\r\n\
  64. -D set date(-D21-06-2019)\r\n\
  65. -N ntp-server default: -N2.ru.pool.ntp.org\r\n\
  66. -Z time-zone default: -Z3\r\n\
  67. -i get datetime from internet\r\n\
  68. -e get datetime from ESP-COM";
  69.  
  70. extern void
  71. dns_resolve(void);
  72.  
  73. void clearStatus(void)
  74. {
  75. }
  76.  
  77. void delay(unsigned long counter)
  78. {
  79.         unsigned long finish;
  80.         counter = counter / 20;
  81.         if (counter < 1)
  82.         {
  83.                 counter = 1;
  84.         }
  85.         finish = time() + counter;
  86.  
  87.         while (time() < finish)
  88.         {
  89.         }
  90. }
  91.  
  92. void exit(int e)
  93. {
  94.         if (s)
  95.                 closesocket(s, 0);
  96.         if (e != 0)
  97.         {
  98.                 puts((char *)e);
  99.         }
  100.         ((void (*)(int))0x0000)(e);
  101. }
  102.  
  103. extern void dns_resolve(void);
  104.  
  105. unsigned char readcmos(unsigned char r)
  106. {
  107.         disable_interrupt();
  108.         if (is_atm == 2 || is_atm == 3)
  109.         {
  110.                 r = regaddr_ve[r];
  111.                 if (r != 0)
  112.                 {
  113.                         input(0x55FE);
  114.                         r = input((r << 8) | 0x00fe);
  115.                 }
  116.         }
  117.         else
  118.         {
  119.                 output(0xdef7, r);
  120.                 r = input(0xbef7);
  121.         }
  122.         enable_interrupt();
  123.         return r;
  124. }
  125.  
  126. void writecmos(unsigned char r, unsigned char v)
  127. {
  128.         disable_interrupt();
  129.         if (is_atm == 2 || is_atm == 3)
  130.         {
  131.                 r = regaddr_ve[r] + 1; // На запись порт + 1
  132.                 if (r != 0)
  133.                 {
  134.                         input(0x55FE);
  135.                         input((r << 8) | 0x00fe);
  136.                         input((v << 8) | 0x00fe);
  137.                 }
  138.         }
  139.         else
  140.         {
  141.                 output(0xdef7, r);
  142.                 output(0xbef7, v);
  143.         }
  144.         enable_interrupt();
  145. }
  146.  
  147. void Unix_to_GMT(void)
  148. {
  149.         unsigned char monthLength = 0;
  150.         // корректировка часового пояса и синхронизация
  151.         int days = 0;
  152.         secsUnix.ul = secsUnix.ul + GMT * 3600;
  153.  
  154.         second = secsUnix.ul % 60;
  155.         secsUnix.ul /= 60; // now it is minutes
  156.         minute = secsUnix.ul % 60;
  157.         secsUnix.ul /= 60; // now it is hours
  158.         hour = secsUnix.ul % 24;
  159.         secsUnix.ul /= 24;                               // now it is days
  160.         weekday = (secsUnix.ul + 4) % 7; // day week, 0-sunday
  161.         year = 70;
  162.         while (days + ((year % 4) ? 365 : 366) <= secsUnix.ul)
  163.         {
  164.                 days += (year % 4) ? 365 : 366;
  165.                 year++;
  166.         }
  167.         secsUnix.ul -= days; // now it is days in this year, starting at 0
  168.  
  169.         days = 0;
  170.         month = 0;
  171.         for (month = 0; month < 12; month++)
  172.         {
  173.                 if (month == 1)
  174.                 { // february
  175.                         if (year % 4)
  176.                                 monthLength = 28;
  177.                         else
  178.                                 monthLength = 29;
  179.                 }
  180.                 else
  181.                         monthLength = monthDays[month];
  182.                 if (secsUnix.ul >= monthLength)
  183.                         secsUnix.ul -= monthLength;
  184.                 else
  185.                         break;
  186.         }
  187.         month++;                           // jan is month 1
  188.         day = secsUnix.ul + 1; // day of month
  189. }
  190. void ntp_resolver(void)
  191. {
  192.         unsigned char i, j;
  193.         signed char res;
  194.         int len;
  195.         ntp_ia.sin_port = 123 << 8;
  196.         ntp_ia.sin_addr = *dns_resolver((void *)defntp);
  197.         if (!ntp_ia.sin_addr.S_un.S_addr)
  198.                 exit((int)"error: domain name not resolved");
  199.         i = 200;
  200. inetloop:
  201.         YIELD();
  202.         i--;
  203.         YIELD();
  204.         if (i == 0)
  205.         {
  206.                 exit((int)"inet error");
  207.         }
  208.         s = socket(AF_INET, SOCK_DGRAM, 0);
  209.         if (s < 0)
  210.         {
  211.                 s = 0;
  212.                 goto inetloop;
  213.         }
  214.         memcpy(netbuf, ntpnead, sizeof(ntpnead));
  215.  
  216.         len = sendto(s, netbuf, 48, 0, &ntp_ia, sizeof(ntp_ia));
  217.         if (res < 0)
  218.         {
  219.                 closesocket(s, 0);
  220.                 s = 0;
  221.                 goto inetloop;
  222.         }
  223.         j = 50;
  224.         while (j)
  225.         {
  226.                 j--;
  227.                 len = recvfrom(s, netbuf, sizeof(netbuf), 0, &ntp_ia, sizeof(ntp_ia));
  228.                 if (len < 0)
  229.                 {
  230.                         YIELD();
  231.                         YIELD();
  232.                         continue;
  233.                 }
  234.                 break;
  235.         }
  236.  
  237.         closesocket(s, 0);
  238.         s = 0;
  239.         if (len <= 0)
  240.         {
  241.                 exit((int)"server error");
  242.         }
  243.         secsUnix.b[3] = netbuf[40];
  244.         secsUnix.b[2] = netbuf[41];
  245.         secsUnix.b[1] = netbuf[42];
  246.         secsUnix.b[0] = netbuf[43];
  247.         secsUnix.ul -= 2208988800UL;
  248.         Unix_to_GMT();
  249. }
  250.  
  251. ///////////////////////////
  252. #include <../common/esp-com.c>
  253. //////////////////////////
  254. void espntp_resolver(void)
  255. {
  256.         unsigned char retry, retryuart, count = 0;
  257.         unsigned int byte;
  258.         unsigned long finish;
  259.         unsigned char *count1;
  260.         loadEspConfig();
  261.         uart_init(divider);
  262.         if (!espReBoot())
  263.         {
  264.                 puts("\r\nerror ESP init...");
  265.                 exit(255);
  266.         }
  267.         puts("\r\nGetting time...");
  268.  
  269.         // AT+CIPSNTPCFG=1,8,"cn.ntp.org.cn","ntp.sjtu.edu.cn"
  270.         weekday = 0;
  271.         month = 0;
  272.         day = 0;
  273.         hour = 0;
  274.         second = 0;
  275.         year = 170;
  276.         retry = 10;
  277.         retryuart = 3;
  278.         sprintf(cmd, "AT+CIPSNTPCFG=1,%u,\"%s\",\"time.google.com\"", GMT, defntp);
  279.         sendcommand(cmd);
  280.         getAnswer3(); // OK
  281.  
  282.         count1 = strstr(netbuf, "ERROR");
  283.         if (count1)
  284.         {
  285.                 printf("Error. You may need to update your AT-Firmware, to a version that supports AT+CIPSNTPCFG");
  286.                 exit(255);
  287.         }
  288.  
  289. retryTime:
  290.         count = 0;
  291.         delay(300);
  292.         finish = time() + 5 * 50;
  293.         sendcommand("AT+CIPSNTPTIME?");
  294.         do
  295.         {
  296.                 byte = uartReadBlock();
  297.                 // printf("[%c]", byte);
  298.                 if (byte == timeUpdated[count])
  299.                 {
  300.                         count++;
  301.                         putchar(byte);
  302.                 }
  303.                 else
  304.                 {
  305.                         count = 0;
  306.                 }
  307.  
  308.                 if (time() > finish)
  309.                 {
  310.                         puts("error getting time...");
  311.                         exit(255);
  312.                 }
  313.         } while (count < strlen(timeUpdated));
  314.  
  315.         if (!getAnswer3()) // TIME
  316.         {
  317.                 if (retryuart != 0)
  318.                 {
  319.                         retryuart--;
  320.                         printf("Retry [UART][%u]\r\n", retryuart);
  321.                         delay(500);
  322.                         goto retryTime;
  323.                 }
  324.                 puts("error getting time...");
  325.                 exit(255);
  326.         }
  327.  
  328.         strncpy(cmd, netbuf, 3);
  329.         cmd[3] = 0;
  330.  
  331.         if (cmd[0] == 'S' && cmd[1] == 'u')
  332.         {
  333.                 weekday = 1;
  334.         }
  335.         else if (cmd[0] == 'M' && cmd[1] == 'o')
  336.         {
  337.                 weekday = 2;
  338.         }
  339.         else if (cmd[0] == 'T' && cmd[1] == 'u')
  340.         {
  341.                 weekday = 3;
  342.         }
  343.         else if (cmd[0] == 'W' && cmd[1] == 'e')
  344.         {
  345.                 weekday = 4;
  346.         }
  347.         else if (cmd[0] == 'T' && cmd[1] == 'h')
  348.         {
  349.                 weekday = 5;
  350.         }
  351.         else if (cmd[0] == 'F' && cmd[1] == 'r')
  352.         {
  353.                 weekday = 6;
  354.         }
  355.         else if (cmd[0] == 'S' && cmd[1] == 'a')
  356.         {
  357.                 weekday = 7;
  358.         }
  359.  
  360.         strncpy(cmd, netbuf + 4, 3);
  361.         cmd[3] = 0;
  362.  
  363.         if (cmd[0] == 'J' && cmd[1] == 'a')
  364.         {
  365.                 month = 1;
  366.         }
  367.         else if (cmd[0] == 'F' && cmd[1] == 'e')
  368.         {
  369.                 month = 2;
  370.         }
  371.         else if (cmd[0] == 'M' && cmd[2] == 'r')
  372.         {
  373.                 month = 3;
  374.         }
  375.         else if (cmd[0] == 'A' && cmd[1] == 'p')
  376.         {
  377.                 month = 4;
  378.         }
  379.         else if (cmd[0] == 'M' && cmd[2] == 'y')
  380.         {
  381.                 month = 5;
  382.         }
  383.         else if (cmd[0] == 'J' && cmd[2] == 'n')
  384.         {
  385.                 month = 6;
  386.         }
  387.         else if (cmd[0] == 'J' && cmd[2] == 'l')
  388.         {
  389.                 month = 7;
  390.         }
  391.         else if (cmd[0] == 'A' && cmd[1] == 'u')
  392.         {
  393.                 month = 8;
  394.         }
  395.         else if (cmd[0] == 'S' && cmd[1] == 'e')
  396.         {
  397.                 month = 9;
  398.         }
  399.         else if (cmd[0] == 'O' && cmd[1] == 'c')
  400.         {
  401.                 month = 10;
  402.         }
  403.         else if (cmd[0] == 'N' && cmd[1] == 'o')
  404.         {
  405.                 month = 11;
  406.         }
  407.         else if (cmd[0] == 'D' && cmd[1] == 'e')
  408.         {
  409.                 month = 12;
  410.         }
  411.  
  412.         strncpy(cmd, netbuf + 8, 2);
  413.         cmd[2] = 0;
  414.         day = atoi(cmd);
  415.  
  416.         strncpy(cmd, netbuf + 11, 2);
  417.         hour = atoi(cmd);
  418.  
  419.         strncpy(cmd, netbuf + 14, 2);
  420.         minute = atoi(cmd);
  421.  
  422.         strncpy(cmd, netbuf + 17, 2);
  423.         second = atoi(cmd);
  424.  
  425.         strncpy(cmd, netbuf + 22, 2);
  426.         cmd[4] = 0;
  427.         year = atoi(cmd) + 100;
  428.  
  429.         getAnswer3(); // OK
  430.  
  431.         // printf("day of week:%u Month:%u day:%u hours:%u minutes:%u seconds:%u year:%u\r\n", weekday, month, day, hour, minute, second, year);
  432.  
  433.         if (year == 170)
  434.         {
  435.                 YIELD();
  436.                 if (retry != 0)
  437.                 {
  438.                         retry--;
  439.                         printf("Retry [NTP][%u]\r\n", retry);
  440.                         delay(500);
  441.                         goto retryTime;
  442.                 }
  443.                 puts("error getting time...");
  444.                 exit(255);
  445.         }
  446. }
  447.  
  448. void set_datetime(void)
  449. {
  450.         writecmos(0x0b, readcmos(0x0b) | 6);
  451.         writecmos(0x07, day);
  452.         writecmos(0x08, month);
  453.         if (is_atm == 2 || is_atm == 3)
  454.         {
  455.                 writecmos(0x09, year - 80);
  456.         }
  457.         else
  458.         {
  459.                 writecmos(0x09, year - 100);
  460.         }
  461.  
  462.         writecmos(0x00, second);
  463.         writecmos(0x02, minute);
  464.         writecmos(0x04, hour);
  465. }
  466. void get_datetime(void)
  467. {
  468.         writecmos(0x0b, readcmos(0x0b) | 6);
  469.         second = readcmos(0x00);
  470.         minute = readcmos(0x02);
  471.         hour = readcmos(0x04);
  472.         weekday = readcmos(0x06) - 1;
  473.         day = readcmos(0x07);
  474.         month = readcmos(0x08);
  475.         if (is_atm == 2 || is_atm == 3)
  476.         {
  477.                 year = readcmos(0x09) + 80;
  478.         }
  479.         else
  480.         {
  481.                 year = readcmos(0x09) + 100;
  482.         }
  483. }
  484.  
  485. C_task main(int argc, char *argv[])
  486. {
  487.         unsigned char i = 1;
  488.         os_initstdio();
  489.         printf("[Build:%s  %s]", __DATE__, __TIME__);
  490.         is_atm = (unsigned char)OS_GETCONFIG();
  491.  
  492.         if (argc == 1)
  493.         {
  494.                 get_datetime();
  495.                 puts(help);
  496.         }
  497.         while (i != argc)
  498.         {
  499.                 char *p = argv[i];
  500.                 if (p[0] != '-')
  501.                         exit((int)"Wrong parameter. Use -H for help");
  502.                 switch (p[1] & 0xdf)
  503.                 {
  504.                 case 'T':
  505.                         get_datetime();
  506.                         if (sscanf(p + 2, "%d:%d:%d", &hour, &minute, &second) == 3)
  507.                         {
  508.                                 disable_interrupt();
  509.                                 set_datetime();
  510.                                 enable_interrupt();
  511.                         }
  512.                         break;
  513.                 case 'D':
  514.                         get_datetime();
  515.                         if (sscanf(p + 2, "%d-%d-%d", &day, &month, &year) == 3)
  516.                         {
  517.                                 disable_interrupt();
  518.                                 year -= 1900;
  519.                                 set_datetime();
  520.                                 enable_interrupt();
  521.                         }
  522.                         break;
  523.                 case 'N':
  524.                         defntp = p + 2;
  525.                         break;
  526.                 case 'Z':
  527.                         if (sscanf(p + 2, "%d", &GMT) != 1)
  528.                         {
  529.                                 GMT = 3;
  530.                         }
  531.                         break;
  532.                 case 'H':
  533.                         exit((int)help);
  534.                         break;
  535.                 case 'I':
  536.                         inet = 1;
  537.                         break;
  538.                 case 'E':
  539.                         espInet = 1;
  540.                         break;
  541.  
  542.                 default:
  543.                         exit((int)"Wrong parameter. Use -H for help");
  544.                 }
  545.                 i++;
  546.         }
  547.         if (inet)
  548.         {
  549.                 ntp_resolver();
  550.                 set_datetime();
  551.                 writecmos(0x06, weekday + 1);
  552.         }
  553.         if (espInet)
  554.         {
  555.                 espntp_resolver();
  556.                 set_datetime();
  557.                 writecmos(0x06, weekday + 1);
  558.                 uartFlush(500);
  559.         }
  560.         puts("Now time:");
  561.         printf("%02u-%02u-%04u ", day, month, year + 1900);
  562.         printf("%02u:%02u:%02u\r\n", hour, minute, second);
  563.         exit(0);
  564.         return 0;
  565. }
  566.