77ffa3cb0bf4bdd8c750fe015fc68a3dff002674
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-oob.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 <dlog.h>
20 #include <string.h>
21 #include <gio/gio.h>
22
23 #include "bluetooth-api.h"
24 #include "bt-service-common.h"
25 #include "bt-service-oob.h"
26 #include "bt-service-event.h"
27
28 int _bt_oob_read_local_data(bt_oob_data_t *local_oob_data)
29 {
30         GDBusProxy *proxy;
31         GVariant *reply;
32         GError *err = NULL;
33         char *adapter_path;
34         unsigned char *local_hash = NULL;
35         unsigned char *local_randomizer = NULL;
36         GDBusConnection *conn;
37         GVariant *hash = NULL;
38         GVariant *randomizer = NULL;
39
40         BT_CHECK_PARAMETER(local_oob_data, return);
41
42         conn = _bt_get_system_conn();
43         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
44
45         adapter_path = _bt_get_adapter_path();
46         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
47
48
49         proxy =  g_dbus_proxy_new_sync(conn,
50                         G_DBUS_PROXY_FLAGS_NONE, NULL,
51                         BT_BLUEZ_NAME, adapter_path,
52                         BT_OOB_INTERFACE, NULL, &err);
53         g_free(adapter_path);
54         if (!proxy) {
55                 BT_ERR("Unable to create proxy");
56                 if (err) {
57                         BT_ERR("Error: %s", err->message);
58                         g_clear_error(&err);
59                 }
60                 return BLUETOOTH_ERROR_INTERNAL;
61         }
62
63         reply = g_dbus_proxy_call_sync(proxy, "ReadLocalData",
64                         NULL,
65                         G_DBUS_CALL_FLAGS_NONE, -1,
66                         NULL, &err);
67         g_object_unref(proxy);
68
69         if (reply == NULL) {
70                 BT_ERR("ReadLocalData dBUS-RPC is failed");
71                 if (err != NULL) {
72                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
73                                         err->code, err->message);
74                         g_clear_error(&err);
75                 }
76                 return BLUETOOTH_ERROR_INTERNAL;
77         }
78
79         g_variant_get(reply ,"(@ay@ay)", &hash, &randomizer);
80         g_variant_unref(reply);
81
82         if(hash != NULL){
83                 local_oob_data->hash_len = (unsigned int)g_variant_get_size(hash);
84                 local_hash = (unsigned char *)g_variant_get_data(hash);
85         } else {
86                 BT_ERR("hash is NULL");
87                 return BLUETOOTH_ERROR_INTERNAL;
88         }
89
90         g_variant_unref(hash);
91
92         if(randomizer != NULL){
93                 local_oob_data->randomizer_len = (unsigned int)g_variant_get_size(randomizer);
94                 local_randomizer = (unsigned char *)g_variant_get_data(randomizer);
95         } else {
96                 BT_ERR("randomizer is NULL");
97                 return BLUETOOTH_ERROR_INTERNAL;
98         }
99
100         g_variant_unref(randomizer);
101
102         if (local_oob_data->hash_len > 0)
103                 memcpy(local_oob_data->hash, local_hash, local_oob_data->hash_len);
104
105         if (local_oob_data->randomizer_len > 0)
106                 memcpy(local_oob_data->randomizer, local_randomizer,
107                                 local_oob_data->randomizer_len);
108
109         return BLUETOOTH_ERROR_NONE;
110 }
111
112 int _bt_oob_add_remote_data(
113                         bluetooth_device_address_t *remote_device_address,
114                         bt_oob_data_t *remote_oob_data)
115 {
116         GDBusProxy *proxy;
117         GVariant *reply;
118         GError *err = NULL;
119         char *dev_addr;
120         char *adapter_path;
121         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
122         unsigned char *remote_hash;
123         unsigned char *remote_randomizer;
124         GDBusConnection *conn;
125         GArray *in_param1 = NULL;
126         GArray *in_param2 = NULL;
127         GVariant *hash;
128         GVariant *randomizer;
129
130         BT_CHECK_PARAMETER(remote_device_address, return);
131         BT_CHECK_PARAMETER(remote_oob_data, return);
132
133         conn = _bt_get_system_conn();
134         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
135
136         adapter_path = _bt_get_adapter_path();
137         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
138
139         _bt_convert_addr_type_to_string(address,
140                 remote_device_address->addr);
141
142         proxy =  g_dbus_proxy_new_sync(conn,
143                         G_DBUS_PROXY_FLAGS_NONE, NULL,
144                         BT_BLUEZ_NAME, adapter_path,
145                         BT_OOB_INTERFACE, NULL, &err);
146         g_free(adapter_path);
147         if (!proxy) {
148                 BT_ERR("Unable to create proxy");
149                 if (err) {
150                         BT_ERR("Error: %s", err->message);
151                         g_clear_error(&err);
152                 }
153                 return BLUETOOTH_ERROR_INTERNAL;
154         }
155
156         remote_hash = remote_oob_data->hash;
157         remote_randomizer = remote_oob_data->randomizer;
158         dev_addr = g_strdup(address);
159
160         BT_DBG("remote hash len = [%d] and remote random len = [%d]\n",
161                 remote_oob_data->hash_len, remote_oob_data->randomizer_len);
162         /*Create array of bytes variant*/
163         in_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
164         in_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
165
166         g_array_append_vals(in_param1, remote_hash,
167                         remote_oob_data->hash_len);
168         g_array_append_vals(in_param2, remote_randomizer,
169                         remote_oob_data->randomizer_len);
170
171         hash = g_variant_new_from_data((const GVariantType *)"ay",
172                         in_param1->data, in_param1->len,
173                         TRUE, NULL, NULL);
174
175         randomizer = g_variant_new_from_data((const GVariantType *)"ay",
176                         in_param2->data, in_param2->len,
177                         TRUE, NULL, NULL);
178
179         g_array_free(in_param1, TRUE);
180         g_array_free(in_param2, TRUE);
181
182         /* Call AddRemoteData Method*/
183         reply = g_dbus_proxy_call_sync(proxy, "AddRemoteData",
184                         g_variant_new("s@ay@ay", dev_addr, hash, randomizer),
185                         G_DBUS_CALL_FLAGS_NONE, -1,
186                         NULL, &err);
187         g_object_unref(proxy);
188         g_free(dev_addr);
189
190         /* Check the reply*/
191         if (reply == NULL) {
192                 BT_ERR("AddRemoteData dBUS-RPC is failed");
193                 if (err != NULL) {
194                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
195                                         err->code, err->message);
196                         g_clear_error(&err);
197                 }
198                 return BLUETOOTH_ERROR_INTERNAL;
199         }
200
201         g_variant_unref(reply);
202         return BLUETOOTH_ERROR_NONE;
203 }
204
205 int _bt_oob_remove_remote_data(
206                         bluetooth_device_address_t *remote_device_address)
207 {
208         GDBusProxy *proxy;
209         GVariant *reply;
210         GError *err = NULL;
211         char *dev_addr;
212         char *adapter_path;
213         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
214         GDBusConnection *conn;
215
216         BT_CHECK_PARAMETER(remote_device_address, return);
217
218         conn = _bt_get_system_conn();
219         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
220
221         adapter_path = _bt_get_adapter_path();
222         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
223
224         _bt_convert_addr_type_to_string(address,
225                 remote_device_address->addr);
226
227         proxy =  g_dbus_proxy_new_sync(conn,
228                         G_DBUS_PROXY_FLAGS_NONE, NULL,
229                         BT_BLUEZ_NAME, adapter_path,
230                         BT_OOB_INTERFACE, NULL, &err);
231         g_free(adapter_path);
232         if (!proxy) {
233                 BT_ERR("Unable to create proxy");
234                 if (err) {
235                         BT_ERR("Error: %s", err->message);
236                         g_clear_error(&err);
237                 }
238                 return BLUETOOTH_ERROR_INTERNAL;
239         }
240
241         dev_addr = g_strdup(address);
242
243         /* Call RemoveRemoteData Method*/
244         reply = g_dbus_proxy_call_sync(proxy, "RemoveRemoteData",
245                         g_variant_new("s", dev_addr),
246                         G_DBUS_CALL_FLAGS_NONE, -1,
247                         NULL, &err);
248         g_object_unref(proxy);
249         g_free(dev_addr);
250
251         /* Check the reply*/
252         if (reply == NULL) {
253                 BT_ERR("RemoveRemoteData dBUS-RPC is failed");
254                 if (err != NULL) {
255                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
256                                         err->code, err->message);
257                         g_clear_error(&err);
258                 }
259                 return BLUETOOTH_ERROR_INTERNAL;
260         }
261
262         g_variant_unref(reply);
263         return BLUETOOTH_ERROR_NONE;
264 }
265