fix memory overwrite on shoutcast meta data reception.

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@431 bbd45198-f89e-11dd-88c7-29a3b14d5316
This commit is contained in:
bernard.xiong 2010-02-22 13:35:47 +00:00
parent f4cb1ba595
commit 3677390335
10 changed files with 222 additions and 282 deletions

View File

@ -94,6 +94,19 @@ void rt_hw_board_init()
GPIO_SetBits(GPIOE,GPIO_Pin_5);
}
#if LCD_VERSION == 2
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_Init(GPIOF,&GPIO_InitStructure);
}
#endif
/* NVIC Configuration */
NVIC_Configuration();

View File

@ -46,6 +46,10 @@
// <i>Default: 1
#define CODEC_VERSION 1
// <o> LCD Modul Version <1=>Version 1 <2=>Version 2
// <i>Default: 1
#define LCD_VERSION 1
void rt_hw_board_led_on(int n);
void rt_hw_board_led_off(int n);
void rt_hw_board_init(void);

View File

@ -1,245 +0,0 @@
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/window.h>
#include <rtgui/widgets/label.h>
#include <finsh.h>
static struct rtgui_timer *timer;
static struct rtgui_label* label;
static struct rtgui_win* msgbox;
static rt_uint8_t label_text[80];
static int cnt = 5;
void diag_close(struct rtgui_timer* timer, void* parameter)
{
rt_sprintf(label_text, "closed then %d second!", cnt);
rtgui_label_set_text(label, label_text);
rtgui_widget_update(RTGUI_WIDGET(label));
if (cnt == 0)
{
rtgui_win_destroy(msgbox);
rtgui_timer_stop(timer);
rtgui_timer_destory(timer);
}
cnt --;
}
void msg()
{
rt_mq_t mq;
rt_thread_t tid;
rt_uint32_t user_data;
struct rtgui_rect rect = {50, 50, 200, 200};
tid = rt_thread_self();
if (tid == RT_NULL) return; /* can't use in none-scheduler environement */
user_data = tid->user_data;
/* create gui message queue */
mq = rt_mq_create("msgbox", 256, 4, RT_IPC_FLAG_FIFO);
/* register message queue on current thread */
rtgui_thread_register(rt_thread_self(), mq);
msgbox = rtgui_win_create(RT_NULL, "Information", &rect, RTGUI_WIN_STYLE_DEFAULT);
if (msgbox != RT_NULL)
{
struct rtgui_box* box = rtgui_box_create(RTGUI_VERTICAL, RT_NULL);
cnt = 5;
rt_sprintf(label_text, "closed then %d second!", cnt);
label = rtgui_label_create(label_text);
rtgui_win_set_box(msgbox, box);
RTGUI_WIDGET(label)->align = RTGUI_ALIGN_CENTER_HORIZONTAL |
RTGUI_ALIGN_CENTER_VERTICAL;
rtgui_widget_set_miniwidth(RTGUI_WIDGET(label),130);
rtgui_box_append(box, RTGUI_WIDGET(label));
rtgui_box_layout(box);
rtgui_win_show(msgbox);
}
timer = rtgui_timer_create(200, RT_TIMER_FLAG_PERIODIC,
diag_close, RT_NULL);
rtgui_timer_start(timer);
rtgui_win_event_loop(msgbox);
rtgui_thread_deregister(rt_thread_self());
/* remove RTGUI message queue */
rt_mq_delete(mq);
/* recover user data */
tid->user_data = user_data;
}
FINSH_FUNCTION_EXPORT(msg, msg on gui)
#include <rtgui/rtgui.h>
#include <rtgui/rtgui_system.h>
#include <rtgui/widgets/box.h>
#include <rtgui/widgets/button.h>
#include <rtgui/widgets/window.h>
#include <rtgui/widgets/textbox.h>
#include <rtgui/widgets/iconbox.h>
#include <rtgui/widgets/view.h>
#include <rtgui/widgets/workbench.h>
/* XPM */
static const char *goto_xpm[] = {
/* columns rows colors chars-per-pixel */
"16 16 43 1",
". c Black",
"e c #0F0F0F",
"= c #2D2D2D",
"> c #3C3C3C",
"X c #010101",
"$ c #101010",
"% c #1F1F1F",
"r c #090909",
"- c #272727",
"3 c #363636",
"+ c #020202",
"# c #111111",
"2 c #3E3E3E",
"i c #4D4D4D",
"q c #191919",
", c #373737",
"f c #464646",
"o c #030303",
"u c #121212",
"p c #212121",
"; c #0B0B0B",
" c None",
"7 c #292929",
"O c #040404",
"6 c #131313",
"5 c #222222",
"t c #313131",
"4 c #4F4F4F",
"@ c #0C0C0C",
"a c #1B1B1B",
"* c #2A2A2A",
"0 c #141414",
"< c #0D0D0D",
": c #3A3A3A",
"9 c #060606",
"& c #151515",
"w c #242424",
"s c #424242",
"d c #1D1D1D",
"8 c #070707",
"1 c #161616",
"g c #252525",
"y c #343434",
/* pixels */
" .XX. ",
" oOOO+ ",
" @####o ",
" $%%%%& ",
" .*===-+ ",
" ;:>,; ",
" <# 12O ",
" 345 .X. ",
" 678 .X++X ",
" +9999o ",
" o0&&&6+ ",
" qwwwwe ",
" rtyy% ",
" +; uip ",
" asd o ",
" $fg "
};
static struct rtgui_image* image = RT_NULL;
static void rtgui_demo_workbench_entry(void* parameter)
{
rt_mq_t mq;
struct rtgui_view* view;
struct rtgui_workbench* workbench;
/* init rtgui demo message queue */
mq = rt_mq_create("mqWB", 256, 4, RT_IPC_FLAG_FIFO);
/* register thread and message queue */
rtgui_thread_register(rt_thread_self(), mq);
/* create container */
workbench = rtgui_workbench_create("main", "widget");
if (workbench == RT_NULL) return;
/************************************************************************/
/* Create View */
/************************************************************************/
view = rtgui_view_create("widget");
rtgui_workbench_add_view(workbench, view);
image = rtgui_image_create_from_mem("xpm", goto_xpm, sizeof(goto_xpm));
{
struct rtgui_box *box = rtgui_box_create(RTGUI_VERTICAL, &rtgui_empty_rect);
struct rtgui_box *hbox = rtgui_box_create(RTGUI_HORIZONTAL, &rtgui_empty_rect);
struct rtgui_button* button = rtgui_button_create("OK");
struct rtgui_textbox *textbox = rtgui_textbox_create("text edit box");
struct rtgui_iconbox *iconbox = rtgui_iconbox_create(image, "icon",
RTGUI_ICONBOX_TEXT_RIGHT);
RTGUI_WIDGET_BACKGROUND(RTGUI_WIDGET(view)) = RTGUI_RGB(255, 255, 224);
rtgui_view_set_box(view, box);
RTGUI_WIDGET(button)->align = RTGUI_ALIGN_CENTER_VERTICAL;
rtgui_widget_set_miniwidth(RTGUI_WIDGET(button), 80);
rtgui_widget_set_miniheight(RTGUI_WIDGET(button), 25);
rtgui_box_append(box, RTGUI_WIDGET(button));
/* add a horizontal box */
rtgui_widget_set_miniwidth(RTGUI_WIDGET(hbox), 20);
rtgui_widget_set_miniheight(RTGUI_WIDGET(hbox), 100);
RTGUI_WIDGET(hbox)->align = RTGUI_ALIGN_EXPAND;
rtgui_box_layout(hbox);
rtgui_box_append(box, RTGUI_WIDGET(hbox));
rtgui_box_append(box, RTGUI_WIDGET(textbox));
/* add icon box */
RTGUI_WIDGET(iconbox)->gc.font = rtgui_font_refer("hz", 16);
rtgui_box_append(box, RTGUI_WIDGET(iconbox));
rtgui_box_layout(box);
}
/* show view */
rtgui_view_show(view);
rtgui_workbench_event_loop(workbench);
}
void rtgui_demo_workbench_init()
{
static rt_bool_t inited = RT_FALSE;
if (inited == RT_FALSE)
{
rt_thread_t tid;
tid = rt_thread_create("tWB",
rtgui_demo_workbench_entry, RT_NULL,
2048, 25, 10);
if (tid != RT_NULL) rt_thread_startup(tid);
inited = RT_TRUE;
}
}
#ifdef RT_USING_RTGUI
#include <finsh.h>
void w()
{
rtgui_demo_workbench_init();
}
FINSH_FUNCTION_EXPORT(w, workbench demo)
#endif

