Ethernet+BLE prototype

Dependencies:   mbed EthernetInterface mbed-rtos

Revision:
5:9fffeb949e5a
Parent:
4:78e96198c879
--- a/HttpSend.cpp	Fri Mar 28 01:10:32 2014 +0000
+++ b/HttpSend.cpp	Mon Mar 31 03:25:14 2014 +0000
@@ -3,42 +3,47 @@
 #include "HttpSend.h"
 #include "mbed.h"
 
+#define BUFFER_NUM 64
 
-MemoryPool<message_t, 64>   m_mpool;
-Queue<message_t, 64>        m_queue;
+Queue<message_t, BUFFER_NUM>    m_queue;
+message_t m_mpool[BUFFER_NUM]={0};
+int m_queueCnt=0;
+EthernetInterface* m_ethernet;
+osThreadId m_send_thread;
+TCPSocketConnection m_sock;
 
-int m_mpCnt=0;
+message_t* getBuffer(){
+    for(int i=0 ; i < BUFFER_NUM ; i++){
+        if( m_mpool[i].used == false ){
+            m_mpool[i].used = true;
+            return &m_mpool[i];
+        }
+    }
+    return NULL;
+}
 
-EthernetInterface* m_ethernet;
-
-osThreadId m_send_thread;
+void releaseBuffer(message_t* buf){
+    memset(buf->data,0,sizeof(buf->data));
+    buf->used=false;
+}
 
 void sendData(char* data, size_t length){
+    char http_cmd[256];
 
-    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));
+    
+//    DEBUG_LOG("------------- DATA ---------------\n%s\n",http_cmd);
+//    DEBUG_LOG("(size=%d:%s",length,data);
+    m_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);
@@ -50,11 +55,10 @@
         INFO_LOG(" Received ret=%d \n chars from server :\n%s\n", ret, buffer);
     }
 */    
-    sock.close();
+    //sock.close();
 }
 
     
-//    Thread::wait(3000);
 void send_thread (void const *argument) {
     INFO_LOG ("send_thread start\n");
 
@@ -62,15 +66,18 @@
         osEvent evt = m_queue.get();
         if (evt.status == osEventMessage) {
             message_t *message = (message_t*)evt.value.p;
-            if(message!=NULL){
-                DEBUG_LOG("(size=%d)(m_mpCnt=%d):%s",message->size,m_mpCnt,message->data);
-//                m_ethernet->connect();
+            if(message!=NULL && message->size!=0 ){
+//                DEBUG_LOG("(size=%d)(m_queueCnt=%d):%s",message->size,m_queueCnt,message->data);
+                DEBUG_LOG("(size=%d)(m_queueCnt=%d)\n",message->size,m_queueCnt);
 //                INFO_LOG ("IP Address is %s \n", m_ethernet->getIPAddress());
-//                sendData(message->data, message->size);
+                sendData(message->data, message->size);
 //                m_ethernet->disconnect();
-                m_mpool.free(message);
-                m_mpCnt--;
+                //m_mpool.free(message);
+                releaseBuffer(message);
+                m_queueCnt--;
 //                DEBUG_LOG ("*message=0x%08X free \n",message);
+            }else{
+                DEBUG_LOG("message ??? \n");
             }
         }
         Thread::wait(100);
@@ -79,21 +86,24 @@
     // unreachable INFO_LOG ("send_thread end\n");
 }
 
+
 osThreadDef(send_thread,  osPriorityNormal, DEFAULT_STACK_SIZE);
 
 void HttpSend::init(EthernetInterface* eth){
     INFO_LOG ("HttpSend::init start\n");
     m_ethernet = eth;
+    m_ethernet->connect();
+    int ret = m_sock.connect(SERVER_HOST, SERVER_HOST_PORT);  
     m_send_thread = osThreadCreate(osThread(send_thread), NULL);
     INFO_LOG ("HttpSend::init end\n");
 }
 
 message_t* HttpSend::getMessageBuffer(){
-    m_mpCnt++;
-    return m_mpool.alloc();
+    return getBuffer();
 }
 
 void HttpSend::send(message_t* message){
+    m_queueCnt++;
     m_queue.put(message);
 }