Browse Source

add radio list update by inkfish

git-svn-id: https://rt-thread.googlecode.com/svn/trunk@490 bbd45198-f89e-11dd-88c7-29a3b14d5316
wuyangyong 15 years ago
parent
commit
e00916b557

+ 1 - 1
bsp/stm32_radio/board.h

@@ -46,7 +46,7 @@
 // <1=>Version 1:fmt0371
 // <2=>Version 2:ili9320/9325
 // 	<i>Default: 1
-#define LCD_VERSION             2
+#define LCD_VERSION             1
 
 /*
  * IMPORTANT NOTICE:

+ 1 - 0
bsp/stm32_radio/player_bg.h

@@ -9,6 +9,7 @@ enum PLAYER_REQUEST_TYPE
 	PLAYER_REQUEST_PLAY_LIST,
 	PLAYER_REQUEST_STOP,
 	PLAYER_REQUEST_FREEZE,
+	PLAYER_REQUEST_UPDATE_RADIO_LIST,
 };
 
 struct player_request

+ 33 - 6
bsp/stm32_radio/player_ui.c

@@ -213,6 +213,33 @@ static void function_play_radio(void* parameter)
 	}
 }
 
+static void function_radio_list_update(void* parameter)
+{
+	extern void update_radio_list_req(void);
+	extern void update_radio_thread(void* parameter);
+	extern rt_mq_t update_radio_mq;
+	rt_thread_t update_radio_list_thread;
+
+
+	if(update_radio_mq == RT_NULL)
+	{
+		update_radio_mq = rt_mq_create("updateRadioList", sizeof(struct player_request),
+			1, RT_IPC_FLAG_FIFO);
+		RT_ASSERT(update_radio_mq != RT_NULL);
+
+		update_radio_list_thread = rt_thread_create("update_bg", update_radio_thread, RT_NULL,
+			1024 ,20, 5);
+
+		if (update_radio_list_thread == RT_NULL) rt_kprintf("updateRadioList thread init failed\n");
+		else
+		{
+			rt_thread_startup(update_radio_list_thread);
+			update_radio_list_req();
+		}
+	}
+	return;
+}
+
 static void function_filelist(void* parameter)
 {
 	rtgui_rect_t rect;
@@ -345,7 +372,7 @@ void function_cable(void* parameter)
 const struct rtgui_list_item function_list[] =
 {
 	{"选择电台", RT_NULL, function_play_radio, RT_NULL},
-	{"更新电台", RT_NULL, function_action, RT_NULL},
+	{"更新电台", RT_NULL, function_radio_list_update, RT_NULL},
 	{"播放文件", RT_NULL, function_filelist, RT_NULL},
 	{"浏览图片", RT_NULL, function_show_picure, RT_NULL},
 	{"设备信息", RT_NULL, function_device, RT_NULL},
@@ -760,23 +787,23 @@ static rt_bool_t home_view_event_handler(struct rtgui_widget* widget, struct rtg
 				rtgui_view_t *view;
 				rtgui_label_t *label;
 				rtgui_rect_t rect = {0, 0, 150, 150}, container_rect;
-			
+
 				rtgui_graphic_driver_get_default_rect(&container_rect);
 				/* set centre */
 				rtgui_rect_moveto_align(&container_rect, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
 				view = rtgui_view_create("USB");
 				rtgui_workbench_add_view(workbench, view);
-			
+
 				/* set container to window rect */
 				container_rect = rect;
-			
+
 				rect.x1 = 0; rect.y1 = 0;
 				rect.x2 = 120; rect.y2 = 20;
 				label = rtgui_label_create("USB 联机中...");
 				rtgui_rect_moveto_align(&container_rect, &rect, RTGUI_ALIGN_CENTER_HORIZONTAL | RTGUI_ALIGN_CENTER_VERTICAL);
 				rtgui_widget_set_rect(RTGUI_WIDGET(label), &rect);
 				rtgui_container_add_child(RTGUI_CONTAINER(view), RTGUI_WIDGET(label));
-			
+
 				rtgui_view_show(view, RT_TRUE);
 				/* never reach hear */
 			}
