A scanner framework that receives advertisements, filters them by MAC, and prints out some data as CSV over the serial port

Dependencies:   BLE_API mbed nRF51822

Fork of BasicScanner by Mobius IoT

main.cpp

Committer:
budoguyiii
Date:
2016-06-01
Revision:
13:8999c8b2e18e
Parent:
12:dbbf0ddc9b12
Child:
14:d8f4a49c9fb3

File content as of revision 13:8999c8b2e18e:


#include "mbed.h"
#include "ble/BLE.h"
#include "TMP_nrf51/TMP_nrf51.h"


#include "UARTService.h"


#define UART_TX     p9
#define UART_RX     p11

#define LOG(...)    { pc.printf(__VA_ARGS__); }

#define SCAN_INTERVAL 500
#define SCAN_WINDOW 500

 //Baud rate: for the TinyBLE use 115200 for term or 4000000 for internal energy monitoring
#define BAUD_RATE 115200

DigitalOut alivenessLED(LED1, 1);
Ticker     ticker;

Serial pc(UART_TX, UART_RX);

UARTService *uartServicePtr;



void periodicCallback(void)
{
    alivenessLED = !alivenessLED; /* Do blinky on LED1 while we're waiting for BLE events. This is optional. */
}

/*
 * This function is called every time we scan an advertisement.
 */
void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params)
{
    // Here is what we do when we receive a packet
    
    /* Search for the manufacturer specific data with matching application-ID */
    int addr_length = 6;

    if(params->peerAddr[addr_length-1] == 0xaa)
    {
        LOG("\nFrom: ");
        for(int i=0; i<addr_length; i++)
             LOG("%02x:", params->peerAddr[addr_length-i-1]);
        //print payload
        LOG(" RSSI: %d", params->rssi);
        LOG("\n          Payload:  ");
        for(int i=0; i < params->advertisingDataLen; i++) 
                LOG(" %02x", params->advertisingData[i]);
        
        //print close of round
        LOG("\n\n");
    }
}

/**
 * This function is called when the ble initialization process has failed
 */
void onBleInitError(BLE &ble, ble_error_t error)
{
    /* Initialization error handling should go here */
    LOG("Crap, the BLE radio is broken\n");
}

/**
 * Callback triggered when the ble initialization process has finished
 */
void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
{
    BLE&        ble   = params->ble;
    ble_error_t error = params->error;

    if (error != BLE_ERROR_NONE) {
        /* In case of error, forward the error handling to onBleInitError */
        onBleInitError(ble, error);
        return;
    }

    /* Ensure that it is the default instance of BLE */
    if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
        return;
    }

    /* Setup and start scanning */
    ble.gap().setScanParams(SCAN_INTERVAL /* scan interval */, SCAN_WINDOW /* scan window */);
    ble.gap().startScan(advertisementCallback);
}

int main(void)
{
   
    pc.baud(BAUD_RATE);
    
    LOG("---- Basic Scanner ACTIVIZE ----\n");
   
    
    ticker.attach(periodicCallback, 1);  /* flash the LED because reasons */

    LOG("Bring up the BLE radio\n");
    BLE &ble = BLE::Instance();
    ble.init(bleInitComplete);

    UARTService uartService(ble);
    uartServicePtr = &uartService;
    //uartService.retargetStdout();

    while (true) {
        ble.waitForEvent();
    }
}