EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

main.cpp

Committer:
Farshad
Date:
2015-10-09
Revision:
3:de77a4ebbf21
Parent:
2:79a9dad8bc5e
Child:
4:76bd50c41d39

File content as of revision 3:de77a4ebbf21:

/* mbed Microcontroller Library
 * Copyright (c) 2006-2013 ARM Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "mbed.h"
#include "BLE.h"
#include "DeviceInformationService.h"
#include "UARTService.h"

#define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
* it will have an impact on code-size and power consumption. */

#define NEED_PARSE_TRACE   0        // only used for parsing tag data- will not work if parse function is called from within serial interrupt

Serial  pc(USBTX, USBRX);
#if NEED_CONSOLE_OUTPUT
//Serial  pc(USBTX, USBRX);
#define DEBUG(...) { pc.printf(__VA_ARGS__); }
#else
#define DEBUG(...) /* nothing */
#define TRACE(...)
#endif /* #if NEED_CONSOLE_OUTPUT */

#if NEED_TRACE
#define TRACE(...) { pc.printf(__VA_ARGS__); }
#else
#define TRACE(...)
#endif /* #if NEED_TRACE */

BLEDevice  ble;
//DigitalOut led(p19);
DigitalOut led(p21);
DigitalOut trigger(p8);
Serial reader(p9, p17);  // tx, rx   === NOTE tx port pin needs to be wired and verified

const static char     DEVICE_NAME[]    = "MCS_RFID";
const static char     MANUFACTURER[]   = "MCS";
const static char     MODEL[]          = "Model 1";
const static char     SERIAL_NO[]      = "SN 1234";
const static char     HARDWARE_REV[]   = "hw-rev 1";
const static char     FIRMWARE_REV[]   = "fw-rev 1";
const static char     SOFTWARE_REV[]   = "soft-rev 1";

UARTService *uartServicePtr;
static uint8_t isConnected = 0;

// these values must macth definitions in the XML file accompanying this device
const static uint16_t tagIdCmd      = 0x0001;
const static uint16_t triggerCmd   = 0x0002;

// definition of the RFID packet
#define HEADER_FLAG             0xFF
#define EPC_READ_CMD            0x29
#define BIT_64_TAG_FLAG         0x60
#define BIT_96_TAG_FLAG         0x80
#define SKIP_TO_BIT_FLAG        22
#define BIT_64_TAG_PC_WORD      0x20
#define BIT_96_TAG_PC_WORD      0x30
#define SKIP_TO_TAG_EPC         2

     
#define SET_PARAM_CMD_MASK      0x8000  // commands with MSB set to 0 are to get the parameter and MSB of 1 to set the parameter
#define READER_BAUD_RATE        115200  
#define READ_BUF_SIZE           255
#define TAG_EPC_BUF_SIZE        12
       

typedef enum {
    AWAITING_HEADER,
    AWAITING_DATA_LEN,
    AWAITING_READ_CMD,
    AWAITING_STATUS_1,
    AWAITING_STATUS_2,
    AWAITING_EPC_LENGTH,
    AWAITING_PC_WORD,
    READING_TAG_EPC,
    READIG_TAG_CRC
} packetState_e;

static uint8_t tagEPC[TAG_EPC_BUF_SIZE] = {0};
static packetState_e state = AWAITING_HEADER;
static uint8_t tagBufIndex = 0;
static uint8_t skip = 0;
static uint16_t tagLen = 0;
static uint16_t dataLen = 0;


void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
{
    DEBUG("Disconnected!\n\r");
    DEBUG("Restarting the advertising process\n\r");
    ble.startAdvertising();
    isConnected = 0;
    led = isConnected;
}

void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
{
    DEBUG("Connected!\n\r");
    isConnected = 1;
    led = isConnected;
}

static void sendBLENotification()
{
    uint8_t buf[READ_BUF_SIZE];
    uint8_t offset = 0;
    memcpy(buf, &tagIdCmd, sizeof(uint16_t));       // command
    offset+=sizeof(uint16_t);
    memcpy(&buf[offset], &tagLen, sizeof(uint16_t));     // length of array
    offset+=sizeof(uint16_t);
    memcpy(&buf[offset], &tagEPC, tagLen);              // tag EPC
    offset+= tagLen;
    ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), buf, offset);
}


static void processData(const GattWriteCallbackParams *params)
{
    if(params->len >= 2) {
        uint16_t command = params->data[0] + (params->data[1] << 8);
        bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK;
        DEBUG("command: %d   \r\n", command);

        switch(command & ~SET_PARAM_CMD_MASK) {
            case tagIdCmd:
                if(!isSetCmd && params->len == 2) {
                    // form the reply to send
                    DEBUG("CMD is GET code\n\r");
                    // TODO can do a trigger for a new read before transimiting, For now just send the latest read
                    sendBLENotification();
                }
                break;

            case triggerCmd:
                if(isSetCmd && params->len == 2) {
                    // TODO also need to send this information to the LED controller
                    DEBUG("CMD is SET triggerCmd\n\r");
                    trigger = 0;
                    wait_ms(10);
                    trigger = 1;
                }
                break;

            default:
                break;
        }
    }
}

