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);
92 value_str = _bt_convert_alert_level_to_string(alert_level);
93 property_str = _bt_convert_property_to_string(property);
95 if (value_str == NULL || property_str == NULL) {
98 return BLUETOOTH_ERROR_INTERNAL;
101 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
103 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
106 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
108 ret = g_dbus_proxy_call_sync(proxy, "Set",
109 g_variant_new("(ssv)", BT_PROXIMITY_MONITOR_INTERFACE, property_str, g_variant_new("s", value_str)),
110 G_DBUS_CALL_FLAGS_NONE,
115 g_variant_unref(ret);
116 g_object_unref(proxy);
117 g_free(property_str);
121 BT_ERR("SetProperty error: [%s]", error->message);
123 return BLUETOOTH_ERROR_INTERNAL;
126 return BLUETOOTH_ERROR_NONE;
129 int bt_get_proximity_property(bluetooth_device_address_t *device_address,
130 unsigned int property, int *alert_level)
133 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
134 GDBusConnection *conn;
135 char *device_path = NULL;
136 GError *error = NULL;
137 GVariant *result = NULL;
140 char *value_str = NULL;
141 char *property_str = NULL;
143 BT_CHECK_PARAMETER(device_address, return);
145 conn = _bt_gdbus_get_system_gconn();
146 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
148 _bt_convert_addr_type_to_string(address, device_address->addr);
150 device_path = _bt_get_device_object_path(address);
152 if (device_path == NULL)
153 return BLUETOOTH_ERROR_NOT_CONNECTED;
155 BT_INFO("device_path is created[%s]", device_path);
157 property_str = _bt_convert_property_to_string(property);
159 if (property_str == NULL)
160 return BLUETOOTH_ERROR_INTERNAL;
162 proxy = g_dbus_proxy_new_sync(conn, G_DBUS_PROXY_FLAGS_NONE,
164 device_path, BT_PROPERTIES_INTERFACE, NULL, NULL);
167 retv_if(proxy == NULL, BLUETOOTH_ERROR_INTERNAL);
169 result = g_dbus_proxy_call_sync(proxy, "GetAll",
170 g_variant_new("(s)", BT_PROXIMITY_MONITOR_INTERFACE),
171 G_DBUS_CALL_FLAGS_NONE,
175 if (result == NULL) {
177 BT_ERR("Error occured in Proxy call [%s]\n", error->message);
180 g_object_unref(proxy);
181 return BLUETOOTH_ERROR_INTERNAL;
183 g_variant_get(result , "(@a{sv})", &value);
184 g_variant_unref(result);
186 tmp_value = g_variant_lookup_value(value, property_str, G_VARIANT_TYPE_STRING);
187 if (tmp_value == NULL) {
188 g_object_unref(proxy);
189 g_variant_unref(value);
190 return BLUETOOTH_ERROR_INTERNAL;
193 value_str = (char *)g_variant_get_string(tmp_value, NULL);
195 *alert_level = _bt_convert_string_to_alert_level(value_str);
197 g_variant_unref(tmp_value);
198 g_variant_unref(value);
199 g_object_unref(proxy);
200 g_free(property_str);
202 return BLUETOOTH_ERROR_NONE;
205 int bt_get_proximity_supported_services(bluetooth_device_address_t *device_address,
206 unsigned int *supported_services)
209 char address[BT_ADDRESS_STRING_SIZE] = { 0 };
211 GDBusConnection *conn;
212 char *device_path = NULL;
213 GError *error = NULL;
214 GVariant *result = NULL;
218 BT_CHECK_PARAMETER(device_address, return);
220 conn = _bt_gdbus_get_system_gconn();
221 retv_if(conn == NULL, BLUETOOTH_ERROR_INTERNAL);
223 _bt_convert_addr_type_to_string(address, device_address->addr);
225 device_path = _bt_get_device_object_path(address);
227 if (device_path == NULL)
228 return 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;