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 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,
116 const gchar *object_path,
117 const gchar *interface_name,
118 const gchar *signal_name,
119 GVariant *parameters,
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,
127 const gchar *object_path,
128 const gchar *interface_name,
129 const gchar *method_name,
130 GVariant *parameters,
131 GDBusMethodInvocation *invocation,
133 static void asyncCreatePairedDeviceCallback(GObject *source, GAsyncResult *result, gpointer user_data);
134 bool removeDevice(const char *address);
136 char *getBluetoothTechnology(); // ConnMan
137 bool setBluetoothPowered(bool value); // ConnMan
138 bool isBtTechnologyPowered(); // ConnMan
141 void freeDeviceInfo(bt_device_info_s *deviceInfo);
142 void freeDiscoveryInfo(bt_adapter_device_discovery_info_s *discoveryInfo);
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);
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);
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>
164 int mAgentRegistrationId;
165 GDBusInterfaceVTable mAgentIfaceVTable;
166 GDBusNodeInfo *mAgentIntrospectionData;
167 char *mBluetoothTechnology;
169 Common::MultiCallbackUserDataPtr mChangeListener;
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;
185 typedef BluetoothAdapter* BluetoothAdapterPtr;
190 #endif // __TIZEN_BLUETOOTH_ADAPTER_H__