Fix the bug in oob get data
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-oob.c
1 /*
2  * Bluetooth-frwk
3  *
4  * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Contact:  Hocheol Seo <hocheol.seo@samsung.com>
7  *               Girishashok Joshi <girish.joshi@samsung.com>
8  *               Chanyeol Park <chanyeol.park@samsung.com>
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  *              http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */
23
24 #include <glib.h>
25 #include <dlog.h>
26 #include <string.h>
27 #include <gio/gio.h>
28
29 #include "bluetooth-api.h"
30 #include "bt-service-common.h"
31 #include "bt-service-oob.h"
32 #include "bt-service-event.h"
33
34 int _bt_oob_read_local_data(bt_oob_data_t *local_oob_data)
35 {
36         GDBusProxy *proxy;
37         GVariant *reply;
38         GError *err = NULL;
39         char *adapter_path;
40         unsigned char *local_hash = NULL;
41         unsigned char *local_randomizer = NULL;
42         GDBusConnection *conn;
43         GVariant *hash = NULL;
44         GVariant *randomizer = NULL;
45
46         BT_CHECK_PARAMETER(local_oob_data, return);
47
48         conn = _bt_get_system_conn();
49         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
50
51         adapter_path = _bt_get_adapter_path();
52         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
53
54
55         proxy =  g_dbus_proxy_new_sync(conn,
56                         G_DBUS_PROXY_FLAGS_NONE, NULL,
57                         BT_BLUEZ_NAME, adapter_path,
58                         BT_OOB_INTERFACE, NULL, &err);
59         g_free(adapter_path);
60         if (!proxy) {
61                 BT_ERR("Unable to create proxy");
62                 if (err) {
63                         BT_ERR("Error: %s", err->message);
64                         g_clear_error(&err);
65                 }
66                 return BLUETOOTH_ERROR_INTERNAL;
67         }
68
69         reply = g_dbus_proxy_call_sync(proxy, "ReadLocalData",
70                         NULL,
71                         G_DBUS_CALL_FLAGS_NONE, -1,
72                         NULL, &err);
73         g_object_unref(proxy);
74
75         if (reply == NULL) {
76                 BT_ERR("ReadLocalData dBUS-RPC is failed");
77                 if (err != NULL) {
78                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
79                                         err->code, err->message);
80                         g_clear_error(&err);
81                 }
82                 return BLUETOOTH_ERROR_INTERNAL;
83         }
84
85         g_variant_get(reply ,"(@ay@ay)", &hash, &randomizer);
86         g_variant_unref(reply);
87
88         if(hash != NULL){
89                 local_oob_data->hash_len = (unsigned int)g_variant_get_size(hash);
90                 local_hash = (unsigned char *)g_variant_get_data(hash);
91         } else {
92                 BT_ERR("hash is NULL");
93                 return BLUETOOTH_ERROR_INTERNAL;
94         }
95
96         g_variant_unref(hash);
97
98         if(randomizer != NULL){
99                 local_oob_data->randomizer_len = (unsigned int)g_variant_get_size(randomizer);
100                 local_randomizer = (unsigned char *)g_variant_get_data(randomizer);
101         } else {
102                 BT_ERR("randomizer is NULL");
103                 return BLUETOOTH_ERROR_INTERNAL;
104         }
105
106         g_variant_unref(randomizer);
107
108         if (local_oob_data->hash_len > 0)
109                 memcpy(local_oob_data->hash, local_hash, local_oob_data->hash_len);
110
111         if (local_oob_data->randomizer_len > 0)
112                 memcpy(local_oob_data->randomizer, local_randomizer,
113                                 local_oob_data->randomizer_len);
114
115
116         return BLUETOOTH_ERROR_NONE;
117 }
118
119 int _bt_oob_add_remote_data(
120                         bluetooth_device_address_t *remote_device_address,
121                         bt_oob_data_t *remote_oob_data)
122 {
123         GDBusProxy *proxy;
124         GVariant *reply;
125         GError *err = NULL;
126         char *dev_addr;
127         char *adapter_path;
128         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
129         unsigned char *remote_hash;
130         unsigned char *remote_randomizer;
131         GDBusConnection *conn;
132         GArray *in_param1 = NULL;
133         GArray *in_param2 = NULL;
134         GVariant *hash;
135         GVariant *randomizer;
136
137         BT_CHECK_PARAMETER(remote_device_address, return);
138         BT_CHECK_PARAMETER(remote_oob_data, return);
139
140         conn = _bt_get_system_conn();
141         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
142
143         adapter_path = _bt_get_adapter_path();
144         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
145
146         _bt_convert_addr_type_to_string(address,
147                 remote_device_address->addr);
148
149         proxy =  g_dbus_proxy_new_sync(conn,
150                         G_DBUS_PROXY_FLAGS_NONE, NULL,
151                         BT_BLUEZ_NAME, adapter_path,
152                         BT_OOB_INTERFACE, NULL, &err);
153         g_free(adapter_path);
154         if (!proxy) {
155                 BT_ERR("Unable to create proxy");
156                 if (err) {
157                         BT_ERR("Error: %s", err->message);
158                         g_clear_error(&err);
159                 }
160                 return BLUETOOTH_ERROR_INTERNAL;
161         }
162
163         remote_hash = remote_oob_data->hash;
164         remote_randomizer = remote_oob_data->randomizer;
165         dev_addr = g_strdup(address);
166
167         BT_DBG("remote hash len = [%d] and remote random len = [%d]\n",
168                 remote_oob_data->hash_len, remote_oob_data->randomizer_len);
169         /*Create array of bytes variant*/
170         in_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
171         in_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
172
173         g_array_append_vals(in_param1, remote_hash,
174                         remote_oob_data->hash_len);
175         g_array_append_vals(in_param2, remote_randomizer,
176                         remote_oob_data->randomizer_len);
177
178         hash = g_variant_new_from_data((const GVariantType *)"ay",
179                         in_param1->data, in_param1->len,
180                         TRUE, NULL, NULL);
181
182         randomizer = g_variant_new_from_data((const GVariantType *)"ay",
183                         in_param2->data, in_param2->len,
184                         TRUE, NULL, NULL);
185
186         g_array_free(in_param1, TRUE);
187         g_array_free(in_param2, TRUE);
188
189         /* Call AddRemoteData Method*/
190         reply = g_dbus_proxy_call_sync(proxy, "AddRemoteData",
191                         g_variant_new("s@ay@ay", dev_addr, hash, randomizer),
192                         G_DBUS_CALL_FLAGS_NONE, -1,
193                         NULL, &err);
194         g_object_unref(proxy);
195         g_free(dev_addr);
196
197         /* Check the reply*/
198         if (reply == NULL) {
199                 BT_ERR("AddRemoteData dBUS-RPC is failed");
200                 if (err != NULL) {
201                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
202                                         err->code, err->message);
203                         g_clear_error(&err);
204                 }
205                 return BLUETOOTH_ERROR_INTERNAL;
206         }
207
208         g_variant_unref(reply);
209         return BLUETOOTH_ERROR_NONE;
210 }
211
212 int _bt_oob_remove_remote_data(
213                         bluetooth_device_address_t *remote_device_address)
214 {
215         GDBusProxy *proxy;
216         GVariant *reply;
217         GError *err = NULL;
218         char *dev_addr;
219         char *adapter_path;
220         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
221         GDBusConnection *conn;
222
223         BT_CHECK_PARAMETER(remote_device_address, return);
224
225         conn = _bt_get_system_conn();
226         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
227
228         adapter_path = _bt_get_adapter_path();
229         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
230
231         _bt_convert_addr_type_to_string(address,
232                 remote_device_address->addr);
233
234         proxy =  g_dbus_proxy_new_sync(conn,
235                         G_DBUS_PROXY_FLAGS_NONE, NULL,
236                         BT_BLUEZ_NAME, adapter_path,
237                         BT_OOB_INTERFACE, NULL, &err);
238         g_free(adapter_path);
239         if (!proxy) {
240                 BT_ERR("Unable to create proxy");
241                 if (err) {
242                         BT_ERR("Error: %s", err->message);
243                         g_clear_error(&err);
244                 }
245                 return BLUETOOTH_ERROR_INTERNAL;
246         }
247
248         dev_addr = g_strdup(address);
249
250         /* Call RemoveRemoteData Method*/
251         reply = g_dbus_proxy_call_sync(proxy, "RemoveRemoteData",
252                         g_variant_new("s", dev_addr),
253                         G_DBUS_CALL_FLAGS_NONE, -1,
254                         NULL, &err);
255         g_object_unref(proxy);
256         g_free(dev_addr);
257
258         /* Check the reply*/
259         if (reply == NULL) {
260                 BT_ERR("RemoveRemoteData dBUS-RPC is failed");
261                 if (err != NULL) {
262                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
263                                         err->code, err->message);
264                         g_clear_error(&err);
265                 }
266                 return BLUETOOTH_ERROR_INTERNAL;
267         }
268
269         g_variant_unref(reply);
270         return BLUETOOTH_ERROR_NONE;
271 }
272