Utility library to read and write Ndef messages from/to a Type4 NFC tag

Dependents:   NFC M2M_2016_STM32 MyongjiElec_capstone1 IDW01M1_Cloud_IBM ... more

Fork of NDefLib by ST Expansion SW Team

NDEF NFC library

This library provides an abstract API to create NDEF formatted messages and records and to read/write them from/to a Type4 NFC Tag.

Implementations

At the moment, the NDEF API is implemented by X_NUCLEO_NFC01A1 and X_NUCLEO_NFC02A1 Dynamic NFC Tag libraries respectively driving the X-NUCLEO-NFC01A1 and X-NUCLEO-NFC02A1 boards.

RecordType/RecordVCard.h

Committer:
giovannivisentini
Date:
2015-12-10
Revision:
7:1ebd6049fa57
Parent:
6:739e3211749d
Child:
8:473f6e0b03df

File content as of revision 7:1ebd6049fa57:

/**
 ******************************************************************************
 * @file    RecordVCard.h
 * @author  ST / CentralLabs
 * @version V1.0.0
 * @date    16 Nov 2015
 * @brief   RecordVCard implementation
 ******************************************************************************
 * @attention
 *
 * <h2><center>&copy; 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 {

//https://en.wikipedia.org/wiki/VCard

/**
 * Specialize the mimeType record for store a VCard information.
 * this record handle the VCard version 3 format
 */
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 for store the vcard information
	 */
	typedef std::map<VCardField_t, std::string> VCardInfo_t;

	/**
	 * create an RecordVCard reading the data from the buffer
 	 * @param header record header already read
	 * @param buffer buffer where read the data
	 * @return an object of type RecordVCard or NULL
	 * @par free the pointer return 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 in the record
	 */
	explicit RecordVCard(const VCardInfo_t &info=VCardInfo_t());

	/**
	 * get the specificic 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;
		//else
		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];
	}

	virtual RecordType_t getType() const {
		return TYPE_MIME_VCARD;
	} //getType


	virtual uint16_t getByteLength() {
		updateContentInfoString();
		return RecordMimeType::getByteLength();
	}

	virtual uint16_t write(uint8_t *buffer){
		updateContentInfoString();
		return RecordMimeType::write(buffer);
	}

	bool operator==(const RecordVCard &other){
		return (mCardInfo==other.mCardInfo);
	}

	virtual ~RecordVCard() {
	}

private:
	VCardInfo_t mCardInfo;
	//buffer containing the Vcard representation for this record
	std::string mCardInfoString;

	bool mContentIsChange;

	void updateContentInfoString();

	/**
	 * check if in the string that start in position offset, is present a known
	 * vcard file, if yes it return type index, otherwise a negative number
	 * @param content string where search
	 * @param offset offset where start to search
	 * @return index of the field found or a negative number
	 */
	static int8_t findVCardFieldType(const std::string &content,
			uint16_t offset);

	/**
	 * search the end of a vcard field
	 * @param content string where search
	 * @param offset offset where start to search
	 * @return field length
	 * @par this function search the first sEndVCardTag appearance
	 */
	static uint16_t findVCardFieldDataLenght(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_ */