⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 station.c

📁 qt91上实现的mp3播放机,支持sam9260,参考用
💻 C
字号:
/*!
 * Copyright (C) 2006-2007 by egnite Software 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 EGNITE SOFTWARE GMBH 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 EGNITE
 * SOFTWARE GMBH 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 station.c
 * \brief Station.
 *
 * \verbatim
 *
 * $Log$
 *
 * \endverbatim
 */

#include <cfg/os.h>
#include <cfg/clock.h>
#include <dev/board.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>

#include <hxmp3/mp3dec.h>

#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <net/route.h>
#include <pro/dhcp.h>

#include "tlv320dac.h"
#include "config.h"
#include "utils.h"
#include "player.h"
#include "shoutcast.h"

/*!
 * \brief Select new radio station.
 *
 * \param idx Start index.
 * \param dir Direction to search, 1, -1 or 0. In the latter case a
 *            station with the same symbol is selected.
 *
 * \return Index of the new station.
 */
u_int StationSelect(u_int idx, int dir)
{
    u_int rc = idx;
    char *symbol;

    if (station[rc].rs_port) {
        symbol = station[idx].rs_symbol;
    }
    else {
        symbol = NULL;
    }

    for (;;) {
        if (dir < 0) {
            if (rc == 0) {
                rc = MAXNUM_STATIONS;
            }
            rc--;
        }
        else if (++rc >= MAXNUM_STATIONS) {
            rc = 0;
        }
        if (rc == idx) {
            break;
        }
        if (station[rc].rs_port) {
            if (symbol == NULL) {
                break;
            }
            if (dir) {
                if (station[rc].rs_symbol == NULL || strcmp(station[rc].rs_symbol, symbol)) {
                    break;
                }
            }
            else {
                if (station[rc].rs_symbol && strcmp(station[rc].rs_symbol, symbol) == 0) {
                    break;
                }
            }
        }
    }
    return rc;
}

/*!
 * \brief Disconnect from a radio station.
 *
 * \param sip Pointer to a station information structure.
 */
void StationDisconnect(STATIONINFO *sip)
{
    printf("Disconnecting\n");
    if (sip) {
        if (sip->si_sock) {
            NutTcpCloseSocket(sip->si_sock);
        }
        TcpReleaseHeaderLines(sip->si_header);
        free(sip);
    }
}

/*!
 * \brief Connect to a radio station.
 *
 * \param scp Pointer to the station configuration table entry.
 *
 * \return Pointer to a new station information structure. In
 *         case of an error, a NULL pointer is returned.
 */
STATIONINFO *StationConnect(STATIONCONF * scp)
{
    STATIONINFO *sip = NULL;
    TCPSOCKET *sock = NULL;
    u_short mss = MAX_TCPSEG_SIZE;
    u_short tcpbufsiz = MAX_TCPBUF_SIZE;
    u_long rx_to = MAX_TCPRCV_WAIT;
    int err = -1;
    int cr;
    char *line;

    /* Create a TCP socket. */
    if ((sock = NutTcpCreateSocket()) != NULL) {
        /* Set socket options. Failures are ignored. */
        NutTcpSetSockOpt(sock, TCP_MAXSEG, &mss, sizeof(mss));
        NutTcpSetSockOpt(sock, SO_RCVBUF, &tcpbufsiz, sizeof(tcpbufsiz));
        NutTcpSetSockOpt(sock, SO_RCVTIMEO, &rx_to, sizeof(rx_to));

        /* Connect the stream server. */
        printf("[CNCT %s:%u", inet_ntoa(scp->rs_ip), scp->rs_port);
        if (proxy.proxy_ip) {
            printf(" via %s:%u]", inet_ntoa(proxy.proxy_ip), proxy.proxy_port);
            cr = NutTcpConnect(sock, proxy.proxy_ip, proxy.proxy_port);
        }
        else {
            putchar(']');
            cr = NutTcpConnect(sock, scp->rs_ip, scp->rs_port);
        }
        if (cr) {
            printf("[CERR=%d]\n", NutTcpError(sock));
        }
        else {
            printf("[CNCTD]");

            /* Create a new station info structure. */
            if ((sip = malloc(sizeof(STATIONINFO))) != NULL) {
                memset(sip, 0, sizeof(STATIONINFO));
                sip->si_sock = sock;
                sip->si_scp = scp;

                /*
                 * Send the HTTP request.
                 */
                line = malloc(256);

                if (proxy.proxy_ip) {
                    /* A proxy requires an absolute URI. */
                    sprintf(line, "GET http://%s:%u/", inet_ntoa(scp->rs_ip), scp->rs_port);
                }
                else {
                    strcpy(line, "GET /");
                }
                if (scp->rs_url_path) {
                    /* URL contains a specific path. */
                    strcat(line, scp->rs_url_path);
                }
                strcat(line, " HTTP/1.0\r\n");
                err = TcpPutString(sock, line);

                if (err == 0) {
                    /* HTTP 1.1 requires this. So just in case... */
                    sprintf(line, "Host: %s\r\n", inet_ntoa(scp->rs_ip));
                    err = TcpPutString(sock, line);
                }

                if (err == 0) {
                    /* Some shoutcast servers seem to require a user-agent line. 
                       "Eat chalk" to get in. */
                    TcpPutString(sock, "User-Agent: WinampMPEG/2.7\r\n" /* */
                        "Accept: */*\r\n"  /* */
                        "Icy-MetaData: 1\r\n"  /* */
                        "Connection: close\r\n\r\n");
                }

                free(line);

                /* Read the servers response. */
                if (TcpGetHeaderLines(sock, &sip->si_header) <= 0) {
                    err = -1;
                }
            }
        }

        /* Release resources in case of any error. */
        if (err) {
            if (sip) {
                StationDisconnect(sip);
                sip = NULL;
            }
            else {
                NutTcpCloseSocket(sock);
            }
        }
    }
    return sip;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -