EEP fORK

Dependencies:   BLE_API mbed nRF51822

Fork of MCS_LRF by Farshad N

Committer:
Farshad
Date:
Thu Oct 08 04:49:36 2015 +0000
Revision:
2:79a9dad8bc5e
Parent:
1:e18634cb382a
Child:
3:de77a4ebbf21
Implemented and tested functionality. Program successfully receives RFID data from the reader through serial port and parses the data to discover ID. It then notifies the connected BLE central of the received ID.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Farshad 0:d58d1cdf43a9 1 /* mbed Microcontroller Library
Farshad 0:d58d1cdf43a9 2 * Copyright (c) 2006-2013 ARM Limited
Farshad 0:d58d1cdf43a9 3 *
Farshad 0:d58d1cdf43a9 4 * Licensed under the Apache License, Version 2.0 (the "License");
Farshad 0:d58d1cdf43a9 5 * you may not use this file except in compliance with the License.
Farshad 0:d58d1cdf43a9 6 * You may obtain a copy of the License at
Farshad 0:d58d1cdf43a9 7 *
Farshad 0:d58d1cdf43a9 8 * http://www.apache.org/licenses/LICENSE-2.0
Farshad 0:d58d1cdf43a9 9 *
Farshad 0:d58d1cdf43a9 10 * Unless required by applicable law or agreed to in writing, software
Farshad 0:d58d1cdf43a9 11 * distributed under the License is distributed on an "AS IS" BASIS,
Farshad 0:d58d1cdf43a9 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Farshad 0:d58d1cdf43a9 13 * See the License for the specific language governing permissions and
Farshad 0:d58d1cdf43a9 14 * limitations under the License.
Farshad 0:d58d1cdf43a9 15 */
Farshad 0:d58d1cdf43a9 16
Farshad 0:d58d1cdf43a9 17 #include "mbed.h"
Farshad 0:d58d1cdf43a9 18 #include "BLE.h"
Farshad 0:d58d1cdf43a9 19 #include "DeviceInformationService.h"
Farshad 0:d58d1cdf43a9 20 #include "UARTService.h"
Farshad 0:d58d1cdf43a9 21
Farshad 0:d58d1cdf43a9 22 #define NEED_CONSOLE_OUTPUT 0 /* Set this if you need debug messages on the console;
Farshad 0:d58d1cdf43a9 23 * it will have an impact on code-size and power consumption. */
Farshad 0:d58d1cdf43a9 24
Farshad 2:79a9dad8bc5e 25 #define NEED_PARSE_TRACE 0 // only used for parsing tag data- will not work if parse function is called from within serial interrupt
Farshad 2:79a9dad8bc5e 26
Farshad 2:79a9dad8bc5e 27 Serial pc(USBTX, USBRX);
Farshad 0:d58d1cdf43a9 28 #if NEED_CONSOLE_OUTPUT
Farshad 2:79a9dad8bc5e 29 //Serial pc(USBTX, USBRX);
Farshad 0:d58d1cdf43a9 30 #define DEBUG(...) { pc.printf(__VA_ARGS__); }
Farshad 0:d58d1cdf43a9 31 #else
Farshad 0:d58d1cdf43a9 32 #define DEBUG(...) /* nothing */
Farshad 2:79a9dad8bc5e 33 #define TRACE(...)
Farshad 0:d58d1cdf43a9 34 #endif /* #if NEED_CONSOLE_OUTPUT */
Farshad 0:d58d1cdf43a9 35
Farshad 2:79a9dad8bc5e 36 #if NEED_TRACE
Farshad 2:79a9dad8bc5e 37 #define TRACE(...) { pc.printf(__VA_ARGS__); }
Farshad 2:79a9dad8bc5e 38 #else
Farshad 2:79a9dad8bc5e 39 #define TRACE(...)
Farshad 2:79a9dad8bc5e 40 #endif /* #if NEED_TRACE */
Farshad 2:79a9dad8bc5e 41
Farshad 0:d58d1cdf43a9 42 BLEDevice ble;
Farshad 2:79a9dad8bc5e 43 //DigitalOut led(p19);
Farshad 2:79a9dad8bc5e 44 DigitalOut led(p21);
Farshad 2:79a9dad8bc5e 45 DigitalOut trigger(p8);
Farshad 2:79a9dad8bc5e 46 Serial reader(p9, p17); // tx, rx === NOTE tx port pin needs to be wired and verified
Farshad 0:d58d1cdf43a9 47
Farshad 2:79a9dad8bc5e 48 const static char DEVICE_NAME[] = "MCS_RFID";
Farshad 0:d58d1cdf43a9 49 const static char MANUFACTURER[] = "MCS";
Farshad 0:d58d1cdf43a9 50 const static char MODEL[] = "Model 1";
Farshad 0:d58d1cdf43a9 51 const static char SERIAL_NO[] = "SN 1234";
Farshad 0:d58d1cdf43a9 52 const static char HARDWARE_REV[] = "hw-rev 1";
Farshad 0:d58d1cdf43a9 53 const static char FIRMWARE_REV[] = "fw-rev 1";
Farshad 0:d58d1cdf43a9 54 const static char SOFTWARE_REV[] = "soft-rev 1";
Farshad 0:d58d1cdf43a9 55
Farshad 0:d58d1cdf43a9 56 UARTService *uartServicePtr;
Farshad 2:79a9dad8bc5e 57 static uint8_t isConnected = 0;
Farshad 0:d58d1cdf43a9 58
Farshad 2:79a9dad8bc5e 59 // these values must macth definitions in the XML file accompanying this device
Farshad 2:79a9dad8bc5e 60 const static uint16_t tagIdCmd = 0x0001;
Farshad 2:79a9dad8bc5e 61 const static uint16_t triggerCmd = 0x0002;
Farshad 2:79a9dad8bc5e 62
Farshad 2:79a9dad8bc5e 63
Farshad 2:79a9dad8bc5e 64 #define HEADER_FLAG 0xFF
Farshad 2:79a9dad8bc5e 65 #define CMD_OFFSET 2
Farshad 2:79a9dad8bc5e 66 #define EPC_READ_CMD 0x29
Farshad 2:79a9dad8bc5e 67 #define STATUS_OFFSET 3
Farshad 2:79a9dad8bc5e 68 #define EPC_LENGTH_OFFSET 26
Farshad 2:79a9dad8bc5e 69 #define BIT_64_TAG_FLAG 0x60
Farshad 2:79a9dad8bc5e 70 #define BIT_96_TAG_FLAG 0x80
Farshad 2:79a9dad8bc5e 71 #define PC_WORD_OFFSET 27
Farshad 2:79a9dad8bc5e 72 #define BIT_64_TAG_PC_WORD 0x20
Farshad 2:79a9dad8bc5e 73 #define BIT_96_TAG_PC_WORD 0x30
Farshad 2:79a9dad8bc5e 74 #define TAG_EPC_OFFSET 29
Farshad 2:79a9dad8bc5e 75
Farshad 2:79a9dad8bc5e 76 #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
Farshad 2:79a9dad8bc5e 77 #define READER_BAUD_RATE 115200 // TODO check this
Farshad 2:79a9dad8bc5e 78 #define READ_BUF_SIZE 255
Farshad 2:79a9dad8bc5e 79 #define TAG_EPC_BUF_SIZE 12
Farshad 2:79a9dad8bc5e 80
Farshad 2:79a9dad8bc5e 81 typedef enum {
Farshad 2:79a9dad8bc5e 82 AWAITING_HEADER,
Farshad 2:79a9dad8bc5e 83 AWAITING_DATA_LEN,
Farshad 2:79a9dad8bc5e 84 AWAITING_READ_CMD,
Farshad 2:79a9dad8bc5e 85 AWAITING_STATUS_1,
Farshad 2:79a9dad8bc5e 86 AWAITING_STATUS_2,
Farshad 2:79a9dad8bc5e 87 AWAITING_EPC_LENGTH,
Farshad 2:79a9dad8bc5e 88 AWAITING_PC_WORD,
Farshad 2:79a9dad8bc5e 89 READING_TAG_EPC,
Farshad 2:79a9dad8bc5e 90 READIG_TAG_CRC
Farshad 2:79a9dad8bc5e 91 } packetState_e;
Farshad 2:79a9dad8bc5e 92
Farshad 2:79a9dad8bc5e 93 static uint8_t tagEPC[TAG_EPC_BUF_SIZE] = {0};
Farshad 2:79a9dad8bc5e 94 static packetState_e state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 95 static uint8_t tagBufIndex = 0;
Farshad 2:79a9dad8bc5e 96 static uint8_t skip = 0;
Farshad 2:79a9dad8bc5e 97 static uint16_t tagLen = 0;
Farshad 2:79a9dad8bc5e 98 static uint16_t dataLen = 0;
Farshad 2:79a9dad8bc5e 99
Farshad 2:79a9dad8bc5e 100 #if 0
Farshad 2:79a9dad8bc5e 101 static uint8_t head = 0;
Farshad 2:79a9dad8bc5e 102 static uint8_t tail = 0;
Farshad 2:79a9dad8bc5e 103 static bool receiving = 0;
Farshad 2:79a9dad8bc5e 104 static uint8_t readBuf[READ_BUF_SIZE] = {0};
Farshad 2:79a9dad8bc5e 105 #endif
Farshad 2:79a9dad8bc5e 106
Farshad 2:79a9dad8bc5e 107
Farshad 2:79a9dad8bc5e 108 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
Farshad 0:d58d1cdf43a9 109 {
Farshad 0:d58d1cdf43a9 110 DEBUG("Disconnected!\n\r");
Farshad 0:d58d1cdf43a9 111 DEBUG("Restarting the advertising process\n\r");
Farshad 0:d58d1cdf43a9 112 ble.startAdvertising();
Farshad 2:79a9dad8bc5e 113 isConnected = 0;
Farshad 2:79a9dad8bc5e 114 led = isConnected;
Farshad 2:79a9dad8bc5e 115
Farshad 2:79a9dad8bc5e 116 }
Farshad 2:79a9dad8bc5e 117
Farshad 2:79a9dad8bc5e 118 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
Farshad 2:79a9dad8bc5e 119 {
Farshad 2:79a9dad8bc5e 120 DEBUG("Connected!\n\r");
Farshad 2:79a9dad8bc5e 121 isConnected = 1;
Farshad 2:79a9dad8bc5e 122 led = isConnected;
Farshad 0:d58d1cdf43a9 123 }
Farshad 0:d58d1cdf43a9 124
Farshad 2:79a9dad8bc5e 125 static void sendBLENotification()
Farshad 2:79a9dad8bc5e 126 {
Farshad 2:79a9dad8bc5e 127 uint8_t buf[READ_BUF_SIZE];
Farshad 2:79a9dad8bc5e 128 uint8_t offset = 0;
Farshad 2:79a9dad8bc5e 129 memcpy(buf, &tagIdCmd, sizeof(uint16_t)); // command
Farshad 2:79a9dad8bc5e 130 offset+=sizeof(uint16_t);
Farshad 2:79a9dad8bc5e 131 memcpy(&buf[offset], &tagLen, sizeof(uint16_t)); // length of array
Farshad 2:79a9dad8bc5e 132 offset+=sizeof(uint16_t);
Farshad 2:79a9dad8bc5e 133 memcpy(&buf[offset], &tagEPC, tagLen); // tag EPC
Farshad 2:79a9dad8bc5e 134 offset+= tagLen;
Farshad 2:79a9dad8bc5e 135 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), buf, offset);
Farshad 2:79a9dad8bc5e 136 }
Farshad 2:79a9dad8bc5e 137
Farshad 2:79a9dad8bc5e 138
Farshad 1:e18634cb382a 139 static void processData(const GattWriteCallbackParams *params)
Farshad 0:d58d1cdf43a9 140 {
Farshad 2:79a9dad8bc5e 141 if(params->len >= 2) {
Farshad 2:79a9dad8bc5e 142 uint16_t command = params->data[0] + (params->data[1] << 8);
Farshad 2:79a9dad8bc5e 143 bool isSetCmd = (command & SET_PARAM_CMD_MASK) == SET_PARAM_CMD_MASK;
Farshad 2:79a9dad8bc5e 144 DEBUG("command: %d \r\n", command);
Farshad 2:79a9dad8bc5e 145
Farshad 2:79a9dad8bc5e 146 switch(command & ~SET_PARAM_CMD_MASK) {
Farshad 2:79a9dad8bc5e 147 case tagIdCmd:
Farshad 2:79a9dad8bc5e 148 if(!isSetCmd && params->len == 2) {
Farshad 2:79a9dad8bc5e 149 // form the reply to send
Farshad 2:79a9dad8bc5e 150 DEBUG("CMD is GET code\n\r");
Farshad 2:79a9dad8bc5e 151 sendBLENotification();
Farshad 2:79a9dad8bc5e 152 }
Farshad 1:e18634cb382a 153 break;
Farshad 0:d58d1cdf43a9 154
Farshad 2:79a9dad8bc5e 155 case triggerCmd:
Farshad 2:79a9dad8bc5e 156 if(isSetCmd && params->len == 2) {
Farshad 2:79a9dad8bc5e 157 // TODO also need to send this information to the LED controller
Farshad 2:79a9dad8bc5e 158 DEBUG("CMD is SET triggerCmd\n\r");
Farshad 2:79a9dad8bc5e 159 trigger = 0;
Farshad 2:79a9dad8bc5e 160 wait_ms(10);
Farshad 2:79a9dad8bc5e 161 trigger = 1;
Farshad 2:79a9dad8bc5e 162 }
Farshad 1:e18634cb382a 163 break;
Farshad 0:d58d1cdf43a9 164
Farshad 1:e18634cb382a 165 default:
Farshad 1:e18634cb382a 166 break;
Farshad 1:e18634cb382a 167 }
Farshad 0:d58d1cdf43a9 168 }
Farshad 0:d58d1cdf43a9 169 }
Farshad 0:d58d1cdf43a9 170
Farshad 0:d58d1cdf43a9 171 void onDataWritten(const GattWriteCallbackParams *params)
Farshad 0:d58d1cdf43a9 172 {
Farshad 0:d58d1cdf43a9 173 if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
Farshad 0:d58d1cdf43a9 174 uint16_t bytesRead = params->len;
Farshad 0:d58d1cdf43a9 175 DEBUG("received %u bytes\n\r", bytesRead);
Farshad 0:d58d1cdf43a9 176 for(int i = 0; i < bytesRead; i++) {
Farshad 2:79a9dad8bc5e 177 DEBUG("0x%X ", params->data[i]);
Farshad 0:d58d1cdf43a9 178 }
Farshad 0:d58d1cdf43a9 179 DEBUG("\n\r", bytesRead);
Farshad 0:d58d1cdf43a9 180
Farshad 2:79a9dad8bc5e 181 // ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
Farshad 1:e18634cb382a 182
Farshad 1:e18634cb382a 183 processData(params);
Farshad 0:d58d1cdf43a9 184 }
Farshad 0:d58d1cdf43a9 185 }
Farshad 0:d58d1cdf43a9 186
Farshad 2:79a9dad8bc5e 187 // This function requires the entire scanned data to be available. It takes a pointer to the beginnig of the data array
Farshad 2:79a9dad8bc5e 188 // and does not do any checking of available data. ie very easy to break (currently not used- Instead use the parsePacket()
Farshad 2:79a9dad8bc5e 189 // function with each byte received from the serial port.
Farshad 2:79a9dad8bc5e 190 #if 0
Farshad 2:79a9dad8bc5e 191 void checkForEPC(uint8_t* d)
Farshad 2:79a9dad8bc5e 192 {
Farshad 2:79a9dad8bc5e 193 DEBUG("%02X ", *d);
Farshad 2:79a9dad8bc5e 194 if(*d == HEADER_FLAG && *(d + CMD_OFFSET) == EPC_READ_CMD) {
Farshad 2:79a9dad8bc5e 195 DEBUG("GOT READ COmmand =======\r\n");
Farshad 2:79a9dad8bc5e 196 // this is response to a read tage EPC command
Farshad 2:79a9dad8bc5e 197 if(*(d + STATUS_OFFSET) == 0 && *(d + STATUS_OFFSET + 1) == 0) {
Farshad 2:79a9dad8bc5e 198 DEBUG("STATUS is OK =======\r\n");
Farshad 2:79a9dad8bc5e 199 // state is Ok
Farshad 2:79a9dad8bc5e 200 if(*(d + EPC_LENGTH_OFFSET) == BIT_64_TAG_FLAG && *(d + PC_WORD_OFFSET) == BIT_64_TAG_PC_WORD) {
Farshad 2:79a9dad8bc5e 201 DEBUG("GOT 64bit EPC =======\r\n");
Farshad 2:79a9dad8bc5e 202 // this is a 64 bit tag so the next 8 bytes are tag id and further 2 bytes are the tag CRC
Farshad 2:79a9dad8bc5e 203 memset(&tagEPC[0], 0, TAG_EPC_BUF_SIZE);
Farshad 2:79a9dad8bc5e 204 memcpy(&tagEPC[0], d + TAG_EPC_OFFSET, 8); // do not worry about the CRC for now
Farshad 2:79a9dad8bc5e 205
Farshad 2:79a9dad8bc5e 206 for(int j = 0; j < 8; j++) {
Farshad 2:79a9dad8bc5e 207 DEBUG("%02X ", tagEPC[j]);
Farshad 2:79a9dad8bc5e 208 }
Farshad 2:79a9dad8bc5e 209 DEBUG("\r\n");
Farshad 2:79a9dad8bc5e 210 } else if(*(d + EPC_LENGTH_OFFSET) == BIT_96_TAG_FLAG && *(d + PC_WORD_OFFSET) == BIT_96_TAG_PC_WORD) {
Farshad 2:79a9dad8bc5e 211 DEBUG("GOT 96bit EPC =======\r\n");
Farshad 2:79a9dad8bc5e 212 // this is a 96 bit tag so the next 12 bytes are tag id and further 2 bytes are the tag CRC
Farshad 2:79a9dad8bc5e 213 memset(&tagEPC[0], 0, TAG_EPC_BUF_SIZE);
Farshad 2:79a9dad8bc5e 214 memcpy(&tagEPC[0], d + TAG_EPC_OFFSET, 12); // do not worry about the CRC for now
Farshad 2:79a9dad8bc5e 215
Farshad 2:79a9dad8bc5e 216 for(int j = 0; j < 12; j++) {
Farshad 2:79a9dad8bc5e 217 DEBUG("%02X ", tagEPC[j]);
Farshad 2:79a9dad8bc5e 218 }
Farshad 2:79a9dad8bc5e 219 DEBUG("\r\n");
Farshad 2:79a9dad8bc5e 220 }
Farshad 2:79a9dad8bc5e 221 } // Status OK
Farshad 2:79a9dad8bc5e 222 } // response to EPC
Farshad 2:79a9dad8bc5e 223 }
Farshad 2:79a9dad8bc5e 224 #endif
Farshad 2:79a9dad8bc5e 225
Farshad 2:79a9dad8bc5e 226 // State machine to parse the tag data received from serial port. It looks for EPC data in the packet
Farshad 2:79a9dad8bc5e 227 static void parsePacket(uint8_t d)
Farshad 2:79a9dad8bc5e 228 {
Farshad 2:79a9dad8bc5e 229 TRACE("%02X ", d);
Farshad 2:79a9dad8bc5e 230 switch (state) {
Farshad 2:79a9dad8bc5e 231 case AWAITING_HEADER:
Farshad 2:79a9dad8bc5e 232 if(d == HEADER_FLAG) {
Farshad 2:79a9dad8bc5e 233 TRACE("AWAITING_DATA_LEN \r\n");
Farshad 2:79a9dad8bc5e 234 state = AWAITING_DATA_LEN;
Farshad 2:79a9dad8bc5e 235 skip = 0;
Farshad 2:79a9dad8bc5e 236 }
Farshad 2:79a9dad8bc5e 237 break;
Farshad 2:79a9dad8bc5e 238
Farshad 2:79a9dad8bc5e 239 case AWAITING_DATA_LEN:
Farshad 2:79a9dad8bc5e 240 if(d > 0) {
Farshad 2:79a9dad8bc5e 241 dataLen = d;
Farshad 2:79a9dad8bc5e 242 state = AWAITING_READ_CMD;
Farshad 2:79a9dad8bc5e 243 TRACE("AWAITING_READ_CMD \r\n");
Farshad 2:79a9dad8bc5e 244 } else {
Farshad 2:79a9dad8bc5e 245 state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 246 }
Farshad 2:79a9dad8bc5e 247 break;
Farshad 2:79a9dad8bc5e 248
Farshad 2:79a9dad8bc5e 249 case AWAITING_READ_CMD:
Farshad 2:79a9dad8bc5e 250 if(d == EPC_READ_CMD) {
Farshad 2:79a9dad8bc5e 251 state = AWAITING_STATUS_1;
Farshad 2:79a9dad8bc5e 252 TRACE("AWAITING_STATUS_1 \r\n");
Farshad 2:79a9dad8bc5e 253 } else {
Farshad 2:79a9dad8bc5e 254 state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 255 }
Farshad 2:79a9dad8bc5e 256 break;
Farshad 2:79a9dad8bc5e 257
Farshad 2:79a9dad8bc5e 258 case AWAITING_STATUS_1:
Farshad 2:79a9dad8bc5e 259 if(d == 0) {
Farshad 2:79a9dad8bc5e 260 state = AWAITING_STATUS_2;
Farshad 2:79a9dad8bc5e 261 TRACE("AWAITING_STATUS_2 \r\n");
Farshad 2:79a9dad8bc5e 262 } else {
Farshad 2:79a9dad8bc5e 263 state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 264 }
Farshad 2:79a9dad8bc5e 265 break;
Farshad 2:79a9dad8bc5e 266
Farshad 2:79a9dad8bc5e 267 case AWAITING_STATUS_2:
Farshad 2:79a9dad8bc5e 268 if(d == 0) {
Farshad 2:79a9dad8bc5e 269 state = AWAITING_EPC_LENGTH;
Farshad 2:79a9dad8bc5e 270 TRACE("AWAITING_EPC_LENGTH_1 \r\n");
Farshad 2:79a9dad8bc5e 271 } else {
Farshad 2:79a9dad8bc5e 272 state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 273 }
Farshad 2:79a9dad8bc5e 274 break;
Farshad 2:79a9dad8bc5e 275
Farshad 2:79a9dad8bc5e 276 case AWAITING_EPC_LENGTH:
Farshad 2:79a9dad8bc5e 277 if(++skip >= 22) {
Farshad 2:79a9dad8bc5e 278 if(d == BIT_64_TAG_FLAG) {
Farshad 2:79a9dad8bc5e 279 state = AWAITING_PC_WORD;
Farshad 2:79a9dad8bc5e 280 tagLen = 8;
Farshad 2:79a9dad8bc5e 281 TRACE("AWAITING_PC_WORD \r\n");
Farshad 2:79a9dad8bc5e 282 } else if (d == BIT_96_TAG_FLAG) {
Farshad 2:79a9dad8bc5e 283 state = AWAITING_PC_WORD;
Farshad 2:79a9dad8bc5e 284 tagLen = 12;
Farshad 2:79a9dad8bc5e 285 TRACE("AWAITING_PC_WORD \r\n");
Farshad 2:79a9dad8bc5e 286 } else {
Farshad 2:79a9dad8bc5e 287 state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 288 }
Farshad 2:79a9dad8bc5e 289 }
Farshad 2:79a9dad8bc5e 290 break;
Farshad 2:79a9dad8bc5e 291
Farshad 2:79a9dad8bc5e 292 case AWAITING_PC_WORD:
Farshad 2:79a9dad8bc5e 293 if((tagLen == 8 && d == BIT_64_TAG_PC_WORD) || (tagLen == 12 && d == BIT_96_TAG_PC_WORD)) {
Farshad 2:79a9dad8bc5e 294 state = READING_TAG_EPC;
Farshad 2:79a9dad8bc5e 295 tagBufIndex = 0;
Farshad 2:79a9dad8bc5e 296 TRACE("READING_TAG_EPC \r\n");
Farshad 2:79a9dad8bc5e 297 skip = 0;
Farshad 2:79a9dad8bc5e 298 } else {
Farshad 2:79a9dad8bc5e 299 state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 300 }
Farshad 2:79a9dad8bc5e 301 break;
Farshad 2:79a9dad8bc5e 302
Farshad 2:79a9dad8bc5e 303 case READING_TAG_EPC:
Farshad 2:79a9dad8bc5e 304 if(++skip >= 2) {
Farshad 2:79a9dad8bc5e 305 if(tagBufIndex < tagLen) {
Farshad 2:79a9dad8bc5e 306 tagEPC[tagBufIndex++] = d;
Farshad 2:79a9dad8bc5e 307 TRACE("%02X ", d);
Farshad 2:79a9dad8bc5e 308 } else {
Farshad 2:79a9dad8bc5e 309 // don't worry about CRC for now
Farshad 2:79a9dad8bc5e 310 state = AWAITING_HEADER;
Farshad 2:79a9dad8bc5e 311 TRACE("AWAITING_HEADER \r\n");
Farshad 2:79a9dad8bc5e 312
Farshad 2:79a9dad8bc5e 313 TRACE("TAG EPC: ============\r\n");
Farshad 2:79a9dad8bc5e 314 //for(int i = 0; i < tagLen; i++) {
Farshad 2:79a9dad8bc5e 315 // TRACE("%02X ", tagEPC[i]);
Farshad 2:79a9dad8bc5e 316 // pc.printf("%02X ", tagEPC[i]);
Farshad 2:79a9dad8bc5e 317 // }
Farshad 2:79a9dad8bc5e 318 // pc.printf("\r\n");
Farshad 2:79a9dad8bc5e 319
Farshad 2:79a9dad8bc5e 320 sendBLENotification();
Farshad 2:79a9dad8bc5e 321 }
Farshad 2:79a9dad8bc5e 322 }
Farshad 2:79a9dad8bc5e 323 break;
Farshad 2:79a9dad8bc5e 324 }
Farshad 2:79a9dad8bc5e 325 }
Farshad 2:79a9dad8bc5e 326
Farshad 0:d58d1cdf43a9 327 void periodicCallback(void)
Farshad 0:d58d1cdf43a9 328 {
Farshad 0:d58d1cdf43a9 329 // led1 = !led1;
Farshad 0:d58d1cdf43a9 330 }
Farshad 0:d58d1cdf43a9 331
Farshad 2:79a9dad8bc5e 332 void readerCallback()
Farshad 2:79a9dad8bc5e 333 {
Farshad 2:79a9dad8bc5e 334 // Note: Need to actually read from the serial to clear the RX interrupt
Farshad 2:79a9dad8bc5e 335 // receiving = true;
Farshad 2:79a9dad8bc5e 336 while(reader.readable()) {
Farshad 2:79a9dad8bc5e 337 uint8_t c = reader.getc();
Farshad 2:79a9dad8bc5e 338 // readBuf[head++] = c;
Farshad 2:79a9dad8bc5e 339 // if(head >= READ_BUF_SIZE) {
Farshad 2:79a9dad8bc5e 340 // head = 0;
Farshad 2:79a9dad8bc5e 341 // }
Farshad 2:79a9dad8bc5e 342 parsePacket(c);
Farshad 2:79a9dad8bc5e 343 }
Farshad 2:79a9dad8bc5e 344
Farshad 2:79a9dad8bc5e 345 // make sure LED stays on
Farshad 2:79a9dad8bc5e 346 led = isConnected;
Farshad 2:79a9dad8bc5e 347
Farshad 2:79a9dad8bc5e 348 // receiving = false;
Farshad 2:79a9dad8bc5e 349 }
Farshad 2:79a9dad8bc5e 350
Farshad 0:d58d1cdf43a9 351 int main(void)
Farshad 0:d58d1cdf43a9 352 {
Farshad 2:79a9dad8bc5e 353 // default state is unknown
Farshad 2:79a9dad8bc5e 354 led = 0;
Farshad 2:79a9dad8bc5e 355
Farshad 0:d58d1cdf43a9 356 Ticker ticker;
Farshad 2:79a9dad8bc5e 357 ticker.attach(periodicCallback, 5);
Farshad 0:d58d1cdf43a9 358
Farshad 0:d58d1cdf43a9 359 DEBUG("Initialising the nRF51822\n\r");
Farshad 0:d58d1cdf43a9 360 ble.init();
Farshad 0:d58d1cdf43a9 361
Farshad 0:d58d1cdf43a9 362 ble.onDisconnection(disconnectionCallback);
Farshad 2:79a9dad8bc5e 363 ble.onConnection(connectionCallback);
Farshad 0:d58d1cdf43a9 364 ble.onDataWritten(onDataWritten);
Farshad 0:d58d1cdf43a9 365
Farshad 0:d58d1cdf43a9 366 /* setup advertising */
Farshad 0:d58d1cdf43a9 367 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
Farshad 0:d58d1cdf43a9 368 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
Farshad 0:d58d1cdf43a9 369 ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME, (const uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME) - 1);
Farshad 0:d58d1cdf43a9 370 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,(const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
Farshad 0:d58d1cdf43a9 371 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS,
Farshad 1:e18634cb382a 372 (uint8_t *)GattService::UUID_DEVICE_INFORMATION_SERVICE, sizeof(GattService::UUID_DEVICE_INFORMATION_SERVICE));
Farshad 2:79a9dad8bc5e 373 ble.setAdvertisingInterval(GapAdvertisingParams::MSEC_TO_ADVERTISEMENT_DURATION_UNITS(1000));
Farshad 0:d58d1cdf43a9 374 ble.startAdvertising();
Farshad 0:d58d1cdf43a9 375
Farshad 0:d58d1cdf43a9 376 /* Setup uart service */
Farshad 0:d58d1cdf43a9 377 UARTService uartService(ble);
Farshad 0:d58d1cdf43a9 378 uartServicePtr = &uartService;
Farshad 0:d58d1cdf43a9 379
Farshad 0:d58d1cdf43a9 380 /* Setup auxiliary service. */
Farshad 0:d58d1cdf43a9 381 DeviceInformationService deviceInfo(ble, MANUFACTURER, MODEL, SERIAL_NO,HARDWARE_REV, FIRMWARE_REV, SOFTWARE_REV);
Farshad 0:d58d1cdf43a9 382
Farshad 2:79a9dad8bc5e 383 // setup serial port to RFID reader
Farshad 2:79a9dad8bc5e 384 reader.baud(READER_BAUD_RATE);
Farshad 2:79a9dad8bc5e 385 reader.attach(&readerCallback);
Farshad 2:79a9dad8bc5e 386
Farshad 0:d58d1cdf43a9 387 while (true) {
Farshad 0:d58d1cdf43a9 388 ble.waitForEvent();
Farshad 0:d58d1cdf43a9 389 }
Farshad 0:d58d1cdf43a9 390 }