void onDataWritten(const GattWriteCallbackParams *params)
{
    if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
        uint16_t bytesRead = params->len;
        DEBUG("received %u bytes\n\r", bytesRead);
        for(int i = 0; i < bytesRead; i++) {
            DEBUG("0x%X ", params->data[i]);
        }
        DEBUG("\n\r", bytesRead);
        
        // echo?
        // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);

        processData(params);
    }
}


// State machine to parse the tag data received from serial port. It only looks for EPC data in the packet
static void parsePacket(uint8_t d)
{
    TRACE("%02X    ", d);
    switch (state) {
        case AWAITING_HEADER:
            if(d == HEADER_FLAG) {
                TRACE("AWAITING_DATA_LEN \r\n");
                state = AWAITING_DATA_LEN;
                skip = 0;
            }
            break;

        case AWAITING_DATA_LEN:
            if(d > 0) {
                dataLen = d;
                state = AWAITING_READ_CMD;
                TRACE("AWAITING_READ_CMD \r\n");
            } else {
                state = AWAITING_HEADER;
            }
            break;

        case AWAITING_READ_CMD:
            if(d == EPC_READ_CMD) {
                state = AWAITING_STATUS_1;
                TRACE("AWAITING_STATUS_1 \r\n");
            } else {
                state = AWAITING_HEADER;
            }
            break;

        case AWAITING_STATUS_1:
            if(d == 0) {
                state = AWAITING_STATUS_2;
                TRACE("AWAITING_STATUS_2 \r\n");
            } else {
                state = AWAITING_HEADER;
            }
            break;

        case AWAITING_STATUS_2:
            if(d == 0) {
                state = AWAITING_EPC_LENGTH;
                TRACE("AWAITING_EPC_LENGTH_1 \r\n");
            } else {
                state = AWAITING_HEADER;
            }
            break;

        case AWAITING_EPC_LENGTH:
         if(++skip >= SKIP_TO_BIT_FLAG) {
            if(d == BIT_64_TAG_FLAG) {
                state = AWAITING_PC_WORD;
                tagLen = 8;
                TRACE("AWAITING_PC_WORD \r\n");
            } else if (d == BIT_96_TAG_FLAG) {
                state = AWAITING_PC_WORD;
                tagLen = 12;
                TRACE("AWAITING_PC_WORD \r\n");
            } else {
                state = AWAITING_HEADER;
            }
            }
            break;

        case AWAITING_PC_WORD:
            if((tagLen == 8 && d == BIT_64_TAG_PC_WORD) || (tagLen == 12 && d == BIT_96_TAG_PC_WORD)) {
                state = READING_TAG_EPC;
                tagBufIndex = 0;
                TRACE("READING_TAG_EPC \r\n");
                skip = 0;
            } else {
                state = AWAITING_HEADER;
            }
            break;

        case READING_TAG_EPC:
            if(++skip >= SKIP_TO_TAG_EPC) {
                if(tagBufIndex < tagLen) {
                    tagEPC[tagBufIndex++] = d;
                    TRACE("%02X ", d);
                } else {
                    // don't worry about CRC for now
                    state = AWAITING_HEADER;
                    TRACE("AWAITING_HEADER \r\n");

                    TRACE("TAG EPC: ============\r\n");
                    //for(int i = 0; i < tagLen; i++) {
//                        TRACE("%02X ", tagEPC[i]);
//                        pc.printf("%02X ", tagEPC[i]);
//                    }
//                    pc.printf("\r\n");
                    
                    sendBLENotification();
                }
            }
            break;
    }
}

void periodicCallback(void)
{
    //  led1 = !led1;
}

// this is an ISR, so do not spend too much time here and be careful with printing debug info
void readerCallback()
{
    // Note: Need to actually read from the serial to clear the RX interrupt
    while(reader.readable()) {           
        parsePacket(reader.getc());
    }
    
    // make sure LED stays on
    led = isConnected;
}

int main(void)
{
    // default state is unknown
    led = 0;

    Ticker ticker;
    ticker.attach(periodicCallback, 5);

    DEBUG("Initialising the nRF51822\n\r");
    ble.init();

    ble.onDisconnection(disconnectionCallback);
    ble.onConnection(connectionCallback);
    ble.onDataWritten(onDataWritten);

    /* setup advertising */
    ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
    ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
                                     (uint8_t *)GattService::UUID_DEVICE_INFORMATION_SERVICE, sizeof(GattService::UUID_DEVICE_INFORMATION_SERVICE));
    ble.setAdvertisingInterval(GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
    ble.startAdvertising();

    /* Setup uart service */
    UARTService uartService(ble);
    uartServicePtr = &uartService;

    /* Setup auxiliary service. */
    DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV);

    // setup serial port to RFID reader
    reader.baud(READER_BAUD_RATE);
    reader.attach(&readerCallback);

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