86354c0c377e5d38d42e62679b309c38a0076ebc
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-otp.c
1 /*
2  * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *              http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <glib.h>
19 #include <gio/gio.h>
20 #include <dlog.h>
21 #include <string.h>
22 #include <syspopup_caller.h>
23 #include <vconf.h>
24 #include <bundle_internal.h>
25
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
28
29 #include "bt-service-common.h"
30 #include "bt-service-device.h"
31 #include "bt-service-util.h"
32 #include "bt-service-event.h"
33 #include "bt-service-otp.h"
34
35 #define BT_OTP_SERVICE_NAME "org.projectx.otp"
36 #define BT_OTP_OBJECT_PATH "/org/projectx/otp"
37 #define BT_OTP_INTERFACE_NAME "org.projectx.otp_service"
38
39 #define BT_OTP_BASE_DIR_PATH "/home/owner/media/otp/"
40
41 static GDBusProxy *otp_gproxy;
42
43 static GDBusProxy *_bt_core_gdbus_init_otp_proxy(void)
44 {
45         GDBusProxy *proxy;
46         GError *err = NULL;
47         GDBusConnection *conn;
48
49         BT_DBG(" ");
50
51         conn = _bt_gdbus_get_system_gconn();
52         if (!conn)
53                 return NULL;
54
55         proxy =  g_dbus_proxy_new_sync(conn,
56                         G_DBUS_PROXY_FLAGS_NONE, NULL,
57                         BT_OTP_SERVICE_NAME,
58                         BT_OTP_OBJECT_PATH,
59                         BT_OTP_INTERFACE_NAME,
60                         NULL, &err);
61         if (proxy == NULL) {
62                 if (err) {
63                          BT_ERR("Unable to create proxy: %s", err->message);
64                          g_clear_error(&err);
65                 }
66                 return NULL;
67         }
68         BT_DBG("1");
69         otp_gproxy = proxy;
70
71         return proxy;
72 }
73
74 GDBusProxy *_bt_core_gdbus_get_otp_proxy(void)
75 {
76         return (otp_gproxy) ? otp_gproxy : _bt_core_gdbus_init_otp_proxy();
77 }
78
79 void server_init_cb(GObject *object, GAsyncResult *res,
80                                 gpointer user_data)
81 {
82         BT_INFO("Server Init completed");
83         GError *error = NULL;
84         GVariant *result, *out_param, *param;
85         request_info_t *req_info = NULL;
86         int status = BLUETOOTH_ERROR_NONE;
87         bool server_state = false;
88
89         result = g_dbus_proxy_call_finish(otp_gproxy, res, &error);
90
91         if (result == NULL) {
92                 BT_ERR("Dbus-RPC is failed\n");
93                 if (error != NULL) {
94                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
95                                 error->code, error->message);
96                         g_clear_error(&error);
97                         status = BLUETOOTH_ERROR_INTERNAL;
98                 }
99         }
100
101         if (result) {
102                 g_variant_get(result, "(i)", &status);
103         }
104
105         BT_DBG("Status [%d]", status);
106
107         if (status == BLUETOOTH_ERROR_NONE)
108                 server_state = true;
109
110         param = g_variant_new("(ib)", status, server_state);
111
112         req_info = _bt_get_request_info(GPOINTER_TO_INT(user_data));
113
114         /* Send the event to application */
115         _bt_send_event(BT_OTP_EVENT,
116                 BLUETOOTH_EVENT_OTP_SERVER_STATE_CHANGED,
117                 param);
118
119         out_param = g_variant_new_from_data((const GVariantType *)"i",
120                                 result, sizeof(int), TRUE, NULL, NULL);
121
122         g_dbus_method_invocation_return_value(req_info->context,
123                                 g_variant_new("(iv)", status, out_param));
124
125         _bt_delete_request_list(req_info->req_id);
126         g_variant_unref(result);
127 }
128
129 int bt_otp_server_init(int request_id, const char *directory)
130 {
131         BT_INFO("relative_path: [%s]", directory);
132         char *base_dir = g_strconcat(BT_OTP_BASE_DIR_PATH, directory, NULL);
133
134         BT_DBG(" ");
135
136         otp_gproxy = _bt_core_gdbus_get_otp_proxy();
137         if (!otp_gproxy) {
138                 BT_DBG("Couldn't get service proxy");
139                 g_free(base_dir);
140                 return BLUETOOTH_ERROR_INTERNAL;
141         }
142
143         g_dbus_proxy_call(otp_gproxy,
144                         "enable",
145                         g_variant_new("(s)",
146                                 base_dir),
147                         G_DBUS_CALL_FLAGS_NONE, -1,
148                         NULL,
149                         (GAsyncReadyCallback) server_init_cb,
150                         GINT_TO_POINTER(request_id));
151
152         g_free(base_dir);
153
154         return BLUETOOTH_ERROR_NONE;
155 }
156
157 void server_deinit_cb(GObject *object, GAsyncResult *res,
158                                 gpointer user_data)
159 {
160         BT_INFO("Server Deinit completed");
161         GError *error = NULL;
162         GVariant *result, *out_param, *param;
163         request_info_t *req_info = NULL;
164         int status = BLUETOOTH_ERROR_NONE;
165         bool server_state = false;
166
167         result = g_dbus_proxy_call_finish(otp_gproxy, res, &error);
168
169         if (result == NULL) {
170                 BT_ERR("Dbus-RPC is failed\n");
171                 if (error != NULL) {
172                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
173                                 error->code, error->message);
174                         g_clear_error(&error);
175                         status = BLUETOOTH_ERROR_INTERNAL;
176                 }
177         }
178
179         if (result) {
180                 g_variant_get(result, "(i)", &status);
181         }
182
183         BT_DBG("Status [%d]", status);
184
185         param = g_variant_new("(ib)", status, server_state);
186
187         req_info = _bt_get_request_info(GPOINTER_TO_INT(user_data));
188
189         /* Send the event to application */
190         _bt_send_event(BT_OTP_EVENT,
191                         BLUETOOTH_EVENT_OTP_SERVER_STATE_CHANGED,
192                         param);
193
194         if (req_info) {
195                 out_param = g_variant_new_from_data((const GVariantType *)"i",
196                                 result, sizeof(int), TRUE, NULL, NULL);
197
198                 g_dbus_method_invocation_return_value(req_info->context,
199                                 g_variant_new("(iv)", status, out_param));
200
201                 _bt_delete_request_list(req_info->req_id);
202         }
203
204         g_variant_unref(result);
205
206         if (otp_gproxy) {
207                 g_object_unref(otp_gproxy);
208                 otp_gproxy = NULL;
209         }
210 }
211
212 int bt_otp_server_deinit(int request_id)
213 {
214         BT_DBG("+");
215
216         otp_gproxy = _bt_core_gdbus_get_otp_proxy();
217         if (!otp_gproxy) {
218                 BT_DBG("Couldn't get service proxy");
219                 return BLUETOOTH_ERROR_INTERNAL;
220         }
221
222         g_dbus_proxy_call(otp_gproxy,
223                         "disable",
224                         NULL, G_DBUS_CALL_FLAGS_NONE,
225                         -1, NULL,
226                         (GAsyncReadyCallback) server_deinit_cb,
227                         GINT_TO_POINTER(request_id));
228
229         BT_DBG("-");
230         return BLUETOOTH_ERROR_NONE;
231 }