Ethernet+BLE prototype

Dependencies:   mbed EthernetInterface mbed-rtos

Committer:
teruo
Date:
Fri Mar 28 01:10:32 2014 +0000
Revision:
4:78e96198c879
Parent:
3:d9445b9e7163
Child:
5:9fffeb949e5a
??????????????
; Serial.attach & callback ?????????

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 "HttpSend.h"
teruo 3:d9445b9e7163 4 #include "mbed.h"
teruo 3:d9445b9e7163 5
teruo 3:d9445b9e7163 6
teruo 3:d9445b9e7163 7 MemoryPool<message_t, 64> m_mpool;
teruo 3:d9445b9e7163 8 Queue<message_t, 64> m_queue;
teruo 3:d9445b9e7163 9
teruo 4:78e96198c879 10 int m_mpCnt=0;
teruo 4:78e96198c879 11
teruo 4:78e96198c879 12 EthernetInterface* m_ethernet;
teruo 4:78e96198c879 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 4:78e96198c879 56
teruo 4:78e96198c879 57 // Thread::wait(3000);
teruo 3:d9445b9e7163 58 void send_thread (void const *argument) {
teruo 3:d9445b9e7163 59 INFO_LOG ("send_thread start\n");
teruo 4:78e96198c879 60
teruo 3:d9445b9e7163 61 while (true) {
teruo 3:d9445b9e7163 62 osEvent evt = m_queue.get();
teruo 3:d9445b9e7163 63 if (evt.status == osEventMessage) {
teruo 3:d9445b9e7163 64 message_t *message = (message_t*)evt.value.p;
teruo 4:78e96198c879 65 if(message!=NULL){
teruo 4:78e96198c879 66 DEBUG_LOG("(size=%d)(m_mpCnt=%d):%s",message->size,m_mpCnt,message->data);
teruo 4:78e96198c879 67 // m_ethernet->connect();
teruo 4:78e96198c879 68 // INFO_LOG ("IP Address is %s \n", m_ethernet->getIPAddress());
teruo 4:78e96198c879 69 // sendData(message->data, message->size);
teruo 4:78e96198c879 70 // m_ethernet->disconnect();
teruo 4:78e96198c879 71 m_mpool.free(message);
teruo 4:78e96198c879 72 m_mpCnt--;
teruo 4:78e96198c879 73 // DEBUG_LOG ("*message=0x%08X free \n",message);
teruo 4:78e96198c879 74 }
teruo 3:d9445b9e7163 75 }
teruo 4:78e96198c879 76 Thread::wait(100);
teruo 3:d9445b9e7163 77 }
teruo 4:78e96198c879 78
teruo 3:d9445b9e7163 79 // unreachable INFO_LOG ("send_thread end\n");
teruo 3:d9445b9e7163 80 }
teruo 3:d9445b9e7163 81
teruo 3:d9445b9e7163 82 osThreadDef(send_thread, osPriorityNormal, DEFAULT_STACK_SIZE);
teruo 3:d9445b9e7163 83
teruo 4:78e96198c879 84 void HttpSend::init(EthernetInterface* eth){
teruo 3:d9445b9e7163 85 INFO_LOG ("HttpSend::init start\n");
teruo 4:78e96198c879 86 m_ethernet = eth;
teruo 3:d9445b9e7163 87 m_send_thread = osThreadCreate(osThread(send_thread), NULL);
teruo 3:d9445b9e7163 88 INFO_LOG ("HttpSend::init end\n");
teruo 3:d9445b9e7163 89 }
teruo 3:d9445b9e7163 90
teruo 3:d9445b9e7163 91 message_t* HttpSend::getMessageBuffer(){
teruo 4:78e96198c879 92 m_mpCnt++;
teruo 3:d9445b9e7163 93 return m_mpool.alloc();
teruo 3:d9445b9e7163 94 }
teruo 3:d9445b9e7163 95
teruo 3:d9445b9e7163 96 void HttpSend::send(message_t* message){
teruo 3:d9445b9e7163 97 m_queue.put(message);
teruo 3:d9445b9e7163 98 }
teruo 3:d9445b9e7163 99
teruo 3:d9445b9e7163 100