config.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2003-2006 by egnite Software GmbH. All rights reserved.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY EGNITE SOFTWARE GMBH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL EGNITE
00021  * SOFTWARE GMBH OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00059 #include <sys/types.h>
00060 
00061 #include <stdlib.h>
00062 #include <string.h>
00063 
00064 #include <arpa/inet.h>
00065 #include <net/if_var.h>
00066 
00067 #include "config.h"
00068 
00069 #include <stdio.h>
00070 
00074 RADIOSTATION *station;
00075 
00079 RADIOCONTROL radio;
00080 
00081 /*
00082  * Save a binary into the EEPROM.
00083  */
00084 static int ConfigSaveBinary(int addr, void *val, size_t len)
00085 {
00086 #if defined(__AVR__)
00087     size_t i;
00088     u_char *cp = val;
00089 
00090     for (i = 0; i < len; cp++, i++)
00091         if (eeprom_read_byte((void *) (addr + i)) != *cp)
00092             eeprom_write_byte((void *) (addr + i), *cp);
00093 #endif /* __AVR__ */
00094 
00095     return len;
00096 }
00097 
00098 /*
00099  * Save a string into the EEPROM.
00100  */
00101 static int ConfigSaveString(int addr, char * str)
00102 {
00103     int rc = 0;
00104 
00105 #if defined(__AVR__)
00106     do {
00107         if (eeprom_read_byte((void *) (addr + rc)) != *str)
00108             eeprom_write_byte((void *) (addr + rc), *str);
00109         rc++;
00110     } while (*str++);
00111 #endif /* __AVR__ */
00112 
00113     return rc;
00114 }
00115 
00116 /*
00117  * Read a string from EEPROM.
00118  */
00119 static size_t ConfigLoadString(int addr, char * str, size_t size)
00120 {
00121     size_t rc = 0;
00122 
00123 #if defined(__AVR__)
00124     while (rc < size) {
00125         *str = eeprom_read_byte((void *) (addr + rc));
00126         rc++;
00127         if (*str++ == 0)
00128             break;
00129     }
00130 #endif /* __AVR__ */
00131 
00132     return rc;
00133 }
00134 
00135 /*
00136  * Read a binary value from EEPROM.
00137  */
00138 static int ConfigLoadBinary(int addr, void *val, size_t len)
00139 {
00140 #if defined(__AVR__)
00141     size_t i;
00142     u_char *cp = val;
00143 
00144     for (i = 0; i < len; cp++, i++)
00145         *cp = eeprom_read_byte((void *) (addr + i));
00146 #endif /* __AVR__ */
00147 
00148     return len;
00149 }
00150 
00156 size_t ConfigSize(void)
00157 {
00158     size_t rc = 0;
00159     u_char idx;
00160     RADIOSTATION *rsp;
00161 
00162     for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00163         rsp = &station[idx];
00164         if (station[idx].rs_port == 0)
00165             break;
00166         rc += sizeof(rsp->rs_port);
00167         rc += sizeof(rsp->rs_ip);
00168         if (rsp->rs_url)
00169             rc += strlen(rsp->rs_url);
00170         rc++;
00171     }
00172     rc += sizeof(station[0].rs_port);
00173 
00174     return rc;
00175 }
00176 
00185 int ConfigStation(u_char idx, CONST char * url)
00186 {
00187     u_long ip;
00188     u_short port = 80;
00189     char *buf;
00190     char *cp;
00191 
00192     if (idx >= MAXNUM_STATIONS) {
00193         idx = 0;
00194         while (idx < MAXNUM_STATIONS && station[idx].rs_port)
00195             idx++;
00196     }
00197     if (idx >= MAXNUM_STATIONS)
00198         return -1;
00199     else {
00200         buf = malloc(strlen(url) + 1);
00201 
00202         /* Extract IP address. */
00203         cp = buf;
00204         while (*url && *url != '/' && *url != ':')
00205             *cp++ = *url++;
00206         *cp = 0;
00207         if ((int) (ip = inet_addr(buf)) == -1)
00208             idx = -1;
00209         else {
00210             /* Extract URL path. */
00211             cp = buf;
00212             if (*url == '/') {
00213                 url++;
00214                 while (*url && *url != ':')
00215                     *cp++ = *url++;
00216             }
00217             *cp = 0;
00218 
00219             /* Extract port. */
00220             if (*url == ':')
00221                 port = atoi(url + 1);
00222 
00223             if (port) {
00224                 station[idx].rs_ip = ip;
00225                 station[idx].rs_port = port;
00226                 if (*buf) {
00227                     station[idx].rs_url = malloc(strlen(buf) + 1);
00228                     strcpy(station[idx].rs_url, buf);
00229                 }
00230             }
00231         }
00232 
00233         free(buf);
00234     }
00235     return idx;
00236 }
00237 
00238 /*
00239  * Allocate heap memory for configuration structure.
00240  */
00241 static void ConfigCreate(void)
00242 {
00243     u_char idx;
00244 
00245     if (station == 0)
00246         station = malloc(MAXNUM_STATIONS * sizeof(RADIOSTATION));
00247     else {
00248         /* Release all memory occupied by the current configuration. */
00249         for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00250             if (station[idx].rs_port == 0)
00251                 break;
00252             if (station[idx].rs_url)
00253                 free(station[idx].rs_url);
00254         }
00255     }
00256     memset(station, 0, MAXNUM_STATIONS * sizeof(RADIOSTATION));
00257 }
00258 
00262 void ConfigResetFactory(void)
00263 {
00264     ConfigCreate();
00265 
00266     /* Initial radio control settings. */
00267     radio.rc_rstation = 2;
00268     radio.rc_rvolume = 223;
00269 
00270     /* 
00271      * Add pre-configured radio stations. 
00272      */
00273 
00274     /* Local server. */
00275     ConfigStation(MAXNUM_STATIONS, "192.168.192.11:8000");
00276     /* elDOradio 56 kbit */
00277     ConfigStation(MAXNUM_STATIONS, "129.217.234.42/128:8000");
00278     /* Virgin 32 kbit. */
00279     ConfigStation(MAXNUM_STATIONS, "212.187.204.62:80");
00280     /* qpop.nl 48 kbit */
00281     ConfigStation(MAXNUM_STATIONS, "194.109.192.226:8010");
00282     /* idobi 24 kbit */
00283     ConfigStation(MAXNUM_STATIONS, "66.28.100.131:8004");
00284     /* Radiosound-7-24 24 kbit. */
00285     ConfigStation(MAXNUM_STATIONS, "64.202.98.51:7650");
00286     /* DH Netradio 56 kbit */
00287     ConfigStation(MAXNUM_STATIONS, "205.188.234.38:8030");
00288     /* Cable radio 56 kbit. */
00289     ConfigStation(MAXNUM_STATIONS, "62.25.96.7:8080");
00290     /* MIBN1 40 kbit. */
00291     ConfigStation(MAXNUM_STATIONS, "216.234.109.21:8000");
00292     /* RFK 56 kbit */
00293     ConfigStation(MAXNUM_STATIONS, "64.202.98.33:2530");
00294     /* Braingell 24 kbit. */
00295     ConfigStation(MAXNUM_STATIONS, "216.237.145.20:8000");
00296     /* HipHop 48 kbit. */
00297     ConfigStation(MAXNUM_STATIONS, "64.202.98.33:6150");
00298     /* Flensburg 56 kbit */
00299     ConfigStation(MAXNUM_STATIONS, "217.160.210.37:8000");
00300     /* Boombastic 24 kbit. */
00301     ConfigStation(MAXNUM_STATIONS, "212.43.230.20:8000");
00302     /* Russia 24 kbit */
00303     ConfigStation(MAXNUM_STATIONS, "62.118.255.5:9000");
00304     /* Frequence3  16 kbit. */
00305     ConfigStation(MAXNUM_STATIONS, "212.180.2.19:8010");
00306     /* KCTI Country 16 kbit. */
00307     ConfigStation(MAXNUM_STATIONS, "63.125.62.117:8000");
00308     /* Klassik 40 kbit. */
00309     ConfigStation(MAXNUM_STATIONS, "62.157.113.86:8000");
00310     /* m2ktalk 8kbit */
00311     ConfigStation(MAXNUM_STATIONS, "209.17.76.226:8010");
00312     /* Christian Media 16 kbit. */
00313     ConfigStation(MAXNUM_STATIONS, "64.202.98.32:6610");
00314     /* Newsradio 24 kbit. */
00315     ConfigStation(MAXNUM_STATIONS, "65.172.162.93:9191");
00316 
00317 #ifdef ETHERNUT2
00318     /*
00319      * These stations require Ethernut 2.
00320      */
00321     /* Grapeshot 128 kbit. */
00322     ConfigStation(MAXNUM_STATIONS, "66.28.45.159:8075");
00323     /* Radiostorm 128 kbit. */
00324     ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1014:80");
00325     /* Digitally imported 128 kbit. */
00326     ConfigStation(MAXNUM_STATIONS, "205.188.209.193/stream/1003:80");
00327     /* SmoothJazz 128 kbit */
00328     ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1005:80");
00329     /* Tosh Jamaica 128 kbit. */
00330     ConfigStation(MAXNUM_STATIONS, "38.144.33.148:8022");
00331     /* weird 160 kbit */
00332     ConfigStation(MAXNUM_STATIONS, "209.98.88.40:8007");
00333     /* Stratovarius 192 kbit */
00334     ConfigStation(MAXNUM_STATIONS, "210.120.247.22:1290");
00335     /* Virgin 128 kbit. */
00336     ConfigStation(MAXNUM_STATIONS, "64.236.34.72/stream/1031:80");
00337     /* Virgin 128 kbit. */
00338     ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1031:80");
00339 #endif
00340 }
00341 
00350 int ConfigLoad(void)
00351 {
00352     int rc = -1;
00353     int addr = CONFAPP_EE_OFFSET;
00354     char *buf;
00355     u_char idx;
00356     RADIOSTATION *rsp;
00357 
00358     buf = malloc(MAXLEN_URL + 1);
00359     addr += ConfigLoadString(addr, buf, sizeof(CONFAPP_EE_NAME));
00360     if (strcmp(buf, CONFAPP_EE_NAME) == 0) {
00361 
00362         ConfigCreate();
00363         rc = 0;
00364 
00365         /* 
00366          * Read radio settings from EEPROM.
00367          */
00368         addr += ConfigLoadBinary(addr, &radio.rc_rstation, sizeof(radio.rc_rstation));
00369         addr += ConfigLoadBinary(addr, &radio.rc_rvolume, sizeof(radio.rc_rvolume));
00370 
00371         /* 
00372          * Read station configuration from EEPROM.
00373          */
00374         for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00375             rsp = &station[idx];
00376             addr += ConfigLoadBinary(addr, &rsp->rs_port, sizeof(rsp->rs_port));
00377             addr += ConfigLoadBinary(addr, &rsp->rs_ip, sizeof(rsp->rs_ip));
00378             addr += ConfigLoadString(addr, buf, MAXLEN_URL);
00379             if (*buf) {
00380                 rsp->rs_url = malloc(strlen(buf) + 1);
00381                 strcpy(rsp->rs_url, buf);
00382             }
00383         }
00384     }
00385     free(buf);
00386 
00387     return rc;
00388 }
00389 
00395 void ConfigSaveControl(void)
00396 {
00397     int addr = CONFAPP_EE_OFFSET + sizeof(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 
00407 void ConfigSave(void)
00408 {
00409     u_char idx;
00410     RADIOSTATION *rsp;
00411     int addr = CONFAPP_EE_OFFSET;
00412 
00413     NutNetSaveConfig();
00414 
00415     /* Save our name. */
00416     addr += ConfigSaveString(addr, CONFAPP_EE_NAME);
00417 
00418     /* Save radio control. */
00419     addr += ConfigSaveBinary(addr, &radio.rc_cstation, sizeof(radio.rc_cstation));
00420     addr += ConfigSaveBinary(addr, &radio.rc_cvolume, sizeof(radio.rc_cvolume));
00421 
00422     /* Save stations. */
00423     for (idx = 0; idx < MAXNUM_STATIONS; idx++) {
00424         rsp = &station[idx];
00425         addr += ConfigSaveBinary(addr, &rsp->rs_port, sizeof(rsp->rs_port));
00426         addr += ConfigSaveBinary(addr, &rsp->rs_ip, sizeof(rsp->rs_ip));
00427         if (rsp->rs_url)
00428             addr += ConfigSaveString(addr, rsp->rs_url);
00429         else
00430             addr += ConfigSaveString(addr, "");
00431     }
00432 }

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