Mwadime Makokha / Mbed 2 deprecated BlueToothSIM868

Dependencies:   mbed

BT.cpp

Committer:
MwadimeMakokha
Date:
2020-01-08
Revision:
4:6f32ed1480a4
Parent:
3:936c6d909891

File content as of revision 4:6f32ed1480a4:

#include "BT.h"
Serial* BT::modemSerial =0;
BT::BT(Serial &port){
    modemSerial = &port;
    //Test if the modem is good
    send_bt_cmd("ATE0");
    get_response(1000,false);
    DEBUG_PRINTLN("%s",modem_response);
    //Power the device and unpair all devices
    power_bt_device(1);
    bt_unpair_device(); 
}
/*
@brief Get the bluetooth status -> The modem response contains the status
*/
void BT::get_bt_status(){
    send_bt_cmd("AT+BTSTATUS?");
    get_response(1000,true);
    DEBUG_PRINTLN("%s",modem_response);
}

/*
@brief power the bt funtionality on/off depending on the params passed
@params bool on/off -> determines if bt powered on or off
*/
void BT::power_bt_device(bool on){
     if(on)send_bt_cmd("AT+BTPOWER=1");
     else send_bt_cmd("AT+BTPOWER=0");
    get_response(1000,false);
    DEBUG_PRINTLN("%s",modem_response);
}

/*
@brief scan for the devices for the time passed in the params and gets the phone id from the mac_address
@params waiting time for scanning
@retval phone id
*/
char BT::bt_scan(uint32_t timeout){
     char id;
     if(timeout>60000){
    int cmd_rounds = timeout/10000;
    for(int k=0;k<cmd_rounds;k++){
    send_bt_cmd("AT+BTSCAN=1,10");
    get_response(10000,true);  
    DEBUG_PRINTLN("%s",modem_response);
    char* mac_address = get_phone_mac_addr();
     id = get_phone_id(mac_address); 
     if(id != '0') break ;
    } 
    }
    return id;
}

/*
@brief pair with device of the id given 
@params id of the phone to be paired
*/
void BT::bt_pair_device(char id){
    DEBUG_PRINTLN("ID returned");
    modemSerial->printf("AT+BTPAIR=0,%c\n\r",id);
    get_response(5000,true);
    DEBUG_PRINTLN("%s",modem_response);
    char* passkey = get_phone_passkey();
    if(passkey != "error"){
        send_bt_cmd("AT+BTPAIR=1,1");
        get_response(3000,true);
    }
    else{
        DEBUG_PRINTLN("Error");
    }    
}

/*
@brief provide mac address of the bt phone 
@retval mac_address
*/
char* BT::get_phone_mac_addr(){
    //TODO: Get mac address from MQTT event
    return mac_addr_1;
}

/*
@brief get the device bt id
@params mac_address of the device 
@retval device bt id
*/
char BT::get_phone_id(char* addr){
    char id=0;
    bool id_found=false;
    char* dev_addr;
    int comma_count =0;
    int arr_index =0;
   while(modem_response[arr_index] != 0){
    if(comma_count==1)id = modem_response[arr_index++];
    if(comma_count==3){
        for(int k=0;k<17;k++){
        dev_addr[k] = modem_response[arr_index+k];    
        }
        if(strcmp(dev_addr,addr) == 0){
            DEBUG_PRINTLN("id = %c",id);
            id_found = true;
            break;
            }
        }
    if(modem_response[arr_index]==',')comma_count++;
    if(modem_response[arr_index]=='\n')comma_count=0;
    arr_index++;
    }
    return id_found?id:'0';
}

/*
@brief get the passkey during pairing
@retval the pairing passkeys
*/
char* BT::get_phone_passkey(){
    int arr_index = 0;
    char* passkey=new char[7];
    bool passkey_found=false;
    int comma_count = 0;
    while(modem_response[arr_index] !=0){
        if(comma_count == 2){
            int i =0;
            while(modem_response[arr_index+i] != 0){
                passkey[i] = modem_response[arr_index+i];
                i++;
            }
            passkey[i] = 0;
            passkey_found = true;
            break;
        }
        if(modem_response[arr_index]==',')comma_count++;
        arr_index++;
    }
    if(passkey_found){
        return passkey;
        }
    DEBUG_PRINTLN("Error");
    return "error";
}

/*
@brief unpair bt device
*/
void BT::bt_unpair_device(){
    send_bt_cmd("AT+BTUNPAIR=0");
    get_response(1000,false);
}

/*
@brief connect to bt device
*/
void BT::bt_connect_device(char id){
    modemSerial->printf("AT+BTCONNECT=%c,10\n\r",id);
     get_response(2000,true);
     DEBUG_PRINTLN("%s",modem_response);
 }

/*
@brief disconnect bt device
*/
void BT::bt_disconnect_device(char id){
    modemSerial->printf("AT+BTDISCONN=%c",id);
   get_response(2000,true);
    DEBUG_PRINTLN("%s",modem_response);
}

/*
@brief gets the bt device available profiles
*/
void BT::bt_get_profile_id(char id){
    modemSerial->printf("AT+BTGETPROF=%c\n\r",id);
    get_response(2000,true);
    DEBUG_PRINTLN("%s",modem_response);
}

void BT::send_bt_cmd(char* cmd){
    modemSerial->printf(cmd);
    modemSerial->printf("\n\r"); 
}
void BT::get_response(uint32_t timeout,bool is_multiline){
    int arr_index=0;
    while(timeout){
    if(arr_index >= 254) break;
    while(modemSerial->readable()){
    char c = modemSerial->getc();
    if(c == '\r') continue;
    if(c=='\n' && arr_index==0){
    continue;
    }
    if(c=='\n' && !is_multiline){
    break;    
    }
    modem_response[arr_index] = c;
    arr_index++;
    }
    timeout--;
    wait_ms(1);
    }
    modem_response[arr_index] = 0;    
}
char* BT::get_response(){
    return modem_response;
}