fix http request; fix netbuf worker stat

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@442 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong 2010-02-24 14:56:28 +00:00
parent 3c63455ffe
commit b54fbbced9
6 changed files with 170 additions and 132 deletions

View File

@ -29,10 +29,6 @@
#ifdef RT_USING_DFS
/* dfs init */
#include <dfs_init.h>
/* dfs filesystem:FAT filesystem init */
#include <dfs_fat.h>
/* dfs filesystem:EFS filesystem init */
#include <dfs_efs.h>
/* dfs filesystem:ELM FatFs filesystem init */
#include <dfs_elm.h>
/* dfs Filesystem APIs */
@ -57,16 +53,7 @@ void rt_init_thread_entry(void *parameter)
{
/* init the device filesystem */
dfs_init();
#ifdef RT_USING_DFS_EFSL
/* init the efsl filesystam*/
efsl_init();
/* mount sd card fat partition 1 as root directory */
if (dfs_mount("sd0", "/", "efs", 0, 0) == 0)
rt_kprintf("File System initialized!\n");
else
rt_kprintf("File System init failed!\n");
#elif defined(RT_USING_DFS_ELMFAT)
/* init the elm FAT filesystam*/
elm_init();
@ -81,7 +68,6 @@ void rt_init_thread_entry(void *parameter)
rt_kprintf("SPI File System initialized!\n");
else
rt_kprintf("SPI File System init failed!\n");
#endif
}
#endif

View File

