Merge the code from tizen_2.4
[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         return BLUETOOTH_ERROR_NONE;
116 }
117
118 int _bt_oob_add_remote_data(
119                         bluetooth_device_address_t *remote_device_address,
120                         bt_oob_data_t *remote_oob_data)
121 {
122         GDBusProxy *proxy;
123         GVariant *reply;
124         GError *err = NULL;
125         char *dev_addr;
126         char *adapter_path;
127         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
128         unsigned char *remote_hash;
129         unsigned char *remote_randomizer;
130         GDBusConnection *conn;
131         GArray *in_param1 = NULL;
132         GArray *in_param2 = NULL;
133         GVariant *hash;
134         GVariant *randomizer;
135
136         BT_CHECK_PARAMETER(remote_device_address, return);
137         BT_CHECK_PARAMETER(remote_oob_data, return);
138
139         conn = _bt_get_system_conn();
140         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
141
142         adapter_path = _bt_get_adapter_path();
143         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
144
145         _bt_convert_addr_type_to_string(address,
146                 remote_device_address->addr);
147
148         proxy =  g_dbus_proxy_new_sync(conn,
149                         G_DBUS_PROXY_FLAGS_NONE, NULL,
150                         BT_BLUEZ_NAME, adapter_path,
151                         BT_OOB_INTERFACE, NULL, &err);
152         g_free(adapter_path);
153         if (!proxy) {
154                 BT_ERR("Unable to create proxy");
155                 if (err) {
156                         BT_ERR("Error: %s", err->message);
157                         g_clear_error(&err);
158                 }
159                 return BLUETOOTH_ERROR_INTERNAL;
160         }
161
162         remote_hash = remote_oob_data->hash;
163         remote_randomizer = remote_oob_data->randomizer;
164         dev_addr = g_strdup(address);
165
166         BT_DBG("remote hash len = [%d] and remote random len = [%d]\n",
167                 remote_oob_data->hash_len, remote_oob_data->randomizer_len);
168         /*Create array of bytes variant*/
169         in_param1 = g_array_new(TRUE, TRUE, sizeof(gchar));
170         in_param2 = g_array_new(TRUE, TRUE, sizeof(gchar));
171
172         g_array_append_vals(in_param1, remote_hash,
173                         remote_oob_data->hash_len);
174         g_array_append_vals(in_param2, remote_randomizer,
175                         remote_oob_data->randomizer_len);
176
177         hash = g_variant_new_from_data((const GVariantType *)"ay",
178                         in_param1->data, in_param1->len,
179                         TRUE, NULL, NULL);
180
181         randomizer = g_variant_new_from_data((const GVariantType *)"ay",
182                         in_param2->data, in_param2->len,
183                         TRUE, NULL, NULL);
184
185         g_array_free(in_param1, TRUE);
186         g_array_free(in_param2, TRUE);
187
188         /* Call AddRemoteData Method*/
189         reply = g_dbus_proxy_call_sync(proxy, "AddRemoteData",
190                         g_variant_new("s@ay@ay", dev_addr, hash, randomizer),
191                         G_DBUS_CALL_FLAGS_NONE, -1,
192                         NULL, &err);
193         g_object_unref(proxy);
194         g_free(dev_addr);
195
196         /* Check the reply*/
197         if (reply == NULL) {
198                 BT_ERR("AddRemoteData dBUS-RPC is failed");
199                 if (err != NULL) {
200                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
201                                         err->code, err->message);
202                         g_clear_error(&err);
203                 }
204                 return BLUETOOTH_ERROR_INTERNAL;
205         }
206
207         g_variant_unref(reply);
208         return BLUETOOTH_ERROR_NONE;
209 }
210
211 int _bt_oob_remove_remote_data(
212                         bluetooth_device_address_t *remote_device_address)
213 {
214         GDBusProxy *proxy;
215         GVariant *reply;
216         GError *err = NULL;
217         char *dev_addr;
218         char *adapter_path;
219         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
220         GDBusConnection *conn;
221
222         BT_CHECK_PARAMETER(remote_device_address, return);
223
224         conn = _bt_get_system_conn();
225         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
226
227         adapter_path = _bt_get_adapter_path();
228         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
229
230         _bt_convert_addr_type_to_string(address,
231                 remote_device_address->addr);
232
233         proxy =  g_dbus_proxy_new_sync(conn,
234                         G_DBUS_PROXY_FLAGS_NONE, NULL,
235                         BT_BLUEZ_NAME, adapter_path,
236                         BT_OOB_INTERFACE, NULL, &err);
237         g_free(adapter_path);
238         if (!proxy) {
239                 BT_ERR("Unable to create proxy");
240                 if (err) {
241                         BT_ERR("Error: %s", err->message);
242                         g_clear_error(&err);
243                 }
244                 return BLUETOOTH_ERROR_INTERNAL;
245         }
246
247         dev_addr = g_strdup(address);
248
249         /* Call RemoveRemoteData Method*/
250         reply = g_dbus_proxy_call_sync(proxy, "RemoveRemoteData",
251                         g_variant_new("s", dev_addr),
252                         G_DBUS_CALL_FLAGS_NONE, -1,
253                         NULL, &err);
254         g_object_unref(proxy);
255         g_free(dev_addr);
256
257         /* Check the reply*/
258         if (reply == NULL) {
259                 BT_ERR("RemoveRemoteData dBUS-RPC is failed");
260                 if (err != NULL) {
261                         BT_ERR("D-Bus API failure: errCode[%x], message[%s]",
262                                         err->code, err->message);
263                         g_clear_error(&err);
264                 }
265                 return BLUETOOTH_ERROR_INTERNAL;
266         }
267
268         g_variant_unref(reply);
269         return BLUETOOTH_ERROR_NONE;
270 }
271