/*!
 * \brief Save configuration in EEPROM.
 */
void ConfigSave(void)
{
    uint8_t idx;
    RADIOSTATION *rsp;
    int addr = CONFAPP_EE_OFFSET;

    NutNetSaveConfig();

    /* Save our name. */
    addr += ConfigSaveString(addr, CONFAPP_EE_NAME);

    /* Save radio control. */
    addr += ConfigSaveBinary(addr, &radio.rc_cstation, sizeof(radio.rc_cstation));
    addr += ConfigSaveBinary(addr, &radio.rc_cvolume, sizeof(radio.rc_cvolume));

    /* Save stations. */
    for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
        rsp = &station[idx];
        addr += ConfigSaveBinary(addr, &rsp->rs_port, sizeof(rsp->rs_port));
        addr += ConfigSaveBinary(addr, &rsp->rs_ip, sizeof(rsp->rs_ip));
        if (rsp->rs_url)
            addr += ConfigSaveString(addr, rsp->rs_url);
        else
            addr += ConfigSaveString(addr, "");
    }
}
/*!
 * \brief Apply new configuration.
 *
 * \param dist Pointer to the discovery datagram. It is assumed, that
 *             the validity of the datagram contents had been checked
 *             by the caller.
 */
int NutDiscoveryAppConf(DISCOVERY_TELE * dist)
{
    memcpy(confos.hostname, dist->dist_hostname, sizeof(confos.hostname));
    NutSaveConfig();

    memcpy(confnet.cdn_mac, dist->dist_mac, sizeof(confnet.cdn_mac));
    confnet.cdn_ip_addr = dist->dist_ip_addr;
    confnet.cdn_ip_mask = dist->dist_ip_mask;
    confnet.cdn_gateway = dist->dist_gateway;
    confnet.cdn_cip_addr = dist->dist_cip_addr;

    return NutNetSaveConfig();
}
/*!
 * \brief Network interface setup.
 *
 * \param dev     Identifies the network device to setup.
 * \param ip_addr Specified IP address in network byte order.
 * \param ip_mask Specified IP network mask in network byte order.
 * \param gateway Optional default gateway.
 *
 * \return 0 on success, -1 otherwise.
 *
 * \note Typical applications do not use this function, but call
 *       NutDhcpIfConfig() or NutNetIfConfig().
 */
int NutNetIfSetup(NUTDEVICE * dev, u_long ip_addr, u_long ip_mask, u_long gateway)
{
    IFNET *nif;

    nif = dev->dev_icb;

    /*
     * Use specified or previously used IP address.
     */
    if (ip_addr == 0 && (ip_addr = confnet.cdn_ip_addr) == 0)
        return -1;
    nif->if_local_ip = ip_addr;

    /*
     * Use specified or default mask.
     */
    if (ip_mask == 0)
        ip_mask = inet_addr("255.255.255.0");
    nif->if_mask = ip_mask;

    /*
     * Add routing entries.
     */
    NutIpRouteAdd(ip_addr & ip_mask, ip_mask, 0, dev);
    if (gateway)
        NutIpRouteAdd(0, 0, gateway, dev);

    /*
     * Save configuration in EEPROM.
     */
    memcpy(confnet.cd_name, dev->dev_name, sizeof(confnet.cd_name));
    /* Never save MAC address 0. */
    if (nif->if_mac[0] || nif->if_mac[1] || nif->if_mac[2] || nif->if_mac[3] || nif->if_mac[4] || nif->if_mac[5]) {
        memcpy(confnet.cdn_mac, nif->if_mac, sizeof(nif->if_mac));
    }
    confnet.cdn_ip_addr = ip_addr;
    confnet.cdn_ip_mask = ip_mask;

    /*
     * Set gateway, if one was provided by the caller. Remove
     * gateway, if it's outside of our network.
     */
    if (gateway || (confnet.cdn_gateway & ip_mask) != (ip_addr & ip_mask))
        confnet.cdn_gateway = gateway;

    return NutNetSaveConfig();
}
/* Editor main routine. */
int main(void)
{
    uint32_t baud = 115200;
    char buf[32];
    uint8_t *cp;
    uint32_t addr;
    char ch;
 
    /* Assign stdin and stdout to the default UART device. */
    NutRegisterDevice(&DEV_CONSOLE, 0, 0);
    freopen(DEV_CONSOLE_NAME, "w", stdout);
    freopen(DEV_CONSOLE_NAME, "r", stdin);    
    _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
    puts("\n\nNetwork Configuration Editor - Compiled " __DATE__ " - " __TIME__);
    ShowHardwareConfiguration();
 
    for (;;) {
        /* Load configuration. */
        if (NutNetLoadConfig(DEV_ETHER_NAME)) {
            puts("\nNo configuration available");
            strcpy(confnet.cd_name, DEV_ETHER_NAME);
        } else {
            puts("\nConfiguration loaded");
        }
 
        /* Edit MAC address. */
        do {
            strcpy(buf, ether_ntoa(confnet.cdn_mac));
            EditLine("MAC Address", buf, 18);
            cp = ether_aton(buf);
        } while (cp == NULL);
        memcpy(confnet.cdn_mac, cp, 6);
 
        /* Edit IP address. */
        do {
            strcpy(buf, inet_ntoa(confnet.cdn_cip_addr));
            EditLine("IP Address", buf, 16);
            addr = inet_addr(buf);
        } while (addr == -1);
        confnet.cdn_cip_addr = addr;
 
        /* Edit IP mask. */
        do {
            strcpy(buf, inet_ntoa(confnet.cdn_ip_mask));
            EditLine("IP Mask", buf, 16);
            addr = inet_addr(buf);
        } while (addr == -1);
        confnet.cdn_ip_mask = addr;
 
        /* Edit IP gate. */
        do {
            strcpy(buf, inet_ntoa(confnet.cdn_gateway));
            EditLine("IP Gate", buf, 16);
            addr = inet_addr(buf);
        } while (addr == -1);
        confnet.cdn_gateway = addr;
 
        /* Prompt for saving. */
        printf("\nPress S to save this configuration ");
 
        /* Flush input buffer and read next character. */
        while (kbhit()) {
            ch = getchar();
        }
        ch = getchar();
 
        /* Save or discard edited configuration. */
        if (ch == 's' || ch == 'S') {
            if (NutNetSaveConfig()) {
                puts("Failed");
            } else {
                puts("Saved");
            }
        } else {
            puts("Discarded");
        }
    }
    return 0;
}