Main Page | Modules | Alphabetical List | Data Structures | Directories | File List | Data Fields | Globals | Related Pages | Examples

logtime/logtime.c

Shows how to use syslog and SNTP.

#include <stdio.h>
#include <io.h>

#include <arpa/inet.h>
#include <net/route.h>
#include <pro/dhcp.h>
#include <pro/sntp.h>

#include <sys/version.h>
#include <sys/confnet.h>
#include <sys/timer.h>
#include <sys/heap.h>
#include <sys/syslog.h>

#include <dev/board.h>

static char *version = "1.0.1";

/*
 * User configuration.
 */

#define MYMAC   0x00, 0x06, 0x98, 0x00, 0x00, 0x00

#define MYIP    "192.168.192.100"

#define MYMASK  "255.255.255.0"

#define MYGATE  "192.168.192.1"

#define MYLOGD  "192.168.192.222"

#define MYTIMED "130.149.17.21"

#define MYUART  "uart0"

#define MYBAUD  115200

#define MYDEV   DEV_UART

#define MYTZ    -1


#ifdef __IMAGECRAFT__
#define COMPILERNAME "ICCAVR"
#else
#define COMPILERNAME "GCC"
#endif

/* Result codes. */
#define UART_OK     0x0001
#define STDOUT_OK   0x0002
#define STDERR_OK   0x0004
#define BAUDRATE_OK 0x0008
#define LANDEV_OK   0x0010
#define NETIF_OK    0x0020
#define NETROUTE_OK 0x0040
#define TIMED_OK    0x0080

/*
 * Application entry.
 */
int main(void)
{
    u_long baud = MYBAUD;
    u_char mac[6] = { MYMAC };
    u_long timeserver = inet_addr(MYTIMED);
    int rc = 0;
    time_t now;

    /*
     * Register UART devices, assign stdout and stderr to this device
     * and set the baudrate.
     */
    if(NutRegisterDevice(&MYDEV, 0, 0) == 0) {
        rc |= UART_OK;
        if(freopen(MYUART, "w", stdout)) {
            rc |= STDOUT_OK;
            if(_ioctl(_fileno(stdout), UART_SETSPEED, &baud) == 0) {
                rc |= BAUDRATE_OK;
            }
        }
        if(freopen(MYUART, "w", stderr)) {
            rc |= STDERR_OK;
        }
    }

    /*
     * Print banner.
     */
    if(rc & STDOUT_OK) {
        printf("\n\nTimeLog %s\nNut/OS %s\n", version, NutVersionString());
        puts("Compiled by " COMPILERNAME);
        puts("Configure network");
    }

    /*
     * Register LAN device and configure network interface.
     */
    if(NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
        rc |= LANDEV_OK;
        if (NutDhcpIfConfig("eth0", 0, 60000) == 0) {
            rc |= NETIF_OK;
        }
        else if (NutDhcpIfConfig("eth0", mac, 60000) == 0) {
            rc |= NETIF_OK;
        }
        else if(NutNetIfConfig("eth0", mac, inet_addr(MYIP), inet_addr(MYMASK)) == 0) {
            rc |= NETIF_OK;
#ifdef MYGATE
            if(NutIpRouteAdd(0, 0, inet_addr(MYGATE), &DEV_ETHER) == 0) {
                rc |= NETROUTE_OK;
            }
#endif
        }
    }

    if(rc & NETIF_OK) {
        /*
         * Set timezone, query SNTP server and set system time.
         */
        if(rc & STDOUT_OK) {
            puts("Query time from " MYTIMED);
        }
        _timezone = MYTZ * 60L * 60L;
        if(NutSNTPGetTime(&timeserver, &now) == 0) {
            rc |= TIMED_OK;
            stime(&now);
        }
    }

    /*
     * Open syslog output and route messages to stderr and to
     * a remote server.
     */
    if(rc & STDOUT_OK) {
        puts("Initialize syslog");
    }
    openlog("logtime", (rc & STDERR_OK) ? LOG_PERROR : 0, LOG_USER);
    if(rc & NETIF_OK) {
        setlogserver(inet_addr(MYLOGD), 0);
    }
    syslog(LOG_INFO, "TimeLog %s started on Nut/OS %s", version, NutVersionString());

    /*
     * Print the result of our initialization.
     */
    if((rc & UART_OK) == 0) {
        syslog(LOG_ERR, "Registering UART device failed");
    }
    else if((rc & STDOUT_OK) == 0) {
        syslog(LOG_ERR, "Assigning stdout failed");
    }
    else if((rc & STDERR_OK) == 0) {
        syslog(LOG_ERR, "Assigning stderr failed");
    }
    else if((rc & BAUDRATE_OK) == 0) {
        syslog(LOG_ERR, "Setting baudrate failed");
    }
    if((rc & LANDEV_OK) == 0) {
        syslog(LOG_ERR, "Registering Ethernet device failed");
    }
    else if((rc & NETIF_OK) == 0) {
        syslog(LOG_ERR, "Configuring network interface failed");
    }
    else {
        syslog(LOG_INFO, "IP %s", inet_ntoa(confnet.cdn_ip_addr));
        syslog(LOG_INFO, "Gate %s", inet_ntoa(confnet.cdn_gateway));
        syslog(LOG_INFO, "Timed " MYTIMED);
        syslog(LOG_INFO, "Syslogd " MYLOGD);
    }

    /*
     * Endless loop.
     */
    for(;;) {
        syslog(LOG_DEBUG, "%d bytes free", NutHeapAvailable());
        NutSleep(60000);
    }
}

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