2 // Tizen Web Device API
3 // Copyright (c) 2012 Samsung Electronics Co., Ltd.
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
9 // http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef __TIZEN_BLUETOOTH_ADAPTER_H__
19 #define __TIZEN_BLUETOOTH_ADAPTER_H__
25 #include <bluetooth.h>
27 #include <MultiCallbackUserData.h>
28 #include <PlatformException.h>
30 #include "BluetoothDevice.h"
31 #include "BluetoothServiceHandler.h"
32 #include "BluetoothSocket.h"
36 #include <dbus/dbus.h>
43 class BluetoothConnReq
46 BluetoothConnReq(std::string uuid, Common::MultiCallbackUserDataPtr userData)
53 Common::MultiCallbackUserDataPtr mUserData;
55 typedef BluetoothConnReq* BluetoothConnReqPtr;
57 class BluetoothAdapter
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);
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);
93 void unregisterUUID(std::string &uuid);
94 bool closeConnectedSocket(int socket);
95 void removeConnReq(std::string &remoteAddress);
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);
105 virtual ~BluetoothAdapter();
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,
117 const gchar *object_path,
118 const gchar *interface_name,
119 const gchar *signal_name,
120 GVariant *parameters,
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,
128 const gchar *object_path,
129 const gchar *interface_name,
130 const gchar *method_name,
131 GVariant *parameters,
132 GDBusMethodInvocation *invocation,
134 static void asyncCreatePairedDeviceCallback(GObject *source, GAsyncResult *result, gpointer user_data);
135 bool removeDevice(const char *address);
137 char *getBluetoothTechnology(); // ConnMan
138 bool setBluetoothPowered(bool value); // ConnMan
139 bool isBtTechnologyPowered(); // ConnMan
142 void freeDeviceInfo(bt_device_info_s *deviceInfo);
143 void freeDiscoveryInfo(bt_adapter_device_discovery_info_s *discoveryInfo);
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);
148 static bool foreachBondedDevicesCB(bt_device_info_s *deviceInfo, void *userData, char *devicePath);
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);
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>
159 int mAgentRegistrationId;
160 GDBusInterfaceVTable mAgentIfaceVTable;
161 GDBusNodeInfo *mAgentIntrospectionData;
162 char *mBluetoothTechnology;
164 Common::MultiCallbackUserDataPtr mChangeListener;
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;
179 typedef BluetoothAdapter* BluetoothAdapterPtr;
184 #endif // __TIZEN_BLUETOOTH_ADAPTER_H__