Merge "Merge branch 'master' into easysetup" into easysetup
[platform/upstream/iotivity.git] / resource / csdk / connectivity / src / bt_le_adapter / linux / context.h
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 #ifndef CA_BLE_LINUX_CONTEXT_H
20 #define CA_BLE_LINUX_CONTEXT_H
21
22 #include "caadapterinterface.h"
23 #include "camutex.h"
24 #include "cathreadpool.h"
25 #include "caleinterface.h"
26
27 #include <gio/gio.h>
28 #include <semaphore.h>
29
30
31 /**
32  * @internal
33  *
34  * BLE Linux adapter base context.
35  */
36 typedef struct _CALEContext
37 {
38     /// Connection to the D-Bus system bus.
39     GDBusConnection * connection;
40
41     /**
42      * Proxy to the BlueZ D-Bus object that implements the
43      * "org.freedesktop.DBus.ObjectManager" interface.
44      *
45      * @todo There's probably no need to keep this around after we've
46      *       retrieved the managed objects the first time around since
47      *       we can rely on signals to alert us to any subsequent
48      *       changes.
49      */
50     GDBusObjectManager * object_manager;
51
52     /**
53      * List of @c GDBusObject objects obtained from the BlueZ
54      * @c ObjectManager.
55      *
56      * @note This list will be updated later on as needed if changes
57      *       in the BlueZ ObjectManager are detected.
58      */
59     GList * objects;
60
61     /**
62      * BlueZ adapter list.
63      *
64      * List of @c GDBusProxy objects for all BlueZ adapters (i.e.
65      * @c org.bluez.Adapter1).  More than one adapter can exist if
66      * multiple Bluetooth hardware interfaces are detected by BlueZ.
67      */
68     GList * adapters;
69
70     /**
71      * BlueZ device list.
72      *
73      * List of @c GDBusProxy objects for all BlueZ devices (i.e.
74      * @c org.bluez.Device1), such as those that matched the discovery
75      * criteria.
76      */
77     GList * devices;
78
79     /**
80      * GATT characteristics to Bluetooth MAC address map.
81      *
82      * Hash table that maps OIC Transport Profile GATT characteristic
83      * to a Bluetooth MAC address.  The key is an interface proxy
84      * (@c GDBusProxy) to an @c org.bluez.GattCharacteristic1 object.
85      * The value is a pointer to the peer @c CAEndpoint_t object.
86      *
87      * On the client side, this maps a response characteristic to the
88      * corresponding MAC address.  On the server side, this maps
89      * request characteristic to the corresponding MAC address.
90      *
91      * @note On the server side a map is overkill since only one
92      *       client is ever connected to the server.  No?
93      *
94      * @todo We may want to have a seperate server-side map to reduce
95      *       contention on this map.
96      */
97     GHashTable * address_map;
98
99     /**
100      * D-Bus signal subscription identifiers.
101      *
102      * The Linux BLE transport implementation subscribes to three
103      * D-Bus signals:
104      *
105      * @li @c org.freedesktop.DBus.ObjectManager.InterfacesAdded
106      * @li @c org.freedesktop.DBus.ObjectManager.InterfacesRemoved
107      *
108      * These subscription identifiers are only used when unsubscribing
109      * from the signals when stopping the LE transport.
110      *
111      * @todo Verify if we need the two property related signals at
112      *       this level.
113      */
114     //@{
115     guint interfaces_added_sub_id;
116     guint interfaces_removed_sub_id;
117     //@}
118
119     /// Glib event loop that drives D-Bus signal handling.
120     GMainLoop * event_loop;
121
122     /**
123      * Callback invoked upon change in local Bluetooth adapter state.
124      */
125     CALEDeviceStateChangedCallback on_device_state_changed;
126
127     /// Callback invoked upon server receiving request data.
128     CABLEDataReceivedCallback on_server_received_data;
129
130     /// Callback invoked upon client receiving response data.
131     CABLEDataReceivedCallback on_client_received_data;
132
133     /**
134      * Handle to thread pool to which client side tasks will be
135      * added.
136      */
137     ca_thread_pool_t client_thread_pool;
138
139     /**
140      * Handle to thread pool to which server side tasks will be
141      * added.
142      */
143     ca_thread_pool_t server_thread_pool;
144
145     /// Callback invoked when reporting a client side error.
146     CABLEErrorHandleCallback on_client_error;
147
148     /// Callback invoked when reporting a server side error.
149     CABLEErrorHandleCallback on_server_error;
150
151     /// Mutex used to synchronize access to context fields.
152     ca_mutex lock;
153
154     /**
155      * BlueZ adapter list initialization condition variable.
156      *
157      * This condition variable is used to prevent the BLE adapter
158      * "start" from completing until the thread performing BlueZ
159      * adapter query completes.  Initialization is performed in the
160      * same thread that will run the event loop.  The condition
161      * variable is also used to wait for peripheral devices to be
162      * discovered.
163      *
164      * @see @c GMainLoop documentation for further details.
165      */
166     ca_cond condition;
167
168     /**
169      * Semaphore that indicates completed start of the LE transport.
170      *
171      * In some corner cases the transport stop will complete before
172      * transport start completes.  In such cases, the event loop
173      * run during LE transport start will never exit since the
174      * transport stop will have completed before the event loop that
175      * drives was
176      * run.  This semaphore is used to force the call to
177      * ::CAStartLEAdapter() to wait for the thread that runs the GLib
178      * event loop that drives D-Bus signal handling to completely
179      * start.
180      */
181     sem_t le_started;
182
183 } CALEContext;
184
185
186 #endif  /* CA_BLE_LINUX_CONTEXT_H */