View File

@ -371,7 +371,7 @@ static int shoutcast_connect(struct shoutcast_session* session,
int socket_handle;
int peer_handle;
int rc;
char mimeBuffer[100];
char mimeBuffer[256];
if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
{
@ -460,7 +460,7 @@ static int shoutcast_connect(struct shoutcast_session* session,
if (strstr(mimeBuffer, "content-type:"))
{
/* check content-type */
if (strstr(mimeBuffer, "content-type: audio/mpeg") == RT_NULL)
if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
{
rt_kprintf("ICY content is not audio/mpeg.\n");
return -1;
@ -470,7 +470,7 @@ static int shoutcast_connect(struct shoutcast_session* session,
if (strstr(mimeBuffer, "Content-Type:"))
{
/* check content-type */
if (strstr(mimeBuffer, "Content-Type: audio/mpeg") == RT_NULL)
if (strstr(mimeBuffer, "audio/mpeg") == RT_NULL)
{
rt_kprintf("ICY content is not audio/mpeg.\n");
return -1;
@ -530,6 +530,7 @@ rt_size_t shoutcast_session_read(struct shoutcast_session* session, rt_uint8_t *
int bytesRead = 0;
int totalRead = 0;
int left = length;
static rt_uint32_t first_meta_size = 0;
// Read until: there is an error, we've read "size" bytes or the remote
// side has closed the connection.
@ -548,32 +549,56 @@ rt_size_t shoutcast_session_read(struct shoutcast_session* session, rt_uint8_t *
} while(left);
/* handle meta */
if (session->current_meta_chunk + totalRead >= session->metaint)
if (first_meta_size > 0)
{
int meta_length, next_chunk_length;
// rt_kprintf("c: %d, total: %d\n", session->current_meta_chunk, totalRead);
/* get the length of meta data */
meta_length = buffer[session->metaint - session->current_meta_chunk] * 16;
next_chunk_length = totalRead - (session->metaint - session->current_meta_chunk) -
(meta_length + 1);
// rt_kprintf("l: %d, n: %d\n", meta_length, next_chunk_length);
/* skip meta data */
memmove(&buffer[session->metaint - session->current_meta_chunk],
&buffer[session->metaint - session->current_meta_chunk + meta_length + 1],
next_chunk_length);
memmove(&buffer[0], &buffer[first_meta_size], totalRead - first_meta_size);
/* set new current meta chunk */
session->current_meta_chunk = next_chunk_length;
totalRead = totalRead - (meta_length + 1);
// rt_kprintf("total: %d\n", totalRead);
// rt_kprintf("remove meta: len %d\n", first_meta_size);
totalRead = totalRead - first_meta_size;
first_meta_size = 0;
session->current_meta_chunk = totalRead;
}
else
else
{
session->current_meta_chunk += totalRead;
if (session->current_meta_chunk + totalRead == session->metaint)
{
rt_uint8_t meta_data;
recv(session->socket, &meta_data, 1, 0);
/* remove meta data in next packet */
first_meta_size = meta_data * 16;
session->current_meta_chunk = 0;
// rt_kprintf("get meta: len %d\n", first_meta_size);
}
else if (session->current_meta_chunk + totalRead > session->metaint)
{
int meta_length, next_chunk_length;
// rt_kprintf("c: %d, total: %d, m: %d\n", session->current_meta_chunk, totalRead, session->metaint);
/* get the length of meta data */
meta_length = buffer[session->metaint - session->current_meta_chunk] * 16;
next_chunk_length = totalRead - (session->metaint - session->current_meta_chunk) -
(meta_length + 1);
// rt_kprintf("l: %d, n: %d\n", meta_length, next_chunk_length);
/* skip meta data */
memmove(&buffer[session->metaint - session->current_meta_chunk],
&buffer[session->metaint - session->current_meta_chunk + meta_length + 1],
next_chunk_length);
/* set new current meta chunk */
session->current_meta_chunk = next_chunk_length;
totalRead = totalRead - (meta_length + 1);
}
else
{
session->current_meta_chunk += totalRead;
}
}
return totalRead;

View File

@ -183,7 +183,8 @@ int mp3_decoder_run(struct mp3_decoder* decoder)
delta += (decoder->bytes_left_before_decoding - decoder->bytes_left);
current_offset += delta;
player_set_position(current_offset);
if (player_get_mode() != PLAYER_PLAY_RADIO)
player_set_position(current_offset);
// rt_kprintf("bytes left after decode: %d\n", decoder->bytes_left);

View File

@ -268,7 +268,7 @@ static void net_buf_do_job(struct net_buffer_job* job)
rt_hw_interrupt_enable(level);
}
rt_kprintf("buffering ... %d %c\n", (data_length * 100) / _netbuf.size, '%');
// rt_kprintf("buffering ... %d %c\n", (data_length * 100) / _netbuf.size, '%');
/* set buffer position */
player_set_position(data_length);

View File

@ -378,6 +378,11 @@ void player_set_buffer_status(rt_bool_t buffering)
strncpy(tinfo.artist, "²¥·ÅÖÐ...", 40);
}
enum PLAYER_MODE player_get_mode()
{
return player_mode;
}
void player_play_file(const char* fn)
{
struct rtgui_dc* dc;
@ -652,6 +657,11 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
switch (next_step)
{
case PLAYER_STEP_STOP:
// #define TEST_MODE
#ifdef TEST_MODE
player_play_file(play_list_start());
next_step = PLAYER_STEP_STOP;
#else
{
struct rtgui_dc* dc;
rtgui_color_t saved;
@ -680,6 +690,7 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
RTGUI_WIDGET_FOREGROUND(widget) = saved;
rtgui_dc_end_drawing(dc);
}
#endif
break;
case PLAYER_STEP_NEXT:

View File

@ -49,4 +49,7 @@ void player_set_position(rt_uint32_t position);
void player_set_title(const char* title);
void player_set_buffer_status(rt_bool_t buffering);
/* get playing mode */
enum PLAYER_MODE player_get_mode(void);
#endif

View File

@ -233,17 +233,6 @@ void EXTI0_IRQHandler(void)
{
}
/*******************************************************************************
* Function Name : EXTI1_IRQHandler
* Description : This function handles External interrupt Line 1 request.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void EXTI1_IRQHandler(void)
{
}
/*******************************************************************************
* Function Name : EXTI2_IRQHandler
* Description : This function handles External interrupt Line 2 request.

139
bsp/stm32_radio/touch.c Normal file
View File

@ -0,0 +1,139 @@
#include "stm32f10x.h"
#include "rtthread.h"
static void Delay_Nus(unsigned int dt)
{
volatile unsigned int a;
while (--dt)
{
for (a=0; a<5000; a++);
}
}
/*
MISO PA6
MOSI PA7
CLK PA5
CS PC4
*/
#define CS_0() GPIO_ResetBits(GPIOC,GPIO_Pin_4)
#define CS_1() GPIO_SetBits(GPIOC,GPIO_Pin_4)
/*
7 6 - 4 3 2 1-0
s A2-A0 MODE SER/DFR PD1-PD0
*/
#define TOUCH_MSR_X 0x90 //读X轴坐标指令 addr:1
#define TOUCH_MSR_Y 0xD0 //读Y轴坐标指令 addr:3
extern unsigned char SPI_WriteByte(unsigned char data);
//SPI写数据
static void WriteDataTo7843(unsigned char num)
{
SPI_WriteByte(num);
}
//SPI 读数据
static unsigned int ReadDataFrom7843(void)
{
unsigned int temp;
temp = SPI_WriteByte(0x00)<<4;
temp |= ( (SPI_WriteByte(0x00)>>4)&0x0F );
return temp;
}
//触摸处理
void Go_Touch(void)
{
unsigned int X;
unsigned int Y;
CS_0();
WriteDataTo7843(TOUCH_MSR_X); //送控制字 10010000 即用差分方式读X坐标 详细请见有关资料
Delay_Nus(100);
Y = ReadDataFrom7843(); //读X轴坐标
WriteDataTo7843(TOUCH_MSR_Y); //送控制字 11010000 即用差分方式读Y坐标 详细请见有关资料
Delay_Nus(50);
X = ReadDataFrom7843(); //读Y轴坐标
CS_1();
rt_kprintf("\r\nX: %04d Y: %04d",X,Y);
}
static void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the EXTI0 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = EXTI1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
static void exti_int_config(void)
{
EXTI_InitTypeDef EXTI_InitStructure;
/* PB1 touch INT */
GPIO_EXTILineConfig(GPIO_PortSourceGPIOB, GPIO_PinSource1);
/* Configure EXTI */
EXTI_InitStructure.EXTI_Line = EXTI_Line1;
EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;
EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;//Falling下降沿 Rising上升
EXTI_InitStructure.EXTI_LineCmd = ENABLE;
EXTI_Init(&EXTI_InitStructure);
EXTI_ClearITPendingBit(EXTI_Line1);
}
#include <finsh.h>
void touch_test(void)
{
rt_kprintf("\r\ntouch testing....\r\n");
RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO,ENABLE);
NVIC_Configuration();
exti_int_config();
/* PC4 touch CS */
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC,ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB,ENABLE);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_1;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOB,&GPIO_InitStructure);
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_4;
GPIO_Init(GPIOC,&GPIO_InitStructure);
CS_1();
}
Delay_Nus( 500 );
CS_0();
WriteDataTo7843(0x00);
CS_1();
}
FINSH_FUNCTION_EXPORT(touch_test, touch_test)
void EXTI1_IRQHandler(void)
{
EXTI_ClearITPendingBit(EXTI_Line1);
Go_Touch();
}
/******************* (C) COPYRIGHT 2008 STMicroelectronics */