config.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2006 by egnite Software 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 
00040 #include <sys/types.h>
00041 
00042 #include <stdlib.h>
00043 #include <string.h>
00044 
00045 #include <arpa/inet.h>
00046 #include <net/if_var.h>
00047 
00048 #include "config.h"
00049 
00050 #include <stdio.h>
00051 
00055 RADIOSTATION *station;
00056 
00060 RADIOCONTROL radio;
00061 
00062 /*
00063  * Save a binary into the EEPROM.
00064  */
00065 static int ConfigSaveBinary(int addr, void *val, size_t len)
00066 {
00067 #if defined(__AVR__)
00068     size_t i;
00069     uint8_t *cp = val;
00070 
00071     for (i = 0; i < len; cp++, i++)
00072         if (eeprom_read_byte((void *) (addr + i)) != *cp)
00073             eeprom_write_byte((void *) (addr + i), *cp);
00074 #endif /* __AVR__ */
00075 
00076     return len;
00077 }
00078 
00079 /*
00080  * Save a string into the EEPROM.
00081  */
00082 static int ConfigSaveString(int addr, char * str)
00083 {
00084     int rc = 0;
00085 
00086 #if defined(__AVR__)
00087     do {
00088         if (eeprom_read_byte((void *) (addr + rc)) != *str)
00089             eeprom_write_byte((void *) (addr + rc), *str);
00090         rc++;
00091     } while (*str++);
00092 #endif /* __AVR__ */
00093 
00094     return rc;
00095 }
00096 
00097 /*
00098  * Read a string from EEPROM.
00099  */
00100 static size_t ConfigLoadString(int addr, char * str, size_t size)
00101 {
00102     size_t rc = 0;
00103 
00104 #if defined(__AVR__)
00105     while (rc < size) {
00106         *str = eeprom_read_byte((void *) (addr + rc));
00107         rc++;
00108         if (*str++ == 0)
00109             break;
00110     }
00111 #endif /* __AVR__ */
00112 
00113     return rc;
00114 }
00115 
00116 /*
00117  * Read a binary value from EEPROM.
00118  */
00119 static int ConfigLoadBinary(int addr, void *val, size_t len)
00120 {
00121 #if defined(__AVR__)
00122     size_t i;
00123     uint8_t *cp = val;
00124 
00125     for (i = 0; i < len; cp++, i++)
00126         *cp = eeprom_read_byte((void *) (addr + i));
00127 #endif /* __AVR__ */
00128 
00129     return len;
00130 }
00131 
00137 size_t ConfigSize(void)
00138 {
00139     size_t rc = 0;
00140     uint8_t idx;
00141     RADIOSTATION *rsp;
00142 
00143     for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00144         rsp = &station[idx];
00145         if (station[idx].rs_port == 0)
00146             break;
00147         rc += sizeof(rsp->rs_port);
00148         rc += sizeof(rsp->rs_ip);
00149         if (rsp->rs_url)
00150             rc += strlen(rsp->rs_url);
00151         rc++;
00152     }
00153     rc += sizeof(station[0].rs_port);
00154 
00155     return rc;
00156 }
00157 
00166 int ConfigStation(uint8_t idx, CONST char * url)
00167 {
00168     uint32_t ip;
00169     uint16_t port = 80;
00170     char *buf;
00171     char *cp;
00172 
00173     if (idx >= MAXNUM_STATIONS) {
00174         idx = 0;
00175         while (idx < MAXNUM_STATIONS && station[idx].rs_port)
00176             idx++;
00177     }
00178     if (idx >= MAXNUM_STATIONS)
00179         return -1;
00180     else {
00181         buf = malloc(strlen(url) + 1);
00182 
00183         /* Extract IP address. */
00184         cp = buf;
00185         while (*url && *url != '/' && *url != ':')
00186             *cp++ = *url++;
00187         *cp = 0;
00188         if ((int) (ip = inet_addr(buf)) == -1)
00189             idx = -1;
00190         else {
00191             /* Extract URL path. */
00192             cp = buf;
00193             if (*url == '/') {
00194                 url++;
00195                 while (*url && *url != ':')
00196                     *cp++ = *url++;
00197             }
00198             *cp = 0;
00199 
00200             /* Extract port. */
00201             if (*url == ':')
00202                 port = atoi(url + 1);
00203 
00204             if (port) {
00205                 station[idx].rs_ip = ip;
00206                 station[idx].rs_port = port;
00207                 if (*buf) {
00208                     station[idx].rs_url = malloc(strlen(buf) + 1);
00209                     strcpy(station[idx].rs_url, buf);
00210                 }
00211             }
00212         }
00213 
00214         free(buf);
00215     }
00216     return idx;
00217 }
00218 
00219 /*
00220  * Allocate heap memory for configuration structure.
00221  */
00222 static void ConfigCreate(void)
00223 {
00224     uint8_t idx;
00225 
00226     if (station == 0)
00227         station = malloc(MAXNUM_STATIONS * sizeof(RADIOSTATION));
00228     else {
00229         /* Release all memory occupied by the current configuration. */
00230         for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00231             if (station[idx].rs_port == 0)
00232                 break;
00233             if (station[idx].rs_url)
00234                 free(station[idx].rs_url);
00235         }
00236     }
00237     memset(station, 0, MAXNUM_STATIONS * sizeof(RADIOSTATION));
00238 }
00239 
00243 void ConfigResetFactory(void)
00244 {
00245     ConfigCreate();
00246 
00247     /* Initial radio control settings. */
00248     radio.rc_rstation = 2;
00249     radio.rc_rvolume = 223;
00250 
00251     /* 
00252      * Add pre-configured radio stations. 
00253      */
00254 
00255     /* Local server. */
00256     ConfigStation(MAXNUM_STATIONS, "192.168.192.11:8000");
00257     /* elDOradio 56 kbit */
00258     ConfigStation(MAXNUM_STATIONS, "129.217.234.42/128:8000");
00259     /* Virgin 32 kbit. */
00260     ConfigStation(MAXNUM_STATIONS, "212.187.204.62:80");
00261     /* qpop.nl 48 kbit */
00262     ConfigStation(MAXNUM_STATIONS, "194.109.192.226:8010");
00263     /* idobi 24 kbit */
00264     ConfigStation(MAXNUM_STATIONS, "66.28.100.131:8004");
00265     /* Radiosound-7-24 24 kbit. */
00266     ConfigStation(MAXNUM_STATIONS, "64.202.98.51:7650");
00267     /* DH Netradio 56 kbit */
00268     ConfigStation(MAXNUM_STATIONS, "205.188.234.38:8030");
00269     /* Cable radio 56 kbit. */
00270     ConfigStation(MAXNUM_STATIONS, "62.25.96.7:8080");
00271     /* MIBN1 40 kbit. */
00272     ConfigStation(MAXNUM_STATIONS, "216.234.109.21:8000");
00273     /* RFK 56 kbit */
00274     ConfigStation(MAXNUM_STATIONS, "64.202.98.33:2530");
00275     /* Braingell 24 kbit. */
00276     ConfigStation(MAXNUM_STATIONS, "216.237.145.20:8000");
00277     /* HipHop 48 kbit. */
00278     ConfigStation(MAXNUM_STATIONS, "64.202.98.33:6150");
00279     /* Flensburg 56 kbit */
00280     ConfigStation(MAXNUM_STATIONS, "217.160.210.37:8000");
00281     /* Boombastic 24 kbit. */
00282     ConfigStation(MAXNUM_STATIONS, "212.43.230.20:8000");
00283     /* Russia 24 kbit */
00284     ConfigStation(MAXNUM_STATIONS, "62.118.255.5:9000");
00285     /* Frequence3  16 kbit. */
00286     ConfigStation(MAXNUM_STATIONS, "212.180.2.19:8010");
00287     /* KCTI Country 16 kbit. */
00288     ConfigStation(MAXNUM_STATIONS, "63.125.62.117:8000");
00289     /* Klassik 40 kbit. */
00290     ConfigStation(MAXNUM_STATIONS, "62.157.113.86:8000");
00291     /* m2ktalk 8kbit */
00292     ConfigStation(MAXNUM_STATIONS, "209.17.76.226:8010");
00293     /* Christian Media 16 kbit. */
00294     ConfigStation(MAXNUM_STATIONS, "64.202.98.32:6610");
00295     /* Newsradio 24 kbit. */
00296     ConfigStation(MAXNUM_STATIONS, "65.172.162.93:9191");
00297 
00298 #ifdef ETHERNUT2
00299     /*
00300      * These stations require Ethernut 2.
00301      */
00302     /* Grapeshot 128 kbit. */
00303     ConfigStation(MAXNUM_STATIONS, "66.28.45.159:8075");
00304     /* Radiostorm 128 kbit. */
00305     ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1014:80");
00306     /* Digitally imported 128 kbit. */
00307     ConfigStation(MAXNUM_STATIONS, "205.188.209.193/stream/1003:80");
00308     /* SmoothJazz 128 kbit */
00309     ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1005:80");
00310     /* Tosh Jamaica 128 kbit. */
00311     ConfigStation(MAXNUM_STATIONS, "38.144.33.148:8022");
00312     /* weird 160 kbit */
00313     ConfigStation(MAXNUM_STATIONS, "209.98.88.40:8007");
00314     /* Stratovarius 192 kbit */
00315     ConfigStation(MAXNUM_STATIONS, "210.120.247.22:1290");
00316     /* Virgin 128 kbit. */
00317     ConfigStation(MAXNUM_STATIONS, "64.236.34.72/stream/1031:80");
00318     /* Virgin 128 kbit. */
00319     ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1031:80");
00320 #endif
00321 }
00322 
00331 int ConfigLoad(void)
00332 {
00333     int rc = -1;
00334     int addr = CONFAPP_EE_OFFSET;
00335     char *buf;
00336     uint8_t idx;
00337     RADIOSTATION *rsp;
00338 
00339     buf = malloc(MAXLEN_URL + 1);
00340     addr += ConfigLoadString(addr, buf, sizeof(CONFAPP_EE_NAME));
00341     if (strcmp(buf, CONFAPP_EE_NAME) == 0) {
00342 
00343         ConfigCreate();
00344         rc = 0;
00345 
00346         /* 
00347          * Read radio settings from EEPROM.
00348          */
00349         addr += ConfigLoadBinary(addr, &radio.rc_rstation, sizeof(radio.rc_rstation));
00350         addr += ConfigLoadBinary(addr, &radio.rc_rvolume, sizeof(radio.rc_rvolume));
00351 
00352         /* 
00353          * Read station configuration from EEPROM.
00354          */
00355         for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00356             rsp = &station[idx];
00357             addr += ConfigLoadBinary(addr, &rsp->rs_port, sizeof(rsp->rs_port));
00358             addr += ConfigLoadBinary(addr, &rsp->rs_ip, sizeof(rsp->rs_ip));
00359             addr += ConfigLoadString(addr, buf, MAXLEN_URL);
00360             if (*buf) {
00361                 rsp->rs_url = malloc(strlen(buf) + 1);
00362                 strcpy(rsp->rs_url, buf);
00363             }
00364         }
00365     }
00366     free(buf);
00367 
00368     return rc;
00369 }
00370 
00376 void ConfigSaveControl(void)
00377 {
00378     int addr = CONFAPP_EE_OFFSET + sizeof(CONFAPP_EE_NAME);
00379 
00380     /* Save radio control. */
00381     addr += ConfigSaveBinary(addr, &radio.rc_cstation, sizeof(radio.rc_cstation));
00382     addr += ConfigSaveBinary(addr, &radio.rc_cvolume, sizeof(radio.rc_cvolume));
00383 }
00384 
00388 void ConfigSave(void)
00389 {
00390     uint8_t idx;
00391     RADIOSTATION *rsp;
00392     int addr = CONFAPP_EE_OFFSET;
00393 
00394     NutNetSaveConfig();
00395 
00396     /* Save our name. */
00397     addr += ConfigSaveString(addr, CONFAPP_EE_NAME);
00398 
00399     /* Save radio control. */
00400     addr += ConfigSaveBinary(addr, &radio.rc_cstation, sizeof(radio.rc_cstation));
00401     addr += ConfigSaveBinary(addr, &radio.rc_cvolume, sizeof(radio.rc_cvolume));
00402 
00403     /* Save stations. */
00404     for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00405         rsp = &station[idx];
00406         addr += ConfigSaveBinary(addr, &rsp->rs_port, sizeof(rsp->rs_port));
00407         addr += ConfigSaveBinary(addr, &rsp->rs_ip, sizeof(rsp->rs_ip));
00408         if (rsp->rs_url)
00409             addr += ConfigSaveString(addr, rsp->rs_url);
00410         else
00411             addr += ConfigSaveString(addr, "");
00412     }
00413 }

© 2000-2010 by contributors - visit http://www.ethernut.de/