watch using the RSSI of Bluetooth

Dependencies:   BaseUsbHost ConfigFile EthernetInterface HTTPClient-long mbed-rtos mbed

main.cpp

Committer:
va009039
Date:
2013-01-20
Revision:
0:600fe65e7c88

File content as of revision 0:600fe65e7c88:

#include "mbed.h"
#include "EthernetInterface.h"
#include "BaseUsbHost.h"
//#define DEBUG
#include "BaseUsbHostDebug.h"
#define TEST
#include "BaseUsbHostTest.h"
#include "UsbBt2.h"
#include "CosmClient.h"
#include "MyThread.h"
#include "ConfigFile.h"

#define CFG_FILE "/local/mimamori.cfg"

LocalFileSystem local("local");
Serial pc(USBTX, USBRX);
EthernetInterface eth;
ConfigFile cfg;
CosmClient client;

BD_ADDR addr;
#define RSSI_NONE (-129)
int rssi = RSSI_NONE;

class bt_client : public MyThread {
public:
    bt_client(bthci* hci):m_hci(hci) {
    }
private:
    virtual void run() {
        inquiry_with_rssi_info rssi_info;
        hci_event* event;
        int max_period_length = 25;
        int min_period_length = 20;
        int inquiry_length = 15;
        int rc = m_hci->cmdSend(HCI_OP_RESET);
        TEST_ASSERT(rc == USB_OK);
        for(int n = 0; ; n++) {
            int r = m_hci->eventReceive(m_buf_int, sizeof(m_buf_int));
            if (r > 0) {
                //DBG("%p eventReceive %d\n", this, r);
                //DBG_HEX(m_buf_int, r);
                event = reinterpret_cast<hci_event*>(m_buf_int);
                switch(event->evt) {
                    case HCI_EV_CMD_COMPLETE:
                        DBG("\nHCI_EV_CMD_COMPLETE\n");
                        switch(event->c.op) {
                            case HCI_OP_RESET:
                                rc = m_hci->cmdSend(HCI_OP_WRITE_INQUIRY_MODE, "B", 0x01); // with RSSI
                                TEST_ASSERT(rc == USB_OK);
                                break;
                            case HCI_OP_WRITE_INQUIRY_MODE:
                                rc = m_hci->cmdSend(HCI_OP_PERIODIC_INQUIRY, "HHBBBBB", 
                                                    max_period_length, min_period_length, 0x33, 0x8B, 0x9E, inquiry_length, 0);
                                TEST_ASSERT(rc == USB_OK);
                                break;
                            default:
                                DBG_HEX(m_buf_int, r);
                                break;
                        }
                        break;
                    case HCI_EV_INQUIRY_RESULT_WITH_RSSI:
                        //DBG("\nHCI_EV_INQUIRY_RESULT_WITH_RSSI\n");
                        rssi_info = *reinterpret_cast<inquiry_with_rssi_info*>(event->c.data);
                        DBG_HEX((uint8_t*)&rssi_info, sizeof(inquiry_with_rssi_info));
                        if (memcmp(&rssi_info.bdaddr, &addr, 6) == 0) {
                            rssi = rssi_info.rssi;
                            DBG("rssi=%d\n", rssi);
                        }
                        break;
                    default:
                        DBG_HEX(m_buf_int, r);
                        break;
                }
            }
        }
    }
    bthci* m_hci;
    uint8_t m_buf_int[250];
};

void no_memory () {
  error("Failed to allocate memory!\n");
}

int main() {
    pc.baud(921600);
    printf("%s\n", __FILE__);
    set_new_handler(no_memory);
    
    if(!cfg.read(CFG_FILE)) {
        error("can not read %s\n", CFG_FILE);
    }
    char buf[128];
    if (!cfg.getValue("apikey", buf, sizeof(buf))) {
        error("apikey?\n");
    }
    client.setApikey(buf);
    if (!cfg.getValue("feedid", buf, sizeof(buf))) {
        error("feedid?\n");
    }
    client.setFeedID(buf);

    if (!cfg.getValue("bdaddr", buf, sizeof(buf))) {
        error("bdaddr?\n");
    }
    addr.set(buf);

    eth.init(); //Use DHCP
    eth.connect();

    BaseUsbHost* usbHost = new BaseUsbHost();
    ControlEp* ctlEp = new ControlEp; // root hub
    bthci* bt_dongle1 = NULL;
    if (bthci::check(ctlEp)) {
        bt_dongle1 = new bthci(ctlEp);
    } else if (UsbHub::check(ctlEp)) {
        UsbHub* hub = new UsbHub(ctlEp);
        for(vector<ControlEp*>::iterator it = hub->PortEp.begin(); it != hub->PortEp.end(); ++it) {
            if (bthci::check(*it)) {
                bt_dongle1 = new bthci(*it);
                break;
            }
        }
    }
    if (bt_dongle1 == NULL) {
        error("USB Bluetooth not found\n");
    }
    bt_client* bt_th = new bt_client(bt_dongle1);
    bt_th->set_stack(DEFAULT_STACK_SIZE);
    bt_th->start();
    for(int n = 0; ; n++) {
        printf("%d Bluetooth rssi: %d stack used: %d/%d bytes\n", n, rssi, bt_th->stack_used(), bt_th->stack_size());

        client.clear();
        client.add("0", rssi);
        client.update();
        rssi = RSSI_NONE;
    
        Thread::wait(40*1000);
    }
}