Ethernet+BLE prototype

Dependencies:   mbed EthernetInterface mbed-rtos

Committer:
teruo
Date:
Thu Mar 27 10:17:11 2014 +0000
Revision:
3:d9445b9e7163
Child:
4:78e96198c879
ddd

Who changed what in which revision?

UserRevisionLine numberNew contents of line
teruo 3:d9445b9e7163 1 #include "config.h"
teruo 3:d9445b9e7163 2 #include "log.h"
teruo 3:d9445b9e7163 3 #include "EthernetInterface.h"
teruo 3:d9445b9e7163 4 #include "HttpSend.h"
teruo 3:d9445b9e7163 5 #include "mbed.h"
teruo 3:d9445b9e7163 6 //#include "rtos.h"
teruo 3:d9445b9e7163 7 #include "cmsis_os.h"
teruo 3:d9445b9e7163 8
teruo 3:d9445b9e7163 9 EthernetInterface m_ethernet;
teruo 3:d9445b9e7163 10
teruo 3:d9445b9e7163 11 MemoryPool<message_t, 64> m_mpool;
teruo 3:d9445b9e7163 12 Queue<message_t, 64> m_queue;
teruo 3:d9445b9e7163 13
teruo 3:d9445b9e7163 14 osThreadId m_send_thread;
teruo 3:d9445b9e7163 15
teruo 3:d9445b9e7163 16 void sendData(char* data, size_t length){
teruo 3:d9445b9e7163 17
teruo 3:d9445b9e7163 18 char http_cmd[256];
teruo 3:d9445b9e7163 19 TCPSocketConnection sock;
teruo 3:d9445b9e7163 20 int ret = sock.connect(SERVER_HOST, SERVER_HOST_PORT);
teruo 3:d9445b9e7163 21
teruo 3:d9445b9e7163 22 //DEBUG_LOG("sock.connect ret=%d\n",ret);
teruo 3:d9445b9e7163 23
teruo 3:d9445b9e7163 24 // header
teruo 3:d9445b9e7163 25 sprintf(http_cmd,"POST /whiteserver/PostData HTTP/1.1\r\n");
teruo 3:d9445b9e7163 26 sprintf(http_cmd,"%sUser-Agent: sample-app\r\n",http_cmd);
teruo 3:d9445b9e7163 27 sprintf(http_cmd,"%sHost: %s\r\n",http_cmd,SERVER_HOST);
teruo 3:d9445b9e7163 28 sprintf(http_cmd,"%sContent-Length: %d\r\n",http_cmd,length);
teruo 3:d9445b9e7163 29 sprintf(http_cmd,"%sContent-Type: application/x-www-form-urlencoded\r\n",http_cmd);
teruo 3:d9445b9e7163 30
teruo 3:d9445b9e7163 31 // data
teruo 3:d9445b9e7163 32 sprintf(http_cmd,"%s\r\n%s",http_cmd,data);
teruo 3:d9445b9e7163 33
teruo 3:d9445b9e7163 34
teruo 3:d9445b9e7163 35 DEBUG_LOG("------------- DATA ---------------\n%s\n",http_cmd);
teruo 3:d9445b9e7163 36
teruo 3:d9445b9e7163 37 ret = sock.send_all(http_cmd, strlen(http_cmd));
teruo 3:d9445b9e7163 38
teruo 3:d9445b9e7163 39 /* 結果は受けない。POSTするのみにしておく
teruo 3:d9445b9e7163 40 DEBUG_LOG("sock.send_all ret=%d \n",ret);
teruo 3:d9445b9e7163 41
teruo 3:d9445b9e7163 42 char buffer[300];
teruo 3:d9445b9e7163 43 while (true) {
teruo 3:d9445b9e7163 44 ret = sock.receive(buffer, sizeof(buffer)-1);
teruo 3:d9445b9e7163 45 DEBUG_LOG("sock.receive ret=%d \n",ret);
teruo 3:d9445b9e7163 46 if (ret <= 0){
teruo 3:d9445b9e7163 47 break;
teruo 3:d9445b9e7163 48 }
teruo 3:d9445b9e7163 49 buffer[ret] = '\0';
teruo 3:d9445b9e7163 50 INFO_LOG(" Received ret=%d \n chars from server :\n%s\n", ret, buffer);
teruo 3:d9445b9e7163 51 }
teruo 3:d9445b9e7163 52 */
teruo 3:d9445b9e7163 53 sock.close();
teruo 3:d9445b9e7163 54 }
teruo 3:d9445b9e7163 55
teruo 3:d9445b9e7163 56 void send_thread (void const *argument) {
teruo 3:d9445b9e7163 57 INFO_LOG ("send_thread start\n");
teruo 3:d9445b9e7163 58 while (true) {
teruo 3:d9445b9e7163 59 osEvent evt = m_queue.get();
teruo 3:d9445b9e7163 60 if (evt.status == osEventMessage) {
teruo 3:d9445b9e7163 61 message_t *message = (message_t*)evt.value.p;
teruo 3:d9445b9e7163 62 DEBUG_LOG("(send_thread)size=%d:%s\n",message->size,message->data);
teruo 3:d9445b9e7163 63 m_ethernet.connect();
teruo 3:d9445b9e7163 64 sendData(message->data, message->size);
teruo 3:d9445b9e7163 65 m_ethernet.disconnect();
teruo 3:d9445b9e7163 66 m_mpool.free(message);
teruo 3:d9445b9e7163 67 DEBUG_LOG ("*message=0x%08X free \n",message);
teruo 3:d9445b9e7163 68 }
teruo 3:d9445b9e7163 69 }
teruo 3:d9445b9e7163 70 // unreachable INFO_LOG ("send_thread end\n");
teruo 3:d9445b9e7163 71 }
teruo 3:d9445b9e7163 72
teruo 3:d9445b9e7163 73 osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
teruo 3:d9445b9e7163 74
teruo 3:d9445b9e7163 75 void HttpSend::init(){
teruo 3:d9445b9e7163 76 INFO_LOG ("HttpSend::init start\n");
teruo 3:d9445b9e7163 77 INFO_LOG ("1111111111 \n");
teruo 3:d9445b9e7163 78 m_ethernet.init();
teruo 3:d9445b9e7163 79 INFO_LOG ("222 \n");
teruo 3:d9445b9e7163 80 INFO_LOG ("IP Address is %s \n", m_ethernet.getIPAddress());
teruo 3:d9445b9e7163 81 INFO_LOG ("333 \n");
teruo 3:d9445b9e7163 82 m_send_thread = osThreadCreate(osThread(send_thread), NULL);
teruo 3:d9445b9e7163 83 INFO_LOG ("HttpSend::init end\n");
teruo 3:d9445b9e7163 84 }
teruo 3:d9445b9e7163 85
teruo 3:d9445b9e7163 86 message_t* HttpSend::getMessageBuffer(){
teruo 3:d9445b9e7163 87 return m_mpool.alloc();
teruo 3:d9445b9e7163 88 }
teruo 3:d9445b9e7163 89
teruo 3:d9445b9e7163 90 void HttpSend::send(message_t* message){
teruo 3:d9445b9e7163 91 m_queue.put(message);
teruo 3:d9445b9e7163 92 }
teruo 3:d9445b9e7163 93
teruo 3:d9445b9e7163 94