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

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