/
nfc_initiator.c
559 lines (494 loc) · 15.2 KB
/
nfc_initiator.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
/*
* Copyright (C) 2020-2023 Slava Monich <slava@monich.com>
* Copyright (C) 2020-2021 Jolla Ltd.
*
* You may use this file under the terms of the 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 ``AS IS'' AND ANY EXPRESSED 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 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) ARISING
* IN ANY WAY OUT OF THE USE OR INABILITY TO USE THIS SOFTWARE, EVEN
* IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* The views and conclusions contained in the software and documentation
* are those of the authors and should not be interpreted as representing
* any official policies, either expressed or implied.
*/
#include "nfc_initiator_p.h"
#include "nfc_initiator_impl.h"
#include "nfc_log.h"
#include <gutil_macros.h>
#include <gutil_misc.h>
struct nfc_initiator_priv {
NfcTransmission* current; /* Pointer */
NfcTransmission* next; /* Reference */
GBytes* next_data;
gboolean deactivated;
};
#define THIS(obj) NFC_INITIATOR(obj)
#define THIS_TYPE NFC_TYPE_INITIATOR
#define PARENT_TYPE G_TYPE_OBJECT
#define PARENT_CLASS nfc_initiator_parent_class
#define GET_THIS_CLASS(obj) G_TYPE_INSTANCE_GET_CLASS(obj, THIS_TYPE, \
NfcInitiatorClass)
G_DEFINE_ABSTRACT_TYPE(NfcInitiator, nfc_initiator, PARENT_TYPE)
enum nfc_initiator_signal {
SIGNAL_TRANSMISSION,
SIGNAL_GONE,
SIGNAL_COUNT
};
#define SIGNAL_TRANSMISSION_NAME "nfc-initiator-transmission"
#define SIGNAL_GONE_NAME "nfc-initiator-gone"
static guint nfc_initiator_signals[SIGNAL_COUNT] = { 0 };
struct nfc_transmission {
NfcInitiator* owner;
NfcTransmissionDoneFunc done;
void* user_data;
gboolean responded;
gint ref_count;
};
/*==========================================================================*
* Implementation
*==========================================================================*/
static
inline
gboolean
nfc_initiator_can_deactivate(
NfcInitiator* self)
{
return self->present && !self->priv->deactivated;
}
static
void
nfc_initiator_do_deactivate(
NfcInitiator* self)
{
/* Caller has checked nfc_initiator_can_deactivate() */
self->priv->deactivated = TRUE;
GET_THIS_CLASS(self)->deactivate(self);
}
static
void
nfc_initiator_drop_transactions(
NfcInitiatorPriv* priv)
{
if (priv->next_data) {
g_bytes_unref(priv->next_data);
priv->next_data = NULL;
}
if (priv->next) {
priv->next->owner = NULL;
/* This is a reference */
nfc_transmission_unref(priv->next);
priv->next = NULL;
}
if (priv->current) {
NfcTransmission* current = priv->current;
/* And this is just a pointer */
priv->current = NULL;
current->owner = NULL;
if (current->responded && current->done) {
NfcTransmissionDoneFunc done = current->done;
/* Make sure completion callback is not invoked twice */
current->done = NULL;
nfc_transmission_ref(current);
done(current, FALSE, current->user_data);
nfc_transmission_unref(current);
}
}
}
/*==========================================================================*
* Transmission API
*==========================================================================*/
static
NfcTransmission*
nfc_transmission_new(
NfcInitiator* initiator)
{
NfcTransmission* self = g_slice_new0(NfcTransmission);
self->owner = initiator;
g_atomic_int_set(&self->ref_count, 1);
return self;
}
static
void
nfc_transmission_free(
NfcTransmission* self)
{
NfcInitiator* owner = self->owner;
if (owner) {
NfcInitiatorPriv* priv = owner->priv;
/*
* Clear the pointer. Note that priv->next is an internal reference
* meaning that NfcTransmission pointed to by priv->next can't get
* here before pointer is cleared and reference is released.
* Therefore, there's no need to check priv->next.
*/
if (priv->current == self) {
priv->current = NULL;
if (!self->responded && nfc_initiator_can_deactivate(owner)) {
/* Transmission was dropped without responding */
GDEBUG("Transmission dropped, deactivating");
nfc_initiator_do_deactivate(owner);
}
}
}
gutil_slice_free(self);
}
NfcTransmission*
nfc_transmission_ref(
NfcTransmission* self)
{
if (G_LIKELY(self)) {
g_atomic_int_inc(&self->ref_count);
}
return self;
}
void
nfc_transmission_unref(
NfcTransmission* self)
{
if (G_LIKELY(self)) {
if (g_atomic_int_dec_and_test(&self->ref_count)) {
nfc_transmission_free(self);
}
}
}
gboolean
nfc_transmission_respond(
NfcTransmission* self,
const void* data,
guint len,
NfcTransmissionDoneFunc done,
void* user_data)
{
if (G_LIKELY(self && !self->responded)) {
NfcInitiator* owner = self->owner;
self->responded = TRUE;
if (owner) {
self->done = done;
self->user_data = user_data;
nfc_transmission_ref(self);
if (GET_THIS_CLASS(owner)->respond(owner, data, len)) {
nfc_transmission_unref(self);
return TRUE;
}
self->done = NULL;
nfc_transmission_unref(self);
}
}
return FALSE;
}
gboolean
nfc_transmission_respond_bytes(
NfcTransmission* self,
GBytes* data,
NfcTransmissionDoneFunc done,
void* user_data)
{
if (G_LIKELY(self && !self->responded)) {
NfcInitiator* owner = self->owner;
self->responded = TRUE;
if (owner) {
self->done = done;
self->user_data = user_data;
nfc_transmission_ref(self);
if (GET_THIS_CLASS(owner)->respond_bytes(owner, data)) {
nfc_transmission_unref(self);
return TRUE;
}
self->done = NULL;
nfc_transmission_unref(self);
}
}
return FALSE;
}
/*==========================================================================*
* Interface
*==========================================================================*/
NfcInitiator*
nfc_initiator_ref(
NfcInitiator* self)
{
if (G_LIKELY(self)) {
g_object_ref(THIS(self));
}
return self;
}
void
nfc_initiator_unref(
NfcInitiator* self)
{
if (G_LIKELY(self)) {
g_object_unref(THIS(self));
}
}
/*==========================================================================*
* Internal interface
*==========================================================================*/
void
nfc_initiator_deactivate(
NfcInitiator* self)
{
if (G_LIKELY(self) && nfc_initiator_can_deactivate(self)) {
nfc_initiator_do_deactivate(self);
}
}
gulong
nfc_initiator_add_transmission_handler(
NfcInitiator* self,
NfcTransmissionHandlerFunc func,
void* user_data)
{
return (G_LIKELY(self) && G_LIKELY(func)) ? g_signal_connect(self,
SIGNAL_TRANSMISSION_NAME, G_CALLBACK(func), user_data) : 0;
}
gulong
nfc_initiator_add_gone_handler(
NfcInitiator* self,
NfcInitiatorFunc func,
void* user_data)
{
return (G_LIKELY(self) && G_LIKELY(func)) ? g_signal_connect(self,
SIGNAL_GONE_NAME, G_CALLBACK(func), user_data) : 0;
}
void
nfc_initiator_remove_handler(
NfcInitiator* self,
gulong id)
{
if (G_LIKELY(self) && G_LIKELY(id)) {
g_signal_handler_disconnect(self, id);
}
}
void
nfc_initiator_remove_handlers(
NfcInitiator* self,
gulong* ids,
guint count)
{
gutil_disconnect_handlers(self, ids, count);
}
/*==========================================================================*
* Internal interface
*==========================================================================*/
static
void
nfc_initiator_handle_transmission(
NfcInitiator* self,
NfcTransmission* tx,
const GUtilData* data)
{
gboolean handled = FALSE;
g_signal_emit(self, nfc_initiator_signals[SIGNAL_TRANSMISSION], 0, tx,
data, &handled);
if (!handled && nfc_initiator_can_deactivate(self)) {
/* Signal wasn't handled, drop the link */
GDEBUG("Incoming transmission not handled, deactivating");
nfc_initiator_do_deactivate(self);
}
}
void
nfc_initiator_transmit(
NfcInitiator* self,
const void* bytes,
guint size)
{
if (G_LIKELY(self)) {
NfcInitiatorPriv* priv = self->priv;
if (priv->current) {
/*
* This can only legitimately happen if we have already responded
* to the current transmission, but haven't yet got a confirmation
* that our response has been sent.
*/
if (!priv->current->responded || priv->next) {
/*
* Stray transmission, should we ignore it? Let's see if
* that ever happens in real life. For now, treat it as an
* unrecoverable error and deactivate the RF interface.
*/
if (nfc_initiator_can_deactivate(self)) {
GDEBUG("Unexpected transmission, deactivating");
nfc_initiator_do_deactivate(self);
}
} else {
/*
* Let's queue it until the current response has been sent.
* There may be some room for optimization here (we could
* start processing the next transmission before the previous
* one has fully completed).
*/
priv->next = nfc_transmission_new(self);
priv->next_data = g_bytes_new(bytes, size); /* Copy the data */
}
} else {
GUtilData data;
NfcTransmission* tx = nfc_transmission_new(self);
/* Fresh new transmission coming in, notify the handler */
priv->current = tx;
data.bytes = bytes;
data.size = size;
nfc_initiator_handle_transmission(self, priv->current, &data);
/*
* If the handler doesn't reference it, this will deallocate
* the transmission and wipe the priv->current pointer. On the
* other hand, priv->current can already be NULL if we have
* been deactivated by now.
*/
nfc_transmission_unref(tx);
}
}
}
void
nfc_initiator_response_sent(
NfcInitiator* self,
NFC_TRANSMIT_STATUS status)
{
if (G_LIKELY(self)) {
NfcInitiatorPriv* priv = self->priv;
NfcTransmission* t = priv->current;
NfcTransmission* next = priv->next; /* This was a reference */
GBytes* bytes = priv->next_data;
/* The next transmission (if any) becomes the current one */
priv->current = next;
priv->next_data= NULL;
priv->next = NULL;
if (t && t->done) {
NfcTransmissionDoneFunc done = t->done;
/* Make sure completion callback is not invoked twice */
t->done = NULL;
/* Let the handler know that response has been sent */
nfc_transmission_ref(t);
done(t, status == NFC_TRANSMIT_STATUS_OK, t->user_data);
nfc_transmission_unref(t);
}
if (next) {
GUtilData data;
data.bytes = g_bytes_get_data(bytes, &data.size);
nfc_initiator_handle_transmission(self, next, &data);
nfc_transmission_unref(next); /* Release our reference */
}
if (bytes) {
g_bytes_unref(bytes);
}
}
}
void
nfc_initiator_gone(
NfcInitiator* self)
{
if (G_LIKELY(self) && self->present) {
self->present = FALSE;
nfc_initiator_ref(self);
nfc_initiator_drop_transactions(self->priv);
GET_THIS_CLASS(self)->gone(self);
nfc_initiator_unref(self);
}
}
/*==========================================================================*
* Methods
*==========================================================================*/
static
gboolean
nfc_initiator_default_respond(
NfcInitiator* self,
const void* data,
guint len)
{
return FALSE;
}
static
gboolean
nfc_initiator_default_respond_bytes(
NfcInitiator* self,
GBytes* bytes)
{
if (bytes) {
gsize len = 0;
const void* data = g_bytes_get_data(bytes, &len);
return GET_THIS_CLASS(self)->respond(self, data, (guint) len);
} else {
return GET_THIS_CLASS(self)->respond(self, NULL, 0);
}
}
static
void
nfc_initiator_nop(
NfcInitiator* self)
{
}
static
void
nfc_initiator_default_gone(
NfcInitiator* self)
{
g_signal_emit(self, nfc_initiator_signals[SIGNAL_GONE], 0);
}
/*==========================================================================*
* Internals
*==========================================================================*/
static
void
nfc_initiator_init(
NfcInitiator* self)
{
NfcInitiatorPriv* priv = G_TYPE_INSTANCE_GET_PRIVATE(self, THIS_TYPE,
NfcInitiatorPriv);
/* When initiator is created, it must be present, right? */
self->present = TRUE;
self->priv = priv;
}
static
void
nfc_initiator_finalize(
GObject* object)
{
nfc_initiator_drop_transactions(THIS(object)->priv);
G_OBJECT_CLASS(PARENT_CLASS)->finalize(object);
}
static
void
nfc_initiator_class_init(
NfcInitiatorClass* klass)
{
GType type = G_OBJECT_CLASS_TYPE(klass);
g_type_class_add_private(klass, sizeof(NfcInitiatorPriv));
klass->respond = nfc_initiator_default_respond;
klass->respond_bytes = nfc_initiator_default_respond_bytes;
klass->deactivate = nfc_initiator_nop;
klass->gone = nfc_initiator_default_gone;
G_OBJECT_CLASS(klass)->finalize = nfc_initiator_finalize;
nfc_initiator_signals[SIGNAL_TRANSMISSION] =
g_signal_new(SIGNAL_TRANSMISSION_NAME, type, G_SIGNAL_RUN_LAST, 0,
g_signal_accumulator_true_handled, NULL, NULL,
G_TYPE_BOOLEAN, 2, G_TYPE_POINTER, G_TYPE_POINTER);
nfc_initiator_signals[SIGNAL_GONE] =
g_signal_new(SIGNAL_GONE_NAME, type, G_SIGNAL_RUN_FIRST, 0,
NULL, NULL, NULL, G_TYPE_NONE, 0);
}
/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/