mirror of
https://gitee.com/muyuchl/nrf51_2in13_epd.git
synced 2025-12-14 17:58:13 +08:00
copy project from century 2in6 esl project
This commit is contained in:
160
COMPONENTS/nfc/ndef/connection_handover/ac_rec/nfc_ac_rec.c
Normal file
160
COMPONENTS/nfc/ndef/connection_handover/ac_rec/nfc_ac_rec.c
Normal file
@@ -0,0 +1,160 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "nfc_ac_rec.h"
|
||||
#include <string.h>
|
||||
#include "nrf_error.h"
|
||||
#include "nrf.h"
|
||||
|
||||
#define AC_REC_CPS_BYTE_SIZE 1 ///< Size of the field with CPS data.
|
||||
#define AC_REC_DATA_REF_LEN_SIZE 1 ///< Size of the Data Reference Length field.
|
||||
#define AC_REC_AUX_DATA_REF_COUNT_SIZE 1 ///< Size of the Data Reference Length field.
|
||||
|
||||
const uint8_t nfc_ac_rec_type_field[2] = {'a', 'c'}; ///< Alternative Carrier Record type.
|
||||
|
||||
/**
|
||||
* @brief Function for calculating the payload length of the NFC NDEF Alternative Carrier record.
|
||||
*/
|
||||
static uint32_t nfc_ac_rec_payload_size_get(nfc_ac_rec_payload_desc_t const * p_ac_rec_payload_desc)
|
||||
{
|
||||
int32_t i = 0;
|
||||
// Initialize with size of byte with CPS.
|
||||
uint32_t payload_size = AC_REC_CPS_BYTE_SIZE;
|
||||
|
||||
// Add Carrier Data Reference size.
|
||||
payload_size += p_ac_rec_payload_desc->carrier_data_ref.length + AC_REC_DATA_REF_LEN_SIZE;
|
||||
|
||||
// Add Auxiliary Data Reference Count size.
|
||||
payload_size += AC_REC_AUX_DATA_REF_COUNT_SIZE;
|
||||
|
||||
for (i = 0; i < p_ac_rec_payload_desc->aux_data_ref_count; i++)
|
||||
{
|
||||
// Add Auxiliary Data Reference size.
|
||||
payload_size += p_ac_rec_payload_desc->p_aux_data_ref[i].length + AC_REC_DATA_REF_LEN_SIZE;
|
||||
}
|
||||
|
||||
return payload_size;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_ac_rec_payload_constructor(nfc_ac_rec_payload_desc_t * p_nfc_rec_ac_payload_desc,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
int32_t i = 0;
|
||||
uint32_t payload_size = nfc_ac_rec_payload_size_get(p_nfc_rec_ac_payload_desc);
|
||||
|
||||
if (p_buff != NULL)
|
||||
{
|
||||
// Not enough space in the buffer, return an error.
|
||||
if (payload_size > *p_len)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
// Invalid CPS value.
|
||||
if ( p_nfc_rec_ac_payload_desc->cps & ~NFC_AC_CPS_MASK )
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
// Copy CPS.
|
||||
*p_buff = p_nfc_rec_ac_payload_desc->cps;
|
||||
p_buff += AC_REC_CPS_BYTE_SIZE;
|
||||
|
||||
// Copy Carrier Data Reference.
|
||||
*p_buff = p_nfc_rec_ac_payload_desc->carrier_data_ref.length;
|
||||
p_buff += AC_REC_DATA_REF_LEN_SIZE;
|
||||
|
||||
memcpy( p_buff,
|
||||
p_nfc_rec_ac_payload_desc->carrier_data_ref.p_data,
|
||||
p_nfc_rec_ac_payload_desc->carrier_data_ref.length );
|
||||
p_buff += p_nfc_rec_ac_payload_desc->carrier_data_ref.length;
|
||||
|
||||
// Copy Auxiliary Data Reference.
|
||||
*p_buff = p_nfc_rec_ac_payload_desc->aux_data_ref_count;
|
||||
p_buff += AC_REC_AUX_DATA_REF_COUNT_SIZE;
|
||||
|
||||
for (i = 0; i < p_nfc_rec_ac_payload_desc->aux_data_ref_count; i++)
|
||||
{
|
||||
*p_buff = p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].length;
|
||||
p_buff += AC_REC_DATA_REF_LEN_SIZE;
|
||||
|
||||
memcpy( p_buff,
|
||||
p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].p_data,
|
||||
p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].length );
|
||||
p_buff += p_nfc_rec_ac_payload_desc->p_aux_data_ref[i].length;
|
||||
}
|
||||
}
|
||||
|
||||
// Assign payload size to the return buffer.
|
||||
*p_len = payload_size;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void nfc_ac_rec_auxiliary_data_ref_clear(nfc_ndef_record_desc_t * p_ac_rec)
|
||||
{
|
||||
nfc_ac_rec_payload_desc_t * p_ac_rec_payload =
|
||||
(nfc_ac_rec_payload_desc_t*)p_ac_rec->p_payload_descriptor;
|
||||
|
||||
p_ac_rec_payload->aux_data_ref_count = 0;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_ac_rec_auxiliary_data_ref_add(nfc_ndef_record_desc_t * p_ac_rec,
|
||||
uint8_t * p_aux_data,
|
||||
uint8_t aux_length)
|
||||
{
|
||||
nfc_ac_rec_payload_desc_t * p_ac_rec_payload =
|
||||
(nfc_ac_rec_payload_desc_t *)p_ac_rec->p_payload_descriptor;
|
||||
|
||||
if (p_ac_rec_payload->aux_data_ref_count >= p_ac_rec_payload->max_aux_data_ref)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
p_ac_rec_payload->p_aux_data_ref[p_ac_rec_payload->aux_data_ref_count].p_data = p_aux_data;
|
||||
p_ac_rec_payload->p_aux_data_ref[p_ac_rec_payload->aux_data_ref_count].length = aux_length;
|
||||
p_ac_rec_payload->aux_data_ref_count++;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
196
COMPONENTS/nfc/ndef/connection_handover/ac_rec/nfc_ac_rec.h
Normal file
196
COMPONENTS/nfc/ndef/connection_handover/ac_rec/nfc_ac_rec.h
Normal file
@@ -0,0 +1,196 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_AC_REC_H__
|
||||
#define NFC_AC_REC_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_ac_rec ac (Alternative carrier) records
|
||||
* @{
|
||||
* @ingroup nfc_ble_pair_msg
|
||||
*
|
||||
* @brief Generation of NFC NDEF Alternative Carrier records for NDEF messages.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_record.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
#define AC_REC_CPS_BYTE_SIZE 1 ///< Size of the field with CPS data.
|
||||
#define AC_REC_DATA_REF_LEN_SIZE 1 ///< Size of the Data Reference Length field.
|
||||
#define AC_REC_AUX_DATA_REF_COUNT_SIZE 1 ///< Size of the Data Reference Length field.
|
||||
|
||||
/**
|
||||
* @brief Carrier Power State.
|
||||
*
|
||||
* Possible Carrier Power State field values in an Alternative Carrier record.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
NFC_AC_CPS_INACTIVE = 0x00, ///< Alternative Carrier inactive.
|
||||
NFC_AC_CPS_ACTIVE = 0x01, ///< Alternative Carrier active.
|
||||
NFC_AC_CPS_ACTIVATING = 0x02, ///< Alternative Carrier activating.
|
||||
NFC_AC_CPS_UNKNOWN = 0x03 ///< Alternative Carrier power status unknown.
|
||||
} nfc_ac_rec_cps_t;
|
||||
|
||||
#define NFC_AC_CPS_MASK (NFC_AC_CPS_UNKNOWN) ///< Mask of Carrier Power State bits in a first ac record byte.
|
||||
|
||||
/**
|
||||
* @brief Carrier Data Reference and Auxiliary Data Reference descriptor.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t length; ///< Length of the data field.
|
||||
uint8_t * p_data; ///< Pointer to the Data Reference characters. Not relevant if length is 0.
|
||||
} nfc_ac_rec_data_ref_t;
|
||||
|
||||
/**
|
||||
* @brief Alternative Carrier record payload descriptor.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nfc_ac_rec_cps_t cps; ///< Carrier Power State value.
|
||||
nfc_ac_rec_data_ref_t carrier_data_ref; ///< Carrier Data Reference.
|
||||
uint8_t const max_aux_data_ref; ///< Maximum number of Auxiliary Data Reference fields.
|
||||
uint8_t aux_data_ref_count; ///< Number of Auxiliary Data Reference fields.
|
||||
nfc_ac_rec_data_ref_t * p_aux_data_ref; ///< Pointer to the Auxiliary Data Reference fields.
|
||||
} nfc_ac_rec_payload_desc_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constructor for an NFC NDEF Alternative Carrier record payload.
|
||||
*
|
||||
* This function encodes the payload of an Alternative Carrier record as specified in the Connection
|
||||
* Handover standard. It implements an API compatible with @ref p_payload_constructor_t.
|
||||
*/
|
||||
ret_code_t nfc_ac_rec_payload_constructor(nfc_ac_rec_payload_desc_t * p_nfc_rec_ac_payload_desc,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len);
|
||||
|
||||
/**
|
||||
* @brief External reference to the type field of the Alternative Carrier record, defined in the
|
||||
* file @c nfc_ac_rec.c. It is used in the @ref NFC_NDEF_AC_RECORD_DESC_DEF macro.
|
||||
*/
|
||||
extern const uint8_t nfc_ac_rec_type_field[2];
|
||||
|
||||
/**
|
||||
* @brief Size of the type field of the Alternative Carrier record, defined in the
|
||||
* file @c nfc_ac_rec.c. It is used in the @ref NFC_NDEF_AC_RECORD_DESC_DEF macro.
|
||||
*/
|
||||
#define NFC_AC_REC_TYPE_LENGTH 2
|
||||
|
||||
/**
|
||||
*@brief Macro for creating and initializing an NFC NDEF record descriptor for an Alternative Carrier record.
|
||||
*
|
||||
* This macro creates and initializes a static instance of type @ref nfc_ndef_record_desc_t and
|
||||
* a static instance of type @ref nfc_ac_rec_payload_desc_t, which together constitute an instance of an Alternative Carrier record.
|
||||
*
|
||||
* Use the macro @ref NFC_NDEF_AC_RECORD_DESC to access the NDEF Alternative Carrier record descriptor instance.
|
||||
*
|
||||
* @param[in] NAME Name of the created record descriptor instance.
|
||||
* @param[in] CPS Carrier Power State value.
|
||||
* @param[in] CARR_DATA_REF_LEN Length of the Carrier Data Reference field.
|
||||
* @param[in] P_CARR_DATA_REF Pointer to the Carrier Data Reference field.
|
||||
* @param[in] MAX_AUX_DATA_REF Maximum number of Auxiliary Data Reference fields.
|
||||
*/
|
||||
#define NFC_NDEF_AC_RECORD_DESC_DEF(NAME, \
|
||||
CPS, \
|
||||
CARR_DATA_REF_LEN, \
|
||||
P_CARR_DATA_REF, \
|
||||
MAX_AUX_DATA_REF) \
|
||||
static nfc_ac_rec_data_ref_t NAME##_nfc_ac_rec_aux_data_ref_array[MAX_AUX_DATA_REF]; \
|
||||
static nfc_ac_rec_payload_desc_t NAME##_nfc_ac_rec_payload_desc = \
|
||||
{ \
|
||||
.cps = CPS, \
|
||||
.carrier_data_ref = {CARR_DATA_REF_LEN, P_CARR_DATA_REF}, \
|
||||
.max_aux_data_ref = MAX_AUX_DATA_REF, \
|
||||
.aux_data_ref_count = 0, \
|
||||
.p_aux_data_ref = NAME##_nfc_ac_rec_aux_data_ref_array \
|
||||
}; \
|
||||
NFC_NDEF_GENERIC_RECORD_DESC_DEF(NAME, \
|
||||
TNF_WELL_KNOWN, \
|
||||
0, \
|
||||
0, \
|
||||
nfc_ac_rec_type_field, \
|
||||
NFC_AC_REC_TYPE_LENGTH, \
|
||||
nfc_ac_rec_payload_constructor, \
|
||||
&(NAME##_nfc_ac_rec_payload_desc))
|
||||
|
||||
/**
|
||||
* @brief Macro for accessing the NFC NDEF Alternative Carrier record descriptor
|
||||
* instance that was created with @ref NFC_NDEF_AC_RECORD_DESC_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_AC_RECORD_DESC(NAME) NFC_NDEF_GENERIC_RECORD_DESC(NAME)
|
||||
|
||||
/**
|
||||
* @brief Function for clearing an Auxiliary Data Reference in an NFC NDEF Alternative Carrier record.
|
||||
*
|
||||
* This function clears the Auxiliary Data References from the Alternative Carrier record.
|
||||
*
|
||||
* @param[in, out] p_ac_rec Pointer to the Alternative Carrier record descriptor.
|
||||
*/
|
||||
void nfc_ac_rec_auxiliary_data_ref_clear(nfc_ndef_record_desc_t * p_ac_rec);
|
||||
|
||||
/**
|
||||
* @brief Function for adding an Auxiliary Data Reference to an NFC NDEF Alternative Carrier record.
|
||||
*
|
||||
* @param[in, out] p_ac_rec Pointer to an ac record.
|
||||
* @param[in] p_aux_data Pointer to the Auxiliary Data Reference data buffer.
|
||||
* @param[in] aux_length Length of the Auxiliary Data Reference data.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the Auxiliary Data Reference was added successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the record already contains the maximum number of Auxiliary Data References.
|
||||
*/
|
||||
ret_code_t nfc_ac_rec_auxiliary_data_ref_add(nfc_ndef_record_desc_t * p_ac_rec,
|
||||
uint8_t * p_aux_data,
|
||||
uint8_t aux_length);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_AC_REC_H__
|
||||
@@ -0,0 +1,393 @@
|
||||
/**
|
||||
* Copyright (c) 2012 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "nfc_ble_oob_advdata.h"
|
||||
#include "sdk_common.h"
|
||||
#include "nfc_ble_pair_msg.h"
|
||||
|
||||
// LESC OOB data AD_TYPE values.
|
||||
#define BLE_GAP_AD_TYPE_LESC_CONFIRM_VALUE 0x22
|
||||
#define BLE_GAP_AD_TYPE_LESC_RANDOM_VALUE 0x23
|
||||
|
||||
/**
|
||||
* @brief Macro for verifying basic parameters used for encoding single BLE AD Type.
|
||||
*
|
||||
* It verifies if provided buffer is NULL and if there is enough space for the encoded data.
|
||||
* In case of NULL pointer buffer, necessary space for current AD Type is calculated.
|
||||
*
|
||||
* @param[in] P_ENCODED_DATA Buffer for the encoded data.
|
||||
* @param[in] P_OFFSET Pointer to index of the first free cell in the buffer.
|
||||
* @param[in] AD_TYPE_SIZE Size of the single AD Type.
|
||||
* @param[in] MAX_SIZE Maximal size of the provided buffer.
|
||||
*/
|
||||
#define NFC_BLE_OOB_ADVDATA_INPUT_VERIFY( P_ENCODED_DATA, P_OFFSET, AD_TYPE_SIZE, MAX_SIZE) \
|
||||
if ( (P_ENCODED_DATA) == NULL ) \
|
||||
{ \
|
||||
*(P_OFFSET) += (AD_TYPE_SIZE); \
|
||||
return NRF_SUCCESS; \
|
||||
} \
|
||||
if ( *(P_OFFSET) + (AD_TYPE_SIZE) > (MAX_SIZE) ) \
|
||||
{ \
|
||||
return NRF_ERROR_DATA_SIZE; \
|
||||
}
|
||||
|
||||
/**@brief Function for encoding data of Security Manager OOB Flags AD Type.
|
||||
*
|
||||
* @param[in] oob_flags Security Manager OOB Flags AD Type payload.
|
||||
* @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
|
||||
* @param[in,out] p_offset \c in: Offset of \p p_encoded_data buffer before this AD type encoding.
|
||||
* \c out: Offset of \p p_encoded_data buffer after this AD type encoding.
|
||||
* @param[in] max_size Size of \p p_encoded_data buffer.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_DATA_SIZE If the provided buffer size is too small.
|
||||
*/
|
||||
static ret_code_t sec_mgr_oob_flags_encode(uint8_t oob_flags,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
NFC_BLE_OOB_ADVDATA_INPUT_VERIFY(p_encoded_data, p_offset, AD_TYPE_OOB_FLAGS_SIZE, max_size);
|
||||
|
||||
// Encode flags.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_OOB_FLAGS_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SECURITY_MANAGER_OOB_FLAGS;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = oob_flags;
|
||||
*p_offset += AD_TYPE_OOB_FLAGS_DATA_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/**@brief Function for encoding data of Security Manager TK Value AD Type.
|
||||
*
|
||||
* @param[in] p_tk_value Security Manager TK Value AD Type payload.
|
||||
* @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
|
||||
* @param[in,out] p_offset \c in: Offset of \p p_encoded_data buffer before this AD type encoding.
|
||||
* \c out: Offset of \p p_encoded_data buffer after this AD type encoding.
|
||||
* @param[in] max_size Size of \p p_encoded_data buffer.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_DATA_SIZE If the provided buffer size is too small.
|
||||
*/
|
||||
static ret_code_t tk_value_encode(ble_advdata_tk_value_t * p_tk_value,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
|
||||
NFC_BLE_OOB_ADVDATA_INPUT_VERIFY(p_encoded_data, p_offset, AD_TYPE_TK_VALUE_SIZE, max_size);
|
||||
|
||||
// Encode TK Value.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_TK_VALUE_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_SECURITY_MANAGER_TK_VALUE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
|
||||
// Remember location of TK in the buffer if this feature was enabled.
|
||||
err_code = nfc_tk_to_group_add(&p_encoded_data[*p_offset]);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
nfc_tk_value_payload_encode(p_tk_value, &p_encoded_data[*p_offset]);
|
||||
(*p_offset) += AD_TYPE_TK_VALUE_DATA_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/**@brief Function for encoding LESC OOB data in the CH NDEF message.
|
||||
*
|
||||
* @param[in] p_lesc_value Pointer to the LESC OOB values to be encoded.
|
||||
* @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
|
||||
* @param[in,out] p_offset \c in: Offset of \p p_encoded_data buffer before this AD type encoding.
|
||||
* \c out: Offset of \p p_encoded_data buffer after this AD type encoding.
|
||||
* @param[in] max_size Size of \p p_encoded_data buffer.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_DATA_SIZE If the provided buffer size is too small.
|
||||
*/
|
||||
static ret_code_t lesc_value_encode(ble_gap_lesc_oob_data_t * p_lesc_value,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
|
||||
NFC_BLE_OOB_ADVDATA_INPUT_VERIFY(p_encoded_data, p_offset, AD_TYPE_LESC_SIZE, max_size);
|
||||
|
||||
// Encode LESC Confirm Value.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_CONFIRM_VALUE_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LESC_CONFIRM_VALUE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
|
||||
memcpy(&p_encoded_data[*p_offset], p_lesc_value->c, sizeof(p_lesc_value->c));
|
||||
|
||||
uint8_t *p_confirm = &p_encoded_data[*p_offset];
|
||||
|
||||
(*p_offset) += AD_TYPE_CONFIRM_VALUE_DATA_SIZE;
|
||||
|
||||
// Encode LESC Random Value.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_RANDOM_VALUE_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LESC_RANDOM_VALUE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
|
||||
memcpy(&p_encoded_data[*p_offset], p_lesc_value->r, sizeof(p_lesc_value->r));
|
||||
|
||||
uint8_t *p_random = &p_encoded_data[*p_offset];
|
||||
|
||||
(*p_offset) += AD_TYPE_RANDOM_VALUE_DATA_SIZE;
|
||||
|
||||
// Remember location of LESC OOB data in the buffer in case of key changes.
|
||||
err_code = nfc_lesc_pos_set(p_confirm, p_random);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/**@brief Function for encoding data of LE Role AD Type.
|
||||
*
|
||||
* @param[in] le_role LE Role AD Type payload.
|
||||
* @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
|
||||
* @param[in,out] p_offset \c in: Offset of \p p_encoded_data buffer before this AD type encoding.
|
||||
* \c out: Offset of \p p_encoded_data buffer after this AD type encoding.
|
||||
* @param[in] max_size Size of \p p_encoded_data buffer.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_DATA_SIZE If the provided buffer size is too small.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If \p le_role parameter has invalid value.
|
||||
*/
|
||||
static ret_code_t le_role_encode(ble_advdata_le_role_t le_role,
|
||||
uint8_t * p_encoded_data,
|
||||
uint16_t * p_offset,
|
||||
uint16_t max_size)
|
||||
{
|
||||
NFC_BLE_OOB_ADVDATA_INPUT_VERIFY(p_encoded_data, p_offset, AD_TYPE_LE_ROLE_SIZE, max_size);
|
||||
|
||||
// Encode LE Role.
|
||||
p_encoded_data[*p_offset] = (uint8_t)(ADV_AD_TYPE_FIELD_SIZE + AD_TYPE_LE_ROLE_DATA_SIZE);
|
||||
*p_offset += ADV_LENGTH_FIELD_SIZE;
|
||||
p_encoded_data[*p_offset] = BLE_GAP_AD_TYPE_LE_ROLE;
|
||||
*p_offset += ADV_AD_TYPE_FIELD_SIZE;
|
||||
switch (le_role)
|
||||
{
|
||||
case BLE_ADVDATA_ROLE_ONLY_PERIPH:
|
||||
p_encoded_data[*p_offset] = NFC_BLE_ADVDATA_ROLE_ENCODED_ONLY_PERIPH;
|
||||
break;
|
||||
case BLE_ADVDATA_ROLE_ONLY_CENTRAL:
|
||||
p_encoded_data[*p_offset] = NFC_BLE_ADVDATA_ROLE_ENCODED_ONLY_CENTRAL;
|
||||
break;
|
||||
case BLE_ADVDATA_ROLE_BOTH_PERIPH_PREFERRED:
|
||||
p_encoded_data[*p_offset] = NFC_BLE_ADVDATA_ROLE_ENCODED_BOTH_PERIPH_PREFERRED;
|
||||
break;
|
||||
case BLE_ADVDATA_ROLE_BOTH_CENTRAL_PREFERRED:
|
||||
p_encoded_data[*p_offset] = NFC_BLE_ADVDATA_ROLE_ENCODED_BOTH_CENTRAL_PREFERRED;
|
||||
break;
|
||||
default:
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
*p_offset += AD_TYPE_LE_ROLE_DATA_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/**@brief Function for calculating the size of Local Name AD Type.
|
||||
*
|
||||
* @param[in] p_advdata Pointer to the structure for specifying the content of encoded data.
|
||||
* @param[out] p_len Size of the buffer that is necessary to encode Local Name AD Type.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval Other Other error codes might be returned depending on
|
||||
* @ref sd_ble_gap_device_name_get function.
|
||||
*/
|
||||
__STATIC_INLINE ret_code_t nfc_ble_oob_name_size_calc(ble_advdata_t const * const p_advdata,
|
||||
uint16_t * const p_len)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
uint16_t device_len;
|
||||
|
||||
if (p_advdata->name_type == BLE_ADVDATA_SHORT_NAME)
|
||||
{
|
||||
device_len = p_advdata->short_name_len;
|
||||
}
|
||||
else
|
||||
{
|
||||
err_code = sd_ble_gap_device_name_get(NULL, &device_len);
|
||||
}
|
||||
|
||||
*p_len += ADV_LENGTH_FIELD_SIZE + ADV_AD_TYPE_FIELD_SIZE + device_len;
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/**@brief Function for calculating the size of AD Types which are encoded by @ref adv_data_encode function.
|
||||
*
|
||||
* @param[in] p_advdata Pointer to the structure for specifying the content of encoded data.
|
||||
* @param[out] p_len Size of the buffer that is necessary to encode AD Types.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval Other Other error codes might be returned depending on
|
||||
* @ref nfc_ble_oob_name_size_calc function.
|
||||
*/
|
||||
static ret_code_t nfc_ble_oob_adv_data_size_calc(ble_advdata_t const * const p_advdata,
|
||||
uint16_t * const p_len)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
|
||||
if (p_advdata->include_ble_device_addr)
|
||||
{
|
||||
*p_len += AD_TYPE_BLE_DEVICE_ADDR_SIZE;
|
||||
}
|
||||
if (p_advdata->include_appearance)
|
||||
{
|
||||
*p_len += AD_TYPE_APPEARANCE_SIZE;
|
||||
}
|
||||
if (p_advdata->flags != 0)
|
||||
{
|
||||
*p_len += AD_TYPE_FLAGS_SIZE;
|
||||
}
|
||||
if (p_advdata->name_type != BLE_ADVDATA_NO_NAME)
|
||||
{
|
||||
err_code = nfc_ble_oob_name_size_calc(p_advdata, p_len);
|
||||
}
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/**@brief Function for verifying if BLE advertising data structure contains only supported AD Types
|
||||
* by this encoding module.
|
||||
*
|
||||
* @param[in] advdata Structure with BLE advertising data.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the verification was successful.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If there is any AD type which is not supported by this
|
||||
* module.
|
||||
*/
|
||||
static ret_code_t nfc_ble_oob_adv_data_check(ble_advdata_t advdata)
|
||||
{
|
||||
advdata.p_sec_mgr_oob_flags = NULL;
|
||||
advdata.p_tk_value = NULL;
|
||||
advdata.le_role = BLE_ADVDATA_ROLE_NOT_PRESENT;
|
||||
advdata.include_ble_device_addr = false;
|
||||
advdata.include_appearance = false;
|
||||
advdata.flags = 0;
|
||||
advdata.name_type = BLE_ADVDATA_NO_NAME;
|
||||
advdata.short_name_len = 0;
|
||||
advdata.p_lesc_data = NULL;
|
||||
|
||||
ble_advdata_t pattern_advdata;
|
||||
memset(&pattern_advdata, 0, sizeof(ble_advdata_t));
|
||||
|
||||
if ( memcmp( &pattern_advdata, &advdata, sizeof(ble_advdata_t)) == 0 )
|
||||
{
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
}
|
||||
|
||||
ret_code_t nfc_ble_oob_adv_data_encode(ble_advdata_t const * const p_advdata,
|
||||
uint8_t * const p_encoded_data,
|
||||
uint16_t * const p_len)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
uint16_t max_size = *p_len;
|
||||
uint16_t offset = 0;
|
||||
|
||||
// Verify ADV data structure.
|
||||
err_code = nfc_ble_oob_adv_data_check(*p_advdata);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
|
||||
// Encode Security Manager OOB Flags.
|
||||
if (p_advdata->p_sec_mgr_oob_flags != NULL)
|
||||
{
|
||||
err_code = sec_mgr_oob_flags_encode(*p_advdata->p_sec_mgr_oob_flags,
|
||||
p_encoded_data,
|
||||
&offset,
|
||||
max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode LESC keys
|
||||
if (p_advdata->p_lesc_data != NULL)
|
||||
{
|
||||
err_code = lesc_value_encode(p_advdata->p_lesc_data, p_encoded_data, &offset, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode Security Manager TK value.
|
||||
if (p_advdata->p_tk_value != NULL)
|
||||
{
|
||||
err_code = tk_value_encode(p_advdata->p_tk_value, p_encoded_data, &offset, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode LE Role.
|
||||
if (BLE_ADVDATA_ROLE_NOT_PRESENT != p_advdata->le_role)
|
||||
{
|
||||
err_code = le_role_encode(p_advdata->le_role, p_encoded_data, &offset, max_size);
|
||||
VERIFY_SUCCESS(err_code);
|
||||
}
|
||||
|
||||
// Encode remaining AD Types or precalculate necessary buffer space.
|
||||
if (p_encoded_data != NULL)
|
||||
{
|
||||
uint16_t adv_data_size = max_size - offset;
|
||||
err_code = adv_data_encode(p_advdata, p_encoded_data + offset, &adv_data_size);
|
||||
*p_len = offset + adv_data_size;
|
||||
}
|
||||
else
|
||||
{
|
||||
err_code = nfc_ble_oob_adv_data_size_calc(p_advdata, &offset);
|
||||
*p_len = offset;
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
void nfc_tk_value_payload_encode(ble_advdata_tk_value_t * p_tk_value,
|
||||
uint8_t * p_tk_payload_data)
|
||||
{
|
||||
for (uint8_t i = 0; i < AD_TYPE_TK_VALUE_DATA_SIZE; i++)
|
||||
{
|
||||
*(p_tk_payload_data++) = p_tk_value->tk[i];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
/**
|
||||
* Copyright (c) 2012 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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.
|
||||
*
|
||||
*/
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @defgroup nfc_ble_oob_advdata Advertising and Scan Response Data Encoder for NFC OOB pairing
|
||||
* @{
|
||||
* @ingroup nfc_ble_pair_msg
|
||||
* @brief Function for encoding data in the Advertising and Scan Response Data format, which
|
||||
* can be used to create payload of NFC message intended for initiating the Out-of-Band
|
||||
* pairing.
|
||||
*/
|
||||
|
||||
#ifndef NFC_BLE_OOB_ADVDATA_H__
|
||||
#define NFC_BLE_OOB_ADVDATA_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "ble_advdata.h"
|
||||
#include "app_util.h"
|
||||
#include "sdk_errors.h"
|
||||
|
||||
|
||||
#define AD_TYPE_LE_ROLE_DATA_SIZE 1UL /**< Data size (in octets) of the LE Bluetooth Device Address AD type. */
|
||||
#define AD_TYPE_LE_ROLE_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_LE_ROLE_DATA_SIZE) /**< Size (in octets) of the LE Bluetooth Device Address AD type. */
|
||||
#define AD_TYPE_TK_VALUE_DATA_SIZE (sizeof(ble_advdata_tk_value_t)) /**< Data size (in octets) of the Security Manager TK value AD type. */
|
||||
#define AD_TYPE_TK_VALUE_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_TK_VALUE_DATA_SIZE) /**< Size (in octets) of the Security Manager TK value AD type. */
|
||||
#define AD_TYPE_OOB_FLAGS_DATA_SIZE 1UL /**< Data size (in octets) of the Security Manager OOB Flags AD type. */
|
||||
#define AD_TYPE_OOB_FLAGS_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_OOB_FLAGS_DATA_SIZE) /**< Size (in octets) of the Security Manager OOB Flags AD type. */
|
||||
|
||||
#define AD_TYPE_CONFIRM_VALUE_DATA_SIZE 16UL /**< Data size (in octets) of the LESC Confirmation value. */
|
||||
#define AD_TYPE_CONFIRM_VALUE_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_CONFIRM_VALUE_DATA_SIZE) /**< Size (in octets) of the LESC Confirmation value AD type. */
|
||||
#define AD_TYPE_RANDOM_VALUE_DATA_SIZE 16UL /**< Data size (in octets) of the LESC Random value. */
|
||||
#define AD_TYPE_RANDOM_VALUE_SIZE (ADV_AD_DATA_OFFSET + \
|
||||
AD_TYPE_RANDOM_VALUE_DATA_SIZE) /**< Size (in octets) of the LESC Random value AD type. */
|
||||
#define AD_TYPE_LESC_SIZE (AD_TYPE_RANDOM_VALUE_SIZE + \
|
||||
AD_TYPE_CONFIRM_VALUE_SIZE) /**< Size (in octets) of the LESC OOB AD data field in NDEF message. */
|
||||
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_SET 1U /**< Security Manager OOB Flag set. Flag selection is done using _POS defines */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_CLEAR 0U /**< Security Manager OOB Flag clear. Flag selection is done using _POS defines */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_OOB_DATA_PRESENT_POS 0UL /**< Security Manager OOB Data Present Flag position. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_OOB_LE_SUPPORTED_POS 1UL /**< Security Manager OOB Low Energy Supported Flag position. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_SIM_LE_AND_EP_POS 2UL /**< Security Manager OOB Simultaneous LE and BR/EDR to Same Device Capable Flag position. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_ADDRESS_TYPE_PUBLIC 0UL /**< Security Manager OOB Public Address type. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_ADDRESS_TYPE_RANDOM 1UL /**< Security Manager OOB Random Address type. */
|
||||
#define AD_TYPE_SEC_MGR_OOB_FLAG_ADDRESS_TYPE_POS 3UL /**< Security Manager OOB Address type Flag (0 = Public Address, 1 = Random Address) position. */
|
||||
|
||||
/**@brief Payload field values of LE Role BLE GAP AD Type. Corresponds with @ref ble_advdata_le_role_t enum. */
|
||||
typedef enum
|
||||
{
|
||||
NFC_BLE_ADVDATA_ROLE_ENCODED_ONLY_PERIPH = 0, /**< Only Peripheral Role supported. */
|
||||
NFC_BLE_ADVDATA_ROLE_ENCODED_ONLY_CENTRAL, /**< Only Central Role supported. */
|
||||
NFC_BLE_ADVDATA_ROLE_ENCODED_BOTH_PERIPH_PREFERRED, /**< Peripheral and Central Role supported. Peripheral Role preferred for connection establishment. */
|
||||
NFC_BLE_ADVDATA_ROLE_ENCODED_BOTH_CENTRAL_PREFERRED /**< Peripheral and Central Role supported. Central Role preferred for connection establishment */
|
||||
} nfc_ble_advdata_le_role_encoded_t;
|
||||
|
||||
/**@brief Function for encoding data in the Advertising and Scan Response data format, which
|
||||
* is used for NFC OOB pairing.
|
||||
*
|
||||
*
|
||||
* @details This function encodes data into the Advertising and Scan Response data format (AD structures).
|
||||
* Encoding is based on the selections in the supplied structures. This function uses
|
||||
* @ref adv_data_encode to encode regular data and adds additional AD Structures which are specific
|
||||
* for NFC OOB pairing: Security Manager TK Value, LESC OOB values, OOB Flags, and LE Role.
|
||||
*
|
||||
* @param[in] p_advdata Pointer to the structure for specifying the content of encoded data.
|
||||
* @param[out] p_encoded_data Pointer to the buffer where encoded data will be returned.
|
||||
* @param[in,out] p_len \c in: Size of \p p_encoded_data buffer.
|
||||
* \c out: Length of encoded data.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If the operation failed because a wrong parameter was provided in \p p_advdata.
|
||||
* @retval NRF_ERROR_DATA_SIZE If the operation failed because not all the requested data could fit into the
|
||||
* provided buffer or some encoded AD structure is too long and its
|
||||
* length cannot be encoded with one octet.
|
||||
*/
|
||||
ret_code_t nfc_ble_oob_adv_data_encode(ble_advdata_t const * const p_advdata,
|
||||
uint8_t * const p_encoded_data,
|
||||
uint16_t * const p_len);
|
||||
|
||||
/**@brief Function for encoding payload field of Security Manager TK Value AD Type.
|
||||
*
|
||||
* @param[in] p_tk_value Security Manager TK Value AD Type payload.
|
||||
* @param[out] p_tk_payload_data Pointer to the buffer where TK payload data will be stored.
|
||||
*
|
||||
*/
|
||||
void nfc_tk_value_payload_encode(ble_advdata_tk_value_t * p_tk_value,
|
||||
uint8_t * p_tk_payload_data);
|
||||
|
||||
#endif // NFC_BLE_OOB_ADVDATA_H__
|
||||
|
||||
/** @} */
|
||||
@@ -0,0 +1,557 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "nfc_ble_pair_msg.h"
|
||||
#include "nfc_hs_rec.h"
|
||||
#include "nfc_ac_rec.h"
|
||||
#include "nfc_le_oob_rec.h"
|
||||
#include "nfc_ep_oob_rec.h"
|
||||
#include "nfc_ndef_msg.h"
|
||||
#include "sdk_macros.h"
|
||||
|
||||
/**
|
||||
* @brief Descriptor of TK value locations in Connection Handover NDEF message.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t ** pp_tk_group; /**< Pointer to array of pointer with TK locations in CH NDEF message. */
|
||||
uint8_t tk_num; /**< Number of valid TK locations. */
|
||||
uint8_t tk_max_num; /**< Maximal number of possible TK locations. */
|
||||
} nfc_ble_tk_group_t;
|
||||
|
||||
/**
|
||||
* @brief Descriptor of LESC OOB data in Connection Handover NDEF message.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t * confirm; /**< Pointer to the LESC OOB confirmation value in the CH NDEF message. */
|
||||
uint8_t * random; /**< Pointer to the LESC OOB random value in the CH NDEF message. */
|
||||
} nfc_ble_lesc_data_pos_t;
|
||||
|
||||
static nfc_ble_lesc_data_pos_t m_lesc_pos = {NULL, NULL}; /**< Descriptor used to update LESC keys in the NDEF Message */
|
||||
static nfc_ble_tk_group_t m_tk_group; /**< Descriptor used to find TK locations in the NDEF Message which require update. */
|
||||
static bool m_tk_modifier_on = false; /**< Flag indicating that TK modifier feature is on. */
|
||||
|
||||
/* Default value for Security Manager Out Of Band Flags field in BLE AD structure */
|
||||
/* which is used for EP OOB Record payload */
|
||||
static const uint8_t sec_mgr_oob_flags =
|
||||
(AD_TYPE_SEC_MGR_OOB_FLAG_SET << AD_TYPE_SEC_MGR_OOB_FLAG_OOB_DATA_PRESENT_POS) |
|
||||
(AD_TYPE_SEC_MGR_OOB_FLAG_SET << AD_TYPE_SEC_MGR_OOB_FLAG_OOB_LE_SUPPORTED_POS) |
|
||||
(AD_TYPE_SEC_MGR_OOB_FLAG_CLEAR << AD_TYPE_SEC_MGR_OOB_FLAG_SIM_LE_AND_EP_POS) |
|
||||
(AD_TYPE_SEC_MGR_OOB_ADDRESS_TYPE_RANDOM << AD_TYPE_SEC_MGR_OOB_FLAG_ADDRESS_TYPE_POS);
|
||||
|
||||
/**@brief Function for configuring TK group modifier feature.
|
||||
*
|
||||
* @details This function configures the structure which is responsible for tracking TK locations.
|
||||
* These locations can be afterwards easily accessed with @ref nfc_tk_group_modifier_update
|
||||
* and modified.
|
||||
*
|
||||
* @param[in] pp_tk_group Pointer to array of TK locations that should be modified with
|
||||
* @ref nfc_tk_group_modifier_update function.
|
||||
* @param[in] max_group_size Maximal number of TK locations that can added to \p pp_tk_group.
|
||||
*/
|
||||
__STATIC_INLINE void nfc_tk_group_modifier_config(uint8_t ** pp_tk_group, uint8_t max_group_size)
|
||||
{
|
||||
m_tk_group.pp_tk_group = pp_tk_group;
|
||||
m_tk_group.tk_num = 0;
|
||||
m_tk_group.tk_max_num = max_group_size;
|
||||
}
|
||||
|
||||
/** @brief Function for generating a description of a simplified LE OOB message according to the BLE
|
||||
* AD structure.
|
||||
*
|
||||
* This function declares and initializes a static instance of a simplified LE OOB message
|
||||
* with Bluetooth Carrier Configuration LE record. Payload of this record can be configured
|
||||
* via AD structure.
|
||||
*
|
||||
* @param[in] p_le_advdata Pointer to the AD for LE OOB record.
|
||||
* @param[out] pp_le_oob_msg_desc Pointer to pointer to the NDEF message instance.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
static ret_code_t nfc_ble_simplified_le_oob_msg_declare(ble_advdata_t const * const p_le_advdata,
|
||||
nfc_ndef_msg_desc_t ** pp_le_oob_msg_desc)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
nfc_ndef_record_desc_t * p_nfc_le_oob_record;
|
||||
|
||||
/* Create NFC NDEF message description, capacity - 1 record */
|
||||
NFC_NDEF_MSG_DEF(nfc_le_oob_msg, 1);
|
||||
|
||||
/* The message description is static, therefore */
|
||||
/* you must clear the message (needed for supporting multiple calls) */
|
||||
nfc_ndef_msg_clear(&NFC_NDEF_MSG(nfc_le_oob_msg));
|
||||
|
||||
if (p_le_advdata != NULL)
|
||||
{
|
||||
/* Create NFC NDEF LE OOB Record description without record ID field */
|
||||
p_nfc_le_oob_record = nfc_le_oob_rec_declare(0 , p_le_advdata);
|
||||
|
||||
/* Add LE OOB Record as lone record to message */
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_le_oob_msg), p_nfc_le_oob_record);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
*pp_le_oob_msg_desc = &NFC_NDEF_MSG(nfc_le_oob_msg);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/** @brief Function for generating a description of a simplified EP OOB message according to the BLE
|
||||
* AD structure.
|
||||
*
|
||||
* This function declares and initializes a static instance of a simplified EP OOB message
|
||||
* with Bluetooth Carrier Configuration EP record. Payload of this record can be configured
|
||||
* via AD structure.
|
||||
*
|
||||
* @param[in] p_ep_advdata Pointer to the AD structure for EP OOB record.
|
||||
* @param[out] pp_ep_oob_msg_desc Pointer to pointer to the NDEF message instance.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
static ret_code_t nfc_ble_simplified_ep_oob_msg_declare(ble_advdata_t const * const p_ep_advdata,
|
||||
nfc_ndef_msg_desc_t ** pp_ep_oob_msg_desc)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
nfc_ndef_record_desc_t * p_nfc_ep_oob_record;
|
||||
|
||||
/* Create NFC NDEF message description, capacity - 1 record */
|
||||
NFC_NDEF_MSG_DEF(nfc_ep_oob_msg, 1);
|
||||
|
||||
/* The message description is static, therefore */
|
||||
/* you must clear the message (needed for supporting multiple calls) */
|
||||
nfc_ndef_msg_clear(&NFC_NDEF_MSG(nfc_ep_oob_msg));
|
||||
|
||||
if (p_ep_advdata != NULL)
|
||||
{
|
||||
/* Create NFC NDEF EP OOB Record description without record ID field */
|
||||
p_nfc_ep_oob_record = nfc_ep_oob_rec_declare(0 , p_ep_advdata);
|
||||
|
||||
/* Add EP OOB Record as lone record to message */
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_ep_oob_msg), p_nfc_ep_oob_record);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
*pp_ep_oob_msg_desc = &NFC_NDEF_MSG(nfc_ep_oob_msg);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/** @brief Function for generating a description of a Handover Select NDEF message according to
|
||||
* the BLE AD structures.
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF message description
|
||||
* of a Handover Select NDEF message with a Hs record and two OOB records (LE and EP with
|
||||
* modifications for Windows). Payload of these records can be configured via AD structures.
|
||||
*
|
||||
* @warning The order of LE and EP records cannot be changed. Android devices are able to pair
|
||||
* correctly only when the LE record appears before the EP record.
|
||||
*
|
||||
* @param[in] p_le_advdata Pointer to the AD structure for LE OOB record.
|
||||
* @param[in] p_ep_advdata Pointer to the AD structure for EP OOB record.
|
||||
* @param[out] pp_bt_oob_full_msg Pointer to a pointer to the NDEF message instance.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
static ret_code_t nfc_ble_full_handover_select_msg_declare(ble_advdata_t const * const p_le_advdata,
|
||||
ble_advdata_t const * const p_ep_advdata,
|
||||
nfc_ndef_msg_desc_t ** pp_bt_oob_full_msg)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
|
||||
// Carrier reference buffers for ac records.
|
||||
static uint8_t carrier_le_reference = '0';
|
||||
static uint8_t carrier_ep_reference = '1';
|
||||
|
||||
// Create ac records for both message types.
|
||||
NFC_NDEF_AC_RECORD_DESC_DEF(ac_rec_le, NFC_AC_CPS_ACTIVE, 1, &carrier_le_reference, 1);
|
||||
NFC_NDEF_AC_RECORD_DESC_DEF(ac_rec_ep, NFC_AC_CPS_ACTIVE, 1, &carrier_ep_reference, 1);
|
||||
|
||||
// Create a Hs record and assign existing ac records to it.
|
||||
NFC_NDEF_HS_RECORD_DESC_DEF(hs_rec, 1, 3, 2);
|
||||
|
||||
nfc_ndef_record_desc_t * p_nfc_hs_record = &NFC_NDEF_HS_RECORD_DESC(hs_rec);
|
||||
|
||||
// Clear the record before assigning local records to it (in case this function has already been called).
|
||||
nfc_hs_rec_local_record_clear(p_nfc_hs_record);
|
||||
|
||||
err_code = nfc_hs_rec_local_record_add(p_nfc_hs_record, &NFC_NDEF_AC_RECORD_DESC(ac_rec_le));
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
err_code = nfc_hs_rec_local_record_add(p_nfc_hs_record, &NFC_NDEF_AC_RECORD_DESC(ac_rec_ep));
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
// Create le and ep records.
|
||||
nfc_ndef_record_desc_t * p_nfc_le_oob_record =
|
||||
nfc_le_oob_rec_declare(carrier_le_reference , p_le_advdata);
|
||||
|
||||
nfc_ndef_record_desc_t * p_nfc_ep_oob_record =
|
||||
nfc_ep_oob_rec_declare(carrier_ep_reference , p_ep_advdata);
|
||||
|
||||
// Create full NDEF Handover Select message for Connection Handover and assign Hs, le and ep records to it.
|
||||
NFC_NDEF_MSG_DEF(hs_full_msg, 3);
|
||||
|
||||
// Clear the message before assigning records to it (in case this function has already been called).
|
||||
nfc_ndef_msg_clear(&NFC_NDEF_MSG(hs_full_msg));
|
||||
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(hs_full_msg), p_nfc_hs_record);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(hs_full_msg), p_nfc_le_oob_record);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(hs_full_msg), p_nfc_ep_oob_record);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
*pp_bt_oob_full_msg = &NFC_NDEF_MSG(hs_full_msg);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/** @brief Function for creating an AD structure with common configuration for EP and LE OOB records.
|
||||
*
|
||||
* This function creates an AD structure and initializes its fields with default content. Only
|
||||
* fields that are common for both EP and LE OOB records are filled.
|
||||
*
|
||||
* @param[in] p_tk_value Pointer to the authentication Temporary Key (TK). If NULL,
|
||||
* TK field of the returned AD structure is empty.
|
||||
* @param[out] p_adv_data Pointer to BLE AD structure with common configuration for EP and
|
||||
* LE OOB records.
|
||||
*/
|
||||
static void common_adv_data_create(ble_advdata_tk_value_t * const p_tk_value,
|
||||
ble_gap_lesc_oob_data_t * const p_lesc_data,
|
||||
ble_advdata_t * const p_adv_data)
|
||||
{
|
||||
memset((uint8_t *) p_adv_data, 0, sizeof(ble_advdata_t));
|
||||
|
||||
/* Set common configuration of AD structure for both Bluetooth EP and LE record */
|
||||
p_adv_data->include_appearance = true;
|
||||
p_adv_data->name_type = BLE_ADVDATA_FULL_NAME;
|
||||
p_adv_data->p_tk_value = NULL;
|
||||
if (p_tk_value != NULL)
|
||||
{
|
||||
p_adv_data->p_tk_value = p_tk_value;
|
||||
}
|
||||
|
||||
p_adv_data->p_lesc_data = p_lesc_data;
|
||||
}
|
||||
|
||||
/** @brief Function for creating an AD structure with default configuration for an LE OOB record.
|
||||
*
|
||||
* This function creates an AD structure and initializes its fields with default content for
|
||||
* LE OOB record payload.
|
||||
*
|
||||
* @param[in] p_tk_value Pointer to the authentication Temporary Key (TK). If NULL,
|
||||
* TK field of the returned AD structure is empty.
|
||||
* @param[out] p_le_adv_data Pointer to BLE AD structure with default configuration
|
||||
* for LE OOB record.
|
||||
*/
|
||||
static void le_oob_specific_adv_data_create(ble_advdata_tk_value_t * const p_tk_value,
|
||||
ble_gap_lesc_oob_data_t * const p_lesc_data,
|
||||
ble_advdata_t * const p_le_adv_data)
|
||||
{
|
||||
/* Create default configuration which is common for both EP and LE OOB Records */
|
||||
common_adv_data_create(p_tk_value, p_lesc_data, p_le_adv_data);
|
||||
|
||||
/* LE specific configuration */
|
||||
p_le_adv_data->include_ble_device_addr = true;
|
||||
p_le_adv_data->le_role = BLE_ADVDATA_ROLE_ONLY_PERIPH;
|
||||
p_le_adv_data->flags = BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
/** @brief Function for creating an AD structure with default configuration for an EP OOB record.
|
||||
*
|
||||
* This function creates an AD structure and initializes its fields with default content for
|
||||
* EP OOB record payload.
|
||||
*
|
||||
* @param[in] p_tk_value Pointer to the authentication Temporary Key (TK). If NULL,
|
||||
* TK field of the returned AD structure is empty.
|
||||
* @param[out] p_ep_adv_data Pointer to BLE AD structure with default configuration
|
||||
* for EP OOB record.
|
||||
*/
|
||||
static void ep_oob_specific_adv_data_create(ble_advdata_tk_value_t * const p_tk_value,
|
||||
ble_gap_lesc_oob_data_t * const p_lesc_data,
|
||||
ble_advdata_t * const p_ep_adv_data)
|
||||
{
|
||||
/* Create default configuration which is common for both EP and LE OOB Records */
|
||||
common_adv_data_create(p_tk_value, p_lesc_data, p_ep_adv_data);
|
||||
|
||||
/* EP specific configuration */
|
||||
p_ep_adv_data->p_sec_mgr_oob_flags = (uint8_t *) &sec_mgr_oob_flags;
|
||||
}
|
||||
|
||||
ret_code_t nfc_ble_simplified_le_oob_msg_encode(ble_advdata_t const * const p_le_advdata,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
nfc_ndef_msg_desc_t * p_le_oob_msg_desc;
|
||||
ret_code_t err_code;
|
||||
|
||||
err_code = nfc_ble_simplified_le_oob_msg_declare(p_le_advdata, &p_le_oob_msg_desc);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
if (!m_tk_modifier_on)
|
||||
{
|
||||
nfc_tk_group_modifier_config(NULL, 0);
|
||||
}
|
||||
|
||||
/* Encode whole message into buffer */
|
||||
err_code = nfc_ndef_msg_encode(p_le_oob_msg_desc,
|
||||
p_buf,
|
||||
p_len);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
ret_code_t nfc_ble_simplified_ep_oob_msg_encode(ble_advdata_t const * const p_ep_advdata,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
nfc_ndef_msg_desc_t * p_ep_oob_msg_desc;
|
||||
ret_code_t err_code;
|
||||
|
||||
err_code = nfc_ble_simplified_ep_oob_msg_declare(p_ep_advdata, &p_ep_oob_msg_desc);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
if (!m_tk_modifier_on)
|
||||
{
|
||||
nfc_tk_group_modifier_config(NULL, 0);
|
||||
}
|
||||
|
||||
/* Encode whole message into buffer */
|
||||
err_code = nfc_ndef_msg_encode(p_ep_oob_msg_desc,
|
||||
p_buf,
|
||||
p_len);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
ret_code_t nfc_ble_full_handover_select_msg_encode(ble_advdata_t const * const p_le_advdata,
|
||||
ble_advdata_t const * const p_ep_advdata,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
nfc_ndef_msg_desc_t * p_full_hs_msg_desc;
|
||||
ret_code_t err_code;
|
||||
|
||||
err_code = nfc_ble_full_handover_select_msg_declare(p_le_advdata,
|
||||
p_ep_advdata,
|
||||
&p_full_hs_msg_desc);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
if (!m_tk_modifier_on)
|
||||
{
|
||||
nfc_tk_group_modifier_config(NULL, 0);
|
||||
}
|
||||
|
||||
/* Encode whole message into buffer */
|
||||
err_code = nfc_ndef_msg_encode(p_full_hs_msg_desc,
|
||||
p_buf,
|
||||
p_len);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
ret_code_t nfc_ble_pair_default_msg_encode(nfc_ble_pair_type_t nfc_ble_pair_type,
|
||||
ble_advdata_tk_value_t * const p_tk_value,
|
||||
ble_gap_lesc_oob_data_t * const p_lesc_data,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
ble_advdata_t le_adv_data;
|
||||
ble_advdata_t ep_adv_data;
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
|
||||
switch (nfc_ble_pair_type)
|
||||
{
|
||||
|
||||
case NFC_BLE_PAIR_MSG_BLUETOOTH_LE_SHORT:
|
||||
le_oob_specific_adv_data_create(p_tk_value, p_lesc_data, &le_adv_data);
|
||||
err_code = nfc_ble_simplified_le_oob_msg_encode(&le_adv_data, p_buf, p_len);
|
||||
break;
|
||||
|
||||
case NFC_BLE_PAIR_MSG_BLUETOOTH_EP_SHORT:
|
||||
ep_oob_specific_adv_data_create(p_tk_value, NULL, &ep_adv_data);
|
||||
err_code = nfc_ble_simplified_ep_oob_msg_encode(&ep_adv_data, p_buf, p_len);
|
||||
break;
|
||||
|
||||
case NFC_BLE_PAIR_MSG_FULL:
|
||||
le_oob_specific_adv_data_create(p_tk_value, p_lesc_data, &le_adv_data);
|
||||
ep_oob_specific_adv_data_create(p_tk_value, NULL, &ep_adv_data);
|
||||
err_code = nfc_ble_full_handover_select_msg_encode(&le_adv_data,
|
||||
&ep_adv_data,
|
||||
p_buf,
|
||||
p_len);
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
ret_code_t nfc_ble_pair_msg_updatable_tk_encode(nfc_ble_pair_type_t nfc_ble_pair_type,
|
||||
ble_advdata_tk_value_t * const p_tk_value,
|
||||
ble_gap_lesc_oob_data_t * const p_lesc_data,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len,
|
||||
uint8_t ** pp_tk_group,
|
||||
uint8_t max_group_size)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
|
||||
m_tk_modifier_on = true;
|
||||
nfc_tk_group_modifier_config(pp_tk_group, max_group_size);
|
||||
err_code = nfc_ble_pair_default_msg_encode(nfc_ble_pair_type, p_tk_value,
|
||||
p_lesc_data, p_buf, p_len);
|
||||
m_tk_modifier_on = false;
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
ret_code_t nfc_tk_group_modifier_update(ble_advdata_tk_value_t * p_tk_value)
|
||||
{
|
||||
VERIFY_PARAM_NOT_NULL(m_tk_group.pp_tk_group);
|
||||
for (uint8_t tk_index = 0; tk_index < m_tk_group.tk_num; ++tk_index)
|
||||
{
|
||||
uint8_t * p_tk_payload_data = m_tk_group.pp_tk_group[tk_index];
|
||||
nfc_tk_value_payload_encode(p_tk_value, p_tk_payload_data);
|
||||
}
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
ret_code_t nfc_tk_to_group_add(uint8_t * p_tk_location)
|
||||
{
|
||||
// Feature was disabled.
|
||||
if (m_tk_group.pp_tk_group == NULL)
|
||||
{
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
if (m_tk_group.tk_num < m_tk_group.tk_max_num)
|
||||
{
|
||||
m_tk_group.pp_tk_group[m_tk_group.tk_num++] = p_tk_location;
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
}
|
||||
|
||||
ret_code_t nfc_lesc_pos_set(uint8_t * p_confirm, uint8_t * p_random)
|
||||
{
|
||||
if((p_confirm != NULL) && (p_random != NULL))
|
||||
{
|
||||
m_lesc_pos.confirm = p_confirm;
|
||||
m_lesc_pos.random = p_random;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_NULL;
|
||||
}
|
||||
}
|
||||
|
||||
ret_code_t nfc_lesc_data_update(ble_gap_lesc_oob_data_t * ble_lesc_oob_data)
|
||||
{
|
||||
if(ble_lesc_oob_data != NULL)
|
||||
{
|
||||
if((ble_lesc_oob_data->c != NULL) && (ble_lesc_oob_data->r != NULL))
|
||||
{
|
||||
memcpy(m_lesc_pos.confirm, ble_lesc_oob_data->c, AD_TYPE_CONFIRM_VALUE_DATA_SIZE);
|
||||
memcpy(m_lesc_pos.random, ble_lesc_oob_data->r, AD_TYPE_RANDOM_VALUE_DATA_SIZE);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
return NRF_ERROR_INVALID_STATE;
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_NULL;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,280 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_BLE_PAIR_MSG_H__
|
||||
#define NFC_BLE_PAIR_MSG_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_modules NDEF message modules
|
||||
* @ingroup nfc_api
|
||||
* @brief Implementation of NDEF messages.
|
||||
*
|
||||
* @defgroup nfc_ndef_messages Predefined NDEF messages
|
||||
* @ingroup nfc_modules
|
||||
* @brief Predefined NDEF messages for standard use.
|
||||
*
|
||||
* @defgroup nfc_ble_pair_msg BLE pairing messages
|
||||
* @{
|
||||
* @ingroup nfc_ndef_messages
|
||||
*
|
||||
* @brief Generation of NFC NDEF messages used for BLE pairing.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "ble_advdata.h"
|
||||
#include "sdk_errors.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Types of BLE pairing message.
|
||||
*
|
||||
* Use one of these values to choose the type of NDEF BLE pairing message.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
NFC_BLE_PAIR_MSG_BLUETOOTH_LE_SHORT, ///< Simplified LE OOB message.
|
||||
NFC_BLE_PAIR_MSG_BLUETOOTH_EP_SHORT, ///< Simplified EP OOB message.
|
||||
NFC_BLE_PAIR_MSG_FULL ///< BLE Handover Select Message.
|
||||
} nfc_ble_pair_type_t;
|
||||
|
||||
/** @brief Function for encoding simplified LE OOB messages.
|
||||
*
|
||||
* This function encodes a simplified LE OOB message into a buffer. The payload of the LE OOB record
|
||||
* inside the message can be configured via the advertising data structure.
|
||||
*
|
||||
* This function was implemented partially according to "Bluetooth Secure Simple Pairing Using NFC"
|
||||
* (denotation "NFCForum-AD-BTSSP_1_1" published on 2014-01-09) chapters 3.1, 3.2, 4.3.2,
|
||||
* and according to "Supplement to the Bluetooth Core Specification" (Version 5, adoption date:
|
||||
* Dec 02 2014).
|
||||
*
|
||||
* @note To be able to encode the message, a SoftDevice must be enabled and configured.
|
||||
*
|
||||
* @param[in] p_le_advdata Pointer to the BLE advertising data structure for the LE OOB record.
|
||||
* @param[out] p_buf Pointer to the buffer for the message.
|
||||
* @param[in,out] p_len Size of the available memory for the message as input.
|
||||
* Size of the generated message as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
ret_code_t nfc_ble_simplified_le_oob_msg_encode(ble_advdata_t const * const p_le_advdata,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len);
|
||||
|
||||
/** @brief Function for encoding simplified EP OOB messages.
|
||||
*
|
||||
* This function encodes a simplified EP OOB message into a buffer. The payload of the EP OOB record
|
||||
* inside the message can be configured via the advertising data structure.
|
||||
*
|
||||
* This function was implemented partially according to "Bluetooth Secure Simple Pairing Using NFC"
|
||||
* (denotation "NFCForum-AD-BTSSP_1_1" published on 2014-01-09) chapters 3.1, 3.2, 4.3.1,
|
||||
* and according to "Supplement to the Bluetooth Core Specification" (Version 5, adoption date:
|
||||
* Dec 02 2014).
|
||||
*
|
||||
* @note To be able to encode the message, a SoftDevice must be enabled and configured.
|
||||
*
|
||||
* @param[in] p_ep_advdata Pointer to the BLE advertising data structure for the EP OOB record.
|
||||
* @param[out] p_buf Pointer to the buffer for the message.
|
||||
* @param[in,out] p_len Size of the available memory for the message as input.
|
||||
* Size of the generated message as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
ret_code_t nfc_ble_simplified_ep_oob_msg_encode(ble_advdata_t const * const p_ep_advdata,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len);
|
||||
|
||||
/** @brief Function for encoding BLE Handover Select Messages.
|
||||
*
|
||||
* This function encodes a BLE Handover Select Message into a buffer. The payload of the LE OOB record
|
||||
* and the EP OOB record inside the message can be configured via the advertising data structures.
|
||||
*
|
||||
* This function was implemented partially according to "Bluetooth Secure Simple Pairing Using NFC"
|
||||
* (denotation "NFCForum-AD-BTSSP_1_1" published on 2014-01-09) chapters 3.1, 3.2, 4.1.1
|
||||
* and 4.1.2 (combined), and according to "Supplement to the Bluetooth Core Specification" (Version 5,
|
||||
* adoption date: Dec 02 2014).
|
||||
*
|
||||
* @note To be able to encode the message, a SoftDevice must be enabled and configured.
|
||||
*
|
||||
* @param[in] p_le_advdata Pointer to the BLE advertising data structure for the LE OOB record.
|
||||
* @param[in] p_ep_advdata Pointer to the BLE advertising data structure for the EP OOB record.
|
||||
* @param[out] p_buf Pointer to the buffer for the message.
|
||||
* @param[in,out] p_len Size of the available memory for the message as input.
|
||||
* Size of the generated message as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
ret_code_t nfc_ble_full_handover_select_msg_encode(ble_advdata_t const * const p_le_advdata,
|
||||
ble_advdata_t const * const p_ep_advdata,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len);
|
||||
|
||||
/** @brief Function for encoding any type of BLE pairing messages with default BLE
|
||||
* advertising data structures.
|
||||
*
|
||||
* This function encodes a BLE pairing message into a buffer. The message can be encoded as
|
||||
* one of the three message types (using @ref nfc_ble_simplified_le_oob_msg_encode,
|
||||
* @ref nfc_ble_simplified_ep_oob_msg_encode, or @ref nfc_ble_full_handover_select_msg_encode),
|
||||
* according to the @p nfc_ble_pair_type parameter. LE and EP OOB records use the default
|
||||
* advertising data structure configuration. Only one field ('Security Manager TK') in the BLE
|
||||
* advertising data can be configured for both records by specifying the @p p_tk_value parameter.
|
||||
*
|
||||
* For LE OOB records, the default BLE advertising data structure configuration fills the required
|
||||
* fields 'LE Bluetooth Device Address' and 'LE Role' and the optional fields 'Appearance',
|
||||
* 'Local Name', and 'Flags'.
|
||||
*
|
||||
* For EP OOB records, the default BLE advertising data structure configuration fills the required
|
||||
* field 'Security Manager Out Of Band Flags' and the optional fields 'Appearance',
|
||||
* 'Local Name', and 'Flags'.
|
||||
*
|
||||
* @note To be able to encode the message, a SoftDevice must be enabled and configured.
|
||||
*
|
||||
* @param[in] nfc_ble_pair_type Type of BLE pairing message.
|
||||
* @param[in] p_tk_value Pointer to the authentication Temporary Key (TK). If NULL,
|
||||
* TK value field is not encoded in the NDEF message.
|
||||
* @param[in] p_lesc_data Pointer to the LESC OOB data. If NULL, LESC OOB fields are
|
||||
* not encoded in the NDEF message.
|
||||
* @param[out] p_buf Pointer to the buffer for the message.
|
||||
* @param[in,out] p_len Size of the available memory for the message as input.
|
||||
* Size of the generated message as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
ret_code_t nfc_ble_pair_default_msg_encode(nfc_ble_pair_type_t nfc_ble_pair_type,
|
||||
ble_advdata_tk_value_t * const p_tk_value,
|
||||
ble_gap_lesc_oob_data_t * const p_lesc_data,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len);
|
||||
|
||||
/** @brief Function for encoding any type of BLE pairing messages with default BLE
|
||||
* advertising data structures and with TK modifier feature.
|
||||
*
|
||||
* This function is very similar to @ref nfc_ble_pair_default_msg_encode function, but
|
||||
* additionaly it enables to track TK locations which were encoded in Connection Handover
|
||||
* NDEF message. After using this function, you can update TK value in NDEF by calling
|
||||
* @ref nfc_tk_group_modifier_update.
|
||||
*
|
||||
* @param[in] nfc_ble_pair_type Type of BLE pairing message.
|
||||
* @param[in] p_tk_value Pointer to the authentication Temporary Key (TK). If NULL,
|
||||
* TK value field is not encoded in the NDEF message.
|
||||
* @param[in] p_lesc_data Pointer to the LESC OOB data. If NULL, LESC OOB values are
|
||||
* not encoded in the NDEF message.
|
||||
* @param[out] p_buf Pointer to the buffer for the message.
|
||||
* @param[in,out] p_len Size of the available memory for the message as input.
|
||||
* Size of the generated message as output.
|
||||
* @param[in] pp_tk_group Pointer to array of TK locations that should be modified with
|
||||
* @ref nfc_tk_group_modifier_update function.
|
||||
* @param[in] max_group_size Maximal number of TK locations that can added to \p pp_tk_group.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
ret_code_t nfc_ble_pair_msg_updatable_tk_encode(nfc_ble_pair_type_t nfc_ble_pair_type,
|
||||
ble_advdata_tk_value_t * const p_tk_value,
|
||||
ble_gap_lesc_oob_data_t * const p_lesc_data,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len,
|
||||
uint8_t ** pp_tk_group,
|
||||
uint8_t max_group_size);
|
||||
|
||||
/**@brief Function for updating the Connection Handover NDEF message with new TK value.
|
||||
*
|
||||
* @details This function updates NDEF message with new TK value. This update is applied to all of
|
||||
* TK locations in the Connection Handover NDEF message. This function can only be used
|
||||
* after calling @ref nfc_ble_pair_msg_updatable_tk_encode, which is used to encode
|
||||
* Connection Handover NDEF message.
|
||||
*
|
||||
* @param[in] p_tk_value Pointer to the new TK value. The NDEF message will be updated with this
|
||||
* value.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_NULL If pointer to TK locations was NULL.
|
||||
*/
|
||||
ret_code_t nfc_tk_group_modifier_update(ble_advdata_tk_value_t * p_tk_value);
|
||||
|
||||
/**@brief Function for adding new location of TK value to the location description structure.
|
||||
*
|
||||
* @param[in] p_tk_location New location of TK value in the Connection Handover NDEF message.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful or if buffer used for holding TK
|
||||
* locations is NULL.
|
||||
* @retval NRF_ERROR_NO_MEM If there is no place in the buffer for the new TK value location.
|
||||
*/
|
||||
ret_code_t nfc_tk_to_group_add(uint8_t * p_tk_location);
|
||||
|
||||
/**@brief Function for updating the Connection Handover NDEF message with a new LESC OOB values.
|
||||
*
|
||||
* @details Updates LESC Confirmation and Random Values based on its locations set by the @ref nfc_lesc_pos_set function.
|
||||
*
|
||||
* @param[in] ble_lesc_oob_data Pointer to the new LESC OOB data. The NDEF message will be updated with this data.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_NULL If pointer to the new LESC OOB data is NULL.
|
||||
* @retval NRF_ERROR_INVALID_STATE If pointer to the LESC OOB data location in NDEF message is NULL.
|
||||
*/
|
||||
ret_code_t nfc_lesc_data_update(ble_gap_lesc_oob_data_t * ble_lesc_oob_data);
|
||||
|
||||
/**@brief Function for storing pointers to the LESC OOB data inside NDEF message.
|
||||
*
|
||||
* @details It allows LESC OOB data update without regenerating entire CH NDEF message.
|
||||
*
|
||||
* @param[in] p_confirm Pointer to the LESC Confirmation Value position in the NDEF message.
|
||||
* @param[in] p_random Pointer to the LESC Random Value position in the NDEF message.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the operation was successful.
|
||||
* @retval NRF_ERROR_NULL If either of pointers is set to NULL.
|
||||
*/
|
||||
ret_code_t nfc_lesc_pos_set(uint8_t * p_confirm, uint8_t * p_random);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_BLE_PAIR_MSG_H__
|
||||
@@ -0,0 +1,47 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <stdint.h>
|
||||
|
||||
/* Record Payload Type for Bluetooth Carrier Configuration LE record */
|
||||
const uint8_t le_oob_rec_type_field[] =
|
||||
{
|
||||
'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.',
|
||||
'b', 'l', 'u', 'e', 't', 'o', 'o', 't', 'h', '.', 'l', 'e', '.', 'o', 'o', 'b'
|
||||
};
|
||||
@@ -0,0 +1,43 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <stdint.h>
|
||||
|
||||
/* Record Payload Type for Bluetooth Carrier Configuration LE record */
|
||||
extern const uint8_t le_oob_rec_type_field[32];
|
||||
@@ -0,0 +1,238 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "nfc_ep_oob_rec.h"
|
||||
#include "sdk_errors.h"
|
||||
#include "ble_gap.h"
|
||||
#include "app_util.h"
|
||||
|
||||
/* NFC OOB EP definitions */
|
||||
#define NFC_EP_OOB_REC_GAP_ADDR_LEN BLE_GAP_ADDR_LEN
|
||||
#define NFC_EP_OOB_REC_OOB_DATA_LEN_SIZE 2UL
|
||||
#define NFC_EP_OOB_REC_PAYLOAD_PREFIX_LEN (NFC_EP_OOB_REC_GAP_ADDR_LEN + \
|
||||
NFC_EP_OOB_REC_OOB_DATA_LEN_SIZE)
|
||||
|
||||
/* Record Payload Type for Bluetooth Carrier Configuration EP record */
|
||||
static const uint8_t ep_oob_rec_type_field[] =
|
||||
{
|
||||
'a', 'p', 'p', 'l', 'i', 'c', 'a', 't', 'i', 'o', 'n', '/', 'v', 'n', 'd', '.',
|
||||
'b', 'l', 'u', 'e', 't', 'o', 'o', 't', 'h', '.', 'e', 'p', '.', 'o', 'o', 'b'
|
||||
};
|
||||
|
||||
/**
|
||||
* @brief Function for validating AD structure content for a Bluetooth Carrier Configuration EP record.
|
||||
*
|
||||
* This function validates AD structure content. LE Bluetooth Device Address and LE Role
|
||||
* fields must not be included. Security Manager OOB Flags structure is required.
|
||||
*
|
||||
* @param[in] p_ble_advdata Pointer to the description of the payload.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the validation was successful.
|
||||
* @retval NRF_ERROR_INVALID_PARAM Otherwise.
|
||||
*/
|
||||
static ret_code_t nfc_ep_oob_adv_data_check(ble_advdata_t const * const p_ble_advdata)
|
||||
{
|
||||
if ((true == p_ble_advdata->include_ble_device_addr) ||
|
||||
(BLE_ADVDATA_ROLE_NOT_PRESENT != p_ble_advdata->le_role) ||
|
||||
(NULL == p_ble_advdata->p_sec_mgr_oob_flags))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* If Flags field in AD structure is present, the BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED flag
|
||||
must be set. */
|
||||
if ((0 != p_ble_advdata->flags) &&
|
||||
((p_ble_advdata->flags & BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) == 0))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for encoding device address to Bluetooth Carrier Configuration EP record.
|
||||
*
|
||||
* This fuction is used to encode device address to Bluetooth Carrier Configuration EP record.
|
||||
*
|
||||
* @param[in] p_encoded_data Pointer to the buffer where encoded data will be returned.
|
||||
* @param[in] max_len Available memory in the buffer.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the encoding was successful.
|
||||
* @retval NRF_ERROR_NO_MEM If available memory was not enough.
|
||||
* @retval NRF_ERROR_xxx If any other error occured.
|
||||
*/
|
||||
static ret_code_t nfc_ep_oob_bluetooth_device_address_encode(uint8_t * const p_encoded_data,
|
||||
uint16_t max_len)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
ble_gap_addr_t device_address;
|
||||
|
||||
memset(&device_address, 0x00, sizeof(device_address));
|
||||
|
||||
if (NFC_EP_OOB_REC_GAP_ADDR_LEN > max_len)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
/* Get BLE address */
|
||||
#if (NRF_SD_BLE_API_VERSION == 2)
|
||||
err_code = sd_ble_gap_address_get(&device_address);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
#endif
|
||||
|
||||
#if (NRF_SD_BLE_API_VERSION == 3)
|
||||
err_code = sd_ble_gap_addr_get(&device_address);
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Encode Bluetooth EP device address */
|
||||
memcpy(p_encoded_data, device_address.addr, NFC_EP_OOB_REC_GAP_ADDR_LEN);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for constructing the payload for a Bluetooth Carrier Configuration EP record.
|
||||
*
|
||||
* This function encodes the record payload according to the BLE AD structure. It implements
|
||||
* an API compatible with @ref p_payload_constructor_t.
|
||||
*
|
||||
* @param[in] p_ble_advdata Pointer to the description of the payload.
|
||||
* @param[out] p_buff Pointer to payload destination. If NULL, function will
|
||||
* calculate the expected size of the record payload.
|
||||
*
|
||||
* @param[in,out] p_len Size of available memory to write as input. Size of generated
|
||||
* payload as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the record payload was encoded successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If available memory was not enough for record payload to be encoded.
|
||||
* @retval Other If any other error occurred during record payload encoding.
|
||||
*/
|
||||
static ret_code_t nfc_ep_oob_payload_constructor(ble_advdata_t * p_ble_advdata,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
uint8_t * p_ad_data = NULL;
|
||||
uint16_t payload_len, ad_data_len;
|
||||
|
||||
/* Check correctness of the configuration structure */
|
||||
err_code = nfc_ep_oob_adv_data_check(p_ble_advdata);
|
||||
if (NRF_SUCCESS != err_code)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
if (p_buff != NULL)
|
||||
{
|
||||
/* Validate if there is enough memory for OOB payload length field and BLE device address */
|
||||
if (NFC_EP_OOB_REC_PAYLOAD_PREFIX_LEN > *p_len)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
/* Set proper memory offset in payload buffer for AD structure and count available memory.
|
||||
* Bluetooth EP device address and OOB payload length field must be inserted before the AD payload */
|
||||
p_ad_data = (uint8_t *) (p_buff + NFC_EP_OOB_REC_PAYLOAD_PREFIX_LEN);
|
||||
ad_data_len = *p_len - NFC_EP_OOB_REC_PAYLOAD_PREFIX_LEN;
|
||||
if ( *p_len - NFC_EP_OOB_REC_PAYLOAD_PREFIX_LEN > UINT16_MAX )
|
||||
{
|
||||
ad_data_len = UINT16_MAX;
|
||||
}
|
||||
}
|
||||
|
||||
/* Encode AD structures into NFC record payload */
|
||||
err_code = nfc_ble_oob_adv_data_encode(p_ble_advdata, p_ad_data, &ad_data_len);
|
||||
if (NRF_SUCCESS != err_code)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/* Now as the final payload length is known OOB payload length field, and Bluetooth device
|
||||
* address can be encoded */
|
||||
payload_len = ad_data_len + NFC_EP_OOB_REC_PAYLOAD_PREFIX_LEN;
|
||||
if (p_buff != NULL)
|
||||
{
|
||||
p_buff += uint16_encode(payload_len, p_buff);
|
||||
err_code = nfc_ep_oob_bluetooth_device_address_encode(p_buff, p_ad_data - p_buff);
|
||||
if (NRF_SUCCESS != err_code)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
}
|
||||
|
||||
/* Update total payload length */
|
||||
*p_len = payload_len;
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
nfc_ndef_record_desc_t * nfc_ep_oob_rec_declare(uint8_t rec_payload_id,
|
||||
ble_advdata_t const * const p_ble_advdata)
|
||||
{
|
||||
static uint8_t payload_id = 0;
|
||||
|
||||
NFC_NDEF_GENERIC_RECORD_DESC_DEF( nfc_ep_oob_rec,
|
||||
TNF_MEDIA_TYPE,
|
||||
&payload_id, // memory for possible ID value
|
||||
0, // no ID by default
|
||||
(ep_oob_rec_type_field),
|
||||
sizeof(ep_oob_rec_type_field),
|
||||
nfc_ep_oob_payload_constructor,
|
||||
NULL);
|
||||
|
||||
nfc_ndef_record_desc_t * nfc_ep_oob_rec = &NFC_NDEF_GENERIC_RECORD_DESC( nfc_ep_oob_rec);
|
||||
|
||||
/* Update record descriptor */
|
||||
nfc_ep_oob_rec->p_payload_descriptor = (void *) p_ble_advdata;
|
||||
|
||||
/* Handle record ID configuration */
|
||||
payload_id = rec_payload_id;
|
||||
nfc_ep_oob_rec->id_length = (rec_payload_id != 0) ? 1 : 0;
|
||||
|
||||
return nfc_ep_oob_rec;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_EP_OOB_REC_H__
|
||||
#define NFC_EP_OOB_REC_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_ep_oob_rec EP OOB records
|
||||
* @{
|
||||
* @ingroup nfc_ble_pair_msg
|
||||
*
|
||||
* @brief Generation of NFC NDEF EP OOB records for NDEF messages.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_record.h"
|
||||
#include "nfc_ble_oob_advdata.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief Function for generating a description of an NFC NDEF Bluetooth Carrier Configuration EP record.
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF record description
|
||||
* for a Bluetooth Carrier Configuration EP record.
|
||||
*
|
||||
* @note The record payload data (@p p_ble_advdata) should be declared as static. If it is
|
||||
* declared as automatic, the NDEF message encoding (see @ref nfc_ble_simplified_ep_oob_msg_encode)
|
||||
* must be done in the same variable scope.
|
||||
*
|
||||
* @param[in] rec_payload_id NDEF record header Payload ID field (limited to one byte).
|
||||
* If 0, no ID is present in the record description.
|
||||
* @param[in] p_ble_advdata Pointer to the encoded BLE advertising data structure. This
|
||||
* data is used to create the record payload.
|
||||
*
|
||||
* @return Pointer to the description of the record.
|
||||
*/
|
||||
nfc_ndef_record_desc_t * nfc_ep_oob_rec_declare(uint8_t rec_payload_id,
|
||||
ble_advdata_t const * const p_ble_advdata);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_EP_OOB_REC_H__
|
||||
103
COMPONENTS/nfc/ndef/connection_handover/hs_rec/nfc_hs_rec.c
Normal file
103
COMPONENTS/nfc/ndef/connection_handover/hs_rec/nfc_hs_rec.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "nfc_hs_rec.h"
|
||||
#include "nfc_ac_rec.h"
|
||||
#include "nrf_error.h"
|
||||
|
||||
#define HS_REC_VERSION_SIZE 1
|
||||
|
||||
const uint8_t nfc_hs_rec_type_field[] = {'H', 's'}; ///< Handover Select record type.
|
||||
|
||||
|
||||
ret_code_t nfc_hs_rec_payload_constructor(nfc_hs_rec_payload_desc_t * p_nfc_hs_rec_payload_desc,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
|
||||
if (p_buff != NULL)
|
||||
{
|
||||
// There must be at least 1 free byte in buffer for version byte.
|
||||
if (*p_len < HS_REC_VERSION_SIZE)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
// Major/minor version byte.
|
||||
*p_buff = ( (p_nfc_hs_rec_payload_desc->major_version << 4) & 0xF0) |
|
||||
( p_nfc_hs_rec_payload_desc->minor_version & 0x0F);
|
||||
p_buff += HS_REC_VERSION_SIZE;
|
||||
|
||||
// Decrement remaining buffer size.
|
||||
*p_len -= HS_REC_VERSION_SIZE;
|
||||
}
|
||||
|
||||
// Encode local records encapsulated in a message.
|
||||
err_code = nfc_ndef_msg_encode(p_nfc_hs_rec_payload_desc->p_local_records, p_buff, p_len);
|
||||
if (err_code!= NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
// Add version byte to the total record size.
|
||||
*p_len += HS_REC_VERSION_SIZE;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void nfc_hs_rec_local_record_clear(nfc_ndef_record_desc_t * p_hs_rec)
|
||||
{
|
||||
nfc_hs_rec_payload_desc_t* p_hs_payload =
|
||||
(nfc_hs_rec_payload_desc_t*)p_hs_rec->p_payload_descriptor;
|
||||
|
||||
nfc_ndef_msg_clear(p_hs_payload->p_local_records);
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_hs_rec_local_record_add(nfc_ndef_record_desc_t * p_hs_rec,
|
||||
nfc_ndef_record_desc_t * p_local_rec)
|
||||
{
|
||||
nfc_hs_rec_payload_desc_t* p_hs_payload =
|
||||
(nfc_hs_rec_payload_desc_t*)p_hs_rec->p_payload_descriptor;
|
||||
|
||||
return nfc_ndef_msg_record_add(p_hs_payload->p_local_records, p_local_rec);
|
||||
}
|
||||
162
COMPONENTS/nfc/ndef/connection_handover/hs_rec/nfc_hs_rec.h
Normal file
162
COMPONENTS/nfc/ndef/connection_handover/hs_rec/nfc_hs_rec.h
Normal file
@@ -0,0 +1,162 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_HS_REC_H__
|
||||
#define NFC_HS_REC_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_hs_rec Hs (Handover Select) records
|
||||
* @{
|
||||
* @ingroup nfc_ble_pair_msg
|
||||
*
|
||||
* @brief Generation of NFC NDEF Handover Select records for NDEF messages.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_record.h"
|
||||
#include "nfc_ndef_msg.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Handover Select record payload descriptor.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t major_version; ///< Major version number of the supported Connection Handover specification.
|
||||
uint8_t minor_version; ///< Minor version number of the supported Connection Handover specification.
|
||||
nfc_ndef_msg_desc_t * p_local_records; ///< Pointer to a message encapsulating local records.
|
||||
} nfc_hs_rec_payload_desc_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief Constructor for an NFC NDEF Handover Select record payload.
|
||||
*
|
||||
* This function encodes the payload of a Handover Select record as specified in the Connection
|
||||
* Handover standard. It implements an API compatible with @ref p_payload_constructor_t.
|
||||
*/
|
||||
|
||||
ret_code_t nfc_hs_rec_payload_constructor(nfc_hs_rec_payload_desc_t * p_nfc_hs_rec_payload_desc,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len);
|
||||
|
||||
/**
|
||||
* @brief An external reference to the type field of the Handover Select record, defined in the
|
||||
* file @c nfc_hs_rec.c. It is used in the @ref NFC_NDEF_HS_RECORD_DESC_DEF macro.
|
||||
*/
|
||||
extern const uint8_t nfc_hs_rec_type_field[];
|
||||
|
||||
/**
|
||||
* @brief Size of the type field of the Handover Select record, defined in the
|
||||
* file @c nfc_hs_rec.c. It is used in the @ref NFC_NDEF_HS_RECORD_DESC_DEF macro.
|
||||
*/
|
||||
#define NFC_HS_REC_TYPE_LENGTH 2
|
||||
|
||||
/**
|
||||
* @brief Macro for creating and initializing an NFC NDEF record descriptor for a Handover Select record.
|
||||
*
|
||||
* This macro creates and initializes a static instance of type @ref nfc_ndef_record_desc_t and
|
||||
* a static instance of type @ref nfc_hs_rec_payload_desc_t, which together constitute an instance of a Handover Select record.
|
||||
*
|
||||
* Use the macro @ref NFC_NDEF_HS_RECORD_DESC to access the NDEF Handover Select record descriptor instance.
|
||||
*
|
||||
* @param[in] NAME Name of the created record descriptor instance.
|
||||
* @param[in] MAJOR_VERSION Major version number of the supported Connection Handover specification.
|
||||
* @param[in] MINOR_VERSION Minor version number of the supported Connection Handover specification.
|
||||
* @param[in] MAX_RECORDS Maximum number of local records (ac records plus optional err record).
|
||||
*/
|
||||
#define NFC_NDEF_HS_RECORD_DESC_DEF(NAME, \
|
||||
MAJOR_VERSION, \
|
||||
MINOR_VERSION, \
|
||||
MAX_RECORDS) \
|
||||
NFC_NDEF_MSG_DEF(NAME, MAX_RECORDS); \
|
||||
static nfc_hs_rec_payload_desc_t NAME##_nfc_hs_rec_payload_desc = \
|
||||
{ \
|
||||
.major_version = MAJOR_VERSION, \
|
||||
.minor_version = MINOR_VERSION, \
|
||||
.p_local_records = &NFC_NDEF_MSG(NAME) \
|
||||
}; \
|
||||
NFC_NDEF_GENERIC_RECORD_DESC_DEF(NAME, \
|
||||
TNF_WELL_KNOWN, \
|
||||
0, \
|
||||
0, \
|
||||
nfc_hs_rec_type_field , \
|
||||
NFC_HS_REC_TYPE_LENGTH, \
|
||||
nfc_hs_rec_payload_constructor, \
|
||||
&(NAME##_nfc_hs_rec_payload_desc))
|
||||
|
||||
/**
|
||||
* @brief Macro for accessing the NFC NDEF Handover Select record descriptor
|
||||
* instance that was created with @ref NFC_NDEF_HS_RECORD_DESC_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_HS_RECORD_DESC(NAME) NFC_NDEF_GENERIC_RECORD_DESC(NAME)
|
||||
|
||||
/**
|
||||
* @brief Function for clearing local records in the NFC NDEF Handover Select record.
|
||||
*
|
||||
* This function clears local records from the Handover Select record.
|
||||
*
|
||||
* @param[in, out] p_hs_rec Pointer to the Handover Select record descriptor.
|
||||
*/
|
||||
void nfc_hs_rec_local_record_clear(nfc_ndef_record_desc_t * p_hs_rec);
|
||||
|
||||
/**
|
||||
* @brief Function for adding a local record to an NFC NDEF Handover Select record.
|
||||
*
|
||||
* @param[in, out] p_hs_rec Pointer to a Handover Select record.
|
||||
* @param[in] p_local_rec Pointer to a local record to add.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the local record was added successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the Handover Select record already contains the maximum number of local records.
|
||||
*/
|
||||
ret_code_t nfc_hs_rec_local_record_add(nfc_ndef_record_desc_t * p_hs_rec,
|
||||
nfc_ndef_record_desc_t * p_local_rec);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_HS_REC_H__
|
||||
@@ -0,0 +1,144 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "nfc_le_oob_rec.h"
|
||||
#include "sdk_errors.h"
|
||||
#include "ble_gap.h"
|
||||
#include "nfc_ble_pair_common.h"
|
||||
|
||||
/**
|
||||
* @brief Function for validating AD structure content for a Bluetooth Carrier Configuration LE record.
|
||||
*
|
||||
* This function validates AD structure content. LE Bluetooth Device Address and LE Role
|
||||
* fields are required. Security Manager Out Of Band Flags structure must not be included.
|
||||
*
|
||||
* @param[in] p_ble_advdata Pointer to the description of the payload.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the validation was successful.
|
||||
* @retval NRF_ERROR_INVALID_PARAM Otherwise.
|
||||
*/
|
||||
static ret_code_t nfc_le_oob_adv_data_check(ble_advdata_t const * const p_ble_advdata)
|
||||
{
|
||||
if ((false == p_ble_advdata->include_ble_device_addr) ||
|
||||
(BLE_ADVDATA_ROLE_NOT_PRESENT == p_ble_advdata->le_role) ||
|
||||
(NULL != p_ble_advdata->p_sec_mgr_oob_flags))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* If Flags field in AD structure is present, the BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED flag
|
||||
must be set. */
|
||||
if ((0 != p_ble_advdata->flags) &&
|
||||
((p_ble_advdata->flags & BLE_GAP_ADV_FLAG_BR_EDR_NOT_SUPPORTED) == 0))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Function for constructing the payload for a Bluetooth Carrier Configuration LE record.
|
||||
*
|
||||
* This function encodes the record payload according to the BLE AD structure. It implements
|
||||
* an API compatible with @ref p_payload_constructor_t
|
||||
*
|
||||
* @param[in] p_ble_advdata Pointer to the description of the payload.
|
||||
* @param[out] p_buff Pointer to payload destination. If NULL, function will
|
||||
* calculate the expected size of the record payload.
|
||||
*
|
||||
* @param[in,out] p_len Size of available memory to write as input. Size of generated
|
||||
* payload as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the record payload was encoded successfully.
|
||||
* @retval Other If the record payload encoding failed.
|
||||
*/
|
||||
static ret_code_t nfc_le_oob_payload_constructor(ble_advdata_t * p_ble_advdata,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
ret_code_t err_code = NRF_SUCCESS;
|
||||
|
||||
/* Check correctness of the configuration structure */
|
||||
err_code = nfc_le_oob_adv_data_check(p_ble_advdata);
|
||||
if (NRF_SUCCESS != err_code)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/* Encode AD structures into NFC record payload */
|
||||
uint16_t buff_len = *p_len;
|
||||
if (*p_len > UINT16_MAX)
|
||||
{
|
||||
buff_len = UINT16_MAX;
|
||||
}
|
||||
err_code = nfc_ble_oob_adv_data_encode(p_ble_advdata, p_buff, &buff_len);
|
||||
|
||||
/* Update total payload length */
|
||||
*p_len = (uint32_t) buff_len;
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
nfc_ndef_record_desc_t * nfc_le_oob_rec_declare(uint8_t rec_payload_id,
|
||||
ble_advdata_t const * const p_ble_advdata)
|
||||
{
|
||||
static uint8_t payload_id = 0;
|
||||
|
||||
NFC_NDEF_GENERIC_RECORD_DESC_DEF( nfc_le_oob_rec,
|
||||
TNF_MEDIA_TYPE,
|
||||
&payload_id, // memory for possible ID value
|
||||
0, // no ID by default
|
||||
(le_oob_rec_type_field),
|
||||
sizeof(le_oob_rec_type_field),
|
||||
nfc_le_oob_payload_constructor,
|
||||
NULL);
|
||||
|
||||
nfc_ndef_record_desc_t * nfc_le_oob_rec = &NFC_NDEF_GENERIC_RECORD_DESC( nfc_le_oob_rec);
|
||||
|
||||
/* Update record descriptor */
|
||||
nfc_le_oob_rec->p_payload_descriptor = (void *) p_ble_advdata;
|
||||
|
||||
/* Handle record ID configuration */
|
||||
payload_id = rec_payload_id;
|
||||
nfc_le_oob_rec->id_length = (rec_payload_id != 0) ? 1 : 0;
|
||||
|
||||
return nfc_le_oob_rec;
|
||||
}
|
||||
@@ -0,0 +1,87 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_LE_OOB_REC_H__
|
||||
#define NFC_LE_OOB_REC_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_le_oob_rec LE OOB records
|
||||
* @{
|
||||
* @ingroup nfc_ble_pair_msg
|
||||
*
|
||||
* @brief Generation of NFC NDEF LE OOB records for NDEF messages.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_record.h"
|
||||
#include "nfc_ble_oob_advdata.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief Function for generating a description of an NFC NDEF Bluetooth Carrier Configuration LE Record.
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF record description
|
||||
* for a Bluetooth Carrier Configuration LE record.
|
||||
*
|
||||
* @note The record payload data (@p p_ble_advdata) should be declared as static. If it is
|
||||
* declared as automatic, the NDEF message encoding (see @ref nfc_ble_simplified_le_oob_msg_encode)
|
||||
* must be done in the same variable scope.
|
||||
*
|
||||
* @param[in] rec_payload_id NDEF record header Payload ID field (Limited to one byte).
|
||||
* If 0, no ID is present in the record description.
|
||||
* @param[in] p_ble_advdata Pointer to the encoded BLE advertising data structure. This
|
||||
* data is used to create the record payload.
|
||||
*
|
||||
* @return Pointer to the description of the record.
|
||||
*/
|
||||
nfc_ndef_record_desc_t * nfc_le_oob_rec_declare(uint8_t rec_payload_id,
|
||||
ble_advdata_t const * const p_ble_advdata);
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_LE_OOB_REC_H__
|
||||
186
COMPONENTS/nfc/ndef/generic/message/nfc_ndef_msg.c
Normal file
186
COMPONENTS/nfc/ndef/generic/message/nfc_ndef_msg.c
Normal file
@@ -0,0 +1,186 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "sdk_config.h"
|
||||
#if NFC_NDEF_MSG_ENABLED
|
||||
|
||||
#include "app_util.h"
|
||||
#include "nfc_ndef_msg.h"
|
||||
#include "nordic_common.h"
|
||||
#include "nrf.h"
|
||||
|
||||
#define TYPE_4_TAG 4U ///< Type 4 Tag identifier.
|
||||
#define NLEN_FIELD_SIZE 2U ///< Size of NLEN field, used to encode NDEF message for Type 4 Tag.
|
||||
|
||||
/**
|
||||
* @brief Resolve the value of record location flags of the NFC NDEF record within an NFC NDEF message.
|
||||
*/
|
||||
__STATIC_INLINE nfc_ndef_record_location_t record_location_get(uint32_t index,
|
||||
uint32_t record_count)
|
||||
{
|
||||
nfc_ndef_record_location_t record_location;
|
||||
|
||||
if (index == 0)
|
||||
{
|
||||
if (record_count == 1)
|
||||
{
|
||||
record_location = NDEF_LONE_RECORD;
|
||||
}
|
||||
else
|
||||
{
|
||||
record_location = NDEF_FIRST_RECORD;
|
||||
}
|
||||
}
|
||||
else if (record_count == index + 1)
|
||||
{
|
||||
record_location = NDEF_LAST_RECORD;
|
||||
}
|
||||
else
|
||||
{
|
||||
record_location = NDEF_MIDDLE_RECORD;
|
||||
}
|
||||
|
||||
return record_location;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_ndef_msg_encode(nfc_ndef_msg_desc_t const * p_ndef_msg_desc,
|
||||
uint8_t * p_msg_buffer,
|
||||
uint32_t * const p_msg_len)
|
||||
{
|
||||
nfc_ndef_record_location_t record_location;
|
||||
uint32_t temp_len;
|
||||
uint32_t i;
|
||||
uint32_t err_code;
|
||||
|
||||
uint32_t sum_of_len = 0;
|
||||
|
||||
if ((p_ndef_msg_desc == NULL) || p_msg_len == NULL)
|
||||
{
|
||||
return NRF_ERROR_NULL;
|
||||
}
|
||||
|
||||
nfc_ndef_record_desc_t * * pp_record_rec_desc = p_ndef_msg_desc->pp_record;
|
||||
|
||||
if (p_ndef_msg_desc->pp_record == NULL)
|
||||
{
|
||||
return NRF_ERROR_NULL;
|
||||
}
|
||||
|
||||
#if NFC_NDEF_MSG_TAG_TYPE == TYPE_4_TAG
|
||||
uint8_t * p_root_msg_buffer = p_msg_buffer;
|
||||
|
||||
if (p_msg_buffer != NULL)
|
||||
{
|
||||
if (*p_msg_len < NLEN_FIELD_SIZE)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
p_msg_buffer += NLEN_FIELD_SIZE;
|
||||
}
|
||||
sum_of_len += NLEN_FIELD_SIZE;
|
||||
#endif
|
||||
|
||||
for (i = 0; i < p_ndef_msg_desc->record_count; i++)
|
||||
{
|
||||
record_location = record_location_get(i, p_ndef_msg_desc->record_count);
|
||||
|
||||
temp_len = *p_msg_len - sum_of_len;
|
||||
|
||||
err_code = nfc_ndef_record_encode(*pp_record_rec_desc,
|
||||
record_location,
|
||||
p_msg_buffer,
|
||||
&temp_len);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
sum_of_len += temp_len;
|
||||
if (p_msg_buffer != NULL)
|
||||
{
|
||||
p_msg_buffer += temp_len;
|
||||
}
|
||||
|
||||
/* next record */
|
||||
pp_record_rec_desc++;
|
||||
}
|
||||
|
||||
#if NFC_NDEF_MSG_TAG_TYPE == TYPE_4_TAG
|
||||
if (p_msg_buffer != NULL)
|
||||
{
|
||||
if (sum_of_len - NLEN_FIELD_SIZE > UINT16_MAX)
|
||||
{
|
||||
return NRF_ERROR_NOT_SUPPORTED;
|
||||
}
|
||||
|
||||
UNUSED_RETURN_VALUE(uint16_big_encode(sum_of_len - NLEN_FIELD_SIZE, p_root_msg_buffer));
|
||||
}
|
||||
#endif
|
||||
|
||||
*p_msg_len = sum_of_len;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
void nfc_ndef_msg_clear(nfc_ndef_msg_desc_t * p_msg)
|
||||
{
|
||||
p_msg->record_count = 0;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_ndef_msg_record_add(nfc_ndef_msg_desc_t * const p_msg,
|
||||
nfc_ndef_record_desc_t * const p_record)
|
||||
{
|
||||
if (p_msg->record_count >= p_msg->max_record_count)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
p_msg->pp_record[p_msg->record_count] = p_record;
|
||||
p_msg->record_count++;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
#endif // NFC_NDEF_MSG_ENABLED
|
||||
189
COMPONENTS/nfc/ndef/generic/message/nfc_ndef_msg.h
Normal file
189
COMPONENTS/nfc/ndef/generic/message/nfc_ndef_msg.h
Normal file
@@ -0,0 +1,189 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_NDEF_MSG_H__
|
||||
#define NFC_NDEF_MSG_H__
|
||||
|
||||
#include "nfc_ndef_record.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_ndef_msg Custom NDEF messages
|
||||
* @{
|
||||
* @ingroup nfc_modules
|
||||
*
|
||||
* @brief Generation of NFC NDEF messages for the NFC tag.
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @brief NDEF message descriptor.
|
||||
*/
|
||||
typedef struct {
|
||||
nfc_ndef_record_desc_t ** pp_record; ///< Pointer to an array of pointers to NDEF record descriptors.
|
||||
uint32_t max_record_count; ///< Number of elements in the allocated pp_record array, which defines the maximum number of records within the NDEF message.
|
||||
uint32_t record_count; ///< Number of records in the NDEF message.
|
||||
} nfc_ndef_msg_desc_t;
|
||||
|
||||
/**
|
||||
* @brief Function for encoding an NDEF message.
|
||||
*
|
||||
* This function encodes an NDEF message according to the provided message descriptor.
|
||||
*
|
||||
* @note The way of encoding an NDEF message may vary depending on tag's platform, which
|
||||
* can be chosen with @ref NFC_NDEF_MSG_TAG_TYPE in @c sdk_config.h.
|
||||
*
|
||||
* @param[in] p_ndef_msg_desc Pointer to the message descriptor.
|
||||
* @param[out] p_msg_buffer Pointer to the message destination. If NULL, function will
|
||||
* calculate the expected size of the message.
|
||||
* @param[in,out] p_msg_len Size of the available memory for the message as input. Size of
|
||||
* the generated message as output.
|
||||
*
|
||||
* @return Return value from @ref nfc_ndef_record_encode.
|
||||
*/
|
||||
ret_code_t nfc_ndef_msg_encode(nfc_ndef_msg_desc_t const * p_ndef_msg_desc,
|
||||
uint8_t * p_msg_buffer,
|
||||
uint32_t * const p_msg_len);
|
||||
|
||||
/**
|
||||
* @brief Function for clearing an NDEF message.
|
||||
*
|
||||
* This function clears an NDEF message descriptor, thus empties the NDEF message.
|
||||
*
|
||||
* @param[in,out] p_msg Pointer to the message descriptor.
|
||||
*/
|
||||
void nfc_ndef_msg_clear( nfc_ndef_msg_desc_t * p_msg);
|
||||
|
||||
/**
|
||||
* @brief Function for adding a record to an NDEF message.
|
||||
*
|
||||
* @param[in] p_record Pointer to the record descriptor.
|
||||
* @param[in,out] p_msg Pointer to the message descriptor.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the record was added successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the message already contains the maximum number of records and the operation is not allowed.
|
||||
*/
|
||||
ret_code_t nfc_ndef_msg_record_add(nfc_ndef_msg_desc_t * const p_msg,
|
||||
nfc_ndef_record_desc_t * const p_record);
|
||||
|
||||
|
||||
/**@brief Macro for creating and initializing an NFC NDEF message descriptor.
|
||||
*
|
||||
* This macro creates and initializes a static instance of type @ref nfc_ndef_msg_desc_t
|
||||
* and a static array of pointers to record descriptors (@ref nfc_ndef_record_desc_t) used
|
||||
* by the message.
|
||||
*
|
||||
* Use the macro @ref NFC_NDEF_MSG to access the NDEF message descriptor instance.
|
||||
*
|
||||
* @param[in] NAME Name of the related instance.
|
||||
* @param[in] MAX_RECORD_CNT Maximal count of records in the message.
|
||||
*/
|
||||
#define NFC_NDEF_MSG_DEF(NAME, MAX_RECORD_CNT) \
|
||||
static nfc_ndef_record_desc_t * NAME##_nfc_ndef_p_record_desc_array[MAX_RECORD_CNT]; \
|
||||
static nfc_ndef_msg_desc_t NAME##_nfc_ndef_msg_desc = \
|
||||
{ \
|
||||
.pp_record = NAME##_nfc_ndef_p_record_desc_array, \
|
||||
.record_count = 0, \
|
||||
.max_record_count = MAX_RECORD_CNT \
|
||||
}
|
||||
|
||||
/** @brief Macro for accessing the NFC NDEF message descriptor instance
|
||||
* that you created with @ref NFC_NDEF_MSG_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_MSG(NAME) (NAME##_nfc_ndef_msg_desc)
|
||||
|
||||
/**
|
||||
* @brief Macro for creating and initializing an NFC NDEF record descriptor with an encapsulated NDEF message.
|
||||
|
||||
* This macro creates and initializes a static instance of type
|
||||
* @ref nfc_ndef_record_desc_t that contains an encapsulated NDEF message as
|
||||
* payload. @ref nfc_ndef_msg_encode is used as payload constructor to encode
|
||||
* the message. The encoded message is then used as payload for the record.
|
||||
*
|
||||
* Use the macro @ref NFC_NDEF_NESTED_NDEF_MSG_RECORD to access the NDEF record descriptor instance.
|
||||
*
|
||||
* @param[in] NAME Name of the created record descriptor instance.
|
||||
* @param[in] TNF Type Name Format (TNF) value for the record.
|
||||
* @param[in] P_ID Pointer to the ID string.
|
||||
* @param[in] ID_LEN Length of the ID string.
|
||||
* @param[in] P_TYPE Pointer to the type string.
|
||||
* @param[in] TYPE_LEN Length of the type string.
|
||||
* @param[in] P_NESTED_MESSAGE Pointer to the message descriptor to encapsulate
|
||||
* as the record's payload.
|
||||
*/
|
||||
#define NFC_NDEF_NESTED_NDEF_MSG_RECORD_DEF( NAME, \
|
||||
TNF, \
|
||||
P_ID, \
|
||||
ID_LEN, \
|
||||
P_TYPE, \
|
||||
TYPE_LEN, \
|
||||
P_NESTED_MESSAGE ) \
|
||||
static nfc_ndef_record_desc_t NAME##_ndef_record_nested_desc = \
|
||||
{ \
|
||||
.tnf = TNF, \
|
||||
\
|
||||
.id_length = ID_LEN, \
|
||||
.p_id = P_ID, \
|
||||
\
|
||||
.type_length = TYPE_LEN, \
|
||||
.p_type = P_TYPE, \
|
||||
\
|
||||
.payload_constructor = (p_payload_constructor_t)(nfc_ndef_msg_encode), \
|
||||
.p_payload_descriptor = (void*) (P_NESTED_MESSAGE) \
|
||||
}
|
||||
|
||||
/** @brief Macro for accessing the NFC NDEF record descriptor instance
|
||||
* that you created with @ref NFC_NDEF_NESTED_NDEF_MSG_RECORD_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_NESTED_NDEF_MSG_RECORD(NAME) (NAME##_ndef_record_nested_desc)
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
||||
|
||||
188
COMPONENTS/nfc/ndef/generic/record/nfc_ndef_record.c
Normal file
188
COMPONENTS/nfc/ndef/generic/record/nfc_ndef_record.c
Normal file
@@ -0,0 +1,188 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <string.h>
|
||||
#include "nfc_ndef_record.h"
|
||||
#include "app_util.h"
|
||||
#include "nrf.h"
|
||||
|
||||
|
||||
/* Sum of sizes of fields: TNF-flags, Type Length, Payload Length in long NDEF record. */
|
||||
#define NDEF_RECORD_BASE_LONG_SIZE (2 + NDEF_RECORD_PAYLOAD_LEN_LONG_SIZE)
|
||||
|
||||
__STATIC_INLINE uint32_t record_header_size_calc(nfc_ndef_record_desc_t const * p_ndef_record_desc)
|
||||
{
|
||||
uint32_t len = NDEF_RECORD_BASE_LONG_SIZE;
|
||||
|
||||
len += p_ndef_record_desc->id_length + p_ndef_record_desc->type_length;
|
||||
|
||||
if (p_ndef_record_desc->id_length > 0)
|
||||
{
|
||||
len++;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_ndef_record_encode(nfc_ndef_record_desc_t const * p_ndef_record_desc,
|
||||
nfc_ndef_record_location_t record_location,
|
||||
uint8_t * p_record_buffer,
|
||||
uint32_t * p_record_len)
|
||||
{
|
||||
uint8_t * p_flags; // use as pointer to TNF + flags field
|
||||
uint8_t * p_payload_len = NULL; // use as pointer to payload length field
|
||||
uint32_t record_payload_len;
|
||||
|
||||
if (p_ndef_record_desc == NULL)
|
||||
{
|
||||
return NRF_ERROR_NULL;
|
||||
}
|
||||
|
||||
// count record length without payload
|
||||
uint32_t record_header_len = record_header_size_calc(p_ndef_record_desc);
|
||||
uint32_t err_code = NRF_SUCCESS;
|
||||
|
||||
if (p_record_buffer != NULL)
|
||||
{
|
||||
/* verify location range */
|
||||
if ((record_location & (~NDEF_RECORD_LOCATION_MASK)) != 0x00)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
/* verify if there is enough available memory */
|
||||
if (record_header_len > *p_record_len)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
p_flags = p_record_buffer;
|
||||
p_record_buffer++;
|
||||
|
||||
// set location bits and clear other bits in 1st byte.
|
||||
*p_flags = record_location;
|
||||
|
||||
*p_flags |= p_ndef_record_desc->tnf;
|
||||
|
||||
/* TYPE LENGTH */
|
||||
*(p_record_buffer++) = p_ndef_record_desc->type_length;
|
||||
|
||||
// use always long record and remember payload len field memory offset.
|
||||
p_payload_len = p_record_buffer;
|
||||
p_record_buffer += NDEF_RECORD_PAYLOAD_LEN_LONG_SIZE;
|
||||
|
||||
/* ID LENGTH - option */
|
||||
if (p_ndef_record_desc->id_length > 0)
|
||||
{
|
||||
*(p_record_buffer++) = p_ndef_record_desc->id_length;
|
||||
|
||||
/* IL flag */
|
||||
*p_flags |= NDEF_RECORD_IL_MASK;
|
||||
}
|
||||
|
||||
/* TYPE */
|
||||
memcpy(p_record_buffer, p_ndef_record_desc->p_type, p_ndef_record_desc->type_length);
|
||||
p_record_buffer += p_ndef_record_desc->type_length;
|
||||
|
||||
/* ID */
|
||||
if (p_ndef_record_desc->id_length > 0)
|
||||
{
|
||||
memcpy(p_record_buffer, p_ndef_record_desc->p_id, p_ndef_record_desc->id_length);
|
||||
p_record_buffer += p_ndef_record_desc->id_length;
|
||||
}
|
||||
|
||||
// count how much memory is left in record buffer for payload field.
|
||||
record_payload_len = (*p_record_len - record_header_len);
|
||||
}
|
||||
|
||||
/* PAYLOAD */
|
||||
if (p_ndef_record_desc->payload_constructor != NULL)
|
||||
{
|
||||
err_code =
|
||||
p_ndef_record_desc->payload_constructor(p_ndef_record_desc->p_payload_descriptor,
|
||||
p_record_buffer,
|
||||
&record_payload_len);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_NULL;
|
||||
}
|
||||
|
||||
if (p_record_buffer != NULL)
|
||||
{
|
||||
/* PAYLOAD LENGTH */
|
||||
(void) uint32_big_encode(record_payload_len, p_payload_len);
|
||||
}
|
||||
|
||||
*p_record_len = record_header_len + record_payload_len;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_ndef_bin_payload_memcopy(nfc_ndef_bin_payload_desc_t * p_payload_descriptor,
|
||||
uint8_t * p_buffer,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
|
||||
if (p_buffer != NULL)
|
||||
{
|
||||
if ( *p_len < p_payload_descriptor->payload_length)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
memcpy(p_buffer,
|
||||
p_payload_descriptor->p_payload,
|
||||
p_payload_descriptor->payload_length);
|
||||
}
|
||||
|
||||
*p_len = p_payload_descriptor->payload_length;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
306
COMPONENTS/nfc/ndef/generic/record/nfc_ndef_record.h
Normal file
306
COMPONENTS/nfc/ndef/generic/record/nfc_ndef_record.h
Normal file
@@ -0,0 +1,306 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_NDEF_RECORD_H__
|
||||
#define NFC_NDEF_RECORD_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include <string.h>
|
||||
#include "compiler_abstraction.h"
|
||||
#include "sdk_errors.h"
|
||||
#include "nrf.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_ndef_record Custom NDEF records
|
||||
* @{
|
||||
* @ingroup nfc_ndef_msg
|
||||
*
|
||||
* @brief Generation of NFC NDEF records for NFC messages.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
#define NDEF_RECORD_IL_MASK 0x08 ///< Mask of the ID field presence bit in the flags byte of an NDEF record.
|
||||
#define NDEF_RECORD_TNF_MASK 0x07 ///< Mask of the TNF value field in the first byte of an NDEF record.
|
||||
#define NDEF_RECORD_SR_MASK 0x10 ///< Mask of the SR flag. If set, this flag indicates that the PAYLOAD_LENGTH field has a size of 1 byte. Otherwise, PAYLOAD_LENGTH has 4 bytes.
|
||||
#define NDEF_RECORD_PAYLOAD_LEN_LONG_SIZE 4 ///< Size of the Payload Length field in a long NDEF record.
|
||||
#define NDEF_RECORD_PAYLOAD_LEN_SHORT_SIZE 1 ///< Size of the Payload Length field in a short NDEF record.
|
||||
#define NDEF_RECORD_ID_LEN_SIZE 1 ///< Size of the ID Length field in an NDEF record.
|
||||
|
||||
|
||||
/**
|
||||
* @brief Payload constructor type.
|
||||
|
||||
* A payload constructor is a function for constructing the payload of an NDEF
|
||||
* record.
|
||||
*
|
||||
* @param[in] p_payload_descriptor Pointer to the input data for the constructor.
|
||||
* @param[out] p_buffer Pointer to the payload destination. If NULL, function will
|
||||
* calculate the expected size of the record payload.
|
||||
*
|
||||
* @param[in,out] p_len Size of the available memory to write as input. Size of the generated
|
||||
* record payload as output. The implementation must check if the payload
|
||||
* will fit in the provided buffer. This must be checked by the caller function.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_xxx If an error occurred.
|
||||
*/
|
||||
typedef ret_code_t (* p_payload_constructor_t)(void * p_payload_descriptor,
|
||||
uint8_t * p_buffer,
|
||||
uint32_t * p_len);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Type Name Format (TNF) Field Values.
|
||||
*
|
||||
* Values to specify the TNF of a record.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
TNF_EMPTY = 0x00, ///< The value indicates that there is no type or payload associated with this record.
|
||||
TNF_WELL_KNOWN = 0x01, ///< NFC Forum well-known type [NFC RTD].
|
||||
TNF_MEDIA_TYPE = 0x02, ///< Media-type as defined in RFC 2046 [RFC 2046].
|
||||
TNF_ABSOLUTE_URI = 0x03, ///< Absolute URI as defined in RFC 3986 [RFC 3986].
|
||||
TNF_EXTERNAL_TYPE = 0x04, ///< NFC Forum external type [NFC RTD].
|
||||
TNF_UNKNOWN_TYPE = 0x05, ///< The value indicates that there is no type associated with this record.
|
||||
TNF_UNCHANGED = 0x06, ///< The value is used for the record chunks used in chunked payload.
|
||||
TNF_RESERVED = 0x07, ///< The value is reserved for future use.
|
||||
} nfc_ndef_record_tnf_t;
|
||||
|
||||
|
||||
/**
|
||||
* @brief NDEF record descriptor.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nfc_ndef_record_tnf_t tnf; ///< Value of the Type Name Format (TNF) field.
|
||||
|
||||
uint8_t id_length; ///< Length of the ID field. If 0, a record format without ID field is assumed.
|
||||
uint8_t const * p_id; ///< Pointer to the ID field data. Not relevant if id_length is 0.
|
||||
|
||||
uint8_t type_length; ///< Length of the type field.
|
||||
uint8_t const * p_type; ///< Pointer to the type field data. Not relevant if type_length is 0.
|
||||
|
||||
p_payload_constructor_t payload_constructor; ///< Pointer to the payload constructor function.
|
||||
void * p_payload_descriptor; ///< Pointer to the data for the payload constructor function.
|
||||
|
||||
} nfc_ndef_record_desc_t;
|
||||
|
||||
/**
|
||||
* @brief Record position within the NDEF message.
|
||||
*
|
||||
* Values to specify the location of a record within the NDEF message.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
NDEF_FIRST_RECORD = 0x80, ///< First record.
|
||||
NDEF_MIDDLE_RECORD = 0x00, ///< Middle record.
|
||||
NDEF_LAST_RECORD = 0x40, ///< Last record.
|
||||
NDEF_LONE_RECORD = 0xC0 ///< Only one record in the message.
|
||||
} nfc_ndef_record_location_t;
|
||||
|
||||
#define NDEF_RECORD_LOCATION_MASK (NDEF_LONE_RECORD) ///< Mask of the Record Location bits in the NDEF record's flags byte.
|
||||
|
||||
/**
|
||||
* @brief Binary data descriptor containing the payload for the record.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
uint8_t const * p_payload; ///< Pointer to the buffer with the data.
|
||||
uint32_t payload_length; ///< Length of data in bytes.
|
||||
} nfc_ndef_bin_payload_desc_t;
|
||||
|
||||
/**
|
||||
* @brief Macro for creating and initializing an NFC NDEF record descriptor for a generic record.
|
||||
*
|
||||
* This macro creates and initializes a static instance of type @ref nfc_ndef_record_desc_t.
|
||||
*
|
||||
* Use the macro @ref NFC_NDEF_GENERIC_RECORD_DESC to access the NDEF record descriptor instance.
|
||||
*
|
||||
* @param[in] NAME Name of the created descriptor instance.
|
||||
* @param[in] TNF Type Name Format (TNF) value for the record.
|
||||
* @param[in] P_ID Pointer to the ID string.
|
||||
* @param[in] ID_LEN Length of the ID string.
|
||||
* @param[in] P_TYPE Pointer to the type string.
|
||||
* @param[in] TYPE_LEN Length of the type string.
|
||||
* @param[in] P_PAYLOAD_CONSTRUCTOR Pointer to the payload constructor function.
|
||||
* The constructor must be of type @ref p_payload_constructor_t.
|
||||
* @param[in] P_PAYLOAD_DESCRIPTOR Pointer to the data for the payload constructor.
|
||||
*/
|
||||
#define NFC_NDEF_GENERIC_RECORD_DESC_DEF(NAME, \
|
||||
TNF, \
|
||||
P_ID, \
|
||||
ID_LEN, \
|
||||
P_TYPE, \
|
||||
TYPE_LEN, \
|
||||
P_PAYLOAD_CONSTRUCTOR, \
|
||||
P_PAYLOAD_DESCRIPTOR) \
|
||||
static nfc_ndef_record_desc_t NAME##_ndef_generic_record_desc = \
|
||||
{ \
|
||||
.tnf = TNF, \
|
||||
\
|
||||
.id_length = ID_LEN, \
|
||||
.p_id = P_ID, \
|
||||
\
|
||||
.type_length = TYPE_LEN, \
|
||||
.p_type = P_TYPE, \
|
||||
\
|
||||
.payload_constructor = (p_payload_constructor_t)P_PAYLOAD_CONSTRUCTOR, \
|
||||
.p_payload_descriptor = (void *) P_PAYLOAD_DESCRIPTOR \
|
||||
}
|
||||
|
||||
|
||||
/** @brief Macro for accessing the NFC NDEF record descriptor instance
|
||||
* that you created with @ref NFC_NDEF_GENERIC_RECORD_DESC_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_GENERIC_RECORD_DESC(NAME) (NAME##_ndef_generic_record_desc)
|
||||
|
||||
/**
|
||||
* @brief Macro for creating and initializing an NFC NDEF record descriptor for a record with
|
||||
* binary payload.
|
||||
*
|
||||
* This macro creates and initializes a static instance of type @ref nfc_ndef_record_desc_t and a binary data descriptor containing the payload data.
|
||||
*
|
||||
* Use the macro @ref NFC_NDEF_RECORD_BIN_DATA to access the NDEF record descriptor instance.
|
||||
*
|
||||
* @param[in] NAME Name of the created descriptor instance.
|
||||
* @param[in] TNF Type Name Format (TNF) value for the record.
|
||||
* @param[in] P_ID Pointer to the ID string.
|
||||
* @param[in] ID_LEN Length of the ID string.
|
||||
* @param[in] P_TYPE Pointer to the type string.
|
||||
* @param[in] TYPE_LEN Length of the type string.
|
||||
* @param[in] P_PAYLOAD Pointer to the payload data that will be copied to the payload field.
|
||||
* @param[in] PAYLOAD_LEN Length of the payload.
|
||||
*/
|
||||
#define NFC_NDEF_RECORD_BIN_DATA_DEF(NAME, \
|
||||
TNF, \
|
||||
P_ID, ID_LEN, \
|
||||
P_TYPE, \
|
||||
TYPE_LEN, \
|
||||
P_PAYLOAD, \
|
||||
PAYLOAD_LEN) \
|
||||
static nfc_ndef_bin_payload_desc_t NAME##_nfc_ndef_bin_payload_desc = \
|
||||
{ \
|
||||
.p_payload = P_PAYLOAD, \
|
||||
.payload_length = PAYLOAD_LEN \
|
||||
}; \
|
||||
\
|
||||
static nfc_ndef_record_desc_t NAME##_nfc_ndef_bin_record_desc = \
|
||||
{ \
|
||||
.tnf = TNF, \
|
||||
\
|
||||
.id_length = ID_LEN, \
|
||||
.p_id = P_ID, \
|
||||
\
|
||||
.type_length = TYPE_LEN, \
|
||||
.p_type = P_TYPE, \
|
||||
\
|
||||
.payload_constructor = (p_payload_constructor_t) nfc_ndef_bin_payload_memcopy, \
|
||||
.p_payload_descriptor = (void *) &NAME##_nfc_ndef_bin_payload_desc \
|
||||
}
|
||||
|
||||
|
||||
/** @brief Macro for accessing the NFC NDEF record descriptor instance
|
||||
* that you created with @ref NFC_NDEF_RECORD_BIN_DATA_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_RECORD_BIN_DATA(NAME) (NAME##_nfc_ndef_bin_record_desc)
|
||||
|
||||
/** @brief Macro for accessing the binary data descriptor that contains
|
||||
* the payload of the record that you created with @ref NFC_NDEF_RECORD_BIN_DATA_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_BIN_PAYLOAD_DESC(NAME) (NAME##_nfc_ndef_bin_payload_desc)
|
||||
|
||||
/**
|
||||
* @brief Function for encoding an NDEF record.
|
||||
*
|
||||
* This function encodes an NDEF record according to the provided record descriptor.
|
||||
*
|
||||
* @param[in] p_ndef_record_desc Pointer to the record descriptor.
|
||||
* @param[in] record_location Location of the record within the NDEF message.
|
||||
* @param[out] p_record_buffer Pointer to the record destination. If NULL, function will
|
||||
* calculate the expected size of the record.
|
||||
* @param[in,out] p_record_len Size of the available memory for the record as input. Size of the generated
|
||||
* record as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the record was encoded successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the predicted record size is bigger than the provided buffer space.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If the location of the record is erroneous.
|
||||
* @retval Other Other codes might be returned depending on the NDEF record payload constructor implementation.
|
||||
*/
|
||||
ret_code_t nfc_ndef_record_encode(nfc_ndef_record_desc_t const * p_ndef_record_desc,
|
||||
nfc_ndef_record_location_t record_location,
|
||||
uint8_t * p_record_buffer,
|
||||
uint32_t * p_record_len);
|
||||
|
||||
/**
|
||||
* @brief Function for constructing the payload for an NFC NDEF record from binary data.
|
||||
*
|
||||
* This function copies data from a binary buffer to the payload field of the NFC NDEF record.
|
||||
*
|
||||
* @param[in] p_payload_descriptor Pointer to the descriptor of the binary data location and size.
|
||||
*
|
||||
* @param[out] p_buffer Pointer to the payload destination. If NULL, function will
|
||||
* calculate the expected size of the record payload.
|
||||
* @param[in,out] p_len Size of the available memory for the payload as input. Size of the copied payload
|
||||
* as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the payload size is bigger than the provided buffer space.
|
||||
*/
|
||||
ret_code_t nfc_ndef_bin_payload_memcopy(nfc_ndef_bin_payload_desc_t * p_payload_descriptor,
|
||||
uint8_t * p_buffer,
|
||||
uint32_t * p_len);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_NDEF_RECORD_H__
|
||||
|
||||
146
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_msg.c
Normal file
146
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_msg.c
Normal file
@@ -0,0 +1,146 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <stdint.h>
|
||||
#include <string.h>
|
||||
#include "nfc_launchapp_rec.h"
|
||||
#include "nfc_launchapp_msg.h"
|
||||
#include "nrf_error.h"
|
||||
|
||||
/** @brief Function for generating a description of an NFC NDEF launch application message.
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF message description
|
||||
* and the NFC NDEF record descriptions that are referenced by this message description.
|
||||
*
|
||||
* @param[in] p_android_package_name Pointer to the Android package name string.
|
||||
* If NULL, the Android Application Record will be skipped.
|
||||
* @param[in] android_package_name_length Length of the Android package name.
|
||||
* @param[in] p_win_app_id Pointer to the Windows application ID string (GUID).
|
||||
* If NULL, the Windows LaunchApp Record will be skipped.
|
||||
* @param[in] win_app_id_length Length of the Windows application ID.
|
||||
* @param[out] pp_launchapp_msg_desc Pointer to pointer to the NDEF message description.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the description was successfully created.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If both p_android_package_name and windows_application_id were
|
||||
* invalid (equal to NULL).
|
||||
*/
|
||||
__STATIC_INLINE ret_code_t nfc_launchapp_msg_declare(uint8_t const * p_android_package_name,
|
||||
uint8_t android_package_name_length,
|
||||
uint8_t const * p_win_app_id,
|
||||
uint8_t win_app_id_length,
|
||||
nfc_ndef_msg_desc_t ** pp_launchapp_msg_desc)
|
||||
{
|
||||
|
||||
uint32_t err_code;
|
||||
|
||||
nfc_ndef_record_desc_t * p_win_rec, * p_android_rec;
|
||||
|
||||
/* Create NFC NDEF message description, capacity - 2 records */
|
||||
NFC_NDEF_MSG_DEF(nfc_launchapp_msg, 2);
|
||||
|
||||
/* The message description is static, therefore you must */
|
||||
/* clear the message (needed for supporting multiple calls). */
|
||||
nfc_ndef_msg_clear(&NFC_NDEF_MSG(nfc_launchapp_msg));
|
||||
|
||||
if (p_win_app_id != NULL)
|
||||
{
|
||||
/* Create NFC NDEF Windows Phone LaunchApp Record description */
|
||||
p_win_rec = nfc_windows_launchapp_rec_declare(p_win_app_id,
|
||||
win_app_id_length);
|
||||
|
||||
/* Add Windows LaunchApp record as first record to message */
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_launchapp_msg), p_win_rec);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
return err_code;
|
||||
}
|
||||
|
||||
if (p_android_package_name != NULL)
|
||||
{
|
||||
/* Create NFC NDEF Android Application Record description */
|
||||
p_android_rec = nfc_android_application_rec_declare(p_android_package_name,
|
||||
android_package_name_length);
|
||||
|
||||
/* Add Android App Record as second record to message */
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_launchapp_msg), p_android_rec);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
return err_code;
|
||||
}
|
||||
|
||||
if (NFC_NDEF_MSG(nfc_launchapp_msg).record_count == 0)
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
*pp_launchapp_msg_desc = &NFC_NDEF_MSG(nfc_launchapp_msg);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
ret_code_t nfc_launchapp_msg_encode(uint8_t const * p_android_package_name,
|
||||
uint8_t android_package_name_length,
|
||||
uint8_t const * p_win_app_id,
|
||||
uint8_t win_app_id_length,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
nfc_ndef_msg_desc_t * p_launchapp_msg_desc;
|
||||
ret_code_t err_code;
|
||||
|
||||
err_code = nfc_launchapp_msg_declare(p_android_package_name,
|
||||
android_package_name_length,
|
||||
p_win_app_id,
|
||||
win_app_id_length,
|
||||
&p_launchapp_msg_desc);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
return err_code;
|
||||
|
||||
/* Encode whole message into buffer */
|
||||
err_code = nfc_ndef_msg_encode(p_launchapp_msg_desc,
|
||||
p_buf,
|
||||
p_len);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
|
||||
|
||||
101
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_msg.h
Normal file
101
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_msg.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_LAUNCHAPP_MSG_H__
|
||||
#define NFC_LAUNCHAPP_MSG_H__
|
||||
|
||||
/** @file
|
||||
*
|
||||
* @defgroup nfc_launch_app_msg Launch app messages
|
||||
* @{
|
||||
* @ingroup nfc_ndef_messages
|
||||
*
|
||||
* @brief Generation of NFC NDEF messages that can be used to launch apps.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_msg.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
|
||||
/** @brief Function for encoding an NFC NDEF launch app message.
|
||||
*
|
||||
* This function encodes an NFC NDEF message into a buffer.
|
||||
*
|
||||
* @param[in] p_android_package_name Pointer to the Android package name string.
|
||||
* If NULL, the Android Application Record will be skipped.
|
||||
* @param[in] android_package_name_length Length of the Android package name.
|
||||
* @param[in] p_win_app_id Pointer to the Windows application ID string (GUID).
|
||||
* If NULL, the Windows LaunchApp record will be skipped.
|
||||
* @param[in] win_app_id_length Length of the Windows application ID.
|
||||
* @param[out] p_buf Pointer to the buffer for the message.
|
||||
* @param[in,out] p_len Size of the available memory for the message as input.
|
||||
* Size of the generated message as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the description was successfully created.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If both p_android_package_name and windows_application_id were
|
||||
* invalid (equal to NULL).
|
||||
* @retval NRF_ERROR_NO_MEM If the predicted message size is bigger than the provided
|
||||
* buffer space.
|
||||
* @retval Other Other codes might be returned depending on
|
||||
* the function @ref nfc_ndef_msg_encode
|
||||
*/
|
||||
ret_code_t nfc_launchapp_msg_encode(uint8_t const * p_android_package_name,
|
||||
uint8_t android_package_name_length,
|
||||
uint8_t const * p_win_app_id,
|
||||
uint8_t win_app_id_length,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_LAUNCHAPP_MSG_H__
|
||||
|
||||
|
||||
165
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_rec.c
Normal file
165
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_rec.c
Normal file
@@ -0,0 +1,165 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <string.h>
|
||||
#include "nrf_error.h"
|
||||
#include "app_util.h"
|
||||
#include "nfc_ndef_record.h"
|
||||
|
||||
/**
|
||||
* @brief Type of description of payload of Windows LaunchApp record.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
const uint8_t * platform;
|
||||
uint8_t platform_length;
|
||||
const uint8_t * app_id;
|
||||
uint8_t app_id_length;
|
||||
} win_launchapp_payload_desc_t;
|
||||
|
||||
/** @brief Description of payload of Windows LaunchApp record. */
|
||||
static win_launchapp_payload_desc_t m_win_launchapp_dsc;
|
||||
|
||||
static const uint8_t launchapp_type_str[] = {'a', 'n', 'd', 'r', 'o', 'i', 'd', '.', 'c', 'o', 'm',
|
||||
':', 'p', 'k', 'g'};
|
||||
|
||||
static const uint8_t win_launchapp_type_str[] = {'w', 'i', 'n', 'd', 'o', 'w', 's', '.', 'c', 'o',
|
||||
'm', '/', 'L', 'a', 'u', 'n', 'c', 'h', 'A', 'p',
|
||||
'p'};
|
||||
|
||||
static const uint8_t win_phone_str[] = {'W', 'i', 'n', 'd', 'o', 'w', 's', 'P', 'h', 'o', 'n', 'e'};
|
||||
|
||||
nfc_ndef_record_desc_t * nfc_android_application_rec_declare(uint8_t const * p_package_name,
|
||||
uint8_t package_name_length)
|
||||
{
|
||||
NFC_NDEF_RECORD_BIN_DATA_DEF(android_app_rec,
|
||||
TNF_EXTERNAL_TYPE, // tnf <- external
|
||||
NULL, // no id
|
||||
0, // no id
|
||||
launchapp_type_str,
|
||||
sizeof(launchapp_type_str),
|
||||
NULL,
|
||||
0);
|
||||
|
||||
NFC_NDEF_BIN_PAYLOAD_DESC(android_app_rec).p_payload = p_package_name;
|
||||
NFC_NDEF_BIN_PAYLOAD_DESC(android_app_rec).payload_length = package_name_length;
|
||||
|
||||
return &NFC_NDEF_RECORD_BIN_DATA(android_app_rec);
|
||||
}
|
||||
|
||||
#define WIN_LAUNCHAPP_EMPTY_PARAMETER 0x20 ///< The empty parameter value for the Windows LaunchApp Record.
|
||||
|
||||
/**
|
||||
* @brief Function for constructing the payload for a Windows LaunchApp record.
|
||||
*
|
||||
* This function encodes the payload according to the LaunchApp record definition. It implements an API
|
||||
* compatible with p_payload_constructor_t.
|
||||
*
|
||||
* @param[in] p_input Pointer to the description of the payload.
|
||||
* @param[out] p_buff Pointer to payload destination. If NULL, function will
|
||||
* calculate the expected size of the LaunchApp record payload.
|
||||
*
|
||||
* @param[in,out] p_len Size of available memory to write as input. Size of generated
|
||||
* payload as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS Always success.
|
||||
*/
|
||||
static ret_code_t nfc_win_launchapp_payload_constructor(win_launchapp_payload_desc_t * p_input,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
|
||||
win_launchapp_payload_desc_t * launch_desc = (win_launchapp_payload_desc_t *) p_input;
|
||||
|
||||
uint32_t temp_len = (uint32_t)launch_desc->platform_length + launch_desc->app_id_length + 7;
|
||||
|
||||
if (p_buff != NULL)
|
||||
{
|
||||
if (temp_len > *p_len)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
*p_buff++ = 0x00; // platform count: 1
|
||||
*p_buff++ = 0x01; // -||-
|
||||
|
||||
*p_buff++ = launch_desc->platform_length;
|
||||
memcpy(p_buff, launch_desc->platform, launch_desc->platform_length); // platform
|
||||
p_buff += launch_desc->platform_length;
|
||||
|
||||
|
||||
*p_buff++ = launch_desc->app_id_length;
|
||||
memcpy(p_buff, launch_desc->app_id, launch_desc->app_id_length);
|
||||
p_buff += launch_desc->app_id_length;
|
||||
|
||||
*p_buff++ = 0x00; // parameters length 1B
|
||||
*p_buff++ = 0x01; // -||-
|
||||
*p_buff++ = WIN_LAUNCHAPP_EMPTY_PARAMETER; // empty parameter
|
||||
}
|
||||
|
||||
*p_len = temp_len;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
nfc_ndef_record_desc_t * nfc_windows_launchapp_rec_declare(const uint8_t * p_win_app_id,
|
||||
uint8_t win_app_id_length)
|
||||
{
|
||||
|
||||
NFC_NDEF_GENERIC_RECORD_DESC_DEF(win_launchapp,
|
||||
TNF_ABSOLUTE_URI,
|
||||
NULL,
|
||||
0,
|
||||
win_launchapp_type_str,
|
||||
sizeof(win_launchapp_type_str),
|
||||
nfc_win_launchapp_payload_constructor,
|
||||
&m_win_launchapp_dsc);
|
||||
|
||||
m_win_launchapp_dsc.platform = win_phone_str;
|
||||
m_win_launchapp_dsc.platform_length = sizeof(win_phone_str);
|
||||
|
||||
m_win_launchapp_dsc.app_id = p_win_app_id;
|
||||
m_win_launchapp_dsc.app_id_length = win_app_id_length;
|
||||
|
||||
return &NFC_NDEF_GENERIC_RECORD_DESC(win_launchapp);
|
||||
}
|
||||
|
||||
|
||||
102
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_rec.h
Normal file
102
COMPONENTS/nfc/ndef/launchapp/nfc_launchapp_rec.h
Normal file
@@ -0,0 +1,102 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_LAUNCHAPP_REC_H__
|
||||
#define NFC_LAUNCHAPP_REC_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_launch_app_rec Launch app records
|
||||
* @{
|
||||
* @ingroup nfc_launch_app_msg
|
||||
*
|
||||
* @brief Generation of NFC NDEF record descriptions that launch apps.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_record.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief Function for generating a description of an NFC NDEF Android Application Record (AAR).
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF record description
|
||||
* of an Android Application Record (AAR).
|
||||
*
|
||||
* @note The record payload data (@p p_package_name) should be declared as
|
||||
* static. If it is declared as automatic, the NDEF message encoding
|
||||
* (see @ref nfc_ndef_msg_encode) must be done in the same variable
|
||||
* scope.
|
||||
*
|
||||
* @param[in] p_package_name Pointer to the Android package name string.
|
||||
* @param[in] package_name_length Length of the Android package name.
|
||||
*
|
||||
* @return Pointer to the description of the record.
|
||||
*/
|
||||
nfc_ndef_record_desc_t * nfc_android_application_rec_declare(uint8_t const * p_package_name,
|
||||
uint8_t package_name_length);
|
||||
|
||||
/** @brief Function for generating a description of an NFC NDEF Windows LaunchApp record.
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF record description
|
||||
* of a Windows LaunchApp record.
|
||||
*
|
||||
* @note The record payload data (@p p_win_app_id) should be declared as
|
||||
* static. If it is declared as automatic, the NDEF message encoding
|
||||
* (see @ref nfc_ndef_msg_encode) must be done in the same variable
|
||||
* scope.
|
||||
*
|
||||
* @param[in] p_win_app_id Pointer to the Windows application ID string (GUID).
|
||||
* @param[in] win_app_id_length Length of the Windows application ID.
|
||||
*
|
||||
* @return Pointer to the description of the record.
|
||||
*/
|
||||
nfc_ndef_record_desc_t * nfc_windows_launchapp_rec_declare(const uint8_t * p_win_app_id,
|
||||
uint8_t win_app_id_length);
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_LAUNCHAPP_REC
|
||||
94
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser.c
Normal file
94
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser.c
Normal file
@@ -0,0 +1,94 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "sdk_common.h"
|
||||
#if NRF_MODULE_ENABLED(NFC_NDEF_MSG_PARSER)
|
||||
|
||||
#include "nfc_ndef_msg_parser.h"
|
||||
#include "nrf_delay.h"
|
||||
|
||||
#define NRF_LOG_MODULE_NAME "NFC_NDEF_MSG_PARSER"
|
||||
#if NFC_NDEF_MSG_PARSER_LOG_ENABLED
|
||||
#define NRF_LOG_LEVEL NFC_NDEF_MSG_PARSER_LOG_LEVEL
|
||||
#define NRF_LOG_INFO_COLOR NFC_NDEF_MSG_PARSER_INFO_COLOR
|
||||
#else // NFC_NDEF_MSG_PARSER_LOG_ENABLED
|
||||
#define NRF_LOG_LEVEL 0
|
||||
#endif // NFC_NDEF_MSG_PARSER_LOG_ENABLED
|
||||
#include "nrf_log.h"
|
||||
|
||||
ret_code_t ndef_msg_parser(uint8_t * const p_result_buf,
|
||||
uint32_t * const p_result_buf_len,
|
||||
uint8_t * const p_nfc_data,
|
||||
uint32_t * const p_nfc_data_len)
|
||||
{
|
||||
ret_code_t ret_code;
|
||||
nfc_ndef_parser_memo_desc_t parser_memory_helper;
|
||||
|
||||
ret_code = ndef_parser_memo_resolve(p_result_buf,
|
||||
p_result_buf_len,
|
||||
&parser_memory_helper);
|
||||
|
||||
if (ret_code != NRF_SUCCESS)
|
||||
{
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
ret_code = internal_ndef_msg_parser(&parser_memory_helper,
|
||||
p_nfc_data,
|
||||
p_nfc_data_len);
|
||||
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
|
||||
void ndef_msg_printout(nfc_ndef_msg_desc_t * const p_msg_desc)
|
||||
{
|
||||
uint32_t i;
|
||||
|
||||
nrf_delay_ms(100);
|
||||
NRF_LOG_INFO("NDEF message contains %d record(s)\r\n\r\n", p_msg_desc->record_count);
|
||||
|
||||
for (i = 0; i < p_msg_desc->record_count; i++)
|
||||
{
|
||||
ndef_record_printout(i, p_msg_desc->pp_record[i]);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // NRF_MODULE_ENABLED(NFC_NDEF_MSG_PARSER)
|
||||
124
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser.h
Normal file
124
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser.h
Normal file
@@ -0,0 +1,124 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_NDEF_MSG_PARSER_H__
|
||||
#define NFC_NDEF_MSG_PARSER_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_ndef_parser NDEF message parser
|
||||
* @{
|
||||
* @ingroup nfc_modules
|
||||
*
|
||||
* @brief Parser for NFC NDEF messages and records.
|
||||
*
|
||||
* @defgroup nfc_ndef_msg_parser Parser for NDEF messages
|
||||
* @{
|
||||
* @ingroup nfc_ndef_parser
|
||||
*
|
||||
* @brief Parser for NFC NDEF messages.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_msg_parser_local.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Macro for calculating the memory size required for holding the
|
||||
* description of a message that consists of a certain number of NDEF records.
|
||||
*
|
||||
* @param[in] max_count_of_records Maximum number of records to hold.
|
||||
*/
|
||||
#define NFC_NDEF_PARSER_REQIRED_MEMO_SIZE_CALC(max_count_of_records) \
|
||||
((uint32_t)(max_count_of_records) <= 1) ? \
|
||||
(sizeof(parsed_ndef_msg_1_t) * (uint32_t)(max_count_of_records)) : \
|
||||
(sizeof(parsed_ndef_msg_1_t) + ((NFC_PARSER_M_DELTA) *((uint32_t)(max_count_of_records) - 1)))
|
||||
|
||||
/**
|
||||
* @brief Function for parsing NFC NDEF messages.
|
||||
*
|
||||
* This function parses NDEF messages using NDEF binary record descriptors.
|
||||
*
|
||||
* @param[out] p_result_buf Pointer to the buffer that will be used to hold
|
||||
* the NDEF message descriptor. After parsing is completed successfully, the first address
|
||||
* in the buffer is filled by the NDEF message descriptor
|
||||
* (@ref nfc_ndef_msg_desc_t), which provides a full description of
|
||||
* the parsed NDEF message.
|
||||
* @param[in,out] p_result_buf_len As input: size of the buffer specified by @p p_result_buf.
|
||||
* As output: size of the reserved (used) part of the buffer specified by
|
||||
* @p p_result_buf.
|
||||
* @param[in] p_nfc_data Pointer to the data to be parsed.
|
||||
* @param[in,out] p_nfc_data_len As input: size of the NFC data in the @p p_nfc_data buffer. As output: size of the parsed message.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the provided buffer is too small to hold a one-record message or
|
||||
* the buffer is too small to hold the actual result of the parsing.
|
||||
* @retval NRF_ERROR_INVALID_LENGTH If the expected message length is bigger than the amount of the provided input data.
|
||||
* @retval NRF_ERROR_INVALID_DATA If the message is not a valid NDEF message.
|
||||
*/
|
||||
ret_code_t ndef_msg_parser(uint8_t * const p_result_buf,
|
||||
uint32_t * const p_result_buf_len,
|
||||
uint8_t * const p_nfc_data,
|
||||
uint32_t * const p_nfc_data_len);
|
||||
|
||||
/**
|
||||
* @brief Function for printing the parsed contents of an NDEF message.
|
||||
*
|
||||
* @param[in] p_msg_desc Pointer to the descriptor of the message that should be printed.
|
||||
*/
|
||||
void ndef_msg_printout(nfc_ndef_msg_desc_t * const p_msg_desc);
|
||||
|
||||
/**
|
||||
* @}
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_NDEF_MSG_PARSER_H__
|
||||
|
||||
|
||||
164
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser_local.c
Normal file
164
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser_local.c
Normal file
@@ -0,0 +1,164 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "sdk_common.h"
|
||||
#if NRF_MODULE_ENABLED(NFC_NDEF_MSG_PARSER)
|
||||
|
||||
#include "nfc_ndef_msg_parser_local.h"
|
||||
|
||||
ret_code_t internal_ndef_msg_parser(nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc,
|
||||
uint8_t const * p_nfc_data,
|
||||
uint32_t * const p_nfc_data_len)
|
||||
{
|
||||
nfc_ndef_record_location_t record_location;
|
||||
|
||||
ret_code_t ret_code;
|
||||
|
||||
uint32_t nfc_data_left = *p_nfc_data_len;
|
||||
uint32_t temp_nfc_data_len = 0;
|
||||
|
||||
// want to modify -> use local copy
|
||||
nfc_ndef_bin_payload_desc_t * p_bin_pay_desc = p_parser_memo_desc->p_bin_pay_desc;
|
||||
nfc_ndef_record_desc_t * p_rec_desc = p_parser_memo_desc->p_rec_desc;
|
||||
|
||||
|
||||
while (nfc_data_left > 0)
|
||||
{
|
||||
temp_nfc_data_len = nfc_data_left;
|
||||
|
||||
ret_code = ndef_record_parser(p_bin_pay_desc,
|
||||
p_rec_desc,
|
||||
&record_location,
|
||||
p_nfc_data,
|
||||
&temp_nfc_data_len);
|
||||
|
||||
if (ret_code != NRF_SUCCESS)
|
||||
{
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
// verify the records location flags
|
||||
if (p_parser_memo_desc->p_msg_desc->record_count == 0)
|
||||
{
|
||||
if ((record_location != NDEF_FIRST_RECORD) && (record_location != NDEF_LONE_RECORD))
|
||||
{
|
||||
return NRF_ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if ((record_location != NDEF_MIDDLE_RECORD) && (record_location != NDEF_LAST_RECORD))
|
||||
{
|
||||
return NRF_ERROR_INVALID_DATA;
|
||||
}
|
||||
}
|
||||
|
||||
ret_code = nfc_ndef_msg_record_add(p_parser_memo_desc->p_msg_desc, p_rec_desc);
|
||||
|
||||
if (ret_code != NRF_SUCCESS)
|
||||
{
|
||||
return ret_code;
|
||||
}
|
||||
|
||||
nfc_data_left -= temp_nfc_data_len;
|
||||
|
||||
if ((record_location == NDEF_LAST_RECORD) || (record_location == NDEF_LONE_RECORD))
|
||||
{
|
||||
*p_nfc_data_len = *p_nfc_data_len - nfc_data_left;
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
else
|
||||
{
|
||||
if (p_parser_memo_desc->p_msg_desc->record_count ==
|
||||
p_parser_memo_desc->p_msg_desc->max_record_count)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
p_nfc_data += temp_nfc_data_len;
|
||||
p_bin_pay_desc++;
|
||||
p_rec_desc++;
|
||||
}
|
||||
}
|
||||
|
||||
return NRF_ERROR_INVALID_DATA;
|
||||
|
||||
}
|
||||
|
||||
|
||||
ret_code_t ndef_parser_memo_resolve(uint8_t * const p_result_buf,
|
||||
uint32_t * const p_result_buf_len,
|
||||
nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc)
|
||||
{
|
||||
|
||||
uint32_t max_rec_num;
|
||||
uint32_t memory_last;
|
||||
uint8_t * p_end;
|
||||
nfc_ndef_record_desc_t * * pp_record_desc_array;
|
||||
|
||||
if (*p_result_buf_len < sizeof(parsed_ndef_msg_1_t))
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
memory_last = (*p_result_buf_len) - sizeof(parsed_ndef_msg_1_t);
|
||||
max_rec_num = (memory_last / (NFC_PARSER_M_DELTA)) + 1;
|
||||
|
||||
p_parser_memo_desc->p_msg_desc = (nfc_ndef_msg_desc_t *) p_result_buf;
|
||||
pp_record_desc_array =
|
||||
(nfc_ndef_record_desc_t * *) &p_parser_memo_desc->p_msg_desc[1];
|
||||
p_parser_memo_desc->p_bin_pay_desc =
|
||||
(nfc_ndef_bin_payload_desc_t *) &pp_record_desc_array[max_rec_num];
|
||||
p_parser_memo_desc->p_rec_desc =
|
||||
(nfc_ndef_record_desc_t *) &p_parser_memo_desc->p_bin_pay_desc[max_rec_num];
|
||||
|
||||
// initialize message description
|
||||
p_parser_memo_desc->p_msg_desc->pp_record = pp_record_desc_array;
|
||||
p_parser_memo_desc->p_msg_desc->max_record_count = max_rec_num;
|
||||
p_parser_memo_desc->p_msg_desc->record_count = 0;
|
||||
|
||||
p_end = (uint8_t *) &p_parser_memo_desc->p_rec_desc[max_rec_num];
|
||||
|
||||
*p_result_buf_len = p_end - p_result_buf;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
#endif // NRF_MODULE_ENABLED(NFC_NDEF_MSG_PARSER)
|
||||
168
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser_local.h
Normal file
168
COMPONENTS/nfc/ndef/parser/message/nfc_ndef_msg_parser_local.h
Normal file
@@ -0,0 +1,168 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_NDEF_MSG_PARSER_LOCAL_H__
|
||||
#define NFC_NDEF_MSG_PARSER_LOCAL_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_ndef_msg_parser_local NDEF message parser (internal)
|
||||
* @{
|
||||
* @ingroup nfc_ndef_msg_parser
|
||||
*
|
||||
* @brief Internal part of the parser for NFC NDEF messages.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <stdint.h>
|
||||
#include "nfc_ndef_msg.h"
|
||||
#include "nfc_ndef_record_parser.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Type for holding descriptors that are used by the NDEF parser.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nfc_ndef_msg_desc_t * p_msg_desc; ///< Pointer to the message descriptor.
|
||||
nfc_ndef_bin_payload_desc_t * p_bin_pay_desc; ///< Pointer to the array of binary payload descriptors.
|
||||
nfc_ndef_record_desc_t * p_rec_desc; ///< Pointer to the array of record descriptors.
|
||||
} nfc_ndef_parser_memo_desc_t;
|
||||
|
||||
/**
|
||||
* @brief Memory allocated for a one-record message.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nfc_ndef_msg_desc_t msg_desc;
|
||||
nfc_ndef_record_desc_t * p_record_desc_array[1];
|
||||
nfc_ndef_bin_payload_desc_t bin_pay_desc[1];
|
||||
nfc_ndef_record_desc_t rec_desc[1];
|
||||
} parsed_ndef_msg_1_t;
|
||||
|
||||
/**
|
||||
* @brief Memory allocated for a two-record message.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nfc_ndef_msg_desc_t msg_desc;
|
||||
nfc_ndef_record_desc_t * p_record_desc_array[2];
|
||||
nfc_ndef_bin_payload_desc_t bin_pay_desc[2];
|
||||
nfc_ndef_record_desc_t rec_desc[2];
|
||||
} parsed_ndef_msg_2_t;
|
||||
|
||||
/**
|
||||
* @brief Amount of memory that is required per record in addition to the memory allocated for the message descriptor.
|
||||
*/
|
||||
#define NFC_PARSER_M_DELTA (sizeof(parsed_ndef_msg_2_t) - sizeof(parsed_ndef_msg_1_t))
|
||||
|
||||
|
||||
/**
|
||||
* @brief Function for resolving data instances in the provided buffer according
|
||||
* to requirements of the function @ref internal_ndef_msg_parser.
|
||||
*
|
||||
* This internal function distributes the provided memory between certain data instances that are required
|
||||
* by @ref internal_ndef_msg_parser.
|
||||
*
|
||||
* This function should not be used directly.
|
||||
*
|
||||
* @param[in] p_result_buf Pointer to the buffer that will be used to allocate
|
||||
* data instances.
|
||||
* @param[in,out] p_result_buf_len As input: size of the buffer specified by @p p_result_buf.
|
||||
* As output: size of the reserved (used) part of the buffer specified by
|
||||
* @p p_result_buf.
|
||||
* @param[out] p_parser_memo_desc Pointer to the structure for holding descriptors of the allocated data
|
||||
* instances.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the provided buffer is too small to hold a one-record message.
|
||||
*/
|
||||
ret_code_t ndef_parser_memo_resolve(uint8_t * const p_result_buf,
|
||||
uint32_t * const p_result_buf_len,
|
||||
nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc);
|
||||
|
||||
|
||||
/**
|
||||
* @brief Function for parsing NFC NDEF messages.
|
||||
*
|
||||
* This internal function parses NDEF messages into certain data instances.
|
||||
*
|
||||
* This function should not be used directly.
|
||||
*
|
||||
* @param[in,out] p_parser_memo_desc Pointer to the structure that holds descriptors of the allocated data
|
||||
* instances for the parser. This structure contains the following fields: @n
|
||||
* .p_msg_desc Pointer to the message descriptor that will
|
||||
* be filled with parsed data. @n
|
||||
* .p_bin_pay_desc Pointer to the array of binary payload
|
||||
* descriptors that will be filled with parsed
|
||||
* data. @n
|
||||
* .p_rec_desc Pointer to the array of record descriptors
|
||||
* that will be filled with parsed data. @n
|
||||
* The arrays specified by @p .p_bin_pay_desc and @p .p_rec_desc must not
|
||||
* contain more elements than the message descriptor
|
||||
* specified by \p .p_msg_desc can hold.
|
||||
*
|
||||
* @param[in] p_nfc_data Pointer to the data to be parsed.
|
||||
* @param[in,out] p_nfc_data_len As input: size of the NFC data in the @p p_nfc_data buffer.
|
||||
* As output: size of the parsed message.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_INVALID_LENGTH If the expected message length is bigger than the amount of provided input data.
|
||||
* @retval NRF_ERROR_INVALID_DATA If the message is not a valid NDEF message.
|
||||
* @retval NRF_ERROR_NO_MEM If the provided memory resources are too small to hold the parsing result.
|
||||
*/
|
||||
ret_code_t internal_ndef_msg_parser(nfc_ndef_parser_memo_desc_t * const p_parser_memo_desc,
|
||||
uint8_t const * p_nfc_data,
|
||||
uint32_t * const p_nfc_data_len);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_NDEF_MSG_PARSER_LOCAL_H__
|
||||
219
COMPONENTS/nfc/ndef/parser/record/nfc_ndef_record_parser.c
Normal file
219
COMPONENTS/nfc/ndef/parser/record/nfc_ndef_record_parser.c
Normal file
@@ -0,0 +1,219 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 "sdk_common.h"
|
||||
#if NRF_MODULE_ENABLED(NFC_NDEF_RECORD_PARSER)
|
||||
|
||||
#include <stdint.h>
|
||||
#include <stdbool.h>
|
||||
#include "nfc_ndef_record_parser.h"
|
||||
#include "app_util.h"
|
||||
#include "nordic_common.h"
|
||||
#include "nrf_delay.h"
|
||||
|
||||
#define NRF_LOG_MODULE_NAME "NFC_NDEF_PARSER"
|
||||
#if NFC_NDEF_RECORD_PARSER_LOG_ENABLED
|
||||
#define NRF_LOG_LEVEL NFC_NDEF_RECORD_PARSER_LOG_LEVEL
|
||||
#define NRF_LOG_INFO_COLOR NFC_NDEF_RECORD_PARSER_INFO_COLOR
|
||||
#else // NFC_NDEF_RECORD_PARSER_LOG_ENABLED
|
||||
#define NRF_LOG_LEVEL 0
|
||||
#endif // NFC_NDEF_RECORD_PARSER_LOG_ENABLED
|
||||
#include "nrf_log.h"
|
||||
|
||||
/* Sum of sizes of fields: TNF-flags, Type Length, Payload Length in short NDEF record. */
|
||||
#define NDEF_RECORD_BASE_LONG_SHORT (2 + NDEF_RECORD_PAYLOAD_LEN_SHORT_SIZE)
|
||||
|
||||
|
||||
ret_code_t ndef_record_parser(nfc_ndef_bin_payload_desc_t * p_bin_pay_desc,
|
||||
nfc_ndef_record_desc_t * p_rec_desc,
|
||||
nfc_ndef_record_location_t * p_record_location,
|
||||
uint8_t const * p_nfc_data,
|
||||
uint32_t * p_nfc_data_len)
|
||||
{
|
||||
uint32_t expected_rec_size = NDEF_RECORD_BASE_LONG_SHORT;
|
||||
|
||||
if (expected_rec_size > *p_nfc_data_len)
|
||||
{
|
||||
return NRF_ERROR_INVALID_LENGTH;
|
||||
}
|
||||
|
||||
p_rec_desc->tnf = (nfc_ndef_record_tnf_t) ((*p_nfc_data) & NDEF_RECORD_TNF_MASK);
|
||||
|
||||
/* An NDEF parser that receives an NDEF record with an unknown or unsupported TNF field value
|
||||
SHOULD treat it as Unknown. See NFCForum-TS-NDEF_1.0 */
|
||||
if (p_rec_desc->tnf == TNF_RESERVED)
|
||||
{
|
||||
p_rec_desc->tnf = TNF_UNKNOWN_TYPE;
|
||||
}
|
||||
|
||||
*p_record_location = (nfc_ndef_record_location_t) ((*p_nfc_data) & NDEF_RECORD_LOCATION_MASK);
|
||||
|
||||
uint8_t flags = *(p_nfc_data++);
|
||||
|
||||
p_rec_desc->type_length = *(p_nfc_data++);
|
||||
|
||||
uint32_t payload_lenght;
|
||||
|
||||
if (flags & NDEF_RECORD_SR_MASK)
|
||||
{
|
||||
payload_lenght = *(p_nfc_data++);
|
||||
}
|
||||
else
|
||||
{
|
||||
expected_rec_size +=
|
||||
NDEF_RECORD_PAYLOAD_LEN_LONG_SIZE - NDEF_RECORD_PAYLOAD_LEN_SHORT_SIZE;
|
||||
|
||||
if (expected_rec_size > *p_nfc_data_len)
|
||||
{
|
||||
return NRF_ERROR_INVALID_LENGTH;
|
||||
}
|
||||
|
||||
payload_lenght = uint32_big_decode(p_nfc_data);
|
||||
p_nfc_data += NDEF_RECORD_PAYLOAD_LEN_LONG_SIZE;
|
||||
}
|
||||
|
||||
if (flags & NDEF_RECORD_IL_MASK)
|
||||
{
|
||||
expected_rec_size += NDEF_RECORD_ID_LEN_SIZE;
|
||||
|
||||
if (expected_rec_size > *p_nfc_data_len)
|
||||
{
|
||||
return NRF_ERROR_INVALID_LENGTH;
|
||||
}
|
||||
|
||||
p_rec_desc->id_length = *(p_nfc_data++);
|
||||
}
|
||||
else
|
||||
{
|
||||
p_rec_desc->id_length = 0;
|
||||
p_rec_desc->p_id = NULL;
|
||||
}
|
||||
|
||||
expected_rec_size += p_rec_desc->type_length + p_rec_desc->id_length + payload_lenght;
|
||||
|
||||
if (expected_rec_size > *p_nfc_data_len)
|
||||
{
|
||||
return NRF_ERROR_INVALID_LENGTH;
|
||||
}
|
||||
|
||||
if (p_rec_desc->type_length > 0)
|
||||
{
|
||||
p_rec_desc->p_type = p_nfc_data;
|
||||
|
||||
p_nfc_data += p_rec_desc->type_length;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_rec_desc->p_type = NULL;
|
||||
}
|
||||
|
||||
if (p_rec_desc->id_length > 0)
|
||||
{
|
||||
p_rec_desc->p_id = p_nfc_data;
|
||||
|
||||
p_nfc_data += p_rec_desc->id_length;
|
||||
}
|
||||
|
||||
if (payload_lenght == 0)
|
||||
{
|
||||
p_bin_pay_desc->p_payload = NULL;
|
||||
}
|
||||
else
|
||||
{
|
||||
p_bin_pay_desc->p_payload = p_nfc_data;
|
||||
}
|
||||
|
||||
p_bin_pay_desc->payload_length = payload_lenght;
|
||||
|
||||
p_rec_desc->p_payload_descriptor = p_bin_pay_desc;
|
||||
p_rec_desc->payload_constructor = (p_payload_constructor_t) nfc_ndef_bin_payload_memcopy;
|
||||
|
||||
*p_nfc_data_len = expected_rec_size;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
char const * const tnf_strings[] =
|
||||
{
|
||||
"Empty\r\n",
|
||||
"NFC Forum well-known type\r\n",
|
||||
"Media-type (RFC 2046)\r\n",
|
||||
"Absolute URI (RFC 3986)\r\n",
|
||||
"NFC Forum external type (NFC RTD)\r\n",
|
||||
"Unknown\r\n",
|
||||
"Unchanged\r\n",
|
||||
"Reserved\r\n"
|
||||
};
|
||||
|
||||
void ndef_record_printout(uint32_t num, nfc_ndef_record_desc_t * const p_rec_desc)
|
||||
{
|
||||
NRF_LOG_INFO("NDEF record %d content:\r\n", num);
|
||||
NRF_LOG_INFO("TNF: %s",(uint32_t)tnf_strings[p_rec_desc->tnf]);
|
||||
|
||||
if (p_rec_desc->p_id != NULL)
|
||||
{
|
||||
NRF_LOG_INFO("ID:\r\n");
|
||||
NRF_LOG_HEXDUMP_INFO((uint8_t *)p_rec_desc->p_id, p_rec_desc->id_length);
|
||||
}
|
||||
|
||||
if (p_rec_desc->p_type != NULL)
|
||||
{
|
||||
NRF_LOG_INFO("type:\r\n");
|
||||
NRF_LOG_HEXDUMP_INFO((uint8_t *)p_rec_desc->p_type, p_rec_desc->type_length);
|
||||
}
|
||||
|
||||
if (p_rec_desc->payload_constructor == (p_payload_constructor_t) nfc_ndef_bin_payload_memcopy)
|
||||
{
|
||||
nfc_ndef_bin_payload_desc_t * p_bin_pay_desc = p_rec_desc->p_payload_descriptor;
|
||||
|
||||
if (p_bin_pay_desc->p_payload != NULL)
|
||||
{
|
||||
NRF_LOG_INFO("Payload data (%d bytes):\r\n", p_bin_pay_desc->payload_length);
|
||||
NRF_LOG_HEXDUMP_INFO((uint8_t *)p_bin_pay_desc->p_payload, p_bin_pay_desc->payload_length);
|
||||
}
|
||||
else
|
||||
{
|
||||
NRF_LOG_INFO("No payload\r\n");
|
||||
}
|
||||
}
|
||||
NRF_LOG_INFO("\r\n\r\n");
|
||||
}
|
||||
|
||||
#endif // NRF_MODULE_ENABLED(NFC_NDEF_RECORD_PARSER)
|
||||
101
COMPONENTS/nfc/ndef/parser/record/nfc_ndef_record_parser.h
Normal file
101
COMPONENTS/nfc/ndef/parser/record/nfc_ndef_record_parser.h
Normal file
@@ -0,0 +1,101 @@
|
||||
/**
|
||||
* Copyright (c) 2016 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_NDEF_RECORD_PARSER_H__
|
||||
#define NFC_NDEF_RECORD_PARSER_H__
|
||||
|
||||
#include <stdint.h>
|
||||
#include "sdk_errors.h"
|
||||
#include "nfc_ndef_record.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_ndef_record_parser Parser for NDEF records
|
||||
* @{
|
||||
* @ingroup nfc_ndef_parser
|
||||
*
|
||||
* @brief Parser for NFC NDEF records.
|
||||
*
|
||||
*/
|
||||
|
||||
|
||||
/**
|
||||
* @brief Function for parsing NDEF records.
|
||||
*
|
||||
* This parsing implementation uses the binary payload descriptor (@ref nfc_ndef_bin_payload_desc_t) to describe the payload for the record.
|
||||
*
|
||||
* @param[out] p_bin_pay_desc Pointer to the binary payload descriptor that will be filled and referenced by the record descriptor.
|
||||
* @param[out] p_rec_desc Pointer to the record descriptor that will be filled with parsed data.
|
||||
* @param[out] p_record_location Pointer to the record location.
|
||||
* @param[in] p_nfc_data Pointer to the raw data to be parsed.
|
||||
* @param[in,out] p_nfc_data_len As input: size of the NFC data in the @p p_nfc_data buffer. As output: size of the parsed record.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the function completed successfully.
|
||||
* @retval NRF_ERROR_INVALID_LENGTH If the expected record length is bigger than the provided input data amount.
|
||||
*/
|
||||
ret_code_t ndef_record_parser(nfc_ndef_bin_payload_desc_t * p_bin_pay_desc,
|
||||
nfc_ndef_record_desc_t * p_rec_desc,
|
||||
nfc_ndef_record_location_t * p_record_location,
|
||||
uint8_t const * p_nfc_data,
|
||||
uint32_t * p_nfc_data_len);
|
||||
|
||||
/**
|
||||
* @brief Function for printing the parsed contents of the NDEF record.
|
||||
*
|
||||
* @param[in] num Sequence number of the record within the NDEF message.
|
||||
* @param[in] p_rec_desc Pointer to the descriptor of the record that should be printed.
|
||||
*
|
||||
*/
|
||||
void ndef_record_printout(uint32_t num, nfc_ndef_record_desc_t * const p_rec_desc);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_NDEF_RECORD_PARSER_H__
|
||||
105
COMPONENTS/nfc/ndef/text/nfc_text_rec.c
Normal file
105
COMPONENTS/nfc/ndef/text/nfc_text_rec.c
Normal file
@@ -0,0 +1,105 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <string.h>
|
||||
#include "nfc_text_rec.h"
|
||||
#include "nrf_error.h"
|
||||
|
||||
#define TEXT_REC_STATUS_SIZE 1 ///< Size of the status.
|
||||
#define TEXT_REC_STATUS_UTF_POS 7 ///< Position of a character encoding type.
|
||||
#define TEXT_REC_RESERVED_POS 6 ///< Reserved position.
|
||||
|
||||
const uint8_t nfc_text_rec_type_field[] = {'T'};
|
||||
|
||||
|
||||
/**
|
||||
* @brief Function for calculating payload size.
|
||||
*/
|
||||
__STATIC_INLINE uint32_t nfc_text_rec_payload_size_get(nfc_text_rec_payload_desc_t * p_nfc_rec_text_payload_desc)
|
||||
{
|
||||
return (TEXT_REC_STATUS_SIZE
|
||||
+ p_nfc_rec_text_payload_desc->lang_code_len
|
||||
+ p_nfc_rec_text_payload_desc->data_len);
|
||||
}
|
||||
|
||||
ret_code_t nfc_text_rec_payload_constructor(nfc_text_rec_payload_desc_t * p_nfc_rec_text_payload_desc,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
if ((p_nfc_rec_text_payload_desc->lang_code_len == 0)
|
||||
|| (p_nfc_rec_text_payload_desc->lang_code_len & (1 << TEXT_REC_RESERVED_POS))
|
||||
|| (p_nfc_rec_text_payload_desc->lang_code_len & (1 << TEXT_REC_STATUS_UTF_POS))
|
||||
|| (p_nfc_rec_text_payload_desc->p_lang_code == NULL)
|
||||
|| (p_nfc_rec_text_payload_desc->data_len == 0)
|
||||
|| (p_nfc_rec_text_payload_desc->p_data == NULL)
|
||||
|| (p_len == NULL))
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
uint32_t payload_size = nfc_text_rec_payload_size_get(p_nfc_rec_text_payload_desc);
|
||||
|
||||
if (p_buff != NULL)
|
||||
{
|
||||
if (payload_size > *p_len)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
*p_buff = (p_nfc_rec_text_payload_desc->lang_code_len
|
||||
+ (p_nfc_rec_text_payload_desc->utf << TEXT_REC_STATUS_UTF_POS));
|
||||
p_buff += TEXT_REC_STATUS_SIZE;
|
||||
|
||||
memcpy(p_buff,
|
||||
p_nfc_rec_text_payload_desc->p_lang_code,
|
||||
p_nfc_rec_text_payload_desc->lang_code_len);
|
||||
p_buff += p_nfc_rec_text_payload_desc->lang_code_len;
|
||||
|
||||
memcpy(p_buff,
|
||||
p_nfc_rec_text_payload_desc->p_data,
|
||||
p_nfc_rec_text_payload_desc->data_len);
|
||||
}
|
||||
|
||||
*p_len = payload_size;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
|
||||
163
COMPONENTS/nfc/ndef/text/nfc_text_rec.h
Normal file
163
COMPONENTS/nfc/ndef/text/nfc_text_rec.h
Normal file
@@ -0,0 +1,163 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_TEXT_REC_H__
|
||||
#define NFC_TEXT_REC_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_text_rec Text records
|
||||
* @{
|
||||
* @ingroup nfc_ndef_messages
|
||||
*
|
||||
* @brief Generation of NFC NDEF Text record descriptions.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nfc_ndef_record.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Type of the Unicode Transformation Format.
|
||||
*
|
||||
* Values to specify the type of UTF for an NFC NDEF Text record.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
UTF_8 = 0, ///< Unicode Transformation Format 8.
|
||||
UTF_16 = 1, ///< Unicode Transformation Format 16.
|
||||
} nfc_text_rec_utf_type_t;
|
||||
|
||||
/**
|
||||
* @brief Text record payload descriptor.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nfc_text_rec_utf_type_t utf; ///< Type of the Unicode Transformation Format.
|
||||
|
||||
uint8_t const * p_lang_code; ///< Pointer to the IANA language code.
|
||||
uint8_t lang_code_len; ///< Length of the IANA language code.
|
||||
|
||||
uint8_t const * p_data; ///< Pointer to the user text.
|
||||
uint32_t data_len; ///< Length of the user text.
|
||||
} nfc_text_rec_payload_desc_t;
|
||||
|
||||
/**
|
||||
* @brief Constructor for an NFC NDEF Text record payload.
|
||||
*
|
||||
* @param[in] p_nfc_rec_text_payload_desc Pointer to the Text record description.
|
||||
* @param[out] p_buff Pointer to the payload destination. If NULL, function will
|
||||
* calculate the expected size of the Text record payload.
|
||||
*
|
||||
* @param[in,out] p_len Size of the available memory to write as input.
|
||||
* Size of the generated record payload as output.
|
||||
*/
|
||||
ret_code_t nfc_text_rec_payload_constructor(nfc_text_rec_payload_desc_t * p_nfc_rec_text_payload_desc,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len);
|
||||
|
||||
/**
|
||||
* @brief External reference to the type field of the Text record, defined in the
|
||||
* file @c nfc_text_rec.c. It is used in the @ref NFC_NDEF_TEXT_RECORD_DESC_DEF macro.
|
||||
*/
|
||||
extern const uint8_t nfc_text_rec_type_field[];
|
||||
|
||||
/**
|
||||
* @brief Size of the type field of the Text record, defined in the
|
||||
* file @c nfc_text_rec.c. It is used in the @ref NFC_NDEF_TEXT_RECORD_DESC_DEF macro.
|
||||
*/
|
||||
#define NFC_TEXT_REC_TYPE_LENGTH 1
|
||||
|
||||
/**
|
||||
*@brief Macro for creating and initializing an NFC NDEF record descriptor for a Text record.
|
||||
*
|
||||
* This macro creates and initializes a static instance of type @ref nfc_ndef_record_desc_t and
|
||||
* a static instance of type @ref nfc_text_rec_payload_desc_t, which together constitute
|
||||
* an instance of a Text record.
|
||||
*
|
||||
* Use the macro @ref NFC_NDEF_TEXT_RECORD_DESC to access the NDEF Text record descriptor instance.
|
||||
*
|
||||
* @param[in] NAME Name of the created record descriptor instance.
|
||||
* @param[in] UTF Unicode Transformation Format.
|
||||
* @param[in] P_LANG_CODE Pointer to the IANA language code.
|
||||
* @param[in] LANG_CODE_LEN Length of the IANA language code.
|
||||
* @param[in] P_DATA Pointer to the user text.
|
||||
* @param[in] DATA_LEN Length of the user text.
|
||||
*/
|
||||
#define NFC_NDEF_TEXT_RECORD_DESC_DEF(NAME, \
|
||||
UTF, \
|
||||
P_LANG_CODE, \
|
||||
LANG_CODE_LEN, \
|
||||
P_DATA, \
|
||||
DATA_LEN) \
|
||||
static nfc_text_rec_payload_desc_t NAME##_nfc_text_rec_payload_desc; \
|
||||
NAME##_nfc_text_rec_payload_desc = (nfc_text_rec_payload_desc_t) \
|
||||
{ \
|
||||
.utf = UTF, \
|
||||
.p_lang_code = P_LANG_CODE, \
|
||||
.lang_code_len = LANG_CODE_LEN, \
|
||||
.p_data = P_DATA, \
|
||||
.data_len = DATA_LEN, \
|
||||
}; \
|
||||
NFC_NDEF_GENERIC_RECORD_DESC_DEF(NAME, \
|
||||
TNF_WELL_KNOWN, \
|
||||
0, \
|
||||
0, \
|
||||
nfc_text_rec_type_field, \
|
||||
NFC_TEXT_REC_TYPE_LENGTH, \
|
||||
nfc_text_rec_payload_constructor, \
|
||||
&(NAME##_nfc_text_rec_payload_desc))
|
||||
|
||||
/**
|
||||
* @brief Macro for accessing the NFC NDEF Text record descriptor
|
||||
* instance that was created with @ref NFC_NDEF_TEXT_RECORD_DESC_DEF.
|
||||
*/
|
||||
#define NFC_NDEF_TEXT_RECORD_DESC(NAME) NFC_NDEF_GENERIC_RECORD_DESC(NAME)
|
||||
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_TEXT_REC_H__
|
||||
125
COMPONENTS/nfc/ndef/uri/nfc_uri_msg.c
Normal file
125
COMPONENTS/nfc/ndef/uri/nfc_uri_msg.c
Normal file
@@ -0,0 +1,125 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <string.h>
|
||||
#include "nfc_uri_msg.h"
|
||||
|
||||
/** @brief Function for generating a description of an NFC NDEF URI message.
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF message description
|
||||
* and NFC NDEF records descriptions.
|
||||
*
|
||||
* @param[in] uri_id_code URI identifier code that defines the protocol field of the URI.
|
||||
* @param[in] p_uri_data Pointer to the URI string.
|
||||
* This string should not contain the protocol field if the protocol
|
||||
* was specified in @p uri_id_code
|
||||
* @param[in] uri_data_len Length of the URI string.
|
||||
* @param[out] pp_uri_msg_desc Pointer to pointer to the NDEF message description.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the description was successfully created.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If the URI string was invalid (equal to NULL).
|
||||
*/
|
||||
static ret_code_t nfc_uri_msg_declare( nfc_uri_id_t uri_id_code,
|
||||
uint8_t const * const p_uri_data,
|
||||
uint8_t uri_data_len,
|
||||
nfc_ndef_msg_desc_t ** pp_uri_msg_desc)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
nfc_ndef_record_desc_t * p_uri_rec;
|
||||
|
||||
/* Create NFC NDEF message description, capacity - 1 record */
|
||||
NFC_NDEF_MSG_DEF(nfc_uri_msg, 1);
|
||||
|
||||
/* The message description is static, therefore */
|
||||
/* you must clear the message (needed for supporting multiple calls) */
|
||||
nfc_ndef_msg_clear(&NFC_NDEF_MSG(nfc_uri_msg));
|
||||
|
||||
if (p_uri_data != NULL)
|
||||
{
|
||||
/* Create NFC NDEF URI Record description */
|
||||
p_uri_rec = nfc_uri_rec_declare(uri_id_code,
|
||||
p_uri_data,
|
||||
uri_data_len);
|
||||
|
||||
/* Add URI record as lone record to message */
|
||||
err_code = nfc_ndef_msg_record_add(&NFC_NDEF_MSG(nfc_uri_msg), p_uri_rec);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
return NRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
|
||||
*pp_uri_msg_desc = &NFC_NDEF_MSG(nfc_uri_msg);
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
ret_code_t nfc_uri_msg_encode( nfc_uri_id_t uri_id_code,
|
||||
uint8_t const * const p_uri_data,
|
||||
uint8_t uri_data_len,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
ret_code_t err_code;
|
||||
nfc_ndef_msg_desc_t * p_uri_msg_desc;
|
||||
|
||||
/* Create NFC NDEF message description with URI record */
|
||||
err_code = nfc_uri_msg_declare( uri_id_code,
|
||||
p_uri_data,
|
||||
uri_data_len,
|
||||
&p_uri_msg_desc);
|
||||
|
||||
if (err_code != NRF_SUCCESS)
|
||||
{
|
||||
return err_code;
|
||||
}
|
||||
|
||||
/* Encode whole message into buffer */
|
||||
err_code = nfc_ndef_msg_encode(p_uri_msg_desc,
|
||||
p_buf,
|
||||
p_len);
|
||||
|
||||
return err_code;
|
||||
}
|
||||
96
COMPONENTS/nfc/ndef/uri/nfc_uri_msg.h
Normal file
96
COMPONENTS/nfc/ndef/uri/nfc_uri_msg.h
Normal file
@@ -0,0 +1,96 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_URI_MSG_H__
|
||||
#define NFC_URI_MSG_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_uri_msg URI messages
|
||||
* @{
|
||||
* @ingroup nfc_ndef_messages
|
||||
*
|
||||
* @brief Generation of NFC NDEF messages with a URI record.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nfc_ndef_msg.h"
|
||||
#include "nfc_uri_rec.h"
|
||||
#include "nrf_error.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/** @brief Function for encoding an NFC NDEF URI message.
|
||||
*
|
||||
* This function encodes an NFC NDEF message into a buffer.
|
||||
*
|
||||
* @param[in] uri_id_code URI identifier code that defines the protocol field of the URI.
|
||||
* @param[in] p_uri_data Pointer to the URI string.
|
||||
* The string should not contain the protocol field if the protocol
|
||||
* was specified in @p uri_id_code.
|
||||
* @param[in] uri_data_len Length of the URI string.
|
||||
* @param[out] p_buf Pointer to the buffer for the message.
|
||||
* @param[in,out] p_len Size of the available memory for the message as input.
|
||||
* Size of the generated message as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the description was successfully created.
|
||||
* @retval NRF_ERROR_INVALID_PARAM If the URI string was invalid (equal to NULL).
|
||||
* @retval NRF_ERROR_NO_MEM If the predicted message size is bigger than the provided
|
||||
* buffer space.
|
||||
* @retval Other Other codes might be returned depending on
|
||||
* the function @ref nfc_ndef_msg_encode.
|
||||
*/
|
||||
ret_code_t nfc_uri_msg_encode( nfc_uri_id_t uri_id_code,
|
||||
uint8_t const * const p_uri_data,
|
||||
uint8_t uri_data_len,
|
||||
uint8_t * p_buf,
|
||||
uint32_t * p_len);
|
||||
|
||||
/**
|
||||
* @}
|
||||
*/
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_URI_MSG_H__
|
||||
115
COMPONENTS/nfc/ndef/uri/nfc_uri_rec.c
Normal file
115
COMPONENTS/nfc/ndef/uri/nfc_uri_rec.c
Normal file
@@ -0,0 +1,115 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 <string.h>
|
||||
#include "nfc_uri_rec.h"
|
||||
#include "nrf_error.h"
|
||||
|
||||
/**
|
||||
* @brief Type of description of the payload of a URI record.
|
||||
*/
|
||||
typedef struct
|
||||
{
|
||||
nfc_uri_id_t uri_id_code; ///< URI identifier code.
|
||||
uint8_t const * p_uri_data; ///< Pointer to a URI string.
|
||||
uint8_t uri_data_len; ///< Length of the URI string.
|
||||
} uri_payload_desc_t;
|
||||
|
||||
/**
|
||||
* @brief Function for constructing the payload for a URI record.
|
||||
*
|
||||
* This function encodes the payload according to the URI record definition. It implements an API
|
||||
* compatible with @ref p_payload_constructor_t.
|
||||
*
|
||||
* @param[in] p_input Pointer to the description of the payload.
|
||||
* @param[out] p_buff Pointer to payload destination. If NULL, function will
|
||||
* calculate the expected size of the URI record payload.
|
||||
*
|
||||
* @param[in,out] p_len Size of available memory to write as input. Size of generated
|
||||
* payload as output.
|
||||
*
|
||||
* @retval NRF_SUCCESS If the payload was encoded successfully.
|
||||
* @retval NRF_ERROR_NO_MEM If the predicted payload size is bigger than the provided buffer space.
|
||||
*/
|
||||
static ret_code_t nfc_uri_payload_constructor( uri_payload_desc_t * p_input,
|
||||
uint8_t * p_buff,
|
||||
uint32_t * p_len)
|
||||
{
|
||||
if (p_buff != NULL)
|
||||
{
|
||||
/* Verify if there is enough available memory */
|
||||
if (p_input->uri_data_len >= *p_len)
|
||||
{
|
||||
return NRF_ERROR_NO_MEM;
|
||||
}
|
||||
|
||||
/* Copy descriptor content into the buffer */
|
||||
*(p_buff++) = p_input->uri_id_code;
|
||||
memcpy(p_buff, p_input->p_uri_data, p_input->uri_data_len );
|
||||
}
|
||||
|
||||
*p_len = p_input->uri_data_len + 1;
|
||||
|
||||
return NRF_SUCCESS;
|
||||
}
|
||||
|
||||
nfc_ndef_record_desc_t * nfc_uri_rec_declare( nfc_uri_id_t uri_id_code,
|
||||
uint8_t const * const p_uri_data,
|
||||
uint8_t uri_data_len)
|
||||
{
|
||||
static uri_payload_desc_t uri_payload_desc;
|
||||
static const uint8_t static_uri_type = 'U';
|
||||
|
||||
NFC_NDEF_GENERIC_RECORD_DESC_DEF( uri_rec,
|
||||
TNF_WELL_KNOWN, // tnf <- well-known
|
||||
NULL,
|
||||
0, // no id
|
||||
&static_uri_type,
|
||||
1, // type size 1B
|
||||
nfc_uri_payload_constructor,
|
||||
&uri_payload_desc);
|
||||
|
||||
uri_payload_desc.uri_id_code = uri_id_code;
|
||||
uri_payload_desc.p_uri_data = p_uri_data;
|
||||
uri_payload_desc.uri_data_len = uri_data_len;
|
||||
|
||||
return &NFC_NDEF_GENERIC_RECORD_DESC( uri_rec);
|
||||
}
|
||||
|
||||
133
COMPONENTS/nfc/ndef/uri/nfc_uri_rec.h
Normal file
133
COMPONENTS/nfc/ndef/uri/nfc_uri_rec.h
Normal file
@@ -0,0 +1,133 @@
|
||||
/**
|
||||
* Copyright (c) 2015 - 2017, Nordic Semiconductor ASA
|
||||
*
|
||||
* All rights reserved.
|
||||
*
|
||||
* 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, except as embedded into a Nordic
|
||||
* Semiconductor ASA integrated circuit in a product or a software update for
|
||||
* such product, 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 Nordic Semiconductor ASA nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* 4. This software, with or without modification, must only be used with a
|
||||
* Nordic Semiconductor ASA integrated circuit.
|
||||
*
|
||||
* 5. Any software provided in binary form under this license must not be reverse
|
||||
* engineered, decompiled, modified and/or disassembled.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
|
||||
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
||||
* OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA 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 NFC_URI_REC_H__
|
||||
#define NFC_URI_REC_H__
|
||||
|
||||
/**@file
|
||||
*
|
||||
* @defgroup nfc_uri_rec URI records
|
||||
* @{
|
||||
* @ingroup nfc_uri_msg
|
||||
*
|
||||
* @brief Generation of NFC NDEF URI record descriptions.
|
||||
*
|
||||
*/
|
||||
|
||||
#include "nfc_ndef_record.h"
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @enum nfc_uri_id_t
|
||||
* @brief URI identifier codes according to "URI Record Type Definition"
|
||||
* (denotation "NFCForum-TS-RTD_URI_1.0" published on 2006-07-24) chapter 3.2.2.
|
||||
*/
|
||||
typedef enum
|
||||
{
|
||||
NFC_URI_NONE = 0x00, /**< No prepending is done. */
|
||||
NFC_URI_HTTP_WWW = 0x01, /**< "http://www." */
|
||||
NFC_URI_HTTPS_WWW = 0x02, /**< "https://www." */
|
||||
NFC_URI_HTTP = 0x03, /**< "http:" */
|
||||
NFC_URI_HTTPS = 0x04, /**< "https:" */
|
||||
NFC_URI_TEL = 0x05, /**< "tel:" */
|
||||
NFC_URI_MAILTO = 0x06, /**< "mailto:" */
|
||||
NFC_URI_FTP_ANONYMOUS = 0x07, /**< "ftp://anonymous:anonymous@" */
|
||||
NFC_URI_FTP_FTP = 0x08, /**< "ftp://ftp." */
|
||||
NFC_URI_FTPS = 0x09, /**< "ftps://" */
|
||||
NFC_URI_SFTP = 0x0A, /**< "sftp://" */
|
||||
NFC_URI_SMB = 0x0B, /**< "smb://" */
|
||||
NFC_URI_NFS = 0x0C, /**< "nfs://" */
|
||||
NFC_URI_FTP = 0x0D, /**< "ftp://" */
|
||||
NFC_URI_DAV = 0x0E, /**< "dav://" */
|
||||
NFC_URI_NEWS = 0x0F, /**< "news:" */
|
||||
NFC_URI_TELNET = 0x10, /**< "telnet://" */
|
||||
NFC_URI_IMAP = 0x11, /**< "imap:" */
|
||||
NFC_URI_RTSP = 0x12, /**< "rtsp://" */
|
||||
NFC_URI_URN = 0x13, /**< "urn:" */
|
||||
NFC_URI_POP = 0x14, /**< "pop:" */
|
||||
NFC_URI_SIP = 0x15, /**< "sip:" */
|
||||
NFC_URI_SIPS = 0x16, /**< "sips:" */
|
||||
NFC_URI_TFTP = 0x17, /**< "tftp:" */
|
||||
NFC_URI_BTSPP = 0x18, /**< "btspp://" */
|
||||
NFC_URI_BTL2CAP = 0x19, /**< "btl2cap://" */
|
||||
NFC_URI_BTGOEP = 0x1A, /**< "btgoep://" */
|
||||
NFC_URI_TCPOBEX = 0x1B, /**< "tcpobex://" */
|
||||
NFC_URI_IRDAOBEX = 0x1C, /**< "irdaobex://" */
|
||||
NFC_URI_FILE = 0x1D, /**< "file://" */
|
||||
NFC_URI_URN_EPC_ID = 0x1E, /**< "urn:epc:id:" */
|
||||
NFC_URI_URN_EPC_TAG = 0x1F, /**< "urn:epc:tag:" */
|
||||
NFC_URI_URN_EPC_PAT = 0x20, /**< "urn:epc:pat:" */
|
||||
NFC_URI_URN_EPC_RAW = 0x21, /**< "urn:epc:raw:" */
|
||||
NFC_URI_URN_EPC = 0x22, /**< "urn:epc:" */
|
||||
NFC_URI_URN_NFC = 0x23, /**< "urn:nfc:" */
|
||||
NFC_URI_RFU = 0xFF /**< No prepending is done. Reserved for future use. */
|
||||
} nfc_uri_id_t;
|
||||
|
||||
/** @brief Function for generating a description of a URI record.
|
||||
*
|
||||
* This function declares and initializes a static instance of an NFC NDEF record description
|
||||
* of a URI record.
|
||||
*
|
||||
* @note The record payload data (@p uri_id_code, @p p_uri_data, and @p
|
||||
* uri_data_len) should be declared as static. If it is declared as
|
||||
* automatic, the NDEF message encoding (see @ref nfc_uri_msg_encode)
|
||||
* must be done in the same variable scope.
|
||||
*
|
||||
* @param[in] uri_id_code URI identifier code that defines the protocol field of the URI.
|
||||
* @param[in] p_uri_data Pointer to the URI string.
|
||||
* The string should not contain the protocol field if the protocol
|
||||
* was specified in @p uri_id_code.
|
||||
* @param[in] uri_data_len Length of the URI string.
|
||||
*
|
||||
* @return Pointer to the description of the record.
|
||||
*/
|
||||
nfc_ndef_record_desc_t * nfc_uri_rec_declare( nfc_uri_id_t uri_id_code,
|
||||
uint8_t const * const p_uri_data,
|
||||
uint8_t uri_data_len);
|
||||
/** @} */
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // NFC_URI_REC_H__
|
||||
Reference in New Issue
Block a user