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

Dependencies:   mbed-http

source/ATCmdManager.cpp

Committer:
ocomeni
Date:
2019-03-20
Revision:
79:a2187bbfa407
Parent:
78:07bb86e3ce14
Child:
80:e8f0e92e3ac9

File content as of revision 79:a2187bbfa407:

#include "ATCmdManager.h"
#include "common_config.h"
#include "common_types.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, 
                           bool debug)
    :
    _serial(tx, rx, DEFAULT_BAUD_RATE),
    _parser(&_serial),
    blePeripheral(blePeripheral),
    _event_queue(event_queue),
    wiFiManager(wiFiManager),
    _aT2WiFimPool(aT2WiFimPool),
    _aT2WiFiCmdQueue(aT2WiFiCmdQueue)

{ 
// constructor
    _serial.set_baud(DEFAULT_BAUD_RATE);
    _parser.debug_on(debug);
    _parser.set_delimiter("\r\n");
    _parser.send("+STARTUP");
    _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+UWSC=", callback(this, &ATCmdManager::_oob_scanWiFiNetworks));
    _parser.oob("AT+UWSCA=", callback(this, &ATCmdManager::_oob_connect2WiFiNetwork));
    

    //_parser.oob("ATE0", callback(this, &ATCmdManager::_oob_startup_hdlr));
    printf("\n --- ATCmdManager constructor completed ---\n");
    
    //AT+UMRS=230400,2,8,1,1,1
    // AT+UBTLE
}


void ATCmdManager::runMain(){
    while(true){
        _process_oob(UBLOX_ODIN_W2_RECV_TIMEOUT, true);
        wait_ms(MAIN_LOOP_WAIT_TIME_MS); // allow BTLE to be
    }
    
}


// OOB processing
void ATCmdManager::_process_oob(uint32_t timeout, bool all){
    set_timeout(timeout);
    // Poll for inbound packets
    while (_parser.process_oob() && all) {
    }
    set_timeout();
}


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


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]);
    } 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(){
    int dataMode;
    _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");
                break;
            case 1:
                printf("\nATCmdParser: Data Mode request received");
                break;
            case 2:
                printf("\nATCmdParser: Extended data Mode request received");
                break;
            default:
                printf("\nATCmdParser: ERROR - UNKNOWN DATA MODE RECEIVED!!! \n");
                break;
        }
    } else {
        printf("\nATCmdParser: Retrieving Uart Options failed");
    }
    _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::queueWiFiCommand(wifi_cmd_t cmd){
    wifi_cmd_message_t *wifiCmd = _aT2WiFimPool->alloc();
    wifiCmd->wifi_cmd            = cmd;
    _aT2WiFiCmdQueue->put(wifiCmd);
    return true;
}

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);
    bool success;
    success = wiFiManager->setNextCommand(cmd);
    if(success){
        printf("\n scan command successfully sent to wiFiManager!!\n");
    }
    else {
        printf("\n ERROR: Failed to send scan command wiFiManager!!\n");
    }
    return;
    /* call WiFi Scan in 20 ms */
    //_event_queue.call_in(
    //    20, wiFiManager,
    //    &WiFiManager::scanNetworks);
    //return;
    WiFiAccessPoint       *ap;
    nsapi_size_or_error_t count;
    //count = wiFiManager->scanNetworks();
    if (count <= 0) {
        _smutex.lock();
        printf("scan() failed with return value: %d\n", count);
        _smutex.unlock();
        return;
    }
    /* Limit number of network arbitrary to 15 */
    count = count < 15 ? count : 15;
    ap = new WiFiAccessPoint[count];
    count = wiFiManager->getAvailableAPs(ap, count);
    if (count <= 0) {
        printf("scan() failed with return value: %d\n", count);
        return;
    }

    for (int i = 0; i < count; i++) {
        printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
    }
    printf("%d networks available.\n", count);

    delete[] ap;
    
}

void ATCmdManager::_oob_connect2WiFiNetwork()
{
    wiFiManager->connect();
}


void ATCmdManager::_oob_disconnectWiFiNetwork()
{
    wiFiManager->disconnect();
}