AppNearMe / MicroNFCBoardAPI

Dependents:   MicroNFCBoardAPI_P2P_Client MicroNFCBoardAPI_Blink MicroNFCBoardAPI_Tag_Emulator MicroNFCBoardAPI_Tag_Reader ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers micronfcboard.cpp Source File

micronfcboard.cpp

00001 /*
00002 MicroNFCBoard mbed API
00003 
00004 Copyright (c) 2014-2015 AppNearMe Ltd
00005 
00006 Licensed under the Apache License, Version 2.0 (the "License");
00007 you may not use this file except in compliance with the License.
00008 You may obtain a copy of the License at
00009 
00010 http://www.apache.org/licenses/LICENSE-2.0
00011 
00012 Unless required by applicable law or agreed to in writing, software
00013 distributed under the License is distributed on an "AS IS" BASIS,
00014 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015 See the License for the specific language governing permissions and
00016 limitations under the License.
00017  */
00018 
00019 #include "micronfcboard.h"
00020 
00021 #define STATUS_POLLING          (1 << 0)
00022 #define STATUS_CONNECTED        (1 << 1)
00023 #define STATUS_NDEF_PRESENT     (1 << 2)
00024 #define STATUS_NDEF_READABLE    (1 << 3)
00025 #define STATUS_NDEF_WRITEABLE   (1 << 4)
00026 #define STATUS_NDEF_BUSY        (1 << 5)
00027 #define STATUS_NDEF_SUCCESS     (1 << 6)
00028 
00029 #define STATUS_TYPE_MASK        (0xFFUL << 8)
00030 #define STATUS_TYPE2            (2UL << 8)
00031 #define STATUS_TYPE4            (4UL << 8)
00032 #define STATUS_P2P              (8UL << 8)
00033 
00034 #define STATUS_INITIATOR        (1UL << 16)
00035 
00036 #define RECORD_URI  1
00037 #define RECORD_TEXT 2
00038 #define RECORD_SP   3
00039 
00040 MicroNFCBoard::MicroNFCBoard(PinName mosi, PinName miso, PinName sck, PinName cs, PinName irq) : _transport(mosi, miso, sck, cs, irq)
00041 {
00042 
00043 }
00044 
00045 void MicroNFCBoard::init()
00046 {
00047   _transport.init();
00048   _status = _transport.status();
00049 }
00050 
00051 void MicroNFCBoard::setLeds(bool led1, bool led2)
00052 {
00053   _transport.leds(led1, led2);
00054 }
00055 
00056 void MicroNFCBoard::updateStatus()
00057 {
00058   if(_transport.statusChanged())
00059   {
00060     _status = _transport.status();
00061   }
00062 }
00063 
00064 bool MicroNFCBoard::connected()
00065 {
00066   updateStatus();
00067   return _status & STATUS_CONNECTED;
00068 }
00069 
00070 bool MicroNFCBoard::type2Tag()
00071 {
00072   updateStatus();
00073   return ((_status & STATUS_TYPE_MASK) == STATUS_TYPE2) && (_status & STATUS_INITIATOR);
00074 }
00075 
00076 bool MicroNFCBoard::type4Emulator()
00077 {
00078   updateStatus();
00079   return ((_status & STATUS_TYPE_MASK) == STATUS_TYPE4) && !(_status & STATUS_INITIATOR);
00080 }
00081 
00082 bool MicroNFCBoard::p2p()
00083 {
00084   updateStatus();
00085   return (_status & STATUS_TYPE_MASK) == STATUS_P2P;
00086 }
00087 
00088 bool MicroNFCBoard::polling()
00089 {
00090   updateStatus();
00091   return _status & STATUS_POLLING;
00092 }
00093 
00094 bool MicroNFCBoard::ndefReadable()
00095 {
00096   updateStatus();
00097   return _status & STATUS_NDEF_READABLE;
00098 }
00099 
00100 bool MicroNFCBoard::ndefWriteable()
00101 {
00102   updateStatus();
00103   return _status & STATUS_NDEF_WRITEABLE;
00104 }
00105 
00106 bool MicroNFCBoard::ndefPresent()
00107 {
00108   updateStatus();
00109   return _status & STATUS_NDEF_PRESENT;
00110 }
00111 
00112 bool MicroNFCBoard::ndefBusy()
00113 {
00114   updateStatus();
00115   return _status & STATUS_NDEF_BUSY;
00116 }
00117 
00118 bool MicroNFCBoard::ndefSuccess()
00119 {
00120   updateStatus();
00121   return _status & STATUS_NDEF_SUCCESS;
00122 }
00123 
00124 void MicroNFCBoard::startPolling(bool readerWriter, bool emulator, bool p2p)
00125 {
00126   _transport.nfcPoll(readerWriter, emulator, p2p);
00127 }
00128 
00129 void MicroNFCBoard::stopPolling()
00130 {
00131   _transport.nfcPoll(false, false, false);
00132 }
00133 
00134 void MicroNFCBoard::ndefRead()
00135 {
00136   _transport.nfcOperation(true, false);
00137 }
00138 
00139 void MicroNFCBoard::ndefWrite()
00140 {
00141   _transport.nfcOperation(false, true);
00142 }
00143 
00144 bool MicroNFCBoard::readNdefUri(char* uri, size_t maxUriLength)
00145 {
00146   if(!ndefPresent())
00147   {
00148     return false;
00149   }
00150 
00151   size_t recordCount = 0;
00152   _transport.nfcGetMessageInfo(&recordCount);
00153 
00154   size_t recordNumber = 0;
00155   uint16_t info[4];
00156   uint16_t type;
00157   size_t infoCount = 4;
00158 
00159   for(recordNumber = 0; recordNumber < recordCount; recordNumber++)
00160   {
00161     _transport.nfcGetRecordInfo(recordNumber, &type, info, infoCount);
00162     if(type == RECORD_URI)
00163     {
00164       break;
00165     }
00166     if(type == RECORD_SP)
00167     {
00168       recordCount += info[1];
00169     }
00170   }
00171   if(recordNumber == recordCount)
00172   {
00173     return false;
00174   }
00175 
00176   //Recover prefix
00177   size_t length = maxUriLength - 1;
00178   _transport.nfcDecodePrefix(info[0], uri, &length);
00179 
00180   maxUriLength -= length;
00181   uri += length;
00182 
00183   if(maxUriLength <= 1)
00184   {
00185     return false;
00186   }
00187 
00188   length = info[1];
00189   if(length > maxUriLength - 1)
00190   {
00191     return false;
00192   }
00193 
00194   size_t off = 0;
00195   while(length > 0)
00196   {
00197     size_t cpyLength = length;
00198     if(cpyLength > 32)
00199     {
00200       cpyLength = 32;
00201     }
00202     _transport.nfcGetRecordData(recordNumber, 0, off, (uint8_t*)uri, cpyLength);
00203     length -= cpyLength;
00204     off += cpyLength;
00205     uri += cpyLength;
00206   }
00207 
00208   uri[0] = '\0';
00209 
00210   return true;
00211 }
00212 
00213 bool MicroNFCBoard::readNdefText(char* text, size_t maxTextLength)
00214 {
00215   if(!ndefPresent())
00216   {
00217     return false;
00218   }
00219 
00220   size_t recordCount = 0;
00221   _transport.nfcGetMessageInfo(&recordCount);
00222 
00223 
00224   size_t recordNumber = 0;
00225   uint16_t info[4];
00226   uint16_t type;
00227   size_t infoCount = 4;
00228 
00229   for(recordNumber = 0; recordNumber < recordCount; recordNumber++)
00230   {
00231     _transport.nfcGetRecordInfo(recordNumber, &type, info, infoCount);
00232     if(type == RECORD_TEXT)
00233     {
00234       break;
00235     }
00236     if(type == RECORD_SP)
00237     {
00238       recordCount += info[1];
00239     }
00240   }
00241   if(recordNumber == recordCount)
00242   {
00243     return false;
00244   }
00245 
00246   size_t length = info[2];
00247   if(length > maxTextLength - 1)
00248   {
00249     return false;
00250   }
00251 
00252   size_t off = 0;
00253   while(length > 0)
00254   {
00255     size_t cpyLength = length;
00256     if(cpyLength > 32)
00257     {
00258       cpyLength = 32;
00259     }
00260     _transport.nfcGetRecordData(recordNumber, 1, off, (uint8_t*)text, cpyLength);
00261     length -= cpyLength;
00262     off += cpyLength;
00263     text += cpyLength;
00264   }
00265 
00266   text[0] = '\0';
00267 
00268   return true;
00269 }
00270 
00271 void MicroNFCBoard::writeNdefUri(const char* uri)
00272 {
00273   _transport.nfcPrepareMessage(true, false);
00274 
00275   size_t uriPrefixLength = strlen(uri);
00276   if( uriPrefixLength > 36 )
00277   {
00278     uriPrefixLength = 36;
00279   }
00280 
00281   uint8_t prefix = 0;
00282     
00283   _transport.nfcEncodePrefix(&prefix, uri, &uriPrefixLength);
00284     
00285   if( uriPrefixLength > strlen(uri) )
00286   {
00287       uriPrefixLength = 0;
00288   }
00289 
00290   size_t uriLength = strlen(uri) - uriPrefixLength;
00291   uri += uriPrefixLength;
00292 
00293   const uint16_t info[] = {prefix, uriLength};
00294   _transport.nfcSetRecordInfo(0, RECORD_URI, info, 2);
00295 
00296   _transport.nfcSetMessageInfo(1);
00297   _transport.nfcPrepareMessage(false, true);
00298 
00299   size_t off = 0;
00300   while(uriLength > 0)
00301   {
00302     size_t cpyLength = uriLength;
00303     if(cpyLength > 32)
00304     {
00305       cpyLength = 32;
00306     }
00307     _transport.nfcSetRecordData(0, 0, off, (uint8_t*)uri, cpyLength);
00308     uriLength -= cpyLength;
00309     off += cpyLength;
00310     uri += cpyLength;
00311   }
00312 }
00313 
00314 void MicroNFCBoard::writeNdefText(const char* lang, const char* text)
00315 {
00316   _transport.nfcPrepareMessage(true, false);
00317 
00318   size_t langLength = strlen(lang);
00319   size_t textLength = strlen(text);
00320 
00321   const uint16_t info[] = {0 /* UTF-8 */, langLength, textLength};
00322   _transport.nfcSetRecordInfo(0, RECORD_TEXT, info, 3);
00323 
00324   _transport.nfcSetMessageInfo(1);
00325   _transport.nfcPrepareMessage(false, true);
00326 
00327   size_t off = 0;
00328   while(langLength > 0)
00329   {
00330     size_t cpyLength = langLength;
00331     if(cpyLength > 32)
00332     {
00333       cpyLength = 32;
00334     }
00335     _transport.nfcSetRecordData(0, 0, off, (uint8_t*)lang, cpyLength);
00336     langLength -= cpyLength;
00337     off += cpyLength;
00338     lang += cpyLength;
00339   }
00340 
00341   off = 0;
00342   while(textLength > 0)
00343   {
00344     size_t cpyLength = textLength;
00345     if(cpyLength > 32)
00346     {
00347       cpyLength = 32;
00348     }
00349     _transport.nfcSetRecordData(0, 1, off, (uint8_t*)text, cpyLength);
00350     textLength -= cpyLength;
00351     off += cpyLength;
00352     text += cpyLength;
00353   }
00354 
00355 }
00356 
00357 
00358