Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[core] Generic access to RF technology specific parameters. JB#50041
The following two new functions have been added to the core API:

   nfc_tag_param()
   nfc_adapter_add_other_tag2()
  • Loading branch information
monich committed Jun 8, 2020
1 parent d085df2 commit 3d9f3f9
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 78 deletions.
21 changes: 14 additions & 7 deletions core/include/nfc_adapter.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018-2019 Jolla Ltd.
* Copyright (C) 2018-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2018-2020 Jolla Ltd.
* Copyright (C) 2018-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand All @@ -14,8 +14,8 @@
* 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.
* 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
Expand Down Expand Up @@ -108,20 +108,27 @@ NfcTag*
nfc_adapter_add_tag_t4a(
NfcAdapter* adapter,
NfcTarget* target,
const NfcParamPollA* tech_param,
const NfcParamPollA* poll_a,
const NfcParamIsoDepPollA* iso_dep_param); /* Since 1.0.20 */

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

NfcTag*
nfc_adapter_add_other_tag(
NfcAdapter* adapter,
NfcTarget* target);
NfcTarget* target)
G_GNUC_DEPRECATED_FOR(nfc_adapter_add_other_tag2);

NfcTag*
nfc_adapter_add_other_tag2(
NfcAdapter* adapter,
NfcTarget* target,
const NfcParamPoll* poll); /* Since 1.0.33 */

void
nfc_adapter_remove_tag(
Expand Down
22 changes: 8 additions & 14 deletions core/include/nfc_tag.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018-2019 Jolla Ltd.
* Copyright (C) 2018-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2018-2020 Jolla Ltd.
* Copyright (C) 2018-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand All @@ -14,8 +14,8 @@
* 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.
* 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
Expand Down Expand Up @@ -62,16 +62,6 @@ GType nfc_tag_get_type(void);
#define NFC_TAG(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
NFC_TYPE_TAG, NfcTag))

struct nfc_param_poll_a {
guint8 sel_res; /* (SAK)*/
GUtilData nfcid1;
};

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

typedef
void
(*NfcTagFunc)(
Expand All @@ -86,6 +76,10 @@ void
nfc_tag_unref(
NfcTag* tag);

const NfcParamPoll*
nfc_tag_param(
NfcTag* tag); /* Since 1.0.33 */

void
nfc_tag_deactivate(
NfcTag* tag);
Expand Down
25 changes: 20 additions & 5 deletions core/include/nfc_types.h
@@ -1,6 +1,6 @@
/*
* Copyright (C) 2018-2019 Jolla Ltd.
* Copyright (C) 2018-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2018-2020 Jolla Ltd.
* Copyright (C) 2018-2020 Slava Monich <slava.monich@jolla.com>
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -53,11 +53,8 @@ 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 Expand Up @@ -103,6 +100,24 @@ typedef enum nfc_transmit_status {
NFC_TRANSMIT_STATUS_TIMEOUT /* No response from NFCC */
} NFC_TRANSMIT_STATUS;

/* RF technology specific parameters */

typedef struct nfc_param_poll_a {
guint8 sel_res; /* (SAK)*/
GUtilData nfcid1;
} NfcParamPollA, /* Since 1.0.8 */
NfcTagParamT2; /* This one for backward compatibility */

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

typedef union nfc_param_poll {
NfcParamPollA a;
NfcParamPollB b;
} NfcParamPoll; /* Since 1.0.33 */

/* Logging */
#define NFC_CORE_LOG_MODULE nfc_core_log
extern GLogModule NFC_CORE_LOG_MODULE;
Expand Down
11 changes: 10 additions & 1 deletion core/src/nfc_adapter.c
Expand Up @@ -445,9 +445,18 @@ NfcTag*
nfc_adapter_add_other_tag(
NfcAdapter* self,
NfcTarget* target)
{
return nfc_adapter_add_other_tag2(self, target, NULL);
}

