Skip to content

Commit

Permalink
[nfcd] Added ISO-DEP support. JB#46767
Browse files Browse the repository at this point in the history
Also called Type 4 tags.
  • Loading branch information
monich committed Sep 6, 2019
1 parent bcd64d0 commit 844f8b2
Show file tree
Hide file tree
Showing 19 changed files with 1,816 additions and 6 deletions.
3 changes: 3 additions & 0 deletions core/Makefile
Expand Up @@ -41,6 +41,9 @@ SRC = \
nfc_plugin.c \
nfc_tag.c \
nfc_tag_t2.c \
nfc_tag_t4.c \
nfc_tag_t4a.c \
nfc_tag_t4b.c \
nfc_target.c \
nfc_tlv.c \
nfc_util.c
Expand Down
14 changes: 14 additions & 0 deletions core/include/nfc_adapter.h
Expand Up @@ -104,6 +104,20 @@ nfc_adapter_add_tag_t2(
NfcTarget* target,
const NfcTagParamT2* params);

NfcTag*
nfc_adapter_add_tag_t4a(
NfcAdapter* adapter,
NfcTarget* target,
const NfcParamPollA* tech_param,
const NfcParamIsoDepPollA* iso_dep_param); /* Since 1.0.20 */

NfcTag*
nfc_adapter_add_tag_t4b(
NfcAdapter* adapter,
NfcTarget* target,
const NfcParamPollB* tech_param,
const NfcParamIsoDepPollB* iso_dep_param); /* Since 1.0.20 */

NfcTag*
nfc_adapter_add_other_tag(
NfcAdapter* adapter,
Expand Down
5 changes: 5 additions & 0 deletions core/include/nfc_tag.h
Expand Up @@ -67,6 +67,11 @@ struct nfc_param_poll_a {
GUtilData nfcid1;
};

struct nfc_param_poll_b {
guint fsc; /* FSC (FSCI converted to bytes) */
GUtilData nfcid0;
};

typedef
void
(*NfcTagFunc)(
Expand Down
115 changes: 115 additions & 0 deletions core/include/nfc_tag_t4.h
@@ -0,0 +1,115 @@
/*
* Copyright (C) 2019 Jolla Ltd.
* Copyright (C) 2019 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the names of the copyright holders nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
* THE POSSIBILITY OF SUCH DAMAGE.
*/

#ifndef NFC_TAG_T4_H
#define NFC_TAG_T4_H

#include "nfc_tag.h"

/* Type 4 tag */

/* Since 1.0.20 */

typedef struct nfc_tag_t4_priv NfcTagType4Priv;

struct nfc_tag_t4 {
NfcTag tag;
NfcTagType4Priv* priv;
};

struct nfc_param_iso_dep_poll_a {
guint fsc; /* FSC (FSDI converted to bytes) */
GUtilData t1; /* T1 to Tk (aka historical bytes) */
};

GType nfc_tag_t4_get_type();
#define NFC_TYPE_TAG_T4 (nfc_tag_t4_get_type())
#define NFC_IS_TAG_T4(obj) G_TYPE_CHECK_INSTANCE_TYPE(obj, NFC_TYPE_TAG_T4)
#define NFC_TAG_T4(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), NFC_TYPE_TAG_T4, \
NfcTagType4))

GType nfc_tag_t4a_get_type();
#define NFC_TYPE_TAG_T4A (nfc_tag_t4a_get_type())
#define NFC_IS_TAG_T4A(obj) G_TYPE_CHECK_INSTANCE_TYPE(obj, NFC_TYPE_TAG_T4A)
#define NFC_TAG_T4A(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), NFC_TYPE_TAG_T4A, \
NfcTagType4a))

GType nfc_tag_t4b_get_type();
#define NFC_TYPE_TAG_T4B (nfc_tag_t4b_get_type())
#define NFC_IS_TAG_T4B(obj) G_TYPE_CHECK_INSTANCE_TYPE(obj, NFC_TYPE_TAG_T4B)
#define NFC_TAG_T4B(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), NFC_TYPE_TAG_T4B, \
NfcTagType4b))

/* Status bytes defined in ISO/IEC 7816-4 */

#define ISO_SW_OK (0x9000) /* Normal completion */
#define ISO_SW_SUCCESS(sw) (((sw) & 0xff00) == ISO_SW_OK)

