Stream Ripper This program will download MP3 data from SHOUTcast stream and save mp3 file in microSD card. Metadata also will be saved as another file when stream includes metadata.

Dependencies:   EthernetNetIf mbed SDFileSystem

Committer:
xshige
Date:
Mon Sep 20 06:47:33 2010 +0000
Revision:
0:5490b791ee3d

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
xshige 0:5490b791ee3d 1
xshige 0:5490b791ee3d 2 /*
xshige 0:5490b791ee3d 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
xshige 0:5490b791ee3d 4
xshige 0:5490b791ee3d 5 Permission is hereby granted, free of charge, to any person obtaining a copy
xshige 0:5490b791ee3d 6 of this software and associated documentation files (the "Software"), to deal
xshige 0:5490b791ee3d 7 in the Software without restriction, including without limitation the rights
xshige 0:5490b791ee3d 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
xshige 0:5490b791ee3d 9 copies of the Software, and to permit persons to whom the Software is
xshige 0:5490b791ee3d 10 furnished to do so, subject to the following conditions:
xshige 0:5490b791ee3d 11
xshige 0:5490b791ee3d 12 The above copyright notice and this permission notice shall be included in
xshige 0:5490b791ee3d 13 all copies or substantial portions of the Software.
xshige 0:5490b791ee3d 14
xshige 0:5490b791ee3d 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
xshige 0:5490b791ee3d 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
xshige 0:5490b791ee3d 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
xshige 0:5490b791ee3d 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
xshige 0:5490b791ee3d 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
xshige 0:5490b791ee3d 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
xshige 0:5490b791ee3d 21 THE SOFTWARE.
xshige 0:5490b791ee3d 22 */
xshige 0:5490b791ee3d 23
xshige 0:5490b791ee3d 24 /** \file
xshige 0:5490b791ee3d 25 NTP Client header file
xshige 0:5490b791ee3d 26 */
xshige 0:5490b791ee3d 27
xshige 0:5490b791ee3d 28 #ifndef NTP_CLIENT_H
xshige 0:5490b791ee3d 29 #define NTP_CLIENT_H
xshige 0:5490b791ee3d 30
xshige 0:5490b791ee3d 31 #include "core/net.h"
xshige 0:5490b791ee3d 32 #include "core/netservice.h"
xshige 0:5490b791ee3d 33 #include "api/UDPSocket.h"
xshige 0:5490b791ee3d 34 #include "api/DNSRequest.h"
xshige 0:5490b791ee3d 35 #include "mbed.h"
xshige 0:5490b791ee3d 36
xshige 0:5490b791ee3d 37 ///NTP Client results
xshige 0:5490b791ee3d 38 enum NTPResult
xshige 0:5490b791ee3d 39 {
xshige 0:5490b791ee3d 40 NTP_OK, ///<Success
xshige 0:5490b791ee3d 41 NTP_PROCESSING, ///<Processing
xshige 0:5490b791ee3d 42 NTP_PRTCL, ///<Protocol error
xshige 0:5490b791ee3d 43 NTP_TIMEOUT, ///<Connection timeout
xshige 0:5490b791ee3d 44 NTP_DNS ///<Could not resolve DNS hostname
xshige 0:5490b791ee3d 45 };
xshige 0:5490b791ee3d 46
xshige 0:5490b791ee3d 47 ///A NTP Client
xshige 0:5490b791ee3d 48 /**
xshige 0:5490b791ee3d 49 The NTP client is a simple UDP client that will update the mbed's RTC
xshige 0:5490b791ee3d 50 */
xshige 0:5490b791ee3d 51 class NTPClient : protected NetService
xshige 0:5490b791ee3d 52 {
xshige 0:5490b791ee3d 53 public:
xshige 0:5490b791ee3d 54 /**
xshige 0:5490b791ee3d 55 Instantiates the NTP client
xshige 0:5490b791ee3d 56 */
xshige 0:5490b791ee3d 57 NTPClient();
xshige 0:5490b791ee3d 58 virtual ~NTPClient();
xshige 0:5490b791ee3d 59
xshige 0:5490b791ee3d 60 //High level setup functions
xshige 0:5490b791ee3d 61
xshige 0:5490b791ee3d 62 ///Gets current time (blocking)
xshige 0:5490b791ee3d 63 /**
xshige 0:5490b791ee3d 64 Updates the time using the server host
xshige 0:5490b791ee3d 65 Blocks until completion
xshige 0:5490b791ee3d 66 @param host : NTP server
xshige 0:5490b791ee3d 67 */
xshige 0:5490b791ee3d 68 NTPResult setTime(const Host& host); //Blocking
xshige 0:5490b791ee3d 69
xshige 0:5490b791ee3d 70 ///Gets current time (non-blocking)
xshige 0:5490b791ee3d 71 /**
xshige 0:5490b791ee3d 72 Updates the time using the server host
xshige 0:5490b791ee3d 73 The function returns immediately and calls the callback on completion or error
xshige 0:5490b791ee3d 74 @param host : NTP server
xshige 0:5490b791ee3d 75 @param pMethod : callback function
xshige 0:5490b791ee3d 76 */
xshige 0:5490b791ee3d 77 NTPResult setTime(const Host& host, void (*pMethod)(NTPResult)); //Non blocking
xshige 0:5490b791ee3d 78
xshige 0:5490b791ee3d 79 ///Gets current time (non-blocking)
xshige 0:5490b791ee3d 80 /**
xshige 0:5490b791ee3d 81 Updates the time
xshige 0:5490b791ee3d 82 @param host : NTP server
xshige 0:5490b791ee3d 83 @param pItem : instance of class on which to execute the callback method
xshige 0:5490b791ee3d 84 @param pMethod : callback method
xshige 0:5490b791ee3d 85 The function returns immediately and calls the callback on completion or error
xshige 0:5490b791ee3d 86 */
xshige 0:5490b791ee3d 87 template<class T>
xshige 0:5490b791ee3d 88 NTPResult setTime(const Host& host, T* pItem, void (T::*pMethod)(NTPResult)) //Non blocking
xshige 0:5490b791ee3d 89 {
xshige 0:5490b791ee3d 90 setOnResult(pItem, pMethod);
xshige 0:5490b791ee3d 91 doSetTime(host);
xshige 0:5490b791ee3d 92 return NTP_PROCESSING;
xshige 0:5490b791ee3d 93 }
xshige 0:5490b791ee3d 94
xshige 0:5490b791ee3d 95 ///Gets current time (non-blocking)
xshige 0:5490b791ee3d 96 /**
xshige 0:5490b791ee3d 97 Updates the time using the server host
xshige 0:5490b791ee3d 98 The function returns immediately and calls the previously set callback on completion or error
xshige 0:5490b791ee3d 99 @param host : NTP server
xshige 0:5490b791ee3d 100 */
xshige 0:5490b791ee3d 101 void doSetTime(const Host& host);
xshige 0:5490b791ee3d 102
xshige 0:5490b791ee3d 103 ///Setups the result callback
xshige 0:5490b791ee3d 104 /**
xshige 0:5490b791ee3d 105 @param pMethod : callback function
xshige 0:5490b791ee3d 106 */
xshige 0:5490b791ee3d 107 void setOnResult( void (*pMethod)(NTPResult) );
xshige 0:5490b791ee3d 108
xshige 0:5490b791ee3d 109 ///Setups the result callback
xshige 0:5490b791ee3d 110 /**
xshige 0:5490b791ee3d 111 @param pItem : instance of class on which to execute the callback method
xshige 0:5490b791ee3d 112 @param pMethod : callback method
xshige 0:5490b791ee3d 113 */
xshige 0:5490b791ee3d 114 class CDummy;
xshige 0:5490b791ee3d 115 template<class T>
xshige 0:5490b791ee3d 116 void setOnResult( T* pItem, void (T::*pMethod)(NTPResult) )
xshige 0:5490b791ee3d 117 {
xshige 0:5490b791ee3d 118 m_pCbItem = (CDummy*) pItem;
xshige 0:5490b791ee3d 119 m_pCbMeth = (void (CDummy::*)(NTPResult)) pMethod;
xshige 0:5490b791ee3d 120 }
xshige 0:5490b791ee3d 121
xshige 0:5490b791ee3d 122 void close();
xshige 0:5490b791ee3d 123
xshige 0:5490b791ee3d 124 protected:
xshige 0:5490b791ee3d 125 virtual void poll(); //Called by NetServices
xshige 0:5490b791ee3d 126
xshige 0:5490b791ee3d 127 private:
xshige 0:5490b791ee3d 128 void init();
xshige 0:5490b791ee3d 129 void open();
xshige 0:5490b791ee3d 130
xshige 0:5490b791ee3d 131 __packed struct NTPPacket //See RFC 4330 for Simple NTP
xshige 0:5490b791ee3d 132 {
xshige 0:5490b791ee3d 133 //WARN: We are in LE! Network is BE!
xshige 0:5490b791ee3d 134 //LSb first
xshige 0:5490b791ee3d 135 unsigned mode : 3;
xshige 0:5490b791ee3d 136 unsigned vn : 3;
xshige 0:5490b791ee3d 137 unsigned li : 2;
xshige 0:5490b791ee3d 138
xshige 0:5490b791ee3d 139 uint8_t stratum;
xshige 0:5490b791ee3d 140 uint8_t poll;
xshige 0:5490b791ee3d 141 uint8_t precision;
xshige 0:5490b791ee3d 142 //32 bits header
xshige 0:5490b791ee3d 143
xshige 0:5490b791ee3d 144 uint32_t rootDelay;
xshige 0:5490b791ee3d 145 uint32_t rootDispersion;
xshige 0:5490b791ee3d 146 uint32_t refId;
xshige 0:5490b791ee3d 147
xshige 0:5490b791ee3d 148 uint32_t refTm_s;
xshige 0:5490b791ee3d 149 uint32_t refTm_f;
xshige 0:5490b791ee3d 150 uint32_t origTm_s;
xshige 0:5490b791ee3d 151 uint32_t origTm_f;
xshige 0:5490b791ee3d 152 uint32_t rxTm_s;
xshige 0:5490b791ee3d 153 uint32_t rxTm_f;
xshige 0:5490b791ee3d 154 uint32_t txTm_s;
xshige 0:5490b791ee3d 155 uint32_t txTm_f;
xshige 0:5490b791ee3d 156 };
xshige 0:5490b791ee3d 157
xshige 0:5490b791ee3d 158 void process(); //Main state-machine
xshige 0:5490b791ee3d 159
xshige 0:5490b791ee3d 160 void setTimeout(int ms);
xshige 0:5490b791ee3d 161 void resetTimeout();
xshige 0:5490b791ee3d 162
xshige 0:5490b791ee3d 163 void onTimeout(); //Connection has timed out
xshige 0:5490b791ee3d 164 void onDNSReply(DNSReply r);
xshige 0:5490b791ee3d 165 void onUDPSocketEvent(UDPSocketEvent e);
xshige 0:5490b791ee3d 166 void onResult(NTPResult r); //Called when exchange completed or on failure
xshige 0:5490b791ee3d 167
xshige 0:5490b791ee3d 168 NTPResult blockingProcess(); //Called in blocking mode, calls Net::poll() until return code is available
xshige 0:5490b791ee3d 169
xshige 0:5490b791ee3d 170 UDPSocket* m_pUDPSocket;
xshige 0:5490b791ee3d 171
xshige 0:5490b791ee3d 172 enum NTPStep
xshige 0:5490b791ee3d 173 {
xshige 0:5490b791ee3d 174 NTP_PING,
xshige 0:5490b791ee3d 175 NTP_PONG
xshige 0:5490b791ee3d 176 };
xshige 0:5490b791ee3d 177
xshige 0:5490b791ee3d 178 NTPStep m_state;
xshige 0:5490b791ee3d 179
xshige 0:5490b791ee3d 180 NTPPacket m_pkt;
xshige 0:5490b791ee3d 181
xshige 0:5490b791ee3d 182 CDummy* m_pCbItem;
xshige 0:5490b791ee3d 183 void (CDummy::*m_pCbMeth)(NTPResult);
xshige 0:5490b791ee3d 184
xshige 0:5490b791ee3d 185 void (*m_pCb)(NTPResult);
xshige 0:5490b791ee3d 186
xshige 0:5490b791ee3d 187 Timer m_watchdog;
xshige 0:5490b791ee3d 188 int m_timeout;
xshige 0:5490b791ee3d 189
xshige 0:5490b791ee3d 190 bool m_closed;
xshige 0:5490b791ee3d 191
xshige 0:5490b791ee3d 192 Host m_host;
xshige 0:5490b791ee3d 193
xshige 0:5490b791ee3d 194 DNSRequest* m_pDnsReq;
xshige 0:5490b791ee3d 195
xshige 0:5490b791ee3d 196 NTPResult m_blockingResult; //Result if blocking mode
xshige 0:5490b791ee3d 197
xshige 0:5490b791ee3d 198 };
xshige 0:5490b791ee3d 199
xshige 0:5490b791ee3d 200 #endif