Check NFC feature support through neard
[platform/core/connectivity/nfc-manager-neard.git] / daemon / net_nfc_server.c
index 6a79137..65b1e29 100644 (file)
@@ -22,6 +22,7 @@
 #include "net_nfc_gdbus.h"
 #include "net_nfc_server.h"
 #include "net_nfc_server_common.h"
+#include "net_nfc_server_phdc.h"
 #include "net_nfc_server_vconf.h"
 #include "net_nfc_server_manager.h"
 #include "net_nfc_server_util.h"
@@ -37,6 +38,8 @@
 #include "net_nfc_server_system_handler.h"
 #include "net_nfc_server_context.h"
 
+#include "neardal.h"
+
 static gboolean use_daemon = FALSE;
 static GMainLoop *loop = NULL;
 
@@ -51,9 +54,9 @@ GOptionEntry option_entries[] = {
 
 pid_t net_nfc_server_gdbus_get_pid(const char *name)
 {
+       GVariant *_ret;
        guint pid = 0;
        GError *error = NULL;
-       GVariant *_ret;
 
        _ret = g_dbus_connection_call_sync(connection,
                        "org.freedesktop.DBus",
@@ -66,7 +69,9 @@ pid_t net_nfc_server_gdbus_get_pid(const char *name)
                        -1,
                        NULL,
                        &error);
-       if (_ret != NULL) {
+
+       if (_ret != NULL)
+       {
                g_variant_get(_ret, "(u)", &pid);
                g_variant_unref(_ret);
        }
@@ -78,14 +83,14 @@ static void _name_owner_changed(GDBusProxy *proxy,
                const gchar *name, const gchar *old_owner,
                const gchar *new_owner, void *user_data)
 {
-       if (name == NULL || old_owner == NULL || new_owner == NULL) {
-               DEBUG_ERR_MSG("invalid parameter");
+       RET_IF(NULL == name);
+       RET_IF(NULL == old_owner);
+       RET_IF(NULL == new_owner);
 
-               return;
-       }
-
-       if (strlen(new_owner) == 0) {
-               if (net_nfc_server_gdbus_check_client_is_running(old_owner)) {
+       if (strlen(new_owner) == 0)
+       {
+               if (net_nfc_server_gdbus_check_client_is_running(old_owner))
+               {
                        /* unregister service */
                        net_nfc_server_llcp_unregister_services(old_owner);
 
@@ -96,28 +101,25 @@ static void _name_owner_changed(GDBusProxy *proxy,
 }
 
 static void _on_name_owner_changed(GDBusConnection *connection,
-               const gchar *sender_name, const gchar *object_path,
-               const gchar *interface_name, const gchar *signal_name,
-               GVariant *parameters, gpointer user_data)
+               const gchar *sender_name,
+               const gchar *object_path,
+               const gchar *interface_name,
+               const gchar *signal_name,
+               GVariant *parameters,
+               gpointer user_data)
 {
        gchar *name;
        gchar *old_owner;
        gchar *new_owner;
 
-       g_variant_get(parameters,
-                       "(sss)",
-                       &name,
-                       &old_owner,
-                       &new_owner);
+       g_variant_get(parameters, "(sss)", &name, &old_owner, &new_owner);
 
-       _name_owner_changed((GDBusProxy *)connection,
-                       name, old_owner, new_owner, user_data);
+       _name_owner_changed((GDBusProxy *)connection, name, old_owner, new_owner, user_data);
 }
 
 static void _subscribe_name_owner_changed_event()
 {
-       if (connection == NULL)
-               return;
+       RET_IF(NULL == connection);
 
        /* subscribe signal */
        subscribe_id = g_dbus_connection_signal_subscribe(connection,
@@ -133,13 +135,11 @@ static void _subscribe_name_owner_changed_event()
 
 static void _unsubscribe_name_owner_changed_event()
 {
-       if (connection == NULL)
-               return;
+       RET_IF(NULL == connection);
 
        /* subscribe signal */
-       if (subscribe_id > 0) {
+       if (subscribe_id > 0)
                g_dbus_connection_signal_unsubscribe(connection, subscribe_id);
-       }
 }
 
 static void net_nfc_server_gdbus_init(void)
@@ -150,9 +150,9 @@ static void net_nfc_server_gdbus_init(void)
                g_object_unref(connection);
 
        connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error);
-       if (connection == NULL)
+       if (NULL == connection)
        {
-               DEBUG_ERR_MSG("Can not get connection %s", error->message);
+               NFC_ERR("Can not get connection %s", error->message);
                g_error_free (error);
                return;
        }
@@ -161,67 +161,73 @@ static void net_nfc_server_gdbus_init(void)
 
        if (net_nfc_server_manager_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init manager");
+               NFC_ERR("Can not init manager");
                return;
        }
 
        if (net_nfc_server_tag_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init tag");
+               NFC_ERR("Can not init tag");
                return;
        }
 
        if (net_nfc_server_ndef_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init ndef");
+               NFC_ERR("Can not init ndef");
                return;
        }
 
        if (net_nfc_server_llcp_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init llcp");
+               NFC_ERR("Can not init llcp");
                return;
        }
 
        if (net_nfc_server_p2p_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init tag");
+               NFC_ERR("Can not init p2p");
                return;
        }
 
        if (net_nfc_server_transceive_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not initialize transceive");
+               NFC_ERR("Can not initialize transceive");
                return;
        }
 
        if (net_nfc_server_handover_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not initialize transceive");
+               NFC_ERR("Can not initialize handover");
                return;
        }
 
        if (net_nfc_server_se_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init Test");
+               NFC_ERR("Can not init se");
                return;
        }
 
        if (net_nfc_server_snep_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init controller thread");
+               NFC_ERR("Can not init snep");
                return;
        }
 
        if (net_nfc_server_system_handler_init(connection) == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init controller thread");
+               NFC_ERR("Can not init system handler");
+               return;
+       }
+
+       if (net_nfc_server_phdc_init(connection) == FALSE)
+       {
+               NFC_ERR("Can not init phdc");
                return;
        }
 
        if (net_nfc_server_controller_thread_init() == FALSE)
        {
-               DEBUG_ERR_MSG("Can not init controller thread");
+               NFC_ERR("Can not init controller thread");
                return;
        }
 
@@ -241,7 +247,7 @@ static void net_nfc_server_gdbus_deinit(void)
        net_nfc_server_se_deinit();
        net_nfc_server_snep_deinit();
        net_nfc_server_system_handler_deinit();
-
+       net_nfc_server_phdc_deinit();
        net_nfc_server_gdbus_deinit_client_context();
 
        net_nfc_server_controller_thread_deinit();
@@ -255,7 +261,7 @@ static void net_nfc_server_gdbus_deinit(void)
 
 void net_nfc_manager_quit()
 {
-       DEBUG_MSG("net_nfc_manager_quit kill the nfc-manager daemon!!");
+       NFC_DBG("net_nfc_manager_quit kill the nfc-manager daemon!!");
 
        if (loop != NULL)
                g_main_loop_quit(loop);
@@ -266,7 +272,7 @@ static void on_bus_acquired(GDBusConnection *connection, const gchar *path,
 {
        gint state;
 
-       DEBUG_MSG("bus path : %s", path);
+       NFC_DBG("bus path : %s", path);
 
        net_nfc_server_gdbus_init();
 
@@ -274,7 +280,7 @@ static void on_bus_acquired(GDBusConnection *connection, const gchar *path,
 
        if (vconf_get_bool(VCONFKEY_NFC_STATE, &state) != 0)
        {
-               DEBUG_MSG("VCONFKEY_NFC_STATE is not exist");
+               NFC_DBG("VCONFKEY_NFC_STATE is not exist");
                net_nfc_manager_quit();
 
                return;
@@ -293,26 +299,43 @@ static void on_bus_acquired(GDBusConnection *connection, const gchar *path,
 static void on_name_acquired(GDBusConnection *connection, const gchar *name,
                gpointer user_data)
 {
-       DEBUG_SERVER_MSG("name : %s", name);
+       NFC_INFO("name : %s", name);
 }
 
 static void on_name_lost(GDBusConnection *connnection, const gchar *name,
                gpointer user_data)
 {
-       DEBUG_SERVER_MSG("name : %s", name);
+       NFC_INFO("name : %s", name);
 
        net_nfc_manager_quit();
 }
 
+static bool net_nfc_neard_nfc_support(void)
+{
+       char **adapters = NULL;
+       int len;
+       errorCode_t err;
+
+       NFC_INFO("checking nfc support");
+       err = neardal_get_adapters(&adapters, &len);
+       if (err != NEARDAL_SUCCESS)
+               return false;
+
+       if (!(len > 0 && adapters != NULL))
+               return false;
+
+       neardal_free_array(&adapters);
+       adapters = NULL;
+       neardal_destroy();
+
+       return true;
+}
 
 int main(int argc, char *argv[])
 {
-
-       void *handle = NULL;
-       guint id = 0;
+       GError *error = NULL;
        gboolean use_daemon = FALSE;
        GOptionContext *option_context;
-       GError *error = NULL;
 
        net_nfc_change_log_tag();
 
@@ -321,62 +344,43 @@ int main(int argc, char *argv[])
 
        if (g_option_context_parse(option_context, &argc, &argv, &error) == FALSE)
        {
-               DEBUG_ERR_MSG("can not parse option: %s", error->message);
+               NFC_ERR("can not parse option: %s", error->message);
                g_error_free(error);
 
                g_option_context_free(option_context);
                return 0;
        }
 
-       DEBUG_SERVER_MSG("start nfc manager");
-       DEBUG_SERVER_MSG("use_daemon : %d", use_daemon);
-
-       net_nfc_manager_init_log();
+       NFC_DBG("start nfc manager");
+       NFC_INFO("use_daemon : %d", use_daemon);
 
        net_nfc_app_util_clean_storage(MESSAGE_STORAGE);
 
-       handle = net_nfc_controller_onload();
-       if (handle == NULL)
+       if (net_nfc_neard_nfc_support() == false)
        {
-               DEBUG_ERR_MSG("load plugin library is failed");
+               NFC_ERR("failed to detect NFC devices");
 
                if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_OFF) != 0)
-                       DEBUG_ERR_MSG("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_OFF);
+                       NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_OFF);
 
                if (vconf_set_bool(VCONFKEY_NFC_STATE, 0) != 0)
-                       DEBUG_ERR_MSG("VCONFKEY_NFC_STATE set to %d failed", 0);
+                       NFC_ERR("VCONFKEY_NFC_STATE set to %d failed", 0);
 
                goto EXIT;
        }
 
        if (vconf_set_bool(VCONFKEY_NFC_FEATURE, VCONFKEY_NFC_FEATURE_ON) != 0)
-               DEBUG_ERR_MSG("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_ON);
-
-       id = g_bus_own_name(G_BUS_TYPE_SYSTEM,
-                       "org.tizen.NetNfcService",
-                       G_BUS_NAME_OWNER_FLAGS_NONE,
-                       on_bus_acquired,
-                       on_name_acquired,
-                       on_name_lost,
-                       NULL,
-                       NULL);
+               NFC_ERR("VCONFKEY_NFC_FEATURE set to %d failed", VCONFKEY_NFC_FEATURE_ON);
+
+       net_nfc_server_vconf_init();
 
        loop = g_main_loop_new(NULL, FALSE);
        g_main_loop_run(loop);
 
 EXIT :
        net_nfc_server_vconf_deinit();
-       net_nfc_server_controller_deinit();
-       net_nfc_server_gdbus_deinit();
-
-       if (id)
-               g_bus_unown_name(id);
-
-       net_nfc_controller_unload(handle);
-
-       net_nfc_manager_fini_log();
 
        g_option_context_free(option_context);
 
        return 0;
-}
\ No newline at end of file
+}