From: saerome.kim Date: Fri, 12 Jan 2018 07:45:13 +0000 (+0900) Subject: mot-agent: change from d2d-subowner to mot-agent X-Git-Tag: submit/tizen/20190131.065036~232 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=8ee62d0c6d2f9eb1be17efbd7632f98cb62340e6;p=platform%2Fcore%2Fapi%2Fmulti-device-group.git mot-agent: change from d2d-subowner to mot-agent - remove d2d-manager directory - rename dbus name 'net.d2ds' -> 'net.ma' Signed-off-by: saerome.kim --- diff --git a/CMakeLists.txt b/CMakeLists.txt index 164be38..ffa6c3d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -40,8 +40,7 @@ ADD_DEFINITIONS("-D__WITH_DTLS__") ADD_DEFINITIONS("-DTCP_ADAPTER") MESSAGE(" - Start building src directories") -ADD_SUBDIRECTORY(src/d2d-manager) -ADD_SUBDIRECTORY(src/d2d-subowner) +ADD_SUBDIRECTORY(src/mot-agent) ADD_SUBDIRECTORY(src/companion-manager) ADD_SUBDIRECTORY(test) ADD_SUBDIRECTORY(capi) diff --git a/packaging/d2d-manager.spec b/packaging/d2d-manager.spec index 99df841..05e505b 100644 --- a/packaging/d2d-manager.spec +++ b/packaging/d2d-manager.spec @@ -5,14 +5,12 @@ Release: 0 Group: Network & Connectivity/Other License: Apache-2.0 Source0: %{name}-%{version}.tar.gz -Source2: d2dm.service Source3: %{name}.manifest Source4: %{name}-devel.manifest -Source5: net.d2dm.service Source6: fnm.service Source7: net.fnm.service -Source8: d2ds.service -Source9: net.d2ds.service +Source8: ma.service +Source9: net.ma.service Source10: capi-network-companion.manifest BuildRequires: cmake @@ -62,20 +60,16 @@ This package contains the development files for %{name} CAPI. %prep %setup -q chmod 644 %{SOURCE0} -chmod 644 %{SOURCE2} chmod 644 %{SOURCE3} chmod 644 %{SOURCE4} -chmod 644 %{SOURCE5} chmod 644 %{SOURCE6} chmod 644 %{SOURCE7} chmod 644 %{SOURCE8} chmod 644 %{SOURCE9} chmod 644 %{SOURCE10} -cp %{SOURCE2} . cp %{SOURCE3} . cp %{SOURCE4} . -cp %{SOURCE5} . cp %{SOURCE6} . cp %{SOURCE7} . cp %{SOURCE8} . @@ -103,36 +97,31 @@ rm -rf %{buildroot}/BUILD/%{name}* %make_install mkdir -p %{buildroot}%{_datadir}/dbus-1/system-services/ -cp %{SOURCE5} %{buildroot}%{_datadir}/dbus-1/system-services/net.d2dm.service -mkdir -p %{buildroot}%{_unitdir} -cp %{SOURCE2} %{buildroot}%{_unitdir}/d2dm.service cp %{SOURCE7} %{buildroot}%{_datadir}/dbus-1/system-services/net.fnm.service mkdir -p %{buildroot}%{_unitdir} cp %{SOURCE6} %{buildroot}%{_unitdir}/fnm.service -cp %{SOURCE9} %{buildroot}%{_datadir}/dbus-1/system-services/net.d2ds.service +cp %{SOURCE9} %{buildroot}%{_datadir}/dbus-1/system-services/net.ma.service mkdir -p %{buildroot}%{_unitdir} -cp %{SOURCE8} %{buildroot}%{_unitdir}/d2ds.service +cp %{SOURCE8} %{buildroot}%{_unitdir}/ma.service cp test/oic_svr_db_client.dat oic_svr_db_subowner_client.dat %postun -rm -rf %{NETWORK_FW_DATADIR}/oic_pdm_d2ds.db -rm -rf %{NETWORK_FW_DATADIR}/oic_pdm_d2ds.db-journal +rm -rf %{NETWORK_FW_DATADIR}/oic_pdm_ma.db +rm -rf %{NETWORK_FW_DATADIR}/oic_pdm_ma.db-journal %files %manifest %{name}.manifest %license LICENSE %defattr(-,root,root,-) -%{_bindir}/%{name} -%{_bindir}/d2d-subowner +%{_bindir}/mot-agent %{_bindir}/companion-manager %attr(644,root,root) %{_datadir}/dbus-1/system-services/* -%attr(644,root,root) %{_unitdir}/d2dm.service %attr(644,root,root) %{_unitdir}/fnm.service -%attr(644,root,root) %{_unitdir}/d2ds.service -%attr(644,network_fw,network_fw) %{NETWORK_FW_DATADIR}/oic_svr_db_d2ds.dat +%attr(644,root,root) %{_unitdir}/ma.service +%attr(644,network_fw,network_fw) %{NETWORK_FW_DATADIR}/oic_svr_db_ma.dat %attr(644,network_fw,network_fw) %{NETWORK_FW_DATADIR}/oic_svr_db_comp_manager.dat %files devel diff --git a/packaging/d2dm.service b/packaging/d2dm.service deleted file mode 100644 index 5e613a5..0000000 --- a/packaging/d2dm.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=OCF D2D Manager -Requires=dbus.socket -After=dbus.socket - -[Service] -Type=dbus -User=network_fw -Group=network_fw -BusName=net.d2dm.enabler -SmackProcessLabel=System -ExecStart=/usr/bin/d2d-manager diff --git a/packaging/d2ds.service b/packaging/d2ds.service deleted file mode 100644 index 70f6105..0000000 --- a/packaging/d2ds.service +++ /dev/null @@ -1,12 +0,0 @@ -[Unit] -Description=OCF D2D Sub-Owner Client -Requires=dbus.socket -After=dbus.socket - -[Service] -Type=dbus -User=network_fw -Group=network_fw -BusName=net.d2ds.enabler -SmackProcessLabel=System -ExecStart=/usr/bin/d2d-subowner diff --git a/packaging/ma.service b/packaging/ma.service new file mode 100644 index 0000000..ec192c1 --- /dev/null +++ b/packaging/ma.service @@ -0,0 +1,12 @@ +[Unit] +Description=OCF MOT Agent +Requires=dbus.socket +After=dbus.socket + +[Service] +Type=dbus +User=network_fw +Group=network_fw +BusName=net.ma.enabler +SmackProcessLabel=System +ExecStart=/usr/bin/mot-agent diff --git a/packaging/net.d2dm.service b/packaging/net.d2dm.service deleted file mode 100644 index 27cc224..0000000 --- a/packaging/net.d2dm.service +++ /dev/null @@ -1,7 +0,0 @@ -[D-BUS Service] -Name=net.d2dm.enabler - -Exec=/bin/false -User=network_fw -Group=network_fw -SystemdService=d2dm.service diff --git a/packaging/net.d2ds.service b/packaging/net.d2ds.service deleted file mode 100644 index a6a624e..0000000 --- a/packaging/net.d2ds.service +++ /dev/null @@ -1,7 +0,0 @@ -[D-BUS Service] -Name=net.d2ds.enabler - -Exec=/bin/false -User=network_fw -Group=network_fw -SystemdService=d2ds.service diff --git a/packaging/net.ma.service b/packaging/net.ma.service new file mode 100644 index 0000000..5a45583 --- /dev/null +++ b/packaging/net.ma.service @@ -0,0 +1,7 @@ +[D-BUS Service] +Name=net.ma.enabler + +Exec=/bin/false +User=network_fw +Group=network_fw +SystemdService=ma.service diff --git a/src/companion-manager/oic_svr_db_comp_manager.json b/src/companion-manager/oic_svr_db_comp_manager.json index 726061d..943566f 100644 --- a/src/companion-manager/oic_svr_db_comp_manager.json +++ b/src/companion-manager/oic_svr_db_comp_manager.json @@ -76,11 +76,11 @@ "rt": ["core.comp.device"], "if": ["oic.if.baseline"] }, { - "href": "/comp/operation", + "href": "/comp/operation", "rel": "", "rt": ["core.comp.operation"], "if": ["oic.if.baseline"] - } + } ], "permission": 31 }, diff --git a/src/companion-manager/src/comp_mot_agent.c b/src/companion-manager/src/comp_mot_agent.c index f4d5f23..35c6a89 100755 --- a/src/companion-manager/src/comp_mot_agent.c +++ b/src/companion-manager/src/comp_mot_agent.c @@ -27,13 +27,13 @@ #include #include -#define AGENT_SERVER_NAME "net.d2ds" -#define AGENT_OBJECT_PATH "/net/d2ds" -#define AGENT_SERVICE_INTERFACE "net.d2ds" +#define AGENT_SERVER_NAME "net.ma" +#define AGENT_OBJECT_PATH "/net/ma" +#define AGENT_SERVICE_INTERFACE "net.ma" -#define AGENT_ENABLER_NAME "net.d2ds.enabler" -#define AGENT_ENABLER_OBJ_PATH "/net/d2ds/enabler" -#define AGENT_ENABLER_INTERFACE "net.d2ds.enabler" +#define AGENT_ENABLER_NAME "net.ma.enabler" +#define AGENT_ENABLER_OBJ_PATH "/net/ma/enabler" +#define AGENT_ENABLER_INTERFACE "net.ma.enabler" #define NOTUSED(var) (var = var) diff --git a/src/d2d-manager/CMakeLists.txt b/src/d2d-manager/CMakeLists.txt deleted file mode 100644 index 7baa27c..0000000 --- a/src/d2d-manager/CMakeLists.txt +++ /dev/null @@ -1,28 +0,0 @@ -MESSAGE("===================================================================") -MESSAGE("Building...d2d-manager") -MESSAGE("===================================================================") - -SET(INTROSPECTION_PATH ${CMAKE_CURRENT_SOURCE_DIR}/introspection) -ADD_CUSTOM_COMMAND( - OUTPUT dbus - COMMAND gdbus-codegen - --interface-prefix net.d2dm. - --generate-c-code d2dm-generated-code - --c-generate-object-manager - --generate-docbook generated-docs - ${INTROSPECTION_PATH}/d2dm.xml - ${INTROSPECTION_PATH}/enabler.xml - COMMENT "Generating d2dm GDBus .c/.h" -) -ADD_CUSTOM_TARGET(GENERATED_DBUS_CODE DEPENDS dbus) - -MESSAGE(" - Set source folder") -FILE(GLOB D2D_MANAGER_SRCS *.c) -SET(D2D_MANAGER_SRCS ${D2D_MANAGER_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/d2dm-generated-code.c) -SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/d2dm-generated-code.c PROPERTIES GENERATED TRUE) - -SET(D2D_MANAGER "d2d-manager") -ADD_EXECUTABLE(${D2D_MANAGER} ${D2D_MANAGER_SRCS}) -ADD_DEPENDENCIES(${D2D_MANAGER} GENERATED_DBUS_CODE) -TARGET_LINK_LIBRARIES(${D2D_MANAGER} ${daemon_pkgs_LIBRARIES} pthread) -INSTALL(TARGETS ${D2D_MANAGER} DESTINATION bin) diff --git a/src/d2d-manager/d2dm-gdbus.c b/src/d2d-manager/d2dm-gdbus.c deleted file mode 100644 index 15714c0..0000000 --- a/src/d2d-manager/d2dm-gdbus.c +++ /dev/null @@ -1,178 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "d2dm-log.h" -#include "d2dm-util.h" -#include "d2dm-gdbus.h" - -static int _d2dm_close_gdbus_call(d2dm_service *service) -{ - /* CHECK: is connection ref count required? */ - g_object_unref(service->connection); - service->connection = NULL; - - return D2DM_ERROR_NONE; -} - -static void _dbus_name_owner_notify(GObject *object, GParamSpec *pspec, - gpointer *user_data) -{ - GDBusProxy *proxy = G_DBUS_PROXY(object); - gchar *name_owner = g_dbus_proxy_get_name_owner(proxy); - d2dm_service *service = (d2dm_service*)user_data; - - NOTUSED(pspec); - - if (NULL == name_owner) { - D2DM_LOGE("name_owner is not exists !"); - _d2dm_close_gdbus_call(service); - } - - g_free(name_owner); -} - -static int _d2dm_create_gdbus_call(d2dm_service *service) -{ - int id; - GError *error = NULL; - - if (NULL == service) - return D2DM_ERROR_INVALID_PARAMETER; - - if (NULL != service->connection) - return D2DM_ERROR_ALREADY_REGISTERED; - - service->connection = g_bus_get_sync(G_BUS_TYPE_SYSTEM, NULL, &error); - if (service->connection == NULL) { - if (error != NULL) { - D2DM_LOGE("Failed to connect to the D-BUS daemon [%s]", error->message); - g_error_free(error); - } - return D2DM_ERROR_IO_ERROR; - } - - id = g_signal_connect(service->connection, "notify::g-name-owner", - G_CALLBACK(_dbus_name_owner_notify), service); - if (0 == id) { - D2DM_LOGE("g_signal_connect() Fail"); - g_object_unref(service->connection); - service->connection = NULL; - return D2DM_ERROR_IO_ERROR; - } - - return D2DM_ERROR_NONE; -} - -static void _d2dm_subscribe_event(d2dm_service *service) -{ - //unsigned int id = 0; - - d2dm_check_null_ret("service", service); -#if 0 - id = g_dbus_connection_signal_subscribe( - (GDBusConnection *)service->connection, - CONNMAN_SERVER_NAME, CONNMAN_INTERFACE_MANAGER, - "ScanDone", "/", NULL, - G_DBUS_CALL_FLAGS_NONE, _wmeshd_signal_handler, service, NULL); - if (0 == id) { - WMESH_LOGE("g_dbus_connection_signal_subscribe(ScanDone) Fail(%d)", errno); - return; - } - service->dbus_sub_ids = g_list_append(service->dbus_sub_ids, GUINT_TO_POINTER(id)); - WMESH_LOGD("[Signal subscribe] : ScanDone (%d)", id); - - /* To monitor mesh profiles */ - id = g_dbus_connection_signal_subscribe( - (GDBusConnection *)service->connection, - CONNMAN_SERVER_NAME, - CONNMAN_INTERFACE_MESH, - "PropertyChanged", - NULL, /* Path */ - NULL, - G_DBUS_CALL_FLAGS_NONE, _wmeshd_signal_handler, service, NULL); - if (0 == id) { - WMESH_LOGE("g_dbus_connection_signal_subscribe(ScanDone) Fail(%d)", errno); - return; - } - service->dbus_sub_ids = g_list_append(service->dbus_sub_ids, GUINT_TO_POINTER(id)); - WMESH_LOGD("[Signal subscribe] : PropertyChanged (%d)", id); -#endif - /* End of subscription */ -} - - -static void _on_unsubscribe_ids(gpointer data, gpointer user_data) -{ - unsigned int id = GPOINTER_TO_UINT(data); - d2dm_service *service = (d2dm_service*)user_data; - - D2DM_LOGD("[Signal unsubscribe] : %d", id); - g_dbus_connection_signal_unsubscribe((GDBusConnection *)service->connection, id); -} - -static void _d2dm_unsubscribe_event(d2dm_service *service) -{ - d2dm_check_null_ret("service", service); - - g_list_foreach(service->dbus_sub_ids, _on_unsubscribe_ids, service); - - g_list_free(service->dbus_sub_ids); - service->dbus_sub_ids = NULL; -} - - -int d2dm_dbus_start(d2dm_service *service) -{ - int rv; - - rv = _d2dm_create_gdbus_call(service); - if (D2DM_ERROR_NONE != rv) - return rv; - - service->ca = g_cancellable_new(); - - /* Create all required proxies here */ -#if 0 - _gproxy_connman = _proxy_get_connman(service); - wmeshd_check_null_ret_error("_gproxy_connman", _gproxy_connman, - WMESHD_ERROR_IO_ERROR); - g_dbus_proxy_set_default_timeout( - G_DBUS_PROXY(_gproxy_connman), WMESH_DBUS_PROXY_TIMEOUT); -#endif - /* Subscribe events */ - _d2dm_subscribe_event(service); - - return D2DM_ERROR_NONE; -} - -int d2dm_dbus_stop(d2dm_service *service) -{ - int rv; - - if (NULL == service) - return D2DM_ERROR_INVALID_PARAMETER; - - /* Unsubscribe events */ - _d2dm_unsubscribe_event(service); - - g_cancellable_cancel(service->ca); - g_object_unref(service->ca); - service->ca = NULL; - - rv = _d2dm_close_gdbus_call(service); - return rv; -} - diff --git a/src/d2d-manager/d2dm-gdbus.h b/src/d2d-manager/d2dm-gdbus.h deleted file mode 100644 index c1714f5..0000000 --- a/src/d2d-manager/d2dm-gdbus.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Copyright (c) 2017 Samsung Electronics Co., Ltd All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __D2DM_GDBUS_H__ -#define __D2DM_GDBUS_H__ - -#include "d2dm.h" - -#ifdef __cplusplus -extern "C" { -#endif - -int d2dm_dbus_start(d2dm_service *service); -int d2dm_dbus_stop(d2dm_service *service); - -#ifdef __cplusplus -} -#endif - -#endif /** __D2DM_GDBUS_H__ */ - diff --git a/src/d2d-manager/d2dm-log.h b/src/d2d-manager/d2dm-log.h deleted file mode 100644 index 571dd25..0000000 --- a/src/d2d-manager/d2dm-log.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2D_LOG_H__ -#define __D2D_LOG_H__ - -#ifdef LOG_TAG -#undef LOG_TAG -#endif - -#ifdef USE_DLOG -#include - -#undef LOG_TAG -#define LOG_TAG "D2D_MANAGER" - -#define D2DM_LOGV(format, args...) LOGV(format, ##args) -#define D2DM_LOGD(format, args...) LOGD(format, ##args) -#define D2DM_LOGI(format, args...) LOGI(format, ##args) -#define D2DM_LOGW(format, args...) LOGW(format, ##args) -#define D2DM_LOGE(format, args...) LOGE(format, ##args) -#define D2DM_LOGF(format, args...) LOGF(format, ##args) - -#define __D2DM_LOG_FUNC_ENTER__ LOGD("Enter") -#define __D2DM_LOG_FUNC_EXIT__ LOGD("Quit") - -#define D2DM_SECLOGI(format, args...) SECURE_LOG(LOG_INFO, LOG_TAG, format, ##args) -#define D2DM_SECLOGD(format, args...) SECURE_LOG(LOG_DEBUG, LOG_TAG, format, ##args) - -#else /* USE_DLOG */ - -#define D2DM_LOGV(format, args...) -#define D2DM_LOGD(format, args...) -#define D2DM_LOGI(format, args...) -#define D2DM_LOGW(format, args...) -#define D2DM_LOGE(format, args...) -#define D2DM_LOGF(format, args...) - -#define __D2DM_LOG_FUNC_ENTER__ -#define __D2DM_LOG_FUNC_EXIT__ - -#define D2DM_SECLOGI(format, args...) -#define D2DM_SECLOGD(format, args...) - -#endif /* USE_DLOG */ - -#endif /* __D2D_LOG_H__ */ diff --git a/src/d2d-manager/d2dm-service-interface.c b/src/d2d-manager/d2dm-service-interface.c deleted file mode 100644 index 160da5e..0000000 --- a/src/d2d-manager/d2dm-service-interface.c +++ /dev/null @@ -1,355 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include - -#include "d2dm.h" -#include "d2dm-log.h" -#include "d2dm-util.h" -#include "d2dm-gdbus.h" -#include "d2dm-service.h" -#include "d2dm-service-interface.h" -#include "d2dm-generated-code.h" - -static NetD2dm *d2dm_dbus_object; -static Enabler *d2dm_activator_dbus_object; - -/* global list to care resource handle for each client */ -static GList *d2dm_dbus_client_list; -static GMutex d2dm_dbus_client_list_mutex; - -typedef struct _d2dm_dbus_client_s { - gchar *bus_name; -} d2dm_dbus_client_s; - -NetD2dm* d2dm_dbus_get_object() -{ - return d2dm_dbus_object; -} - -static int _d2dm_dbus_client_list_cleanup(GList *client_list) -{ - d2dm_dbus_client_s *client; - - d2dm_check_null_ret_error("client_list", client_list, FALSE); - - client = client_list->data; - - g_free(client->bus_name); - client->bus_name = NULL; - g_free(client); - g_list_free(client_list); - - return D2DM_ERROR_NONE; -} - -static int _d2dm_dbus_client_list_compare_bus_name(const void *a, const void *b) -{ - const d2dm_dbus_client_s *client = a; - return g_strcmp0(client->bus_name, b); -} - -static inline GList* _d2dm_dbus_client_list_find_client(const gchar *owner) -{ - return g_list_find_custom(d2dm_dbus_client_list, owner, - _d2dm_dbus_client_list_compare_bus_name); -} - -static void _d2dm_dbus_name_owner_changed_cb(GDBusConnection *conn, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - int ret; - GList *client = NULL; - gchar *name, *old_owner, *new_owner; - - NOTUSED(conn); - NOTUSED(sender_name); - NOTUSED(object_path); - NOTUSED(interface_name); - NOTUSED(signal_name); - NOTUSED(user_data); - - g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner); - - if (0 == strlen(new_owner)) { - g_mutex_lock(&d2dm_dbus_client_list_mutex); - client = _d2dm_dbus_client_list_find_client(old_owner); - if (client) { /* found bus name in our bus list */ - D2DM_LOGD("bus(%s) stopped", old_owner); - d2dm_dbus_client_list = g_list_remove_link(d2dm_dbus_client_list, client); - } - g_mutex_unlock(&d2dm_dbus_client_list_mutex); - - if (client) { - ret = _d2dm_dbus_client_list_cleanup(client); - if (D2DM_ERROR_NONE != ret) - D2DM_LOGE("_d2dm_dbus_client_list_cleanup() Fail(%d)", ret); - } - } -} - -static int _d2dm_dbus_subscribe_name_owner_changed(GDBusConnection *conn) -{ - unsigned int id; - - id = g_dbus_connection_signal_subscribe(conn, - "org.freedesktop.DBus", /* bus name */ - "org.freedesktop.DBus", /* interface */ - "NameOwnerChanged", /* member */ - "/org/freedesktop/DBus", /* path */ - NULL, /* arg0 */ - G_DBUS_SIGNAL_FLAGS_NONE, - _d2dm_dbus_name_owner_changed_cb, - NULL, - NULL); - if (0 == id) { - D2DM_LOGE("g_dbus_connection_signal_subscribe() Fail"); - return D2DM_ERROR_IO_ERROR; - } - - return D2DM_ERROR_NONE; -} - - -static gboolean _d2dm_dbus_handle_find_peer(NetD2dm *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - int ret = D2DM_ERROR_NONE; - d2dm_service *service = (d2dm_service *)user_data; - - d2dm_check_null_ret_error("service", service, FALSE); - -#if 0 - ret = d2dm_request_find_peers(service); - if (D2DM_ERROR_NONE != ret) - D2DM_LOGE("Failed to wmesh_request_scan !"); -#endif - net_d2dm_complete_find_peer(object, invocation, ret); - - return TRUE; -} - - -static void _d2dm_dbus_on_bus_acquired(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - gboolean ret; - GError *error = NULL; - d2dm_service *service = (d2dm_service *)user_data; - - NOTUSED(name); - - d2dm_dbus_object = net_d2dm_skeleton_new(); - if (NULL == d2dm_dbus_object) { - D2DM_LOGE("net_d2dm_skeleton_new() Fail"); - return; - } - - g_signal_connect(d2dm_dbus_object, "handle-find-peers", - G_CALLBACK(_d2dm_dbus_handle_find_peer), service); - - ret = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(d2dm_dbus_object), - conn, D2DM_DBUS_OBJPATH, &error); - if (FALSE == ret) { - D2DM_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message); - g_error_free(error); - } - - ret = _d2dm_dbus_subscribe_name_owner_changed(conn); - if (D2DM_ERROR_NONE != ret) { - D2DM_LOGE("_d2dm_dbus_subscribe_name_owner_changed() Fail(%d)", ret); - return; - } - -} - -static void _d2dm_dbus_on_name_lost(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - NOTUSED(conn); - NOTUSED(user_data); - - D2DM_LOGD("Lost the name %s", name); -} - -static void _d2dm_dbus_on_name_acquired(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - NOTUSED(conn); - NOTUSED(user_data); - - D2DM_LOGD("Acquired the name %s", name); -} - -static gboolean _d2dm_dbus_handle_enable(Enabler *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - int ret = D2DM_ERROR_NONE; - d2dm_service *service = (d2dm_service *)user_data; - - __D2DM_LOG_FUNC_ENTER__; - - /* Do API response first */ - enabler_complete_enable(object, invocation, ret); - service->d2dm_activated = TRUE; - - if (!service->d2dp_activated) { - /* Enable d2d proxy */ - } - service->d2dp_activated = TRUE; - - /* Notify d2d-manager enabled */ - net_d2dm_emit_proxy_enabled(d2dm_dbus_get_object(), ret); - - __D2DM_LOG_FUNC_EXIT__; - - return TRUE; -} - -static gboolean _d2dm_dbus_handle_disable(Enabler *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - int ret = D2DM_ERROR_NONE; - d2dm_service *service = (d2dm_service *)user_data; - - __D2DM_LOG_FUNC_ENTER__; - - /* Make response first */ - enabler_complete_disable(object, invocation, ret); - - /* Terminate daemon */ - d2dm_service_exit(service); - - return TRUE; -} - -static void _d2dm_dbus_on_activator_bus_acquired(GDBusConnection *conn, - const gchar *name, gpointer user_data) -{ - gboolean ret; - GError *error = NULL; - d2dm_service *service = (d2dm_service *)user_data; - - NOTUSED(name); - - __D2DM_LOG_FUNC_ENTER__; - - d2dm_activator_dbus_object = enabler_skeleton_new(); - if (NULL == d2dm_activator_dbus_object) { - D2DM_LOGE("enabler_skeleton_new() Fail"); - return; - } - - g_signal_connect(d2dm_activator_dbus_object, "handle-enable", - G_CALLBACK(_d2dm_dbus_handle_enable), service); - g_signal_connect(d2dm_activator_dbus_object, "handle-disable", - G_CALLBACK(_d2dm_dbus_handle_disable), service); - - ret = g_dbus_interface_skeleton_export( - G_DBUS_INTERFACE_SKELETON(d2dm_activator_dbus_object), - conn, D2DM_DBUS_ENABLER_OBJPATH, &error); - if (FALSE == ret) { - D2DM_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message); - g_error_free(error); - } - - __D2DM_LOG_FUNC_EXIT__; - -} - -static gboolean _d2dm_dbus_interface_init(d2dm_service *service) -{ - guint id; - guint activation_dbus_id; - d2dm_check_null_ret_error("service", service, FALSE); - - id = g_bus_own_name(G_BUS_TYPE_SYSTEM, - D2DM_DBUS_INTERFACE, - G_BUS_NAME_OWNER_FLAGS_REPLACE, - _d2dm_dbus_on_bus_acquired, - _d2dm_dbus_on_name_acquired, - _d2dm_dbus_on_name_lost, - service, - NULL); - if (0 == id) { - D2DM_LOGE("g_bus_own_name() Fail"); - return FALSE; - } - - /* Get D-Bus owner to activate mesh service daemon */ - activation_dbus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, - D2DM_DBUS_INTERFACE".enabler", - G_BUS_NAME_OWNER_FLAGS_REPLACE, - _d2dm_dbus_on_activator_bus_acquired, - NULL, - NULL, - service, - NULL); - - service->dbus_id = id; - service->activation_dbus_id = activation_dbus_id; - - /* Intiailze DBus sender */ - d2dm_dbus_start(service); - - return TRUE; -} - -static void _d2dm_dbus_deinit(d2dm_service *service) -{ - d2dm_check_null_ret("service", service); - - g_bus_unown_name(service->dbus_id); - g_bus_unown_name(service->activation_dbus_id); -} - -gboolean d2dm_service_interface_init(d2dm_service *service) -{ - guint ret; - d2dm_check_null_ret_error("service", service, FALSE); - - __D2DM_LOG_FUNC_ENTER__; - - /* Initialize dbus interface */ - ret = _d2dm_dbus_interface_init(service); - if (FALSE == ret) { - D2DM_LOGE("_d2dm_dbus_interface_init failed!!!"); - return FALSE; - } - - __D2DM_LOG_FUNC_EXIT__; - - return TRUE; -} - -void d2dm_service_interface_deinit(d2dm_service *service) -{ - d2dm_check_null_ret("service", service); - - /* De-initialize dbus interface */ - _d2dm_dbus_deinit(service); -} - diff --git a/src/d2d-manager/d2dm-service-interface.h b/src/d2d-manager/d2dm-service-interface.h deleted file mode 100644 index 343ccb5..0000000 --- a/src/d2d-manager/d2dm-service-interface.h +++ /dev/null @@ -1,52 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DM_SERVICE_INTERFACE_H__ -#define __D2DM_SERVICE_INTERFACE_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -/**< Mesh D-BUS service*/ -#define D2DM_DBUS_SERVICE "net.d2dm" - -/**< Mesh D-BUS service path */ -#define D2DM_DBUS_PATH "/net/d2dm" - -#ifndef D2DM_DBUS_INTERFACE -#define D2DM_DBUS_INTERFACE "net.d2dm" -#endif - -#ifndef D2DM_DBUS_OBJPATH -#define D2DM_DBUS_OBJPATH "/net/d2dm" -#endif - -#ifndef D2DM_DBUS_ENABLER_OBJPATH -#define D2DM_DBUS_ENABLER_OBJPATH "/net/d2dm/enabler" -#endif - -gboolean d2dm_service_interface_init(d2dm_service *service); -void d2dm_service_interface_deinit(d2dm_service *service); - -#ifdef __cplusplus -} -#endif - -#endif /* __D2DM_SERVICE_INTERFACE_H__ */ - diff --git a/src/d2d-manager/d2dm-service.c b/src/d2d-manager/d2dm-service.c deleted file mode 100644 index 6c3cdbf..0000000 --- a/src/d2d-manager/d2dm-service.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include - -#include "d2dm.h" -#include "d2dm-log.h" -#include "d2dm-util.h" - -d2dm_service *d2dm_service_new() -{ - d2dm_service *service; - - service = g_malloc0(sizeof(struct _d2d_service)); - - /* Create g-main loop */ - service->main_loop = g_main_loop_new(NULL, FALSE); - if (G_UNLIKELY(NULL == service->main_loop)) { - D2DM_LOGE("g-main loop creation failed!!!"); - g_free(service); - return NULL; - } - - return service; -} - -void d2dm_service_free(d2dm_service *service) -{ - d2dm_check_null_ret("service", service); - - /* Unref 'g-main loop' */ - if (service->main_loop) - g_main_loop_unref(service->main_loop); - - g_free(service); -} - -gboolean d2dm_service_run(d2dm_service *service) -{ - d2dm_check_null_ret_error("service", service, FALSE); - d2dm_check_null_ret_error("service->main_loop", service->main_loop, FALSE); - - g_main_loop_run(service->main_loop); - - return TRUE; -} - -gboolean d2dm_service_exit(d2dm_service *service) -{ - d2dm_check_null_ret_error("service", service, FALSE); - d2dm_check_null_ret_error("service->main_loop", service->main_loop, FALSE); - - g_main_loop_quit(service->main_loop); - - return TRUE; -} diff --git a/src/d2d-manager/d2dm-service.h b/src/d2d-manager/d2dm-service.h deleted file mode 100644 index 71230c9..0000000 --- a/src/d2d-manager/d2dm-service.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DM_SERVICE_H__ -#define __D2DM_SERVICE_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -d2dm_service *d2dm_service_new(); -void d2dm_service_free(d2dm_service *service); -gboolean d2dm_service_run(d2dm_service *service); -gboolean d2dm_service_exit(d2dm_service *service); - -#ifdef __cplusplus -} -#endif - -#endif /* __D2DM_SERVICE_H__ */ - diff --git a/src/d2d-manager/d2dm-util.h b/src/d2d-manager/d2dm-util.h deleted file mode 100644 index db7537c..0000000 --- a/src/d2d-manager/d2dm-util.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DM_UTIL_H__ -#define __D2DM_UTIL_H__ - -#include -#include -#include - -#include "d2dm-log.h" - -#define NOTUSED(var) (var = var) - -#define d2dm_check_null_ret_error(name, value, error) do { \ - if (G_UNLIKELY(NULL == (value))) { \ - D2DM_LOGE("%s is NULL", name); \ - return error; \ - } \ -} while (FALSE) - -#define d2dm_check_null_ret(name, value) do { \ - if (G_UNLIKELY(NULL == (value))) { \ - D2DM_LOGE("%s is NULL", name); \ - return; \ - } \ -} while (FALSE) - -#endif /* __D2DM_UTIL_H__ */ diff --git a/src/d2d-manager/d2dm.c b/src/d2d-manager/d2dm.c deleted file mode 100644 index c72c506..0000000 --- a/src/d2d-manager/d2dm.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include -#include -#include - -#include - -#include "d2dm.h" -#include "d2dm-log.h" -#include "d2dm-util.h" -#include "d2dm-service.h" -#include "d2dm-service-interface.h" - -/**< d2d-manager service */ -d2dm_service *d2dsvc; - -int main(int argc, char *argv[]) -{ - d2dm_service *service = NULL; - int ret_code = 0; - gboolean ret; - - __D2DM_LOG_FUNC_ENTER__; - -#if !GLIB_CHECK_VERSION(2, 32, 0) - if (!g_thread_supported()) - g_thread_init(NULL); -#endif - -#if !GLIB_CHECK_VERSION(2, 36, 0) - g_type_init(); -#endif - NOTUSED(argc); - NOTUSED(argv); - - D2DM_LOGI("service mainloop start"); - - /* Create d2d-manager service */ - d2dsvc = service = d2dm_service_new(); - - /* Initialize d2d-manager service interface layer */ - ret = d2dm_service_interface_init(service); - if (G_UNLIKELY(FALSE == ret)) { - D2DM_LOGE("Service interface init. failed!"); - ret_code = EXIT_FAILURE; - goto END; - } - /* Run d2d-manager service */ - ret = d2dm_service_run(service); - if (G_UNLIKELY(FALSE == ret)) { - D2DM_LOGE("Run service failed!"); - ret_code = EXIT_FAILURE; - } - -END: - /* Free d2d-manager service */ - d2dm_service_interface_deinit(service); - d2dm_service_free(service); - d2dsvc = NULL; - - __D2DM_LOG_FUNC_EXIT__; - return ret_code; -} - diff --git a/src/d2d-manager/d2dm.h b/src/d2d-manager/d2dm.h deleted file mode 100644 index 7136a84..0000000 --- a/src/d2d-manager/d2dm.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2D_MANAGER_H__ -#define __D2D_MANAGER_H__ - -#include -#include -#include - -/**< Internal error code with d2dm daemon. It should be matched with API side */ -typedef enum { - D2DM_ERROR_NONE = 0, /**< Successful */ - D2DM_ERROR_IO_ERROR, /**< I/O error */ - D2DM_ERROR_NO_DATA, /**< Data not exists */ - D2DM_ERROR_OUT_OF_MEMORY, /**< out of memory */ - D2DM_ERROR_OPERATION_FAILED, /**< operation failed */ - D2DM_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ - D2DM_ERROR_ALREADY_REGISTERED, /**< Request already registered */ - D2DM_ERROR_IN_PROGRESS /**< operation is in progress */ -} d2dm_error_e; - -/**< d2d-manager service structure */ -typedef struct _d2d_service { - GMainLoop *main_loop; /**< Service main-loop */ - guint dbus_id; /**< D-Bus id */ - guint activation_dbus_id; /**< D-Bus id for activation */ - - gpointer connection; /**< Connection handle for D-Bus call to other process */ - GCancellable *ca; /**< Cancellable object for D-Bus call (Daemon->other) */ - GList *dbus_sub_ids; /**< The list of subscribed signals */ - - gboolean d2dp_activated; /**< Whetehr d2d-proxy enabled or not */ - gboolean d2dm_activated; /**< Whether d2d-manager enabled or not */ - -} d2dm_service; - -#endif /* __D2D_MANAGER_H__ */ diff --git a/src/d2d-manager/introspection/.d2d-generated-code.h.swp b/src/d2d-manager/introspection/.d2d-generated-code.h.swp deleted file mode 100644 index d6676cb..0000000 Binary files a/src/d2d-manager/introspection/.d2d-generated-code.h.swp and /dev/null differ diff --git a/src/d2d-manager/introspection/d2dm.xml b/src/d2d-manager/introspection/d2dm.xml deleted file mode 100644 index 9184d21..0000000 --- a/src/d2d-manager/introspection/d2dm.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - - - - - - - - - - - - - - - diff --git a/src/d2d-manager/introspection/enabler.xml b/src/d2d-manager/introspection/enabler.xml deleted file mode 100644 index 05c396b..0000000 --- a/src/d2d-manager/introspection/enabler.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/src/d2d-manager/introspection/gen.sh b/src/d2d-manager/introspection/gen.sh deleted file mode 100755 index 7017da9..0000000 --- a/src/d2d-manager/introspection/gen.sh +++ /dev/null @@ -1,6 +0,0 @@ -gdbus-codegen --interface-prefix net.d2dm. \ - --generate-c-code d2dm-generated-code \ - --c-generate-object-manager \ - --generate-docbook generated-docs \ - d2dm.xml \ - enabler.xml diff --git a/src/d2d-subowner/CMakeLists.txt b/src/d2d-subowner/CMakeLists.txt deleted file mode 100644 index a9f670b..0000000 --- a/src/d2d-subowner/CMakeLists.txt +++ /dev/null @@ -1,40 +0,0 @@ -MESSAGE("===================================================================") -MESSAGE("Building...subowner client") -MESSAGE("===================================================================") - -INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/include/cjson) - -SET(INTROSPECTION_PATH ${CMAKE_CURRENT_SOURCE_DIR}/introspection) -ADD_CUSTOM_COMMAND( - OUTPUT dbus1 - COMMAND gdbus-codegen - --interface-prefix net.d2ds. - --generate-c-code d2ds-generated-code - --c-generate-object-manager - --generate-docbook generated-docs - ${INTROSPECTION_PATH}/d2ds.xml - ${INTROSPECTION_PATH}/enabler.xml - COMMENT "Generating d2ds GDBus .c/.h" -) - -MESSAGE(" - Check cmake packagesion") -pkg_check_modules(d2ds_pkgs REQUIRED ${COMMON_DEPS} ${TIZEN_DEPS} - capi-network-wifi-manager - sqlite3 - ) - -ADD_CUSTOM_TARGET(GENERATED_D2DS_DBUS_CODE DEPENDS dbus1) -MESSAGE(" - Set source folder") -FILE(GLOB SUBOWNER_SRCS *.c) -SET(SUBOWNER_SRCS ${SUBOWNER_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/d2ds-generated-code.c) -SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/d2ds-generated-code.c PROPERTIES GENERATED TRUE) - -INCLUDE_DIRECTORIES(${d2ds_pkgs_INCLUDE_DIRS}) -LINK_DIRECTORIES(${d2ds_pkgs_LIBRARY_DIRS}) - -SET(SUBOWNER "d2d-subowner") -ADD_EXECUTABLE(${SUBOWNER} ${SUBOWNER_SRCS}) -ADD_DEPENDENCIES(${SUBOWNER} GENERATED_D2DS_DBUS_CODE) -TARGET_LINK_LIBRARIES(${SUBOWNER} ${d2ds_pkgs_LIBRARIES} pthread) -INSTALL(TARGETS ${SUBOWNER} DESTINATION bin) -INSTALL(FILES oic_svr_db_d2ds.dat DESTINATION ${NETWORK_FW_DATADIR}) diff --git a/src/d2d-subowner/d2ds-log.h b/src/d2d-subowner/d2ds-log.h deleted file mode 100644 index 82aadd7..0000000 --- a/src/d2d-subowner/d2ds-log.h +++ /dev/null @@ -1,62 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DS_LOG_H__ -#define __D2DS_LOG_H__ - -#ifdef LOG_TAG -#undef LOG_TAG -#endif - -#ifdef USE_DLOG -#include - -#undef LOG_TAG -#define LOG_TAG "D2D_SUBOWNER" - -#define D2DS_LOGV(format, args...) LOGV(format, ##args) -#define D2DS_LOGD(format, args...) LOGD(format, ##args) -#define D2DS_LOGI(format, args...) LOGI(format, ##args) -#define D2DS_LOGW(format, args...) LOGW(format, ##args) -#define D2DS_LOGE(format, args...) LOGE(format, ##args) -#define D2DS_LOGF(format, args...) LOGF(format, ##args) - -#define __D2DS_LOG_FUNC_ENTER__ LOGD("Enter") -#define __D2DS_LOG_FUNC_EXIT__ LOGD("Quit") - -#define D2DS_SECLOGI(format, args...) SECURE_LOG(LOG_INFO, D2DS_LOG_TAG, format, ##args) -#define D2DS_SECLOGD(format, args...) SECURE_LOG(LOG_DEBUG, D2DS_LOG_TAG, format, ##args) - -#else /* USE_DLOG */ - -#define D2DS_LOGV(format, args...) -#define D2DS_LOGD(format, args...) -#define D2DS_LOGI(format, args...) -#define D2DS_LOGW(format, args...) -#define D2DS_LOGE(format, args...) -#define D2DS_LOGF(format, args...) - -#define __D2DS_LOG_FUNC_ENTER__ -#define __D2DS_LOG_FUNC_EXIT__ - -#define D2DS_SECLOGI(format, args...) -#define D2DS_SECLOGD(format, args...) - -#endif /* USE_DLOG */ - -#endif /* __D2DS_LOG_H__ */ diff --git a/src/d2d-subowner/d2ds-service-interface.c b/src/d2d-subowner/d2ds-service-interface.c deleted file mode 100644 index 2acc0d8..0000000 --- a/src/d2d-subowner/d2ds-service-interface.c +++ /dev/null @@ -1,542 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include - -#include "d2ds.h" -#include "d2ds-log.h" -#include "d2ds-util.h" -#include "d2ds-service.h" -#include "d2ds-subowner.h" -#include "d2ds-service-interface.h" -#include "d2ds-generated-code.h" - -static NetD2ds *d2ds_dbus_object; -static Enabler *d2ds_activator_dbus_object; - -/* global list to care resource handle for each client */ -static GList *d2ds_dbus_client_list; -static GMutex d2ds_dbus_client_list_mutex; - -typedef struct _d2ds_dbus_client_s { - gchar *bus_name; -} d2ds_dbus_client_s; - -NetD2ds* d2ds_dbus_get_object() -{ - return d2ds_dbus_object; -} - -static int _d2ds_dbus_client_list_cleanup(GList *client_list) -{ - d2ds_dbus_client_s *client; - - d2ds_check_null_ret_error("client_list", client_list, FALSE); - - client = client_list->data; - - g_free(client->bus_name); - client->bus_name = NULL; - g_free(client); - g_list_free(client_list); - - return D2DS_ERROR_NONE; -} - -static int _d2ds_dbus_client_list_compare_bus_name(const void *a, const void *b) -{ - const d2ds_dbus_client_s *client = a; - return g_strcmp0(client->bus_name, b); -} - -static inline GList* _d2ds_dbus_client_list_find_client(const gchar *owner) -{ - return g_list_find_custom(d2ds_dbus_client_list, owner, - _d2ds_dbus_client_list_compare_bus_name); -} - -static void _d2ds_dbus_name_owner_changed_cb(GDBusConnection *conn, - const gchar *sender_name, - const gchar *object_path, - const gchar *interface_name, - const gchar *signal_name, - GVariant *parameters, - gpointer user_data) -{ - int ret; - GList *client = NULL; - gchar *name, *old_owner, *new_owner; - - NOTUSED(conn); - NOTUSED(sender_name); - NOTUSED(object_path); - NOTUSED(interface_name); - NOTUSED(signal_name); - NOTUSED(user_data); - - g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner); - - if (0 == strlen(new_owner)) { - g_mutex_lock(&d2ds_dbus_client_list_mutex); - client = _d2ds_dbus_client_list_find_client(old_owner); - if (client) { /* found bus name in our bus list */ - D2DS_LOGD("bus(%s) stopped", old_owner); - d2ds_dbus_client_list = g_list_remove_link(d2ds_dbus_client_list, client); - } - g_mutex_unlock(&d2ds_dbus_client_list_mutex); - - if (client) { - ret = _d2ds_dbus_client_list_cleanup(client); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("_d2ds_dbus_client_list_cleanup() Fail(%d)", ret); - } - } -} - -static int _d2ds_dbus_subscribe_name_owner_changed(GDBusConnection *conn) -{ - unsigned int id; - - id = g_dbus_connection_signal_subscribe(conn, - "org.freedesktop.DBus", /* bus name */ - "org.freedesktop.DBus", /* interface */ - "NameOwnerChanged", /* member */ - "/org/freedesktop/DBus", /* path */ - NULL, /* arg0 */ - G_DBUS_SIGNAL_FLAGS_NONE, - _d2ds_dbus_name_owner_changed_cb, - NULL, - NULL); - if (0 == id) { - D2DS_LOGE("g_dbus_connection_signal_subscribe() Fail"); - return D2DS_ERROR_IO_ERROR; - } - - return D2DS_ERROR_NONE; -} - - -static gboolean _d2ds_dbus_handle_disc_mot_enb_devs(NetD2ds *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - ret = d2ds_request_disc_mot_enb_devs(service); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_disc_mot_enb_devs !"); - - net_d2ds_complete_disc_mot_enb_devs(object, invocation, ret); - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_disc_mowned_devs(NetD2ds *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - - ret = d2ds_request_disc_owned_devs(service); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_disc_owned_devs !"); - - net_d2ds_complete_disc_mowned_devs(object, invocation, ret); - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_mot(NetD2ds *object, - GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_pin, gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - - ret = d2ds_request_mot(service, arg_target, arg_pin); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_disc_moted_devs !"); - - net_d2ds_complete_mot(object, invocation, ret); - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_remove_mo(NetD2ds *object, - GDBusMethodInvocation *invocation, gchar *arg_target, gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - - ret = d2ds_request_remove_mo(service, arg_target); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_remove_mo !"); - - net_d2ds_complete_remove_mo(object, invocation, ret); - - return TRUE; -} - - -static gboolean _d2ds_dbus_handle_acl(NetD2ds *object, - GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_subject, gchar *arg_uri, - gchar *arg_rt, gchar *arg_interface, gint arg_permission, gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - - ret = d2ds_request_prov_acl(service, arg_target, arg_subject, arg_uri, arg_rt, arg_interface, arg_permission); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_prov_acl !"); - - net_d2ds_complete_acl(object, invocation, ret); - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_cred(NetD2ds *object, - GDBusMethodInvocation *invocation, gchar *arg_dev1, gchar *arg_dev2, gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - - ret = d2ds_request_prov_cred(service, arg_dev1, arg_dev2); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_prov_cred !"); - - net_d2ds_complete_cred(object, invocation, ret); - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_get_ownerid(NetD2ds *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - gchar *uuid_str = NULL; - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - ret = d2ds_request_get_ownerid(service, &uuid_str); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_get_ownerid = %d", ret); - - net_d2ds_complete_get_ownerid(object, invocation, uuid_str, ret); - - if (uuid_str) - g_free(uuid_str); - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_pairwise(NetD2ds *object, - GDBusMethodInvocation *invocation, gchar *target_1, - gchar *subject_1, gchar *uri_1, gchar *rt_1, - gchar *interface_1, int permission_1, gchar *target_2, - gchar *subject_2, gchar *uri_2, gchar *rt_2, - gchar *interface_2, int permission_2, gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - - ret = d2ds_request_pairwise(service, target_1, subject_1, uri_1, rt_1, - interface_1, permission_1, target_2, subject_2, uri_2, - rt_2, interface_2, permission_2); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_pairwise = %d", ret); - - net_d2ds_complete_pairwise(object, invocation, ret); - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_unpair(NetD2ds *object, - GDBusMethodInvocation *invocation, gchar *uuid_dev, gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - d2ds_check_null_ret_error("service", service, FALSE); - - ret = d2ds_request_unpair(service, uuid_dev); - if (D2DS_ERROR_NONE != ret) - D2DS_LOGE("Failed to d2ds_request_unpair = %d", ret); - - net_d2ds_complete_unpair(object, invocation, ret); - - return TRUE; -} - -static void _d2ds_dbus_on_bus_acquired(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - gboolean ret; - GError *error = NULL; - d2ds_service *service = (d2ds_service *)user_data; - - NOTUSED(name); - - d2ds_dbus_object = net_d2ds_skeleton_new(); - if (NULL == d2ds_dbus_object) { - D2DS_LOGE("net_d2ds_skeleton_new() Fail"); - return; - } - - g_signal_connect(d2ds_dbus_object, "handle-disc-mot-enb-devs", - G_CALLBACK(_d2ds_dbus_handle_disc_mot_enb_devs), service); - g_signal_connect(d2ds_dbus_object, "handle-disc-mowned-devs", - G_CALLBACK(_d2ds_dbus_handle_disc_mowned_devs), service); - g_signal_connect(d2ds_dbus_object, "handle-mot", - G_CALLBACK(_d2ds_dbus_handle_mot), service); - g_signal_connect(d2ds_dbus_object, "handle-remove-mo", - G_CALLBACK(_d2ds_dbus_handle_remove_mo), service); - g_signal_connect(d2ds_dbus_object, "handle-acl", - G_CALLBACK(_d2ds_dbus_handle_acl), service); - g_signal_connect(d2ds_dbus_object, "handle-cred", - G_CALLBACK(_d2ds_dbus_handle_cred), service); - g_signal_connect(d2ds_dbus_object, "handle-get-ownerid", - G_CALLBACK(_d2ds_dbus_handle_get_ownerid), service); - g_signal_connect(d2ds_dbus_object, "handle-pairwise", - G_CALLBACK(_d2ds_dbus_handle_pairwise), service); - g_signal_connect(d2ds_dbus_object, "handle-unpair", - G_CALLBACK(_d2ds_dbus_handle_unpair), service); - - ret = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(d2ds_dbus_object), - conn, D2DS_DBUS_OBJPATH, &error); - if (FALSE == ret) { - D2DS_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message); - g_error_free(error); - } - - ret = _d2ds_dbus_subscribe_name_owner_changed(conn); - if (D2DS_ERROR_NONE != ret) { - D2DS_LOGE("_d2ds_dbus_subscribe_name_owner_changed() Fail(%d)", ret); - return; - } -} - -static void _d2ds_dbus_on_name_lost(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - NOTUSED(conn); - NOTUSED(user_data); - - D2DS_LOGD("Lost the name %s", name); -} - -static void _d2ds_dbus_on_name_acquired(GDBusConnection *conn, const gchar *name, - gpointer user_data) -{ - NOTUSED(conn); - NOTUSED(user_data); - - D2DS_LOGD("Acquired the name %s", name); -} - -static gboolean _d2ds_dbus_handle_enable(Enabler *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - __D2DS_LOG_FUNC_ENTER__; - - /* Do API response first */ - enabler_complete_enable(object, invocation, ret); - service->d2ds_activated = TRUE; - - /* Intialize subowner client */ - ret = d2ds_request_enable(service); - if (D2DS_ERROR_NONE != ret) { - D2DS_LOGE("d2ds_request_enable() Fail(%d)", ret); - return FALSE; - } - - /* Notify d2ds enabled */ - net_d2ds_emit_subowner_enabled(d2ds_dbus_get_object(), ret); - - __D2DS_LOG_FUNC_EXIT__; - - return TRUE; -} - -static gboolean _d2ds_dbus_handle_disable(Enabler *object, - GDBusMethodInvocation *invocation, - gpointer user_data) -{ - int ret = D2DS_ERROR_NONE; - d2ds_service *service = (d2ds_service *)user_data; - - __D2DS_LOG_FUNC_ENTER__; - - /* Make response first */ - enabler_complete_disable(object, invocation, ret); - - /* De-Intialize subowner client */ - ret = d2ds_request_disable(service); - if (D2DS_ERROR_NONE != ret) { - D2DS_LOGE("d2ds_request_disable() Fail(%d)", ret); - return FALSE; - } - - /* Terminate daemon */ - d2ds_service_exit(service); - - return TRUE; -} - -static void _d2ds_dbus_on_activator_bus_acquired(GDBusConnection *conn, - const gchar *name, gpointer user_data) -{ - gboolean ret; - GError *error = NULL; - d2ds_service *service = (d2ds_service *)user_data; - - NOTUSED(name); - - __D2DS_LOG_FUNC_ENTER__; - - d2ds_activator_dbus_object = enabler_skeleton_new(); - if (NULL == d2ds_activator_dbus_object) { - D2DS_LOGE("enabler_skeleton_new() Fail"); - return; - } - - g_signal_connect(d2ds_activator_dbus_object, "handle-enable", - G_CALLBACK(_d2ds_dbus_handle_enable), service); - g_signal_connect(d2ds_activator_dbus_object, "handle-disable", - G_CALLBACK(_d2ds_dbus_handle_disable), service); - - ret = g_dbus_interface_skeleton_export( - G_DBUS_INTERFACE_SKELETON(d2ds_activator_dbus_object), - conn, D2DS_DBUS_ENABLER_OBJPATH, &error); - if (FALSE == ret) { - D2DS_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message); - g_error_free(error); - } - - __D2DS_LOG_FUNC_EXIT__; - -} - -static gboolean _d2ds_dbus_interface_init(d2ds_service *service) -{ - guint id; - guint activation_dbus_id; - d2ds_check_null_ret_error("service", service, FALSE); - - id = g_bus_own_name(G_BUS_TYPE_SYSTEM, - D2DS_DBUS_INTERFACE, - G_BUS_NAME_OWNER_FLAGS_REPLACE, - _d2ds_dbus_on_bus_acquired, - _d2ds_dbus_on_name_acquired, - _d2ds_dbus_on_name_lost, - service, - NULL); - if (0 == id) { - D2DS_LOGE("g_bus_own_name() Fail"); - return FALSE; - } - - /* Get D-Bus owner to activate subowner service daemon */ - activation_dbus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, - D2DS_DBUS_INTERFACE".enabler", - G_BUS_NAME_OWNER_FLAGS_REPLACE, - _d2ds_dbus_on_activator_bus_acquired, - NULL, - NULL, - service, - NULL); - - service->dbus_id = id; - service->activation_dbus_id = activation_dbus_id; - - return TRUE; -} - -void d2ds_notify_mot_env_devs(GVariant *devices) -{ - /* Notify d2ds enabled */ - net_d2ds_emit_disc_mot_enb_devs_done(d2ds_dbus_get_object(), devices); -} - -void d2ds_notify_mowned_devs(GVariant *devices) -{ - /* Notify d2ds enabled */ - net_d2ds_emit_disc_mowned_devs_done(d2ds_dbus_get_object(), devices); -} - -static void _d2ds_dbus_deinit(d2ds_service *service) -{ - d2ds_check_null_ret("service", service); - - g_bus_unown_name(service->dbus_id); - g_bus_unown_name(service->activation_dbus_id); -} - -gboolean d2ds_service_interface_init(d2ds_service *service) -{ - guint ret; - d2ds_check_null_ret_error("service", service, FALSE); - - __D2DS_LOG_FUNC_ENTER__; - - /* Initialize dbus interface */ - ret = _d2ds_dbus_interface_init(service); - if (FALSE == ret) { - D2DS_LOGE("%s failed!!!", __func__); - return FALSE; - } - - __D2DS_LOG_FUNC_EXIT__; - - return TRUE; -} - -void d2ds_service_interface_deinit(d2ds_service *service) -{ - d2ds_check_null_ret("service", service); - - /* De-initialize dbus interface */ - _d2ds_dbus_deinit(service); -} - diff --git a/src/d2d-subowner/d2ds-service-interface.h b/src/d2d-subowner/d2ds-service-interface.h deleted file mode 100644 index 0350960..0000000 --- a/src/d2d-subowner/d2ds-service-interface.h +++ /dev/null @@ -1,58 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DS_SERVICE_INTERFACE_H__ -#define __D2DS_SERVICE_INTERFACE_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -/**< Mesh D-BUS service*/ -#define D2DS_DBUS_SERVICE "net.d2ds" - -/**< Mesh D-BUS service path */ -#define D2DS_DBUS_PATH "/net/d2ds" - -#ifndef D2DS_DBUS_INTERFACE -#define D2DS_DBUS_INTERFACE "net.d2ds" -#endif - -#ifndef D2DS_DBUS_OBJPATH -#define D2DS_DBUS_OBJPATH "/net/d2ds" -#endif - -#ifndef D2DS_DBUS_ENABLER_OBJPATH -#define D2DS_DBUS_ENABLER_OBJPATH "/net/d2ds/enabler" -#endif - -struct _NetD2ds; -typedef struct _NetD2ds NetD2ds; - -NetD2ds* d2ds_dbus_get_object(); -gboolean d2ds_service_interface_init(d2ds_service *service); -void d2ds_service_interface_deinit(d2ds_service *service); -void d2ds_notify_mot_env_devs(GVariant *devices); -void d2ds_notify_mowned_devs(GVariant *devices); - -#ifdef __cplusplus -} -#endif - -#endif /* __D2DS_SERVICE_INTERFACE_H__ */ - diff --git a/src/d2d-subowner/d2ds-service.c b/src/d2d-subowner/d2ds-service.c deleted file mode 100644 index 650eef8..0000000 --- a/src/d2d-subowner/d2ds-service.c +++ /dev/null @@ -1,71 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include - -#include "d2ds.h" -#include "d2ds-log.h" -#include "d2ds-util.h" - -d2ds_service *d2ds_service_new() -{ - d2ds_service *service; - - service = g_malloc0(sizeof(struct _d2ds_service)); - - /* Create g-main loop */ - service->main_loop = g_main_loop_new(NULL, FALSE); - if (G_UNLIKELY(NULL == service->main_loop)) { - D2DS_LOGE("g-main loop creation failed!!!"); - g_free(service); - return NULL; - } - - return service; -} - -void d2ds_service_free(d2ds_service *service) -{ - d2ds_check_null_ret("service", service); - - /* Unref 'g-main loop' */ - if (service->main_loop) - g_main_loop_unref(service->main_loop); - - g_free(service); -} - -gboolean d2ds_service_run(d2ds_service *service) -{ - d2ds_check_null_ret_error("service", service, FALSE); - d2ds_check_null_ret_error("service->main_loop", service->main_loop, FALSE); - - g_main_loop_run(service->main_loop); - - return TRUE; -} - -gboolean d2ds_service_exit(d2ds_service *service) -{ - d2ds_check_null_ret_error("service", service, FALSE); - d2ds_check_null_ret_error("service->main_loop", service->main_loop, FALSE); - - g_main_loop_quit(service->main_loop); - - return TRUE; -} diff --git a/src/d2d-subowner/d2ds-service.h b/src/d2d-subowner/d2ds-service.h deleted file mode 100644 index f5792dc..0000000 --- a/src/d2d-subowner/d2ds-service.h +++ /dev/null @@ -1,36 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DS_SERVICE_H__ -#define __D2DS_SERVICE_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -d2ds_service* d2ds_service_new(); -void d2ds_service_free(d2ds_service *service); -gboolean d2ds_service_run(d2ds_service *service); -gboolean d2ds_service_exit(d2ds_service *service); - -#ifdef __cplusplus -} -#endif - -#endif /* __D2DS_SERVICE_H__ */ - diff --git a/src/d2d-subowner/d2ds-subowner.c b/src/d2d-subowner/d2ds-subowner.c deleted file mode 100644 index 942cbaa..0000000 --- a/src/d2d-subowner/d2ds-subowner.c +++ /dev/null @@ -1,2123 +0,0 @@ -/****************************************************************** - * - * Copyright 2016 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *****************************************************************/ -#include -#include -#include -#ifdef HAVE_UNISTD_H -#include -#endif -#include -#include - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include -#include - -#include "d2ds.h" -#include "d2ds-log.h" -#include "d2ds-util.h" -#include "d2ds-subowner.h" -#include "d2ds-service-interface.h" -#include "d2ds-generated-code.h" -#include "magent-db.h" - -#define WIFI_MAC_ADDR_LENGTH 17 - -#define MAX_FILE_PATH_LEN 1024 -#define ACL_RESRC_MAX_LEN 128 - -/*This value is used only for sample (not OCF spec) */ -#define ACL_RESRC_ARRAY_SIZE 3 - -#define DISCOVERY_TIMEOUT 3 /**< 3 sec */ -#define CALLBACK_TIMEOUT_10S 10 * 1000 /**< 10 sec = 10 * 1000 * 1ms */ -#define CALLBACK_TIMEOUT_5S 5 * 1000 /**< 5sec = 5 * 1000 * 1ms */ - -#define TIMEOUT_USED - -/* '_' for separaing from the same constant variable in srmresourcestrings.c */ -static const char* SVR_DB_FILE_NAME = "oic_svr_db_d2ds.dat"; -static const char* PRVN_DB_FILE_NAME = "oic_pdm_d2ds.db"; - -#ifdef TEST -static const OicSecPrm_t SUPPORTED_PRMS[2] = { PRM_PRE_CONFIGURED, PRM_RANDOM_PIN }; -#endif - -OCStackResult GetDoxmDevOwnerId(OicUuid_t *devownerid); - -typedef struct { - d2ds_cmd_id_e cid; /**< Command ID */ - int tid; /**< Timer ID */ - GThread *thread; /**< Thread handle */ - unsigned int sid; /**< Subcribed signal ID */ - bool found; /**< Whether we discoverd devices */ - void *cb; /**< Callback function pointer */ - void *userdata; /**< User data */ - - /* To-Do : Change belows to union type */ - - /* For MOT */ - gchar *uuid_target_str; /**< Target device*/ - gchar *pin; /**< PIN */ - - /* For ACL provisioning */ - gchar *uuid_subject_str;/**< Subject device for ACL */ - gchar *rsrc_uri; /**< Resource URI */ - gchar *rsrc_type; /**< Resouce Type */ - gchar *rsrc_interface; /**< Resource Interface Array To-Do : Make this as array */ - int permission;/**< Permision */ - - /* For CRED provisioning */ - gchar *uuid_dev1; /**< 1st device for CRED provisiong */ - gchar *uuid_dev2; /**< 2nd device for CRED provisiong */ - - /* For Pairwaise linking */ - gchar *target_1; /**< Target device 1*/ - gchar *target_2; /**< Target device 1*/ - - gchar *subject_1; /**< Target device for ACL */ - gchar *rsrc_uri_1; /**< Resource URI */ - gchar *rsrc_type_1; /**< Resouce Type */ - gchar *rsrc_interface_1; /**< Resource Interface Array To-Do : Make this as array */ - int permission_1;/**< Permision */ - gchar *subject_2; /**< Target device for ACL */ - gchar *rsrc_uri_2; /**< Resource URI */ - gchar *rsrc_type_2; /**< Resouce Type */ - gchar *rsrc_interface_2; /**< Resource Interface Array To-Do : Make this as array */ - int permission_2;/**< Permision */ - - /* For unpair */ - gchar *target_to_revoke; /**< Target device to revoke */ - -} d2ds_req_cb_s; - -typedef struct _d2ds_subowner { - OCProvisionDev_t* g_motdev_list; /**< Discoverd MOT enabled devices list */ - OCProvisionDev_t* g_mowned_list; /**< My owned MOT enabled devices list */ - - int g_motdev_cnt; /**< Number of MOT enabled devices */ - int g_mowndev_cnt; /**< Number of my owned MOT enabled devices */ - - bool g_doneCB; /**< This means callback completed or not */ - - char *rnd_pin; /**< Random PIN */ - int rnd_pin_len; /**< Random PIN length */ - -} d2ds_subowner_s; - -d2ds_subowner_s *g_client; - -#define CASE_TO_STR(x) case x: return #x; -const char* _error_to_string(OCStackResult err) -{ - switch (err) { - /* CHECK: List all enum values here */ - CASE_TO_STR(OC_STACK_OK) - CASE_TO_STR(OC_STACK_RESOURCE_CREATED) - CASE_TO_STR(OC_STACK_RESOURCE_DELETED) - CASE_TO_STR(OC_STACK_CONTINUE) - CASE_TO_STR(OC_STACK_RESOURCE_CHANGED) - CASE_TO_STR(OC_STACK_INVALID_URI) - CASE_TO_STR(OC_STACK_INVALID_QUERY) - CASE_TO_STR(OC_STACK_INVALID_IP) - CASE_TO_STR(OC_STACK_INVALID_PORT) - CASE_TO_STR(OC_STACK_INVALID_CALLBACK) - CASE_TO_STR(OC_STACK_INVALID_METHOD) - CASE_TO_STR(OC_STACK_INVALID_PARAM) - CASE_TO_STR(OC_STACK_INVALID_OBSERVE_PARAM) - - - CASE_TO_STR(OC_STACK_NO_MEMORY) - CASE_TO_STR(OC_STACK_COMM_ERROR) - CASE_TO_STR(OC_STACK_TIMEOUT) - CASE_TO_STR(OC_STACK_ADAPTER_NOT_ENABLED) - CASE_TO_STR(OC_STACK_NOTIMPL) - CASE_TO_STR(OC_STACK_RESOURCE_ERROR) - CASE_TO_STR(OC_STACK_SLOW_RESOURCE) - CASE_TO_STR(OC_STACK_DUPLICATE_REQUEST) - CASE_TO_STR(OC_STACK_OBSERVER_NOT_FOUND) - CASE_TO_STR(OC_STACK_VIRTUAL_DO_NOT_HANDLE) - CASE_TO_STR(OC_STACK_INVALID_OPTION) - CASE_TO_STR(OC_STACK_MALFORMED_RESPONSE) - CASE_TO_STR(OC_STACK_PERSISTENT_BUFFER_REQUIRED) - - CASE_TO_STR(OC_STACK_INVALID_REQUEST_HANDLE) - CASE_TO_STR(OC_STACK_INVALID_DEVICE_INFO) - CASE_TO_STR(OC_STACK_INVALID_JSON) - CASE_TO_STR(OC_STACK_UNAUTHORIZED_REQ) - CASE_TO_STR(OC_STACK_TOO_LARGE_REQ) - CASE_TO_STR(OC_STACK_PDM_IS_NOT_INITIALIZED) - CASE_TO_STR(OC_STACK_DUPLICATE_UUID) - CASE_TO_STR(OC_STACK_INCONSISTENT_DB) - CASE_TO_STR(OC_STACK_SVR_DB_NOT_EXIST) - CASE_TO_STR(OC_STACK_AUTHENTICATION_FAILURE) - CASE_TO_STR(OC_STACK_NOT_ALLOWED_OXM) -#ifdef WITH_PRESENCE - CASE_TO_STR(OC_STACK_PRESENCE_STOPPED) - CASE_TO_STR(OC_STACK_PRESENCE_TIMEOUT) - CASE_TO_STR(OC_STACK_PRESENCE_DO_NOT_HANDLE) -#endif - CASE_TO_STR(OC_STACK_USER_DENIED_REQ) - CASE_TO_STR(OC_STACK_NOT_ACCEPTABLE) - CASE_TO_STR(OC_STACK_METHOD_NOT_ALLOWED) - CASE_TO_STR(OC_STACK_FORBIDDEN_REQ) - CASE_TO_STR(OC_STACK_INTERNAL_SERVER_ERROR) - CASE_TO_STR(OC_STACK_NOT_IMPLEMENTED) - CASE_TO_STR(OC_STACK_BAD_GATEWAY) - CASE_TO_STR(OC_STACK_SERVICE_UNAVAILABLE) - CASE_TO_STR(OC_STACK_GATEWAY_TIMEOUT) - CASE_TO_STR(OC_STACK_PROXY_NOT_SUPPORTED) - CASE_TO_STR(OC_STACK_ERROR) - default : - return "Unknown Error"; - } -} - -static const char* _wifi_error_to_string(wifi_manager_error_e err) -{ - switch (err) { - /* CHECK: List all enum values here */ - CASE_TO_STR(WIFI_MANAGER_ERROR_NONE) - CASE_TO_STR(WIFI_MANAGER_ERROR_INVALID_PARAMETER) - CASE_TO_STR(WIFI_MANAGER_ERROR_OUT_OF_MEMORY) - CASE_TO_STR(WIFI_MANAGER_ERROR_INVALID_OPERATION) - CASE_TO_STR(WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED) - CASE_TO_STR(WIFI_MANAGER_ERROR_OPERATION_FAILED) - CASE_TO_STR(WIFI_MANAGER_ERROR_NO_CONNECTION) - CASE_TO_STR(WIFI_MANAGER_ERROR_NOW_IN_PROGRESS) - CASE_TO_STR(WIFI_MANAGER_ERROR_ALREADY_EXISTS) - CASE_TO_STR(WIFI_MANAGER_ERROR_OPERATION_ABORTED) - CASE_TO_STR(WIFI_MANAGER_ERROR_DHCP_FAILED) - CASE_TO_STR(WIFI_MANAGER_ERROR_INVALID_KEY) - CASE_TO_STR(WIFI_MANAGER_ERROR_NO_REPLY) - CASE_TO_STR(WIFI_MANAGER_ERROR_SECURITY_RESTRICTED) - CASE_TO_STR(WIFI_MANAGER_ERROR_ALREADY_INITIALIZED) - CASE_TO_STR(WIFI_MANAGER_ERROR_PERMISSION_DENIED) - CASE_TO_STR(WIFI_MANAGER_ERROR_NOT_SUPPORTED) - default : - return "WIFI_MANAGER_ERROR_UNKNOWN"; - } -} - -OicUuid_t* _convert_uuid(gchar *device_id) -{ - OicUuid_t *uuid; - - if (NULL == device_id) - return NULL; - - uuid = g_malloc0(sizeof(struct OicUuid)); - if (NULL == uuid) { - D2DS_LOGE("g_malloc0() Fail(%d)", errno); - return NULL; - } - - sscanf(&device_id[0], "%2hhx", &uuid->id[0]); - sscanf(&device_id[2], "%2hhx", &uuid->id[1]); - sscanf(&device_id[4], "%2hhx", &uuid->id[2]); - sscanf(&device_id[6], "%2hhx", &uuid->id[3]); - /* device_id[8] == '-' */ - sscanf(&device_id[9], "%2hhx", &uuid->id[4]); - sscanf(&device_id[11], "%2hhx", &uuid->id[5]); - /* device_id[13] == '-' */ - sscanf(&device_id[14], "%2hhx", &uuid->id[6]); - sscanf(&device_id[16], "%2hhx", &uuid->id[7]); - /* device_id[18] == '-' */ - sscanf(&device_id[19], "%2hhx", &uuid->id[8]); - sscanf(&device_id[21], "%2hhx", &uuid->id[9]); - /* device_id[23] == '-' */ - sscanf(&device_id[24], "%2hhx", &uuid->id[10]); - sscanf(&device_id[26], "%2hhx", &uuid->id[11]); - sscanf(&device_id[28], "%2hhx", &uuid->id[12]); - sscanf(&device_id[30], "%2hhx", &uuid->id[13]); - sscanf(&device_id[32], "%2hhx", &uuid->id[14]); - sscanf(&device_id[34], "%2hhx", &uuid->id[15]); - - return uuid; -} - -static void _print_no_uuid(int idx, const OicUuid_t* uuid) -{ - char uuid_string[256] = {0}; - snprintf(uuid_string, sizeof(uuid_string), - "[%d] %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - idx, - (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], - (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], - (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], - (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); - D2DS_LOGI("%s", uuid_string); -} -#ifdef DEBUG_UUID -static void _print_uuid(const OicUuid_t* uuid) -{ - char uuid_string[256] = {0}; - snprintf(uuid_string, sizeof(uuid_string), - "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], - (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], - (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], - (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); - D2DS_LOGI("%s", uuid_string); -} -#endif - -static void _print_uuid_res(const OicUuid_t* uuid, int res) -{ - char uuid_string[256] = {0}; - snprintf(uuid_string, sizeof(uuid_string), - "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x : [%d] [%s]", - (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], - (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], - (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], - (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15], - res, _error_to_string(res)); - D2DS_LOGI("%s", uuid_string); -} - -#ifdef TEST -static OCProvisionDev_t* _get_dev_by_id(const OCProvisionDev_t* dev_lst, const int dev_num) -{ - if (!dev_lst || 0>=dev_num) { - D2DS_LOGI("Device List is Empty..\n"); - return NULL; - } - - OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst; - for (int i=0; lst; ) { - if (dev_num == ++i) { - return lst; - } - lst = lst->next; - } - - return NULL; -} -#endif - -static OCProvisionDev_t* _get_dev_by_uuid(const OCProvisionDev_t* dev_lst, - const OicUuid_t* uuid) -{ - if (!dev_lst ) { - D2DS_LOGI("dev_lst is empty"); - return NULL; - } - - if (!uuid ) { - D2DS_LOGI("uuid is NULL"); - return NULL; - } - - OCProvisionDev_t* lst = (OCProvisionDev_t*)dev_lst; -#ifdef DEBUG_UUID - _print_uuid(&lst->doxm->deviceID); -#endif - while (lst) { - if (!memcmp((const void *)&lst->doxm->deviceID, (const void *)uuid, sizeof(OicUuid_t))) - return lst; -#ifdef DEBUG_UUID - _print_uuid(uuid); -#endif - lst = lst->next; - } - - return NULL; -} - -static int _print_dev_list(const OCProvisionDev_t* dev_lst) -{ - if (!dev_lst) { - D2DS_LOGI("Device List is Empty..\n\n"); - return 0; - } - - OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst; - int lst_cnt = 0; - for ( ; lst; ) { - _print_no_uuid(++lst_cnt, (const OicUuid_t*) &lst->doxm->deviceID); - lst = lst->next; - } - - return lst_cnt; -} - -static int _print_result_list(const OCProvisionResult_t* rslt_lst, const int rslt_cnt) -{ - if (!rslt_lst || 0>=rslt_cnt) { - D2DS_LOGI(" Device List is Empty..\n\n"); - return 0; - } - - int lst_cnt = 0; - for ( ; rslt_cnt>lst_cnt; ++lst_cnt) { - _print_uuid_res((const OicUuid_t*) &rslt_lst[lst_cnt].deviceId, rslt_lst[lst_cnt].res); - } - - return lst_cnt; -} - -static FILE* _fopen_prvn_mng(const char* path, const char* mode) -{ - char data_dir[MAX_FILE_PATH_LEN] = {0,}; - - snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s", - tzplatform_getenv(TZ_SYS_GLOBALUSER_DATA), SVR_DB_FILE_NAME); - - NOTUSED(path); -#if 0 - D2DS_LOGD("Unsed DB path %s\n", path); -#endif - - return fopen(data_dir, mode); -} - -static int _wait_cb_ret(int msec) -{ - OCStackResult ret = OC_STACK_OK; - - d2ds_check_null_ret_error("g_client", g_client, OC_STACK_INVALID_PARAM); - - for (int i=0; !g_client->g_doneCB && msec > i; ++i) { - usleep(1000); - ret = OCProcess(); - if (OC_STACK_OK != ret) { - D2DS_LOGE( "OCStack process error = %d", ret); - return ret; - } - } - - if (!g_client->g_doneCB) - OCPDMCleanupForTimeout(); - - return ret; -} - -static void _input_pin_cb(char* pin, size_t len) -{ - d2ds_check_null_ret("g_client", g_client); - - if (!pin || OXM_RANDOM_PIN_MAX_SIZE>=len) { - D2DS_LOGE( "inputPinCB invalid parameters"); - return; - } - - memcpy(pin, g_client->rnd_pin, g_client->rnd_pin_len); - D2DS_LOGI("Random PIN : %s", g_client->rnd_pin); -} - -static void _remove_mot_client() -{ - GMutex motdev_mutex; - - d2ds_check_null_ret("g_client", g_client); - - g_mutex_init(&motdev_mutex); - g_mutex_lock(&motdev_mutex); - if (g_client->g_motdev_list) { - OCDeleteDiscoveredDevices(g_client->g_motdev_list); - g_client->g_motdev_list = NULL; - } - g_mutex_unlock(&motdev_mutex); -} - -static void _remove_owned_client() -{ - GMutex owned_dev_mutex; - - d2ds_check_null_ret("g_client", g_client); - - /* delete un/owned device lists before updating them */ - g_mutex_init(&owned_dev_mutex); - g_mutex_lock(&owned_dev_mutex); - if (g_client->g_mowned_list) { - OCDeleteDiscoveredDevices(g_client->g_mowned_list); - g_client->g_mowned_list = NULL; - } - g_mutex_unlock(&owned_dev_mutex); -} - -static int _set_device_id_seed(void) -{ - int ret = WIFI_MANAGER_ERROR_NONE; - gchar *mac_addr = NULL; - gchar *test_mac = "00:12:36:11:22:33"; - - wifi_manager_h wifi_handle = NULL; - bool wifi_activated = false; - - /* Initialize Wi-Fi driver */ - ret = wifi_manager_initialize(&wifi_handle); - if (WIFI_MANAGER_ERROR_NONE != ret) - D2DS_LOGE("Failed to get wifi manager handle ! [%d][%s]", ret, _wifi_error_to_string(ret)); - - wifi_manager_is_activated(wifi_handle, &wifi_activated); - if (false == wifi_activated) { - ret = wifi_manager_activate(wifi_handle, NULL, NULL); - if (WIFI_MANAGER_ERROR_NONE != ret) - D2DS_LOGE("Failed to activate wifi ! [%d][%s]", ret, _wifi_error_to_string(ret)); - } - - ret = wifi_manager_get_mac_address(wifi_handle, &mac_addr); - if (WIFI_MANAGER_ERROR_NONE != ret) { - D2DS_LOGE("Fail to get MAC address [%d][%s]", ret, _wifi_error_to_string(ret)); - /* Make deviceID based on Fake MAC address */ - ret = SetDeviceIdSeed((const guchar *)test_mac, WIFI_MAC_ADDR_LENGTH); - } else { - /* Make deviceID based on Wi-Fi MAC address */ - ret = SetDeviceIdSeed((const guchar *)mac_addr, WIFI_MAC_ADDR_LENGTH); - g_free(mac_addr); - } - if (OC_STACK_OK != ret ) { - D2DS_LOGE( "SetDeviceIdSeed() error = [%d][%s]", ret, _error_to_string(ret)); - } - wifi_manager_deinitialize(wifi_handle); - return ret; -} - -static int _init_provision_client(void) -{ - int ret = OC_STACK_OK; - char data_dir[MAX_FILE_PATH_LEN] = {0,}; - - snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s", - tzplatform_getenv(TZ_SYS_GLOBALUSER_DATA), PRVN_DB_FILE_NAME); - - /* initialize persistent storage for SVR DB */ - static OCPersistentStorage ps = { _fopen_prvn_mng, fread, fwrite, fclose, unlink }; - - ret = OCRegisterPersistentStorageHandler(&ps); - if (OC_STACK_OK != ret ) { - D2DS_LOGE( "OCRegisterPersistentStorageHandler error = %d", ret); - return ret; - } - - _set_device_id_seed(); - - /* initialize OC stack and provisioning manager */ - ret = OCInit(NULL, 0, OC_CLIENT_SERVER); - if (OC_STACK_OK != ret ) { - D2DS_LOGE( "OCInit() error = [%d][%s]", ret, _error_to_string(ret)); - return ret; - } - - /* Do Self-Ownership Transfer */ - ret = OCConfigSelfOwnership(); - if (OC_STACK_OK != ret ) { - D2DS_LOGE( "OCConfigSelfOwnership() error = [%d][%s]", ret, _error_to_string(ret)); - //return ret; - } - - ret = OCInitPM(data_dir); - if (OC_STACK_OK != ret ) { - D2DS_LOGE( "OCInitPM() error = [%d][%s]", ret, _error_to_string(ret)); - return ret; - } - - SetInputPinCB(_input_pin_cb); - - g_client = g_malloc0(sizeof(d2ds_subowner_s)); - if (!g_client) { - D2DS_LOGE( "memory allocation error = %d", errno); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_client->g_motdev_list = NULL; - g_client->g_mowned_list = NULL; - g_client->rnd_pin = NULL; - - return ret; -} -static void _request_cleanup(gpointer data) -{ - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - d2ds_check_null_ret("con", con); - d2ds_service *service = (d2ds_service *)con->userdata; - d2ds_check_null_ret("service", service); - - if (!con) { - D2DS_LOGI("container is null"); - return; - } - - if (con->tid) { - g_source_remove(con->tid); - con->tid = 0; - } - con->sid = 0; - con->found = FALSE; - - if (con->pin) { - g_free(con->pin); - con->pin = NULL; - } - if (con->rsrc_uri) { - g_free(con->rsrc_uri); - con->rsrc_uri = NULL; - } - if (con->rsrc_type) { - g_free(con->rsrc_type); - con->rsrc_type = NULL; - } - if (con->rsrc_interface) { - g_free(con->rsrc_interface); - con->rsrc_interface = NULL; - } - if (con->uuid_target_str) { - g_free(con->uuid_target_str); - con->uuid_target_str = NULL; - } - if (con->uuid_dev1) { - g_free(con->uuid_dev1); - con->uuid_dev1 = NULL; - } - if (con->uuid_dev2) { - g_free(con->uuid_dev2); - con->uuid_dev2 = NULL; - } - if (con->target_1) { - g_free(con->target_1); - con->target_1 = NULL; - } - if (con->subject_1) { - g_free(con->subject_1); - con->subject_1 = NULL; - } - if (con->rsrc_uri_1) { - g_free(con->rsrc_uri_1); - con->rsrc_uri_1 = NULL; - } - if (con->rsrc_type_1) { - g_free(con->rsrc_type_1); - con->rsrc_type_1 = NULL; - } - if (con->rsrc_interface_1) { - g_free(con->rsrc_interface_1); - con->rsrc_interface_1 = NULL; - } - if (con->target_2) { - g_free(con->target_1); - con->target_1 = NULL; - } - if (con->subject_2) { - g_free(con->subject_1); - con->subject_1 = NULL; - } - if (con->rsrc_uri_2) { - g_free(con->rsrc_uri_2); - con->rsrc_uri_2 = NULL; - } - if (con->rsrc_type_2) { - g_free(con->rsrc_type_2); - con->rsrc_type_2 = NULL; - } - if (con->rsrc_interface_2) { - g_free(con->rsrc_interface_2); - con->rsrc_interface_2 = NULL; - } - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 0); - - if (con) { - g_free(con); - con = NULL; - } -} - -static void __notify_found_devs(void *data) -{ - GVariantBuilder builder; - GVariant* devices; - OCProvisionDev_t *iter = NULL; - - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - d2ds_check_null_ret("con", con); - d2ds_service *service= (d2ds_service *)con->userdata; - d2ds_check_null_ret("service", service); - - /* g_mowned_list would be filled above request */ - g_variant_builder_init(&builder, G_VARIANT_TYPE("aa{sv}")); - - if (D2DS_DISC_MOT_ENB_DEVS == con->cid) { - iter = g_client->g_motdev_list; - g_client->g_motdev_cnt = _print_dev_list(g_client->g_motdev_list); - D2DS_LOGD("Found %d MOT enabled devices", g_client->g_motdev_cnt); - } else if (D2DS_DISC_MOWNED_DEVS == con->cid) { - iter = g_client->g_mowned_list; - g_client->g_mowndev_cnt = _print_dev_list(g_client->g_mowned_list); - D2DS_LOGD("Found %d Mowned devices", g_client->g_mowndev_cnt); - } - - while (iter != NULL) { - char uuid_string[256] = {0}; - - OicUuid_t *uuid = &iter->doxm->deviceID; - d2ds_check_null_ret("uuid", uuid); - - snprintf(uuid_string, sizeof(uuid_string), - "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", - (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], - (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], - (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], - (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); - - D2DS_LOGI("uuid %s", uuid_string); - - g_variant_builder_open(&builder, G_VARIANT_TYPE_VARDICT); - - g_variant_builder_add(&builder, "{sv}", "deviceId", - g_variant_new_string(uuid_string)); - g_variant_builder_add(&builder, "{sv}", "adapter", - g_variant_new_uint32(iter->endpoint.adapter)); - g_variant_builder_add(&builder, "{sv}", "flags", - g_variant_new_int32(iter->endpoint.flags)); - g_variant_builder_add(&builder, "{sv}", "port", - g_variant_new_uint16(iter->endpoint.port)); - g_variant_builder_add(&builder, "{sv}", "addr", - g_variant_new_string(iter->endpoint.addr)); - g_variant_builder_add(&builder, "{sv}", "ifindex", - g_variant_new_int32(iter->endpoint.ifindex)); - g_variant_builder_add(&builder, "{sv}", "routeData", - g_variant_new_string(iter->endpoint.routeData)); - g_variant_builder_add(&builder, "{sv}", "remoteId", - g_variant_new_string(iter->endpoint.remoteId)); - - D2DS_LOGI("%s", iter->endpoint.remoteId); - - g_variant_builder_add(&builder, "{sv}", "connType", - g_variant_new_uint32(iter->connType)); - g_variant_builder_add(&builder, "{sv}", "securePort", - g_variant_new_uint16(iter->securePort)); -#ifdef WITH_TCP - g_variant_builder_add(&builder, "{sv}", "tcpPort", - g_variant_new_uint16(iter->tcpPort)); -#endif - g_variant_builder_add(&builder, "{sv}", "secVer", - g_variant_new_string(iter->secVer)); - g_variant_builder_add(&builder, "{sv}", "devStatus", - g_variant_new_uint32(iter->devStatus)); - g_variant_builder_close(&builder); - - iter = iter->next; - } - - devices = g_variant_builder_end(&builder); - - if (D2DS_DISC_MOT_ENB_DEVS == con->cid) - d2ds_notify_mot_env_devs(devices); - else if (D2DS_DISC_MOWNED_DEVS == con->cid) - d2ds_notify_mowned_devs(devices); - - g_variant_builder_unref(&builder); - g_variant_unref(devices); - -} -#ifdef TIMEOUT_USED -static gboolean _d2ds_timeout_cb(gpointer data) -{ - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - d2ds_check_null_ret_error("con", con, G_SOURCE_REMOVE); - - if (NULL == con) { - D2DS_LOGE("container is NULL"); - return G_SOURCE_REMOVE; - } - if (con->found) { - D2DS_LOGE("cb was alreay handled"); - return G_SOURCE_REMOVE; - } - - if(OC_STACK_OK != OCProcess()) { - D2DS_LOGE( "OCStack process error"); - return G_SOURCE_REMOVE; - } - - OCPDMCleanupForTimeout(); - - switch (con->cid) { - case D2DS_DISC_MOT_ENB_DEVS: - D2DS_LOGE("Discovered Multiple Ownership Transfer Enabled Devices"); - __notify_found_devs(con); - break; - case D2DS_DISC_MOWNED_DEVS: - D2DS_LOGE("Discovered Multiple Ownership Transfer Enabled Devices"); - __notify_found_devs(con); - break; - case D2DS_MOT: - net_d2ds_emit_mot_done(d2ds_dbus_get_object(), D2DS_ERROR_OPERATION_FAILED); - break; - case D2DS_PROV_ACL: - net_d2ds_emit_acl_done(d2ds_dbus_get_object(), D2DS_ERROR_OPERATION_FAILED); - break; - case D2DS_PROV_CRED: - net_d2ds_emit_cred_done(d2ds_dbus_get_object(), D2DS_ERROR_OPERATION_FAILED); - break; - case D2DS_REMOVE_MOT: - net_d2ds_emit_remove_mo_done(d2ds_dbus_get_object(), D2DS_ERROR_OPERATION_FAILED); - break; - case D2DS_UNPAIR: - net_d2ds_emit_unpair_done(d2ds_dbus_get_object(), D2DS_ERROR_OPERATION_FAILED); - break; - case D2DS_PROV_PAIRWISE: - net_d2ds_emit_pairwise_done(d2ds_dbus_get_object(), D2DS_ERROR_OPERATION_FAILED); - break; - } - - _request_cleanup((gpointer)con); - - return G_SOURCE_REMOVE; -} -#endif -static gpointer _disc_mot_env_devs_func(gpointer data) -{ - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - d2ds_check_null_ret_error("con", con, NULL); - - /* delete un/owned device lists before updating them */ - _remove_mot_client(); - - D2DS_LOGI("Discovering Multiple Ownership Transfer enabled Devices on Network.."); - if (OC_STACK_OK != OCDiscoverMultipleOwnerEnabledDevices(DISCOVERY_TIMEOUT, - &g_client->g_motdev_list)) { - D2DS_LOGE( "OCDiscoverMultipleOwnerEnalbedDevices API error"); - goto DISC_MOT_ENB_DEVS_END; - } - - __notify_found_devs(con); - -DISC_MOT_ENB_DEVS_END: - _request_cleanup(con); - g_thread_exit(GINT_TO_POINTER (1)); - - return NULL; -} - -static int _disc_mot_enb_devs(d2ds_service *service) -{ - d2ds_req_cb_s *con = NULL; - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (NULL == con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->thread = g_thread_try_new("disc_mot_env_devs", _disc_mot_env_devs_func, con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); - - con->userdata = service; - con->cid = D2DS_DISC_MOT_ENB_DEVS; -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(DISCOVERY_TIMEOUT * 2, _d2ds_timeout_cb, con); -#endif - return D2DS_ERROR_NONE; -} - -static gpointer _disc_owned_devs_func(gpointer data) -{ - OCStackResult ret = OC_STACK_OK; - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - - d2ds_check_null_ret_error("g_client", g_client, NULL); - - /* delete un/owned device lists before updating them */ - _remove_owned_client(); - - D2DS_LOGD("Discovering Multiple Owned Devices"); - ret = OCDiscoverMultipleOwnedDevices(DISCOVERY_TIMEOUT, &g_client->g_mowned_list); - if (OC_STACK_OK != ret) { - D2DS_LOGE( "OCDiscoverMultipleOwnerEnabledDevices API error"); - goto DISC_OWNED_DEVS_END; - } - - __notify_found_devs(con); - -DISC_OWNED_DEVS_END: - _request_cleanup(con); - g_thread_exit(GINT_TO_POINTER (1)); - - return NULL; -} - -static int _disc_owned_devs(d2ds_service *service) -{ - d2ds_req_cb_s *con = NULL; - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (NULL == con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->userdata = service; - con->cid = D2DS_DISC_MOWNED_DEVS; - - con->thread = g_thread_try_new("disc_owned_devs", _disc_owned_devs_func, con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(DISCOVERY_TIMEOUT * 2, _d2ds_timeout_cb, con); -#endif - return D2DS_ERROR_NONE; -} - -static void _mot_cb(void* ctx, int num, OCProvisionResult_t* arr, bool has_error) -{ - int i = 0; - int ret = OC_STACK_OK; - d2ds_subowner_s *client = (d2ds_subowner_s *)ctx; - - if (!has_error) { - D2DS_LOGD("Multiple Ownership Transfer SUCCEEDED"); - } else { - D2DS_LOGD( "Multiple Ownership Transfer FAILED "); - - _print_result_list((const OCProvisionResult_t*) arr, num); - - ret = openDB(); - if (OC_STACK_OK != ret) - D2DS_LOGE( "openDB: ret = %d (%s)", ret, _error_to_string(ret)); - - for ( ; num > i; ++i) { - if (OC_STACK_OK !=arr[i].res) { - ret = delete_mowned_device_db((const OicUuid_t*) &arr[i].deviceId); - } - } - ret = closeDB(); - if (OC_STACK_OK != ret) - D2DS_LOGE( "closeDB: ret = %d (%s)", ret, _error_to_string(ret)); - } - client->g_doneCB = true; -} - -static gpointer _mot_func(gpointer data) -{ - OCStackResult ret = OC_STACK_OK; - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - - OCProvisionDev_t *src_dev = NULL; - OicUuid_t *uuid_target = NULL; - - if (!con->uuid_target_str) { - D2DS_LOGE("Target device UUID is NULL"); - goto MOT_ENDED; - } - - uuid_target = _convert_uuid(con->uuid_target_str); - src_dev = _get_dev_by_uuid(g_client->g_motdev_list, uuid_target); - if (!src_dev) { - D2DS_LOGE("We can't find in MOT dev list"); - goto MOT_ENDED; - } - - g_client->g_doneCB = false; - - OCProvisionDev_t* dev = NULL; - LL_FOREACH(src_dev, dev) { - if(OIC_PRECONFIG_PIN == dev->doxm->oxmSel) { - /* Pre-Configured PIN initialization */ - const char* testPreconfigPin = con->pin;/* Ex) "12341234" */ - ret = OCAddPreconfigPin(dev, testPreconfigPin, strlen(testPreconfigPin)); - if(OC_STACK_OK != ret) { - D2DS_LOGE("Failed to save the pre-configured PIN"); - D2DS_LOGE("You can't use the pre-configured PIN OxM for MOT"); - goto MOT_ENDED; - } - } - } - - ret = OCDoMultipleOwnershipTransfer(g_client, src_dev, _mot_cb); - if (OC_STACK_OK != ret ) { - D2DS_LOGE( "OCDoMultipleOwnershipTransfer: ret = %d (%s)", ret, _error_to_string(ret)); - goto MOT_ENDED; - } - - ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); - if(ret) { - D2DS_LOGE( "OCDoMultipleOwnershipTransfer callback error = %d (%s)", ret, _error_to_string(ret)); - goto MOT_ENDED; - } - - D2DS_LOGE("Registered Discovered Devices"); - -MOT_ENDED: - - net_d2ds_emit_mot_done(d2ds_dbus_get_object(), (int)ret); - if (uuid_target) - g_free(uuid_target); - _request_cleanup(data); - g_thread_exit(GINT_TO_POINTER (1)); - - return NULL; -} - -static int _d2ds_mot(d2ds_service *service, gchar *uuid_str, gchar *pin) -{ - d2ds_req_cb_s *con = NULL; - - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (NULL == con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - /* Unset d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 0); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->userdata = service; - con->cid = D2DS_MOT; - con->uuid_target_str = g_strdup(uuid_str); - - if (pin) { - con->pin = g_malloc0(OXM_RANDOM_PIN_MAX_SIZE+1); - if (!pin) { - D2DS_LOGE("Failed to allocate PIN number"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - memcpy(con->pin, pin, OXM_RANDOM_PIN_MAX_SIZE); - } - - con->thread = g_thread_try_new("mot", _mot_func, con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); - -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _d2ds_timeout_cb, con); -#endif - return D2DS_ERROR_NONE; -} - -static void _remove_mo_cb(void* ctx, int num, OCProvisionResult_t* arr, bool has_error) -{ - d2ds_subowner_s *client = (d2ds_subowner_s *)ctx; - - if (!has_error) { - D2DS_LOGD("Remove Multiple Ownership SUCCEEDED"); - } else { - D2DS_LOGD( "Remove Multiple Ownership FAILED "); - _print_result_list((const OCProvisionResult_t*) arr, num); - } - client->g_doneCB = true; -} - -static gpointer _remove_mo_func(gpointer data) -{ - OCStackResult ret = D2DS_ERROR_OPERATION_FAILED; - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - - OCProvisionDev_t *src_dev = NULL; - OicUuid_t uuid_mowner; - OicUuid_t *uuid_target = NULL; - - if (!con->uuid_target_str) { - D2DS_LOGE("Target device UUID is NULL"); - goto MOT_ENDED; - } - - uuid_target = _convert_uuid(con->uuid_target_str); - src_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid_target); - if (!src_dev) { - D2DS_LOGE("We can't find in MOT dev list"); - goto MOT_ENDED; - } - - g_client->g_doneCB = false; - - ret = GetDoxmDevOwnerId(&uuid_mowner); - if (OC_STACK_OK != ret) { - D2DS_LOGE("GetDoxmDevOwnerId faild = [%d][%s]", ret, _error_to_string(ret)); - goto MOT_ENDED; - } - - ret = openDB(); - if (OC_STACK_OK != ret) - D2DS_LOGE( "openDB: ret = %d (%s)", ret, _error_to_string(ret)); - ret = delete_mowned_device_db((const OicUuid_t*) uuid_target); - if (OC_STACK_OK != ret) - D2DS_LOGE( "delete_mowned_device_db: ret = %d (%s)", ret, _error_to_string(ret)); - ret = closeDB(); - if (OC_STACK_OK != ret) - D2DS_LOGE( "closeDB: ret = %d (%s)", ret, _error_to_string(ret)); - - ret = OCRemoveSubOwner(g_client, src_dev, &uuid_mowner, _remove_mo_cb); - if (OC_STACK_OK != ret ) { - D2DS_LOGE( "OCRemoveSubOwner: ret = %d (%s)", ret, _error_to_string(ret)); - goto MOT_ENDED; - } - - ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); - if(ret) { - D2DS_LOGE( "OCRemoveSubOwner callback error = %d (%s)", ret, _error_to_string(ret)); - goto MOT_ENDED; - } - - D2DS_LOGE("Remove Multiple Ownership Done"); - -MOT_ENDED: - - net_d2ds_emit_remove_mo_done(d2ds_dbus_get_object(), (int)ret); - - if (uuid_target) - g_free(uuid_target); - _request_cleanup(data); - g_thread_exit(GINT_TO_POINTER (1)); - - return NULL; -} - -static int _d2ds_remove_mo(d2ds_service *service, gchar *uuid_str) -{ - d2ds_req_cb_s *con = NULL; - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (NULL == con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - /* Unset d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 0); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->userdata = service; - con->cid = D2DS_REMOVE_MOT; - con->uuid_target_str = g_strdup(uuid_str); - - con->thread = g_thread_try_new("remove_mo", _remove_mo_func, con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); - -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _d2ds_timeout_cb, con); -#endif - return D2DS_ERROR_NONE; -} - -#if 0 /* TEST */ -static int sendGetLed() -{ - int selDevNum; - char query[256] = {0}; - OCCallbackData cbData; - cbData.cb = &LedCB; - cbData.context = NULL; - cbData.cd = NULL; - - printDevList(g_mowned_list); - - // select device for provisioning access control list - for( ; ; ) { - printf(" > Enter Device Number, for sending GET LED request: "); - for(int ret=0; 1!=ret; ) { - ret = scanf("%d", &selDevNum); - for( ; 0x20<=getchar(); ); // for removing overflow garbages - // '0x20<=code' is character region - } - if(0=selDevNum) { - break; - } - printf(" Entered Wrong Number. Please Enter Again\n"); - } - - OCProvisionDev_t* selDev = _get_dev_by_id(g_mowned_list, selDevNum); - if(NULL == selDev) { - printf("Failed to _get_dev_by_id()\n"); - return -1; - } - - if(PMGenerateQuery(true, selDev->endpoint.addr, selDev->securePort, selDev->connType, - query, sizeof(query), "/a/led")) { - g_doneCB = false; - printf("query=%s\n", query); - if(OC_STACK_OK != OCDoResource(NULL, OC_REST_GET, query, NULL, NULL, selDev->connType, - OC_HIGH_QOS, &cbData, NULL, 0)) { - printf("********************************\n"); - printf("Failed to send GET request to %s\n", query); - printf("********************************\n"); - g_doneCB = true; - return -1; - } - - waitCallbackRet(); - } else { - printf("Failed to generate GET request for /a/led\n"); - return -1; - } - - return 0; -} - -static int sendPutLed() -{ - int selDevNum; - char query[256] = {0}; - OCCallbackData cbData; - cbData.cb = &LedCB; - cbData.context = NULL; - cbData.cd = NULL; - - printDevList(g_mowned_list); - // select device for provisioning access control list - for( ; ; ) { - printf(" > Enter Device Number, for sending PUT LED request: "); - for(int ret=0; 1!=ret; ) { - ret = scanf("%d", &selDevNum); - for( ; 0x20<=getchar(); ); // for removing overflow garbages - // '0x20<=code' is character region - } - if(0=selDevNum) { - break; - } - printf(" Entered Wrong Number. Please Enter Again\n"); - } - - OCProvisionDev_t* selDev = _get_dev_by_id(g_mowned_list, selDevNum); - if (NULL == selDev) { - printf("Failed to _get_dev_by_id()\n"); - return -1; - } - - if (PMGenerateQuery(true, selDev->endpoint.addr, selDev->securePort, selDev->connType, query, - sizeof(query), "/a/led")) { - g_doneCB = false; - D2DS_LOGI("query=%s\n", query); - if(OC_STACK_OK != OCDoResource(NULL, OC_REST_PUT, query, NULL, NULL, selDev->connType, - OC_LOW_QOS, &cbData, NULL, 0)) { - D2DS_LOGI("********************************\n"); - D2DS_LOGI("Failed to send PUT request to %s\n", query); - D2DS_LOGI("********************************\n"); - g_doneCB = true; - return -1; - } - - waitCallbackRet(); - } else { - printf("Failed to generate PUT request for /a/led\n"); - return -1; - } - - return 0; -} -#endif /* TEST */ -static OicSecAcl_t* _create_target_acl(const OicUuid_t* target, const OicUuid_t* subject, - const char *rsrc_uri, const char *rsrc_type, const char *rsrc_interface, int permission) -{ - /* For example - * subejct = "12341234-1234-1234-12341234" - * rsrc_uri = "/a/led" - * rsrc_type = "oic.r.core" - * rsrc_interface = "oic.if.baseline" - * rsrc_permission = D2DS_PERMISSION_FULL - */ - size_t len; - OicSecAcl_t* acl = NULL; - OicSecAce_t* ace = NULL; - OicSecRsrc_t* rsrc = NULL; - - if (NULL == target) { - D2DS_LOGE( "target is NULL"); - return NULL; - } - - if (NULL == subject) { - D2DS_LOGE( "subejct is NULL"); - return NULL; - } -#ifdef DEBUG_UUID - _print_uuid(target); - _print_uuid(subject); -#endif - acl = (OicSecAcl_t*) OICCalloc(1, sizeof(OicSecAcl_t)); - if (!acl) { - D2DS_LOGE( "acl : OICCalloc failed =%d", errno); - return NULL; - } - ace = (OicSecAce_t*) OICCalloc(1, sizeof(OicSecAce_t)); - if (!ace) { - D2DS_LOGE( "ace : OICCalloc failed =%d", errno); - return NULL; - } - LL_APPEND(acl->aces, ace); - memcpy(ace->subjectuuid.id, subject->id, sizeof(subject->id)); - - /* fill the href */ - rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t)); - if (!rsrc) { - D2DS_LOGE( "rsrc: OICCalloc failed =%d", errno); - goto CREATE_ACL_ERROR; - } - - len = strlen(rsrc_uri)+1; // '1' for null termination - rsrc->href = (char*) OICCalloc(len, sizeof(char)); - if (!rsrc->href) { - D2DS_LOGE( "rsrc->href: OICCalloc failed =%d", errno); - goto CREATE_ACL_ERROR; - } - memcpy(rsrc->href, rsrc_uri, len); - - /* Fill the resource type (rt) */ - rsrc->typeLen = 1; - rsrc->types = (char**)OICCalloc(1, sizeof(char*)); - if (!rsrc->types) { - D2DS_LOGE( "rsrc->types: OICCalloc failed =%d", errno); - goto CREATE_ACL_ERROR; - } - rsrc->types[0] = g_strdup(rsrc_type); - if (!rsrc->types[0]) { - D2DS_LOGE( "rsrc->types[0]: g_strdup failed =%d", errno); - goto CREATE_ACL_ERROR; - } - - /* Fill the interface (if) */ - rsrc->interfaceLen = 1; - rsrc->interfaces = (char**)OICCalloc(1, sizeof(char*)); - if (!rsrc->interfaces) { - D2DS_LOGE( "rsrc->interfaces: OICCalloc failed =%d", errno); - goto CREATE_ACL_ERROR; - } - rsrc->interfaces[0] = g_strdup(rsrc_interface); - if (!rsrc->interfaces[0]) { - D2DS_LOGE( "rsrc->interfaces[0] : g_strdup failed =%d", errno); - goto CREATE_ACL_ERROR; - } - - LL_APPEND(ace->resources, rsrc); - - /* Fill permission for the reource */ - ace->permission = permission; - - ace->eownerID = (OicUuid_t*)OICCalloc(1, sizeof(OicUuid_t)); - if (NULL == ace->eownerID) { - D2DS_LOGE( "ace->eownerID : OICCalloc failed =%d", errno); - goto CREATE_ACL_ERROR; - } - memcpy(ace->eownerID->id, subject->id, sizeof(subject->id)); - - return acl; - -CREATE_ACL_ERROR: - OCDeleteACLList(acl); - return NULL; -} - -static void _provisioning_acl_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) -{ - d2ds_subowner_s *client = (d2ds_subowner_s *)ctx; - - if (!has_error) { - D2DS_LOGD("Provision ACL SUCCEEDED"); - } else { - D2DS_LOGD( "Provision ACL FAILED "); - _print_result_list((const OCProvisionResult_t*) arr, nOfRes); - } - client->g_doneCB = true; -} - -static void _provisioning_pairwise_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) -{ - d2ds_subowner_s *client = (d2ds_subowner_s *)ctx; - - if (!has_error) { - D2DS_LOGD("Provision pairwise SUCCEEDED"); - } else { - D2DS_LOGD( "Provision pairwise FAILED "); - _print_result_list((const OCProvisionResult_t*) arr, nOfRes); - } - client->g_doneCB = true; -} - -static void _unpair_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) -{ - d2ds_subowner_s *client = (d2ds_subowner_s *)ctx; - - if (!has_error) { - D2DS_LOGD("unpair SUCCEEDED"); - } else { - D2DS_LOGD( "unpair FAILED "); - _print_result_list((const OCProvisionResult_t*) arr, nOfRes); - } - client->g_doneCB = true; -} - -static gpointer _provisioning_acl_func(gpointer data) -{ - OCStackResult ret = OC_STACK_OK; - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - OicSecAcl_t* acl = NULL; - OicUuid_t *uuid = NULL; - OCProvisionDev_t *target_dev = NULL; - OCProvisionDev_t *subject_dev = NULL; - - if (!con->uuid_subject_str ||!con->uuid_target_str || !con->rsrc_uri || !con->rsrc_type || !con->rsrc_interface) { - D2DS_LOGE("Some parameters are wrong"); - D2DS_LOGE("%s", con->uuid_target_str ); - D2DS_LOGE("%s", con->uuid_subject_str ); - D2DS_LOGE("%s", con->rsrc_uri ); - D2DS_LOGE("%s", con->rsrc_type ); - D2DS_LOGE("%s", con->rsrc_interface ); - goto PVACL_ERROR; - } - - uuid = _convert_uuid(con->uuid_target_str); - target_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid); - if (!target_dev) { - D2DS_LOGE("We can't find target in MOWNED dev list"); - goto PVACL_ERROR; - } - g_free(uuid); - - uuid = _convert_uuid(con->uuid_subject_str); - subject_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid); - if (!subject_dev) { - D2DS_LOGE("We can't find subject in MOWNED dev list"); - goto PVACL_ERROR; - } - g_free(uuid); - - g_client->g_doneCB = false; - D2DS_LOGI(" Provisioning Selected ACL.."); - - acl = _create_target_acl(&target_dev->doxm->deviceID, &subject_dev->doxm->deviceID, con->rsrc_uri, con->rsrc_type, - con->rsrc_interface, con->permission); - if (NULL == acl) { - D2DS_LOGE( "Failed to create ACL for %s", con->rsrc_uri); - goto PVACL_ERROR; - } - - ret= OCProvisionACL((void*) g_client, target_dev, acl, _provisioning_acl_cb); - if (OC_STACK_OK != ret) { - D2DS_LOGD( "OCProvisionACL API error: %d (%s)", ret, _error_to_string(ret)); - goto PVACL_ERROR; - } - - ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); - if (ret) { - D2DS_LOGE( "OCProvisionCredentials Faild = %d", errno); - goto PVACL_ERROR; - } - /* display the ACL-provisioned result */ - D2DS_LOGI(" Provisioned Selected ACL Successfully"); - -PVACL_ERROR: - /* Notify ACL result */ - net_d2ds_emit_acl_done(d2ds_dbus_get_object(), (int)ret); - - if (acl) - OCDeleteACLList(acl); - if (uuid) - g_free(uuid); - _request_cleanup(con); - g_thread_exit(GINT_TO_POINTER (1)); - return NULL; -} - -static int _provisioning_acl(d2ds_service *service, gchar *target, gchar *subject, - gchar *rsrc_uri, gchar *rsrc_type, gchar *rsrc_interface, int permission) -{ - d2ds_req_cb_s *con = NULL; - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (NULL == con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - /* Unset d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 0); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->userdata = service; - con->cid = D2DS_PROV_ACL; - con->uuid_target_str = g_strdup(target); - con->uuid_subject_str = g_strdup(subject); - con->rsrc_uri = g_strdup(rsrc_uri); - con->rsrc_type = g_strdup(rsrc_type); - con->rsrc_interface = g_strdup(rsrc_interface); - - con->thread = g_thread_try_new("prov_acl", _provisioning_acl_func, con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _d2ds_timeout_cb, con); -#endif - return D2DS_ERROR_NONE; -} - -static gpointer _provisioning_pairwise_func(gpointer data) -{ - OCStackResult ret = OC_STACK_OK; - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - - OicUuid_t *uuid_1 = NULL; - OicUuid_t *uuid_2 = NULL; - OicUuid_t *uuid_3 = NULL; - OicUuid_t *uuid_4 = NULL; - - OicSecAcl_t* acl_1 = NULL; - OicSecAcl_t* acl_2 = NULL; - - OCProvisionDev_t *target_dev_1 = NULL; - OCProvisionDev_t *target_dev_2 = NULL; - OCProvisionDev_t *subject_dev_1 = NULL; - OCProvisionDev_t *subject_dev_2 = NULL; - - if (!con->subject_1 || !con->rsrc_uri_1 || !con->rsrc_type_1 || !con->rsrc_interface_1 || - !con->subject_2 || !con->rsrc_uri_2 || !con->rsrc_type_2 || !con->rsrc_interface_2) { - D2DS_LOGE("Some parameters are wrong"); - D2DS_LOGE("%s", con->subject_1 ); - D2DS_LOGE("%s", con->rsrc_uri_1 ); - D2DS_LOGE("%s", con->rsrc_type_1 ); - D2DS_LOGE("%s", con->rsrc_interface_1 ); - D2DS_LOGE("%s", con->subject_2 ); - D2DS_LOGE("%s", con->rsrc_uri_2 ); - D2DS_LOGE("%s", con->rsrc_type_2 ); - D2DS_LOGE("%s", con->rsrc_interface_2 ); - - goto PVACL_ERROR; - } - - uuid_1 = _convert_uuid(con->target_1); - target_dev_1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_1); - if (!target_dev_1) { - D2DS_LOGE("We can't find target in MOWNED dev list"); - goto PVACL_ERROR; - } - - uuid_2 = _convert_uuid(con->target_2); - target_dev_2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_2); - if (!target_dev_2) { - D2DS_LOGE("We can't find target in MOWNED dev list"); - goto PVACL_ERROR; - } - - uuid_3 = _convert_uuid(con->subject_1); - subject_dev_1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_3); - if (!subject_dev_1) { - D2DS_LOGE("We can't find subject in MOWNED dev list"); - goto PVACL_ERROR; - } - - uuid_4 = _convert_uuid(con->subject_2); - subject_dev_2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_4); - if (!subject_dev_2) { - D2DS_LOGE("We can't find subject in MOWNED dev list"); - goto PVACL_ERROR; - } - - g_client->g_doneCB = false; - D2DS_LOGI(" Provisioning Selected Pairwise.."); - - acl_1 = _create_target_acl(&target_dev_1->doxm->deviceID, - &subject_dev_1->doxm->deviceID, con->rsrc_uri_1, - con->rsrc_type_1, - con->rsrc_interface_1, con->permission_1); - if (NULL == acl_1) { - D2DS_LOGE( "Failed to create ACL for %s", con->rsrc_uri); - goto PVACL_ERROR; - } - - acl_2 = _create_target_acl(&target_dev_2->doxm->deviceID, - &subject_dev_2->doxm->deviceID, con->rsrc_uri_2, - con->rsrc_type_2, - con->rsrc_interface_2, con->permission_2); - if (NULL == acl_2) { - D2DS_LOGE( "Failed to create ACL for %s", con->rsrc_uri); - goto PVACL_ERROR; - } - - ret = OCProvisionPairwiseDevices((void*) g_client, SYMMETRIC_PAIR_WISE_KEY, - OWNER_PSK_LENGTH_256, target_dev_1, acl_1, - target_dev_2, acl_2, _provisioning_pairwise_cb); - if (OC_STACK_OK != ret) { - D2DS_LOGD( "OCProvisionPairwiseDevices API error: %d (%s)", ret, - _error_to_string(ret)); - goto PVACL_ERROR; - } - - ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); - if (ret) { - D2DS_LOGE( "OCProvisionPairwise Faild = %d", errno); - goto PVACL_ERROR; - } - /* display the pairwise-provisioned result */ - D2DS_LOGI(" Provision pairwise Successfully"); - -PVACL_ERROR: - /* Notify ACL result */ - net_d2ds_emit_pairwise_done(d2ds_dbus_get_object(), (int)ret); - - if (uuid_1) - g_free(uuid_1); - if (uuid_2) - g_free(uuid_2); - if (uuid_3) - g_free(uuid_3); - if (uuid_4) - g_free(uuid_4); - if (acl_1) - OCDeleteACLList(acl_1); - if (acl_2) - OCDeleteACLList(acl_2); - - _request_cleanup(con); - g_thread_exit(GINT_TO_POINTER (1)); - return NULL; -} - -static gpointer _unpair_func(gpointer data) -{ - OCStackResult ret = OC_STACK_OK; - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - - OicUuid_t *uuid = NULL; - - if (!con->target_to_revoke) { - D2DS_LOGE("Some parameters are wrong"); - D2DS_LOGE("%s", con->target_to_revoke ); - goto PVACL_ERROR; - } - - uuid = _convert_uuid(con->target_to_revoke); - - g_client->g_doneCB = false; - D2DS_LOGI(" unpair device.."); - - ret = OCRemoveDeviceWithUuid((void*) g_client, CALLBACK_TIMEOUT_5S, uuid, _unpair_cb ); - if (OC_STACK_OK != ret) { - D2DS_LOGD( "OCRemoveDevice API error: %d (%s)", ret, _error_to_string(ret)); - goto PVACL_ERROR; - } - - ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); - if (ret) { - D2DS_LOGE( "OCRemoveDevice Faild = %d", errno); - goto PVACL_ERROR; - } - /* display the pairwise-provisioned result */ - D2DS_LOGI(" Provision pairwise Successfully"); - -PVACL_ERROR: - /* Notify ACL result */ - net_d2ds_emit_unpair_done(d2ds_dbus_get_object(), (int)ret); - if (uuid) - g_free(uuid); - _request_cleanup(con); - g_thread_exit(GINT_TO_POINTER (1)); - return NULL; -} - -static int _provisioning_unpair(d2ds_service *service, gchar *target) -{ - d2ds_req_cb_s *con = NULL; - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (NULL == con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - /* Unset d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 0); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->userdata = service; - con->cid = D2DS_UNPAIR; - - con->target_to_revoke = g_strdup(target); - - con->thread = g_thread_try_new("unpair_device", _unpair_func, con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _d2ds_timeout_cb, con); -#endif - return D2DS_ERROR_NONE; -} - -static int _provisioning_pairwise_link(d2ds_service *service, gchar *target_1, gchar *subject_1, - gchar *rsrc_uri_1, gchar *rsrc_type_1, gchar *rsrc_interface_1, int permission_1, - gchar *target_2, gchar *subject_2,gchar *rsrc_uri_2, gchar *rsrc_type_2, gchar *rsrc_interface_2, int permission_2) -{ - d2ds_req_cb_s *con = NULL; - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (NULL == con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - /* Unset d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 0); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->userdata = service; - con->cid = D2DS_PROV_PAIRWISE; - - con->target_1 = g_strdup(target_1); - con->subject_1 = g_strdup(subject_1); - con->rsrc_uri_1 = g_strdup(rsrc_uri_1); - con->rsrc_type_1 = g_strdup(rsrc_type_1); - con->rsrc_interface_1 = g_strdup(rsrc_interface_1); - - con->target_2 = g_strdup(target_2); - con->subject_2 = g_strdup(subject_2); - con->rsrc_uri_2 = g_strdup(rsrc_uri_2); - con->rsrc_type_2 = g_strdup(rsrc_type_2); - con->rsrc_interface_2 = g_strdup(rsrc_interface_2); - - con->thread = g_thread_try_new("prov_pairwise", _provisioning_pairwise_func, - con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _d2ds_timeout_cb, - con); -#endif - return D2DS_ERROR_NONE; -} - - -static void _provision_cred_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) -{ - d2ds_subowner_s *client = (d2ds_subowner_s *)ctx; - - if (!has_error) { - D2DS_LOGD("Provision ACL SUCCEEDED"); - } else { - D2DS_LOGD( "Provision ACL FAILED "); - _print_result_list((const OCProvisionResult_t*) arr, nOfRes); - } - client->g_doneCB = true; -} - -static gpointer _cred_provisioning_func(gpointer data) -{ - OCStackResult ret = OC_STACK_OK; - d2ds_req_cb_s *con = (d2ds_req_cb_s *)data; - - OicUuid_t *uuid = NULL; - OCProvisionDev_t *dev1 = NULL; - OCProvisionDev_t *dev2 = NULL; - - if (!con->uuid_dev1 ) { - D2DS_LOGE("dev1 is NULL"); - goto PVPWS_ERROR; - } - if (!con->uuid_dev2) { - D2DS_LOGE("dev2 is NULL"); - goto PVPWS_ERROR; - } - - uuid = _convert_uuid(con->uuid_dev1); - dev1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid); - if (!dev1) { - D2DS_LOGE("We can't find in MOWNED dev list"); - goto PVPWS_ERROR; - } - g_free(uuid); - - uuid = _convert_uuid(con->uuid_dev2); - dev2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid); - if (!dev2) { - D2DS_LOGE("We can't find in MOWNED dev list"); - goto PVPWS_ERROR; - } - g_free(uuid); - - /* To do ACL provisioning only one */ - dev1->next = NULL; - - /* To do ACL provisioning only one */ - dev2->next = NULL; - - g_client->g_doneCB = false; - - D2DS_LOGI("Provisioning Selected Pairwise Devices.."); - ret = OCProvisionCredentials((void*) g_client, - SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_256, - dev1, dev2, _provision_cred_cb); - if (OC_STACK_OK != ret) { - D2DS_LOGD( "OCProvisionPairwiseDevices API error: %d (%s)", ret, _error_to_string(ret)); - goto PVPWS_ERROR; - } - - ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); - if (ret) { - D2DS_LOGE( "OCProvisionCredentials Faild = %d", errno); - goto PVPWS_ERROR; - } - - /* display the pairwise-provisioned result */ - D2DS_LOGI("Provisioned Selected Pairwise Devices"); - - return 0; - -PVPWS_ERROR: - /* Notify ACL result */ - net_d2ds_emit_cred_done(d2ds_dbus_get_object(), (int)ret); - - if (uuid) - g_free(uuid); - _request_cleanup(con); - - g_thread_exit(GINT_TO_POINTER (1)); - - return NULL; -} - -static int _provisioning_cred(d2ds_service *service, - gchar *uuid_dev1, gchar *uuid_dev2) -{ - d2ds_req_cb_s *con = NULL; - con = g_malloc0(sizeof(d2ds_req_cb_s)); - if (!con) { - D2DS_LOGE( "g_malloc0() Fail=%d", errno); - /* Unset d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 0); - return D2DS_ERROR_OUT_OF_MEMORY; - } - - con->userdata = service; - con->cid = D2DS_PROV_CRED; - - if (!uuid_dev1 ||!uuid_dev2) { - D2DS_LOGE( "Invaild Prameters"); - _request_cleanup(con); - return D2DS_ERROR_INVALID_PARAMETER; - } - - con->uuid_dev1 = g_strdup(uuid_dev1); - con->uuid_dev2 = g_strdup(uuid_dev2); - - con->thread = g_thread_try_new("prov_cred", _cred_provisioning_func, con, NULL); - if (!con->thread) { - D2DS_LOGE("Failed to create thread"); - _request_cleanup(con); - return D2DS_ERROR_OUT_OF_MEMORY; - } - g_thread_unref(con->thread); -#ifdef TIMEOUT_USED - con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _d2ds_timeout_cb, con); -#endif - return D2DS_ERROR_NONE; -} - -int d2ds_request_enable(d2ds_service *service) -{ - int ret = D2DS_ERROR_NONE; - d2ds_check_null_ret_error("service", service, FALSE); - ret = _init_provision_client(); - return ret; -} - -int d2ds_request_disc_mot_enb_devs(d2ds_service *service) -{ - int ret =D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Discovery MOT enabled devices"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _disc_mot_enb_devs(service); - - return ret; -} - -int d2ds_request_disc_owned_devs(d2ds_service *service) -{ - int ret =D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Discovery Owned devices"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _disc_owned_devs(service); - - return ret; -} - -int d2ds_request_mot(d2ds_service *service, gchar* uuid_str, gchar *pin) -{ - int ret =D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Mullti Ownership Transfer"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - if (!uuid_str || !pin) { - D2DS_LOGE("uuid_str = %s", uuid_str); - D2DS_LOGE("pin = %s", pin); - return D2DS_ERROR_INVALID_PARAMETER; - } - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _d2ds_mot(service, uuid_str, pin); - - return ret; -} - -int d2ds_request_remove_mo(d2ds_service *service, gchar* uuid_str) -{ - int ret =D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Remove Mulltiple Ownership"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - if (!uuid_str) { - D2DS_LOGE("uuid_str = %s", uuid_str); - return D2DS_ERROR_INVALID_PARAMETER; - } - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _d2ds_remove_mo(service, uuid_str); - - return ret; -} - -int d2ds_request_prov_acl(d2ds_service *service, gchar *target, gchar *subject, - gchar *rsrc_uri, gchar *rsrc_type, gchar *rsrc_interface, int permission) -{ - int ret =D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Provisioning ACL"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - if (!target || !subject || !rsrc_uri || !rsrc_type || !rsrc_interface) { - D2DS_LOGE("target = %s", target); - D2DS_LOGE("subject = %s", subject); - D2DS_LOGE("rsrc_uri = %s", rsrc_uri); - D2DS_LOGE("rsrc_type = %s", rsrc_type); - D2DS_LOGE("rsrc_interface = %s", rsrc_interface); - return D2DS_ERROR_INVALID_PARAMETER; - } - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _provisioning_acl(service, target, subject, rsrc_uri, rsrc_type, rsrc_interface, permission); - - return ret; -} - -int d2ds_request_prov_cred(d2ds_service *service, gchar *uuid_dev1, gchar *uuid_dev2) -{ - int ret =D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Provisioning Credential"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - if (!uuid_dev1 || !uuid_dev2) { - D2DS_LOGE("uuid_dev1 = %s", uuid_dev1); - D2DS_LOGE("uuid_dev2 = %s", uuid_dev2); - return D2DS_ERROR_INVALID_PARAMETER; - } - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _provisioning_cred(service, uuid_dev1, uuid_dev2); - - return ret; -} - -int d2ds_request_get_ownerid(d2ds_service *service, gchar **uuid_str) -{ - int ret =D2DS_ERROR_NONE; - OicUuid_t uuid; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Get Device ID"); - - ret = GetDoxmDevOwnerId(&uuid); - if (OC_STACK_OK != ret) - D2DS_LOGE("GetDoxmDevOwnerId faild = [%d][%s]", ret, _error_to_string(ret)); - - ret = ConvertUuidToStr(&uuid, uuid_str); - if (OC_STACK_OK != ret) - D2DS_LOGE("ConvertUuidToStr faild = [%d][%s]", ret, _error_to_string(ret)); - - return ret; -} - -int d2ds_request_disable(d2ds_service *service) -{ - NOTUSED(service); - - if (OC_STACK_OK != OCStop()) { - D2DS_LOGE( "OCStack stop error"); - } - - OCTerminatePM(); - - if (!g_client) - return D2DS_ERROR_NONE; - - if (g_client->g_mowned_list) - OCDeleteDiscoveredDevices(g_client->g_mowned_list); - if (g_client->g_motdev_list) - OCDeleteDiscoveredDevices(g_client->g_motdev_list); - - if (g_client->rnd_pin) { - g_free(g_client->rnd_pin); - g_client->rnd_pin = NULL; - } - - g_free(g_client); - g_client = NULL; - - return D2DS_ERROR_NONE; -} - -int d2ds_request_pairwise(d2ds_service *service, gchar *target_1, - gchar *subject_1, gchar *rsrc_uri_1, gchar *rsrc_type_1, - gchar *rsrc_interface_1, int permission_1, - gchar *target_2, gchar *subject_2,gchar *rsrc_uri_2, - gchar *rsrc_type_2, gchar *rsrc_interface_2, - int permission_2) -{ - int ret =D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Link Pairwise"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - if (!target_1 || !subject_1 || !rsrc_uri_1 || !rsrc_type_1 || - !rsrc_interface_1 || !target_2 || !subject_2 || !rsrc_uri_2 || - !rsrc_type_2 || !rsrc_interface_2) { - D2DS_LOGE("target_1 = %s", target_1); - D2DS_LOGE("subject_1 = %s", subject_1); - D2DS_LOGE("rsrc_uri_1 = %s", rsrc_uri_1); - D2DS_LOGE("rsrc_type_1 = %s", rsrc_type_1); - D2DS_LOGE("rsrc_interface_1 = %s", rsrc_interface_1); - D2DS_LOGE("target_2 = %s", target_2); - D2DS_LOGE("subject_2 = %s", subject_2); - D2DS_LOGE("rsrc_uri_2 = %s", rsrc_uri_2); - D2DS_LOGE("rsrc_type_2 = %s", rsrc_type_2); - D2DS_LOGE("rsrc_interface_2 = %s", rsrc_interface_2); - return D2DS_ERROR_INVALID_PARAMETER; - } - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _provisioning_pairwise_link(service, target_1, subject_1, rsrc_uri_1, - rsrc_type_1, rsrc_interface_1, permission_1, target_2, - subject_2, rsrc_uri_2, rsrc_type_2, rsrc_interface_2, - permission_2); - - return ret; - -} - -int d2ds_request_unpair(d2ds_service *service, gchar *uuid_dev) -{ - int ret = D2DS_ERROR_NONE; - - d2ds_check_null_ret_error("service", service, FALSE); - - D2DS_LOGD("[IPC] Unpair device"); - - /* If we are working now? */ - if (g_atomic_int_get(&service->pending)) - return D2DS_ERROR_IN_PROGRESS; - - if (!uuid_dev) { - D2DS_LOGE("uuid_dev = %s", uuid_dev); - - return D2DS_ERROR_INVALID_PARAMETER; - } - - /* Set d2ds status 'pending' */ - g_atomic_int_set(&service->pending, 1); - - ret = _provisioning_unpair(service, uuid_dev); - - return ret; - -} - diff --git a/src/d2d-subowner/d2ds-subowner.h b/src/d2d-subowner/d2ds-subowner.h deleted file mode 100644 index aa5829b..0000000 --- a/src/d2d-subowner/d2ds-subowner.h +++ /dev/null @@ -1,69 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DS_SUBONWER_H__ -#define __D2DS_SUBONWER_H__ - -#include "d2ds.h" - -#ifdef __cplusplus -extern "C" { -#endif - -/* command id */ -typedef enum { - D2DS_DISC_MOT_ENB_DEVS = 0, /**< Discover MOT enabled devices */ - D2DS_DISC_MOWNED_DEVS, /**< Discover my owned MOT enabled devices */ - D2DS_MOT, /**< Do Multiple Onwership Transfer */ - D2DS_PROV_ACL, /**< ACL Provisioning */ - D2DS_PROV_CRED, /**< CRED Provisioning */ - D2DS_REMOVE_MOT, /**< Remove Multiple Ownership */ - D2DS_UNPAIR, /**< Unpair */ - D2DS_PROV_PAIRWISE, /** < Pairwise Provisioning */ -} d2ds_cmd_id_e; - -/* ACL Permission type */ -typedef enum { - D2DS_PERMISSION_CREATE = (1 << 0), /**< C */ - D2DS_PERMISSION_READ = (1 << 1), /**< R */ - D2DS_PERMISSION_WRITE = (1 << 2), /**< U */ - D2DS_PERMISSION_DELETE = (1 << 3), /**< D */ - D2DS_PERMISSION_NOTIFY = (1 << 4), /**< N */ - D2DS_PERMISSION_FULL = 0x11111 /**< CRUDN */ -} d2ds_permission_e; - -int d2ds_request_enable(d2ds_service *service); -int d2ds_request_disc_mot_enb_devs(d2ds_service *service); -int d2ds_request_disc_owned_devs(d2ds_service *service); -int d2ds_request_mot(d2ds_service *service, gchar* uuid, gchar *pin); -int d2ds_request_remove_mo(d2ds_service *service, gchar* uuid_str); -int d2ds_request_prov_acl(d2ds_service *service, gchar *target, gchar *subject, - gchar *rsrc_uri, gchar *rsrc_type, gchar *rsrc_interface, int permission); -int d2ds_request_prov_cred(d2ds_service *service, gchar *uuid_dev1, gchar *uuid_dev2); -int d2ds_request_unpair(d2ds_service *service, gchar *uuid_dev); -int d2ds_request_get_ownerid(d2ds_service *service, gchar **uuid_str); -int d2ds_request_disable(d2ds_service *service); -int d2ds_request_pairwise(d2ds_service *service, gchar *target_1, gchar *subject_1, - gchar *rsrc_uri_1, gchar *rsrc_type_1, gchar *rsrc_interface_1, int permission_1, - gchar *target_2, gchar *subject_2,gchar *rsrc_uri_2, gchar *rsrc_type_2, gchar *rsrc_interface_2, int permission_2); - -#ifdef __cplusplus -} -#endif - -#endif /* __D2DS_SUBONWER_H__ */ diff --git a/src/d2d-subowner/d2ds-util.h b/src/d2d-subowner/d2ds-util.h deleted file mode 100644 index 3418b44..0000000 --- a/src/d2d-subowner/d2ds-util.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2DS_UTIL_H__ -#define __D2DS_UTIL_H__ - -#include -#include -#include - -#include "d2ds-log.h" - -#define NOTUSED(var) (var = var) - -#define d2ds_check_null_ret_error(name, value, error) do { \ - if (G_UNLIKELY(NULL == (value))) { \ - D2DS_LOGE("%s is NULL", name); \ - return error; \ - } \ -} while (FALSE) - -#define d2ds_check_null_ret(name, value) do { \ - if (G_UNLIKELY(NULL == (value))) { \ - D2DS_LOGE("%s is NULL", name); \ - return; \ - } \ -} while (FALSE) - -#endif /* __D2DS_UTIL_H__ */ diff --git a/src/d2d-subowner/d2ds.c b/src/d2d-subowner/d2ds.c deleted file mode 100644 index 1aa8429..0000000 --- a/src/d2d-subowner/d2ds.c +++ /dev/null @@ -1,81 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#include -#include -#include - -#include - -#include "d2ds.h" -#include "d2ds-log.h" -#include "d2ds-util.h" -#include "d2ds-service.h" -#include "d2ds-service-interface.h" - -/**< d2d-subowner service */ -d2ds_service *d2dssvc; - -int main(int argc, char *argv[]) -{ - d2ds_service *service = NULL; - int ret_code = 0; - gboolean ret; - - __D2DS_LOG_FUNC_ENTER__; - -#if !GLIB_CHECK_VERSION(2, 32, 0) - if (!g_thread_supported()) - g_thread_init(NULL); -#endif - -#if !GLIB_CHECK_VERSION(2, 36, 0) - g_type_init(); -#endif - NOTUSED(argc); - NOTUSED(argv); - - D2DS_LOGI("service mainloop start"); - - /* Create d2d-subowner service */ - d2dssvc = service = d2ds_service_new(); - - /* Initialize d2d-subowner service interface layer */ - ret = d2ds_service_interface_init(service); - if (G_UNLIKELY(FALSE == ret)) { - D2DS_LOGE("Service interface init. failed!"); - ret_code = EXIT_FAILURE; - goto END; - } - /* Run d2d-subowner service */ - ret = d2ds_service_run(service); - if (G_UNLIKELY(FALSE == ret)) { - D2DS_LOGE("Run service failed!"); - ret_code = EXIT_FAILURE; - } - -END: - /* Free d2d-manager service */ - d2ds_service_interface_deinit(service); - d2ds_service_free(service); - d2dssvc = NULL; - - __D2DS_LOG_FUNC_EXIT__; - return ret_code; -} - diff --git a/src/d2d-subowner/d2ds.h b/src/d2d-subowner/d2ds.h deleted file mode 100644 index a7b302d..0000000 --- a/src/d2d-subowner/d2ds.h +++ /dev/null @@ -1,53 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __D2D_SUBOWNER_H__ -#define __D2D_SUBOWNER_H__ - -#include -#include -#include - -/**< Internal error code with d2ds daemon. It should be matched with API side */ -typedef enum { - D2DS_ERROR_NONE = 0, /**< Successful */ - D2DS_ERROR_IO_ERROR, /**< I/O error */ - D2DS_ERROR_NO_DATA, /**< Data not exists */ - D2DS_ERROR_OUT_OF_MEMORY, /**< out of memory */ - D2DS_ERROR_OPERATION_FAILED, /**< operation failed */ - D2DS_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ - D2DS_ERROR_ALREADY_REGISTERED, /**< Request already registered */ - D2DS_ERROR_IN_PROGRESS /**< operation is in progress */ -} d2ds_error_e; - -/**< d2d-subowner service structure */ -typedef struct _d2ds_service { - GMainLoop *main_loop; /**< Service main-loop */ - guint dbus_id; /**< D-Bus id */ - guint activation_dbus_id; /**< D-Bus id for activation */ - - gint pending; /**< Is there any pending operation? */ - - gpointer connection; /**< Connection handle for D-Bus call to other process */ - GCancellable *ca; /**< Cancellable object for D-Bus call (Daemon->other) */ - GList *dbus_sub_ids; /**< The list of subscribed signals */ - - gboolean d2ds_activated; /**< Whether d2d-manager enabled or not */ -} d2ds_service; - -#endif /* __D2D_SUBOWNER_H__ */ diff --git a/src/d2d-subowner/introspection/d2ds.xml b/src/d2d-subowner/introspection/d2ds.xml deleted file mode 100644 index e6858a5..0000000 --- a/src/d2d-subowner/introspection/d2ds.xml +++ /dev/null @@ -1,92 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/d2d-subowner/introspection/enabler.xml b/src/d2d-subowner/introspection/enabler.xml deleted file mode 100644 index 7eed59a..0000000 --- a/src/d2d-subowner/introspection/enabler.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/src/d2d-subowner/introspection/gen.sh b/src/d2d-subowner/introspection/gen.sh deleted file mode 100755 index 003d818..0000000 --- a/src/d2d-subowner/introspection/gen.sh +++ /dev/null @@ -1,6 +0,0 @@ -gdbus-codegen --interface-prefix net.d2ds. \ - --generate-c-code d2ds-generated-code \ - --c-generate-object-manager \ - --generate-docbook generated-docs \ - d2ds.xml \ - enabler.xml diff --git a/src/d2d-subowner/magent-db.c b/src/d2d-subowner/magent-db.c deleted file mode 100644 index b587805..0000000 --- a/src/d2d-subowner/magent-db.c +++ /dev/null @@ -1,257 +0,0 @@ -/****************************************************************** - * - * Copyright 2016 Samsung Electronics All Rights Reserved. - * - * - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - *****************************************************************/ -#include "iotivity_config.h" - -#include -#include -#include -#ifdef HAVE_UNISTD_H -#include -#endif -#include -#include -#include - -#include "oic_malloc.h" -#include "oic_string.h" -#include "ocprovisioningmanager.h" - -#include - -#include "d2ds.h" -#include "d2ds-log.h" -#include "d2ds-util.h" - -#define MAX_FILE_PATH_LEN 1024 - -static const char* PRVN_DB_FILE_NAME = "oic_pdm_d2ds.db"; /**< Provisining DB file */ - -#define UUID_LENGTH 128/8 /**< 128-bit GUID length */ - -typedef enum PdmDeviceState { - PDM_DEVICE_ACTIVE = 0, - PDM_DEVICE_STALE = 1, - PDM_DEVICE_INIT = 2, - PDM_DEVICE_UNKNOWN = 99 -} PdmDeviceState_t; - -#define PDM_FIRST_INDEX 0 -#define PDM_SECOND_INDEX 1 - -#define PDM_BIND_INDEX_FIRST 1 -#define PDM_BIND_INDEX_SECOND 2 -#define PDM_BIND_INDEX_THIRD 3 - -#define PDM_CREATE_T_DEVICE_LIST "create table T_DEVICE_LIST(ID INTEGER PRIMARY KEY AUTOINCREMENT,\ - UUID BLOB NOT NULL UNIQUE, STATE INT NOT NULL);" - -#define PDM_CREATE_T_DEVICE_LINK "create table T_DEVICE_LINK_STATE(ID INT NOT NULL, ID2 INT NOT \ - NULL,STATE INT NOT NULL, PRIMARY KEY (ID, ID2));" - -#define PDM_VERIFY_SQLITE_OK(arg, logLevel, retValue) do{ if (SQLITE_OK != (arg)) \ - { D2DS_LOGE("Error in " #arg ", Error Message: %s", \ - sqlite3_errmsg(g_db)); return retValue; }}while(0) - -#define PDM_SQLITE_TRANSACTION_BEGIN "BEGIN TRANSACTION;" -#define PDM_SQLITE_TRANSACTION_COMMIT "COMMIT;" -#define PDM_SQLITE_TRANSACTION_ROLLBACK "ROLLBACK;" -#define PDM_SQLITE_GET_STALE_INFO "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE STATE = ?" -#define PDM_SQLITE_INSERT_T_DEVICE_LIST "INSERT INTO T_DEVICE_LIST VALUES(?,?,?)" -#define PDM_SQLITE_GET_ID "SELECT ID FROM T_DEVICE_LIST WHERE UUID like ?" -#define PDM_SQLITE_INSERT_LINK_DATA "INSERT INTO T_DEVICE_LINK_STATE VALUES(?,?,?)" -#define PDM_SQLITE_DELETE_LINK "DELETE FROM T_DEVICE_LINK_STATE WHERE ID = ? and ID2 = ?" -#define PDM_SQLITE_DELETE_DEVICE_LINK "DELETE FROM T_DEVICE_LINK_STATE WHERE ID = ? or ID2 = ?" -#define PDM_SQLITE_DELETE_DEVICE "DELETE FROM T_DEVICE_LIST WHERE ID = ?" -#define PDM_SQLITE_DELETE_DEVICE_WITH_STATE "DELETE FROM T_DEVICE_LIST WHERE STATE= ?" -#define PDM_SQLITE_UPDATE_LINK "UPDATE T_DEVICE_LINK_STATE SET STATE = ? WHERE ID = ? and ID2 = ?" -#define PDM_SQLITE_LIST_ALL_UUID "SELECT UUID FROM T_DEVICE_LIST WHERE STATE = 0" -#define PDM_SQLITE_GET_UUID "SELECT UUID,STATE FROM T_DEVICE_LIST WHERE ID = ?" -#define PDM_SQLITE_GET_LINKED_DEVICES "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE \ - (ID = ? or ID2 = ?) and state = 0" -#define PDM_SQLITE_GET_DEVICE_LINKS "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE \ - ID = ? and ID2 = ? and state = 0" -#define PDM_SQLITE_UPDATE_DEVICE "UPDATE T_DEVICE_LIST SET STATE = ? WHERE UUID like ?" -#define PDM_SQLITE_GET_DEVICE_STATUS "SELECT STATE FROM T_DEVICE_LIST WHERE UUID like ?" -#define PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE "UPDATE T_DEVICE_LINK_STATE SET STATE = 1\ - WHERE ID = ? or ID2 = ?" - -#define CHECK_PDM_INIT() do{if(true != gInit)\ - { D2DS_LOGE("PDB is not initialized"); \ - return OC_STACK_PDM_IS_NOT_INITIALIZED; }}while(0) - -static sqlite3 *g_db = NULL; -static bool gInit = false; /* Only if we can open sqlite db successfully, gInit is true. */ - -static OCStackResult begin() -{ - int res = 0; - res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_BEGIN, NULL, NULL, NULL); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - return OC_STACK_OK; -} - -static OCStackResult commit() -{ - int res = 0; - res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_COMMIT, NULL, NULL, NULL); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - return OC_STACK_OK; -} - -static OCStackResult rollback() -{ - int res = 0; - res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_ROLLBACK, NULL, NULL, NULL); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - return OC_STACK_OK; -} - -int openDB() -{ - int result = 0; - char path[MAX_FILE_PATH_LEN] = {0,}; - - snprintf(path, MAX_FILE_PATH_LEN, "%s/network/%s", - tzplatform_getenv(TZ_SYS_GLOBALUSER_DATA), PRVN_DB_FILE_NAME); - - result = sqlite3_open_v2(path, &g_db, SQLITE_OPEN_READWRITE, NULL); - PDM_VERIFY_SQLITE_OK(result, ERROR, OC_STACK_ERROR); - - gInit = true; - - return OC_STACK_OK; -} - -int closeDB() -{ - CHECK_PDM_INIT(); - int res = 0; - res = sqlite3_close(g_db); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - return OC_STACK_OK; -} - -static OCStackResult getUUIDforId(int id, OicUuid_t *uid, bool *result) -{ - sqlite3_stmt *stmt = 0; - int res = 0; - res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_UUID, - strlen(PDM_SQLITE_GET_UUID) + 1, &stmt, NULL); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - - res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - - while (SQLITE_ROW == sqlite3_step(stmt)) - { - const void *ptr = sqlite3_column_blob(stmt, PDM_FIRST_INDEX); - memcpy(uid, ptr, sizeof(OicUuid_t)); - - int temp = sqlite3_column_int(stmt, PDM_SECOND_INDEX); - if(PDM_DEVICE_STALE == temp) - { - if(result) - { - *result = true; - } - } - else - { - if(result) - { - *result = false; - } - } - sqlite3_finalize(stmt); - return OC_STACK_OK; - } - sqlite3_finalize(stmt); - - return OC_STACK_INVALID_PARAM; -} - -static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id) -{ - sqlite3_stmt *stmt = 0; - int res = 0; - res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, strlen(PDM_SQLITE_GET_ID) + 1, &stmt, NULL); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - - res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, UUID, UUID_LENGTH, SQLITE_STATIC); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - - D2DS_LOGD("Binding Done"); - while (SQLITE_ROW == sqlite3_step(stmt)) - { - int tempId = sqlite3_column_int(stmt, PDM_FIRST_INDEX); - D2DS_LOGD("ID is %d", tempId); - *id = tempId; - sqlite3_finalize(stmt); - return OC_STACK_OK; - } - sqlite3_finalize(stmt); - return OC_STACK_INVALID_PARAM; -} - -static OCStackResult removeFromDeviceList(int id) -{ - sqlite3_stmt *stmt = 0; - int res = 0; - res = sqlite3_prepare_v2(g_db, PDM_SQLITE_DELETE_DEVICE, - strlen(PDM_SQLITE_DELETE_DEVICE) + 1, &stmt, NULL); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - - res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id); - PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); - - if (sqlite3_step(stmt) != SQLITE_DONE) - { - D2DS_LOGE("Error message: %s", sqlite3_errmsg(g_db)); - sqlite3_finalize(stmt); - return OC_STACK_ERROR; - } - sqlite3_finalize(stmt); - - return OC_STACK_OK; -} - -int delete_mowned_device_db(const OicUuid_t *UUID) -{ - CHECK_PDM_INIT(); - if (NULL == UUID) - { - return OC_STACK_INVALID_PARAM; - } - int id = 0; - if (OC_STACK_OK != getIdForUUID(UUID, &id)) - { - D2DS_LOGE("Requested value not found"); - return OC_STACK_INVALID_PARAM; - } - begin(); - if(OC_STACK_OK != removeFromDeviceList(id)) - { - rollback(); - D2DS_LOGE("Requested value not found"); - return OC_STACK_ERROR; - } - commit(); - return OC_STACK_OK; -} diff --git a/src/d2d-subowner/magent-db.h b/src/d2d-subowner/magent-db.h deleted file mode 100644 index 83ea83d..0000000 --- a/src/d2d-subowner/magent-db.h +++ /dev/null @@ -1,34 +0,0 @@ -/* - * Network Configuration Module - * - * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - */ -#ifndef __MAGENT_DB_H__ -#define __MAGENT_DB_H__ - -#ifdef __cplusplus -extern "C" { -#endif - -int openDB(); -int closeDB(); -int delete_mowned_device_db(const OicUuid_t *UUID); - -#ifdef __cplusplus -} -#endif - -#endif /* __MAGENT_DB_H__ */ diff --git a/src/d2d-subowner/oic_svr_db_d2ds.dat b/src/d2d-subowner/oic_svr_db_d2ds.dat deleted file mode 100644 index 7b089cb..0000000 Binary files a/src/d2d-subowner/oic_svr_db_d2ds.dat and /dev/null differ diff --git a/src/d2d-subowner/oic_svr_db_d2ds.json b/src/d2d-subowner/oic_svr_db_d2ds.json deleted file mode 100644 index 7942689..0000000 --- a/src/d2d-subowner/oic_svr_db_d2ds.json +++ /dev/null @@ -1,73 +0,0 @@ -{ - "acl": { - "aclist": { - "aces": [ - { - "subjectuuid": "*", - "resources": [ - { - "href": "/oic/res", - "rel": "", - "rt": ["oic.wk.res"], - "if": ["oic.if.ll"] - },{ - "href": "/oic/d", - "rel": "", - "rt": ["oic.wk.d"], - "if": ["oic.if.baseline", "oic.if.r"] - },{ - "href": "/oic/p", - "rel": "", - "rt": ["oic.wk.p"], - "if": ["oic.if.baseline", "oic.if.r"] - } - ], - "permission": 2 - }, - { - "subjectuuid": "*", - "resources": [ - { - "href": "/oic/sec/doxm", - "rel": "", - "rt": ["oic.r.doxm"], - "if": ["oic.if.baseline"] - }, - { - "href": "/oic/sec/pstat", - "rel": "", - "rt": ["oic.r.pstat"], - "if": ["oic.if.baseline"] - }, - { - "href": "/oic/sec/cred", - "rel": "", - "rt": ["oic.r.cred"], - "if": ["oic.if.baseline"] - } - ], - "permission": 6 - } - ] - }, - "rowneruuid": "00000000-0000-0000-0000-000000000000" - }, - "pstat": { - "isop": false, - "deviceuuid": "00000000-0000-0000-0000-000000000000", - "rowneruuid": "00000000-0000-0000-0000-000000000000", - "cm": 2, - "tm": 0, - "om": 4, - "sm": 4 - }, - "doxm": { - "oxms": [0], - "oxmsel": 0, - "sct": 0, - "owned": false, - "deviceuuid": "00000000-0000-0000-0000-000000000000", - "devowneruuid": "", - "rowneruuid": "00000000-0000-0000-0000-000000000000" - } -} diff --git a/src/mot-agent/CMakeLists.txt b/src/mot-agent/CMakeLists.txt new file mode 100644 index 0000000..80d7124 --- /dev/null +++ b/src/mot-agent/CMakeLists.txt @@ -0,0 +1,40 @@ +MESSAGE("===================================================================") +MESSAGE("Building...MOT client") +MESSAGE("===================================================================") + +INCLUDE_DIRECTORIES(${CMAKE_SOURCE_DIR}/src/include/cjson) + +SET(INTROSPECTION_PATH ${CMAKE_CURRENT_SOURCE_DIR}/introspection) +ADD_CUSTOM_COMMAND( + OUTPUT dbus1 + COMMAND gdbus-codegen + --interface-prefix net.ma. + --generate-c-code ma-generated-code + --c-generate-object-manager + --generate-docbook generated-docs + ${INTROSPECTION_PATH}/ma.xml + ${INTROSPECTION_PATH}/enabler.xml + COMMENT "Generating MOT agent GDBus .c/.h" +) + +MESSAGE(" - Check cmake packagesion") +pkg_check_modules(ma_pkgs REQUIRED ${COMMON_DEPS} ${TIZEN_DEPS} + capi-network-wifi-manager + sqlite3 + ) + +ADD_CUSTOM_TARGET(GENERATED_MA_DBUS_CODE DEPENDS dbus1) +MESSAGE(" - Set source folder") +FILE(GLOB MOT_AGENT_SRCS *.c) +SET(MOT_AGENT_SRCS ${MOT_AGENT_SRCS} ${CMAKE_CURRENT_SOURCE_DIR}/ma-generated-code.c) +SET_SOURCE_FILES_PROPERTIES(${CMAKE_CURRENT_SOURCE_DIR}/ma-generated-code.c PROPERTIES GENERATED TRUE) + +INCLUDE_DIRECTORIES(${ma_pkgs_INCLUDE_DIRS}) +LINK_DIRECTORIES(${ma_pkgs_LIBRARY_DIRS}) + +SET(MOT_AGENT "mot-agent") +ADD_EXECUTABLE(${MOT_AGENT} ${MOT_AGENT_SRCS}) +ADD_DEPENDENCIES(${MOT_AGENT} GENERATED_MA_DBUS_CODE) +TARGET_LINK_LIBRARIES(${MOT_AGENT} ${ma_pkgs_LIBRARIES} pthread) +INSTALL(TARGETS ${MOT_AGENT} DESTINATION bin) +INSTALL(FILES oic_svr_db_ma.dat DESTINATION ${NETWORK_FW_DATADIR}) diff --git a/src/mot-agent/introspection/enabler.xml b/src/mot-agent/introspection/enabler.xml new file mode 100644 index 0000000..03cb9bd --- /dev/null +++ b/src/mot-agent/introspection/enabler.xml @@ -0,0 +1,17 @@ + + + + + + + + + + + + + + + diff --git a/src/mot-agent/introspection/gen.sh b/src/mot-agent/introspection/gen.sh new file mode 100755 index 0000000..07eba3e --- /dev/null +++ b/src/mot-agent/introspection/gen.sh @@ -0,0 +1,6 @@ +gdbus-codegen --interface-prefix net.ma. \ + --generate-c-code ma-generated-code \ + --c-generate-object-manager \ + --generate-docbook generated-docs \ + ma.xml \ + enabler.xml diff --git a/src/mot-agent/introspection/ma.xml b/src/mot-agent/introspection/ma.xml new file mode 100644 index 0000000..51f6fe8 --- /dev/null +++ b/src/mot-agent/introspection/ma.xml @@ -0,0 +1,92 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/mot-agent/ma-db.c b/src/mot-agent/ma-db.c new file mode 100644 index 0000000..26b8b0a --- /dev/null +++ b/src/mot-agent/ma-db.c @@ -0,0 +1,257 @@ +/****************************************************************** + * + * Copyright 2016 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *****************************************************************/ +#include "iotivity_config.h" + +#include +#include +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#include +#include +#include + +#include "oic_malloc.h" +#include "oic_string.h" +#include "ocprovisioningmanager.h" + +#include + +#include "ma.h" +#include "ma-log.h" +#include "ma-util.h" + +#define MAX_FILE_PATH_LEN 1024 + +static const char* PRVN_DB_FILE_NAME = "oic_pdm_ma.db"; /**< Provisining DB file */ + +#define UUID_LENGTH 128/8 /**< 128-bit GUID length */ + +typedef enum PdmDeviceState { + PDM_DEVICE_ACTIVE = 0, + PDM_DEVICE_STALE = 1, + PDM_DEVICE_INIT = 2, + PDM_DEVICE_UNKNOWN = 99 +} PdmDeviceState_t; + +#define PDM_FIRST_INDEX 0 +#define PDM_SECOND_INDEX 1 + +#define PDM_BIND_INDEX_FIRST 1 +#define PDM_BIND_INDEX_SECOND 2 +#define PDM_BIND_INDEX_THIRD 3 + +#define PDM_CREATE_T_DEVICE_LIST "create table T_DEVICE_LIST(ID INTEGER PRIMARY KEY AUTOINCREMENT,\ + UUID BLOB NOT NULL UNIQUE, STATE INT NOT NULL);" + +#define PDM_CREATE_T_DEVICE_LINK "create table T_DEVICE_LINK_STATE(ID INT NOT NULL, ID2 INT NOT \ + NULL,STATE INT NOT NULL, PRIMARY KEY (ID, ID2));" + +#define PDM_VERIFY_SQLITE_OK(arg, logLevel, retValue) do{ if (SQLITE_OK != (arg)) \ + { MA_LOGE("Error in " #arg ", Error Message: %s", \ + sqlite3_errmsg(g_db)); return retValue; }}while(0) + +#define PDM_SQLITE_TRANSACTION_BEGIN "BEGIN TRANSACTION;" +#define PDM_SQLITE_TRANSACTION_COMMIT "COMMIT;" +#define PDM_SQLITE_TRANSACTION_ROLLBACK "ROLLBACK;" +#define PDM_SQLITE_GET_STALE_INFO "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE STATE = ?" +#define PDM_SQLITE_INSERT_T_DEVICE_LIST "INSERT INTO T_DEVICE_LIST VALUES(?,?,?)" +#define PDM_SQLITE_GET_ID "SELECT ID FROM T_DEVICE_LIST WHERE UUID like ?" +#define PDM_SQLITE_INSERT_LINK_DATA "INSERT INTO T_DEVICE_LINK_STATE VALUES(?,?,?)" +#define PDM_SQLITE_DELETE_LINK "DELETE FROM T_DEVICE_LINK_STATE WHERE ID = ? and ID2 = ?" +#define PDM_SQLITE_DELETE_DEVICE_LINK "DELETE FROM T_DEVICE_LINK_STATE WHERE ID = ? or ID2 = ?" +#define PDM_SQLITE_DELETE_DEVICE "DELETE FROM T_DEVICE_LIST WHERE ID = ?" +#define PDM_SQLITE_DELETE_DEVICE_WITH_STATE "DELETE FROM T_DEVICE_LIST WHERE STATE= ?" +#define PDM_SQLITE_UPDATE_LINK "UPDATE T_DEVICE_LINK_STATE SET STATE = ? WHERE ID = ? and ID2 = ?" +#define PDM_SQLITE_LIST_ALL_UUID "SELECT UUID FROM T_DEVICE_LIST WHERE STATE = 0" +#define PDM_SQLITE_GET_UUID "SELECT UUID,STATE FROM T_DEVICE_LIST WHERE ID = ?" +#define PDM_SQLITE_GET_LINKED_DEVICES "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE \ + (ID = ? or ID2 = ?) and state = 0" +#define PDM_SQLITE_GET_DEVICE_LINKS "SELECT ID,ID2 FROM T_DEVICE_LINK_STATE WHERE \ + ID = ? and ID2 = ? and state = 0" +#define PDM_SQLITE_UPDATE_DEVICE "UPDATE T_DEVICE_LIST SET STATE = ? WHERE UUID like ?" +#define PDM_SQLITE_GET_DEVICE_STATUS "SELECT STATE FROM T_DEVICE_LIST WHERE UUID like ?" +#define PDM_SQLITE_UPDATE_LINK_STALE_FOR_STALE_DEVICE "UPDATE T_DEVICE_LINK_STATE SET STATE = 1\ + WHERE ID = ? or ID2 = ?" + +#define CHECK_PDM_INIT() do{if(true != gInit)\ + { MA_LOGE("PDB is not initialized"); \ + return OC_STACK_PDM_IS_NOT_INITIALIZED; }}while(0) + +static sqlite3 *g_db = NULL; +static bool gInit = false; /* Only if we can open sqlite db successfully, gInit is true. */ + +static OCStackResult begin() +{ + int res = 0; + res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_BEGIN, NULL, NULL, NULL); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + return OC_STACK_OK; +} + +static OCStackResult commit() +{ + int res = 0; + res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_COMMIT, NULL, NULL, NULL); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + return OC_STACK_OK; +} + +static OCStackResult rollback() +{ + int res = 0; + res = sqlite3_exec(g_db, PDM_SQLITE_TRANSACTION_ROLLBACK, NULL, NULL, NULL); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + return OC_STACK_OK; +} + +int openDB() +{ + int result = 0; + char path[MAX_FILE_PATH_LEN] = {0,}; + + snprintf(path, MAX_FILE_PATH_LEN, "%s/network/%s", + tzplatform_getenv(TZ_SYS_GLOBALUSER_DATA), PRVN_DB_FILE_NAME); + + result = sqlite3_open_v2(path, &g_db, SQLITE_OPEN_READWRITE, NULL); + PDM_VERIFY_SQLITE_OK(result, ERROR, OC_STACK_ERROR); + + gInit = true; + + return OC_STACK_OK; +} + +int closeDB() +{ + CHECK_PDM_INIT(); + int res = 0; + res = sqlite3_close(g_db); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + return OC_STACK_OK; +} + +static OCStackResult getUUIDforId(int id, OicUuid_t *uid, bool *result) +{ + sqlite3_stmt *stmt = 0; + int res = 0; + res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_UUID, + strlen(PDM_SQLITE_GET_UUID) + 1, &stmt, NULL); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + + res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + + while (SQLITE_ROW == sqlite3_step(stmt)) + { + const void *ptr = sqlite3_column_blob(stmt, PDM_FIRST_INDEX); + memcpy(uid, ptr, sizeof(OicUuid_t)); + + int temp = sqlite3_column_int(stmt, PDM_SECOND_INDEX); + if(PDM_DEVICE_STALE == temp) + { + if(result) + { + *result = true; + } + } + else + { + if(result) + { + *result = false; + } + } + sqlite3_finalize(stmt); + return OC_STACK_OK; + } + sqlite3_finalize(stmt); + + return OC_STACK_INVALID_PARAM; +} + +static OCStackResult getIdForUUID(const OicUuid_t *UUID , int *id) +{ + sqlite3_stmt *stmt = 0; + int res = 0; + res = sqlite3_prepare_v2(g_db, PDM_SQLITE_GET_ID, strlen(PDM_SQLITE_GET_ID) + 1, &stmt, NULL); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + + res = sqlite3_bind_blob(stmt, PDM_BIND_INDEX_FIRST, UUID, UUID_LENGTH, SQLITE_STATIC); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + + MA_LOGD("Binding Done"); + while (SQLITE_ROW == sqlite3_step(stmt)) + { + int tempId = sqlite3_column_int(stmt, PDM_FIRST_INDEX); + MA_LOGD("ID is %d", tempId); + *id = tempId; + sqlite3_finalize(stmt); + return OC_STACK_OK; + } + sqlite3_finalize(stmt); + return OC_STACK_INVALID_PARAM; +} + +static OCStackResult removeFromDeviceList(int id) +{ + sqlite3_stmt *stmt = 0; + int res = 0; + res = sqlite3_prepare_v2(g_db, PDM_SQLITE_DELETE_DEVICE, + strlen(PDM_SQLITE_DELETE_DEVICE) + 1, &stmt, NULL); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + + res = sqlite3_bind_int(stmt, PDM_BIND_INDEX_FIRST, id); + PDM_VERIFY_SQLITE_OK(res, ERROR, OC_STACK_ERROR); + + if (sqlite3_step(stmt) != SQLITE_DONE) + { + MA_LOGE("Error message: %s", sqlite3_errmsg(g_db)); + sqlite3_finalize(stmt); + return OC_STACK_ERROR; + } + sqlite3_finalize(stmt); + + return OC_STACK_OK; +} + +int delete_mowned_device_db(const OicUuid_t *UUID) +{ + CHECK_PDM_INIT(); + if (NULL == UUID) + { + return OC_STACK_INVALID_PARAM; + } + int id = 0; + if (OC_STACK_OK != getIdForUUID(UUID, &id)) + { + MA_LOGE("Requested value not found"); + return OC_STACK_INVALID_PARAM; + } + begin(); + if(OC_STACK_OK != removeFromDeviceList(id)) + { + rollback(); + MA_LOGE("Requested value not found"); + return OC_STACK_ERROR; + } + commit(); + return OC_STACK_OK; +} diff --git a/src/mot-agent/ma-db.h b/src/mot-agent/ma-db.h new file mode 100644 index 0000000..83ea83d --- /dev/null +++ b/src/mot-agent/ma-db.h @@ -0,0 +1,34 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __MAGENT_DB_H__ +#define __MAGENT_DB_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +int openDB(); +int closeDB(); +int delete_mowned_device_db(const OicUuid_t *UUID); + +#ifdef __cplusplus +} +#endif + +#endif /* __MAGENT_DB_H__ */ diff --git a/src/mot-agent/ma-log.h b/src/mot-agent/ma-log.h new file mode 100644 index 0000000..3d4c707 --- /dev/null +++ b/src/mot-agent/ma-log.h @@ -0,0 +1,62 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __MA_LOG_H__ +#define __MA_LOG_H__ + +#ifdef LOG_TAG +#undef LOG_TAG +#endif + +#ifdef USE_DLOG +#include + +#undef LOG_TAG +#define LOG_TAG "MOT_AGENT" + +#define MA_LOGV(format, args...) LOGV(format, ##args) +#define MA_LOGD(format, args...) LOGD(format, ##args) +#define MA_LOGI(format, args...) LOGI(format, ##args) +#define MA_LOGW(format, args...) LOGW(format, ##args) +#define MA_LOGE(format, args...) LOGE(format, ##args) +#define MA_LOGF(format, args...) LOGF(format, ##args) + +#define __MA_LOG_FUNC_ENTER__ LOGD("Enter") +#define __MA_LOG_FUNC_EXIT__ LOGD("Quit") + +#define MA_SECLOGI(format, args...) SECURE_LOG(LOG_INFO, MA_LOG_TAG, format, ##args) +#define MA_SECLOGD(format, args...) SECURE_LOG(LOG_DEBUG, MA_LOG_TAG, format, ##args) + +#else /* USE_DLOG */ + +#define MA_LOGV(format, args...) +#define MA_LOGD(format, args...) +#define MA_LOGI(format, args...) +#define MA_LOGW(format, args...) +#define MA_LOGE(format, args...) +#define MA_LOGF(format, args...) + +#define __MA_LOG_FUNC_ENTER__ +#define __MA_LOG_FUNC_EXIT__ + +#define MA_SECLOGI(format, args...) +#define MA_SECLOGD(format, args...) + +#endif /* USE_DLOG */ + +#endif /* __MA_LOG_H__ */ diff --git a/src/mot-agent/ma-service-interface.c b/src/mot-agent/ma-service-interface.c new file mode 100644 index 0000000..47a125d --- /dev/null +++ b/src/mot-agent/ma-service-interface.c @@ -0,0 +1,542 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include + +#include "ma.h" +#include "ma-log.h" +#include "ma-util.h" +#include "ma-service.h" +#include "ma-subowner.h" +#include "ma-service-interface.h" +#include "ma-generated-code.h" + +static NetMa *ma_dbus_object; +static Enabler *ma_activator_dbus_object; + +/* global list to care resource handle for each client */ +static GList *ma_dbus_client_list; +static GMutex ma_dbus_client_list_mutex; + +typedef struct _ma_dbus_client_s { + gchar *bus_name; +} ma_dbus_client_s; + +NetMa* ma_dbus_get_object() +{ + return ma_dbus_object; +} + +static int _ma_dbus_client_list_cleanup(GList *client_list) +{ + ma_dbus_client_s *client; + + ma_check_null_ret_error("client_list", client_list, FALSE); + + client = client_list->data; + + g_free(client->bus_name); + client->bus_name = NULL; + g_free(client); + g_list_free(client_list); + + return MA_ERROR_NONE; +} + +static int _ma_dbus_client_list_compare_bus_name(const void *a, const void *b) +{ + const ma_dbus_client_s *client = a; + return g_strcmp0(client->bus_name, b); +} + +static inline GList* _ma_dbus_client_list_find_client(const gchar *owner) +{ + return g_list_find_custom(ma_dbus_client_list, owner, + _ma_dbus_client_list_compare_bus_name); +} + +static void _ma_dbus_name_owner_changed_cb(GDBusConnection *conn, + const gchar *sender_name, + const gchar *object_path, + const gchar *interface_name, + const gchar *signal_name, + GVariant *parameters, + gpointer user_data) +{ + int ret; + GList *client = NULL; + gchar *name, *old_owner, *new_owner; + + NOTUSED(conn); + NOTUSED(sender_name); + NOTUSED(object_path); + NOTUSED(interface_name); + NOTUSED(signal_name); + NOTUSED(user_data); + + g_variant_get(parameters, "(&s&s&s)", &name, &old_owner, &new_owner); + + if (0 == strlen(new_owner)) { + g_mutex_lock(&ma_dbus_client_list_mutex); + client = _ma_dbus_client_list_find_client(old_owner); + if (client) { /* found bus name in our bus list */ + MA_LOGD("bus(%s) stopped", old_owner); + ma_dbus_client_list = g_list_remove_link(ma_dbus_client_list, client); + } + g_mutex_unlock(&ma_dbus_client_list_mutex); + + if (client) { + ret = _ma_dbus_client_list_cleanup(client); + if (MA_ERROR_NONE != ret) + MA_LOGE("_ma_dbus_client_list_cleanup() Fail(%d)", ret); + } + } +} + +static int _ma_dbus_subscribe_name_owner_changed(GDBusConnection *conn) +{ + unsigned int id; + + id = g_dbus_connection_signal_subscribe(conn, + "org.freedesktop.DBus", /* bus name */ + "org.freedesktop.DBus", /* interface */ + "NameOwnerChanged", /* member */ + "/org/freedesktop/DBus", /* path */ + NULL, /* arg0 */ + G_DBUS_SIGNAL_FLAGS_NONE, + _ma_dbus_name_owner_changed_cb, + NULL, + NULL); + if (0 == id) { + MA_LOGE("g_dbus_connection_signal_subscribe() Fail"); + return MA_ERROR_IO_ERROR; + } + + return MA_ERROR_NONE; +} + + +static gboolean _ma_dbus_handle_disc_mot_enb_devs(NetMa *object, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + ret = ma_request_disc_mot_enb_devs(service); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_disc_mot_enb_devs !"); + + net_ma_complete_disc_mot_enb_devs(object, invocation, ret); + + return TRUE; +} + +static gboolean _ma_dbus_handle_disc_mowned_devs(NetMa *object, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + + ret = ma_request_disc_owned_devs(service); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_disc_owned_devs !"); + + net_ma_complete_disc_mowned_devs(object, invocation, ret); + + return TRUE; +} + +static gboolean _ma_dbus_handle_mot(NetMa *object, + GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_pin, gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + + ret = ma_request_mot(service, arg_target, arg_pin); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_disc_moted_devs !"); + + net_ma_complete_mot(object, invocation, ret); + + return TRUE; +} + +static gboolean _ma_dbus_handle_remove_mo(NetMa *object, + GDBusMethodInvocation *invocation, gchar *arg_target, gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + + ret = ma_request_remove_mo(service, arg_target); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_remove_mo !"); + + net_ma_complete_remove_mo(object, invocation, ret); + + return TRUE; +} + + +static gboolean _ma_dbus_handle_acl(NetMa *object, + GDBusMethodInvocation *invocation, gchar *arg_target, gchar *arg_subject, gchar *arg_uri, + gchar *arg_rt, gchar *arg_interface, gint arg_permission, gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + + ret = ma_request_prov_acl(service, arg_target, arg_subject, arg_uri, arg_rt, arg_interface, arg_permission); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_prov_acl !"); + + net_ma_complete_acl(object, invocation, ret); + + return TRUE; +} + +static gboolean _ma_dbus_handle_cred(NetMa *object, + GDBusMethodInvocation *invocation, gchar *arg_dev1, gchar *arg_dev2, gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + + ret = ma_request_prov_cred(service, arg_dev1, arg_dev2); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_prov_cred !"); + + net_ma_complete_cred(object, invocation, ret); + + return TRUE; +} + +static gboolean _ma_dbus_handle_get_ownerid(NetMa *object, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + gchar *uuid_str = NULL; + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + ret = ma_request_get_ownerid(service, &uuid_str); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_get_ownerid = %d", ret); + + net_ma_complete_get_ownerid(object, invocation, uuid_str, ret); + + if (uuid_str) + g_free(uuid_str); + + return TRUE; +} + +static gboolean _ma_dbus_handle_pairwise(NetMa *object, + GDBusMethodInvocation *invocation, gchar *target_1, + gchar *subject_1, gchar *uri_1, gchar *rt_1, + gchar *interface_1, int permission_1, gchar *target_2, + gchar *subject_2, gchar *uri_2, gchar *rt_2, + gchar *interface_2, int permission_2, gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + + ret = ma_request_pairwise(service, target_1, subject_1, uri_1, rt_1, + interface_1, permission_1, target_2, subject_2, uri_2, + rt_2, interface_2, permission_2); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_pairwise = %d", ret); + + net_ma_complete_pairwise(object, invocation, ret); + + return TRUE; +} + +static gboolean _ma_dbus_handle_unpair(NetMa *object, + GDBusMethodInvocation *invocation, gchar *uuid_dev, gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + ma_check_null_ret_error("service", service, FALSE); + + ret = ma_request_unpair(service, uuid_dev); + if (MA_ERROR_NONE != ret) + MA_LOGE("Failed to ma_request_unpair = %d", ret); + + net_ma_complete_unpair(object, invocation, ret); + + return TRUE; +} + +static void _ma_dbus_on_bus_acquired(GDBusConnection *conn, const gchar *name, + gpointer user_data) +{ + gboolean ret; + GError *error = NULL; + ma_service *service = (ma_service *)user_data; + + NOTUSED(name); + + ma_dbus_object = net_ma_skeleton_new(); + if (NULL == ma_dbus_object) { + MA_LOGE("net_ma_skeleton_new() Fail"); + return; + } + + g_signal_connect(ma_dbus_object, "handle-disc-mot-enb-devs", + G_CALLBACK(_ma_dbus_handle_disc_mot_enb_devs), service); + g_signal_connect(ma_dbus_object, "handle-disc-mowned-devs", + G_CALLBACK(_ma_dbus_handle_disc_mowned_devs), service); + g_signal_connect(ma_dbus_object, "handle-mot", + G_CALLBACK(_ma_dbus_handle_mot), service); + g_signal_connect(ma_dbus_object, "handle-remove-mo", + G_CALLBACK(_ma_dbus_handle_remove_mo), service); + g_signal_connect(ma_dbus_object, "handle-acl", + G_CALLBACK(_ma_dbus_handle_acl), service); + g_signal_connect(ma_dbus_object, "handle-cred", + G_CALLBACK(_ma_dbus_handle_cred), service); + g_signal_connect(ma_dbus_object, "handle-get-ownerid", + G_CALLBACK(_ma_dbus_handle_get_ownerid), service); + g_signal_connect(ma_dbus_object, "handle-pairwise", + G_CALLBACK(_ma_dbus_handle_pairwise), service); + g_signal_connect(ma_dbus_object, "handle-unpair", + G_CALLBACK(_ma_dbus_handle_unpair), service); + + ret = g_dbus_interface_skeleton_export(G_DBUS_INTERFACE_SKELETON(ma_dbus_object), + conn, MA_DBUS_OBJPATH, &error); + if (FALSE == ret) { + MA_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message); + g_error_free(error); + } + + ret = _ma_dbus_subscribe_name_owner_changed(conn); + if (MA_ERROR_NONE != ret) { + MA_LOGE("_ma_dbus_subscribe_name_owner_changed() Fail(%d)", ret); + return; + } +} + +static void _ma_dbus_on_name_lost(GDBusConnection *conn, const gchar *name, + gpointer user_data) +{ + NOTUSED(conn); + NOTUSED(user_data); + + MA_LOGD("Lost the name %s", name); +} + +static void _ma_dbus_on_name_acquired(GDBusConnection *conn, const gchar *name, + gpointer user_data) +{ + NOTUSED(conn); + NOTUSED(user_data); + + MA_LOGD("Acquired the name %s", name); +} + +static gboolean _ma_dbus_handle_enable(Enabler *object, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + __MA_LOG_FUNC_ENTER__; + + /* Do API response first */ + enabler_complete_enable(object, invocation, ret); + service->ma_activated = TRUE; + + /* Intialize subowner client */ + ret = ma_request_enable(service); + if (MA_ERROR_NONE != ret) { + MA_LOGE("ma_request_enable() Fail(%d)", ret); + return FALSE; + } + + /* Notify d2ds enabled */ + net_ma_emit_subowner_enabled(ma_dbus_get_object(), ret); + + __MA_LOG_FUNC_EXIT__; + + return TRUE; +} + +static gboolean _ma_dbus_handle_disable(Enabler *object, + GDBusMethodInvocation *invocation, + gpointer user_data) +{ + int ret = MA_ERROR_NONE; + ma_service *service = (ma_service *)user_data; + + __MA_LOG_FUNC_ENTER__; + + /* Make response first */ + enabler_complete_disable(object, invocation, ret); + + /* De-Intialize subowner client */ + ret = ma_request_disable(service); + if (MA_ERROR_NONE != ret) { + MA_LOGE("ma_request_disable() Fail(%d)", ret); + return FALSE; + } + + /* Terminate daemon */ + ma_service_exit(service); + + return TRUE; +} + +static void _ma_dbus_on_activator_bus_acquired(GDBusConnection *conn, + const gchar *name, gpointer user_data) +{ + gboolean ret; + GError *error = NULL; + ma_service *service = (ma_service *)user_data; + + NOTUSED(name); + + __MA_LOG_FUNC_ENTER__; + + ma_activator_dbus_object = enabler_skeleton_new(); + if (NULL == ma_activator_dbus_object) { + MA_LOGE("enabler_skeleton_new() Fail"); + return; + } + + g_signal_connect(ma_activator_dbus_object, "handle-enable", + G_CALLBACK(_ma_dbus_handle_enable), service); + g_signal_connect(ma_activator_dbus_object, "handle-disable", + G_CALLBACK(_ma_dbus_handle_disable), service); + + ret = g_dbus_interface_skeleton_export( + G_DBUS_INTERFACE_SKELETON(ma_activator_dbus_object), + conn, MA_DBUS_ENABLER_OBJPATH, &error); + if (FALSE == ret) { + MA_LOGE("g_dbus_interface_skeleton_export() Fail(%s)", error->message); + g_error_free(error); + } + + __MA_LOG_FUNC_EXIT__; + +} + +static gboolean _ma_dbus_interface_init(ma_service *service) +{ + guint id; + guint activation_dbus_id; + ma_check_null_ret_error("service", service, FALSE); + + id = g_bus_own_name(G_BUS_TYPE_SYSTEM, + MA_DBUS_INTERFACE, + G_BUS_NAME_OWNER_FLAGS_REPLACE, + _ma_dbus_on_bus_acquired, + _ma_dbus_on_name_acquired, + _ma_dbus_on_name_lost, + service, + NULL); + if (0 == id) { + MA_LOGE("g_bus_own_name() Fail"); + return FALSE; + } + + /* Get D-Bus owner to activate subowner service daemon */ + activation_dbus_id = g_bus_own_name(G_BUS_TYPE_SYSTEM, + MA_DBUS_INTERFACE".enabler", + G_BUS_NAME_OWNER_FLAGS_REPLACE, + _ma_dbus_on_activator_bus_acquired, + NULL, + NULL, + service, + NULL); + + service->dbus_id = id; + service->activation_dbus_id = activation_dbus_id; + + return TRUE; +} + +void ma_notify_mot_env_devs(GVariant *devices) +{ + /* Notify d2ds enabled */ + net_ma_emit_disc_mot_enb_devs_done(ma_dbus_get_object(), devices); +} + +void ma_notify_mowned_devs(GVariant *devices) +{ + /* Notify d2ds enabled */ + net_ma_emit_disc_mowned_devs_done(ma_dbus_get_object(), devices); +} + +static void _ma_dbus_deinit(ma_service *service) +{ + ma_check_null_ret("service", service); + + g_bus_unown_name(service->dbus_id); + g_bus_unown_name(service->activation_dbus_id); +} + +gboolean ma_service_interface_init(ma_service *service) +{ + guint ret; + ma_check_null_ret_error("service", service, FALSE); + + __MA_LOG_FUNC_ENTER__; + + /* Initialize dbus interface */ + ret = _ma_dbus_interface_init(service); + if (FALSE == ret) { + MA_LOGE("%s failed!!!", __func__); + return FALSE; + } + + __MA_LOG_FUNC_EXIT__; + + return TRUE; +} + +void ma_service_interface_deinit(ma_service *service) +{ + ma_check_null_ret("service", service); + + /* De-initialize dbus interface */ + _ma_dbus_deinit(service); +} + diff --git a/src/mot-agent/ma-service-interface.h b/src/mot-agent/ma-service-interface.h new file mode 100644 index 0000000..e608201 --- /dev/null +++ b/src/mot-agent/ma-service-interface.h @@ -0,0 +1,58 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __MA_SERVICE_INTERFACE_H__ +#define __MA_SERVICE_INTERFACE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/**< Mesh D-BUS service*/ +#define MA_DBUS_SERVICE "net.ma" + +/**< Mesh D-BUS service path */ +#define MA_DBUS_PATH "/net/ma" + +#ifndef MA_DBUS_INTERFACE +#define MA_DBUS_INTERFACE "net.ma" +#endif + +#ifndef MA_DBUS_OBJPATH +#define MA_DBUS_OBJPATH "/net/ma" +#endif + +#ifndef MA_DBUS_ENABLER_OBJPATH +#define MA_DBUS_ENABLER_OBJPATH "/net/ma/enabler" +#endif + +struct _NetMa; +typedef struct _NetMa NetMa; + +NetMa* ma_dbus_get_object(); +gboolean ma_service_interface_init(ma_service *service); +void ma_service_interface_deinit(ma_service *service); +void ma_notify_mot_env_devs(GVariant *devices); +void ma_notify_mowned_devs(GVariant *devices); + +#ifdef __cplusplus +} +#endif + +#endif /* __MA_SERVICE_INTERFACE_H__ */ + diff --git a/src/mot-agent/ma-service.c b/src/mot-agent/ma-service.c new file mode 100644 index 0000000..1579f01 --- /dev/null +++ b/src/mot-agent/ma-service.c @@ -0,0 +1,71 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include + +#include "ma.h" +#include "ma-log.h" +#include "ma-util.h" + +ma_service *ma_service_new() +{ + ma_service *service; + + service = g_malloc0(sizeof(struct _ma_service)); + + /* Create g-main loop */ + service->main_loop = g_main_loop_new(NULL, FALSE); + if (G_UNLIKELY(NULL == service->main_loop)) { + MA_LOGE("g-main loop creation failed!!!"); + g_free(service); + return NULL; + } + + return service; +} + +void ma_service_free(ma_service *service) +{ + ma_check_null_ret("service", service); + + /* Unref 'g-main loop' */ + if (service->main_loop) + g_main_loop_unref(service->main_loop); + + g_free(service); +} + +gboolean ma_service_run(ma_service *service) +{ + ma_check_null_ret_error("service", service, FALSE); + ma_check_null_ret_error("service->main_loop", service->main_loop, FALSE); + + g_main_loop_run(service->main_loop); + + return TRUE; +} + +gboolean ma_service_exit(ma_service *service) +{ + ma_check_null_ret_error("service", service, FALSE); + ma_check_null_ret_error("service->main_loop", service->main_loop, FALSE); + + g_main_loop_quit(service->main_loop); + + return TRUE; +} diff --git a/src/mot-agent/ma-service.h b/src/mot-agent/ma-service.h new file mode 100644 index 0000000..48e5120 --- /dev/null +++ b/src/mot-agent/ma-service.h @@ -0,0 +1,36 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __MA_SERVICE_H__ +#define __MA_SERVICE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +ma_service* ma_service_new(); +void ma_service_free(ma_service *service); +gboolean ma_service_run(ma_service *service); +gboolean ma_service_exit(ma_service *service); + +#ifdef __cplusplus +} +#endif + +#endif /* __MA_SERVICE_H__ */ + diff --git a/src/mot-agent/ma-subowner.c b/src/mot-agent/ma-subowner.c new file mode 100644 index 0000000..a4c185b --- /dev/null +++ b/src/mot-agent/ma-subowner.c @@ -0,0 +1,2123 @@ +/****************************************************************** + * + * Copyright 2016 Samsung Electronics All Rights Reserved. + * + * + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + *****************************************************************/ +#include +#include +#include +#ifdef HAVE_UNISTD_H +#include +#endif +#include +#include + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#include "ma.h" +#include "ma-log.h" +#include "ma-util.h" +#include "ma-subowner.h" +#include "ma-service-interface.h" +#include "ma-generated-code.h" +#include "ma-db.h" + +#define WIFI_MAC_ADDR_LENGTH 17 + +#define MAX_FILE_PATH_LEN 1024 +#define ACL_RESRC_MAX_LEN 128 + +/*This value is used only for sample (not OCF spec) */ +#define ACL_RESRC_ARRAY_SIZE 3 + +#define DISCOVERY_TIMEOUT 3 /**< 3 sec */ +#define CALLBACK_TIMEOUT_10S 10 * 1000 /**< 10 sec = 10 * 1000 * 1ms */ +#define CALLBACK_TIMEOUT_5S 5 * 1000 /**< 5sec = 5 * 1000 * 1ms */ + +#define TIMEOUT_USED + +/* '_' for separaing from the same constant variable in srmresourcestrings.c */ +static const char* SVR_DB_FILE_NAME = "oic_svr_db_ma.dat"; +static const char* PRVN_DB_FILE_NAME = "oic_pdm_ma.db"; + +#ifdef TEST +static const OicSecPrm_t SUPPORTED_PRMS[2] = { PRM_PRE_CONFIGURED, PRM_RANDOM_PIN }; +#endif + +OCStackResult GetDoxmDevOwnerId(OicUuid_t *devownerid); + +typedef struct { + ma_cmd_id_e cid; /**< Command ID */ + int tid; /**< Timer ID */ + GThread *thread; /**< Thread handle */ + unsigned int sid; /**< Subcribed signal ID */ + bool found; /**< Whether we discoverd devices */ + void *cb; /**< Callback function pointer */ + void *userdata; /**< User data */ + + /* To-Do : Change belows to union type */ + + /* For MOT */ + gchar *uuid_target_str; /**< Target device*/ + gchar *pin; /**< PIN */ + + /* For ACL provisioning */ + gchar *uuid_subject_str;/**< Subject device for ACL */ + gchar *rsrc_uri; /**< Resource URI */ + gchar *rsrc_type; /**< Resouce Type */ + gchar *rsrc_interface; /**< Resource Interface Array To-Do : Make this as array */ + int permission;/**< Permision */ + + /* For CRED provisioning */ + gchar *uuid_dev1; /**< 1st device for CRED provisiong */ + gchar *uuid_dev2; /**< 2nd device for CRED provisiong */ + + /* For Pairwaise linking */ + gchar *target_1; /**< Target device 1*/ + gchar *target_2; /**< Target device 1*/ + + gchar *subject_1; /**< Target device for ACL */ + gchar *rsrc_uri_1; /**< Resource URI */ + gchar *rsrc_type_1; /**< Resouce Type */ + gchar *rsrc_interface_1; /**< Resource Interface Array To-Do : Make this as array */ + int permission_1;/**< Permision */ + gchar *subject_2; /**< Target device for ACL */ + gchar *rsrc_uri_2; /**< Resource URI */ + gchar *rsrc_type_2; /**< Resouce Type */ + gchar *rsrc_interface_2; /**< Resource Interface Array To-Do : Make this as array */ + int permission_2;/**< Permision */ + + /* For unpair */ + gchar *target_to_revoke; /**< Target device to revoke */ + +} ma_req_cb_s; + +typedef struct _ma_subowner { + OCProvisionDev_t* g_motdev_list; /**< Discoverd MOT enabled devices list */ + OCProvisionDev_t* g_mowned_list; /**< My owned MOT enabled devices list */ + + int g_motdev_cnt; /**< Number of MOT enabled devices */ + int g_mowndev_cnt; /**< Number of my owned MOT enabled devices */ + + bool g_doneCB; /**< This means callback completed or not */ + + char *rnd_pin; /**< Random PIN */ + int rnd_pin_len; /**< Random PIN length */ + +} ma_subowner_s; + +ma_subowner_s *g_client; + +#define CASE_TO_STR(x) case x: return #x; +const char* _error_to_string(OCStackResult err) +{ + switch (err) { + /* CHECK: List all enum values here */ + CASE_TO_STR(OC_STACK_OK) + CASE_TO_STR(OC_STACK_RESOURCE_CREATED) + CASE_TO_STR(OC_STACK_RESOURCE_DELETED) + CASE_TO_STR(OC_STACK_CONTINUE) + CASE_TO_STR(OC_STACK_RESOURCE_CHANGED) + CASE_TO_STR(OC_STACK_INVALID_URI) + CASE_TO_STR(OC_STACK_INVALID_QUERY) + CASE_TO_STR(OC_STACK_INVALID_IP) + CASE_TO_STR(OC_STACK_INVALID_PORT) + CASE_TO_STR(OC_STACK_INVALID_CALLBACK) + CASE_TO_STR(OC_STACK_INVALID_METHOD) + CASE_TO_STR(OC_STACK_INVALID_PARAM) + CASE_TO_STR(OC_STACK_INVALID_OBSERVE_PARAM) + + + CASE_TO_STR(OC_STACK_NO_MEMORY) + CASE_TO_STR(OC_STACK_COMM_ERROR) + CASE_TO_STR(OC_STACK_TIMEOUT) + CASE_TO_STR(OC_STACK_ADAPTER_NOT_ENABLED) + CASE_TO_STR(OC_STACK_NOTIMPL) + CASE_TO_STR(OC_STACK_RESOURCE_ERROR) + CASE_TO_STR(OC_STACK_SLOW_RESOURCE) + CASE_TO_STR(OC_STACK_DUPLICATE_REQUEST) + CASE_TO_STR(OC_STACK_OBSERVER_NOT_FOUND) + CASE_TO_STR(OC_STACK_VIRTUAL_DO_NOT_HANDLE) + CASE_TO_STR(OC_STACK_INVALID_OPTION) + CASE_TO_STR(OC_STACK_MALFORMED_RESPONSE) + CASE_TO_STR(OC_STACK_PERSISTENT_BUFFER_REQUIRED) + + CASE_TO_STR(OC_STACK_INVALID_REQUEST_HANDLE) + CASE_TO_STR(OC_STACK_INVALID_DEVICE_INFO) + CASE_TO_STR(OC_STACK_INVALID_JSON) + CASE_TO_STR(OC_STACK_UNAUTHORIZED_REQ) + CASE_TO_STR(OC_STACK_TOO_LARGE_REQ) + CASE_TO_STR(OC_STACK_PDM_IS_NOT_INITIALIZED) + CASE_TO_STR(OC_STACK_DUPLICATE_UUID) + CASE_TO_STR(OC_STACK_INCONSISTENT_DB) + CASE_TO_STR(OC_STACK_SVR_DB_NOT_EXIST) + CASE_TO_STR(OC_STACK_AUTHENTICATION_FAILURE) + CASE_TO_STR(OC_STACK_NOT_ALLOWED_OXM) +#ifdef WITH_PRESENCE + CASE_TO_STR(OC_STACK_PRESENCE_STOPPED) + CASE_TO_STR(OC_STACK_PRESENCE_TIMEOUT) + CASE_TO_STR(OC_STACK_PRESENCE_DO_NOT_HANDLE) +#endif + CASE_TO_STR(OC_STACK_USER_DENIED_REQ) + CASE_TO_STR(OC_STACK_NOT_ACCEPTABLE) + CASE_TO_STR(OC_STACK_METHOD_NOT_ALLOWED) + CASE_TO_STR(OC_STACK_FORBIDDEN_REQ) + CASE_TO_STR(OC_STACK_INTERNAL_SERVER_ERROR) + CASE_TO_STR(OC_STACK_NOT_IMPLEMENTED) + CASE_TO_STR(OC_STACK_BAD_GATEWAY) + CASE_TO_STR(OC_STACK_SERVICE_UNAVAILABLE) + CASE_TO_STR(OC_STACK_GATEWAY_TIMEOUT) + CASE_TO_STR(OC_STACK_PROXY_NOT_SUPPORTED) + CASE_TO_STR(OC_STACK_ERROR) + default : + return "Unknown Error"; + } +} + +static const char* _wifi_error_to_string(wifi_manager_error_e err) +{ + switch (err) { + /* CHECK: List all enum values here */ + CASE_TO_STR(WIFI_MANAGER_ERROR_NONE) + CASE_TO_STR(WIFI_MANAGER_ERROR_INVALID_PARAMETER) + CASE_TO_STR(WIFI_MANAGER_ERROR_OUT_OF_MEMORY) + CASE_TO_STR(WIFI_MANAGER_ERROR_INVALID_OPERATION) + CASE_TO_STR(WIFI_MANAGER_ERROR_ADDRESS_FAMILY_NOT_SUPPORTED) + CASE_TO_STR(WIFI_MANAGER_ERROR_OPERATION_FAILED) + CASE_TO_STR(WIFI_MANAGER_ERROR_NO_CONNECTION) + CASE_TO_STR(WIFI_MANAGER_ERROR_NOW_IN_PROGRESS) + CASE_TO_STR(WIFI_MANAGER_ERROR_ALREADY_EXISTS) + CASE_TO_STR(WIFI_MANAGER_ERROR_OPERATION_ABORTED) + CASE_TO_STR(WIFI_MANAGER_ERROR_DHCP_FAILED) + CASE_TO_STR(WIFI_MANAGER_ERROR_INVALID_KEY) + CASE_TO_STR(WIFI_MANAGER_ERROR_NO_REPLY) + CASE_TO_STR(WIFI_MANAGER_ERROR_SECURITY_RESTRICTED) + CASE_TO_STR(WIFI_MANAGER_ERROR_ALREADY_INITIALIZED) + CASE_TO_STR(WIFI_MANAGER_ERROR_PERMISSION_DENIED) + CASE_TO_STR(WIFI_MANAGER_ERROR_NOT_SUPPORTED) + default : + return "WIFI_MANAGER_ERROR_UNKNOWN"; + } +} + +OicUuid_t* _convert_uuid(gchar *device_id) +{ + OicUuid_t *uuid; + + if (NULL == device_id) + return NULL; + + uuid = g_malloc0(sizeof(struct OicUuid)); + if (NULL == uuid) { + MA_LOGE("g_malloc0() Fail(%d)", errno); + return NULL; + } + + sscanf(&device_id[0], "%2hhx", &uuid->id[0]); + sscanf(&device_id[2], "%2hhx", &uuid->id[1]); + sscanf(&device_id[4], "%2hhx", &uuid->id[2]); + sscanf(&device_id[6], "%2hhx", &uuid->id[3]); + /* device_id[8] == '-' */ + sscanf(&device_id[9], "%2hhx", &uuid->id[4]); + sscanf(&device_id[11], "%2hhx", &uuid->id[5]); + /* device_id[13] == '-' */ + sscanf(&device_id[14], "%2hhx", &uuid->id[6]); + sscanf(&device_id[16], "%2hhx", &uuid->id[7]); + /* device_id[18] == '-' */ + sscanf(&device_id[19], "%2hhx", &uuid->id[8]); + sscanf(&device_id[21], "%2hhx", &uuid->id[9]); + /* device_id[23] == '-' */ + sscanf(&device_id[24], "%2hhx", &uuid->id[10]); + sscanf(&device_id[26], "%2hhx", &uuid->id[11]); + sscanf(&device_id[28], "%2hhx", &uuid->id[12]); + sscanf(&device_id[30], "%2hhx", &uuid->id[13]); + sscanf(&device_id[32], "%2hhx", &uuid->id[14]); + sscanf(&device_id[34], "%2hhx", &uuid->id[15]); + + return uuid; +} + +static void _print_no_uuid(int idx, const OicUuid_t* uuid) +{ + char uuid_string[256] = {0}; + snprintf(uuid_string, sizeof(uuid_string), + "[%d] %02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + idx, + (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], + (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], + (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], + (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); + MA_LOGI("%s", uuid_string); +} +#ifdef DEBUG_UUID +static void _print_uuid(const OicUuid_t* uuid) +{ + char uuid_string[256] = {0}; + snprintf(uuid_string, sizeof(uuid_string), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], + (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], + (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], + (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); + MA_LOGI("%s", uuid_string); +} +#endif + +static void _print_uuid_res(const OicUuid_t* uuid, int res) +{ + char uuid_string[256] = {0}; + snprintf(uuid_string, sizeof(uuid_string), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x : [%d] [%s]", + (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], + (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], + (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], + (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15], + res, _error_to_string(res)); + MA_LOGI("%s", uuid_string); +} + +#ifdef TEST +static OCProvisionDev_t* _get_dev_by_id(const OCProvisionDev_t* dev_lst, const int dev_num) +{ + if (!dev_lst || 0>=dev_num) { + MA_LOGI("Device List is Empty..\n"); + return NULL; + } + + OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst; + for (int i=0; lst; ) { + if (dev_num == ++i) { + return lst; + } + lst = lst->next; + } + + return NULL; +} +#endif + +static OCProvisionDev_t* _get_dev_by_uuid(const OCProvisionDev_t* dev_lst, + const OicUuid_t* uuid) +{ + if (!dev_lst ) { + MA_LOGI("dev_lst is empty"); + return NULL; + } + + if (!uuid ) { + MA_LOGI("uuid is NULL"); + return NULL; + } + + OCProvisionDev_t* lst = (OCProvisionDev_t*)dev_lst; +#ifdef DEBUG_UUID + _print_uuid(&lst->doxm->deviceID); +#endif + while (lst) { + if (!memcmp((const void *)&lst->doxm->deviceID, (const void *)uuid, sizeof(OicUuid_t))) + return lst; +#ifdef DEBUG_UUID + _print_uuid(uuid); +#endif + lst = lst->next; + } + + return NULL; +} + +static int _print_dev_list(const OCProvisionDev_t* dev_lst) +{ + if (!dev_lst) { + MA_LOGI("Device List is Empty..\n\n"); + return 0; + } + + OCProvisionDev_t* lst = (OCProvisionDev_t*) dev_lst; + int lst_cnt = 0; + for ( ; lst; ) { + _print_no_uuid(++lst_cnt, (const OicUuid_t*) &lst->doxm->deviceID); + lst = lst->next; + } + + return lst_cnt; +} + +static int _print_result_list(const OCProvisionResult_t* rslt_lst, const int rslt_cnt) +{ + if (!rslt_lst || 0>=rslt_cnt) { + MA_LOGI(" Device List is Empty..\n\n"); + return 0; + } + + int lst_cnt = 0; + for ( ; rslt_cnt>lst_cnt; ++lst_cnt) { + _print_uuid_res((const OicUuid_t*) &rslt_lst[lst_cnt].deviceId, rslt_lst[lst_cnt].res); + } + + return lst_cnt; +} + +static FILE* _fopen_prvn_mng(const char* path, const char* mode) +{ + char data_dir[MAX_FILE_PATH_LEN] = {0,}; + + snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s", + tzplatform_getenv(TZ_SYS_GLOBALUSER_DATA), SVR_DB_FILE_NAME); + + NOTUSED(path); +#if 0 + MA_LOGD("Unsed DB path %s\n", path); +#endif + + return fopen(data_dir, mode); +} + +static int _wait_cb_ret(int msec) +{ + OCStackResult ret = OC_STACK_OK; + + ma_check_null_ret_error("g_client", g_client, OC_STACK_INVALID_PARAM); + + for (int i=0; !g_client->g_doneCB && msec > i; ++i) { + usleep(1000); + ret = OCProcess(); + if (OC_STACK_OK != ret) { + MA_LOGE( "OCStack process error = %d", ret); + return ret; + } + } + + if (!g_client->g_doneCB) + OCPDMCleanupForTimeout(); + + return ret; +} + +static void _input_pin_cb(char* pin, size_t len) +{ + ma_check_null_ret("g_client", g_client); + + if (!pin || OXM_RANDOM_PIN_MAX_SIZE>=len) { + MA_LOGE( "inputPinCB invalid parameters"); + return; + } + + memcpy(pin, g_client->rnd_pin, g_client->rnd_pin_len); + MA_LOGI("Random PIN : %s", g_client->rnd_pin); +} + +static void _remove_mot_client() +{ + GMutex motdev_mutex; + + ma_check_null_ret("g_client", g_client); + + g_mutex_init(&motdev_mutex); + g_mutex_lock(&motdev_mutex); + if (g_client->g_motdev_list) { + OCDeleteDiscoveredDevices(g_client->g_motdev_list); + g_client->g_motdev_list = NULL; + } + g_mutex_unlock(&motdev_mutex); +} + +static void _remove_owned_client() +{ + GMutex owned_dev_mutex; + + ma_check_null_ret("g_client", g_client); + + /* delete un/owned device lists before updating them */ + g_mutex_init(&owned_dev_mutex); + g_mutex_lock(&owned_dev_mutex); + if (g_client->g_mowned_list) { + OCDeleteDiscoveredDevices(g_client->g_mowned_list); + g_client->g_mowned_list = NULL; + } + g_mutex_unlock(&owned_dev_mutex); +} + +static int _set_device_id_seed(void) +{ + int ret = WIFI_MANAGER_ERROR_NONE; + gchar *mac_addr = NULL; + gchar *test_mac = "00:12:36:11:22:33"; + + wifi_manager_h wifi_handle = NULL; + bool wifi_activated = false; + + /* Initialize Wi-Fi driver */ + ret = wifi_manager_initialize(&wifi_handle); + if (WIFI_MANAGER_ERROR_NONE != ret) + MA_LOGE("Failed to get wifi manager handle ! [%d][%s]", ret, _wifi_error_to_string(ret)); + + wifi_manager_is_activated(wifi_handle, &wifi_activated); + if (false == wifi_activated) { + ret = wifi_manager_activate(wifi_handle, NULL, NULL); + if (WIFI_MANAGER_ERROR_NONE != ret) + MA_LOGE("Failed to activate wifi ! [%d][%s]", ret, _wifi_error_to_string(ret)); + } + + ret = wifi_manager_get_mac_address(wifi_handle, &mac_addr); + if (WIFI_MANAGER_ERROR_NONE != ret) { + MA_LOGE("Fail to get MAC address [%d][%s]", ret, _wifi_error_to_string(ret)); + /* Make deviceID based on Fake MAC address */ + ret = SetDeviceIdSeed((const guchar *)test_mac, WIFI_MAC_ADDR_LENGTH); + } else { + /* Make deviceID based on Wi-Fi MAC address */ + ret = SetDeviceIdSeed((const guchar *)mac_addr, WIFI_MAC_ADDR_LENGTH); + g_free(mac_addr); + } + if (OC_STACK_OK != ret ) { + MA_LOGE( "SetDeviceIdSeed() error = [%d][%s]", ret, _error_to_string(ret)); + } + wifi_manager_deinitialize(wifi_handle); + return ret; +} + +static int _init_provision_client(void) +{ + int ret = OC_STACK_OK; + char data_dir[MAX_FILE_PATH_LEN] = {0,}; + + snprintf(data_dir, MAX_FILE_PATH_LEN, "%s/network/%s", + tzplatform_getenv(TZ_SYS_GLOBALUSER_DATA), PRVN_DB_FILE_NAME); + + /* initialize persistent storage for SVR DB */ + static OCPersistentStorage ps = { _fopen_prvn_mng, fread, fwrite, fclose, unlink }; + + ret = OCRegisterPersistentStorageHandler(&ps); + if (OC_STACK_OK != ret ) { + MA_LOGE( "OCRegisterPersistentStorageHandler error = %d", ret); + return ret; + } + + _set_device_id_seed(); + + /* initialize OC stack and provisioning manager */ + ret = OCInit(NULL, 0, OC_CLIENT_SERVER); + if (OC_STACK_OK != ret ) { + MA_LOGE( "OCInit() error = [%d][%s]", ret, _error_to_string(ret)); + return ret; + } + + /* Do Self-Ownership Transfer */ + ret = OCConfigSelfOwnership(); + if (OC_STACK_OK != ret ) { + MA_LOGE( "OCConfigSelfOwnership() error = [%d][%s]", ret, _error_to_string(ret)); + //return ret; + } + + ret = OCInitPM(data_dir); + if (OC_STACK_OK != ret ) { + MA_LOGE( "OCInitPM() error = [%d][%s]", ret, _error_to_string(ret)); + return ret; + } + + SetInputPinCB(_input_pin_cb); + + g_client = g_malloc0(sizeof(ma_subowner_s)); + if (!g_client) { + MA_LOGE( "memory allocation error = %d", errno); + return MA_ERROR_OUT_OF_MEMORY; + } + g_client->g_motdev_list = NULL; + g_client->g_mowned_list = NULL; + g_client->rnd_pin = NULL; + + return ret; +} +static void _request_cleanup(gpointer data) +{ + ma_req_cb_s *con = (ma_req_cb_s *)data; + ma_check_null_ret("con", con); + ma_service *service = (ma_service *)con->userdata; + ma_check_null_ret("service", service); + + if (!con) { + MA_LOGI("container is null"); + return; + } + + if (con->tid) { + g_source_remove(con->tid); + con->tid = 0; + } + con->sid = 0; + con->found = FALSE; + + if (con->pin) { + g_free(con->pin); + con->pin = NULL; + } + if (con->rsrc_uri) { + g_free(con->rsrc_uri); + con->rsrc_uri = NULL; + } + if (con->rsrc_type) { + g_free(con->rsrc_type); + con->rsrc_type = NULL; + } + if (con->rsrc_interface) { + g_free(con->rsrc_interface); + con->rsrc_interface = NULL; + } + if (con->uuid_target_str) { + g_free(con->uuid_target_str); + con->uuid_target_str = NULL; + } + if (con->uuid_dev1) { + g_free(con->uuid_dev1); + con->uuid_dev1 = NULL; + } + if (con->uuid_dev2) { + g_free(con->uuid_dev2); + con->uuid_dev2 = NULL; + } + if (con->target_1) { + g_free(con->target_1); + con->target_1 = NULL; + } + if (con->subject_1) { + g_free(con->subject_1); + con->subject_1 = NULL; + } + if (con->rsrc_uri_1) { + g_free(con->rsrc_uri_1); + con->rsrc_uri_1 = NULL; + } + if (con->rsrc_type_1) { + g_free(con->rsrc_type_1); + con->rsrc_type_1 = NULL; + } + if (con->rsrc_interface_1) { + g_free(con->rsrc_interface_1); + con->rsrc_interface_1 = NULL; + } + if (con->target_2) { + g_free(con->target_1); + con->target_1 = NULL; + } + if (con->subject_2) { + g_free(con->subject_1); + con->subject_1 = NULL; + } + if (con->rsrc_uri_2) { + g_free(con->rsrc_uri_2); + con->rsrc_uri_2 = NULL; + } + if (con->rsrc_type_2) { + g_free(con->rsrc_type_2); + con->rsrc_type_2 = NULL; + } + if (con->rsrc_interface_2) { + g_free(con->rsrc_interface_2); + con->rsrc_interface_2 = NULL; + } + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 0); + + if (con) { + g_free(con); + con = NULL; + } +} + +static void __notify_found_devs(void *data) +{ + GVariantBuilder builder; + GVariant* devices; + OCProvisionDev_t *iter = NULL; + + ma_req_cb_s *con = (ma_req_cb_s *)data; + ma_check_null_ret("con", con); + ma_service *service= (ma_service *)con->userdata; + ma_check_null_ret("service", service); + + /* g_mowned_list would be filled above request */ + g_variant_builder_init(&builder, G_VARIANT_TYPE("aa{sv}")); + + if (MA_DISC_MOT_ENB_DEVS == con->cid) { + iter = g_client->g_motdev_list; + g_client->g_motdev_cnt = _print_dev_list(g_client->g_motdev_list); + MA_LOGD("Found %d MOT enabled devices", g_client->g_motdev_cnt); + } else if (MA_DISC_MOWNED_DEVS == con->cid) { + iter = g_client->g_mowned_list; + g_client->g_mowndev_cnt = _print_dev_list(g_client->g_mowned_list); + MA_LOGD("Found %d Mowned devices", g_client->g_mowndev_cnt); + } + + while (iter != NULL) { + char uuid_string[256] = {0}; + + OicUuid_t *uuid = &iter->doxm->deviceID; + ma_check_null_ret("uuid", uuid); + + snprintf(uuid_string, sizeof(uuid_string), + "%02x%02x%02x%02x-%02x%02x-%02x%02x-%02x%02x-%02x%02x%02x%02x%02x%02x", + (*uuid).id[0], (*uuid).id[1], (*uuid).id[2], (*uuid).id[3], + (*uuid).id[4], (*uuid).id[5], (*uuid).id[6], (*uuid).id[7], + (*uuid).id[8], (*uuid).id[9], (*uuid).id[10], (*uuid).id[11], + (*uuid).id[12], (*uuid).id[13], (*uuid).id[14], (*uuid).id[15]); + + MA_LOGI("uuid %s", uuid_string); + + g_variant_builder_open(&builder, G_VARIANT_TYPE_VARDICT); + + g_variant_builder_add(&builder, "{sv}", "deviceId", + g_variant_new_string(uuid_string)); + g_variant_builder_add(&builder, "{sv}", "adapter", + g_variant_new_uint32(iter->endpoint.adapter)); + g_variant_builder_add(&builder, "{sv}", "flags", + g_variant_new_int32(iter->endpoint.flags)); + g_variant_builder_add(&builder, "{sv}", "port", + g_variant_new_uint16(iter->endpoint.port)); + g_variant_builder_add(&builder, "{sv}", "addr", + g_variant_new_string(iter->endpoint.addr)); + g_variant_builder_add(&builder, "{sv}", "ifindex", + g_variant_new_int32(iter->endpoint.ifindex)); + g_variant_builder_add(&builder, "{sv}", "routeData", + g_variant_new_string(iter->endpoint.routeData)); + g_variant_builder_add(&builder, "{sv}", "remoteId", + g_variant_new_string(iter->endpoint.remoteId)); + + MA_LOGI("%s", iter->endpoint.remoteId); + + g_variant_builder_add(&builder, "{sv}", "connType", + g_variant_new_uint32(iter->connType)); + g_variant_builder_add(&builder, "{sv}", "securePort", + g_variant_new_uint16(iter->securePort)); +#ifdef WITH_TCP + g_variant_builder_add(&builder, "{sv}", "tcpPort", + g_variant_new_uint16(iter->tcpPort)); +#endif + g_variant_builder_add(&builder, "{sv}", "secVer", + g_variant_new_string(iter->secVer)); + g_variant_builder_add(&builder, "{sv}", "devStatus", + g_variant_new_uint32(iter->devStatus)); + g_variant_builder_close(&builder); + + iter = iter->next; + } + + devices = g_variant_builder_end(&builder); + + if (MA_DISC_MOT_ENB_DEVS == con->cid) + ma_notify_mot_env_devs(devices); + else if (MA_DISC_MOWNED_DEVS == con->cid) + ma_notify_mowned_devs(devices); + + g_variant_builder_unref(&builder); + g_variant_unref(devices); + +} +#ifdef TIMEOUT_USED +static gboolean _ma_timeout_cb(gpointer data) +{ + ma_req_cb_s *con = (ma_req_cb_s *)data; + ma_check_null_ret_error("con", con, G_SOURCE_REMOVE); + + if (NULL == con) { + MA_LOGE("container is NULL"); + return G_SOURCE_REMOVE; + } + if (con->found) { + MA_LOGE("cb was alreay handled"); + return G_SOURCE_REMOVE; + } + + if(OC_STACK_OK != OCProcess()) { + MA_LOGE( "OCStack process error"); + return G_SOURCE_REMOVE; + } + + OCPDMCleanupForTimeout(); + + switch (con->cid) { + case MA_DISC_MOT_ENB_DEVS: + MA_LOGE("Discovered Multiple Ownership Transfer Enabled Devices"); + __notify_found_devs(con); + break; + case MA_DISC_MOWNED_DEVS: + MA_LOGE("Discovered Multiple Ownership Transfer Enabled Devices"); + __notify_found_devs(con); + break; + case MA_MOT: + net_ma_emit_mot_done(ma_dbus_get_object(), MA_ERROR_OPERATION_FAILED); + break; + case MA_PROV_ACL: + net_ma_emit_acl_done(ma_dbus_get_object(), MA_ERROR_OPERATION_FAILED); + break; + case MA_PROV_CRED: + net_ma_emit_cred_done(ma_dbus_get_object(), MA_ERROR_OPERATION_FAILED); + break; + case MA_REMOVE_MOT: + net_ma_emit_remove_mo_done(ma_dbus_get_object(), MA_ERROR_OPERATION_FAILED); + break; + case MA_UNPAIR: + net_ma_emit_unpair_done(ma_dbus_get_object(), MA_ERROR_OPERATION_FAILED); + break; + case MA_PROV_PAIRWISE: + net_ma_emit_pairwise_done(ma_dbus_get_object(), MA_ERROR_OPERATION_FAILED); + break; + } + + _request_cleanup((gpointer)con); + + return G_SOURCE_REMOVE; +} +#endif +static gpointer _disc_mot_env_devs_func(gpointer data) +{ + ma_req_cb_s *con = (ma_req_cb_s *)data; + ma_check_null_ret_error("con", con, NULL); + + /* delete un/owned device lists before updating them */ + _remove_mot_client(); + + MA_LOGI("Discovering Multiple Ownership Transfer enabled Devices on Network.."); + if (OC_STACK_OK != OCDiscoverMultipleOwnerEnabledDevices(DISCOVERY_TIMEOUT, + &g_client->g_motdev_list)) { + MA_LOGE( "OCDiscoverMultipleOwnerEnalbedDevices API error"); + goto DISC_MOT_ENB_DEVS_END; + } + + __notify_found_devs(con); + +DISC_MOT_ENB_DEVS_END: + _request_cleanup(con); + g_thread_exit(GINT_TO_POINTER (1)); + + return NULL; +} + +static int _disc_mot_enb_devs(ma_service *service) +{ + ma_req_cb_s *con = NULL; + con = g_malloc0(sizeof(ma_req_cb_s)); + if (NULL == con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->thread = g_thread_try_new("disc_mot_env_devs", _disc_mot_env_devs_func, con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); + + con->userdata = service; + con->cid = MA_DISC_MOT_ENB_DEVS; +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(DISCOVERY_TIMEOUT * 2, _ma_timeout_cb, con); +#endif + return MA_ERROR_NONE; +} + +static gpointer _disc_owned_devs_func(gpointer data) +{ + OCStackResult ret = OC_STACK_OK; + ma_req_cb_s *con = (ma_req_cb_s *)data; + + ma_check_null_ret_error("g_client", g_client, NULL); + + /* delete un/owned device lists before updating them */ + _remove_owned_client(); + + MA_LOGD("Discovering Multiple Owned Devices"); + ret = OCDiscoverMultipleOwnedDevices(DISCOVERY_TIMEOUT, &g_client->g_mowned_list); + if (OC_STACK_OK != ret) { + MA_LOGE( "OCDiscoverMultipleOwnerEnabledDevices API error"); + goto DISC_OWNED_DEVS_END; + } + + __notify_found_devs(con); + +DISC_OWNED_DEVS_END: + _request_cleanup(con); + g_thread_exit(GINT_TO_POINTER (1)); + + return NULL; +} + +static int _disc_owned_devs(ma_service *service) +{ + ma_req_cb_s *con = NULL; + con = g_malloc0(sizeof(ma_req_cb_s)); + if (NULL == con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->userdata = service; + con->cid = MA_DISC_MOWNED_DEVS; + + con->thread = g_thread_try_new("disc_owned_devs", _disc_owned_devs_func, con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(DISCOVERY_TIMEOUT * 2, _ma_timeout_cb, con); +#endif + return MA_ERROR_NONE; +} + +static void _mot_cb(void* ctx, int num, OCProvisionResult_t* arr, bool has_error) +{ + int i = 0; + int ret = OC_STACK_OK; + ma_subowner_s *client = (ma_subowner_s *)ctx; + + if (!has_error) { + MA_LOGD("Multiple Ownership Transfer SUCCEEDED"); + } else { + MA_LOGD( "Multiple Ownership Transfer FAILED "); + + _print_result_list((const OCProvisionResult_t*) arr, num); + + ret = openDB(); + if (OC_STACK_OK != ret) + MA_LOGE( "openDB: ret = %d (%s)", ret, _error_to_string(ret)); + + for ( ; num > i; ++i) { + if (OC_STACK_OK !=arr[i].res) { + ret = delete_mowned_device_db((const OicUuid_t*) &arr[i].deviceId); + } + } + ret = closeDB(); + if (OC_STACK_OK != ret) + MA_LOGE( "closeDB: ret = %d (%s)", ret, _error_to_string(ret)); + } + client->g_doneCB = true; +} + +static gpointer _mot_func(gpointer data) +{ + OCStackResult ret = OC_STACK_OK; + ma_req_cb_s *con = (ma_req_cb_s *)data; + + OCProvisionDev_t *src_dev = NULL; + OicUuid_t *uuid_target = NULL; + + if (!con->uuid_target_str) { + MA_LOGE("Target device UUID is NULL"); + goto MOT_ENDED; + } + + uuid_target = _convert_uuid(con->uuid_target_str); + src_dev = _get_dev_by_uuid(g_client->g_motdev_list, uuid_target); + if (!src_dev) { + MA_LOGE("We can't find in MOT dev list"); + goto MOT_ENDED; + } + + g_client->g_doneCB = false; + + OCProvisionDev_t* dev = NULL; + LL_FOREACH(src_dev, dev) { + if(OIC_PRECONFIG_PIN == dev->doxm->oxmSel) { + /* Pre-Configured PIN initialization */ + const char* testPreconfigPin = con->pin;/* Ex) "12341234" */ + ret = OCAddPreconfigPin(dev, testPreconfigPin, strlen(testPreconfigPin)); + if(OC_STACK_OK != ret) { + MA_LOGE("Failed to save the pre-configured PIN"); + MA_LOGE("You can't use the pre-configured PIN OxM for MOT"); + goto MOT_ENDED; + } + } + } + + ret = OCDoMultipleOwnershipTransfer(g_client, src_dev, _mot_cb); + if (OC_STACK_OK != ret ) { + MA_LOGE( "OCDoMultipleOwnershipTransfer: ret = %d (%s)", ret, _error_to_string(ret)); + goto MOT_ENDED; + } + + ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); + if(ret) { + MA_LOGE( "OCDoMultipleOwnershipTransfer callback error = %d (%s)", ret, _error_to_string(ret)); + goto MOT_ENDED; + } + + MA_LOGE("Registered Discovered Devices"); + +MOT_ENDED: + + net_ma_emit_mot_done(ma_dbus_get_object(), (int)ret); + if (uuid_target) + g_free(uuid_target); + _request_cleanup(data); + g_thread_exit(GINT_TO_POINTER (1)); + + return NULL; +} + +static int _ma_mot(ma_service *service, gchar *uuid_str, gchar *pin) +{ + ma_req_cb_s *con = NULL; + + con = g_malloc0(sizeof(ma_req_cb_s)); + if (NULL == con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + /* Unset d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 0); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->userdata = service; + con->cid = MA_MOT; + con->uuid_target_str = g_strdup(uuid_str); + + if (pin) { + con->pin = g_malloc0(OXM_RANDOM_PIN_MAX_SIZE+1); + if (!pin) { + MA_LOGE("Failed to allocate PIN number"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + memcpy(con->pin, pin, OXM_RANDOM_PIN_MAX_SIZE); + } + + con->thread = g_thread_try_new("mot", _mot_func, con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); + +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con); +#endif + return MA_ERROR_NONE; +} + +static void _remove_mo_cb(void* ctx, int num, OCProvisionResult_t* arr, bool has_error) +{ + ma_subowner_s *client = (ma_subowner_s *)ctx; + + if (!has_error) { + MA_LOGD("Remove Multiple Ownership SUCCEEDED"); + } else { + MA_LOGD( "Remove Multiple Ownership FAILED "); + _print_result_list((const OCProvisionResult_t*) arr, num); + } + client->g_doneCB = true; +} + +static gpointer _remove_mo_func(gpointer data) +{ + OCStackResult ret = MA_ERROR_OPERATION_FAILED; + ma_req_cb_s *con = (ma_req_cb_s *)data; + + OCProvisionDev_t *src_dev = NULL; + OicUuid_t uuid_mowner; + OicUuid_t *uuid_target = NULL; + + if (!con->uuid_target_str) { + MA_LOGE("Target device UUID is NULL"); + goto MOT_ENDED; + } + + uuid_target = _convert_uuid(con->uuid_target_str); + src_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid_target); + if (!src_dev) { + MA_LOGE("We can't find in MOT dev list"); + goto MOT_ENDED; + } + + g_client->g_doneCB = false; + + ret = GetDoxmDevOwnerId(&uuid_mowner); + if (OC_STACK_OK != ret) { + MA_LOGE("GetDoxmDevOwnerId faild = [%d][%s]", ret, _error_to_string(ret)); + goto MOT_ENDED; + } + + ret = openDB(); + if (OC_STACK_OK != ret) + MA_LOGE( "openDB: ret = %d (%s)", ret, _error_to_string(ret)); + ret = delete_mowned_device_db((const OicUuid_t*) uuid_target); + if (OC_STACK_OK != ret) + MA_LOGE( "delete_mowned_device_db: ret = %d (%s)", ret, _error_to_string(ret)); + ret = closeDB(); + if (OC_STACK_OK != ret) + MA_LOGE( "closeDB: ret = %d (%s)", ret, _error_to_string(ret)); + + ret = OCRemoveSubOwner(g_client, src_dev, &uuid_mowner, _remove_mo_cb); + if (OC_STACK_OK != ret ) { + MA_LOGE( "OCRemoveSubOwner: ret = %d (%s)", ret, _error_to_string(ret)); + goto MOT_ENDED; + } + + ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); + if(ret) { + MA_LOGE( "OCRemoveSubOwner callback error = %d (%s)", ret, _error_to_string(ret)); + goto MOT_ENDED; + } + + MA_LOGE("Remove Multiple Ownership Done"); + +MOT_ENDED: + + net_ma_emit_remove_mo_done(ma_dbus_get_object(), (int)ret); + + if (uuid_target) + g_free(uuid_target); + _request_cleanup(data); + g_thread_exit(GINT_TO_POINTER (1)); + + return NULL; +} + +static int _ma_remove_mo(ma_service *service, gchar *uuid_str) +{ + ma_req_cb_s *con = NULL; + con = g_malloc0(sizeof(ma_req_cb_s)); + if (NULL == con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + /* Unset d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 0); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->userdata = service; + con->cid = MA_REMOVE_MOT; + con->uuid_target_str = g_strdup(uuid_str); + + con->thread = g_thread_try_new("remove_mo", _remove_mo_func, con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); + +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con); +#endif + return MA_ERROR_NONE; +} + +#if 0 /* TEST */ +static int sendGetLed() +{ + int selDevNum; + char query[256] = {0}; + OCCallbackData cbData; + cbData.cb = &LedCB; + cbData.context = NULL; + cbData.cd = NULL; + + printDevList(g_mowned_list); + + // select device for provisioning access control list + for( ; ; ) { + printf(" > Enter Device Number, for sending GET LED request: "); + for(int ret=0; 1!=ret; ) { + ret = scanf("%d", &selDevNum); + for( ; 0x20<=getchar(); ); // for removing overflow garbages + // '0x20<=code' is character region + } + if(0=selDevNum) { + break; + } + printf(" Entered Wrong Number. Please Enter Again\n"); + } + + OCProvisionDev_t* selDev = _get_dev_by_id(g_mowned_list, selDevNum); + if(NULL == selDev) { + printf("Failed to _get_dev_by_id()\n"); + return -1; + } + + if(PMGenerateQuery(true, selDev->endpoint.addr, selDev->securePort, selDev->connType, + query, sizeof(query), "/a/led")) { + g_doneCB = false; + printf("query=%s\n", query); + if(OC_STACK_OK != OCDoResource(NULL, OC_REST_GET, query, NULL, NULL, selDev->connType, + OC_HIGH_QOS, &cbData, NULL, 0)) { + printf("********************************\n"); + printf("Failed to send GET request to %s\n", query); + printf("********************************\n"); + g_doneCB = true; + return -1; + } + + waitCallbackRet(); + } else { + printf("Failed to generate GET request for /a/led\n"); + return -1; + } + + return 0; +} + +static int sendPutLed() +{ + int selDevNum; + char query[256] = {0}; + OCCallbackData cbData; + cbData.cb = &LedCB; + cbData.context = NULL; + cbData.cd = NULL; + + printDevList(g_mowned_list); + // select device for provisioning access control list + for( ; ; ) { + printf(" > Enter Device Number, for sending PUT LED request: "); + for(int ret=0; 1!=ret; ) { + ret = scanf("%d", &selDevNum); + for( ; 0x20<=getchar(); ); // for removing overflow garbages + // '0x20<=code' is character region + } + if(0=selDevNum) { + break; + } + printf(" Entered Wrong Number. Please Enter Again\n"); + } + + OCProvisionDev_t* selDev = _get_dev_by_id(g_mowned_list, selDevNum); + if (NULL == selDev) { + printf("Failed to _get_dev_by_id()\n"); + return -1; + } + + if (PMGenerateQuery(true, selDev->endpoint.addr, selDev->securePort, selDev->connType, query, + sizeof(query), "/a/led")) { + g_doneCB = false; + MA_LOGI("query=%s\n", query); + if(OC_STACK_OK != OCDoResource(NULL, OC_REST_PUT, query, NULL, NULL, selDev->connType, + OC_LOW_QOS, &cbData, NULL, 0)) { + MA_LOGI("********************************\n"); + MA_LOGI("Failed to send PUT request to %s\n", query); + MA_LOGI("********************************\n"); + g_doneCB = true; + return -1; + } + + waitCallbackRet(); + } else { + printf("Failed to generate PUT request for /a/led\n"); + return -1; + } + + return 0; +} +#endif /* TEST */ +static OicSecAcl_t* _create_target_acl(const OicUuid_t* target, const OicUuid_t* subject, + const char *rsrc_uri, const char *rsrc_type, const char *rsrc_interface, int permission) +{ + /* For example + * subejct = "12341234-1234-1234-12341234" + * rsrc_uri = "/a/led" + * rsrc_type = "oic.r.core" + * rsrc_interface = "oic.if.baseline" + * rsrc_permission = MA_PERMISSION_FULL + */ + size_t len; + OicSecAcl_t* acl = NULL; + OicSecAce_t* ace = NULL; + OicSecRsrc_t* rsrc = NULL; + + if (NULL == target) { + MA_LOGE( "target is NULL"); + return NULL; + } + + if (NULL == subject) { + MA_LOGE( "subejct is NULL"); + return NULL; + } +#ifdef DEBUG_UUID + _print_uuid(target); + _print_uuid(subject); +#endif + acl = (OicSecAcl_t*) OICCalloc(1, sizeof(OicSecAcl_t)); + if (!acl) { + MA_LOGE( "acl : OICCalloc failed =%d", errno); + return NULL; + } + ace = (OicSecAce_t*) OICCalloc(1, sizeof(OicSecAce_t)); + if (!ace) { + MA_LOGE( "ace : OICCalloc failed =%d", errno); + return NULL; + } + LL_APPEND(acl->aces, ace); + memcpy(ace->subjectuuid.id, subject->id, sizeof(subject->id)); + + /* fill the href */ + rsrc = (OicSecRsrc_t*)OICCalloc(1, sizeof(OicSecRsrc_t)); + if (!rsrc) { + MA_LOGE( "rsrc: OICCalloc failed =%d", errno); + goto CREATE_ACL_ERROR; + } + + len = strlen(rsrc_uri)+1; // '1' for null termination + rsrc->href = (char*) OICCalloc(len, sizeof(char)); + if (!rsrc->href) { + MA_LOGE( "rsrc->href: OICCalloc failed =%d", errno); + goto CREATE_ACL_ERROR; + } + memcpy(rsrc->href, rsrc_uri, len); + + /* Fill the resource type (rt) */ + rsrc->typeLen = 1; + rsrc->types = (char**)OICCalloc(1, sizeof(char*)); + if (!rsrc->types) { + MA_LOGE( "rsrc->types: OICCalloc failed =%d", errno); + goto CREATE_ACL_ERROR; + } + rsrc->types[0] = g_strdup(rsrc_type); + if (!rsrc->types[0]) { + MA_LOGE( "rsrc->types[0]: g_strdup failed =%d", errno); + goto CREATE_ACL_ERROR; + } + + /* Fill the interface (if) */ + rsrc->interfaceLen = 1; + rsrc->interfaces = (char**)OICCalloc(1, sizeof(char*)); + if (!rsrc->interfaces) { + MA_LOGE( "rsrc->interfaces: OICCalloc failed =%d", errno); + goto CREATE_ACL_ERROR; + } + rsrc->interfaces[0] = g_strdup(rsrc_interface); + if (!rsrc->interfaces[0]) { + MA_LOGE( "rsrc->interfaces[0] : g_strdup failed =%d", errno); + goto CREATE_ACL_ERROR; + } + + LL_APPEND(ace->resources, rsrc); + + /* Fill permission for the reource */ + ace->permission = permission; + + ace->eownerID = (OicUuid_t*)OICCalloc(1, sizeof(OicUuid_t)); + if (NULL == ace->eownerID) { + MA_LOGE( "ace->eownerID : OICCalloc failed =%d", errno); + goto CREATE_ACL_ERROR; + } + memcpy(ace->eownerID->id, subject->id, sizeof(subject->id)); + + return acl; + +CREATE_ACL_ERROR: + OCDeleteACLList(acl); + return NULL; +} + +static void _provisioning_acl_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) +{ + ma_subowner_s *client = (ma_subowner_s *)ctx; + + if (!has_error) { + MA_LOGD("Provision ACL SUCCEEDED"); + } else { + MA_LOGD( "Provision ACL FAILED "); + _print_result_list((const OCProvisionResult_t*) arr, nOfRes); + } + client->g_doneCB = true; +} + +static void _provisioning_pairwise_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) +{ + ma_subowner_s *client = (ma_subowner_s *)ctx; + + if (!has_error) { + MA_LOGD("Provision pairwise SUCCEEDED"); + } else { + MA_LOGD( "Provision pairwise FAILED "); + _print_result_list((const OCProvisionResult_t*) arr, nOfRes); + } + client->g_doneCB = true; +} + +static void _unpair_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) +{ + ma_subowner_s *client = (ma_subowner_s *)ctx; + + if (!has_error) { + MA_LOGD("unpair SUCCEEDED"); + } else { + MA_LOGD( "unpair FAILED "); + _print_result_list((const OCProvisionResult_t*) arr, nOfRes); + } + client->g_doneCB = true; +} + +static gpointer _provisioning_acl_func(gpointer data) +{ + OCStackResult ret = OC_STACK_OK; + ma_req_cb_s *con = (ma_req_cb_s *)data; + OicSecAcl_t* acl = NULL; + OicUuid_t *uuid = NULL; + OCProvisionDev_t *target_dev = NULL; + OCProvisionDev_t *subject_dev = NULL; + + if (!con->uuid_subject_str ||!con->uuid_target_str || !con->rsrc_uri || !con->rsrc_type || !con->rsrc_interface) { + MA_LOGE("Some parameters are wrong"); + MA_LOGE("%s", con->uuid_target_str ); + MA_LOGE("%s", con->uuid_subject_str ); + MA_LOGE("%s", con->rsrc_uri ); + MA_LOGE("%s", con->rsrc_type ); + MA_LOGE("%s", con->rsrc_interface ); + goto PVACL_ERROR; + } + + uuid = _convert_uuid(con->uuid_target_str); + target_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid); + if (!target_dev) { + MA_LOGE("We can't find target in MOWNED dev list"); + goto PVACL_ERROR; + } + g_free(uuid); + + uuid = _convert_uuid(con->uuid_subject_str); + subject_dev = _get_dev_by_uuid(g_client->g_mowned_list, uuid); + if (!subject_dev) { + MA_LOGE("We can't find subject in MOWNED dev list"); + goto PVACL_ERROR; + } + g_free(uuid); + + g_client->g_doneCB = false; + MA_LOGI(" Provisioning Selected ACL.."); + + acl = _create_target_acl(&target_dev->doxm->deviceID, &subject_dev->doxm->deviceID, con->rsrc_uri, con->rsrc_type, + con->rsrc_interface, con->permission); + if (NULL == acl) { + MA_LOGE( "Failed to create ACL for %s", con->rsrc_uri); + goto PVACL_ERROR; + } + + ret= OCProvisionACL((void*) g_client, target_dev, acl, _provisioning_acl_cb); + if (OC_STACK_OK != ret) { + MA_LOGD( "OCProvisionACL API error: %d (%s)", ret, _error_to_string(ret)); + goto PVACL_ERROR; + } + + ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); + if (ret) { + MA_LOGE( "OCProvisionCredentials Faild = %d", errno); + goto PVACL_ERROR; + } + /* display the ACL-provisioned result */ + MA_LOGI(" Provisioned Selected ACL Successfully"); + +PVACL_ERROR: + /* Notify ACL result */ + net_ma_emit_acl_done(ma_dbus_get_object(), (int)ret); + + if (acl) + OCDeleteACLList(acl); + if (uuid) + g_free(uuid); + _request_cleanup(con); + g_thread_exit(GINT_TO_POINTER (1)); + return NULL; +} + +static int _provisioning_acl(ma_service *service, gchar *target, gchar *subject, + gchar *rsrc_uri, gchar *rsrc_type, gchar *rsrc_interface, int permission) +{ + ma_req_cb_s *con = NULL; + con = g_malloc0(sizeof(ma_req_cb_s)); + if (NULL == con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + /* Unset d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 0); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->userdata = service; + con->cid = MA_PROV_ACL; + con->uuid_target_str = g_strdup(target); + con->uuid_subject_str = g_strdup(subject); + con->rsrc_uri = g_strdup(rsrc_uri); + con->rsrc_type = g_strdup(rsrc_type); + con->rsrc_interface = g_strdup(rsrc_interface); + + con->thread = g_thread_try_new("prov_acl", _provisioning_acl_func, con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con); +#endif + return MA_ERROR_NONE; +} + +static gpointer _provisioning_pairwise_func(gpointer data) +{ + OCStackResult ret = OC_STACK_OK; + ma_req_cb_s *con = (ma_req_cb_s *)data; + + OicUuid_t *uuid_1 = NULL; + OicUuid_t *uuid_2 = NULL; + OicUuid_t *uuid_3 = NULL; + OicUuid_t *uuid_4 = NULL; + + OicSecAcl_t* acl_1 = NULL; + OicSecAcl_t* acl_2 = NULL; + + OCProvisionDev_t *target_dev_1 = NULL; + OCProvisionDev_t *target_dev_2 = NULL; + OCProvisionDev_t *subject_dev_1 = NULL; + OCProvisionDev_t *subject_dev_2 = NULL; + + if (!con->subject_1 || !con->rsrc_uri_1 || !con->rsrc_type_1 || !con->rsrc_interface_1 || + !con->subject_2 || !con->rsrc_uri_2 || !con->rsrc_type_2 || !con->rsrc_interface_2) { + MA_LOGE("Some parameters are wrong"); + MA_LOGE("%s", con->subject_1 ); + MA_LOGE("%s", con->rsrc_uri_1 ); + MA_LOGE("%s", con->rsrc_type_1 ); + MA_LOGE("%s", con->rsrc_interface_1 ); + MA_LOGE("%s", con->subject_2 ); + MA_LOGE("%s", con->rsrc_uri_2 ); + MA_LOGE("%s", con->rsrc_type_2 ); + MA_LOGE("%s", con->rsrc_interface_2 ); + + goto PVACL_ERROR; + } + + uuid_1 = _convert_uuid(con->target_1); + target_dev_1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_1); + if (!target_dev_1) { + MA_LOGE("We can't find target in MOWNED dev list"); + goto PVACL_ERROR; + } + + uuid_2 = _convert_uuid(con->target_2); + target_dev_2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_2); + if (!target_dev_2) { + MA_LOGE("We can't find target in MOWNED dev list"); + goto PVACL_ERROR; + } + + uuid_3 = _convert_uuid(con->subject_1); + subject_dev_1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_3); + if (!subject_dev_1) { + MA_LOGE("We can't find subject in MOWNED dev list"); + goto PVACL_ERROR; + } + + uuid_4 = _convert_uuid(con->subject_2); + subject_dev_2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid_4); + if (!subject_dev_2) { + MA_LOGE("We can't find subject in MOWNED dev list"); + goto PVACL_ERROR; + } + + g_client->g_doneCB = false; + MA_LOGI(" Provisioning Selected Pairwise.."); + + acl_1 = _create_target_acl(&target_dev_1->doxm->deviceID, + &subject_dev_1->doxm->deviceID, con->rsrc_uri_1, + con->rsrc_type_1, + con->rsrc_interface_1, con->permission_1); + if (NULL == acl_1) { + MA_LOGE( "Failed to create ACL for %s", con->rsrc_uri); + goto PVACL_ERROR; + } + + acl_2 = _create_target_acl(&target_dev_2->doxm->deviceID, + &subject_dev_2->doxm->deviceID, con->rsrc_uri_2, + con->rsrc_type_2, + con->rsrc_interface_2, con->permission_2); + if (NULL == acl_2) { + MA_LOGE( "Failed to create ACL for %s", con->rsrc_uri); + goto PVACL_ERROR; + } + + ret = OCProvisionPairwiseDevices((void*) g_client, SYMMETRIC_PAIR_WISE_KEY, + OWNER_PSK_LENGTH_256, target_dev_1, acl_1, + target_dev_2, acl_2, _provisioning_pairwise_cb); + if (OC_STACK_OK != ret) { + MA_LOGD( "OCProvisionPairwiseDevices API error: %d (%s)", ret, + _error_to_string(ret)); + goto PVACL_ERROR; + } + + ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); + if (ret) { + MA_LOGE( "OCProvisionPairwise Faild = %d", errno); + goto PVACL_ERROR; + } + /* display the pairwise-provisioned result */ + MA_LOGI(" Provision pairwise Successfully"); + +PVACL_ERROR: + /* Notify ACL result */ + net_ma_emit_pairwise_done(ma_dbus_get_object(), (int)ret); + + if (uuid_1) + g_free(uuid_1); + if (uuid_2) + g_free(uuid_2); + if (uuid_3) + g_free(uuid_3); + if (uuid_4) + g_free(uuid_4); + if (acl_1) + OCDeleteACLList(acl_1); + if (acl_2) + OCDeleteACLList(acl_2); + + _request_cleanup(con); + g_thread_exit(GINT_TO_POINTER (1)); + return NULL; +} + +static gpointer _unpair_func(gpointer data) +{ + OCStackResult ret = OC_STACK_OK; + ma_req_cb_s *con = (ma_req_cb_s *)data; + + OicUuid_t *uuid = NULL; + + if (!con->target_to_revoke) { + MA_LOGE("Some parameters are wrong"); + MA_LOGE("%s", con->target_to_revoke ); + goto PVACL_ERROR; + } + + uuid = _convert_uuid(con->target_to_revoke); + + g_client->g_doneCB = false; + MA_LOGI(" unpair device.."); + + ret = OCRemoveDeviceWithUuid((void*) g_client, CALLBACK_TIMEOUT_5S, uuid, _unpair_cb ); + if (OC_STACK_OK != ret) { + MA_LOGD( "OCRemoveDevice API error: %d (%s)", ret, _error_to_string(ret)); + goto PVACL_ERROR; + } + + ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); + if (ret) { + MA_LOGE( "OCRemoveDevice Faild = %d", errno); + goto PVACL_ERROR; + } + /* display the pairwise-provisioned result */ + MA_LOGI(" Provision pairwise Successfully"); + +PVACL_ERROR: + /* Notify ACL result */ + net_ma_emit_unpair_done(ma_dbus_get_object(), (int)ret); + if (uuid) + g_free(uuid); + _request_cleanup(con); + g_thread_exit(GINT_TO_POINTER (1)); + return NULL; +} + +static int _provisioning_unpair(ma_service *service, gchar *target) +{ + ma_req_cb_s *con = NULL; + con = g_malloc0(sizeof(ma_req_cb_s)); + if (NULL == con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + /* Unset d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 0); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->userdata = service; + con->cid = MA_UNPAIR; + + con->target_to_revoke = g_strdup(target); + + con->thread = g_thread_try_new("unpair_device", _unpair_func, con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con); +#endif + return MA_ERROR_NONE; +} + +static int _provisioning_pairwise_link(ma_service *service, gchar *target_1, gchar *subject_1, + gchar *rsrc_uri_1, gchar *rsrc_type_1, gchar *rsrc_interface_1, int permission_1, + gchar *target_2, gchar *subject_2,gchar *rsrc_uri_2, gchar *rsrc_type_2, gchar *rsrc_interface_2, int permission_2) +{ + ma_req_cb_s *con = NULL; + con = g_malloc0(sizeof(ma_req_cb_s)); + if (NULL == con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + /* Unset d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 0); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->userdata = service; + con->cid = MA_PROV_PAIRWISE; + + con->target_1 = g_strdup(target_1); + con->subject_1 = g_strdup(subject_1); + con->rsrc_uri_1 = g_strdup(rsrc_uri_1); + con->rsrc_type_1 = g_strdup(rsrc_type_1); + con->rsrc_interface_1 = g_strdup(rsrc_interface_1); + + con->target_2 = g_strdup(target_2); + con->subject_2 = g_strdup(subject_2); + con->rsrc_uri_2 = g_strdup(rsrc_uri_2); + con->rsrc_type_2 = g_strdup(rsrc_type_2); + con->rsrc_interface_2 = g_strdup(rsrc_interface_2); + + con->thread = g_thread_try_new("prov_pairwise", _provisioning_pairwise_func, + con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, + con); +#endif + return MA_ERROR_NONE; +} + + +static void _provision_cred_cb(void* ctx, int nOfRes, OCProvisionResult_t* arr, bool has_error) +{ + ma_subowner_s *client = (ma_subowner_s *)ctx; + + if (!has_error) { + MA_LOGD("Provision ACL SUCCEEDED"); + } else { + MA_LOGD( "Provision ACL FAILED "); + _print_result_list((const OCProvisionResult_t*) arr, nOfRes); + } + client->g_doneCB = true; +} + +static gpointer _cred_provisioning_func(gpointer data) +{ + OCStackResult ret = OC_STACK_OK; + ma_req_cb_s *con = (ma_req_cb_s *)data; + + OicUuid_t *uuid = NULL; + OCProvisionDev_t *dev1 = NULL; + OCProvisionDev_t *dev2 = NULL; + + if (!con->uuid_dev1 ) { + MA_LOGE("dev1 is NULL"); + goto PVPWS_ERROR; + } + if (!con->uuid_dev2) { + MA_LOGE("dev2 is NULL"); + goto PVPWS_ERROR; + } + + uuid = _convert_uuid(con->uuid_dev1); + dev1 = _get_dev_by_uuid(g_client->g_mowned_list, uuid); + if (!dev1) { + MA_LOGE("We can't find in MOWNED dev list"); + goto PVPWS_ERROR; + } + g_free(uuid); + + uuid = _convert_uuid(con->uuid_dev2); + dev2 = _get_dev_by_uuid(g_client->g_mowned_list, uuid); + if (!dev2) { + MA_LOGE("We can't find in MOWNED dev list"); + goto PVPWS_ERROR; + } + g_free(uuid); + + /* To do ACL provisioning only one */ + dev1->next = NULL; + + /* To do ACL provisioning only one */ + dev2->next = NULL; + + g_client->g_doneCB = false; + + MA_LOGI("Provisioning Selected Pairwise Devices.."); + ret = OCProvisionCredentials((void*) g_client, + SYMMETRIC_PAIR_WISE_KEY, OWNER_PSK_LENGTH_256, + dev1, dev2, _provision_cred_cb); + if (OC_STACK_OK != ret) { + MA_LOGD( "OCProvisionPairwiseDevices API error: %d (%s)", ret, _error_to_string(ret)); + goto PVPWS_ERROR; + } + + ret = _wait_cb_ret(CALLBACK_TIMEOUT_5S); + if (ret) { + MA_LOGE( "OCProvisionCredentials Faild = %d", errno); + goto PVPWS_ERROR; + } + + /* display the pairwise-provisioned result */ + MA_LOGI("Provisioned Selected Pairwise Devices"); + + return 0; + +PVPWS_ERROR: + /* Notify ACL result */ + net_ma_emit_cred_done(ma_dbus_get_object(), (int)ret); + + if (uuid) + g_free(uuid); + _request_cleanup(con); + + g_thread_exit(GINT_TO_POINTER (1)); + + return NULL; +} + +static int _provisioning_cred(ma_service *service, + gchar *uuid_dev1, gchar *uuid_dev2) +{ + ma_req_cb_s *con = NULL; + con = g_malloc0(sizeof(ma_req_cb_s)); + if (!con) { + MA_LOGE( "g_malloc0() Fail=%d", errno); + /* Unset d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 0); + return MA_ERROR_OUT_OF_MEMORY; + } + + con->userdata = service; + con->cid = MA_PROV_CRED; + + if (!uuid_dev1 ||!uuid_dev2) { + MA_LOGE( "Invaild Prameters"); + _request_cleanup(con); + return MA_ERROR_INVALID_PARAMETER; + } + + con->uuid_dev1 = g_strdup(uuid_dev1); + con->uuid_dev2 = g_strdup(uuid_dev2); + + con->thread = g_thread_try_new("prov_cred", _cred_provisioning_func, con, NULL); + if (!con->thread) { + MA_LOGE("Failed to create thread"); + _request_cleanup(con); + return MA_ERROR_OUT_OF_MEMORY; + } + g_thread_unref(con->thread); +#ifdef TIMEOUT_USED + con->tid = g_timeout_add_seconds(CALLBACK_TIMEOUT_5S + 1, _ma_timeout_cb, con); +#endif + return MA_ERROR_NONE; +} + +int ma_request_enable(ma_service *service) +{ + int ret = MA_ERROR_NONE; + ma_check_null_ret_error("service", service, FALSE); + ret = _init_provision_client(); + return ret; +} + +int ma_request_disc_mot_enb_devs(ma_service *service) +{ + int ret =MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Discovery MOT enabled devices"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _disc_mot_enb_devs(service); + + return ret; +} + +int ma_request_disc_owned_devs(ma_service *service) +{ + int ret =MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Discovery Owned devices"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _disc_owned_devs(service); + + return ret; +} + +int ma_request_mot(ma_service *service, gchar* uuid_str, gchar *pin) +{ + int ret =MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Mullti Ownership Transfer"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + if (!uuid_str || !pin) { + MA_LOGE("uuid_str = %s", uuid_str); + MA_LOGE("pin = %s", pin); + return MA_ERROR_INVALID_PARAMETER; + } + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _ma_mot(service, uuid_str, pin); + + return ret; +} + +int ma_request_remove_mo(ma_service *service, gchar* uuid_str) +{ + int ret =MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Remove Mulltiple Ownership"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + if (!uuid_str) { + MA_LOGE("uuid_str = %s", uuid_str); + return MA_ERROR_INVALID_PARAMETER; + } + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _ma_remove_mo(service, uuid_str); + + return ret; +} + +int ma_request_prov_acl(ma_service *service, gchar *target, gchar *subject, + gchar *rsrc_uri, gchar *rsrc_type, gchar *rsrc_interface, int permission) +{ + int ret =MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Provisioning ACL"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + if (!target || !subject || !rsrc_uri || !rsrc_type || !rsrc_interface) { + MA_LOGE("target = %s", target); + MA_LOGE("subject = %s", subject); + MA_LOGE("rsrc_uri = %s", rsrc_uri); + MA_LOGE("rsrc_type = %s", rsrc_type); + MA_LOGE("rsrc_interface = %s", rsrc_interface); + return MA_ERROR_INVALID_PARAMETER; + } + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _provisioning_acl(service, target, subject, rsrc_uri, rsrc_type, rsrc_interface, permission); + + return ret; +} + +int ma_request_prov_cred(ma_service *service, gchar *uuid_dev1, gchar *uuid_dev2) +{ + int ret =MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Provisioning Credential"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + if (!uuid_dev1 || !uuid_dev2) { + MA_LOGE("uuid_dev1 = %s", uuid_dev1); + MA_LOGE("uuid_dev2 = %s", uuid_dev2); + return MA_ERROR_INVALID_PARAMETER; + } + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _provisioning_cred(service, uuid_dev1, uuid_dev2); + + return ret; +} + +int ma_request_get_ownerid(ma_service *service, gchar **uuid_str) +{ + int ret =MA_ERROR_NONE; + OicUuid_t uuid; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Get Device ID"); + + ret = GetDoxmDevOwnerId(&uuid); + if (OC_STACK_OK != ret) + MA_LOGE("GetDoxmDevOwnerId faild = [%d][%s]", ret, _error_to_string(ret)); + + ret = ConvertUuidToStr(&uuid, uuid_str); + if (OC_STACK_OK != ret) + MA_LOGE("ConvertUuidToStr faild = [%d][%s]", ret, _error_to_string(ret)); + + return ret; +} + +int ma_request_disable(ma_service *service) +{ + NOTUSED(service); + + if (OC_STACK_OK != OCStop()) { + MA_LOGE( "OCStack stop error"); + } + + OCTerminatePM(); + + if (!g_client) + return MA_ERROR_NONE; + + if (g_client->g_mowned_list) + OCDeleteDiscoveredDevices(g_client->g_mowned_list); + if (g_client->g_motdev_list) + OCDeleteDiscoveredDevices(g_client->g_motdev_list); + + if (g_client->rnd_pin) { + g_free(g_client->rnd_pin); + g_client->rnd_pin = NULL; + } + + g_free(g_client); + g_client = NULL; + + return MA_ERROR_NONE; +} + +int ma_request_pairwise(ma_service *service, gchar *target_1, + gchar *subject_1, gchar *rsrc_uri_1, gchar *rsrc_type_1, + gchar *rsrc_interface_1, int permission_1, + gchar *target_2, gchar *subject_2,gchar *rsrc_uri_2, + gchar *rsrc_type_2, gchar *rsrc_interface_2, + int permission_2) +{ + int ret =MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Link Pairwise"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + if (!target_1 || !subject_1 || !rsrc_uri_1 || !rsrc_type_1 || + !rsrc_interface_1 || !target_2 || !subject_2 || !rsrc_uri_2 || + !rsrc_type_2 || !rsrc_interface_2) { + MA_LOGE("target_1 = %s", target_1); + MA_LOGE("subject_1 = %s", subject_1); + MA_LOGE("rsrc_uri_1 = %s", rsrc_uri_1); + MA_LOGE("rsrc_type_1 = %s", rsrc_type_1); + MA_LOGE("rsrc_interface_1 = %s", rsrc_interface_1); + MA_LOGE("target_2 = %s", target_2); + MA_LOGE("subject_2 = %s", subject_2); + MA_LOGE("rsrc_uri_2 = %s", rsrc_uri_2); + MA_LOGE("rsrc_type_2 = %s", rsrc_type_2); + MA_LOGE("rsrc_interface_2 = %s", rsrc_interface_2); + return MA_ERROR_INVALID_PARAMETER; + } + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _provisioning_pairwise_link(service, target_1, subject_1, rsrc_uri_1, + rsrc_type_1, rsrc_interface_1, permission_1, target_2, + subject_2, rsrc_uri_2, rsrc_type_2, rsrc_interface_2, + permission_2); + + return ret; + +} + +int ma_request_unpair(ma_service *service, gchar *uuid_dev) +{ + int ret = MA_ERROR_NONE; + + ma_check_null_ret_error("service", service, FALSE); + + MA_LOGD("[IPC] Unpair device"); + + /* If we are working now? */ + if (g_atomic_int_get(&service->pending)) + return MA_ERROR_IN_PROGRESS; + + if (!uuid_dev) { + MA_LOGE("uuid_dev = %s", uuid_dev); + + return MA_ERROR_INVALID_PARAMETER; + } + + /* Set d2ds status 'pending' */ + g_atomic_int_set(&service->pending, 1); + + ret = _provisioning_unpair(service, uuid_dev); + + return ret; + +} + diff --git a/src/mot-agent/ma-subowner.h b/src/mot-agent/ma-subowner.h new file mode 100644 index 0000000..9fb77f7 --- /dev/null +++ b/src/mot-agent/ma-subowner.h @@ -0,0 +1,69 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __MA_SUBONWER_H__ +#define __MA_SUBONWER_H__ + +#include "ma.h" + +#ifdef __cplusplus +extern "C" { +#endif + +/* command id */ +typedef enum { + MA_DISC_MOT_ENB_DEVS = 0, /**< Discover MOT enabled devices */ + MA_DISC_MOWNED_DEVS, /**< Discover my owned MOT enabled devices */ + MA_MOT, /**< Do Multiple Onwership Transfer */ + MA_PROV_ACL, /**< ACL Provisioning */ + MA_PROV_CRED, /**< CRED Provisioning */ + MA_REMOVE_MOT, /**< Remove Multiple Ownership */ + MA_UNPAIR, /**< Unpair */ + MA_PROV_PAIRWISE, /** < Pairwise Provisioning */ +} ma_cmd_id_e; + +/* ACL Permission type */ +typedef enum { + MA_PERMISSION_CREATE = (1 << 0), /**< C */ + MA_PERMISSION_READ = (1 << 1), /**< R */ + MA_PERMISSION_WRITE = (1 << 2), /**< U */ + MA_PERMISSION_DELETE = (1 << 3), /**< D */ + MA_PERMISSION_NOTIFY = (1 << 4), /**< N */ + MA_PERMISSION_FULL = 0x11111 /**< CRUDN */ +} ma_permission_e; + +int ma_request_enable(ma_service *service); +int ma_request_disc_mot_enb_devs(ma_service *service); +int ma_request_disc_owned_devs(ma_service *service); +int ma_request_mot(ma_service *service, gchar* uuid, gchar *pin); +int ma_request_remove_mo(ma_service *service, gchar* uuid_str); +int ma_request_prov_acl(ma_service *service, gchar *target, gchar *subject, + gchar *rsrc_uri, gchar *rsrc_type, gchar *rsrc_interface, int permission); +int ma_request_prov_cred(ma_service *service, gchar *uuid_dev1, gchar *uuid_dev2); +int ma_request_unpair(ma_service *service, gchar *uuid_dev); +int ma_request_get_ownerid(ma_service *service, gchar **uuid_str); +int ma_request_disable(ma_service *service); +int ma_request_pairwise(ma_service *service, gchar *target_1, gchar *subject_1, + gchar *rsrc_uri_1, gchar *rsrc_type_1, gchar *rsrc_interface_1, int permission_1, + gchar *target_2, gchar *subject_2,gchar *rsrc_uri_2, gchar *rsrc_type_2, gchar *rsrc_interface_2, int permission_2); + +#ifdef __cplusplus +} +#endif + +#endif /* __MA_SUBONWER_H__ */ diff --git a/src/mot-agent/ma-util.h b/src/mot-agent/ma-util.h new file mode 100644 index 0000000..a82f3f7 --- /dev/null +++ b/src/mot-agent/ma-util.h @@ -0,0 +1,44 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __MA_UTIL_H__ +#define __MA_UTIL_H__ + +#include +#include +#include + +#include "ma-log.h" + +#define NOTUSED(var) (var = var) + +#define ma_check_null_ret_error(name, value, error) do { \ + if (G_UNLIKELY(NULL == (value))) { \ + MA_LOGE("%s is NULL", name); \ + return error; \ + } \ +} while (FALSE) + +#define ma_check_null_ret(name, value) do { \ + if (G_UNLIKELY(NULL == (value))) { \ + MA_LOGE("%s is NULL", name); \ + return; \ + } \ +} while (FALSE) + +#endif /* __MA_UTIL_H__ */ diff --git a/src/mot-agent/ma.c b/src/mot-agent/ma.c new file mode 100644 index 0000000..8cb8c87 --- /dev/null +++ b/src/mot-agent/ma.c @@ -0,0 +1,81 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#include +#include +#include + +#include + +#include "ma.h" +#include "ma-log.h" +#include "ma-util.h" +#include "ma-service.h" +#include "ma-service-interface.h" + +/**< d2d-subowner service */ +ma_service *d2dssvc; + +int main(int argc, char *argv[]) +{ + ma_service *service = NULL; + int ret_code = 0; + gboolean ret; + + __MA_LOG_FUNC_ENTER__; + +#if !GLIB_CHECK_VERSION(2, 32, 0) + if (!g_thread_supported()) + g_thread_init(NULL); +#endif + +#if !GLIB_CHECK_VERSION(2, 36, 0) + g_type_init(); +#endif + NOTUSED(argc); + NOTUSED(argv); + + MA_LOGI("service mainloop start"); + + /* Create MOT agent */ + d2dssvc = service = ma_service_new(); + + /* Initialize MOT agent service interface layer */ + ret = ma_service_interface_init(service); + if (G_UNLIKELY(FALSE == ret)) { + MA_LOGE("Service interface init. failed!"); + ret_code = EXIT_FAILURE; + goto END; + } + /* Run MOT agent service */ + ret = ma_service_run(service); + if (G_UNLIKELY(FALSE == ret)) { + MA_LOGE("Run service failed!"); + ret_code = EXIT_FAILURE; + } + +END: + /* Free MOT agent service */ + ma_service_interface_deinit(service); + ma_service_free(service); + d2dssvc = NULL; + + __MA_LOG_FUNC_EXIT__; + return ret_code; +} + diff --git a/src/mot-agent/ma.h b/src/mot-agent/ma.h new file mode 100644 index 0000000..a0199b5 --- /dev/null +++ b/src/mot-agent/ma.h @@ -0,0 +1,53 @@ +/* + * Network Configuration Module + * + * Copyright (c) 2017 Samsung Electronics Co., Ltd. All rights reserved. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ +#ifndef __MA_AGENT_H__ +#define __MA_AGENT_H__ + +#include +#include +#include + +/**< Internal error code with d2ds daemon. It should be matched with API side */ +typedef enum { + MA_ERROR_NONE = 0, /**< Successful */ + MA_ERROR_IO_ERROR, /**< I/O error */ + MA_ERROR_NO_DATA, /**< Data not exists */ + MA_ERROR_OUT_OF_MEMORY, /**< out of memory */ + MA_ERROR_OPERATION_FAILED, /**< operation failed */ + MA_ERROR_INVALID_PARAMETER, /**< Invalid parameter */ + MA_ERROR_ALREADY_REGISTERED, /**< Request already registered */ + MA_ERROR_IN_PROGRESS /**< operation is in progress */ +} ma_error_e; + +/**< d2d-subowner service structure */ +typedef struct _ma_service { + GMainLoop *main_loop; /**< Service main-loop */ + guint dbus_id; /**< D-Bus id */ + guint activation_dbus_id; /**< D-Bus id for activation */ + + gint pending; /**< Is there any pending operation? */ + + gpointer connection; /**< Connection handle for D-Bus call to other process */ + GCancellable *ca; /**< Cancellable object for D-Bus call (Daemon->other) */ + GList *dbus_sub_ids; /**< The list of subscribed signals */ + + gboolean ma_activated; /**< Whether d2d-manager enabled or not */ +} ma_service; + +#endif /* __MA_AGENT_H__ */ diff --git a/src/mot-agent/oic_svr_db_ma.dat b/src/mot-agent/oic_svr_db_ma.dat new file mode 100644 index 0000000..7b089cb Binary files /dev/null and b/src/mot-agent/oic_svr_db_ma.dat differ diff --git a/src/mot-agent/oic_svr_db_ma.json b/src/mot-agent/oic_svr_db_ma.json new file mode 100644 index 0000000..7942689 --- /dev/null +++ b/src/mot-agent/oic_svr_db_ma.json @@ -0,0 +1,73 @@ +{ + "acl": { + "aclist": { + "aces": [ + { + "subjectuuid": "*", + "resources": [ + { + "href": "/oic/res", + "rel": "", + "rt": ["oic.wk.res"], + "if": ["oic.if.ll"] + },{ + "href": "/oic/d", + "rel": "", + "rt": ["oic.wk.d"], + "if": ["oic.if.baseline", "oic.if.r"] + },{ + "href": "/oic/p", + "rel": "", + "rt": ["oic.wk.p"], + "if": ["oic.if.baseline", "oic.if.r"] + } + ], + "permission": 2 + }, + { + "subjectuuid": "*", + "resources": [ + { + "href": "/oic/sec/doxm", + "rel": "", + "rt": ["oic.r.doxm"], + "if": ["oic.if.baseline"] + }, + { + "href": "/oic/sec/pstat", + "rel": "", + "rt": ["oic.r.pstat"], + "if": ["oic.if.baseline"] + }, + { + "href": "/oic/sec/cred", + "rel": "", + "rt": ["oic.r.cred"], + "if": ["oic.if.baseline"] + } + ], + "permission": 6 + } + ] + }, + "rowneruuid": "00000000-0000-0000-0000-000000000000" + }, + "pstat": { + "isop": false, + "deviceuuid": "00000000-0000-0000-0000-000000000000", + "rowneruuid": "00000000-0000-0000-0000-000000000000", + "cm": 2, + "tm": 0, + "om": 4, + "sm": 4 + }, + "doxm": { + "oxms": [0], + "oxmsel": 0, + "sct": 0, + "owned": false, + "deviceuuid": "00000000-0000-0000-0000-000000000000", + "devowneruuid": "", + "rowneruuid": "00000000-0000-0000-0000-000000000000" + } +}