Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[nfcd] NFC-DEP support. JB#49576
  • Loading branch information
monich committed Jan 15, 2021
1 parent 1d4726e commit 12f2b0b
Show file tree
Hide file tree
Showing 102 changed files with 21,790 additions and 350 deletions.
16 changes: 15 additions & 1 deletion core/Makefile
Expand Up @@ -8,7 +8,7 @@
# Required packages
#

PKGS = libglibutil glib-2.0 gobject-2.0
PKGS = libglibutil glib-2.0 gobject-2.0 gio-2.0 gio-unix-2.0

#
# Default target
Expand All @@ -32,14 +32,28 @@ SRC = \
nfc_adapter.c \
nfc_crc.c \
nfc_core.c \
nfc_initiator.c \
nfc_llc.c \
nfc_llc_io.c \
nfc_llc_io_initiator.c \
nfc_llc_io_target.c \
nfc_llc_param.c \
nfc_locale.c \
nfc_manager.c \
nfc_ndef_rec.c \
nfc_ndef_rec_sp.c \
nfc_ndef_rec_u.c \
nfc_ndef_rec_t.c \
nfc_peer.c \
nfc_peer_connection.c \
nfc_peer_initiator.c \
nfc_peer_service.c \
nfc_peer_services.c \
nfc_peer_socket.c \
nfc_peer_target.c \
nfc_plugins.c \
nfc_plugin.c \
nfc_snep_server.c \
nfc_tag.c \
nfc_tag_t2.c \
nfc_tag_t4.c \
Expand Down
66 changes: 65 additions & 1 deletion core/include/nfc_adapter.h
Expand Up @@ -54,7 +54,7 @@ struct nfc_adapter {
NFC_MODE supported_modes;
NFC_MODE mode_requested;
NFC_MODE mode;
gboolean target_present;
gboolean target_present; /* Presence of anything, actually */
};

GType nfc_adapter_get_type(void) NFCD_EXPORT;
Expand All @@ -75,6 +75,13 @@ void
NfcTag* tag,
void* user_data);

typedef
void
(*NfcAdapterPeerFunc)(
NfcAdapter* adapter,
NfcPeer* peer,
void* user_data); /* Since 1.1.0 */

NfcAdapter*
nfc_adapter_ref(
NfcAdapter* adapter)
Expand All @@ -97,6 +104,11 @@ nfc_adapter_request_mode(
NFC_MODE mode)
NFCD_EXPORT;

NfcPeer**
nfc_adapter_peers(
NfcAdapter* adapter) /* Since 1.1.0 */
NFCD_EXPORT;