@@ -796,7 +823,7 @@ rt_bool_t player_workbench_event_handler(rtgui_widget_t *widget, rtgui_event_t *
     if (event->type == RTGUI_EVENT_KBD)
     {
         struct rtgui_event_kbd* ekbd = (struct rtgui_event_kbd*)event;
-        if (((ekbd->type == RTGUI_KEYUP) && ekbd->key == RTGUIK_HOME) 
+        if (((ekbd->type == RTGUI_KEYUP) && ekbd->key == RTGUIK_HOME)
 			&& !RTGUI_WORKBENCH_IS_MODAL_MODE(workbench))
         {
             /* active home view */

+ 1 - 0
bsp/stm32_radio/project.Uv2

@@ -48,6 +48,7 @@ File 1,1,<.\picture.c><picture.c>
 File 1,1,<.\station_list.c><station_list.c>
 File 1,1,<.\ili9325\ili9325.c><ili9325.c>
 File 1,1,<.\touch.c><touch.c>
+File 1,1,<.\radio_list_update.c><radio_list_update.c>
 File 2,1,<..\..\src\clock.c><clock.c>
 File 2,1,<..\..\src\idle.c><idle.c>
 File 2,1,<..\..\src\ipc.c><ipc.c>

+ 214 - 0
bsp/stm32_radio/radio_list_update.c

@@ -0,0 +1,214 @@
+#include <stm32f10x.h>
+#include <rtthread.h>
+#include <dfs_posix.h>
+#include <lwip/sockets.h>
+#include <lwip/netdb.h>
+
+#include "http.h"
+#include "player_bg.h"
+
+/* */
+#define RADIO_FN                    "/radio.pls"
+#define RADIO_LIST_UPDATE_URL       "http://radio.rt-thread.org/radio.pls"
+/* */
+
+/* */
+extern int http_read_line( int socket, char * buffer, int size );
+extern int http_is_error_header(char *mime_buf);
+extern int http_resolve_address(struct sockaddr_in *server, const char * url, char *host_addr, char** request);
+/* */
+
+const char _radio_list_update_get[] = "GET %s HTTP/1.0\r\nHost: %s:%d\r\nUser-Agent: RT-Thread HTTP Agent\r\nConnection: close\r\n\r\n";
+rt_mq_t update_radio_mq = RT_NULL;
+
+
+
+int  radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_addr, const char* url)
+{
+    int socket_handle;
+    int peer_handle;
+    int rc;
+    char mimeBuffer[256];
+
+    if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
+    {
+        rt_kprintf( "RLUS: SOCKET FAILED\n" );
+        return -1;
+    }
+
+    peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
+    if ( peer_handle < 0 )
+    {
+        rt_kprintf( "RLUS: CONNECT FAILED %i\n", peer_handle );
+        return -1;
+    }
+
+    {
+        char *buf;
+        rt_uint32_t length;
+
+        buf = rt_malloc (512);
+        if (*url)
+            length = rt_snprintf(buf, 512, _radio_list_update_get, url, host_addr, ntohs(server->sin_port));
+        else
+            length = rt_snprintf(buf, 512, _radio_list_update_get, "/", host_addr, ntohs(server->sin_port));
+
+        rc = send(peer_handle, buf, length, 0);
+        rt_kprintf("radio list update request:\n%s", buf);
+
+        /* release buffer */
+        rt_free(buf);
+    }
+    /* read the header information */
+    while ( 1 )
+    {
+        // read a line from the header information.
+        rc = http_read_line(peer_handle, mimeBuffer, 100);
+        rt_kprintf(">>%s", mimeBuffer);
+
+        if ( rc < 0 ) return rc;
+
+        // End of headers is a blank line.  exit.
+        if (rc == 0) break;
+        if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
+
+        if (strstr(mimeBuffer, "HTTP/1."))
+        {
+            rc = http_is_error_header(mimeBuffer);
+            if(rc)
+            {
+                rt_kprintf("HTTP: status code = %d!\n", rc);
+                return -rc;
+            }
+        }
+
+        if (strstr(mimeBuffer, "content-type:"))
+        {
+            /* check content-type */
+            if (strstr(mimeBuffer, "text/plain") == RT_NULL)
+            {
+                rt_kprintf("radio list update content is not text/plain.\n");
+                return -1;
+            }
+        }
+
+        if (strstr(mimeBuffer, "Content-Type:"))
+        {
+            /* check content-type */
+            if (strstr(mimeBuffer, "text/plain") == RT_NULL)
+            {
+                rt_kprintf("radio list update content is not text/plain.\n");
+                return -1;
+            }
+        }
+    }
+
+    return peer_handle;
+}
+int radio_list_update_servicer_session_open(char* url)
+{
+    int peer_handle = 0;
+    struct sockaddr_in server;
+    char *request, host_addr[32];
+
+    if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
+    {
+        return -1;
+    }
+    rt_kprintf("connect to: %s...\n", host_addr);
+
+    if((peer_handle = radio_list_update_servicer_connect(&server, host_addr, request)) < 0)
+    {
+        rt_kprintf("radio list update: failed to connect to '%s'!\n", host_addr);
+    }
+    return peer_handle;
+
+}
+void update_radio_list(char* url)
+{
+    char *buf = NULL;
+    int peer_handle = 0;
+    int fd;
+    int rc;
+
+    peer_handle = radio_list_update_servicer_session_open(url);
+
+    if(peer_handle < 0)
+    {
+        return;
+    }
+
+    fd = open(RADIO_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
+    if(fd < 0)
+    {
+        return;
+    }
+    buf = rt_malloc (512);
+    while ( 1 )
+    {
+        // read a line from the header information.
+        rc = http_read_line(peer_handle, buf, 100);
+
+        if ( rc < 0 ) break;
+
+        // End of headers is a blank line.  exit.
+        if (rc == 0) break;
+        if ((rc == 2) && (buf[0] == '\r')) break;
+        rt_kprintf(">>%s", buf);
+
+        write(fd, buf, rc);
+    }
+    rt_free(buf);
+
+    if(close(fd) == 0)
+    {
+        rt_kprintf("Update radio list succeed \r\n");
+    }
+
+}
+
+void update_radio_list_req(void)
+{
+
+    struct player_request request;
+    extern rt_mq_t player_thread_mq;
+    char* ch = RT_NULL;
+
+    request.type = PLAYER_REQUEST_UPDATE_RADIO_LIST;
+    ch = strncpy(request.fn, RADIO_LIST_UPDATE_URL, strlen(RADIO_LIST_UPDATE_URL));
+
+    /* send to message queue */
+    rt_mq_send(update_radio_mq, (void*)&request, sizeof(struct player_request));
+
+}
+
+void update_radio_thread(void* parameter)
+{
+    rt_err_t result;
+    struct player_request request;
+    rt_thread_t update_radio_list_thread;
+    while(1)
+    {
+        /* get request from message queue */
+        result = rt_mq_recv(update_radio_mq, (void*)&request,
+                            sizeof(struct player_request), RT_WAITING_FOREVER);
+        if (result == RT_EOK)
+        {
+            switch (request.type)
+            {
+            case PLAYER_REQUEST_UPDATE_RADIO_LIST:
+                if ((strstr(request.fn, "http://") == request.fn ||
+                        (strstr(request.fn, "HTTP://") == request.fn )))
+                {
+                    update_radio_list(request.fn);
+                }
+                break;
+            }
+        }
+        rt_mq_delete(update_radio_mq);
+        update_radio_mq = RT_NULL;
+        update_radio_list_thread = rt_thread_self();
+        rt_thread_delete(update_radio_list_thread);
+    }
+}
+