Revert "[Bluetooth][OTP-Client] Handle GATT operations for OTP"
[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         if (req_info) {
123                 g_dbus_method_invocation_return_value(req_info->context,
124                                 g_variant_new("(iv)", status, out_param));
125
126                 _bt_delete_request_list(req_info->req_id);
127         }
128         g_variant_unref(result);
129 }
130
131 int bt_otp_server_init(int request_id, const char *directory)
132 {
133         BT_INFO("relative_path: [%s]", directory);
134         char *base_dir = g_strconcat(BT_OTP_BASE_DIR_PATH, directory, NULL);
135
136         BT_DBG(" ");
137
138         otp_gproxy = _bt_core_gdbus_get_otp_proxy();
139         if (!otp_gproxy) {
140                 BT_DBG("Couldn't get service proxy");
141                 g_free(base_dir);
142                 return BLUETOOTH_ERROR_INTERNAL;
143         }
144
145         g_dbus_proxy_call(otp_gproxy,
146                         "enable",
147                         g_variant_new("(s)",
148                                 base_dir),
149                         G_DBUS_CALL_FLAGS_NONE, -1,
150                         NULL,
151                         (GAsyncReadyCallback) server_init_cb,
152                         GINT_TO_POINTER(request_id));
153
154         g_free(base_dir);
155
156         return BLUETOOTH_ERROR_NONE;
157 }
158
159 void server_deinit_cb(GObject *object, GAsyncResult *res,
160                                 gpointer user_data)
161 {
162         BT_INFO("Server Deinit completed");
163         GError *error = NULL;
164         GVariant *result, *out_param, *param;
165         request_info_t *req_info = NULL;
166         int status = BLUETOOTH_ERROR_NONE;
167         bool server_state = false;
168
169         result = g_dbus_proxy_call_finish(otp_gproxy, res, &error);
170
171         if (result == NULL) {
172                 BT_ERR("Dbus-RPC is failed\n");
173                 if (error != NULL) {
174                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]\n",
175                                 error->code, error->message);
176                         g_clear_error(&error);
177                         status = BLUETOOTH_ERROR_INTERNAL;
178                 }
179         }
180
181         if (result) {
182                 g_variant_get(result, "(i)", &status);
183         }
184
185         BT_DBG("Status [%d]", status);
186
187         param = g_variant_new("(ib)", status, server_state);
188
189         req_info = _bt_get_request_info(GPOINTER_TO_INT(user_data));
190
191         /* Send the event to application */
192         _bt_send_event(BT_OTP_EVENT,
193                         BLUETOOTH_EVENT_OTP_SERVER_STATE_CHANGED,
194                         param);
195
196         if (req_info) {
197                 out_param = g_variant_new_from_data((const GVariantType *)"i",
198                                 result, sizeof(int), TRUE, NULL, NULL);
199
200                 g_dbus_method_invocation_return_value(req_info->context,
201                                 g_variant_new("(iv)", status, out_param));
202
203                 _bt_delete_request_list(req_info->req_id);
204         }
205
206         g_variant_unref(result);
207
208         if (otp_gproxy) {
209                 g_object_unref(otp_gproxy);
210                 otp_gproxy = NULL;
211         }
212 }
213
214 int bt_otp_server_deinit(int request_id)
215 {
216         BT_DBG("+");
217
218         otp_gproxy = _bt_core_gdbus_get_otp_proxy();
219         if (!otp_gproxy) {
220                 BT_DBG("Couldn't get service proxy");
221                 return BLUETOOTH_ERROR_INTERNAL;
222         }
223
224         g_dbus_proxy_call(otp_gproxy,
225                         "disable",
226                         NULL, G_DBUS_CALL_FLAGS_NONE,
227                         -1, NULL,
228                         (GAsyncReadyCallback) server_deinit_cb,
229                         GINT_TO_POINTER(request_id));
230
231         BT_DBG("-");
232         return BLUETOOTH_ERROR_NONE;
233 }