Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*
3 : : * This file is part of the LibreOffice project.
4 : : *
5 : : * This Source Code Form is subject to the terms of the Mozilla Public
6 : : * License, v. 2.0. If a copy of the MPL was not distributed with this
7 : : * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 : : */
9 : : #include "BluetoothServer.hxx"
10 : : #include <stdio.h>
11 : :
12 : : #ifdef LINUX
13 : : #include <glib.h>
14 : : #include <gio/gio.h>
15 : : #include <sys/unistd.h>
16 : : #include <sys/socket.h>
17 : : #endif
18 : : #include <tools/debug.hxx>
19 : : #include <tools/stream.hxx>
20 : :
21 : : #ifdef LINUX
22 : : #include "bluetooth/bluetooth.h"
23 : : #include "bluetooth/rfcomm.h"
24 : : #endif
25 : :
26 : :
27 : : #include "Communicator.hxx"
28 : :
29 : : using namespace sd;
30 : :
31 : 0 : BluetoothServer::BluetoothServer( std::vector<Communicator*>* pCommunicators ):
32 : : Thread( "BluetoothServer" ),
33 : 0 : mpCommunicators( pCommunicators )
34 : : {
35 : 0 : }
36 : :
37 : 0 : BluetoothServer::~BluetoothServer()
38 : : {
39 [ # # ]: 0 : }
40 : :
41 : : struct oslSocketImpl {
42 : : int m_Socket;
43 : : int m_nLastError;
44 : : void* m_CloseCallback;
45 : : void* m_CallbackArg;
46 : : oslInterlockedCount m_nRefCount;
47 : : #if defined(LINUX)
48 : : sal_Bool m_bIsAccepting;
49 : : sal_Bool m_bIsInShutdown;
50 : : #endif
51 : : };
52 : :
53 : :
54 : 0 : void BluetoothServer::execute()
55 : : {
56 : : #ifdef LINUX
57 : : #ifdef GLIB_VERSION_2_26
58 [ # # ]: 0 : g_type_init();
59 : 0 : GError* aError = NULL;
60 [ # # ]: 0 : GDBusConnection* aConnection = g_bus_get_sync( G_BUS_TYPE_SYSTEM, NULL, &aError );
61 [ # # ]: 0 : if ( aError )
62 : : {
63 [ # # ]: 0 : g_error_free( aError );
64 : : }
65 : :
66 : : GVariant *aAdapter = g_dbus_connection_call_sync( aConnection,
67 : : "org.bluez", "/", "org.bluez.Manager",
68 : : "DefaultAdapter", NULL,
69 : : G_VARIANT_TYPE_TUPLE,
70 [ # # ]: 0 : G_DBUS_CALL_FLAGS_NONE, -1, NULL, &aError);
71 [ # # ]: 0 : GVariant *aAdapterName = g_variant_get_child_value( aAdapter, 0 );
72 [ # # ]: 0 : if ( aError )
73 : : {
74 [ # # ]: 0 : g_error_free( aError );
75 : : }
76 : : // fprintf( stderr, (const char*) g_variant_get_string( aAdapterName, NULL ) );
77 : :
78 : :
79 : : // GDBusObjectManager* aManager = g_dbus_object_manager_client_new_sync( aConnection,
80 : : // G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, "org.bluez.Manager", "/org/bluez",
81 : : // NULL, NULL, NULL, NULL, &aError );
82 : : // if ( aError )
83 : : // {
84 : : // fprintf( stderr, aError->message );
85 : : // g_error_free( aError );
86 : : // }
87 : :
88 : : GVariant *aRecordHandle = g_dbus_connection_call_sync( aConnection,
89 : : "org.bluez", g_variant_get_string( aAdapterName, NULL ), "org.bluez.Service",
90 : : "AddRecord",
91 : : g_variant_new("(s)",
92 : : "<?xml version='1.0' encoding= 'UTF-8' ?><record><attribute id='0x0001'><sequence><uuid value='0x1101' /></sequence></attribute><attribute id='0x0004'><sequence><sequence><uuid value='0x0100' /></sequence><sequence><uuid value='0x0003' /><uint8 value='0x05' /></sequence></sequence></attribute><attribute id='0x0005'><sequence><uuid value='0x1002' /></sequence></attribute><attribute id='0x0006'><sequence><uint16 value='0x656e' /><uint16 value='0x006a' /><uint16 value='0x0100' /></sequence></attribute><attribute id='0x0009'><sequence><sequence><uuid value='0x1101' /><uint16 value='0x0100' /></sequence></sequence></attribute><attribute id='0x0100'><text value='Serial Port' /></attribute><attribute id='0x0101'><text value='COM Port' /></attribute></record>"),
93 : : G_VARIANT_TYPE_TUPLE,
94 [ # # ][ # # ]: 0 : G_DBUS_CALL_FLAGS_NONE, -1, NULL, &aError);
[ # # ]
95 [ # # ]: 0 : if ( aError )
96 : : {
97 [ # # ]: 0 : g_error_free( aError );
98 : : }
99 : : (void) aRecordHandle;
100 : : // Remove handle again at some point
101 : : // g_variant_unref( aRet );
102 : : // fprintf( stderr, "Manager gotten\n" );
103 : : //
104 : : // // Name for default adapter
105 : : // GVariant *aAdapter = g_dbus_connection_call_sync( aConnection,
106 : : // "org.bluez", "/", "org.bluez.Manager",
107 : : // "DefaultAdapter", NULL,
108 : : // G_VARIANT_TYPE_TUPLE,
109 : : // G_DBUS_CALL_FLAGS_NONE, -1, NULL, &aError);
110 : : // GVariant *aAdapterName = g_variant_get_child_value( aAdapter, 0 );
111 : : // if ( aError )
112 : : // {
113 : : // fprintf( stderr, aError->message );
114 : : // g_error_free( aError );
115 : : // }
116 : : // fprintf( stderr, (const char*) g_variant_get_string( aAdapterName, NULL ) );
117 : :
118 : :
119 : :
120 : :
121 : : // g_type_init();
122 : : // GError* aError = NULL;
123 : : // GDBusConnection* aConnection = g_bus_get_sync( G_BUS_TYPE_SYSTEM, NULL, &aError );
124 : : // fprintf( stderr, "Connection gotten\n" );
125 : : // if ( aError )
126 : : // {
127 : : // fprintf( stderr, aError->message );
128 : : // g_error_free( aError );
129 : : // }
130 : : // // GDBusObjectManager* aManager = g_dbus_object_manager_client_new_sync( aConnection,
131 : : // // G_DBUS_OBJECT_MANAGER_CLIENT_FLAGS_NONE, "org.bluez.Manager", "/org/bluez",
132 : : // // NULL, NULL, NULL, NULL, &aError );
133 : : // // if ( aError )
134 : : // // {
135 : : // // fprintf( stderr, aError->message );
136 : : // // g_error_free( aError );
137 : : // // }
138 : : // fprintf( stderr, "Manager gotten\n" );
139 : : //
140 : : // // Name for default adapter
141 : : // GVariant *aAdapter = g_dbus_connection_call_sync( aConnection,
142 : : // "org.bluez", "/", "org.bluez.Manager",
143 : : // "DefaultAdapter", NULL,
144 : : // G_VARIANT_TYPE_TUPLE,
145 : : // G_DBUS_CALL_FLAGS_NONE, -1, NULL, &aError);
146 : : // GVariant *aAdapterName = g_variant_get_child_value( aAdapter, 0 );
147 : : // if ( aError )
148 : : // {
149 : : // fprintf( stderr, aError->message );
150 : : // g_error_free( aError );
151 : : // }
152 : : // fprintf( stderr, (const char*) g_variant_get_string( aAdapterName, NULL ) );
153 : :
154 : :
155 : : // ---------------- DEVICE ADDRESS
156 : : int aSocket;
157 [ # # ]: 0 : if ( (aSocket = socket( AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM )) < 0 )
158 : : {
159 : : // Error
160 : : return;
161 : : }
162 : :
163 : : sockaddr_rc aAddr;
164 : 0 : aAddr.rc_family = AF_BLUETOOTH;
165 : : // aAddr.rc_bdaddr = {{0, 0, 0, 0, 0, 0}}; // BDADDR_ANY is broken
166 : 0 : memset( &aAddr.rc_bdaddr, 0, sizeof( aAddr.rc_bdaddr ) );
167 : 0 : aAddr.rc_channel = 5;
168 : :
169 [ # # ]: 0 : if ( bind( aSocket, (sockaddr*) &aAddr, sizeof(aAddr)) < 0 ) {
170 [ # # ]: 0 : close( aSocket );
171 : : return;
172 : : }
173 : :
174 [ # # ]: 0 : if ( listen( aSocket, 1 ) < 0 )
175 : : {
176 [ # # ]: 0 : close( aSocket );
177 : : return;
178 : : }
179 : :
180 : : sockaddr_rc aRemoteAddr;
181 : 0 : socklen_t aRemoteAddrLen = sizeof(aRemoteAddr);
182 : : int bSocket;
183 [ # # ][ # # ]: 0 : if ( (bSocket = accept(aSocket, (sockaddr*) &aRemoteAddr, &aRemoteAddrLen)) < 0 )
184 : : {
185 [ # # ]: 0 : close( aSocket );
186 : : return;
187 : : } else {
188 : : // fprintf( stderr, "Accepted Bluetooth\n" );
189 : :
190 [ # # ][ # # ]: 0 : Communicator* pCommunicator = new Communicator( new BufferedStreamSocket( bSocket) );
[ # # ][ # # ]
191 [ # # ]: 0 : mpCommunicators->push_back( pCommunicator );
192 [ # # ]: 0 : pCommunicator->launch();
193 : :
194 : : }
195 : :
196 : : #endif // GLIB_VERSION_2_26
197 : : #endif // LINUX
198 : :
199 : : #ifdef WIN32
200 : :
201 : : #endif
202 : :
203 : : #ifdef MACOSX
204 : :
205 : : #endif
206 : : }
207 : :
208 : :
209 : : BluetoothServer *sd::BluetoothServer::spServer = NULL;
210 : :
211 : 0 : void BluetoothServer::setup( std::vector<Communicator*>* pCommunicators )
212 : : {
213 [ # # ]: 0 : if (spServer)
214 : 0 : return;
215 : :
216 [ # # ]: 0 : spServer = new BluetoothServer( pCommunicators );
217 : 0 : spServer->launch();
218 : : }
219 : :
220 : :
221 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|