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@1:a0eeb478a45a, 2015-11-24 (annotated)
- Committer:
- giovannivisentini
- Date:
- Tue Nov 24 14:33:06 2015 +0000
- Revision:
- 1:a0eeb478a45a
- Parent:
- 0:04b82ae7aa43
- Child:
- 4:eaf6c49a86e4
implement tag reading
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 | 1:a0eeb478a45a | 12 | #include "RecordType/RecordAAR.h" |
giovannivisentini | 1:a0eeb478a45a | 13 | #include "RecordType/RecordMimeType.h" |
giovannivisentini | 1:a0eeb478a45a | 14 | #include "RecordType/RecordURI.h" |
giovannivisentini | 0:04b82ae7aa43 | 15 | |
giovannivisentini | 0:04b82ae7aa43 | 16 | namespace NDefLib { |
giovannivisentini | 0:04b82ae7aa43 | 17 | |
giovannivisentini | 0:04b82ae7aa43 | 18 | uint16_t Message::getByteLenght()const{ |
giovannivisentini | 0:04b82ae7aa43 | 19 | uint16_t lenght=2; //length size |
giovannivisentini | 0:04b82ae7aa43 | 20 | |
giovannivisentini | 0:04b82ae7aa43 | 21 | if(mRecords.size()==0) |
giovannivisentini | 0:04b82ae7aa43 | 22 | return lenght+EmptyRecord().getByteLenght(); |
giovannivisentini | 0:04b82ae7aa43 | 23 | |
giovannivisentini | 0:04b82ae7aa43 | 24 | std::vector<Record*>::const_iterator it = mRecords.begin(); |
giovannivisentini | 0:04b82ae7aa43 | 25 | const std::vector<Record*>::const_iterator end = mRecords.end(); |
giovannivisentini | 0:04b82ae7aa43 | 26 | |
giovannivisentini | 0:04b82ae7aa43 | 27 | for(;it!=end;++it){ |
giovannivisentini | 0:04b82ae7aa43 | 28 | lenght+= (*it)->getByteLenght(); |
giovannivisentini | 0:04b82ae7aa43 | 29 | }//for |
giovannivisentini | 0:04b82ae7aa43 | 30 | |
giovannivisentini | 0:04b82ae7aa43 | 31 | return lenght; |
giovannivisentini | 0:04b82ae7aa43 | 32 | }//getByteLenght |
giovannivisentini | 0:04b82ae7aa43 | 33 | |
giovannivisentini | 1:a0eeb478a45a | 34 | uint16_t Message::write(uint8_t *buffer) const { |
giovannivisentini | 0:04b82ae7aa43 | 35 | |
giovannivisentini | 0:04b82ae7aa43 | 36 | const uint16_t length =getByteLenght()-2; |
giovannivisentini | 0:04b82ae7aa43 | 37 | uint16_t offset=0; |
giovannivisentini | 0:04b82ae7aa43 | 38 | buffer[offset++]=(uint8_t)((length & 0xFF00)>>8); |
giovannivisentini | 0:04b82ae7aa43 | 39 | buffer[offset++]=(uint8_t)((length & 0x00FF)); |
giovannivisentini | 0:04b82ae7aa43 | 40 | |
giovannivisentini | 0:04b82ae7aa43 | 41 | const uint32_t nRecord = mRecords.size(); |
giovannivisentini | 0:04b82ae7aa43 | 42 | |
giovannivisentini | 0:04b82ae7aa43 | 43 | if(mRecords.size()==0){ |
giovannivisentini | 0:04b82ae7aa43 | 44 | offset += EmptyRecord().write(buffer+offset); |
giovannivisentini | 0:04b82ae7aa43 | 45 | return offset; |
giovannivisentini | 0:04b82ae7aa43 | 46 | }//else |
giovannivisentini | 0:04b82ae7aa43 | 47 | |
giovannivisentini | 0:04b82ae7aa43 | 48 | for(uint32_t i=0;i<nRecord; i++){ |
giovannivisentini | 0:04b82ae7aa43 | 49 | Record *r = mRecords[i]; |
giovannivisentini | 0:04b82ae7aa43 | 50 | |
giovannivisentini | 0:04b82ae7aa43 | 51 | r->setAsMiddleRecord(); |
giovannivisentini | 0:04b82ae7aa43 | 52 | if(i==0) |
giovannivisentini | 0:04b82ae7aa43 | 53 | r->setAsFirstRecord(); |
giovannivisentini | 0:04b82ae7aa43 | 54 | if(i==nRecord-1) |
giovannivisentini | 0:04b82ae7aa43 | 55 | r->setAsLastRecord(); |
giovannivisentini | 0:04b82ae7aa43 | 56 | |
giovannivisentini | 0:04b82ae7aa43 | 57 | offset+= r->write(buffer+offset); |
giovannivisentini | 0:04b82ae7aa43 | 58 | }//for |
giovannivisentini | 0:04b82ae7aa43 | 59 | |
giovannivisentini | 0:04b82ae7aa43 | 60 | return offset; |
giovannivisentini | 0:04b82ae7aa43 | 61 | }//write |
giovannivisentini | 0:04b82ae7aa43 | 62 | |
giovannivisentini | 1:a0eeb478a45a | 63 | void Message::parseMessage(const uint8_t *const rawNdefFile, |
giovannivisentini | 1:a0eeb478a45a | 64 | const uint8_t length,Message *msg){ |
giovannivisentini | 1:a0eeb478a45a | 65 | uint16_t offset=0; |
giovannivisentini | 1:a0eeb478a45a | 66 | Record *r; |
giovannivisentini | 0:04b82ae7aa43 | 67 | |
giovannivisentini | 1:a0eeb478a45a | 68 | Record::RecordHeader header; |
giovannivisentini | 0:04b82ae7aa43 | 69 | do{ |
giovannivisentini | 1:a0eeb478a45a | 70 | const uint8_t headerLenght = header.loadHeader(rawNdefFile+offset); |
giovannivisentini | 1:a0eeb478a45a | 71 | r = RecordText::parse(header,rawNdefFile+offset+headerLenght); |
giovannivisentini | 1:a0eeb478a45a | 72 | if(r==NULL) |
giovannivisentini | 1:a0eeb478a45a | 73 | r = RecordAAR::parse(header,rawNdefFile+offset+headerLenght); |
giovannivisentini | 1:a0eeb478a45a | 74 | if(r==NULL) |
giovannivisentini | 1:a0eeb478a45a | 75 | r = RecordMimeType::parse(header,rawNdefFile+offset+headerLenght); |
giovannivisentini | 1:a0eeb478a45a | 76 | if(r==NULL) |
giovannivisentini | 1:a0eeb478a45a | 77 | r = RecordURI::parse(header,rawNdefFile+offset+headerLenght); |
giovannivisentini | 0:04b82ae7aa43 | 78 | |
giovannivisentini | 1:a0eeb478a45a | 79 | offset += header.getRecordLength(); |
giovannivisentini | 1:a0eeb478a45a | 80 | msg->addRecord(r); |
giovannivisentini | 1:a0eeb478a45a | 81 | }while(offset<length); |
giovannivisentini | 0:04b82ae7aa43 | 82 | |
giovannivisentini | 0:04b82ae7aa43 | 83 | } |
giovannivisentini | 0:04b82ae7aa43 | 84 | |
giovannivisentini | 0:04b82ae7aa43 | 85 | |
giovannivisentini | 0:04b82ae7aa43 | 86 | } /* namespace NDefLib */ |