ST / NDefLib

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of NDefLib by ST Expansion SW Team

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers RecordGeo.cpp Source File

RecordGeo.cpp

00001 /**
00002  ******************************************************************************
00003  * @file    RecordGEO.cpp
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   RecordGEO implementation.
00008  ******************************************************************************
00009  * @attention
00010  *
00011  * <h2><center>&copy; COPYRIGHT(c) 2015 STMicroelectronics</center></h2>
00012  *
00013  * Redistribution and use in source and binary forms, with or without modification,
00014  * are permitted provided that the following conditions are met:
00015  *   1. Redistributions of source code must retain the above copyright notice,
00016  *      this list of conditions and the following disclaimer.
00017  *   2. Redistributions in binary form must reproduce the above copyright notice,
00018  *      this list of conditions and the following disclaimer in the documentation
00019  *      and/or other materials provided with the distribution.
00020  *   3. Neither the name of STMicroelectronics nor the names of its contributors
00021  *      may be used to endorse or promote products derived from this software
00022  *      without specific prior written permission.
00023  *
00024  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00025  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00026  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00027  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00028  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00029  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00030  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00031  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00032  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00033  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00034  *
00035  ******************************************************************************
00036  */
00037 #include <cstdio>
00038 
00039 #include "RecordGeo.h"
00040 
00041 namespace NDefLib {
00042 
00043 const std::string RecordGeo::sGeoTag = ("geo:");
00044 
00045 //number of digits used for print the coordintate
00046 #define N_FLOAT_DIGIS 4
00047 #define N_INTEGER_DIGIS 3
00048 //string with the 2 coordinate format
00049 #define COORDINATE_WRITE_FORMAT "%3.4f,%3.4f"
00050 //+1 = sign, +1 = .
00051 #define COORDINATE_STR_SIZE (1+N_INTEGER_DIGIS+1+N_FLOAT_DIGIS)
00052 
00053 //string to use for read a couple of coordinate
00054 #define COORDINATE_READ_FORMAT "%f,%f"
00055 
00056 
00057 RecordGeo::RecordGeo(const float lat, const float lon) :
00058         RecordURI(sGeoTag), mLatitiude(lat), mLongitude(lon), mContentIsChange(true) {
00059 };
00060 
00061 void RecordGeo::update_content(){
00062     if(!mContentIsChange) {
00063         return;
00064     }
00065 
00066     //2 coordinate +1 for the separator +1 for the \0
00067     char buffer[2*COORDINATE_STR_SIZE+1+1];
00068     std::sprintf(buffer,COORDINATE_WRITE_FORMAT,mLatitiude,mLongitude);
00069     mContent.clear();
00070     mContent.append(buffer);
00071 
00072     mContentIsChange=false;
00073 }
00074 
00075 RecordGeo* RecordGeo::parse(const RecordHeader &header, const uint8_t * const buffer) {
00076     uint16_t offset = 0;
00077     if (buffer[offset++] != RecordURI::sNDEFUriIdCode) {
00078         return NULL;
00079     }
00080     if (buffer[offset++] != RecordURI::UNKNOWN) {
00081         return NULL;
00082     }
00083     if (sGeoTag.compare(0, sGeoTag.size(), (const char*) buffer + offset, sGeoTag.size()) != 0) {
00084         return NULL;
00085     }
00086     offset += sGeoTag.size();
00087 
00088     const std::string uriContent((const char*) (buffer + offset), header.get_payload_length() - offset);
00089 
00090     float lat, lon;
00091     //build the record only if both the coordinate are available
00092     if (std::sscanf(uriContent.c_str(),COORDINATE_READ_FORMAT,&lat,&lon)!=2) {
00093         return NULL;
00094     }
00095 
00096     return new RecordGeo(lat,lon);
00097 }
00098 
00099 } /* namespace NDefLib */
00100 
00101 
00102 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/