Subversion Repositories NedoOS

Rev

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