d436b5de503d960665332e325899c18abbd47b66
[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 <dbus/dbus-glib.h>
25 #include <dbus/dbus.h>
26 #include <glib.h>
27 #include <dlog.h>
28 #include <string.h>
29
30 #include "bluetooth-api.h"
31 #include "bt-service-common.h"
32 #include "bt-service-oob.h"
33 #include "bt-service-event.h"
34
35 int _bt_oob_read_local_data(bt_oob_data_t *local_oob_data)
36 {
37         DBusMessage *msg;
38         DBusMessage *reply;
39         DBusError err;
40         char *adapter_path;
41         unsigned char *local_hash = NULL;
42         unsigned char *local_randomizer = NULL;
43         DBusConnection *conn;
44
45         BT_CHECK_PARAMETER(local_oob_data, return);
46
47         conn = _bt_get_system_conn();
48         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
49
50         adapter_path = _bt_get_adapter_path();
51         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
52
53         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, adapter_path,
54                                         BT_OOB_INTERFACE, "ReadLocalData");
55
56         g_free(adapter_path);
57
58         retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
59
60         dbus_error_init(&err);
61         reply = dbus_connection_send_with_reply_and_block(conn,
62                                         msg, -1, &err);
63
64         dbus_message_unref(msg);
65         if (!reply) {
66                 BT_ERR("Error in ReadLocalData \n");
67                 if (dbus_error_is_set(&err)) {
68                         BT_ERR("%s", err.message);
69                         dbus_error_free(&err);
70                 }
71                 return BLUETOOTH_ERROR_INTERNAL;
72         }
73
74         if (!dbus_message_get_args(reply, NULL,
75                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
76                         &local_hash, &local_oob_data->hash_len,
77                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
78                         &local_randomizer, &local_oob_data->randomizer_len,
79                         DBUS_TYPE_INVALID)) {
80                 BT_ERR("Error in reading arguments\n");
81                 dbus_message_unref(reply);
82                 return BLUETOOTH_ERROR_INVALID_DATA;
83         }
84
85         if (NULL != local_hash)
86                 memcpy(local_oob_data->hash, local_hash, local_oob_data->hash_len);
87
88         if (NULL != local_randomizer)
89                 memcpy(local_oob_data->randomizer, local_randomizer,
90                                         local_oob_data->randomizer_len);
91
92         dbus_message_unref(reply);
93
94         return BLUETOOTH_ERROR_NONE;
95 }
96
97 int _bt_oob_add_remote_data(
98                         bluetooth_device_address_t *remote_device_address,
99                         bt_oob_data_t *remote_oob_data)
100 {
101         DBusMessage *msg;
102         DBusMessage *reply;
103         DBusError err;
104         char *dev_addr;
105         char *adapter_path;
106         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
107         unsigned char *remote_hash;
108         unsigned char *remote_randomizer;
109         DBusConnection *conn;
110
111         BT_CHECK_PARAMETER(remote_device_address, return);
112         BT_CHECK_PARAMETER(remote_oob_data, return);
113
114         conn = _bt_get_system_conn();
115         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
116
117         adapter_path = _bt_get_adapter_path();
118         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
119
120         _bt_convert_addr_type_to_string(address,
121                 remote_device_address->addr);
122
123         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, adapter_path,
124                                 BT_OOB_INTERFACE, "AddRemoteData");
125
126         g_free(adapter_path);
127
128         retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
129
130         BT_DBG("remote hash len = [%d] and remote random len = [%d]\n",
131                 remote_oob_data->hash_len, remote_oob_data->randomizer_len);
132
133         remote_hash = remote_oob_data->hash;
134         remote_randomizer = remote_oob_data->randomizer;
135
136         dev_addr = g_strdup(address);
137
138         dbus_message_append_args(msg,
139                 DBUS_TYPE_STRING, &dev_addr,
140                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
141                 &remote_hash, remote_oob_data->hash_len,
142                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
143                 &remote_randomizer, remote_oob_data->randomizer_len,
144                 DBUS_TYPE_INVALID);
145
146         dbus_error_init(&err);
147         reply = dbus_connection_send_with_reply_and_block(conn,
148                                         msg, -1, &err);
149
150         dbus_message_unref(msg);
151         if (!reply) {
152                 BT_ERR("Error in AddRemoteData \n");
153                 if (dbus_error_is_set(&err)) {
154                         BT_ERR("%s", err.message);
155                         dbus_error_free(&err);
156                         g_free(dev_addr);
157                         return BLUETOOTH_ERROR_INTERNAL;
158                 }
159         }
160
161         g_free(dev_addr);
162         dbus_message_unref(reply);
163
164         return BLUETOOTH_ERROR_NONE;
165 }
166
167 int _bt_oob_remove_remote_data(
168                         bluetooth_device_address_t *remote_device_address)
169 {
170         DBusMessage *msg;
171         DBusMessage *reply;
172         DBusError err;
173         char *dev_addr;
174         char *adapter_path;
175         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
176         DBusConnection *conn;
177
178         BT_CHECK_PARAMETER(remote_device_address, return);
179
180         conn = _bt_get_system_conn();
181         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
182
183         adapter_path = _bt_get_adapter_path();
184         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
185
186         _bt_convert_addr_type_to_string(address,
187                 remote_device_address->addr);
188
189         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, adapter_path,
190                                 BT_OOB_INTERFACE, "RemoveRemoteData");
191
192         g_free(adapter_path);
193
194         retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
195
196         dev_addr = g_strdup(address);
197
198         dbus_message_append_args(msg, DBUS_TYPE_STRING,
199                 &dev_addr, DBUS_TYPE_INVALID);
200
201         dbus_error_init(&err);
202         reply = dbus_connection_send_with_reply_and_block(conn,
203                                         msg, -1, &err);
204
205         dbus_message_unref(msg);
206         if (!reply) {
207                 BT_ERR("Error in RemoveRemoteData \n");
208                 if (dbus_error_is_set(&err)) {
209                         BT_ERR("%s", err.message);
210                         dbus_error_free(&err);
211                         g_free(dev_addr);
212                         return BLUETOOTH_ERROR_INTERNAL;
213                 }
214         }
215
216         g_free(dev_addr);
217         dbus_message_unref(reply);
218
219         return BLUETOOTH_ERROR_NONE;
220 }
221