LibreOffice Module sd (master) 1
BluetoothServer.cxx
Go to the documentation of this file.
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
10#include "BluetoothServer.hxx"
11
12#include <iostream>
13#include <memory>
14#include <new>
15#include <string_view>
16
17#include <sal/log.hxx>
18
19#ifdef LINUX_BLUETOOTH
20 #include <glib.h>
21 #include <dbus/dbus.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <unistd.h>
25 #include <sys/socket.h>
26 #include <bluetooth/bluetooth.h>
27 #include <bluetooth/rfcomm.h>
30#endif
31
32#ifdef _WIN32
33 // LO vs WinAPI conflict
34 #undef WB_LEFT
35 #undef WB_RIGHT
36 #include <winsock2.h>
37 #include <ws2bth.h>
39#endif
40
41#ifdef MACOSX
42 #include <iomanip>
43 #include <osl/conditn.hxx>
44 #include <premac.h>
45 #import <CoreFoundation/CoreFoundation.h>
46 #import <IOBluetooth/IOBluetoothUtilities.h>
47 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
48 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
49 #include <postmac.h>
50 #import "OSXBluetooth.h"
52#endif
53
54#include "Communicator.hxx"
55
56using namespace sd;
57
58#ifdef LINUX_BLUETOOTH
59
60namespace {
61
62struct DBusObject {
63 OString maBusName;
64 OString maPath;
65 OString maInterface;
66
67 DBusObject() { }
68 DBusObject( const char *pBusName, const char *pPath, const char *pInterface )
69 : maBusName( pBusName ), maPath( pPath ), maInterface( pInterface ) { }
70
71 DBusMessage *getMethodCall( const char *pName )
72 {
73 return dbus_message_new_method_call( maBusName.getStr(), maPath.getStr(),
74 maInterface.getStr(), pName );
75 }
76 std::unique_ptr<DBusObject> cloneForInterface( const char *pInterface )
77 {
78 std::unique_ptr<DBusObject> pObject(new DBusObject());
79
80 pObject->maBusName = maBusName;
81 pObject->maPath = maPath;
82 pObject->maInterface = pInterface;
83
84 return pObject;
85 }
86};
87
88}
89
90static std::unique_ptr<DBusObject> getBluez5Adapter(DBusConnection *pConnection);
91
92struct sd::BluetoothServer::Impl {
93 // the glib mainloop running in the thread
94 GMainContext *mpContext;
95 DBusConnection *mpConnection;
96 std::unique_ptr<DBusObject> mpService;
97 enum class BluezVersion { BLUEZ4, BLUEZ5, UNKNOWN };
98 BluezVersion maBluezVersion;
99
100 Impl()
101 : mpContext( g_main_context_new() )
102 , mpConnection( nullptr )
103 , maBluezVersion( BluezVersion::UNKNOWN )
104 { }
105
106 std::unique_ptr<DBusObject> getAdapter()
107 {
108 if (mpService)
109 {
110 return mpService->cloneForInterface( "org.bluez.Adapter" );
111 }
112 else if (spServer->mpImpl->maBluezVersion == BluezVersion::BLUEZ5)
113 {
114 return getBluez5Adapter(mpConnection);
115 }
116 else
117 {
118 return nullptr;
119 }
120 }
121};
122
123static DBusConnection *
124dbusConnectToNameOnBus()
125{
126 DBusError aError;
127 DBusConnection *pConnection;
128
129 dbus_error_init( &aError );
130
131 pConnection = dbus_bus_get( DBUS_BUS_SYSTEM, &aError );
132 if( !pConnection || dbus_error_is_set( &aError ))
133 {
134 SAL_WARN( "sdremote.bluetooth", "failed to get dbus system bus: " << aError.message );
135 dbus_error_free( &aError );
136 return nullptr;
137 }
138
139 return pConnection;
140}
141
142static DBusMessage *
143sendUnrefAndWaitForReply( DBusConnection *pConnection, DBusMessage *pMsg )
144{
145 DBusPendingCall *pPending = nullptr;
146
147 if( !pMsg || !dbus_connection_send_with_reply( pConnection, pMsg, &pPending,
148 -1 /* default timeout */ ) )
149 {
150 SAL_WARN( "sdremote.bluetooth", "Memory allocation failed on message send" );
151 dbus_message_unref( pMsg );
152 return nullptr;
153 }
154 dbus_connection_flush( pConnection );
155 dbus_message_unref( pMsg );
156
157 dbus_pending_call_block( pPending ); // block for reply
158
159 pMsg = dbus_pending_call_steal_reply( pPending );
160 if( !pMsg )
161 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
162
163 dbus_pending_call_unref( pPending );
164 return pMsg;
165}
166
167static bool
168isBluez5Available(DBusConnection *pConnection)
169{
170 DBusMessage *pMsg;
171
172 // Simplest ways to check whether we have Bluez 5+ is to check
173 // that we can obtain adapters using the new interfaces.
174 // The first two error checks however don't tell us anything as they should
175 // succeed as long as dbus is working correctly.
176 pMsg = DBusObject( "org.bluez", "/", "org.freedesktop.DBus.ObjectManager" ).getMethodCall( "GetManagedObjects" );
177 if (!pMsg)
178 {
179 SAL_INFO("sdremote.bluetooth", "No GetManagedObjects call created");
180 return false;
181 }
182
183 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
184 if (!pMsg)
185 {
186 SAL_INFO("sdremote.bluetooth", "No reply received");
187 return false;
188 }
189
190 // If dbus is working correctly and we aren't on bluez 5 this is where we
191 // should actually get the error.
192 if (dbus_message_get_error_name( pMsg ))
193 {
194 SAL_INFO( "sdremote.bluetooth", "GetManagedObjects call failed with \""
195 << dbus_message_get_error_name( pMsg )
196 << "\" -- we don't seem to have Bluez 5 available");
197 return false;
198 }
199 SAL_INFO("sdremote.bluetooth", "GetManagedObjects call seems to have succeeded -- we must be on Bluez 5");
200 dbus_message_unref(pMsg);
201 return true;
202}
203
204static std::unique_ptr<DBusObject>
205getBluez5Adapter(DBusConnection *pConnection)
206{
207 DBusMessage *pMsg;
208 // This returns a list of objects where we need to find the first
209 // org.bluez.Adapter1 .
210 pMsg = DBusObject( "org.bluez", "/", "org.freedesktop.DBus.ObjectManager" ).getMethodCall( "GetManagedObjects" );
211 if (!pMsg)
212 return nullptr;
213
214 const gchar* const pInterfaceType = "org.bluez.Adapter1";
215
216 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
217
218 DBusMessageIter aObjectIterator;
219 if (pMsg && dbus_message_iter_init(pMsg, &aObjectIterator))
220 {
221 if (DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&aObjectIterator))
222 {
223 DBusMessageIter aObject;
224 dbus_message_iter_recurse(&aObjectIterator, &aObject);
225 do
226 {
227 if (DBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&aObject))
228 {
229 DBusMessageIter aContainerIter;
230 dbus_message_iter_recurse(&aObject, &aContainerIter);
231 char *pPath = nullptr;
232 do
233 {
234 if (DBUS_TYPE_OBJECT_PATH == dbus_message_iter_get_arg_type(&aContainerIter))
235 {
236 dbus_message_iter_get_basic(&aContainerIter, &pPath);
237 SAL_INFO( "sdremote.bluetooth", "Something retrieved: '"
238 << pPath << "' '");
239 }
240 else if (DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&aContainerIter))
241 {
242 DBusMessageIter aInnerIter;
243 dbus_message_iter_recurse(&aContainerIter, &aInnerIter);
244 do
245 {
246 if (DBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&aInnerIter))
247 {
248 DBusMessageIter aInnerInnerIter;
249 dbus_message_iter_recurse(&aInnerIter, &aInnerInnerIter);
250 do
251 {
252 if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&aInnerInnerIter))
253 {
254 char* pMessage;
255
256 dbus_message_iter_get_basic(&aInnerInnerIter, &pMessage);
257 if (pMessage == std::string_view("org.bluez.Adapter1"))
258 {
259 dbus_message_unref(pMsg);
260 if (pPath)
261 {
262 return std::make_unique<DBusObject>( "org.bluez", pPath, pInterfaceType );
263 }
264 assert(false); // We should already have pPath provided for us.
265 }
266 }
267 }
268 while (dbus_message_iter_next(&aInnerInnerIter));
269 }
270 }
271 while (dbus_message_iter_next(&aInnerIter));
272 }
273 }
274 while (dbus_message_iter_next(&aContainerIter));
275 }
276 }
277 while (dbus_message_iter_next(&aObject));
278 }
279 dbus_message_unref(pMsg);
280 }
281
282 return nullptr;
283}
284
285static DBusObject *
286bluez4GetDefaultService( DBusConnection *pConnection )
287{
288 DBusMessage *pMsg;
289 DBusMessageIter it;
290 const gchar* const pInterfaceType = "org.bluez.Service";
291
292 // org.bluez.manager only exists for bluez 4.
293 // getMethodCall should return NULL if there is any issue e.g. the
294 // if org.bluez.manager doesn't exist.
295 pMsg = DBusObject( "org.bluez", "/", "org.bluez.Manager" ).getMethodCall( "DefaultAdapter" );
296
297 if (!pMsg)
298 {
299 SAL_WARN("sdremote.bluetooth", "Couldn't retrieve DBusObject for DefaultAdapter");
300 return nullptr;
301 }
302
303 SAL_INFO("sdremote.bluetooth", "successfully retrieved org.bluez.Manager.DefaultAdapter, attempting to use.");
304 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
305
306 if(!pMsg || !dbus_message_iter_init( pMsg, &it ) )
307 {
308 return nullptr;
309 }
310
311 // This works for Bluez 4
312 if( DBUS_TYPE_OBJECT_PATH == dbus_message_iter_get_arg_type( &it ) )
313 {
314 const char *pObjectPath = nullptr;
315 dbus_message_iter_get_basic( &it, &pObjectPath );
316 SAL_INFO( "sdremote.bluetooth", "DefaultAdapter retrieved: '"
317 << pObjectPath << "' '" << pInterfaceType << "'" );
318 dbus_message_unref( pMsg );
319 return new DBusObject( "org.bluez", pObjectPath, pInterfaceType );
320 }
321 // Some form of error, e.g. if we have bluez 5 we get a message that
322 // this method doesn't exist.
323 else if ( DBUS_TYPE_STRING == dbus_message_iter_get_arg_type( &it ) )
324 {
325 const char *pMessage = nullptr;
326 dbus_message_iter_get_basic( &it, &pMessage );
327 SAL_INFO( "sdremote.bluetooth", "Error message: '"
328 << pMessage << "' '" << pInterfaceType << "'" );
329 }
330 else
331 {
332 SAL_INFO( "sdremote.bluetooth", "invalid type of reply to DefaultAdapter: '"
333 << static_cast<char>(dbus_message_iter_get_arg_type( &it )) << "'" );
334 }
335 dbus_message_unref(pMsg);
336 return nullptr;
337}
338
339static bool
340bluez4RegisterServiceRecord( DBusConnection *pConnection, DBusObject *pAdapter,
341 const char *pServiceRecord )
342{
343 DBusMessage *pMsg;
344 DBusMessageIter it;
345
346 pMsg = pAdapter->getMethodCall( "AddRecord" );
347 dbus_message_iter_init_append( pMsg, &it );
348 dbus_message_iter_append_basic( &it, DBUS_TYPE_STRING, &pServiceRecord );
349
350 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
351
352 if( !pMsg || !dbus_message_iter_init( pMsg, &it ) ||
353 dbus_message_iter_get_arg_type( &it ) != DBUS_TYPE_UINT32 )
354 {
355 SAL_WARN( "sdremote.bluetooth", "SDP registration failed" );
356 return false;
357 }
358
359 // We ignore the uint de-registration handle we get back:
360 // bluez will clean us up automatically on exit
361
362 return true;
363}
364
365static void
366bluezCreateAttachListeningSocket( GMainContext *pContext, GPollFD *pSocketFD )
367{
368 int nSocket;
369
370 pSocketFD->fd = -1;
371
372 if( ( nSocket = socket( AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM ) ) < 0 )
373 {
374 SAL_WARN( "sdremote.bluetooth", "failed to open bluetooth socket with error " << nSocket );
375 return;
376 }
377
378 sockaddr_rc aAddr;
379 // Initialize whole structure. Mainly to appease valgrind, which
380 // doesn't know about the padding at the end of sockaddr_rc which
381 // it will dutifully check for definedness. But also the standard
382 // definition of BDADDR_ANY is unusable in C++ code, so just use
383 // memset to set aAddr.rc_bdaddr to 0.
384 memset( &aAddr, 0, sizeof( aAddr ) );
385 aAddr.rc_family = AF_BLUETOOTH;
386 aAddr.rc_channel = 5;
387
388 int a;
389 if ( ( a = bind( nSocket, reinterpret_cast<sockaddr*>(&aAddr), sizeof(aAddr) ) ) < 0 ) {
390 SAL_WARN( "sdremote.bluetooth", "bind failed with error" << a );
391 close( nSocket );
392 return;
393 }
394
395 if ( ( a = listen( nSocket, 1 ) ) < 0 )
396 {
397 SAL_WARN( "sdremote.bluetooth", "listen failed with error" << a );
398 close( nSocket );
399 return;
400 }
401
402 // set non-blocking behaviour ...
403 if( fcntl( nSocket, F_SETFL, O_NONBLOCK) < 0 )
404 {
405 close( nSocket );
406 return;
407 }
408
409 pSocketFD->fd = nSocket;
410 pSocketFD->events = G_IO_IN | G_IO_PRI;
411 pSocketFD->revents = 0;
412
413 g_main_context_add_poll( pContext, pSocketFD, G_PRIORITY_DEFAULT );
414}
415
416static void
417bluezDetachCloseSocket( GMainContext *pContext, GPollFD *pSocketFD )
418{
419 if( pSocketFD->fd >= 0 )
420 {
421 close( pSocketFD->fd );
422 g_main_context_remove_poll( pContext, pSocketFD );
423 pSocketFD->fd = -1;
424 }
425}
426
427#endif // LINUX_BLUETOOTH
428
429#if defined(MACOSX)
430
431OSXBluetoothWrapper::OSXBluetoothWrapper( IOBluetoothRFCOMMChannel* channel ) :
432 mpChannel(channel),
433 mnMTU(0),
434 mHaveBytes(),
435 mMutex(),
436 mBuffer()
437{
438 // silly enough, can't write more than mnMTU bytes at once
439 mnMTU = [channel getMTU];
440
441 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::OSXBluetoothWrapper(): mnMTU=" << mnMTU );
442}
443
444sal_Int32 OSXBluetoothWrapper::readLine( OString& aLine )
445{
446 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine()" );
447
448 while( true )
449 {
450 {
451 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: entering mutex" );
452 ::osl::MutexGuard aQueueGuard( mMutex );
453 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: entered mutex" );
454
455#ifdef SAL_LOG_INFO
456 // We should have in the sal logging some standard way to
457 // output char buffers with non-printables escaped.
458 std::ostringstream s;
459 if (mBuffer.size() > 0)
460 {
461 for (unsigned char *p = reinterpret_cast<unsigned char *>(mBuffer.data()); p != reinterpret_cast<unsigned char *>(mBuffer.data()) + mBuffer.size(); p++)
462 {
463 if (*p == '\n')
464 s << "\\n";
465 else if (*p < ' ' || *p >= 0x7F)
466 s << "\\0x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(*p) << std::setfill(' ') << std::setw(1) << std::dec;
467 else
468 s << *p;
469 }
470 }
471 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine mBuffer: \"" << s.str() << "\"" );
472#endif
473
474 // got enough bytes to return a line?
475 std::vector<char>::iterator aIt;
476 if ( (aIt = find( mBuffer.begin(), mBuffer.end(), '\n' ))
477 != mBuffer.end() )
478 {
479 sal_uInt64 aLocation = aIt - mBuffer.begin();
480
481 aLine = OString( &(*mBuffer.begin()), aLocation );
482
483 mBuffer.erase( mBuffer.begin(), aIt + 1 ); // Also delete the empty line
484
485 // yeps
486 SAL_INFO( "sdremote.bluetooth", " returning, got \"" << OStringToOUString( aLine, RTL_TEXTENCODING_UTF8 ) << "\"" );
487 return aLine.getLength() + 1;
488 }
489
490 // nope - wait some more (after releasing the mutex)
491 SAL_INFO( "sdremote.bluetooth", " resetting mHaveBytes" );
492 mHaveBytes.reset();
493 SAL_INFO( "sdremote.bluetooth", " leaving mutex" );
494 }
495
496 SAL_INFO( "sdremote.bluetooth", " waiting for mHaveBytes" );
497 mHaveBytes.wait();
498 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: got mHaveBytes" );
499 }
500}
501
502sal_Int32 OSXBluetoothWrapper::write( const void* pBuffer, sal_uInt32 n )
503{
504 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::write(" << pBuffer << ", " << n << ") mpChannel=" << mpChannel );
505
506 char const * ptr = static_cast<char const *>(pBuffer);
507 sal_uInt32 nBytesWritten = 0;
508
509 if (mpChannel == nil)
510 return 0;
511
512 while( nBytesWritten < n )
513 {
514 int toWrite = n - nBytesWritten;
515 toWrite = toWrite <= mnMTU ? toWrite : mnMTU;
516 if ( [mpChannel writeSync:const_cast<char *>(ptr) length:toWrite] != kIOReturnSuccess )
517 {
518 SAL_INFO( "sdremote.bluetooth", " [mpChannel writeSync:" << static_cast<void const *>(ptr) << " length:" << toWrite << "] returned error, total written " << nBytesWritten );
519 return nBytesWritten;
520 }
521 ptr += toWrite;
522 nBytesWritten += toWrite;
523 }
524 SAL_INFO( "sdremote.bluetooth", " total written " << nBytesWritten );
525 return nBytesWritten;
526}
527
528void OSXBluetoothWrapper::appendData(void* pBuffer, size_t len)
529{
530 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData(" << pBuffer << ", " << len << ")" );
531
532 if( len )
533 {
534 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData: entering mutex" );
535 ::osl::MutexGuard aQueueGuard( mMutex );
536 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData: entered mutex" );
537 mBuffer.insert(mBuffer.begin()+mBuffer.size(),
538 static_cast<char*>(pBuffer), static_cast<char *>(pBuffer)+len);
539 SAL_INFO( "sdremote.bluetooth", " setting mHaveBytes" );
540 mHaveBytes.set();
541 SAL_INFO( "sdremote.bluetooth", " leaving mutex" );
542 }
543}
544
546{
547 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::channelClosed()" );
548
549 mpChannel = nil;
550}
551
552void incomingCallback( void *userRefCon,
553 IOBluetoothUserNotificationRef,
554 IOBluetoothObjectRef objectRef )
555{
556 SAL_INFO( "sdremote.bluetooth", "incomingCallback()" );
557
558 BluetoothServer* pServer = static_cast<BluetoothServer*>(userRefCon);
559
560 IOBluetoothRFCOMMChannel* channel = [IOBluetoothRFCOMMChannel withRFCOMMChannelRef:reinterpret_cast<IOBluetoothRFCOMMChannelRef>(objectRef)];
561
562 OSXBluetoothWrapper* socket = new OSXBluetoothWrapper( channel);
563 Communicator* pCommunicator = new Communicator( std::unique_ptr<IBluetoothSocket>(socket) );
564 pServer->addCommunicator( pCommunicator );
565
566 ChannelDelegate* delegate = [[ChannelDelegate alloc] initWithCommunicatorAndSocket: pCommunicator socket: socket];
567 [channel setDelegate: delegate];
568 [delegate retain];
569
570 pCommunicator->launch();
571}
572
573void BluetoothServer::addCommunicator( Communicator* pCommunicator )
574{
575 mpCommunicators->push_back( pCommunicator );
576}
577
578#endif // MACOSX
579
580#ifdef LINUX_BLUETOOTH
581
582extern "C" {
583 static gboolean ensureDiscoverable_cb(gpointer)
584 {
586 return FALSE; // remove source
587 }
588 static gboolean restoreDiscoverable_cb(gpointer)
589 {
591 return FALSE; // remove source
592 }
593}
594
595/*
596 * Bluez 4 uses custom methods for setting properties, whereas Bluez 5+
597 * implements properties using the generic "org.freedesktop.DBus.Properties"
598 * interface -- hence we have a specific Bluez 4 function to deal with the
599 * old style of reading properties.
600 */
601static bool
602getBluez4BooleanProperty( DBusConnection *pConnection, DBusObject *pAdapter,
603 const char *pPropertyName, bool *pBoolean )
604{
605 *pBoolean = false;
606
607 if( !pAdapter )
608 return false;
609
610 DBusMessage *pMsg;
611 pMsg = sendUnrefAndWaitForReply( pConnection,
612 pAdapter->getMethodCall( "GetProperties" ) );
613
614 DBusMessageIter it;
615 if( !pMsg || !dbus_message_iter_init( pMsg, &it ) )
616 {
617 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
618 return false;
619 }
620
621 if( DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type( &it ) )
622 {
623 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
624 return false;
625 }
626
627 DBusMessageIter arrayIt;
628 dbus_message_iter_recurse( &it, &arrayIt );
629
630 while( dbus_message_iter_get_arg_type( &arrayIt ) == DBUS_TYPE_DICT_ENTRY )
631 {
632 DBusMessageIter dictIt;
633 dbus_message_iter_recurse( &arrayIt, &dictIt );
634
635 const char *pName = nullptr;
636 if( dbus_message_iter_get_arg_type( &dictIt ) == DBUS_TYPE_STRING )
637 {
638 dbus_message_iter_get_basic( &dictIt, &pName );
639 if( pName != nullptr && !strcmp( pName, pPropertyName ) )
640 {
641 SAL_INFO( "sdremote.bluetooth", "hit " << pPropertyName << " property" );
642 dbus_message_iter_next( &dictIt );
643 dbus_bool_t bBool = false;
644
645 if( dbus_message_iter_get_arg_type( &dictIt ) == DBUS_TYPE_VARIANT )
646 {
647 DBusMessageIter variantIt;
648 dbus_message_iter_recurse( &dictIt, &variantIt );
649
650 if( dbus_message_iter_get_arg_type( &variantIt ) == DBUS_TYPE_BOOLEAN )
651 {
652 dbus_message_iter_get_basic( &variantIt, &bBool );
653 SAL_INFO( "sdremote.bluetooth", "" << pPropertyName << " is " << bBool );
654 *pBoolean = bBool;
655 return true;
656 }
657 else
658 SAL_WARN( "sdremote.bluetooth", "" << pPropertyName << " type " <<
659 dbus_message_iter_get_arg_type( &variantIt ) );
660 }
661 else
662 SAL_WARN( "sdremote.bluetooth", "variant type ? " <<
663 dbus_message_iter_get_arg_type( &dictIt ) );
664 }
665 else
666 {
667 const char *pStr = pName ? pName : "<null>";
668 SAL_INFO( "sdremote.bluetooth", "property '" << pStr << "'" );
669 }
670 }
671 else
672 SAL_WARN( "sdremote.bluetooth", "unexpected property key type "
673 << dbus_message_iter_get_arg_type( &dictIt ) );
674 dbus_message_iter_next( &arrayIt );
675 }
676 dbus_message_unref( pMsg );
677
678 return false;
679}
680
681/*
682 * This gets an org.freedesktop.DBus.Properties boolean
683 * (as opposed to the old Bluez 4 custom properties methods as visible above).
684 */
685static bool
686getDBusBooleanProperty( DBusConnection *pConnection, DBusObject *pAdapter,
687 const char *pPropertyName, bool *pBoolean )
688{
689 assert( pAdapter );
690
691 *pBoolean = false;
692 bool bRet = false;
693
694 std::unique_ptr< DBusObject > pProperties (
695 pAdapter->cloneForInterface( "org.freedesktop.DBus.Properties" ) );
696
697 DBusMessage *pMsg = pProperties->getMethodCall( "Get" );
698
699 DBusMessageIter itIn;
700 dbus_message_iter_init_append( pMsg, &itIn );
701 const char* pInterface = "org.bluez.Adapter1";
702 dbus_message_iter_append_basic( &itIn, DBUS_TYPE_STRING, &pInterface );
703 dbus_message_iter_append_basic( &itIn, DBUS_TYPE_STRING, &pPropertyName );
704 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
705
706 DBusMessageIter it;
707 if( !pMsg || !dbus_message_iter_init( pMsg, &it ) )
708 {
709 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
710 return false;
711 }
712
713 if( DBUS_TYPE_VARIANT != dbus_message_iter_get_arg_type( &it ) )
714 {
715 SAL_WARN( "sdremote.bluetooth", "invalid return type" );
716 }
717 else
718 {
719 DBusMessageIter variantIt;
720 dbus_message_iter_recurse( &it, &variantIt );
721
722 if( dbus_message_iter_get_arg_type( &variantIt ) == DBUS_TYPE_BOOLEAN )
723 {
724 dbus_bool_t bBool = false;
725 dbus_message_iter_get_basic( &variantIt, &bBool );
726 SAL_INFO( "sdremote.bluetooth", "" << pPropertyName << " is " << bBool );
727 *pBoolean = bBool;
728 bRet = true;
729 }
730 else
731 {
732 SAL_WARN( "sdremote.bluetooth", "" << pPropertyName << " type " <<
733 dbus_message_iter_get_arg_type( &variantIt ) );
734 }
735
736 const char* pError = dbus_message_get_error_name( pMsg );
737 if ( pError )
738 {
739 SAL_WARN( "sdremote.bluetooth",
740 "Get failed for " << pPropertyName << " on " <<
741 pAdapter->maPath << " with error: " << pError );
742 }
743 }
744 dbus_message_unref( pMsg );
745
746 return bRet;
747}
748
749static void
750setDBusBooleanProperty( DBusConnection *pConnection, DBusObject *pAdapter,
751 const char *pPropertyName, bool bBoolean )
752{
753 assert( pAdapter );
754
755 std::unique_ptr< DBusObject > pProperties(
756 pAdapter->cloneForInterface( "org.freedesktop.DBus.Properties" ) );
757
758 DBusMessage *pMsg = pProperties->getMethodCall( "Set" );
759
760 DBusMessageIter itIn;
761 dbus_message_iter_init_append( pMsg, &itIn );
762 const char* pInterface = "org.bluez.Adapter1";
763 dbus_message_iter_append_basic( &itIn, DBUS_TYPE_STRING, &pInterface );
764 dbus_message_iter_append_basic( &itIn, DBUS_TYPE_STRING, &pPropertyName );
765
766 {
767 DBusMessageIter varIt;
768 dbus_message_iter_open_container( &itIn, DBUS_TYPE_VARIANT,
769 DBUS_TYPE_BOOLEAN_AS_STRING, &varIt );
770 dbus_bool_t bDBusBoolean = bBoolean;
771 dbus_message_iter_append_basic( &varIt, DBUS_TYPE_BOOLEAN, &bDBusBoolean );
772 dbus_message_iter_close_container( &itIn, &varIt );
773 }
774
775 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
776
777 if( !pMsg )
778 {
779 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
780 }
781 else
782 {
783 const char* pError = dbus_message_get_error_name( pMsg );
784 if ( pError )
785 {
786 SAL_WARN( "sdremote.bluetooth",
787 "Set failed for " << pPropertyName << " on " <<
788 pAdapter->maPath << " with error: " << pError );
789 }
790 dbus_message_unref( pMsg );
791 }
792}
793
794static bool
795getDiscoverable( DBusConnection *pConnection, DBusObject *pAdapter )
796{
797 if (pAdapter->maInterface == "org.bluez.Adapter") // Bluez 4
798 {
799 bool bDiscoverable;
800 if( getBluez4BooleanProperty(pConnection, pAdapter, "Discoverable", &bDiscoverable ) )
801 return bDiscoverable;
802 }
803 else if (pAdapter->maInterface == "org.bluez.Adapter1") // Bluez 5
804 {
805 bool bDiscoverable;
806 if ( getDBusBooleanProperty(pConnection, pAdapter, "Discoverable", &bDiscoverable ) )
807 return bDiscoverable;
808 }
809 return false;
810}
811
812static void
813setDiscoverable( DBusConnection *pConnection, DBusObject *pAdapter, bool bDiscoverable )
814{
815 SAL_INFO( "sdremote.bluetooth", "setDiscoverable to " << bDiscoverable );
816
817 if (pAdapter->maInterface == "org.bluez.Adapter") // Bluez 4
818 {
819 bool bPowered = false;
820 if( !getBluez4BooleanProperty( pConnection, pAdapter, "Powered", &bPowered ) || !bPowered )
821 return; // nothing to do
822
823 DBusMessage *pMsg;
824 DBusMessageIter it, varIt;
825
826 // set timeout to zero
827 pMsg = pAdapter->getMethodCall( "SetProperty" );
828 dbus_message_iter_init_append( pMsg, &it );
829 const char *pTimeoutStr = "DiscoverableTimeout";
830 dbus_message_iter_append_basic( &it, DBUS_TYPE_STRING, &pTimeoutStr );
831 dbus_message_iter_open_container( &it, DBUS_TYPE_VARIANT,
832 DBUS_TYPE_UINT32_AS_STRING, &varIt );
833 dbus_uint32_t nTimeout = 0;
834 dbus_message_iter_append_basic( &varIt, DBUS_TYPE_UINT32, &nTimeout );
835 dbus_message_iter_close_container( &it, &varIt );
836 dbus_connection_send( pConnection, pMsg, nullptr ); // async send - why not ?
837 dbus_message_unref( pMsg );
838
839 // set discoverable value
840 pMsg = pAdapter->getMethodCall( "SetProperty" );
841 dbus_message_iter_init_append( pMsg, &it );
842 const char *pDiscoverableStr = "Discoverable";
843 dbus_message_iter_append_basic( &it, DBUS_TYPE_STRING, &pDiscoverableStr );
844 dbus_message_iter_open_container( &it, DBUS_TYPE_VARIANT,
845 DBUS_TYPE_BOOLEAN_AS_STRING, &varIt );
846 dbus_bool_t bValue = bDiscoverable;
847 dbus_message_iter_append_basic( &varIt, DBUS_TYPE_BOOLEAN, &bValue );
848 dbus_message_iter_close_container( &it, &varIt ); // async send - why not ?
849 dbus_connection_send( pConnection, pMsg, nullptr );
850 dbus_message_unref( pMsg );
851 }
852 else if (pAdapter->maInterface == "org.bluez.Adapter1") // Bluez 5
853 {
854 setDBusBooleanProperty(pConnection, pAdapter, "Discoverable", bDiscoverable );
855 }
856}
857
858static std::unique_ptr<DBusObject>
859registerWithDefaultAdapter( DBusConnection *pConnection )
860{
861 std::unique_ptr<DBusObject> pService(bluez4GetDefaultService( pConnection ));
862 if( pService )
863 {
864 if( !bluez4RegisterServiceRecord( pConnection, pService.get(),
866 {
867 return nullptr;
868 }
869 }
870
871 return pService;
872}
873
874static void ProfileUnregisterFunction
875(DBusConnection *, void *)
876{
877 // We specifically don't need to do anything here.
878}
879
880static DBusHandlerResult ProfileMessageFunction
881(DBusConnection *pConnection, DBusMessage *pMessage, void *user_data)
882{
883 SAL_INFO("sdremote.bluetooth", "ProfileMessageFunction||" << dbus_message_get_interface(pMessage) << "||" << dbus_message_get_member(pMessage));
884
885 if (dbus_message_get_interface(pMessage) == std::string_view("org.bluez.Profile1"))
886 {
887 if (dbus_message_get_member(pMessage) == std::string_view("Release"))
888 {
889 return DBUS_HANDLER_RESULT_HANDLED;
890 }
891 else if (dbus_message_get_member(pMessage) == std::string_view("NewConnection"))
892 {
893 if (!dbus_message_has_signature(pMessage, "oha{sv}"))
894 {
895 SAL_WARN("sdremote.bluetooth", "wrong signature for NewConnection");
896 }
897
898 DBusMessageIter it;
899 if (!dbus_message_iter_init(pMessage, &it))
900 SAL_WARN( "sdremote.bluetooth", "error init dbus" );
901 else
902 {
903 char* pPath;
904 dbus_message_iter_get_basic(&it, &pPath);
905 SAL_INFO("sdremote.bluetooth", "Adapter path:" << pPath);
906
907 if (!dbus_message_iter_next(&it))
908 SAL_WARN("sdremote.bluetooth", "not enough parameters passed");
909
910 // DBUS_TYPE_UNIX_FD == 'h' -- doesn't exist in older versions
911 // of dbus (< 1.3?) hence defined manually for now
912 if ('h' == dbus_message_iter_get_arg_type(&it))
913 {
914
915 int nDescriptor;
916 dbus_message_iter_get_basic(&it, &nDescriptor);
917 std::vector<Communicator*>* pCommunicators = static_cast<std::vector<Communicator*>*>(user_data);
918
919 // Bluez gives us non-blocking sockets, but our code relies
920 // on blocking behaviour.
921 (void)fcntl(nDescriptor, F_SETFL, fcntl(nDescriptor, F_GETFL) & ~O_NONBLOCK);
922
923 SAL_INFO( "sdremote.bluetooth", "connection accepted " << nDescriptor);
924 Communicator* pCommunicator = new Communicator( std::make_unique<BufferedStreamSocket>( nDescriptor ) );
925 pCommunicators->push_back( pCommunicator );
926 pCommunicator->launch();
927 }
928
929 // For some reason an (empty?) reply is expected.
930 DBusMessage* pRet = dbus_message_new_method_return(pMessage);
931 dbus_connection_send(pConnection, pRet, nullptr);
932 dbus_message_unref(pRet);
933
934 // We could read the remote profile version and features here
935 // (i.e. they are provided as part of the DBusMessage),
936 // however for us they are irrelevant (as our protocol handles
937 // equivalent functionality independently of whether we're on
938 // bluetooth or normal network connection).
939 return DBUS_HANDLER_RESULT_HANDLED;
940 }
941 }
942 else if (dbus_message_get_member(pMessage) == std::string_view("RequestDisconnection"))
943 {
944 return DBUS_HANDLER_RESULT_HANDLED;
945 }
946 }
947 SAL_WARN("sdremote.bluetooth", "Couldn't handle message correctly.");
948 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
949
950}
951
952static void
953setupBluez5Profile1(DBusConnection* pConnection, std::vector<Communicator*>* pCommunicators)
954{
955 bool bErr;
956
957 SAL_INFO("sdremote.bluetooth", "Attempting to register our org.bluez.Profile1");
958 static DBusObjectPathVTable aVTable;
959 aVTable.unregister_function = ProfileUnregisterFunction;
960 aVTable.message_function = ProfileMessageFunction;
961
962 // dbus_connection_try_register_object_path could be used but only exists for
963 // dbus >= 1.2 -- we really shouldn't be trying this twice in any case.
964 // (dbus_connection_try_register_object_path also returns an error with more
965 // information which could be useful for debugging purposes.)
966 bErr = !dbus_connection_register_object_path(pConnection, "/org/libreoffice/bluez/profile1", &aVTable, pCommunicators);
967
968 if (bErr)
969 {
970 SAL_WARN("sdremote.bluetooth", "Failed to register Bluez 5 Profile1 callback, bluetooth won't work.");
971 }
972
973 dbus_connection_flush( pConnection );
974}
975
976static void
977unregisterBluez5Profile(DBusConnection* pConnection)
978{
979 DBusMessage* pMsg = dbus_message_new_method_call("org.bluez", "/org/bluez",
980 "org.bluez.ProfileManager1", "UnregisterProfile");
981 DBusMessageIter it;
982 dbus_message_iter_init_append(pMsg, &it);
983
984 const char *pPath = "/org/libreoffice/bluez/profile1";
985 dbus_message_iter_append_basic(&it, DBUS_TYPE_OBJECT_PATH, &pPath);
986
987 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
988
989 if (pMsg)
990 dbus_message_unref(pMsg);
991
992 dbus_connection_unregister_object_path( pConnection, "/org/libreoffice/bluez/profile1");
993
994 dbus_connection_flush(pConnection);
995}
996
997static bool
998registerBluez5Profile(DBusConnection* pConnection, std::vector<Communicator*>* pCommunicators)
999{
1000 setupBluez5Profile1(pConnection, pCommunicators);
1001
1002 DBusMessage *pMsg;
1003 DBusMessageIter it;
1004
1005 pMsg = dbus_message_new_method_call("org.bluez", "/org/bluez",
1006 "org.bluez.ProfileManager1", "RegisterProfile");
1007 dbus_message_iter_init_append(pMsg, &it);
1008
1009 const char *pPath = "/org/libreoffice/bluez/profile1";
1010 dbus_message_iter_append_basic(&it, DBUS_TYPE_OBJECT_PATH, &pPath);
1011 const char *pUUID = "spp"; // Bluez translates this to 0x1101 for spp
1012 dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &pUUID);
1013
1014 DBusMessageIter aOptionsIter;
1015 dbus_message_iter_open_container(&it, DBUS_TYPE_ARRAY, "{sv}", &aOptionsIter);
1016
1017 DBusMessageIter aEntry;
1018
1019 {
1020 dbus_message_iter_open_container(&aOptionsIter, DBUS_TYPE_DICT_ENTRY, nullptr, &aEntry);
1021
1022 const char *pString = "Name";
1023 dbus_message_iter_append_basic(&aEntry, DBUS_TYPE_STRING, &pString);
1024
1025 const char *pValue = "LibreOffice Impress Remote";
1026 DBusMessageIter aValue;
1027 dbus_message_iter_open_container(&aEntry, DBUS_TYPE_VARIANT, "s", &aValue);
1028 dbus_message_iter_append_basic(&aValue, DBUS_TYPE_STRING, &pValue);
1029 dbus_message_iter_close_container(&aEntry, &aValue);
1030 dbus_message_iter_close_container(&aOptionsIter, &aEntry);
1031 }
1032
1033 dbus_message_iter_close_container(&it, &aOptionsIter);
1034
1035 // Other properties that we could set (but don't, since they appear
1036 // to be useless for us):
1037 // "Service": "0x1101" (not needed, but we used to have it in the manually defined profile).
1038 // "Role": setting this to "server" breaks things, although we think we're a server?
1039 // "Channel": seems to be dealt with automatically (but we used to use 5 in the manual profile).
1040
1041 bool bSuccess = true;
1042
1043 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
1044
1045 DBusError aError;
1046 dbus_error_init(&aError);
1047 if (pMsg && dbus_set_error_from_message( &aError, pMsg ))
1048 {
1049 bSuccess = false;
1050 SAL_WARN("sdremote.bluetooth",
1051 "Failed to register our Profile1 with bluez ProfileManager "
1052 << (aError.message ? aError.message : "<null>"));
1053 }
1054
1055 dbus_error_free(&aError);
1056 if (pMsg)
1057 dbus_message_unref(pMsg);
1058
1059 dbus_connection_flush(pConnection);
1060
1061 return bSuccess;
1062}
1063
1064#endif // LINUX_BLUETOOTH
1065
1066BluetoothServer::BluetoothServer( std::vector<Communicator*>* pCommunicators )
1067 : meWasDiscoverable( UNKNOWN ),
1068 mpCommunicators( pCommunicators )
1069{
1070#ifdef LINUX_BLUETOOTH
1071 // D-Bus requires the following in order to be thread-safe (and we
1072 // potentially access D-Bus from different threads in different places of
1073 // the code base):
1074 if (!dbus_threads_init_default()) {
1075 throw std::bad_alloc();
1076 }
1077
1078 mpImpl.reset(new BluetoothServer::Impl());
1079#endif
1080}
1081
1083{
1084}
1085
1087{
1088#ifdef LINUX_BLUETOOTH
1089 // Push it all across into our mainloop
1090 if( !spServer )
1091 return;
1092 GSource *pIdle = g_idle_source_new();
1093 g_source_set_callback( pIdle, ensureDiscoverable_cb, nullptr, nullptr );
1094 g_source_set_priority( pIdle, G_PRIORITY_DEFAULT );
1095 g_source_attach( pIdle, spServer->mpImpl->mpContext );
1096 g_source_unref( pIdle );
1097#endif
1098}
1099
1101{
1102#ifdef LINUX_BLUETOOTH
1103 // Push it all across into our mainloop
1104 if( !spServer )
1105 return;
1106 GSource *pIdle = g_idle_source_new();
1107 g_source_set_callback( pIdle, restoreDiscoverable_cb, nullptr, nullptr );
1108 g_source_set_priority( pIdle, G_PRIORITY_DEFAULT_IDLE );
1109 g_source_attach( pIdle, spServer->mpImpl->mpContext );
1110 g_source_unref( pIdle );
1111#endif
1112}
1113
1115{
1116#ifdef LINUX_BLUETOOTH
1117 if (!spServer->mpImpl->mpConnection ||
1119 return;
1120
1121 // Find out if we are discoverable already ...
1122 std::unique_ptr<DBusObject> pAdapter = spServer->mpImpl->getAdapter();
1123 if( !pAdapter )
1124 return;
1125
1126 bool bDiscoverable = getDiscoverable(spServer->mpImpl->mpConnection, pAdapter.get() );
1127
1129 if( !bDiscoverable )
1130 setDiscoverable( spServer->mpImpl->mpConnection, pAdapter.get(), true );
1131#endif
1132}
1133
1135{
1137 {
1138#ifdef LINUX_BLUETOOTH
1139 std::unique_ptr<DBusObject> pAdapter = spServer->mpImpl->getAdapter();
1140 if( !pAdapter )
1141 return;
1142 setDiscoverable( spServer->mpImpl->mpConnection, pAdapter.get(), false );
1143#endif
1144 }
1146}
1147
1148// We have to have all our clients shut otherwise we can't
1149// re-bind to the same port number it appears.
1151{
1152 for (auto& rpCommunicator : *mpCommunicators)
1153 rpCommunicator->forceClose();
1154 // the hope is that all the threads then terminate cleanly and
1155 // clean themselves up.
1156}
1157
1159{
1160 SAL_INFO( "sdremote.bluetooth", "BluetoothServer::run called" );
1161 osl::Thread::setName("BluetoothServer");
1162#ifdef LINUX_BLUETOOTH
1163 DBusConnection *pConnection = dbusConnectToNameOnBus();
1164 if( !pConnection )
1165 return;
1166
1167 // For either implementation we need to poll the dbus fd
1168 int fd = -1;
1169 GPollFD aDBusFD;
1170 if( dbus_connection_get_unix_fd( pConnection, &fd ) && fd >= 0 )
1171 {
1172 aDBusFD.fd = fd;
1173 aDBusFD.events = G_IO_IN | G_IO_PRI;
1174 g_main_context_add_poll( mpImpl->mpContext, &aDBusFD, G_PRIORITY_DEFAULT );
1175 }
1176 else
1177 SAL_WARN( "sdremote.bluetooth", "failed to poll for incoming dbus signals" );
1178
1179 if (isBluez5Available(pConnection))
1180 {
1181 SAL_INFO("sdremote.bluetooth", "Using Bluez 5");
1182 registerBluez5Profile(pConnection, mpCommunicators);
1183 mpImpl->mpConnection = pConnection;
1184 mpImpl->maBluezVersion = Impl::BluezVersion::BLUEZ5;
1185
1186 // We don't need to listen to adapter changes anymore -- profile
1187 // registration is done globally for the entirety of bluez, so we only
1188 // need adapters when setting discoverability, which can be done
1189 // dynamically without the need to listen for changes.
1190
1191 // TODO: exit on SD deinit
1192 // Probably best to do that in SdModule::~SdModule?
1193 while (true)
1194 {
1195 aDBusFD.revents = 0;
1196 g_main_context_iteration( mpImpl->mpContext, true );
1197 if( aDBusFD.revents )
1198 {
1199 dbus_connection_read_write( pConnection, 0 );
1200 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status( pConnection ))
1201 dbus_connection_dispatch( pConnection );
1202 }
1203 if ((false)) break;
1204 // silence Clang -Wunreachable-code after loop (TODO: proper
1205 // fix?)
1206 }
1207 unregisterBluez5Profile( pConnection );
1208 g_main_context_unref( mpImpl->mpContext );
1209 mpImpl->mpConnection = nullptr;
1210 mpImpl->mpContext = nullptr;
1211 return;
1212 }
1213
1214 // Otherwise we could be on Bluez 4 and continue as usual.
1215 mpImpl->maBluezVersion = Impl::BluezVersion::BLUEZ4;
1216
1217 // Try to setup the default adapter, otherwise wait for add/remove signal
1218 mpImpl->mpService = registerWithDefaultAdapter( pConnection );
1219 // listen for connection state and power changes - we need to close
1220 // and re-create our socket code on suspend / resume, enable/disable
1221 DBusError aError;
1222 dbus_error_init( &aError );
1223 dbus_bus_add_match( pConnection, "type='signal',interface='org.bluez.Manager'", &aError );
1224 dbus_connection_flush( pConnection );
1225
1226 // Try to setup the default adapter, otherwise wait for add/remove signal
1227 mpImpl->mpService = registerWithDefaultAdapter( pConnection );
1228
1229 // poll on our bluetooth socket - if we can.
1230 GPollFD aSocketFD;
1231 if( mpImpl->mpService )
1232 bluezCreateAttachListeningSocket( mpImpl->mpContext, &aSocketFD );
1233
1234 mpImpl->mpConnection = pConnection;
1235
1236 while( true )
1237 {
1238 aDBusFD.revents = 0;
1239 aSocketFD.revents = 0;
1240 g_main_context_iteration( mpImpl->mpContext, true );
1241
1242 SAL_INFO( "sdremote.bluetooth", "main-loop spin "
1243 << aDBusFD.revents << " " << aSocketFD.revents );
1244 if( aDBusFD.revents )
1245 {
1246 dbus_connection_read_write( pConnection, 0 );
1247 DBusMessage *pMsg = dbus_connection_pop_message( pConnection );
1248 if( pMsg )
1249 {
1250 if( dbus_message_is_signal( pMsg, "org.bluez.Manager", "AdapterRemoved" ) )
1251 {
1252 SAL_WARN( "sdremote.bluetooth", "lost adapter - cleaning up sockets" );
1253 bluezDetachCloseSocket( mpImpl->mpContext, &aSocketFD );
1255 }
1256 else if( dbus_message_is_signal( pMsg, "org.bluez.Manager", "AdapterAdded" ) ||
1257 dbus_message_is_signal( pMsg, "org.bluez.Manager", "DefaultAdapterChanged" ) )
1258 {
1259 SAL_WARN( "sdremote.bluetooth", "gained adapter - re-generating sockets" );
1260 bluezDetachCloseSocket( mpImpl->mpContext, &aSocketFD );
1262 mpImpl->mpService = registerWithDefaultAdapter( pConnection );
1263 if( mpImpl->mpService )
1264 bluezCreateAttachListeningSocket( mpImpl->mpContext, &aSocketFD );
1265 }
1266 else
1267 SAL_INFO( "sdremote.bluetooth", "unknown incoming dbus message, "
1268 " type: " << dbus_message_get_type( pMsg )
1269 << " path: '" << dbus_message_get_path( pMsg )
1270 << "' interface: '" << dbus_message_get_interface( pMsg )
1271 << "' member: '" << dbus_message_get_member( pMsg ) );
1272 }
1273 dbus_message_unref( pMsg );
1274 }
1275
1276 if( aSocketFD.revents )
1277 {
1278 sockaddr_rc aRemoteAddr;
1279 socklen_t aRemoteAddrLen = sizeof(aRemoteAddr);
1280
1281 SAL_INFO( "sdremote.bluetooth", "performing accept" );
1282 int nClient = accept( aSocketFD.fd, reinterpret_cast<sockaddr*>(&aRemoteAddr), &aRemoteAddrLen);
1283 if ( nClient < 0 && errno != EAGAIN )
1284 {
1285 SAL_WARN( "sdremote.bluetooth", "accept failed with errno " << errno );
1286 } else {
1287 SAL_INFO( "sdremote.bluetooth", "connection accepted " << nClient );
1288 Communicator* pCommunicator = new Communicator( std::make_unique<BufferedStreamSocket>( nClient ) );
1289 mpCommunicators->push_back( pCommunicator );
1290 pCommunicator->launch();
1291 }
1292 }
1293 if ((false)) break;
1294 // silence Clang -Wunreachable-code after loop (TODO: proper fix?)
1295 }
1296
1297 unregisterBluez5Profile( pConnection );
1298 g_main_context_unref( mpImpl->mpContext );
1299 mpImpl->mpConnection = nullptr;
1300 mpImpl->mpContext = nullptr;
1301
1302#elif defined(_WIN32)
1303 WORD wVersionRequested;
1304 WSADATA wsaData;
1305
1306 wVersionRequested = MAKEWORD(2, 2);
1307
1308 if ( WSAStartup(wVersionRequested, &wsaData) )
1309 {
1310 return; // winsock dll couldn't be loaded
1311 }
1312
1313 int aSocket = socket( AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM );
1314 if ( !aSocket )
1315 {
1316 WSACleanup();
1317 return;
1318 }
1319 SOCKADDR_BTH aAddr;
1320 aAddr.addressFamily = AF_BTH;
1321 aAddr.btAddr = 0;
1322 aAddr.serviceClassId = GUID_NULL;
1323 aAddr.port = BT_PORT_ANY; // Select any free socket.
1324 if ( bind( aSocket, reinterpret_cast<SOCKADDR*>(&aAddr), sizeof(aAddr) ) == SOCKET_ERROR )
1325 {
1326 closesocket( aSocket );
1327 WSACleanup();
1328 return;
1329 }
1330
1331 SOCKADDR_BTH aName;
1332 int aNameSize = sizeof(aName);
1333 getsockname( aSocket, reinterpret_cast<SOCKADDR*>(&aName), &aNameSize ); // Retrieve the local address and port
1334
1335 CSADDR_INFO aAddrInfo = {};
1336 aAddrInfo.LocalAddr.lpSockaddr = reinterpret_cast<SOCKADDR*>(&aName);
1337 aAddrInfo.LocalAddr.iSockaddrLength = sizeof( SOCKADDR_BTH );
1338 aAddrInfo.iSocketType = SOCK_STREAM;
1339 aAddrInfo.iProtocol = BTHPROTO_RFCOMM;
1340
1341 // To be used for setting a custom UUID once available.
1342// GUID uuid;
1343// uuid.Data1 = 0x00001101;
1344// memset( &uuid, 0x1000 + UUID*2^96, sizeof( GUID ) );
1345// uuid.Data2 = 0;
1346// uuid.Data3 = 0x1000;
1347// ULONGLONG aData4 = 0x800000805F9B34FB;
1348// memcpy( uuid.Data4, &aData4, sizeof(uuid.Data4) );
1349
1350 WSAQUERYSETW aRecord = {};
1351 aRecord.dwSize = sizeof(aRecord);
1352 aRecord.lpszServiceInstanceName = const_cast<wchar_t *>(
1353 L"LibreOffice Impress Remote Control");
1354 aRecord.lpszComment = const_cast<wchar_t *>(
1355 L"Remote control of presentations over bluetooth.");
1356 aRecord.lpServiceClassId = const_cast<LPGUID>(&SerialPortServiceClass_UUID);
1357 aRecord.dwNameSpace = NS_BTH;
1358 aRecord.dwNumberOfCsAddrs = 1;
1359 aRecord.lpcsaBuffer = &aAddrInfo;
1360 if (WSASetServiceW( &aRecord, RNRSERVICE_REGISTER, 0 ) == SOCKET_ERROR)
1361 {
1362 closesocket( aSocket );
1363 WSACleanup();
1364 return;
1365 }
1366
1367 if ( listen( aSocket, 1 ) == SOCKET_ERROR )
1368 {
1369 closesocket( aSocket );
1370 WSACleanup();
1371 return;
1372 }
1373
1374 SOCKADDR_BTH aRemoteAddr;
1375 int aRemoteAddrLen = sizeof(aRemoteAddr);
1376 while ( true )
1377 {
1378 SOCKET socket;
1379 if ( (socket = accept(aSocket, reinterpret_cast<sockaddr*>(&aRemoteAddr), &aRemoteAddrLen)) == INVALID_SOCKET )
1380 {
1381 closesocket( aSocket );
1382 WSACleanup();
1383 return;
1384 } else {
1385 Communicator* pCommunicator = new Communicator( std::make_unique<BufferedStreamSocket>( socket) );
1386 mpCommunicators->push_back( pCommunicator );
1387 pCommunicator->launch();
1388 }
1389 }
1390
1391#elif defined(MACOSX)
1392 // Build up dictionary at run-time instead of bothering with a
1393 // .plist file, using the Objective-C API
1394
1395 // Compare to BluetoothServiceRecord.hxx
1396
1397 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
1398
1399 NSDictionary *dict =
1400 [NSDictionary dictionaryWithObjectsAndKeys:
1401
1402 // Service class ID list
1403 [NSArray arrayWithObject:
1404 [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16ServiceClassSerialPort]],
1405 @"0001 - ServiceClassIDList",
1406
1407 // Protocol descriptor list
1408 [NSArray arrayWithObjects:
1409 [NSArray arrayWithObject: [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16L2CAP]],
1410 [NSArray arrayWithObjects:
1411 [IOBluetoothSDPUUID uuid16: kBluetoothL2CAPPSMRFCOMM],
1412 [NSDictionary dictionaryWithObjectsAndKeys:
1413 [NSNumber numberWithInt: 1],
1414 @"DataElementSize",
1415 [NSNumber numberWithInt: 1],
1416 @"DataElementType",
1417 [NSNumber numberWithInt: 5], // RFCOMM port number, will be replaced if necessary automatically
1418 @"DataElementValue",
1419 nil],
1420 nil],
1421 nil],
1422 @"0004 - Protocol descriptor list",
1423
1424 // Browse group list
1425 [NSArray arrayWithObject:
1426 [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16ServiceClassPublicBrowseGroup]],
1427 @"0005 - BrowseGroupList",
1428
1429 // Language base attribute ID list
1430 [NSArray arrayWithObjects:
1431 [NSData dataWithBytes: "en" length: 2],
1432 [NSDictionary dictionaryWithObjectsAndKeys:
1433 [NSNumber numberWithInt: 2],
1434 @"DataElementSize",
1435 [NSNumber numberWithInt: 1],
1436 @"DataElementType",
1437 [NSNumber numberWithInt: 0x006a], // encoding
1438 @"DataElementValue",
1439 nil],
1440 [NSDictionary dictionaryWithObjectsAndKeys:
1441 [NSNumber numberWithInt: 2],
1442 @"DataElementSize",
1443 [NSNumber numberWithInt: 1],
1444 @"DataElementType",
1445 [NSNumber numberWithInt: 0x0100], // offset
1446 @"DataElementValue",
1447 nil],
1448 nil],
1449 @"0006 - LanguageBaseAttributeIDList",
1450
1451 // Bluetooth profile descriptor list
1452 [NSArray arrayWithObject:
1453 [NSArray arrayWithObjects:
1454 [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16ServiceClassSerialPort],
1455 [NSDictionary dictionaryWithObjectsAndKeys:
1456 [NSNumber numberWithInt: 2],
1457 @"DataElementSize",
1458 [NSNumber numberWithInt: 1],
1459 @"DataElementType",
1460 [NSNumber numberWithInt: 0x0100], // version number ?
1461 @"DataElementValue",
1462 nil],
1463 nil]],
1464 @"0009 - BluetoothProfileDescriptorList",
1465
1466 // Attributes pointed to by the LanguageBaseAttributeIDList
1467 @"LibreOffice Impress Remote Control",
1468 @"0100 - ServiceName",
1469 @"The Document Foundation",
1470 @"0102 - ProviderName",
1471 nil];
1472
1473 // Create service
1474 IOBluetoothSDPServiceRecordRef serviceRecordRef;
1475 SAL_WNODEPRECATED_DECLARATIONS_PUSH //TODO: 10.9 IOBluetoothAddServiceDict
1476 IOReturn rc = IOBluetoothAddServiceDict(reinterpret_cast<CFDictionaryRef>(dict), &serviceRecordRef);
1478
1479 SAL_INFO("sdremote.bluetooth", "IOBluetoothAddServiceDict returned " << rc);
1480
1481 if (rc == kIOReturnSuccess)
1482 {
1483 IOBluetoothSDPServiceRecord *serviceRecord =
1484 [IOBluetoothSDPServiceRecord withSDPServiceRecordRef: serviceRecordRef];
1485
1486 BluetoothRFCOMMChannelID channelID;
1487 [serviceRecord getRFCOMMChannelID: &channelID];
1488
1489 BluetoothSDPServiceRecordHandle serviceRecordHandle;
1490 [serviceRecord getServiceRecordHandle: &serviceRecordHandle];
1491
1492 // Register callback for incoming connections
1493 IOBluetoothRegisterForFilteredRFCOMMChannelOpenNotifications(
1494 incomingCallback,
1495 this,
1496 channelID,
1497 kIOBluetoothUserNotificationChannelDirectionIncoming);
1498
1499 [serviceRecord release];
1500 }
1501
1502 [pool release];
1503
1504 (void) mpCommunicators;
1505#else
1506 (void) mpCommunicators; // avoid warnings about unused member
1507#endif
1508}
1509
1511
1512void BluetoothServer::setup( std::vector<Communicator*>* pCommunicators )
1513{
1514 if (spServer)
1515 return;
1516
1517 spServer = new BluetoothServer( pCommunicators );
1518 spServer->create();
1519}
1520
1521/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const char *const bluetooth_service_record
const char * pName
Any maPath
FILE * init(int, char **)
enum sd::BluetoothServer::@0 meWasDiscoverable
static void restoreDiscoverable()
restore the state of discoverability from before ensureDiscoverable
static void setup(std::vector< Communicator * > *pCommunicators)
static void ensureDiscoverable()
ensure that Bluetooth discoverability is on
std::vector< Communicator * > * mpCommunicators
static void doEnsureDiscoverable()
virtual void SAL_CALL run() override
static BluetoothServer * spServer
BluetoothServer(std::vector< Communicator * > *pCommunicators)
virtual ~BluetoothServer() override
static void doRestoreDiscoverable()
Class used for communication with one single client, dealing with all tasks specific to this client.
std::vector< char > mBuffer
IOBluetoothRFCOMMChannel * mpChannel
void appendData(void *pBuffer, size_t len)
virtual sal_Int32 write(const void *pBuffer, sal_uInt32 len) override
Write a number of bytes.
bool close
EmbeddedObjectRef * pObject
UNKNOWN
#define FALSE
OUString aName
void * p
sal_Int64 n
uno_Any a
unsigned short WORD
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
OSQLColumns::const_iterator find(const OSQLColumns::const_iterator &first, const OSQLColumns::const_iterator &last, std::u16string_view _rVal, const ::comphelper::UStringMixEqual &_rCase)
ParserContextSharedPtr mpContext
#define SAL_WNODEPRECATED_DECLARATIONS_POP
#define SAL_WNODEPRECATED_DECLARATIONS_PUSH