Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / linux / advertisement.c
1 /* ****************************************************************
2  *
3  * Copyright 2015 Intel Corporation All Rights Reserved.
4  *
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  ******************************************************************/
18
19 #include "advertisement.h"
20 #include "gatt_dbus.h"
21
22 #include "cagattservice.h"
23 #include "logger.h"
24
25 #include <assert.h>
26
27
28 // Logging tag.
29 static char const TAG[] = "BLE_ADVERTISEMENT";
30
31 bool CALEAdvertisementInitialize(CALEAdvertisement * a,
32                                  GDBusConnection * connection,
33                                  GList * managers)
34 {
35     assert(a != NULL);
36     assert(connection != NULL);
37
38     /*
39       D-Bus object path for the LEAdvertisement1 object of the form
40       /org/iotivity/gatt/advertisement0.
41
42       The same object path is registered with all
43       org.bluez.LEAdvertisingManager1 objects since the advertisement
44       data is same for all Bluetooth hardware adapters.
45     */
46     static char const object_path[] =
47         CA_GATT_SERVICE_ROOT_PATH "/" CA_LE_ADVERTISEMENT_PATH;
48
49     assert(g_variant_is_object_path(object_path));
50
51     a->advertisement  = leadvertisement1_skeleton_new();
52
53     /*
54       Setting the BlueZ advertisement type to "peripheral" causes the
55       Bluetooth adapter to go into LE connectable and general
56       discoverable modes upon successful registration of the
57       advertisement with BlueZ.
58      */
59     leadvertisement1_set_type_(a->advertisement, "peripheral");
60
61     static char const * service_uuids[] = {
62         CA_GATT_SERVICE_UUID,  // Advertise OIC Transport Profile
63         NULL
64     };
65
66     leadvertisement1_set_service_uuids(a->advertisement, service_uuids);
67     leadvertisement1_set_manufacturer_data(a->advertisement, NULL);
68     leadvertisement1_set_solicit_uuids(a->advertisement, NULL);
69     leadvertisement1_set_service_data(a->advertisement, NULL);
70     leadvertisement1_set_include_tx_power(a->advertisement, FALSE);
71
72     a->managers = managers;
73
74     // Export the LEAdvertisement1 interface skeleton.
75     GError * error = NULL;
76     if (!g_dbus_interface_skeleton_export(
77             G_DBUS_INTERFACE_SKELETON(a->advertisement),
78             connection,
79             object_path,
80             &error))
81     {
82         OIC_LOG_V(ERROR,
83                   TAG,
84                   "Unable to export LE advertisement interface: %s\n",
85                   error->message);
86
87         return false;
88     }
89
90     return true;
91 }
92
93 void CALEAdvertisementDestroy(CALEAdvertisement * a)
94 {
95     if (a->advertisement != NULL)
96     {
97         char const * const advertisement_path =
98             g_dbus_interface_skeleton_get_object_path(
99                 G_DBUS_INTERFACE_SKELETON(
100                     a->advertisement));
101
102         if (advertisement_path != NULL)
103         {
104             for (GList * l = a->managers; l != NULL; l = l->next)
105             {
106                 GDBusProxy * const manager = G_DBUS_PROXY(l->data);
107
108                 GVariant * const parameters =
109                     g_variant_new("(o)", advertisement_path, NULL);
110
111                 /*
112                   Unregister our LE advertisement from the BlueZ LE
113                   advertising manager.
114                 */
115                 g_dbus_proxy_call(
116                     manager,
117                     "UnregisterAdvertisement",
118                     parameters,
119                     G_DBUS_CALL_FLAGS_NONE,
120                     -1,    // timeout (default == -1),
121                     NULL,  // cancellable
122                     NULL,  // callback
123                     NULL);
124             }
125         }
126
127         g_clear_object(&a->advertisement);
128     }
129
130     g_list_free_full(a->managers, g_object_unref);
131     a->managers = NULL;
132 }