NfcTag*
nfc_adapter_add_tag_t2(
NfcAdapter* adapter,
Expand Down Expand Up @@ -140,6 +152,44 @@ nfc_adapter_remove_tag(
const char* name)
NFCD_EXPORT;

NfcPeer*
nfc_adapter_add_peer_initiator_a(
NfcAdapter* adapter,
NfcTarget* target,
const NfcParamPollA* tech_param,
const NfcParamNfcDepInitiator* nfc_dep_param) /* Since 1.1.0 */
NFCD_EXPORT;

NfcPeer*
nfc_adapter_add_peer_initiator_f(
NfcAdapter* adapter,
NfcTarget* target,
const NfcParamPollF* tech_param,
const NfcParamNfcDepInitiator* nfc_dep_param) /* Since 1.1.0 */
NFCD_EXPORT;

NfcPeer*
nfc_adapter_add_peer_target_a(
NfcAdapter* adapter,
NfcInitiator* initiator,
const NfcParamListenA* tech_param,
const NfcParamNfcDepTarget* nfc_dep_param) /* Since 1.1.0 */
NFCD_EXPORT;

NfcPeer*
nfc_adapter_add_peer_target_f(
NfcAdapter* adapter,
NfcInitiator* initiator,
const NfcParamListenF* tech_param,
const NfcParamNfcDepTarget* nfc_dep_param) /* Since 1.1.0 */
NFCD_EXPORT;

void
nfc_adapter_remove_peer(
NfcAdapter* adapter,
const char* name) /* Since 1.1.0 */
NFCD_EXPORT;

gulong
nfc_adapter_add_target_presence_handler(
NfcAdapter* adapter,
Expand All @@ -161,6 +211,20 @@ nfc_adapter_add_tag_removed_handler(
void* user_data)
NFCD_EXPORT;

gulong
nfc_adapter_add_peer_added_handler(
NfcAdapter* adapter,
NfcAdapterPeerFunc func,
void* user_data) /* Since 1.1.0 */
NFCD_EXPORT;

gulong
nfc_adapter_add_peer_removed_handler(
NfcAdapter* adapter,
NfcAdapterPeerFunc func,
void* user_data) /* Since 1.1.0 */
NFCD_EXPORT;

gulong
nfc_adapter_add_powered_changed_handler(
NfcAdapter* adapter,
Expand Down
80 changes: 80 additions & 0 deletions core/include/nfc_initiator.h
@@ -0,0 +1,80 @@
/*
* Copyright (C) 2020-2021 Jolla Ltd.
* Copyright (C) 2020-2021 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_INITIATOR_H
#define NFC_INITIATOR_H

#include "nfc_types.h"

#include <glib-object.h>

G_BEGIN_DECLS

/* Since 1.1.0 */

typedef struct nfc_initiator_priv NfcInitiatorPriv;

struct nfc_initiator {
GObject object;
NfcInitiatorPriv* priv;
NFC_TECHNOLOGY technology;
NFC_PROTOCOL protocol;
/* This one-way flag is set to FALSE when peer goes away. */
gboolean present;
};

GType nfc_initiator_get_type(void) NFCD_EXPORT;
#define NFC_TYPE_INITIATOR (nfc_initiator_get_type())
#define NFC_INITIATOR(obj) (G_TYPE_CHECK_INSTANCE_CAST((obj), \
NFC_TYPE_INITIATOR, NfcInitiator))

NfcInitiator*
nfc_initiator_ref(
NfcInitiator* initiator)
NFCD_EXPORT;

void
nfc_initiator_unref(
NfcInitiator* initiator)
NFCD_EXPORT;

G_END_DECLS

#endif /* NFC_INITIATOR_H */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/
131 changes: 131 additions & 0 deletions core/include/nfc_initiator_impl.h
@@ -0,0 +1,131 @@
/*
* Copyright (C) 2020-2021 Jolla Ltd.
* Copyright (C) 2020-2021 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_INITIATOR_IMPL_H
#define NFC_INITIATOR_IMPL_H

#include "nfc_initiator.h"

/* Internal API for use by NfcInitiator implemenations */

G_BEGIN_DECLS

/* Since 1.1.0 */

typedef struct nfc_initiator_class {
GObjectClass parent;

/* Base class makes sure that there are no overlapping responses.
* When transmission completes, nfc_initiator_response_sent() is called
* by the derived class. */
gboolean (*respond)(NfcInitiator* initiator, const void* data, guint len);

/* This should deactivate the initiator. When the initiator gets
* deactivated, subclass calls nfc_initiator_gone() to update the
* 'present' flag. */
void (*deactivate)(NfcInitiator* initiator);

/* These base implementation emits signal, must always be called. */
void (*gone)(NfcInitiator* initiator);

/* Padding for future expansion */
void (*_reserved1)(void);
void (*_reserved2)(void);
void (*_reserved3)(void);
void (*_reserved4)(void);
void (*_reserved5)(void);
void (*_reserved6)(void);
void (*_reserved7)(void);
void (*_reserved8)(void);
void (*_reserved9)(void);
void (*_reserved10)(void);
} NfcInitiatorClass;

#define NFC_INITIATOR_CLASS(klass) G_TYPE_CHECK_CLASS_CAST((klass), \
NFC_TYPE_INITIATOR, NfcInitiatorClass)

/*
* Normally it goes like this:
*
* 1. Data is coming in. Derived class calls nfc_initiator_transmit()
* 2. Base class issues a signal passing in NfcTransmission object
* 3. Transmission handler does whatever and calls nfc_transmission_respond()
* That translates into respond() call to the derived class.
* 4. Derived class calls nfc_initiator_response_sent() when response is sent.
* 5. At this point initiator is ready to receive a new portion of data.
*
* Now, if anything goes wrong... Basically, if anything goes wrong,
* RF interface is deactivated by invoking deactivate() callback of the
* derived class. Here is what can go wrong:
* a. No one responds to the signal at step 2.
* b. nfc_transmission_unref() is called before nfc_transmission_respond()
* in other words, transmission is received but dropped with no reply
* provided.
* c. nfc_initiator_response_sent() receives an error status at step 4.
*
* It's not quite clear what to do when the next portion of data arrives
* before we have sent a response to the previous one. Even though it
* shouldn't happen in real life, lower level APIs (e.g. NCI) often
* allow it. Currently it's being treated as an error (or was treated
* at the time of this writing). Let's see how it goes.
*/

void
nfc_initiator_transmit(
NfcInitiator* initiator,
const void* data,
guint len)
NFCD_EXPORT;

void
nfc_initiator_response_sent(
NfcInitiator* initiator,
NFC_TRANSMIT_STATUS status)
NFCD_EXPORT;

void
nfc_initiator_gone(
NfcInitiator* initiator)
NFCD_EXPORT;

G_END_DECLS

#endif /* NFC_INITIATOR_IMPL_H */

/*
* Local Variables:
* mode: C
* c-basic-offset: 4
* indent-tabs-mode: nil
* End:
*/

0 comments on commit 12f2b0b

Please sign in to comment.