@ -121,18 +121,16 @@ int http_read_line( int socket, char * buffer, int size )
return count;
}
//
// Before we can connect we need to parse the server address and optional
// port from the url provided. the format of "url" as passed to this function
// is "//192.168.0.1:8080/blah.elf" where "192.168.0.1" can be either an IP
// or a domain name and ":8080" is the optional port to connect to, default
// port is 80.
//
// This function will return a filename string for use in GET
// requests, and fill the structure pointed to by *server with the
// correct values.
//
const char *http_resolve_address( struct sockaddr_in *server, const char * url, char *host_addr)
/*
* resolve server address
* @param server the server sockaddress
* @param url the input URL address, for example, http://www.rt-thread.org/index.html
* @param host_addr the buffer pointer to save server host address
* @param request the pointer to point the request url, for example, /index.html
*
* @return 0 on resolve server address OK, others failed
*/
int http_resolve_address(struct sockaddr_in *server, const char * url, char *host_addr, char** request)
{
char *ptr;
char port[6] = "80"; /* default port of 80(HTTP) */
@ -147,26 +145,31 @@ const char *http_resolve_address( struct sockaddr_in *server, const char * url,
}
/* URL must start with double forward slashes. */
if((url[0] != '/') || (url[1] != '/' )) return(NULL);
if((url[0] != '/') || (url[1] != '/' )) return -1;
url += 2; is_domain = 0;
for(i = 0; ((url[i] != '\0') && (url[i] != '/')) && (i < 30); i++)
i = 0;
/* allow specification of port in URL like http://www.server.net:8080/ */
while (*url)
{
if((((host_addr[i] = url[i]) < '0') || (url[i] > '9')) && (url[i] != '.'))
if (*url == '/') break;
if (*url == ':')
{
if(url[i] == ':')
{
unsigned char w;
/* allow specification of port in URL like http://www.server.net:8080/ */
for(w = 0; ((w + i + 1) < 127) && (w < 5) && (url[w + i + 1] != '/') && (url[w + i + 1] != '\0'); w++)
port[w] = url[w + i + 1];
port[w] = '\0';
break;
}
else is_domain = 1;
unsigned char w;
for (w = 0; w < 5 && url[w] != '/' && url[w] != '\0'; w ++)
port[w] = url[w + 1];
/* get port ok */
port[w] = '\0';
url += w + 1;
break;
}
host_addr[i++] = *url;
url ++;
}
*request = (char*)url;
/* get host addr ok. */
host_addr[i] = '\0';
@ -177,7 +180,7 @@ const char *http_resolve_address( struct sockaddr_in *server, const char * url,
if(hptr == 0)
{
rt_kprintf("HTTP: failed to resolve domain '%s'\n", host_addr);
return RT_NULL;
return -1;
}
memcpy(&server->sin_addr, *hptr->h_addr_list, sizeof(server->sin_addr));
}
@ -189,8 +192,7 @@ const char *http_resolve_address( struct sockaddr_in *server, const char * url,
server->sin_port = htons((int) strtol(port, NULL, 10));
server->sin_family = AF_INET;
while (*url != '/') url ++;
return url;
return 0;
}
//
@ -199,7 +201,7 @@ const char *http_resolve_address( struct sockaddr_in *server, const char * url,
// to leave the stream at the start of the real data.
//
static int http_connect(struct http_session* session,
struct sockaddr_in * server, char *host_addr, const char * url)
struct sockaddr_in * server, char *host_addr, const char *url)
{
int socket_handle;
int peer_handle;
@ -224,8 +226,11 @@ static int http_connect(struct http_session* session,
rt_uint32_t length;
buf = rt_malloc (512);
length = rt_snprintf(buf, 512, _http_get, url, host_addr, server->sin_port);
if (*url)
length = rt_snprintf(buf, 512, _http_get, url, host_addr, server->sin_port);
else
length = rt_snprintf(buf, 512, _http_get, "/", host_addr, server->sin_port);
rc = send(peer_handle, buf, length, 0);
rt_kprintf("HTTP request:\n%s", buf);
@ -278,8 +283,7 @@ struct http_session* http_session_open(char* url)
{
int peer_handle = 0;
struct sockaddr_in server;
const char *get_name;
char host_addr[32];
char *request, host_addr[32];
struct http_session* session;
session = (struct http_session*) rt_malloc(sizeof(struct http_session));
@ -289,8 +293,7 @@ struct http_session* http_session_open(char* url)
session->position = 0;
/* Check valid IP address and URL */
get_name = http_resolve_address(&server, url, &host_addr[0]);
if(get_name == NULL)
if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
{
rt_free(session);
return RT_NULL;
@ -298,7 +301,7 @@ struct http_session* http_session_open(char* url)
// Now we connect and initiate the transfer by sending a
// request header to the server, and receiving the response header
if((peer_handle = http_connect(session, &server, host_addr, get_name)) < 0)
if((peer_handle = http_connect(session, &server, host_addr, request)) < 0)
{
rt_kprintf("HTTP: failed to connect to '%s'!\n", host_addr);
rt_free(session);
@ -366,7 +369,7 @@ int http_session_close(struct http_session* session)
// to leave the stream at the start of the real data.
//
static int shoutcast_connect(struct shoutcast_session* session,
struct sockaddr_in* server, char *host_addr, const char * url)
struct sockaddr_in* server, char* host_addr, const char* url)
{
int socket_handle;
int peer_handle;
@ -391,7 +394,10 @@ static int shoutcast_connect(struct shoutcast_session* session,
rt_uint32_t length;
buf = rt_malloc (512);
length = rt_snprintf(buf, 512, _shoutcast_get, url, host_addr, server->sin_port);
if (*url)
length = rt_snprintf(buf, 512, _shoutcast_get, url, host_addr, server->sin_port);
else
length = rt_snprintf(buf, 512, _shoutcast_get, "/", host_addr, server->sin_port);
rc = send(peer_handle, buf, length, 0);
rt_kprintf("SHOUTCAST request:\n%s", buf);
@ -487,8 +493,7 @@ struct shoutcast_session* shoutcast_session_open(char* url)
{
int peer_handle = 0;
struct sockaddr_in server;
const char *get_name;
char host_addr[32];
char *request, host_addr[32];
struct shoutcast_session* session;
session = (struct shoutcast_session*) rt_malloc(sizeof(struct shoutcast_session));
@ -500,8 +505,7 @@ struct shoutcast_session* shoutcast_session_open(char* url)
session->station_name = RT_NULL;
/* Check valid IP address and URL */
get_name = http_resolve_address(&server, url, &host_addr[0]);
if(get_name == NULL)
if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
{
rt_free(session);
return RT_NULL;
@ -509,7 +513,7 @@ struct shoutcast_session* shoutcast_session_open(char* url)
// Now we connect and initiate the transfer by sending a
// request header to the server, and receiving the response header
if((peer_handle = shoutcast_connect(session, &server, host_addr, get_name)) < 0)
if((peer_handle = shoutcast_connect(session, &server, host_addr, request)) < 0)
{
rt_kprintf("SHOUTCAST: failed to connect to '%s'!\n", host_addr);
if (session->station_name != RT_NULL)

View File

@ -8,7 +8,7 @@
#include "player_ui.h"
#include "player_bg.h"
#define MP3_AUDIO_BUF_SZ 8192
#define MP3_AUDIO_BUF_SZ (5 * 1024)
#ifndef MIN
#define MIN(x, y) ((x) < (y)? (x) : (y))
#endif
@ -518,22 +518,28 @@ void http_mp3(char* url)
session = http_session_open(url);
if (session != RT_NULL)
{
/* add a job to netbuf worker */
net_buf_add_job(http_fetch, http_close, (void*)session);
decoder = mp3_decoder_create();
if (decoder != RT_NULL)
/* start a job to netbuf worker */
if (net_buf_start_job(http_fetch, http_close, (void*)session) == 0)
{
decoder->fetch_data = http_data_fetch;
decoder->fetch_parameter = RT_NULL;
decoder = mp3_decoder_create();
if (decoder != RT_NULL)
{
decoder->fetch_data = http_data_fetch;
decoder->fetch_parameter = RT_NULL;
current_offset = 0;
while (mp3_decoder_run(decoder) != -1);
current_offset = 0;
while (mp3_decoder_run(decoder) != -1);
/* delete decoder object */
mp3_decoder_delete(decoder);
/* delete decoder object */
mp3_decoder_delete(decoder);
}
session = RT_NULL;
}
else
{
/* start job failed, close session */
http_session_close(session);
}
session = RT_NULL;
}
}
FINSH_FUNCTION_EXPORT(http_mp3, http mp3 decode test);
@ -571,22 +577,28 @@ void ice_mp3(char* url)
session = shoutcast_session_open(url);
if (session != RT_NULL)
{
/* add a job to netbuf worker */
net_buf_add_job(ice_fetch, ice_close, (void*)session);
decoder = mp3_decoder_create();
if (decoder != RT_NULL)
/* start a job to netbuf worker */
if (net_buf_start_job(ice_fetch, ice_close, (void*)session) == 0)
{
decoder->fetch_data = ice_data_fetch;
decoder->fetch_parameter = RT_NULL;
decoder = mp3_decoder_create();
if (decoder != RT_NULL)
{
decoder->fetch_data = ice_data_fetch;
decoder->fetch_parameter = RT_NULL;
current_offset = 0;
while (mp3_decoder_run(decoder) != -1);
current_offset = 0;
while (mp3_decoder_run(decoder) != -1);
/* delete decoder object */
mp3_decoder_delete(decoder);
/* delete decoder object */
mp3_decoder_delete(decoder);
}
session = RT_NULL;
}
else
{
/* start a job failed, close session */
shoutcast_session_close(session);
}
session = RT_NULL;
}
}
FINSH_FUNCTION_EXPORT(ice_mp3, shoutcast mp3 decode test);

View File

@ -3,6 +3,7 @@
#include "netbuffer.h"
#include "player_ui.h"
#include "player_bg.h"
#define MP3_DECODE_MP_CNT 2
#define MP3_DECODE_MP_SZ 2560
@ -39,11 +40,10 @@ void sbuf_release(void* ptr)
#if STM32_EXT_SRAM
/* netbuf worker stat */
#define NETBUF_STAT_FREE 0
#define NETBUF_STAT_STOPPED 0
#define NETBUF_STAT_BUFFERING 1
#define NETBUF_STAT_BUSY 2
#define NETBUF_STAT_SUSPEND 2
#define NETBUF_STAT_STOPPING 3
#define NETBUF_STAT_STOPPED 4
/* net buffer module */
struct net_buffer
@ -58,7 +58,7 @@ struct net_buffer
/* buffer ready water mater */
rt_uint32_t ready_wm, resume_wm;
rt_bool_t is_wait_ready, is_wait_resume;
rt_bool_t is_wait_ready;
rt_sem_t wait_ready, wait_resume;
/* netbuf worker stat */
@ -75,6 +75,7 @@ struct net_buffer_job
static struct net_buffer _netbuf;
static rt_mq_t _netbuf_mq = RT_NULL;
/* netbuf worker public API */
rt_size_t net_buf_read(rt_uint8_t* buffer, rt_size_t length)
{
rt_size_t data_length, read_index;
@ -83,32 +84,23 @@ rt_size_t net_buf_read(rt_uint8_t* buffer, rt_size_t length)
data_length = _netbuf.data_length;
if ((data_length == 0) &&
(_netbuf.stat != NETBUF_STAT_STOPPED && _netbuf.stat != NETBUF_STAT_STOPPING))
(_netbuf.stat == NETBUF_STAT_BUFFERING || _netbuf.stat == NETBUF_STAT_SUSPEND))
{
/* set stat */
_netbuf.stat = NETBUF_STAT_BUFFERING;
rt_kprintf("stat -> buffering\n");
rt_err_t result;
/* buffer is not ready. */
_netbuf.is_wait_ready = RT_TRUE;
rt_kprintf("wait ready, data len: %d, stat %d\n", data_length, _netbuf.stat);
/* set buffer status to buffering */
player_set_buffer_status(RT_TRUE);
rt_sem_take(_netbuf.wait_ready, RT_WAITING_FOREVER);
}
result = rt_sem_take(_netbuf.wait_ready, RT_WAITING_FOREVER);
if ((data_length <= _netbuf.ready_wm) &&
(_netbuf.stat == NETBUF_STAT_BUFFERING))
{
/* buffer is not ready. */
_netbuf.is_wait_ready = RT_TRUE;
rt_kprintf("wait ready, data len: %d, stat %d\n", data_length, _netbuf.stat);
rt_sem_take(_netbuf.wait_ready, RT_WAITING_FOREVER);
/* take semaphore failed, netbuf worker is stopped */
if (result != RT_EOK) return 0;
}
/* get read and save index */
read_index = _netbuf.read_index;
/* re-get data legnth */
data_length = _netbuf.data_length;
@ -139,12 +131,13 @@ rt_size_t net_buf_read(rt_uint8_t* buffer, rt_size_t length)
_netbuf.data_length -= length;
data_length = _netbuf.data_length;
if ((_netbuf.is_wait_resume == RT_TRUE) && data_length < _netbuf.resume_wm)
if ((_netbuf.stat == NETBUF_STAT_SUSPEND) && data_length < _netbuf.resume_wm)
{
_netbuf.is_wait_resume = RT_FALSE;
_netbuf.stat = NETBUF_STAT_BUFFERING;
rt_hw_interrupt_enable(level);
rt_kprintf("resume netbuf worker\n");
/* resume netbuf worker */
rt_kprintf("stat[suspend] -> buffering\n");
rt_sem_release(_netbuf.wait_resume);
}
else
@ -156,17 +149,34 @@ rt_size_t net_buf_read(rt_uint8_t* buffer, rt_size_t length)
return length;
}
void net_buf_add_job(rt_size_t (*fetch)(rt_uint8_t* ptr, rt_size_t len, void* parameter),
int net_buf_start_job(rt_size_t (*fetch)(rt_uint8_t* ptr, rt_size_t len, void* parameter),
void (*close)(void* parameter),
void* parameter)
{
struct net_buffer_job job;
rt_uint32_t level;
/* job message */
job.fetch = fetch;
job.close = close;
job.parameter = parameter;
rt_mq_send(_netbuf_mq, (void*)&job, sizeof(struct net_buffer_job));
level = rt_hw_interrupt_disable();
/* check netbuf worker is stopped */
if (_netbuf.stat == NETBUF_STAT_STOPPED)
{
/* change stat to buffering if netbuf stopped */
_netbuf.stat = NETBUF_STAT_BUFFERING;
rt_hw_interrupt_enable(level);
rt_kprintf("stat[stoppped] -> buffering\n");
rt_mq_send(_netbuf_mq, (void*)&job, sizeof(struct net_buffer_job));
return 0;
}
rt_hw_interrupt_enable(level);
return -1;
}
void net_buf_stop_job()
@ -174,8 +184,20 @@ void net_buf_stop_job()
rt_uint32_t level;
level = rt_hw_interrupt_disable();
_netbuf.stat = NETBUF_STAT_STOPPING;
rt_kprintf("stat -> stopping\n");
if (_netbuf.stat == NETBUF_STAT_SUSPEND)
{
/* resume the net buffer worker */
rt_sem_release(_netbuf.wait_resume);
_netbuf.stat = NETBUF_STAT_STOPPING;
rt_kprintf("stat[suspend] -> stopping\n");
}
else if (_netbuf.stat == NETBUF_STAT_BUFFERING)
{
/* netbuf worker is working, set stat to stopping */
_netbuf.stat = NETBUF_STAT_STOPPING;
rt_kprintf("stat[buffering] -> stopping\n");
}
rt_hw_interrupt_enable(level);
}
@ -183,6 +205,8 @@ static void net_buf_do_stop(struct net_buffer_job* job)
{
/* source closed */
job->close(job->parameter);
/* set stat to stopped */
_netbuf.stat = NETBUF_STAT_STOPPED;
rt_kprintf("stat -> stopped\n");
if (_netbuf.is_wait_ready == RT_TRUE)
@ -191,8 +215,12 @@ static void net_buf_do_stop(struct net_buffer_job* job)
_netbuf.is_wait_ready = RT_FALSE;
rt_sem_release(_netbuf.wait_ready);
}
/* reset buffer stat */
_netbuf.data_length = 0;
_netbuf.read_index = 0 ;
_netbuf.save_index = 0;
rt_kprintf("job done, stat %d\n", _netbuf.stat);
rt_kprintf("job done\n");
}
#define NETBUF_BLOCK_SIZE 4096
@ -227,10 +255,13 @@ static void net_buf_do_job(struct net_buffer_job* job)
/* check avaible buffer to save */
if ((_netbuf.size - data_length) < read_length)
{
rt_err_t result;
rt_err_t result, level;
_netbuf.is_wait_resume = RT_TRUE;
rt_kprintf("netbuf suspend, avaible room %d\n", data_length);
/* no free space yet, suspend itself */
rt_kprintf("stat[buffering] -> suspend, avaible room %d\n", data_length);
level = rt_hw_interrupt_disable();
_netbuf.stat = NETBUF_STAT_SUSPEND;
rt_hw_interrupt_enable(level);
result = rt_sem_take(_netbuf.wait_resume, RT_WAITING_FOREVER);
if (result != RT_EOK)
{
@ -240,7 +271,7 @@ static void net_buf_do_job(struct net_buffer_job* job)
}
}
/* there are free space to fetch data */
/* there are enough free space to fetch data */
if ((_netbuf.size - _netbuf.save_index) < read_length)
{
rt_memcpy(&_netbuf.buffer_data[_netbuf.save_index],
@ -273,20 +304,17 @@ static void net_buf_do_job(struct net_buffer_job* job)
/* set buffer position */
player_set_position(data_length);
if ((_netbuf.stat == NETBUF_STAT_BUFFERING) && (data_length >= _netbuf.ready_wm))
if ((_netbuf.stat == NETBUF_STAT_BUFFERING)
&& (data_length >= _netbuf.ready_wm)
&& _netbuf.is_wait_ready == RT_TRUE)
{
_netbuf.stat = NETBUF_STAT_BUSY;
rt_kprintf("stat -> busy\n");
/* notify the thread for waitting buffer ready */
rt_kprintf("resume wait buffer\n");
if (_netbuf.is_wait_ready == RT_TRUE)
{
_netbuf.is_wait_ready = RT_FALSE;
/* set buffer status to playing */
player_set_buffer_status(RT_FALSE);
rt_sem_release(_netbuf.wait_ready);
}
_netbuf.is_wait_ready = RT_FALSE;
/* set buffer status to playing */
player_set_buffer_status(RT_FALSE);
rt_sem_release(_netbuf.wait_ready);
}
}
@ -305,11 +333,16 @@ static void net_buf_thread_entry(void* parameter)
result = rt_mq_recv(_netbuf_mq, (void*)&job, sizeof(struct net_buffer_job), RT_WAITING_FOREVER);
if (result == RT_EOK)
{
_netbuf.stat = NETBUF_STAT_BUFFERING;
rt_kprintf("stat -> buffering\n");
/* set stat to buffering */
if (_netbuf.stat == NETBUF_STAT_BUFFERING)
{
/* reset data length and read/save index */
_netbuf.data_length = 0;
_netbuf.read_index = _netbuf.save_index = 0;
/* perform the job */
net_buf_do_job(&job);
/* perform the job */
net_buf_do_job(&job);
}
}
}
}
@ -331,13 +364,12 @@ void net_buf_init(rt_size_t size)
_netbuf.resume_wm = _netbuf.size * 80/100;
/* set init stat */
_netbuf.stat = NETBUF_STAT_FREE;
rt_kprintf("stat -> free\n");
_netbuf.stat = NETBUF_STAT_STOPPED;
rt_kprintf("stat -> stopped\n");
_netbuf.wait_ready = rt_sem_create("nready", 0, RT_IPC_FLAG_FIFO);
_netbuf.wait_resume = rt_sem_create("nresum", 0, RT_IPC_FLAG_FIFO);
_netbuf.is_wait_ready = RT_FALSE;
_netbuf.is_wait_resume = RT_FALSE;
/* crate message queue */
_netbuf_mq = rt_mq_create("njob", sizeof(struct net_buffer_job),

View File

@ -14,10 +14,12 @@ void sbuf_release(void* ptr);
void net_buf_init(rt_size_t size);
rt_size_t net_buf_read(rt_uint8_t* buffer, rt_size_t length);
void net_buf_add_job(rt_size_t (*fetch)(rt_uint8_t* ptr, rt_size_t len, void* parameter),
int net_buf_start_job(rt_size_t (*fetch)(rt_uint8_t* ptr, rt_size_t len, void* parameter),
void (*close)(void* parameter),
void* parameter);
void net_buf_stop_job(void);
#endif
#endif

View File

@ -71,6 +71,8 @@ void player_thread(void* parameter)
is_playing = RT_TRUE;
player_notify_play();
ice_mp3(request.fn);
/* notfiy net buffer worker to stop */
net_buf_stop_job();
player_notify_stop();
is_playing = RT_FALSE;
}