Astyle formatting

pull/7822/head
Donatien Garnier 2018-08-21 15:19:51 +01:00
parent ce39e77f3d
commit 0e2484f7f3
74 changed files with 5018 additions and 5438 deletions

View File

@ -37,11 +37,10 @@ extern "C" {
#include "stddef.h"
#include "stdbool.h"
typedef struct __ac_buffer
{
const uint8_t* data;
typedef struct __ac_buffer {
const uint8_t *data;
size_t size;
struct __ac_buffer* pNext;
struct __ac_buffer *pNext;
} ac_buffer_t;
/** Initialize ac_buffer using underlying byte array, set ac_buffer's length to 0 (empty)
@ -49,19 +48,19 @@ typedef struct __ac_buffer
* \param data byte array to use
* \param size size of byte array
*/
void ac_buffer_init(ac_buffer_t* pBuf, const uint8_t* data, size_t size);
void ac_buffer_init(ac_buffer_t *pBuf, const uint8_t *data, size_t size);
/** Copy pBufIn to pBuf
* \param pBuf pointer to ac_buffer_t structure to initialize
* \param pBufIn the source buffer
*/
void ac_buffer_dup(ac_buffer_t* pBuf, const ac_buffer_t* pBufIn);
void ac_buffer_dup(ac_buffer_t *pBuf, const ac_buffer_t *pBufIn);
/** Get buffer's underlying byte array
* \param pBuf pointer to ac_buffer_t structure
* \return underlying array
*/
static inline const uint8_t* ac_buffer_data(const ac_buffer_t* pBuf)
static inline const uint8_t *ac_buffer_data(const ac_buffer_t *pBuf)
{
return pBuf->data;
}
@ -70,7 +69,7 @@ static inline const uint8_t* ac_buffer_data(const ac_buffer_t* pBuf)
* \param pBuf pointer to ac_buffer_t structure
* \return buffer's size
*/
static inline size_t ac_buffer_size(const ac_buffer_t* pBuf)
static inline size_t ac_buffer_size(const ac_buffer_t *pBuf)
{
return pBuf->size;
}
@ -79,7 +78,7 @@ static inline size_t ac_buffer_size(const ac_buffer_t* pBuf)
* \param pBuf pointer to ac_buffer_t structure
* \return pointer to next buffer
*/
static inline ac_buffer_t* ac_buffer_next(const ac_buffer_t* pBuf)
static inline ac_buffer_t *ac_buffer_next(const ac_buffer_t *pBuf)
{
return pBuf->pNext;
}
@ -88,16 +87,16 @@ static inline ac_buffer_t* ac_buffer_next(const ac_buffer_t* pBuf)
* \param pBuf pointer to ac_buffer_t structure
* \param pNextBuf pointer to next buffer (or NULL to break chain)
*/
static inline void ac_buffer_set_next(ac_buffer_t* pBuf, ac_buffer_t* pNextBuf)
static inline void ac_buffer_set_next(ac_buffer_t *pBuf, ac_buffer_t *pNextBuf)
{
pBuf->pNext = (ac_buffer_t*) pNextBuf;
pBuf->pNext = (ac_buffer_t *) pNextBuf;
}
/** Append buffer to end of chain
* \param pBuf pointer to ac_buffer_t structure
* \param pAppBuf pointer to buffer to append to chain
*/
void ac_buffer_append(ac_buffer_t* pBuf, ac_buffer_t* pAppBuf);
void ac_buffer_append(ac_buffer_t *pBuf, ac_buffer_t *pAppBuf);
/** Truncate pBuf to length bytes and save the remaining bytes in pEndBuf
* \param pBuf The buffer to split (will be set to invalid state)
@ -105,10 +104,10 @@ void ac_buffer_append(ac_buffer_t* pBuf, ac_buffer_t* pAppBuf);
* \param pEndBuf A new buffer at the tail of the split
* \param length How long pStartBuf should be (if longer than pBuf, then pStartBuf will be pBuf)
*/
void ac_buffer_split(ac_buffer_t* pStartBuf, ac_buffer_t* pEndBuf, ac_buffer_t* pBuf, size_t length);
void ac_buffer_split(ac_buffer_t *pStartBuf, ac_buffer_t *pEndBuf, ac_buffer_t *pBuf, size_t length);
//Debug
void ac_buffer_dump(ac_buffer_t* pBuf);
void ac_buffer_dump(ac_buffer_t *pBuf);
#ifdef __cplusplus
}

View File

@ -39,10 +39,9 @@ extern "C" {
#include "acore/buffer.h"
typedef struct __ac_buffer_builder
{
typedef struct __ac_buffer_builder {
ac_buffer_t ac_buffer;
uint8_t* data;
uint8_t *data;
size_t size;
} ac_buffer_builder_t;
@ -51,14 +50,14 @@ typedef struct __ac_buffer_builder
* \param buf pointer to data
* \param size the data size
*/
void ac_buffer_builder_write_be(ac_buffer_builder_t* pBuilder, const uint8_t* buf, size_t size);
void ac_buffer_builder_write_be(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size);
/** Write data to little endian ac_buffer (on a LE architecture, byte order will be preserved)
* \param pBuilder ac_buffer builder to use
* \param buf pointer to data
* \param size the data size
*/
void ac_buffer_builder_write_le(ac_buffer_builder_t* pBuilder, const uint8_t* buf, size_t size);
void ac_buffer_builder_write_le(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size);
/** Write data to big endian ac_buffer at specific position (on a LE architecture, byte order will be swapped)
* \param pBuilder ac_buffer builder to use
@ -66,7 +65,7 @@ void ac_buffer_builder_write_le(ac_buffer_builder_t* pBuilder, const uint8_t* bu
* \param buf pointer to data
* \param size the data size
*/
void ac_buffer_builder_write_be_at(ac_buffer_builder_t* pBuilder, size_t pos, const uint8_t* buf, size_t size);
void ac_buffer_builder_write_be_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size);
/** Write data to little endian ac_buffer at specific position (on a LE architecture, byte order will be preserved)
* \param pBuilder ac_buffer builder to use
@ -74,35 +73,35 @@ void ac_buffer_builder_write_be_at(ac_buffer_builder_t* pBuilder, size_t pos, co
* \param buf pointer to data
* \param size the data size
*/
void ac_buffer_builder_write_le_at(ac_buffer_builder_t* pBuilder, size_t pos, const uint8_t* buf, size_t size);
void ac_buffer_builder_write_le_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size);
/** Initialize ac_buffer builder
* \param pBuilder ac_buffer builder to init
* \param data pointer to byte array to use
* \param size of byte array
*/
void ac_buffer_builder_init(ac_buffer_builder_t* pBuilder, uint8_t* data, size_t size);
void ac_buffer_builder_init(ac_buffer_builder_t *pBuilder, uint8_t *data, size_t size);
/** Initialize ac_buffer builder from underlying ac_buffer
* \param pBuilder ac_buffer builder to init
*/
void ac_buffer_builder_from_ac_buffer(ac_buffer_builder_t* pBuilder);
void ac_buffer_builder_from_ac_buffer(ac_buffer_builder_t *pBuilder);
/** Reset ac_buffer builder
* \param pBuilder ac_buffer builder to reset
*/
void ac_buffer_builder_reset(ac_buffer_builder_t* pBuilder);
void ac_buffer_builder_reset(ac_buffer_builder_t *pBuilder);
/** Set ac_buffer builder's ac_buffer to full size
* \param pBuilder ac_buffer builder to set to full size
*/
void ac_buffer_builder_set_full(ac_buffer_builder_t* pBuilder);
void ac_buffer_builder_set_full(ac_buffer_builder_t *pBuilder);
/** Get ac_buffer builder's length
* \param pBuilder ac_buffer builder to get length of
* \return number of valid bytes in ac_buffer
*/
static inline size_t ac_buffer_builder_length(ac_buffer_builder_t* pBuilder)
static inline size_t ac_buffer_builder_length(ac_buffer_builder_t *pBuilder)
{
return ac_buffer_size(&pBuilder->ac_buffer);
}
@ -111,10 +110,9 @@ static inline size_t ac_buffer_builder_length(ac_buffer_builder_t* pBuilder)
* \param pBuilder ac_buffer builder to set length of
* \param length number of valid bytes in ac_buffer
*/
static inline void ac_buffer_builder_set_length(ac_buffer_builder_t* pBuilder, size_t length)
static inline void ac_buffer_builder_set_length(ac_buffer_builder_t *pBuilder, size_t length)
{
if( ac_buffer_data(&pBuilder->ac_buffer) + length > pBuilder->data + pBuilder->size )
{
if (ac_buffer_data(&pBuilder->ac_buffer) + length > pBuilder->data + pBuilder->size) {
return;
}
pBuilder->ac_buffer.size = length;
@ -124,16 +122,16 @@ static inline void ac_buffer_builder_set_length(ac_buffer_builder_t* pBuilder, s
* \param pBuilder ac_buffer builder
* \return pointer to write position
*/
static inline uint8_t* ac_buffer_builder_write_position(ac_buffer_builder_t* pBuilder)
static inline uint8_t *ac_buffer_builder_write_position(ac_buffer_builder_t *pBuilder)
{
return (uint8_t*)ac_buffer_data(&pBuilder->ac_buffer) + ac_buffer_size(&pBuilder->ac_buffer);
return (uint8_t *)ac_buffer_data(&pBuilder->ac_buffer) + ac_buffer_size(&pBuilder->ac_buffer);
}
/** Get ac_buffer builder's write offset
* \param pBuilder ac_buffer builder
* \return write offset
*/
static inline size_t ac_buffer_builder_write_offset(ac_buffer_builder_t* pBuilder)
static inline size_t ac_buffer_builder_write_offset(ac_buffer_builder_t *pBuilder)
{
return ac_buffer_data(&pBuilder->ac_buffer) + ac_buffer_size(&pBuilder->ac_buffer) - pBuilder->data;
}
@ -142,18 +140,14 @@ static inline size_t ac_buffer_builder_write_offset(ac_buffer_builder_t* pBuilde
* \param pBuilder ac_buffer builder
* \param off new write offset
*/
static inline void ac_buffer_builder_set_write_offset(ac_buffer_builder_t* pBuilder, size_t off)
static inline void ac_buffer_builder_set_write_offset(ac_buffer_builder_t *pBuilder, size_t off)
{
if( off > pBuilder->size )
{
if (off > pBuilder->size) {
return;
}
if( pBuilder->data + off > pBuilder->ac_buffer.data )
{
if (pBuilder->data + off > pBuilder->ac_buffer.data) {
pBuilder->ac_buffer.size = off - (pBuilder->ac_buffer.data - pBuilder->data);
}
else
{
} else {
pBuilder->ac_buffer.size = 0;
}
}
@ -162,7 +156,7 @@ static inline void ac_buffer_builder_set_write_offset(ac_buffer_builder_t* pBuil
* \param pBuilder ac_buffer builder
* \return read offset
*/
static inline size_t ac_buffer_builder_read_offset(ac_buffer_builder_t* pBuilder)
static inline size_t ac_buffer_builder_read_offset(ac_buffer_builder_t *pBuilder)
{
return ac_buffer_data(&pBuilder->ac_buffer) - pBuilder->data;
}
@ -171,18 +165,14 @@ static inline size_t ac_buffer_builder_read_offset(ac_buffer_builder_t* pBuilder
* \param pBuilder ac_buffer builder
* \param off new read offset
*/
static inline void ac_buffer_builder_set_read_offset(ac_buffer_builder_t* pBuilder, size_t off)
static inline void ac_buffer_builder_set_read_offset(ac_buffer_builder_t *pBuilder, size_t off)
{
if( off > pBuilder->size )
{
if (off > pBuilder->size) {
return;
}
if( pBuilder->data + off < pBuilder->ac_buffer.data + pBuilder->ac_buffer.size )
{
if (pBuilder->data + off < pBuilder->ac_buffer.data + pBuilder->ac_buffer.size) {
pBuilder->ac_buffer.size = pBuilder->ac_buffer.data - (pBuilder->data + off);
}
else
{
} else {
pBuilder->ac_buffer.size = 0;
}
pBuilder->ac_buffer.data = pBuilder->data + off;
@ -192,7 +182,7 @@ static inline void ac_buffer_builder_set_read_offset(ac_buffer_builder_t* pBuild
* \param pBuilder ac_buffer builder
* \return ac_buffer
*/
static inline ac_buffer_t* ac_buffer_builder_buffer(ac_buffer_builder_t* pBuilder)
static inline ac_buffer_t *ac_buffer_builder_buffer(ac_buffer_builder_t *pBuilder)
{
return &pBuilder->ac_buffer;
}
@ -201,7 +191,7 @@ static inline ac_buffer_t* ac_buffer_builder_buffer(ac_buffer_builder_t* pBuilde
* \param pBuilder ac_buffer builder
* \return number of free bytes in ac_buffer builder
*/
static inline size_t ac_buffer_builder_space(ac_buffer_builder_t* pBuilder)
static inline size_t ac_buffer_builder_space(ac_buffer_builder_t *pBuilder)
{
return pBuilder->size - (ac_buffer_data(&pBuilder->ac_buffer) - pBuilder->data + ac_buffer_size(&pBuilder->ac_buffer));
}
@ -210,7 +200,7 @@ static inline size_t ac_buffer_builder_space(ac_buffer_builder_t* pBuilder)
* \param pBuilder ac_buffer builder
* \return true if ac_buffer builder is empty
*/
static inline bool ac_buffer_builder_empty(ac_buffer_builder_t* pBuilder)
static inline bool ac_buffer_builder_empty(ac_buffer_builder_t *pBuilder)
{
return (ac_buffer_builder_length(pBuilder) == 0);
}
@ -219,7 +209,7 @@ static inline bool ac_buffer_builder_empty(ac_buffer_builder_t* pBuilder)
* \param pBuilder ac_buffer builder
* \return true if ac_buffer builder is full
*/
static inline bool ac_buffer_full(ac_buffer_builder_t* pBuilder)
static inline bool ac_buffer_full(ac_buffer_builder_t *pBuilder)
{
return (ac_buffer_builder_space(pBuilder) == 0);
}
@ -228,7 +218,7 @@ static inline bool ac_buffer_full(ac_buffer_builder_t* pBuilder)
* \param pBuilder ac_buffer builder
* \param hu8 8-bit value to write
*/
static inline void ac_buffer_builder_write_nu8(ac_buffer_builder_t* pBuilder, uint8_t hu8)
static inline void ac_buffer_builder_write_nu8(ac_buffer_builder_t *pBuilder, uint8_t hu8)
{
ac_buffer_builder_write_be(pBuilder, &hu8, 1);
}
@ -237,36 +227,36 @@ static inline void ac_buffer_builder_write_nu8(ac_buffer_builder_t* pBuilder, ui
* \param pBuilder ac_buffer builder
* \param hu16 16-bit value to write in big-endian format
*/
static inline void ac_buffer_builder_write_nu16(ac_buffer_builder_t* pBuilder, uint16_t hu16)
static inline void ac_buffer_builder_write_nu16(ac_buffer_builder_t *pBuilder, uint16_t hu16)
{
ac_buffer_builder_write_be(pBuilder, (uint8_t*)&hu16, 2);
ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu16, 2);
}
/** Write 24-bit value in ac_buffer builder
* \param pBuilder ac_buffer builder
* \param hu24 24-bit value to write in big-endian format
*/
static inline void ac_buffer_builder_write_nu24(ac_buffer_builder_t* pBuilder, uint32_t hu24)
static inline void ac_buffer_builder_write_nu24(ac_buffer_builder_t *pBuilder, uint32_t hu24)
{
ac_buffer_builder_write_be(pBuilder, (uint8_t*)&hu24, 3);
ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu24, 3);
}
/** Write 32-bit value in ac_buffer builder
* \param pBuilder ac_buffer builder
* \param hu32 32-bit value to write in big-endian format
*/
static inline void ac_buffer_builder_write_nu32(ac_buffer_builder_t* pBuilder, uint32_t hu32)
static inline void ac_buffer_builder_write_nu32(ac_buffer_builder_t *pBuilder, uint32_t hu32)
{
ac_buffer_builder_write_be(pBuilder, (uint8_t*)&hu32, 4);
ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu32, 4);
}
/** Write 64-bit value in ac_buffer builder
* \param pBuilder ac_buffer builder
* \param hu64 64-bit value to write in big-endian format
*/
static inline void ac_buffer_builder_write_nu64(ac_buffer_builder_t* pBuilder, uint64_t hu64)
static inline void ac_buffer_builder_write_nu64(ac_buffer_builder_t *pBuilder, uint64_t hu64)
{
ac_buffer_builder_write_be(pBuilder, (uint8_t*)&hu64, 8);
ac_buffer_builder_write_be(pBuilder, (uint8_t *)&hu64, 8);
}
/** Write n-bytes value in ac_buffer builder
@ -274,7 +264,7 @@ static inline void ac_buffer_builder_write_nu64(ac_buffer_builder_t* pBuilder, u
* \param data data to write
* \param size data length
*/
static inline void ac_buffer_builder_write_n_bytes(ac_buffer_builder_t* pBuilder, const uint8_t* data, size_t size)
static inline void ac_buffer_builder_write_n_bytes(ac_buffer_builder_t *pBuilder, const uint8_t *data, size_t size)
{
ac_buffer_builder_write_le(pBuilder, data, size);
}
@ -284,7 +274,7 @@ static inline void ac_buffer_builder_write_n_bytes(ac_buffer_builder_t* pBuilder
* \param off offset at which to write
* \param hu8 8-bit value to write
*/
static inline void ac_buffer_builder_write_nu8_at(ac_buffer_builder_t* pBuilder, size_t off, uint8_t hu8)
static inline void ac_buffer_builder_write_nu8_at(ac_buffer_builder_t *pBuilder, size_t off, uint8_t hu8)
{
ac_buffer_builder_write_be_at(pBuilder, off, &hu8, 1);
}
@ -294,9 +284,9 @@ static inline void ac_buffer_builder_write_nu8_at(ac_buffer_builder_t* pBuilder,
* \param off offset at which to write
* \param hu16 16-bit value to write
*/
static inline void ac_buffer_builder_write_nu16_at(ac_buffer_builder_t* pBuilder, size_t off, uint16_t hu16)
static inline void ac_buffer_builder_write_nu16_at(ac_buffer_builder_t *pBuilder, size_t off, uint16_t hu16)
{
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t*)&hu16, 2);
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu16, 2);
}
/** Write 24-bit value in ac_buffer builder at specified position
@ -304,9 +294,9 @@ static inline void ac_buffer_builder_write_nu16_at(ac_buffer_builder_t* pBuilder
* \param off offset at which to write
* \param hu24 24-bit value to write
*/
static inline void ac_buffer_builder_write_nu24_at(ac_buffer_builder_t* pBuilder, size_t off, uint32_t hu24)
static inline void ac_buffer_builder_write_nu24_at(ac_buffer_builder_t *pBuilder, size_t off, uint32_t hu24)
{
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t*)&hu24, 3);
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu24, 3);
}
/** Write 32-bit value in ac_buffer builder at specified position
@ -314,9 +304,9 @@ static inline void ac_buffer_builder_write_nu24_at(ac_buffer_builder_t* pBuilder
* \param off offset at which to write
* \param hu32 32-bit value to write
*/
static inline void ac_buffer_builder_write_nu32_at(ac_buffer_builder_t* pBuilder, size_t off, uint32_t hu32)
static inline void ac_buffer_builder_write_nu32_at(ac_buffer_builder_t *pBuilder, size_t off, uint32_t hu32)
{
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t*)&hu32, 4);
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu32, 4);
}
/** Write 64-bit value in ac_buffer builder at specified position
@ -324,9 +314,9 @@ static inline void ac_buffer_builder_write_nu32_at(ac_buffer_builder_t* pBuilder
* \param off offset at which to write
* \param hu64 64-bit value to write
*/
static inline void ac_buffer_builder_write_nu64_at(ac_buffer_builder_t* pBuilder, size_t off, uint64_t hu64)
static inline void ac_buffer_builder_write_nu64_at(ac_buffer_builder_t *pBuilder, size_t off, uint64_t hu64)
{
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t*)&hu64, 8);
ac_buffer_builder_write_be_at(pBuilder, off, (uint8_t *)&hu64, 8);
}
/** Write n-bytes value in ac_buffer builder at specified position
@ -335,7 +325,7 @@ static inline void ac_buffer_builder_write_nu64_at(ac_buffer_builder_t* pBuilder
* \param data data to write
* \param size data length
*/
static inline void ac_buffer_builder_write_n_bytes_at(ac_buffer_builder_t* pBuilder, size_t off, const uint8_t* data, size_t size)
static inline void ac_buffer_builder_write_n_bytes_at(ac_buffer_builder_t *pBuilder, size_t off, const uint8_t *data, size_t size)
{
ac_buffer_builder_write_be_at(pBuilder, off, data, size);
}
@ -344,26 +334,26 @@ static inline void ac_buffer_builder_write_n_bytes_at(ac_buffer_builder_t* pBuil
* \param pBuilder ac_buffer builder
* \param size number of bytes to skip
*/
void ac_buffer_builder_write_n_skip(ac_buffer_builder_t* pBuilder, size_t size);
void ac_buffer_builder_write_n_skip(ac_buffer_builder_t *pBuilder, size_t size);
/** Copy n bytes from buffer to builder
* \param pBuilderOut ac_buffer builder
* \param pBufIn the input buffer
* \param size number of bytes to copy
*/
void ac_buffer_builder_copy_n_bytes(ac_buffer_builder_t* pBuilderOut, ac_buffer_t* pBufIn, size_t size);
void ac_buffer_builder_copy_n_bytes(ac_buffer_builder_t *pBuilderOut, ac_buffer_t *pBufIn, size_t size);
/** Compact builder
* Will move underlying buffer's byte to start of allocated buffer
* \param pBuilder ac_buffer builder
*/
void ac_buffer_builder_compact(ac_buffer_builder_t* pBuilder);
void ac_buffer_builder_compact(ac_buffer_builder_t *pBuilder);
/** Get number of writable bytes in ac_buffer builder
* \param pBuilder ac_buffer builder
* \return number of free bytes in ac_buffer builder
*/
static inline size_t ac_buffer_builder_writable(ac_buffer_builder_t* pBuilder)
static inline size_t ac_buffer_builder_writable(ac_buffer_builder_t *pBuilder)
{
return ac_buffer_builder_space(pBuilder);
}

View File

@ -44,20 +44,20 @@ extern "C" {
* \param buf the array to write to
* \param size the number of bytes to read
*/
void ac_buffer_read_be(ac_buffer_t* pBuf, uint8_t* buf, size_t size);
void ac_buffer_read_be(ac_buffer_t *pBuf, uint8_t *buf, size_t size);
/** Read n-bytes in little-endian format from buffer reader and advance read posiion
* \param pBuf the buffer to read from
* \param buf the array to write to
* \param size the number of bytes to read
*/
void ac_buffer_read_le(ac_buffer_t* pBuf, uint8_t* buf, size_t size);
void ac_buffer_read_le(ac_buffer_t *pBuf, uint8_t *buf, size_t size);
/** Read 8-bit value from buffer reader and advance read posiion
* \param pBuf the buffer to read from
* \return 8-bit value read
*/
static inline uint8_t ac_buffer_read_nu8(ac_buffer_t* pBuf)
static inline uint8_t ac_buffer_read_nu8(ac_buffer_t *pBuf)
{
uint8_t hu8;
ac_buffer_read_be(pBuf, &hu8, 1);
@ -68,10 +68,10 @@ static inline uint8_t ac_buffer_read_nu8(ac_buffer_t* pBuf)
* \param pBuf the buffer to read from
* \return 16-bit value read
*/
static inline uint16_t ac_buffer_read_nu16(ac_buffer_t* pBuf)
static inline uint16_t ac_buffer_read_nu16(ac_buffer_t *pBuf)
{
uint16_t hu16;
ac_buffer_read_be(pBuf, (uint8_t*)&hu16, 2);
ac_buffer_read_be(pBuf, (uint8_t *)&hu16, 2);
return hu16;
}
@ -79,10 +79,10 @@ static inline uint16_t ac_buffer_read_nu16(ac_buffer_t* pBuf)
* \param pBuf the buffer to read from
* \return 24-bit value read
*/
static inline uint32_t ac_buffer_read_nu24(ac_buffer_t* pBuf)
static inline uint32_t ac_buffer_read_nu24(ac_buffer_t *pBuf)
{
uint32_t hu24;
ac_buffer_read_be(pBuf, (uint8_t*)&hu24, 3);
ac_buffer_read_be(pBuf, (uint8_t *)&hu24, 3);
return hu24;
}
@ -90,10 +90,10 @@ static inline uint32_t ac_buffer_read_nu24(ac_buffer_t* pBuf)
* \param pBuf the buffer to read from
* \return 32-bit value read
*/
static inline uint32_t ac_buffer_read_nu32(ac_buffer_t* pBuf)
static inline uint32_t ac_buffer_read_nu32(ac_buffer_t *pBuf)
{
uint32_t hu32;
ac_buffer_read_be(pBuf, (uint8_t*)&hu32, 4);
ac_buffer_read_be(pBuf, (uint8_t *)&hu32, 4);
return hu32;
}
@ -101,10 +101,10 @@ static inline uint32_t ac_buffer_read_nu32(ac_buffer_t* pBuf)
* \param pBuf the buffer to read from
* \return 64-bit value read
*/
static inline uint64_t ac_buffer_read_nu64(ac_buffer_t* pBuf)
static inline uint64_t ac_buffer_read_nu64(ac_buffer_t *pBuf)
{
uint64_t hu64;
ac_buffer_read_be(pBuf, (uint8_t*)&hu64, 8);
ac_buffer_read_be(pBuf, (uint8_t *)&hu64, 8);
return hu64;
}
@ -113,7 +113,7 @@ static inline uint64_t ac_buffer_read_nu64(ac_buffer_t* pBuf)
* \param data the array to write bytes to
* \param size the number of bytes to read
*/
static inline void ac_buffer_read_n_bytes(ac_buffer_t* pBuf, uint8_t* data, size_t size)
static inline void ac_buffer_read_n_bytes(ac_buffer_t *pBuf, uint8_t *data, size_t size)
{
ac_buffer_read_le(pBuf, data, size);
}
@ -122,25 +122,25 @@ static inline void ac_buffer_read_n_bytes(ac_buffer_t* pBuf, uint8_t* data, size
* \param pBuf the buffer to read from
* \param size the number of bytes to skip
*/
void ac_buffer_read_n_skip(ac_buffer_t* pBuf, size_t size);
void ac_buffer_read_n_skip(ac_buffer_t *pBuf, size_t size);
/** Get number of bytes readable from buffer
* \param pBuf the buffer to read from
* \return The number of bytes which can be read
*/
size_t ac_buffer_reader_readable(const ac_buffer_t* pBuf);
size_t ac_buffer_reader_readable(const ac_buffer_t *pBuf);
/** Get a pointer to the current position within this buffer's current backing array
* \param pBuf the buffer to read from
* \return A pointer to the current position within the current backing array
*/
const uint8_t* ac_buffer_reader_current_buffer_pointer(ac_buffer_t* pBuf);
const uint8_t *ac_buffer_reader_current_buffer_pointer(ac_buffer_t *pBuf);
/** Get the number of bytes readable within the current backing array
* \param pBuf the buffer to read from
* \return The number of bytes readable within the current backing array
*/
size_t ac_buffer_reader_current_buffer_length(ac_buffer_t* pBuf);
size_t ac_buffer_reader_current_buffer_length(ac_buffer_t *pBuf);
/** Compare buffer with array (does not advance read position)
* \param pBuf the buffer to compare from
@ -148,14 +148,14 @@ size_t ac_buffer_reader_current_buffer_length(ac_buffer_t* pBuf);
* \param length the array length
* \return Whether the buffer is AT LEAST as long as the array AND the buffer and array have the same content
*/
bool ac_buffer_reader_cmp_bytes(const ac_buffer_t* pBuf, const uint8_t* bytes, size_t length);
bool ac_buffer_reader_cmp_bytes(const ac_buffer_t *pBuf, const uint8_t *bytes, size_t length);
/** Compare buffer with array (does not advance read position)
* \param pBuf1 the buffer to compare from
* \param pBuf2 the buffer to compare with
* \return Whether the buffers have the same length and content
*/
bool ac_buffer_reader_cmp(const ac_buffer_t* pBuf1, const ac_buffer_t* pBuf2);
bool ac_buffer_reader_cmp(const ac_buffer_t *pBuf1, const ac_buffer_t *pBuf2);
#ifdef __cplusplus
}

View File

@ -39,32 +39,30 @@ typedef struct __ac_ostream ac_ostream_t;
#include "acore/buffer.h"
typedef void (*ac_istream_fn)(ac_buffer_t* pDataIn, bool* pClose, size_t maxLength, void* pUserParam);
typedef void (*ac_ostream_fn)(ac_buffer_t* pDataOut, bool closed, void* pUserParam);
typedef void (*ac_istream_fn)(ac_buffer_t *pDataIn, bool *pClose, size_t maxLength, void *pUserParam);
typedef void (*ac_ostream_fn)(ac_buffer_t *pDataOut, bool closed, void *pUserParam);
//Input stream -- pulled by consumer
struct __ac_istream
{
struct __ac_istream {
ac_istream_fn fn;
void* pUserParam;
void *pUserParam;
};
//Output stream -- pushed by supplier
struct __ac_ostream
{
struct __ac_ostream {
ac_ostream_fn fn;
void* pUserParam;
void *pUserParam;
};
//Called by supplier
void ac_istream_init(ac_istream_t* pac_istream, ac_istream_fn fn, void* pUserParam);
void ac_istream_init(ac_istream_t *pac_istream, ac_istream_fn fn, void *pUserParam);
//Called by consumer
void ac_istream_pull(ac_istream_t* pac_istream, ac_buffer_t* pDataIn, bool* pClose, size_t maxLength);
void ac_istream_pull(ac_istream_t *pac_istream, ac_buffer_t *pDataIn, bool *pClose, size_t maxLength);
//Called by consumer
void ac_ostream_init(ac_ostream_t* pac_ostream, ac_ostream_fn fn, void* pUserParam);
void ac_ostream_init(ac_ostream_t *pac_ostream, ac_ostream_fn fn, void *pUserParam);
//Called by supplier
void ac_ostream_push(ac_ostream_t* pac_ostream, ac_buffer_t* pDataOut, bool closed);
void ac_ostream_push(ac_ostream_t *pac_ostream, ac_buffer_t *pDataOut, bool closed);
#ifdef __cplusplus
}

View File

@ -29,7 +29,7 @@
#include "acore/debug.h"
void ac_buffer_init(ac_buffer_t* pBuf, const uint8_t* data, size_t size)
void ac_buffer_init(ac_buffer_t *pBuf, const uint8_t *data, size_t size)
{
pBuf->data = data;
pBuf->size = size;
@ -37,32 +37,29 @@ void ac_buffer_init(ac_buffer_t* pBuf, const uint8_t* data, size_t size)
pBuf->pNext = NULL;
}
void ac_buffer_dup(ac_buffer_t* pBuf, const ac_buffer_t* pBufIn)
void ac_buffer_dup(ac_buffer_t *pBuf, const ac_buffer_t *pBufIn)
{
if(pBuf != pBufIn)
{
if (pBuf != pBufIn) {
memcpy(pBuf, pBufIn, sizeof(ac_buffer_t));
}
}
void ac_buffer_append(ac_buffer_t* pBuf, ac_buffer_t* pAppBuf)
void ac_buffer_append(ac_buffer_t *pBuf, ac_buffer_t *pAppBuf)
{
while(pBuf->pNext != NULL)
{
while (pBuf->pNext != NULL) {
pBuf = pBuf->pNext;
}
pBuf->pNext = pAppBuf;
}
void ac_buffer_split(ac_buffer_t* pStartBuf, ac_buffer_t* pEndBuf, ac_buffer_t* pBuf, size_t length)
void ac_buffer_split(ac_buffer_t *pStartBuf, ac_buffer_t *pEndBuf, ac_buffer_t *pBuf, size_t length)
{
ac_buffer_dup(pStartBuf, pBuf);
ac_buffer_dup(pEndBuf, pBuf);
ac_buffer_read_n_skip(pEndBuf, length);
while( length > ac_buffer_size(pStartBuf) )
{
while (length > ac_buffer_size(pStartBuf)) {
length -= pStartBuf->size;
pStartBuf = pStartBuf->pNext;
}
@ -74,27 +71,23 @@ void ac_buffer_split(ac_buffer_t* pStartBuf, ac_buffer_t* pEndBuf, ac_buffer_t*
/** Dump a ac_buffer's content to stdout (useful for debugging)
* \param pBuf pointer to ac_buffer_t structure
*/
void ac_buffer_dump(ac_buffer_t* pBuf)
void ac_buffer_dump(ac_buffer_t *pBuf)
{
#if !defined(NDEBUG)
ACORE_STDIO_LOCK();
while(pBuf != NULL)
{
while (pBuf != NULL) {
size_t r = ac_buffer_size(pBuf);
size_t i = 0;
size_t j = 0;
while(i < r)
{
for(j = i; j < MIN(i + 16, r); j++)
{
while (i < r) {
for (j = i; j < MIN(i + 16, r); j++) {
ACORE_STDIO_PRINT("%02x ", ac_buffer_data(pBuf)[j]);
}
ACORE_STDIO_PRINT("\r\n");
i = j;
}
pBuf = ac_buffer_next(pBuf);
if(pBuf != NULL)
{
if (pBuf != NULL) {
ACORE_STDIO_PRINT("->\r\n");
}
}

View File

@ -29,36 +29,35 @@
#define VOID
#define ENSURE_WRITE_LENGTH(pBuilder, n) do{ if( ac_buffer_builder_space(pBuilder) < n ) { return; } }while(0);
void ac_buffer_builder_init(ac_buffer_builder_t* pBuilder, uint8_t* data, size_t size)
void ac_buffer_builder_init(ac_buffer_builder_t *pBuilder, uint8_t *data, size_t size)
{
pBuilder->data = data;
pBuilder->size = size;
ac_buffer_init(&pBuilder->ac_buffer, data, 0);
}
void ac_buffer_builder_from_ac_buffer(ac_buffer_builder_t* pBuilder)
void ac_buffer_builder_from_ac_buffer(ac_buffer_builder_t *pBuilder)
{
pBuilder->data = (uint8_t*)pBuilder->ac_buffer.data;
pBuilder->data = (uint8_t *)pBuilder->ac_buffer.data;
pBuilder->size = pBuilder->ac_buffer.size;
pBuilder->ac_buffer.size = 0;
}
void ac_buffer_builder_reset(ac_buffer_builder_t* pBuilder)
void ac_buffer_builder_reset(ac_buffer_builder_t *pBuilder)
{
ac_buffer_init(&pBuilder->ac_buffer, pBuilder->data, 0);
}
void ac_buffer_builder_set_full(ac_buffer_builder_t* pBuilder)
void ac_buffer_builder_set_full(ac_buffer_builder_t *pBuilder)
{
ac_buffer_init(&pBuilder->ac_buffer, pBuilder->data, pBuilder->size);
}
void ac_buffer_builder_write_be(ac_buffer_builder_t* pBuilder, const uint8_t* buf, size_t size)
void ac_buffer_builder_write_be(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size)
{
ENSURE_WRITE_LENGTH(pBuilder, size);
buf += size;
while(size > 0)
{
while (size > 0) {
buf--;
*ac_buffer_builder_write_position(pBuilder) = *buf;
pBuilder->ac_buffer.size++;
@ -66,14 +65,14 @@ void ac_buffer_builder_write_be(ac_buffer_builder_t* pBuilder, const uint8_t* bu
}
}
void ac_buffer_builder_write_le(ac_buffer_builder_t* pBuilder, const uint8_t* buf, size_t size)
void ac_buffer_builder_write_le(ac_buffer_builder_t *pBuilder, const uint8_t *buf, size_t size)
{
ENSURE_WRITE_LENGTH(pBuilder, size);
memcpy(ac_buffer_builder_write_position(pBuilder), buf, size);
pBuilder->ac_buffer.size+=size;
pBuilder->ac_buffer.size += size;
}
void ac_buffer_builder_write_be_at(ac_buffer_builder_t* pBuilder, size_t pos, const uint8_t* buf, size_t size)
void ac_buffer_builder_write_be_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size)
{
size_t currentPos = pBuilder->ac_buffer.size;
pBuilder->ac_buffer.size = pos;
@ -81,7 +80,7 @@ void ac_buffer_builder_write_be_at(ac_buffer_builder_t* pBuilder, size_t pos, co
pBuilder->ac_buffer.size = currentPos;
}
void ac_buffer_builder_write_le_at(ac_buffer_builder_t* pBuilder, size_t pos, const uint8_t* buf, size_t size)
void ac_buffer_builder_write_le_at(ac_buffer_builder_t *pBuilder, size_t pos, const uint8_t *buf, size_t size)
{
size_t currentPos = pBuilder->ac_buffer.size;
pBuilder->ac_buffer.size = pos;
@ -89,21 +88,19 @@ void ac_buffer_builder_write_le_at(ac_buffer_builder_t* pBuilder, size_t pos, co
pBuilder->ac_buffer.size = currentPos;
}
void ac_buffer_builder_write_n_skip(ac_buffer_builder_t* pBuilder, size_t size)
void ac_buffer_builder_write_n_skip(ac_buffer_builder_t *pBuilder, size_t size)
{
ENSURE_WRITE_LENGTH(pBuilder, size);
pBuilder->ac_buffer.size += size;
}
void ac_buffer_builder_copy_n_bytes(ac_buffer_builder_t* pBuilderOut, ac_buffer_t* pBufIn, size_t size)
void ac_buffer_builder_copy_n_bytes(ac_buffer_builder_t *pBuilderOut, ac_buffer_t *pBufIn, size_t size)
{
ENSURE_WRITE_LENGTH(pBuilderOut, size);
if( ac_buffer_reader_readable(pBufIn) < size )
{
if (ac_buffer_reader_readable(pBufIn) < size) {
return;
}
while(size > 0)
{
while (size > 0) {
size_t cpy = ac_buffer_reader_current_buffer_length(pBufIn);
cpy = MIN(cpy, size);
ac_buffer_builder_write_n_bytes(pBuilderOut, ac_buffer_reader_current_buffer_pointer(pBufIn), cpy);
@ -112,7 +109,7 @@ void ac_buffer_builder_copy_n_bytes(ac_buffer_builder_t* pBuilderOut, ac_buffer_
}
}
void ac_buffer_builder_compact(ac_buffer_builder_t* pBuilder)
void ac_buffer_builder_compact(ac_buffer_builder_t *pBuilder)
{
memmove(pBuilder->data, ac_buffer_data(&pBuilder->ac_buffer), ac_buffer_size(&pBuilder->ac_buffer));
pBuilder->ac_buffer.data = pBuilder->data;

View File

@ -28,33 +28,26 @@
#define VOID
#define ENSURE_READ_LENGTH(pBuf, n) do{ if( ac_buffer_reader_readable(pBuf) < n ) { return; } }while(0);
static inline void update_buf(ac_buffer_t* pBuf)
static inline void update_buf(ac_buffer_t *pBuf)
{
while( ac_buffer_size(pBuf) == 0 )
{
if( ac_buffer_next(pBuf) != NULL )
{
ac_buffer_t* pNext = ac_buffer_next(pBuf);
while (ac_buffer_size(pBuf) == 0) {
if (ac_buffer_next(pBuf) != NULL) {
ac_buffer_t *pNext = ac_buffer_next(pBuf);
ac_buffer_init(pBuf, ac_buffer_data(pNext), ac_buffer_size(pNext));
pBuf->pNext = ac_buffer_next(pNext);
}
else if( pBuf->data != NULL )
{
} else if (pBuf->data != NULL) {
ac_buffer_init(pBuf, NULL, 0);
}
else
{
} else {
return;
}
}
}
void ac_buffer_read_be(ac_buffer_t* pBuf, uint8_t* buf, size_t size)
void ac_buffer_read_be(ac_buffer_t *pBuf, uint8_t *buf, size_t size)
{
ENSURE_READ_LENGTH(pBuf, size);
buf += size;
while(size > 0)
{
while (size > 0) {
buf--;
*buf = *ac_buffer_data(pBuf);
pBuf->data++;
@ -64,80 +57,73 @@ void ac_buffer_read_be(ac_buffer_t* pBuf, uint8_t* buf, size_t size)
}
}
void ac_buffer_read_le(ac_buffer_t* pBuf, uint8_t* buf, size_t size)
void ac_buffer_read_le(ac_buffer_t *pBuf, uint8_t *buf, size_t size)
{
ENSURE_READ_LENGTH(pBuf, size);
while(size > 0)
{
while (size > 0) {
size_t cpy = ac_buffer_size(pBuf);
cpy = MIN(cpy, size);
memcpy(buf, ac_buffer_data(pBuf), cpy);
pBuf->data+=cpy;
pBuf->size-=cpy;
pBuf->data += cpy;
pBuf->size -= cpy;
update_buf(pBuf);
size-=cpy;
buf+=cpy;
size -= cpy;
buf += cpy;
}
}
void ac_buffer_read_n_skip(ac_buffer_t* pBuf, size_t size)
void ac_buffer_read_n_skip(ac_buffer_t *pBuf, size_t size)
{
ENSURE_READ_LENGTH(pBuf, size);
while(size > 0)
{
while (size > 0) {
size_t cpy = ac_buffer_size(pBuf);
cpy = MIN(cpy, size);
pBuf->data+=cpy;
pBuf->size-=cpy;
pBuf->data += cpy;
pBuf->size -= cpy;
update_buf(pBuf);
size-=cpy;
size -= cpy;
}
}
size_t ac_buffer_reader_readable(const ac_buffer_t* pBuf)
size_t ac_buffer_reader_readable(const ac_buffer_t *pBuf)
{
size_t r = 0;
while( pBuf != NULL )
{
while (pBuf != NULL) {
r += ac_buffer_size(pBuf);
pBuf = ac_buffer_next(pBuf);
}
return r;
}
const uint8_t* ac_buffer_reader_current_buffer_pointer(ac_buffer_t* pBuf)
const uint8_t *ac_buffer_reader_current_buffer_pointer(ac_buffer_t *pBuf)
{
update_buf(pBuf);
return ac_buffer_data(pBuf);
}
size_t ac_buffer_reader_current_buffer_length(ac_buffer_t* pBuf)
size_t ac_buffer_reader_current_buffer_length(ac_buffer_t *pBuf)
{
update_buf(pBuf);
return ac_buffer_size(pBuf);
}
bool ac_buffer_reader_cmp_bytes(const ac_buffer_t* pBuf, const uint8_t* bytes, size_t length)
bool ac_buffer_reader_cmp_bytes(const ac_buffer_t *pBuf, const uint8_t *bytes, size_t length)
{
ac_buffer_t reader;
if( length > ac_buffer_reader_readable(pBuf) )
{
if (length > ac_buffer_reader_readable(pBuf)) {
return false;
}
ac_buffer_dup(&reader, pBuf);
while(length > 0)
{
while (length > 0) {
size_t sz = ac_buffer_reader_current_buffer_length(&reader);
if( sz > length )
{
if (sz > length) {
sz = length;
}
int c = memcmp(ac_buffer_reader_current_buffer_pointer(&reader), bytes, sz);
if(c)
{
if (c) {
return false;
}
length -= sz;
@ -148,13 +134,12 @@ bool ac_buffer_reader_cmp_bytes(const ac_buffer_t* pBuf, const uint8_t* bytes, s
return true;
}
bool ac_buffer_reader_cmp(const ac_buffer_t* pBuf1, const ac_buffer_t* pBuf2)
bool ac_buffer_reader_cmp(const ac_buffer_t *pBuf1, const ac_buffer_t *pBuf2)
{
ac_buffer_t reader1;
ac_buffer_t reader2;
if( ac_buffer_reader_readable(pBuf1) != ac_buffer_reader_readable(pBuf2) )
{
if (ac_buffer_reader_readable(pBuf1) != ac_buffer_reader_readable(pBuf2)) {
return false;
}
@ -162,16 +147,14 @@ bool ac_buffer_reader_cmp(const ac_buffer_t* pBuf1, const ac_buffer_t* pBuf2)
ac_buffer_dup(&reader2, pBuf2);
size_t length = ac_buffer_reader_readable(pBuf1);
while(length > 0)
{
while (length > 0) {
size_t sz1 = ac_buffer_reader_current_buffer_length(&reader1);
size_t sz2 = ac_buffer_reader_current_buffer_length(&reader2);
size_t sz = MIN(sz1, sz2);
int c = memcmp(ac_buffer_reader_current_buffer_pointer(&reader1), ac_buffer_reader_current_buffer_pointer(&reader2), sz);
if(c)
{
if (c) {
return false;
}
length -= sz;

View File

@ -24,27 +24,27 @@
#include "acore/macros.h"
//Called by supplier
void ac_istream_init(ac_istream_t* pac_istream, ac_istream_fn fn, void* pUserParam)
void ac_istream_init(ac_istream_t *pac_istream, ac_istream_fn fn, void *pUserParam)
{
pac_istream->fn = fn;
pac_istream->pUserParam = pUserParam;
}
//Called by consumer
void ac_istream_pull(ac_istream_t* pac_istream, ac_buffer_t* pDataIn, bool* pClose, size_t maxLength)
void ac_istream_pull(ac_istream_t *pac_istream, ac_buffer_t *pDataIn, bool *pClose, size_t maxLength)
{
pac_istream->fn(pDataIn, pClose, maxLength, pac_istream->pUserParam);
}
//Called by consumer
void ac_ostream_init(ac_ostream_t* pac_ostream, ac_ostream_fn fn, void* pUserParam)
void ac_ostream_init(ac_ostream_t *pac_ostream, ac_ostream_fn fn, void *pUserParam)
{
pac_ostream->fn = fn;
pac_ostream->pUserParam = pUserParam;
}
//Called by supplier
void ac_ostream_push(ac_ostream_t* pac_ostream, ac_buffer_t* pDataOut, bool closed)
void ac_ostream_push(ac_ostream_t *pac_ostream, ac_buffer_t *pDataOut, bool closed)
{
pac_ostream->fn(pDataOut, closed, pac_ostream->pUserParam);
}

View File

@ -25,21 +25,21 @@
namespace mbed {
namespace nfc {
struct PN512TransportDriver;
class PN512Driver : public NFCControllerDriver, private PN512TransportDriver::Delegate {
public:
PN512Driver(PN512TransportDriver* transport_driver);
struct PN512TransportDriver;
class PN512Driver : public NFCControllerDriver, private PN512TransportDriver::Delegate {
public:
PN512Driver(PN512TransportDriver *transport_driver);
virtual nfc_transceiver_t* initialize(nfc_scheduler_timer_t* scheduler_timer);
virtual void get_supported_nfc_techs(nfc_tech_t* initiator, nfc_tech_t* target) const;
virtual nfc_transceiver_t *initialize(nfc_scheduler_timer_t *scheduler_timer);
virtual void get_supported_nfc_techs(nfc_tech_t *initiator, nfc_tech_t *target) const;
private:
private:
// PN512TransportDriver::Delegate implementation
void on_hw_interrupt();
PN512TransportDriver* _transport_driver;
PN512TransportDriver *_transport_driver;
pn512_t _pn512;
};
};
} // namespace nfc
} // namespace mbed

View File

@ -27,27 +27,27 @@
namespace mbed {
namespace nfc {
class PN512SPITransportDriver : public PN512TransportDriver {
public:
class PN512SPITransportDriver : public PN512TransportDriver {
public:
PN512SPITransportDriver(PinName mosi, PinName miso, PinName sclk, PinName ssel, PinName irq, PinName rst);
private:
private:
virtual void initialize();
virtual nfc_transport_t* get_transport() const;
virtual nfc_transport_t *get_transport() const;
void transport_write( uint8_t address, const uint8_t* outBuf, size_t outLen );
void transport_read( uint8_t address, uint8_t* inBuf, size_t inLen );
void transport_write(uint8_t address, const uint8_t *outBuf, size_t outLen);
void transport_read(uint8_t address, uint8_t *inBuf, size_t inLen);
// Callbacks from munfc
static void s_transport_write( uint8_t address, const uint8_t* outBuf, size_t outLen, void* pUser );
static void s_transport_read( uint8_t address, uint8_t* inBuf, size_t inLen, void* pUser );
static void s_transport_write(uint8_t address, const uint8_t *outBuf, size_t outLen, void *pUser);
static void s_transport_read(uint8_t address, uint8_t *inBuf, size_t inLen, void *pUser);
nfc_transport_t _nfc_transport;
SPI _spi;
DigitalOut _ssel;
InterruptIn _irq;
DigitalOut _rst;
};
};
} // namespace nfc
} // namespace mbed

View File

@ -23,11 +23,11 @@
namespace mbed {
namespace nfc {
/**
/**
* The PN512 supports multiple transport mechanisms (SPI, I2C, UART): this class provides a unified API across these transports
*/
class PN512TransportDriver {
public:
class PN512TransportDriver {
public:
/**
* The PN512TransportDriver delegate
*/
@ -53,24 +53,24 @@ namespace nfc {
*
* @return a pointer to a nfc_transport_t struct
*/
virtual nfc_transport_t* get_transport() const = 0;
virtual nfc_transport_t *get_transport() const = 0;
/**
* Set this instance's delegate
*
* @param[in] delegate the delegate instance to use
*/
void set_delegate(Delegate* delegate);
protected:
void set_delegate(Delegate *delegate);
protected:
/**
* An implementation must call this function (can be called from interrupt context)
* when the PN512 asserts its interrupt line
*/
void hw_interrupt();
private:
Delegate* _delegate;
};
private:
Delegate *_delegate;
};
} // namespace nfc
} // namespace mbed

View File

@ -25,17 +25,17 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
class Type4RemoteInitiator;
class Type4RemoteInitiator;
/**
/**
* This base class represents an ISO7816-4 application.
*/
class ISO7816App {
class ISO7816App {
/**
* This class describes an ISO7816-4 exchange (Command/Response).
*/
@ -44,8 +44,7 @@ namespace nfc {
/**
* A Command APDU (Application Protocol Data Unit) .
*/
struct CAPDU
{
struct CAPDU {
uint8_t cla; ///< CLA - Instruction class
uint8_t ins; ///< INS - Instruction code
uint8_t p1; ///< P1 - First parameter
@ -57,8 +56,7 @@ namespace nfc {
/**
* A Response APDU (Application Protocol Data Unit)
*/
struct RAPDU
{
struct RAPDU {
ac_buffer_t dataOut; ///< Response data
uint16_t sw; ///< Status word
};
@ -68,14 +66,14 @@ namespace nfc {
*
* @return a const reference to the Command ADPU
*/
const CAPDU& command() const;
const CAPDU &command() const;
/**
* Access Response APDU.
*
* @return a mutable reference to the Command ADPU
*/
RAPDU& response();
RAPDU &response();
/**
* Respond to command.
@ -87,7 +85,7 @@ namespace nfc {
private:
CAPDU _command;
RAPDU _response;
ISO7816App* _iso7816_app;
ISO7816App *_iso7816_app;
};
/**
@ -95,7 +93,7 @@ namespace nfc {
*/
ISO7816App();
private:
private:
friend class Type4RemoteInitiator;
/**
@ -104,7 +102,7 @@ namespace nfc {
*
* @return a pointer to a const buffer containing the application's identifier (AID).
*/
virtual const ac_buffer_t* get_aid() const = 0;
virtual const ac_buffer_t *get_aid() const = 0;
/**
* Called when the application is selected and before any exchange is performed.
@ -122,12 +120,12 @@ namespace nfc {
*
* @param[in] exchange an instance of the Exchange class populated with the C-APDU which was received
*/
virtual void on_exchange(Exchange* exchange) = 0;
virtual void on_exchange(Exchange *exchange) = 0;
nfc_tech_iso7816_app_t iso7816_app;
};
};
/**
/**
* @}
*/

View File

@ -27,16 +27,16 @@
namespace mbed {
namespace nfc {
class NFCRemoteInitiator;
class NFCRemoteTarget;
class NFCControllerDriver;
class NFCRemoteInitiator;
class NFCRemoteTarget;
class NFCControllerDriver;
/**
/**
* @addtogroup nfc
* @{
*/
/**
/**
* This class represents a NFC Controller.
*
* A controller can be in one of three different states:
@ -47,8 +47,8 @@ namespace nfc {
* A NFCController instance needs to be initialized with a NFCControllerDriver instance which abstracts the specific controller being used.
* A delegate needs to be set by the user to receive discovery events.
*/
class NFCController : private NFCControllerDriver::Delegate {
public:
class NFCController : private NFCControllerDriver::Delegate {
public:
/**
* The NFCController delegate. Users of the NFCController class need to implement this delegate's methods to receive events.
@ -73,13 +73,13 @@ namespace nfc {
* A remote initiator was discovered (the local controller is in target mode).
* @param[in] the NFCRemoteInitiator instance
*/
virtual void on_nfc_initiator_discovered(const SharedPtr<NFCRemoteInitiator>& nfc_initiator) {}
virtual void on_nfc_initiator_discovered(const SharedPtr<NFCRemoteInitiator> &nfc_initiator) {}
/**
* A remote target was discovered (the local controller is in initiator mode).
* @param[in] the NFCRemoteTarget instance
*/
virtual void on_nfc_target_discovered(const SharedPtr<NFCRemoteTarget>& nfc_target) {}
virtual void on_nfc_target_discovered(const SharedPtr<NFCRemoteTarget> &nfc_target) {}
};
/**
@ -90,7 +90,7 @@ namespace nfc {
* @param[in] ndef_buffer a bytes array used to store NDEF messages
* @param[in] ndef_buffer_sz the array size in bytes
*/
NFCController(NFCControllerDriver* driver, events::EventQueue* queue, uint8_t* ndef_buffer, size_t ndef_buffer_sz);
NFCController(NFCControllerDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz);
/**
* Initialize the NFC controller
@ -106,7 +106,7 @@ namespace nfc {
*
* @oaram[in] delegate the delegate instance to use
*/
void set_delegate(Delegate* delegate);
void set_delegate(Delegate *delegate);
/**
* Get the list of RF protocols supported by this controller.
@ -142,18 +142,18 @@ namespace nfc {
*/
nfc_err_t cancel_discovery();
private:
private:
friend class NFCRemoteEndpoint;
friend class NFCRemoteInitiator;
friend class Type4RemoteInitiator;
nfc_transceiver_t* transceiver() const;
nfc_transceiver_t *transceiver() const;
void polling_callback(nfc_err_t ret);
// NFC Stack scheduler
void scheduler_process(bool hw_interrupt);
// Callbacks from NFC stack
static void s_polling_callback(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void* pUserData);
static void s_polling_callback(nfc_transceiver_t *pTransceiver, nfc_err_t ret, void *pUserData);
// Implementation of NFCControllerDriver::Delegate
void on_hw_interrupt();
@ -161,19 +161,19 @@ namespace nfc {
// Triggers when scheduler must be run again
void on_timeout();
NFCControllerDriver* _driver;
events::EventQueue* _queue;
nfc_transceiver_t* _transceiver;
nfc_scheduler_t* _scheduler;
NFCControllerDriver *_driver;
events::EventQueue *_queue;
nfc_transceiver_t *_transceiver;
nfc_scheduler_t *_scheduler;
Timer _timer;
Timeout _timeout;
Delegate* _delegate;
Delegate *_delegate;
bool _discovery_running;
uint8_t* _ndef_buffer;
uint8_t *_ndef_buffer;
size_t _ndef_buffer_sz;
};
};
/**
/**
* @}
*/

View File

@ -27,16 +27,16 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
/**
/**
* The abstraction for a NFC controller driver.
* Implementers need to derive from this class and implement its methods.
*/
class NFCControllerDriver {
class NFCControllerDriver {
/**
* Instantiate a NFCControllerDriver
*/
@ -58,33 +58,33 @@ namespace nfc {
* @param[in] scheduler_timer a timer to initialize the controller's scheduler instance with
* @return an initialized MicroNFC nfc_transceiver_t instance
*/
virtual nfc_transceiver_t* initialize(nfc_scheduler_timer_t* scheduler_timer) = 0;
virtual nfc_transceiver_t *initialize(nfc_scheduler_timer_t *scheduler_timer) = 0;
/**
* Retrieve list of technologies supported by the controller
* @param[out] initiator bitmask of technologies supported when the controller is in initiator mode
* @param[out] target bitmask of technologies supported when the controller is in target mode
*/
virtual void get_supported_nfc_techs(nfc_tech_t* initiator, nfc_tech_t* target) const = 0;
virtual void get_supported_nfc_techs(nfc_tech_t *initiator, nfc_tech_t *target) const = 0;
/**
* Set this instance's delegate
*
* @param[in] delegate the delegate instance to use
*/
void set_delegate(Delegate* delegate);
protected:
void set_delegate(Delegate *delegate);
protected:
/**
* An implementation must call this function (can be called from interrupt context)
* when the controller asserts its interrupt line
*/
void hw_interrupt();
private:
Delegate* _delegate;
};
private:
Delegate *_delegate;
};
/**
/**
* @}
*/

View File

@ -24,8 +24,7 @@
namespace mbed {
namespace nfc {
struct nfc_rf_protocols_bitmask_t
{
struct nfc_rf_protocols_bitmask_t {
uint8_t initiator_t1t : 1;
uint8_t initiator_t2t : 1;
uint8_t initiator_t3t : 1;
@ -39,16 +38,16 @@ namespace nfc {
uint8_t target_iso_dep : 1;
uint8_t target_nfc_dep : 1;
uint8_t target_t5t : 1;
};
};
enum nfc_tag_type_t {
enum nfc_tag_type_t {
nfc_tag_type_1,
nfc_tag_type_2,
nfc_tag_type_3,
nfc_tag_type_4a,
nfc_tag_type_4b,
nfc_tag_type_5
};
};
} // namespace nfc
} // namespace mbed

View File

@ -26,12 +26,12 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
/**
/**
* The NFC EEPROM class represents a NFC target device connected using a wired
* link (I2C, SPI, etc).
*
@ -42,8 +42,8 @@ namespace nfc {
* by the NFC Forum, therefore encoding NDEF data in these EEPROMs will
* ensure that it is understandable by a NFC reader.
*/
class NFCEEPROM : public NFCTarget, public NFCEEPROMDriver::Delegate {
public:
class NFCEEPROM : public NFCTarget, public NFCEEPROMDriver::Delegate {
public:
/**
* Construct a NFCEEPROM instance.
*
@ -52,7 +52,7 @@ namespace nfc {
* @param[in] ndef_buffer a bytes array used to store NDEF messages
* @param[in] ndef_buffer_sz the array size in bytes
*/
NFCEEPROM(NFCEEPROMDriver* driver, events::EventQueue* queue, uint8_t* ndef_buffer, size_t ndef_buffer_sz);
NFCEEPROM(NFCEEPROMDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz);
/**
* The NFCEEPROM delegate. Users of the NFCEEPROM class need to implement this delegate's methods to receive events.
@ -74,9 +74,9 @@ namespace nfc {
*
* @oaram[in] delegate the delegate instance to use
*/
void set_delegate(Delegate* delegate);
void set_delegate(Delegate *delegate);
private:
private:
// Implementation of NFCTarget
virtual void write_ndef_message();
virtual void read_ndef_message();
@ -116,8 +116,8 @@ namespace nfc {
nfc_eeprom_erase_end_session
};
NFCEEPROMDriver* _driver;
events::EventQueue* _queue;
NFCEEPROMDriver *_driver;
events::EventQueue *_queue;
bool _initialized;
nfc_eeprom_operation_t _current_op;
@ -125,9 +125,9 @@ namespace nfc {
size_t _ndef_buffer_read_sz;
uint32_t _eeprom_address;
nfc_err_t _operation_result;
};
};
/**
/**
* @}
*/

View File

@ -26,17 +26,17 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
/**
/**
* The abstraction for a NFC EEPROM driver.
* Implementers need to derive from this class and implement its methods.
*/
class NFCEEPROMDriver {
public:
class NFCEEPROMDriver {
public:
/**
* Construct a NFCEEPROM driver instance.
*/
@ -110,7 +110,7 @@ namespace nfc {
*
* @oaram[in] delegate the delegate instance to use
*/
void set_delegate(Delegate* delegate);
void set_delegate(Delegate *delegate);
/**
* Reset and initialize the EEPROM.
@ -150,7 +150,7 @@ namespace nfc {
* @oaram[in] count the number of bytes to read.
* This method should complete asynchronously by calling has_read_bytes().
*/
virtual void read_bytes(uint32_t address, uint8_t* bytes, size_t count) = 0;
virtual void read_bytes(uint32_t address, uint8_t *bytes, size_t count) = 0;
/**
* Write bytes to memory.
@ -160,7 +160,7 @@ namespace nfc {
* @oaram[in] count the number of bytes to write.
* This method should complete asynchronously by calling has_written_bytes().
*/
virtual void write_bytes(uint32_t address, const uint8_t* bytes, size_t count) = 0;
virtual void write_bytes(uint32_t address, const uint8_t *bytes, size_t count) = 0;
/**
* Retrieve the size of the addressable memory.
@ -183,14 +183,14 @@ namespace nfc {
*/
virtual void erase_bytes(uint32_t address, size_t size) = 0;
protected:
Delegate* delegate();
protected:
Delegate *delegate();
private:
Delegate* _delegate;
};
private:
Delegate *_delegate;
};
/**
/**
* @}
*/

View File

@ -24,29 +24,32 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
/**
/**
* The base class for all endpoints that can support NDEF content.
*/
class NFCNDEFCapable {
public:
class NFCNDEFCapable {
public:
/**
* Construct a NFCNDEFCapable instance.
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
NFCNDEFCapable(uint8_t* buffer, size_t buffer_size);
NFCNDEFCapable(uint8_t *buffer, size_t buffer_size);
/**
* Check if this instance actually supports NDEF content.
*
* @return whether NDEF content is supported
*/
virtual bool is_ndef_supported() const { return false; }
virtual bool is_ndef_supported() const
{
return false;
}
struct Delegate {
/**
@ -55,7 +58,7 @@ namespace nfc {
* @param[in] buffer a buffer containing the message to parse
* @param[in] size the buffer's size
*/
virtual void parse_ndef_message(const uint8_t* buffer, size_t size) { }
virtual void parse_ndef_message(const uint8_t *buffer, size_t size) { }
/**
* Build a NDEF message.
@ -64,7 +67,10 @@ namespace nfc {
* @param[in] capacity the buffer's capacity
* @return the number of bytes actually used
*/
virtual size_t build_ndef_message(uint8_t* buffer, size_t capacity) { return 0; }
virtual size_t build_ndef_message(uint8_t *buffer, size_t capacity)
{
return 0;
}
};
/**
@ -72,15 +78,15 @@ namespace nfc {
*
* @oaram[in] delegate the delegate instance to use
*/
void set_ndef_delegate(Delegate* delegate);
void set_ndef_delegate(Delegate *delegate);
protected:
protected:
/**
* Parse a NDEF message.
*
* @param[in] a buffer containing a NDEF message
*/
void parse_ndef_message(const ac_buffer_t& buffer);
void parse_ndef_message(const ac_buffer_t &buffer);
/**
* Build NDEF message.
@ -88,27 +94,27 @@ namespace nfc {
* @param[in,out] a buffer builder in which to create the NDEF message.
* The backing buffer is guaranteed to be continuous.
*/
void build_ndef_message(ac_buffer_builder_t& buffer_builder);
void build_ndef_message(ac_buffer_builder_t &buffer_builder);
/**
* Retrieve underlying NDEF message instance
* @return pointer to NDEF message instance
*/
ndef_msg_t* ndef_message();
ndef_msg_t *ndef_message();
private:
private:
// Callbacks from NDEF stack
static nfc_err_t s_ndef_encode(ndef_msg_t* pTag, buffer_builder_t* pBufferBldr, void* pUserData);
static nfc_err_t s_ndef_decode(ndef_msg_t* pTag, buffer_t* pBuffer, void* pUserData);
nfc_err_t ndef_encode(buffer_builder_t* pBufferBldr);
nfc_err_t ndef_decode(buffer_t* pBuffer);
static nfc_err_t s_ndef_encode(ndef_msg_t *pTag, buffer_builder_t *pBufferBldr, void *pUserData);
static nfc_err_t s_ndef_decode(ndef_msg_t *pTag, buffer_t *pBuffer, void *pUserData);
nfc_err_t ndef_encode(buffer_builder_t *pBufferBldr);
nfc_err_t ndef_decode(buffer_t *pBuffer);
Delegate* _delegate;
Delegate *_delegate;
ndef_msg_t _ndef_message;
};
};
/**
/**
* @}
*/

View File

@ -24,19 +24,19 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
class NFCController;
class NFCController;
/**
/**
* This is the base class for all remote endpoints (initiators and targets)
* addressable over the air interface.
*/
class NFCRemoteEndpoint {
public:
class NFCRemoteEndpoint {
public:
NFCRemoteEndpoint();
/**
@ -86,7 +86,7 @@ namespace nfc {
*/
virtual nfc_rf_protocols_bitmask_t rf_protocols() = 0;
protected:
protected:
/**
* Mark endpoint as connected
*/
@ -96,9 +96,9 @@ namespace nfc {
* Mark endpoint as disconnected
*/
void disconnected();
};
};
/**
/**
* @}
*/

View File

@ -26,26 +26,26 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
class NFCController;
class NFCController;
/**
/**
* This class represents a remote NFC initiator (the local controller being in target mode).
*
* An initiator can be a NFC reader, a NFC-enabled phone or other NFC device capable of generating a RF field.
*/
class NFCRemoteInitiator : public NFCRemoteEndpoint, public NFCNDEFCapable {
public:
class NFCRemoteInitiator : public NFCRemoteEndpoint, public NFCNDEFCapable {
public:
/**
* Create a NFCRemoteInitiator.
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
NFCRemoteInitiator(uint8_t* buffer, size_t buffer_size);
NFCRemoteInitiator(uint8_t *buffer, size_t buffer_size);
virtual ~NFCRemoteInitiator();
/**
@ -60,7 +60,7 @@ namespace nfc {
*
* @oaram[in] delegate the delegate instance to use
*/
void set_remote_initiator_delegate(Delegate* delegate);
void set_remote_initiator_delegate(Delegate *delegate);
/**
* Retrieve the NFC tag type exposed by the controller to communicate with the initiator.
@ -81,13 +81,13 @@ namespace nfc {
*
* @param[in] application a pointer to an ISO7816App instance
*/
virtual void add_iso7816_application(ISO7816App* application) = 0;
virtual void add_iso7816_application(ISO7816App *application) = 0;
private:
Delegate* _delegate;
};
private:
Delegate *_delegate;
};
/**
/**
* @}
*/

View File

@ -26,25 +26,25 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
/**
/**
* This class represents a NFC target (either a remote target when the local controller in in initiator mode, or a target connected through a wired connection).
*
* A target can be a NFC tag/card, a NFC-enabled phone or other NFC device capable of modulating a RF field.
*/
class NFCTarget : public NFCNDEFCapable {
public:
class NFCTarget : public NFCNDEFCapable {
public:
/**
* Create a NFCTarget.
*
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
NFCTarget(uint8_t* buffer, size_t buffer_size);
NFCTarget(uint8_t *buffer, size_t buffer_size);
virtual ~NFCTarget();
struct Delegate {
@ -90,9 +90,9 @@ namespace nfc {
* on_ndef_message_erased() will be called on completion.
*/
virtual void erase_ndef_message() = 0;
};
};
/**
/**
* @}
*/

View File

@ -29,16 +29,16 @@
namespace mbed {
namespace nfc {
/**
/**
* @addtogroup nfc
* @{
*/
/**
/**
* This class is an implementation of the Type 4 tag application.
*/
class Type4RemoteInitiator : public NFCRemoteInitiator {
private:
class Type4RemoteInitiator : public NFCRemoteInitiator {
private:
/**
* Create a Type4RemoteInitiator.
*
@ -46,7 +46,7 @@ namespace nfc {
* @param[in] buffer a bytes array used to store NDEF messages
* @param[in] buffer_size the array size in bytes
*/
Type4RemoteInitiator(NFCController* controller, uint8_t* buffer, size_t buffer_size);
Type4RemoteInitiator(NFCController *controller, uint8_t *buffer, size_t buffer_size);
// NFCRemoteEndpoint implementation
virtual nfc_err_t connect();
@ -58,23 +58,23 @@ namespace nfc {
// NFCRemoteInitiator implementation
virtual nfc_tag_type_t nfc_tag_type() const;
virtual bool is_iso7816_supported() const;
virtual void add_iso7816_application(ISO7816App* application);
virtual void add_iso7816_application(ISO7816App *application);
// NFCNDEFCapable implementation
virtual bool is_ndef_supported() const;
// Callbacks from NFC stack
void disconnected_callback(bool deselected);
static void s_disconnected_callback(nfc_tech_iso7816_t* pIso7816, bool deselected, void* pUserData);
static void s_disconnected_callback(nfc_tech_iso7816_t *pIso7816, bool deselected, void *pUserData);
NFCController* _controller;
NFCController *_controller;
bool _is_connected;
bool _is_disconnected;
nfc_tech_iso7816_t _iso7816;
nfc_tech_type4_target_t _type4;
};
};
/**
/**
* @}
*/

View File

@ -16,17 +16,18 @@
#include "PN512Driver.h"
PN512Driver::PN512Driver(PN512TransportDriver* transport_driver) : NFCControllerDriver(), _transport_driver(transport_driver) {
PN512Driver::PN512Driver(PN512TransportDriver *transport_driver) : NFCControllerDriver(), _transport_driver(transport_driver)
{
_transport_driver->set_delegate(this);
}
nfc_transceiver_t* PN512Driver::initialize(nfc_scheduler_timer_t* scheduler_timer) {
nfc_transceiver_t *PN512Driver::initialize(nfc_scheduler_timer_t *scheduler_timer)
{
// Initialize transport
_transport_driver->initialize();
nfc_err_t ret = pn512_init(&pn512, _transport_driver->get_transport(), scheduler_timer);
if( ret != NFC_OK )
{
if (ret != NFC_OK) {
NFC_ERR("PN512 init error (%d)", ret);
return NULL;
}
@ -35,7 +36,8 @@ nfc_transceiver_t* PN512Driver::initialize(nfc_scheduler_timer_t* scheduler_time
return pn512_get_transceiver(&pn512);
}
void PN512Driver::get_supported_nfc_techs(nfc_tech_t* initiator, nfc_tech_t* target) const {
void PN512Driver::get_supported_nfc_techs(nfc_tech_t *initiator, nfc_tech_t *target) const
{
initiator->nfc_type1 = 0;
initiator->nfc_type2 = 1;
initiator->nfc_type3 = 1;
@ -55,6 +57,7 @@ void PN512Driver::get_supported_nfc_techs(nfc_tech_t* initiator, nfc_tech_t* tar
target->nfc_nfc_dep_f_424 = 1;
}
void PN512Driver::on_hw_interrupt() {
void PN512Driver::on_hw_interrupt()
{
hw_interrupt(); // Propagate interrupt signal
}

View File

@ -26,7 +26,8 @@ PN512SPITransportDriver::PN512SPITransportDriver(PinName mosi, PinName miso, Pin
_spi(mosi, miso, sclk),
_ssel(ssel, 1),
_irq(irq, PullNone),
_rst(rst, 1) {
_rst(rst, 1)
{
// Use SPI mode 0
_spi.format(8, 0);
@ -38,7 +39,8 @@ PN512SPITransportDriver::PN512SPITransportDriver(PinName mosi, PinName miso, Pin
nfc_transport_init(&_nfc_transport, &PN512SPITransportDriver::s_transport_write, &PN512SPITransportDriver::s_transport_read, this);
}
void PN512SPITransportDriver::initialize() {
void PN512SPITransportDriver::initialize()
{
// Deactivate IRQ
_irq.rise(callback(NULL));
@ -53,12 +55,14 @@ void PN512SPITransportDriver::initialize() {
_irq.rise(callback(this, &PN512SPITransportDriver::hw_interrupt));
}
nfc_transport_t* PN512SPITransportDriver::get_transport() const {
nfc_transport_t *PN512SPITransportDriver::get_transport() const
{
return &_nfc_transport;
}
void PN512SPITransportDriver::transport_write( uint8_t address, const uint8_t* outBuf, size_t outLen ) {
if( outLen == 0 ) {
void PN512SPITransportDriver::transport_write(uint8_t address, const uint8_t *outBuf, size_t outLen)
{
if (outLen == 0) {
return;
}
@ -70,8 +74,9 @@ void PN512SPITransportDriver::transport_write( uint8_t address, const uint8_t* o
_ssel = 1;
}
void PN512SPITransportDriver::transport_read( uint8_t address, uint8_t* inBuf, size_t inLen ) {
if( inLen == 0 ) {
void PN512SPITransportDriver::transport_read(uint8_t address, uint8_t *inBuf, size_t inLen)
{
if (inLen == 0) {
return;
}
@ -93,12 +98,14 @@ void PN512SPITransportDriver::transport_read( uint8_t address, uint8_t* inBuf, s
}
// Callbacks from munfc
static void PN512SPITransportDriver::s_transport_write( uint8_t address, const uint8_t* outBuf, size_t outLen, void* pUser ) {
PN512SPITransportDriver* self = (PN512SPITransportDriver*)pUser;
static void PN512SPITransportDriver::s_transport_write(uint8_t address, const uint8_t *outBuf, size_t outLen, void *pUser)
{
PN512SPITransportDriver *self = (PN512SPITransportDriver *)pUser;
self->transport_write(address, outBuf, outLen);
}
static void PN512SPITransportDriver::s_transport_read( uint8_t address, uint8_t* inBuf, size_t inLen, void* pUser ) {
PN512SPITransportDriver* self = (PN512SPITransportDriver*)pUser;
static void PN512SPITransportDriver::s_transport_read(uint8_t address, uint8_t *inBuf, size_t inLen, void *pUser)
{
PN512SPITransportDriver *self = (PN512SPITransportDriver *)pUser;
self->transport_read(address, inBuf, inLen);
}

View File

@ -19,16 +19,19 @@
using namespace mbed;
using namespace mbed::nfc;
PN512TransportDriver::PN512TransportDriver() : _delegate(NULL) {
PN512TransportDriver::PN512TransportDriver() : _delegate(NULL)
{
}
void PN512TransportDriver::set_delegate(Delegate* delegate) {
void PN512TransportDriver::set_delegate(Delegate *delegate)
{
_delegate = delegate;
}
void PN512TransportDriver::hw_interrupt() {
if(_delegate != NULL) {
void PN512TransportDriver::hw_interrupt()
{
if (_delegate != NULL) {
_delegate->on_hw_interrupt();
}
}

View File

@ -23,7 +23,7 @@
using namespace mbed;
using namespace mbed::nfc;
NFCController::NFCController(NFCControllerDriver* driver, events::EventQueue* queue, uint8_t* ndef_buffer, size_t ndef_buffer_sz) :
NFCController::NFCController(NFCControllerDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz) :
_driver(driver), _queue(queue), _transceiver(NULL), _scheduler(NULL), _delegate(NULL), _discovery_running(false), _ndef_buffer_sz(ndef_buffer_sz)
{
_driver->set_delegate(this);
@ -32,9 +32,9 @@ NFCController::NFCController(NFCControllerDriver* driver, events::EventQueue* qu
nfc_err_t NFCController::initialize()
{
MBED_ASSERT(_transceiver == NULL); // Initialize should only be called once
_transceiver = _driver->initialize((nfc_scheduler_timer_t*)&_timer); // See implementation below
_transceiver = _driver->initialize((nfc_scheduler_timer_t *)&_timer); // See implementation below
if( _transceiver == NULL ) {
if (_transceiver == NULL) {
// Initialization error
return NFC_ERR_CONTROLLER; // Controller error
}
@ -46,7 +46,7 @@ nfc_err_t NFCController::initialize()
_queue->call(this, NFCController::scheduler_process, false);
}
void NFCController::set_delegate(Delegate* delegate)
void NFCController::set_delegate(Delegate *delegate)
{
_delegate = delegate;
}
@ -81,7 +81,7 @@ nfc_rf_protocols_bitmask_t NFCController::get_supported_rf_protocols() const
nfc_err_t NFCController::configure_rf_protocols(nfc_rf_protocols_bitmask_t rf_protocols)
{
if( _discovery_running ) {
if (_discovery_running) {
// Cannot configure RF protocols if discovery is running
return NFC_ERR_BUSY;
}
@ -103,7 +103,7 @@ nfc_err_t NFCController::configure_rf_protocols(nfc_rf_protocols_bitmask_t rf_pr
nfc_err_t NFCController::start_discovery()
{
if( _discovery_running ) {
if (_discovery_running) {
// Cannot start discovery if it's already running
return NFC_ERR_BUSY;
}
@ -113,14 +113,14 @@ nfc_err_t NFCController::start_discovery()
nfc_err_t NFCController::cancel_discovery()
{
if( !_discovery_running ) {
if (!_discovery_running) {
return NFC_OK;
}
transceiver_abort(_transceiver);
}
nfc_transceiver_t* NFCController::transceiver() const
nfc_transceiver_t *NFCController::transceiver() const
{
return _transceiver;
}
@ -130,16 +130,16 @@ void NFCController::polling_callback(nfc_err_t ret)
// Polling has completed
_discovery_running = false;
if( ret == NFC_OK ) {
if (ret == NFC_OK) {
// Check if a remote initiator was detected and if so, instantiate it
if( !transceiver_is_initiator_mode(_transceiver) ) {
if (!transceiver_is_initiator_mode(_transceiver)) {
nfc_tech_t active_tech = transceiver_get_active_techs(_transceiver);
if( active_tech.nfc_iso_dep_a || active_tech.nfc_iso_dep_b ) {
if (active_tech.nfc_iso_dep_a || active_tech.nfc_iso_dep_b) {
Type4RemoteInitiator type4_remote_initiator_ptr = new (std::nothrow) Type4RemoteInitiator(_transceiver, _ndef_buffer, _ndef_buffer_sz);
if( type4_remote_initiator_ptr == NULL ) {
if (type4_remote_initiator_ptr == NULL) {
// No memory :(
SharedPtr<Type4RemoteInitiator> type4_remote_initiator( type4_remote_initiator_ptr );
if( _delegate != NULL ) {
SharedPtr<Type4RemoteInitiator> type4_remote_initiator(type4_remote_initiator_ptr);
if (_delegate != NULL) {
_delegate->on_nfc_initiator_discovered(type4_remote_initiator);
}
}
@ -147,11 +147,11 @@ void NFCController::polling_callback(nfc_err_t ret)
}
}
if( _delegate != NULL ) {
if (_delegate != NULL) {
nfc_discovery_terminated_reason_t reason;
// Map reason
switch(ret) {
switch (ret) {
case NFC_OK:
reason = nfc_discovery_terminated_completed;
break;
@ -168,53 +168,61 @@ void NFCController::polling_callback(nfc_err_t ret)
}
}
void NFCController::scheduler_process(bool hw_interrupt) {
void NFCController::scheduler_process(bool hw_interrupt)
{
_timer.detach(); // Cancel timeout - if it triggers, it's ok as we'll have an "early" iteration which will likely be a no-op
// Process stack events
uint32_t timeout = nfc_scheduler_iteration(_scheduler, hw_interrupt?EVENT_HW_INTERRUPT:EVENT_NONE);
uint32_t timeout = nfc_scheduler_iteration(_scheduler, hw_interrupt ? EVENT_HW_INTERRUPT : EVENT_NONE);
_timer.attach(callback(this, &NFCController::on_timeout));
}
void NFCController::on_hw_interrupt() {
void NFCController::on_hw_interrupt()
{
// Run scheduler - this is called in interrupt context
_timer.detach(); // Cancel timeout - if it triggers anyways, it's ok
_queue->call(this, NFCController::scheduler_process, true);
}
void NFCController::on_timeout() {
void NFCController::on_timeout()
{
// Run scheduler - this is called in interrupt context
_queue->call(this, NFCController::scheduler_process, false);
}
static void NFCController::s_polling_callback(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void* pUserData)
static void NFCController::s_polling_callback(nfc_transceiver_t *pTransceiver, nfc_err_t ret, void *pUserData)
{
NFCController* self = (NFCController*) pUserData;
NFCController *self = (NFCController *) pUserData;
self->polling_callback(ret);
}
// Implementation nfc_scheduler_timer_t
void nfc_scheduler_timer_init(nfc_scheduler_timer_t* timer) {
void nfc_scheduler_timer_init(nfc_scheduler_timer_t *timer)
{
(void)timer; // This is a no-op
}
void nfc_scheduler_timer_start(nfc_scheduler_timer_t* timer) {
Timer* mbed_timer = (Timer*)timer;
void nfc_scheduler_timer_start(nfc_scheduler_timer_t *timer)
{
Timer *mbed_timer = (Timer *)timer;
mbed_timer->start();
}
uint32_t nfc_scheduler_timer_get(nfc_scheduler_timer_t* timer) {
Timer* mbed_timer = (Timer*)timer;
uint32_t nfc_scheduler_timer_get(nfc_scheduler_timer_t *timer)
{
Timer *mbed_timer = (Timer *)timer;
return (uint32_t)mbed_timer->read_ms();
}
void nfc_scheduler_timer_stop(nfc_scheduler_timer_t* timer) {
Timer* mbed_timer = (Timer*)timer;
void nfc_scheduler_timer_stop(nfc_scheduler_timer_t *timer)
{
Timer *mbed_timer = (Timer *)timer;
mbed_timer->stop();
}
void nfc_scheduler_timer_reset(nfc_scheduler_timer_t* timer) {
Timer* mbed_timer = (Timer*)timer;
void nfc_scheduler_timer_reset(nfc_scheduler_timer_t *timer)
{
Timer *mbed_timer = (Timer *)timer;
mbed_timer->reset();
}

View File

@ -19,16 +19,19 @@
using namespace mbed;
using namespace mbed::nfc;
NFCControllerDriver::NFCControllerDriver() : _delegate(NULL) {
NFCControllerDriver::NFCControllerDriver() : _delegate(NULL)
{
}
void NFCControllerDriver::set_delegate(Delegate* delegate) {
void NFCControllerDriver::set_delegate(Delegate *delegate)
{
_delegate = delegate;
}
void NFCControllerDriver::hw_interrupt() {
if(_delegate != NULL) {
void NFCControllerDriver::hw_interrupt()
{
if (_delegate != NULL) {
_delegate->on_hw_interrupt();
}
}

View File

@ -20,13 +20,14 @@
using namespace mbed;
using namespace mbed::nfc;
NFCEEPROM(NFCEEPROMDriver* driver, events::EventQueue* queue, uint8_t* ndef_buffer, size_t ndef_buffer_sz) : NFCTarget( ndef_buffer, ndef_buffer_sz )
NFCEEPROM(NFCEEPROMDriver *driver, events::EventQueue *queue, uint8_t *ndef_buffer, size_t ndef_buffer_sz) : NFCTarget(ndef_buffer, ndef_buffer_sz)
_driver(driver), _queue(queue), _initialized(false), _current_op(nfc_eeprom_idle), _eeprom_address(0), _operation_result(NFC_ERR_UNKNOWN)
{
_driver->set_delegate(this);
}
nfc_err_t NFCEEPROM::initialize() {
nfc_err_t NFCEEPROM::initialize()
{
MBED_ASSERT(_initialized == false); // Initialize should only be called once
// Initialize driver
@ -34,14 +35,16 @@ nfc_err_t NFCEEPROM::initialize() {
_initialized = true;
}
void NFCEEPROM::set_delegate(NFCEEPROM::Delegate* delegate) {
void NFCEEPROM::set_delegate(NFCEEPROM::Delegate *delegate)
{
_delegate = delegate;
}
void NFCEEPROM::write_ndef_message() {
void NFCEEPROM::write_ndef_message()
{
MBED_ASSERT(_initialized == true);
if(_current_op != nfc_eeprom_idle) {
if(_delegate != NULL) {
if (_current_op != nfc_eeprom_idle) {
if (_delegate != NULL) {
_delegate->on_ndef_message_written(NFC_ERR_BUSY);
}
return;
@ -53,10 +56,10 @@ void NFCEEPROM::write_ndef_message() {
_current_op = nfc_eeprom_write_start_session;
// Retrieve reader
ac_buffer_dup(&_ndef_buffer_reader, ac_buffer_builder_buffer(ndef_msg_buffer_builder(ndef_message()) );
ac_buffer_dup(&_ndef_buffer_reader, ac_buffer_builder_buffer(ndef_msg_buffer_builder(ndef_message()));
// Check that NDEF message is not too big
if( ac_buffer_reader_readable(&_ndef_buffer_reader) > _driver->get_max_size() ) {
if (ac_buffer_reader_readable(&_ndef_buffer_reader) > _driver->get_max_size()) {
handle_error(NFC_ERR_BUFFER_TOO_SMALL);
return;
}
@ -73,10 +76,11 @@ void NFCEEPROM::write_ndef_message() {
// 4 - End session
}
void NFCEEPROM::read_ndef_message() {
void NFCEEPROM::read_ndef_message()
{
MBED_ASSERT(_initialized == true);
if(_current_op != nfc_eeprom_idle) {
if(_delegate != NULL) {
if (_current_op != nfc_eeprom_idle) {
if (_delegate != NULL) {
_delegate->on_ndef_message_written(NFC_ERR_BUSY);
}
return;
@ -96,13 +100,14 @@ void NFCEEPROM::read_ndef_message() {
// 4 - End session
}
void NFCEEPROM::erase_ndef_message() {
void NFCEEPROM::erase_ndef_message()
{
// We don't want to take any risks, so erase the whole address space
// And set the message size to 0
MBED_ASSERT(_initialized == true);
if(_current_op != nfc_eeprom_idle) {
if(_delegate != NULL) {
if (_current_op != nfc_eeprom_idle) {
if (_delegate != NULL) {
_delegate->on_ndef_message_erased(NFC_ERR_BUSY);
}
return;
@ -122,10 +127,11 @@ void NFCEEPROM::erase_ndef_message() {
// 4 - End session
}
void NFCEEPROM::on_session_started(bool success) {
switch(_current_op) {
void NFCEEPROM::on_session_started(bool success)
{
switch (_current_op) {
case nfc_eeprom_write_start_session:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER); // An EEPROM is not really a controller but close enough
return;
}
@ -134,7 +140,7 @@ void NFCEEPROM::on_session_started(bool success) {
break;
case nfc_eeprom_read_start_session:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
@ -143,7 +149,7 @@ void NFCEEPROM::on_session_started(bool success) {
break;
case nfc_eeprom_erase_start_session:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
@ -158,21 +164,22 @@ void NFCEEPROM::on_session_started(bool success) {
}
}
void NFCEEPROM::on_session_ended(bool success) {
switch(_current_op) {
void NFCEEPROM::on_session_ended(bool success)
{
switch (_current_op) {
case nfc_eeprom_write_end_session:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
_current_op = nfc_eeprom_idle;
if( _delegate != NULL ) {
if (_delegate != NULL) {
_driver->on_ndef_message_written(_operation_result);
}
break;
case nfc_eeprom_read_end_session:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
@ -181,18 +188,18 @@ void NFCEEPROM::on_session_ended(bool success) {
// Try to parse the NDEF message
ndef_msg_decode(ndef_message());
if( _delegate != NULL ) {
if (_delegate != NULL) {
_driver->on_ndef_message_read(_operation_result);
}
break;
case nfc_eeprom_erase_end_session:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
_current_op = nfc_eeprom_idle;
if( _delegate != NULL ) {
if (_delegate != NULL) {
_driver->on_ndef_message_erased(_operation_result);
}
break;
@ -204,17 +211,18 @@ void NFCEEPROM::on_session_ended(bool success) {
}
}
void NFCEEPROM::on_bytes_read(size_t count) {
switch(_current_op) {
void NFCEEPROM::on_bytes_read(size_t count)
{
switch (_current_op) {
case nfc_eeprom_read_read_bytes:
if(count == 0) {
if (count == 0) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
// Discard bytes that were actually read and update address
_eeprom_address += count;
ac_buffer_builder_t* buffer_builder = ndef_msg_buffer_builder(ndef_message());
ac_buffer_builder_t *buffer_builder = ndef_msg_buffer_builder(ndef_message());
ac_buffer_builder_write_n_skip(buffer_builder, count);
// Continue reading
@ -227,10 +235,11 @@ void NFCEEPROM::on_bytes_read(size_t count) {
}
}
void NFCEEPROM::on_bytes_written(size_t count) {
switch(_current_op) {
void NFCEEPROM::on_bytes_written(size_t count)
{
switch (_current_op) {
case nfc_eeprom_write_write_bytes:
if(count == 0) {
if (count == 0) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
@ -249,10 +258,11 @@ void NFCEEPROM::on_bytes_written(size_t count) {
}
}
void NFCEEPROM::on_size_written(bool success) {
switch(_current_op) {
void NFCEEPROM::on_size_written(bool success)
{
switch (_current_op) {
case nfc_eeprom_write_write_size:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
@ -263,7 +273,7 @@ void NFCEEPROM::on_size_written(bool success) {
_driver->end_session();
break;
case nfc_eeprom_erase_write_size:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
@ -280,21 +290,21 @@ void NFCEEPROM::on_size_written(bool success) {
}
}
void NFCEEPROM::on_size_read(bool success, size_t size) {
switch(_current_op) {
void NFCEEPROM::on_size_read(bool success, size_t size)
{
switch (_current_op) {
case nfc_eeprom_read_read_size:
if(!success) {
if (!success) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
// Reset NDEF message buffer builder
ac_buffer_builder_t* buffer_builder = ndef_msg_buffer_builder(ndef_message());
ac_buffer_builder_reset( buffer_builder );
ac_buffer_builder_t *buffer_builder = ndef_msg_buffer_builder(ndef_message());
ac_buffer_builder_reset(buffer_builder);
// Check that we have a big enough buffer to read the message
if( size > ac_buffer_builder_writable(buffer_builder) )
{
if (size > ac_buffer_builder_writable(buffer_builder)) {
// Not enough space, close session
_current_op = nfc_eeprom_read_end_session;
_operation_result = NFC_ERR_BUFFER_TOO_SMALL;
@ -317,10 +327,11 @@ void NFCEEPROM::on_size_read(bool success, size_t size) {
}
}
void NFCEEPROM::on_bytes_erased(size_t count) {
switch(_current_op) {
void NFCEEPROM::on_bytes_erased(size_t count)
{
switch (_current_op) {
case nfc_eeprom_erase_erase_bytes:
if(count == 0) {
if (count == 0) {
handle_error(NFC_ERR_CONTROLLER);
return;
}
@ -338,61 +349,61 @@ void NFCEEPROM::on_bytes_erased(size_t count) {
}
}
void NFCEEPROM::on_event() {
void NFCEEPROM::on_event()
{
// Just schedule a task on event queue
_queue->call(_driver, NFCEEPROMDriver::process_events);
}
void NFCEEPROM::continue_write() {
if( ac_buffer_reader_readable(&_ndef_buffer_reader) > 0 ) {
void NFCEEPROM::continue_write()
{
if (ac_buffer_reader_readable(&_ndef_buffer_reader) > 0) {
// Continue writing
_driver->write_bytes(_eeprom_address, ac_buffer_reader_current_buffer_pointer(&_ndef_buffer_reader), ac_buffer_reader_current_buffer_length(&_ndef_buffer_reader));
}
else {
} else {
// Now update size
_current_op = nfc_eeprom_write_write_size;
_driver->write_size(_eeprom_address);
}
}
void NFCEEPROM::continue_erase() {
if( _eeprom_address < _driver->get_max_size() ) {
void NFCEEPROM::continue_erase()
{
if (_eeprom_address < _driver->get_max_size()) {
// Continue erasing
_driver->erase_bytes(_eeprom_address, _driver->get_max_size() - _eeprom_address);
}
else {
} else {
// Now update size
_current_op = nfc_eeprom_erase_write_size;
_driver->write_size(0);
}
}
void NFCEEPROM::continue_read() {
if( _eeprom_address < _ndef_buffer_read_sz ) {
void NFCEEPROM::continue_read()
{
if (_eeprom_address < _ndef_buffer_read_sz) {
// Continue reading
ac_buffer_builder_t* buffer_builder = ndef_msg_buffer_builder(ndef_message());
ac_buffer_builder_t *buffer_builder = ndef_msg_buffer_builder(ndef_message());
_driver->read_bytes(_eeprom_address, ac_buffer_builder_write_position(buffer_builder), _ndef_buffer_read_sz - _eeprom_address);
}
else {
} else {
// Done, close session
_operation_result = NFC_OK;
_driver->end_session();
}
}
void NFCEEPROM::handle_error(nfc_err_t ret) {
void NFCEEPROM::handle_error(nfc_err_t ret)
{
// Save & reset current op
nfc_eeprom_operation_t last_op = _current_op;
_current_op = nfc_eeprom_idle;
if(_delegate != NULL) {
if(last_op <= nfc_eeprom_write_end_session) {
if (_delegate != NULL) {
if (last_op <= nfc_eeprom_write_end_session) {
_delegate->on_ndef_message_written(ret);
}
else if(last_op <= nfc_eeprom_read_end_session) {
} else if (last_op <= nfc_eeprom_read_end_session) {
_delegate->on_ndef_message_read(ret);
}
else if(last_op <= nfc_eeprom_erase_end_session) {
} else if (last_op <= nfc_eeprom_erase_end_session) {
_delegate->on_ndef_message_erased(ret);
}
}

View File

@ -19,10 +19,12 @@
using namespace mbed;
using namespace mbed::nfc;
NFCEEPROMDriver::NFCEEPROMDriver() : _delegate(NULL) {
NFCEEPROMDriver::NFCEEPROMDriver() : _delegate(NULL)
{
}
void NFCEEPROMDriver::set_delegate(Delegate* delegate) {
void NFCEEPROMDriver::set_delegate(Delegate *delegate)
{
_delegate = delegate;
}

View File

@ -25,49 +25,53 @@
using namespace mbed;
using namespace mbed::nfc;
NFCNDEFCapable::NFCNDEFCapable(uint8_t* buffer, size_t buffer_size) : _delegate(NULL)
NFCNDEFCapable::NFCNDEFCapable(uint8_t *buffer, size_t buffer_size) : _delegate(NULL)
{
ndef_msg_init(&_ndef_message, s_encode_callback, s_decode_callback);
}
void NFCNDEFCapable::set_ndef_delegate(NFCNDEFCapable::Delegate* delegate)
void NFCNDEFCapable::set_ndef_delegate(NFCNDEFCapable::Delegate *delegate)
{
_delegate = delegate;
}
void NFCNDEFCapable::parse_ndef_message(const ac_buffer_t& buffer)
void NFCNDEFCapable::parse_ndef_message(const ac_buffer_t &buffer)
{
ac_buffer_t reader;
ac_buffer_dup(&reader, &buffer);
if(_delegate != NULL) {
if (_delegate != NULL) {
_delegate->parse_ndef_message(ac_buffer_reader_current_buffer_pointer(&reader), ac_buffer_reader_current_buffer_length(&reader));
}
}
void NFCNDEFCapable::build_ndef_message(ac_buffer_builder_t& buffer_builder)
void NFCNDEFCapable::build_ndef_message(ac_buffer_builder_t &buffer_builder)
{
if(_delegate != NULL) {
size_t count = _delegate->build_ndef_message( ac_buffer_builder_write_position(&buffer_builder), ac_buffer_builder_writable(&buffer_builder) );
if (_delegate != NULL) {
size_t count = _delegate->build_ndef_message(ac_buffer_builder_write_position(&buffer_builder), ac_buffer_builder_writable(&buffer_builder));
ac_buffer_builder_write_n_skip(&buffer_builder, count);
}
}
nfc_err_t NFCNDEFCapable::s_ndef_encode(ndef_msg_t* pTag, buffer_builder_t* pBufferBldr, void* pUserData) {
NFCNDEFCapable* self = (NFCNDEFCapable*)pUserData;
nfc_err_t NFCNDEFCapable::s_ndef_encode(ndef_msg_t *pTag, buffer_builder_t *pBufferBldr, void *pUserData)
{
NFCNDEFCapable *self = (NFCNDEFCapable *)pUserData;
self->ndef_encode(pBufferBldr);
}
nfc_err_t NFCNDEFCapable::s_ndef_decode(ndef_msg_t* pTag, buffer_t* pBuffer, void* pUserData) {
NFCNDEFCapable* self = (NFCNDEFCapable*)pUserData;
nfc_err_t NFCNDEFCapable::s_ndef_decode(ndef_msg_t *pTag, buffer_t *pBuffer, void *pUserData)
{
NFCNDEFCapable *self = (NFCNDEFCapable *)pUserData;
self->ndef_decode(pBuffer);
}
nfc_err_t NFCNDEFCapable::ndef_encode(buffer_builder_t* pBufferBldr) {
nfc_err_t NFCNDEFCapable::ndef_encode(buffer_builder_t *pBufferBldr)
{
build_ndef_message(buffer_builder);
return NFC_OK;
}
nfc_err_t NFCNDEFCapable::ndef_decode(buffer_t* pBuffer) {
nfc_err_t NFCNDEFCapable::ndef_decode(buffer_t *pBuffer)
{
parse_ndef_message(pBuffer);
return NFC_OK;
}

View File

@ -25,19 +25,21 @@
using namespace mbed;
using namespace mbed::nfc;
NFCRemoteEndpoint::NFCRemoteEndpoint(NFCController* controller) : _controller(controller), _is_lost(false) {
NFCRemoteEndpoint::NFCRemoteEndpoint(NFCController *controller) : _controller(controller), _is_lost(false)
{
}
bool NFCRemoteEndpoint::is_lost() const {
bool NFCRemoteEndpoint::is_lost() const
{
return _is_lost;
}
nfc_rf_protocols_bitmask_t NFCRemoteEndpoint::rf_protocols() {
nfc_rf_protocols_bitmask_t NFCRemoteEndpoint::rf_protocols()
{
nfc_rf_protocols_bitmask_t rf_protocols = {0};
nfc_tech_t active_tech = transceiver_get_active_techs(_transceiver);
if(!transceiver_is_initiator_mode(_transceiver))
{
if (!transceiver_is_initiator_mode(_transceiver)) {
// Note: We only support ISO-DEP for now
rf_protocols.target_iso_dep = active_tech.nfc_iso_dep_a || active_tech.nfc_iso_dep_b;
}
@ -45,11 +47,13 @@ nfc_rf_protocols_bitmask_t NFCRemoteEndpoint::rf_protocols() {
return rf_protocols;
}
void NFCRemoteEndpoint::set_lost() {
void NFCRemoteEndpoint::set_lost()
{
_is_lost = true;
}
NFCController* NFCRemoteEndpoint::nfc_controller() const {
NFCController *NFCRemoteEndpoint::nfc_controller() const
{
return _controller;
}

View File

@ -19,17 +19,19 @@
using namespace mbed;
using namespace mbed::nfc;
NFCRemoteInitiator::NFCRemoteInitiator(NFCController* controller, uint8_t* buffer, size_t buffer_size) :
NFCRemoteEndpoint(controller), NFCNDEFCapable(buffer, buffer_size) {
NFCRemoteInitiator::NFCRemoteInitiator(NFCController *controller, uint8_t *buffer, size_t buffer_size) :
NFCRemoteEndpoint(controller), NFCNDEFCapable(buffer, buffer_size)
{
}
NFCRemoteInitiator::~NFCRemoteInitiator() {
NFCRemoteInitiator::~NFCRemoteInitiator()
{
}
void NFCRemoteInitiator::set_remote_initiator_delegate(Delegate* delegate)
void NFCRemoteInitiator::set_remote_initiator_delegate(Delegate *delegate)
{
_delegate = delegate;
}

View File

@ -19,11 +19,13 @@
using namespace mbed;
using namespace mbed::nfc;
NFCTarget::NFCTarget(uint8_t* buffer, size_t buffer_size) :
NFCNDEFCapable(buffer, buffer_size) {
NFCTarget::NFCTarget(uint8_t *buffer, size_t buffer_size) :
NFCNDEFCapable(buffer, buffer_size)
{
}
NFCTarget::~NFCTarget() {
NFCTarget::~NFCTarget()
{
}

View File

@ -28,9 +28,10 @@
using namespace mbed;
using namespace mbed::nfc;
Type4RemoteInitiator::Type4RemoteInitiator(NFCController* controller, uint8_t* buffer, size_t buffer_size) :
Type4RemoteInitiator::Type4RemoteInitiator(NFCController *controller, uint8_t *buffer, size_t buffer_size) :
NFCRemoteInitiator(ndef_buffer, ndef_buffer_sz),
_controller(controller), _is_connected(false) , _is_disconnected(false), _apps(NULL) {
_controller(controller), _is_connected(false), _is_disconnected(false), _apps(NULL)
{
// Init ISO7816
nfc_tech_iso7816_init(&_iso7816, _controller->transceiver(), &Type4RemoteInitiator::s_disconnected_callback, this);
@ -38,12 +39,13 @@ Type4RemoteInitiator::Type4RemoteInitiator(NFCController* controller, uint8_t* b
nfc_tech_type4_target_init(&_type4, &_iso7816, ndef_message());
}
nfc_err_t Type4RemoteInitiator::connect() {
if(_is_connected) {
nfc_err_t Type4RemoteInitiator::connect()
{
if (_is_connected) {
return NFC_BUSY;
}
if(_is_disconnected) {
if (_is_disconnected) {
return NFC_ERR_DISCONNECTED;
}
@ -54,12 +56,13 @@ nfc_err_t Type4RemoteInitiator::connect() {
connected();
}
nfc_err_t Type4RemoteInitiator::disconnect() {
if(!_is_connected) {
nfc_err_t Type4RemoteInitiator::disconnect()
{
if (!_is_connected) {
return NFC_OK;
}
if(_is_disconnected) {
if (_is_disconnected) {
return NFC_OK;
}
@ -67,19 +70,21 @@ nfc_err_t Type4RemoteInitiator::disconnect() {
nfc_tech_iso7816_disconnect(&_iso7816);
}
bool Type4RemoteInitiator::is_connected() const {
bool Type4RemoteInitiator::is_connected() const
{
return _is_connected;
}
bool Type4RemoteInitiator::is_disconnected() const {
bool Type4RemoteInitiator::is_disconnected() const
{
return _is_disconnected;
}
nfc_rf_protocols_bitmask_t Type4RemoteInitiator::rf_protocols() {
nfc_rf_protocols_bitmask_t Type4RemoteInitiator::rf_protocols()
{
nfc_rf_protocols_bitmask_t rf_protocols = {0};
nfc_tech_t active_tech = transceiver_get_active_techs(_transceiver);
if(!transceiver_is_initiator_mode(_transceiver))
{
if (!transceiver_is_initiator_mode(_transceiver)) {
// We only support ISO-DEP
rf_protocols.target_iso_dep = active_tech.nfc_iso_dep_a || active_tech.nfc_iso_dep_b;
}
@ -87,24 +92,27 @@ nfc_rf_protocols_bitmask_t Type4RemoteInitiator::rf_protocols() {
return rf_protocols;
}
virtual nfc_tag_type_t Type4RemoteInitiator::nfc_tag_type() const {
virtual nfc_tag_type_t Type4RemoteInitiator::nfc_tag_type() const
{
nfc_tech_t active_tech = transceiver_get_active_techs(_transceiver);
if(active_tech.nfc_iso_dep_a) {
if (active_tech.nfc_iso_dep_a) {
return nfc_tag_type_4a;
}
else { // if(active_tech.nfc_iso_dep_b)
} else { // if(active_tech.nfc_iso_dep_b)
return nfc_tag_type_4b;
}
}
virtual bool Type4RemoteInitiator::is_iso7816_supported() const {
virtual bool Type4RemoteInitiator::is_iso7816_supported() const
{
return true;
}
virtual void Type4RemoteInitiator::add_iso7816_application(ISO7816App* application) {
virtual void Type4RemoteInitiator::add_iso7816_application(ISO7816App *application)
{
nfc_tech_iso7816_add_app(&_iso7816, application->_iso7816_app);
}
virtual bool Type4RemoteInitiator::is_ndef_supported() const {
virtual bool Type4RemoteInitiator::is_ndef_supported() const
{
return true;
}

View File

@ -39,7 +39,7 @@
* \param buffer_size size of the underlying buffer
* \param pImpl pointer to actual implementation
*/
void ndef_msg_init( ndef_msg_t* pNdef, ndef_encode_fn_t encode, ndef_decode_fn_t decode, uint8_t* data, size_t size, void* pUserData )
void ndef_msg_init(ndef_msg_t *pNdef, ndef_encode_fn_t encode, ndef_decode_fn_t decode, uint8_t *data, size_t size, void *pUserData)
{
pNdef->encode = encode;
pNdef->decode = decode;

View File

@ -43,43 +43,40 @@ typedef struct __ndef_msg ndef_msg_t;
* \param pTag pointer to ndef_tag_t instance
* \param type pMem buffer in which to store the generated content
*/
typedef nfc_err_t (*ndef_encode_fn_t)(ndef_msg_t* pTag, buffer_builder_t* pBufferBldr, void* pUserData);
typedef nfc_err_t (*ndef_encode_fn_t)(ndef_msg_t *pTag, buffer_builder_t *pBufferBldr, void *pUserData);
/** Function called to decode the tag's content on write (target mode) or read (reader mode)
* \param pTag pointer to ndef_tag_t instance
* \param type pMem buffer containing the tag's content
*/
typedef nfc_err_t (*ndef_decode_fn_t)(ndef_msg_t* pTag, buffer_t* pBuffer, void* pUserData);
typedef nfc_err_t (*ndef_decode_fn_t)(ndef_msg_t *pTag, buffer_t *pBuffer, void *pUserData);
typedef struct __ndef_msg
{
typedef struct __ndef_msg {
ndef_encode_fn_t encode;
ndef_decode_fn_t decode;
buffer_builder_t bufferBldr;
void* pUserData;
void *pUserData;
} ndef_msg_t;
void ndef_msg_init( ndef_msg_t* pNdef, ndef_encode_fn_t encode, ndef_decode_fn_t decode, uint8_t* data, size_t size, void* pUserData );
void ndef_msg_init(ndef_msg_t *pNdef, ndef_encode_fn_t encode, ndef_decode_fn_t decode, uint8_t *data, size_t size, void *pUserData);
static inline nfc_err_t ndef_msg_encode(ndef_msg_t* pNdef)
static inline nfc_err_t ndef_msg_encode(ndef_msg_t *pNdef)
{
if(pNdef->encode == NULL)
{
if (pNdef->encode == NULL) {
return NFC_OK;
}
return pNdef->encode(pNdef, &pNdef->bufferBldr, pNdef->pUserData);
}
static inline nfc_err_t ndef_msg_decode(ndef_msg_t* pNdef)
static inline nfc_err_t ndef_msg_decode(ndef_msg_t *pNdef)
{
if(pNdef->decode == NULL)
{
if (pNdef->decode == NULL) {
return NFC_OK;
}
return pNdef->decode(pNdef, buffer_builder_buffer(&pNdef->bufferBldr), pNdef->pUserData);
}
static inline buffer_builder_t* ndef_msg_buffer_builder(ndef_msg_t* pNdef)
static inline buffer_builder_t *ndef_msg_buffer_builder(ndef_msg_t *pNdef)
{
return &pNdef->bufferBldr;
}

View File

@ -21,7 +21,8 @@
#if NFC_DEBUG && !defined(NDEBUG)
#include "stdio.h"
#include "stdarg.h"
static inline void nfc_dbg_print(const char* type, const char* module, unsigned int line, const char* fmt, ...) {
static inline void nfc_dbg_print(const char *type, const char *module, unsigned int line, const char *fmt, ...)
{
#if !defined(NDEBUG)
printf("NFC [%s] %s:%u ", type, module, line);
va_list args;

View File

@ -27,7 +27,7 @@
#include "platform/nfc_scheduler.h"
void nfc_scheduler_init(nfc_scheduler_t* pScheduler, nfc_scheduler_timer_t* pTimer)
void nfc_scheduler_init(nfc_scheduler_t *pScheduler, nfc_scheduler_timer_t *pTimer)
{
pScheduler->pNext = NULL;
pScheduler->pTimer = pTimer;
@ -38,20 +38,18 @@ void nfc_scheduler_init(nfc_scheduler_t* pScheduler, nfc_scheduler_timer_t* pTim
#define MAX_TIMEOUT UINT32_MAX
uint32_t nfc_scheduler_iteration(nfc_scheduler_t* pScheduler, uint32_t events)
uint32_t nfc_scheduler_iteration(nfc_scheduler_t *pScheduler, uint32_t events)
{
bool triggered = false;
while(true)
{
nfc_task_t* pPrioTask = NULL;
nfc_task_t* pPrioTaskPrevious = NULL;
while (true) {
nfc_task_t *pPrioTask = NULL;
nfc_task_t *pPrioTaskPrevious = NULL;
uint32_t prioTaskEvent = 0;
int64_t timeout;
nfc_task_t* pPreviousTask = NULL;
nfc_task_t* pTask = pScheduler->pNext;
nfc_task_t *pPreviousTask = NULL;
nfc_task_t *pTask = pScheduler->pNext;
if( pTask == NULL )
{
if (pTask == NULL) {
NFC_DBG("Empty queue, %lu ms elapsed", nfc_scheduler_timer_get(pScheduler->pTimer));
//Empty queue, return
return MAX_TIMEOUT;
@ -62,25 +60,21 @@ uint32_t nfc_scheduler_iteration(nfc_scheduler_t* pScheduler, uint32_t events)
NFC_DBG("%lu ms elapsed", timeElapsed);
nfc_scheduler_timer_reset(pScheduler->pTimer);
do
{
do {
//Apply timeouts
if( pTask->events & EVENT_TIMEOUT )
{
if (pTask->events & EVENT_TIMEOUT) {
pTask->timeout -= timeElapsed;
}
pPreviousTask = pTask;
pTask = pTask->pNext;
} while( pTask != NULL );
} while (pTask != NULL);
pTask = pScheduler->pNext;
pPreviousTask = NULL;
timeout = MAX_TIMEOUT;
do
{
do {
//Check which task should be woken up first
if( (events & EVENT_HW_INTERRUPT) && (pTask->events & EVENT_HW_INTERRUPT) )
{
if ((events & EVENT_HW_INTERRUPT) && (pTask->events & EVENT_HW_INTERRUPT)) {
//Hardware interrupts have prio
pPrioTask = pTask;
pPrioTaskPrevious = pPreviousTask;
@ -88,9 +82,7 @@ uint32_t nfc_scheduler_iteration(nfc_scheduler_t* pScheduler, uint32_t events)
events &= ~EVENT_HW_INTERRUPT; //Only one task gets triggered per event
prioTaskEvent = EVENT_HW_INTERRUPT;
break;
}
else if( (pTask->events & EVENT_TIMEOUT) && (pTask->timeout < timeout) )
{
} else if ((pTask->events & EVENT_TIMEOUT) && (pTask->timeout < timeout)) {
pPrioTask = pTask;
pPrioTaskPrevious = pPreviousTask;
timeout = pTask->timeout;
@ -98,37 +90,29 @@ uint32_t nfc_scheduler_iteration(nfc_scheduler_t* pScheduler, uint32_t events)
}
pPreviousTask = pTask;
pTask = pTask->pNext;
} while( pTask != NULL );
} while (pTask != NULL);
if( pPrioTask == NULL )
{
if (pPrioTask == NULL) {
//No task to wake up, exit
NFC_DBG("No task to wake up");
return MAX_TIMEOUT;
}
if( timeout > 0 )
{
if (timeout > 0) {
//No task to wake up yet
if( timeout > MAX_TIMEOUT )
{
if (timeout > MAX_TIMEOUT) {
NFC_DBG("No task to wake up");
return MAX_TIMEOUT;
}
else
{
} else {
NFC_DBG("No task to wake up, wait %lu ms", timeout);
return timeout;
}
}
//Dequeue task
if( pPrioTaskPrevious == NULL )
{
if (pPrioTaskPrevious == NULL) {
pScheduler->pNext = pPrioTask->pNext;
}
else
{
} else {
pPrioTaskPrevious->pNext = pPrioTask->pNext;
}
pPrioTask->pNext = NULL;
@ -142,51 +126,43 @@ uint32_t nfc_scheduler_iteration(nfc_scheduler_t* pScheduler, uint32_t events)
return MAX_TIMEOUT;
}
void nfc_scheduler_queue_task(nfc_scheduler_t* pScheduler, nfc_task_t* pTask)
void nfc_scheduler_queue_task(nfc_scheduler_t *pScheduler, nfc_task_t *pTask)
{
pTask->timeout = pTask->timeoutInitial + nfc_scheduler_timer_get(pScheduler->pTimer);
NFC_DBG("Queuing task %p: events %1X, timeout %lu ms", pTask, pTask->events, pTask->timeout);
//Find last task
nfc_task_t* pPrevTask = pScheduler->pNext;
nfc_task_t *pPrevTask = pScheduler->pNext;
pTask->pNext = NULL;
if( pPrevTask == NULL )
{
if (pPrevTask == NULL) {
pScheduler->pNext = pTask;
return;
}
while( pPrevTask->pNext != NULL )
{
while (pPrevTask->pNext != NULL) {
pPrevTask = pPrevTask->pNext;
}
pPrevTask->pNext = pTask;
}
void nfc_scheduler_dequeue_task(nfc_scheduler_t* pScheduler, bool abort, nfc_task_t* pTask)
void nfc_scheduler_dequeue_task(nfc_scheduler_t *pScheduler, bool abort, nfc_task_t *pTask)
{
NFC_DBG("Dequeuing task %p", pTask);
//Find task
nfc_task_t* pPrevTask = pScheduler->pNext;
if( pPrevTask == NULL )
{
nfc_task_t *pPrevTask = pScheduler->pNext;
if (pPrevTask == NULL) {
pTask->pNext = NULL;
return;
}
if( pPrevTask == pTask )
{
if(abort)
{
if (pPrevTask == pTask) {
if (abort) {
pTask->fn(EVENT_ABORTED, pTask->pUserData);
}
pScheduler->pNext = pTask->pNext;
pTask->pNext = NULL;
return;
}
while( pPrevTask->pNext != NULL )
{
if(pPrevTask->pNext == pTask)
{
if(abort)
{
while (pPrevTask->pNext != NULL) {
if (pPrevTask->pNext == pTask) {
if (abort) {
pTask->fn(EVENT_ABORTED, pTask->pUserData);
}
pPrevTask->pNext = pTask->pNext;
@ -198,7 +174,7 @@ void nfc_scheduler_dequeue_task(nfc_scheduler_t* pScheduler, bool abort, nfc_tas
pTask->pNext = NULL;
}
void task_init(nfc_task_t* pTask, uint32_t events, uint32_t timeout, nfc_task_fn fn, void* pUserData)
void task_init(nfc_task_t *pTask, uint32_t events, uint32_t timeout, nfc_task_fn fn, void *pUserData)
{
pTask->events = events;
pTask->timeoutInitial = timeout;

View File

@ -45,62 +45,60 @@ typedef struct __nfc_timer nfc_scheduler_timer_t;
struct __nfc_task;
typedef struct __nfc_task nfc_task_t;
typedef struct __scheduler
{
nfc_task_t* pNext;
nfc_scheduler_timer_t* pTimer;
typedef struct __scheduler {
nfc_task_t *pNext;
nfc_scheduler_timer_t *pTimer;
} nfc_scheduler_t;
typedef void (*nfc_task_fn)(uint32_t events, void* pUserData);
typedef void (*nfc_task_fn)(uint32_t events, void *pUserData);
struct __nfc_task
{
struct __nfc_task {
uint32_t events;
int64_t timeout; //millisecs
int64_t timeoutInitial;
nfc_task_fn fn;
void* pUserData;
void *pUserData;
nfc_task_t* pNext;
nfc_task_t *pNext;
};
void nfc_scheduler_timer_init(nfc_scheduler_timer_t* timer);
void nfc_scheduler_timer_init(nfc_scheduler_timer_t *timer);
void nfc_scheduler_timer_start(nfc_scheduler_timer_t* timer);
void nfc_scheduler_timer_start(nfc_scheduler_timer_t *timer);
uint32_t nfc_scheduler_timer_get(nfc_scheduler_timer_t* timer);
uint32_t nfc_scheduler_timer_get(nfc_scheduler_timer_t *timer);
void nfc_scheduler_timer_stop(nfc_scheduler_timer_t* timer);
void nfc_scheduler_timer_stop(nfc_scheduler_timer_t *timer);
void nfc_scheduler_timer_reset(nfc_scheduler_timer_t* timer);
void nfc_scheduler_timer_reset(nfc_scheduler_timer_t *timer);
/** Init scheduler
* \param pScheduler scheduler instance to init
* \param pTimer timer instance
*/
void nfc_scheduler_init(nfc_scheduler_t* pScheduler, nfc_scheduler_timer_t* pTimer);
void nfc_scheduler_init(nfc_scheduler_t *pScheduler, nfc_scheduler_timer_t *pTimer);
/** Iterate through all tasks
* \param pScheduler scheduler instance
* \param events mask of events (except EVENT_TIMEOUT) that have been raised since this function last returned (0 on first call)
* \return time after which this function must be called again if no other event arises
*/
uint32_t nfc_scheduler_iteration(nfc_scheduler_t* pScheduler, uint32_t events);
uint32_t nfc_scheduler_iteration(nfc_scheduler_t *pScheduler, uint32_t events);
/** Queue a task to execute
* \param pScheduler scheduler instance
* \param pTask task to queue
*
*/
void nfc_scheduler_queue_task(nfc_scheduler_t* pScheduler, nfc_task_t* pTask);
void nfc_scheduler_queue_task(nfc_scheduler_t *pScheduler, nfc_task_t *pTask);
/** Remove a task to execute
* \param pScheduler scheduler instance
* \param pTask task to remove
* \param abort abort task if queued
*/
void nfc_scheduler_dequeue_task(nfc_scheduler_t* pScheduler, bool abort, nfc_task_t* pTask);
void nfc_scheduler_dequeue_task(nfc_scheduler_t *pScheduler, bool abort, nfc_task_t *pTask);
/** Initialize task with the following parameters
* \param pTask task to initialize
@ -109,7 +107,7 @@ void nfc_scheduler_dequeue_task(nfc_scheduler_t* pScheduler, bool abort, nfc_tas
* \param fn function to be called
* \param pUserData data that will be passed to function
*/
void task_init(nfc_task_t* pTask, uint32_t events, uint32_t timeout, nfc_task_fn fn, void* pUserData);
void task_init(nfc_task_t *pTask, uint32_t events, uint32_t timeout, nfc_task_fn fn, void *pUserData);
#ifdef __cplusplus
}

View File

@ -36,7 +36,7 @@
* \param read transport read function
* \param pUser parameter that will be passed to any of the above functions
*/
void nfc_transport_init( nfc_transport_t* pTransport, nfc_transport_write_fn_t write, nfc_transport_read_fn_t read, void* pUser )
void nfc_transport_init(nfc_transport_t *pTransport, nfc_transport_write_fn_t write, nfc_transport_read_fn_t read, void *pUser)
{
pTransport->write = write;
pTransport->read = read;

View File

@ -41,7 +41,7 @@ extern "C" {
* \param outLen buffer's length
* \param pUser parameter passed to the nfc_transport_init function
*/
typedef void (*nfc_transport_write_fn_t)( uint8_t address, const uint8_t* outBuf, size_t outLen, void* pUser );
typedef void (*nfc_transport_write_fn_t)(uint8_t address, const uint8_t *outBuf, size_t outLen, void *pUser);
/** Function called to read a register's value
* \param address address to read packet from
@ -49,25 +49,24 @@ typedef void (*nfc_transport_write_fn_t)( uint8_t address, const uint8_t* outBuf
* \param outLen buffer's length
* \param pUser parameter passed to the nfc_transport_init function
*/
typedef void (*nfc_transport_read_fn_t)( uint8_t address, uint8_t* inBuf, size_t inLen, void* pUser );
typedef void (*nfc_transport_read_fn_t)(uint8_t address, uint8_t *inBuf, size_t inLen, void *pUser);
typedef struct __transport
{
typedef struct __transport {
nfc_transport_write_fn_t write;
nfc_transport_read_fn_t read;
void* pUser;
void *pUser;
} nfc_transport_t;
void nfc_transport_init( nfc_transport_t* pTransport, nfc_transport_write_fn_t write, nfc_transport_read_fn_t read, void* pUser );
void nfc_transport_init(nfc_transport_t *pTransport, nfc_transport_write_fn_t write, nfc_transport_read_fn_t read, void *pUser);
static inline void nfc_transport_write( nfc_transport_t* pTransport, uint8_t address, const uint8_t* outBuf, size_t outLen )
static inline void nfc_transport_write(nfc_transport_t *pTransport, uint8_t address, const uint8_t *outBuf, size_t outLen)
{
pTransport->write( address, outBuf, outLen, pTransport->pUser );
pTransport->write(address, outBuf, outLen, pTransport->pUser);
}
static inline void nfc_transport_read( nfc_transport_t* pTransport, uint8_t address, uint8_t* inBuf, size_t inLen )
static inline void nfc_transport_read(nfc_transport_t *pTransport, uint8_t address, uint8_t *inBuf, size_t inLen)
{
pTransport->read( address, inBuf, inLen, pTransport->pUser );
pTransport->read(address, inBuf, inLen, pTransport->pUser);
}
#ifdef __cplusplus

View File

@ -32,23 +32,23 @@
#include "iso7816_defs.h"
#include "tech/isodep/isodep_target.h"
static void iso7816_disconnected(nfc_tech_iso7816_t* pIso7816, bool deselected);
static void iso7816_disconnected(nfc_tech_iso7816_t *pIso7816, bool deselected);
static nfc_err_t iso7816_parse(nfc_tech_iso7816_t* pIso7816);
static nfc_err_t iso7816_parse(nfc_tech_iso7816_t *pIso7816);
static void iso7816_receive(nfc_tech_iso7816_t* pIso7816);
static nfc_err_t iso7816_transmit(nfc_tech_iso7816_t* pIso7816);
static void iso7816_receive(nfc_tech_iso7816_t *pIso7816);
static nfc_err_t iso7816_transmit(nfc_tech_iso7816_t *pIso7816);
static bool iso7816_mf_command(nfc_tech_iso7816_t* pIso7816);
static bool iso7816_mf_command(nfc_tech_iso7816_t *pIso7816);
static void iso_dep_received_cb(nfc_tech_isodep_t* pIsodep, nfc_err_t ret, void* pUserData);
static void iso_dep_transmitted_cb(nfc_tech_isodep_t* pIsodep, nfc_err_t ret, void* pUserData);
static void iso_dep_disconnected_cb(nfc_tech_isodep_t* pIsodep, bool deselected, void* pUserData);
static void iso_dep_received_cb(nfc_tech_isodep_t *pIsodep, nfc_err_t ret, void *pUserData);
static void iso_dep_transmitted_cb(nfc_tech_isodep_t *pIsodep, nfc_err_t ret, void *pUserData);
static void iso_dep_disconnected_cb(nfc_tech_isodep_t *pIsodep, bool deselected, void *pUserData);
static void iso_dep_stream_transmit_cb(buffer_t* ppDataIn, bool* pClose, size_t maxLength, void* pUserParam);
static void iso_dep_stream_receive_cb(buffer_t* pDataOut, bool closed, void* pUserParam);
static void iso_dep_stream_transmit_cb(buffer_t *ppDataIn, bool *pClose, size_t maxLength, void *pUserParam);
static void iso_dep_stream_receive_cb(buffer_t *pDataOut, bool closed, void *pUserParam);
void nfc_tech_iso7816_init(nfc_tech_iso7816_t* pIso7816, nfc_transceiver_t* pTransceiver, nfc_tech_iso7816_disconnected_cb disconnectedCb, void* pUserData)
void nfc_tech_iso7816_init(nfc_tech_iso7816_t *pIso7816, nfc_transceiver_t *pTransceiver, nfc_tech_iso7816_disconnected_cb disconnectedCb, void *pUserData)
{
buffer_init(&pIso7816->hist, NULL, 0);
nfc_tech_isodep_target_init(&pIso7816->isoDepTarget, pTransceiver, &pIso7816->hist, iso_dep_disconnected_cb, pIso7816);
@ -66,7 +66,7 @@ void nfc_tech_iso7816_init(nfc_tech_iso7816_t* pIso7816, nfc_transceiver_t* pTra
pIso7816->pUserData = pUserData;
}
void nfc_tech_iso7816_connect(nfc_tech_iso7816_t* pIso7816)
void nfc_tech_iso7816_connect(nfc_tech_iso7816_t *pIso7816)
{
pIso7816->disconnected = false;
pIso7816->responseReady = true;
@ -75,23 +75,22 @@ void nfc_tech_iso7816_connect(nfc_tech_iso7816_t* pIso7816)
nfc_tech_isodep_target_connect(&pIso7816->isoDepTarget);
}
void nfc_tech_iso7816_disconnect(nfc_tech_iso7816_t* pIso7816)
void nfc_tech_iso7816_disconnect(nfc_tech_iso7816_t *pIso7816)
{
nfc_tech_isodep_target_disconnect(&pIso7816->isoDepTarget);
}
void nfc_tech_iso7816_add_app(nfc_tech_iso7816_t* pIso7816, nfc_tech_iso7816_app_t* pIso7816App)
void nfc_tech_iso7816_add_app(nfc_tech_iso7816_t *pIso7816, nfc_tech_iso7816_app_t *pIso7816App)
{
nfc_tech_iso7816_app_t** ppPrevApp = &pIso7816->pAppList;
while( *ppPrevApp != NULL )
{
nfc_tech_iso7816_app_t **ppPrevApp = &pIso7816->pAppList;
while (*ppPrevApp != NULL) {
ppPrevApp = &((*ppPrevApp)->pNext);
}
*ppPrevApp = pIso7816App;
pIso7816App->pNext = NULL;
}
nfc_err_t nfc_tech_iso7816_reply(nfc_tech_iso7816_t* pIso7816)
nfc_err_t nfc_tech_iso7816_reply(nfc_tech_iso7816_t *pIso7816)
{
nfc_err_t ret;
@ -102,22 +101,20 @@ nfc_err_t nfc_tech_iso7816_reply(nfc_tech_iso7816_t* pIso7816)
buffer_append(&pIso7816->rApdu.dataOut, buffer_builder_buffer(&pIso7816->txBldr));
NFC_DBG("R-ADPU: (LE):%02X SW:%04X", buffer_reader_readable(&pIso7816->rApdu.dataOut), pIso7816->rApdu.sw);
DBG_BLOCK( buffer_dump(&pIso7816->rApdu.dataOut); )
DBG_BLOCK(buffer_dump(&pIso7816->rApdu.dataOut);)
ret = iso7816_transmit(pIso7816);
if(ret)
{
if (ret) {
return ret;
}
return NFC_OK;
}
void iso7816_disconnected(nfc_tech_iso7816_t* pIso7816, bool deselected)
void iso7816_disconnected(nfc_tech_iso7816_t *pIso7816, bool deselected)
{
pIso7816->disconnected = true;
if(pIso7816->pSelectedApp != NULL)
{
if (pIso7816->pSelectedApp != NULL) {
//Deselect previous app
pIso7816->pSelectedApp->deselected(pIso7816->pSelectedApp, pIso7816->pSelectedApp->pUserData);
pIso7816->pSelectedApp = NULL;
@ -125,16 +122,15 @@ void iso7816_disconnected(nfc_tech_iso7816_t* pIso7816, bool deselected)
pIso7816->disconnectedCb(pIso7816, deselected, pIso7816->pUserData);
}
nfc_err_t iso7816_parse(nfc_tech_iso7816_t* pIso7816)
nfc_err_t iso7816_parse(nfc_tech_iso7816_t *pIso7816)
{
//Reset R-APDU
buffer_init(&pIso7816->rApdu.dataOut, NULL, 0);
pIso7816->rApdu.sw = ISO7816_SW_OK;
DBG_BLOCK( buffer_dump(buffer_builder_buffer(&pIso7816->rxBldr)); )
DBG_BLOCK(buffer_dump(buffer_builder_buffer(&pIso7816->rxBldr));)
if( buffer_reader_readable( buffer_builder_buffer(&pIso7816->rxBldr) ) < 4 )
{
if (buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) < 4) {
NFC_ERR("C-APDU is too small");
pIso7816->rApdu.sw = ISO7816_SW_INVALID_CLASS;
nfc_tech_iso7816_reply(pIso7816);
@ -148,46 +144,38 @@ nfc_err_t iso7816_parse(nfc_tech_iso7816_t* pIso7816)
buffer_init(&pIso7816->cApdu.dataIn, NULL, 0);
pIso7816->cApdu.maxRespLength = 0;
if( buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) > 1 )
{
if (buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) > 1) {
size_t lc = buffer_read_nu8(buffer_builder_buffer(&pIso7816->rxBldr));
if( buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) >= lc )
{
if (buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) >= lc) {
buffer_split(&pIso7816->cApdu.dataIn, buffer_builder_buffer(&pIso7816->rxBldr), buffer_builder_buffer(&pIso7816->rxBldr), lc);
}
else
{
} else {
pIso7816->rApdu.sw = ISO7816_SW_WRONG_LENGTH;
nfc_tech_iso7816_reply(pIso7816);
return NFC_ERR_LENGTH; //Not a valid frame
}
}
if( buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) >= 1 )
{
if (buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) >= 1) {
pIso7816->cApdu.maxRespLength = buffer_read_nu8(buffer_builder_buffer(&pIso7816->rxBldr));
}
NFC_DBG("C-APDU: CLA:%02X INS:%02X P1:%02X P2:%02X LC:%02X LE:%02X", pIso7816->cApdu.cla, pIso7816->cApdu.ins, pIso7816->cApdu.p1, pIso7816->cApdu.p2,
buffer_reader_readable(&pIso7816->cApdu.dataIn), pIso7816->cApdu.maxRespLength);
if( buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) > 0 )
{
if (buffer_reader_readable(buffer_builder_buffer(&pIso7816->rxBldr)) > 0) {
pIso7816->rApdu.sw = ISO7816_SW_WRONG_LENGTH;
nfc_tech_iso7816_reply(pIso7816);
return NFC_ERR_LENGTH; //Not a valid frame
}
//See if can select an app
if( iso7816_mf_command(pIso7816) )
{
if (iso7816_mf_command(pIso7816)) {
nfc_tech_iso7816_reply(pIso7816);
return NFC_OK;
}
//Pass command to selected app
if( pIso7816->pSelectedApp == NULL )
{
if (pIso7816->pSelectedApp == NULL) {
pIso7816->rApdu.sw = ISO7816_SW_NOT_FOUND;
nfc_tech_iso7816_reply(pIso7816);
return NFC_ERR_NOT_FOUND; //Not a valid frame
@ -198,13 +186,13 @@ nfc_err_t iso7816_parse(nfc_tech_iso7816_t* pIso7816)
return NFC_OK;
}
void iso7816_receive(nfc_tech_iso7816_t* pIso7816)
void iso7816_receive(nfc_tech_iso7816_t *pIso7816)
{
buffer_builder_reset(&pIso7816->rxBldr);
nfc_tech_isodep_target_receive(&pIso7816->isoDepTarget, &pIso7816->outputStream, iso_dep_received_cb, pIso7816);
}
nfc_err_t iso7816_transmit(nfc_tech_iso7816_t* pIso7816)
nfc_err_t iso7816_transmit(nfc_tech_iso7816_t *pIso7816)
{
return nfc_tech_isodep_target_transmit(&pIso7816->isoDepTarget, &pIso7816->inputStream, iso_dep_transmitted_cb, pIso7816);
}
@ -220,28 +208,21 @@ nfc_err_t iso7816_transmit(nfc_tech_iso7816_t* pIso7816)
* \param SW status word
* \return true if command was handled, false if it should be passed to the selected application
*/
bool iso7816_mf_command(nfc_tech_iso7816_t* pIso7816)
bool iso7816_mf_command(nfc_tech_iso7816_t *pIso7816)
{
nfc_tech_iso7816_app_t* pApp;
if(pIso7816->cApdu.cla != 0x00)
{
nfc_tech_iso7816_app_t *pApp;
if (pIso7816->cApdu.cla != 0x00) {
return false;
}
switch(pIso7816->cApdu.ins)
{
switch (pIso7816->cApdu.ins) {
case ISO7816_INS_SELECT:
switch(pIso7816->cApdu.p1)
{
switch (pIso7816->cApdu.p1) {
case 0x04: //Selection by DF name
pApp = pIso7816->pAppList;
while(pApp != NULL)
{
if( buffer_reader_readable(&pIso7816->cApdu.dataIn) <= pApp->aidSize )
{
if( buffer_reader_cmp_bytes(&pIso7816->cApdu.dataIn, pApp->aid, buffer_reader_readable(&pIso7816->cApdu.dataIn)) )
{
if(pIso7816->pSelectedApp != NULL)
{
while (pApp != NULL) {
if (buffer_reader_readable(&pIso7816->cApdu.dataIn) <= pApp->aidSize) {
if (buffer_reader_cmp_bytes(&pIso7816->cApdu.dataIn, pApp->aid, buffer_reader_readable(&pIso7816->cApdu.dataIn))) {
if (pIso7816->pSelectedApp != NULL) {
//Deselect previous app
pIso7816->pSelectedApp->deselected(pIso7816->pSelectedApp, pIso7816->pSelectedApp->pUserData);
}
@ -256,60 +237,51 @@ bool iso7816_mf_command(nfc_tech_iso7816_t* pIso7816)
pIso7816->rApdu.sw = ISO7816_SW_NOT_FOUND;
return true;
default:
if(pIso7816->pSelectedApp == NULL)
{
if (pIso7816->pSelectedApp == NULL) {
pIso7816->rApdu.sw = ISO7816_SW_NOT_FOUND;
return true;
}
else
{
} else {
return false;
}
}
break;
default:
if(pIso7816->pSelectedApp == NULL)
{
if (pIso7816->pSelectedApp == NULL) {
pIso7816->rApdu.sw = ISO7816_SW_INVALID_INS;
return true;
}
else
{
} else {
return false;
}
break;
}
}
void iso_dep_received_cb(nfc_tech_isodep_t* pIsodep, nfc_err_t ret, void* pUserData)
void iso_dep_received_cb(nfc_tech_isodep_t *pIsodep, nfc_err_t ret, void *pUserData)
{
nfc_tech_iso7816_t* pIso7816 = (nfc_tech_iso7816_t*) pUserData;
nfc_tech_iso7816_t *pIso7816 = (nfc_tech_iso7816_t *) pUserData;
(void) pIsodep;
if( ret )
{
if (ret) {
NFC_WARN("Got error %d", ret);
return;
}
//Parse received APDU
ret = iso7816_parse(pIso7816);
if( ret )
{
if (ret) {
NFC_WARN("Got error %d", ret);
return;
}
}
void iso_dep_transmitted_cb(nfc_tech_isodep_t* pIsodep, nfc_err_t ret, void* pUserData)
void iso_dep_transmitted_cb(nfc_tech_isodep_t *pIsodep, nfc_err_t ret, void *pUserData)
{
nfc_tech_iso7816_t* pIso7816 = (nfc_tech_iso7816_t*) pUserData;
nfc_tech_iso7816_t *pIso7816 = (nfc_tech_iso7816_t *) pUserData;
(void) pIsodep;
if( ret )
{
if (ret) {
NFC_WARN("Got error %d", ret);
return;
}
@ -320,9 +292,9 @@ void iso_dep_transmitted_cb(nfc_tech_isodep_t* pIsodep, nfc_err_t ret, void* pUs
iso7816_receive(pIso7816);
}
void iso_dep_disconnected_cb(nfc_tech_isodep_t* pIsodep, bool deselected, void* pUserData)
void iso_dep_disconnected_cb(nfc_tech_isodep_t *pIsodep, bool deselected, void *pUserData)
{
nfc_tech_iso7816_t* pIso7816 = (nfc_tech_iso7816_t*) pUserData;
nfc_tech_iso7816_t *pIso7816 = (nfc_tech_iso7816_t *) pUserData;
(void) pIsodep;
@ -330,34 +302,31 @@ void iso_dep_disconnected_cb(nfc_tech_isodep_t* pIsodep, bool deselected, void*
iso7816_disconnected(pIso7816, deselected);
}
void iso_dep_stream_transmit_cb(buffer_t* pDataIn, bool* pClose, size_t maxLength, void* pUserParam)
void iso_dep_stream_transmit_cb(buffer_t *pDataIn, bool *pClose, size_t maxLength, void *pUserParam)
{
nfc_tech_iso7816_t* pIso7816 = (nfc_tech_iso7816_t*) pUserParam;
nfc_tech_iso7816_t *pIso7816 = (nfc_tech_iso7816_t *) pUserParam;
//Only close if buffer fits in this frame
if( maxLength >= buffer_reader_readable(&pIso7816->rApdu.dataOut) )
if (maxLength >= buffer_reader_readable(&pIso7816->rApdu.dataOut))
//if( buffer_total_length(&pLlcp->tx) <= maxLength )
{
maxLength = buffer_reader_readable(&pIso7816->rApdu.dataOut);
*pClose = true;
}
else
{
} else {
*pClose = false;
}
buffer_split(pDataIn, &pIso7816->rApdu.dataOut, &pIso7816->rApdu.dataOut, maxLength);
}
void iso_dep_stream_receive_cb(buffer_t* pDataOut, bool closed, void* pUserParam)
void iso_dep_stream_receive_cb(buffer_t *pDataOut, bool closed, void *pUserParam)
{
nfc_tech_iso7816_t* pIso7816 = (nfc_tech_iso7816_t*) pUserParam;
nfc_tech_iso7816_t *pIso7816 = (nfc_tech_iso7816_t *) pUserParam;
(void) closed;
if( buffer_reader_readable(pDataOut) > buffer_builder_writeable(&pIso7816->rxBldr) )
{
NFC_ERR("Frame will not fit (%d > %d)", buffer_reader_readable(pDataOut), buffer_builder_writeable(&pIso7816->rxBldr) );
if (buffer_reader_readable(pDataOut) > buffer_builder_writeable(&pIso7816->rxBldr)) {
NFC_ERR("Frame will not fit (%d > %d)", buffer_reader_readable(pDataOut), buffer_builder_writeable(&pIso7816->rxBldr));
}
//Feed rx buffer

View File

@ -32,8 +32,7 @@ extern "C" {
#include "tech/isodep/isodep_target.h"
struct nfc_tech_iso7816_c_apdu
{
struct nfc_tech_iso7816_c_apdu {
uint8_t cla;
uint8_t ins;
uint8_t p1;
@ -42,8 +41,7 @@ struct nfc_tech_iso7816_c_apdu
size_t maxRespLength;
};
struct nfc_tech_iso7816_r_apdu
{
struct nfc_tech_iso7816_r_apdu {
buffer_t dataOut;
uint16_t sw;
};
@ -55,17 +53,16 @@ typedef struct nfc_tech_iso7816_r_apdu nfc_tech_iso7816_r_apdu_t;
typedef struct nfc_tech_iso7816 nfc_tech_iso7816_t;
typedef void (*nfc_tech_iso7816_disconnected_cb)(nfc_tech_iso7816_t* pIso7816, bool deselected, void* pUserData);
typedef void (*nfc_tech_iso7816_disconnected_cb)(nfc_tech_iso7816_t *pIso7816, bool deselected, void *pUserData);
struct nfc_tech_iso7816_app;
typedef struct nfc_tech_iso7816_app nfc_tech_iso7816_app_t;
struct nfc_tech_iso7816
{
struct nfc_tech_iso7816 {
nfc_tech_isodep_target_t isoDepTarget;
nfc_tech_iso7816_app_t* pAppList;
nfc_tech_iso7816_app_t* pSelectedApp;
nfc_tech_iso7816_app_t *pAppList;
nfc_tech_iso7816_app_t *pSelectedApp;
bool disconnected;
@ -75,7 +72,7 @@ struct nfc_tech_iso7816
bool responseReady;
nfc_tech_iso7816_disconnected_cb disconnectedCb;
void* pUserData;
void *pUserData;
buffer_t hist; //Historical bytes
@ -91,18 +88,18 @@ struct nfc_tech_iso7816
buffer_builder_t rxBldr;
};
void nfc_tech_iso7816_init(nfc_tech_iso7816_t* pIso7816, nfc_transceiver_t* pTransceiver, nfc_tech_iso7816_disconnected_cb disconnectedCb, void* pUserData);
void nfc_tech_iso7816_add_app(nfc_tech_iso7816_t* pIso7816, nfc_tech_iso7816_app_t* pIso7816App);
void nfc_tech_iso7816_connect(nfc_tech_iso7816_t* pIso7816);
void nfc_tech_iso7816_disconnect(nfc_tech_iso7816_t* pIso7816);
nfc_err_t nfc_tech_iso7816_reply(nfc_tech_iso7816_t* pIso7816);
void nfc_tech_iso7816_init(nfc_tech_iso7816_t *pIso7816, nfc_transceiver_t *pTransceiver, nfc_tech_iso7816_disconnected_cb disconnectedCb, void *pUserData);
void nfc_tech_iso7816_add_app(nfc_tech_iso7816_t *pIso7816, nfc_tech_iso7816_app_t *pIso7816App);
void nfc_tech_iso7816_connect(nfc_tech_iso7816_t *pIso7816);
void nfc_tech_iso7816_disconnect(nfc_tech_iso7816_t *pIso7816);
nfc_err_t nfc_tech_iso7816_reply(nfc_tech_iso7816_t *pIso7816);
inline static nfc_tech_iso7816_c_apdu_t* nfc_tech_iso7816_c_apdu(nfc_tech_iso7816_t* pIso7816)
inline static nfc_tech_iso7816_c_apdu_t *nfc_tech_iso7816_c_apdu(nfc_tech_iso7816_t *pIso7816)
{
return &pIso7816->cApdu;
}
inline static nfc_tech_iso7816_r_apdu_t* nfc_tech_iso7816_r_apdu(nfc_tech_iso7816_t* pIso7816)
inline static nfc_tech_iso7816_r_apdu_t *nfc_tech_iso7816_r_apdu(nfc_tech_iso7816_t *pIso7816)
{
return &pIso7816->rApdu;
}

View File

@ -22,14 +22,14 @@
#include "iso7816_app.h"
void nfc_tech_iso7816_app_init(nfc_tech_iso7816_app_t* pIso7816App,
nfc_tech_iso7816_t* pIso7816,
const uint8_t* aid, size_t aidSize,
void nfc_tech_iso7816_app_init(nfc_tech_iso7816_app_t *pIso7816App,
nfc_tech_iso7816_t *pIso7816,
const uint8_t *aid, size_t aidSize,
nfc_tech_iso7816_app_cb selected,
nfc_tech_iso7816_app_cb deselected,
nfc_tech_iso7816_app_cb apdu,
void* pUserData
)
void *pUserData
)
{
pIso7816App->pIso7816 = pIso7816;
pIso7816App->aid = aid;

View File

@ -34,42 +34,41 @@ struct nfc_tech_iso7816_app;
typedef struct nfc_tech_iso7816 nfc_tech_iso7816_t;
typedef struct nfc_tech_iso7816_app nfc_tech_iso7816_app_t;
typedef void (*nfc_tech_iso7816_app_cb)( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData );
typedef void (*nfc_tech_iso7816_app_cb)(nfc_tech_iso7816_app_t *pIso7816App, void *pUserData);
struct nfc_tech_iso7816_app
{
nfc_tech_iso7816_t* pIso7816;
struct nfc_tech_iso7816_app {
nfc_tech_iso7816_t *pIso7816;
const uint8_t* aid;
const uint8_t *aid;
size_t aidSize;
nfc_tech_iso7816_app_cb selected;
nfc_tech_iso7816_app_cb deselected;
nfc_tech_iso7816_app_cb apdu;
void* pUserData;
void *pUserData;
nfc_tech_iso7816_app_t* pNext;
nfc_tech_iso7816_app_t *pNext;
};
void nfc_tech_iso7816_app_init(nfc_tech_iso7816_app_t* pIso7816App, nfc_tech_iso7816_t* pIso7816, const uint8_t* aid, size_t aidSize,
void nfc_tech_iso7816_app_init(nfc_tech_iso7816_app_t *pIso7816App, nfc_tech_iso7816_t *pIso7816, const uint8_t *aid, size_t aidSize,
nfc_tech_iso7816_app_cb selected,
nfc_tech_iso7816_app_cb deselected,
nfc_tech_iso7816_app_cb apdu,
void* pUserData
);
void *pUserData
);
inline static nfc_err_t nfc_tech_iso7816_app_reply(nfc_tech_iso7816_app_t* pIso7816App)
inline static nfc_err_t nfc_tech_iso7816_app_reply(nfc_tech_iso7816_app_t *pIso7816App)
{
return nfc_tech_iso7816_reply(pIso7816App->pIso7816);
}
inline static nfc_tech_iso7816_c_apdu_t* nfc_tech_iso7816_app_c_apdu(nfc_tech_iso7816_app_t* pIso7816App)
inline static nfc_tech_iso7816_c_apdu_t *nfc_tech_iso7816_app_c_apdu(nfc_tech_iso7816_app_t *pIso7816App)
{
return nfc_tech_iso7816_c_apdu(pIso7816App->pIso7816);
}
inline static nfc_tech_iso7816_r_apdu_t* nfc_tech_iso7816_app_r_apdu(nfc_tech_iso7816_app_t* pIso7816App)
inline static nfc_tech_iso7816_r_apdu_t *nfc_tech_iso7816_app_r_apdu(nfc_tech_iso7816_app_t *pIso7816App)
{
return nfc_tech_iso7816_r_apdu(pIso7816App->pIso7816);
}

View File

@ -32,12 +32,11 @@ extern "C" {
struct nfc_tech_isodep;
typedef struct nfc_tech_isodep nfc_tech_isodep_t;
typedef void (*nfc_tech_isodep_cb_t)(nfc_tech_isodep_t* pIsodep, nfc_err_t ret, void* pUserData);
typedef void (*nfc_tech_isodep_disconnected_cb)(nfc_tech_isodep_t* pIsodep, bool deselected, void* pUserData);
typedef void (*nfc_tech_isodep_cb_t)(nfc_tech_isodep_t *pIsodep, nfc_err_t ret, void *pUserData);
typedef void (*nfc_tech_isodep_disconnected_cb)(nfc_tech_isodep_t *pIsodep, bool deselected, void *pUserData);
struct nfc_tech_isodep
{
struct nfc_tech_isodep {
};

View File

@ -74,42 +74,41 @@
#define FWI 14 //Max time before answer is ~ 19.3ms
typedef enum __dep_type dep_type_t;
enum __dep_type
{
enum __dep_type {
dep_type_information,
dep_type_response,
dep_type_supervisory,
};
//Local functions
static void dep_init(nfc_tech_isodep_target_t* pIsodepTarget);
static bool dep_ready(nfc_tech_isodep_target_t* pIsodepTarget);
static void dep_init(nfc_tech_isodep_target_t *pIsodepTarget);
static bool dep_ready(nfc_tech_isodep_target_t *pIsodepTarget);
static void dep_req_information(nfc_tech_isodep_target_t* pIsodepTarget, buffer_t* pReq, bool moreInformation, uint8_t blockNumber);
static void dep_req_response(nfc_tech_isodep_target_t* pIsodepTarget, bool ack, uint8_t blockNumber);
static void dep_req_supervisory(nfc_tech_isodep_target_t* pIsodepTarget, bool wtxNDeselect, uint8_t wtxm);
static void dep_req_information(nfc_tech_isodep_target_t *pIsodepTarget, buffer_t *pReq, bool moreInformation, uint8_t blockNumber);
static void dep_req_response(nfc_tech_isodep_target_t *pIsodepTarget, bool ack, uint8_t blockNumber);
static void dep_req_supervisory(nfc_tech_isodep_target_t *pIsodepTarget, bool wtxNDeselect, uint8_t wtxm);
static dep_type_t dep_res_type(nfc_tech_isodep_target_t* pIsodepTarget);
static void dep_res_information(nfc_tech_isodep_target_t* pIsodepTarget, size_t maxLength, buffer_t** ppRes, bool* pMoreInformation, uint8_t* pBlockNumber);
static void dep_res_response(nfc_tech_isodep_target_t* pIsodepTarget, bool* pAck, uint8_t* pBlockNumber);
static void dep_res_supervisory(nfc_tech_isodep_target_t* pIsodepTarget, bool *pWtxNDeselect, uint8_t* pWtxm);
static dep_type_t dep_res_type(nfc_tech_isodep_target_t *pIsodepTarget);
static void dep_res_information(nfc_tech_isodep_target_t *pIsodepTarget, size_t maxLength, buffer_t **ppRes, bool *pMoreInformation, uint8_t *pBlockNumber);
static void dep_res_response(nfc_tech_isodep_target_t *pIsodepTarget, bool *pAck, uint8_t *pBlockNumber);
static void dep_res_supervisory(nfc_tech_isodep_target_t *pIsodepTarget, bool *pWtxNDeselect, uint8_t *pWtxm);
static void dep_disconnected(nfc_tech_isodep_target_t* pIsodepTarget, bool deselected);
static void dep_disconnected(nfc_tech_isodep_target_t *pIsodepTarget, bool deselected);
static void command_init(nfc_tech_isodep_target_t* pIsodepTarget);
static void command_init(nfc_tech_isodep_target_t *pIsodepTarget);
static nfc_err_t command_ats_req(nfc_tech_isodep_target_t* pIsodepTarget);
static nfc_err_t command_dep_req(nfc_tech_isodep_target_t* pIsodepTarget);
static nfc_err_t command_ats_req(nfc_tech_isodep_target_t *pIsodepTarget);
static nfc_err_t command_dep_req(nfc_tech_isodep_target_t *pIsodepTarget);
static nfc_err_t command_ats_res(nfc_tech_isodep_target_t* pIsodepTarget);
static nfc_err_t command_dep_res(nfc_tech_isodep_target_t* pIsodepTarget);
static nfc_err_t command_ats_res(nfc_tech_isodep_target_t *pIsodepTarget);
static nfc_err_t command_dep_res(nfc_tech_isodep_target_t *pIsodepTarget);
static void command_reply(nfc_tech_isodep_target_t* pIsodepTarget, bool depWait);
static void command_transceiver_cb(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void* pUserData);
static void command_reply(nfc_tech_isodep_target_t *pIsodepTarget, bool depWait);
static void command_transceiver_cb(nfc_transceiver_t *pTransceiver, nfc_err_t ret, void *pUserData);
//High-level Target functions
void nfc_tech_isodep_target_init(nfc_tech_isodep_target_t* pIsodepTarget, nfc_transceiver_t* pTransceiver,
buffer_t* pHist, nfc_tech_isodep_disconnected_cb disconnectedCb, void* pUserData)
void nfc_tech_isodep_target_init(nfc_tech_isodep_target_t *pIsodepTarget, nfc_transceiver_t *pTransceiver,
buffer_t *pHist, nfc_tech_isodep_disconnected_cb disconnectedCb, void *pUserData)
{
pIsodepTarget->pTransceiver = pTransceiver;
@ -122,7 +121,7 @@ void nfc_tech_isodep_target_init(nfc_tech_isodep_target_t* pIsodepTarget, nfc_tr
command_init(pIsodepTarget);
}
nfc_err_t nfc_tech_isodep_target_connect(nfc_tech_isodep_target_t* pIsodepTarget)
nfc_err_t nfc_tech_isodep_target_connect(nfc_tech_isodep_target_t *pIsodepTarget)
{
NFC_DBG("Connecting");
pIsodepTarget->commands.state = ISO_DEP_TARGET_COMMANDS_CONNECTING;
@ -133,7 +132,7 @@ nfc_err_t nfc_tech_isodep_target_connect(nfc_tech_isodep_target_t* pIsodepTarget
return NFC_OK;
}
void nfc_tech_isodep_target_disconnect(nfc_tech_isodep_target_t* pIsodepTarget)
void nfc_tech_isodep_target_disconnect(nfc_tech_isodep_target_t *pIsodepTarget)
{
// This should not be called within a callback
@ -142,10 +141,9 @@ void nfc_tech_isodep_target_disconnect(nfc_tech_isodep_target_t* pIsodepTarget)
dep_disconnected(pIsodepTarget, false);
}
nfc_err_t nfc_tech_isodep_target_transmit(nfc_tech_isodep_target_t* pIsodepTarget, istream_t* pStream, nfc_tech_isodep_cb_t cb, void* pUserData)
nfc_err_t nfc_tech_isodep_target_transmit(nfc_tech_isodep_target_t *pIsodepTarget, istream_t *pStream, nfc_tech_isodep_cb_t cb, void *pUserData)
{
if( pIsodepTarget->dep.pReqStream != NULL )
{
if (pIsodepTarget->dep.pReqStream != NULL) {
return NFC_ERR_BUSY;
}
@ -154,18 +152,16 @@ nfc_err_t nfc_tech_isodep_target_transmit(nfc_tech_isodep_target_t* pIsodepTarge
pIsodepTarget->dep.pResUserData = pUserData;
//Do we need to start transceiving?
if( pIsodepTarget->commands.state == ISO_DEP_TARGET_COMMANDS_DEP_REQ_RECVD )
{
if (pIsodepTarget->commands.state == ISO_DEP_TARGET_COMMANDS_DEP_REQ_RECVD) {
command_reply(pIsodepTarget, false); //Force reply
}
return NFC_OK;
}
nfc_err_t nfc_tech_isodep_target_receive(nfc_tech_isodep_target_t* pIsodepTarget, ostream_t* pStream, nfc_tech_isodep_cb_t cb, void* pUserData)
nfc_err_t nfc_tech_isodep_target_receive(nfc_tech_isodep_target_t *pIsodepTarget, ostream_t *pStream, nfc_tech_isodep_cb_t cb, void *pUserData)
{
if( pIsodepTarget->dep.pResStream != NULL )
{
if (pIsodepTarget->dep.pResStream != NULL) {
return NFC_ERR_BUSY;
}
@ -177,7 +173,7 @@ nfc_err_t nfc_tech_isodep_target_receive(nfc_tech_isodep_target_t* pIsodepTarget
}
//DEP Layer
void dep_init(nfc_tech_isodep_target_t* pIsodepTarget)
void dep_init(nfc_tech_isodep_target_t *pIsodepTarget)
{
//buffer_init(&pIsodepTarget->dep.res, NULL, 0);
pIsodepTarget->dep.pReqStream = NULL;
@ -196,51 +192,44 @@ void dep_init(nfc_tech_isodep_target_t* pIsodepTarget)
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_IDLE;
}
bool dep_ready(nfc_tech_isodep_target_t* pIsodepTarget)
bool dep_ready(nfc_tech_isodep_target_t *pIsodepTarget)
{
//Anything to send back?
if( pIsodepTarget->dep.frameState != ISO_DEP_TARGET_DEP_FRAME_INFORMATION_RECEIVED )
{
if (pIsodepTarget->dep.frameState != ISO_DEP_TARGET_DEP_FRAME_INFORMATION_RECEIVED) {
return true;
}
if( (pIsodepTarget->dep.pResStream != NULL) )
{
if ((pIsodepTarget->dep.pResStream != NULL)) {
return true;
}
else
{
} else {
return false;
}
}
void dep_req_information(nfc_tech_isodep_target_t* pIsodepTarget, buffer_t* pReq, bool moreInformation, uint8_t blockNumber)
void dep_req_information(nfc_tech_isodep_target_t *pIsodepTarget, buffer_t *pReq, bool moreInformation, uint8_t blockNumber)
{
(void) blockNumber;
pIsodepTarget->dep.blockNumber++;
pIsodepTarget->dep.blockNumber%=2;
pIsodepTarget->dep.blockNumber %= 2;
// Note: callbacks can call nfc_tech_isodep_target_transmit() - however we must make sure that we wait AFTER this routine has been processed to actually transmit
// To do so, reset state to ATS_RES_SENT state
pIsodepTarget->commands.state = ISO_DEP_TARGET_COMMANDS_ATS_RES_SENT;
if( !pIsodepTarget->dep.chaining
&& (pIsodepTarget->dep.pResStream != NULL) )
{
if (!pIsodepTarget->dep.chaining
&& (pIsodepTarget->dep.pResStream != NULL)) {
//Sent the full frame
pIsodepTarget->dep.pResStream = NULL;
pIsodepTarget->dep.resCb((nfc_tech_isodep_t*)pIsodepTarget, NFC_OK, pIsodepTarget->dep.pResUserData);
pIsodepTarget->dep.resCb((nfc_tech_isodep_t *)pIsodepTarget, NFC_OK, pIsodepTarget->dep.pResUserData);
}
if( pIsodepTarget->dep.pReqStream != NULL )
{
if (pIsodepTarget->dep.pReqStream != NULL) {
// Pull more
ostream_push(pIsodepTarget->dep.pReqStream, pReq, !moreInformation);
if(!moreInformation)
{
if (!moreInformation) {
//Got the full frame
pIsodepTarget->dep.pReqStream = NULL;
pIsodepTarget->dep.reqCb((nfc_tech_isodep_t*)pIsodepTarget, NFC_OK, pIsodepTarget->dep.pReqUserData);
pIsodepTarget->dep.reqCb((nfc_tech_isodep_t *)pIsodepTarget, NFC_OK, pIsodepTarget->dep.pReqUserData);
}
}
@ -249,74 +238,55 @@ void dep_req_information(nfc_tech_isodep_target_t* pIsodepTarget, buffer_t* pReq
pIsodepTarget->dep.chaining = moreInformation;
}
void dep_req_response(nfc_tech_isodep_target_t* pIsodepTarget, bool ack, uint8_t blockNumber)
void dep_req_response(nfc_tech_isodep_target_t *pIsodepTarget, bool ack, uint8_t blockNumber)
{
if( blockNumber != pIsodepTarget->dep.blockNumber )
{
if (blockNumber != pIsodepTarget->dep.blockNumber) {
//Should be NACK
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_NACK_DIFF_BLOCK_NUMBER_RECEIVED;
}
else
{
if(ack)
{
} else {
if (ack) {
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_ACK_RECEIVED;
}
else
{
} else {
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_NACK_RECEIVED;
}
}
}
void dep_req_supervisory(nfc_tech_isodep_target_t* pIsodepTarget, bool wtxNDeselect, uint8_t wtxm)
void dep_req_supervisory(nfc_tech_isodep_target_t *pIsodepTarget, bool wtxNDeselect, uint8_t wtxm)
{
(void) wtxm;
if(wtxNDeselect)
{
if((pIsodepTarget->dep.frameState != ISO_DEP_TARGET_DEP_FRAME_WTX_SENT))
{
if (wtxNDeselect) {
if ((pIsodepTarget->dep.frameState != ISO_DEP_TARGET_DEP_FRAME_WTX_SENT)) {
NFC_WARN("Unexpected WTX frame");
}
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_WTX_RECEIVED;
}
else
{
} else {
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_DESELECT_RECEIVED;
}
}
dep_type_t dep_res_type(nfc_tech_isodep_target_t* pIsodepTarget)
dep_type_t dep_res_type(nfc_tech_isodep_target_t *pIsodepTarget)
{
dep_type_t depType;
switch( pIsodepTarget->dep.frameState )
{
switch (pIsodepTarget->dep.frameState) {
case ISO_DEP_TARGET_DEP_FRAME_DESELECT_RECEIVED:
depType = dep_type_supervisory; //Deselect
break;
case ISO_DEP_TARGET_DEP_FRAME_INFORMATION_RECEIVED:
case ISO_DEP_TARGET_DEP_FRAME_WTX_RECEIVED:
if( pIsodepTarget->dep.chaining ) //Need to ack?
{
if (pIsodepTarget->dep.chaining) { //Need to ack?
depType = dep_type_response;
}
else if( pIsodepTarget->dep.pResStream != NULL ) //Anything to send back?
{
} else if (pIsodepTarget->dep.pResStream != NULL) { //Anything to send back?
depType = dep_type_information;
}
else
{
} else {
depType = dep_type_supervisory; //WTX
}
break;
case ISO_DEP_TARGET_DEP_FRAME_ACK_RECEIVED:
if(( pIsodepTarget->dep.pResStream != NULL ) && ( pIsodepTarget->dep.chaining ))
{
if ((pIsodepTarget->dep.pResStream != NULL) && (pIsodepTarget->dep.chaining)) {
depType = dep_type_information;
}
else
{
} else {
depType = dep_type_supervisory; //WTX
}
break;
@ -333,20 +303,16 @@ dep_type_t dep_res_type(nfc_tech_isodep_target_t* pIsodepTarget)
return depType;
}
void dep_res_information(nfc_tech_isodep_target_t* pIsodepTarget, size_t maxLength, buffer_t** ppRes, bool* pMoreInformation, uint8_t* pBlockNumber)
void dep_res_information(nfc_tech_isodep_target_t *pIsodepTarget, size_t maxLength, buffer_t **ppRes, bool *pMoreInformation, uint8_t *pBlockNumber)
{
*pBlockNumber = pIsodepTarget->dep.blockNumber;
if( pIsodepTarget->dep.frameState != ISO_DEP_TARGET_DEP_FRAME_NACK_RECEIVED )
{
if( pIsodepTarget->dep.pResStream != NULL )
{
if (pIsodepTarget->dep.frameState != ISO_DEP_TARGET_DEP_FRAME_NACK_RECEIVED) {
if (pIsodepTarget->dep.pResStream != NULL) {
bool lastFrame = true;
istream_pull(pIsodepTarget->dep.pResStream, &pIsodepTarget->dep.res, &lastFrame, maxLength);
pIsodepTarget->dep.chaining = !lastFrame;
}
}
else
{
} else {
//Retransmit previous frame (leave it as it is)
}
*ppRes = &pIsodepTarget->dep.res;
@ -354,7 +320,7 @@ void dep_res_information(nfc_tech_isodep_target_t* pIsodepTarget, size_t maxLeng
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_INFORMATION_SENT;
}
void dep_res_response(nfc_tech_isodep_target_t* pIsodepTarget, bool* pAck, uint8_t* pBlockNumber)
void dep_res_response(nfc_tech_isodep_target_t *pIsodepTarget, bool *pAck, uint8_t *pBlockNumber)
{
//Continue chaining or send ACK
*pAck = true;
@ -362,43 +328,37 @@ void dep_res_response(nfc_tech_isodep_target_t* pIsodepTarget, bool* pAck, uint8
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_ACK_SENT;
}
void dep_res_supervisory(nfc_tech_isodep_target_t* pIsodepTarget, bool *pWtxNDeselect, uint8_t* pWtxm)
void dep_res_supervisory(nfc_tech_isodep_target_t *pIsodepTarget, bool *pWtxNDeselect, uint8_t *pWtxm)
{
if( pIsodepTarget->dep.frameState == ISO_DEP_TARGET_DEP_FRAME_DESELECT_RECEIVED )
{
if (pIsodepTarget->dep.frameState == ISO_DEP_TARGET_DEP_FRAME_DESELECT_RECEIVED) {
*pWtxNDeselect = false;
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_DESELECT_SENT;
}
else
{
} else {
*pWtxNDeselect = true;
*pWtxm = WTXM_DEFAULT;
pIsodepTarget->dep.frameState = ISO_DEP_TARGET_DEP_FRAME_WTX_SENT;
}
}
void dep_disconnected(nfc_tech_isodep_target_t* pIsodepTarget, bool deselected)
void dep_disconnected(nfc_tech_isodep_target_t *pIsodepTarget, bool deselected)
{
//Call callbacks if needed
if( pIsodepTarget->dep.pReqStream != NULL )
{
pIsodepTarget->dep.reqCb((nfc_tech_isodep_t*)pIsodepTarget, NFC_ERR_DISCONNECTED, pIsodepTarget->dep.pReqUserData);
if (pIsodepTarget->dep.pReqStream != NULL) {
pIsodepTarget->dep.reqCb((nfc_tech_isodep_t *)pIsodepTarget, NFC_ERR_DISCONNECTED, pIsodepTarget->dep.pReqUserData);
pIsodepTarget->dep.pReqStream = NULL;
}
if( pIsodepTarget->dep.pReqStream != NULL )
{
pIsodepTarget->dep.resCb((nfc_tech_isodep_t*)pIsodepTarget, NFC_ERR_DISCONNECTED, pIsodepTarget->dep.pResUserData);
if (pIsodepTarget->dep.pReqStream != NULL) {
pIsodepTarget->dep.resCb((nfc_tech_isodep_t *)pIsodepTarget, NFC_ERR_DISCONNECTED, pIsodepTarget->dep.pResUserData);
pIsodepTarget->dep.pResStream = NULL;
}
if( pIsodepTarget->commands.state != ISO_DEP_TARGET_COMMANDS_DISCONNECTED )
{
if (pIsodepTarget->commands.state != ISO_DEP_TARGET_COMMANDS_DISCONNECTED) {
pIsodepTarget->commands.state = ISO_DEP_TARGET_COMMANDS_DISCONNECTED;
pIsodepTarget->disconnectedCb((nfc_tech_isodep_t*)pIsodepTarget, deselected, pIsodepTarget->pUserData);
pIsodepTarget->disconnectedCb((nfc_tech_isodep_t *)pIsodepTarget, deselected, pIsodepTarget->pUserData);
}
}
//Commands Layer
void command_init(nfc_tech_isodep_target_t* pIsodepTarget)
void command_init(nfc_tech_isodep_target_t *pIsodepTarget)
{
buffer_builder_init(&pIsodepTarget->commands.respBldr, pIsodepTarget->commands.respBuf, sizeof(pIsodepTarget->commands.respBuf));
pIsodepTarget->commands.pReq = NULL;
@ -409,16 +369,14 @@ void command_init(nfc_tech_isodep_target_t* pIsodepTarget)
pIsodepTarget->commands.inPayloadSize = 0;
}
nfc_err_t command_ats_req(nfc_tech_isodep_target_t* pIsodepTarget)
nfc_err_t command_ats_req(nfc_tech_isodep_target_t *pIsodepTarget)
{
//Check we are in a correct state -- this should be the first command received
if( pIsodepTarget->commands.state != ISO_DEP_TARGET_COMMANDS_CONNECTING )
{
if (pIsodepTarget->commands.state != ISO_DEP_TARGET_COMMANDS_CONNECTING) {
return NFC_ERR_PROTOCOL;
}
if( buffer_reader_readable(pIsodepTarget->commands.pReq) < 2 )
{
if (buffer_reader_readable(pIsodepTarget->commands.pReq) < 2) {
NFC_ERR("Payload too short");
return NFC_ERR_PROTOCOL;
}
@ -438,15 +396,13 @@ nfc_err_t command_ats_req(nfc_tech_isodep_target_t* pIsodepTarget)
return NFC_OK;
}
nfc_err_t command_dep_req(nfc_tech_isodep_target_t* pIsodepTarget)
nfc_err_t command_dep_req(nfc_tech_isodep_target_t *pIsodepTarget)
{
if( pIsodepTarget->commands.state < ISO_DEP_TARGET_COMMANDS_ATS_RES_SENT )
{
if (pIsodepTarget->commands.state < ISO_DEP_TARGET_COMMANDS_ATS_RES_SENT) {
return NFC_ERR_PROTOCOL;
}
if( buffer_reader_readable(pIsodepTarget->commands.pReq) < 1 )
{
if (buffer_reader_readable(pIsodepTarget->commands.pReq) < 1) {
NFC_ERR("Payload too short");
return NFC_ERR_PROTOCOL;
}
@ -477,8 +433,7 @@ nfc_err_t command_dep_req(nfc_tech_isodep_target_t* pIsodepTarget)
*/
uint8_t wtxm = 0;
switch( PCB_TYPE(pcb) )
{
switch (PCB_TYPE(pcb)) {
case I_BLOCK_PCB:
dep_req_information(pIsodepTarget, pIsodepTarget->commands.pReq, PCB_CHAINING(pcb), PCB_BLOCK_TOGGLE(pcb));
break;
@ -486,10 +441,8 @@ nfc_err_t command_dep_req(nfc_tech_isodep_target_t* pIsodepTarget)
dep_req_response(pIsodepTarget, !PCB_NACK(pcb), PCB_BLOCK_TOGGLE(pcb));
break;
case S_BLOCK_PCB:
if( PCB_WTX(pcb) )
{
if( buffer_reader_readable(pIsodepTarget->commands.pReq) < 1 )
{
if (PCB_WTX(pcb)) {
if (buffer_reader_readable(pIsodepTarget->commands.pReq) < 1) {
NFC_ERR("Payload too short");
return NFC_ERR_PROTOCOL;
}
@ -507,18 +460,17 @@ nfc_err_t command_dep_req(nfc_tech_isodep_target_t* pIsodepTarget)
return NFC_OK;
}
nfc_err_t command_ats_res(nfc_tech_isodep_target_t* pIsodepTarget)
nfc_err_t command_ats_res(nfc_tech_isodep_target_t *pIsodepTarget)
{
//Send ATS back
if (buffer_builder_writeable(&pIsodepTarget->commands.respBldr) < 5 )
{
if (buffer_builder_writeable(&pIsodepTarget->commands.respBldr) < 5) {
return NFC_ERR_BUFFER_TOO_SMALL;
}
buffer_builder_write_nu8(&pIsodepTarget->commands.respBldr, (5 + buffer_size(pIsodepTarget->pHist)) & 0xFF);
//T0
buffer_builder_write_nu8(&pIsodepTarget->commands.respBldr, (0x7 << 4) | FSC_TO_FSCI( FSC )); //TA(1), TB(1) and TC(1) are transmitted
buffer_builder_write_nu8(&pIsodepTarget->commands.respBldr, (0x7 << 4) | FSC_TO_FSCI(FSC)); //TA(1), TB(1) and TC(1) are transmitted
//TA(1)
//For now only 106kbps supported
@ -542,7 +494,7 @@ nfc_err_t command_ats_res(nfc_tech_isodep_target_t* pIsodepTarget)
return NFC_OK;
}
nfc_err_t command_dep_res(nfc_tech_isodep_target_t* pIsodepTarget)
nfc_err_t command_dep_res(nfc_tech_isodep_target_t *pIsodepTarget)
{
uint8_t pcb = 0;
@ -553,7 +505,7 @@ nfc_err_t command_dep_res(nfc_tech_isodep_target_t* pIsodepTarget)
pcb |= PFB_DID;
}*/
buffer_t* pDepBuf = NULL;
buffer_t *pDepBuf = NULL;
bool moreInformation = false;
bool ack = false;
bool wtxNDeselect = false;
@ -562,8 +514,7 @@ nfc_err_t command_dep_res(nfc_tech_isodep_target_t* pIsodepTarget)
size_t maxLength = pIsodepTarget->commands.inPayloadSize - 1;
switch( dep_res_type(pIsodepTarget) )
{
switch (dep_res_type(pIsodepTarget)) {
case dep_type_information:
dep_res_information(pIsodepTarget, maxLength, &pDepBuf, &moreInformation, &blockNumber);
pcb = BUILD_I_BLOCK_PCB(moreInformation, false, false, blockNumber);
@ -579,18 +530,15 @@ nfc_err_t command_dep_res(nfc_tech_isodep_target_t* pIsodepTarget)
}
buffer_builder_write_nu8(&pIsodepTarget->commands.respBldr, pcb);
/*
/*
if( pIsodepTarget->commands.didUsed )
{
buffer_builder_write_nu8(&pIsodepTarget->commands.respBldr, pIsodepTarget->commands.did);
}
*/
if( pDepBuf != NULL )
{
*/
if (pDepBuf != NULL) {
buffer_set_next(buffer_builder_buffer(&pIsodepTarget->commands.respBldr), pDepBuf);
}
else if(wtxNDeselect)
{
} else if (wtxNDeselect) {
buffer_builder_write_nu8(&pIsodepTarget->commands.respBldr, wtxm);
}
@ -599,21 +547,19 @@ nfc_err_t command_dep_res(nfc_tech_isodep_target_t* pIsodepTarget)
return NFC_OK;
}
void command_reply(nfc_tech_isodep_target_t* pIsodepTarget, bool depWait)
void command_reply(nfc_tech_isodep_target_t *pIsodepTarget, bool depWait)
{
nfc_err_t ret;
//Check whether we want to reply or wait for the higher layer to send us something
if((pIsodepTarget->commands.state == ISO_DEP_TARGET_COMMANDS_DEP_REQ_RECVD) && depWait && !dep_ready(pIsodepTarget))
{
if ((pIsodepTarget->commands.state == ISO_DEP_TARGET_COMMANDS_DEP_REQ_RECVD) && depWait && !dep_ready(pIsodepTarget)) {
return;
}
//Reply
buffer_builder_reset(&pIsodepTarget->commands.respBldr);
switch(pIsodepTarget->commands.state)
{
switch (pIsodepTarget->commands.state) {
case ISO_DEP_TARGET_COMMANDS_ATS_REQ_RECVD:
ret = command_ats_res(pIsodepTarget);
break;
@ -627,8 +573,7 @@ void command_reply(nfc_tech_isodep_target_t* pIsodepTarget, bool depWait)
return;
}
if(ret)
{
if (ret) {
NFC_ERR("Error %d", ret);
//Go back to receive mode
nfc_transceiver_transceive(pIsodepTarget->pTransceiver, command_transceiver_cb, pIsodepTarget);
@ -637,16 +582,13 @@ void command_reply(nfc_tech_isodep_target_t* pIsodepTarget, bool depWait)
NFC_DBG("Transceive");
if( pIsodepTarget->dep.frameState == ISO_DEP_TARGET_DEP_FRAME_DESELECT_SENT )
{
if (pIsodepTarget->dep.frameState == ISO_DEP_TARGET_DEP_FRAME_DESELECT_SENT) {
transceiver_set_transceive_options(pIsodepTarget->pTransceiver, true, false, true);
}
else
{
} else {
transceiver_set_transceive_options(pIsodepTarget->pTransceiver, true, true, false);
}
DBG_BLOCK( buffer_dump(buffer_builder_buffer(&pIsodepTarget->commands.respBldr)); )
DBG_BLOCK(buffer_dump(buffer_builder_buffer(&pIsodepTarget->commands.respBldr));)
//Send next frame
transceiver_set_write(pIsodepTarget->pTransceiver, buffer_builder_buffer(&pIsodepTarget->commands.respBldr));
@ -655,18 +597,16 @@ void command_reply(nfc_tech_isodep_target_t* pIsodepTarget, bool depWait)
NFC_DBG("Processed");
}
void command_transceiver_cb(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void* pUserData)
void command_transceiver_cb(nfc_transceiver_t *pTransceiver, nfc_err_t ret, void *pUserData)
{
nfc_tech_isodep_target_t* pIsodepTarget = (nfc_tech_isodep_target_t*) pUserData;
nfc_tech_isodep_target_t *pIsodepTarget = (nfc_tech_isodep_target_t *) pUserData;
if( ret == NFC_ERR_ABORTED )
{
if (ret == NFC_ERR_ABORTED) {
// Just return
return;
}
if( pIsodepTarget->dep.frameState == ISO_DEP_TARGET_DEP_FRAME_DESELECT_SENT )
{
if (pIsodepTarget->dep.frameState == ISO_DEP_TARGET_DEP_FRAME_DESELECT_SENT) {
//We are now disconnected (deselected)
//Reset status
dep_init(pIsodepTarget);
@ -684,14 +624,11 @@ void command_transceiver_cb(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void
transceiver_set_write(pTransceiver, NULL);
transceiver_set_transceive_options(pTransceiver, false, true, false);
if( ret == NFC_ERR_FIELD )
{
if (ret == NFC_ERR_FIELD) {
NFC_WARN("Lost initiator");
dep_disconnected(pIsodepTarget, false);
return;
}
else if(ret)
{
} else if (ret) {
//We should ignore this error and wait for another frame
NFC_WARN("Got invalid frame (error %d)", ret);
@ -700,13 +637,12 @@ void command_transceiver_cb(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void
}
NFC_DBG("Reading data from initiator");
buffer_t* pDataInitiator = transceiver_get_read(pTransceiver); //In buffer
buffer_t *pDataInitiator = transceiver_get_read(pTransceiver); //In buffer
DBG_BLOCK( buffer_dump(pDataInitiator); )
DBG_BLOCK(buffer_dump(pDataInitiator);)
//Framing is handled by transceiver
if( (buffer_reader_readable(pDataInitiator) < 1) )
{
if ((buffer_reader_readable(pDataInitiator) < 1)) {
NFC_ERR("Empty initiator message");
//Go back to receive mode
@ -721,8 +657,7 @@ void command_transceiver_cb(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void
buffer_dup(&dataInitiatorDup, pDataInitiator);
uint8_t req = buffer_read_nu8(&dataInitiatorDup);
switch(req)
{
switch (req) {
case RATS:
ret = command_ats_req(pIsodepTarget);
break;
@ -731,8 +666,7 @@ void command_transceiver_cb(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void
break;
}
if(ret)
{
if (ret) {
NFC_ERR("Error %d", ret);
//Go back to receive mode

View File

@ -35,29 +35,26 @@ struct nfc_tech_isodep_target;
typedef struct nfc_tech_isodep_target nfc_tech_isodep_target_t;
typedef struct nfc_tech_isodep_target nfc_tech_isodep_target_t;
struct nfc_tech_isodep_target
{
struct nfc_tech_isodep_target {
nfc_tech_isodep_t isodep;
nfc_transceiver_t* pTransceiver;
nfc_transceiver_t *pTransceiver;
struct
{
ostream_t* pReqStream;
istream_t* pResStream;
struct {
ostream_t *pReqStream;
istream_t *pResStream;
nfc_tech_isodep_cb_t reqCb;
void* pReqUserData;
void *pReqUserData;
nfc_tech_isodep_cb_t resCb;
void* pResUserData;
void *pResUserData;
buffer_t res;
bool chaining;
uint8_t blockNumber;
enum
{
enum {
ISO_DEP_TARGET_DEP_FRAME_IDLE,
ISO_DEP_TARGET_DEP_FRAME_WTX_RECEIVED,
ISO_DEP_TARGET_DEP_FRAME_WTX_SENT,
@ -72,10 +69,8 @@ struct nfc_tech_isodep_target
ISO_DEP_TARGET_DEP_FRAME_DESELECT_SENT,
} frameState;
} dep;
struct
{
enum
{
struct {
enum {
ISO_DEP_TARGET_COMMANDS_DISCONNECTED,
ISO_DEP_TARGET_COMMANDS_CONNECTING,
@ -92,24 +87,24 @@ struct nfc_tech_isodep_target
buffer_builder_t respBldr;
uint8_t respBuf[32];
buffer_t* pReq;
buffer_t *pReq;
} commands;
buffer_t* pHist;
buffer_t *pHist;
nfc_tech_isodep_disconnected_cb disconnectedCb;
void* pUserData;
void *pUserData;
};
//High-level Target functions
void nfc_tech_isodep_target_init(nfc_tech_isodep_target_t* pIsodepTarget, nfc_transceiver_t* pTransceiver,
buffer_t* pHist, nfc_tech_isodep_disconnected_cb disconnectedCb, void* pUserData);
void nfc_tech_isodep_target_init(nfc_tech_isodep_target_t *pIsodepTarget, nfc_transceiver_t *pTransceiver,
buffer_t *pHist, nfc_tech_isodep_disconnected_cb disconnectedCb, void *pUserData);
nfc_err_t nfc_tech_isodep_target_connect(nfc_tech_isodep_target_t* pIsodepTarget);
void nfc_tech_isodep_target_disconnect(nfc_tech_isodep_target_t* pIsodepTarget);
nfc_err_t nfc_tech_isodep_target_connect(nfc_tech_isodep_target_t *pIsodepTarget);
void nfc_tech_isodep_target_disconnect(nfc_tech_isodep_target_t *pIsodepTarget);
nfc_err_t nfc_tech_isodep_target_transmit(nfc_tech_isodep_target_t* pIsodepTarget, istream_t* pStream, nfc_tech_isodep_cb_t cb, void* pUserData);
nfc_err_t nfc_tech_isodep_target_receive(nfc_tech_isodep_target_t* pIsodepTarget, ostream_t* pStream, nfc_tech_isodep_cb_t cb, void* pUserData);
nfc_err_t nfc_tech_isodep_target_transmit(nfc_tech_isodep_target_t *pIsodepTarget, istream_t *pStream, nfc_tech_isodep_cb_t cb, void *pUserData);
nfc_err_t nfc_tech_isodep_target_receive(nfc_tech_isodep_target_t *pIsodepTarget, ostream_t *pStream, nfc_tech_isodep_cb_t cb, void *pUserData);
#ifdef __cplusplus

View File

@ -41,14 +41,14 @@ static const uint8_t aid[] = { 0xD2, 0x76, 0x00, 0x00, 0x85, 0x01, 0x00 };
#define NDEF_FILE 0xA443
#define DEFAULT_FILE 0x0000
static void app_selected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData );
static void app_deselected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData );
static void app_apdu( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData );
static void app_selected(nfc_tech_iso7816_app_t *pIso7816App, void *pUserData);
static void app_deselected(nfc_tech_iso7816_app_t *pIso7816App, void *pUserData);
static void app_apdu(nfc_tech_iso7816_app_t *pIso7816App, void *pUserData);
static nfc_err_t data_read(nfc_tech_type4_target_t* pType4Target, buffer_t* pBuf, uint16_t file, size_t off, size_t len);
static nfc_err_t data_write(nfc_tech_type4_target_t* pType4Target, buffer_t* pBuf, uint16_t file, size_t off);
static nfc_err_t data_read(nfc_tech_type4_target_t *pType4Target, buffer_t *pBuf, uint16_t file, size_t off, size_t len);
static nfc_err_t data_write(nfc_tech_type4_target_t *pType4Target, buffer_t *pBuf, uint16_t file, size_t off);
void nfc_tech_type4_target_init(nfc_tech_type4_target_t* pType4Target, nfc_tech_iso7816_t* pIso7816, ndef_msg_t* pNdef)
void nfc_tech_type4_target_init(nfc_tech_type4_target_t *pType4Target, nfc_tech_iso7816_t *pIso7816, ndef_msg_t *pNdef)
{
buffer_builder_init(&pType4Target->ccFileBldr, pType4Target->ccFileBuf, /*sizeof(pType4Target->ccFileBuf)*/15);
@ -63,9 +63,9 @@ void nfc_tech_type4_target_init(nfc_tech_type4_target_t* pType4Target, nfc_tech_
nfc_tech_iso7816_add_app(pIso7816, &pType4Target->app);
}
void app_selected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
void app_selected(nfc_tech_iso7816_app_t *pIso7816App, void *pUserData)
{
nfc_tech_type4_target_t* pType4Target = (nfc_tech_type4_target_t*) pUserData;
nfc_tech_type4_target_t *pType4Target = (nfc_tech_type4_target_t *) pUserData;
DBG("Selected");
(void) pIso7816App;
@ -74,20 +74,20 @@ void app_selected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
//Populate CC file
buffer_builder_reset(&pType4Target->ccFileBldr);
buffer_builder_write_nu16( &pType4Target->ccFileBldr, 15 ); //CC file is 15 bytes long
buffer_builder_write_nu16(&pType4Target->ccFileBldr, 15); //CC file is 15 bytes long
#if TYPE4_NDEF_VERSION == 2
buffer_builder_write_nu8( &pType4Target->ccFileBldr, 0x20 ); //NFC Forum Tag Type 4 V2.0 compliant
buffer_builder_write_nu8(&pType4Target->ccFileBldr, 0x20); //NFC Forum Tag Type 4 V2.0 compliant
#else
buffer_builder_write_nu8( &pType4Target->ccFileBldr, 0x10 ); //NFC Forum Tag Type 4 V1.0 compliant
buffer_builder_write_nu8(&pType4Target->ccFileBldr, 0x10); //NFC Forum Tag Type 4 V1.0 compliant
#endif
buffer_builder_write_nu16( &pType4Target->ccFileBldr, 256 /* Max frame size */ - 2 /* SW */ - 3 /* ISO-DEP PFB + DID + NAD */ ); //Max data size that can be read from the tag
buffer_builder_write_nu16( &pType4Target->ccFileBldr, 256 /* Max frame size */ - 6 /* CLA INS P1 P2 LC LE */ - 3 /* ISO-DEP PFB + DID + NAD */ ); //Max data size that can be written to the tag
buffer_builder_write_nu8( &pType4Target->ccFileBldr, 0x04 ); //NDEF File Control TLV - Type
buffer_builder_write_nu8( &pType4Target->ccFileBldr, 6 ); //NDEF File Control TLV - Length
buffer_builder_write_nu16( &pType4Target->ccFileBldr, NDEF_FILE ); //NDEF file id
buffer_builder_write_nu16( &pType4Target->ccFileBldr, 2 /* length header */ + buffer_builder_writeable( ndef_msg_buffer_builder(pType4Target->pNdef) ) ); //Max size of NDEF data
buffer_builder_write_nu8( &pType4Target->ccFileBldr, 0x00 ); //Open read access
buffer_builder_write_nu8( &pType4Target->ccFileBldr, 0x00 ); //Open write access
buffer_builder_write_nu16(&pType4Target->ccFileBldr, 256 /* Max frame size */ - 2 /* SW */ - 3 /* ISO-DEP PFB + DID + NAD */); //Max data size that can be read from the tag
buffer_builder_write_nu16(&pType4Target->ccFileBldr, 256 /* Max frame size */ - 6 /* CLA INS P1 P2 LC LE */ - 3 /* ISO-DEP PFB + DID + NAD */); //Max data size that can be written to the tag
buffer_builder_write_nu8(&pType4Target->ccFileBldr, 0x04); //NDEF File Control TLV - Type
buffer_builder_write_nu8(&pType4Target->ccFileBldr, 6); //NDEF File Control TLV - Length
buffer_builder_write_nu16(&pType4Target->ccFileBldr, NDEF_FILE); //NDEF file id
buffer_builder_write_nu16(&pType4Target->ccFileBldr, 2 /* length header */ + buffer_builder_writeable(ndef_msg_buffer_builder(pType4Target->pNdef))); //Max size of NDEF data
buffer_builder_write_nu8(&pType4Target->ccFileBldr, 0x00); //Open read access
buffer_builder_write_nu8(&pType4Target->ccFileBldr, 0x00); //Open write access
//Encode NDEF file
ndef_msg_encode(pType4Target->pNdef);
@ -95,11 +95,10 @@ void app_selected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
//Populate NDEF file
buffer_builder_init(&pType4Target->ndefFileBldr, pType4Target->ndefFileBuf, /*sizeof(pType4Target->ndefFileBuf)*/2);
buffer_builder_write_nu16( &pType4Target->ndefFileBldr, buffer_reader_readable( buffer_builder_buffer(ndef_msg_buffer_builder(pType4Target->pNdef)) ) );
buffer_builder_write_nu16(&pType4Target->ndefFileBldr, buffer_reader_readable(buffer_builder_buffer(ndef_msg_buffer_builder(pType4Target->pNdef))));
//Pad NDEF file with 0s
while( buffer_builder_writeable( ndef_msg_buffer_builder(pType4Target->pNdef) ) > 0 )
{
while (buffer_builder_writeable(ndef_msg_buffer_builder(pType4Target->pNdef)) > 0) {
buffer_builder_write_nu8(ndef_msg_buffer_builder(pType4Target->pNdef), 0);
}
@ -109,9 +108,9 @@ void app_selected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
pType4Target->written = false;
}
void app_deselected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
void app_deselected(nfc_tech_iso7816_app_t *pIso7816App, void *pUserData)
{
nfc_tech_type4_target_t* pType4Target = (nfc_tech_type4_target_t*) pUserData;
nfc_tech_type4_target_t *pType4Target = (nfc_tech_type4_target_t *) pUserData;
(void) pIso7816App;
@ -122,69 +121,57 @@ void app_deselected( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
DBG("Deselected");
if(pType4Target->written)
{
if (pType4Target->written) {
DBG("New content has been written");
//Try to parse NDEF
//Set buffer length based on file header
size_t length = buffer_read_nu16(buffer_builder_buffer(&pType4Target->ndefFileBldr));
DBG("Length is %lu", length);
if( length < buffer_builder_writeable( ndef_msg_buffer_builder(pType4Target->pNdef) ))
{
buffer_builder_set_write_offset( ndef_msg_buffer_builder(pType4Target->pNdef), length );
if (length < buffer_builder_writeable(ndef_msg_buffer_builder(pType4Target->pNdef))) {
buffer_builder_set_write_offset(ndef_msg_buffer_builder(pType4Target->pNdef), length);
ndef_msg_decode(pType4Target->pNdef);
}
else
{
} else {
ERR("Invalid length");
}
}
}
void app_apdu( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
void app_apdu(nfc_tech_iso7816_app_t *pIso7816App, void *pUserData)
{
nfc_tech_type4_target_t* pType4Target = (nfc_tech_type4_target_t*) pUserData;
nfc_tech_type4_target_t *pType4Target = (nfc_tech_type4_target_t *) pUserData;
//Reset buffers
buffer_builder_set_full(&pType4Target->ccFileBldr);
buffer_builder_set_full(&pType4Target->ndefFileBldr);
buffer_builder_set_full( ndef_msg_buffer_builder(pType4Target->pNdef) ); //Set offset to 0, size to max
buffer_builder_set_full(ndef_msg_buffer_builder(pType4Target->pNdef)); //Set offset to 0, size to max
buffer_set_next(buffer_builder_buffer(&pType4Target->ndefFileBldr), buffer_builder_buffer(ndef_msg_buffer_builder(pType4Target->pNdef)));
//Recover PDU
nfc_tech_iso7816_c_apdu_t* pCApdu = nfc_tech_iso7816_app_c_apdu(pIso7816App);
nfc_tech_iso7816_r_apdu_t* pRApdu = nfc_tech_iso7816_app_r_apdu(pIso7816App);
nfc_tech_iso7816_c_apdu_t *pCApdu = nfc_tech_iso7816_app_c_apdu(pIso7816App);
nfc_tech_iso7816_r_apdu_t *pRApdu = nfc_tech_iso7816_app_r_apdu(pIso7816App);
nfc_err_t ret;
switch(pCApdu->ins)
{
switch (pCApdu->ins) {
case ISO7816_INS_SELECT:
switch (pCApdu->p1)
{
switch (pCApdu->p1) {
case 0x00: //Selection by ID
case 0x02: //Selection by child ID
if ( buffer_reader_readable(&pCApdu->dataIn) != 2)
{
if (buffer_reader_readable(&pCApdu->dataIn) != 2) {
pRApdu->sw = ISO7816_SW_NOT_FOUND;
break;
}
uint16_t file = buffer_read_nu16(&pCApdu->dataIn);
if ( file == NDEF_FILE )
{
if (file == NDEF_FILE) {
pType4Target->selFile = NDEF_FILE;
DBG("NDEF File selected");
pRApdu->sw = ISO7816_SW_OK;
}
else if ( file == CC_FILE )
{
} else if (file == CC_FILE) {
pType4Target->selFile = CC_FILE;
DBG("CC File selected");
pRApdu->sw = ISO7816_SW_OK;
}
else
{
} else {
//file = DEFAULT_FILE;
DBG("Could not select file %04X", file);
pRApdu->sw = ISO7816_SW_NOT_FOUND;
@ -198,14 +185,11 @@ void app_apdu( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
case 0xB0: //Read binary
DBG("Trying to read %d bytes at offset %d from file %04x", pCApdu->maxRespLength, (pCApdu->p1 << 8) | pCApdu->p2, pType4Target->selFile);
ret = data_read(pType4Target, &pRApdu->dataOut, pType4Target->selFile, (pCApdu->p1 << 8) | pCApdu->p2, pCApdu->maxRespLength);
if (ret == NFC_OK)
{
if (ret == NFC_OK) {
DBG("Read %d bytes", buffer_reader_readable(&pRApdu->dataOut));
DBG_BLOCK( buffer_dump(&pRApdu->dataOut); )
DBG_BLOCK(buffer_dump(&pRApdu->dataOut);)
pRApdu->sw = ISO7816_SW_OK;
}
else
{
} else {
DBG("Failed with ret code %d", ret);
pRApdu->sw = ISO7816_SW_WRONG_LENGTH;
}
@ -213,14 +197,11 @@ void app_apdu( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
case 0xD6: //Update binary
DBG("Trying to write %d bytes at offset %d to file %04x", buffer_reader_readable(&pCApdu->dataIn), (pCApdu->p1 << 8) | pCApdu->p2, pType4Target->selFile);
ret = data_write(pType4Target, &pCApdu->dataIn, pType4Target->selFile, (pCApdu->p1 << 8) | pCApdu->p2);
if (ret == NFC_OK)
{
if (ret == NFC_OK) {
DBG("OK");
pRApdu->sw = ISO7816_SW_OK;
pType4Target->written = true;
}
else
{
} else {
DBG("Failed with ret code %d", ret);
pRApdu->sw = ISO7816_SW_WRONG_LENGTH;
}
@ -234,11 +215,10 @@ void app_apdu( nfc_tech_iso7816_app_t* pIso7816App, void* pUserData )
nfc_tech_iso7816_app_reply(pIso7816App);
}
nfc_err_t data_read(nfc_tech_type4_target_t* pType4Target, buffer_t* pBuf, uint16_t file, size_t off, size_t len)
nfc_err_t data_read(nfc_tech_type4_target_t *pType4Target, buffer_t *pBuf, uint16_t file, size_t off, size_t len)
{
buffer_t* pFile;
switch(file)
{
buffer_t *pFile;
switch (file) {
case CC_FILE:
pFile = buffer_builder_buffer(&pType4Target->ccFileBldr);
break;
@ -249,15 +229,13 @@ nfc_err_t data_read(nfc_tech_type4_target_t* pType4Target, buffer_t* pBuf, uint1
return NFC_ERR_NOT_FOUND;
}
if( off > buffer_reader_readable(pFile) )
{
if (off > buffer_reader_readable(pFile)) {
return NFC_ERR_LENGTH;
}
buffer_read_n_skip(pFile, off);
if( len > buffer_reader_readable(pFile))
{
if (len > buffer_reader_readable(pFile)) {
len = buffer_reader_readable(pFile);
}
@ -266,11 +244,10 @@ nfc_err_t data_read(nfc_tech_type4_target_t* pType4Target, buffer_t* pBuf, uint1
return NFC_OK;
}
nfc_err_t data_write(nfc_tech_type4_target_t* pType4Target, buffer_t* pBuf, uint16_t file, size_t off)
nfc_err_t data_write(nfc_tech_type4_target_t *pType4Target, buffer_t *pBuf, uint16_t file, size_t off)
{
buffer_t* pFile;
switch(file)
{
buffer_t *pFile;
switch (file) {
case NDEF_FILE:
pFile = buffer_builder_buffer(&pType4Target->ndefFileBldr);
break;
@ -281,20 +258,17 @@ nfc_err_t data_write(nfc_tech_type4_target_t* pType4Target, buffer_t* pBuf, uint
size_t len = buffer_reader_readable(pBuf);
if( off > buffer_reader_readable(pFile) )
{
if (off > buffer_reader_readable(pFile)) {
return NFC_ERR_LENGTH;
}
buffer_read_n_skip(pFile, off);
if( len > buffer_reader_readable(pFile) )
{
if (len > buffer_reader_readable(pFile)) {
len = buffer_reader_readable(pFile);
}
while( len > 0 )
{
while (len > 0) {
size_t cpy;
buffer_builder_t builder;
buffer_dup(buffer_builder_buffer(&builder), pFile);

View File

@ -35,13 +35,12 @@ extern "C" {
typedef struct nfc_tech_type4_target nfc_tech_type4_target_t;
typedef void (*nfc_tech_type4_cb)(nfc_tech_type4_target_t* pType4Target, nfc_err_t ret, void* pUserData);
typedef void (*nfc_tech_type4_cb)(nfc_tech_type4_target_t *pType4Target, nfc_err_t ret, void *pUserData);
struct nfc_tech_type4_target
{
struct nfc_tech_type4_target {
nfc_tech_iso7816_app_t app;
ndef_msg_t* pNdef;
ndef_msg_t *pNdef;
uint8_t ccFileBuf[15];
buffer_builder_t ccFileBldr;
@ -54,7 +53,7 @@ struct nfc_tech_type4_target
bool written;
};
void nfc_tech_type4_target_init(nfc_tech_type4_target_t* pType4Target, nfc_tech_iso7816_t* pIso7816, ndef_msg_t* pNdef);
void nfc_tech_type4_target_init(nfc_tech_type4_target_t *pType4Target, nfc_tech_iso7816_t *pIso7816, ndef_msg_t *pNdef);
#ifdef __cplusplus
}

View File

@ -60,8 +60,7 @@
//PN 512 VTABLE
static const transceiver_impl_t pn512_impl =
{
static const transceiver_impl_t pn512_impl = {
.set_protocols = pn512_set_protocols,
.poll = pn512_poll,
.transceive = pn512_transceive,
@ -84,19 +83,19 @@ static const transceiver_impl_t pn512_impl =
* \param pTransport pointer to already initialized nfc_transport_t structure
* \return NFC_OK (0) on success or NFC_ERR_* error on failure
*/
nfc_err_t pn512_init(pn512_t* pPN512, nfc_transport_t* pTransport, nfc_scheduler_timer_t* pTimer)
nfc_err_t pn512_init(pn512_t *pPN512, nfc_transport_t *pTransport, nfc_scheduler_timer_t *pTimer)
{
////
//For Self-test
////
#if NFC_PN512_SELFTEST
#if NFC_PN512_SELFTEST
const uint8_t null_array[25] = {0};
#endif
#endif
////
uint8_t r;
//Init transceiver
transceiver_init((nfc_transceiver_t*)pPN512, pTransport, pTimer);
transceiver_init((nfc_transceiver_t *)pPN512, pTransport, pTimer);
//Init buffer
buffer_builder_init(&pPN512->readBufBldr, pPN512->payload, 256);
@ -141,26 +140,25 @@ nfc_err_t pn512_init(pn512_t* pPN512, nfc_transport_t* pTransport, nfc_scheduler
pn512_cmd_exec(pPN512, PN512_CMD_SOFTRST);
pn512_cmd_wait_idle(pPN512, -1);
const uint8_t null_array_buf[25]={0}; //FIXME
const uint8_t null_array_buf[25] = {0}; //FIXME
buffer_t null_array;
buffer_init(&null_array, null_array_buf, 25);
//Perform self test
pn512_fifo_write(pPN512, &null_array);
pn512_cmd_exec(pPN512, PN512_CMD_CONFIG);
while(pn512_cmd_get(pPN512) != PN512_CMD_IDLE);
while (pn512_cmd_get(pPN512) != PN512_CMD_IDLE);
pn512_register_write(pPN512, PN512_REG_AUTOTEST, 0x09);
buffer_init(&null_array, null_array_buf, 1);
pn512_fifo_write(pPN512, &null_array);
pn512_cmd_exec(pPN512, PN512_CMD_CRC);
while(pn512_cmd_get(pPN512) != PN512_CMD_IDLE);
while (pn512_cmd_get(pPN512) != PN512_CMD_IDLE);
DBGX_ENTER();
NFC_DBG("Test result:");
while(pn512_fifo_length(pPN512))
{
while (pn512_fifo_length(pPN512)) {
buffer_builder_t read_byte;
buffer_builder_init(&read_byte, null_array_buf, 1);
@ -177,8 +175,7 @@ nfc_err_t pn512_init(pn512_t* pPN512, nfc_transport_t* pTransport, nfc_scheduler
NFC_DBG("PN512 version %02x", r);
)
if((r != 0x82) && (r != 0xB1) && (r != 0xB2))
{
if ((r != 0x82) && (r != 0xB1) && (r != 0xB2)) {
return NFC_ERR_UNSUPPORTED; //PN512 not found
}
@ -189,20 +186,18 @@ nfc_err_t pn512_init(pn512_t* pPN512, nfc_transport_t* pTransport, nfc_scheduler
* \param pPN512 pointer to pn512_t instance
* \return pointer to initialized nfc_transceiver_t instance
*/
nfc_transceiver_t* pn512_get_transceiver(pn512_t* pPN512)
nfc_transceiver_t *pn512_get_transceiver(pn512_t *pPN512)
{
return &pPN512->transceiver;
}
void pn512_set_protocols(nfc_transceiver_t* pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options)
void pn512_set_protocols(nfc_transceiver_t *pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
//If different, reconfigure
if( memcmp(&initiators, &pPN512->config.initiators, sizeof(nfc_tech_t)) || memcmp(&targets, &pPN512->config.targets, sizeof(nfc_tech_t)) )
{
if (memcmp(&initiators, &pPN512->config.initiators, sizeof(nfc_tech_t)) || memcmp(&targets, &pPN512->config.targets, sizeof(nfc_tech_t))) {
pPN512->config.initiators = initiators;
if( memcmp(&targets, &pPN512->config.targets, sizeof(nfc_tech_t)) )
{
if (memcmp(&targets, &pPN512->config.targets, sizeof(nfc_tech_t))) {
pPN512->config.targets = targets;
pn512_poll_setup(pPN512);
}
@ -212,66 +207,55 @@ void pn512_set_protocols(nfc_transceiver_t* pTransceiver, nfc_tech_t initiators,
pPN512->config.options = options;
}
void pn512_poll(nfc_transceiver_t* pTransceiver)
void pn512_poll(nfc_transceiver_t *pTransceiver)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
pPN512->nextFrameMode = pn512_transceive_mode_transceive;
return pn512_poll_hw(pPN512, pn512_transceiver_callback);
}
void pn512_set_crc(nfc_transceiver_t* pTransceiver, bool crc_out, bool crc_in)
void pn512_set_crc(nfc_transceiver_t *pTransceiver, bool crc_out, bool crc_in)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
pn512_framing_crc_set(pPN512, crc_out, crc_in);
}
void pn512_set_timeout(nfc_transceiver_t* pTransceiver, int timeout)
void pn512_set_timeout(nfc_transceiver_t *pTransceiver, int timeout)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
pPN512->timeout = timeout;
}
void pn512_set_transceive_options(nfc_transceiver_t* pTransceiver, bool transmit, bool receive, bool repoll)
void pn512_set_transceive_options(nfc_transceiver_t *pTransceiver, bool transmit, bool receive, bool repoll)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
if( transmit && receive )
{
pn512_t *pPN512 = (pn512_t *) pTransceiver;
if (transmit && receive) {
pPN512->nextFrameMode = pn512_transceive_mode_transceive;
}
else if(transmit && repoll)
{
} else if (transmit && repoll) {
pPN512->nextFrameMode = pn512_transceive_mode_transmit_and_target_autocoll;
}
else if(transmit)
{
} else if (transmit) {
pPN512->nextFrameMode = pn512_transceive_mode_transmit;
}
else if(receive)
{
} else if (receive) {
pPN512->nextFrameMode = pn512_transceive_mode_receive;
}
else
{
} else {
pPN512->nextFrameMode = pn512_transceive_mode_target_autocoll;
}
}
void pn512_set_transceive_framing(nfc_transceiver_t* pTransceiver, nfc_framing_t framing)
void pn512_set_transceive_framing(nfc_transceiver_t *pTransceiver, nfc_framing_t framing)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
pn512_framing_set(pPN512, framing);
//Switch NFC tech if NFC DEP
if( pTransceiver->active_tech.nfc_nfc_dep_a
if (pTransceiver->active_tech.nfc_nfc_dep_a
|| pTransceiver->active_tech.nfc_nfc_dep_f_212
|| pTransceiver->active_tech.nfc_nfc_dep_f_424 )
{
|| pTransceiver->active_tech.nfc_nfc_dep_f_424) {
//FIXME
pTransceiver->active_tech.nfc_nfc_dep_a = 0;
pTransceiver->active_tech.nfc_nfc_dep_f_212 = 0;
pTransceiver->active_tech.nfc_nfc_dep_f_424 = 0;
switch(framing)
{
switch (framing) {
case nfc_framing_target_a_106:
case nfc_framing_initiator_a_106:
pTransceiver->active_tech.nfc_nfc_dep_a = 1;
@ -290,60 +274,58 @@ void pn512_set_transceive_framing(nfc_transceiver_t* pTransceiver, nfc_framing_t
}
}
void pn512_set_write(nfc_transceiver_t* pTransceiver, buffer_t* pWriteBuf)
void pn512_set_write(nfc_transceiver_t *pTransceiver, buffer_t *pWriteBuf)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
if( pWriteBuf == NULL )
{
pn512_t *pPN512 = (pn512_t *) pTransceiver;
if (pWriteBuf == NULL) {
buffer_init(&pPN512->writeBuf, NULL, 0);
return;
}
buffer_dup(&pPN512->writeBuf, pWriteBuf);
}
buffer_t* pn512_get_read(nfc_transceiver_t* pTransceiver)
buffer_t *pn512_get_read(nfc_transceiver_t *pTransceiver)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
return buffer_builder_buffer(&pPN512->readBufBldr);
}
void pn512_set_last_byte_length(nfc_transceiver_t* pTransceiver, size_t lastByteLength)
void pn512_set_last_byte_length(nfc_transceiver_t *pTransceiver, size_t lastByteLength)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
if( (lastByteLength > 8) || (lastByteLength == 0) )
{
pn512_t *pPN512 = (pn512_t *) pTransceiver;
if ((lastByteLength > 8) || (lastByteLength == 0)) {
lastByteLength = 8;
}
pPN512->writeLastByteLength = lastByteLength;
}
void pn512_set_first_byte_align(nfc_transceiver_t* pTransceiver, size_t firstByteAlign)
void pn512_set_first_byte_align(nfc_transceiver_t *pTransceiver, size_t firstByteAlign)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
firstByteAlign &= 0x7;
pPN512->readFirstByteAlign = firstByteAlign;
}
size_t pn512_get_last_byte_length(nfc_transceiver_t* pTransceiver)
size_t pn512_get_last_byte_length(nfc_transceiver_t *pTransceiver)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
return pPN512->readLastByteLength;
}
void pn512_transceive(nfc_transceiver_t* pTransceiver)
void pn512_transceive(nfc_transceiver_t *pTransceiver)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
pn512_transceive_hw(pPN512, pPN512->nextFrameMode, pn512_transceiver_callback);
pPN512->nextFrameMode = pn512_transceive_mode_transceive;
}
void pn512_abort(nfc_transceiver_t* pTransceiver)
void pn512_abort(nfc_transceiver_t *pTransceiver)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
scheduler_dequeue_task(&pTransceiver->scheduler, true, &pPN512->transceiver.task);
}
void pn512_close(nfc_transceiver_t* pTransceiver)
void pn512_close(nfc_transceiver_t *pTransceiver)
{
//pn512_t* pPN512 = (pn512_t*) pTransceiver;
(void) pTransceiver;
@ -351,22 +333,19 @@ void pn512_close(nfc_transceiver_t* pTransceiver)
return;
}
void pn512_sleep(nfc_transceiver_t* pTransceiver, bool sleep)
void pn512_sleep(nfc_transceiver_t *pTransceiver, bool sleep)
{
pn512_t* pPN512 = (pn512_t*) pTransceiver;
pn512_t *pPN512 = (pn512_t *) pTransceiver;
if(sleep)
{
if (sleep) {
pn512_register_write(pPN512, PN512_REG_COMMAND, 0x30); //Receiver off + soft power down
}
else
{
} else {
pn512_register_write(pPN512, PN512_REG_COMMAND, 0x00);
while( pn512_register_read(pPN512, PN512_REG_COMMAND) & 0x10 );
while (pn512_register_read(pPN512, PN512_REG_COMMAND) & 0x10);
}
}
void pn512_transceiver_callback(pn512_t* pPN512, nfc_err_t ret)
void pn512_transceiver_callback(pn512_t *pPN512, nfc_err_t ret)
{
transceiver_callback(&pPN512->transceiver, ret);
}

View File

@ -34,8 +34,7 @@ typedef struct __pn512 pn512_t;
#include "pn512_callback.h"
#include "pn512_types.h"
typedef enum __pn512_state
{
typedef enum __pn512_state {
pn512_state_ready,
pn512_state_target_autocoll,
pn512_state_initiator_transceive_first_frame,
@ -43,8 +42,7 @@ typedef enum __pn512_state
pn512_state_transceive_last_frame,
} pn512_state_t;
typedef enum __pn512_transceive_mode
{
typedef enum __pn512_transceive_mode {
pn512_transceive_mode_idle,
pn512_transceive_mode_target_autocoll,
pn512_transceive_mode_transmit,
@ -53,21 +51,18 @@ typedef enum __pn512_transceive_mode
pn512_transceive_mode_receive,
} pn512_transceive_mode_t;
typedef struct __pn512
{
typedef struct __pn512 {
nfc_transceiver_t transceiver;
//Impl specific
pn512_registers_t registers;
bool rf_on;
struct
{
struct {
bool out;
bool in;
} crc;
int timeout;
struct
{
struct {
nfc_tech_t initiators;
nfc_tech_t targets;
polling_options_t options;
@ -88,11 +83,9 @@ typedef struct __pn512
uint8_t writeLastByteLength;
//Task parameters
struct
{
struct {
//Polling
struct
{
struct {
enum {
pn512_polling_state_start_listening,
@ -120,30 +113,24 @@ typedef struct __pn512
pn512_cb_t cb;
} poll;
struct
{
struct {
pn512_cb_t cb;
pn512_transceive_mode_t mode;
} transceive;
struct
{
struct {
pn512_cb_t cb;
} rf;
struct
{
union
{
struct {
union {
// ISO A
struct
{
struct {
bool more_targets; // Collision detected
uint8_t cascade_level;
uint8_t cln[5];
uint8_t valid_bits; // valid bits within cascade level
} iso_a;
// ISO B
struct
{
struct {
bool more_targets; // Collision detected
uint8_t slots_num_exponent;
uint8_t slot_number;
@ -156,9 +143,9 @@ typedef struct __pn512
} pn512_t;
nfc_err_t pn512_init(pn512_t* pPN512, nfc_transport_t* pTransport, nfc_scheduler_timer_t* pTimer);
nfc_err_t pn512_init(pn512_t *pPN512, nfc_transport_t *pTransport, nfc_scheduler_timer_t *pTimer);
nfc_transceiver_t* pn512_get_transceiver(pn512_t* pPN512);
nfc_transceiver_t *pn512_get_transceiver(pn512_t *pPN512);
#ifdef __cplusplus
}

View File

@ -28,7 +28,7 @@ extern "C" {
#endif
typedef struct __pn512 pn512_t;
typedef void (*pn512_cb_t)(pn512_t* pPN512, nfc_err_t ret);
typedef void (*pn512_cb_t)(pn512_t *pPN512, nfc_err_t ret);
#ifdef __cplusplus
}

View File

@ -47,7 +47,7 @@
/** \internal Initialize underlying pn512_cmd_t structure
* \param pPN512 pointer to pn512_t structure
*/
void pn512_cmd_init(pn512_t* pPN512)
void pn512_cmd_init(pn512_t *pPN512)
{
(void) pPN512;
}
@ -57,7 +57,7 @@ void pn512_cmd_init(pn512_t* pPN512)
* \param pPN512 pointer to pn512_t structure
* \param pData buffer to write
*/
void pn512_fifo_write(pn512_t* pPN512, buffer_t* pData)
void pn512_fifo_write(pn512_t *pPN512, buffer_t *pData)
{
uint8_t fifo_space = pn512_fifo_space(pPN512); //Do not call this fn twice
size_t len = buffer_reader_readable(pData);
@ -71,7 +71,7 @@ void pn512_fifo_write(pn512_t* pPN512, buffer_t* pData)
* \param pPN512 pointer to pn512_t structure
* \param pData buffer in which to read
*/
void pn512_fifo_read(pn512_t* pPN512, buffer_builder_t* pData)
void pn512_fifo_read(pn512_t *pPN512, buffer_builder_t *pData)
{
uint8_t fifo_len = pn512_fifo_length(pPN512); //Do not call this fn twice
size_t len = buffer_builder_writeable(pData);
@ -85,7 +85,7 @@ void pn512_fifo_read(pn512_t* pPN512, buffer_builder_t* pData)
* Removes any bytes left in FIFO
* \param pPN512 pointer to pn512_t structure
*/
void pn512_fifo_clear(pn512_t* pPN512)
void pn512_fifo_clear(pn512_t *pPN512)
{
pn512_register_write(pPN512, PN512_REG_FIFOLEVEL, 0x80); //Flush FIFO
}
@ -94,7 +94,7 @@ void pn512_fifo_clear(pn512_t* pPN512)
* \param pPN512 pointer to pn512_t structure
* \return number of bytes that can be written to FIFO
*/
size_t pn512_fifo_space(pn512_t* pPN512)
size_t pn512_fifo_space(pn512_t *pPN512)
{
return PN512_FIFO_SIZE - pn512_register_read(pPN512, PN512_REG_FIFOLEVEL);
}
@ -103,7 +103,7 @@ size_t pn512_fifo_space(pn512_t* pPN512)
* \param pPN512 pointer to pn512_t structure
* \return number of bytes that can be read from FIFO
*/
size_t pn512_fifo_length(pn512_t* pPN512)
size_t pn512_fifo_length(pn512_t *pPN512)
{
return pn512_register_read(pPN512, PN512_REG_FIFOLEVEL);
}
@ -112,7 +112,7 @@ size_t pn512_fifo_length(pn512_t* pPN512)
* \param pPN512 pointer to pn512_t structure
* \param cmd PN512 command to execute
*/
void pn512_cmd_exec(pn512_t* pPN512, uint8_t cmd)
void pn512_cmd_exec(pn512_t *pPN512, uint8_t cmd)
{
pn512_register_write(pPN512, PN512_REG_COMMAND, cmd);
}
@ -122,11 +122,10 @@ void pn512_cmd_exec(pn512_t* pPN512, uint8_t cmd)
* \param timeout timeout in milliseconds or -1 for blocking mode
* \return NFC_OK on success or NFC_ERR_TIMEOUT on timeout
*/
nfc_err_t pn512_cmd_wait_idle(pn512_t* pPN512, int timeout)
nfc_err_t pn512_cmd_wait_idle(pn512_t *pPN512, int timeout)
{
(void) timeout;
while( pn512_cmd_get(pPN512) != PN512_CMD_IDLE )
{
while (pn512_cmd_get(pPN512) != PN512_CMD_IDLE) {
}
return NFC_OK;
@ -137,7 +136,7 @@ nfc_err_t pn512_cmd_wait_idle(pn512_t* pPN512, int timeout)
* \param pPN512 pointer to pn512_t structure
* \return PN512 command being executed
*/
uint8_t pn512_cmd_get(pn512_t* pPN512)
uint8_t pn512_cmd_get(pn512_t *pPN512)
{
return pn512_register_read(pPN512, PN512_REG_COMMAND) & PN512_CMD_REG_MASK;
}

View File

@ -47,28 +47,28 @@ extern "C" {
#define PN512_CMD_REG_MASK 0x0F
void pn512_cmd_init(pn512_t* pPN512);
void pn512_cmd_init(pn512_t *pPN512);
//Fifo read / write
void pn512_fifo_write(pn512_t* pPN512, buffer_t* pData);
void pn512_fifo_read(pn512_t* pPN512, buffer_builder_t* pData);
void pn512_fifo_write(pn512_t *pPN512, buffer_t *pData);
void pn512_fifo_read(pn512_t *pPN512, buffer_builder_t *pData);
//Fifo clear
void pn512_fifo_clear(pn512_t* pPN512);
void pn512_fifo_clear(pn512_t *pPN512);
//Fifo bytes read
size_t pn512_fifo_space(pn512_t* pPN512);
size_t pn512_fifo_length(pn512_t* pPN512);
size_t pn512_fifo_space(pn512_t *pPN512);
size_t pn512_fifo_length(pn512_t *pPN512);
//Execute command
void pn512_cmd_exec(pn512_t* pPN512, uint8_t cmd);
void pn512_cmd_exec(pn512_t *pPN512, uint8_t cmd);
//Wait for command completion
nfc_err_t pn512_cmd_wait_idle(pn512_t* pPN512, int timeout);
nfc_err_t pn512_cmd_wait_idle(pn512_t *pPN512, int timeout);
//Read executed command
uint8_t pn512_cmd_get(pn512_t* pPN512);
uint8_t pn512_cmd_get(pn512_t *pPN512);
#ifdef __cplusplus

View File

@ -40,7 +40,7 @@
/** \internal Initialize underlying pn512_hw_t structure
* \param pPN512 pointer to pn512_t structure
*/
void pn512_hw_init(pn512_t* pPN512)
void pn512_hw_init(pn512_t *pPN512)
{
//Nothing to init in this implementation
(void) pPN512;

View File

@ -35,7 +35,7 @@ extern "C" {
#define PN512_SPI_ADDR_R(x) ((1<<7) | ((x) << 1))
#define PN512_SPI_ADDR_W(x) ((0<<7) | ((x) << 1))
void pn512_hw_init(pn512_t* pPN512);
void pn512_hw_init(pn512_t *pPN512);
/** \internal Write bytes at the specified address on the underlying transport link
* \param pPN512 pointer to pn512_t structure
@ -43,9 +43,9 @@ void pn512_hw_init(pn512_t* pPN512);
* \param buf buffer to write
* \param len length of buffer
*/
static inline void pn512_hw_write(pn512_t* pPN512, uint8_t addr, uint8_t* buf, size_t len)
static inline void pn512_hw_write(pn512_t *pPN512, uint8_t addr, uint8_t *buf, size_t len)
{
nfc_transport_write(((nfc_transceiver_t*)pPN512)->pTransport, addr, buf, len);
nfc_transport_write(((nfc_transceiver_t *)pPN512)->pTransport, addr, buf, len);
}
/** \internal Read bytes from the specified address on the underlying transport link
@ -54,37 +54,33 @@ static inline void pn512_hw_write(pn512_t* pPN512, uint8_t addr, uint8_t* buf, s
* \param buf buffer to read
* \param len length of buffer
*/
static inline void pn512_hw_read(pn512_t* pPN512, uint8_t addr, uint8_t* buf, size_t len)
static inline void pn512_hw_read(pn512_t *pPN512, uint8_t addr, uint8_t *buf, size_t len)
{
nfc_transport_read(((nfc_transceiver_t*)pPN512)->pTransport, addr, buf, len);
nfc_transport_read(((nfc_transceiver_t *)pPN512)->pTransport, addr, buf, len);
}
static inline void pn512_hw_write_buffer(pn512_t* pPN512, uint8_t addr, buffer_t* pData, size_t len)
static inline void pn512_hw_write_buffer(pn512_t *pPN512, uint8_t addr, buffer_t *pData, size_t len)
{
while( len > 0 )
{
if( buffer_reader_readable(pData) == 0 )
{
while (len > 0) {
if (buffer_reader_readable(pData) == 0) {
return;
}
size_t cpyLen = MIN(len, buffer_reader_current_buffer_length(pData));
nfc_transport_write(((nfc_transceiver_t*)pPN512)->pTransport, addr, buffer_reader_current_buffer_pointer(pData), cpyLen);
nfc_transport_write(((nfc_transceiver_t *)pPN512)->pTransport, addr, buffer_reader_current_buffer_pointer(pData), cpyLen);
buffer_read_n_skip(pData, cpyLen);
len -= cpyLen;
}
}
static inline void pn512_hw_read_buffer(pn512_t* pPN512, uint8_t addr, buffer_builder_t* pData, size_t len)
static inline void pn512_hw_read_buffer(pn512_t *pPN512, uint8_t addr, buffer_builder_t *pData, size_t len)
{
while( len > 0 )
{
if( buffer_builder_writeable(pData) == 0 )
{
while (len > 0) {
if (buffer_builder_writeable(pData) == 0) {
return;
}
//Read payload
size_t cpyLen = MIN(len, buffer_builder_space(pData));
nfc_transport_read(((nfc_transceiver_t*)pPN512)->pTransport, addr, buffer_builder_write_position(pData), cpyLen);
nfc_transport_read(((nfc_transceiver_t *)pPN512)->pTransport, addr, buffer_builder_write_position(pData), cpyLen);
buffer_builder_write_n_skip(pData, cpyLen);
len -= cpyLen;
}

View File

@ -35,40 +35,40 @@ extern "C" {
#include "pn512_callback.h"
//Public
void pn512_set_protocols(nfc_transceiver_t* pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options);
void pn512_poll(nfc_transceiver_t* pTransceiver);
void pn512_set_crc(nfc_transceiver_t* pTransceiver, bool crc_out, bool crc_in);
void pn512_set_timeout(nfc_transceiver_t* pTransceiver, int timeout);
void pn512_set_transceive_options(nfc_transceiver_t* pTransceiver, bool transmit, bool receive, bool repoll);
void pn512_set_transceive_framing(nfc_transceiver_t* pTransceiver, nfc_framing_t framing);
void pn512_set_write(nfc_transceiver_t* pTransceiver, buffer_t* pWriteBuf);
buffer_t* pn512_get_read(nfc_transceiver_t* pTransceiver);
size_t pn512_get_last_byte_length(nfc_transceiver_t* pTransceiver);
void pn512_set_last_byte_length(nfc_transceiver_t* pTransceiver, size_t lastByteLength);
void pn512_set_first_byte_align(nfc_transceiver_t* pTransceiver, size_t firstByteAlign);
void pn512_abort(nfc_transceiver_t* pTransceiver);
void pn512_transceive(nfc_transceiver_t* pTransceiver);
void pn512_close(nfc_transceiver_t* pTransceiver);
void pn512_sleep(nfc_transceiver_t* pTransceiver, bool sleep);
void pn512_set_protocols(nfc_transceiver_t *pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options);
void pn512_poll(nfc_transceiver_t *pTransceiver);
void pn512_set_crc(nfc_transceiver_t *pTransceiver, bool crc_out, bool crc_in);
void pn512_set_timeout(nfc_transceiver_t *pTransceiver, int timeout);
void pn512_set_transceive_options(nfc_transceiver_t *pTransceiver, bool transmit, bool receive, bool repoll);
void pn512_set_transceive_framing(nfc_transceiver_t *pTransceiver, nfc_framing_t framing);
void pn512_set_write(nfc_transceiver_t *pTransceiver, buffer_t *pWriteBuf);
buffer_t *pn512_get_read(nfc_transceiver_t *pTransceiver);
size_t pn512_get_last_byte_length(nfc_transceiver_t *pTransceiver);
void pn512_set_last_byte_length(nfc_transceiver_t *pTransceiver, size_t lastByteLength);
void pn512_set_first_byte_align(nfc_transceiver_t *pTransceiver, size_t firstByteAlign);
void pn512_abort(nfc_transceiver_t *pTransceiver);
void pn512_transceive(nfc_transceiver_t *pTransceiver);
void pn512_close(nfc_transceiver_t *pTransceiver);
void pn512_sleep(nfc_transceiver_t *pTransceiver, bool sleep);
void pn512_transceiver_callback(pn512_t* pPN512, nfc_err_t ret);
void pn512_transceiver_callback(pn512_t *pPN512, nfc_err_t ret);
static inline void pn512_rf_callback(pn512_t* pPN512, nfc_err_t ret)
static inline void pn512_rf_callback(pn512_t *pPN512, nfc_err_t ret)
{
pPN512->rf.cb(pPN512, ret);
}
static inline void pn512_poll_callback(pn512_t* pPN512, nfc_err_t ret)
static inline void pn512_poll_callback(pn512_t *pPN512, nfc_err_t ret)
{
pPN512->poll.cb(pPN512, ret);
}
static inline void pn512_anticollision_callback(pn512_t* pPN512, nfc_err_t ret)
static inline void pn512_anticollision_callback(pn512_t *pPN512, nfc_err_t ret)
{
pPN512->anticollision.cb(pPN512, ret);
}
static inline void pn512_transceive_callback(pn512_t* pPN512, nfc_err_t ret)
static inline void pn512_transceive_callback(pn512_t *pPN512, nfc_err_t ret)
{
pPN512->transceive.cb(pPN512, ret);
}

View File

@ -65,7 +65,7 @@ extern "C" {
* \param pPN512 pointer to pn512_t structure
* \param irqs MSB is DIVIEN value, LSB is COMIEN value
*/
static inline void pn512_irq_set(pn512_t* pPN512, uint16_t irqs) //ORed
static inline void pn512_irq_set(pn512_t *pPN512, uint16_t irqs) //ORed
{
pn512_register_write(pPN512, PN512_REG_COMIEN, PN512_REG_COMIEN_VAL | (PN512_REG_COMIEN_MASK & (irqs & 0xFF)));
pn512_register_write(pPN512, PN512_REG_DIVIEN, PN512_REG_DIVIEN_VAL | (PN512_REG_DIVIEN_MASK & (irqs >> 8)));
@ -76,7 +76,7 @@ static inline void pn512_irq_set(pn512_t* pPN512, uint16_t irqs) //ORed
* \param pPN512 pointer to pn512_t structure
* \return MSB is DIVIEN value, LSB is COMIEN value
*/
static inline uint16_t pn512_irq_enabled(pn512_t* pPN512) //ORed
static inline uint16_t pn512_irq_enabled(pn512_t *pPN512) //ORed
{
return pPN512->irqsEn /*(pn512_register_read(pPN512, PN512_REG_COMIEN_VAL) & PN512_REG_COMIEN_MASK)
| ((pn512_register_read(pPN512, PN512_REG_DIVIEN_VAL) & PN512_REG_DIVIEN_MASK) << 8)*/;
@ -86,7 +86,7 @@ static inline uint16_t pn512_irq_enabled(pn512_t* pPN512) //ORed
* \param pPN512 pointer to pn512_t structure
* \return MSB is DIVIRQ value, LSB is COMIRQ value
*/
static inline uint16_t pn512_irq_get(pn512_t* pPN512) //ORed
static inline uint16_t pn512_irq_get(pn512_t *pPN512) //ORed
{
return ((pn512_register_read(pPN512, PN512_REG_COMIRQ) & PN512_REG_COMIEN_MASK)
| ((pn512_register_read(pPN512, PN512_REG_DIVIRQ) & PN512_REG_DIVIEN_MASK) << 8)) & pPN512->irqsEn;
@ -96,7 +96,7 @@ static inline uint16_t pn512_irq_get(pn512_t* pPN512) //ORed
* \param pPN512 pointer to pn512_t structure
* \param irqs MSB is DIVIEN value, LSB is COMIEN value
*/
static inline void pn512_irq_clear(pn512_t* pPN512, uint16_t irqs)
static inline void pn512_irq_clear(pn512_t *pPN512, uint16_t irqs)
{
pn512_register_write(pPN512, PN512_REG_COMIRQ, PN512_REG_COMIRQ_CLEAR | (PN512_REG_COMIRQ_MASK & (irqs & 0xFF)));
pn512_register_write(pPN512, PN512_REG_DIVIRQ, PN512_REG_DIVIRQ_CLEAR | (PN512_REG_DIVIRQ_MASK & (irqs >> 8)));

File diff suppressed because it is too large Load Diff

View File

@ -30,8 +30,8 @@ extern "C" {
#include "inc/nfc.h"
void pn512_poll_setup(pn512_t* pPN512);
void pn512_poll_hw(pn512_t* pPN512, pn512_cb_t cb);
void pn512_poll_setup(pn512_t *pPN512);
void pn512_poll_hw(pn512_t *pPN512, pn512_cb_t cb);
#ifdef __cplusplus

View File

@ -43,12 +43,12 @@
* @{
*/
static void pn512_register_switch_page_intl(pn512_t* pPN512, uint8_t page);
static void pn512_register_switch_page_intl(pn512_t *pPN512, uint8_t page);
/** \internal Initialize underlying pn512_registers_t structure
* \param pPN512 pointer to pn512_t structure
*/
void pn512_registers_init(pn512_t* pPN512)
void pn512_registers_init(pn512_t *pPN512)
{
pPN512->registers.registers_page = 0;
}
@ -80,11 +80,10 @@ static const uint8_t PN512_CFG_INIT_VALS[] = {
/** \internal Switch to known (0) registers page, reset registers state
* \param pPN512 pointer to pn512_t structure
*/
void pn512_registers_reset(pn512_t* pPN512)
void pn512_registers_reset(pn512_t *pPN512)
{
pn512_register_switch_page_intl(pPN512, 0);
for(int i = 0; i < PN512_CFG_INIT_LEN; i++)
{
for (int i = 0; i < PN512_CFG_INIT_LEN; i++) {
pn512_register_write(pPN512, PN512_CFG_INIT_REGS[i], PN512_CFG_INIT_VALS[i]);
}
}
@ -94,14 +93,13 @@ void pn512_registers_reset(pn512_t* pPN512)
* \param address register address
* \param data value to write in register
*/
void pn512_register_write(pn512_t* pPN512, uint8_t address, uint8_t data)
void pn512_register_write(pn512_t *pPN512, uint8_t address, uint8_t data)
{
NFC_DBG("Write [%02x] << %02x", address, data);
if(REGISTER_PAGE(address) != pPN512->registers.registers_page)
{
if (REGISTER_PAGE(address) != pPN512->registers.registers_page) {
pn512_register_switch_page_intl(pPN512, REGISTER_PAGE(address));
}
address=REGISTER_ADDR(address);
address = REGISTER_ADDR(address);
pn512_hw_write(pPN512, address, &data, 1);
}
@ -110,27 +108,25 @@ void pn512_register_write(pn512_t* pPN512, uint8_t address, uint8_t data)
* \param address register address
* \return data value read from register
*/
uint8_t pn512_register_read(pn512_t* pPN512, uint8_t address)
uint8_t pn512_register_read(pn512_t *pPN512, uint8_t address)
{
uint8_t data;
DBG_BLOCK(
uint8_t __dbg_addr;
__dbg_addr = address; //FIXME
)
if(REGISTER_PAGE(address) != pPN512->registers.registers_page)
{
if (REGISTER_PAGE(address) != pPN512->registers.registers_page) {
pn512_register_switch_page_intl(pPN512, REGISTER_PAGE(address));
}
address=REGISTER_ADDR(address);
address = REGISTER_ADDR(address);
pn512_hw_read(pPN512, address, &data, 1);
NFC_DBG("Read [%02x] >> %02x", __dbg_addr, data);
return data;
}
void pn512_register_switch_page(pn512_t* pPN512, uint8_t address)
void pn512_register_switch_page(pn512_t *pPN512, uint8_t address)
{
if(REGISTER_PAGE(address) != pPN512->registers.registers_page)
{
if (REGISTER_PAGE(address) != pPN512->registers.registers_page) {
pn512_register_switch_page_intl(pPN512, REGISTER_PAGE(address));
}
}
@ -139,7 +135,7 @@ void pn512_register_switch_page(pn512_t* pPN512, uint8_t address)
* \param pPN512 pointer to pn512_t structure
* \param page registers page
*/
void pn512_register_switch_page_intl(pn512_t* pPN512, uint8_t page)
void pn512_register_switch_page_intl(pn512_t *pPN512, uint8_t page)
{
uint8_t pageRegValue;
pageRegValue = (1 << 7) | page;

View File

@ -99,13 +99,13 @@ extern "C" {
#define PN512_REG_TESTADC 0x3B //Shows the actual value of ADC I and Q
void pn512_registers_init(pn512_t* pPN512);
void pn512_registers_reset(pn512_t* pPN512);
void pn512_registers_init(pn512_t *pPN512);
void pn512_registers_reset(pn512_t *pPN512);
void pn512_register_write(pn512_t* pPN512, uint8_t address, uint8_t data);
uint8_t pn512_register_read(pn512_t* pPN512, uint8_t address);
void pn512_register_write(pn512_t *pPN512, uint8_t address, uint8_t data);
uint8_t pn512_register_read(pn512_t *pPN512, uint8_t address);
void pn512_register_switch_page(pn512_t* pPN512, uint8_t address);
void pn512_register_switch_page(pn512_t *pPN512, uint8_t address);
#ifdef __cplusplus
}

View File

@ -46,18 +46,16 @@ static const uint8_t framing_registers_target_iso14443a_106k[] = { 0x3D, 0x80, 0
static const uint8_t framing_registers_felica_212k[] = { 0x3A, 0x92, 0x92, 0x12, 0x55, 0x15 };
static const uint8_t framing_registers_felica_414k[] = { 0x3A, 0xA2, 0xA2, 0x12, 0x55, 0x0A };
nfc_err_t pn512_framing_set(pn512_t* pPN512, nfc_framing_t framing)
nfc_err_t pn512_framing_set(pn512_t *pPN512, nfc_framing_t framing)
{
if(framing == pPN512->framing) //No need to do anything
{
if (framing == pPN512->framing) { //No need to do anything
return NFC_OK;
}
NFC_DBG("Switching to %u", framing);
const uint8_t* framing_registers_values;
switch(framing)
{
const uint8_t *framing_registers_values;
switch (framing) {
case nfc_framing_target_mode_detector:
framing_registers_values = framing_registers_mode_detector;
break;
@ -82,8 +80,7 @@ nfc_err_t pn512_framing_set(pn512_t* pPN512, nfc_framing_t framing)
return NFC_ERR_UNSUPPORTED;
}
for(int i = 0; i < PN512_FRAMING_REGS; i++)
{
for (int i = 0; i < PN512_FRAMING_REGS; i++) {
pn512_register_write(pPN512, framing_registers[i], framing_registers_values[i]);
}
@ -93,8 +90,7 @@ nfc_err_t pn512_framing_set(pn512_t* pPN512, nfc_framing_t framing)
//TODO initiator: PN512_REG_MODGSP
switch(pPN512->framing)
{
switch (pPN512->framing) {
case nfc_framing_initiator_a_106:
case nfc_framing_initiator_b_106:
case nfc_framing_initiator_f_212:
@ -111,24 +107,20 @@ nfc_err_t pn512_framing_set(pn512_t* pPN512, nfc_framing_t framing)
return NFC_ERR_UNSUPPORTED;
}
#if 1
if( (pPN512->framing == nfc_framing_initiator_a_106) /*|| (pPN512->framing == pn512_framing_target_iso14443a_106k)*/ )
{
if ((pPN512->framing == nfc_framing_initiator_a_106) /*|| (pPN512->framing == pn512_framing_target_iso14443a_106k)*/) {
//Enable 100% ASK Modulation
pn512_register_write(pPN512, PN512_REG_TXAUTO, pn512_register_read(pPN512, PN512_REG_TXAUTO) | 0x40 );
}
else
{
pn512_register_write(pPN512, PN512_REG_TXAUTO, pn512_register_read(pPN512, PN512_REG_TXAUTO) & (~0x40) );
pn512_register_write(pPN512, PN512_REG_TXAUTO, pn512_register_read(pPN512, PN512_REG_TXAUTO) | 0x40);
} else {
pn512_register_write(pPN512, PN512_REG_TXAUTO, pn512_register_read(pPN512, PN512_REG_TXAUTO) & (~0x40));
}
#endif
return NFC_OK;
}
nfc_err_t pn512_framing_crc_set(pn512_t* pPN512, bool out, bool in)
nfc_err_t pn512_framing_crc_set(pn512_t *pPN512, bool out, bool in)
{
const uint8_t* framing_registers_values;
switch(pPN512->framing)
{
const uint8_t *framing_registers_values;
switch (pPN512->framing) {
case nfc_framing_target_mode_detector:
framing_registers_values = framing_registers_mode_detector;
break;
@ -153,25 +145,22 @@ nfc_err_t pn512_framing_crc_set(pn512_t* pPN512, bool out, bool in)
return NFC_ERR_UNSUPPORTED;
}
if(pPN512->crc.out != out)
{
pn512_register_write(pPN512, framing_registers[1], (framing_registers_values[1] & 0x7F) | (out?0x80:0x00)); //TXMODE
if (pPN512->crc.out != out) {
pn512_register_write(pPN512, framing_registers[1], (framing_registers_values[1] & 0x7F) | (out ? 0x80 : 0x00)); //TXMODE
pPN512->crc.out = out;
}
if(pPN512->crc.in != in)
{
pn512_register_write(pPN512, framing_registers[2], (framing_registers_values[2] & 0x7F) | (in?0x80:0x00)); //RXMODE
if (pPN512->crc.in != in) {
pn512_register_write(pPN512, framing_registers[2], (framing_registers_values[2] & 0x7F) | (in ? 0x80 : 0x00)); //RXMODE
pPN512->crc.in = in;
}
return NFC_OK;
}
nfc_err_t pn512_framing_rx_multiple_enable(pn512_t* pPN512)
nfc_err_t pn512_framing_rx_multiple_enable(pn512_t *pPN512)
{
const uint8_t* framing_registers_values;
switch(pPN512->framing)
{
const uint8_t *framing_registers_values;
switch (pPN512->framing) {
case nfc_framing_target_mode_detector:
framing_registers_values = framing_registers_mode_detector;
break;
@ -196,21 +185,21 @@ nfc_err_t pn512_framing_rx_multiple_enable(pn512_t* pPN512)
return NFC_ERR_UNSUPPORTED;
}
pn512_register_write(pPN512, framing_registers[2], (framing_registers_values[2] & 0x7F) | (pPN512->crc.in?0x80:0x00) | 0x04); //RXMODE
pn512_register_write(pPN512, framing_registers[2], (framing_registers_values[2] & 0x7F) | (pPN512->crc.in ? 0x80 : 0x00) | 0x04); //RXMODE
return NFC_OK;
}
void pn512_rf_field_switch_off(pn512_t* pPN512)
void pn512_rf_field_switch_off(pn512_t *pPN512)
{
pn512_register_write(pPN512, PN512_REG_TXAUTO, 0x00);
pn512_register_write(pPN512, PN512_REG_TXCONTROL, 0x80);
pPN512->rf_on = false;
}
void pn512_rf_field_nfcip1_rf_collision_avoidance_complete(uint32_t events, void* pUserData)
void pn512_rf_field_nfcip1_rf_collision_avoidance_complete(uint32_t events, void *pUserData)
{
pn512_t* pPN512 = (pn512_t*) pUserData;
pn512_t *pPN512 = (pn512_t *) pUserData;
uint16_t irq_res = pn512_irq_get(pPN512);
@ -221,8 +210,7 @@ void pn512_rf_field_nfcip1_rf_collision_avoidance_complete(uint32_t events, void
pn512_irq_set(pPN512, PN512_IRQ_NONE);
pn512_irq_clear(pPN512, PN512_IRQ_RF_ON | PN512_IRQ_TIMER);
if(irq_res & PN512_IRQ_RF_ON)
{
if (irq_res & PN512_IRQ_RF_ON) {
NFC_DBG("External field on");
//Clear TXAUTO register
@ -234,8 +222,7 @@ void pn512_rf_field_nfcip1_rf_collision_avoidance_complete(uint32_t events, void
}
//Has our RF field been switched on?
if( pn512_register_read(pPN512, PN512_REG_TXAUTO) & 0x40 ) //InitialRFOn bit is cleared automatically, if the RF field is switched on
{
if (pn512_register_read(pPN512, PN512_REG_TXAUTO) & 0x40) { //InitialRFOn bit is cleared automatically, if the RF field is switched on
NFC_ERR("InitialRFOn bit still set");
pn512_rf_callback(pPN512, NFC_ERR_UNKNOWN);
return;
@ -247,7 +234,7 @@ void pn512_rf_field_nfcip1_rf_collision_avoidance_complete(uint32_t events, void
pn512_rf_callback(pPN512, NFC_OK);
}
void pn512_rf_field_nfcip1_rf_collision_avoidance(pn512_t* pPN512, pn512_cb_t cb)
void pn512_rf_field_nfcip1_rf_collision_avoidance(pn512_t *pPN512, pn512_cb_t cb)
{
pPN512->rf.cb = cb;
pn512_irq_clear(pPN512, PN512_IRQ_RF_ON | PN512_IRQ_TIMER);
@ -256,14 +243,13 @@ void pn512_rf_field_nfcip1_rf_collision_avoidance(pn512_t* pPN512, pn512_cb_t cb
pn512_timer_config(pPN512, true, 3, 8475);
pn512_irq_set(pPN512, PN512_IRQ_RF_ON /* External field switched on */
| PN512_IRQ_TIMER /* Timer reached 0 */ );
| PN512_IRQ_TIMER /* Timer reached 0 */);
//Try to enable RF field in compliance with NFC-IP1
pn512_register_write(pPN512, PN512_REG_TXAUTO, 0x0F);
//Is external RF Field already on?
if( pn512_register_read(pPN512, PN512_REG_STATUS1) & 0x4 )
{
if (pn512_register_read(pPN512, PN512_REG_STATUS1) & 0x4) {
NFC_DBG("External field already on");
pPN512->rf_on = false; //External field on
@ -280,27 +266,25 @@ void pn512_rf_field_nfcip1_rf_collision_avoidance(pn512_t* pPN512, pn512_cb_t cb
scheduler_queue_task(&pPN512->transceiver.scheduler, &pPN512->transceiver.task);
}
void pn512_rf_field_wait_for_external_complete_task(uint32_t events, void* pUserData)
void pn512_rf_field_wait_for_external_complete_task(uint32_t events, void *pUserData)
{
pn512_t* pPN512 = (pn512_t*) pUserData;
pn512_t *pPN512 = (pn512_t *) pUserData;
NFC_DBG("%lu events", events);
//Wake up PN512
pn512_register_write(pPN512, PN512_REG_COMMAND, 0x00);
while( pn512_register_read(pPN512, PN512_REG_COMMAND) & 0x10 );
while (pn512_register_read(pPN512, PN512_REG_COMMAND) & 0x10);
pn512_irq_set(pPN512, PN512_IRQ_NONE);
pn512_irq_clear(pPN512, PN512_IRQ_RF_ON);
if(events & EVENT_ABORTED)
{
if (events & EVENT_ABORTED) {
pn512_rf_callback(pPN512, NFC_ERR_ABORTED);
return;
}
if( events & EVENT_TIMEOUT )
{
if (events & EVENT_TIMEOUT) {
NFC_DBG("Timeout");
pn512_rf_callback(pPN512, NFC_ERR_TIMEOUT);
return;
@ -310,7 +294,7 @@ void pn512_rf_field_wait_for_external_complete_task(uint32_t events, void* pUser
pn512_rf_callback(pPN512, NFC_OK);
}
void pn512_rf_field_wait_for_external(pn512_t* pPN512, int timeout, pn512_cb_t cb)
void pn512_rf_field_wait_for_external(pn512_t *pPN512, int timeout, pn512_cb_t cb)
{
pPN512->rf.cb = cb;
@ -320,8 +304,7 @@ void pn512_rf_field_wait_for_external(pn512_t* pPN512, int timeout, pn512_cb_t c
//Is external RF Field already on?
pn512_irq_set(pPN512, PN512_IRQ_RF_ON /* External field switched on */);
if( pn512_register_read(pPN512, PN512_REG_STATUS1) & 0x4 )
{
if (pn512_register_read(pPN512, PN512_REG_STATUS1) & 0x4) {
NFC_DBG("RF field already on");
pn512_irq_set(pPN512, PN512_IRQ_NONE);
pn512_irq_clear(pPN512, PN512_IRQ_RF_ON);

View File

@ -34,19 +34,19 @@ extern "C" {
typedef struct __pn512 pn512_t;
nfc_err_t pn512_framing_set(pn512_t* pPN512, nfc_framing_t framing);
nfc_err_t pn512_framing_set(pn512_t *pPN512, nfc_framing_t framing);
nfc_err_t pn512_framing_crc_set(pn512_t* pPN512, bool out, bool in);
nfc_err_t pn512_framing_crc_set(pn512_t *pPN512, bool out, bool in);
nfc_err_t pn512_framing_rx_multiple_enable(pn512_t* pPN512);
nfc_err_t pn512_framing_rx_multiple_enable(pn512_t *pPN512);
#define PN512_FRAMING_IS_TARGET( framing ) ((framing) <= nfc_framing_target_f_424)
void pn512_rf_field_switch_off(pn512_t* pPN512);
void pn512_rf_field_switch_off(pn512_t *pPN512);
void pn512_rf_field_nfcip1_rf_collision_avoidance(pn512_t* pPN512, pn512_cb_t cb);
void pn512_rf_field_nfcip1_rf_collision_avoidance(pn512_t *pPN512, pn512_cb_t cb);
void pn512_rf_field_wait_for_external(pn512_t* pPN512, int timeout, pn512_cb_t cb);
void pn512_rf_field_wait_for_external(pn512_t *pPN512, int timeout, pn512_cb_t cb);
#ifdef __cplusplus

View File

@ -25,7 +25,7 @@
#include "pn512_timer.h"
#include "pn512_registers.h"
void pn512_timer_config(pn512_t* pPN512, bool autostart, uint16_t prescaler, uint16_t countdown_value)
void pn512_timer_config(pn512_t *pPN512, bool autostart, uint16_t prescaler, uint16_t countdown_value)
{
pn512_timer_stop(pPN512); //just in case...
@ -33,14 +33,13 @@ void pn512_timer_config(pn512_t* pPN512, bool autostart, uint16_t prescaler, uin
pn512_register_write(pPN512, PN512_REG_TRELOADHIGH, (countdown_value >> 8) & 0xFF);
pn512_register_write(pPN512, PN512_REG_TPRESCALERLOW, prescaler & 0xFF);
pn512_register_write(pPN512, PN512_REG_TMODE_TPRESCALERHIGH, (autostart?0x80:0x00) | ((prescaler>>8) & 0x0F) );
pn512_register_write(pPN512, PN512_REG_TMODE_TPRESCALERHIGH, (autostart ? 0x80 : 0x00) | ((prescaler >> 8) & 0x0F));
}
void pn512_timer_start(pn512_t* pPN512)
void pn512_timer_start(pn512_t *pPN512)
{
//The control register also contains the initiator bit that we must set correctly
switch(pPN512->framing)
{
switch (pPN512->framing) {
case nfc_framing_initiator_a_106:
case nfc_framing_initiator_f_212:
case nfc_framing_initiator_f_424:
@ -56,11 +55,10 @@ void pn512_timer_start(pn512_t* pPN512)
}
}
void pn512_timer_stop(pn512_t* pPN512)
void pn512_timer_stop(pn512_t *pPN512)
{
//The control register also contains the initiator bit that we must set correctly
switch(pPN512->framing)
{
switch (pPN512->framing) {
case nfc_framing_initiator_a_106:
case nfc_framing_initiator_f_212:
case nfc_framing_initiator_f_424:

View File

@ -31,10 +31,10 @@ extern "C" {
typedef struct __pn512 pn512_t;
void pn512_timer_config(pn512_t* pPN512, bool autostart, uint16_t prescaler, uint16_t countdown_value);
void pn512_timer_config(pn512_t *pPN512, bool autostart, uint16_t prescaler, uint16_t countdown_value);
void pn512_timer_start(pn512_t* pPN512);
void pn512_timer_stop(pn512_t* pPN512);
void pn512_timer_start(pn512_t *pPN512);
void pn512_timer_stop(pn512_t *pPN512);
#ifdef __cplusplus
}

View File

@ -38,29 +38,23 @@
#define TIMEOUT 1000
void pn512_transceive_hw_tx_iteration(pn512_t* pPN512, bool start)
void pn512_transceive_hw_tx_iteration(pn512_t *pPN512, bool start)
{
uint16_t irqs_en = pn512_irq_enabled(pPN512);
if( buffer_reader_readable(&pPN512->writeBuf) > 0 )
{
if (buffer_reader_readable(&pPN512->writeBuf) > 0) {
//Fill FIFO
pn512_fifo_write(pPN512, &pPN512->writeBuf);
if (buffer_reader_readable(&pPN512->writeBuf) > 0) //Did not fit in FIFO
{
if (buffer_reader_readable(&pPN512->writeBuf) > 0) { //Did not fit in FIFO
pn512_irq_clear(pPN512, PN512_IRQ_LOW_ALERT);
//Has low FIFO alert IRQ already been enabled?
if (!(irqs_en & PN512_IRQ_LOW_ALERT))
{
if (!(irqs_en & PN512_IRQ_LOW_ALERT)) {
irqs_en |= PN512_IRQ_LOW_ALERT;
pn512_irq_set(pPN512, irqs_en);
}
}
else
{
if (irqs_en & PN512_IRQ_LOW_ALERT)
{
} else {
if (irqs_en & PN512_IRQ_LOW_ALERT) {
//Buffer has been fully sent
irqs_en &= ~PN512_IRQ_LOW_ALERT;
pn512_irq_set(pPN512, irqs_en);
@ -69,19 +63,15 @@ void pn512_transceive_hw_tx_iteration(pn512_t* pPN512, bool start)
}
if (start)
{
if ( (pPN512->transceive.mode == pn512_transceive_mode_transmit) || (pPN512->transceive.mode == pn512_transceive_mode_transmit_and_target_autocoll) )
{
if (start) {
if ((pPN512->transceive.mode == pn512_transceive_mode_transmit) || (pPN512->transceive.mode == pn512_transceive_mode_transmit_and_target_autocoll)) {
//Update bitframing register
pn512_register_write(pPN512, PN512_REG_BITFRAMING,
0x00 | ((pPN512->readFirstByteAlign & 0x7) << 4) | (pPN512->writeLastByteLength & 0x7));
//Use transmit command
pn512_cmd_exec(pPN512, PN512_CMD_TRANSMIT);
}
else
{
} else {
NFC_DBG("Bitframing %02X", 0x80 | ((pPN512->readFirstByteAlign & 0x7) << 4) | (pPN512->writeLastByteLength & 0x7));
//Update bitframing register to start transmission
pn512_register_write(pPN512, PN512_REG_BITFRAMING,
@ -98,12 +88,11 @@ void pn512_transceive_hw_tx_iteration(pn512_t* pPN512, bool start)
scheduler_queue_task(&pPN512->transceiver.scheduler, &pPN512->transceiver.task);
}
void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
void pn512_transceive_hw_tx_task(uint32_t events, void *pUserData)
{
pn512_t* pPN512 = (pn512_t*) pUserData;
pn512_t *pPN512 = (pn512_t *) pUserData;
if(events & EVENT_ABORTED)
{
if (events & EVENT_ABORTED) {
//Stop command
pn512_cmd_exec(pPN512, PN512_CMD_IDLE);
pPN512->transceive.mode = pn512_transceive_mode_idle;
@ -118,8 +107,7 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
}
NFC_DBG("TX task");
if(events & EVENT_TIMEOUT)
{
if (events & EVENT_TIMEOUT) {
// Check status
NFC_DBG("Status = %02X %02X", pn512_register_read(pPN512, PN512_REG_STATUS1), pn512_register_read(pPN512, PN512_REG_STATUS2));
@ -140,8 +128,7 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
uint16_t irqs_en = pn512_irq_enabled(pPN512);
uint16_t irqs = pn512_irq_get(pPN512);
if( irqs & PN512_IRQ_RF_OFF )
{
if (irqs & PN512_IRQ_RF_OFF) {
//Stop command
pn512_cmd_exec(pPN512, PN512_CMD_IDLE);
pPN512->transceive.mode = pn512_transceive_mode_idle;
@ -152,10 +139,8 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
pn512_transceive_callback(pPN512, NFC_ERR_FIELD);
return;
}
if( irqs & PN512_IRQ_TX )
{
if( irqs_en & PN512_IRQ_LOW_ALERT )
{
if (irqs & PN512_IRQ_TX) {
if (irqs_en & PN512_IRQ_LOW_ALERT) {
//If the transmission has been completed without us getting a chance to fill the buffer up it means that we had a buffer underflow
NFC_ERR("Buffer underflow");
pn512_irq_set(pPN512, PN512_IRQ_NONE);
@ -171,10 +156,8 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
//Start receiving
NFC_DBG("Transmission complete");
if(pPN512->transceive.mode != pn512_transceive_mode_transmit)
{
if(pPN512->transceive.mode == pn512_transceive_mode_transmit_and_target_autocoll)
{
if (pPN512->transceive.mode != pn512_transceive_mode_transmit) {
if (pPN512->transceive.mode == pn512_transceive_mode_transmit_and_target_autocoll) {
//Make sure bitframing reg is clean
pn512_register_write(pPN512, PN512_REG_BITFRAMING, 0x00);
@ -183,15 +166,11 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
//Start autocoll
pn512_cmd_exec(pPN512, PN512_CMD_AUTOCOLL);
}
else
{
} else {
pn512_transceive_hw_rx_start(pPN512);
}
return;
}
else
{
} else {
pn512_irq_set(pPN512, PN512_IRQ_NONE);
pn512_irq_clear(pPN512, PN512_IRQ_RX | PN512_IRQ_HIGH_ALERT);
@ -199,8 +178,7 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
return;
}
}
if( (irqs & PN512_IRQ_LOW_ALERT) && (buffer_reader_readable(&pPN512->writeBuf) > 0) )
{
if ((irqs & PN512_IRQ_LOW_ALERT) && (buffer_reader_readable(&pPN512->writeBuf) > 0)) {
//Continue to fill FIFO
pn512_irq_clear(pPN512, PN512_IRQ_LOW_ALERT);
@ -208,8 +186,7 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
return;
}
if( irqs & PN512_IRQ_IDLE )
{
if (irqs & PN512_IRQ_IDLE) {
pn512_irq_clear(pPN512, PN512_IRQ_ERR);
NFC_ERR("Modem went to idle");
@ -227,11 +204,10 @@ void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData)
pn512_transceive_hw_tx_iteration(pPN512, false);
}
void pn512_transceive_hw_rx_start(pn512_t* pPN512)
void pn512_transceive_hw_rx_start(pn512_t *pPN512)
{
uint16_t irqs_en = PN512_IRQ_RX | PN512_IRQ_HIGH_ALERT | PN512_IRQ_ERR;
if(PN512_FRAMING_IS_TARGET(pPN512->framing))
{
if (PN512_FRAMING_IS_TARGET(pPN512->framing)) {
irqs_en |= PN512_IRQ_RF_OFF;
}
@ -247,13 +223,12 @@ void pn512_transceive_hw_rx_start(pn512_t* pPN512)
&pPN512->transceiver.task);
}
void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
void pn512_transceive_hw_rx_task(uint32_t events, void *pUserData)
{
pn512_t* pPN512 = (pn512_t*) pUserData;
pn512_t *pPN512 = (pn512_t *) pUserData;
NFC_DBG("RX task");
if(events & EVENT_ABORTED)
{
if (events & EVENT_ABORTED) {
//Stop command
pn512_cmd_exec(pPN512, PN512_CMD_IDLE);
pPN512->transceive.mode = pn512_transceive_mode_idle;
@ -267,8 +242,7 @@ void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
return;
}
if(events & EVENT_TIMEOUT)
{
if (events & EVENT_TIMEOUT) {
NFC_WARN("Timeout");
//Stop command
pn512_cmd_exec(pPN512, PN512_CMD_IDLE);
@ -285,31 +259,26 @@ void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
uint16_t irqs = pn512_irq_get(pPN512);
NFC_DBG("irqs %04x", irqs);
bool collision_detected = false;
if( irqs & PN512_IRQ_ERR )
{
if (irqs & PN512_IRQ_ERR) {
pn512_irq_clear(pPN512, PN512_IRQ_ERR);
uint8_t err_reg = pn512_register_read(pPN512, PN512_REG_ERROR);
NFC_ERR("Got error - error reg is %02X", err_reg);
// if err_reg == 0, sticky error that must have been cleared automatically, continue
if( err_reg != 0 )
{
if (err_reg != 0) {
//If it's a collsision, flag it but still carry on with RX procedure
collision_detected = true;
if( (err_reg == 0x08) || (err_reg == 0x0A) ) // Collision (and maybe parity) (and no other error)
{
if ((err_reg == 0x08) || (err_reg == 0x0A)) { // Collision (and maybe parity) (and no other error)
irqs &= ~PN512_IRQ_ERR;
irqs |= PN512_IRQ_RX;
}
else
{
} else {
DBG_BLOCK(
//Empty FIFO into buffer
pn512_fifo_read(pPN512, &pPN512->readBufBldr);
NFC_DBG("Received");
buffer_dump( buffer_builder_buffer(&pPN512->readBufBldr) );
buffer_dump(buffer_builder_buffer(&pPN512->readBufBldr));
NFC_DBG("Computed CRC = %02X %02X", pn512_register_read(pPN512, PN512_REG_CRCRESULT_MSB), pn512_register_read(pPN512, PN512_REG_CRCRESULT_LSB));
@ -328,13 +297,11 @@ void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
}
}
}
if( (irqs & PN512_IRQ_RX) || (irqs & PN512_IRQ_HIGH_ALERT) )
{
if ((irqs & PN512_IRQ_RX) || (irqs & PN512_IRQ_HIGH_ALERT)) {
//Empty FIFO into buffer
pn512_fifo_read(pPN512, &pPN512->readBufBldr);
if( (buffer_builder_writeable(&pPN512->readBufBldr) == 0) && (pn512_fifo_length(pPN512) > 0) )
{
if ((buffer_builder_writeable(&pPN512->readBufBldr) == 0) && (pn512_fifo_length(pPN512) > 0)) {
//Stop command
pn512_cmd_exec(pPN512, PN512_CMD_IDLE);
pPN512->transceive.mode = pn512_transceive_mode_idle;
@ -348,19 +315,16 @@ void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
return; //overflow
}
if( irqs & PN512_IRQ_HIGH_ALERT )
{
if (irqs & PN512_IRQ_HIGH_ALERT) {
NFC_DBG("High alert");
pn512_irq_clear(pPN512, PN512_IRQ_HIGH_ALERT);
}
if( irqs & PN512_IRQ_RX )
{
if (irqs & PN512_IRQ_RX) {
pn512_irq_clear(pPN512, PN512_IRQ_RX);
size_t last_byte_length = pn512_register_read(pPN512, PN512_REG_CONTROL) & 0x7;
if( last_byte_length == 0 )
{
if (last_byte_length == 0) {
last_byte_length = 8;
}
pPN512->readLastByteLength = last_byte_length;
@ -369,13 +333,11 @@ void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
pn512_irq_clear(pPN512, PN512_IRQ_RX | PN512_IRQ_HIGH_ALERT);
NFC_DBG("Received:");
DBG_BLOCK( buffer_dump( buffer_builder_buffer(&pPN512->readBufBldr) ); )
DBG_BLOCK(buffer_dump(buffer_builder_buffer(&pPN512->readBufBldr));)
if( (pPN512->transceive.mode == pn512_transceive_mode_target_autocoll) || (pPN512->transceive.mode == pn512_transceive_mode_transmit_and_target_autocoll) )
{
if ((pPN512->transceive.mode == pn512_transceive_mode_target_autocoll) || (pPN512->transceive.mode == pn512_transceive_mode_transmit_and_target_autocoll)) {
//Check if target was activated
if( !(pn512_register_read(pPN512, PN512_REG_STATUS2) & 0x10) )
{
if (!(pn512_register_read(pPN512, PN512_REG_STATUS2) & 0x10)) {
pPN512->transceive.mode = pn512_transceive_mode_idle;
pn512_irq_set(pPN512, PN512_IRQ_NONE);
@ -386,27 +348,21 @@ void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
}
//PN512 switches to transceive automatically
pPN512->transceive.mode = pn512_transceive_mode_transceive;
}
else if( pPN512->transceive.mode == pn512_transceive_mode_receive )
{
} else if (pPN512->transceive.mode == pn512_transceive_mode_receive) {
pPN512->transceive.mode = pn512_transceive_mode_transceive;
//pn512_cmd_exec(pPN512, PN512_CMD_IDLE); //Useful?
}
if(!collision_detected)
{
if (!collision_detected) {
pn512_transceive_callback(pPN512, NFC_OK);
}
else
{
} else {
pn512_transceive_callback(pPN512, NFC_ERR_COLLISION);
}
return;
}
}
if( irqs & PN512_IRQ_RF_OFF )
{
if (irqs & PN512_IRQ_RF_OFF) {
//Stop command
pn512_cmd_exec(pPN512, PN512_CMD_IDLE);
pPN512->transceive.mode = pn512_transceive_mode_idle;
@ -426,7 +382,7 @@ void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData)
&pPN512->transceiver.task);
}
void pn512_transceive_hw(pn512_t* pPN512, pn512_transceive_mode_t mode, pn512_cb_t cb)
void pn512_transceive_hw(pn512_t *pPN512, pn512_transceive_mode_t mode, pn512_cb_t cb)
{
uint16_t irqs_en;
@ -437,33 +393,26 @@ void pn512_transceive_hw(pn512_t* pPN512, pn512_transceive_mode_t mode, pn512_cb
pn512_fifo_clear(pPN512);
//Clear previous IRQs if present
pn512_irq_clear(pPN512, PN512_IRQ_RX | PN512_IRQ_TX | PN512_IRQ_HIGH_ALERT | PN512_IRQ_LOW_ALERT | PN512_IRQ_ERR | PN512_IRQ_IDLE | PN512_IRQ_RF_OFF );
pn512_irq_clear(pPN512, PN512_IRQ_RX | PN512_IRQ_TX | PN512_IRQ_HIGH_ALERT | PN512_IRQ_LOW_ALERT | PN512_IRQ_ERR | PN512_IRQ_IDLE | PN512_IRQ_RF_OFF);
if( PN512_FRAMING_IS_TARGET(pPN512->framing) )
{
if (PN512_FRAMING_IS_TARGET(pPN512->framing)) {
//RF off?
if(!(pn512_register_read(pPN512, PN512_REG_STATUS1) & 0x04))
{
if (!(pn512_register_read(pPN512, PN512_REG_STATUS1) & 0x04)) {
//Call callback
pn512_transceive_callback(pPN512, NFC_ERR_FIELD);
return;
}
}
else if((pPN512->transceive.mode != mode) && (mode == pn512_transceive_mode_transceive))
{
} else if ((pPN512->transceive.mode != mode) && (mode == pn512_transceive_mode_transceive)) {
pn512_cmd_exec(pPN512, PN512_CMD_TRANSCEIVE);
}
pPN512->transceive.mode = mode;
if( mode == pn512_transceive_mode_receive )
{
if (mode == pn512_transceive_mode_receive) {
pn512_cmd_exec(pPN512, PN512_CMD_IDLE);
pn512_transceive_hw_rx_start(pPN512);
pn512_cmd_exec(pPN512, PN512_CMD_TRANSCEIVE);
}
else if(mode == pn512_transceive_mode_target_autocoll)
{
} else if (mode == pn512_transceive_mode_target_autocoll) {
//Make sure bitframing reg is clean
pn512_register_write(pPN512, PN512_REG_BITFRAMING, 0x00);
@ -472,16 +421,13 @@ void pn512_transceive_hw(pn512_t* pPN512, pn512_transceive_mode_t mode, pn512_cb
//Start autocoll
pn512_cmd_exec(pPN512, PN512_CMD_AUTOCOLL);
return;
}
else
{
} else {
NFC_DBG("Sending:");
DBG_BLOCK( buffer_dump(&pPN512->writeBuf); )
DBG_BLOCK(buffer_dump(&pPN512->writeBuf);)
//Transmit a frame to remote target/initiator
irqs_en = PN512_IRQ_TX | PN512_IRQ_IDLE;
if( PN512_FRAMING_IS_TARGET(pPN512->framing) )
{
if (PN512_FRAMING_IS_TARGET(pPN512->framing)) {
irqs_en |= PN512_IRQ_RF_OFF;
}

View File

@ -29,13 +29,13 @@ extern "C" {
#include "pn512.h"
void pn512_transceive_hw(pn512_t* pPN512, pn512_transceive_mode_t mode, pn512_cb_t cb);
void pn512_transceive_hw(pn512_t *pPN512, pn512_transceive_mode_t mode, pn512_cb_t cb);
void pn512_transceive_hw_tx_task(uint32_t events, void* pUserData);
void pn512_transceive_hw_tx_iteration(pn512_t* pPN512, bool start);
void pn512_transceive_hw_tx_task(uint32_t events, void *pUserData);
void pn512_transceive_hw_tx_iteration(pn512_t *pPN512, bool start);
void pn512_transceive_hw_rx_start(pn512_t* pPN512);
void pn512_transceive_hw_rx_task(uint32_t events, void* pUserData);
void pn512_transceive_hw_rx_start(pn512_t *pPN512);
void pn512_transceive_hw_rx_task(uint32_t events, void *pUserData);
#ifdef __cplusplus

View File

@ -27,8 +27,7 @@
extern "C" {
#endif
typedef struct __pn512_registers
{
typedef struct __pn512_registers {
int8_t registers_page;
} pn512_registers_t;

View File

@ -34,8 +34,7 @@
extern "C" {
#endif
typedef enum __RF_PROTOCOL
{
typedef enum __RF_PROTOCOL {
__RF_PROTOCOL_UNKNOWN = 0,
//Reader
RF_PROTOCOL_ISO_14443_A_READER,
@ -67,12 +66,11 @@ typedef uint32_t RF_OPTION;
#define RF_OPTION_CHECK_PARITY 0x08
#define RF_OPTION_CLOSE 0x10 //Last frame
typedef enum __RF_BITRATE
{
RF_BITRATE_106K=0x00,
RF_BITRATE_212K=0x01,
RF_BITRATE_424K=0x02,
RF_BITRATE_848K=0x03,
typedef enum __RF_BITRATE {
RF_BITRATE_106K = 0x00,
RF_BITRATE_212K = 0x01,
RF_BITRATE_424K = 0x02,
RF_BITRATE_848K = 0x03,
} RF_BITRATE;

View File

@ -34,7 +34,7 @@
* \param pTransport pointer to already initialized nfc_transport_t structure
* \param pImpl pointer to the structure implementing the transceiver interface (eg pn512_t or pn532_t)
*/
void transceiver_init(nfc_transceiver_t* pTransceiver, nfc_transport_t* pTransport, nfc_scheduler_timer_t* pTimer)
void transceiver_init(nfc_transceiver_t *pTransceiver, nfc_transport_t *pTransport, nfc_scheduler_timer_t *pTimer)
{
pTransceiver->pTransport = pTransport;
scheduler_init(&pTransceiver->scheduler, pTimer);

View File

@ -51,8 +51,7 @@ enum __nfc_framing {
nfc_framing_initiator_f_424,
};
typedef struct __nfc_tech
{
typedef struct __nfc_tech {
unsigned int nfc_type1 : 1;
unsigned int nfc_type2 : 1;
unsigned int nfc_type3 : 1;
@ -64,33 +63,31 @@ typedef struct __nfc_tech
} nfc_tech_t;
typedef struct __polling_options polling_options_t;
struct __polling_options
{
struct __polling_options {
unsigned int bail_at_first_target : 1;
unsigned int bail_at_first_tech : 1;
int32_t listen_for;
};
typedef void (*transceiver_cb_t)(nfc_transceiver_t* pTransceiver, nfc_err_t ret, void* pUserData);
typedef void (*set_protocols_fn_t)(nfc_transceiver_t* pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options);
typedef void (*poll_fn_t)(nfc_transceiver_t* pTransceiver);
typedef void (*set_crc_fn_t)(nfc_transceiver_t* pTransceiver, bool crcOut, bool crcIn);
typedef void (*set_timeout_fn_t)(nfc_transceiver_t* pTransceiver, int timeout);
typedef void (*set_transceive_options_fn_t)(nfc_transceiver_t* pTransceiver, bool transmit, bool receive, bool repoll);
typedef void (*set_transceive_framing_fn_t)(nfc_transceiver_t* pTransceiver, nfc_framing_t framing);
typedef void (*set_write_fn_t)(nfc_transceiver_t* pTransceiver, buffer_t* pWriteBuf); //Set write buffer
typedef buffer_t* (*get_read_fn_t)(nfc_transceiver_t* pTransceiver); //Get read buffer
typedef size_t (*get_last_byte_length_fn_t)(nfc_transceiver_t* pTransceiver);
typedef void (*set_last_byte_length_fn_t)(nfc_transceiver_t* pTransceiver, size_t lastByteLength);
typedef size_t (*get_first_byte_align_fn_t)(nfc_transceiver_t* pTransceiver);
typedef void (*set_first_byte_align_fn_t)(nfc_transceiver_t* pTransceiver, size_t firstByteAlign);
typedef void (*transceive_fn_t)(nfc_transceiver_t* pTransceiver);
typedef void (*abort_fn_t)(nfc_transceiver_t* pTransceiver);
typedef void (*close_fn_t)(nfc_transceiver_t* pTransceiver);
typedef void (*sleep_fn_t)(nfc_transceiver_t* pTransceiver, bool sleep);
typedef void (*transceiver_cb_t)(nfc_transceiver_t *pTransceiver, nfc_err_t ret, void *pUserData);
typedef void (*set_protocols_fn_t)(nfc_transceiver_t *pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options);
typedef void (*poll_fn_t)(nfc_transceiver_t *pTransceiver);
typedef void (*set_crc_fn_t)(nfc_transceiver_t *pTransceiver, bool crcOut, bool crcIn);
typedef void (*set_timeout_fn_t)(nfc_transceiver_t *pTransceiver, int timeout);
typedef void (*set_transceive_options_fn_t)(nfc_transceiver_t *pTransceiver, bool transmit, bool receive, bool repoll);
typedef void (*set_transceive_framing_fn_t)(nfc_transceiver_t *pTransceiver, nfc_framing_t framing);
typedef void (*set_write_fn_t)(nfc_transceiver_t *pTransceiver, buffer_t *pWriteBuf); //Set write buffer
typedef buffer_t *(*get_read_fn_t)(nfc_transceiver_t *pTransceiver); //Get read buffer
typedef size_t (*get_last_byte_length_fn_t)(nfc_transceiver_t *pTransceiver);
typedef void (*set_last_byte_length_fn_t)(nfc_transceiver_t *pTransceiver, size_t lastByteLength);
typedef size_t (*get_first_byte_align_fn_t)(nfc_transceiver_t *pTransceiver);
typedef void (*set_first_byte_align_fn_t)(nfc_transceiver_t *pTransceiver, size_t firstByteAlign);
typedef void (*transceive_fn_t)(nfc_transceiver_t *pTransceiver);
typedef void (*abort_fn_t)(nfc_transceiver_t *pTransceiver);
typedef void (*close_fn_t)(nfc_transceiver_t *pTransceiver);
typedef void (*sleep_fn_t)(nfc_transceiver_t *pTransceiver, bool sleep);
struct __transceiver_impl
{
struct __transceiver_impl {
set_protocols_fn_t set_protocols;
poll_fn_t poll;
set_crc_fn_t set_crc;
@ -109,8 +106,7 @@ struct __transceiver_impl
};
typedef struct __nfc_a_info nfc_a_info_t;
struct __nfc_a_info
{
struct __nfc_a_info {
uint8_t uid[10];
size_t uidLength;
uint8_t sak;
@ -118,23 +114,20 @@ struct __nfc_a_info
};
typedef struct __nfc_b_info nfc_b_info_t;
struct __nfc_b_info
{
struct __nfc_b_info {
uint8_t pupi[4];
uint8_t application_data[4];
uint8_t protocol_info[3];
};
typedef struct __nfc_f_info nfc_f_info_t;
struct __nfc_f_info
{
struct __nfc_f_info {
uint8_t nfcid2[8];
};
typedef struct __nfc_info nfc_info_t;
struct __nfc_info
{
struct __nfc_info {
nfc_tech_t type;
union {
nfc_a_info_t nfcA;
@ -144,9 +137,8 @@ struct __nfc_info
};
#define MUNFC_MAX_REMOTE_TARGETS 4
struct __transceiver
{
const transceiver_impl_t* fn; //vtable
struct __transceiver {
const transceiver_impl_t *fn; //vtable
bool initiator_ntarget;
nfc_info_t remote_targets[MUNFC_MAX_REMOTE_TARGETS];
@ -155,119 +147,118 @@ struct __transceiver
nfc_tech_t active_tech;
transceiver_cb_t cb; //Callback to upper layer
void* pUserData;
void *pUserData;
nfc_task_t task; //Task for deferred execution
nfc_transport_t* pTransport;
nfc_transport_t *pTransport;
nfc_scheduler_t scheduler;
};
void transceiver_init(nfc_transceiver_t* pTransceiver, nfc_transport_t* pTransport, nfc_scheduler_timer_t* pTimer);
void transceiver_init(nfc_transceiver_t *pTransceiver, nfc_transport_t *pTransport, nfc_scheduler_timer_t *pTimer);
static inline void transceiver_set_protocols(nfc_transceiver_t* pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options)
static inline void transceiver_set_protocols(nfc_transceiver_t *pTransceiver, nfc_tech_t initiators, nfc_tech_t targets, polling_options_t options)
{
pTransceiver->fn->set_protocols(pTransceiver, initiators, targets, options);
}
static inline void transceiver_poll(nfc_transceiver_t* pTransceiver, transceiver_cb_t cb, void* pUserData)
static inline void transceiver_poll(nfc_transceiver_t *pTransceiver, transceiver_cb_t cb, void *pUserData)
{
pTransceiver->cb = cb;
pTransceiver->pUserData = pUserData;
pTransceiver->fn->poll(pTransceiver);
}
static inline void transceiver_set_crc(nfc_transceiver_t* pTransceiver, bool crcOut, bool crcIn)
static inline void transceiver_set_crc(nfc_transceiver_t *pTransceiver, bool crcOut, bool crcIn)
{
pTransceiver->fn->set_crc(pTransceiver, crcOut, crcIn);
}
static inline void transceiver_set_timeout(nfc_transceiver_t* pTransceiver, int timeout)
static inline void transceiver_set_timeout(nfc_transceiver_t *pTransceiver, int timeout)
{
pTransceiver->fn->set_timeout(pTransceiver, timeout);
}
static inline void transceiver_set_transceive_options(nfc_transceiver_t* pTransceiver, bool transmit, bool receive, bool repoll)
static inline void transceiver_set_transceive_options(nfc_transceiver_t *pTransceiver, bool transmit, bool receive, bool repoll)
{
pTransceiver->fn->set_transceive_options(pTransceiver, transmit, receive, repoll);
}
static inline void transceiver_set_transceive_framing(nfc_transceiver_t* pTransceiver, nfc_framing_t framing)
static inline void transceiver_set_transceive_framing(nfc_transceiver_t *pTransceiver, nfc_framing_t framing)
{
pTransceiver->fn->set_transceive_framing(pTransceiver, framing);
}
static inline void transceiver_set_write(nfc_transceiver_t* pTransceiver, buffer_t* pWriteBuf)
static inline void transceiver_set_write(nfc_transceiver_t *pTransceiver, buffer_t *pWriteBuf)
{
pTransceiver->fn->set_write(pTransceiver, pWriteBuf);
}
static inline buffer_t* transceiver_get_read(nfc_transceiver_t* pTransceiver)
static inline buffer_t *transceiver_get_read(nfc_transceiver_t *pTransceiver)
{
return pTransceiver->fn->get_read(pTransceiver);
}
static inline size_t transceiver_get_last_byte_length(nfc_transceiver_t* pTransceiver)
static inline size_t transceiver_get_last_byte_length(nfc_transceiver_t *pTransceiver)
{
return pTransceiver->fn->get_last_byte_length(pTransceiver);
}
static inline void transceiver_set_last_byte_length(nfc_transceiver_t* pTransceiver, size_t lastByteLength)
static inline void transceiver_set_last_byte_length(nfc_transceiver_t *pTransceiver, size_t lastByteLength)
{
pTransceiver->fn->set_last_byte_length(pTransceiver, lastByteLength);
}
static inline void transceiver_set_first_byte_align(nfc_transceiver_t* pTransceiver, size_t firstByteAlign)
static inline void transceiver_set_first_byte_align(nfc_transceiver_t *pTransceiver, size_t firstByteAlign)
{
pTransceiver->fn->set_first_byte_align(pTransceiver, firstByteAlign);
}
static inline void nfc_transceiver_transceive(nfc_transceiver_t* pTransceiver, transceiver_cb_t cb, void* pUserData)
static inline void nfc_transceiver_transceive(nfc_transceiver_t *pTransceiver, transceiver_cb_t cb, void *pUserData)
{
pTransceiver->cb = cb;
pTransceiver->pUserData = pUserData;
pTransceiver->fn->transceive(pTransceiver);
}
static inline void transceiver_abort(nfc_transceiver_t* pTransceiver)
static inline void transceiver_abort(nfc_transceiver_t *pTransceiver)
{
pTransceiver->fn->abort(pTransceiver);
}
static inline void transceiver_close(nfc_transceiver_t* pTransceiver)
static inline void transceiver_close(nfc_transceiver_t *pTransceiver)
{
pTransceiver->fn->close(pTransceiver);
}
static inline bool transceiver_is_initiator_mode(nfc_transceiver_t* pTransceiver)
static inline bool transceiver_is_initiator_mode(nfc_transceiver_t *pTransceiver)
{
return pTransceiver->initiator_ntarget;
}
static inline nfc_tech_t transceiver_get_active_techs(nfc_transceiver_t* pTransceiver)
static inline nfc_tech_t transceiver_get_active_techs(nfc_transceiver_t *pTransceiver)
{
return pTransceiver->active_tech;
}
static inline nfc_scheduler_t* transceiver_get_scheduler(nfc_transceiver_t* pTransceiver)
static inline nfc_scheduler_t *transceiver_get_scheduler(nfc_transceiver_t *pTransceiver)
{
return &pTransceiver->scheduler;
}
static inline const nfc_info_t* transceiver_get_remote_target_info(nfc_transceiver_t* pTransceiver, size_t number)
static inline const nfc_info_t *transceiver_get_remote_target_info(nfc_transceiver_t *pTransceiver, size_t number)
{
if( number > pTransceiver->remote_targets_count )
{
if (number > pTransceiver->remote_targets_count) {
return NULL;
}
return &pTransceiver->remote_targets[number];
}
static inline size_t transceiver_get_remote_targets_count(nfc_transceiver_t* pTransceiver)
static inline size_t transceiver_get_remote_targets_count(nfc_transceiver_t *pTransceiver)
{
return pTransceiver->remote_targets_count;
}
static inline void transceiver_sleep(nfc_transceiver_t* pTransceiver, bool sleep)
static inline void transceiver_sleep(nfc_transceiver_t *pTransceiver, bool sleep)
{
pTransceiver->fn->sleep(pTransceiver, sleep);
}

View File

@ -29,7 +29,7 @@ extern "C" {
#include "transceiver.h"
static inline void transceiver_callback(nfc_transceiver_t* pTransceiver, nfc_err_t ret)
static inline void transceiver_callback(nfc_transceiver_t *pTransceiver, nfc_err_t ret)
{
pTransceiver->cb(pTransceiver, ret, pTransceiver->pUserData);
}