Main Page | Alphabetical List | Data Structures | File List | Data Fields | Globals

webradio.c

Go to the documentation of this file.
00001 
00045 /*
00046  * Limited AT91SAM9260-EK MP3 player sample.
00047  *
00048  * Will sequentially play all MP3 files from an MMC's root directory.
00049  *
00050  * MultiMedia Cards only, no SD Cards (see arch/arm/dev/at91_mmc.c).
00051  * Root directory must contain MP3 files only.
00052  * Hardcoded for 180MHz CPU clock and 44.1kHz sampling rate (also see ./at73dac.c).
00053  */
00054 
00055 #include <cfg/os.h>
00056 #include <cfg/clock.h>
00057 #include <dev/board.h>
00058 #include <dev/st7036.h>
00059 
00060 #include <stdlib.h>
00061 #include <string.h>
00062 #include <stdio.h>
00063 #include <time.h>
00064 #include <fcntl.h>
00065 #include <io.h>
00066 
00067 #include <sys/version.h>
00068 #include <sys/confnet.h>
00069 #include <sys/atom.h>
00070 #include <sys/heap.h>
00071 #include <sys/thread.h>
00072 #include <sys/timer.h>
00073 #include <sys/event.h>
00074 #include <sys/socket.h>
00075 
00076 #include <hxmp3/mp3dec.h>
00077 
00078 #include <netinet/tcp.h>
00079 #include <arpa/inet.h>
00080 #include <net/route.h>
00081 
00082 #include <pro/dhcp.h>
00083 #include <pro/sntp.h>
00084 #include <pro/discover.h>
00085 
00086 #include "tlv320dac.h"
00087 #include "config.h"
00088 #include "shoutcast.h"
00089 #include "player.h"
00090 #include "httpserv.h"
00091 #include "userif.h"
00092 
00093 /*
00094  * Unique MAC address of the Ethernut Board.
00095  *
00096  * Ignored if EEPROM contains a valid configuration.
00097  */
00098 #define MY_MAC { 0x00, 0x06, 0x98, 0x30, 0x00, 0x35 }
00099 
00100 /*
00101  * Unique IP address of the Ethernut Board.
00102  *
00103  * Ignored if DHCP is used.
00104  */
00105 #define MY_IPADDR "192.168.192.35"
00106 
00107 /*
00108  * IP network mask of the Ethernut Board.
00109  *
00110  * Ignored if DHCP is used.
00111  */
00112 #define MY_IPMASK "255.255.255.0"
00113 
00114 /*
00115  * Gateway IP address for the Ethernut Board.
00116  *
00117  * Ignored if DHCP is used.
00118  */
00119 #define MY_IPGATE "192.168.192.1"
00120 
00121 /*
00122  * Wether we should run a discovery responder.
00123  */
00124 #if defined(AT91SAM9260_EK)
00125 #define USE_DISCOVERY
00126 #endif
00127 
00128 /*
00129  * IP address of the host running a time daemon. 
00130  */
00131 #if defined(AT91SAM9260_EK)
00132 //#define MY_TIMED "130.149.17.21"
00133 #endif
00134 
00135 /*
00136  * Local timezone, -1 for Central Europe. 
00137  */
00138 #ifndef MY_TZ
00139 #define MY_TZ    -1
00140 #endif
00141 
00142 RADIOCONTROL radio;
00143 
00144 /*
00145  * Query a time server and optionally update the hardware clock.
00146  */
00147 static int QueryTimeServer(void)
00148 {
00149     int rc = -1;
00150 
00151 #ifdef MY_TIMED
00152     int retries = 5;
00153 
00154     _timezone = MY_TZ * 60L * 60L;
00155     while (retries--) {
00156         time_t now;
00157         u_long timeserver = inet_addr(MY_TIMED);
00158 
00159         /* Query network time service and set the system time. */
00160         printf("Query time from %s...", MY_TIMED);
00161         if(NutSNTPGetTime(&timeserver, &now) == 0) {
00162             puts("OK");
00163             rc = 0;
00164             stime(&now);
00165             break;
00166         }
00167         else {
00168             puts("failed");
00169         }
00170     }
00171 #endif
00172 
00173     return rc;
00174 }
00175 
00176 int InitEthernetDevice(void)
00177 {
00178     u_long ip_addr = inet_addr(MY_IPADDR);
00179     u_long ip_mask = inet_addr(MY_IPMASK);
00180     u_long ip_gate = inet_addr(MY_IPGATE);
00181     u_char mac[6] = MY_MAC;
00182 
00183     for (;;) {
00184         printf("Register %s...", DEV_ETHER_NAME);
00185         if (NutRegisterDevice(&DEV_ETHER, 0x8300, 5) == 0) {
00186             puts("OK");
00187             break;
00188         }
00189         puts("No Ethernet, retry");
00190     }
00191 
00192     printf("Configure %s...", DEV_ETHER_NAME);
00193     if (NutDhcpIfConfig(DEV_ETHER_NAME, 0, 60000) == 0) {
00194         puts("OK");
00195         return 0;
00196     }
00197     printf("initial boot...");
00198     if (NutDhcpIfConfig(DEV_ETHER_NAME, mac, 60000) == 0) {
00199         puts("OK");
00200         return 0;
00201     }
00202     printf("No DHCP...");
00203     NutNetIfConfig(DEV_ETHER_NAME, mac, ip_addr, ip_mask);
00204     /* Without DHCP we had to set the default gateway manually.*/
00205     if(ip_gate) {
00206         printf("hard coded gate...");
00207         NutIpRouteAdd(0, 0, ip_gate, &DEV_ETHER);
00208     }
00209     puts("OK");
00210 
00211     return 0;
00212 }
00213 
00217 int main(void)
00218 {
00219     u_long baud = 115200;
00220     u_long ipgate;
00221     u_int failcnt = 0;
00222 
00223     /*
00224      * Register and open the DBGU device, which we will use for debug 
00225      * output via stdout.
00226      */
00227     NutRegisterDevice(&DEV_DEBUG, 0, 0);
00228     freopen(DEV_DEBUG_NAME, "w", stdout);
00229     _ioctl(_fileno(stdout), UART_SETSPEED, &baud);
00230 
00231     /*
00232      * Display some general information.
00233      */
00234     printf("\n\nSAM Internet Radio %s - Nut/OS %s\n", VERSION, NutVersionString());
00235     printf("%u bytes free\n", (u_int)NutHeapAvailable());
00236     printf("CPU Clock   : %lu\n", NutGetCpuClock());
00237     printf("Master Clock: %lu\n", At91GetMasterClock());
00238 
00239     /*
00240      * Initialize the user interface, LCD and 3 buttons.
00241      */
00242     if (NutRegisterDevice(&devSbiLcd, 0, 0)) {
00243         puts("Failed to register LCD");
00244     }
00245     if (UserIfInit("sbilcd")) {
00246         puts("LCD init failure\n");
00247     }
00248 
00249     /* 
00250      * Initialize the Ethernet device and print our IP setting. 
00251      */
00252     InitEthernetDevice();
00253     printf("IP Addr: %s\n", inet_ntoa(confnet.cdn_ip_addr));
00254     printf("IP Mask: %s\n", inet_ntoa(confnet.cdn_ip_mask));
00255     NutIpRouteQuery(0, &ipgate);
00256     printf("IP Gate: %s\n", inet_ntoa(ipgate));
00257 
00258     /* 
00259      * Register a discovery responder. This optional feature
00260      * allows to discover all Nut/OS devices in a local network.
00261      */
00262 #ifdef USE_DISCOVERY
00263     printf("Start Responder...");
00264     if (NutRegisterDiscovery((u_long)-1, 0, DISF_INITAL_ANN)) {
00265         puts("failed");
00266     }
00267     else {
00268         puts("OK");
00269     }
00270 #endif
00271 
00272     /*
00273      * Try to get current date and time from an SNTP server.
00274      */
00275     if (QueryTimeServer() == 0) {
00276         time_t now = time(0);
00277         struct _tm *lot = localtime(&now);
00278         printf("Date: %02u.%02u.%u\n", lot->tm_mday, lot->tm_mon + 1, 1900 + lot->tm_year);
00279         printf("Time: %02u:%02u:%02u\n", lot->tm_hour, lot->tm_min, lot->tm_sec);
00280 #ifdef USE_HTTPSERVER
00281         h_timevalid = 1;
00282 #endif
00283     }
00284 
00285 #ifdef USE_HTTPSERVER
00286     HttpServerStart();
00287 #endif
00288 
00289     ConfigResetFactory();
00290 
00291     /*
00292      * Create all required receiver and player instances.
00293      */
00294     radio.rc_rip = ReceiverCreate(&rpiShoutcast);
00295     radio.rc_pip = PlayerCreate(&ppiMp3);
00296 
00297     /*
00298      * Never ending application loop.
00299      */
00300     for (;;) {
00301         if (failcnt > 3) {
00302             printf("Too many failures. Select next station.\n");
00303             radio.rc_rstation = StationSelect(radio.rc_cstation, 1);
00304             failcnt = 0;
00305         }
00306         radio.rc_cstation = radio.rc_rstation;
00307         printf("Trying station entry %u\n", radio.rc_cstation);
00308 
00309         /*
00310          * Connect station.
00311          */
00312         UserIfShowStatus(DIST_CONNECTING);
00313         radio.rc_sip = StationConnect(&station[radio.rc_cstation]);
00314         if (radio.rc_sip == NULL) {
00315             /* Connection failed. */
00316             UserIfShowStatus(DIST_DEAD);
00317             NutSleep(1000);
00318             radio.rc_rstation = StationSelect(radio.rc_cstation, 0);
00319             failcnt++;
00320             continue;
00321         }
00322 
00323         /*
00324          * Station connected.
00325          */
00326         printf("Connected: %u bytes free\n", (u_int)NutHeapAvailable());
00327         if (ReceiverStart(radio.rc_rip, radio.rc_sip) == 0) {
00328             UserIfShowStatus(DIST_CONNECTED);
00329             if (radio.rc_sip->si_ptype == PLAYER_TYPE_MP3) {
00330                 if (PlayerStart(radio.rc_pip, radio.rc_rip) == 0) {
00331                     failcnt = 0;
00332                     NutSleep(1000);
00333                     while (radio.rc_cstation == radio.rc_rstation) {
00334                         if (PlayerStatus(radio.rc_pip) != PSTAT_RUNNING) {
00335                             radio.rc_rstation = StationSelect(radio.rc_cstation, 0);
00336                             failcnt++;
00337                             break;
00338                         }
00339                         NutSleep(1000);
00340                     }
00341                     printf("\nStop player ");
00342                     PlayerStop(radio.rc_pip);
00343                 }
00344                 else {
00345                     radio.rc_rstation = StationSelect(radio.rc_cstation, 0);
00346                     failcnt++;
00347                 }
00348             }
00349             else {
00350                 printf("Bad content type\n");
00351                 radio.rc_rstation = StationSelect(radio.rc_cstation, 0);
00352                 failcnt++;
00353             }
00354             printf("\nStop receiver ");
00355             ReceiverStop(radio.rc_rip);
00356         }
00357         else {
00358             radio.rc_rstation = StationSelect(radio.rc_cstation, 0);
00359             failcnt++;
00360         }
00361         printf("\nDisconnect station ");
00362         StationDisconnect(radio.rc_sip);
00363         radio.rc_sip = NULL;
00364     }
00365 }

Generated on Fri Feb 23 17:28:49 2007 for SAM Internet Radio by  doxygen 1.4.4