Nut/OS  4.10.3
API Reference
editconf.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 by egnite GmbH
00003  *
00004  * All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  * 1. Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  * 2. Redistributions in binary form must reproduce the above copyright
00013  *    notice, this list of conditions and the following disclaimer in the
00014  *    documentation and/or other materials provided with the distribution.
00015  * 3. Neither the name of the copyright holders nor the names of
00016  *    contributors may be used to endorse or promote products derived
00017  *    from this software without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00020  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00021  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00022  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00023  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00024  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00025  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00026  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00027  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00028  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00029  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00030  * SUCH DAMAGE.
00031  *
00032  * For additional information see http://www.ethernut.de/
00033  *
00034  */
00035 
00054 #include <dev/board.h>
00055 #include <sys/confnet.h>
00056  
00057 #include <string.h>
00058 #include <stdint.h>
00059 #include <stdio.h>
00060 #include <io.h>
00061 #include <arpa/inet.h>
00062 #include <netinet/if_ether.h>
00063 
00064 #include "meminfo.h"
00065 
00066 /* Reads line from standard input. */
00067 static int EditLine(char *prompt, char *line, int siz)
00068 {
00069     int ch;
00070     int pos = strlen(line);
00071  
00072     printf("%s: %s", prompt, line);
00073     for (;;) {
00074         ch = getchar();
00075         if (ch == 8) {
00076             if (pos) {
00077                 pos--;
00078                 printf("\b \b");
00079             }
00080         }
00081         else if (ch < ' ') {
00082             break;
00083         }
00084         else if (pos + 1 < siz) {
00085             putchar(ch);
00086             line[pos++] = ch;
00087         }
00088         else {
00089             putchar('\a');
00090         }
00091     }
00092     line[pos] = 0;
00093     putchar('\n');
00094  
00095     return 0;
00096 }
00097 
00098 /* Editor main routine. */
00099 int main(void)
00100 {
00101     uint32_t baud = 115200;
00102     char buf[32];
00103     uint8_t *cp;
00104     uint32_t addr;
00105     char ch;
00106  
00107     /* Assign stdin and stdout to the default UART device. */
00108     NutRegisterDevice(&DEV_CONSOLE, 0, 0);
00109     freopen(DEV_CONSOLE_NAME, "w", stdout);
00110     freopen(DEV_CONSOLE_NAME, "r", stdin);    
00111     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00112     puts("\n\nNetwork Configuration Editor - Compiled " __DATE__ " - " __TIME__);
00113     ShowHardwareConfiguration();
00114  
00115     for (;;) {
00116         /* Load configuration. */
00117         if (NutNetLoadConfig(DEV_ETHER_NAME)) {
00118             puts("\nNo configuration available");
00119             strcpy(confnet.cd_name, DEV_ETHER_NAME);
00120         } else {
00121             puts("\nConfiguration loaded");
00122         }
00123  
00124         /* Edit MAC address. */
00125         do {
00126             strcpy(buf, ether_ntoa(confnet.cdn_mac));
00127             EditLine("MAC Address", buf, 18);
00128             cp = ether_aton(buf);
00129         } while (cp == NULL);
00130         memcpy(confnet.cdn_mac, cp, 6);
00131  
00132         /* Edit IP address. */
00133         do {
00134             strcpy(buf, inet_ntoa(confnet.cdn_cip_addr));
00135             EditLine("IP Address", buf, 16);
00136             addr = inet_addr(buf);
00137         } while (addr == -1);
00138         confnet.cdn_cip_addr = addr;
00139  
00140         /* Edit IP mask. */
00141         do {
00142             strcpy(buf, inet_ntoa(confnet.cdn_ip_mask));
00143             EditLine("IP Mask", buf, 16);
00144             addr = inet_addr(buf);
00145         } while (addr == -1);
00146         confnet.cdn_ip_mask = addr;
00147  
00148         /* Edit IP gate. */
00149         do {
00150             strcpy(buf, inet_ntoa(confnet.cdn_gateway));
00151             EditLine("IP Gate", buf, 16);
00152             addr = inet_addr(buf);
00153         } while (addr == -1);
00154         confnet.cdn_gateway = addr;
00155  
00156         /* Prompt for saving. */
00157         printf("\nPress S to save this configuration ");
00158  
00159         /* Flush input buffer and read next character. */
00160         while (kbhit()) {
00161             ch = getchar();
00162         }
00163         ch = getchar();
00164  
00165         /* Save or discard edited configuration. */
00166         if (ch == 's' || ch == 'S') {
00167             if (NutNetSaveConfig()) {
00168                 puts("Failed");
00169             } else {
00170                 puts("Saved");
00171             }
00172         } else {
00173             puts("Discarded");
00174         }
00175     }
00176     return 0;
00177 }