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
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
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
00087
00088 return len;
00089 }
00090
00091
00092
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
00105
00106 return rc;
00107 }
00108
00109
00110
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
00124
00125 return rc;
00126 }
00127
00128
00129
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
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
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
00204 cp = buf;
00205 if (*url == '/') {
00206 url++;
00207 while (*url && *url != ':')
00208 *cp++ = *url++;
00209 }
00210 *cp = 0;
00211
00212
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
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
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
00260 radio.rc_rstation = 2;
00261 radio.rc_rvolume = 223;
00262
00263
00264
00265
00266
00267
00268 ConfigStation(MAXNUM_STATIONS, "192.168.192.11:8000");
00269
00270 ConfigStation(MAXNUM_STATIONS, "129.217.234.42/128:8000");
00271
00272 ConfigStation(MAXNUM_STATIONS, "212.187.204.62:80");
00273
00274 ConfigStation(MAXNUM_STATIONS, "194.109.192.226:8010");
00275
00276 ConfigStation(MAXNUM_STATIONS, "66.28.100.131:8004");
00277
00278 ConfigStation(MAXNUM_STATIONS, "64.202.98.51:7650");
00279
00280 ConfigStation(MAXNUM_STATIONS, "205.188.234.38:8030");
00281
00282 ConfigStation(MAXNUM_STATIONS, "62.25.96.7:8080");
00283
00284 ConfigStation(MAXNUM_STATIONS, "216.234.109.21:8000");
00285
00286 ConfigStation(MAXNUM_STATIONS, "64.202.98.33:2530");
00287
00288 ConfigStation(MAXNUM_STATIONS, "216.237.145.20:8000");
00289
00290 ConfigStation(MAXNUM_STATIONS, "64.202.98.33:6150");
00291
00292 ConfigStation(MAXNUM_STATIONS, "217.160.210.37:8000");
00293
00294 ConfigStation(MAXNUM_STATIONS, "212.43.230.20:8000");
00295
00296 ConfigStation(MAXNUM_STATIONS, "62.118.255.5:9000");
00297
00298 ConfigStation(MAXNUM_STATIONS, "212.180.2.19:8010");
00299
00300 ConfigStation(MAXNUM_STATIONS, "63.125.62.117:8000");
00301
00302 ConfigStation(MAXNUM_STATIONS, "62.157.113.86:8000");
00303
00304 ConfigStation(MAXNUM_STATIONS, "209.17.76.226:8010");
00305
00306 ConfigStation(MAXNUM_STATIONS, "64.202.98.32:6610");
00307
00308 ConfigStation(MAXNUM_STATIONS, "65.172.162.93:9191");
00309
00310 #ifdef ETHERNUT2
00311
00312
00313
00314
00315 ConfigStation(MAXNUM_STATIONS, "66.28.45.159:8075");
00316
00317 ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1014:80");
00318
00319 ConfigStation(MAXNUM_STATIONS, "205.188.209.193/stream/1003:80");
00320
00321 ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1005:80");
00322
00323 ConfigStation(MAXNUM_STATIONS, "38.144.33.148:8022");
00324
00325 ConfigStation(MAXNUM_STATIONS, "209.98.88.40:8007");
00326
00327 ConfigStation(MAXNUM_STATIONS, "210.120.247.22:1290");
00328
00329 ConfigStation(MAXNUM_STATIONS, "64.236.34.72/stream/1031:80");
00330
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
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
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
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
00409 addr += ConfigSaveString(addr, CONFAPP_EE_NAME);
00410
00411
00412 addr += ConfigSaveBinary(addr, &radio.rc_cstation, sizeof(radio.rc_cstation));
00413 addr += ConfigSaveBinary(addr, &radio.rc_cvolume, sizeof(radio.rc_cvolume));
00414
00415
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 }