From 4b84df01d14fc8f4b10164833a3d25010acc9e18 Mon Sep 17 00:00:00 2001 From: Samuel Ortiz Date: Tue, 31 Jan 2012 15:06:41 +0100 Subject: [PATCH] adapter: Implement Powered property handling --- src/adapter.c | 30 ++++++++++++++++++++++++++++++ src/near.h | 1 + src/netlink.c | 38 ++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+) diff --git a/src/adapter.c b/src/adapter.c index 06ed957..fb1cdf3 100644 --- a/src/adapter.c +++ b/src/adapter.c @@ -236,8 +236,38 @@ static DBusMessage *get_properties(DBusConnection *conn, static DBusMessage *set_property(DBusConnection *conn, DBusMessage *msg, void *data) { + struct near_adapter *adapter = data; + DBusMessageIter iter, value; + const char *name; + int type, err; + DBG("conn %p", conn); + if (dbus_message_iter_init(msg, &iter) == FALSE) + return __near_error_invalid_arguments(msg); + + dbus_message_iter_get_basic(&iter, &name); + dbus_message_iter_next(&iter); + dbus_message_iter_recurse(&iter, &value); + + type = dbus_message_iter_get_arg_type(&value); + + if (g_str_equal(name, "Powered") == TRUE) { + near_bool_t powered; + + if (type != DBUS_TYPE_BOOLEAN) + return __near_error_invalid_arguments(msg); + + dbus_message_iter_get_basic(&value, &powered); + + err = __near_netlink_adapter_enable(adapter->idx, powered); + if (err < 0) + return __near_error_failed(msg, -err); + + adapter->powered = powered; + } else + return __near_error_invalid_property(msg); + return g_dbus_create_reply(msg, DBUS_TYPE_INVALID); } diff --git a/src/near.h b/src/near.h index 153091b..74e501d 100644 --- a/src/near.h +++ b/src/near.h @@ -134,6 +134,7 @@ int __near_netlink_stop_poll(int idx); int __near_netlink_dep_link_up(uint32_t idx, uint32_t target_idx, uint8_t comm_mode, uint8_t rf_mode); int __near_netlink_dep_link_down(uint32_t idx); +int __near_netlink_adapter_enable(int idx, near_bool_t enable); int __near_netlink_activate_target(uint32_t adapter_idx, uint32_t target_idx, uint32_t protocol); diff --git a/src/netlink.c b/src/netlink.c index 8ddd091..68777bd 100644 --- a/src/netlink.c +++ b/src/netlink.c @@ -347,6 +347,44 @@ nla_put_failure: return err; } +int __near_netlink_adapter_enable(int idx, near_bool_t enable) +{ + struct nl_msg *msg; + void *hdr; + int family, err = 0; + uint8_t cmd; + + DBG(""); + + msg = nlmsg_alloc(); + if (msg == NULL) + return -ENOMEM; + + family = genl_family_get_id(nfc_state->nlnfc); + + if (enable == TRUE) + cmd = NFC_CMD_DEV_UP; + else + cmd = NFC_CMD_DEV_DOWN; + + hdr = genlmsg_put(msg, NL_AUTO_PID, NL_AUTO_SEQ, family, 0, + NLM_F_REQUEST, cmd, NFC_GENL_VERSION); + if (hdr == NULL) { + err = -EINVAL; + goto nla_put_failure; + } + + NLA_PUT_U32(msg, NFC_ATTR_DEVICE_INDEX, 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(""); -- 2.7.4