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
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056 #include <cfg/os.h>
00057 #include <cfg/clock.h>
00058 #include <dev/board.h>
00059 #include <dev/st7036.h>
00060
00061 #include <stdlib.h>
00062 #include <string.h>
00063 #include <stdio.h>
00064 #include <time.h>
00065 #include <fcntl.h>
00066 #include <io.h>
00067
00068 #include <sys/version.h>
00069 #include <sys/confos.h>
00070 #include <sys/confnet.h>
00071 #include <sys/atom.h>
00072 #include <sys/heap.h>
00073 #include <sys/thread.h>
00074 #include <sys/timer.h>
00075 #include <sys/event.h>
00076 #include <sys/socket.h>
00077
00078 #if defined(USE_SOFTWARE_CODEC)
00079 #include <hxmp3/mp3dec.h>
00080 #endif
00081
00082 #include <netinet/tcp.h>
00083 #include <arpa/inet.h>
00084 #include <net/route.h>
00085 #include <netdb.h>
00086
00087 #include <pro/dhcp.h>
00088 #include <pro/sntp.h>
00089 #include <pro/discover.h>
00090
00091 #include <dev/vscodec.h>
00092
00093 #include "config.h"
00094 #include "logmsg.h"
00095 #include "favlist.h"
00096 #include "shoutcast.h"
00097 #include "httpserv.h"
00098 #include "xmlserv.h"
00099 #include "userif.h"
00100
00101
00102
00103
00104
00105
00106 #define MY_MAC { 0x00, 0x06, 0x98, 0x30, 0x00, 0x39 }
00107
00108
00109
00110
00111
00112
00113 #define MY_IPADDR "192.168.192.39"
00114
00115
00116
00117
00118
00119
00120 #define MY_IPMASK "255.255.255.0"
00121
00122
00123
00124
00125
00126
00127 #define MY_IPGATE "192.168.192.1"
00128
00129
00130
00131
00132 #if defined(AT91SAM9260_EK) || defined(ELEKTOR_IR1)
00133 #define USE_DISCOVERY
00134 #endif
00135
00136
00137
00138
00139 #if defined(AT91SAM9260_EK) || defined(ELEKTOR_IR1)
00140
00141 #endif
00142
00143
00144
00145
00146 #ifndef MY_TZ
00147 #define MY_TZ -1
00148 #endif
00149
00150
00151 WEBRADIO webradio;
00152
00153
00154
00155
00156 static int QueryTimeServer(void)
00157 {
00158 int rc = -1;
00159
00160 #ifdef MY_TIMED
00161 int retries = 5;
00162
00163 _timezone = MY_TZ * 60L * 60L;
00164 while (retries--) {
00165 time_t now;
00166 u_long timeserver = inet_addr(MY_TIMED);
00167
00168
00169 LogMsg(LOG_STARTUP, "Query time from %s...", MY_TIMED);
00170 if(NutSNTPGetTime(×erver, &now) == 0) {
00171 LogMsg(LOG_STARTUP, "OK\n");
00172 rc = 0;
00173 stime(&now);
00174 break;
00175 }
00176 else {
00177 LogMsg(LOG_STARTUP, "failed\n");
00178 }
00179 }
00180 #endif
00181
00182 return rc;
00183 }
00184
00185 int InitEthernetDevice(void)
00186 {
00187 u_long ip_addr = inet_addr(MY_IPADDR);
00188 u_long ip_mask = inet_addr(MY_IPMASK);
00189 u_long ip_gate = inet_addr(MY_IPGATE);
00190 u_char mac[6] = MY_MAC;
00191
00192 for (;;) {
00193 LogMsg(LOG_STARTUP, "Register %s...", DEV_ETHER_NAME);
00194 if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
00195 LogMsg(LOG_STARTUP, "OK\n");
00196 break;
00197 }
00198 LogMsg(LOG_STARTUP, "No Ethernet, retry\n");
00199 }
00200
00201 LogMsg(LOG_STARTUP, "Configure %s...", DEV_ETHER_NAME);
00202 if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000) == 0) {
00203 LogMsg(LOG_STARTUP, "OK\n");
00204 return 0;
00205 }
00206 LogMsg(LOG_STARTUP, "initial boot...");
00207 if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 60000) == 0) {
00208 LogMsg(LOG_STARTUP, "OK\n");
00209 return 0;
00210 }
00211 LogMsg(LOG_STARTUP, "No DHCP...");
00212 NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask);
00213
00214 if(ip_gate) {
00215 LogMsg(LOG_STARTUP, "hard coded gate...");
00216 NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER);
00217 }
00218 LogMsg(LOG_STARTUP, "OK\n");
00219
00220 return 0;
00221 }
00222
00223
00227 int main(void)
00228 {
00229 u_long baud = 115200;
00230 u_long ipgate;
00231 int stream_idx = 0;
00232 u_int rstat;
00233 u_int ostat = 0;
00234
00235
00236
00237
00238
00239 NutRegisterDevice(&DEV_DEBUG, 0, 0);
00240 freopen(DEV_DEBUG_NAME, "w", stdout);
00241 _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00242
00243
00244
00245
00246 #ifdef LOG_ENABLE
00247 #if LOG_ENABLE
00248 LogMsg((u_int)-1, "Internet Radio %s - Nut/OS %s\n", VERSION, NutVersionString());
00249 #endif
00250 #endif
00251 LogMsg(LOG_STARTUP, "%u bytes free\n", (u_int)NutHeapAvailable());
00252 LogMsg(LOG_STARTUP, "CPU Clock : %lu\n", NutGetCpuClock());
00253 LogMsg(LOG_STARTUP, "Master Clock: %lu\n", At91GetMasterClock());
00254
00255 if (NutLoadConfig() == 0) {
00256 LogMsg(LOG_STARTUP, "Hostname: %s\n", confos.hostname);
00257 }
00258
00259
00260
00261
00262 if (ConfigInit()) {
00263
00264 ConfigResetFactory();
00265 FavListResetFactory();
00266 }
00267 else {
00268 if (ConfigLoad()) {
00269
00270 ConfigResetFactory();
00271 ConfigSave();
00272 }
00273 if (FavListLoad()) {
00274
00275 FavListResetFactory();
00276 FavListSave();
00277 }
00278 }
00279
00280
00281
00282
00283 if (NutRegisterDevice(&devVsCodec, 0, 0)) {
00284 LogMsg(LOG_ERROR, "No audio decoder\n");
00285 for(;;);
00286 }
00287 #if USE_DISPLAY
00288
00289
00290
00291 if (NutRegisterDevice(&devSbiLcd, 0, 0)) {
00292 LogMsg(LOG_ERROR, "Failed to register LCD\n");
00293 }
00294 #endif
00295
00296 if (UserIfInit("sbilcd")) {
00297 LogMsg(LOG_ERROR, "LCD init failure\n");
00298 }
00299
00300
00301
00302
00303 InitEthernetDevice();
00304 NutIpRouteQuery(0, &ipgate);
00305 LogMsg(LOG_STARTUP, "IP Addr: %s\n", inet_ntoa(confnet.cdn_ip_addr));
00306 LogMsg(LOG_STARTUP, "IP Mask: %s\n", inet_ntoa(confnet.cdn_ip_mask));
00307 LogMsg(LOG_STARTUP, "IP Gate: %s\n", inet_ntoa(ipgate));
00308 LogMsg(LOG_STARTUP, "PrimDNS: %s\n", inet_ntoa(webradio.wr_pridns));
00309 LogMsg(LOG_STARTUP, "Sec DNS: %s\n", inet_ntoa(webradio.wr_secdns));
00310 if (confnet.cdn_cip_addr) {
00311 NutDnsConfig2(NULL, NULL, webradio.wr_pridns, webradio.wr_secdns);
00312 }
00313
00314
00315
00316
00317
00318 #ifdef USE_DISCOVERY
00319 LogMsg(LOG_STARTUP, "Start Responder...");
00320 if (NutRegisterDiscovery((u_long)-1, 0, DISF_INITAL_ANN)) {
00321 LogMsg(LOG_STARTUP, "failed\n");
00322 }
00323 else {
00324 LogMsg(LOG_STARTUP, "OK\n");
00325 }
00326 #endif
00327
00328
00329
00330
00331 if (QueryTimeServer() == 0) {
00332 time_t now = time(0);
00333 struct _tm *lot = localtime(&now);
00334 LogMsg(LOG_STARTUP, "Date: %02u.%02u.%u\n", lot->tm_mday, lot->tm_mon + 1, 1900 + lot->tm_year);
00335 LogMsg(LOG_STARTUP, "Time: %02u:%02u:%02u\n", lot->tm_hour, lot->tm_min, lot->tm_sec);
00336 #ifdef USE_HTTPSERVER
00337 h_timevalid = 1;
00338 #endif
00339 }
00340
00341 #ifdef USE_HTTPSERVER
00342 HttpServerStart();
00343 XmlServerStart();
00344 #endif
00345
00346
00347
00348
00349 webradio.wr_ritab[0] = ReceiverCreate(&rpiShoutcast);
00350
00351
00352
00353
00354 for (;;) {
00355
00356
00357
00358 UserIfShowStatus(DIST_CONNECTING);
00359 webradio.wr_sip = StationConnect(&favlist[1], stream_idx);
00360 if (webradio.wr_sip == NULL) {
00361
00362 UserIfShowStatus(DIST_DEAD);
00363 NutSleep(1000);
00364 } else {
00365
00366 webradio.wr_rip = ReceiverStart(webradio.wr_ritab, webradio.wr_sip);
00367 if (webradio.wr_rip == NULL) {
00368
00369 UserIfShowStatus(DIST_DEAD);
00370 NutSleep(1000);
00371 } else {
00372
00373 UserIfShowStatus(DIST_CONNECTED);
00374
00375 webradio.wr_favupd = 10;
00376
00377 for (;;) {
00378 NutSleep(1000);
00379 if (favlist[TOP_FAVORITE].rs_streams) {
00380 break;
00381 }
00382
00383 if (webradio.wr_favupd && --webradio.wr_favupd == 0) {
00384 FavListSave();
00385 }
00386
00387 if (webradio.wr_reboot) {
00388 webradio.wr_reboot--;
00389 if (webradio.wr_reboot == 0) {
00390 outr(RSTC_CR, RSTC_KEY | RSTC_PROCRST);
00391 }
00392 LogMsg(LOG_WARN, "Reboot in %ds\n", webradio.wr_reboot);
00393 }
00394
00395 rstat = ReceiverStatus(webradio.wr_rip);
00396 if (rstat != ostat) {
00397 if (rstat & (RSTAT_IDLE | RSTAT_STOP)) {
00398 break;
00399 }
00400 UserIfShowStatus(DIST_FORCE);
00401 ostat = rstat;
00402 }
00403 }
00404 UserIfShowStatus(DIST_NONE);
00405 ReceiverStop(webradio.wr_rip);
00406 }
00407 StationDisconnect(webradio.wr_sip);
00408 webradio.wr_sip = NULL;
00409 }
00410 if (favlist[TOP_FAVORITE].rs_streams) {
00411 LogMsg(LOG_STATION, "Selected %s\n", favlist[TOP_FAVORITE].rs_name);
00412 FavListCopy(TOP_FAVORITE, LAST_FAVORITE);
00413 FavListSet(TOP_FAVORITE, NULL, NULL);
00414 stream_idx = 0;
00415 } else {
00416 if (++stream_idx >= favlist[LAST_FAVORITE].rs_streams) {
00417 stream_idx = 0;
00418 }
00419 }
00420 }
00421 }