webradio/favlist.c

Go to the documentation of this file.
00001 
00045 #include <cfg/os.h>
00046 #include <cfg/clock.h>
00047 #include <dev/board.h>
00048 #include <dev/at45db.h>
00049 #include <dev/at91_spi.h>
00050 
00051 #include <stdlib.h>
00052 #include <string.h>
00053 #include <stdio.h>
00054 #include <fcntl.h>
00055 #include <io.h>
00056 
00057 #include <sys/version.h>
00058 #include <sys/confnet.h>
00059 #include <sys/atom.h>
00060 #include <sys/heap.h>
00061 #include <sys/thread.h>
00062 #include <sys/timer.h>
00063 #include <sys/event.h>
00064 #include <sys/socket.h>
00065 
00066 #if defined(USE_SOFTWARE_CODEC)
00067 #include <hxmp3/mp3dec.h>
00068 #endif
00069 
00070 #include <netinet/tcp.h>
00071 #include <arpa/inet.h>
00072 #include <net/route.h>
00073 #include <pro/dhcp.h>
00074 
00075 #include "config.h"
00076 #include "logmsg.h"
00077 #include "favlist.h"
00078 #include "webradio.h"
00079 
00080 #define FAVLIST_MAGIC   "FL1"
00081 #define FAVLIST_SECTOR  16
00082 
00083 
00084 RADIOSTATION favlist[MAX_FAVORITES];
00085 
00091 static void FavListClear(void)
00092 {
00093     int idx;
00094     int i;
00095 
00096     /* Release all memory occupied by the current configuration. */
00097     for (idx = 0; idx < MAX_FAVORITES; idx++) {
00098         if (favlist[idx].rs_name) {
00099             free(favlist[idx].rs_name);
00100         }
00101         for (i = 0; i < favlist[idx].rs_streams; i++) {
00102             if (favlist[idx].rs_uri[i]) {
00103                 free(favlist[idx].rs_uri[i]);
00104             }
00105         }
00106     }
00107     memset(favlist, 0, MAX_FAVORITES * sizeof(RADIOSTATION));
00108 }
00109 
00123 int FavListSet(int idx, CONST char *name, CONST char * uri)
00124 {
00125     int i;
00126 
00127     LogMsg(LOG_CONFIG, "Set %d,%s,%s\n", idx, name, uri);
00128     if (idx >= MAX_FAVORITES) {
00129         /* New entry. Find a free slot. */
00130         idx = LAST_FAVORITE + 1;
00131         while (idx < MAX_FAVORITES && favlist[idx].rs_name) {
00132             idx++;
00133         }
00134     }
00135     if (idx >= MAX_FAVORITES) {
00136         /* We are full. */
00137         return -1;
00138     }
00139     if (name || (name == NULL && uri == NULL)) {
00140         if (favlist[idx].rs_name) {
00141             free(favlist[idx].rs_name);
00142         }
00143         favlist[idx].rs_name = NULL;
00144     }
00145     if (name) {
00146         favlist[idx].rs_name = strdup(name);
00147     }
00148 
00149     if (uri) {
00150         if (favlist[idx].rs_streams < MAXNUM_STREAMS) {
00151             favlist[idx].rs_uri[favlist[idx].rs_streams] = strdup(uri);
00152             favlist[idx].rs_streams++;
00153         }
00154         else {
00155             idx = -1;
00156         }
00157     }
00158     else {
00159         for (i = 0; i < favlist[idx].rs_streams; i++) {
00160             if (favlist[idx].rs_uri[i]) {
00161                 free(favlist[idx].rs_uri[i]);
00162             }
00163             favlist[idx].rs_uri[i] = NULL;
00164         }
00165         favlist[idx].rs_streams = 0;
00166     }
00167     return idx;
00168 }
00169 
00178 int FavListCopy(int src, int dst)
00179 {
00180     int i;
00181 
00182     /*
00183      * Release an existing entry at the destination index.
00184      */
00185     LogMsg(LOG_CONFIG, "Copying %d to %d\n", src, dst);
00186     if (favlist[dst].rs_name) {
00187         free(favlist[dst].rs_name);
00188     }
00189     for (i = 0; i < favlist[dst].rs_streams; i++) {
00190         if (favlist[dst].rs_uri[i]) {
00191             free(favlist[dst].rs_uri[i]);
00192         }
00193     }
00194     memset(&favlist[dst], 0, sizeof(RADIOSTATION));
00195 
00196     /*
00197      * Copy the source entry to the destination index.
00198      */
00199     if (favlist[src].rs_name) {
00200         favlist[dst].rs_name = strdup(favlist[src].rs_name);
00201         for (i = 0; i < favlist[src].rs_streams; i++) {
00202             if (favlist[src].rs_uri[i]) {
00203                 favlist[dst].rs_uri[favlist[dst].rs_streams++] = strdup(favlist[src].rs_uri[i]);
00204             }
00205         }
00206         return 0;
00207     }
00208     return -1;
00209 }
00210 
00219 int FavListSearch(int idx, int dir)
00220 {
00221     int rc = idx;
00222 
00223     LogMsg(LOG_CONFIG, "Searching %d %d\n", idx, dir);
00224     for (;;) {
00225         rc += dir;
00226         if (rc <= LAST_FAVORITE) {
00227             rc = MAX_FAVORITES - 1;
00228         }
00229         else if (rc >= MAX_FAVORITES) {
00230             rc = LAST_FAVORITE + 1;
00231         }
00232         if ((favlist[rc].rs_name && favlist[rc].rs_streams)) {
00233             /* New entry found. */
00234             break;
00235         }
00236         if (rc == idx) {
00237             /* Whole list searched, but no entry found. */
00238             return -1;
00239         }
00240     }
00241     return rc;
00242 }
00243 
00249 size_t FavListSize(void)
00250 {
00251     size_t rc = 0;
00252     int idx;
00253     int i;
00254     RADIOSTATION *flp;
00255 
00256     for (idx = 1; idx < MAX_FAVORITES; idx++) {
00257         flp = &favlist[idx];
00258         if (flp->rs_name) {
00259             rc += strlen(flp->rs_name) + 1;
00260             for (i = 0; i < flp->rs_streams; i++) {
00261                 if (flp->rs_uri[i]) {
00262                     rc += strlen(flp->rs_uri[i]) + 1;
00263                 }
00264             }
00265         }
00266     }
00267     return rc + 1;
00268 }
00269 
00275 int FavListSave(void)
00276 {
00277     int idx;
00278     int i;
00279     RADIOSTATION *rsp;
00280 
00281     /* Rewind and save the magic name. */
00282     ConfigRewind(FAVLIST_SECTOR);
00283     ConfigSaveString(FAVLIST_MAGIC);
00284 
00285     /* Save stations. */
00286     for (idx = LAST_FAVORITE; idx < MAX_FAVORITES; idx++) {
00287         rsp = &favlist[idx];
00288         if (rsp->rs_name) {
00289             LogMsg(LOG_CONFIG, "Saving %d %s\n", idx, rsp->rs_name);
00290             ConfigSaveString(rsp->rs_name);
00291             for (i = 0; i < rsp->rs_streams; i++) {
00292                 if (rsp->rs_uri[i]) {
00293                     LogMsg(LOG_CONFIG, "  URI=%s\n", rsp->rs_uri[i]);
00294                     ConfigSaveString(rsp->rs_uri[i]);
00295                 }
00296             }
00297             ConfigSaveString("");
00298         }
00299         else if (idx == LAST_FAVORITE) {
00300             ConfigSaveString("None");
00301             ConfigSaveString("");
00302         }
00303         NutThreadYield();
00304     }
00305     ConfigSaveString("");
00306     ConfigFlush();
00307 
00308     return 0;
00309 }
00310 
00316 int FavListLoad(void)
00317 {
00318     int rc = -1;
00319     char *buf;
00320     int idx;
00321     int i;
00322 
00323     buf = malloc(255);
00324     ConfigRewind(FAVLIST_SECTOR);
00325     ConfigLoadString(buf, 4);
00326     if (strcmp(buf, FAVLIST_MAGIC) == 0) {
00327         FavListClear();
00328         rc = 0;
00329 
00330         for (idx = LAST_FAVORITE; idx < MAX_FAVORITES; idx++) {
00331             ConfigLoadString(buf, 255);
00332             if (*buf == 0) {
00333                 break;
00334             }
00335             favlist[idx].rs_name = strdup(buf);
00336             LogMsg(LOG_CONFIG, "Loading %d %s\n", idx, favlist[idx].rs_name);
00337             for (i = 0; i < MAXNUM_STREAMS; i++) {
00338                 ConfigLoadString(buf, 255);
00339                 if (*buf == 0) {
00340                     break;
00341                 }
00342                 LogMsg(LOG_CONFIG, "  URI=%s\n", buf);
00343                 favlist[idx].rs_uri[i] = strdup(buf);
00344                 favlist[idx].rs_streams++;
00345             }
00346         }
00347     }
00348     free(buf);
00349 
00350     return rc;
00351 }
00352 
00356 void FavListResetFactory(void)
00357 {
00358     int idx;
00359 
00360     FavListClear();
00361 
00362     /* 
00363      * Add pre-configured radio stations. 
00364      */
00365     if ((idx = FavListSet(MAX_FAVORITES, "FREQUENCE3", "88.191.22.199:8000")) == -1) {
00366         return;
00367     }
00368     FavListSet(idx, NULL, "194.158.114.68:8000");
00369     FavListSet(idx, NULL, "194.158.114.67:8000");
00370     FavListSet(idx, NULL, "193.251.154.243:8000");
00371     FavListSet(idx, NULL, "213.186.56.206:8000");
00372     FavListSet(idx, NULL, "194.158.114.66:8000");
00373     FavListSet(idx, NULL, "213.186.41.160:8000");
00374     FavListSet(idx, NULL, "88.191.11.19:8000");
00375     FavListSet(idx, NULL, "62.4.21.91:8000");
00376     FavListSet(idx, NULL, "212.129.62.140:8000");
00377     FavListSet(idx, NULL, "194.117.194.66:8000");
00378     FavListSet(idx, NULL, "213.186.37.124:8000");
00379     FavListSet(idx, NULL, "80.65.234.120:8000");
00380     FavListSet(idx, NULL, "213.161.201.21:8000");
00381     FavListSet(idx, NULL, "213.186.60.54:8000");
00382     FavListSet(idx, NULL, "193.251.154.242:8000");
00383     FavListSet(idx, NULL, "83.143.18.76:8000");
00384     FavListSet(idx, NULL, "193.17.192.35:8000");
00385     FavListSet(idx, NULL, "193.222.128.135:8000");
00386 
00387     /* Start with this reliable station. */
00388     FavListCopy(idx, LAST_FAVORITE);
00389 
00390     if ((idx = FavListSet(MAX_FAVORITES, "FREQUENCE3 Ogg", "ogg.frequence3.net:19000/frequence3.ogg")) == -1) {
00391         return;
00392     }
00393     FavListSet(idx, NULL, "ganymede.frequence3.net:19000/frequence3.ogg");
00394     FavListSet(idx, NULL, "aspairt.frequence3.net:19000/frequence3.ogg");
00395     FavListSet(idx, NULL, "mao.frequence3.net:19000/frequence3.ogg");
00396     FavListSet(idx, NULL, "pluton.frequence3.net:19000/frequence3.ogg");
00397 
00398     if ((idx = FavListSet(MAX_FAVORITES, "VPR Classical", "205.234.168.42:8000")) == -1) {
00399         return;
00400     }
00401     FavListSet(idx, NULL, "205.234.168.42:80");
00402 
00403     if (FavListSet(MAX_FAVORITES, "All Classical 89.9 KBPS Portland", "216.218.147.60:8220") == -1) {
00404         return;
00405     }
00406 
00407     if ((idx = FavListSet(MAX_FAVORITES, ".977 Hitz Channel", "64.236.34.97/stream/1074")) == -1) {
00408         return;
00409     }
00410     FavListSet(idx, NULL, "205.188.215.230:8002");
00411 
00412     if ((idx = FavListSet(MAX_FAVORITES, "Rock'One", "88.191.12.222:6005")) == -1) {
00413         return;
00414     }
00415     FavListSet(idx, NULL, "80.65.234.119:6005");
00416     FavListSet(idx, NULL, "194.117.194.66:6005");
00417 
00418     if (FavListSet(MAX_FAVORITES, "veNet Radio", "71.62.186.136:1039") == -1) {
00419         return;
00420     }
00421     if (FavListSet(MAX_FAVORITES, "gamer-FM", "82.149.224.55:8500") == -1) {
00422         return;
00423     }
00424 
00425     if ((idx = FavListSet(MAX_FAVORITES, "BigB", "70.87.68.38:9000")) == -1) {
00426         return;
00427     }
00428     FavListSet(idx, NULL, "74.52.7.164:9000");
00429     FavListSet(idx, NULL, "67.19.249.226:8000");
00430 
00431     if ((idx = FavListSet(MAX_FAVORITES, "SKY.FM Hits", "212.23.5.99:8006")) == -1) {
00432         return;
00433     }
00434     FavListSet(idx, NULL, "160.79.128.62:6628");
00435     FavListSet(idx, NULL, "64.74.207.40:8628");
00436 
00437     if ((idx = FavListSet(MAX_FAVORITES, "181.FM Classic Hits", "scfire-nyk-aa01.stream.aol.com:80/stream/1094")) == -1) {
00438         return;
00439     }
00440     FavListSet(idx, NULL, "scfire-nyk-aa02.stream.aol.com:80/stream/1094");
00441     FavListSet(idx, NULL, "scfire-nyk-aa03.stream.aol.com:80/stream/1094");
00442     FavListSet(idx, NULL, "scfire-nyk-aa04.stream.aol.com:80/stream/1094");
00443     FavListSet(idx, NULL, "scfire-nyk-aa05.stream.aol.com:80/stream/1094");
00444     FavListSet(idx, NULL, "scfire-dll-aa02.stream.aol.com:80/stream/1094");
00445     FavListSet(idx, NULL, "scfire-dll-aa03.stream.aol.com:80/stream/1094");
00446     FavListSet(idx, NULL, "scfire-dll-aa04.stream.aol.com:80/stream/1094");
00447     FavListSet(idx, NULL, "scfire-dll-aa05.stream.aol.com:80/stream/1094");
00448     FavListSet(idx, NULL, "scfire-chi-aa01.stream.aol.com:80/stream/1094");
00449     FavListSet(idx, NULL, "scfire-chi-aa02.stream.aol.com:80/stream/1094");
00450     FavListSet(idx, NULL, "scfire-chi-aa03.stream.aol.com:80/stream/1094");
00451     FavListSet(idx, NULL, "scfire-chi-aa04.stream.aol.com:80/stream/1094");
00452     FavListSet(idx, NULL, "scfire-chi-aa05.stream.aol.com:80/stream/1094");
00453     FavListSet(idx, NULL, "scfire-ntc-aa01.stream.aol.com:80/stream/1094");
00454     FavListSet(idx, NULL, "scfire-ntc-aa02.stream.aol.com:80/stream/1094");
00455     FavListSet(idx, NULL, "scfire-ntc-aa03.stream.aol.com:80/stream/1094");
00456     FavListSet(idx, NULL, "scfire-ntc-aa04.stream.aol.com:80/stream/1094");
00457     FavListSet(idx, NULL, "scfire-ntc-aa05.stream.aol.com:80/stream/1094");
00458     FavListSet(idx, NULL, "207.200.96.227:8008");
00459 
00460     if ((idx = FavListSet(MAX_FAVORITES, "Virgin Radio", "ice-mp3-vr-128.smgradio.com:80/vr.mp3")) == -1) {
00461         return;
00462     }
00463     FavListSet(idx, NULL, "85.159.184.44:80");
00464 
00465     if (FavListSet(MAX_FAVORITES, "Virgin Radio Ogg", "ogg2.smgradio.com:80/gr32.ogg") == -1) {
00466         return;
00467     }
00468 
00469     if (FavListSet(MAX_FAVORITES, "Metal Only", "80.67.23.74:6665") == -1) {
00470         return;
00471     }
00472 
00473     if ((idx = FavListSet(MAX_FAVORITES, "Radio K", "160.94.138.250:8128")) == -1) {
00474         return;
00475     }
00476     FavListSet(idx, NULL, "radiokstreams.cce.umn.edu:8128");
00477 
00478     if ((idx = FavListSet(MAX_FAVORITES, "SpacialNet", "64.124.106.176:22122")) == -1) {
00479         return;
00480     }
00481     FavListSet(idx, NULL, "sc9.spacialnet.com:22122");
00482 
00483     if ((idx = FavListSet(MAX_FAVORITES, "Blue Sugar Radio", "209.51.162.170:9560")) == -1) {
00484         return;
00485     }
00486     FavListSet(idx, NULL, "66.92.79.161:8000");
00487 
00488     if (FavListSet(MAX_FAVORITES, "Pre Party Radio", "ds130.twilightinc.nl:8000/ogg-128-prepartyradio-com.ogg") == -1) {
00489         return;
00490     }
00491 
00492     if (FavListSet(MAX_FAVORITES, "KAMP Student Radio", "128.196.31.10:8000/high.ogg") == -1) {
00493         return;
00494     }
00495 
00496     if (FavListSet(MAX_FAVORITES, "Project FX", "85.25.134.48:8000/evil.ogg") == -1) {
00497         return;
00498     }
00499     if (FavListSet(MAX_FAVORITES, "Coco Radio", "67.159.26.47:1010/Cocoradio32k.ogg?refid=1") == -1) {
00500         return;
00501     }
00502     if (FavListSet(MAX_FAVORITES, "European Music Radio", "nednl.net:8000/emr.ogg") == -1) {
00503         return;
00504     }
00505     if (FavListSet(MAX_FAVORITES, "Radio Bangkok", "216.66.69.100:8000/live.ogg") == -1) {
00506         return;
00507     }
00508     if (FavListSet(MAX_FAVORITES, "Radio Swiss Classic", "stream-1.ssatr.ch:80/rsc32.ogg") == -1) {
00509         return;
00510     }
00511     if ((idx = FavListSet(MAX_FAVORITES, "Eins Live", "gffstream.ic.llnwd.net/stream/gffstream_stream_wdr_einslive_a")) == -1) {
00512         return;
00513     }
00514     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_stream_wdr_einslive_b");
00515 
00516     if ((idx = FavListSet(MAX_FAVORITES, "Eins Live Diggi", "gffstream.ic.llnwd.net/stream/gffstream_einslive_a")) == -1) {
00517         return;
00518     }
00519     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_einslive_b");
00520 
00521     if ((idx = FavListSet(MAX_FAVORITES, "Eins Live Kunst", "gffstream.ic.llnwd.net/stream/gffstream_gffstream5")) == -1) {
00522         return;
00523     }
00524     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_gffstream6");
00525 
00526     if ((idx = FavListSet(MAX_FAVORITES, "WDR 2", "gffstream.ic.llnwd.net/stream/gffstream_w17b")) == -1) {
00527         return;
00528     }
00529     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_w17a");
00530 
00531     if ((idx = FavListSet(MAX_FAVORITES, "WDR 3", "gffstream.ic.llnwd.net/stream/gffstream_w21a")) == -1) {
00532         return;
00533     }
00534     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_w21b");
00535 
00536     if ((idx = FavListSet(MAX_FAVORITES, "WDR 4", "gffstream.ic.llnwd.net/stream/gffstream_w18b")) == -1) {
00537         return;
00538     }
00539     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_w18a");
00540 
00541     if ((idx = FavListSet(MAX_FAVORITES, "WDR 5", "gffstream.ic.llnwd.net/stream/gffstream_w19a")) == -1) {
00542         return;
00543     }
00544     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_w19b");
00545 
00546     if ((idx = FavListSet(MAX_FAVORITES, "Kiraka", "gffstream.ic.llnwd.net/stream/gffstream_gffwdr1a")) == -1) {
00547         return;
00548     }
00549     FavListSet(idx, NULL, "gffstream.ic.llnwd.net/stream/gffstream_gffwdr1b");
00550 
00551 
00552     if ((idx = FavListSet(MAX_FAVORITES, "Decades Radio", "203.16.26.101:80/decades192.ogg")) == -1) {
00553         return;
00554     }
00555     FavListSet(idx, NULL, "bcast1.qld.abbn.tv:80/decades192.ogg");
00556 }
00557 
00558 /*
00559 
00560 */

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