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);
86 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_CONNECTED);
88 BT_INFO("device_path is created[%s]", device_path);
90 value_str = _bt_convert_alert_level_to_string(alert_level);
91 property_str = _bt_convert_property_to_string(property);
93 if (value_str == NULL || property_str == NULL) {
96 return BLUETOOTH_ERROR_INTERNAL;
99 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
101 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
106 g_free(property_str);
108 return BLUETOOTH_ERROR_INTERNAL;
111 ret = g_dbus_proxy_call_sync(proxy, "Set",
112 g_variant_new("(ssv)", BT_PROXIMITY_MONITOR_INTERFACE, property_str, g_variant_new("s", value_str)),
113 G_DBUS_CALL_FLAGS_NONE,
118 g_variant_unref(ret);
119 g_object_unref(proxy);
120 g_free(property_str);
124 BT_ERR("SetProperty error: [%s]", error->message);
126 return BLUETOOTH_ERROR_INTERNAL;
129 return BLUETOOTH_ERROR_NONE;
132 int bt_get_proximity_property(bluetooth_device_address_t *device_address,
133 unsigned int property, int *alert_level)
136 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
137 GDBusConnection *conn;
138 char *device_path = NULL;
139 GError *error = NULL;
140 GVariant *result = NULL;
143 char *value_str = NULL;
144 char *property_str = NULL;
146 BT_CHECK_PARAMETER(device_address, return);
148 conn = _bt_gdbus_get_system_gconn();
149 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
151 _bt_convert_addr_type_to_string(address, device_address->addr);
153 device_path = _bt_get_device_object_path(address);
154 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_CONNECTED);
156 BT_INFO("device_path is created[%s]", device_path);
158 property_str = _bt_convert_property_to_string(property);
160 if (property_str == NULL)
161 return BLUETOOTH_ERROR_INTERNAL;
163 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
165 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
168 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
170 result = g_dbus_proxy_call_sync(proxy, "GetAll",
171 g_variant_new("(s)", BT_PROXIMITY_MONITOR_INTERFACE),
172 G_DBUS_CALL_FLAGS_NONE,
176 if (result == NULL) {
178 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
181 g_object_unref(proxy);
182 return BLUETOOTH_ERROR_INTERNAL;
184 g_variant_get(result , "(@a{sv})", &value);
185 g_variant_unref(result);
187 tmp_value = g_variant_lookup_value(value, property_str, G_VARIANT_TYPE_STRING);
188 if (tmp_value == NULL) {
189 g_object_unref(proxy);
190 g_variant_unref(value);
191 return BLUETOOTH_ERROR_INTERNAL;
194 value_str = (char *)g_variant_get_string(tmp_value, NULL);
196 *alert_level = _bt_convert_string_to_alert_level(value_str);
198 g_variant_unref(tmp_value);
199 g_variant_unref(value);
200 g_object_unref(proxy);
201 g_free(property_str);
204 return BLUETOOTH_ERROR_NONE;
207 int bt_get_proximity_supported_services(bluetooth_device_address_t *device_address,
208 unsigned int *supported_services)
211 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
213 GDBusConnection *conn;
214 char *device_path = NULL;
215 GError *error = NULL;
216 GVariant *result = NULL;
220 BT_CHECK_PARAMETER(device_address, return);
222 conn = _bt_gdbus_get_system_gconn();
223 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
225 _bt_convert_addr_type_to_string(address, device_address->addr);
227 device_path = _bt_get_device_object_path(address);
228 retv_if(device_path == NULL, BLUETOOTH_ERROR_NOT_CONNECTED);
230 BT_INFO("device_path is created[%s]", device_path);
232 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
234 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
237 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
239 result = g_dbus_proxy_call_sync(proxy, "GetAll",
240 g_variant_new("(s)", BT_PROXIMITY_MONITOR_INTERFACE),
241 G_DBUS_CALL_FLAGS_NONE,
245 if (result == NULL) {
247 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
250 g_object_unref(proxy);
251 return BLUETOOTH_ERROR_INTERNAL;
253 g_variant_get(result , "(@a{sv})", &value);
254 g_variant_unref(result);
256 *supported_services = 0;
257 tmp_value = g_variant_lookup_value(value, "LinkLossAlertLevel", G_VARIANT_TYPE_STRING);
258 if (tmp_value == NULL) {
259 g_object_unref(proxy);
260 g_variant_unref(value);
261 return BLUETOOTH_ERROR_INTERNAL;
263 *supported_services |= BT_PXP_PROPERTY_LLS;
264 g_variant_unref(tmp_value);
267 tmp_value = g_variant_lookup_value(value, "ImmediateAlertLevel", G_VARIANT_TYPE_STRING);
268 if (tmp_value == NULL) {
269 if (*supported_services == 0) {
270 g_object_unref(proxy);
271 g_variant_unref(value);
272 return BLUETOOTH_ERROR_INTERNAL;
275 *supported_services |= BT_PXP_PROPERTY_IAS;
276 g_variant_unref(tmp_value);
279 tmp_value = g_variant_lookup_value(value, "SignalLevel", G_VARIANT_TYPE_STRING);
280 if (tmp_value == NULL) {
281 if (*supported_services == 0) {
282 g_object_unref(proxy);
283 g_variant_unref(value);
284 return BLUETOOTH_ERROR_INTERNAL;
287 *supported_services |= BT_PXP_PROPERTY_TX_POWER;
288 g_variant_unref(tmp_value);
291 g_variant_unref(value);
292 g_object_unref(proxy);
294 return BLUETOOTH_ERROR_NONE;
297 int bt_register_proximity_reporter()
301 GDBusConnection *conn;
302 char *adapter_path = NULL;
303 GError *error = NULL;
304 GVariant *result = NULL;
306 conn = _bt_gdbus_get_system_gconn();
307 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
309 adapter_path = _bt_get_adapter_path();
310 if (adapter_path == NULL) {
311 BT_ERR("Could not get adapter path\n");
312 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
315 BT_INFO("Adapter path %s", adapter_path);
317 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
318 NULL, BT_BLUEZ_NAME, adapter_path,
319 BT_PROXIMITY_REPORTER_INTERFACE, NULL, NULL);
321 g_free(adapter_path);
322 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
324 result = g_dbus_proxy_call_sync(proxy, "RegisterProximity",
325 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
327 if (result == NULL) {
329 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
332 g_object_unref(proxy);
333 return BLUETOOTH_ERROR_INTERNAL;
335 g_object_unref(proxy);
337 return BLUETOOTH_ERROR_NONE;
340 int bt_unregister_proximity_reporter()
344 GDBusConnection *conn;
345 char *adapter_path = NULL;
346 GError *error = NULL;
347 GVariant *result = NULL;
349 conn = _bt_gdbus_get_system_gconn();
350 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
352 adapter_path = _bt_get_adapter_path();
353 if (adapter_path == NULL) {
354 BT_ERR("Could not get adapter path\n");
355 return BLUETOOTH_ERROR_DEVICE_NOT_ENABLED;
358 BT_INFO("Adapter path %s", adapter_path);
360 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
361 NULL, BT_BLUEZ_NAME, adapter_path,
362 BT_PROXIMITY_REPORTER_INTERFACE, NULL, NULL);
364 g_free(adapter_path);
365 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
367 result = g_dbus_proxy_call_sync(proxy, "UnregisterProximity",
368 NULL, G_DBUS_CALL_FLAGS_NONE, -1,
370 if (result == NULL) {
372 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
375 g_object_unref(proxy);
376 return BLUETOOTH_ERROR_INTERNAL;
378 g_object_unref(proxy);
380 return BLUETOOTH_ERROR_NONE;