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

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