/*
* SW1 value '60', as well as any value different from '9X' and '6X'
* are invalid according to ISO/IEC 7816-4. We use zero to indicate
* low level (non-protocol) I/O error.
*/
#define ISO_SW_IO_ERR (0)

typedef
void
(*NfcTagType4ResponseFunc)(
NfcTagType4* tag,
guint sw, /* 16 bits (SW1 << 8)|SW2 */
const void* data,
guint len,
void* user_data);

guint
nfc_isodep_transmit(
NfcTagType4* tag,
guint8 cla, /* Class byte */
guint8 ins, /* Instruction byte */
guint8 p1, /* Parameter byte 1 */
guint8 p2, /* Parameter byte 2 */
const GUtilData* data, /* Command data */
guint le, /* Expected length, zero if none */
NfcTargetSequence* seq,
NfcTagType4ResponseFunc resp,
GDestroyNotify destroy,
void* user_data);

#endif /* NFC_TAG_T4_H */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
6 changes: 6 additions & 0 deletions core/include/nfc_types.h
Expand Up @@ -47,10 +47,16 @@ typedef struct nfc_plugin NfcPlugin;
typedef struct nfc_plugin_desc NfcPluginDesc;
typedef struct nfc_tag NfcTag;
typedef struct nfc_tag_t2 NfcTagType2;
typedef struct nfc_tag_t4 NfcTagType4; /* Since 1.0.20 */
typedef struct nfc_tag_t4a NfcTagType4a; /* Since 1.0.20 */
typedef struct nfc_tag_t4b NfcTagType4b; /* Since 1.0.20 */
typedef struct nfc_target NfcTarget;
typedef struct nfc_target_sequence NfcTargetSequence;

typedef struct nfc_param_poll_a NfcParamPollA; /* Since 1.0.8 */
typedef struct nfc_param_poll_b NfcParamPollB; /* Since 1.0.20 */
typedef struct nfc_param_iso_dep_poll_a NfcParamIsoDepPollA; /* Since 1.0.20 */
typedef struct nfc_param_iso_dep_poll_b NfcParamIsoDepPollB; /* Since 1.0.20 */
typedef NfcParamPollA NfcTagParamT2; /* For backward compatibility */

/* Constants */
Expand Down
38 changes: 36 additions & 2 deletions core/src/nfc_adapter.c
Expand Up @@ -32,7 +32,7 @@

#include "nfc_adapter_p.h"
#include "nfc_tag_p.h"
#include "nfc_tag_t2.h"
#include "nfc_tag_t4_p.h"
#include "nfc_log.h"

#include <gutil_misc.h>
Expand Down Expand Up @@ -281,7 +281,7 @@ nfc_adapter_add_tag(
NfcTag* tag)
{
/* This function takes ownership of the tag */
if (tag->present) {
if (tag && tag->present) {
NfcAdapterPriv* priv = self->priv;
NfcAdapterTagEntry* entry = g_slice_new(NfcAdapterTagEntry);
char* name = g_strdup_printf(NFC_TAG_NAME_FORMAT, priv->next_tag_index);
Expand Down Expand Up @@ -404,6 +404,40 @@ nfc_adapter_add_tag_t2(
return NULL;
}

NfcTag*
nfc_adapter_add_tag_t4a(
NfcAdapter* self,
NfcTarget* target,
const NfcParamPollA* tech_param,
const NfcParamIsoDepPollA* iso_dep_param) /* Since 1.0.20 */
{
if (G_LIKELY(self) && G_LIKELY(target)) {
NfcTagType4a* t4a = nfc_tag_t4a_new(target, tech_param, iso_dep_param);

if (t4a) {
return nfc_adapter_add_tag(self, NFC_TAG(t4a));
}
}
return NULL;
}

NfcTag*
nfc_adapter_add_tag_t4b(
NfcAdapter* self,
NfcTarget* target,
const NfcParamPollB* tech_param,
const NfcParamIsoDepPollB* iso_dep_param) /* Since 1.0.20 */
{
if (G_LIKELY(self) && G_LIKELY(target)) {
NfcTagType4b* t4b = nfc_tag_t4b_new(target, tech_param, iso_dep_param);

if (t4b) {
return nfc_adapter_add_tag(self, NFC_TAG(t4b));
}
}
return NULL;
}

NfcTag*
nfc_adapter_add_other_tag(
NfcAdapter* self,
Expand Down

0 comments on commit 844f8b2

Please sign in to comment.