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