📄 favlist.c
字号:
/*!
* Copyright (C) 2006-2007 by egnite Software GmbH. All rights reserved.
* Copyright (C) 2008 by egnite GmbH. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the copyright holders nor the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
* For additional information see http://www.ethernut.de/
*/
/*!
* \file config.c
* \brief Radio configuration.
*
* \verbatim
*
* $Log$
*
* \endverbatim
*/
#include <cfg/os.h>
#include <cfg/clock.h>
#include <dev/board.h>
#include <dev/at45db.h>
#include <dev/at91_spi.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <fcntl.h>
#include <io.h>
#include <sys/version.h>
#include <sys/confnet.h>
#include <sys/atom.h>
#include <sys/heap.h>
#include <sys/thread.h>
#include <sys/timer.h>
#include <sys/event.h>
#include <sys/socket.h>
#if defined(USE_SOFTWARE_CODEC)
#include <hxmp3/mp3dec.h>
#endif
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <pro/dhcp.h>
#include "config.h"
#include "logmsg.h"
#include "favlist.h"
#include "webradio.h"
#define FAVLIST_MAGIC "FL1"
#define FAVLIST_SECTOR 16
RADIOSTATION favlist[MAX_FAVORITES];
/*!
* \brief Clear favorites list.
*
* Will be called by FavListLoad() before loading a new list.
*/
static void FavListClear(void)
{
int idx;
int i;
/* Release all memory occupied by the current configuration. */
for (idx = 0; idx < MAX_FAVORITES; idx++) {
if (favlist[idx].rs_name) {
free(favlist[idx].rs_name);
}
for (i = 0; i < favlist[idx].rs_streams; i++) {
if (favlist[idx].rs_uri[i]) {
free(favlist[idx].rs_uri[i]);
}
}
}
memset(favlist, 0, MAX_FAVORITES * sizeof(RADIOSTATION));
}
/*!
* \brief Set favorite list entry.
*
* \param idx Index of the entry. Use MAX_FAVORITES to add a new entry.
* \param name Name of the station. Must be provided, if a new station
* is added. May be NULL when adding a URI. When used with
* an existing entry, the current name will be replaced.
* \param uri URI of the stream, that will be added. If NULL, then all
* existing entries are cleared. If both, the name and the
* URI are NULL, then the list entry is cleared.
*
* \return Entry index on success, -1 otherwise.
*/
int FavListSet(int idx, CONST char *name, CONST char * uri)
{
int i;
LogMsg(LOG_CONFIG, "Set %d,%s,%s\n", idx, name, uri);
if (idx >= MAX_FAVORITES) {
/* New entry. Find a free slot. */
idx = LAST_FAVORITE + 1;
while (idx < MAX_FAVORITES && favlist[idx].rs_name) {
idx++;
}
}
if (idx >= MAX_FAVORITES) {
/* We are full. */
return -1;
}
if (name || (name == NULL && uri == NULL)) {
if (favlist[idx].rs_name) {
free(favlist[idx].rs_name);
}
favlist[idx].rs_name = NULL;
}
if (name) {
favlist[idx].rs_name = strdup(name);
}
if (uri) {
if (favlist[idx].rs_streams < MAXNUM_STREAMS) {
favlist[idx].rs_uri[favlist[idx].rs_streams] = strdup(uri);
favlist[idx].rs_streams++;
}
else {
idx = -1;
}
}
else {
for (i = 0; i < favlist[idx].rs_streams; i++) {
if (favlist[idx].rs_uri[i]) {
free(favlist[idx].rs_uri[i]);
}
favlist[idx].rs_uri[i] = NULL;
}
favlist[idx].rs_streams = 0;
}
return idx;
}
/*!
* \brief Copy a favorites list entry.
*
* \param src Index of the source entry.
* \param dst Index of the destination entry.
*
* \return 0 on success, -1 otherwise.
*/
int FavListCopy(int src, int dst)
{
int i;
/*
* Release an existing entry at the destination index.
*/
LogMsg(LOG_CONFIG, "Copying %d to %d\n", src, dst);
if (favlist[dst].rs_name) {
free(favlist[dst].rs_name);
}
for (i = 0; i < favlist[dst].rs_streams; i++) {
if (favlist[dst].rs_uri[i]) {
free(favlist[dst].rs_uri[i]);
}
}
memset(&favlist[dst], 0, sizeof(RADIOSTATION));
/*
* Copy the source entry to the destination index.
*/
if (favlist[src].rs_name) {
favlist[dst].rs_name = strdup(favlist[src].rs_name);
for (i = 0; i < favlist[src].rs_streams; i++) {
if (favlist[src].rs_uri[i]) {
favlist[dst].rs_uri[favlist[dst].rs_streams++] = strdup(favlist[src].rs_uri[i]);
}
}
return 0;
}
return -1;
}
/*!
* \brief Search favorite radio station.
*
* \param idx Start index.
* \param dir Direction to search, 1 (forward) or -1 (backwards).
*
* \return Index of the new station or -1 if none available.
*/
int FavListSearch(int idx, int dir)
{
int rc = idx;
LogMsg(LOG_CONFIG, "Searching %d %d\n", idx, dir);
for (;;) {
rc += dir;
if (rc <= LAST_FAVORITE) {
rc = MAX_FAVORITES - 1;
}
else if (rc >= MAX_FAVORITES) {
rc = LAST_FAVORITE + 1;
}
if ((favlist[rc].rs_name && favlist[rc].rs_streams)) {
/* New entry found. */
break;
}
if (rc == idx) {
/* Whole list searched, but no entry found. */
return -1;
}
}
return rc;
}
/*!
* \brief Calculate total size of the favorites list.
*
* \return Number of bytes used.
*/
size_t FavListSize(void)
{
size_t rc = 0;
int idx;
int i;
RADIOSTATION *flp;
for (idx = 1; idx < MAX_FAVORITES; idx++) {
flp = &favlist[idx];
if (flp->rs_name) {
rc += strlen(flp->rs_name) + 1;
for (i = 0; i < flp->rs_streams; i++) {
if (flp->rs_uri[i]) {
rc += strlen(flp->rs_uri[i]) + 1;
}
}
}
}
return rc + 1;
}
/*!
* \brief Save favorites list in non-volatile memory.
*
* \return 0 on success, -1 otherwise.
*/
int FavListSave(void)
{
int idx;
int i;
RADIOSTATION *rsp;
/* Rewind and save the magic name. */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -