Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nfcd] Change GetSerial() method to return tag UID. JB#51099
Before this commit Serial included only part of original UID
(last 4 bytes for 7 byte UID). Now Serial contains full UID
that is same as NFCID1 from poll parameters for Tag A Type 2.
  • Loading branch information
ikamaletdinov committed Oct 22, 2020
1 parent 075e542 commit ae1ea87
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 33 deletions.
3 changes: 2 additions & 1 deletion core/include/nfc_tag_t2.h
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2018-2019 Jolla Ltd.
* Copyright (C) 2018-2019 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2020 Open Mobile Platform LLC.
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -54,7 +55,7 @@ struct nfc_tag_t2 {
NFC_TAG_T2_FLAGS t2flags;
guint block_size; /* Valid only when initialized */
guint data_size; /* Valid only when initialized */
GUtilData serial; /* Valid only when initialized */
GUtilData serial; /* Same as UID (nfcid1) */
};

GType nfc_tag_t2_get_type();
Expand Down
17 changes: 7 additions & 10 deletions core/src/nfc_tag_t2.c
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2018-2020 Jolla Ltd.
* Copyright (C) 2018-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2020 Open Mobile Platform LLC.
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -114,7 +115,6 @@ struct nfc_tag_t2_priv {
GHashTable* reads;
GHashTable* writes;
GByteArray* cached_blocks;
guint8 serial[4];
guint sector_count;
NfcTagType2Sector* sectors;
guint init_id;
Expand Down Expand Up @@ -902,22 +902,17 @@ nfc_tag_t2_control_area_read_resp(
priv->init_id = 0;
if (status == NFC_TRANSMIT_STATUS_OK && len == 16) {
const guint8* bytes = data;
const guint8* serial = bytes + 4;
const guint8* cc = bytes + 12;

/*
* Layout of the first 4 blocks accorting to NFCForum-TS-Type-2-Tag:
*
* Bytes 0..3 - UID / Internal
* Bytes 4..7 - Serial Number
* Bytes 8..11 - Internal / Lock
* Bytes 0..9 - UID / Internal
* Bytes 10..11 - Lock
* Bytes 12..15 - Capability Container (CC)
*/
memcpy(priv->serial, serial, 4);
self->serial.bytes = priv->serial;
self->serial.size = 4;
GDEBUG("Serial: %02x %02x %02x %02x", priv->serial[0],
priv->serial[1], priv->serial[2], priv->serial[3]);
GDEBUG("Internal data:");
nfc_hexdump(bytes, 10);

if (cc[0] == NFC_TAG_T2_CC_NFC_FORUM_MAGIC &&
cc[1] >= NFC_TAG_T2_CC_MIN_VERSION) {
Expand Down Expand Up @@ -970,6 +965,8 @@ nfc_tag_t2_init2(
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;
/* serial is same as nfcid1 */
self->serial = nfc_tag_param(tag)->a.nfcid1;
self->sel_res = poll_a->sel_res;
} else {
nfc_tag_init_base(tag, target, NULL);
Expand Down
29 changes: 7 additions & 22 deletions unit/core_tag_t2/test_core_tag_t2.c
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2018-2020 Jolla Ltd.
* Copyright (C) 2018-2020 Slava Monich <slava.monich@jolla.com>
* Copyright (C) 2020 Open Mobile Platform LLC.
*
* You may use this file under the terms of BSD license as follows:
*
Expand Down Expand Up @@ -233,7 +234,7 @@ static const guint8 test_data_jolla[] = { /* "https://www.jolla.com" */
};

/*
* Serial: 9a 85 5c 80
* UID: 04 ea 3d 9a 85 5c 80
* Data size: 872 bytes
*/
static const guint8 test_data_ntag216[] = { /* "https://www.merproject.org" */
Expand Down Expand Up @@ -684,11 +685,8 @@ test_basic_exit(
void* user_data)
{
NfcTagType2* t2 = NFC_TAG_T2(tag);
GUtilData serial;

serial.bytes = test_data_empty + 4;
serial.size = 4;
g_assert(gutil_data_equal(&t2->serial, &serial));
g_assert(gutil_data_equal(&t2->serial, &t2->nfcid1));
g_main_loop_quit((GMainLoop*)user_data);
}

Expand Down Expand Up @@ -747,15 +745,10 @@ test_unsup_done(
void* user_data)
{
NfcTagType2* t2 = NFC_TAG_T2(tag);
TestTarget* test = TEST_TARGET(tag->target);
GUtilData serial;

g_assert(tag->flags & NFC_TAG_FLAG_INITIALIZED);

/* Serial should be there */
serial.bytes = test->storage + 4;
serial.size = 4;
g_assert(gutil_data_equal(&t2->serial, &serial));
g_assert(gutil_data_equal(&t2->serial, &t2->nfcid1));

/* But no NDEF and no size */
g_assert(!tag->ndef);
Expand Down Expand Up @@ -796,7 +789,7 @@ test_init_err1_done(
NfcTagType2* t2 = NFC_TAG_T2(tag);

g_assert(tag->flags & NFC_TAG_FLAG_INITIALIZED);
g_assert(!t2->serial.size);
g_assert(gutil_data_equal(&t2->serial, &t2->nfcid1));
g_assert(!t2->data_size);
g_assert(!tag->ndef);
g_main_loop_quit((GMainLoop*)user_data);
Expand Down Expand Up @@ -838,16 +831,11 @@ test_init_err2_done(
void* user_data)
{
NfcTagType2* t2 = NFC_TAG_T2(tag);
TestTarget* test = TEST_TARGET(tag->target);
GUtilData serial;

g_assert(tag->flags & NFC_TAG_FLAG_INITIALIZED);
g_assert(t2->data_size);

/* Serial should be there */
serial.bytes = test->storage + 4;
serial.size = 4;
g_assert(gutil_data_equal(&t2->serial, &serial));
g_assert(gutil_data_equal(&t2->serial, &t2->nfcid1));

/* But no NDEF */
g_assert(!tag->ndef);
Expand Down Expand Up @@ -909,12 +897,9 @@ test_read_data_start(
TestTarget* test = TEST_TARGET(tag->target);
NfcTagType2* t2 = NFC_TAG_T2(tag);
NfcNdefRec* rec = tag->ndef;
GUtilData serial;
guint8* buf;

serial.bytes = test_data_google + 4;
serial.size = 4;
g_assert(gutil_data_equal(&t2->serial, &serial));
g_assert(gutil_data_equal(&t2->serial, &t2->nfcid1));
g_assert(t2->data_size == test->data.size - TEST_DATA_OFFSET);

g_assert(rec);
Expand Down

0 comments on commit ae1ea87

Please sign in to comment.