NfcTag*
nfc_adapter_add_other_tag2(
NfcAdapter* self,
NfcTarget* target,
const NfcParamPoll* poll) /* Since 1.0.33 */
{
if (G_LIKELY(self)) {
NfcTag* tag = nfc_tag_new(target);
NfcTag* tag = nfc_tag_new(target, poll);

if (tag) {
return nfc_adapter_add_tag(self, tag);
Expand Down
55 changes: 52 additions & 3 deletions core/src/nfc_tag.c
Expand Up @@ -38,10 +38,12 @@
#include "nfc_log.h"

#include <gutil_misc.h>
#include <gutil_macros.h>

struct nfc_tag_priv {
char* name;
gulong gone_id;
NfcParamPoll* param;
};

G_DEFINE_TYPE(NfcTag, nfc_tag, G_TYPE_OBJECT)
Expand Down Expand Up @@ -77,12 +79,13 @@ nfc_tag_gone(

NfcTag*
nfc_tag_new(
NfcTarget* target)
NfcTarget* target,
const NfcParamPoll* poll)
{
if (G_LIKELY(target)) {
NfcTag* self = g_object_new(NFC_TYPE_TAG, NULL);

nfc_tag_init_base(self, target);
nfc_tag_init_base(self, target, poll);
return self;
}
return NULL;
Expand All @@ -107,6 +110,13 @@ nfc_tag_unref(
}
}

const NfcParamPoll*
nfc_tag_param(
NfcTag* self) /* Since 1.0.33 */
{
return G_LIKELY(self) ? self->priv->param : NULL;
}

void
nfc_tag_deactivate(
NfcTag* self)
Expand Down Expand Up @@ -162,7 +172,8 @@ nfc_tag_remove_handlers(
void
nfc_tag_init_base(
NfcTag* self,
NfcTarget* target)
NfcTarget* target,
const NfcParamPoll* poll)
{
NfcTagPriv* priv = self->priv;

Expand All @@ -172,6 +183,43 @@ nfc_tag_init_base(
self->present = target->present;
self->target = nfc_target_ref(target);
priv->gone_id = nfc_target_add_gone_handler(target, nfc_tag_gone, self);
if (poll) {
const gsize aligned_size = G_ALIGN8(sizeof(*poll));
const GUtilData* src;
gsize size;

/*
* Allocate the whole thing (including additional data) from a
* single memory block and adjust the pointers.
*/
switch (target->technology) {
case NFC_TECHNOLOGY_A:
src = &poll->a.nfcid1;
size = src->size ? (aligned_size + src->size) : sizeof(*poll);
*(priv->param = g_malloc0(size)) = *poll;
if (src->bytes) {
guint8* dest = (guint8*)priv->param + aligned_size;

memcpy(dest, src->bytes, src->size);
priv->param->a.nfcid1.bytes = dest;
}
break;
case NFC_TECHNOLOGY_B:
src = &poll->b.nfcid0;
size = src->size ? (aligned_size + src->size) : sizeof(*poll);
*(priv->param = g_malloc0(size)) = *poll;
if (src->bytes) {
guint8* dest = (guint8*)priv->param + aligned_size;

memcpy(dest, src->bytes, src->size);
priv->param->b.nfcid0.bytes = dest;
}
break;
case NFC_TECHNOLOGY_F:
case NFC_TECHNOLOGY_UNKNOWN:
break;
}
}
}

void
Expand Down Expand Up @@ -219,6 +267,7 @@ nfc_tag_finalize(
nfc_target_unref(self->target);
nfc_ndef_rec_unref(self->ndef);
g_free(priv->name);
g_free(priv->param);
G_OBJECT_CLASS(nfc_tag_parent_class)->finalize(object);
}

Expand Down
8 changes: 5 additions & 3 deletions core/src/nfc_tag_p.h
Expand Up @@ -43,19 +43,21 @@ typedef struct nfc_tag_class {

NfcTag*
nfc_tag_new(
NfcTarget* target)
NfcTarget* target,
const NfcParamPoll* poll)
NFCD_INTERNAL;

NfcTagType2*
nfc_tag_t2_new(
NfcTarget* target,
const NfcParamPollA* param)
const NfcParamPollA* poll_a)
NFCD_INTERNAL;

void
nfc_tag_init_base(
NfcTag* tag,
NfcTarget* target)
NfcTarget* target,
const NfcParamPoll* poll)
NFCD_INTERNAL;

void
Expand Down
25 changes: 15 additions & 10 deletions core/src/nfc_tag_t2.c
Expand Up @@ -115,7 +115,6 @@ struct nfc_tag_t2_priv {
GHashTable* writes;
GByteArray* cached_blocks;
guint8 serial[4];
void* nfcid1;
guint sector_count;
NfcTagType2Sector* sectors;
guint init_id;
Expand Down Expand Up @@ -957,18 +956,25 @@ void
nfc_tag_t2_init2(
NfcTagType2* self,
NfcTarget* target,
const NfcParamPollA* param)
const NfcParamPollA* poll_a)
{
NfcTagType2Priv* priv = self->priv;
NfcTag* tag = &self->tag;

nfc_tag_init_base(&self->tag, target);
priv->init_seq = nfc_target_sequence_new(target);
if (param) {
priv->nfcid1 = g_memdup(param->nfcid1.bytes, param->nfcid1.size);
self->sel_res = param->sel_res;
self->nfcid1.size = param->nfcid1.size;
self->nfcid1.bytes = priv->nfcid1;
if (poll_a) {
NfcParamPoll poll;

GASSERT(target->technology == NFC_TECHNOLOGY_A);
memset(&poll, 0, sizeof(poll));
poll.a = *poll_a;
nfc_tag_init_base(tag, target, &poll);
/* nfc_tag_init_base has copied nfcid1 to the internal storage */
self->nfcid1 = nfc_tag_param(tag)->a.nfcid1;
self->sel_res = poll_a->sel_res;
} else {
nfc_tag_init_base(tag, target, NULL);
}
priv->init_seq = nfc_target_sequence_new(target);
}

/*==========================================================================*
Expand Down Expand Up @@ -1387,7 +1393,6 @@ nfc_tag_t2_finalize(
}
nfc_target_cancel_transmit(self->tag.target, priv->init_id);
nfc_target_sequence_unref(priv->init_seq);
g_free(priv->nfcid1);
G_OBJECT_CLASS(nfc_tag_t2_parent_class)->finalize(object);
}

Expand Down
5 changes: 3 additions & 2 deletions core/src/nfc_tag_t4.c
Expand Up @@ -671,12 +671,13 @@ void
nfc_tag_t4_init_base(
NfcTagType4* self,
NfcTarget* target,
guint mtu)
guint mtu,
const NfcParamPoll* poll)
{
NfcTag* tag = &self->tag;
NfcTagType4Priv* priv = self->priv;

nfc_tag_init_base(tag, target);
nfc_tag_init_base(tag, target, poll);
priv->mtu = mtu;

/*
Expand Down
11 changes: 6 additions & 5 deletions core/src/nfc_tag_t4_p.h
Expand Up @@ -43,22 +43,23 @@ typedef struct nfc_tag_t4_class {
NfcTagType4a*
nfc_tag_t4a_new(
NfcTarget* target,
const NfcParamPollA* tech_param,
const NfcParamIsoDepPollA* interface_param)
const NfcParamPollA* poll_a,
const NfcParamIsoDepPollA* iso_dep_param)
NFCD_INTERNAL;

NfcTagType4b*
nfc_tag_t4b_new(
NfcTarget* target,
const NfcParamPollB* tech_param,
const NfcParamIsoDepPollB* interface_param)
const NfcParamPollB* poll_b,
const NfcParamIsoDepPollB* iso_dep_param)
NFCD_INTERNAL;

void
nfc_tag_t4_init_base(
NfcTagType4* tag,
NfcTarget* target,
guint mtu)
guint mtu,
const NfcParamPoll* poll)
NFCD_INTERNAL;

#endif /* NFC_TAG_T4_PRIVATE_H */
Expand Down

0 comments on commit 3d9f3f9

Please sign in to comment.