00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
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
00094
00095 return len;
00096 }
00097
00098
00099
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
00112
00113 return rc;
00114 }
00115
00116
00117
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
00131
00132 return rc;
00133 }
00134
00135
00136
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
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
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
00211 cp = buf;
00212 if (*url == '/') {
00213 url++;
00214 while (*url && *url != ':')
00215 *cp++ = *url++;
00216 }
00217 *cp = 0;
00218
00219
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
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
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
00267 radio.rc_rstation = 2;
00268 radio.rc_rvolume = 223;
00269
00270
00271
00272
00273
00274
00275 ConfigStation(MAXNUM_STATIONS, "192.168.192.11:8000");
00276
00277 ConfigStation(MAXNUM_STATIONS, "129.217.234.42/128:8000");
00278
00279 ConfigStation(MAXNUM_STATIONS, "212.187.204.62:80");
00280
00281 ConfigStation(MAXNUM_STATIONS, "194.109.192.226:8010");
00282
00283 ConfigStation(MAXNUM_STATIONS, "66.28.100.131:8004");
00284
00285 ConfigStation(MAXNUM_STATIONS, "64.202.98.51:7650");
00286
00287 ConfigStation(MAXNUM_STATIONS, "205.188.234.38:8030");
00288
00289 ConfigStation(MAXNUM_STATIONS, "62.25.96.7:8080");
00290
00291 ConfigStation(MAXNUM_STATIONS, "216.234.109.21:8000");
00292
00293 ConfigStation(MAXNUM_STATIONS, "64.202.98.33:2530");
00294
00295 ConfigStation(MAXNUM_STATIONS, "216.237.145.20:8000");
00296
00297 ConfigStation(MAXNUM_STATIONS, "64.202.98.33:6150");
00298
00299 ConfigStation(MAXNUM_STATIONS, "217.160.210.37:8000");
00300
00301 ConfigStation(MAXNUM_STATIONS, "212.43.230.20:8000");
00302
00303 ConfigStation(MAXNUM_STATIONS, "62.118.255.5:9000");
00304
00305 ConfigStation(MAXNUM_STATIONS, "212.180.2.19:8010");
00306
00307 ConfigStation(MAXNUM_STATIONS, "63.125.62.117:8000");
00308
00309 ConfigStation(MAXNUM_STATIONS, "62.157.113.86:8000");
00310
00311 ConfigStation(MAXNUM_STATIONS, "209.17.76.226:8010");
00312
00313 ConfigStation(MAXNUM_STATIONS, "64.202.98.32:6610");
00314
00315 ConfigStation(MAXNUM_STATIONS, "65.172.162.93:9191");
00316
00317 #ifdef ETHERNUT2
00318
00319
00320
00321
00322 ConfigStation(MAXNUM_STATIONS, "66.28.45.159:8075");
00323
00324 ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1014:80");
00325
00326 ConfigStation(MAXNUM_STATIONS, "205.188.209.193/stream/1003:80");
00327
00328 ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1005:80");
00329
00330 ConfigStation(MAXNUM_STATIONS, "38.144.33.148:8022");
00331
00332 ConfigStation(MAXNUM_STATIONS, "209.98.88.40:8007");
00333
00334 ConfigStation(MAXNUM_STATIONS, "210.120.247.22:1290");
00335
00336 ConfigStation(MAXNUM_STATIONS, "64.236.34.72/stream/1031:80");
00337
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
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
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
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
00416 addr += ConfigSaveString(addr, CONFAPP_EE_NAME);
00417
00418
00419 addr += ConfigSaveBinary(addr, &radio.rc_cstation, sizeof(radio.rc_cstation));
00420 addr += ConfigSaveBinary(addr, &radio.rc_cvolume, sizeof(radio.rc_cvolume));
00421
00422
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 }