Merge branch 'master' into tizen_2.1
[platform/core/connectivity/bluetooth-frwk.git] / bt-service / bt-service-oob.c
1 /*
2  * bluetooth-frwk
3  *
4  * Copyright (c) 2012-2013 Samsung Electronics Co., Ltd.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *              http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #include <dbus/dbus-glib.h>
21 #include <dbus/dbus.h>
22 #include <glib.h>
23 #include <dlog.h>
24 #include <string.h>
25
26 #include "bluetooth-api.h"
27 #include "bt-service-common.h"
28 #include "bt-service-oob.h"
29 #include "bt-service-event.h"
30
31 int _bt_oob_read_local_data(bt_oob_data_t *local_oob_data)
32 {
33         DBusMessage *msg;
34         DBusMessage *reply;
35         DBusError err;
36         char *adapter_path;
37         unsigned char *local_hash = NULL;
38         unsigned char *local_randomizer = NULL;
39         DBusConnection *conn;
40
41         BT_CHECK_PARAMETER(local_oob_data, return);
42
43         conn = _bt_get_system_conn();
44         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
45
46         adapter_path = _bt_get_adapter_path();
47         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
48
49         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, adapter_path,
50                                         BT_OOB_INTERFACE, "ReadLocalData");
51
52         g_free(adapter_path);
53
54         retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
55
56         dbus_error_init(&err);
57         reply = dbus_connection_send_with_reply_and_block(conn,
58                                         msg, -1, &err);
59
60         dbus_message_unref(msg);
61         if (!reply) {
62                 BT_ERR("Error in ReadLocalData \n");
63                 if (dbus_error_is_set(&err)) {
64                         BT_ERR("%s", err.message);
65                         dbus_error_free(&err);
66                 }
67                 return BLUETOOTH_ERROR_INTERNAL;
68         }
69
70         if (!dbus_message_get_args(reply, NULL,
71                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
72                         &local_hash, &local_oob_data->hash_len,
73                         DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
74                         &local_randomizer, &local_oob_data->randomizer_len,
75                         DBUS_TYPE_INVALID)) {
76                 BT_ERR("Error in reading arguments\n");
77                 dbus_message_unref(reply);
78                 return BLUETOOTH_ERROR_INVALID_DATA;
79         }
80
81         if (NULL != local_hash)
82                 memcpy(local_oob_data->hash, local_hash, local_oob_data->hash_len);
83
84         if (NULL != local_randomizer)
85                 memcpy(local_oob_data->randomizer, local_randomizer,
86                                         local_oob_data->randomizer_len);
87
88         dbus_message_unref(reply);
89
90         return BLUETOOTH_ERROR_NONE;
91 }
92
93 int _bt_oob_add_remote_data(
94                         bluetooth_device_address_t *remote_device_address,
95                         bt_oob_data_t *remote_oob_data)
96 {
97         DBusMessage *msg;
98         DBusMessage *reply;
99         DBusError err;
100         char *dev_addr;
101         char *adapter_path;
102         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
103         unsigned char *remote_hash;
104         unsigned char *remote_randomizer;
105         DBusConnection *conn;
106
107         BT_CHECK_PARAMETER(remote_device_address, return);
108         BT_CHECK_PARAMETER(remote_oob_data, return);
109
110         conn = _bt_get_system_conn();
111         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
112
113         adapter_path = _bt_get_adapter_path();
114         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
115
116         _bt_convert_addr_type_to_string(address,
117                 remote_device_address->addr);
118
119         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, adapter_path,
120                                 BT_OOB_INTERFACE, "AddRemoteData");
121
122         retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
123
124         BT_DBG("remote hash len = [%d] and remote random len = [%d]\n",
125                 remote_oob_data->hash_len, remote_oob_data->randomizer_len);
126
127         remote_hash = remote_oob_data->hash;
128         remote_randomizer = remote_oob_data->randomizer;
129
130         dev_addr = g_strdup(address);
131
132         dbus_message_append_args(msg,
133                 DBUS_TYPE_STRING, &dev_addr,
134                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
135                 &remote_hash, remote_oob_data->hash_len,
136                 DBUS_TYPE_ARRAY, DBUS_TYPE_BYTE,
137                 &remote_randomizer, remote_oob_data->randomizer_len,
138                 DBUS_TYPE_INVALID);
139
140         dbus_error_init(&err);
141         reply = dbus_connection_send_with_reply_and_block(conn,
142                                         msg, -1, &err);
143
144         dbus_message_unref(msg);
145         if (!reply) {
146                 BT_ERR("Error in AddRemoteData \n");
147                 if (dbus_error_is_set(&err)) {
148                         BT_ERR("%s", err.message);
149                         dbus_error_free(&err);
150                         g_free(dev_addr);
151                         return BLUETOOTH_ERROR_INTERNAL;
152                 }
153         }
154
155         g_free(dev_addr);
156         dbus_message_unref(reply);
157
158         return BLUETOOTH_ERROR_NONE;
159 }
160
161 int _bt_oob_remove_remote_data(
162                         bluetooth_device_address_t *remote_device_address)
163 {
164         DBusMessage *msg;
165         DBusMessage *reply;
166         DBusError err;
167         char *dev_addr;
168         char *adapter_path;
169         char address[BT_ADDRESS_STRING_SIZE] = { 0 };
170         DBusConnection *conn;
171
172         BT_CHECK_PARAMETER(remote_device_address, return);
173
174         conn = _bt_get_system_conn();
175         retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
176
177         adapter_path = _bt_get_adapter_path();
178         retv_if(adapter_path == NULL, BLUETOOTH_ERROR_INTERNAL);
179
180         _bt_convert_addr_type_to_string(address,
181                 remote_device_address->addr);
182
183         msg = dbus_message_new_method_call(BT_BLUEZ_NAME, adapter_path,
184                                 BT_OOB_INTERFACE, "RemoveRemoteData");
185
186         retv_if(msg == NULL, BLUETOOTH_ERROR_INTERNAL);
187
188         dev_addr = g_strdup(address);
189
190         dbus_message_append_args(msg, DBUS_TYPE_STRING,
191                 &dev_addr, DBUS_TYPE_INVALID);
192
193         dbus_error_init(&err);
194         reply = dbus_connection_send_with_reply_and_block(conn,
195                                         msg, -1, &err);
196
197         dbus_message_unref(msg);
198         if (!reply) {
199                 BT_ERR("Error in RemoveRemoteData \n");
200                 if (dbus_error_is_set(&err)) {
201                         BT_DBG("%s", err.message);
202                         dbus_error_free(&err);
203                         g_free(dev_addr);
204                         return BLUETOOTH_ERROR_INTERNAL;
205                 }
206         }
207
208         g_free(dev_addr);
209         dbus_message_unref(reply);
210
211         return BLUETOOTH_ERROR_NONE;
212 }
213