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 RecordURI.cpp Source File

RecordURI.cpp

Go to the documentation of this file.
00001 /**
00002  ******************************************************************************
00003  * @file    RecordURI.cpp
00004  * @author  ST / Central Labs
00005  * @version V2.0.0
00006  * @date    28 Apr 2017
00007  * @brief   RecordURI 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 
00038 #include <cstring>
00039 
00040 #include "RecordURI.h"
00041 #include "RecordMail.h"
00042 #include "RecordSMS.h"
00043 #include "RecordGeo.h"
00044 
00045 namespace NDefLib {
00046 
00047 const uint8_t RecordURI::sNDEFUriIdCode = 'U';
00048 
00049 const std::string RecordURI::sKnowUriPrefix[] = { "", "http://www.",
00050     "https://www.", "http://", "https://", "tel:", "mailto:",
00051     "ftp://anonymous:anonymous@", "ftp://ftp.", "ftps://", "sftp://",
00052     "smb://", "nfs://", "ftp://", "dav://", "news:", "telnet://", "imap:",
00053     "rtsp://", "urn:", "pop:", "sip:", "sips:", "tftp:", "btspp://",
00054     "btl2cap://", "btgoep://", "tcpobex://", "irdaobex://", "file://",
00055     "urn:epc:id:", "urn:epc:tag", "urn:epc:pat:", "urn:epc:raw:",
00056     "urn:epc:", "urn:nfc:" };
00057 
00058 void RecordURI::set_record_header() {
00059     mRecordHeader.set_FNT(RecordHeader::NFC_well_known);
00060     mRecordHeader.set_type_length(sizeof(sNDEFUriIdCode));
00061 }
00062 
00063 RecordURI::RecordURI(knowUriId_t uriId, const std::string &uriContent) :
00064     mUriTypeId(uriId), mTypeString("") {
00065     set_content(uriContent);
00066     set_record_header();
00067 }
00068 
00069 RecordURI::RecordURI(const std::string &uriType, const std::string &uriContent) :
00070     mContent(uriContent),mUriTypeId(UNKNOWN), mTypeString(uriType)  {
00071     update_record_header();
00072     set_record_header();
00073 }
00074 
00075 uint16_t RecordURI::write(uint8_t *buffer) {
00076     uint16_t offset = 0;
00077     update_content();
00078 
00079     offset += mRecordHeader.write_header(buffer);
00080 
00081     buffer[offset++] = sNDEFUriIdCode;
00082     buffer[offset++] = (uint8_t) mUriTypeId;
00083 
00084     if (mUriTypeId == UNKNOWN) {
00085         std::memcpy(buffer + offset, mTypeString.c_str(), mTypeString.size());
00086         offset += mTypeString.size();
00087     }
00088 
00089     std::memcpy(buffer + offset, mContent.c_str(), mContent.size());
00090     offset += mContent.size();
00091     return offset;
00092 }
00093 
00094 RecordURI* RecordURI::parse(const RecordHeader &header,
00095     const uint8_t *buffer) {
00096     uint16_t offset = 0;
00097 
00098     if (buffer[offset++] != sNDEFUriIdCode) {
00099         return NULL;
00100     }
00101     knowUriId_t uriType = (knowUriId_t) buffer[offset++];
00102     //it is a standard type handle by a specific class
00103     if (uriType == MAIL) {
00104         return RecordMail::parse(header, buffer);
00105     }
00106 
00107     //is an standard type without a specific class
00108     if (uriType != UNKNOWN) {
00109         return new RecordURI(uriType, std::string((const char*) buffer + offset, header.get_payload_length() - 1));
00110     }
00111 
00112     //is an unknown type with a specific class
00113     RecordURI *r = RecordSMS::parse(header, buffer);
00114     if (r != NULL) {
00115         return r;
00116     }
00117     r = RecordGeo::parse(header, buffer);
00118     if (r != NULL) {
00119         return r;
00120     } //else is an unknown type without a specific class
00121 
00122     return new RecordURI(uriType,
00123         std::string((const char*) buffer + offset, header.get_payload_length() - 1));
00124 }
00125 
00126 } /* namespace NDefLib */
00127 
00128 
00129 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/