Ethernet+BLE prototype
Dependencies: mbed EthernetInterface mbed-rtos
Diff: HttpSend.cpp
- Revision:
- 3:d9445b9e7163
- Child:
- 4:78e96198c879
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HttpSend.cpp Thu Mar 27 10:17:11 2014 +0000 @@ -0,0 +1,94 @@ +#include "config.h" +#include "log.h" +#include "EthernetInterface.h" +#include "HttpSend.h" +#include "mbed.h" +//#include "rtos.h" +#include "cmsis_os.h" + +EthernetInterface m_ethernet; + +MemoryPool<message_t, 64> m_mpool; +Queue<message_t, 64> m_queue; + +osThreadId m_send_thread; + +void sendData(char* data, size_t length){ + + char http_cmd[256]; + TCPSocketConnection sock; + int ret = sock.connect(SERVER_HOST, SERVER_HOST_PORT); + + //DEBUG_LOG("sock.connect ret=%d\n",ret); + + // header + sprintf(http_cmd,"POST /whiteserver/PostData HTTP/1.1\r\n"); + sprintf(http_cmd,"%sUser-Agent: sample-app\r\n",http_cmd); + sprintf(http_cmd,"%sHost: %s\r\n",http_cmd,SERVER_HOST); + sprintf(http_cmd,"%sContent-Length: %d\r\n",http_cmd,length); + sprintf(http_cmd,"%sContent-Type: application/x-www-form-urlencoded\r\n",http_cmd); + + // data + sprintf(http_cmd,"%s\r\n%s",http_cmd,data); + + + DEBUG_LOG("------------- DATA ---------------\n%s\n",http_cmd); + + ret = sock.send_all(http_cmd, strlen(http_cmd)); + +/* 結果は受けない。POSTするのみにしておく + DEBUG_LOG("sock.send_all ret=%d \n",ret); + + char buffer[300]; + while (true) { + ret = sock.receive(buffer, sizeof(buffer)-1); + DEBUG_LOG("sock.receive ret=%d \n",ret); + if (ret <= 0){ + break; + } + buffer[ret] = '\0'; + INFO_LOG(" Received ret=%d \n chars from server :\n%s\n", ret, buffer); + } +*/ + sock.close(); +} + +void send_thread (void const *argument) { + INFO_LOG ("send_thread start\n"); + while (true) { + osEvent evt = m_queue.get(); + if (evt.status == osEventMessage) { + message_t *message = (message_t*)evt.value.p; + DEBUG_LOG("(send_thread)size=%d:%s\n",message->size,message->data); + m_ethernet.connect(); + sendData(message->data, message->size); + m_ethernet.disconnect(); + m_mpool.free(message); + DEBUG_LOG ("*message=0x%08X free \n",message); + } + } + // unreachable INFO_LOG ("send_thread end\n"); +} + +osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE); + +void HttpSend::init(){ + INFO_LOG ("HttpSend::init start\n"); + INFO_LOG ("1111111111 \n"); + m_ethernet.init(); + INFO_LOG ("222 \n"); + INFO_LOG ("IP Address is %s \n", m_ethernet.getIPAddress()); + INFO_LOG ("333 \n"); + m_send_thread = osThreadCreate(osThread(send_thread), NULL); + INFO_LOG ("HttpSend::init end\n"); +} + +message_t* HttpSend::getMessageBuffer(){ + return m_mpool.alloc(); +} + +void HttpSend::send(message_t* message){ + m_queue.put(message); +} + +