logtime.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. /*!
  2. * Copyright (C) 2001-2005 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. */
  34. /*!
  35. * $Id: logtime.c 4640 2012-09-24 12:05:56Z u_bonnes $
  36. */
  37. #include <stdio.h>
  38. #include <io.h>
  39. #include <arpa/inet.h>
  40. #include <net/route.h>
  41. #include <pro/dhcp.h>
  42. #include <pro/sntp.h>
  43. #include <sys/version.h>
  44. #include <sys/confnet.h>
  45. #include <sys/timer.h>
  46. #include <sys/heap.h>
  47. #include <sys/syslog.h>
  48. #include <dev/board.h>
  49. /*!
  50. * \example logtime/logtime.c
  51. *
  52. * Shows how to use syslog and SNTP.
  53. */
  54. static char *version = "1.0.1";
  55. /*
  56. * User configuration.
  57. */
  58. /*! \brief Default MAC.
  59. * Used if EEPROM is empty. */
  60. #define MYMAC 0x00, 0x06, 0x98, 0x00, 0x00, 0x00
  61. /*! \brief Default local IP address.
  62. * Used if EEPROM configuration and DHCP is unavailable. */
  63. #define MYIP "192.168.192.100"
  64. /*! \brief Default IP mask.
  65. * Used if EEPROM configuration and DHCP is unavailable. */
  66. #define MYMASK "255.255.255.0"
  67. /*! \brief Default gateway address.
  68. * Used if EEPROM configuration and DHCP is unavailable.
  69. * Only required if syslog or time server are located in
  70. * a different network. */
  71. #define MYGATE "192.168.192.1"
  72. /*! \brief IP address of the host running a syslog daemon. */
  73. #define MYLOGD "192.168.192.222"
  74. /*! \brief IP address of the host running a time daemon. */
  75. #define MYTIMED "130.149.17.21"
  76. /*! \brief Device name used for UART output. */
  77. #define MYUART DEV_CONSOLE.dev_name
  78. /*! \brief UART baudrate. */
  79. #define MYBAUD 115200
  80. /*! \brief Output device. */
  81. #define MYDEV DEV_CONSOLE
  82. /*! \brief Local timezone, -1 for Central Europe. */
  83. #define MYTZ -1
  84. #ifdef __IMAGECRAFT__
  85. #define COMPILERNAME "ICCAVR"
  86. #else
  87. #define COMPILERNAME "GCC"
  88. #endif
  89. /* Result codes. */
  90. #define UART_OK 0x0001
  91. #define STDOUT_OK 0x0002
  92. #define STDERR_OK 0x0004
  93. #define BAUDRATE_OK 0x0008
  94. #define LANDEV_OK 0x0010
  95. #define NETIF_OK 0x0020
  96. #define NETROUTE_OK 0x0040
  97. #define TIMED_OK 0x0080
  98. /*
  99. * Application entry.
  100. */
  101. int main(void)
  102. {
  103. uint32_t baud = MYBAUD;
  104. uint8_t mac[6] = { MYMAC };
  105. uint32_t timeserver = inet_addr(MYTIMED);
  106. int rc = 0;
  107. time_t now;
  108. /*
  109. * Register UART devices, assign stdout and stderr to this device
  110. * and set the baudrate.
  111. */
  112. if(NutRegisterDevice(&MYDEV, 0, 0) == 0) {
  113. rc |= UART_OK;
  114. if(freopen(MYUART, "w", stdout)) {
  115. rc |= STDOUT_OK;
  116. if(_ioctl(_fileno(stdout), UART_SETSPEED, &baud) == 0) {
  117. rc |= BAUDRATE_OK;
  118. }
  119. }
  120. if(freopen(MYUART, "w", stderr)) {
  121. rc |= STDERR_OK;
  122. }
  123. }
  124. /*
  125. * Print banner.
  126. */
  127. if(rc & STDOUT_OK) {
  128. printf("\n\nTimeLog %s\nNut/OS %s\n", version, NutVersionString());
  129. puts("Compiled by " COMPILERNAME);
  130. puts("Configure network");
  131. }
  132. #ifdef DEV_ETHER
  133. /*
  134. * Register LAN device and configure network interface.
  135. */
  136. if(NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
  137. rc |= LANDEV_OK;
  138. if (NutDhcpIfConfig("eth0", 0, 60000) == 0) {
  139. rc |= NETIF_OK;
  140. }
  141. else if (NutDhcpIfConfig("eth0", mac, 60000) == 0) {
  142. rc |= NETIF_OK;
  143. }
  144. else if(NutNetIfConfig("eth0", mac, inet_addr(MYIP), inet_addr(MYMASK)) == 0) {
  145. rc |= NETIF_OK;
  146. #ifdef MYGATE
  147. if(NutIpRouteAdd(0, 0, inet_addr(MYGATE), &DEV_ETHER) == 0) {
  148. rc |= NETROUTE_OK;
  149. }
  150. #endif
  151. }
  152. }
  153. if(rc & NETIF_OK) {
  154. /*
  155. * Set timezone, query SNTP server and set system time.
  156. */
  157. if(rc & STDOUT_OK) {
  158. puts("Query time from " MYTIMED);
  159. }
  160. _timezone = MYTZ * 60L * 60L;
  161. if(NutSNTPGetTime(&timeserver, &now) == 0) {
  162. rc |= TIMED_OK;
  163. stime(&now);
  164. }
  165. }
  166. /*
  167. * Open syslog output and route messages to stderr and to
  168. * a remote server.
  169. */
  170. if(rc & STDOUT_OK) {
  171. puts("Initialize syslog");
  172. }
  173. openlog("logtime", (rc & STDERR_OK) ? LOG_PERROR : 0, LOG_USER);
  174. if(rc & NETIF_OK) {
  175. setlogserver(inet_addr(MYLOGD), 0);
  176. }
  177. syslog(LOG_INFO, "TimeLog %s started on Nut/OS %s", version, NutVersionString());
  178. /*
  179. * Print the result of our initialization.
  180. */
  181. if((rc & UART_OK) == 0) {
  182. syslog(LOG_ERR, "Registering UART device failed");
  183. }
  184. else if((rc & STDOUT_OK) == 0) {
  185. syslog(LOG_ERR, "Assigning stdout failed");
  186. }
  187. else if((rc & STDERR_OK) == 0) {
  188. syslog(LOG_ERR, "Assigning stderr failed");
  189. }
  190. else if((rc & BAUDRATE_OK) == 0) {
  191. syslog(LOG_ERR, "Setting baudrate failed");
  192. }
  193. if((rc & LANDEV_OK) == 0) {
  194. syslog(LOG_ERR, "Registering Ethernet device failed");
  195. }
  196. else if((rc & NETIF_OK) == 0) {
  197. syslog(LOG_ERR, "Configuring network interface failed");
  198. }
  199. else {
  200. syslog(LOG_INFO, "IP %s", inet_ntoa(confnet.cdn_ip_addr));
  201. syslog(LOG_INFO, "Gate %s", inet_ntoa(confnet.cdn_gateway));
  202. syslog(LOG_INFO, "Timed " MYTIMED);
  203. syslog(LOG_INFO, "Syslogd " MYLOGD);
  204. }
  205. /*
  206. * Endless loop.
  207. */
  208. for(;;) {
  209. syslog(LOG_DEBUG, "%d bytes free", NutHeapAvailable());
  210. NutSleep(60000);
  211. }
  212. #endif
  213. return 0;
  214. }