Newer
Older
mbed-os / connectivity / nfc / libraries / stack / ndef / ndef.h
/*
 * Copyright (c) 2013-2018, ARM Limited, All Rights Reserved
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may
 * not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/**
 * \file ndef.h
 * \copyright Copyright (c) ARM Ltd 2013
 * \author Donatien Garnier
 */

/** \addtogroup NDEF
 *  \ingroup nfc
 *  @{
 *  \name Generic NDEF Tag
 *  @{
 */

#ifndef NDEF_H_
#define NDEF_H_

#include "stack/nfc_common.h"

#ifdef __cplusplus
extern "C" {
#endif

//Generic interface for NDEF messages
typedef struct __ndef_msg ndef_msg_t;

/** Function called to generate the tag's content on read (target mode)
 * \param pTag pointer to ndef_tag_t instance
 * \param pBufferBldr buffer in which to store the generated content
 * \param pUserData User data pointer passed to #ndef_msg_init
 */
typedef nfc_err_t (*ndef_encode_fn_t)(ndef_msg_t *pTag, ac_buffer_builder_t *pBufferBldr, void *pUserData);

/** Function called to decode the tag's content on write (target mode) or read (reader mode)
 * \param pTag pointer to ndef_tag_t instance
 * \param pBuffer buffer containing the tag's content
 * \param pUserData User data pointer passed to #ndef_msg_init
 */
typedef nfc_err_t (*ndef_decode_fn_t)(ndef_msg_t *pTag, ac_buffer_t *pBuffer, void *pUserData);

struct __ndef_msg {
    ndef_encode_fn_t encode;
    ndef_decode_fn_t decode;
    ac_buffer_builder_t bufferBldr;
    void *pUserData;
};

/** Initialize NDEF tag abstraction
 * \param pNdef pointer to ndef_tag_t structure to initialize
 * \param encode function that will be called to generate the NDEF message before sending it to the other party
 * \param decode function that will be called to parse the NDEF message after receiving it from the other party
 * \param data underlying buffer to use (it should be big enough so that any NDEF message you might need could be stored inside)
 * \param size size of the underlying buffer
 * \param pUserData User data pointer to pass to callbacks.
 */
void ndef_msg_init(ndef_msg_t *pNdef, ndef_encode_fn_t encode, ndef_decode_fn_t decode, uint8_t *data, size_t size, void *pUserData);

static inline nfc_err_t ndef_msg_encode(ndef_msg_t *pNdef)
{
    if (pNdef->encode == NULL) {
        return NFC_OK;
    }
    return pNdef->encode(pNdef, &pNdef->bufferBldr, pNdef->pUserData);
}

static inline nfc_err_t ndef_msg_decode(ndef_msg_t *pNdef)
{
    if (pNdef->decode == NULL) {
        return NFC_OK;
    }
    return pNdef->decode(pNdef, ac_buffer_builder_buffer(&pNdef->bufferBldr), pNdef->pUserData);
}

static inline ac_buffer_builder_t *ndef_msg_buffer_builder(ndef_msg_t *pNdef)
{
    return &pNdef->bufferBldr;
}

//void* ndef_tag_impl(ndef_tag_t* pNdefTag);

#ifdef __cplusplus
}
#endif

#endif /* NDEF_H_ */

/**
 * @}
 * @}
 * */