1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include "BluetoothServer.hxx"
15 #include <string_view>
17 #include <sal/log.hxx>
19 #ifdef LINUX_BLUETOOTH
21 #include <dbus/dbus.h>
25 #include <sys/socket.h>
26 #include <bluetooth/bluetooth.h>
27 #include <bluetooth/rfcomm.h>
28 #include "BluetoothServiceRecord.hxx"
29 #include "BufferedStreamSocket.hxx"
33 // LO vs WinAPI conflict
38 #include "BufferedStreamSocket.hxx"
43 #include <osl/conditn.hxx>
45 #import <CoreFoundation/CoreFoundation.h>
46 #import <IOBluetooth/IOBluetoothUtilities.h>
47 #import <IOBluetooth/objc/IOBluetoothSDPUUID.h>
48 #import <IOBluetooth/objc/IOBluetoothSDPServiceRecord.h>
50 #import "OSXBluetooth.h"
51 #include "OSXBluetoothWrapper.hxx"
54 #include "Communicator.hxx"
55 #include <RemoteServer.hxx>
57 #include <osl/mutex.hxx>
61 #ifdef LINUX_BLUETOOTH
71 DBusObject( const char *pBusName
, const char *pPath
, const char *pInterface
)
72 : maBusName( pBusName
), maPath( pPath
), maInterface( pInterface
) { }
74 DBusMessage
*getMethodCall( const char *pName
)
76 return dbus_message_new_method_call( maBusName
.getStr(), maPath
.getStr(),
77 maInterface
.getStr(), pName
);
79 std::unique_ptr
<DBusObject
> cloneForInterface( const char *pInterface
)
81 std::unique_ptr
<DBusObject
> pObject(new DBusObject());
83 pObject
->maBusName
= maBusName
;
84 pObject
->maPath
= maPath
;
85 pObject
->maInterface
= pInterface
;
93 static std::unique_ptr
<DBusObject
> getBluez5Adapter(DBusConnection
*pConnection
);
95 struct sd::BluetoothServer::Impl
{
96 // the glib mainloop running in the thread
97 GMainContext
*mpContext
;
98 DBusConnection
*mpConnection
;
99 std::unique_ptr
<DBusObject
> mpService
;
100 enum class BluezVersion
{ BLUEZ4
, BLUEZ5
, UNKNOWN
};
101 BluezVersion maBluezVersion
;
104 : mpContext( g_main_context_new() )
105 , mpConnection( nullptr )
106 , maBluezVersion( BluezVersion::UNKNOWN
)
109 std::unique_ptr
<DBusObject
> getAdapter()
113 return mpService
->cloneForInterface( "org.bluez.Adapter" );
115 else if (spServer
->mpImpl
->maBluezVersion
== BluezVersion::BLUEZ5
)
117 return getBluez5Adapter(mpConnection
);
126 static DBusConnection
*
127 dbusConnectToNameOnBus()
130 DBusConnection
*pConnection
;
132 dbus_error_init( &aError
);
134 pConnection
= dbus_bus_get( DBUS_BUS_SYSTEM
, &aError
);
135 if( !pConnection
|| dbus_error_is_set( &aError
))
137 SAL_WARN( "sdremote.bluetooth", "failed to get dbus system bus: " << aError
.message
);
138 dbus_error_free( &aError
);
146 sendUnrefAndWaitForReply( DBusConnection
*pConnection
, DBusMessage
*pMsg
)
148 DBusPendingCall
*pPending
= nullptr;
150 if( !pMsg
|| !dbus_connection_send_with_reply( pConnection
, pMsg
, &pPending
,
151 -1 /* default timeout */ ) )
153 SAL_WARN( "sdremote.bluetooth", "Memory allocation failed on message send" );
154 dbus_message_unref( pMsg
);
157 dbus_connection_flush( pConnection
);
158 dbus_message_unref( pMsg
);
160 dbus_pending_call_block( pPending
); // block for reply
162 pMsg
= dbus_pending_call_steal_reply( pPending
);
164 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
166 dbus_pending_call_unref( pPending
);
171 isBluez5Available(DBusConnection
*pConnection
)
175 // Simplest ways to check whether we have Bluez 5+ is to check
176 // that we can obtain adapters using the new interfaces.
177 // The first two error checks however don't tell us anything as they should
178 // succeed as long as dbus is working correctly.
179 pMsg
= DBusObject( "org.bluez", "/", "org.freedesktop.DBus.ObjectManager" ).getMethodCall( "GetManagedObjects" );
182 SAL_INFO("sdremote.bluetooth", "No GetManagedObjects call created");
186 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
189 SAL_INFO("sdremote.bluetooth", "No reply received");
193 // If dbus is working correctly and we aren't on bluez 5 this is where we
194 // should actually get the error.
195 if (dbus_message_get_error_name( pMsg
))
197 SAL_INFO( "sdremote.bluetooth", "GetManagedObjects call failed with \""
198 << dbus_message_get_error_name( pMsg
)
199 << "\" -- we don't seem to have Bluez 5 available");
202 SAL_INFO("sdremote.bluetooth", "GetManagedObjects call seems to have succeeded -- we must be on Bluez 5");
203 dbus_message_unref(pMsg
);
207 static std::unique_ptr
<DBusObject
>
208 getBluez5Adapter(DBusConnection
*pConnection
)
211 // This returns a list of objects where we need to find the first
212 // org.bluez.Adapter1 .
213 pMsg
= DBusObject( "org.bluez", "/", "org.freedesktop.DBus.ObjectManager" ).getMethodCall( "GetManagedObjects" );
217 const gchar
* const pInterfaceType
= "org.bluez.Adapter1";
219 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
221 DBusMessageIter aObjectIterator
;
222 if (pMsg
&& dbus_message_iter_init(pMsg
, &aObjectIterator
))
224 if (DBUS_TYPE_ARRAY
== dbus_message_iter_get_arg_type(&aObjectIterator
))
226 DBusMessageIter aObject
;
227 dbus_message_iter_recurse(&aObjectIterator
, &aObject
);
230 if (DBUS_TYPE_DICT_ENTRY
== dbus_message_iter_get_arg_type(&aObject
))
232 DBusMessageIter aContainerIter
;
233 dbus_message_iter_recurse(&aObject
, &aContainerIter
);
234 char *pPath
= nullptr;
237 if (DBUS_TYPE_OBJECT_PATH
== dbus_message_iter_get_arg_type(&aContainerIter
))
239 dbus_message_iter_get_basic(&aContainerIter
, &pPath
);
240 SAL_INFO( "sdremote.bluetooth", "Something retrieved: '"
243 else if (DBUS_TYPE_ARRAY
== dbus_message_iter_get_arg_type(&aContainerIter
))
245 DBusMessageIter aInnerIter
;
246 dbus_message_iter_recurse(&aContainerIter
, &aInnerIter
);
249 if (DBUS_TYPE_DICT_ENTRY
== dbus_message_iter_get_arg_type(&aInnerIter
))
251 DBusMessageIter aInnerInnerIter
;
252 dbus_message_iter_recurse(&aInnerIter
, &aInnerInnerIter
);
255 if (DBUS_TYPE_STRING
== dbus_message_iter_get_arg_type(&aInnerInnerIter
))
259 dbus_message_iter_get_basic(&aInnerInnerIter
, &pMessage
);
260 if (pMessage
== std::string_view("org.bluez.Adapter1"))
262 dbus_message_unref(pMsg
);
265 return std::make_unique
<DBusObject
>( "org.bluez", pPath
, pInterfaceType
);
267 assert(false); // We should already have pPath provided for us.
271 while (dbus_message_iter_next(&aInnerInnerIter
));
274 while (dbus_message_iter_next(&aInnerIter
));
277 while (dbus_message_iter_next(&aContainerIter
));
280 while (dbus_message_iter_next(&aObject
));
282 dbus_message_unref(pMsg
);
289 bluez4GetDefaultService( DBusConnection
*pConnection
)
293 const gchar
* const pInterfaceType
= "org.bluez.Service";
295 // org.bluez.manager only exists for bluez 4.
296 // getMethodCall should return NULL if there is any issue e.g. the
297 // if org.bluez.manager doesn't exist.
298 pMsg
= DBusObject( "org.bluez", "/", "org.bluez.Manager" ).getMethodCall( "DefaultAdapter" );
302 SAL_WARN("sdremote.bluetooth", "Couldn't retrieve DBusObject for DefaultAdapter");
306 SAL_INFO("sdremote.bluetooth", "successfully retrieved org.bluez.Manager.DefaultAdapter, attempting to use.");
307 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
309 if(!pMsg
|| !dbus_message_iter_init( pMsg
, &it
) )
314 // This works for Bluez 4
315 if( DBUS_TYPE_OBJECT_PATH
== dbus_message_iter_get_arg_type( &it
) )
317 const char *pObjectPath
= nullptr;
318 dbus_message_iter_get_basic( &it
, &pObjectPath
);
319 SAL_INFO( "sdremote.bluetooth", "DefaultAdapter retrieved: '"
320 << pObjectPath
<< "' '" << pInterfaceType
<< "'" );
321 dbus_message_unref( pMsg
);
322 return new DBusObject( "org.bluez", pObjectPath
, pInterfaceType
);
324 // Some form of error, e.g. if we have bluez 5 we get a message that
325 // this method doesn't exist.
326 else if ( DBUS_TYPE_STRING
== dbus_message_iter_get_arg_type( &it
) )
328 const char *pMessage
= nullptr;
329 dbus_message_iter_get_basic( &it
, &pMessage
);
330 SAL_INFO( "sdremote.bluetooth", "Error message: '"
331 << pMessage
<< "' '" << pInterfaceType
<< "'" );
335 SAL_INFO( "sdremote.bluetooth", "invalid type of reply to DefaultAdapter: '"
336 << static_cast<char>(dbus_message_iter_get_arg_type( &it
)) << "'" );
338 dbus_message_unref(pMsg
);
343 bluez4RegisterServiceRecord( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
344 const char *pServiceRecord
)
349 pMsg
= pAdapter
->getMethodCall( "AddRecord" );
350 dbus_message_iter_init_append( pMsg
, &it
);
351 dbus_message_iter_append_basic( &it
, DBUS_TYPE_STRING
, &pServiceRecord
);
353 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
355 if( !pMsg
|| !dbus_message_iter_init( pMsg
, &it
) ||
356 dbus_message_iter_get_arg_type( &it
) != DBUS_TYPE_UINT32
)
358 SAL_WARN( "sdremote.bluetooth", "SDP registration failed" );
362 // We ignore the uint de-registration handle we get back:
363 // bluez will clean us up automatically on exit
369 bluezCreateAttachListeningSocket( GMainContext
*pContext
, GPollFD
*pSocketFD
)
375 if( ( nSocket
= socket( AF_BLUETOOTH
, SOCK_STREAM
, BTPROTO_RFCOMM
) ) < 0 )
377 SAL_WARN( "sdremote.bluetooth", "failed to open bluetooth socket with error " << nSocket
);
382 // Initialize whole structure. Mainly to appease valgrind, which
383 // doesn't know about the padding at the end of sockaddr_rc which
384 // it will dutifully check for definedness. But also the standard
385 // definition of BDADDR_ANY is unusable in C++ code, so just use
386 // memset to set aAddr.rc_bdaddr to 0.
387 memset( &aAddr
, 0, sizeof( aAddr
) );
388 aAddr
.rc_family
= AF_BLUETOOTH
;
389 aAddr
.rc_channel
= 5;
392 if ( ( a
= bind( nSocket
, reinterpret_cast<sockaddr
*>(&aAddr
), sizeof(aAddr
) ) ) < 0 ) {
393 SAL_WARN( "sdremote.bluetooth", "bind failed with error" << a
);
398 if ( ( a
= listen( nSocket
, 1 ) ) < 0 )
400 SAL_WARN( "sdremote.bluetooth", "listen failed with error" << a
);
405 // set non-blocking behaviour ...
406 if( fcntl( nSocket
, F_SETFL
, O_NONBLOCK
) < 0 )
412 pSocketFD
->fd
= nSocket
;
413 pSocketFD
->events
= G_IO_IN
| G_IO_PRI
;
414 pSocketFD
->revents
= 0;
416 g_main_context_add_poll( pContext
, pSocketFD
, G_PRIORITY_DEFAULT
);
420 bluezDetachCloseSocket( GMainContext
*pContext
, GPollFD
*pSocketFD
)
422 if( pSocketFD
->fd
>= 0 )
424 close( pSocketFD
->fd
);
425 g_main_context_remove_poll( pContext
, pSocketFD
);
430 #endif // LINUX_BLUETOOTH
434 OSXBluetoothWrapper::OSXBluetoothWrapper( IOBluetoothRFCOMMChannel
* channel
) :
441 // silly enough, can't write more than mnMTU bytes at once
442 mnMTU
= [channel getMTU
];
444 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::OSXBluetoothWrapper(): mnMTU=" << mnMTU
);
447 sal_Int32
OSXBluetoothWrapper::readLine( OString
& aLine
)
449 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine()" );
454 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: entering mutex" );
455 ::osl::MutexGuard
aQueueGuard( mMutex
);
456 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: entered mutex" );
459 // We should have in the sal logging some standard way to
460 // output char buffers with non-printables escaped.
461 std::ostringstream s
;
462 if (mBuffer
.size() > 0)
464 for (unsigned char *p
= reinterpret_cast<unsigned char *>(mBuffer
.data()); p
!= reinterpret_cast<unsigned char *>(mBuffer
.data()) + mBuffer
.size(); p
++)
468 else if (*p
< ' ' || *p
>= 0x7F)
469 s
<< "\\0x" << std::hex
<< std::setw(2) << std::setfill('0') << static_cast<int>(*p
) << std::setfill(' ') << std::setw(1) << std::dec
;
474 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine mBuffer: \"" << s
.str() << "\"" );
477 // got enough bytes to return a line?
478 std::vector
<char>::iterator aIt
;
479 if ( (aIt
= find( mBuffer
.begin(), mBuffer
.end(), '\n' ))
482 sal_uInt64 aLocation
= aIt
- mBuffer
.begin();
484 aLine
= OString( &(*mBuffer
.begin()), aLocation
);
486 mBuffer
.erase( mBuffer
.begin(), aIt
+ 1 ); // Also delete the empty line
489 SAL_INFO( "sdremote.bluetooth", " returning, got \"" << OStringToOUString( aLine
, RTL_TEXTENCODING_UTF8
) << "\"" );
490 return aLine
.getLength() + 1;
493 // nope - wait some more (after releasing the mutex)
494 SAL_INFO( "sdremote.bluetooth", " resetting mHaveBytes" );
496 SAL_INFO( "sdremote.bluetooth", " leaving mutex" );
499 SAL_INFO( "sdremote.bluetooth", " waiting for mHaveBytes" );
501 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: got mHaveBytes" );
505 sal_Int32
OSXBluetoothWrapper::write( const void* pBuffer
, sal_uInt32 n
)
507 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::write(" << pBuffer
<< ", " << n
<< ") mpChannel=" << mpChannel
);
509 char const * ptr
= static_cast<char const *>(pBuffer
);
510 sal_uInt32 nBytesWritten
= 0;
512 if (mpChannel
== nil
)
515 while( nBytesWritten
< n
)
517 int toWrite
= n
- nBytesWritten
;
518 toWrite
= toWrite
<= mnMTU
? toWrite
: mnMTU
;
519 if ( [mpChannel writeSync
:const_cast<char *>(ptr
) length
:toWrite
] != kIOReturnSuccess
)
521 SAL_INFO( "sdremote.bluetooth", " [mpChannel writeSync:" << static_cast<void const *>(ptr
) << " length:" << toWrite
<< "] returned error, total written " << nBytesWritten
);
522 return nBytesWritten
;
525 nBytesWritten
+= toWrite
;
527 SAL_INFO( "sdremote.bluetooth", " total written " << nBytesWritten
);
528 return nBytesWritten
;
531 void OSXBluetoothWrapper::appendData(void* pBuffer
, size_t len
)
533 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData(" << pBuffer
<< ", " << len
<< ")" );
537 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData: entering mutex" );
538 ::osl::MutexGuard
aQueueGuard( mMutex
);
539 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData: entered mutex" );
540 mBuffer
.insert(mBuffer
.begin()+mBuffer
.size(),
541 static_cast<char*>(pBuffer
), static_cast<char *>(pBuffer
)+len
);
542 SAL_INFO( "sdremote.bluetooth", " setting mHaveBytes" );
544 SAL_INFO( "sdremote.bluetooth", " leaving mutex" );
548 void OSXBluetoothWrapper::channelClosed()
550 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::channelClosed()" );
555 void incomingCallback( void *userRefCon
,
556 IOBluetoothUserNotificationRef
,
557 IOBluetoothObjectRef objectRef
)
559 SAL_INFO( "sdremote.bluetooth", "incomingCallback()" );
561 BluetoothServer
* pServer
= static_cast<BluetoothServer
*>(userRefCon
);
563 IOBluetoothRFCOMMChannel
* channel
= [IOBluetoothRFCOMMChannel withRFCOMMChannelRef
:reinterpret_cast<IOBluetoothRFCOMMChannelRef
>(objectRef
)];
565 OSXBluetoothWrapper
* socket
= new OSXBluetoothWrapper( channel
);
566 Communicator
* pCommunicator
= new Communicator( std::unique_ptr
<IBluetoothSocket
>(socket
) );
567 pServer
->addCommunicator( pCommunicator
);
569 ChannelDelegate
* delegate
= [[ChannelDelegate alloc
] initWithCommunicatorAndSocket
: pCommunicator socket
: socket
];
570 [channel setDelegate
: delegate
];
573 pCommunicator
->launch();
576 void BluetoothServer::addCommunicator( Communicator
* pCommunicator
)
578 ::osl::MutexGuard
aGuard(RemoteServer::sDataMutex
);
579 mpCommunicators
->push_back( pCommunicator
);
584 #ifdef LINUX_BLUETOOTH
587 static gboolean
ensureDiscoverable_cb(gpointer
)
589 BluetoothServer::doEnsureDiscoverable();
590 return FALSE
; // remove source
592 static gboolean
restoreDiscoverable_cb(gpointer
)
594 BluetoothServer::doRestoreDiscoverable();
595 return FALSE
; // remove source
600 * Bluez 4 uses custom methods for setting properties, whereas Bluez 5+
601 * implements properties using the generic "org.freedesktop.DBus.Properties"
602 * interface -- hence we have a specific Bluez 4 function to deal with the
603 * old style of reading properties.
606 getBluez4BooleanProperty( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
607 const char *pPropertyName
, bool *pBoolean
)
615 pMsg
= sendUnrefAndWaitForReply( pConnection
,
616 pAdapter
->getMethodCall( "GetProperties" ) );
619 if( !pMsg
|| !dbus_message_iter_init( pMsg
, &it
) )
621 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
625 if( DBUS_TYPE_ARRAY
!= dbus_message_iter_get_arg_type( &it
) )
627 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
631 DBusMessageIter arrayIt
;
632 dbus_message_iter_recurse( &it
, &arrayIt
);
634 while( dbus_message_iter_get_arg_type( &arrayIt
) == DBUS_TYPE_DICT_ENTRY
)
636 DBusMessageIter dictIt
;
637 dbus_message_iter_recurse( &arrayIt
, &dictIt
);
639 const char *pName
= nullptr;
640 if( dbus_message_iter_get_arg_type( &dictIt
) == DBUS_TYPE_STRING
)
642 dbus_message_iter_get_basic( &dictIt
, &pName
);
643 if( pName
!= nullptr && !strcmp( pName
, pPropertyName
) )
645 SAL_INFO( "sdremote.bluetooth", "hit " << pPropertyName
<< " property" );
646 dbus_message_iter_next( &dictIt
);
647 dbus_bool_t bBool
= false;
649 if( dbus_message_iter_get_arg_type( &dictIt
) == DBUS_TYPE_VARIANT
)
651 DBusMessageIter variantIt
;
652 dbus_message_iter_recurse( &dictIt
, &variantIt
);
654 if( dbus_message_iter_get_arg_type( &variantIt
) == DBUS_TYPE_BOOLEAN
)
656 dbus_message_iter_get_basic( &variantIt
, &bBool
);
657 SAL_INFO( "sdremote.bluetooth", "" << pPropertyName
<< " is " << bBool
);
662 SAL_WARN( "sdremote.bluetooth", "" << pPropertyName
<< " type " <<
663 dbus_message_iter_get_arg_type( &variantIt
) );
666 SAL_WARN( "sdremote.bluetooth", "variant type ? " <<
667 dbus_message_iter_get_arg_type( &dictIt
) );
671 const char *pStr
= pName
? pName
: "<null>";
672 SAL_INFO( "sdremote.bluetooth", "property '" << pStr
<< "'" );
676 SAL_WARN( "sdremote.bluetooth", "unexpected property key type "
677 << dbus_message_iter_get_arg_type( &dictIt
) );
678 dbus_message_iter_next( &arrayIt
);
680 dbus_message_unref( pMsg
);
686 * This gets an org.freedesktop.DBus.Properties boolean
687 * (as opposed to the old Bluez 4 custom properties methods as visible above).
690 getDBusBooleanProperty( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
691 const char *pPropertyName
, bool *pBoolean
)
698 std::unique_ptr
< DBusObject
> pProperties (
699 pAdapter
->cloneForInterface( "org.freedesktop.DBus.Properties" ) );
701 DBusMessage
*pMsg
= pProperties
->getMethodCall( "Get" );
703 DBusMessageIter itIn
;
704 dbus_message_iter_init_append( pMsg
, &itIn
);
705 const char* pInterface
= "org.bluez.Adapter1";
706 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pInterface
);
707 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pPropertyName
);
708 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
711 if( !pMsg
|| !dbus_message_iter_init( pMsg
, &it
) )
713 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
717 if( DBUS_TYPE_VARIANT
!= dbus_message_iter_get_arg_type( &it
) )
719 SAL_WARN( "sdremote.bluetooth", "invalid return type" );
723 DBusMessageIter variantIt
;
724 dbus_message_iter_recurse( &it
, &variantIt
);
726 if( dbus_message_iter_get_arg_type( &variantIt
) == DBUS_TYPE_BOOLEAN
)
728 dbus_bool_t bBool
= false;
729 dbus_message_iter_get_basic( &variantIt
, &bBool
);
730 SAL_INFO( "sdremote.bluetooth", "" << pPropertyName
<< " is " << bBool
);
736 SAL_WARN( "sdremote.bluetooth", "" << pPropertyName
<< " type " <<
737 dbus_message_iter_get_arg_type( &variantIt
) );
740 const char* pError
= dbus_message_get_error_name( pMsg
);
743 SAL_WARN( "sdremote.bluetooth",
744 "Get failed for " << pPropertyName
<< " on " <<
745 pAdapter
->maPath
<< " with error: " << pError
);
748 dbus_message_unref( pMsg
);
754 setDBusBooleanProperty( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
755 const char *pPropertyName
, bool bBoolean
)
759 std::unique_ptr
< DBusObject
> pProperties(
760 pAdapter
->cloneForInterface( "org.freedesktop.DBus.Properties" ) );
762 DBusMessage
*pMsg
= pProperties
->getMethodCall( "Set" );
764 DBusMessageIter itIn
;
765 dbus_message_iter_init_append( pMsg
, &itIn
);
766 const char* pInterface
= "org.bluez.Adapter1";
767 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pInterface
);
768 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pPropertyName
);
771 DBusMessageIter varIt
;
772 dbus_message_iter_open_container( &itIn
, DBUS_TYPE_VARIANT
,
773 DBUS_TYPE_BOOLEAN_AS_STRING
, &varIt
);
774 dbus_bool_t bDBusBoolean
= bBoolean
;
775 dbus_message_iter_append_basic( &varIt
, DBUS_TYPE_BOOLEAN
, &bDBusBoolean
);
776 dbus_message_iter_close_container( &itIn
, &varIt
);
779 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
783 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
787 const char* pError
= dbus_message_get_error_name( pMsg
);
790 SAL_WARN( "sdremote.bluetooth",
791 "Set failed for " << pPropertyName
<< " on " <<
792 pAdapter
->maPath
<< " with error: " << pError
);
794 dbus_message_unref( pMsg
);
799 getDiscoverable( DBusConnection
*pConnection
, DBusObject
*pAdapter
)
801 if (pAdapter
->maInterface
== "org.bluez.Adapter") // Bluez 4
804 if( getBluez4BooleanProperty(pConnection
, pAdapter
, "Discoverable", &bDiscoverable
) )
805 return bDiscoverable
;
807 else if (pAdapter
->maInterface
== "org.bluez.Adapter1") // Bluez 5
810 if ( getDBusBooleanProperty(pConnection
, pAdapter
, "Discoverable", &bDiscoverable
) )
811 return bDiscoverable
;
817 setDiscoverable( DBusConnection
*pConnection
, DBusObject
*pAdapter
, bool bDiscoverable
)
819 SAL_INFO( "sdremote.bluetooth", "setDiscoverable to " << bDiscoverable
);
821 if (pAdapter
->maInterface
== "org.bluez.Adapter") // Bluez 4
823 bool bPowered
= false;
824 if( !getBluez4BooleanProperty( pConnection
, pAdapter
, "Powered", &bPowered
) || !bPowered
)
825 return; // nothing to do
828 DBusMessageIter it
, varIt
;
830 // set timeout to zero
831 pMsg
= pAdapter
->getMethodCall( "SetProperty" );
832 dbus_message_iter_init_append( pMsg
, &it
);
833 const char *pTimeoutStr
= "DiscoverableTimeout";
834 dbus_message_iter_append_basic( &it
, DBUS_TYPE_STRING
, &pTimeoutStr
);
835 dbus_message_iter_open_container( &it
, DBUS_TYPE_VARIANT
,
836 DBUS_TYPE_UINT32_AS_STRING
, &varIt
);
837 dbus_uint32_t nTimeout
= 0;
838 dbus_message_iter_append_basic( &varIt
, DBUS_TYPE_UINT32
, &nTimeout
);
839 dbus_message_iter_close_container( &it
, &varIt
);
840 dbus_connection_send( pConnection
, pMsg
, nullptr ); // async send - why not ?
841 dbus_message_unref( pMsg
);
843 // set discoverable value
844 pMsg
= pAdapter
->getMethodCall( "SetProperty" );
845 dbus_message_iter_init_append( pMsg
, &it
);
846 const char *pDiscoverableStr
= "Discoverable";
847 dbus_message_iter_append_basic( &it
, DBUS_TYPE_STRING
, &pDiscoverableStr
);
848 dbus_message_iter_open_container( &it
, DBUS_TYPE_VARIANT
,
849 DBUS_TYPE_BOOLEAN_AS_STRING
, &varIt
);
850 dbus_bool_t bValue
= bDiscoverable
;
851 dbus_message_iter_append_basic( &varIt
, DBUS_TYPE_BOOLEAN
, &bValue
);
852 dbus_message_iter_close_container( &it
, &varIt
); // async send - why not ?
853 dbus_connection_send( pConnection
, pMsg
, nullptr );
854 dbus_message_unref( pMsg
);
856 else if (pAdapter
->maInterface
== "org.bluez.Adapter1") // Bluez 5
858 setDBusBooleanProperty(pConnection
, pAdapter
, "Discoverable", bDiscoverable
);
862 static std::unique_ptr
<DBusObject
>
863 registerWithDefaultAdapter( DBusConnection
*pConnection
)
865 std::unique_ptr
<DBusObject
> pService(bluez4GetDefaultService( pConnection
));
868 if( !bluez4RegisterServiceRecord( pConnection
, pService
.get(),
869 bluetooth_service_record
) )
878 static void ProfileUnregisterFunction
879 (DBusConnection
*, void *)
881 // We specifically don't need to do anything here.
884 static DBusHandlerResult ProfileMessageFunction
885 (DBusConnection
*pConnection
, DBusMessage
*pMessage
, void *user_data
)
887 SAL_INFO("sdremote.bluetooth", "ProfileMessageFunction||" << dbus_message_get_interface(pMessage
) << "||" << dbus_message_get_member(pMessage
));
889 if (dbus_message_get_interface(pMessage
) == std::string_view("org.bluez.Profile1"))
891 if (dbus_message_get_member(pMessage
) == std::string_view("Release"))
893 return DBUS_HANDLER_RESULT_HANDLED
;
895 else if (dbus_message_get_member(pMessage
) == std::string_view("NewConnection"))
897 if (!dbus_message_has_signature(pMessage
, "oha{sv}"))
899 SAL_WARN("sdremote.bluetooth", "wrong signature for NewConnection");
903 if (!dbus_message_iter_init(pMessage
, &it
))
904 SAL_WARN( "sdremote.bluetooth", "error init dbus" );
908 dbus_message_iter_get_basic(&it
, &pPath
);
909 SAL_INFO("sdremote.bluetooth", "Adapter path:" << pPath
);
911 if (!dbus_message_iter_next(&it
))
912 SAL_WARN("sdremote.bluetooth", "not enough parameters passed");
914 // DBUS_TYPE_UNIX_FD == 'h' -- doesn't exist in older versions
915 // of dbus (< 1.3?) hence defined manually for now
916 if ('h' == dbus_message_iter_get_arg_type(&it
))
920 dbus_message_iter_get_basic(&it
, &nDescriptor
);
921 std::vector
<Communicator
*>* pCommunicators
= static_cast<std::vector
<Communicator
*>*>(user_data
);
923 // Bluez gives us non-blocking sockets, but our code relies
924 // on blocking behaviour.
925 (void)fcntl(nDescriptor
, F_SETFL
, fcntl(nDescriptor
, F_GETFL
) & ~O_NONBLOCK
);
927 SAL_INFO( "sdremote.bluetooth", "connection accepted " << nDescriptor
);
928 Communicator
* pCommunicator
= new Communicator( std::make_unique
<BufferedStreamSocket
>( nDescriptor
) );
930 ::osl::MutexGuard
aGuard(RemoteServer::sDataMutex
);
931 pCommunicators
->push_back( pCommunicator
);
933 pCommunicator
->launch();
936 // For some reason an (empty?) reply is expected.
937 DBusMessage
* pRet
= dbus_message_new_method_return(pMessage
);
938 dbus_connection_send(pConnection
, pRet
, nullptr);
939 dbus_message_unref(pRet
);
941 // We could read the remote profile version and features here
942 // (i.e. they are provided as part of the DBusMessage),
943 // however for us they are irrelevant (as our protocol handles
944 // equivalent functionality independently of whether we're on
945 // bluetooth or normal network connection).
946 return DBUS_HANDLER_RESULT_HANDLED
;
949 else if (dbus_message_get_member(pMessage
) == std::string_view("RequestDisconnection"))
951 return DBUS_HANDLER_RESULT_HANDLED
;
954 SAL_WARN("sdremote.bluetooth", "Couldn't handle message correctly.");
955 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
960 setupBluez5Profile1(DBusConnection
* pConnection
, std::vector
<Communicator
*>* pCommunicators
)
964 SAL_INFO("sdremote.bluetooth", "Attempting to register our org.bluez.Profile1");
965 static DBusObjectPathVTable aVTable
;
966 aVTable
.unregister_function
= ProfileUnregisterFunction
;
967 aVTable
.message_function
= ProfileMessageFunction
;
969 // dbus_connection_try_register_object_path could be used but only exists for
970 // dbus >= 1.2 -- we really shouldn't be trying this twice in any case.
971 // (dbus_connection_try_register_object_path also returns an error with more
972 // information which could be useful for debugging purposes.)
973 bErr
= !dbus_connection_register_object_path(pConnection
, "/org/libreoffice/bluez/profile1", &aVTable
, pCommunicators
);
977 SAL_WARN("sdremote.bluetooth", "Failed to register Bluez 5 Profile1 callback, bluetooth won't work.");
980 dbus_connection_flush( pConnection
);
984 unregisterBluez5Profile(DBusConnection
* pConnection
)
986 DBusMessage
* pMsg
= dbus_message_new_method_call("org.bluez", "/org/bluez",
987 "org.bluez.ProfileManager1", "UnregisterProfile");
989 dbus_message_iter_init_append(pMsg
, &it
);
991 const char *pPath
= "/org/libreoffice/bluez/profile1";
992 dbus_message_iter_append_basic(&it
, DBUS_TYPE_OBJECT_PATH
, &pPath
);
994 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
997 dbus_message_unref(pMsg
);
999 dbus_connection_unregister_object_path( pConnection
, "/org/libreoffice/bluez/profile1");
1001 dbus_connection_flush(pConnection
);
1005 registerBluez5Profile(DBusConnection
* pConnection
, std::vector
<Communicator
*>* pCommunicators
)
1007 setupBluez5Profile1(pConnection
, pCommunicators
);
1012 pMsg
= dbus_message_new_method_call("org.bluez", "/org/bluez",
1013 "org.bluez.ProfileManager1", "RegisterProfile");
1014 dbus_message_iter_init_append(pMsg
, &it
);
1016 const char *pPath
= "/org/libreoffice/bluez/profile1";
1017 dbus_message_iter_append_basic(&it
, DBUS_TYPE_OBJECT_PATH
, &pPath
);
1018 const char *pUUID
= "spp"; // Bluez translates this to 0x1101 for spp
1019 dbus_message_iter_append_basic(&it
, DBUS_TYPE_STRING
, &pUUID
);
1021 DBusMessageIter aOptionsIter
;
1022 dbus_message_iter_open_container(&it
, DBUS_TYPE_ARRAY
, "{sv}", &aOptionsIter
);
1024 DBusMessageIter aEntry
;
1027 dbus_message_iter_open_container(&aOptionsIter
, DBUS_TYPE_DICT_ENTRY
, nullptr, &aEntry
);
1029 const char *pString
= "Name";
1030 dbus_message_iter_append_basic(&aEntry
, DBUS_TYPE_STRING
, &pString
);
1032 const char *pValue
= "LibreOffice Impress Remote";
1033 DBusMessageIter aValue
;
1034 dbus_message_iter_open_container(&aEntry
, DBUS_TYPE_VARIANT
, "s", &aValue
);
1035 dbus_message_iter_append_basic(&aValue
, DBUS_TYPE_STRING
, &pValue
);
1036 dbus_message_iter_close_container(&aEntry
, &aValue
);
1037 dbus_message_iter_close_container(&aOptionsIter
, &aEntry
);
1040 dbus_message_iter_close_container(&it
, &aOptionsIter
);
1042 // Other properties that we could set (but don't, since they appear
1043 // to be useless for us):
1044 // "Service": "0x1101" (not needed, but we used to have it in the manually defined profile).
1045 // "Role": setting this to "server" breaks things, although we think we're a server?
1046 // "Channel": seems to be dealt with automatically (but we used to use 5 in the manual profile).
1048 bool bSuccess
= true;
1050 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
1053 dbus_error_init(&aError
);
1054 if (pMsg
&& dbus_set_error_from_message( &aError
, pMsg
))
1057 SAL_WARN("sdremote.bluetooth",
1058 "Failed to register our Profile1 with bluez ProfileManager "
1059 << (aError
.message
? aError
.message
: "<null>"));
1062 dbus_error_free(&aError
);
1064 dbus_message_unref(pMsg
);
1066 dbus_connection_flush(pConnection
);
1071 #endif // LINUX_BLUETOOTH
1073 BluetoothServer::BluetoothServer( std::vector
<Communicator
*>* pCommunicators
)
1074 : meWasDiscoverable( UNKNOWN
),
1075 mpCommunicators( pCommunicators
)
1077 #ifdef LINUX_BLUETOOTH
1078 // D-Bus requires the following in order to be thread-safe (and we
1079 // potentially access D-Bus from different threads in different places of
1081 if (!dbus_threads_init_default()) {
1082 throw std::bad_alloc();
1085 mpImpl
.reset(new BluetoothServer::Impl());
1089 BluetoothServer::~BluetoothServer()
1093 void BluetoothServer::ensureDiscoverable()
1095 #ifdef LINUX_BLUETOOTH
1096 // Push it all across into our mainloop
1099 GSource
*pIdle
= g_idle_source_new();
1100 g_source_set_callback( pIdle
, ensureDiscoverable_cb
, nullptr, nullptr );
1101 g_source_set_priority( pIdle
, G_PRIORITY_DEFAULT
);
1102 g_source_attach( pIdle
, spServer
->mpImpl
->mpContext
);
1103 g_source_unref( pIdle
);
1107 void BluetoothServer::restoreDiscoverable()
1109 #ifdef LINUX_BLUETOOTH
1110 // Push it all across into our mainloop
1113 GSource
*pIdle
= g_idle_source_new();
1114 g_source_set_callback( pIdle
, restoreDiscoverable_cb
, nullptr, nullptr );
1115 g_source_set_priority( pIdle
, G_PRIORITY_DEFAULT_IDLE
);
1116 g_source_attach( pIdle
, spServer
->mpImpl
->mpContext
);
1117 g_source_unref( pIdle
);
1121 void BluetoothServer::doEnsureDiscoverable()
1123 #ifdef LINUX_BLUETOOTH
1124 if (!spServer
->mpImpl
->mpConnection
||
1125 spServer
->meWasDiscoverable
!= UNKNOWN
)
1128 // Find out if we are discoverable already ...
1129 std::unique_ptr
<DBusObject
> pAdapter
= spServer
->mpImpl
->getAdapter();
1133 bool bDiscoverable
= getDiscoverable(spServer
->mpImpl
->mpConnection
, pAdapter
.get() );
1135 spServer
->meWasDiscoverable
= bDiscoverable
? DISCOVERABLE
: NOT_DISCOVERABLE
;
1136 if( !bDiscoverable
)
1137 setDiscoverable( spServer
->mpImpl
->mpConnection
, pAdapter
.get(), true );
1141 void BluetoothServer::doRestoreDiscoverable()
1143 if( spServer
->meWasDiscoverable
== NOT_DISCOVERABLE
)
1145 #ifdef LINUX_BLUETOOTH
1146 std::unique_ptr
<DBusObject
> pAdapter
= spServer
->mpImpl
->getAdapter();
1149 setDiscoverable( spServer
->mpImpl
->mpConnection
, pAdapter
.get(), false );
1152 spServer
->meWasDiscoverable
= UNKNOWN
;
1155 // We have to have all our clients shut otherwise we can't
1156 // re-bind to the same port number it appears.
1157 void BluetoothServer::cleanupCommunicators()
1159 ::osl::MutexGuard
aGuard(RemoteServer::sDataMutex
);
1160 for (auto& rpCommunicator
: *mpCommunicators
)
1161 rpCommunicator
->forceClose();
1162 // the hope is that all the threads then terminate cleanly and
1163 // clean themselves up.
1166 void SAL_CALL
BluetoothServer::run()
1168 SAL_INFO( "sdremote.bluetooth", "BluetoothServer::run called" );
1169 osl::Thread::setName("BluetoothServer");
1170 #ifdef LINUX_BLUETOOTH
1171 DBusConnection
*pConnection
= dbusConnectToNameOnBus();
1175 // For either implementation we need to poll the dbus fd
1178 if( dbus_connection_get_unix_fd( pConnection
, &fd
) && fd
>= 0 )
1181 aDBusFD
.events
= G_IO_IN
| G_IO_PRI
;
1182 g_main_context_add_poll( mpImpl
->mpContext
, &aDBusFD
, G_PRIORITY_DEFAULT
);
1185 SAL_WARN( "sdremote.bluetooth", "failed to poll for incoming dbus signals" );
1187 if (isBluez5Available(pConnection
))
1189 SAL_INFO("sdremote.bluetooth", "Using Bluez 5");
1190 registerBluez5Profile(pConnection
, mpCommunicators
);
1191 mpImpl
->mpConnection
= pConnection
;
1192 mpImpl
->maBluezVersion
= Impl::BluezVersion::BLUEZ5
;
1194 // We don't need to listen to adapter changes anymore -- profile
1195 // registration is done globally for the entirety of bluez, so we only
1196 // need adapters when setting discoverability, which can be done
1197 // dynamically without the need to listen for changes.
1199 // TODO: exit on SD deinit
1200 // Probably best to do that in SdModule::~SdModule?
1203 aDBusFD
.revents
= 0;
1204 g_main_context_iteration( mpImpl
->mpContext
, true );
1205 if( aDBusFD
.revents
)
1207 dbus_connection_read_write( pConnection
, 0 );
1208 while (DBUS_DISPATCH_DATA_REMAINS
== dbus_connection_get_dispatch_status( pConnection
))
1209 dbus_connection_dispatch( pConnection
);
1212 // silence Clang -Wunreachable-code after loop (TODO: proper
1215 unregisterBluez5Profile( pConnection
);
1216 g_main_context_unref( mpImpl
->mpContext
);
1217 mpImpl
->mpConnection
= nullptr;
1218 mpImpl
->mpContext
= nullptr;
1222 // Otherwise we could be on Bluez 4 and continue as usual.
1223 mpImpl
->maBluezVersion
= Impl::BluezVersion::BLUEZ4
;
1225 // Try to setup the default adapter, otherwise wait for add/remove signal
1226 mpImpl
->mpService
= registerWithDefaultAdapter( pConnection
);
1227 // listen for connection state and power changes - we need to close
1228 // and re-create our socket code on suspend / resume, enable/disable
1230 dbus_error_init( &aError
);
1231 dbus_bus_add_match( pConnection
, "type='signal',interface='org.bluez.Manager'", &aError
);
1232 dbus_connection_flush( pConnection
);
1234 // Try to setup the default adapter, otherwise wait for add/remove signal
1235 mpImpl
->mpService
= registerWithDefaultAdapter( pConnection
);
1237 // poll on our bluetooth socket - if we can.
1239 if( mpImpl
->mpService
)
1240 bluezCreateAttachListeningSocket( mpImpl
->mpContext
, &aSocketFD
);
1242 mpImpl
->mpConnection
= pConnection
;
1246 aDBusFD
.revents
= 0;
1247 aSocketFD
.revents
= 0;
1248 g_main_context_iteration( mpImpl
->mpContext
, true );
1250 SAL_INFO( "sdremote.bluetooth", "main-loop spin "
1251 << aDBusFD
.revents
<< " " << aSocketFD
.revents
);
1252 if( aDBusFD
.revents
)
1254 dbus_connection_read_write( pConnection
, 0 );
1255 DBusMessage
*pMsg
= dbus_connection_pop_message( pConnection
);
1258 if( dbus_message_is_signal( pMsg
, "org.bluez.Manager", "AdapterRemoved" ) )
1260 SAL_WARN( "sdremote.bluetooth", "lost adapter - cleaning up sockets" );
1261 bluezDetachCloseSocket( mpImpl
->mpContext
, &aSocketFD
);
1262 cleanupCommunicators();
1264 else if( dbus_message_is_signal( pMsg
, "org.bluez.Manager", "AdapterAdded" ) ||
1265 dbus_message_is_signal( pMsg
, "org.bluez.Manager", "DefaultAdapterChanged" ) )
1267 SAL_WARN( "sdremote.bluetooth", "gained adapter - re-generating sockets" );
1268 bluezDetachCloseSocket( mpImpl
->mpContext
, &aSocketFD
);
1269 cleanupCommunicators();
1270 mpImpl
->mpService
= registerWithDefaultAdapter( pConnection
);
1271 if( mpImpl
->mpService
)
1272 bluezCreateAttachListeningSocket( mpImpl
->mpContext
, &aSocketFD
);
1275 SAL_INFO( "sdremote.bluetooth", "unknown incoming dbus message, "
1276 " type: " << dbus_message_get_type( pMsg
)
1277 << " path: '" << dbus_message_get_path( pMsg
)
1278 << "' interface: '" << dbus_message_get_interface( pMsg
)
1279 << "' member: '" << dbus_message_get_member( pMsg
) );
1281 dbus_message_unref( pMsg
);
1284 if( aSocketFD
.revents
)
1286 sockaddr_rc aRemoteAddr
;
1287 socklen_t aRemoteAddrLen
= sizeof(aRemoteAddr
);
1289 SAL_INFO( "sdremote.bluetooth", "performing accept" );
1290 int nClient
= accept( aSocketFD
.fd
, reinterpret_cast<sockaddr
*>(&aRemoteAddr
), &aRemoteAddrLen
);
1291 if ( nClient
< 0 && errno
!= EAGAIN
)
1293 SAL_WARN( "sdremote.bluetooth", "accept failed with errno " << errno
);
1295 SAL_INFO( "sdremote.bluetooth", "connection accepted " << nClient
);
1296 Communicator
* pCommunicator
= new Communicator( std::make_unique
<BufferedStreamSocket
>( nClient
) );
1298 ::osl::MutexGuard
aGuard(RemoteServer::sDataMutex
);
1299 mpCommunicators
->push_back( pCommunicator
);
1301 pCommunicator
->launch();
1305 // silence Clang -Wunreachable-code after loop (TODO: proper fix?)
1308 unregisterBluez5Profile( pConnection
);
1309 g_main_context_unref( mpImpl
->mpContext
);
1310 mpImpl
->mpConnection
= nullptr;
1311 mpImpl
->mpContext
= nullptr;
1313 #elif defined(_WIN32)
1314 WORD wVersionRequested
;
1317 wVersionRequested
= MAKEWORD(2, 2);
1319 if ( WSAStartup(wVersionRequested
, &wsaData
) )
1321 return; // winsock dll couldn't be loaded
1324 int aSocket
= socket( AF_BTH
, SOCK_STREAM
, BTHPROTO_RFCOMM
);
1331 aAddr
.addressFamily
= AF_BTH
;
1333 aAddr
.serviceClassId
= GUID_NULL
;
1334 aAddr
.port
= BT_PORT_ANY
; // Select any free socket.
1335 if ( bind( aSocket
, reinterpret_cast<SOCKADDR
*>(&aAddr
), sizeof(aAddr
) ) == SOCKET_ERROR
)
1337 closesocket( aSocket
);
1343 int aNameSize
= sizeof(aName
);
1344 getsockname( aSocket
, reinterpret_cast<SOCKADDR
*>(&aName
), &aNameSize
); // Retrieve the local address and port
1346 CSADDR_INFO aAddrInfo
= {};
1347 aAddrInfo
.LocalAddr
.lpSockaddr
= reinterpret_cast<SOCKADDR
*>(&aName
);
1348 aAddrInfo
.LocalAddr
.iSockaddrLength
= sizeof( SOCKADDR_BTH
);
1349 aAddrInfo
.iSocketType
= SOCK_STREAM
;
1350 aAddrInfo
.iProtocol
= BTHPROTO_RFCOMM
;
1352 // To be used for setting a custom UUID once available.
1354 // uuid.Data1 = 0x00001101;
1355 // memset( &uuid, 0x1000 + UUID*2^96, sizeof( GUID ) );
1357 // uuid.Data3 = 0x1000;
1358 // ULONGLONG aData4 = 0x800000805F9B34FB;
1359 // memcpy( uuid.Data4, &aData4, sizeof(uuid.Data4) );
1361 WSAQUERYSETW aRecord
= {};
1362 aRecord
.dwSize
= sizeof(aRecord
);
1363 aRecord
.lpszServiceInstanceName
= const_cast<wchar_t *>(
1364 L
"LibreOffice Impress Remote Control");
1365 aRecord
.lpszComment
= const_cast<wchar_t *>(
1366 L
"Remote control of presentations over bluetooth.");
1367 aRecord
.lpServiceClassId
= const_cast<LPGUID
>(&SerialPortServiceClass_UUID
);
1368 aRecord
.dwNameSpace
= NS_BTH
;
1369 aRecord
.dwNumberOfCsAddrs
= 1;
1370 aRecord
.lpcsaBuffer
= &aAddrInfo
;
1371 if (WSASetServiceW( &aRecord
, RNRSERVICE_REGISTER
, 0 ) == SOCKET_ERROR
)
1373 closesocket( aSocket
);
1378 if ( listen( aSocket
, 1 ) == SOCKET_ERROR
)
1380 closesocket( aSocket
);
1385 SOCKADDR_BTH aRemoteAddr
;
1386 int aRemoteAddrLen
= sizeof(aRemoteAddr
);
1390 if ( (socket
= accept(aSocket
, reinterpret_cast<sockaddr
*>(&aRemoteAddr
), &aRemoteAddrLen
)) == INVALID_SOCKET
)
1392 closesocket( aSocket
);
1396 Communicator
* pCommunicator
= new Communicator( std::make_unique
<BufferedStreamSocket
>( socket
) );
1398 ::osl::MutexGuard
aGuard(RemoteServer::sDataMutex
);
1399 mpCommunicators
->push_back( pCommunicator
);
1401 pCommunicator
->launch();
1405 #elif defined(MACOSX)
1406 // Build up dictionary at run-time instead of bothering with a
1407 // .plist file, using the Objective-C API
1409 // Compare to BluetoothServiceRecord.hxx
1411 NSAutoreleasePool
* pool
= [[NSAutoreleasePool alloc
] init
];
1413 NSDictionary
*dict
=
1414 [NSDictionary dictionaryWithObjectsAndKeys
:
1416 // Service class ID list
1417 [NSArray arrayWithObject
:
1418 [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16ServiceClassSerialPort
]],
1419 @
"0001 - ServiceClassIDList",
1421 // Protocol descriptor list
1422 [NSArray arrayWithObjects
:
1423 [NSArray arrayWithObject
: [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16L2CAP
]],
1424 [NSArray arrayWithObjects
:
1425 [IOBluetoothSDPUUID uuid16
: kBluetoothL2CAPPSMRFCOMM
],
1426 [NSDictionary dictionaryWithObjectsAndKeys
:
1427 [NSNumber numberWithInt
: 1],
1429 [NSNumber numberWithInt
: 1],
1431 [NSNumber numberWithInt
: 5], // RFCOMM port number, will be replaced if necessary automatically
1432 @
"DataElementValue",
1436 @
"0004 - Protocol descriptor list",
1438 // Browse group list
1439 [NSArray arrayWithObject
:
1440 [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16ServiceClassPublicBrowseGroup
]],
1441 @
"0005 - BrowseGroupList",
1443 // Language base attribute ID list
1444 [NSArray arrayWithObjects
:
1445 [NSData dataWithBytes
: "en" length
: 2],
1446 [NSDictionary dictionaryWithObjectsAndKeys
:
1447 [NSNumber numberWithInt
: 2],
1449 [NSNumber numberWithInt
: 1],
1451 [NSNumber numberWithInt
: 0x006a], // encoding
1452 @
"DataElementValue",
1454 [NSDictionary dictionaryWithObjectsAndKeys
:
1455 [NSNumber numberWithInt
: 2],
1457 [NSNumber numberWithInt
: 1],
1459 [NSNumber numberWithInt
: 0x0100], // offset
1460 @
"DataElementValue",
1463 @
"0006 - LanguageBaseAttributeIDList",
1465 // Bluetooth profile descriptor list
1466 [NSArray arrayWithObject
:
1467 [NSArray arrayWithObjects
:
1468 [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16ServiceClassSerialPort
],
1469 [NSDictionary dictionaryWithObjectsAndKeys
:
1470 [NSNumber numberWithInt
: 2],
1472 [NSNumber numberWithInt
: 1],
1474 [NSNumber numberWithInt
: 0x0100], // version number ?
1475 @
"DataElementValue",
1478 @
"0009 - BluetoothProfileDescriptorList",
1480 // Attributes pointed to by the LanguageBaseAttributeIDList
1481 @
"LibreOffice Impress Remote Control",
1482 @
"0100 - ServiceName",
1483 @
"The Document Foundation",
1484 @
"0102 - ProviderName",
1488 IOBluetoothSDPServiceRecordRef serviceRecordRef
;
1489 SAL_WNODEPRECATED_DECLARATIONS_PUSH
//TODO: 10.9 IOBluetoothAddServiceDict
1490 IOReturn rc
= IOBluetoothAddServiceDict(reinterpret_cast<CFDictionaryRef
>(dict
), &serviceRecordRef
);
1491 SAL_WNODEPRECATED_DECLARATIONS_POP
1493 SAL_INFO("sdremote.bluetooth", "IOBluetoothAddServiceDict returned " << rc
);
1495 if (rc
== kIOReturnSuccess
)
1497 IOBluetoothSDPServiceRecord
*serviceRecord
=
1498 [IOBluetoothSDPServiceRecord withSDPServiceRecordRef
: serviceRecordRef
];
1500 BluetoothRFCOMMChannelID channelID
;
1501 [serviceRecord getRFCOMMChannelID
: &channelID
];
1503 BluetoothSDPServiceRecordHandle serviceRecordHandle
;
1504 [serviceRecord getServiceRecordHandle
: &serviceRecordHandle
];
1506 // Register callback for incoming connections
1507 IOBluetoothRegisterForFilteredRFCOMMChannelOpenNotifications(
1511 kIOBluetoothUserNotificationChannelDirectionIncoming
);
1513 [serviceRecord release
];
1518 (void) mpCommunicators
;
1520 (void) mpCommunicators
; // avoid warnings about unused member
1524 BluetoothServer
*sd::BluetoothServer::spServer
= nullptr;
1526 void BluetoothServer::setup( std::vector
<Communicator
*>* pCommunicators
)
1531 spServer
= new BluetoothServer( pCommunicators
);
1535 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */