Fork for the GitHub

Committer:
DiegoOstuni
Date:
Thu Nov 14 10:34:11 2019 +0000
Revision:
0:de13951f30f6
Add files

Who changed what in which revision?

UserRevisionLine numberNew contents of line
DiegoOstuni 0:de13951f30f6 1 /**
DiegoOstuni 0:de13951f30f6 2 ******************************************************************************
DiegoOstuni 0:de13951f30f6 3 * @file lib_NDEF_Text.h
DiegoOstuni 0:de13951f30f6 4 * @author MMY Application Team
DiegoOstuni 0:de13951f30f6 5 * @version $Revision: 3210 $
DiegoOstuni 0:de13951f30f6 6 * @date $Date: 2016-12-05 15:37:48 +0100 (Mon, 05 Dec 2016) $
DiegoOstuni 0:de13951f30f6 7 * @brief This file help to manage Text NDEF file.
DiegoOstuni 0:de13951f30f6 8 ******************************************************************************
DiegoOstuni 0:de13951f30f6 9 * @attention
DiegoOstuni 0:de13951f30f6 10 *
DiegoOstuni 0:de13951f30f6 11 * <h2><center>&copy; COPYRIGHT 2015 STMicroelectronics</center></h2>
DiegoOstuni 0:de13951f30f6 12 *
DiegoOstuni 0:de13951f30f6 13 * Licensed under ST MYLIBERTY SOFTWARE LICENSE AGREEMENT (the "License");
DiegoOstuni 0:de13951f30f6 14 * You may not use this file except in compliance with the License.
DiegoOstuni 0:de13951f30f6 15 * You may obtain a copy of the License at:
DiegoOstuni 0:de13951f30f6 16 *
DiegoOstuni 0:de13951f30f6 17 * http://www.st.com/myliberty
DiegoOstuni 0:de13951f30f6 18 *
DiegoOstuni 0:de13951f30f6 19 * Unless required by applicable law or agreed to in writing, software
DiegoOstuni 0:de13951f30f6 20 * distributed under the License is distributed on an "AS IS" BASIS,
DiegoOstuni 0:de13951f30f6 21 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied,
DiegoOstuni 0:de13951f30f6 22 * AND SPECIFICALLY DISCLAIMING THE IMPLIED WARRANTIES OF MERCHANTABILITY,
DiegoOstuni 0:de13951f30f6 23 * FITNESS FOR A PARTICULAR PURPOSE, AND NON-INFRINGEMENT.
DiegoOstuni 0:de13951f30f6 24 * See the License for the specific language governing permissions and
DiegoOstuni 0:de13951f30f6 25 * limitations under the License.
DiegoOstuni 0:de13951f30f6 26 *
DiegoOstuni 0:de13951f30f6 27 ******************************************************************************
DiegoOstuni 0:de13951f30f6 28 */
DiegoOstuni 0:de13951f30f6 29
DiegoOstuni 0:de13951f30f6 30 /* Includes ------------------------------------------------------------------*/
DiegoOstuni 0:de13951f30f6 31 #include "lib_NDEF_Text.h"
DiegoOstuni 0:de13951f30f6 32
DiegoOstuni 0:de13951f30f6 33 /** @addtogroup NFC_libraries
DiegoOstuni 0:de13951f30f6 34 * @{
DiegoOstuni 0:de13951f30f6 35 * @brief <b>This is the library used to manage the content of the TAG (data)
DiegoOstuni 0:de13951f30f6 36 * But also the specific feature of the tag, for instance
DiegoOstuni 0:de13951f30f6 37 * password, gpo... </b>
DiegoOstuni 0:de13951f30f6 38 */
DiegoOstuni 0:de13951f30f6 39
DiegoOstuni 0:de13951f30f6 40
DiegoOstuni 0:de13951f30f6 41 /** @addtogroup libNFC_FORUM
DiegoOstuni 0:de13951f30f6 42 * @{
DiegoOstuni 0:de13951f30f6 43 * @brief This part of the library manage data which follow NFC forum organisation.
DiegoOstuni 0:de13951f30f6 44 */
DiegoOstuni 0:de13951f30f6 45
DiegoOstuni 0:de13951f30f6 46 /**
DiegoOstuni 0:de13951f30f6 47 * @brief This buffer contains the data send/received by TAG
DiegoOstuni 0:de13951f30f6 48 */
DiegoOstuni 0:de13951f30f6 49 extern uint8_t NDEF_Buffer [];
DiegoOstuni 0:de13951f30f6 50
DiegoOstuni 0:de13951f30f6 51 /**
DiegoOstuni 0:de13951f30f6 52 * @}
DiegoOstuni 0:de13951f30f6 53 */
DiegoOstuni 0:de13951f30f6 54
DiegoOstuni 0:de13951f30f6 55 /** @defgroup libEmail_Public_Functions
DiegoOstuni 0:de13951f30f6 56 * @{
DiegoOstuni 0:de13951f30f6 57 * @brief This file is used to manage Email (stored or loaded in tag)
DiegoOstuni 0:de13951f30f6 58 */
DiegoOstuni 0:de13951f30f6 59
DiegoOstuni 0:de13951f30f6 60 /**
DiegoOstuni 0:de13951f30f6 61 * @brief This function write the text in the TAG.
DiegoOstuni 0:de13951f30f6 62 * @param text : text to write.
DiegoOstuni 0:de13951f30f6 63 * @retval NDEF_OK : NDEF file data written in the tag.
DiegoOstuni 0:de13951f30f6 64 * @retval NDEF_ERROR : not able to store NDEF in tag.
DiegoOstuni 0:de13951f30f6 65 * @retval NDEF_ERROR_MEMORY_INTERNAL : Cannot write to tag.
DiegoOstuni 0:de13951f30f6 66 * @retval NDEF_ERROR_NOT_FORMATED : CCFile data not supported or not present.
DiegoOstuni 0:de13951f30f6 67 * @retval NDEF_ERROR_MEMORY_TAG : Size not compatible with memory.
DiegoOstuni 0:de13951f30f6 68 * @retval NDEF_ERROR_LOCKED : Tag locked, cannot be write.
DiegoOstuni 0:de13951f30f6 69 */
DiegoOstuni 0:de13951f30f6 70 uint16_t NDEF_WriteText( char *text, I2C * mi2cChannel )
DiegoOstuni 0:de13951f30f6 71 {
DiegoOstuni 0:de13951f30f6 72 uint16_t status = NDEF_ERROR;
DiegoOstuni 0:de13951f30f6 73 uint32_t textSize, Offset = 0;
DiegoOstuni 0:de13951f30f6 74
DiegoOstuni 0:de13951f30f6 75
DiegoOstuni 0:de13951f30f6 76 /* TEXT : 1+en+message */
DiegoOstuni 0:de13951f30f6 77 textSize = 3 + strlen(text);
DiegoOstuni 0:de13951f30f6 78
DiegoOstuni 0:de13951f30f6 79 /* TEXT header */
DiegoOstuni 0:de13951f30f6 80 NDEF_Buffer[Offset] = 0xD1;
DiegoOstuni 0:de13951f30f6 81 if( textSize < 256 ) NDEF_Buffer[Offset] |= 0x10; // Set the SR bit
DiegoOstuni 0:de13951f30f6 82 Offset++;
DiegoOstuni 0:de13951f30f6 83
DiegoOstuni 0:de13951f30f6 84 NDEF_Buffer[Offset++] = TEXT_TYPE_STRING_LENGTH;
DiegoOstuni 0:de13951f30f6 85 if( textSize > 255 )
DiegoOstuni 0:de13951f30f6 86 {
DiegoOstuni 0:de13951f30f6 87 NDEF_Buffer[Offset++] = (textSize & 0xFF000000) >> 24;
DiegoOstuni 0:de13951f30f6 88 NDEF_Buffer[Offset++] = (textSize & 0x00FF0000) >> 16;
DiegoOstuni 0:de13951f30f6 89 NDEF_Buffer[Offset++] = (textSize & 0x0000FF00) >> 8;
DiegoOstuni 0:de13951f30f6 90 NDEF_Buffer[Offset++] = textSize & 0x000000FF;
DiegoOstuni 0:de13951f30f6 91 }
DiegoOstuni 0:de13951f30f6 92 else
DiegoOstuni 0:de13951f30f6 93 {
DiegoOstuni 0:de13951f30f6 94 NDEF_Buffer[Offset++] = (uint8_t)textSize;
DiegoOstuni 0:de13951f30f6 95 }
DiegoOstuni 0:de13951f30f6 96 memcpy( &NDEF_Buffer[Offset], TEXT_TYPE_STRING, TEXT_TYPE_STRING_LENGTH );
DiegoOstuni 0:de13951f30f6 97 Offset += TEXT_TYPE_STRING_LENGTH;
DiegoOstuni 0:de13951f30f6 98
DiegoOstuni 0:de13951f30f6 99 /* TEXT payload */
DiegoOstuni 0:de13951f30f6 100 NDEF_Buffer[Offset++] = ISO_ENGLISH_CODE_STRING_LENGTH;
DiegoOstuni 0:de13951f30f6 101 memcpy( &NDEF_Buffer[Offset], ISO_ENGLISH_CODE_STRING, ISO_ENGLISH_CODE_STRING_LENGTH );
DiegoOstuni 0:de13951f30f6 102 Offset += ISO_ENGLISH_CODE_STRING_LENGTH;
DiegoOstuni 0:de13951f30f6 103
DiegoOstuni 0:de13951f30f6 104 memcpy( &NDEF_Buffer[Offset], text, strlen(text) );
DiegoOstuni 0:de13951f30f6 105 Offset += strlen(text);
DiegoOstuni 0:de13951f30f6 106
DiegoOstuni 0:de13951f30f6 107 status = NfcTag_WriteNDEF( Offset , NDEF_Buffer, mi2cChannel );
DiegoOstuni 0:de13951f30f6 108
DiegoOstuni 0:de13951f30f6 109 return status;
DiegoOstuni 0:de13951f30f6 110 }
DiegoOstuni 0:de13951f30f6 111
DiegoOstuni 0:de13951f30f6 112
DiegoOstuni 0:de13951f30f6 113 /**
DiegoOstuni 0:de13951f30f6 114 * @brief This function the Text information in a record if any.
DiegoOstuni 0:de13951f30f6 115 * @param pRecordStruct Pointer on a NDEF record structure.
DiegoOstuni 0:de13951f30f6 116 * @param pText A pointer on a text information structure, used to return the text & its metadata.
DiegoOstuni 0:de13951f30f6 117 * @retval NDEF_OK Text information has been retrieved.
DiegoOstuni 0:de13951f30f6 118 * @retval NDEF_ERROR The record is not a Text record.
DiegoOstuni 0:de13951f30f6 119 * @retval NDEF_ERROR_MEMORY_INTERNAL Language code or Text length is too big for the buffers.
DiegoOstuni 0:de13951f30f6 120 */
DiegoOstuni 0:de13951f30f6 121 uint16_t NDEF_ReadText( sRecordInfo_t *pRecordStruct, NDEF_Text_info_t *pText )
DiegoOstuni 0:de13951f30f6 122 {
DiegoOstuni 0:de13951f30f6 123 uint16_t status = NDEF_ERROR;
DiegoOstuni 0:de13951f30f6 124
DiegoOstuni 0:de13951f30f6 125 if( pRecordStruct->NDEF_Type == TEXT_TYPE )
DiegoOstuni 0:de13951f30f6 126 {
DiegoOstuni 0:de13951f30f6 127 /* Get the text metadata (status byte (encoding & language code length) + language code) */
DiegoOstuni 0:de13951f30f6 128 NDEF_Text_metadata_t *text_record_info = (NDEF_Text_metadata_t *)pRecordStruct->PayloadBufferAdd;
DiegoOstuni 0:de13951f30f6 129
DiegoOstuni 0:de13951f30f6 130 uint32_t text_length = pRecordStruct->PayloadLength /* record length */
DiegoOstuni 0:de13951f30f6 131 - text_record_info->language_length /* minus language code length */
DiegoOstuni 0:de13951f30f6 132 - sizeof(uint8_t); /* minus the status byte length */
DiegoOstuni 0:de13951f30f6 133
DiegoOstuni 0:de13951f30f6 134 if((text_record_info->language_length >= NDEF_TEXT_LANGUAGE_CODE_MAX_LENGTH) ||
DiegoOstuni 0:de13951f30f6 135 (text_length >= NDEF_TEXT_MAX_LENGTH))
DiegoOstuni 0:de13951f30f6 136 {
DiegoOstuni 0:de13951f30f6 137 /* One of the text info structure buffer is too small */
DiegoOstuni 0:de13951f30f6 138 return NDEF_ERROR_MEMORY_INTERNAL;
DiegoOstuni 0:de13951f30f6 139 }
DiegoOstuni 0:de13951f30f6 140
DiegoOstuni 0:de13951f30f6 141 /* Retrieve the encoding */
DiegoOstuni 0:de13951f30f6 142 pText->encoding = (NDEF_Text_encoding_t)text_record_info->encoding;
DiegoOstuni 0:de13951f30f6 143
DiegoOstuni 0:de13951f30f6 144 /* Save the language code string (adding null char at the end) */
DiegoOstuni 0:de13951f30f6 145 memcpy(&pText->language_code,text_record_info->language,text_record_info->language_length);
DiegoOstuni 0:de13951f30f6 146 pText->language_code[text_record_info->language_length] = '\0';
DiegoOstuni 0:de13951f30f6 147
DiegoOstuni 0:de13951f30f6 148 /* Copy the text string itself (adding null char at the end) */
DiegoOstuni 0:de13951f30f6 149 memcpy(&pText->text, text_record_info->language + text_record_info->language_length,text_length);
DiegoOstuni 0:de13951f30f6 150 pText->text[text_length] = '\0';
DiegoOstuni 0:de13951f30f6 151
DiegoOstuni 0:de13951f30f6 152 status = NDEF_OK;
DiegoOstuni 0:de13951f30f6 153 }
DiegoOstuni 0:de13951f30f6 154 else
DiegoOstuni 0:de13951f30f6 155 {
DiegoOstuni 0:de13951f30f6 156 /* Not a text record, exit in error */
DiegoOstuni 0:de13951f30f6 157 status = NDEF_ERROR;
DiegoOstuni 0:de13951f30f6 158 }
DiegoOstuni 0:de13951f30f6 159
DiegoOstuni 0:de13951f30f6 160 return status;
DiegoOstuni 0:de13951f30f6 161 }
DiegoOstuni 0:de13951f30f6 162
DiegoOstuni 0:de13951f30f6 163 /**
DiegoOstuni 0:de13951f30f6 164 * @}
DiegoOstuni 0:de13951f30f6 165 */
DiegoOstuni 0:de13951f30f6 166
DiegoOstuni 0:de13951f30f6 167 /**
DiegoOstuni 0:de13951f30f6 168 * @}
DiegoOstuni 0:de13951f30f6 169 */
DiegoOstuni 0:de13951f30f6 170
DiegoOstuni 0:de13951f30f6 171 /**
DiegoOstuni 0:de13951f30f6 172 * @}
DiegoOstuni 0:de13951f30f6 173 */
DiegoOstuni 0:de13951f30f6 174
DiegoOstuni 0:de13951f30f6 175 /******************* (C) COPYRIGHT 2015 STMicroelectronics *****END OF FILE****/