radio_list_update.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. #include <stm32f10x.h>
  2. #include <rtthread.h>
  3. #include <dfs_posix.h>
  4. #include <lwip/sockets.h>
  5. #include <lwip/netdb.h>
  6. #include "http.h"
  7. #include "player_bg.h"
  8. /* */
  9. #define RADIO_FN "/radio.pls"
  10. #define RADIO_LIST_UPDATE_URL "http://radio.rt-thread.org/radio.pls"
  11. /* */
  12. /* */
  13. extern int http_read_line( int socket, char * buffer, int size );
  14. extern int http_is_error_header(char *mime_buf);
  15. extern int http_resolve_address(struct sockaddr_in *server, const char * url, char *host_addr, char** request);
  16. /* */
  17. 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";
  18. rt_mq_t update_radio_mq = RT_NULL;
  19. int radio_list_update_servicer_connect(struct sockaddr_in* server, char* host_addr, const char* url)
  20. {
  21. int socket_handle;
  22. int peer_handle;
  23. int rc;
  24. char mimeBuffer[256];
  25. if((socket_handle = socket( PF_INET, SOCK_STREAM, IPPROTO_TCP )) < 0)
  26. {
  27. rt_kprintf( "RLUS: SOCKET FAILED\n" );
  28. return -1;
  29. }
  30. peer_handle = connect( socket_handle, (struct sockaddr *) server, sizeof(*server));
  31. if ( peer_handle < 0 )
  32. {
  33. rt_kprintf( "RLUS: CONNECT FAILED %i\n", peer_handle );
  34. return -1;
  35. }
  36. {
  37. char *buf;
  38. rt_uint32_t length;
  39. buf = rt_malloc (512);
  40. if (*url)
  41. length = rt_snprintf(buf, 512, _radio_list_update_get, url, host_addr, ntohs(server->sin_port));
  42. else
  43. length = rt_snprintf(buf, 512, _radio_list_update_get, "/", host_addr, ntohs(server->sin_port));
  44. rc = send(peer_handle, buf, length, 0);
  45. rt_kprintf("radio list update request:\n%s", buf);
  46. /* release buffer */
  47. rt_free(buf);
  48. }
  49. /* read the header information */
  50. while ( 1 )
  51. {
  52. // read a line from the header information.
  53. rc = http_read_line(peer_handle, mimeBuffer, 100);
  54. rt_kprintf(">>%s", mimeBuffer);
  55. if ( rc < 0 ) return rc;
  56. // End of headers is a blank line. exit.
  57. if (rc == 0) break;
  58. if ((rc == 2) && (mimeBuffer[0] == '\r')) break;
  59. if (strstr(mimeBuffer, "HTTP/1."))
  60. {
  61. rc = http_is_error_header(mimeBuffer);
  62. if(rc)
  63. {
  64. rt_kprintf("HTTP: status code = %d!\n", rc);
  65. return -rc;
  66. }
  67. }
  68. if (strstr(mimeBuffer, "content-type:"))
  69. {
  70. /* check content-type */
  71. if (strstr(mimeBuffer, "text/plain") == RT_NULL)
  72. {
  73. rt_kprintf("radio list update content is not text/plain.\n");
  74. return -1;
  75. }
  76. }
  77. if (strstr(mimeBuffer, "Content-Type:"))
  78. {
  79. /* check content-type */
  80. if (strstr(mimeBuffer, "text/plain") == RT_NULL)
  81. {
  82. rt_kprintf("radio list update content is not text/plain.\n");
  83. return -1;
  84. }
  85. }
  86. }
  87. return peer_handle;
  88. }
  89. int radio_list_update_servicer_session_open(char* url)
  90. {
  91. int peer_handle = 0;
  92. struct sockaddr_in server;
  93. char *request, host_addr[32];
  94. if(http_resolve_address(&server, url, &host_addr[0], &request) != 0)
  95. {
  96. return -1;
  97. }
  98. rt_kprintf("connect to: %s...\n", host_addr);
  99. if((peer_handle = radio_list_update_servicer_connect(&server, host_addr, request)) < 0)
  100. {
  101. rt_kprintf("radio list update: failed to connect to '%s'!\n", host_addr);
  102. }
  103. return peer_handle;
  104. }
  105. void update_radio_list(char* url)
  106. {
  107. char *buf = NULL;
  108. int peer_handle = 0;
  109. int fd;
  110. int rc;
  111. peer_handle = radio_list_update_servicer_session_open(url);
  112. if(peer_handle < 0)
  113. {
  114. return;
  115. }
  116. fd = open(RADIO_FN, O_WRONLY | O_CREAT | O_TRUNC, 0);
  117. if(fd < 0)
  118. {
  119. return;
  120. }
  121. buf = rt_malloc (512);
  122. while ( 1 )
  123. {
  124. // read a line from the header information.
  125. rc = http_read_line(peer_handle, buf, 100);
  126. if ( rc < 0 ) break;
  127. // End of headers is a blank line. exit.
  128. if (rc == 0) break;
  129. if ((rc == 2) && (buf[0] == '\r')) break;
  130. rt_kprintf(">>%s", buf);
  131. write(fd, buf, rc);
  132. }
  133. rt_free(buf);
  134. if(close(fd) == 0)
  135. {
  136. rt_kprintf("Update radio list succeed \r\n");
  137. }
  138. }
  139. void update_radio_list_req(void)
  140. {
  141. struct player_request request;
  142. extern rt_mq_t player_thread_mq;
  143. char* ch = RT_NULL;
  144. request.type = PLAYER_REQUEST_UPDATE_RADIO_LIST;
  145. ch = strncpy(request.fn, RADIO_LIST_UPDATE_URL, strlen(RADIO_LIST_UPDATE_URL));
  146. /* send to message queue */
  147. rt_mq_send(update_radio_mq, (void*)&request, sizeof(struct player_request));
  148. }
  149. void update_radio_thread(void* parameter)
  150. {
  151. rt_err_t result;
  152. struct player_request request;
  153. rt_thread_t update_radio_list_thread;
  154. while(1)
  155. {
  156. /* get request from message queue */
  157. result = rt_mq_recv(update_radio_mq, (void*)&request,
  158. sizeof(struct player_request), RT_WAITING_FOREVER);
  159. if (result == RT_EOK)
  160. {
  161. switch (request.type)
  162. {
  163. case PLAYER_REQUEST_UPDATE_RADIO_LIST:
  164. if ((strstr(request.fn, "http://") == request.fn ||
  165. (strstr(request.fn, "HTTP://") == request.fn )))
  166. {
  167. update_radio_list(request.fn);
  168. }
  169. break;
  170. }
  171. }
  172. rt_mq_delete(update_radio_mq);
  173. update_radio_mq = RT_NULL;
  174. update_radio_list_thread = rt_thread_self();
  175. rt_thread_delete(update_radio_list_thread);
  176. }
  177. }