this is using the mbed os version 5-13-1

Dependencies:   mbed-http

source/ATCmdManager.cpp

Committer:
ocomeni
Date:
2019-03-31
Revision:
88:7ffa053be662
Parent:
87:99b37d26ff2a
Child:
89:45f6db09a76d

File content as of revision 88:7ffa053be662:

#include "ATCmdManager.h"
#include "common_config.h"
#include "common_types.h"
#include "http_request.h"
//#include "mbed_memory_status.h"

ATCmdManager::ATCmdManager(PinName tx, PinName rx, SMDevicePeripheral *blePeripheral, 
                           events::EventQueue &event_queue, WiFiManager *wifi, 
                           MemoryPool<wifi_cmd_message_t, 16> *aT2WiFimPool, 
                           Queue<wifi_cmd_message_t, 16>      *aT2WiFiCmdQueue, 
                           MemoryPool<at_resp_message_t, 16>  *wiFi2ATmPool, 
                           Queue<at_resp_message_t, 16>       *wiFi2ATCmdQueue, 
                           MemoryPool<wifi_data_msg_t, PQDSZ>     *aT2WiFiDatamPool, 
                           Queue<wifi_data_msg_t, PQDSZ>          *aT2WiFiDataQueue, 
                           MemoryPool<at_data_msg_t, PQDSZ>       *wiFi2ATDatamPool, 
                           Queue<at_data_msg_t, PQDSZ>            *wiFi2ATDataQueue, 
                           bool debug)
    :
    _serial(tx, rx, DEFAULT_BAUD_RATE),
    blePeripheral(blePeripheral),
    _event_queue(event_queue),
    wiFiManager(wiFiManager),
    _aT2WiFimPool(aT2WiFimPool),
    _aT2WiFiCmdQueue(aT2WiFiCmdQueue),
    
    _wiFi2ATmPool(wiFi2ATmPool),
    _wiFi2ATCmdQueue(wiFi2ATCmdQueue),
    
    _aT2WiFiDatamPool(aT2WiFiDatamPool),
    _aT2WiFiDataQueue(aT2WiFiDataQueue),
    
    _wiFi2ATDatamPool(wiFi2ATDatamPool),
    _wiFi2ATDataQueue(wiFi2ATDataQueue),
    
    _parser(&_serial)
    

{ 
// constructor
    _serial.set_baud(DEFAULT_BAUD_RATE);
    _parser.debug_on(debug);
    _parser.set_delimiter("\r\n");
    _parser.send("+STARTUP");
    _parser.oob("AT\n", callback(this, &ATCmdManager::_oob_ok_hdlr));
    _parser.oob("ATE0", callback(this, &ATCmdManager::_oob_echo_off));
    _parser.oob("ATE1", callback(this, &ATCmdManager::_oob_echo_on));
    _parser.oob("AT+UMRS", callback(this, &ATCmdManager::_oob_uart_setup));
    
    _parser.oob("ATO", callback(this, &ATCmdManager::_oob_data_mode));
    _parser.oob("AT+UMLA", callback(this, &ATCmdManager::_oob_get_mac_addr));
    _parser.oob("AT+UBTLE?", callback(this, &ATCmdManager::_oob_get_ble_role));
    _parser.oob("AT+UBTLE=2", callback(this, &ATCmdManager::_oob_ena_ble_peri));
    _parser.oob("AT+CPWROFF", callback(this, &ATCmdManager::_oob_reboot));
    _parser.oob("AT+CGMR", callback(this, &ATCmdManager::_oob_get_fw_ver));
    _parser.oob("AT+UWSCAN", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
    _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_connect2WiFiNetwork));
    _parser.oob("AT+UWSCD=", callback(this, &ATCmdManager::_oob_disconnectWiFiNetwork));
    _parser.oob("AT+UDDRP", callback(this, &ATCmdManager::_oob_setupInternetConnection));
    _parser.oob("AT+UWSC=0,2", callback(this, &ATCmdManager::_oob_setWiFiSSID));
    _parser.oob("AT+UWSC=0,8", callback(this, &ATCmdManager::_oob_setWiFiPWD));
    _parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_setWiFiSecurity));
    //_parser.oob("AT+UWSC=0,5", callback(this, &ATCmdManager::_oob_sendHttpMessage));
 
