#include <stdio.h>
#include <stdlib.h>
+#include <unistd.h>
#include <errno.h>
#include <glib.h>
#include <gdbus.h>
-#include <linux/nfc.h>
-
#include "near.h"
static DBusConnection *connection = NULL;
GList *target_list;
struct near_target *active_target;
+ int active_sock;
};
+/* HACK HACK */
+#define AF_NFC 39
+
static void free_adapter(gpointer data)
{
struct near_adapter *adapter = data;
DBG("protocols 0x%x", adapter->protocols);
- if (adapter->protocols & NFC_PROTO_FELICA) {
+ if (adapter->protocols & NFC_PROTO_FELICA_MASK) {
str = "Felica";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (adapter->protocols & NFC_PROTO_MIFARE) {
+ if (adapter->protocols & NFC_PROTO_MIFARE_MASK) {
str = "MIFARE";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (adapter->protocols & NFC_PROTO_JEWEL) {
+ if (adapter->protocols & NFC_PROTO_JEWEL_MASK) {
str = "Jewel";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (adapter->protocols & NFC_PROTO_ISO14443_4) {
+ if (adapter->protocols & NFC_PROTO_ISO14443_MASK) {
str = "ISO-DEP";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (adapter->protocols & NFC_PROTO_NFC_DEP) {
+ if (adapter->protocols & NFC_PROTO_NFC_DEP_MASK) {
str = "NFC-DEP";
dbus_message_iter_append_basic(iter,
adapter->idx = idx;
adapter->protocols = protocols;
adapter->powered = TRUE;
+ adapter->active_target = NULL;
+ adapter->active_sock = -1;
adapter->path = g_strdup_printf("%s/nfc%d", NFC_PATH, idx);
return -ENODEV;
if (adapter->active_target == target) {
- __near_netlink_deactivate_target(idx, target_idx);
+ if (adapter->active_sock != -1)
+ close(adapter->active_sock);
adapter->active_target = NULL;
}
struct near_adapter *adapter;
struct near_target *target;
uint32_t protocols;
- int err;
+ struct sockaddr_nfc addr;
+ int err, sock;
DBG("idx %d", idx);
protocols = __near_target_get_protocols(target);
- err = __near_netlink_activate_target(idx, target_idx, protocols);
- if (err == 0)
- adapter->active_target = target;
+ sock = socket(AF_NFC, SOCK_SEQPACKET, protocols);
+ if (sock == -1)
+ return sock;
+
+ addr.sa_family = AF_NFC;
+ addr.dev_idx = idx;
+ addr.target_idx = target_idx;
+
+ err = connect(sock, (struct sockaddr *) &addr, sizeof(addr));
+ if (err) {
+ close(sock);
+ return err;
+ }
+
+ adapter->active_target = target;
+ adapter->active_sock = sock;
- return err;
+ return 0;
}
-int near_adapter_disconnect(uint32_t idx, uint32_t target_idx)
+int near_adapter_disconnect(uint32_t idx)
{
struct near_adapter *adapter;
- struct near_target *target;
- int err;
DBG("idx %d", idx);
if (adapter == NULL)
return -ENODEV;
- target = find_target(adapter, target_idx);
- if (target == NULL || adapter->active_target == NULL)
+ if (adapter->active_sock == -1)
return -ENOLINK;
- err = __near_netlink_deactivate_target(idx, target_idx);
- if (err == 0)
- adapter->active_target = NULL;
+ close(adapter->active_sock);
+ adapter->active_sock = -1;
- return err;
+ return 0;
}
int __near_adapter_init(void)
#include <netlink/genl/family.h>
#include <netlink/genl/ctrl.h>
-#include <linux/nfc.h>
-
#include "near.h"
struct nlnfc_state {
return err;
}
-int __near_netlink_activate_target(uint32_t adapter_idx,
- uint32_t target_idx,
- uint32_t protocol)
-{
- struct nl_msg *msg;
- void *hdr;
- int family, err = 0;
-
- DBG("");
-
- msg = nlmsg_alloc();
- if (msg == NULL)
- return -ENOMEM;
-
- family = genl_family_get_id(nfc_state->nlnfc);
-
- hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
- NLM_F_REQUEST, NFC_CMD_ACTIVATE_TARGET,
- NFC_GENL_VERSION);
- if (hdr == NULL) {
- err = -EINVAL;
- goto nla_put_failure;
- }
-
- NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, adapter_idx);
- NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
- NLA_PUT_U32(msg, NFC_ATTR_PROTOCOLS, protocol);
-
- err = nl_send_msg(nfc_state->nl_sock, msg, NULL, NULL);
-
-nla_put_failure:
- nlmsg_free(msg);
-
- return err;
-}
-
-int __near_netlink_deactivate_target(uint32_t adapter_idx,
- uint32_t target_idx)
-{
- struct nl_msg *msg;
- void *hdr;
- int family, err = 0;
-
- DBG("");
-
- msg = nlmsg_alloc();
- if (msg == NULL)
- return -ENOMEM;
-
- family = genl_family_get_id(nfc_state->nlnfc);
-
- hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0,
- NLM_F_REQUEST, NFC_CMD_DEACTIVATE_TARGET,
- NFC_GENL_VERSION);
- if (hdr == NULL) {
- err = -EINVAL;
- goto nla_put_failure;
- }
-
- NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, adapter_idx);
- NLA_PUT_U32(msg, NFC_ATTR_TARGET_INDEX, target_idx);
-
- err = nl_send_msg(nfc_state->nl_sock, msg, NULL, NULL);
-
-nla_put_failure:
- nlmsg_free(msg);
-
- return err;
-}
-
static int no_seq_check(struct nl_msg *n, void *arg)
{
DBG("");
#include <gdbus.h>
-#include <linux/nfc.h>
-
#include "near.h"
struct near_target {
DBG("protocols 0x%x", target->protocols);
- if (target->protocols & NFC_PROTO_FELICA) {
+ if (target->protocols & NFC_PROTO_FELICA_MASK) {
str = "Felica";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (target->protocols & NFC_PROTO_MIFARE) {
+ if (target->protocols & NFC_PROTO_MIFARE_MASK) {
str = "MIFARE";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (target->protocols & NFC_PROTO_JEWEL) {
+ if (target->protocols & NFC_PROTO_JEWEL_MASK) {
str = "Jewel";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (target->protocols & NFC_PROTO_ISO14443_4) {
+ if (target->protocols & NFC_PROTO_ISO14443_MASK) {
str = "ISO-DEP";
dbus_message_iter_append_basic(iter,
DBUS_TYPE_STRING, &str);
}
- if (target->protocols & NFC_PROTO_NFC_DEP) {
+ if (target->protocols & NFC_PROTO_NFC_DEP_MASK) {
str = "NFC-DEP";
dbus_message_iter_append_basic(iter,
{ }
};
-#define NFC_TAG_A (NFC_PROTO_ISO14443_4 | NFC_PROTO_NFC_DEP | NFC_PROTO_MIFARE)
+#define NFC_TAG_A (NFC_PROTO_ISO14443_MASK | NFC_PROTO_NFC_DEP_MASK | \
+ NFC_PROTO_MIFARE_MASK)
#define NFC_TAG_A_TYPE1 0x00
#define NFC_TAG_A_TYPE4 0x01
#define NFC_TAG_A_NFC_DEP 0x02