logtime/logtime.c

Copyright (C) 2001-2005 by egnite Software GmbH. All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 3. Neither the name of the copyright holders nor the names of contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

For additional information see http://www.ethernut.de/

$Log$ Revision 1.3 2008/01/31 09:38:15 haraldkipp Added return statement in main to avoid warnings with latest GCC.

Revision 1.2 2005/11/22 09:14:13 haraldkipp Replaced specific device names by generalized macros.

Revision 1.1 2004/09/19 11:23:12 haraldkipp Syslog/SNTP application added

Shows how to use syslog and SNTP.

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

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/