//AT+UWSC=0,2,<SSID>
//AT+UWSC=0,8,<PWD>
//AT+UWSC=0,5,2 (WPA)
    //_parser.oob("ATE0", callback(this, &ATCmdManager::_oob_startup_hdlr));
    printf("\n --- ATCmdManager constructor completed ---\n");
    at_resp = AT_RESP_NONE;
    dataMode = AT_CMD_DATA_MODE;
    event_queue.call_every(120000,&print_memory_info);
    //AT+UMRS=230400,2,8,1,1,1
    // AT+UBTLE
}


void ATCmdManager::runMain(){
    printf("\r\n [ATCMD MAN]  Thread Id = %d\r\n", Thread::gettid());
    while(true){
        _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true);
        wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE/WiFi some time 
        processResponses();
    }
    
}

void ATCmdManager::processResponses(){
        dequeueATresponse();
        dequeueWiFidataResponse();
        switch(at_resp){
            case AT_RESP_NONE:
                // IDLE response state
                break;
            case AT_SCAN_RESP:
                // AT_SCAN_RESP response state
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI SCAN RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            case AT_DETAILED_SCAN_RESP:
                // AT_DETAILED_SCAN_RESP response state
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI DETAILED SCAN RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            case AT_CONNECT_RESP:  
                // AT_CONNECT_RESP response state 
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI CONNECT RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            case AT_DISCONNECT_RESP:  
                // AT_DISCONNECT_RESP response state 
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI DISCONNECT RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            case AT_CONFIG_RESP:  
                // AT_CONFIG_RESP response state 
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI CONFIG RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            case AT_INTERNET_CONFIG_RESP:  
                // AT_CONFIG_RESP response state 
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI INTERNET_CONFIG RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            case AT_HTTPS_RESP:  
                // AT_HTTP_RESP response state 
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI HTTPS RESPONSE RECEIVED!!\n");
                return_response();
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            case AT_HTTP_RESP:  
                // AT_HTTP_RESP response state 
                //////_smutex.lock();
                printf("\n [ATCMD MAN] WIFI HTTP RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
            default:
                // UNKNOWN response state
                //////_smutex.lock();
                printf("\n [ATCMD MAN] UNKNOWN RESPONSE RECEIVED!!\n");
                _parser.send("OK\n");
                //////_smutex.unlock();
                at_resp = AT_RESP_NONE;
                break;
        }
}


bool ATCmdManager::validate(edm_header_t edm_header)
{
    if(edm_header.startByte != EDM_START_BYTE)return false; // unexpected start byte found!
    if(edm_header.payloadID != CONNECT_EVENT &&
       edm_header.payloadID != DISCONNECT_EVENT &&
       edm_header.payloadID != DATA_EVENT &&
       edm_header.payloadID != DATA_COMMAND &&
       edm_header.payloadID != AT_REQUEST &&
       edm_header.payloadID != AT_CONFIRMATION &&
       edm_header.payloadID != AT_EVENT 
       )return false; // unexpected payload ID found!
    if(edm_header.payloadLen > MAX_EDM_PAYLOAD_LEN ||
       edm_header.payloadLen < MIN_EDM_PAYLOAD_LEN 
       ) return false; // unexpected length received!
    return true;
}

http_method ATCmdManager::str2HttpMethod(const char * methodStr)
{
    http_method _http_method;
    if(strstr(methodStr, "POST")!= NULL){
        _http_method = HTTP_POST;
    }
    else if(strstr(methodStr, "GET")!= NULL){
        _http_method = HTTP_GET;
    }
    else{
        _http_method = HTTP_DELETE; // unsupported method - set to HTTP_DELETE
    }
    return _http_method;
}
bool ATCmdManager::createHttpRequest()
{
    http_request_t http_req;
    printf("\nsizeof(http_req) on creation = %d\n", sizeof(http_req));

    char s1[32];
    char s2[32];
    char s3[32];
    int n = 0;
    int pos = 0;
    char * strPtr = (char *)&rx_buf_ptr[1];
    char * p;
    char * p2 = strstr(strPtr, "\r\n\r\n");
    char * nxtPtr = strPtr;
    char * bodyPtr = p2+4;
    printf("\nstrPtr address= %x",strPtr);
    printf("\np2 address= %x", p2);
    for(int i = 0; i < 5; i++){
        if(i == 0)// firstline scan method uri and http_ver
        {
            //n = sscanf(nxtPtr,"%s %s %s", &s1[0], &s2[0], &s3[0]);
            n = sscanf(nxtPtr,"%s %s %s", s1, s2, s3);
            printf("\nmethod = %s\nuri = %s\nhttp_ver = %s",s1, s2, s3 );
            //http_req.method = str2HttpMethod(s1.c_str());
            http_req.method = str2HttpMethod(s1);
            http_req.request_URI.assign(s2);
            http_req.http_version.assign(s3);
            printf("\nhttp_req.method = %d \n", http_req.method);
        }
        else{ // scan header pairs
            //n = sscanf(nxtPtr,"%s %s", &s1[0], &s2[0]);
            n = sscanf(nxtPtr,"%s %s", s1, s2);
            p = strstr(s1,":");
            *p = NULL;
            //pos = s1.find(":");
            //s1.replace(pos,1,"");
            printf("\nname = %s value = %s",s1, s2);
            //if(s1.find("Host")!= string::npos){
            if(strstr(s1, "Host")!= NULL){
                http_req.hostName.assign(s2);
            }
            else if(strstr(s1, "Accept")!= NULL){
                http_req.AcceptVal.assign(s2);
            }
            else if(strstr(s1, "Content-Type")!= NULL){
                http_req.contentType.assign(s2);
            }
            else if(strstr(s1, "Content-Length")!= NULL){
                http_req.contentLen.assign(s2);
            }
            
            
        }
        nxtPtr = strstr(nxtPtr, "\r\n")+2; // goto next line
        if(nxtPtr >= p2) break;
    }
    int bodyLen = edm_hdr.payloadLen -(p2+7-strPtr);
    printf("\nLen = %d\n", bodyLen);
    memcpy(http_req.body, bodyPtr, bodyLen);
    // package and send on wifi data queue
    wifi_data_msg_t data_msg;
    data_msg.wifi_cmd = WIFI_CMD_SEND_HTTPS_REQ;
    printf("\nsizeof(http_req) on population = %d\n", sizeof(http_req));

    data_msg.dataLen = sizeof(http_req.method) + 
                       sizeof(http_req.body) + 
                       http_req.request_URI.capacity() +
                       http_req.http_version.capacity() + 
                       http_req.hostName.capacity() +
                       http_req.AcceptVal.capacity() +
                       http_req.contentType.capacity() +
                       http_req.contentLen.capacity();

/*
    data_msg.dataLen = sizeof(http_req.method) + 
                       sizeof(http_req.body) + 
                       http_req.request_URI.size() +
                       http_req.http_version.size() + 
                       http_req.hostName.size() +
                       http_req.AcceptVal.size() +
                       http_req.contentType.size() +
                       http_req.contentLen.size();
*/
    memcpy(data_msg.buffer,&http_req, data_msg.dataLen);
    // queue next data request
    queueWiFiDataRequest(data_msg);
    return true;
}
// OOB processing
void ATCmdManager::_process_oob(uint32_t timeout, bool all){
    set_timeout(timeout);
    static int cnt = 0;
    int start;
    if(dataMode == AT_EXT_DATA_MODE)
    {
        int n;
        //if(cnt++ % 10 == 0)printf("In EDM mode\n");
        uint8_t edm[EDM_HDR_LEN];
        
        // Poll for edm packets
        do{
            n = _parser.read((char *)edm, EDM_HDR_LEN);
            edm_hdr.startByte = edm[0];
            edm_hdr.payloadLen = edm[1]*256 + edm[2];
            edm_hdr.payloadID = edm[3]*256 + edm[4];
            start = Kernel::get_ms_count();
            if(n == -1) break; // break if it times out
            printf("%d bytes read!\n", n);
            if(n==5)
            printf("Start = %d, payloadID = %d len = %d\n", edm_hdr.startByte, 
                                                            edm_hdr.payloadID, 
                                                            edm_hdr.payloadLen);
            if(n == EDM_HDR_LEN && validate(edm_hdr)) // if AT command use process oob to decode
            {
                if(edm_hdr.payloadID == AT_REQUEST)
                {
                    _parser.process_oob();
                    break;
                }
                else
                {
                    int pLen = edm_hdr.payloadLen-1;
                    rx_buf_ptr = (uint8_t *) malloc (pLen); // we already read 2 bytes from payload but expect 1 stop byte
                    rx_buf_ptr[pLen-1] = 0x00; // clear last byte so the readback value is as expected
                    n = _parser.read((char *)rx_buf_ptr, pLen);
                    if(n == -1) break; // timeout!
                    printf("%d bytes read - expected %d!\n", n, pLen);
                    for(int i =0;i<pLen;i++){
                        if(i%8==0) printf("\n[%3d]",i/8);
                        printf("%02x ", rx_buf_ptr[i]);
                    }
                    printf("\n");
                    printf("rx_buf_ptr[pLen-1] = %0x\n",rx_buf_ptr[pLen-1]);
                    if(rx_buf_ptr[pLen-1] != EDM_STOP_BYTE) {
                        break; // exit if stop byte now found - possible data corruption!
                    }
                    switch(edm_hdr.payloadID)
                    {
                        case CONNECT_EVENT:
                            printf("Connection Event received!\n");
                            break;
                        case DISCONNECT_EVENT:
                            printf("DISCONNECT_EVENT received!\n");
                            break;
                        case DATA_EVENT:
                            printf("DATA_EVENT received!\n");
                            break;
                        case DATA_COMMAND:
                            printf("DATA_COMMAND received!\n");
                            if(createHttpRequest() == true){
                                _parser.send("OK");
                            }
                            else{
                                _parser.send("NACK");
                            }
                            int stop = Kernel::get_ms_count();
                            printf("\n Time Elapsed = %d\n", stop-start);
                            break;
                        case AT_REQUEST:
                            printf("AT_REQUEST received!\n");
                            break;
                        case AT_CONFIRMATION:
                            printf("AT_CONFIRMATION received!\n");
                            break;
                        case AT_EVENT:
                            printf("AT_EVENT received!\n");
                            break;
                        default:
                            printf("UNKNOWN MESSAGE received!\n");
                            break;
                    }
                }
                //_parser.process_oob();
            }
            else // incorrect # of bytes received abort!!
            {
                break;
            }
        }while (all);  // continue to process until timeout
    }
    else
    {
        // Poll for inbound packets
        while (_parser.process_oob() && all) {
        }
    }        
    set_timeout();
}


// OOB message handlers
void ATCmdManager::_oob_startup_hdlr(){
}

void ATCmdManager::_oob_ok_hdlr(){
    //_smutex.lock();
    _parser.send("OK\n");
    //_smutex.unlock();
}


void ATCmdManager::_oob_bleRole_hdlr(){
}


void ATCmdManager::_oob_wifiMode_err(){
}


void ATCmdManager::_oob_conn_already(){
}


void ATCmdManager::_oob_err(){
}

void ATCmdManager::_oob_get_fw_ver()
{
}


void ATCmdManager::_oob_uart_setup(){
    int uOpts[NUM_UART_OPTIONS];
    //if(_parser.recv("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5])) {
    if(_parser.scanf("=%d,%d,%d,%d,%d,%d", &uOpts[0], &uOpts[1], &uOpts[2], &uOpts[3], &uOpts[4], &uOpts[5]) >0) {
        printf("\nATCmdParser: Uart Options=%d,%d,%d,%d,%d,%d\n", uOpts[0], uOpts[1], uOpts[2], uOpts[3], uOpts[4], uOpts[5]);
       //AT+UMRS=230400,2,8,1,1,1
        printf("\n Changing Baud Rate to %d\n", uOpts[0]);

        _serial.set_baud(uOpts[0]);
        printf("\n Baud Rate now %d\n", uOpts[0]);

    } else {
        printf("\nATCmdParser: Retrieving Uart Options failed");
    }
}

void ATCmdManager::set_timeout(uint32_t timeout_ms)
{
    _parser.set_timeout(timeout_ms);
}


void ATCmdManager::_oob_echo_off()
{
    //_smutex.lock();
    printf("\n Received ATEO OOB command!!\n");
    printf("\n turning echo OFF!!\n");
    _parser.debug_on(false);
    _parser.send("OK\n");
    //_smutex.unlock();
}


void ATCmdManager::_oob_echo_on()
{
    //_smutex.lock();
    printf("\n Received ATE1 OOB command!!\n");
    printf("\n turning echo ON!!\n");
    _parser.debug_on(true);
    _parser.send("OK\n");
    //_smutex.unlock();
}


void ATCmdManager::_oob_data_mode(){
    //_smutex.lock();
    printf("\n Received EDM mode command!!\n");
    if(_parser.scanf("%d", &dataMode) >0) {
        printf("\nATCmdParser: Data mode=%d\n", dataMode);
        switch(dataMode)
        {
            case 0:
                printf("\nATCmdParser: Command Mode request received\n");
                dataMode = AT_CMD_DATA_MODE;
                break;
            case 1:
                printf("\nATCmdParser: Data Mode request received\n");
                dataMode = AT_STD_DATA_MODE;
                break;
            case 2:
                printf("\nATCmdParser: Extended data Mode request received\n");
                dataMode = AT_EXT_DATA_MODE;
                break;
            default:
                printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
                break;
        }
    } else {
        printf("\nATCmdParser: Retrieving Uart Options failed\n");
    }
    _parser.send("OK\n");
    //_smutex.unlock();
}

void ATCmdManager::_oob_get_mac_addr(){
    int bleOrWifi;
    //_smutex.lock();
    if(_parser.scanf("=%d", &bleOrWifi) >0) {
        switch(bleOrWifi)
        {
            case 1:
                printf("\nATCmdParser: BLE MAC Address request received");
                break;
            case 2:
                printf("\nATCmdParser: WiFi MAC Address request received");
                break;
            default:
                printf("\nATCmdParser: ERROR - UNKNOWN MAC ADDRESS REQUEST RECEIVED!!! \n");
                break;
        }
    } else {
        printf("\nATCmdParser: Retrieving Uart Options failed");
    }
    _parser.send("OK\n");
    //_smutex.unlock();
}

void ATCmdManager::_oob_get_ble_role(){
    //_smutex.lock();
    printf("\n Received get BLE role command!!\n");
    _parser.send("OK\n");
    //_smutex.unlock();
}

void ATCmdManager::_oob_ena_ble_peri(){
    //_smutex.lock();
    printf("\n Received enable BLE Peripheral command!!\n");
    _parser.send("OK\n");
    //_smutex.unlock();
}

void ATCmdManager::_oob_reboot(){
    //_smutex.lock();
    printf("\n Received reboot command!!\n");
    _parser.send("OK\n");
    _parser.send("System Resetting....\n");
    system_reset();
    //_smutex.unlock();
}

const char * ATCmdManager::sec2str(nsapi_security_t sec)
{
    switch (sec) {
        case NSAPI_SECURITY_NONE:
            return "None";
        case NSAPI_SECURITY_WEP:
            return "WEP";
        case NSAPI_SECURITY_WPA:
            return "WPA";
        case NSAPI_SECURITY_WPA2:
            return "WPA2";
        case NSAPI_SECURITY_WPA_WPA2:
            return "WPA/WPA2";
        case NSAPI_SECURITY_UNKNOWN:
        default:
            return "Unknown";
    }
}

bool ATCmdManager::setNextResponse(at_cmd_resp_t resp)
{
     if(at_resp == AT_RESP_NONE){
        at_resp = resp;
        return true; // success
    }
    return false; // wiFiManager busy
}


void ATCmdManager::_oob_scanWiFiNetworks(){
    //_smutex.lock();
    printf("\n Received scanWiFiNetworks command!!\n");
    _parser.send("OK\n");
    //_smutex.unlock();
    wifi_cmd_t cmd = WIFI_CMD_SCAN;
    // queue next command
    queueWiFiCommand(cmd);
    return;    
}

void ATCmdManager::_oob_connect2WiFiNetwork()
{
    wifi_cmd_t cmd = WIFI_CMD_CONNECT;
    // queue next command
    queueWiFiCommand(cmd);
    return;
}


void ATCmdManager::_oob_disconnectWiFiNetwork()
{
    wifi_cmd_t cmd = WIFI_CMD_DISCONNECT;
    // queue next command
    queueWiFiCommand(cmd);
    return;
}

void ATCmdManager::_oob_setupInternetConnection()
{
    char str[200];
    char url[60];
    int n;
    internet_config_t internet_config;
    //_smutex.lock();
    printf("sizeof internet_config_t = %d\n", sizeof(internet_config_t));
    n = ReadBytes((uint8_t *)str, 199);
    str[n]=NULL;
    printf("\n read string = %s , n = %d\n", str, n);
    n = sscanf(str, "=%1d,%199[^,],%1d", &internet_config.peer_id, 
                                      url,
                                     &internet_config.connectionScheme);
    strncpy(internet_config.url, url, strlen(url)+1);
    printf("\n read string = %s , n = %d -- strlen(url) = %d\n", internet_config.url, n, strlen(url));
    if(n>0) 
    {
        printf("peer_id = %1d, url = %s, connScheme = %1d\n", internet_config.peer_id, 
                                                          internet_config.url, ///->c_str(), 
                                                          internet_config.connectionScheme);
        // package and send on wifi data queue
        wifi_data_msg_t data_msg; // = (wifi_data_msg_t *) malloc(sizeof(wifi_data_msg_t));
        data_msg.wifi_cmd = WIFI_CMD_INTERNET_CONFIG;
        data_msg.dataLen = sizeof(internet_config_t); // + strlen(internet_config.url);
        printf("\n url size = %d url str = %s\n",strlen(internet_config.url), internet_config.url );
        memcpy(data_msg.buffer,&internet_config, data_msg.dataLen);
        // queue next data request
        queueWiFiDataRequest(data_msg);
        print_memory_info();
        _parser.send("OK\n");
    } else {
        printf("\n[ATCMD MAN]: internet configuration failed %d fields parsed \n", n);
        _parser.send("NAK\n");
    }
    //_smutex.unlock();
}

wifi_config_t ATCmdManager::init_wifi_config()
{ 
    wifi_config_t wifi_cfg;
    wifi_cfg.ssid[0]  = NULL;
    wifi_cfg.pass[0]  = NULL;
    wifi_cfg.security = NSAPI_SECURITY_UNKNOWN;
    return wifi_cfg;    
}
/*  read ASCII characters into buffer and null terminate */
int ATCmdManager::readStringBytes(uint8_t *buf, int maxBytes)
{ 
    int c;
    int sptr = 0;
    for(int i=0;i<maxBytes;i++){
        c = _parser.getc();
        if(c==-1){
            buf[sptr] = NULL; // null terminate if string
            return i;
        }
        if(c != ',' && c!= '"'){
            buf[sptr++] = (uint8_t) c;
        }
    }
    return maxBytes;
}

int ATCmdManager::ReadBytes(uint8_t *buf, int maxBytes)
{ 
    int c;
    int sptr = 0;
    for(int i=0;i<maxBytes;i++){
        c = _parser.getc();
        if(c==-1){
            return i;
        }
        buf[sptr++] = (uint8_t) c;
    }
    return maxBytes;
}

void ATCmdManager::_oob_setWiFiSSID()
{
    int n;
    wifi_config_t wifi_cfg = init_wifi_config();
    //_smutex.lock();
    n = readStringBytes((uint8_t *)wifi_cfg.ssid, 32);
    printf("[ATCMD MAN]: number of bytes read = %d\n", n);
    if(n>0) 
    {
        printf("[ATCMD MAN]: wifi_cfg.ssid = %s\n", wifi_cfg.ssid);
        // package and send on wifi data queue
        wifi_data_msg_t data_msg;
        data_msg.wifi_cmd = WIFI_CMD_CONFIG;
        data_msg.dataLen = sizeof(wifi_config_t);
        memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
        queueWiFiDataRequest(data_msg);
        _parser.send("OK\n");
    } else {
        printf("\n[ATCMD MAN]: wifi configuration failed  \n");
        _parser.send("NAK\n");
    }
    //_smutex.unlock();
}

void ATCmdManager::_oob_setWiFiPWD()
{
    int n;
    wifi_config_t wifi_cfg = init_wifi_config();
    //_smutex.lock();
    //n = _parser.scanf("%31[^\r\n]", wifi_cfg.pass);
    n = readStringBytes((uint8_t *)wifi_cfg.pass, 32);
    if(n>0) 
    {
        printf("ATCMD MAN]: wifi_cfg.pass = %s\n", wifi_cfg.pass);
        // package and send on wifi data queue
        wifi_data_msg_t data_msg;
        data_msg.wifi_cmd = WIFI_CMD_CONFIG;
        data_msg.dataLen = sizeof(wifi_config_t);
        memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
        queueWiFiDataRequest(data_msg);
        _parser.send("OK\n");
    } else {
        printf("\n[ATCMD MAN]: wifi configuration failed  \n");
        _parser.send("NAK\n");
    }
    //_smutex.unlock();
}

void ATCmdManager::_oob_setWiFiSecurity()
{
    int n;
    wifi_config_t wifi_cfg = init_wifi_config();
    //_smutex.lock();
    n = _parser.scanf(",%d", &wifi_cfg.security);
    if(n>0) 
    {
        printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
        // package and send on wifi data queue
        wifi_data_msg_t data_msg;
        data_msg.wifi_cmd = WIFI_CMD_CONFIG;
        data_msg.dataLen = sizeof(wifi_config_t);
        memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
        queueWiFiDataRequest(data_msg);
        _parser.send("OK\n");
    } else {
        printf("\n[ATCMD MAN]: wifi configuration failed  \n");
        _parser.send("NAK\n");
    }
    //_smutex.unlock();
}


void ATCmdManager::_oob_sendHttpMessage()
{
    int n;
    http_request_t http_post_request;
    //_smutex.lock();
    /*
    n = _parser.scanf(",%d", &wifi_cfg.security);
    if(n>0) 
    {
        printf("ATCMD MAN]: wifi_cfg.security = %s\n", sec2str(wifi_cfg.security));
        // package and send on wifi data queue
        wifi_data_msg_t data_msg;
        data_msg.wifi_cmd = WIFI_CMD_CONFIG;
        data_msg.dataLen = sizeof(wifi_config_t);
        memcpy(data_msg.buffer,&wifi_cfg, data_msg.dataLen);
        queueWiFiDataRequest(data_msg);
        _parser.send("OK\n");
    } else {
        printf("\n[ATCMD MAN]: wifi configuration failed  \n");
        _parser.send("NAK\n");
    }
    */
    //_smutex.unlock();

}

bool  ATCmdManager::queueWiFiCommand(wifi_cmd_t cmd){
    wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
    wifiCmd->wifi_cmd            = cmd;
    _aT2WiFiCmdQueue->put(wifiCmd);
    return true;
}

bool  ATCmdManager::dequeueATresponse(){
    if(at_resp != AT_RESP_NONE) return false; // busy
    osEvent evt = _wiFi2ATCmdQueue->get(0);
    if(evt.status == osEventMessage){
        at_resp_message_t *resp = (at_resp_message_t*)evt.value.p;
        setNextResponse(resp->at_resp);
        _wiFi2ATmPool->free(resp);
    }
    return true;
}

bool  ATCmdManager::queueWiFiDataRequest(wifi_data_msg_t data_req){
    wifi_data_msg_t *wifiData = _aT2WiFiDatamPool->alloc();
    wifiData->wifi_cmd        = data_req.wifi_cmd;
    wifiData->dataLen        = data_req.dataLen;
    memcpy(wifiData->buffer, data_req.buffer, data_req.dataLen);
    _aT2WiFiDataQueue->put(wifiData);
    printf("[ATCMD MAN] queued data size = %d : wifi_cmd = %d\n", data_req.dataLen, data_req.wifi_cmd);
    //free(&data_req);
    return true;
}

bool  ATCmdManager::dequeueWiFidataResponse(){
    if(at_resp != AT_RESP_NONE) return false; // busy
    osEvent evt = _wiFi2ATDataQueue->get(0);
    if(evt.status == osEventMessage){
        resp_data = (at_data_msg_t*)evt.value.p;
        setNextResponse(resp_data->at_resp);
        //_wiFi2ATDatamPool->free(resp_data);
    }
    return true;
}


void ATCmdManager::return_response() {
    char * resp = (char *) resp_data->buffer;
    printf("\n[ATCMD MAN] received response:\n");
    printf("%s\r\n", (char *)resp);
    _parser.write(resp, resp_data->dataLen);
    _wiFi2ATDatamPool->free(resp_data);
    
    
}