Nut/OS  4.10.3
API Reference
logtime.c
Go to the documentation of this file.
00001 
00039 #include <stdio.h>
00040 #include <io.h>
00041 
00042 #include <arpa/inet.h>
00043 #include <net/route.h>
00044 #include <pro/dhcp.h>
00045 #include <pro/sntp.h>
00046 
00047 #include <sys/version.h>
00048 #include <sys/confnet.h>
00049 #include <sys/timer.h>
00050 #include <sys/heap.h>
00051 #include <sys/syslog.h>
00052 
00053 #include <dev/board.h>
00054 
00060 static char *version = "1.0.1";
00061 
00062 /*
00063  * User configuration.
00064  */
00065 
00068 #define MYMAC   0x00, 0x06, 0x98, 0x00, 0x00, 0x00
00069 
00072 #define MYIP    "192.168.192.100"
00073 
00076 #define MYMASK  "255.255.255.0"
00077 
00082 #define MYGATE  "192.168.192.1"
00083 
00085 #define MYLOGD  "192.168.192.222"
00086 
00088 #define MYTIMED "130.149.17.21"
00089 
00091 #define MYUART  DEV_CONSOLE_NAME
00092 
00094 #define MYBAUD  115200
00095 
00097 #define MYDEV   DEV_CONSOLE
00098 
00100 #define MYTZ    -1
00101 
00102 
00103 #ifdef __IMAGECRAFT__
00104 #define COMPILERNAME "ICCAVR"
00105 #else
00106 #define COMPILERNAME "GCC"
00107 #endif
00108 
00109 /* Result codes. */
00110 #define UART_OK     0x0001
00111 #define STDOUT_OK   0x0002
00112 #define STDERR_OK   0x0004
00113 #define BAUDRATE_OK 0x0008
00114 #define LANDEV_OK   0x0010
00115 #define NETIF_OK    0x0020
00116 #define NETROUTE_OK 0x0040
00117 #define TIMED_OK    0x0080
00118 
00119 /*
00120  * Application entry.
00121  */
00122 int main(void)
00123 {
00124     uint32_t baud = MYBAUD;
00125     uint8_t mac[6] = { MYMAC };
00126     uint32_t timeserver = inet_addr(MYTIMED);
00127     int rc = 0;
00128     time_t now;
00129 
00130     /*
00131      * Register UART devices, assign stdout and stderr to this device
00132      * and set the baudrate.
00133      */
00134     if(NutRegisterDevice(&MYDEV, 0, 0) == 0) {
00135         rc |= UART_OK;
00136         if(freopen(MYUART, "w", stdout)) {
00137             rc |= STDOUT_OK;
00138             if(_ioctl(_fileno(stdout), UART_SETSPEED, &baud) == 0) {
00139                 rc |= BAUDRATE_OK;
00140             }
00141         }
00142         if(freopen(MYUART, "w", stderr)) {
00143             rc |= STDERR_OK;
00144         }
00145     }
00146 
00147     /*
00148      * Print banner.
00149      */
00150     if(rc & STDOUT_OK) {
00151         printf("\n\nTimeLog %s\nNut/OS %s\n", version, NutVersionString());
00152         puts("Compiled by " COMPILERNAME);
00153         puts("Configure network");
00154     }
00155 
00156 #ifdef DEV_ETHER
00157     /*
00158      * Register LAN device and configure network interface.
00159      */
00160     if(NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
00161         rc |= LANDEV_OK;
00162         if (NutDhcpIfConfig("eth0", 0, 60000) == 0) {
00163             rc |= NETIF_OK;
00164         }
00165         else if (NutDhcpIfConfig("eth0", mac, 60000) == 0) {
00166             rc |= NETIF_OK;
00167         }
00168         else if(NutNetIfConfig("eth0", mac, inet_addr(MYIP), inet_addr(MYMASK)) == 0) {
00169             rc |= NETIF_OK;
00170 #ifdef MYGATE
00171             if(NutIpRouteAdd(0, 0, inet_addr(MYGATE), &DEV_ETHER) == 0) {
00172                 rc |= NETROUTE_OK;
00173             }
00174 #endif
00175         }
00176     }
00177 
00178     if(rc & NETIF_OK) {
00179         /*
00180          * Set timezone, query SNTP server and set system time.
00181          */
00182         if(rc & STDOUT_OK) {
00183             puts("Query time from " MYTIMED);
00184         }
00185         _timezone = MYTZ * 60L * 60L;
00186         if(NutSNTPGetTime(&timeserver, &now) == 0) {
00187             rc |= TIMED_OK;
00188             stime(&now);
00189         }
00190     }
00191 
00192     /*
00193      * Open syslog output and route messages to stderr and to
00194      * a remote server.
00195      */
00196     if(rc & STDOUT_OK) {
00197         puts("Initialize syslog");
00198     }
00199     openlog("logtime", (rc & STDERR_OK) ? LOG_PERROR : 0, LOG_USER);
00200     if(rc & NETIF_OK) {
00201         setlogserver(inet_addr(MYLOGD), 0);
00202     }
00203     syslog(LOG_INFO, "TimeLog %s started on Nut/OS %s", version, NutVersionString());
00204 
00205     /*
00206      * Print the result of our initialization.
00207      */
00208     if((rc & UART_OK) == 0) {
00209         syslog(LOG_ERR, "Registering UART device failed");
00210     }
00211     else if((rc & STDOUT_OK) == 0) {
00212         syslog(LOG_ERR, "Assigning stdout failed");
00213     }
00214     else if((rc & STDERR_OK) == 0) {
00215         syslog(LOG_ERR, "Assigning stderr failed");
00216     }
00217     else if((rc & BAUDRATE_OK) == 0) {
00218         syslog(LOG_ERR, "Setting baudrate failed");
00219     }
00220     if((rc & LANDEV_OK) == 0) {
00221         syslog(LOG_ERR, "Registering Ethernet device failed");
00222     }
00223     else if((rc & NETIF_OK) == 0) {
00224         syslog(LOG_ERR, "Configuring network interface failed");
00225     }
00226     else {
00227         syslog(LOG_INFO, "IP %s", inet_ntoa(confnet.cdn_ip_addr));
00228         syslog(LOG_INFO, "Gate %s", inet_ntoa(confnet.cdn_gateway));
00229         syslog(LOG_INFO, "Timed " MYTIMED);
00230         syslog(LOG_INFO, "Syslogd " MYLOGD);
00231     }
00232 
00233     /*
00234      * Endless loop.
00235      */
00236     for(;;) {
00237         syslog(LOG_DEBUG, "%d bytes free", NutHeapAvailable());
00238         NutSleep(60000);
00239     }
00240 #endif
00241     return 0;
00242 }