webradio/receiver.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2007 by egnite Software GmbH. All rights reserved.
00003  * Copyright (C) 2008 by egnite GmbH. All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  *
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  * 3. Neither the name of the copyright holders nor the names of
00015  *    contributors may be used to endorse or promote products derived
00016  *    from this software without specific prior written permission.
00017  *
00018  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00021  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00022  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00023  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00024  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00025  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00026  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00028  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00029  * SUCH DAMAGE.
00030  *
00031  * For additional information see http://www.ethernut.de/
00032  */
00033 
00045 #include <cfg/os.h>
00046 #include <cfg/clock.h>
00047 #include <dev/board.h>
00048 
00049 #include <stdlib.h>
00050 #include <string.h>
00051 #include <stdio.h>
00052 #include <fcntl.h>
00053 #include <io.h>
00054 
00055 #include <sys/version.h>
00056 #include <sys/confnet.h>
00057 #include <sys/atom.h>
00058 #include <sys/heap.h>
00059 #include <sys/thread.h>
00060 #include <sys/timer.h>
00061 #include <sys/event.h>
00062 #include <sys/socket.h>
00063 
00064 #if defined(USE_SOFTWARE_CODEC)
00065 #include <hxmp3/mp3dec.h>
00066 #endif
00067 
00068 #include <netinet/tcp.h>
00069 #include <arpa/inet.h>
00070 #include <net/route.h>
00071 #include <pro/dhcp.h>
00072 
00073 #include "config.h"
00074 #include "logmsg.h"
00075 #include "receiver.h"
00076 #include "shoutcast.h"
00077 
00086 RECEIVERINFO * ReceiverCreate(RECEIVERPLUGIN *plugin)
00087 {
00088     RECEIVERINFO *rip;
00089 
00090     /* Allocate and initialize the receiver info structure. */
00091     if ((rip = malloc(sizeof(RECEIVERINFO))) != NULL) {
00092         memset(rip, 0, sizeof(RECEIVERINFO));
00093         rip->ri_rpp = plugin;
00094         /* Associate a new player plug-in instance. */
00095         if ((*plugin->rp_create) (rip)) {
00096             free(rip);
00097             rip = NULL;
00098         }
00099     }
00100     return rip;
00101 }
00102 
00113 static int ReceiverPlugInControl(RECEIVERINFO *rip, u_int sst, u_int xst)
00114 {
00115     int retries = 10;
00116 
00117     while ((rip->ri_status & xst) == 0) {
00118         rip->ri_status |= sst;
00119         NutEventPost(&rip->ri_cmdevt);
00120         NutEventWait(&rip->ri_stsevt, 500);
00121         if (retries-- <= 0) {
00122             LogMsg(LOG_ERROR, "No receiver control (%02x->%02x->%02x)\n", sst, xst, rip->ri_status);
00123             return -1;
00124         }
00125     }
00126     return 0;
00127 }
00128 
00136 u_int ReceiverStatus(RECEIVERINFO *rip)
00137 {
00138     return rip->ri_status;
00139 }
00140 
00148 int ReceiverStop(RECEIVERINFO *rip)
00149 {
00150     return ReceiverPlugInControl(rip, RSTAT_STOP, RSTAT_IDLE);
00151 }
00152 
00162 RECEIVERINFO *ReceiverStart(RECEIVERINFO *ritab[], STATIONINFO *sip)
00163 {
00164     RECEIVERINFO *rip;
00165     int i;
00166 
00167     for (i = 0; ritab[i]; i++) {
00168         rip = ritab[i];
00169         if ((*rip->ri_rpp->rp_setup)(rip, sip) == 0) {
00170             /* Found the right receiver. Make sure it has stopped. */
00171             if (ReceiverStop(rip) == 0) {
00172                 /* Now start this receiver. */
00173                 rip->ri_sip = sip;
00174                 if (ReceiverPlugInControl(rip, RSTAT_START, RSTAT_BUFFERING | RSTAT_RUNNING) == 0) {
00175                     /* Success. */
00176                     return rip;
00177                 }
00178             }
00179         }
00180     }
00181     LogMsg(LOG_WARN, "Receiver start failed\n");
00182 
00183     return NULL;
00184 }
00185 

© 2008 by egnite GmbH - visit www.ethernut.de