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"
17 #include <sal/log.hxx>
18 #include <osl/socket.hxx>
20 #ifdef LINUX_BLUETOOTH
22 #include <dbus/dbus.h>
25 #include <sys/unistd.h>
26 #include <sys/socket.h>
27 #include <bluetooth/bluetooth.h>
28 #include <bluetooth/rfcomm.h>
29 #include "BluetoothServiceRecord.hxx"
30 #include "BufferedStreamSocket.hxx"
34 // LO vs WinAPI conflict
39 #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"
58 #ifdef LINUX_BLUETOOTH
66 DBusObject( const char *pBusName
, const char *pPath
, const char *pInterface
)
67 : maBusName( pBusName
), maPath( pPath
), maInterface( pInterface
) { }
69 DBusMessage
*getMethodCall( const char *pName
)
71 return dbus_message_new_method_call( maBusName
.getStr(), maPath
.getStr(),
72 maInterface
.getStr(), pName
);
74 std::unique_ptr
<DBusObject
> cloneForInterface( const char *pInterface
)
76 std::unique_ptr
<DBusObject
> pObject(new DBusObject());
78 pObject
->maBusName
= maBusName
;
79 pObject
->maPath
= maPath
;
80 pObject
->maInterface
= pInterface
;
86 static std::unique_ptr
<DBusObject
> getBluez5Adapter(DBusConnection
*pConnection
);
88 struct sd::BluetoothServer::Impl
{
89 // the glib mainloop running in the thread
90 GMainContext
*mpContext
;
91 DBusConnection
*mpConnection
;
92 std::unique_ptr
<DBusObject
> mpService
;
93 enum class BluezVersion
{ BLUEZ4
, BLUEZ5
, UNKNOWN
};
94 BluezVersion maBluezVersion
;
97 : mpContext( g_main_context_new() )
98 , mpConnection( nullptr )
99 , maBluezVersion( BluezVersion::UNKNOWN
)
102 std::unique_ptr
<DBusObject
> getAdapter()
106 return mpService
->cloneForInterface( "org.bluez.Adapter" );
108 else if (spServer
->mpImpl
->maBluezVersion
== BluezVersion::BLUEZ5
)
110 return getBluez5Adapter(mpConnection
);
119 static DBusConnection
*
120 dbusConnectToNameOnBus()
123 DBusConnection
*pConnection
;
125 dbus_error_init( &aError
);
127 pConnection
= dbus_bus_get( DBUS_BUS_SYSTEM
, &aError
);
128 if( !pConnection
|| dbus_error_is_set( &aError
))
130 SAL_WARN( "sdremote.bluetooth", "failed to get dbus system bus: " << aError
.message
);
131 dbus_error_free( &aError
);
139 sendUnrefAndWaitForReply( DBusConnection
*pConnection
, DBusMessage
*pMsg
)
141 DBusPendingCall
*pPending
= nullptr;
143 if( !pMsg
|| !dbus_connection_send_with_reply( pConnection
, pMsg
, &pPending
,
144 -1 /* default timeout */ ) )
146 SAL_WARN( "sdremote.bluetooth", "Memory allocation failed on message send" );
147 dbus_message_unref( pMsg
);
150 dbus_connection_flush( pConnection
);
151 dbus_message_unref( pMsg
);
153 dbus_pending_call_block( pPending
); // block for reply
155 pMsg
= dbus_pending_call_steal_reply( pPending
);
157 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
159 dbus_pending_call_unref( pPending
);
164 isBluez5Available(DBusConnection
*pConnection
)
168 // Simplest ways to check whether we have Bluez 5+ is to check
169 // that we can obtain adapters using the new interfaces.
170 // The first two error checks however don't tell us anything as they should
171 // succeed as long as dbus is working correctly.
172 pMsg
= DBusObject( "org.bluez", "/", "org.freedesktop.DBus.ObjectManager" ).getMethodCall( "GetManagedObjects" );
175 SAL_INFO("sdremote.bluetooth", "No GetManagedObjects call created");
179 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
182 SAL_INFO("sdremote.bluetooth", "No reply received");
186 // If dbus is working correctly and we aren't on bluez 5 this is where we
187 // should actually get the error.
188 if (dbus_message_get_error_name( pMsg
))
190 SAL_INFO( "sdremote.bluetooth", "GetManagedObjects call failed with \""
191 << dbus_message_get_error_name( pMsg
)
192 << "\" -- we don't seem to have Bluez 5 available");
195 SAL_INFO("sdremote.bluetooth", "GetManagedObjects call seems to have succeeded -- we must be on Bluez 5");
196 dbus_message_unref(pMsg
);
200 static std::unique_ptr
<DBusObject
>
201 getBluez5Adapter(DBusConnection
*pConnection
)
204 // This returns a list of objects where we need to find the first
205 // org.bluez.Adapter1 .
206 pMsg
= DBusObject( "org.bluez", "/", "org.freedesktop.DBus.ObjectManager" ).getMethodCall( "GetManagedObjects" );
210 const gchar
* const pInterfaceType
= "org.bluez.Adapter1";
212 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
214 DBusMessageIter aObjectIterator
;
215 if (pMsg
&& dbus_message_iter_init(pMsg
, &aObjectIterator
))
217 if (DBUS_TYPE_ARRAY
== dbus_message_iter_get_arg_type(&aObjectIterator
))
219 DBusMessageIter aObject
;
220 dbus_message_iter_recurse(&aObjectIterator
, &aObject
);
223 if (DBUS_TYPE_DICT_ENTRY
== dbus_message_iter_get_arg_type(&aObject
))
225 DBusMessageIter aContainerIter
;
226 dbus_message_iter_recurse(&aObject
, &aContainerIter
);
227 char *pPath
= nullptr;
230 if (DBUS_TYPE_OBJECT_PATH
== dbus_message_iter_get_arg_type(&aContainerIter
))
232 dbus_message_iter_get_basic(&aContainerIter
, &pPath
);
233 SAL_INFO( "sdremote.bluetooth", "Something retrieved: '"
236 else if (DBUS_TYPE_ARRAY
== dbus_message_iter_get_arg_type(&aContainerIter
))
238 DBusMessageIter aInnerIter
;
239 dbus_message_iter_recurse(&aContainerIter
, &aInnerIter
);
242 if (DBUS_TYPE_DICT_ENTRY
== dbus_message_iter_get_arg_type(&aInnerIter
))
244 DBusMessageIter aInnerInnerIter
;
245 dbus_message_iter_recurse(&aInnerIter
, &aInnerInnerIter
);
248 if (DBUS_TYPE_STRING
== dbus_message_iter_get_arg_type(&aInnerInnerIter
))
252 dbus_message_iter_get_basic(&aInnerInnerIter
, &pMessage
);
253 if (OString(pMessage
) == "org.bluez.Adapter1")
255 dbus_message_unref(pMsg
);
258 return std::make_unique
<DBusObject
>( "org.bluez", pPath
, pInterfaceType
);
260 assert(false); // We should already have pPath provided for us.
264 while (dbus_message_iter_next(&aInnerInnerIter
));
267 while (dbus_message_iter_next(&aInnerIter
));
270 while (dbus_message_iter_next(&aContainerIter
));
273 while (dbus_message_iter_next(&aObject
));
275 dbus_message_unref(pMsg
);
282 bluez4GetDefaultService( DBusConnection
*pConnection
)
286 const gchar
* const pInterfaceType
= "org.bluez.Service";
288 // org.bluez.manager only exists for bluez 4.
289 // getMethodCall should return NULL if there is any issue e.g. the
290 // if org.bluez.manager doesn't exist.
291 pMsg
= DBusObject( "org.bluez", "/", "org.bluez.Manager" ).getMethodCall( "DefaultAdapter" );
295 SAL_WARN("sdremote.bluetooth", "Couldn't retrieve DBusObject for DefaultAdapter");
299 SAL_INFO("sdremote.bluetooth", "successfully retrieved org.bluez.Manager.DefaultAdapter, attempting to use.");
300 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
302 if(!pMsg
|| !dbus_message_iter_init( pMsg
, &it
) )
307 // This works for Bluez 4
308 if( DBUS_TYPE_OBJECT_PATH
== dbus_message_iter_get_arg_type( &it
) )
310 const char *pObjectPath
= nullptr;
311 dbus_message_iter_get_basic( &it
, &pObjectPath
);
312 SAL_INFO( "sdremote.bluetooth", "DefaultAdapter retrieved: '"
313 << pObjectPath
<< "' '" << pInterfaceType
<< "'" );
314 dbus_message_unref( pMsg
);
315 return new DBusObject( "org.bluez", pObjectPath
, pInterfaceType
);
317 // Some form of error, e.g. if we have bluez 5 we get a message that
318 // this method doesn't exist.
319 else if ( DBUS_TYPE_STRING
== dbus_message_iter_get_arg_type( &it
) )
321 const char *pMessage
= nullptr;
322 dbus_message_iter_get_basic( &it
, &pMessage
);
323 SAL_INFO( "sdremote.bluetooth", "Error message: '"
324 << pMessage
<< "' '" << pInterfaceType
<< "'" );
328 SAL_INFO( "sdremote.bluetooth", "invalid type of reply to DefaultAdapter: '"
329 << static_cast<char>(dbus_message_iter_get_arg_type( &it
)) << "'" );
331 dbus_message_unref(pMsg
);
336 bluez4RegisterServiceRecord( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
337 const char *pServiceRecord
)
342 pMsg
= pAdapter
->getMethodCall( "AddRecord" );
343 dbus_message_iter_init_append( pMsg
, &it
);
344 dbus_message_iter_append_basic( &it
, DBUS_TYPE_STRING
, &pServiceRecord
);
346 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
348 if( !pMsg
|| !dbus_message_iter_init( pMsg
, &it
) ||
349 dbus_message_iter_get_arg_type( &it
) != DBUS_TYPE_UINT32
)
351 SAL_WARN( "sdremote.bluetooth", "SDP registration failed" );
355 // We ignore the uint de-registration handle we get back:
356 // bluez will clean us up automatically on exit
362 bluezCreateAttachListeningSocket( GMainContext
*pContext
, GPollFD
*pSocketFD
)
368 if( ( nSocket
= socket( AF_BLUETOOTH
, SOCK_STREAM
, BTPROTO_RFCOMM
) ) < 0 )
370 SAL_WARN( "sdremote.bluetooth", "failed to open bluetooth socket with error " << nSocket
);
375 // Initialize whole structure. Mainly to appease valgrind, which
376 // doesn't know about the padding at the end of sockaddr_rc which
377 // it will dutifully check for definedness. But also the standard
378 // definition of BDADDR_ANY is unusable in C++ code, so just use
379 // memset to set aAddr.rc_bdaddr to 0.
380 memset( &aAddr
, 0, sizeof( aAddr
) );
381 aAddr
.rc_family
= AF_BLUETOOTH
;
382 aAddr
.rc_channel
= 5;
385 if ( ( a
= bind( nSocket
, reinterpret_cast<sockaddr
*>(&aAddr
), sizeof(aAddr
) ) ) < 0 ) {
386 SAL_WARN( "sdremote.bluetooth", "bind failed with error" << a
);
391 if ( ( a
= listen( nSocket
, 1 ) ) < 0 )
393 SAL_WARN( "sdremote.bluetooth", "listen failed with error" << a
);
398 // set non-blocking behaviour ...
399 if( fcntl( nSocket
, F_SETFL
, O_NONBLOCK
) < 0 )
405 pSocketFD
->fd
= nSocket
;
406 pSocketFD
->events
= G_IO_IN
| G_IO_PRI
;
407 pSocketFD
->revents
= 0;
409 g_main_context_add_poll( pContext
, pSocketFD
, G_PRIORITY_DEFAULT
);
413 bluezDetachCloseSocket( GMainContext
*pContext
, GPollFD
*pSocketFD
)
415 if( pSocketFD
->fd
>= 0 )
417 close( pSocketFD
->fd
);
418 g_main_context_remove_poll( pContext
, pSocketFD
);
423 #endif // LINUX_BLUETOOTH
427 OSXBluetoothWrapper::OSXBluetoothWrapper( IOBluetoothRFCOMMChannel
* channel
) :
434 // silly enough, can't write more than mnMTU bytes at once
435 mnMTU
= [channel getMTU
];
437 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::OSXBluetoothWrapper(): mnMTU=" << mnMTU
);
440 sal_Int32
OSXBluetoothWrapper::readLine( OString
& aLine
)
442 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine()" );
447 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: entering mutex" );
448 ::osl::MutexGuard
aQueueGuard( mMutex
);
449 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: entered mutex" );
452 // We should have in the sal logging some standard way to
453 // output char buffers with non-printables escaped.
454 std::ostringstream s
;
455 if (mBuffer
.size() > 0)
457 for (unsigned char *p
= reinterpret_cast<unsigned char *>(mBuffer
.data()); p
!= reinterpret_cast<unsigned char *>(mBuffer
.data()) + mBuffer
.size(); p
++)
461 else if (*p
< ' ' || *p
>= 0x7F)
462 s
<< "\\0x" << std::hex
<< std::setw(2) << std::setfill('0') << static_cast<int>(*p
) << std::setfill(' ') << std::setw(1) << std::dec
;
467 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine mBuffer: \"" << s
.str() << "\"" );
470 // got enough bytes to return a line?
471 std::vector
<char>::iterator aIt
;
472 if ( (aIt
= find( mBuffer
.begin(), mBuffer
.end(), '\n' ))
475 sal_uInt64 aLocation
= aIt
- mBuffer
.begin();
477 aLine
= OString( &(*mBuffer
.begin()), aLocation
);
479 mBuffer
.erase( mBuffer
.begin(), aIt
+ 1 ); // Also delete the empty line
482 SAL_INFO( "sdremote.bluetooth", " returning, got \"" << OStringToOUString( aLine
, RTL_TEXTENCODING_UTF8
) << "\"" );
483 return aLine
.getLength() + 1;
486 // nope - wait some more (after releasing the mutex)
487 SAL_INFO( "sdremote.bluetooth", " resetting mHaveBytes" );
489 SAL_INFO( "sdremote.bluetooth", " leaving mutex" );
492 SAL_INFO( "sdremote.bluetooth", " waiting for mHaveBytes" );
494 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::readLine: got mHaveBytes" );
498 sal_Int32
OSXBluetoothWrapper::write( const void* pBuffer
, sal_uInt32 n
)
500 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::write(" << pBuffer
<< ", " << n
<< ") mpChannel=" << mpChannel
);
502 char const * ptr
= static_cast<char const *>(pBuffer
);
503 sal_uInt32 nBytesWritten
= 0;
505 if (mpChannel
== nil
)
508 while( nBytesWritten
< n
)
510 int toWrite
= n
- nBytesWritten
;
511 toWrite
= toWrite
<= mnMTU
? toWrite
: mnMTU
;
512 if ( [mpChannel writeSync
:const_cast<char *>(ptr
) length
:toWrite
] != kIOReturnSuccess
)
514 SAL_INFO( "sdremote.bluetooth", " [mpChannel writeSync:" << static_cast<void const *>(ptr
) << " length:" << toWrite
<< "] returned error, total written " << nBytesWritten
);
515 return nBytesWritten
;
518 nBytesWritten
+= toWrite
;
520 SAL_INFO( "sdremote.bluetooth", " total written " << nBytesWritten
);
521 return nBytesWritten
;
524 void OSXBluetoothWrapper::appendData(void* pBuffer
, size_t len
)
526 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData(" << pBuffer
<< ", " << len
<< ")" );
530 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData: entering mutex" );
531 ::osl::MutexGuard
aQueueGuard( mMutex
);
532 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::appendData: entered mutex" );
533 mBuffer
.insert(mBuffer
.begin()+mBuffer
.size(),
534 static_cast<char*>(pBuffer
), static_cast<char *>(pBuffer
)+len
);
535 SAL_INFO( "sdremote.bluetooth", " setting mHaveBytes" );
537 SAL_INFO( "sdremote.bluetooth", " leaving mutex" );
541 void OSXBluetoothWrapper::channelClosed()
543 SAL_INFO( "sdremote.bluetooth", "OSXBluetoothWrapper::channelClosed()" );
548 void incomingCallback( void *userRefCon
,
549 IOBluetoothUserNotificationRef
,
550 IOBluetoothObjectRef objectRef
)
552 SAL_INFO( "sdremote.bluetooth", "incomingCallback()" );
554 BluetoothServer
* pServer
= static_cast<BluetoothServer
*>(userRefCon
);
556 IOBluetoothRFCOMMChannel
* channel
= [IOBluetoothRFCOMMChannel withRFCOMMChannelRef
:reinterpret_cast<IOBluetoothRFCOMMChannelRef
>(objectRef
)];
558 OSXBluetoothWrapper
* socket
= new OSXBluetoothWrapper( channel
);
559 Communicator
* pCommunicator
= new Communicator( std::unique_ptr
<IBluetoothSocket
>(socket
) );
560 pServer
->addCommunicator( pCommunicator
);
562 ChannelDelegate
* delegate
= [[ChannelDelegate alloc
] initWithCommunicatorAndSocket
: pCommunicator socket
: socket
];
563 [channel setDelegate
: delegate
];
566 pCommunicator
->launch();
569 void BluetoothServer::addCommunicator( Communicator
* pCommunicator
)
571 mpCommunicators
->push_back( pCommunicator
);
576 #ifdef LINUX_BLUETOOTH
579 static gboolean
ensureDiscoverable_cb(gpointer
)
581 BluetoothServer::doEnsureDiscoverable();
582 return FALSE
; // remove source
584 static gboolean
restoreDiscoverable_cb(gpointer
)
586 BluetoothServer::doRestoreDiscoverable();
587 return FALSE
; // remove source
592 * Bluez 4 uses custom methods for setting properties, whereas Bluez 5+
593 * implements properties using the generic "org.freedesktop.DBus.Properties"
594 * interface -- hence we have a specific Bluez 4 function to deal with the
595 * old style of reading properties.
598 getBluez4BooleanProperty( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
599 const char *pPropertyName
, bool *pBoolean
)
607 pMsg
= sendUnrefAndWaitForReply( pConnection
,
608 pAdapter
->getMethodCall( "GetProperties" ) );
611 if( !pMsg
|| !dbus_message_iter_init( pMsg
, &it
) )
613 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
617 if( DBUS_TYPE_ARRAY
!= dbus_message_iter_get_arg_type( &it
) )
619 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
623 DBusMessageIter arrayIt
;
624 dbus_message_iter_recurse( &it
, &arrayIt
);
626 while( dbus_message_iter_get_arg_type( &arrayIt
) == DBUS_TYPE_DICT_ENTRY
)
628 DBusMessageIter dictIt
;
629 dbus_message_iter_recurse( &arrayIt
, &dictIt
);
631 const char *pName
= nullptr;
632 if( dbus_message_iter_get_arg_type( &dictIt
) == DBUS_TYPE_STRING
)
634 dbus_message_iter_get_basic( &dictIt
, &pName
);
635 if( pName
!= nullptr && !strcmp( pName
, pPropertyName
) )
637 SAL_INFO( "sdremote.bluetooth", "hit " << pPropertyName
<< " property" );
638 dbus_message_iter_next( &dictIt
);
639 dbus_bool_t bBool
= false;
641 if( dbus_message_iter_get_arg_type( &dictIt
) == DBUS_TYPE_VARIANT
)
643 DBusMessageIter variantIt
;
644 dbus_message_iter_recurse( &dictIt
, &variantIt
);
646 if( dbus_message_iter_get_arg_type( &variantIt
) == DBUS_TYPE_BOOLEAN
)
648 dbus_message_iter_get_basic( &variantIt
, &bBool
);
649 SAL_INFO( "sdremote.bluetooth", "" << pPropertyName
<< " is " << bBool
);
654 SAL_WARN( "sdremote.bluetooth", "" << pPropertyName
<< " type " <<
655 dbus_message_iter_get_arg_type( &variantIt
) );
658 SAL_WARN( "sdremote.bluetooth", "variant type ? " <<
659 dbus_message_iter_get_arg_type( &dictIt
) );
663 const char *pStr
= pName
? pName
: "<null>";
664 SAL_INFO( "sdremote.bluetooth", "property '" << pStr
<< "'" );
668 SAL_WARN( "sdremote.bluetooth", "unexpected property key type "
669 << dbus_message_iter_get_arg_type( &dictIt
) );
670 dbus_message_iter_next( &arrayIt
);
672 dbus_message_unref( pMsg
);
678 * This gets an org.freedesktop.DBus.Properties boolean
679 * (as opposed to the old Bluez 4 custom properties methods as visible above).
682 getDBusBooleanProperty( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
683 const char *pPropertyName
, bool *pBoolean
)
690 std::unique_ptr
< DBusObject
> pProperties (
691 pAdapter
->cloneForInterface( "org.freedesktop.DBus.Properties" ) );
693 DBusMessage
*pMsg
= pProperties
->getMethodCall( "Get" );
695 DBusMessageIter itIn
;
696 dbus_message_iter_init_append( pMsg
, &itIn
);
697 const char* pInterface
= "org.bluez.Adapter1";
698 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pInterface
);
699 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pPropertyName
);
700 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
703 if( !pMsg
|| !dbus_message_iter_init( pMsg
, &it
) )
705 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
709 if( DBUS_TYPE_VARIANT
!= dbus_message_iter_get_arg_type( &it
) )
711 SAL_WARN( "sdremote.bluetooth", "invalid return type" );
715 DBusMessageIter variantIt
;
716 dbus_message_iter_recurse( &it
, &variantIt
);
718 if( dbus_message_iter_get_arg_type( &variantIt
) == DBUS_TYPE_BOOLEAN
)
720 dbus_bool_t bBool
= false;
721 dbus_message_iter_get_basic( &variantIt
, &bBool
);
722 SAL_INFO( "sdremote.bluetooth", "" << pPropertyName
<< " is " << bBool
);
728 SAL_WARN( "sdremote.bluetooth", "" << pPropertyName
<< " type " <<
729 dbus_message_iter_get_arg_type( &variantIt
) );
732 const char* pError
= dbus_message_get_error_name( pMsg
);
735 SAL_WARN( "sdremote.bluetooth",
736 "Get failed for " << pPropertyName
<< " on " <<
737 pAdapter
->maPath
<< " with error: " << pError
);
740 dbus_message_unref( pMsg
);
746 setDBusBooleanProperty( DBusConnection
*pConnection
, DBusObject
*pAdapter
,
747 const char *pPropertyName
, bool bBoolean
)
751 std::unique_ptr
< DBusObject
> pProperties(
752 pAdapter
->cloneForInterface( "org.freedesktop.DBus.Properties" ) );
754 DBusMessage
*pMsg
= pProperties
->getMethodCall( "Set" );
756 DBusMessageIter itIn
;
757 dbus_message_iter_init_append( pMsg
, &itIn
);
758 const char* pInterface
= "org.bluez.Adapter1";
759 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pInterface
);
760 dbus_message_iter_append_basic( &itIn
, DBUS_TYPE_STRING
, &pPropertyName
);
763 DBusMessageIter varIt
;
764 dbus_message_iter_open_container( &itIn
, DBUS_TYPE_VARIANT
,
765 DBUS_TYPE_BOOLEAN_AS_STRING
, &varIt
);
766 dbus_bool_t bDBusBoolean
= bBoolean
;
767 dbus_message_iter_append_basic( &varIt
, DBUS_TYPE_BOOLEAN
, &bDBusBoolean
);
768 dbus_message_iter_close_container( &itIn
, &varIt
);
771 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
775 SAL_WARN( "sdremote.bluetooth", "no valid reply / timeout" );
779 const char* pError
= dbus_message_get_error_name( pMsg
);
782 SAL_WARN( "sdremote.bluetooth",
783 "Set failed for " << pPropertyName
<< " on " <<
784 pAdapter
->maPath
<< " with error: " << pError
);
786 dbus_message_unref( pMsg
);
791 getDiscoverable( DBusConnection
*pConnection
, DBusObject
*pAdapter
)
793 if (pAdapter
->maInterface
== "org.bluez.Adapter") // Bluez 4
796 if( getBluez4BooleanProperty(pConnection
, pAdapter
, "Discoverable", &bDiscoverable
) )
797 return bDiscoverable
;
799 else if (pAdapter
->maInterface
== "org.bluez.Adapter1") // Bluez 5
802 if ( getDBusBooleanProperty(pConnection
, pAdapter
, "Discoverable", &bDiscoverable
) )
803 return bDiscoverable
;
809 setDiscoverable( DBusConnection
*pConnection
, DBusObject
*pAdapter
, bool bDiscoverable
)
811 SAL_INFO( "sdremote.bluetooth", "setDiscoverable to " << bDiscoverable
);
813 if (pAdapter
->maInterface
== "org.bluez.Adapter") // Bluez 4
815 bool bPowered
= false;
816 if( !getBluez4BooleanProperty( pConnection
, pAdapter
, "Powered", &bPowered
) || !bPowered
)
817 return; // nothing to do
820 DBusMessageIter it
, varIt
;
822 // set timeout to zero
823 pMsg
= pAdapter
->getMethodCall( "SetProperty" );
824 dbus_message_iter_init_append( pMsg
, &it
);
825 const char *pTimeoutStr
= "DiscoverableTimeout";
826 dbus_message_iter_append_basic( &it
, DBUS_TYPE_STRING
, &pTimeoutStr
);
827 dbus_message_iter_open_container( &it
, DBUS_TYPE_VARIANT
,
828 DBUS_TYPE_UINT32_AS_STRING
, &varIt
);
829 dbus_uint32_t nTimeout
= 0;
830 dbus_message_iter_append_basic( &varIt
, DBUS_TYPE_UINT32
, &nTimeout
);
831 dbus_message_iter_close_container( &it
, &varIt
);
832 dbus_connection_send( pConnection
, pMsg
, nullptr ); // async send - why not ?
833 dbus_message_unref( pMsg
);
835 // set discoverable value
836 pMsg
= pAdapter
->getMethodCall( "SetProperty" );
837 dbus_message_iter_init_append( pMsg
, &it
);
838 const char *pDiscoverableStr
= "Discoverable";
839 dbus_message_iter_append_basic( &it
, DBUS_TYPE_STRING
, &pDiscoverableStr
);
840 dbus_message_iter_open_container( &it
, DBUS_TYPE_VARIANT
,
841 DBUS_TYPE_BOOLEAN_AS_STRING
, &varIt
);
842 dbus_bool_t bValue
= bDiscoverable
;
843 dbus_message_iter_append_basic( &varIt
, DBUS_TYPE_BOOLEAN
, &bValue
);
844 dbus_message_iter_close_container( &it
, &varIt
); // async send - why not ?
845 dbus_connection_send( pConnection
, pMsg
, nullptr );
846 dbus_message_unref( pMsg
);
848 else if (pAdapter
->maInterface
== "org.bluez.Adapter1") // Bluez 5
850 setDBusBooleanProperty(pConnection
, pAdapter
, "Discoverable", bDiscoverable
);
854 static std::unique_ptr
<DBusObject
>
855 registerWithDefaultAdapter( DBusConnection
*pConnection
)
857 std::unique_ptr
<DBusObject
> pService(bluez4GetDefaultService( pConnection
));
860 if( !bluez4RegisterServiceRecord( pConnection
, pService
.get(),
861 bluetooth_service_record
) )
870 static void ProfileUnregisterFunction
871 (DBusConnection
*, void *)
873 // We specifically don't need to do anything here.
876 static DBusHandlerResult ProfileMessageFunction
877 (DBusConnection
*pConnection
, DBusMessage
*pMessage
, void *user_data
)
879 SAL_INFO("sdremote.bluetooth", "ProfileMessageFunction||" << dbus_message_get_interface(pMessage
) << "||" << dbus_message_get_member(pMessage
));
881 if (OString(dbus_message_get_interface(pMessage
)) == "org.bluez.Profile1")
883 if (OString(dbus_message_get_member(pMessage
)) == "Release")
885 return DBUS_HANDLER_RESULT_HANDLED
;
887 else if (OString(dbus_message_get_member(pMessage
)) == "NewConnection")
889 if (!dbus_message_has_signature(pMessage
, "oha{sv}"))
891 SAL_WARN("sdremote.bluetooth", "wrong signature for NewConnection");
895 if (!dbus_message_iter_init(pMessage
, &it
))
896 SAL_WARN( "sdremote.bluetooth", "error init dbus" );
900 dbus_message_iter_get_basic(&it
, &pPath
);
901 SAL_INFO("sdremote.bluetooth", "Adapter path:" << pPath
);
903 if (!dbus_message_iter_next(&it
))
904 SAL_WARN("sdremote.bluetooth", "not enough parameters passed");
906 // DBUS_TYPE_UNIX_FD == 'h' -- doesn't exist in older versions
907 // of dbus (< 1.3?) hence defined manually for now
908 if ('h' == dbus_message_iter_get_arg_type(&it
))
912 dbus_message_iter_get_basic(&it
, &nDescriptor
);
913 std::vector
<Communicator
*>* pCommunicators
= static_cast<std::vector
<Communicator
*>*>(user_data
);
915 // Bluez gives us non-blocking sockets, but our code relies
916 // on blocking behaviour.
917 (void)fcntl(nDescriptor
, F_SETFL
, fcntl(nDescriptor
, F_GETFL
) & ~O_NONBLOCK
);
919 SAL_INFO( "sdremote.bluetooth", "connection accepted " << nDescriptor
);
920 Communicator
* pCommunicator
= new Communicator( std::make_unique
<BufferedStreamSocket
>( nDescriptor
) );
921 pCommunicators
->push_back( pCommunicator
);
922 pCommunicator
->launch();
925 // For some reason an (empty?) reply is expected.
926 DBusMessage
* pRet
= dbus_message_new_method_return(pMessage
);
927 dbus_connection_send(pConnection
, pRet
, nullptr);
928 dbus_message_unref(pRet
);
930 // We could read the remote profile version and features here
931 // (i.e. they are provided as part of the DBusMessage),
932 // however for us they are irrelevant (as our protocol handles
933 // equivalent functionality independently of whether we're on
934 // bluetooth or normal network connection).
935 return DBUS_HANDLER_RESULT_HANDLED
;
938 else if (OString(dbus_message_get_member(pMessage
)) == "RequestDisconnection")
940 return DBUS_HANDLER_RESULT_HANDLED
;
943 SAL_WARN("sdremote.bluetooth", "Couldn't handle message correctly.");
944 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED
;
949 setupBluez5Profile1(DBusConnection
* pConnection
, std::vector
<Communicator
*>* pCommunicators
)
953 SAL_INFO("sdremote.bluetooth", "Attempting to register our org.bluez.Profile1");
954 static DBusObjectPathVTable aVTable
;
955 aVTable
.unregister_function
= ProfileUnregisterFunction
;
956 aVTable
.message_function
= ProfileMessageFunction
;
958 // dbus_connection_try_register_object_path could be used but only exists for
959 // dbus >= 1.2 -- we really shouldn't be trying this twice in any case.
960 // (dbus_connection_try_register_object_path also returns an error with more
961 // information which could be useful for debugging purposes.)
962 bErr
= !dbus_connection_register_object_path(pConnection
, "/org/libreoffice/bluez/profile1", &aVTable
, pCommunicators
);
966 SAL_WARN("sdremote.bluetooth", "Failed to register Bluez 5 Profile1 callback, bluetooth won't work.");
969 dbus_connection_flush( pConnection
);
973 unregisterBluez5Profile(DBusConnection
* pConnection
)
975 DBusMessage
* pMsg
= dbus_message_new_method_call("org.bluez", "/org/bluez",
976 "org.bluez.ProfileManager1", "UnregisterProfile");
978 dbus_message_iter_init_append(pMsg
, &it
);
980 const char *pPath
= "/org/libreoffice/bluez/profile1";
981 dbus_message_iter_append_basic(&it
, DBUS_TYPE_OBJECT_PATH
, &pPath
);
983 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
986 dbus_message_unref(pMsg
);
988 dbus_connection_unregister_object_path( pConnection
, "/org/libreoffice/bluez/profile1");
990 dbus_connection_flush(pConnection
);
994 registerBluez5Profile(DBusConnection
* pConnection
, std::vector
<Communicator
*>* pCommunicators
)
996 setupBluez5Profile1(pConnection
, pCommunicators
);
1001 pMsg
= dbus_message_new_method_call("org.bluez", "/org/bluez",
1002 "org.bluez.ProfileManager1", "RegisterProfile");
1003 dbus_message_iter_init_append(pMsg
, &it
);
1005 const char *pPath
= "/org/libreoffice/bluez/profile1";
1006 dbus_message_iter_append_basic(&it
, DBUS_TYPE_OBJECT_PATH
, &pPath
);
1007 const char *pUUID
= "spp"; // Bluez translates this to 0x1101 for spp
1008 dbus_message_iter_append_basic(&it
, DBUS_TYPE_STRING
, &pUUID
);
1010 DBusMessageIter aOptionsIter
;
1011 dbus_message_iter_open_container(&it
, DBUS_TYPE_ARRAY
, "{sv}", &aOptionsIter
);
1013 DBusMessageIter aEntry
;
1016 dbus_message_iter_open_container(&aOptionsIter
, DBUS_TYPE_DICT_ENTRY
, nullptr, &aEntry
);
1018 const char *pString
= "Name";
1019 dbus_message_iter_append_basic(&aEntry
, DBUS_TYPE_STRING
, &pString
);
1021 const char *pValue
= "LibreOffice Impress Remote";
1022 DBusMessageIter aValue
;
1023 dbus_message_iter_open_container(&aEntry
, DBUS_TYPE_VARIANT
, "s", &aValue
);
1024 dbus_message_iter_append_basic(&aValue
, DBUS_TYPE_STRING
, &pValue
);
1025 dbus_message_iter_close_container(&aEntry
, &aValue
);
1026 dbus_message_iter_close_container(&aOptionsIter
, &aEntry
);
1029 dbus_message_iter_close_container(&it
, &aOptionsIter
);
1031 // Other properties that we could set (but don't, since they appear
1032 // to be useless for us):
1033 // "Service": "0x1101" (not needed, but we used to have it in the manually defined profile).
1034 // "Role": setting this to "server" breaks things, although we think we're a server?
1035 // "Channel": seems to be dealt with automatically (but we used to use 5 in the manual profile).
1037 bool bSuccess
= true;
1039 pMsg
= sendUnrefAndWaitForReply( pConnection
, pMsg
);
1042 dbus_error_init(&aError
);
1043 if (pMsg
&& dbus_set_error_from_message( &aError
, pMsg
))
1046 SAL_WARN("sdremote.bluetooth",
1047 "Failed to register our Profile1 with bluez ProfileManager "
1048 << (aError
.message
? aError
.message
: "<null>"));
1051 dbus_error_free(&aError
);
1053 dbus_message_unref(pMsg
);
1055 dbus_connection_flush(pConnection
);
1060 #endif // LINUX_BLUETOOTH
1062 BluetoothServer::BluetoothServer( std::vector
<Communicator
*>* pCommunicators
)
1063 : meWasDiscoverable( UNKNOWN
),
1064 mpCommunicators( pCommunicators
)
1066 #ifdef LINUX_BLUETOOTH
1067 // D-Bus requires the following in order to be thread-safe (and we
1068 // potentially access D-Bus from different threads in different places of
1070 if (!dbus_threads_init_default()) {
1071 throw std::bad_alloc();
1074 mpImpl
.reset(new BluetoothServer::Impl());
1078 BluetoothServer::~BluetoothServer()
1082 void BluetoothServer::ensureDiscoverable()
1084 #ifdef LINUX_BLUETOOTH
1085 // Push it all across into our mainloop
1088 GSource
*pIdle
= g_idle_source_new();
1089 g_source_set_callback( pIdle
, ensureDiscoverable_cb
, nullptr, nullptr );
1090 g_source_set_priority( pIdle
, G_PRIORITY_DEFAULT
);
1091 g_source_attach( pIdle
, spServer
->mpImpl
->mpContext
);
1092 g_source_unref( pIdle
);
1096 void BluetoothServer::restoreDiscoverable()
1098 #ifdef LINUX_BLUETOOTH
1099 // Push it all across into our mainloop
1102 GSource
*pIdle
= g_idle_source_new();
1103 g_source_set_callback( pIdle
, restoreDiscoverable_cb
, nullptr, nullptr );
1104 g_source_set_priority( pIdle
, G_PRIORITY_DEFAULT_IDLE
);
1105 g_source_attach( pIdle
, spServer
->mpImpl
->mpContext
);
1106 g_source_unref( pIdle
);
1110 void BluetoothServer::doEnsureDiscoverable()
1112 #ifdef LINUX_BLUETOOTH
1113 if (!spServer
->mpImpl
->mpConnection
||
1114 spServer
->meWasDiscoverable
!= UNKNOWN
)
1117 // Find out if we are discoverable already ...
1118 std::unique_ptr
<DBusObject
> pAdapter
= spServer
->mpImpl
->getAdapter();
1122 bool bDiscoverable
= getDiscoverable(spServer
->mpImpl
->mpConnection
, pAdapter
.get() );
1124 spServer
->meWasDiscoverable
= bDiscoverable
? DISCOVERABLE
: NOT_DISCOVERABLE
;
1125 if( !bDiscoverable
)
1126 setDiscoverable( spServer
->mpImpl
->mpConnection
, pAdapter
.get(), true );
1130 void BluetoothServer::doRestoreDiscoverable()
1132 if( spServer
->meWasDiscoverable
== NOT_DISCOVERABLE
)
1134 #ifdef LINUX_BLUETOOTH
1135 std::unique_ptr
<DBusObject
> pAdapter
= spServer
->mpImpl
->getAdapter();
1138 setDiscoverable( spServer
->mpImpl
->mpConnection
, pAdapter
.get(), false );
1141 spServer
->meWasDiscoverable
= UNKNOWN
;
1144 // We have to have all our clients shut otherwise we can't
1145 // re-bind to the same port number it appears.
1146 void BluetoothServer::cleanupCommunicators()
1148 for (auto& rpCommunicator
: *mpCommunicators
)
1149 rpCommunicator
->forceClose();
1150 // the hope is that all the threads then terminate cleanly and
1151 // clean themselves up.
1154 void SAL_CALL
BluetoothServer::run()
1156 SAL_INFO( "sdremote.bluetooth", "BluetoothServer::run called" );
1157 osl::Thread::setName("BluetoothServer");
1158 #ifdef LINUX_BLUETOOTH
1159 DBusConnection
*pConnection
= dbusConnectToNameOnBus();
1163 // For either implementation we need to poll the dbus fd
1166 if( dbus_connection_get_unix_fd( pConnection
, &fd
) && fd
>= 0 )
1169 aDBusFD
.events
= G_IO_IN
| G_IO_PRI
;
1170 g_main_context_add_poll( mpImpl
->mpContext
, &aDBusFD
, G_PRIORITY_DEFAULT
);
1173 SAL_WARN( "sdremote.bluetooth", "failed to poll for incoming dbus signals" );
1175 if (isBluez5Available(pConnection
))
1177 SAL_INFO("sdremote.bluetooth", "Using Bluez 5");
1178 registerBluez5Profile(pConnection
, mpCommunicators
);
1179 mpImpl
->mpConnection
= pConnection
;
1180 mpImpl
->maBluezVersion
= Impl::BluezVersion::BLUEZ5
;
1182 // We don't need to listen to adapter changes anymore -- profile
1183 // registration is done globally for the entirety of bluez, so we only
1184 // need adapters when setting discoverability, which can be done
1185 // dynamically without the need to listen for changes.
1187 // TODO: exit on SD deinit
1188 // Probably best to do that in SdModule::~SdModule?
1191 aDBusFD
.revents
= 0;
1192 g_main_context_iteration( mpImpl
->mpContext
, TRUE
);
1193 if( aDBusFD
.revents
)
1195 dbus_connection_read_write( pConnection
, 0 );
1196 while (DBUS_DISPATCH_DATA_REMAINS
== dbus_connection_get_dispatch_status( pConnection
))
1197 dbus_connection_dispatch( pConnection
);
1200 // silence Clang -Wunreachable-code after loop (TODO: proper
1203 unregisterBluez5Profile( pConnection
);
1204 g_main_context_unref( mpImpl
->mpContext
);
1205 mpImpl
->mpConnection
= nullptr;
1206 mpImpl
->mpContext
= nullptr;
1210 // Otherwise we could be on Bluez 4 and continue as usual.
1211 mpImpl
->maBluezVersion
= Impl::BluezVersion::BLUEZ4
;
1213 // Try to setup the default adapter, otherwise wait for add/remove signal
1214 mpImpl
->mpService
= registerWithDefaultAdapter( pConnection
);
1215 // listen for connection state and power changes - we need to close
1216 // and re-create our socket code on suspend / resume, enable/disable
1218 dbus_error_init( &aError
);
1219 dbus_bus_add_match( pConnection
, "type='signal',interface='org.bluez.Manager'", &aError
);
1220 dbus_connection_flush( pConnection
);
1222 // Try to setup the default adapter, otherwise wait for add/remove signal
1223 mpImpl
->mpService
= registerWithDefaultAdapter( pConnection
);
1225 // poll on our bluetooth socket - if we can.
1227 if( mpImpl
->mpService
)
1228 bluezCreateAttachListeningSocket( mpImpl
->mpContext
, &aSocketFD
);
1230 mpImpl
->mpConnection
= pConnection
;
1234 aDBusFD
.revents
= 0;
1235 aSocketFD
.revents
= 0;
1236 g_main_context_iteration( mpImpl
->mpContext
, TRUE
);
1238 SAL_INFO( "sdremote.bluetooth", "main-loop spin "
1239 << aDBusFD
.revents
<< " " << aSocketFD
.revents
);
1240 if( aDBusFD
.revents
)
1242 dbus_connection_read_write( pConnection
, 0 );
1243 DBusMessage
*pMsg
= dbus_connection_pop_message( pConnection
);
1246 if( dbus_message_is_signal( pMsg
, "org.bluez.Manager", "AdapterRemoved" ) )
1248 SAL_WARN( "sdremote.bluetooth", "lost adapter - cleaning up sockets" );
1249 bluezDetachCloseSocket( mpImpl
->mpContext
, &aSocketFD
);
1250 cleanupCommunicators();
1252 else if( dbus_message_is_signal( pMsg
, "org.bluez.Manager", "AdapterAdded" ) ||
1253 dbus_message_is_signal( pMsg
, "org.bluez.Manager", "DefaultAdapterChanged" ) )
1255 SAL_WARN( "sdremote.bluetooth", "gained adapter - re-generating sockets" );
1256 bluezDetachCloseSocket( mpImpl
->mpContext
, &aSocketFD
);
1257 cleanupCommunicators();
1258 mpImpl
->mpService
= registerWithDefaultAdapter( pConnection
);
1259 if( mpImpl
->mpService
)
1260 bluezCreateAttachListeningSocket( mpImpl
->mpContext
, &aSocketFD
);
1263 SAL_INFO( "sdremote.bluetooth", "unknown incoming dbus message, "
1264 " type: " << dbus_message_get_type( pMsg
)
1265 << " path: '" << dbus_message_get_path( pMsg
)
1266 << "' interface: '" << dbus_message_get_interface( pMsg
)
1267 << "' member: '" << dbus_message_get_member( pMsg
) );
1269 dbus_message_unref( pMsg
);
1272 if( aSocketFD
.revents
)
1274 sockaddr_rc aRemoteAddr
;
1275 socklen_t aRemoteAddrLen
= sizeof(aRemoteAddr
);
1278 SAL_INFO( "sdremote.bluetooth", "performing accept" );
1279 if ( ( nClient
= accept( aSocketFD
.fd
, reinterpret_cast<sockaddr
*>(&aRemoteAddr
), &aRemoteAddrLen
)) < 0 &&
1282 SAL_WARN( "sdremote.bluetooth", "accept failed with errno " << errno
);
1284 SAL_INFO( "sdremote.bluetooth", "connection accepted " << nClient
);
1285 Communicator
* pCommunicator
= new Communicator( std::make_unique
<BufferedStreamSocket
>( nClient
) );
1286 mpCommunicators
->push_back( pCommunicator
);
1287 pCommunicator
->launch();
1291 // silence Clang -Wunreachable-code after loop (TODO: proper fix?)
1294 unregisterBluez5Profile( pConnection
);
1295 g_main_context_unref( mpImpl
->mpContext
);
1296 mpImpl
->mpConnection
= nullptr;
1297 mpImpl
->mpContext
= nullptr;
1299 #elif defined(_WIN32)
1300 WORD wVersionRequested
;
1303 wVersionRequested
= MAKEWORD(2, 2);
1305 if ( WSAStartup(wVersionRequested
, &wsaData
) )
1307 return; // winsock dll couldn't be loaded
1310 int aSocket
= socket( AF_BTH
, SOCK_STREAM
, BTHPROTO_RFCOMM
);
1317 aAddr
.addressFamily
= AF_BTH
;
1319 aAddr
.serviceClassId
= GUID_NULL
;
1320 aAddr
.port
= BT_PORT_ANY
; // Select any free socket.
1321 if ( bind( aSocket
, reinterpret_cast<SOCKADDR
*>(&aAddr
), sizeof(aAddr
) ) == SOCKET_ERROR
)
1323 closesocket( aSocket
);
1329 int aNameSize
= sizeof(aName
);
1330 getsockname( aSocket
, reinterpret_cast<SOCKADDR
*>(&aName
), &aNameSize
); // Retrieve the local address and port
1332 CSADDR_INFO aAddrInfo
;
1333 memset( &aAddrInfo
, 0, sizeof(aAddrInfo
) );
1334 aAddrInfo
.LocalAddr
.lpSockaddr
= reinterpret_cast<SOCKADDR
*>(&aName
);
1335 aAddrInfo
.LocalAddr
.iSockaddrLength
= sizeof( SOCKADDR_BTH
);
1336 aAddrInfo
.iSocketType
= SOCK_STREAM
;
1337 aAddrInfo
.iProtocol
= BTHPROTO_RFCOMM
;
1339 // To be used for setting a custom UUID once available.
1341 // uuid.Data1 = 0x00001101;
1342 // memset( &uuid, 0x1000 + UUID*2^96, sizeof( GUID ) );
1344 // uuid.Data3 = 0x1000;
1345 // ULONGLONG aData4 = 0x800000805F9B34FB;
1346 // memcpy( uuid.Data4, &aData4, sizeof(uuid.Data4) );
1348 WSAQUERYSETW aRecord
;
1349 memset( &aRecord
, 0, sizeof(aRecord
));
1350 aRecord
.dwSize
= sizeof(aRecord
);
1351 aRecord
.lpszServiceInstanceName
= const_cast<wchar_t *>(
1352 L
"LibreOffice Impress Remote Control");
1353 aRecord
.lpszComment
= const_cast<wchar_t *>(
1354 L
"Remote control of presentations over bluetooth.");
1355 aRecord
.lpServiceClassId
= const_cast<LPGUID
>(&SerialPortServiceClass_UUID
);
1356 aRecord
.dwNameSpace
= NS_BTH
;
1357 aRecord
.dwNumberOfCsAddrs
= 1;
1358 aRecord
.lpcsaBuffer
= &aAddrInfo
;
1359 if (WSASetServiceW( &aRecord
, RNRSERVICE_REGISTER
, 0 ) == SOCKET_ERROR
)
1361 closesocket( aSocket
);
1366 if ( listen( aSocket
, 1 ) == SOCKET_ERROR
)
1368 closesocket( aSocket
);
1373 SOCKADDR_BTH aRemoteAddr
;
1374 int aRemoteAddrLen
= sizeof(aRemoteAddr
);
1378 if ( (socket
= accept(aSocket
, reinterpret_cast<sockaddr
*>(&aRemoteAddr
), &aRemoteAddrLen
)) == INVALID_SOCKET
)
1380 closesocket( aSocket
);
1384 Communicator
* pCommunicator
= new Communicator( std::make_unique
<BufferedStreamSocket
>( socket
) );
1385 mpCommunicators
->push_back( pCommunicator
);
1386 pCommunicator
->launch();
1390 #elif defined(MACOSX)
1391 // Build up dictionary at run-time instead of bothering with a
1392 // .plist file, using the Objective-C API
1394 // Compare to BluetoothServiceRecord.hxx
1396 NSAutoreleasePool
* pool
= [[NSAutoreleasePool alloc
] init
];
1398 NSDictionary
*dict
=
1399 [NSDictionary dictionaryWithObjectsAndKeys
:
1401 // Service class ID list
1402 [NSArray arrayWithObject
:
1403 [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16ServiceClassSerialPort
]],
1404 @
"0001 - ServiceClassIDList",
1406 // Protocol descriptor list
1407 [NSArray arrayWithObjects
:
1408 [NSArray arrayWithObject
: [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16L2CAP
]],
1409 [NSArray arrayWithObjects
:
1410 [IOBluetoothSDPUUID uuid16
: kBluetoothL2CAPPSMRFCOMM
],
1411 [NSDictionary dictionaryWithObjectsAndKeys
:
1412 [NSNumber numberWithInt
: 1],
1414 [NSNumber numberWithInt
: 1],
1416 [NSNumber numberWithInt
: 5], // RFCOMM port number, will be replaced if necessary automatically
1417 @
"DataElementValue",
1421 @
"0004 - Protocol descriptor list",
1423 // Browse group list
1424 [NSArray arrayWithObject
:
1425 [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16ServiceClassPublicBrowseGroup
]],
1426 @
"0005 - BrowseGroupList",
1428 // Language base attribute ID list
1429 [NSArray arrayWithObjects
:
1430 [NSData dataWithBytes
: "en" length
: 2],
1431 [NSDictionary dictionaryWithObjectsAndKeys
:
1432 [NSNumber numberWithInt
: 2],
1434 [NSNumber numberWithInt
: 1],
1436 [NSNumber numberWithInt
: 0x006a], // encoding
1437 @
"DataElementValue",
1439 [NSDictionary dictionaryWithObjectsAndKeys
:
1440 [NSNumber numberWithInt
: 2],
1442 [NSNumber numberWithInt
: 1],
1444 [NSNumber numberWithInt
: 0x0100], // offset
1445 @
"DataElementValue",
1448 @
"0006 - LanguageBaseAttributeIDList",
1450 // Bluetooth profile descriptor list
1451 [NSArray arrayWithObject
:
1452 [NSArray arrayWithObjects
:
1453 [IOBluetoothSDPUUID uuid16
: kBluetoothSDPUUID16ServiceClassSerialPort
],
1454 [NSDictionary dictionaryWithObjectsAndKeys
:
1455 [NSNumber numberWithInt
: 2],
1457 [NSNumber numberWithInt
: 1],
1459 [NSNumber numberWithInt
: 0x0100], // version number ?
1460 @
"DataElementValue",
1463 @
"0009 - BluetoothProfileDescriptorList",
1465 // Attributes pointed to by the LanguageBaseAttributeIDList
1466 @
"LibreOffice Impress Remote Control",
1467 @
"0100 - ServiceName",
1468 @
"The Document Foundation",
1469 @
"0102 - ProviderName",
1473 IOBluetoothSDPServiceRecordRef serviceRecordRef
;
1474 SAL_WNODEPRECATED_DECLARATIONS_PUSH
//TODO: 10.9 IOBluetoothAddServiceDict
1475 IOReturn rc
= IOBluetoothAddServiceDict(reinterpret_cast<CFDictionaryRef
>(dict
), &serviceRecordRef
);
1476 SAL_WNODEPRECATED_DECLARATIONS_POP
1478 SAL_INFO("sdremote.bluetooth", "IOBluetoothAddServiceDict returned " << rc
);
1480 if (rc
== kIOReturnSuccess
)
1482 IOBluetoothSDPServiceRecord
*serviceRecord
=
1483 [IOBluetoothSDPServiceRecord withSDPServiceRecordRef
: serviceRecordRef
];
1485 BluetoothRFCOMMChannelID channelID
;
1486 [serviceRecord getRFCOMMChannelID
: &channelID
];
1488 BluetoothSDPServiceRecordHandle serviceRecordHandle
;
1489 [serviceRecord getServiceRecordHandle
: &serviceRecordHandle
];
1491 // Register callback for incoming connections
1492 IOBluetoothRegisterForFilteredRFCOMMChannelOpenNotifications(
1496 kIOBluetoothUserNotificationChannelDirectionIncoming
);
1498 [serviceRecord release
];
1503 (void) mpCommunicators
;
1505 (void) mpCommunicators
; // avoid warnings about unused member
1509 BluetoothServer
*sd::BluetoothServer::spServer
= nullptr;
1511 void BluetoothServer::setup( std::vector
<Communicator
*>* pCommunicators
)
1516 spServer
= new BluetoothServer( pCommunicators
);
1520 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */