NFC API for mbed using the MicroNFCBoard as a peripheral
Dependents: MicroNFCBoardAPI_P2P_Client MicroNFCBoardAPI_Blink MicroNFCBoardAPI_Tag_Emulator MicroNFCBoardAPI_Tag_Reader ... more
Diff: micronfcboard.cpp
- Revision:
- 2:9b0733b8fa95
- Parent:
- 1:1d246e0872c6
diff -r 1d246e0872c6 -r 9b0733b8fa95 micronfcboard.cpp --- a/micronfcboard.cpp Fri Apr 24 12:59:31 2015 +0000 +++ b/micronfcboard.cpp Thu May 14 16:41:27 2015 +0000 @@ -26,9 +26,12 @@ #define STATUS_NDEF_BUSY (1 << 5) #define STATUS_NDEF_SUCCESS (1 << 6) -#define STATUS_TYPE_MASK (0xFF << 8) -#define STATUS_TYPE2 (2 << 8) -#define STATUS_P2P (8 << 8) +#define STATUS_TYPE_MASK (0xFFUL << 8) +#define STATUS_TYPE2 (2UL << 8) +#define STATUS_TYPE4 (4UL << 8) +#define STATUS_P2P (8UL << 8) + +#define STATUS_INITIATOR (1UL << 16) #define RECORD_URI 1 #define RECORD_TEXT 2 @@ -64,10 +67,16 @@ return _status & STATUS_CONNECTED; } -bool MicroNFCBoard::type2() +bool MicroNFCBoard::type2Tag() { updateStatus(); - return (_status & STATUS_TYPE_MASK) == STATUS_TYPE2; + return ((_status & STATUS_TYPE_MASK) == STATUS_TYPE2) && (_status & STATUS_INITIATOR); +} + +bool MicroNFCBoard::type4Emulator() +{ + updateStatus(); + return ((_status & STATUS_TYPE_MASK) == STATUS_TYPE4) && !(_status & STATUS_INITIATOR); } bool MicroNFCBoard::p2p() @@ -112,14 +121,14 @@ return _status & STATUS_NDEF_SUCCESS; } -void MicroNFCBoard::startPolling() +void MicroNFCBoard::startPolling(bool readerWriter, bool emulator, bool p2p) { - _transport.nfcPoll(true); + _transport.nfcPoll(readerWriter, emulator, p2p); } void MicroNFCBoard::stopPolling() { - _transport.nfcPoll(false); + _transport.nfcPoll(false, false, false); } void MicroNFCBoard::ndefRead() @@ -142,7 +151,6 @@ size_t recordCount = 0; _transport.nfcGetMessageInfo(&recordCount); - size_t recordNumber = 0; uint16_t info[4]; uint16_t type; @@ -260,5 +268,91 @@ return true; } +void MicroNFCBoard::writeNdefUri(const char* uri) +{ + _transport.nfcPrepareMessage(true, false); + + size_t uriPrefixLength = strlen(uri); + if( uriPrefixLength > 36 ) + { + uriPrefixLength = 36; + } + + uint8_t prefix = 0; + + _transport.nfcEncodePrefix(&prefix, uri, &uriPrefixLength); + + if( uriPrefixLength > strlen(uri) ) + { + uriPrefixLength = 0; + } + + size_t uriLength = strlen(uri) - uriPrefixLength; + uri += uriPrefixLength; + + const uint16_t info[] = {prefix, uriLength}; + _transport.nfcSetRecordInfo(0, RECORD_URI, info, 2); + + _transport.nfcSetMessageInfo(1); + _transport.nfcPrepareMessage(false, true); + + size_t off = 0; + while(uriLength > 0) + { + size_t cpyLength = uriLength; + if(cpyLength > 32) + { + cpyLength = 32; + } + _transport.nfcSetRecordData(0, 0, off, (uint8_t*)uri, cpyLength); + uriLength -= cpyLength; + off += cpyLength; + uri += cpyLength; + } +} + +void MicroNFCBoard::writeNdefText(const char* lang, const char* text) +{ + _transport.nfcPrepareMessage(true, false); + + size_t langLength = strlen(lang); + size_t textLength = strlen(text); + + const uint16_t info[] = {0 /* UTF-8 */, langLength, textLength}; + _transport.nfcSetRecordInfo(0, RECORD_TEXT, info, 3); + + _transport.nfcSetMessageInfo(1); + _transport.nfcPrepareMessage(false, true); + + size_t off = 0; + while(langLength > 0) + { + size_t cpyLength = langLength; + if(cpyLength > 32) + { + cpyLength = 32; + } + _transport.nfcSetRecordData(0, 0, off, (uint8_t*)lang, cpyLength); + langLength -= cpyLength; + off += cpyLength; + lang += cpyLength; + } + + off = 0; + while(textLength > 0) + { + size_t cpyLength = textLength; + if(cpyLength > 32) + { + cpyLength = 32; + } + _transport.nfcSetRecordData(0, 1, off, (uint8_t*)text, cpyLength); + textLength -= cpyLength; + off += cpyLength; + text += cpyLength; + } + +} +