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.
RecordType/RecordGeo.cpp@21:72c86cbd49be, 2017-08-21 (annotated)
- Committer:
- giovannivisentini
- Date:
- Mon Aug 21 12:02:31 2017 +0000
- Revision:
- 21:72c86cbd49be
- Parent:
- 20:31f727872290
change on_message_write callback; the message parameter was unused and it pointed to an invalid object
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
giovannivisentini | 6:739e3211749d | 1 | /** |
giovannivisentini | 6:739e3211749d | 2 | ****************************************************************************** |
giovannivisentini | 6:739e3211749d | 3 | * @file RecordGEO.cpp |
giovannivisentini | 8:473f6e0b03df | 4 | * @author ST / Central Labs |
giovannivisentini | 19:13d84b136a62 | 5 | * @version V2.0.0 |
giovannivisentini | 19:13d84b136a62 | 6 | * @date 28 Apr 2017 |
giovannivisentini | 12:ed4d9b8d1410 | 7 | * @brief RecordGEO implementation. |
giovannivisentini | 6:739e3211749d | 8 | ****************************************************************************** |
giovannivisentini | 6:739e3211749d | 9 | * @attention |
giovannivisentini | 6:739e3211749d | 10 | * |
giovannivisentini | 6:739e3211749d | 11 | * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> |
giovannivisentini | 0:04b82ae7aa43 | 12 | * |
giovannivisentini | 6:739e3211749d | 13 | * Redistribution and use in source and binary forms, with or without modification, |
giovannivisentini | 6:739e3211749d | 14 | * are permitted provided that the following conditions are met: |
giovannivisentini | 6:739e3211749d | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
giovannivisentini | 6:739e3211749d | 16 | * this list of conditions and the following disclaimer. |
giovannivisentini | 6:739e3211749d | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
giovannivisentini | 6:739e3211749d | 18 | * this list of conditions and the following disclaimer in the documentation |
giovannivisentini | 6:739e3211749d | 19 | * and/or other materials provided with the distribution. |
giovannivisentini | 6:739e3211749d | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
giovannivisentini | 6:739e3211749d | 21 | * may be used to endorse or promote products derived from this software |
giovannivisentini | 6:739e3211749d | 22 | * without specific prior written permission. |
giovannivisentini | 6:739e3211749d | 23 | * |
giovannivisentini | 6:739e3211749d | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
giovannivisentini | 6:739e3211749d | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
giovannivisentini | 6:739e3211749d | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
giovannivisentini | 6:739e3211749d | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
giovannivisentini | 6:739e3211749d | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
giovannivisentini | 6:739e3211749d | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
giovannivisentini | 6:739e3211749d | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
giovannivisentini | 6:739e3211749d | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
giovannivisentini | 6:739e3211749d | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
giovannivisentini | 6:739e3211749d | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
giovannivisentini | 6:739e3211749d | 34 | * |
giovannivisentini | 6:739e3211749d | 35 | ****************************************************************************** |
giovannivisentini | 0:04b82ae7aa43 | 36 | */ |
giovannivisentini | 4:eaf6c49a86e4 | 37 | #include <cstdio> |
giovannivisentini | 0:04b82ae7aa43 | 38 | |
giovannivisentini | 1:a0eeb478a45a | 39 | #include "RecordGeo.h" |
giovannivisentini | 0:04b82ae7aa43 | 40 | |
giovannivisentini | 0:04b82ae7aa43 | 41 | namespace NDefLib { |
giovannivisentini | 12:ed4d9b8d1410 | 42 | |
giovannivisentini | 4:eaf6c49a86e4 | 43 | const std::string RecordGeo::sGeoTag = ("geo:"); |
giovannivisentini | 0:04b82ae7aa43 | 44 | |
giovannivisentini | 4:eaf6c49a86e4 | 45 | //number of digits used for print the coordintate |
giovannivisentini | 4:eaf6c49a86e4 | 46 | #define N_FLOAT_DIGIS 4 |
giovannivisentini | 4:eaf6c49a86e4 | 47 | #define N_INTEGER_DIGIS 3 |
giovannivisentini | 4:eaf6c49a86e4 | 48 | //string with the 2 coordinate format |
giovannivisentini | 4:eaf6c49a86e4 | 49 | #define COORDINATE_WRITE_FORMAT "%3.4f,%3.4f" |
giovannivisentini | 4:eaf6c49a86e4 | 50 | //+1 = sign, +1 = . |
giovannivisentini | 4:eaf6c49a86e4 | 51 | #define COORDINATE_STR_SIZE (1+N_INTEGER_DIGIS+1+N_FLOAT_DIGIS) |
giovannivisentini | 4:eaf6c49a86e4 | 52 | |
giovannivisentini | 4:eaf6c49a86e4 | 53 | //string to use for read a couple of coordinate |
giovannivisentini | 4:eaf6c49a86e4 | 54 | #define COORDINATE_READ_FORMAT "%f,%f" |
giovannivisentini | 4:eaf6c49a86e4 | 55 | |
giovannivisentini | 4:eaf6c49a86e4 | 56 | |
giovannivisentini | 4:eaf6c49a86e4 | 57 | RecordGeo::RecordGeo(const float lat, const float lon) : |
Davidroid | 20:31f727872290 | 58 | RecordURI(sGeoTag), mLatitiude(lat), mLongitude(lon), mContentIsChange(true) { |
giovannivisentini | 0:04b82ae7aa43 | 59 | }; |
giovannivisentini | 0:04b82ae7aa43 | 60 | |
giovannivisentini | 19:13d84b136a62 | 61 | void RecordGeo::update_content(){ |
Davidroid | 20:31f727872290 | 62 | if(!mContentIsChange) { |
giovannivisentini | 4:eaf6c49a86e4 | 63 | return; |
Davidroid | 20:31f727872290 | 64 | } |
giovannivisentini | 4:eaf6c49a86e4 | 65 | |
giovannivisentini | 4:eaf6c49a86e4 | 66 | //2 coordinate +1 for the separator +1 for the \0 |
giovannivisentini | 4:eaf6c49a86e4 | 67 | char buffer[2*COORDINATE_STR_SIZE+1+1]; |
giovannivisentini | 4:eaf6c49a86e4 | 68 | std::sprintf(buffer,COORDINATE_WRITE_FORMAT,mLatitiude,mLongitude); |
giovannivisentini | 4:eaf6c49a86e4 | 69 | mContent.clear(); |
giovannivisentini | 4:eaf6c49a86e4 | 70 | mContent.append(buffer); |
giovannivisentini | 4:eaf6c49a86e4 | 71 | |
giovannivisentini | 4:eaf6c49a86e4 | 72 | mContentIsChange=false; |
giovannivisentini | 4:eaf6c49a86e4 | 73 | } |
giovannivisentini | 4:eaf6c49a86e4 | 74 | |
Davidroid | 20:31f727872290 | 75 | RecordGeo* RecordGeo::parse(const RecordHeader &header, const uint8_t * const buffer) { |
giovannivisentini | 4:eaf6c49a86e4 | 76 | uint16_t offset = 0; |
Davidroid | 20:31f727872290 | 77 | if (buffer[offset++] != RecordURI::sNDEFUriIdCode) { |
giovannivisentini | 1:a0eeb478a45a | 78 | return NULL; |
Davidroid | 20:31f727872290 | 79 | } |
Davidroid | 20:31f727872290 | 80 | if (buffer[offset++] != RecordURI::UNKNOWN) { |
giovannivisentini | 4:eaf6c49a86e4 | 81 | return NULL; |
Davidroid | 20:31f727872290 | 82 | } |
Davidroid | 20:31f727872290 | 83 | if (sGeoTag.compare(0, sGeoTag.size(), (const char*) buffer + offset, sGeoTag.size()) != 0) { |
giovannivisentini | 4:eaf6c49a86e4 | 84 | return NULL; |
Davidroid | 20:31f727872290 | 85 | } |
giovannivisentini | 4:eaf6c49a86e4 | 86 | offset += sGeoTag.size(); |
giovannivisentini | 0:04b82ae7aa43 | 87 | |
Davidroid | 20:31f727872290 | 88 | const std::string uriContent((const char*) (buffer + offset), header.get_payload_length() - offset); |
giovannivisentini | 4:eaf6c49a86e4 | 89 | |
Davidroid | 20:31f727872290 | 90 | float lat, lon; |
giovannivisentini | 4:eaf6c49a86e4 | 91 | //build the record only if both the coordinate are available |
Davidroid | 20:31f727872290 | 92 | if (std::sscanf(uriContent.c_str(),COORDINATE_READ_FORMAT,&lat,&lon)!=2) { |
giovannivisentini | 1:a0eeb478a45a | 93 | return NULL; |
Davidroid | 20:31f727872290 | 94 | } |
Davidroid | 20:31f727872290 | 95 | |
giovannivisentini | 4:eaf6c49a86e4 | 96 | return new RecordGeo(lat,lon); |
giovannivisentini | 0:04b82ae7aa43 | 97 | } |
giovannivisentini | 0:04b82ae7aa43 | 98 | |
giovannivisentini | 0:04b82ae7aa43 | 99 | } /* namespace NDefLib */ |
Davidroid | 20:31f727872290 | 100 | |
Davidroid | 20:31f727872290 | 101 | |
Davidroid | 20:31f727872290 | 102 | /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/ |