Subversion Repositories NedoOS

Rev

Rev 2405 | 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' Check espType may be you have 8266.");
  306.                         writeLog("Timeout waiting '+TIME_UPDATED'", "espntp_resolver");
  307.                 }
  308.         }
  309.  
  310. retryTime:
  311.         count = 0;
  312.         delay(300);
  313.         finish = time() + (5 * 50);
  314.         sendcommand("AT+CIPSNTPTIME?");
  315.  
  316.         if (!getAnswer3()) // TIME......
  317.         {
  318.                 puts("Timeout waiting answer to AT+CIPSNTPTIME?");
  319.                 writeLog("Timeout waiting answer to AT+CIPSNTPTIME?", "espntp_resolver");
  320.                 exit(255);
  321.         }
  322.  
  323.         count1 = strstr(netbuf, "+CIPSNTPTIME:");
  324.         if (count1 == NULL)
  325.         {
  326.                 puts("Error. No '+CIPSNTPTIME:' in answer ");
  327.                 writeLog("Error. No '+CIPSNTPTIME:' in answer", "espntp_resolver");
  328.                 exit(255);
  329.         }
  330.  
  331.         strncpy(cmd, netbuf + 13, 3);
  332.         cmd[3] = 0;
  333.  
  334.         if (cmd[0] == 'S' && cmd[1] == 'u')
  335.         {
  336.                 weekday = 1;
  337.         }
  338.         else if (cmd[0] == 'M' && cmd[1] == 'o')
  339.         {
  340.                 weekday = 2;
  341.         }
  342.         else if (cmd[0] == 'T' && cmd[1] == 'u')
  343.         {
  344.                 weekday = 3;
  345.         }
  346.         else if (cmd[0] == 'W' && cmd[1] == 'e')
  347.         {
  348.                 weekday = 4;
  349.         }
  350.         else if (cmd[0] == 'T' && cmd[1] == 'h')
  351.         {
  352.                 weekday = 5;
  353.         }
  354.         else if (cmd[0] == 'F' && cmd[1] == 'r')
  355.         {
  356.                 weekday = 6;
  357.         }
  358.         else if (cmd[0] == 'S' && cmd[1] == 'a')
  359.         {
  360.                 weekday = 7;
  361.         }
  362.  
  363.         strncpy(cmd, netbuf + 4 + 13, 3);
  364.         cmd[3] = 0;
  365.  
  366.         if (cmd[0] == 'J' && cmd[1] == 'a')
  367.         {
  368.                 month = 1;
  369.         }
  370.         else if (cmd[0] == 'F' && cmd[1] == 'e')
  371.         {
  372.                 month = 2;
  373.         }
  374.         else if (cmd[0] == 'M' && cmd[2] == 'r')
  375.         {
  376.                 month = 3;
  377.         }
  378.         else if (cmd[0] == 'A' && cmd[1] == 'p')
  379.         {
  380.                 month = 4;
  381.         }
  382.         else if (cmd[0] == 'M' && cmd[2] == 'y')
  383.         {
  384.                 month = 5;
  385.         }
  386.         else if (cmd[0] == 'J' && cmd[2] == 'n')
  387.         {
  388.                 month = 6;
  389.         }
  390.         else if (cmd[0] == 'J' && cmd[2] == 'l')
  391.         {
  392.                 month = 7;
  393.         }
  394.         else if (cmd[0] == 'A' && cmd[1] == 'u')
  395.         {
  396.                 month = 8;
  397.         }
  398.         else if (cmd[0] == 'S' && cmd[1] == 'e')
  399.         {
  400.                 month = 9;
  401.         }
  402.         else if (cmd[0] == 'O' && cmd[1] == 'c')
  403.         {
  404.                 month = 10;
  405.         }
  406.         else if (cmd[0] == 'N' && cmd[1] == 'o')
  407.         {
  408.                 month = 11;
  409.         }
  410.         else if (cmd[0] == 'D' && cmd[1] == 'e')
  411.         {
  412.                 month = 12;
  413.         }
  414.  
  415.         strncpy(cmd, netbuf + 8 + 13, 2);
  416.         cmd[2] = 0;
  417.         day = atoi(cmd);
  418.  
  419.         strncpy(cmd, netbuf + 11 + 13, 2);
  420.         hour = atoi(cmd);
  421.  
  422.         strncpy(cmd, netbuf + 14 + 13, 2);
  423.         minute = atoi(cmd);
  424.  
  425.         strncpy(cmd, netbuf + 17 + 13, 2);
  426.         second = atoi(cmd);
  427.  
  428.         strncpy(cmd, netbuf + 22 + 13, 2);
  429.         cmd[4] = 0;
  430.         year = atoi(cmd) + 100;
  431.  
  432.         // 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);
  433.  
  434.         if (!getAnswer3()) // OK
  435.         {
  436.                 puts("Timeout waiting last OK. Continue");
  437.                 writeLog("Timeout waiting last OK. Continue", "espntp_resolver");
  438.         }
  439.  
  440.         if (year == 170)
  441.         {
  442.                 YIELD();
  443.                 if (retry == 0)
  444.                 {
  445.                         puts("error getting time...");
  446.                         exit(255);
  447.                 }
  448.  
  449.                 retry--;
  450.                 printf("Retry [NTP][%u]\r\n", retry);
  451.                 writeLog("Retry [NTP] incorrect year", "espntp_resolver");
  452.                 delay(500);
  453.                 goto retryTime;
  454.         }
  455. }
  456.  
  457. void set_datetime(void)
  458. {
  459.         writecmos(0x0b, readcmos(0x0b) | 6);
  460.         writecmos(0x07, day);
  461.         writecmos(0x08, month);
  462.         if (is_atm == 2 || is_atm == 3)
  463.         {
  464.                 writecmos(0x09, year - 80);
  465.         }
  466.         else
  467.         {
  468.                 writecmos(0x09, year - 100);
  469.         }
  470.  
  471.         writecmos(0x00, second);
  472.         writecmos(0x02, minute);
  473.         writecmos(0x04, hour);
  474. }
  475. void get_datetime(void)
  476. {
  477.         writecmos(0x0b, readcmos(0x0b) | 6);
  478.         second = readcmos(0x00);
  479.         minute = readcmos(0x02);
  480.         hour = readcmos(0x04);
  481.         weekday = readcmos(0x06) - 1;
  482.         day = readcmos(0x07);
  483.         month = readcmos(0x08);
  484.         if (is_atm == 2 || is_atm == 3)
  485.         {
  486.                 year = readcmos(0x09) + 80;
  487.         }
  488.         else
  489.         {
  490.                 year = readcmos(0x09) + 100;
  491.         }
  492. }
  493.  
  494. C_task main(int argc, char *argv[])
  495. {
  496.         unsigned char i = 1;
  497.         os_initstdio();
  498.         printf("[TIME2 Build:%s  %s]\r\n\r\n", __DATE__, __TIME__);
  499.         is_atm = (unsigned char)OS_GETCONFIG();
  500.  
  501.         if (argc == 1)
  502.         {
  503.                 get_datetime();
  504.                 puts(help);
  505.         }
  506.  
  507.         while (i != argc)
  508.         {
  509.                 char *p = argv[i];
  510.                 if (p[0] != '-')
  511.                         exit((int)"Wrong parameter. Use -H for help");
  512.                 switch (p[1] & 0xdf)
  513.                 {
  514.                 case 'T':
  515.                         get_datetime();
  516.                         if (sscanf(p + 2, "%d:%d:%d", &hour, &minute, &second) == 3)
  517.                         {
  518.                                 disable_interrupt();
  519.                                 set_datetime();
  520.                                 enable_interrupt();
  521.                         }
  522.                         break;
  523.                 case 'D':
  524.                         get_datetime();
  525.                         if (sscanf(p + 2, "%d-%d-%d", &day, &month, &year) == 3)
  526.                         {
  527.                                 disable_interrupt();
  528.                                 year -= 1900;
  529.                                 set_datetime();
  530.                                 enable_interrupt();
  531.                         }
  532.                         break;
  533.                 case 'N':
  534.                         defntp = p + 2;
  535.                         break;
  536.                 case 'Z':
  537.                         if (sscanf(p + 2, "%d", &GMT) != 1)
  538.                         {
  539.                                 GMT = 3;
  540.                         }
  541.                         break;
  542.                 case 'H':
  543.                         exit((int)help);
  544.                         break;
  545.                 case 'I':
  546.                         inet = 1;
  547.                         break;
  548.                 case 'E':
  549.                         espInet = 1;
  550.                         break;
  551.  
  552.                 default:
  553.                         exit((int)"Wrong parameter. Use -H for help");
  554.                 }
  555.                 i++;
  556.         }
  557.         if (inet)
  558.         {
  559.                 ntp_resolver();
  560.                 set_datetime();
  561.                 writecmos(0x06, weekday + 1);
  562.         }
  563.         if (espInet)
  564.         {
  565.                 espntp_resolver();
  566.                 set_datetime();
  567.                 writecmos(0x06, weekday + 1);
  568.                 uartFlush(500);
  569.         }
  570.         puts("Now time:");
  571.         printf("%02u-%02u-%04u ", day, month, year + 1900);
  572.         printf("%02u:%02u:%02u\r\n", hour, minute, second);
  573.         exit(0);
  574.         return 0;
  575. }
  576.