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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
|
/*****************************************************************************
**
** Name: PeerToPeer.h
**
** Description: Communicate with a peer using NFC-DEP, LLCP, SNEP.
**
** Copyright (c) 2012, Broadcom Corp., All Rights Reserved.
** Proprietary and confidential.
**
*****************************************************************************/
#pragma once
#include "SyncEvent.h"
#include "NfcJniUtil.h"
#include <string>
extern "C"
{
#include "nfa_p2p_api.h"
#include "nfa_snep_api.h"
}
class P2pServer;
class P2pClient;
class NfaConn;
#define MAX_NFA_CONNS_PER_SERVER 5
/*****************************************************************************
**
** Name: PeerToPeer
**
** Description: Communicate with a peer using NFC-DEP, LLCP, SNEP.
**
*****************************************************************************/
class PeerToPeer
{
public:
typedef unsigned int tJNI_HANDLE;
/*******************************************************************************
**
** Function: PeerToPeer
**
** Description: Initialize member variables.
**
** Returns: None
**
*******************************************************************************/
PeerToPeer ();
/*******************************************************************************
**
** Function: ~PeerToPeer
**
** Description: Free all resources.
**
** Returns: None
**
*******************************************************************************/
~PeerToPeer ();
/*******************************************************************************
**
** Function: getInstance
**
** Description: Get the singleton PeerToPeer object.
**
** Returns: Singleton PeerToPeer object.
**
*******************************************************************************/
static PeerToPeer& getInstance();
/*******************************************************************************
**
** Function: initialize
**
** Description: Initialize member variables.
**
** Returns: None
**
*******************************************************************************/
void initialize ();
/*******************************************************************************
**
** Function: llcpActivatedHandler
**
** Description: Receive LLLCP-activated event from stack.
** nat: JVM-related data.
** activated: Event data.
**
** Returns: None
**
*******************************************************************************/
void llcpActivatedHandler (nfc_jni_native_data* nativeData, tNFA_LLCP_ACTIVATED& activated);
/*******************************************************************************
**
** Function: llcpDeactivatedHandler
**
** Description: Receive LLLCP-deactivated event from stack.
** nat: JVM-related data.
** deactivated: Event data.
**
** Returns: None
**
*******************************************************************************/
void llcpDeactivatedHandler (nfc_jni_native_data* nativeData, tNFA_LLCP_DEACTIVATED& deactivated);
/*******************************************************************************
**
** Function: connectionEventHandler
**
** Description: Receive events from the stack.
** event: Event code.
** eventData: Event data.
**
** Returns: None
**
*******************************************************************************/
void connectionEventHandler (UINT8 event, tNFA_CONN_EVT_DATA* eventData);
/*******************************************************************************
**
** Function: registerServer
**
** Description: Let a server start listening for peer's connection request.
** jniHandle: Connection handle.
** serviceName: Server's service name.
**
** Returns: True if ok.
**
*******************************************************************************/
bool registerServer (tJNI_HANDLE jniHandle, const char* serviceName);
/*******************************************************************************
**
** Function: deregisterServer
**
** Description: Stop a P2pServer from listening for peer.
**
** Returns: True if ok.
**
*******************************************************************************/
bool deregisterServer (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: accept
**
** Description: Accept a peer's request to connect.
** serverJniHandle: Server's handle.
** connJniHandle: Connection handle.
** maxInfoUnit: Maximum information unit.
** recvWindow: Receive window size.
**
** Returns: True if ok.
**
*******************************************************************************/
bool accept (tJNI_HANDLE serverJniHandle, tJNI_HANDLE connJniHandle, int maxInfoUnit, int recvWindow);
/*******************************************************************************
**
** Function: createClient
**
** Description: Create a P2pClient object for a new out-bound connection.
** jniHandle: Connection handle.
** miu: Maximum information unit.
** rw: Receive window size.
**
** Returns: True if ok.
**
*******************************************************************************/
bool createClient (tJNI_HANDLE jniHandle, UINT16 miu, UINT8 rw);
/*******************************************************************************
**
** Function: connectConnOriented
**
** Description: Estabish a connection-oriented connection to a peer.
** jniHandle: Connection handle.
** serviceName: Peer's service name.
**
** Returns: True if ok.
**
*******************************************************************************/
bool connectConnOriented (tJNI_HANDLE jniHandle, const char* serviceName);
/*******************************************************************************
**
** Function: connectConnOriented
**
** Description: Estabish a connection-oriented connection to a peer.
** jniHandle: Connection handle.
** destinationSap: Peer's service access point.
**
** Returns: True if ok.
**
*******************************************************************************/
bool connectConnOriented (tJNI_HANDLE jniHandle, UINT8 destinationSap);
/*******************************************************************************
**
** Function: send
**
** Description: Send data to peer.
** jniHandle: Handle of connection.
** buffer: Buffer of data.
** bufferLen: Length of data.
**
** Returns: True if ok.
**
*******************************************************************************/
bool send (tJNI_HANDLE jniHandle, UINT8* buffer, UINT16 bufferLen);
/*******************************************************************************
**
** Function: receive
**
** Description: Receive data from peer.
** jniHandle: Handle of connection.
** buffer: Buffer to store data.
** bufferLen: Max length of buffer.
** actualLen: Actual length received.
**
** Returns: True if ok.
**
*******************************************************************************/
bool receive (tJNI_HANDLE jniHandle, UINT8* buffer, UINT16 bufferLen, UINT16& actualLen);
/*******************************************************************************
**
** Function: disconnectConnOriented
**
** Description: Disconnect a connection-oriented connection with peer.
** jniHandle: Handle of connection.
**
** Returns: True if ok.
**
*******************************************************************************/
bool disconnectConnOriented (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: getRemoteMaxInfoUnit
**
** Description: Get peer's max information unit.
** jniHandle: Handle of the connection.
**
** Returns: Peer's max information unit.
**
*******************************************************************************/
UINT16 getRemoteMaxInfoUnit (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: getRemoteRecvWindow
**
** Description: Get peer's receive window size.
** jniHandle: Handle of the connection.
**
** Returns: Peer's receive window size.
**
*******************************************************************************/
UINT8 getRemoteRecvWindow (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: setP2pListenMask
**
** Description: Sets the p2p listen technology mask.
** p2pListenMask: the p2p listen mask to be set?
**
** Returns: None
**
*******************************************************************************/
void setP2pListenMask (tNFA_TECHNOLOGY_MASK p2pListenMask);
/*******************************************************************************
**
** Function: enableP2pListening
**
** Description: Start/stop polling/listening to peer that supports P2P.
** isEnable: Is enable polling/listening?
**
** Returns: None
**
*******************************************************************************/
void enableP2pListening (bool isEnable);
/*******************************************************************************
**
** Function: handleNfcOnOff
**
** Description: Handle events related to turning NFC on/off by the user.
** isOn: Is NFC turning on?
**
** Returns: None
**
*******************************************************************************/
void handleNfcOnOff (bool isOn);
/*******************************************************************************
**
** Function: getNextJniHandle
**
** Description: Get a new JNI handle.
**
** Returns: A new JNI handle.
**
*******************************************************************************/
tJNI_HANDLE getNewJniHandle ();
private:
static const int sMax = 10;
static PeerToPeer sP2p;
static const std::string sSnepServiceName;
static const std::string sNppServiceName;
UINT16 mRemoteWKS; // Peer's well known services
bool mIsP2pListening; // If P2P listening is enabled or not
tNFA_TECHNOLOGY_MASK mP2pListenTechMask; // P2P Listen mask
tJNI_HANDLE mNextJniHandle;
P2pServer *mServers [sMax];
P2pClient *mClients [sMax];
SyncEvent mSetTechEvent; // completion event for NFA_SetP2pListenTech()
SyncEvent mSnepDefaultServerStartStopEvent; // completion event for NFA_SnepStartDefaultServer(), NFA_SnepStopDefaultServer()
SyncEvent mSnepRegisterEvent; // completion event for NFA_SnepRegisterClient()
Mutex mDisconnectMutex; // synchronize the disconnect operation
Mutex mNewJniHandleMutex; // synchronize the creation of a new JNI handle
/*******************************************************************************
**
** Function: nfaServerCallback
**
** Description: Receive LLCP-related events from the stack.
** p2pEvent: Event code.
** eventData: Event data.
**
** Returns: None
**
*******************************************************************************/
static void nfaServerCallback (tNFA_P2P_EVT p2pEvent, tNFA_P2P_EVT_DATA *eventData);
/*******************************************************************************
**
** Function: nfaClientCallback
**
** Description: Receive LLCP-related events from the stack.
** p2pEvent: Event code.
** eventData: Event data.
**
** Returns: None
**
*******************************************************************************/
static void nfaClientCallback (tNFA_P2P_EVT p2pEvent, tNFA_P2P_EVT_DATA *eventData);
/*******************************************************************************
**
** Function: snepClientCallback
**
** Description: Receive SNEP-related events from the stack.
** snepEvent: Event code.
** eventData: Event data.
**
** Returns: None
**
*******************************************************************************/
static void snepClientCallback (tNFA_SNEP_EVT snepEvent, tNFA_SNEP_EVT_DATA *eventData);
/*******************************************************************************
**
** Function: ndefTypeCallback
**
** Description: Receive NDEF-related events from the stack.
** ndefEvent: Event code.
** eventData: Event data.
**
** Returns: None
**
*******************************************************************************/
static void ndefTypeCallback (tNFA_NDEF_EVT event, tNFA_NDEF_EVT_DATA *evetnData);
/*******************************************************************************
**
** Function: findServer
**
** Description: Find a PeerToPeer object by connection handle.
** nfaP2pServerHandle: Connectin handle.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
P2pServer *findServer (tNFA_HANDLE nfaP2pServerHandle);
/*******************************************************************************
**
** Function: findServer
**
** Description: Find a PeerToPeer object by connection handle.
** serviceName: service name.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
P2pServer *findServer (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: findServer
**
** Description: Find a PeerToPeer object by service name
** serviceName: service name.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
P2pServer *findServer (const char *serviceName);
/*******************************************************************************
**
** Function: removeServer
**
** Description: Free resources related to a server.
** jniHandle: Connection handle.
**
** Returns: None
**
*******************************************************************************/
void removeServer (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: removeConn
**
** Description: Free resources related to a connection.
** jniHandle: Connection handle.
**
** Returns: None
**
*******************************************************************************/
void removeConn (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: createDataLinkConn
**
** Description: Establish a connection-oriented connection to a peer.
** jniHandle: Connection handle.
** serviceName: Peer's service name.
** destinationSap: Peer's service access point.
**
** Returns: True if ok.
**
*******************************************************************************/
bool createDataLinkConn (tJNI_HANDLE jniHandle, const char* serviceName, UINT8 destinationSap);
/*******************************************************************************
**
** Function: findClient
**
** Description: Find a PeerToPeer object with a client connection handle.
** nfaConnHandle: Connection handle.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
P2pClient *findClient (tNFA_HANDLE nfaConnHandle);
/*******************************************************************************
**
** Function: findClient
**
** Description: Find a PeerToPeer object with a client connection handle.
** jniHandle: Connection handle.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
P2pClient *findClient (tJNI_HANDLE jniHandle);
/*******************************************************************************
**
** Function: findClientCon
**
** Description: Find a PeerToPeer object with a client connection handle.
** nfaConnHandle: Connection handle.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
P2pClient *findClientCon (tNFA_HANDLE nfaConnHandle);
/*******************************************************************************
**
** Function: findConnection
**
** Description: Find a PeerToPeer object with a connection handle.
** nfaConnHandle: Connection handle.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
NfaConn *findConnection (tNFA_HANDLE nfaConnHandle);
/*******************************************************************************
**
** Function: findConnection
**
** Description: Find a PeerToPeer object with a connection handle.
** jniHandle: Connection handle.
**
** Returns: PeerToPeer object.
**
*******************************************************************************/
NfaConn *findConnection (tJNI_HANDLE jniHandle);
};
/*****************************************************************************
**
** Name: NfaConn
**
** Description: Store information about a connection related to a peer.
**
*****************************************************************************/
class NfaConn
{
public:
tNFA_HANDLE mNfaConnHandle; // NFA handle of the P2P connection
PeerToPeer::tJNI_HANDLE mJniHandle; // JNI handle of the P2P connection
UINT16 mMaxInfoUnit;
UINT8 mRecvWindow;
UINT16 mRemoteMaxInfoUnit;
UINT8 mRemoteRecvWindow;
SyncEvent mReadEvent; // event for reading
SyncEvent mCongEvent; // event for congestion
SyncEvent mDisconnectingEvent; // event for disconnecting
/*******************************************************************************
**
** Function: NfaConn
**
** Description: Initialize member variables.
**
** Returns: None
**
*******************************************************************************/
NfaConn();
};
/*****************************************************************************
**
** Name: P2pServer
**
** Description: Store information about an in-bound connection from a peer.
**
*****************************************************************************/
class P2pServer
{
public:
tNFA_HANDLE mNfaP2pServerHandle; // NFA p2p handle of local server
PeerToPeer::tJNI_HANDLE mJniHandle; // JNI Handle
SyncEvent mRegServerEvent; // for NFA_P2pRegisterServer()
SyncEvent mConnRequestEvent; // for accept()
std::string mServiceName;
NfaConn *mServerConn[MAX_NFA_CONNS_PER_SERVER];
/*******************************************************************************
**
** Function: P2pServer
**
** Description: Initialize member variables.
**
** Returns: None
**
*******************************************************************************/
P2pServer ();
/*******************************************************************************
**
** Function: findServerConnection
**
** Description: Find a P2pServer that has the handle.
** nfaConnHandle: NFA connection handle.
**
** Returns: P2pServer object.
**
*******************************************************************************/
NfaConn *findServerConnection (tNFA_HANDLE nfaConnHandle);
};
/*****************************************************************************
**
** Name: P2pClient
**
** Description: Store information about an out-bound connection to a peer.
**
*****************************************************************************/
class P2pClient
{
public:
tNFA_HANDLE mNfaP2pClientHandle; // NFA p2p handle of client
bool mIsConnecting; // Set true while connecting
NfaConn mClientConn;
SyncEvent mRegisteringEvent; // For client registration
SyncEvent mConnectingEvent; // for NFA_P2pConnectByName or Sap()
SyncEvent mSnepEvent; // To wait for SNEP completion
/*******************************************************************************
**
** Function: P2pClient
**
** Description: Initialize member variables.
**
** Returns: None
**
*******************************************************************************/
P2pClient ();
/*******************************************************************************
**
** Function: ~P2pClient
**
** Description: Free all resources.
**
** Returns: None
**
*******************************************************************************/
~P2pClient ();
};
|