2 * Copyright (c) 2011 Samsung Electronics Co., Ltd All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
22 #include <syspopup_caller.h>
24 #include <bundle_internal.h>
26 #include "bluetooth-api.h"
27 #include "bt-internal-types.h"
29 #include "bt-service-common.h"
30 #include "bt-service-device.h"
31 #include "bt-service-proximity.h"
33 char *_bt_convert_alert_level_to_string(int value)
35 if (value == BT_PXP_ALERT_MILD)
36 return g_strdup("mild");
37 else if (value == BT_PXP_ALERT_HIGH)
38 return g_strdup("high");
40 return g_strdup("none");
43 int _bt_convert_string_to_alert_level(const char *str)
45 if (g_strcmp0("high", str) == 0)
46 return BT_PXP_ALERT_HIGH;
47 else if (g_strcmp0("mild", str) == 0)
48 return BT_PXP_ALERT_MILD;
50 return BT_PXP_ALERT_NONE;
53 char *_bt_convert_property_to_string(int value)
55 if (value == BT_PXP_PROPERTY_LLS)
56 return g_strdup("LinkLossAlertLevel");
57 else if (value == BT_PXP_PROPERTY_IAS)
58 return g_strdup("ImmediateAlertLevel");
59 else if (value == BT_PXP_PROPERTY_TX_POWER)
60 return g_strdup("SignalLevel");
65 int bt_set_proximity_property(bluetooth_device_address_t *device_address,
66 unsigned int property, int alert_level)
69 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
71 GDBusConnection *conn;
72 char *device_path = NULL;
75 char *value_str = NULL;
76 char *property_str = NULL;
78 BT_CHECK_PARAMETER(device_address, return);
80 conn = _bt_gdbus_get_system_gconn();
81 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
83 _bt_convert_addr_type_to_string(address, device_address->addr);
85 device_path = _bt_get_device_object_path(address);
87 if (device_path == NULL) {
88 return BLUETOOTH_ERROR_NOT_CONNECTED;
90 BT_INFO("device_path is created[%s]", device_path);
93 value_str = _bt_convert_alert_level_to_string(alert_level);
94 property_str =_bt_convert_property_to_string(property);
96 if (value_str == NULL || property_str == NULL) {
99 return BLUETOOTH_ERROR_INTERNAL;
102 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
104 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
107 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
109 ret = g_dbus_proxy_call_sync(proxy, "Set",
110 g_variant_new("(ssv)", BT_PROXIMITY_MONITOR_INTERFACE, property_str, g_variant_new("s", value_str)),
111 G_DBUS_CALL_FLAGS_NONE,
116 g_variant_unref(ret);
117 g_object_unref(proxy);
118 g_free(property_str);
122 BT_ERR("SetProperty error: [%s]", error->message);
124 return BLUETOOTH_ERROR_INTERNAL;
127 return BLUETOOTH_ERROR_NONE;
130 int bt_get_proximity_property(bluetooth_device_address_t *device_address,
131 unsigned int property, int *alert_level)
134 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
135 GDBusConnection *conn;
136 char *device_path = NULL;
137 GError *error = NULL;
138 GVariant *result = NULL;
141 char *value_str = NULL;
142 char *property_str = NULL;
144 BT_CHECK_PARAMETER(device_address, return);
146 conn = _bt_gdbus_get_system_gconn();
147 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
149 _bt_convert_addr_type_to_string(address, device_address->addr);
151 device_path = _bt_get_device_object_path(address);
153 if (device_path == NULL) {
154 return BLUETOOTH_ERROR_NOT_CONNECTED;
156 BT_INFO("device_path is created[%s]", device_path);
159 property_str = _bt_convert_property_to_string(property);
161 if (property_str == NULL)
162 return BLUETOOTH_ERROR_INTERNAL;
164 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
166 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
169 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
171 result = g_dbus_proxy_call_sync(proxy, "GetAll",
172 g_variant_new("(s)", BT_PROXIMITY_MONITOR_INTERFACE),
173 G_DBUS_CALL_FLAGS_NONE,
177 if (result == NULL) {
179 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
182 g_object_unref(proxy);
183 return BLUETOOTH_ERROR_INTERNAL;
185 g_variant_get(result , "(@a{sv})", &value);
186 g_variant_unref(result);
188 tmp_value = g_variant_lookup_value(value, property_str, G_VARIANT_TYPE_STRING);
189 if (tmp_value == NULL) {
190 g_object_unref(proxy);
191 g_variant_unref(value);
192 return BLUETOOTH_ERROR_INTERNAL;
195 value_str = (char *)g_variant_get_string(tmp_value, NULL);
197 *alert_level = _bt_convert_string_to_alert_level(value_str);
199 g_variant_unref(tmp_value);
200 g_variant_unref(value);
201 g_object_unref(proxy);
202 g_free(property_str);
205 return BLUETOOTH_ERROR_NONE;
208 int bt_get_proximity_supported_services(bluetooth_device_address_t *device_address,
209 unsigned int *supported_services)
212 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
214 GDBusConnection *conn;
215 char *device_path = NULL;
216 GError *error = NULL;
217 GVariant *result = NULL;
221 BT_CHECK_PARAMETER(device_address, return);
223 conn = _bt_gdbus_get_system_gconn();
224 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
226 _bt_convert_addr_type_to_string(address, device_address->addr);
228 device_path = _bt_get_device_object_path(address);
230 if (device_path == NULL) {
231 return BLUETOOTH_ERROR_NOT_CONNECTED;
233 BT_INFO("device_path is created[%s]", device_path);
236 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
238 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
241 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
243 result = g_dbus_proxy_call_sync(proxy, "GetAll",
244 g_variant_new("(s)", BT_PROXIMITY_MONITOR_INTERFACE),
245 G_DBUS_CALL_FLAGS_NONE,
249 if (result == NULL) {
251 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
254 g_object_unref(proxy);
255 return BLUETOOTH_ERROR_INTERNAL;
257 g_variant_get(result , "(@a{sv})", &value);
258 g_variant_unref(result);
260 *supported_services = 0;
261 tmp_value = g_variant_lookup_value(value, "LinkLossAlertLevel", G_VARIANT_TYPE_STRING);
262 if (tmp_value == NULL) {
263 g_object_unref(proxy);
264 g_variant_unref(value);
265 return BLUETOOTH_ERROR_INTERNAL;
267 *supported_services |= BT_PXP_PROPERTY_LLS;
268 g_variant_unref(tmp_value);
271 tmp_value = g_variant_lookup_value(value, "ImmediateAlertLevel", G_VARIANT_TYPE_STRING);
272 if (tmp_value == NULL) {
273 if (*supported_services == 0) {
274 g_object_unref(proxy);
275 g_variant_unref(value);
276 return BLUETOOTH_ERROR_INTERNAL;
279 *supported_services |= BT_PXP_PROPERTY_IAS;
280 g_variant_unref(tmp_value);
283 tmp_value = g_variant_lookup_value(value, "SignalLevel", G_VARIANT_TYPE_STRING);
284 if (tmp_value == NULL) {
285 if (*supported_services == 0) {
286 g_object_unref(proxy);
287 g_variant_unref(value);
288 return BLUETOOTH_ERROR_INTERNAL;
291 *supported_services |= BT_PXP_PROPERTY_TX_POWER;
292 g_variant_unref(tmp_value);
295 g_variant_unref(value);
296 g_object_unref(proxy);
298 return BLUETOOTH_ERROR_NONE;
301 int bt_register_proximity_reporter()
305 GDBusConnection *conn;
306 char *adapter_path = NULL;
307 GError *error = NULL;
308 GVariant *result = NULL;
310 conn = _bt_gdbus_get_system_gconn();
311 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
313 adapter_path = _bt_get_adapter_path();
314 if (adapter_path == NULL) {
315 BT_ERR("Could not get adapter path\n");
316 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
319 BT_INFO("Adapter path %s", adapter_path);
321 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
322 NULL, BT_BLUEZ_NAME, adapter_path,
323 BT_PROXIMITY_REPORTER_INTERFACE, NULL, NULL);
325 g_free(adapter_path);
326 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
328 result = g_dbus_proxy_call_sync(proxy, "RegisterProximity",
329 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
331 if (result == NULL) {
333 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
336 g_object_unref(proxy);
337 return BLUETOOTH_ERROR_INTERNAL;
339 g_object_unref(proxy);
341 return BLUETOOTH_ERROR_NONE;
344 int bt_unregister_proximity_reporter()
348 GDBusConnection *conn;
349 char *adapter_path = NULL;
350 GError *error = NULL;
351 GVariant *result = NULL;
353 conn = _bt_gdbus_get_system_gconn();
354 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
356 adapter_path = _bt_get_adapter_path();
357 if (adapter_path == NULL) {
358 BT_ERR("Could not get adapter path\n");
359 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
362 BT_INFO("Adapter path %s", adapter_path);
364 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
365 NULL, BT_BLUEZ_NAME, adapter_path,
366 BT_PROXIMITY_REPORTER_INTERFACE, NULL, NULL);
368 g_free(adapter_path);
369 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
371 result = g_dbus_proxy_call_sync(proxy, "UnregisterProximity",
372 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
374 if (result == NULL) {
376 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
379 g_object_unref(proxy);
380 return BLUETOOTH_ERROR_INTERNAL;
382 g_object_unref(proxy);
384 return BLUETOOTH_ERROR_NONE;