2c11d30519cfbd3da90c5393640ff96a26e77c8a
[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     bool isAdapterPowered();
110     bool isDevicePaired(const char *device);
111     bool setAdapterPowered(bool value);
112     bool getDeviceInfo(bt_device_info_s *deviceInfo, const char *device);
113     bool getDeviceInfoByAddress(bt_device_info_s *deviceInfo, const char *address);
114     static void handleSignal(GDBusConnection *connection,
115                              const gchar     *sender,
116                              const gchar     *object_path,
117                              const gchar     *interface_name,
118                              const gchar     *signal_name,
119                              GVariant        *parameters,
120                              gpointer         user_data);
121     void onPoweredChanged(bool powered);
122     void onNameChanged(const char *name);
123     void onVisibilityChanged(bool visible);
124     bool setupAgent(const char *agent);
125     static void agentHandleMethodCall( GDBusConnection       *connection,
126                                        const gchar           *sender,
127                                        const gchar           *object_path,
128                                        const gchar           *interface_name,
129                                        const gchar           *method_name,
130                                        GVariant              *parameters,
131                                        GDBusMethodInvocation *invocation,
132                                        gpointer               user_data);
133     static void asyncCreatePairedDeviceCallback(GObject *source, GAsyncResult *result, gpointer user_data);
134     bool removeDevice(const char *address);
135
136     char *getBluetoothTechnology(); // ConnMan
137     bool setBluetoothPowered(bool value); // ConnMan
138     bool isBtTechnologyPowered(); // ConnMan
139     // BLUEZ
140
141     void freeDeviceInfo(bt_device_info_s *deviceInfo);
142     void freeDiscoveryInfo(bt_adapter_device_discovery_info_s *discoveryInfo);
143
144     // checks whther a device given by its MAC address is in the list, or not
145     bool isDeviceInList(std::vector<BluetoothDeviceSharedPtr> list, const char *address);
146
147     //static void onStateChangedCB(int result, bt_adapter_state_e adapterState, void *userData);
148     //static void onNameChangedCB(char *name, void *userData);
149     //static void onVisibilityChangedCB(int result, bt_adapter_visibility_mode_e visibilityMode, void *userData);
150     //static void  onDiscoveryStateChangedCB(int result, bt_adapter_device_discovery_state_e discoveryState,
151     //        bt_adapter_device_discovery_info_s *discoveryInfo, void *userData);
152     static bool foreachBondedDevicesCB(bt_device_info_s *deviceInfo, void *userData);
153     //static void onBondCreatedCB(int result, bt_device_info_s *deviceInfo, void *userData);
154     //static void onBondDestroyedCB(int result, char *remoteAddress, void *userData);
155     static void onSocketConnected(int result, bt_socket_connection_state_e state, bt_socket_connection_s *connection, void *userData);
156     static void onSocketReceivedCB(bt_socket_received_data_s *data, void *userData);
157
158     typedef std::multimap<std::string, BluetoothConnReqPtr> ConnReqMultiMapT;   // <remoteAddress, BluetoothConnReqPtr>
159     typedef std::map<std::string, BluetoothServiceHandlerPtr> RegisteredUUIDMapT;   // <UUID, BluetoothServiceHandlerPtr>
160     typedef std::map<int, BluetoothSocketPtr> ConnectedSocketMapT;   // <socketFD, BluetoothSocketPtr>
161
162     // BLUEZ
163     gchar* mAdapterPath;
164     int mAgentRegistrationId;
165     GDBusInterfaceVTable mAgentIfaceVTable;
166     GDBusNodeInfo   *mAgentIntrospectionData;
167     char *mBluetoothTechnology;
168     //
169     Common::MultiCallbackUserDataPtr mChangeListener;
170     bool mEnabled;
171     ConnReqMultiMapT mConnReqMap;
172     RegisteredUUIDMapT mRegisteredUUID;
173     ConnectedSocketMapT mConnectedSocket;
174     //bool mRequestedState;
175     std::string mRequestedName;
176     bt_adapter_visibility_mode_e mRequestedVisibility;
177     std::string mCreateBondingAddress;
178     std::string mDestroyBondingAddress;
179     Common::MultiCallbackUserDataPtr mUserDataList[DESTROY_BONDING + 1];
180     std::vector<std::string> mDisappearedDevices;
181     std::vector<BluetoothDeviceSharedPtr> mFoundDevices;
182     std::vector<BluetoothDeviceSharedPtr> knownDevices;
183 };
184
185 typedef BluetoothAdapter* BluetoothAdapterPtr;
186
187 } // Bluetooth
188 } // DeviceAPI
189
190 #endif // __TIZEN_BLUETOOTH_ADAPTER_H__