#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "nfc_hw.h"
#include "t2_emulation.h"
#include "ndef.h"
#define NFC_MEMORY_SIZE 1024
static uint8_t m_nfc_memory[NFC_MEMORY_SIZE] = {0};
#define TXT_HDR_SIZE 0x3
static bool m_field_on;
static bool m_data_written;
static uint32_t process_ndef(void)
{
tlv_msg_t tlv;
uint16_t idx;
ndef_record_t ndef;
int sleep = 1;
int start = 1;
tlv.len = 0;
tlv.offset = 0;
ret = tlv_decode( &m_nfc_memory[T2_HEADER_SIZE],
NFC_MEMORY_SIZE-T2_HEADER_SIZE,
&tlv );
if ((ret == TLV_RES_BLOCK) && (tlv.len))
{
idx=0;
do
{
ndef_res_e ret2 = ndef_parse(
&ndef,
&m_nfc_memory[T2_HEADER_SIZE+tlv.offset+idx]);
sleep = memcmp("sleep",
&m_nfc_memory[T2_HEADER_SIZE+tlv.offset+idx+ndef.payload_offset+TXT_HDR_SIZE],
5);
start = memcmp("start",
&m_nfc_memory[T2_HEADER_SIZE+tlv.offset+idx+ndef.payload_offset+TXT_HDR_SIZE],
5);
idx += ndef.length;
if ((ret2 != NDEF_RES_OK) || (idx > tlv.len))
break;
} while(true);
}
if (!sleep)
{
nfc_hw_wake_from();
}
if(!start)
{
lib_state->startStack();
}
}
static void t2_callback(t2_emu_event_e event)
{
switch (event)
{
case T2_EMU_EVENT_FIELD_ON:
m_field_on = true;
break;
case T2_EMU_EVENT_FIELD_OFF:
m_field_on = false;
break;
case T2_EMU_DATA_WRITTEN:
m_data_written = true;
break;
case T2_EMU_EVENT_SELECTED:
break;
default:
break;
}
if ((!m_field_on) && (m_data_written))
{
m_data_written = false;
}
}
static void fill_t2_with_ndef(uint8_t num)
{
ndef_record_t ndef_msg;
uint16_t ndef_msg_size;
uint16_t tlv_msg_size = 0;
size_t msg_size;
uint8_t ndef_msg_buf[NFC_MEMORY_SIZE];
uint8_t payload[255] = {0x02, 0x65, 0x6e};
ndef_msg.buffer = &ndef_msg_buf[0];
for (uint8_t index=1; index <= num ;index++)
{
strcpy((char*)&payload[TXT_HDR_SIZE], " :THIS IS A NDEF RECORD");
msg_size = strlen( (const char*)&payload[TXT_HDR_SIZE]);
payload[TXT_HDR_SIZE] = 0x30 + index;
ndef_msg_size = ndef_create (
&ndef_msg,
0x01,
(index == 1) ? true : false,
(index == num ) ? true : false,
"T",
1,
(const uint8_t*)payload,
msg_size+TXT_HDR_SIZE );
tlv_msg_size += ndef_msg_size;
ndef_msg.buffer = &ndef_msg_buf[tlv_msg_size];
}
tlv_encode(&ndef_msg_buf[0], tlv_msg_size, &m_nfc_memory[T2_HEADER_SIZE]);
}
{
t2_emu_res_e t2ret;
{
return;
}
nfc_hw_is_nfc_reset_reason();
t2ret = t2_emu_init(t2_callback, &m_nfc_memory[0], NFC_MEMORY_SIZE);
if (t2ret == T2_EMU_RES_OK)
{
fill_t2_with_ndef(3);
nfc_hw_start();
}
}