ongoing Development Project for interfacing a BM019 Module with nrf51-dk, acting as a nfc 2 ble bridge. Base project for opensource blueReader Device

Dependencies:   BLE_API mbed nRF51822

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "ble/BLE.h"
00003 
00004 #include "ble/services/UARTService.h"
00005 
00006 #include "log.h"
00007 /*
00008  This is an example of using the implemented functions of the bm019-library with an nrf51, acting as UART-Service.
00009  This code uses the spi version of the interface.
00010  */
00011 #include "bm019.h"
00012 
00013 BLEDevice  ble;
00014 UARTService *uartServicePtr;
00015 
00016 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
00017 {
00018     DEBUG("Disconnected!\n\r");
00019     DEBUG("Restarting the advertising process\n\r");
00020     ble.startAdvertising();
00021 }
00022 char answer[512];
00023 
00024 Ticker ticker;
00025 void timerCallback(void)
00026 {
00027     DEBUG("start timer callback");
00028     sprintf(answer,"+.!");
00029     DEBUG("writing beatpulse \"%s\" with len %d to ble\n",answer,strlen(answer));
00030     int l = strlen(answer);
00031     for(int i = 0; i*20 < strlen(answer); i++) {
00032         int len = 20 < l ? 20 : l;
00033         ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)&answer[i*20], len);
00034         l -= 20;
00035     }
00036     ticker.detach();
00037 }
00038 void onDataWritten(const GattWriteCallbackParams *params)
00039 {
00040     if ((uartServicePtr != NULL) && (params->handle == uartServicePtr->getTXCharacteristicHandle())) {
00041         uint16_t bytesRead = params->len;
00042         bool handled = false;
00043 
00044 
00045         for(int i = 0; !handled && i < bytesRead; i++) {
00046             switch(params->data[i]) {
00047                 case '?': {
00048                     DEBUG("Handle ?\n");
00049                     switch(getStateBM019()) {
00050                         case BM019_STATE_UNKNOWN:
00051                             DEBUG("BM019 Status: unknown\n");
00052                             sprintf(answer,"+?:0!");
00053                             break;
00054                         case BM019_STATE_ANSWERING:
00055                             DEBUG("BM019 Status: answering\n");
00056                             sprintf(answer,"+?:1!");
00057                             break;
00058                         case BM019_STATE_PROTOCOL:
00059                             DEBUG("BM019 Status: protocol set\n");
00060                             sprintf(answer,"+?:2!");
00061                             break;
00062                         default:
00063                             sprintf(answer,"-?%d!",getStateBM019());
00064                             DEBUG("BM019 Status: forgotten state\n");
00065                     }
00066                     handled = true;
00067                 }
00068                 break;
00069 
00070                 case 'b':
00071                 case 'B':
00072                     DEBUG("Handling b\n");
00073                     if(i + 5 <= bytesRead) {
00074                         sprintf(answer,"+b!");
00075                         int adr = 0;
00076                         char b[3];
00077                         b[0] = params->data[i+4];
00078                         b[1] = params->data[i+5];
00079                         b[2] = 0;
00080                         sscanf(b,"%x",&adr);
00081                         DEBUG("beat in %d sec\n",adr);
00082                         ticker.attach(timerCallback, adr);
00083                         i+=5;
00084                         handled=true;
00085                     } else {
00086                         sprintf(answer,"-b!");
00087                         handled=true;
00088                     }
00089 
00090                     break;
00091                 case 'h':
00092                 case 'H': {
00093                     DEBUG("Handling h\n");
00094                     if(hybernateBM019()) {
00095                         sprintf(answer,"+h!");
00096                     } else {
00097                         DEBUG("BM019 did hybernate wake\n")
00098                         sprintf(answer,"-h!");
00099                     }
00100                     handled=true;
00101 
00102                 }
00103                 break;
00104 
00105                 case 'w':
00106                 case 'W': {
00107                     DEBUG("handle w\n")
00108 
00109                     if(wakeBM019(100)) {
00110                         DEBUG("BM019 did wake\n")
00111                         sprintf(answer,"+w!");
00112                     } else {
00113                         DEBUG("BM019 did NOT wake\n")
00114                         sprintf(answer,"-w!");
00115                     }
00116                     handled = true;
00117                 }
00118                 break;
00119 
00120                 case 'i':
00121                 case 'I': {
00122                     DEBUG("handle i\n");
00123                     BM019_IDN idn;
00124                     if(idnBM019(&idn)) {
00125                         sprintf(answer,"+i:");
00126                         int i;
00127                         for(i = 0; i < 13; i++) {
00128                             sprintf(&answer[strlen(answer)],"%x",idn.deviceID[i]);
00129                         }
00130                         sprintf(&answer[strlen(answer)],":");
00131                         sprintf(&answer[strlen(answer)],"%x%x!",idn.romCRC[0],idn.romCRC[1]);
00132                         DEBUG("answered: %s",answer);
00133                     } else {
00134                         DEBUG("BM019 failed idn\n")
00135                         sprintf(answer,"-i!");
00136                     }
00137 
00138                     handled = true;
00139                 }
00140                 break;
00141 
00142                 case 'p':
00143                 case 'P': {
00144                     DEBUG("handle p\n");
00145                     if(setProtocolISO_EIC_15693BM019((BM019_PROTOCOL_ISO_IEC_15693_BYTE_0)(
00146                                                          BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_0_CRC |
00147                                                          BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_1_SINGLE_SUBCARRIER |
00148                                                          BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_2_10_MODULATION |
00149                                                          BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_3_WAIT_FOR_SOF |
00150                                                          BM019_PROTOCOL_ISO_IEC_15693_BYTE_0_45_26_KBPS)
00151                                                     )) {
00152                         DEBUG("BM019 proto\n")
00153                         sprintf(answer,"+p!");
00154                     } else {
00155                         DEBUG("BM019 failed proto\n")
00156                         sprintf(answer,"-p!");
00157                     }
00158 
00159                     handled = true;
00160                 }
00161                 break;
00162 
00163                 case 'r':
00164                 case 'R': {
00165                     DEBUG("handle r\n");
00166                     resetBM019();
00167                     sprintf(answer,"+r!");
00168                     handled = true;
00169                 }
00170                 break;
00171 
00172                 case 'e':
00173                 case 'E': {
00174                     DEBUG("handle e\n");
00175                     if(echoBM019()) {
00176                         DEBUG("BM019 sent echo\n");
00177                         sprintf(answer,"+e!");
00178                     } else {
00179                         DEBUG("BM019 NOT sent echo\n");
00180                         sprintf(answer,"-e!");
00181                     }
00182                     handled = true;
00183                 }
00184                 break;
00185 
00186                 case 't':
00187                 case 'T': {
00188                     DEBUG("handle t\n");
00189                     BM019_TAG tag;
00190                     if(inventoryISO_IES_15693BM019(&tag)) {
00191                         DEBUG("BM019 answered inventory\n");
00192                         sprintf(answer,"+t:");
00193                         for(int i = 0; i < 8; i++) {
00194                             sprintf(&answer[strlen(answer)],"%02x",tag.uid[i]);
00195                         }
00196                         sprintf(&answer[strlen(answer)],"!");
00197                     } else {
00198                         DEBUG("BM019 NOT answered inventory\n");
00199                         sprintf(answer,"-t!");
00200                     }
00201                     handled = true;
00202                 }
00203                 break;
00204 
00205                 case 'd':
00206                 case 'D': {
00207                     DEBUG("handle d\n");
00208                     if(i + 5 <= bytesRead) {
00209                         int adr = 0;
00210                         char b[3];
00211                         b[0] = params->data[i+4];
00212                         b[1] = params->data[i+5];
00213                         b[2] = 0;
00214                         sscanf(b,"%x",&adr);
00215                         DEBUG("read from %#04x\n",adr);
00216                         i+=5;
00217                         uint8_t rb[256];
00218                         int l = readBM019(adr,rb,256);
00219                         if(l>0) {
00220                             DEBUG("BM019 answered read\n");
00221                             sprintf(answer,"+d:");
00222                             for(int i = 0; i < l; i++) {
00223                                 sprintf(&answer[strlen(answer)],"%02x",rb[i]);
00224                             }
00225                             sprintf(&answer[strlen(answer)],"!");
00226                         } else {
00227                             DEBUG("BM019 NOT answered read\n");
00228                             sprintf(answer,"-d!");
00229                         }
00230                     } else {
00231                         DEBUG("BM019 NOT answered read, no adr given\n");
00232                         sprintf(answer,"-d!");
00233                     }
00234                     handled = true;
00235                 }
00236                 break;
00237 
00238                 case 'm':
00239                 case 'M': {
00240                     DEBUG("handle multi d\n");
00241                     if(i + 10 <= bytesRead) {
00242                         int adr = 0;
00243                         char b[3];
00244                         b[0] = params->data[i+4];
00245                         b[1] = params->data[i+5];
00246                         b[2] = 0;
00247                         sscanf(b,"%x",&adr);
00248 
00249                         int count = 0;
00250                         b[0] = params->data[i+9];
00251                         b[1] = params->data[i+10];
00252                         b[2] = 0;
00253                         sscanf(b,"%x",&count);
00254                         DEBUG("read from %#04x for %d\n",adr,count);
00255                         i+=10;
00256                         uint8_t rb[256];
00257                         int l = readMultiBM019(adr,count,rb,256);
00258                         if(l>0) {
00259                             DEBUG("BM019 answered multi\n");
00260                             sprintf(answer,"+m:");
00261                             for(int i = 0; i < l; i++) {
00262                                 sprintf(&answer[strlen(answer)],"%02x",rb[i]);
00263                             }
00264                             sprintf(&answer[strlen(answer)],"!");
00265                         } else {
00266                             DEBUG("BM019 NOT answered multi\n");
00267                             sprintf(answer,"-m!");
00268                         }
00269                     } else {
00270                         DEBUG("BM019 NOT answered read, no adr&count given\n");
00271                         sprintf(answer,"-m!");
00272                     }
00273                     handled = true;
00274                 }
00275                 break;
00276             }
00277         }
00278 
00279         if(handled) {
00280             DEBUG("writing \"%s\" with len %d to ble\n",answer,strlen(answer));
00281             int l = strlen(answer);
00282             for(int i = 0; i*20 < strlen(answer); i++) {
00283                 int len = 20 < l ? 20 : l;
00284                 ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), (uint8_t *)&answer[i*20], len);
00285                 l -= 20;
00286             }
00287 
00288         } else {
00289             DEBUG("received %u bytes.. nothing handled.. echo\n", bytesRead);
00290             ble.updateCharacteristicValue(uartServicePtr->getRXCharacteristicHandle(), params->data, bytesRead);
00291         }
00292     }
00293 }
00294 
00295 int main(void)
00296 {
00297     initBM019();
00298 
00299     DEBUG("Initialising the nRF51822\n\r");
00300     ble.init();
00301     ble.onDisconnection(disconnectionCallback);
00302     ble.onDataWritten(onDataWritten);
00303 
00304     /* setup advertising */
00305     ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED);
00306     ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
00307     //ble.setAdvertisingType(GapAdvertisingParams::ADV_SCANNABLE_UNDIRECTED);
00308     ble.accumulateAdvertisingPayload(GapAdvertisingData::SHORTENED_LOCAL_NAME,
00309                                      (const uint8_t *)"BLE UART2NFC", sizeof("BLE UART2NFC") - 1);
00310 
00311 
00312     /*ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00313                                      (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00314     */
00315 
00316     ble.accumulateScanResponse(GapAdvertisingData::COMPLETE_LIST_128BIT_SERVICE_IDS,
00317                                (const uint8_t *)UARTServiceUUID_reversed, sizeof(UARTServiceUUID_reversed));
00318     ble.setAdvertisingInterval(1000); /* 1000ms; in multiples of 0.625ms. */
00319     //ble.setAdvertisingTimeout(0x1);
00320     ble.startAdvertising();
00321 
00322     UARTService uartService(ble);
00323     uartServicePtr = &uartService;
00324 
00325     while (true) {
00326         ble.waitForEvent();
00327     }
00328 }