[libamb] - added value quality, removed deprecated GetFoo call, made updateFrequency...
[profile/ivi/automotive-message-broker.git] / plugins / common / bluetooth.hpp
1 #ifndef BLUETOOTHWRAPPER
2 #define BLUETOOTHWRAPPER
3
4 #include <string>
5 #include <debugout.h>
6
7 #include "bluetoothmanagerproxy.h"
8 #include "bluetoothadapterproxy.h"
9 #include "bluetoothserialproxy.h"
10
11
12 class BluetoothDevice
13 {
14 public:
15
16         std::string getDeviceForAddress(std::string address, std::string adapterAddy = "")
17         {
18                 GError* error = NULL;
19                 OrgBluezManager* manager = org_bluez_manager_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
20                                                                                                                                                                  G_DBUS_PROXY_FLAGS_NONE,
21                                                                                                                                                                  "org.bluez","/",NULL, &error);
22
23                 if(!manager)
24                 {
25                         DebugOut(DebugOut::Warning)<<"Error getting bluetooth manager proxy: "<<error->message<<endl;
26                         g_error_free(error);
27                         return "";
28                 }
29
30                 error = NULL;
31
32                 gchar* adapterPath;
33
34                 if(adapterAddy != "")
35                 {
36                         if(!org_bluez_manager_call_find_adapter_sync(manager,adapterAddy.c_str(), &adapterPath, NULL, &error))
37                         {
38                                 DebugOut(DebugOut::Warning)<<"Error getting bluetooth adapter ("<<adapterAddy<<"): "<<error->message<<endl;
39                                 g_error_free(error);
40                                 return "";
41                         }
42
43                         error = NULL;
44                 }
45
46                 else
47                 {
48                         if(!org_bluez_manager_call_default_adapter_sync(manager,&adapterPath, NULL, &error))
49                         {
50                                 DebugOut(DebugOut::Warning)<<"Error getting bluetooth default adapter: "<<error->message<<endl;
51                                 g_error_free(error);
52                                 return "";
53                         }
54
55                         error = NULL;
56                 }
57
58                 OrgBluezAdapter* adapter = org_bluez_adapter_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
59                                                                                                                                                                  G_DBUS_PROXY_FLAGS_NONE,
60                                                                                                                                                                  "org.bluez",adapterPath,NULL,&error);
61                 if(!adapter)
62                 {
63                         DebugOut(DebugOut::Warning)<<"Error getting bluetooth adapter proxy: "<<error->message<<endl;
64                         g_error_free(error);
65                         return "";
66                 }
67
68                 error = NULL;
69
70                 gchar* devicePath;
71                 if(!org_bluez_adapter_call_find_device_sync(adapter,address.c_str(),&devicePath,NULL,&error) ||
72                                 std::string(devicePath) == "")
73                 {
74                         DebugOut(DebugOut::Warning)<<"Error finding bluetooth device: "<<address<<error->message<<endl;
75                         g_error_free(error);
76                         return "";
77                 }
78
79                 error = NULL;
80
81                 OrgBluezSerial* serialDevice = org_bluez_serial_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
82                                                                                                                                                                         G_DBUS_PROXY_FLAGS_NONE,
83                                                                                                                                                                         "org.bluez",devicePath,NULL,&error);
84
85                 if(!serialDevice)
86                 {
87                         DebugOut(DebugOut::Warning)<<"Error getting bluetooth serial device proxy: "<<error->message<<endl;
88                         g_error_free(error);
89                         return "";
90                 }
91
92                 gchar* serialDeviceName;
93                 if(!org_bluez_serial_call_connect_sync(serialDevice,"spp",&serialDeviceName,NULL,&error))
94                 {
95                         DebugOut(DebugOut::Warning)<<"Error connecting bluetooth serial device: "<<address<<" - "<<error->message<<endl;
96                         g_error_free(error);
97                         return "";
98                 }
99
100                 return serialDeviceName;
101         }
102
103         void disconnect(std::string address, std::string adapterAddy = "")
104         {
105                 GError* error = NULL;
106                 OrgBluezManager* manager = org_bluez_manager_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
107                                                                                                                                                                  G_DBUS_PROXY_FLAGS_NONE,
108                                                                                                                                                                  "org.bluez","/",NULL, &error);
109
110                 if(!manager)
111                 {
112                         DebugOut(DebugOut::Warning)<<"Error getting bluetooth manager proxy: "<<error->message<<endl;
113                         g_error_free(error);
114                         return ;
115                 }
116
117                 error = NULL;
118
119                 gchar* adapterPath;
120
121                 if(adapterAddy != "")
122                 {
123                         if(!org_bluez_manager_call_find_adapter_sync(manager,adapterAddy.c_str(), &adapterPath, NULL, &error))
124                         {
125                                 DebugOut(DebugOut::Warning)<<"Error getting bluetooth adapter ("<<adapterAddy<<"): "<<error->message<<endl;
126                                 g_error_free(error);
127                                 return ;
128                         }
129
130                         error = NULL;
131                 }
132
133                 else
134                 {
135                         if(!org_bluez_manager_call_default_adapter_sync(manager,&adapterPath, NULL, &error))
136                         {
137                                 DebugOut(DebugOut::Warning)<<"Error getting bluetooth default adapter: "<<error->message<<endl;
138                                 g_error_free(error);
139                                 return ;
140                         }
141
142                         error = NULL;
143                 }
144
145                 OrgBluezAdapter* adapter = org_bluez_adapter_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
146                                                                                                                                                                  G_DBUS_PROXY_FLAGS_NONE,
147                                                                                                                                                                  "org.bluez",adapterPath,NULL,&error);
148                 if(!adapter)
149                 {
150                         DebugOut(DebugOut::Warning)<<"Error getting bluetooth adapter proxy: "<<error->message<<endl;
151                         g_error_free(error);
152                         return ;
153                 }
154
155                 error = NULL;
156
157                 gchar* devicePath;
158                 if(!org_bluez_adapter_call_find_device_sync(adapter,address.c_str(),&devicePath,NULL,&error) ||
159                                 std::string(devicePath) == "")
160                 {
161                         DebugOut(DebugOut::Warning)<<"Error finding bluetooth device: "<<address<<error->message<<endl;
162                         g_error_free(error);
163                         return ;
164                 }
165
166                 error = NULL;
167
168                 OrgBluezSerial* serialDevice = org_bluez_serial_proxy_new_for_bus_sync(G_BUS_TYPE_SYSTEM,
169                                                                                                                                                                         G_DBUS_PROXY_FLAGS_NONE,
170                                                                                                                                                                         "org.bluez",devicePath,NULL,&error);
171
172                 if(!serialDevice)
173                 {
174                         DebugOut(DebugOut::Warning)<<"Error getting bluetooth serial device proxy: "<<error->message<<endl;
175                         g_error_free(error);
176                         return ;
177                 }
178
179                 gchar* serialDeviceName;
180                 if(!org_bluez_serial_call_disconnect_sync(serialDevice,"spp",NULL,&error))
181                 {
182                         DebugOut(DebugOut::Warning)<<"Error disconnecting bluetooth serial device: "<<address<<" - "<<error->message<<endl;
183                         g_error_free(error);
184                         return ;
185                 }
186         }
187
188 };
189
190
191 #endif