Ethernet+BLE prototype

Dependencies:   mbed EthernetInterface mbed-rtos

HttpSend.cpp

Committer:
teruo
Date:
2014-03-28
Revision:
4:78e96198c879
Parent:
3:d9445b9e7163
Child:
5:9fffeb949e5a

File content as of revision 4:78e96198c879:

#include "config.h"
#include "log.h"
#include "HttpSend.h"
#include "mbed.h"


MemoryPool<message_t, 64>   m_mpool;
Queue<message_t, 64>        m_queue;

int m_mpCnt=0;

EthernetInterface* m_ethernet;

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();
}

    
//    Thread::wait(3000);
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;
            if(message!=NULL){
                DEBUG_LOG("(size=%d)(m_mpCnt=%d):%s",message->size,m_mpCnt,message->data);
//                m_ethernet->connect();
//                INFO_LOG ("IP Address is %s \n", m_ethernet->getIPAddress());
//                sendData(message->data, message->size);
//                m_ethernet->disconnect();
                m_mpool.free(message);
                m_mpCnt--;
//                DEBUG_LOG ("*message=0x%08X free \n",message);
            }
        }
        Thread::wait(100);
    }
        
    // 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_send_thread = osThreadCreate(osThread(send_thread), NULL);
    INFO_LOG ("HttpSend::init end\n");
}

message_t* HttpSend::getMessageBuffer(){
    m_mpCnt++;
    return m_mpool.alloc();
}

void HttpSend::send(message_t* message){
    m_queue.put(message);
}