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
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
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
00075
00076 return len;
00077 }
00078
00079
00080
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
00093
00094 return rc;
00095 }
00096
00097
00098
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
00112
00113 return rc;
00114 }
00115
00116
00117
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
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
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
00192 cp = buf;
00193 if (*url == '/') {
00194 url++;
00195 while (*url && *url != ':')
00196 *cp++ = *url++;
00197 }
00198 *cp = 0;
00199
00200
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
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
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
00248 radio.rc_rstation = 2;
00249 radio.rc_rvolume = 223;
00250
00251
00252
00253
00254
00255
00256 ConfigStation(MAXNUM_STATIONS, "192.168.192.11:8000");
00257
00258 ConfigStation(MAXNUM_STATIONS, "129.217.234.42/128:8000");
00259
00260 ConfigStation(MAXNUM_STATIONS, "212.187.204.62:80");
00261
00262 ConfigStation(MAXNUM_STATIONS, "194.109.192.226:8010");
00263
00264 ConfigStation(MAXNUM_STATIONS, "66.28.100.131:8004");
00265
00266 ConfigStation(MAXNUM_STATIONS, "64.202.98.51:7650");
00267
00268 ConfigStation(MAXNUM_STATIONS, "205.188.234.38:8030");
00269
00270 ConfigStation(MAXNUM_STATIONS, "62.25.96.7:8080");
00271
00272 ConfigStation(MAXNUM_STATIONS, "216.234.109.21:8000");
00273
00274 ConfigStation(MAXNUM_STATIONS, "64.202.98.33:2530");
00275
00276 ConfigStation(MAXNUM_STATIONS, "216.237.145.20:8000");
00277
00278 ConfigStation(MAXNUM_STATIONS, "64.202.98.33:6150");
00279
00280 ConfigStation(MAXNUM_STATIONS, "217.160.210.37:8000");
00281
00282 ConfigStation(MAXNUM_STATIONS, "212.43.230.20:8000");
00283
00284 ConfigStation(MAXNUM_STATIONS, "62.118.255.5:9000");
00285
00286 ConfigStation(MAXNUM_STATIONS, "212.180.2.19:8010");
00287
00288 ConfigStation(MAXNUM_STATIONS, "63.125.62.117:8000");
00289
00290 ConfigStation(MAXNUM_STATIONS, "62.157.113.86:8000");
00291
00292 ConfigStation(MAXNUM_STATIONS, "209.17.76.226:8010");
00293
00294 ConfigStation(MAXNUM_STATIONS, "64.202.98.32:6610");
00295
00296 ConfigStation(MAXNUM_STATIONS, "65.172.162.93:9191");
00297
00298 #ifdef ETHERNUT2
00299
00300
00301
00302
00303 ConfigStation(MAXNUM_STATIONS, "66.28.45.159:8075");
00304
00305 ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1014:80");
00306
00307 ConfigStation(MAXNUM_STATIONS, "205.188.209.193/stream/1003:80");
00308
00309 ConfigStation(MAXNUM_STATIONS, "64.236.34.141/stream/1005:80");
00310
00311 ConfigStation(MAXNUM_STATIONS, "38.144.33.148:8022");
00312
00313 ConfigStation(MAXNUM_STATIONS, "209.98.88.40:8007");
00314
00315 ConfigStation(MAXNUM_STATIONS, "210.120.247.22:1290");
00316
00317 ConfigStation(MAXNUM_STATIONS, "64.236.34.72/stream/1031:80");
00318
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
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
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
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
00397 addr += ConfigSaveString(addr, 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
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 }