CUSTOMIZED FOR WATER MONITOR
Revision 0:51d7bdf88981, committed 2017-12-07
- Comitter:
- DuyLionTran
- Date:
- Thu Dec 07 17:07:22 2017 +0000
- Commit message:
- CUSTOMIZED FOR WATER MONITOR
Changed in this revision
diff -r 000000000000 -r 51d7bdf88981 EmptyRecord.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/EmptyRecord.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,73 @@ +/** + ****************************************************************************** + * @file EmptyRecord.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief Implement an Empyt Record. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_EMPTYRECORD_H_ +#define NDEFLIB_RECORDTYPE_EMPTYRECORD_H_ + +#include "NDefLib/Record.h" + +namespace NDefLib { + +/** + * Define an empty Record. + */ +class EmptyRecord: public Record { +public: + + EmptyRecord() { + mRecordHeader.set_FNT(RecordHeader::Empty); + mRecordHeader.set_MB(true); + mRecordHeader.set_ME(true); + mRecordHeader.set_type_length(0); + mRecordHeader.set_payload_length(0); + } + + /** + * Write the 3 bytes used to define an empty record. + * @param[out] buffer Buffer to write the record into. + * @return number of write bytes + */ + virtual uint16_t write(uint8_t *buffer) { + return mRecordHeader.write_header(buffer); + } //write + + virtual ~EmptyRecord() { } +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_EMPTYRECORD_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordAAR.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordAAR.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,82 @@ +/** + ****************************************************************************** + * @file RecordAAR.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordAAR implementation. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#include <cstring> +#include "RecordAAR.h" + +namespace NDefLib { + +const char RecordAAR::sRecordType[] = { 'a', 'n', 'd', 'r', 'o', 'i', 'd', '.', + 'c', 'o', 'm', ':', 'p', 'k', 'g' }; + +RecordAAR::RecordAAR(const std::string &packageName) : + mPackageName(packageName) { + mRecordHeader.set_FNT(RecordHeader::NFC_external); + mRecordHeader.set_type_length(sizeof(sRecordType)); + mRecordHeader.set_payload_length(mPackageName.size()); + +} + +uint16_t RecordAAR::write(uint8_t *buffer) { + uint16_t offset = 0; + offset += mRecordHeader.write_header(buffer); + std::memcpy(buffer + offset, sRecordType, sizeof(sRecordType)); + offset += sizeof(sRecordType); + std::memcpy(buffer + offset, mPackageName.c_str(), mPackageName.size()); + offset += mPackageName.size(); + return offset; +} + +RecordAAR* RecordAAR::parse(const RecordHeader &header, + const uint8_t *buffer) { + uint8_t offset = 0; + if ((header.get_FNT() != RecordHeader::NFC_external) + && (header.get_type_length() != sizeof(sRecordType))) { + return NULL; + } //else + + if (std::memcmp(buffer + offset, sRecordType, sizeof(sRecordType)) != 0) { + return NULL; + } //else tagType == android.com:pkg + + offset += sizeof(sRecordType); + + return new RecordAAR( + std::string((const char*) buffer + offset, + header.get_payload_length())); +} + +} /* namespace NDefLib */
diff -r 000000000000 -r 51d7bdf88981 RecordAAR.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordAAR.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,120 @@ +/** + ****************************************************************************** + * @file RecordAAR.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief Create a Record that can start an application in an Android mobile. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_RECORDAAR_H_ +#define NDEFLIB_RECORDTYPE_RECORDAAR_H_ + +#include <string> + +#include "NDefLib/Record.h" + +namespace NDefLib { + +/** + * Create a Record that can start an application in an Android mobile. + * @par The package data are copied inside the class. + */ +class RecordAAR: public Record { +public: + + /** + * Create an RecordAAR reading the data from the buffer. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return an object of type recordAAR or NULL + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordAAR* parse(const RecordHeader &header, + const uint8_t * const buffer); + + /** + * Build a new record. + * @param packageName Package of the application to start + */ + explicit RecordAAR(const std::string &packageName); + + /** + * Get the record type. + * @return TYPE_AAR + */ + virtual RecordType_t get_type() const { + return TYPE_AAR; + } //getType + + /** + * Get the package inside this record + * @return get the package inside this record + */ + const std::string& get_package() const { + return mPackageName; + } + + /** + * Change the package name of this record. + * @param package new package + */ + void set_package(const std::string& package){ + mPackageName=package; + mRecordHeader.set_payload_length(mPackageName.size()); + } + + virtual uint16_t write(uint8_t *buffer); + virtual ~RecordAAR() { }; + + /** + * compare two objects + * @return true if the records have the same package name + */ + bool operator==(const RecordAAR &other) const{ + return (mPackageName==other.mPackageName); + } + +private: + /** + * Application to start + */ + std::string mPackageName; + + /** + * String to use as record type for this record + */ + static const char sRecordType[]; +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDAAR_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordGeo.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordGeo.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,102 @@ +/** + ****************************************************************************** + * @file RecordGEO.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordGEO implementation. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#include <cstdio> + +#include "RecordGeo.h" + +namespace NDefLib { + +const std::string RecordGeo::sGeoTag = ("geo:"); + +//number of digits used for print the coordintate +#define N_FLOAT_DIGIS 4 +#define N_INTEGER_DIGIS 3 +//string with the 2 coordinate format +#define COORDINATE_WRITE_FORMAT "%3.4f,%3.4f" +//+1 = sign, +1 = . +#define COORDINATE_STR_SIZE (1+N_INTEGER_DIGIS+1+N_FLOAT_DIGIS) + +//string to use for read a couple of coordinate +#define COORDINATE_READ_FORMAT "%f,%f" + + +RecordGeo::RecordGeo(const float lat, const float lon) : + RecordURI(sGeoTag), mLatitiude(lat), mLongitude(lon), mContentIsChange(true) { +}; + +void RecordGeo::update_content(){ + if(!mContentIsChange) { + return; + } + + //2 coordinate +1 for the separator +1 for the \0 + char buffer[2*COORDINATE_STR_SIZE+1+1]; + std::sprintf(buffer,COORDINATE_WRITE_FORMAT,mLatitiude,mLongitude); + mContent.clear(); + mContent.append(buffer); + + mContentIsChange=false; +} + +RecordGeo* RecordGeo::parse(const RecordHeader &header, const uint8_t * const buffer) { + uint16_t offset = 0; + if (buffer[offset++] != RecordURI::sNDEFUriIdCode) { + return NULL; + } + if (buffer[offset++] != RecordURI::UNKNOWN) { + return NULL; + } + if (sGeoTag.compare(0, sGeoTag.size(), (const char*) buffer + offset, sGeoTag.size()) != 0) { + return NULL; + } + offset += sGeoTag.size(); + + const std::string uriContent((const char*) (buffer + offset), header.get_payload_length() - offset); + + float lat, lon; + //build the record only if both the coordinate are available + if (std::sscanf(uriContent.c_str(),COORDINATE_READ_FORMAT,&lat,&lon)!=2) { + return NULL; + } + + return new RecordGeo(lat,lon); +} + +} /* namespace NDefLib */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordGeo.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordGeo.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,157 @@ +/** + ****************************************************************************** + * @file RecordGEO.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief Specialize the @{link RecordURI} to handle a location position, the location + * is stored with latitude and longitude. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_RECORDGEO_H_ +#define NDEFLIB_RECORDTYPE_RECORDGEO_H_ + +#include "RecordURI.h" + +namespace NDefLib { + +/** + * Specialize the {@link RecordUri} to handle a location position, the location + * is stored with latitude and longitude. + * @par The coordinates are floats with 4 decimal positions. + */ +class RecordGeo: public RecordURI { +public: + + /** + * Create an RecordGeo reading the data from the buffer. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return an object of type RecordGeo or NULL + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordGeo* parse(const RecordHeader &header, + const uint8_t * const buffer); + + + /** + * Create a record that contains a gps coordinate. + * @param lat Latitude. + * @param lon Longitude. + */ + RecordGeo(const float lat, const float lon); + + virtual ~RecordGeo() { }; + + /** + * Return the longitude coordinate + * @return the longitude coordinate + */ + float get_longitude()const { + return mLongitude; + } + + /** + * Return the latitude coordinate + * @return the latitude coordinate + */ + float get_latitude()const { + return mLatitiude; + } + + /** + * Change the latitude coordinate. + * @param lat new latitude. + */ + void set_latitude(const float lat){ + mContentIsChange=true; + mLatitiude=lat; + } + + /** + * Change the longitude coordinate. + * @param lon new longitude + */ + void set_longitude(const float lon){ + mContentIsChange=true; + mLongitude=lon; + } + + /** + * Get the record type. + * @return TYPE_URI_GEOLOCATION + */ + virtual RecordType_t get_type() const { + return TYPE_URI_GEOLOCATION; + } //getType + + /** + * Compare two RecordGeo tags. + * @return true if the two tags have the same latitude and longitude + */ + bool operator==(const RecordGeo &other) const { + return (mLatitiude == other.mLatitiude) && + (mLongitude == other.mLongitude) ; + } + +protected: + + /** + * This function updates the uri message to write into the tag. + */ + virtual void update_content(); + +private: + + /** + * Latitude information. + */ + float mLatitiude; + + /** + * Longitude information. + */ + float mLongitude; + + /** + * True if the user has updated the latitude or longitude. + */ + bool mContentIsChange; + + /** + * Uri type to use for this record. + */ + static const std::string sGeoTag; +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDGEO_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordMail.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordMail.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,91 @@ +/** + ****************************************************************************** + * @file RecordMAIL.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordMail implementation. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#include "RecordMail.h" + +namespace NDefLib { + +const std::string RecordMail::sSubjectTag("?subject="); +const std::string RecordMail::sBodyTag("&body="); + +void RecordMail::update_content(){ + if (!mContentIsChange) { + return; + } + + mContent.reserve(mToAddress.size() + sSubjectTag.size() + mSubject.size() + sBodyTag.size() + mBody.size()); + mContent = mToAddress; + mContent += sSubjectTag; + mContent += mSubject; + mContent += sBodyTag; + mContent += mBody; + + mContentIsChange=false; +} + +RecordMail* RecordMail::parse(const RecordHeader &header, const uint8_t* buffer) { + //not a uri tag or a mail tag + if (buffer[0] != RecordURI::sNDEFUriIdCode || buffer[1] != RecordURI::MAIL) { + return NULL; + } + const std::string uriContent((const char*) (buffer + 2), header.get_payload_length() - 1); + + std::size_t subjectStart = uriContent.find(sSubjectTag); + if (subjectStart == std::string::npos) { //subject not present + return NULL; + } + const std::size_t destSize = subjectStart; + subjectStart += sSubjectTag.size(); //skip the tag string + + std::size_t bodyStart = uriContent.find(sBodyTag); + if (bodyStart == std::string::npos) { //body not present + return NULL; + } + const std::size_t subjectLenght = bodyStart - subjectStart; + bodyStart += sBodyTag.size(); // skip the tag string + + const std::size_t bodyLenght = uriContent.size() - bodyStart; + + return new RecordMail(uriContent.substr(0, destSize), + uriContent.substr(subjectStart, subjectLenght), + uriContent.substr(bodyStart, bodyLenght)); + +} + +} /* namespace NDefLib */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordMail.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordMail.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,164 @@ +/** + ****************************************************************************** + * @file RecordMail.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief Class that specializes the {@link RecordUri} to store a mail content. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#ifndef NDEFLIB_RECORDTYPE_RECORDMAIL_H_s +#define NDEFLIB_RECORDTYPE_RECORDMAIL_H_ + +#include "RecordURI.h" + +namespace NDefLib { + +/** + * Class that specializes the {@link RecordUri} to store a mail content. + */ +class RecordMail: public RecordURI { + +public: + + /** + * Create a RecordMail reading the data from the buffer. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return an object of type RecordMail or NULL + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordMail* parse(const RecordHeader &header, + const uint8_t* buffer); + + /** + * Create a mail. + * @param toAddress Mail recipient. + * @param subject Mail subject. + * @param msg Message. + */ + RecordMail(const std::string &toAddress, const std::string &subject, + const std::string &msg) : + RecordURI(RecordURI::MAIL), mToAddress(toAddress), mSubject( + subject), mBody(msg),mContentIsChange(true) { } ; + + virtual ~RecordMail() { }; + + /** + * @return the mail recipient address + */ + const std::string& get_to_address() const { + return mToAddress; + } + + /** + * @return the mail subject + */ + const std::string& get_subject() const { + return mSubject; + } + + /** + * @return the mail body + */ + const std::string& get_body() const { + return mBody; + } + + /** + * Change the mail recipient. + * @param dest Recipient address. + * @par The string is copied inside the class. + */ + void set_to_address(const std::string& dest){ + mContentIsChange=true; + mToAddress=dest; + } + + /** + * Change the mail subject. + * @param subj New mail subject. + * @par The string is copied inside the class. + */ + void set_subject(const std::string& subj) { + mContentIsChange=true; + mSubject=subj; + } + + /** + * Change the mail body. + * @param body New mail body. + * @par The string is copied inside the class. + */ + void set_body(const std::string& body) { + mContentIsChange=true; + mBody=body; + } + + /** + * Get the record type. + * @return TYPE_URI_MAIL + */ + virtual RecordType_t get_type() const { + return TYPE_URI_MAIL; + } //getType + + /** + * Compare 2 RecordMails. + *Â @return true if both the tags have the same addresses,subjects and bodies + */ + bool operator==(const RecordMail& other)const{ + return mToAddress == other.mToAddress && + mSubject == other.mSubject && + mBody == other.mBody; + } + +protected: + + /** + * Generate the uri content that encodes the mail content. + * @see RecordUri#updateContent + */ + virtual void update_content(); + +private: + + std::string mToAddress; ///< Mail address. + std::string mSubject; ///< Mail subject. + std::string mBody; ///< Mail body. + bool mContentIsChange; ///< True if the use update some mail field. + + static const std::string sSubjectTag; ///< String to use bofore the subject. + static const std::string sBodyTag; ///< String to use before the mail body. +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDMAIL_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordMimeType.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordMimeType.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,117 @@ +/** + ****************************************************************************** + * @file RecordMimeType.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordMimeType implementation + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#include <cstring> +#include "RecordMimeType.h" +#include "RecordVCard.h" +#include "RecordWifiConf.h" + +namespace NDefLib { + +void RecordMimeType::initialize_header_data() { + mRecordHeader.set_FNT(RecordHeader::Mime_media_type); + mRecordHeader.set_type_length(mMimeType.size()); + mRecordHeader.set_payload_length(mDataLength); +} + +RecordMimeType::RecordMimeType(const std::string &mimeType, const uint8_t *data, uint32_t nDataLenght) : + mMimeType(mimeType), mDataLength(nDataLenght),mData(NULL),mDataToFree(false) { + + if(data!=NULL && nDataLenght!=0) { + mDataToFree=true; + mData = new uint8_t[nDataLenght]; + std::memcpy(mData,data,nDataLenght); + } + + initialize_header_data(); +} + +RecordMimeType::RecordMimeType(const std::string &mimeType, const std::string &data) : + mMimeType(mimeType), mDataLength(data.size()), mData(new uint8_t[mDataLength]), mDataToFree(true) { + + std::memcpy(mData,data.c_str(),mDataLength); + + initialize_header_data(); +} + +uint16_t RecordMimeType::write(uint8_t *buffer) { + uint16_t offset = 0; + offset += mRecordHeader.write_header(buffer); + std::memcpy(buffer + offset, mMimeType.c_str(), mMimeType.size()); + offset += mMimeType.size(); + std::memcpy(buffer + offset, mData, mDataLength); + offset += mDataLength; + return offset; +} + +RecordMimeType* RecordMimeType::parse(const RecordHeader &header, + const uint8_t* buffer) { + uint32_t offset = 0; + if (header.get_FNT() != RecordHeader::Mime_media_type) { + return NULL; + } //else + + //check if it is a know subtype + RecordMimeType *r = RecordVCard::parse(header, buffer); + if (r != NULL) { + return r; + } + + r = RecordWifiConf::parse(header, buffer); + if (r != NULL) { + return r; + } + + //build a generic mameType + uint32_t dataOffset = offset + header.get_type_length()+header.get_id_length(); + return new RecordMimeType( + std::string((const char*) buffer + offset, header.get_type_length()), + buffer + dataOffset, header.get_payload_length()); +} + +void RecordMimeType::copy_mime_data(const uint8_t* data, uint32_t dataLength) { + delete_mime_data(); + mData = new uint8_t[dataLength]; + mDataLength=dataLength; + std::memcpy(mData,data,dataLength); + mRecordHeader.set_payload_length(dataLength); + mDataToFree=true; +} + +} /* namespace NDefLib */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordMimeType.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordMimeType.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,180 @@ +/** + ****************************************************************************** + * @file RecordMimeType.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief {@link Record} that contains data encoded as MimeType. + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_RECORDMIMETYPE_H_ +#define NDEFLIB_RECORDTYPE_RECORDMIMETYPE_H_ + +#include <cstring> +#include <string> + +#include "NDefLib/Record.h" + +namespace NDefLib { + +/** + * {@link Record} that contains data encoded as MimeType. + */ +class RecordMimeType: public Record { +public: + + /** + * Create an RecordMimeType reading the data from the buffer. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return an object of type RecordMimeType or NULL + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordMimeType* parse(const RecordHeader &header, + const uint8_t* buffer); + + /** + * Create a new record with a specific MIME type. + * @param mimeType MIME type. + * @param data Content. + * @param nDataLenght Content length in bytes. + * @par Data content is copied inside the class, unless NULL pointer is passed and + * @{link setMimeDataPointer} is used to set the data pointer. + */ + RecordMimeType(const std::string &mimeType, const uint8_t *data=NULL, + uint32_t nDataLenght=0); + + /** + * Create a new RecordMimeType. + * @param mimeType MIME type. + * @param data String that contains the data. + * @par The string is copied inside the class. + */ + RecordMimeType(const std::string &mimeType, const std::string &data); + + /** + * Return the mime type of the content. + * @return the mime type of the content. + */ + const std::string& get_mime_type() const { + return mMimeType; + } + + /** + * Change the data pointer used by this record. + * @param data Pointer used by this record. + * @param dataLength Number of byte to write in this record. + * @par The buffer is not copied or freed by this class. + * With this function only the data pointer is copied, so it must not be freed before the object is deallocated. + */ + void set_mime_data_pointer(uint8_t* data, uint32_t dataLength){ + delete_mime_data(); + mData = data; + mDataLength=dataLength; + mRecordHeader.set_payload_length(dataLength); + } + + /** + * Change the data linked with this record, with this function the + * data are copied inside the object. + * @param data Pointer to the data buffer. + * @param dataLength Number of bytes to write. + */ + void copy_mime_data(const uint8_t* data, uint32_t dataLength); + + /** + * @return number of bytes used to store the content data + */ + uint32_t get_mime_data_lenght() const { + return mDataLength; + } + + /** + * @return pointer to the content data + * @par DO NOT free this pointer, it is managed by the class. + */ + uint8_t const* get_mime_data() const { + return mData; + } + + /** + * Get the record type. + * @return TYPE_MIME + */ + virtual RecordType_t get_type() const { + return TYPE_MIME; + } //getType + + + virtual uint16_t write(uint8_t *buffer); + + /** + * If needed free the memory used to store the content data. + */ + virtual ~RecordMimeType() { + delete_mime_data(); + }; + + /** + * Compare two objects + * @return true if the 2 records have the same mime type, length and data. + */ + bool operator==(const RecordMimeType &other) const { + return mMimeType==other.mMimeType && + mDataLength == other.mDataLength && + (std::memcmp(mData,other.mData,mDataLength)==0); + } + +private: + + void initialize_header_data(); + + /** + * Delete the mData buffer if it is allocated by this class. + */ + void delete_mime_data(){ + if(mDataToFree && mData!=NULL){ + mDataToFree=false; + delete [] mData; + mData=NULL; + }//if + }//deleteMimeData + + const std::string mMimeType; + uint32_t mDataLength; + uint8_t *mData; + bool mDataToFree; + +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDMIMETYPE_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordSMS.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordSMS.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,93 @@ +/** + ****************************************************************************** + * @file RecordSMS.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordSMS implementation + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include <cstring> + +#include "RecordSMS.h" + +namespace NDefLib { + +const std::string RecordSMS::sSmsTag("sms:"); +const std::string RecordSMS::sBodyTag("?body="); + +RecordSMS::RecordSMS(const std::string &number, const std::string &message) : + RecordURI(sSmsTag), mNumber(number), mMsg(message),mContentIsChange(true){ +} + +void RecordSMS::update_content() { + if (!mContentIsChange) { + return; + } + + mContent.reserve(mNumber.size()+sBodyTag.size()+mMsg.size()); + + mContent = mNumber; + mContent += sBodyTag; + mContent += mMsg; + + mContentIsChange=false; +} + +RecordSMS* RecordSMS::parse(const RecordHeader &header, + const uint8_t *buffer) { + uint16_t offset = 0; + if (buffer[offset++] != RecordURI::sNDEFUriIdCode) { + return NULL; + } + if (buffer[offset++] != RecordURI::UNKNOWN) { + return NULL; + } + if (sSmsTag.compare(0, sSmsTag.size(), (const char*) buffer + offset, sSmsTag.size()) != 0) { + return NULL; + } + offset += sSmsTag.size(); + + const std::string uriContent((const char*) (buffer + offset), header.get_payload_length() - offset+1); + + std::size_t numberEnd = uriContent.find(sBodyTag); + if (numberEnd == std::string::npos) { + return NULL; + } + + return new RecordSMS(uriContent.substr(0, numberEnd), + uriContent.substr(numberEnd + sBodyTag.size())); +} + +} /* namespace NDefLib */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordSMS.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordSMS.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,157 @@ +/** + ****************************************************************************** + * @file RecordSMS.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief Extend the {@link RecordUri} to handle the SMS content + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_RECORDSMS_H_ +#define NDEFLIB_RECORDTYPE_RECORDSMS_H_ + +#include <string> + +#include "RecordURI.h" + +namespace NDefLib { + +/** + * Extend the {@link RecordUri} to handle the SMS content. + */ +class RecordSMS: public RecordURI { +public: + + /** + * Read a recordSMS. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return a RecordSMS type or NULL if it was not possible to build this record + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordSMS* parse(const RecordHeader &header, + const uint8_t *buffer); + + /** + * Build a sms record. + * @param number Sms recipient phone number. + * @param message Message to send. + * @par Strings are copied inside the class. + */ + RecordSMS(const std::string &number, const std::string &message); + + /** + * Get the record type. + * @return TYPE_URI_SMS + */ + virtual RecordType_t get_type() const { + return TYPE_URI_SMS; + } //getType + + /** + * Get recipient number. + * @return recipient number + */ + const std::string& get_number() const { + return mNumber; + } + + /** + * Get SMS text message. + * @return SMS text message + */ + const std::string& get_messagge() const { + return mMsg; + } + + /** + * Change the recipent number. + * @param number New recipent number. + */ + void set_number(const std::string &number){ + mNumber=number; + mContentIsChange=true; + } + + /** + * Change the message content. + * @param message New message. + */ + void set_message(const std::string &message){ + mMsg=message; + mContentIsChange=true; + } + + /** + * Compare 2 objects. + * @return true if the records have the same message and number. + */ + bool operator==(const RecordSMS &other) const { + return (mMsg == other.mMsg) && + (mNumber == other.mNumber); + } + + virtual ~RecordSMS() { + }; + +protected: + + /** + * Generate the URI content that encodes the sms. + * @see RecordUri#updateContent + */ + virtual void update_content(); + +private: + std::string mNumber; ///< Recipient number. + std::string mMsg; ///< Sms text. + + /** + * This variable is true when we need to update the URI content, + * it is an optimization to avoid to rebuild multiple times the URI content when + * data has not changed + */ + bool mContentIsChange; + + /** + * String used as URI type. + */ + static const std::string sSmsTag; + + /** + * String used to separate the number and body data. + */ + static const std::string sBodyTag; +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDSMS_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordText.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordText.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,98 @@ +/** + ****************************************************************************** + * @file RecordText.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordText implementation + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include <cstring> +#include <NDefLib/RecordType/RecordText.h> + +namespace NDefLib { + +const uint8_t RecordText::NDEFTextIdCode = 'T'; + +void RecordText::set_record_header() { + mRecordHeader.set_FNT(RecordHeader::NFC_well_known); + mRecordHeader.set_type_length(sizeof(NDEFTextIdCode)); + update_playload_length(); +} + +RecordText::RecordText(const std::string &text) : + mEncode(UTF8), mLanguage("en"), mText(text), mTextStatus (get_text_status(mEncode, mLanguage)) { + set_record_header(); +} + +RecordText::RecordText(const TextEncoding encoding, const std::string &language, + const std::string &text) : + mEncode(encoding), mLanguage(language), mText(text), mTextStatus(get_text_status(mEncode, mLanguage)) { + set_record_header(); +} + +uint16_t RecordText::write(uint8_t *buffer) { + int16_t offset = 0; + offset += mRecordHeader.write_header(buffer); + + buffer[offset++] = NDEFTextIdCode; + buffer[offset++] = mTextStatus; + + std::memcpy(buffer + offset, mLanguage.c_str(), mLanguage.size()); + offset += mLanguage.size(); + std::memcpy(buffer + offset, mText.c_str(), mText.size()); + offset += mText.size(); + + return offset; +} + +RecordText* RecordText::parse(const RecordHeader &header, const uint8_t * const buffer) { + uint32_t index = 0; + if (header.get_FNT() == RecordHeader::NFC_well_known && buffer[index++] == NDEFTextIdCode) { + const uint8_t textStatus = buffer[index++]; + const TextEncoding enc = get_encoding(textStatus); + const uint8_t langSize = get_language_length(textStatus); + //-1 is the textStatus + const uint8_t textSize = header.get_payload_length() - langSize - 1; + + return new RecordText(enc, + std::string((const char*) (buffer + index), langSize), + std::string((const char*) (buffer + index + langSize), + textSize)); + } else { + return NULL; + } +} + +} /* namespace NDefLib */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordText.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordText.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,198 @@ +/** + ****************************************************************************** + * @file RecordText.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief {@link Record} containing a simple text + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + + +#ifndef NDEFLIB_RECORDTYPE_RECORDTEXT_H_ +#define NDEFLIB_RECORDTYPE_RECORDTEXT_H_ +#include <string> +#include <NDefLib/Record.h> + +namespace NDefLib { + +/** + * {@link Record} containing a simple text. + * @par The text is copied inside the class. + */ +class RecordText: public Record { +public: + + /** + * Read a recordText from a buffer. + * @param header Record header. + * @param buffer Buffer to read the record content from. + * @return a record of type Text or NULL if it was not possible build this type of record + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordText* parse(const RecordHeader &header, + const uint8_t * const buffer); + + /** + * Set the type of encoding used to store the text data + */ + typedef enum { + UTF8, //!< UTF8 + UTF16,//!< UTF16 + } TextEncoding; + + /** + * Build a utf8, English text record. + * @param text Text to store in the record. + */ + explicit RecordText(const std::string &text=""); + + /** + * Build a text record. + * @param encoding Rype used to store the message. + * @param language Language used in the text. + * @param Text record text. + */ + RecordText(const TextEncoding encoding, const std::string &language, + const std::string &text); + + /** + * Get the record type. + * @return TYPE_TEXT + */ + virtual RecordType_t get_type() const { + return TYPE_TEXT; + } //getType + + /** + * Get the text inside this record. + * @return the text content + */ + const std::string& get_text() const { + return mText; + } + + /** + * Change the text content. + * @param text New text to store. + */ + void set_text (const std::string &text){ + mText = text; + update_playload_length(); + } + + /** + * Get the language used in the text. + * @return Language used in the text. + */ + const std::string& get_language()const{ + return mLanguage; + } + + /** + * Get the encoding used to store the text. + * @return get the encoding used to store the text + */ + TextEncoding get_encoding()const{ + return mEncode; + } + + /** + * Compare 2 objects. + * @return true if the objects have the same encoding,language and text + */ + bool operator==(const RecordText &other)const{ + return mTextStatus == other.mTextStatus && + mLanguage == other.mLanguage && + mText == other.mText; + + } + + virtual uint16_t write(uint8_t *buffer); + virtual ~RecordText() { }; + +private: + + void set_record_header(); + + void update_playload_length(){ + mRecordHeader.set_payload_length(1 + mLanguage.size() + mText.size()); + } + + /** + * encode the text encoding type and language size in a single byte + * @param enc encoding used by this record + * @param language language used by this record + * @return text encoding and language size in a single byte + */ + static uint8_t get_text_status(TextEncoding enc, + const std::string &language) { + uint8_t status = language.size(); + if (enc == UTF16) { + status &= 0x80; // set to 1 the bit 7 + } //if + return status; + } //getTextStatus + + /** + * extract the encoding information from a status byte + * @param textStatus status byte + * @return encoding used by this record + */ + static TextEncoding get_encoding(const uint8_t textStatus) { + if ((textStatus & 0x80) == 0) + return UTF8; + else + return UTF16; + } + + /** + * extract language string length from a status byte + * @param textStatus status byte + * @return number of bytes needed to store the language name used by this record + */ + static uint8_t get_language_length(const uint8_t textStatus) { + //take the fist 5 bits + return textStatus & 0x1F; + } + + const TextEncoding mEncode; + const std::string mLanguage; + std::string mText; + + const uint8_t mTextStatus; + + static const uint8_t NDEFTextIdCode; + +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDTEXT_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordURI.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordURI.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,129 @@ +/** + ****************************************************************************** + * @file RecordURI.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordURI implementation + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#include <cstring> + +#include "RecordURI.h" +#include "RecordMail.h" +#include "RecordSMS.h" +#include "RecordGeo.h" + +namespace NDefLib { + +const uint8_t RecordURI::sNDEFUriIdCode = 'U'; + +const std::string RecordURI::sKnowUriPrefix[] = { "", "http://www.", + "https://www.", "http://", "https://", "tel:", "mailto:", + "ftp://anonymous:anonymous@", "ftp://ftp.", "ftps://", "sftp://", + "smb://", "nfs://", "ftp://", "dav://", "news:", "telnet://", "imap:", + "rtsp://", "urn:", "pop:", "sip:", "sips:", "tftp:", "btspp://", + "btl2cap://", "btgoep://", "tcpobex://", "irdaobex://", "file://", + "urn:epc:id:", "urn:epc:tag", "urn:epc:pat:", "urn:epc:raw:", + "urn:epc:", "urn:nfc:" }; + +void RecordURI::set_record_header() { + mRecordHeader.set_FNT(RecordHeader::NFC_well_known); + mRecordHeader.set_type_length(sizeof(sNDEFUriIdCode)); +} + +RecordURI::RecordURI(knowUriId_t uriId, const std::string &uriContent) : + mUriTypeId(uriId), mTypeString("") { + set_content(uriContent); + set_record_header(); +} + +RecordURI::RecordURI(const std::string &uriType, const std::string &uriContent) : + mContent(uriContent),mUriTypeId(UNKNOWN), mTypeString(uriType) { + update_record_header(); + set_record_header(); +} + +uint16_t RecordURI::write(uint8_t *buffer) { + uint16_t offset = 0; + update_content(); + + offset += mRecordHeader.write_header(buffer); + + buffer[offset++] = sNDEFUriIdCode; + buffer[offset++] = (uint8_t) mUriTypeId; + + if (mUriTypeId == UNKNOWN) { + std::memcpy(buffer + offset, mTypeString.c_str(), mTypeString.size()); + offset += mTypeString.size(); + } + + std::memcpy(buffer + offset, mContent.c_str(), mContent.size()); + offset += mContent.size(); + return offset; +} + +RecordURI* RecordURI::parse(const RecordHeader &header, + const uint8_t *buffer) { + uint16_t offset = 0; + + if (buffer[offset++] != sNDEFUriIdCode) { + return NULL; + } + knowUriId_t uriType = (knowUriId_t) buffer[offset++]; + //it is a standard type handle by a specific class + if (uriType == MAIL) { + return RecordMail::parse(header, buffer); + } + + //is an standard type without a specific class + if (uriType != UNKNOWN) { + return new RecordURI(uriType, std::string((const char*) buffer + offset, header.get_payload_length() - 1)); + } + + //is an unknown type with a specific class + RecordURI *r = RecordSMS::parse(header, buffer); + if (r != NULL) { + return r; + } + r = RecordGeo::parse(header, buffer); + if (r != NULL) { + return r; + } //else is an unknown type without a specific class + + return new RecordURI(uriType, + std::string((const char*) buffer + offset, header.get_payload_length() - 1)); +} + +} /* namespace NDefLib */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordURI.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordURI.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,254 @@ +/** + ****************************************************************************** + * @file RecordURI.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief {@link Record} that contains an URI address + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_RECORDURI_H_ +#define NDEFLIB_RECORDTYPE_RECORDURI_H_ +#include <map> +#include <string> +#include <NDefLib/Record.h> + +namespace NDefLib { + +/** + * {@link Record} that contains an URI address. + */ +class RecordURI: public Record { + +public: + + /** + * Load a record URI from a buffer. + * @param header Record header. + * @param buffer Buffer to read the tag playload from. + * @return record or NULL if it was not possible build it + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordURI* parse(const RecordHeader &header, + const uint8_t *buffer); + + /** + * If you want encode an know URI you can use this define to + * avoid to encode the URI type as string + */ + typedef enum { + UNKNOWN = 0X00, //!< UNKNOWN + HTTP_WWW = 0X01, //!< HTTP_WWW + HTTPS_WWW = 0X02, //!< HTTPS_WWW + HTTP = 0X03, //!< HTTP + HTTPS = 0X04, //!< HTTPS + TEL = 0x05, //!< TEL + MAIL = 0X06, //!< MAIL + FTP_ANONIMUS = 0X07,//!< FTP_ANONIMUS + FTP_FTP = 0X08, //!< FTP_FTP + FTPS = 0X09, //!< FTPS + SFTP = 0X0A, //!< SFTP + SMB = 0X0B, //!< SMB + NFS = 0X0C, //!< NFS + FTP = 0X0d, //!< FTP + DAV = 0X0E, //!< DAV + NEWS = 0X0F, //!< NEWS + TELNET = 0X10, //!< TELNET + IMAP = 0X11, //!< IMAP + RTSP = 0X12, //!< RTSP + URN = 0X13, //!< URN + POP = 0X14, //!< POP + SIP = 0X15, //!< SIP + SIPS = 0X016, //!< SIPS + TFTP = 0X017, //!< TFTP + BTSPP = 0x018, //!< BTSPP + BTL2CAP = 0x019, //!< BTL2CAP + BTGOEP = 0X01A, //!< BTGOEP + TCPOBEX = 0X1B, //!< TCPOBEX + IRDAOBEX = 0X1C, //!< IRDAOBEX + FILE = 0X1D, //!< FILE + URN_EPC_ID = 0X1E, //!< URN_EPC_ID + URN_EPC_TAG = 0X1F, //!< URN_EPC_TAG + URN_EPC_PAT = 0X20, //!< URN_EPC_PAT + URN_EPC_RAW = 0X21, //!< URN_EPC_RAW + URN_EPC = 0X22, //!< URN_EPC + URN_NFC = 0X23 //!< URN_NFC + } knowUriId_t; + + /** + * Build RecordUri with a defined URI type. + * @param URIId URI type. + * @param URIContent URI content. + * @par The URI prefix is removed. + * @par The string is copied inside the class. + */ + explicit RecordURI(knowUriId_t URIId, const std::string &URIContent=""); + + /** + * Build a custom URI type. + * @param URIType string with the URI type. + * @param URIContent URI content. + */ + explicit RecordURI(const std::string &URIType, const std::string &URIContent=""); + + /** + * Get the record type. + * @return TYPE_URI + */ + virtual RecordType_t get_type() const { + return TYPE_URI; + } //getType + + /** + * Returns the URI type. + * @return URI type inside this record, UNKNOWN if unknown + */ + knowUriId_t get_uri_id() const { + return mUriTypeId; + } + + virtual uint16_t get_byte_length() { + update_content_and_header(); + return mRecordHeader.get_record_length(); + } + + /** + * Returns the URI content. + * @return URI content + */ + std::string& get_content() { + update_content_and_header(); + return mContent; + } + + /** + * Change the URI content. + * @param URI new URI content. + */ + void set_content(const std::string &URI){ + if(mUriTypeId!=UNKNOWN) + store_removeing_prefix(sKnowUriPrefix[mUriTypeId],URI); + else + mContent=URI; + update_content_and_header(); + } + + /** + * If the URI type Id is {@code UNKNOWN} this return the user + * URI type. + * @return URI type set by the user or an empyt string + */ + const std::string& get_uri_type() const { + return mTypeString; + } + + bool operator==(const RecordURI &other) const { + return (mUriTypeId==other.mUriTypeId) && + (mTypeString==other.mTypeString) && + (mContent==other.mContent); + } + + virtual uint16_t write(uint8_t *buffer); + virtual ~RecordURI() { + }; + +protected: + + /** + * Record id to write to be recognizable as an URI record. + */ + static const uint8_t sNDEFUriIdCode; + + /** + * A subclass must implement this function to store the tag content and update the mContent + * variable accordingly. + * @par You should not call this function directly but use updateContentAndHeader that keeps the + * header information in sync with the content. + */ + virtual void update_content(){}; + + /** + * Update the tag content and update the header with the new content size. + */ + void update_content_and_header(){ + update_content(); + update_record_header(); + } + + /** + * The subclass must store in this variable the content to write to the tag. + * This class will ask to update the content throught the updateContent callback. + */ + std::string mContent; + +private: + + /** + * Set the record header flags. + */ + void set_record_header(); + + /** + * Set the correct size of the payload. + */ + void update_record_header(){ + //+1 = size of the URITypeId + mRecordHeader.set_payload_length(1 + mTypeString.size() + mContent.size()); + } + + void store_removeing_prefix(const std::string &prefix,const std::string &content){ + //check that the content doens't contain the prefix + if (content.compare(0, prefix.size(), prefix) == 0) { + mContent = std::string(content, prefix.size()); + } else + mContent = content; + } + + /** + * URI type used by this record + */ + const knowUriId_t mUriTypeId; + + /** + * In case of unknown URI type, it stores the used defined URI type. + */ + const std::string mTypeString; + + + /** + * Array of known prefix of known URI type + */ + static const std::string sKnowUriPrefix[]; +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDURI_H_ */
diff -r 000000000000 -r 51d7bdf88981 RecordVCard.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordVCard.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,146 @@ +/** + ****************************************************************************** + * @file RecordVCard.cpp + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief RecordVCard implementation + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#include <cstring> +#include "RecordVCard.h" + +namespace NDefLib { + +const std::string RecordVCard::sEmptyTagContent(""); +const std::string RecordVCard::sVcardMimeType("text/vcard"); +const std::string RecordVCard::sStartVCardTag("BEGIN:VCARD\nVERSION:3.0\n"); +const std::string RecordVCard::sStartFieldTag[] = { + "ADR:", "ADR;TYPE=home:", "ADR;TYPE=work:", "AGENT:", "BDAY:", + "CATEGORIES:", "EMAIL:", "EMAIL;TYPE=home:", "EMAIL;TYPE=work:", + "FN:", "GEO:", "IMPP:", "KEY;TYPE=PGP:", + "KEY;TYPE=PGP;ENCODING=B:","LOGO:", "LOGO;VALUE=uri:", + "LOGO;ENCODING=B;", "N:", "NICKNAME:", "NOTE:", "ORG:", + "PHOTO;VALUE=uri:", "PHOTO;ENCODING=B;", "REV:", "SOURCE:", + "TEL:", "TEL;TYPE=HOME:", "TEL;TYPE=WORK:", "TEL;TYPE=CELL:", + "TITLE:", "URL:" +}; + +const std::string RecordVCard::sEndFieldTag("\n"); +const std::string RecordVCard::sEndVCardTag("END:VCARD"); + +RecordVCard::RecordVCard(const VCardInfo_t &info) : + RecordMimeType(sVcardMimeType), mCardInfo(info),mContentIsChange(true) { + update_content_info_string(); +} + +void RecordVCard::update_content_info_string() { + if (!mContentIsChange) { + return; + } + + mCardInfoString = sStartVCardTag; + + VCardInfo_t::const_iterator it = mCardInfo.begin(); + VCardInfo_t::const_iterator end = mCardInfo.end(); + + for (; it != end; ++it) { + mCardInfoString += sStartFieldTag[it->first]; + mCardInfoString += it->second; + mCardInfoString += sEndFieldTag; + } + + mCardInfoString += sEndVCardTag; + + set_mime_data_pointer((uint8_t*)mCardInfoString.data(),mCardInfoString.size()); + + mContentIsChange=false; +} + +uint16_t RecordVCard::find_VCard_field_data_lenght(const std::string &content, uint16_t offset) { + std::size_t pos = content.find(sEndFieldTag, offset); + if (pos == std::string::npos) { //if we don't find the end, lets consume all the content + return content.size() - offset; + } + return content.find(sEndFieldTag, offset) - offset; +} + +int8_t RecordVCard::find_VCard_field_type(const std::string &content, uint16_t offset) { + const uint32_t nFieldTag = sizeof(sStartFieldTag) / sizeof(sStartFieldTag[0]); + for (uint32_t i = 0; i < nFieldTag; i++) { + if (content.find(sStartFieldTag[i], offset) != std::string::npos) + return i; + } + + return -1; +} + +RecordVCard* RecordVCard::parse(const RecordHeader &header, const uint8_t* buffer) { + if (header.get_FNT() != RecordHeader::Mime_media_type || header.get_type_length() != sVcardMimeType.size()) { + return NULL; + } + if (sVcardMimeType.compare(0, sVcardMimeType.size(), (const char*) buffer, sVcardMimeType.size()) != 0) { + return NULL; + } + buffer += header.get_type_length(); + //we are at the start of the vcard data + if (sStartVCardTag.compare(0, sStartVCardTag.size(), (const char*) (buffer), sStartVCardTag.size()) != 0) { + return NULL; + } + //the version is ok + const std::string vCardContent((const char*) buffer + sStartVCardTag.size(), + header.get_payload_length() - sStartVCardTag.size()); + uint16_t offset = 0; + buffer += sStartVCardTag.size(); // for debug + VCardInfo_t info; + do { + const int8_t type = find_VCard_field_type(vCardContent, offset); + + if (type >= 0) { //if is a valid type + const VCardField_t fieldType = (VCardField_t) type; + offset += sStartFieldTag[fieldType].size(); // skip the tag name + //search the tag size + const uint16_t length = find_VCard_field_data_lenght(vCardContent, offset); + info[fieldType] = vCardContent.substr(offset, length); + offset += length + sEndFieldTag.size(); + } else { + //unknow field, skip until next one + offset += find_VCard_field_data_lenght(vCardContent, offset) + sEndFieldTag.size(); + } + + } while (offset < vCardContent.size()); + + return new RecordVCard(info); +} + +} /* namespace NDefLib */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordVCard.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordVCard.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,221 @@ +/** + ****************************************************************************** + * @file RecordVCard.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief {@link RecordMimeType} that contains a VCard data + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_RECORDVCARD_H_ +#define NDEFLIB_RECORDTYPE_RECORDVCARD_H_ + +#include <map> +#include <string> +#include "RecordMimeType.h" + +namespace NDefLib { + +/** + * Specialize the {@link RecordMimeType} to store VCard information. + * This record handles the VCard version 3 format. + * @see https://en.wikipedia.org/wiki/VCard + */ +class RecordVCard: public RecordMimeType { +public: + + /** + * Type of information that you can store inside the tag + */ + typedef enum { + ADDRESS, //!< ADDRESS + ADDRESS_HOME, //!< ADDRESS_HOME + ADDRESS_WORK, //!< ADDRESS_WORK + AGENT, //!< AGENT + BIRDAY, //!< BIRDAY + CATEGORIES, //!< CATEGORIES + EMAIL, //!< EMAIL + EMAIL_HOME, //!< EMAIL_HOME + EMAIL_WORK, //!< EMAIL_WORK + FORMATTED_NAME,//!< FORMATTED_NAME + GEO, //!< GEO latitude and longitude ; separated + IMPP, //!< IMPP + PGPKEY_URL, //!< PGPKEY_URL + PGPGKEY_BASE64,//!< PGPGKEY_BASE64 + LOGO, //!< LOGO + LOGO_URI, //!< LOGO_URI + LOGO_BASE64, //!< generic hardcoded image add TYPE=XXXX:imagebyte + NAME, //!< NAME + NICKNAME, //!< NICKNAME + NOTE, //!< NOTE + ORGANIZATION, //!< ORGANIZATION + PHOTO_URI, //!< PHOTO_URI + PHOTO_BASE64, //!< generic hardcoded image add TYPE=XXXX:imagebyte + REVISION, //!< REVISION + SOURCE_URL, //!< SOURCE_URL + TEL, //!< TEL + TEL_HOME, //!< TEL_HOME + TEL_WORK, //!< TEL_WORK + TEL_MOBILE, //!< TEL_MOBILE + TITLE, //!< TITLE + URL, //!< URL + } VCardField_t; + + /** + * Type used to store the vcard information. + */ + typedef std::map<VCardField_t, std::string> VCardInfo_t; + + /** + * Create a RecordVCard reading the data from the buffer. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return an object of type RecordVCard or NULL + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordVCard* parse(const RecordHeader &header, + const uint8_t* buffer); + + /** + * Create a record with the specific information. + * @param Info optional information to store into the record. + */ + explicit RecordVCard(const VCardInfo_t &info=VCardInfo_t()); + + /** + * Get the specific information stored in this record. + * @param Type type of information to get. + * @return if present, the information or an empty string + */ + const std::string& operator[](const VCardField_t &type)const { + VCardInfo_t::const_iterator elem = mCardInfo.find(type); + if (elem == mCardInfo.end()) { + return sEmptyTagContent; + } + + return elem->second; + } + + /** + * Get or set/change an information associated with this record. + * @param type Type of information to change. + * @return reference to the string information + */ + std::string& operator[](const VCardField_t &type) { + mContentIsChange=true; + return mCardInfo[type]; + } + + /** + * Get the record type. + * @return TYPE_MIME_VCARD + */ + virtual RecordType_t get_type() const { + return TYPE_MIME_VCARD; + } //getType + + /** + * @return update the record content and return the number of + * bytes needed to store this record + */ + virtual uint16_t get_byte_length() { + update_content_info_string(); + return RecordMimeType::get_byte_length(); + } + + /** + * Update the content and write it on the buffer. + * @param[out] buffer buffer to write the record content into. + * @return number of bytes written + * @see Record#write + */ + virtual uint16_t write(uint8_t *buffer){ + update_content_info_string(); + return RecordMimeType::write(buffer); + } + + /** + * Compare two objects. + * @return true if the records have the same Vcard information + */ + bool operator==(const RecordVCard &other){ + return (mCardInfo==other.mCardInfo); + } + + virtual ~RecordVCard() { + } + +private: + VCardInfo_t mCardInfo; + + std::string mCardInfoString; ///< buffer containing the Vcard representation for this record + + bool mContentIsChange; ///< true if we have to upadte the string rappresentation of the data + + /** + * Generate a string representing the vcard info + */ + void update_content_info_string(); + + /** + * Check if the string that starts from the offset position is a valid + * vcard field. + * @param content String to search into. + * @param offset Search offset. + * @return type index of the found field or a negative number if not a valid vcard + */ + static int8_t find_VCard_field_type(const std::string &content, + uint16_t offset); + + /** + * Return the lenght of a vcard field. + * @param Content string to search into. + * @param Offset search offset. + * @return field length + * @par This function searches the first sEndVCardTag appearance. + */ + static uint16_t find_VCard_field_data_lenght(const std::string &content, + uint16_t offset); + + static const std::string sVcardMimeType; + static const std::string sStartFieldTag[]; + static const std::string sEndFieldTag; + static const std::string sStartVCardTag; + static const std::string sEndVCardTag; + static const std::string sEmptyTagContent; +}; + +} /* namespace NDefLib */ + +#endif /* NDEFLIB_RECORDTYPE_RECORDVCARD_H_ */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordWifiConf.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordWifiConf.cpp Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,221 @@ +/** + ****************************************************************************** + * @file RecordWifiConf.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief {@link RecordMimeType} that contains a Wifi configuration data + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ +#include <algorithm> + +#include <RecordWifiConf.h> +namespace NDefLib { +const std::string RecordWifiConf::sWifiConfMimeType("application/vnd.wfa.wsc"); + +const RecordWifiConf::fieldType_t RecordWifiConf::sCredentialField_id = 0x100E; +const RecordWifiConf::fieldType_t RecordWifiConf::sNetworkIdField_id = 0x1026; +const uint8_t RecordWifiConf::sDefaultNetworkId=0x01; +const RecordWifiConf::fieldType_t RecordWifiConf::sSsidField_id = 0x1045; +const RecordWifiConf::fieldType_t RecordWifiConf::sNetworkKeyField_id = 0x1027; +const RecordWifiConf::fieldType_t RecordWifiConf::sAuthTypeField_id = 0x1003; +const RecordWifiConf::fieldType_t RecordWifiConf::sEncTypeField_id = 0x100F; + +RecordWifiConf::RecordWifiConf(const std::string &ssid,const std::string &pass, authType_t authType,encryptionType_t encType): + RecordMimeType(sWifiConfMimeType), + mSsid(ssid), + mPasskey(pass), + mAuthType(authType), + mEncType(encType), + mContentIsChange(true), + mMimeData(NULL), + mMimeDataLenght(0) { +} + +/** + * move the data into the buffer swapping the byte order + * @param data data to write into the buffer + * @param outBuffer buffer where write the data +*/ +template<typename T> +void writeBEFromLE(const T& data,uint8_t *outBuffer) { + uint8_t *buffer = (uint8_t*)&data; + std::reverse_copy(buffer,buffer+sizeof(T),outBuffer); +} + +template<typename T> +uint32_t RecordWifiConf::write_data_field(const fieldType_t& dataType,const T& data,uint8_t *buffer){ + uint32_t offset=0; + writeBEFromLE(dataType,buffer+offset); + offset+=sizeof(dataType); + const fieldLenght_t dataSize = sizeof(T); + writeBEFromLE(dataSize,buffer+offset); + offset+=sizeof(dataSize); + writeBEFromLE(data,buffer+offset); + return offset+dataSize; +} + +uint32_t RecordWifiConf::write_data_field(const fieldType_t& dataType, + const char *data,const fieldLenght_t& size, uint8_t *buffer){ + uint32_t offset=0; + writeBEFromLE(dataType,buffer+offset); + offset+=sizeof(dataType);; + writeBEFromLE(size,buffer+offset); + offset+=sizeof(size); + if (data!=NULL || size!=0) { + std::memcpy(buffer+offset,data,size); + } + return offset+size; +} + +void RecordWifiConf::update_mime_data(){ + if (!mContentIsChange) { + return; + } + + mMimeDataLenght = sizeof(sCredentialField_id)+sizeof(fieldLenght_t)+ + sizeof(sNetworkIdField_id)+sizeof(fieldLenght_t)+sizeof(sDefaultNetworkId)+ + sizeof(sSsidField_id)+sizeof(fieldLenght_t)+mSsid.length()+ + sizeof(sNetworkKeyField_id)+sizeof(fieldLenght_t)+mPasskey.length()+ + sizeof(sAuthTypeField_id)+sizeof(fieldLenght_t)+2+ + sizeof(sEncTypeField_id)+sizeof(fieldLenght_t)+2; + + if (mMimeData!=NULL) { + delete [] mMimeData; + } + + mMimeData = new uint8_t[mMimeDataLenght]; + + uint32_t writeOffset=0; + writeBEFromLE(sCredentialField_id,mMimeData+writeOffset); + writeOffset+=sizeof(sCredentialField_id); + writeBEFromLE((fieldLenght_t)(mMimeDataLenght-4),mMimeData+writeOffset); + writeOffset+=sizeof(fieldLenght_t); + + writeOffset += write_data_field(sNetworkIdField_id,sDefaultNetworkId,mMimeData+writeOffset); + fieldLenght_t tempLength = (fieldLenght_t)sizeof(std::string::value_type)*mSsid.length(); + writeOffset += write_data_field(sSsidField_id,mSsid.data(),tempLength,mMimeData+writeOffset); + writeOffset += write_data_field(sAuthTypeField_id,(int16_t)mAuthType,mMimeData+writeOffset); + writeOffset += write_data_field(sEncTypeField_id,(int16_t)mEncType,mMimeData+writeOffset); + tempLength = (fieldLenght_t)sizeof(std::string::value_type)*mPasskey.length(); + writeOffset += write_data_field(sNetworkKeyField_id,mPasskey.data(),tempLength,mMimeData+writeOffset); + + set_mime_data_pointer(mMimeData,mMimeDataLenght); + mContentIsChange=false; +} + +/** + * Read a data from a buffer, where the buffer is in little endian and we are in + * a big endian system + * @param buffer buffer where read the data + * @param out pointer to the variable where store the read data + */ +template<typename T> +void readLEFromBE(const uint8_t *const buffer,T* out){ + const uint32_t typeSize = sizeof(T); + uint8_t *outBuffer = (uint8_t*)out; + std::reverse_copy(buffer,buffer+typeSize,outBuffer); +} + +/** + * Create a RecordWifiConf reading the data from the buffer. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return an object of type RecordVCard or NULL + * @par User is in charge of freeing the pointer returned by this function. + */ +RecordWifiConf* RecordWifiConf::parse(const RecordHeader &header, const uint8_t* buffer){ + if (header.get_FNT() != RecordHeader::Mime_media_type || header.get_type_length() != sWifiConfMimeType.size()) { + return NULL; + } + + if (sWifiConfMimeType.compare(0, sWifiConfMimeType.size(), (const char*) buffer, sWifiConfMimeType.size()) != 0) { + return NULL; + } + + buffer += header.get_type_length(); + buffer += header.get_id_length(); + + fieldType_t type; + fieldLenght_t dataLength; + + readLEFromBE(buffer,&type); + buffer+=sizeof(fieldType_t); + readLEFromBE(buffer,&dataLength); + buffer+=sizeof(fieldLenght_t); + + if(type!=sCredentialField_id) { + return NULL; + } + + std::string ssid; + std::string pass; + authType_t authType=AUTH_OPEN; + encryptionType_t encType=ENC_TYPE_NONE; + uint16_t enumValue; + fieldLenght_t readData=0; + while (readData!=dataLength) { + readLEFromBE(buffer+readData,&type); + readData+=sizeof(fieldType_t); + fieldLenght_t length; + readLEFromBE(buffer+readData,&length); + readData+=sizeof(fieldLenght_t); + + switch(type) { + case sSsidField_id: { + ssid.insert(0,(const char*)buffer+readData,length); + break; + } + case sNetworkKeyField_id: { + pass.insert(0,(const char*)buffer+readData,length); + break; + } + case sAuthTypeField_id: { + readLEFromBE(buffer+readData,&enumValue); + authType = (authType_t)enumValue; + break; + } + case sEncTypeField_id: { + readLEFromBE(buffer+readData,&enumValue); + encType = (encryptionType_t)enumValue; + break; + } + } + + readData+=length; + } + + return new RecordWifiConf(ssid,pass,authType,encType); +}//parse + +} + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
diff -r 000000000000 -r 51d7bdf88981 RecordWifiConf.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/RecordWifiConf.h Thu Dec 07 17:07:22 2017 +0000 @@ -0,0 +1,228 @@ +/** + ****************************************************************************** + * @file RecordWifiConf.h + * @author ST / Central Labs + * @version V2.0.0 + * @date 28 Apr 2017 + * @brief {@link RecordMimeType} that contains a Wifi configuration data + ****************************************************************************** + * @attention + * + * <h2><center>© COPYRIGHT(c) 2015 STMicroelectronics</center></h2> + * + * Redistribution and use in source and binary forms, with or without modification, + * are permitted provided that the following conditions are met: + * 1. Redistributions of source code must retain the above copyright notice, + * this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * 3. Neither the name of STMicroelectronics nor the names of its contributors + * may be used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************** + */ + +#ifndef NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_ +#define NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_ + +#include <RecordMimeType.h> +namespace NDefLib { + +/** + * Specialize the {@link RecordMimeType} to store Wifi configuration information. + */ +class RecordWifiConf: public NDefLib::RecordMimeType { +public: + + /** + * Authentication required by the wifi network + */ + typedef enum { + AUTH_OPEN = 0x0000, //!< no authentication + AUTH_WPA_PSK = 0x0002, + AUTH_WPA_EAP = 0x0008, + AUTH_WPA2_EAP = 0x0010, + AUTH_WPA2_PSK = 0x0020, + }authType_t; + + /** + * Encryption used by the network + */ + typedef enum { + ENC_TYPE_NONE = 0x0001, //!< no authentication + ENC_TYPE_WEP = 0x0002, //!< wep is deprecated + ENC_TYPE_TKIP = 0x0004, //!< deprecated -> only with mixed mode (0x000c) + ENC_TYPE_AES = 0x0008, //!< includes CCMP and GCMP + ENC_TYPE_AES_TKIP = 0x000c, //!< mixed mode + }encryptionType_t; + + /** + * Create a RecordWifiConf reading the data from the buffer. + * @param header Record header. + * @param buffer Buffer to read the data from. + * @return an object of type RecordVCard or NULL + * @par User is in charge of freeing the pointer returned by this function. + */ + static RecordWifiConf* parse(const RecordHeader &header, + const uint8_t* buffer); + + + /** + * Create a record with the wifi configuration, the default parameters create an open network + * @param ssid network name + * @param pass network password + * @param authType authentication used in the network + * @param encType encryption used in the network + */ + RecordWifiConf(const std::string &ssid,const std::string &pass="", + authType_t authType=AUTH_OPEN,encryptionType_t encType=ENC_TYPE_NONE); + + /** + * Get the record type. + * @return TYPE_WIFI_CONF + */ + virtual RecordType_t get_type() const { + return TYPE_WIFI_CONF; + } //getType + + /** + * @return update the record content and return the number of + * bytes needed to store this record + */ + virtual uint16_t get_byte_length() { + update_mime_data(); + return RecordMimeType::get_byte_length(); + } + + /** + * Update the content and write it on the buffer. + * @param[out] buffer buffer to write the record content into. + * @return number of bytes written + * @see Record#write + */ + virtual uint16_t write(uint8_t *buffer){ + update_mime_data(); + return RecordMimeType::write(buffer); + } + + /** + * Compare two objects. + * @return true if the records have the same Vcard information + */ + bool operator==(const RecordWifiConf &other){ + return (mSsid==other.mSsid) && + (mPasskey==other.mPasskey) && + (mAuthType==other.mAuthType) && + (mEncType==other.mEncType); + } + + const std::string& get_network_key() const{ + return mPasskey; + } + + void set_network_key(const std::string &newKey){ + mPasskey=newKey; + mContentIsChange=true; + } + + const std::string& get_network_ssid() const{ + return mSsid; + } + + void set_network_ssid(const std::string &newSsid){ + mSsid=newSsid; + mContentIsChange=true; + } + + authType_t get_auth_type() const{ + return mAuthType; + } + + void set_auth_type(const authType_t &newAuth){ + mAuthType = newAuth; + mContentIsChange=true; + } + + encryptionType_t get_encryption() const{ + return mEncType; + } + + void set_encryption_type(const encryptionType_t &newEncript){ + mEncType = newEncript; + mContentIsChange=true; + } + + virtual ~RecordWifiConf(){ + if(mMimeData!=NULL) + delete [] mMimeData; + } + +private: + + /** + * function that update the ndef data format + */ + void update_mime_data(); + + typedef uint16_t fieldType_t; //< type to use for store a field type + typedef uint16_t fieldLenght_t; //<type to use for store a filed length + + /** + * write a data field into the output buffer + * @param dataType field type + * @param data field content + * @param buffer buffer where write the data field + * @return number of write bytes + */ + template<typename T> + uint32_t write_data_field(const fieldType_t& dataType, + const T& data, uint8_t *buffer); + + /** + * write a data buffer inside the output buffer + * @param dataType field type + * @param data buffer where read the data + * @param size number of byte to move + * @param out output buffer + * @return number of write bytes + */ + uint32_t write_data_field(const fieldType_t& dataType, + const char *data,const fieldLenght_t& size, uint8_t *buffer); + + std::string mSsid; + std::string mPasskey; + authType_t mAuthType; + encryptionType_t mEncType; + + bool mContentIsChange; ///< true if we have to update the string representation of the data + uint8_t *mMimeData; //< buffer where store the record playload + fieldLenght_t mMimeDataLenght; + + static const std::string sWifiConfMimeType; + static const fieldType_t sCredentialField_id; + static const fieldType_t sNetworkIdField_id; + static const uint8_t sDefaultNetworkId; + static const fieldType_t sSsidField_id; + static const fieldType_t sNetworkKeyField_id; + static const fieldType_t sAuthTypeField_id; + static const fieldType_t sEncTypeField_id; +}; +} + +#endif /* NDEFLIB_RECORDTYPE_RECORDWIFICONF_H_ */ + + +/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/