Updated this component to use Bluez 5 API calls. This fixes TIVI-2936
[profile/ivi/wrt-plugins-ivi-bt.git] / src / BluetoothAdapter.h
1 //
2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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 #ifndef __TIZEN_BLUETOOTH_ADAPTER_H__
19 #define __TIZEN_BLUETOOTH_ADAPTER_H__
20
21 #include <string>
22 #include <vector>
23 #include <map>
24
25 #include <bluetooth.h>
26
27 #include <MultiCallbackUserData.h>
28 #include <PlatformException.h>
29
30 #include "BluetoothDevice.h"
31 #include "BluetoothServiceHandler.h"
32 #include "BluetoothSocket.h"
33
34 // BLUEZ
35 #include <glib.h>
36 #include <dbus/dbus.h>
37 #include <gio/gio.h>
38 // BLUEZ
39
40 namespace DeviceAPI {
41 namespace Bluetooth {
42
43 class BluetoothConnReq
44 {
45 public:
46     BluetoothConnReq(std::string uuid, Common::MultiCallbackUserDataPtr userData)
47     {
48         mUUID = uuid;
49         mUserData = userData;
50     };
51
52     std::string mUUID;
53     Common::MultiCallbackUserDataPtr mUserData;
54 };
55 typedef BluetoothConnReq* BluetoothConnReqPtr;
56
57 class BluetoothAdapter
58 {
59 public:
60     enum adapterAsyncE
61     {
62         SET_POWERED = 0,
63         SET_NAME,
64         SET_VISIBLE,
65         DISCOVER_DEVICES,
66         STOP_DISCOVERY,
67         CREATE_BONDING,
68         DESTROY_BONDING,
69     };
70
71     bool getPowered() const;
72     void setPowered(bool powered, Common::MultiCallbackUserDataPtr userData);
73     std::string getName() const;
74     void setName(std::string &name, Common::MultiCallbackUserDataPtr userData);
75     std::string getAddress() const;
76     bool getVisible() const;
77     void setVisible(bool visible, unsigned int timeout, Common::MultiCallbackUserDataPtr userData);
78     void setChangeListener(Common::MultiCallbackUserDataPtr userData);
79     void unsetChangeListener();
80     void discoverDevices(Common::MultiCallbackUserDataPtr userData);
81     void stopDiscovery(Common::MultiCallbackUserDataPtr userData);
82     void getKnownDevices(Common::MultiCallbackUserDataPtr userData);
83     void getDevice(std::string &address, Common::MultiCallbackUserDataPtr userData);
84     void createBonding(std::string &address, Common::MultiCallbackUserDataPtr userData);
85     void destroyBonding(std::string &address, Common::MultiCallbackUserDataPtr userData);
86     void registerRFCOMMServiceByUUID(std::string &uuid, std::string &name, Common::MultiCallbackUserDataPtr userData);
87     void connectToServiceByUUID(std::string &remoteAddress, std::string &uuid, Common::MultiCallbackUserDataPtr userData);
88
89     void returnKnownDevices(Common::MultiCallbackUserDataPtr userData);
90     void returnDevice(std::string &address, Common::MultiCallbackUserDataPtr userData);
91     void returnRegisteredService(std::string &uuid, std::string &name, Common::MultiCallbackUserDataPtr userData);
92
93     void unregisterUUID(std::string &uuid);
94     bool closeConnectedSocket(int socket);
95     void removeConnReq(std::string &remoteAddress);
96
97     static BluetoothAdapter* getInstance();
98     static bool isBluetoothSupported();
99     static bool isValidAddress(std::string &address);
100     static bool isValidUUID(std::string &uuid);
101     void unloadFrame(JSContextRef context);
102
103 private:
104     BluetoothAdapter();
105     virtual ~BluetoothAdapter();
106
107     // BLUEZ
108     gchar* getDefaultAdapter() const;
109     gchar* getDeviceFromAddress(std::string &address) const;
110     bool isAdapterPowered();
111     bool isDevicePaired(const char *device);
112     bool setAdapterPowered(bool value);
113     bool getDeviceInfo(bt_device_info_s *deviceInfo, const char *device);
114     bool getDeviceInfoByAddress(bt_device_info_s *deviceInfo, const char *address);
115     static void handleSignal(GDBusConnection *connection,
116                              const gchar     *sender,
117                              const gchar     *object_path,
118                              const gchar     *interface_name,
119                              const gchar     *signal_name,
120                              GVariant        *parameters,
121                              gpointer         user_data);
122     void onPoweredChanged(bool powered);
123     void onNameChanged(const char *name);
124     void onVisibilityChanged(bool visible);
125     bool setupAgent(const char *agent);
126     static void agentHandleMethodCall( GDBusConnection       *connection,
127                                        const gchar           *sender,
128                                        const gchar           *object_path,
129                                        const gchar           *interface_name,
130                                        const gchar           *method_name,
131                                        GVariant              *parameters,
132                                        GDBusMethodInvocation *invocation,
133                                        gpointer               user_data);
134     static void asyncCreatePairedDeviceCallback(GObject *source, GAsyncResult *result, gpointer user_data);
135     bool removeDevice(const char *address);
136
137     char *getBluetoothTechnology(); // ConnMan
138     bool setBluetoothPowered(bool value); // ConnMan
139     bool isBtTechnologyPowered(); // ConnMan
140     // BLUEZ
141
142     void freeDeviceInfo(bt_device_info_s *deviceInfo);
143     void freeDiscoveryInfo(bt_adapter_device_discovery_info_s *discoveryInfo);
144
145     // checks whther a device given by its MAC address is in the list, or not
146     bool isDeviceInList(std::vector<BluetoothDeviceSharedPtr> list, const char *address);
147
148     static bool foreachBondedDevicesCB(bt_device_info_s *deviceInfo, void *userData, char *devicePath);
149
150     static void onSocketConnected(int result, bt_socket_connection_state_e state, bt_socket_connection_s *connection, void *userData);
151     static void onSocketReceivedCB(bt_socket_received_data_s *data, void *userData);
152
153     typedef std::multimap<std::string, BluetoothConnReqPtr> ConnReqMultiMapT;   // <remoteAddress, BluetoothConnReqPtr>
154     typedef std::map<std::string, BluetoothServiceHandlerPtr> RegisteredUUIDMapT;   // <UUID, BluetoothServiceHandlerPtr>
155     typedef std::map<int, BluetoothSocketPtr> ConnectedSocketMapT;   // <socketFD, BluetoothSocketPtr>
156
157     // BLUEZ
158     gchar* mAdapterPath;
159     int mAgentRegistrationId;
160     GDBusInterfaceVTable mAgentIfaceVTable;
161     GDBusNodeInfo   *mAgentIntrospectionData;
162     char *mBluetoothTechnology;
163     //
164     Common::MultiCallbackUserDataPtr mChangeListener;
165     bool mEnabled;
166     ConnReqMultiMapT mConnReqMap;
167     RegisteredUUIDMapT mRegisteredUUID;
168     ConnectedSocketMapT mConnectedSocket;
169     std::string mRequestedName;
170     bt_adapter_visibility_mode_e mRequestedVisibility;
171     std::string mCreateBondingAddress;
172     std::string mDestroyBondingAddress;
173     Common::MultiCallbackUserDataPtr mUserDataList[DESTROY_BONDING + 1];
174     std::vector<std::string> mDisappearedDevices;
175     std::vector<BluetoothDeviceSharedPtr> mFoundDevices;
176     std::vector<BluetoothDeviceSharedPtr> knownDevices;
177 };
178
179 typedef BluetoothAdapter* BluetoothAdapterPtr;
180
181 } // Bluetooth
182 } // DeviceAPI
183
184 #endif // __TIZEN_BLUETOOTH_ADAPTER_H__