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