Utility library to read and write Ndef messages from/to a Type4 NFC tag
Dependents: NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more
Fork of NDefLib by
NDEF NFC library
This library provides an abstract API to create NDEF formatted messages and records and to read/write them from/to a Type4 NFC Tag.
Implementations
At the moment, the NDEF API is implemented by X_NUCLEO_NFC01A1 and X_NUCLEO_NFC02A1 Dynamic NFC Tag libraries respectively driving the X-NUCLEO-NFC01A1 and X-NUCLEO-NFC02A1 boards.
Message.cpp@0:04b82ae7aa43, 2015-11-19 (annotated)
- Committer:
- giovannivisentini
- Date:
- Thu Nov 19 08:49:47 2015 +0000
- Revision:
- 0:04b82ae7aa43
- Child:
- 1:a0eeb478a45a
First Version, the tag is only write
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 0:04b82ae7aa43 | 1 | /* |
giovannivisentini | 0:04b82ae7aa43 | 2 | * Message.cpp |
giovannivisentini | 0:04b82ae7aa43 | 3 | * |
giovannivisentini | 0:04b82ae7aa43 | 4 | * Created on: Nov 6, 2015 |
giovannivisentini | 0:04b82ae7aa43 | 5 | * Author: giovanni visentini |
giovannivisentini | 0:04b82ae7aa43 | 6 | */ |
giovannivisentini | 0:04b82ae7aa43 | 7 | |
giovannivisentini | 0:04b82ae7aa43 | 8 | #include <cstdlib> |
giovannivisentini | 0:04b82ae7aa43 | 9 | #include "NDefLib/Message.h" |
giovannivisentini | 0:04b82ae7aa43 | 10 | #include "RecordType/EmptyRecord.h" |
giovannivisentini | 0:04b82ae7aa43 | 11 | #include "RecordType/RecordText.h" |
giovannivisentini | 0:04b82ae7aa43 | 12 | |
giovannivisentini | 0:04b82ae7aa43 | 13 | namespace NDefLib { |
giovannivisentini | 0:04b82ae7aa43 | 14 | |
giovannivisentini | 0:04b82ae7aa43 | 15 | uint16_t Message::getByteLenght()const{ |
giovannivisentini | 0:04b82ae7aa43 | 16 | uint16_t lenght=2; //length size |
giovannivisentini | 0:04b82ae7aa43 | 17 | |
giovannivisentini | 0:04b82ae7aa43 | 18 | if(mRecords.size()==0) |
giovannivisentini | 0:04b82ae7aa43 | 19 | return lenght+EmptyRecord().getByteLenght(); |
giovannivisentini | 0:04b82ae7aa43 | 20 | |
giovannivisentini | 0:04b82ae7aa43 | 21 | std::vector<Record*>::const_iterator it = mRecords.begin(); |
giovannivisentini | 0:04b82ae7aa43 | 22 | const std::vector<Record*>::const_iterator end = mRecords.end(); |
giovannivisentini | 0:04b82ae7aa43 | 23 | |
giovannivisentini | 0:04b82ae7aa43 | 24 | for(;it!=end;++it){ |
giovannivisentini | 0:04b82ae7aa43 | 25 | lenght+= (*it)->getByteLenght(); |
giovannivisentini | 0:04b82ae7aa43 | 26 | }//for |
giovannivisentini | 0:04b82ae7aa43 | 27 | |
giovannivisentini | 0:04b82ae7aa43 | 28 | return lenght; |
giovannivisentini | 0:04b82ae7aa43 | 29 | }//getByteLenght |
giovannivisentini | 0:04b82ae7aa43 | 30 | |
giovannivisentini | 0:04b82ae7aa43 | 31 | uint16_t Message::write(uint8_t *buffer){ |
giovannivisentini | 0:04b82ae7aa43 | 32 | |
giovannivisentini | 0:04b82ae7aa43 | 33 | const uint16_t length =getByteLenght()-2; |
giovannivisentini | 0:04b82ae7aa43 | 34 | uint16_t offset=0; |
giovannivisentini | 0:04b82ae7aa43 | 35 | buffer[offset++]=(uint8_t)((length & 0xFF00)>>8); |
giovannivisentini | 0:04b82ae7aa43 | 36 | buffer[offset++]=(uint8_t)((length & 0x00FF)); |
giovannivisentini | 0:04b82ae7aa43 | 37 | |
giovannivisentini | 0:04b82ae7aa43 | 38 | const uint32_t nRecord = mRecords.size(); |
giovannivisentini | 0:04b82ae7aa43 | 39 | |
giovannivisentini | 0:04b82ae7aa43 | 40 | if(mRecords.size()==0){ |
giovannivisentini | 0:04b82ae7aa43 | 41 | offset += EmptyRecord().write(buffer+offset); |
giovannivisentini | 0:04b82ae7aa43 | 42 | return offset; |
giovannivisentini | 0:04b82ae7aa43 | 43 | }//else |
giovannivisentini | 0:04b82ae7aa43 | 44 | |
giovannivisentini | 0:04b82ae7aa43 | 45 | for(uint32_t i=0;i<nRecord; i++){ |
giovannivisentini | 0:04b82ae7aa43 | 46 | Record *r = mRecords[i]; |
giovannivisentini | 0:04b82ae7aa43 | 47 | |
giovannivisentini | 0:04b82ae7aa43 | 48 | r->setAsMiddleRecord(); |
giovannivisentini | 0:04b82ae7aa43 | 49 | if(i==0) |
giovannivisentini | 0:04b82ae7aa43 | 50 | r->setAsFirstRecord(); |
giovannivisentini | 0:04b82ae7aa43 | 51 | if(i==nRecord-1) |
giovannivisentini | 0:04b82ae7aa43 | 52 | r->setAsLastRecord(); |
giovannivisentini | 0:04b82ae7aa43 | 53 | |
giovannivisentini | 0:04b82ae7aa43 | 54 | offset+= r->write(buffer+offset); |
giovannivisentini | 0:04b82ae7aa43 | 55 | }//for |
giovannivisentini | 0:04b82ae7aa43 | 56 | |
giovannivisentini | 0:04b82ae7aa43 | 57 | return offset; |
giovannivisentini | 0:04b82ae7aa43 | 58 | }//write |
giovannivisentini | 0:04b82ae7aa43 | 59 | |
giovannivisentini | 0:04b82ae7aa43 | 60 | void Message::parseMessage(uint8_t *rawNdefFile,std::vector<Record*> *parsedRecords){ |
giovannivisentini | 0:04b82ae7aa43 | 61 | |
giovannivisentini | 0:04b82ae7aa43 | 62 | //const uint16_t length = (rawNdefFile[0]<<8 | rawNdefFile[1]); |
giovannivisentini | 0:04b82ae7aa43 | 63 | //skip the message length info |
giovannivisentini | 0:04b82ae7aa43 | 64 | uint16_t offset=2; |
giovannivisentini | 0:04b82ae7aa43 | 65 | Record *r; |
giovannivisentini | 0:04b82ae7aa43 | 66 | do{ |
giovannivisentini | 0:04b82ae7aa43 | 67 | r = RecordText::parse(rawNdefFile+offset); |
giovannivisentini | 0:04b82ae7aa43 | 68 | if(r==NULL){ |
giovannivisentini | 0:04b82ae7aa43 | 69 | |
giovannivisentini | 0:04b82ae7aa43 | 70 | |
giovannivisentini | 0:04b82ae7aa43 | 71 | |
giovannivisentini | 0:04b82ae7aa43 | 72 | }//else |
giovannivisentini | 0:04b82ae7aa43 | 73 | |
giovannivisentini | 0:04b82ae7aa43 | 74 | offset += r->getByteLenght(); |
giovannivisentini | 0:04b82ae7aa43 | 75 | parsedRecords->push_back(r); |
giovannivisentini | 0:04b82ae7aa43 | 76 | }while(!r->isLastRecord()); |
giovannivisentini | 0:04b82ae7aa43 | 77 | |
giovannivisentini | 0:04b82ae7aa43 | 78 | } |
giovannivisentini | 0:04b82ae7aa43 | 79 | |
giovannivisentini | 0:04b82ae7aa43 | 80 | |
giovannivisentini | 0:04b82ae7aa43 | 81 | } /* namespace NDefLib */ |