21 #include <dbus/dbus.h>
25 #include <sys/socket.h>
26 #include <bluetooth/bluetooth.h>
27 #include <bluetooth/rfcomm.h>
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>
68 DBusObject(
const char *pBusName,
const char *pPath,
const char *pInterface )
69 : maBusName( pBusName ),
maPath( pPath ), maInterface( pInterface ) { }
71 DBusMessage *getMethodCall(
const char *
pName )
73 return dbus_message_new_method_call( maBusName.getStr(),
maPath.getStr(),
74 maInterface.getStr(),
pName );
76 std::unique_ptr<DBusObject> cloneForInterface(
const char *pInterface )
78 std::unique_ptr<DBusObject>
pObject(
new DBusObject());
82 pObject->maInterface = pInterface;
90static std::unique_ptr<DBusObject> getBluez5Adapter(DBusConnection *pConnection);
92struct sd::BluetoothServer::Impl {
95 DBusConnection *mpConnection;
96 std::unique_ptr<DBusObject> mpService;
97 enum class BluezVersion { BLUEZ4, BLUEZ5,
UNKNOWN };
98 BluezVersion maBluezVersion;
102 , mpConnection( nullptr )
103 , maBluezVersion( BluezVersion::
UNKNOWN )
106 std::unique_ptr<DBusObject> getAdapter()
110 return mpService->cloneForInterface(
"org.bluez.Adapter" );
112 else if (
spServer->mpImpl->maBluezVersion == BluezVersion::BLUEZ5)
114 return getBluez5Adapter(mpConnection);
123static DBusConnection *
124dbusConnectToNameOnBus()
127 DBusConnection *pConnection;
129 dbus_error_init( &aError );
131 pConnection = dbus_bus_get( DBUS_BUS_SYSTEM, &aError );
132 if( !pConnection || dbus_error_is_set( &aError ))
134 SAL_WARN(
"sdremote.bluetooth",
"failed to get dbus system bus: " << aError.message );
135 dbus_error_free( &aError );
143sendUnrefAndWaitForReply( DBusConnection *pConnection, DBusMessage *pMsg )
145 DBusPendingCall *pPending =
nullptr;
147 if( !pMsg || !dbus_connection_send_with_reply( pConnection, pMsg, &pPending,
150 SAL_WARN(
"sdremote.bluetooth",
"Memory allocation failed on message send" );
151 dbus_message_unref( pMsg );
154 dbus_connection_flush( pConnection );
155 dbus_message_unref( pMsg );
157 dbus_pending_call_block( pPending );
159 pMsg = dbus_pending_call_steal_reply( pPending );
161 SAL_WARN(
"sdremote.bluetooth",
"no valid reply / timeout" );
163 dbus_pending_call_unref( pPending );
168isBluez5Available(DBusConnection *pConnection)
176 pMsg = DBusObject(
"org.bluez",
"/",
"org.freedesktop.DBus.ObjectManager" ).getMethodCall(
"GetManagedObjects" );
179 SAL_INFO(
"sdremote.bluetooth",
"No GetManagedObjects call created");
183 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
186 SAL_INFO(
"sdremote.bluetooth",
"No reply received");
192 if (dbus_message_get_error_name( pMsg ))
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");
199 SAL_INFO(
"sdremote.bluetooth",
"GetManagedObjects call seems to have succeeded -- we must be on Bluez 5");
200 dbus_message_unref(pMsg);
204static std::unique_ptr<DBusObject>
205getBluez5Adapter(DBusConnection *pConnection)
210 pMsg = DBusObject(
"org.bluez",
"/",
"org.freedesktop.DBus.ObjectManager" ).getMethodCall(
"GetManagedObjects" );
214 const gchar*
const pInterfaceType =
"org.bluez.Adapter1";
216 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
218 DBusMessageIter aObjectIterator;
219 if (pMsg && dbus_message_iter_init(pMsg, &aObjectIterator))
221 if (DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&aObjectIterator))
223 DBusMessageIter aObject;
224 dbus_message_iter_recurse(&aObjectIterator, &aObject);
227 if (DBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&aObject))
229 DBusMessageIter aContainerIter;
230 dbus_message_iter_recurse(&aObject, &aContainerIter);
231 char *pPath =
nullptr;
234 if (DBUS_TYPE_OBJECT_PATH == dbus_message_iter_get_arg_type(&aContainerIter))
236 dbus_message_iter_get_basic(&aContainerIter, &pPath);
237 SAL_INFO(
"sdremote.bluetooth",
"Something retrieved: '"
240 else if (DBUS_TYPE_ARRAY == dbus_message_iter_get_arg_type(&aContainerIter))
242 DBusMessageIter aInnerIter;
243 dbus_message_iter_recurse(&aContainerIter, &aInnerIter);
246 if (DBUS_TYPE_DICT_ENTRY == dbus_message_iter_get_arg_type(&aInnerIter))
248 DBusMessageIter aInnerInnerIter;
249 dbus_message_iter_recurse(&aInnerIter, &aInnerInnerIter);
252 if (DBUS_TYPE_STRING == dbus_message_iter_get_arg_type(&aInnerInnerIter))
256 dbus_message_iter_get_basic(&aInnerInnerIter, &pMessage);
257 if (pMessage == std::string_view(
"org.bluez.Adapter1"))
259 dbus_message_unref(pMsg);
262 return std::make_unique<DBusObject>(
"org.bluez", pPath, pInterfaceType );
268 while (dbus_message_iter_next(&aInnerInnerIter));
271 while (dbus_message_iter_next(&aInnerIter));
274 while (dbus_message_iter_next(&aContainerIter));
277 while (dbus_message_iter_next(&aObject));
279 dbus_message_unref(pMsg);
286bluez4GetDefaultService( DBusConnection *pConnection )
290 const gchar*
const pInterfaceType =
"org.bluez.Service";
295 pMsg = DBusObject(
"org.bluez",
"/",
"org.bluez.Manager" ).getMethodCall(
"DefaultAdapter" );
299 SAL_WARN(
"sdremote.bluetooth",
"Couldn't retrieve DBusObject for DefaultAdapter");
303 SAL_INFO(
"sdremote.bluetooth",
"successfully retrieved org.bluez.Manager.DefaultAdapter, attempting to use.");
304 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
306 if(!pMsg || !dbus_message_iter_init( pMsg, &it ) )
312 if( DBUS_TYPE_OBJECT_PATH == dbus_message_iter_get_arg_type( &it ) )
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 );
323 else if ( DBUS_TYPE_STRING == dbus_message_iter_get_arg_type( &it ) )
325 const char *pMessage =
nullptr;
326 dbus_message_iter_get_basic( &it, &pMessage );
327 SAL_INFO(
"sdremote.bluetooth",
"Error message: '"
328 << pMessage <<
"' '" << pInterfaceType <<
"'" );
332 SAL_INFO(
"sdremote.bluetooth",
"invalid type of reply to DefaultAdapter: '"
333 <<
static_cast<char>(dbus_message_iter_get_arg_type( &it )) <<
"'" );
335 dbus_message_unref(pMsg);
340bluez4RegisterServiceRecord( DBusConnection *pConnection, DBusObject *pAdapter,
341 const char *pServiceRecord )
346 pMsg = pAdapter->getMethodCall(
"AddRecord" );
347 dbus_message_iter_init_append( pMsg, &it );
348 dbus_message_iter_append_basic( &it, DBUS_TYPE_STRING, &pServiceRecord );
350 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
352 if( !pMsg || !dbus_message_iter_init( pMsg, &it ) ||
353 dbus_message_iter_get_arg_type( &it ) != DBUS_TYPE_UINT32 )
355 SAL_WARN(
"sdremote.bluetooth",
"SDP registration failed" );
366bluezCreateAttachListeningSocket( GMainContext *pContext, GPollFD *pSocketFD )
372 if( ( nSocket = socket( AF_BLUETOOTH, SOCK_STREAM, BTPROTO_RFCOMM ) ) < 0 )
374 SAL_WARN(
"sdremote.bluetooth",
"failed to open bluetooth socket with error " << nSocket );
384 memset( &aAddr, 0,
sizeof( aAddr ) );
385 aAddr.rc_family = AF_BLUETOOTH;
386 aAddr.rc_channel = 5;
389 if ( ( a = bind( nSocket,
reinterpret_cast<sockaddr*
>(&aAddr),
sizeof(aAddr) ) ) < 0 ) {
390 SAL_WARN(
"sdremote.bluetooth",
"bind failed with error" << a );
395 if ( ( a = listen( nSocket, 1 ) ) < 0 )
397 SAL_WARN(
"sdremote.bluetooth",
"listen failed with error" << a );
403 if( fcntl( nSocket, F_SETFL, O_NONBLOCK) < 0 )
409 pSocketFD->fd = nSocket;
410 pSocketFD->events = G_IO_IN | G_IO_PRI;
411 pSocketFD->revents = 0;
413 g_main_context_add_poll( pContext, pSocketFD, G_PRIORITY_DEFAULT );
417bluezDetachCloseSocket( GMainContext *pContext, GPollFD *pSocketFD )
419 if( pSocketFD->fd >= 0 )
421 close( pSocketFD->fd );
422 g_main_context_remove_poll( pContext, pSocketFD );
431OSXBluetoothWrapper::OSXBluetoothWrapper( IOBluetoothRFCOMMChannel* channel ) :
439 mnMTU = [channel getMTU];
441 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::OSXBluetoothWrapper(): mnMTU=" << mnMTU );
444sal_Int32 OSXBluetoothWrapper::readLine( OString& aLine )
446 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::readLine()" );
451 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::readLine: entering mutex" );
452 ::osl::MutexGuard aQueueGuard(
mMutex );
453 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::readLine: entered mutex" );
458 std::ostringstream s;
461 for (
unsigned char *p =
reinterpret_cast<unsigned char *
>(
mBuffer.data()); p !=
reinterpret_cast<unsigned char *
>(
mBuffer.data()) +
mBuffer.size(); p++)
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;
471 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::readLine mBuffer: \"" << s.str() <<
"\"" );
475 std::vector<char>::iterator aIt;
479 sal_uInt64 aLocation = aIt -
mBuffer.begin();
481 aLine = OString( &(*
mBuffer.begin()), aLocation );
486 SAL_INFO(
"sdremote.bluetooth",
" returning, got \"" << OStringToOUString( aLine, RTL_TEXTENCODING_UTF8 ) <<
"\"" );
487 return aLine.getLength() + 1;
491 SAL_INFO(
"sdremote.bluetooth",
" resetting mHaveBytes" );
493 SAL_INFO(
"sdremote.bluetooth",
" leaving mutex" );
496 SAL_INFO(
"sdremote.bluetooth",
" waiting for mHaveBytes" );
498 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::readLine: got mHaveBytes" );
504 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::write(" << pBuffer <<
", " << n <<
") mpChannel=" <<
mpChannel );
506 char const * ptr =
static_cast<char const *
>(pBuffer);
507 sal_uInt32 nBytesWritten = 0;
512 while( nBytesWritten < n )
514 int toWrite =
n - nBytesWritten;
516 if ( [
mpChannel writeSync:
const_cast<char *
>(ptr)
length:toWrite] != kIOReturnSuccess )
518 SAL_INFO(
"sdremote.bluetooth",
" [mpChannel writeSync:" <<
static_cast<void const *
>(ptr) <<
" length:" << toWrite <<
"] returned error, total written " << nBytesWritten );
519 return nBytesWritten;
522 nBytesWritten += toWrite;
524 SAL_INFO(
"sdremote.bluetooth",
" total written " << nBytesWritten );
525 return nBytesWritten;
530 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::appendData(" << pBuffer <<
", " << len <<
")" );
534 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::appendData: entering mutex" );
535 ::osl::MutexGuard aQueueGuard(
mMutex );
536 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::appendData: entered mutex" );
538 static_cast<char*
>(pBuffer),
static_cast<char *
>(pBuffer)+len);
539 SAL_INFO(
"sdremote.bluetooth",
" setting mHaveBytes" );
541 SAL_INFO(
"sdremote.bluetooth",
" leaving mutex" );
547 SAL_INFO(
"sdremote.bluetooth",
"OSXBluetoothWrapper::channelClosed()" );
552void incomingCallback(
void *userRefCon,
553 IOBluetoothUserNotificationRef,
554 IOBluetoothObjectRef objectRef )
556 SAL_INFO(
"sdremote.bluetooth",
"incomingCallback()" );
560 IOBluetoothRFCOMMChannel* channel = [IOBluetoothRFCOMMChannel withRFCOMMChannelRef:
reinterpret_cast<IOBluetoothRFCOMMChannelRef
>(objectRef)];
564 pServer->addCommunicator( pCommunicator );
567 [channel setDelegate: delegate];
573void BluetoothServer::addCommunicator(
Communicator* pCommunicator )
580#ifdef LINUX_BLUETOOTH
583 static gboolean ensureDiscoverable_cb(gpointer)
588 static gboolean restoreDiscoverable_cb(gpointer)
602getBluez4BooleanProperty( DBusConnection *pConnection, DBusObject *pAdapter,
603 const char *pPropertyName,
bool *pBoolean )
611 pMsg = sendUnrefAndWaitForReply( pConnection,
612 pAdapter->getMethodCall(
"GetProperties" ) );
615 if( !pMsg || !dbus_message_iter_init( pMsg, &it ) )
617 SAL_WARN(
"sdremote.bluetooth",
"no valid reply / timeout" );
621 if( DBUS_TYPE_ARRAY != dbus_message_iter_get_arg_type( &it ) )
623 SAL_WARN(
"sdremote.bluetooth",
"no valid reply / timeout" );
627 DBusMessageIter arrayIt;
628 dbus_message_iter_recurse( &it, &arrayIt );
630 while( dbus_message_iter_get_arg_type( &arrayIt ) == DBUS_TYPE_DICT_ENTRY )
632 DBusMessageIter dictIt;
633 dbus_message_iter_recurse( &arrayIt, &dictIt );
635 const char *
pName =
nullptr;
636 if( dbus_message_iter_get_arg_type( &dictIt ) == DBUS_TYPE_STRING )
638 dbus_message_iter_get_basic( &dictIt, &
pName );
639 if(
pName !=
nullptr && !strcmp(
pName, pPropertyName ) )
641 SAL_INFO(
"sdremote.bluetooth",
"hit " << pPropertyName <<
" property" );
642 dbus_message_iter_next( &dictIt );
643 dbus_bool_t bBool =
false;
645 if( dbus_message_iter_get_arg_type( &dictIt ) == DBUS_TYPE_VARIANT )
647 DBusMessageIter variantIt;
648 dbus_message_iter_recurse( &dictIt, &variantIt );
650 if( dbus_message_iter_get_arg_type( &variantIt ) == DBUS_TYPE_BOOLEAN )
652 dbus_message_iter_get_basic( &variantIt, &bBool );
653 SAL_INFO(
"sdremote.bluetooth",
"" << pPropertyName <<
" is " << bBool );
658 SAL_WARN(
"sdremote.bluetooth",
"" << pPropertyName <<
" type " <<
659 dbus_message_iter_get_arg_type( &variantIt ) );
662 SAL_WARN(
"sdremote.bluetooth",
"variant type ? " <<
663 dbus_message_iter_get_arg_type( &dictIt ) );
668 SAL_INFO(
"sdremote.bluetooth",
"property '" << pStr <<
"'" );
672 SAL_WARN(
"sdremote.bluetooth",
"unexpected property key type "
673 << dbus_message_iter_get_arg_type( &dictIt ) );
674 dbus_message_iter_next( &arrayIt );
676 dbus_message_unref( pMsg );
686getDBusBooleanProperty( DBusConnection *pConnection, DBusObject *pAdapter,
687 const char *pPropertyName,
bool *pBoolean )
694 std::unique_ptr< DBusObject > pProperties (
695 pAdapter->cloneForInterface(
"org.freedesktop.DBus.Properties" ) );
697 DBusMessage *pMsg = pProperties->getMethodCall(
"Get" );
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 );
707 if( !pMsg || !dbus_message_iter_init( pMsg, &it ) )
709 SAL_WARN(
"sdremote.bluetooth",
"no valid reply / timeout" );
713 if( DBUS_TYPE_VARIANT != dbus_message_iter_get_arg_type( &it ) )
715 SAL_WARN(
"sdremote.bluetooth",
"invalid return type" );
719 DBusMessageIter variantIt;
720 dbus_message_iter_recurse( &it, &variantIt );
722 if( dbus_message_iter_get_arg_type( &variantIt ) == DBUS_TYPE_BOOLEAN )
724 dbus_bool_t bBool =
false;
725 dbus_message_iter_get_basic( &variantIt, &bBool );
726 SAL_INFO(
"sdremote.bluetooth",
"" << pPropertyName <<
" is " << bBool );
732 SAL_WARN(
"sdremote.bluetooth",
"" << pPropertyName <<
" type " <<
733 dbus_message_iter_get_arg_type( &variantIt ) );
736 const char* pError = dbus_message_get_error_name( pMsg );
740 "Get failed for " << pPropertyName <<
" on " <<
741 pAdapter->maPath <<
" with error: " << pError );
744 dbus_message_unref( pMsg );
750setDBusBooleanProperty( DBusConnection *pConnection, DBusObject *pAdapter,
751 const char *pPropertyName,
bool bBoolean )
755 std::unique_ptr< DBusObject > pProperties(
756 pAdapter->cloneForInterface(
"org.freedesktop.DBus.Properties" ) );
758 DBusMessage *pMsg = pProperties->getMethodCall(
"Set" );
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 );
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 );
775 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
779 SAL_WARN(
"sdremote.bluetooth",
"no valid reply / timeout" );
783 const char* pError = dbus_message_get_error_name( pMsg );
787 "Set failed for " << pPropertyName <<
" on " <<
788 pAdapter->maPath <<
" with error: " << pError );
790 dbus_message_unref( pMsg );
795getDiscoverable( DBusConnection *pConnection, DBusObject *pAdapter )
797 if (pAdapter->maInterface ==
"org.bluez.Adapter")
800 if( getBluez4BooleanProperty(pConnection, pAdapter,
"Discoverable", &bDiscoverable ) )
801 return bDiscoverable;
803 else if (pAdapter->maInterface ==
"org.bluez.Adapter1")
806 if ( getDBusBooleanProperty(pConnection, pAdapter,
"Discoverable", &bDiscoverable ) )
807 return bDiscoverable;
813setDiscoverable( DBusConnection *pConnection, DBusObject *pAdapter,
bool bDiscoverable )
815 SAL_INFO(
"sdremote.bluetooth",
"setDiscoverable to " << bDiscoverable );
817 if (pAdapter->maInterface ==
"org.bluez.Adapter")
819 bool bPowered =
false;
820 if( !getBluez4BooleanProperty( pConnection, pAdapter,
"Powered", &bPowered ) || !bPowered )
824 DBusMessageIter it, varIt;
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 );
837 dbus_message_unref( pMsg );
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 );
849 dbus_connection_send( pConnection, pMsg,
nullptr );
850 dbus_message_unref( pMsg );
852 else if (pAdapter->maInterface ==
"org.bluez.Adapter1")
854 setDBusBooleanProperty(pConnection, pAdapter,
"Discoverable", bDiscoverable );
858static std::unique_ptr<DBusObject>
859registerWithDefaultAdapter( DBusConnection *pConnection )
861 std::unique_ptr<DBusObject> pService(bluez4GetDefaultService( pConnection ));
864 if( !bluez4RegisterServiceRecord( pConnection, pService.get(),
874static void ProfileUnregisterFunction
875(DBusConnection *,
void *)
880static DBusHandlerResult ProfileMessageFunction
881(DBusConnection *pConnection, DBusMessage *pMessage,
void *user_data)
883 SAL_INFO(
"sdremote.bluetooth",
"ProfileMessageFunction||" << dbus_message_get_interface(pMessage) <<
"||" << dbus_message_get_member(pMessage));
885 if (dbus_message_get_interface(pMessage) == std::string_view(
"org.bluez.Profile1"))
887 if (dbus_message_get_member(pMessage) == std::string_view(
"Release"))
889 return DBUS_HANDLER_RESULT_HANDLED;
891 else if (dbus_message_get_member(pMessage) == std::string_view(
"NewConnection"))
893 if (!dbus_message_has_signature(pMessage,
"oha{sv}"))
895 SAL_WARN(
"sdremote.bluetooth",
"wrong signature for NewConnection");
899 if (!dbus_message_iter_init(pMessage, &it))
900 SAL_WARN(
"sdremote.bluetooth",
"error init dbus" );
904 dbus_message_iter_get_basic(&it, &pPath);
905 SAL_INFO(
"sdremote.bluetooth",
"Adapter path:" << pPath);
907 if (!dbus_message_iter_next(&it))
908 SAL_WARN(
"sdremote.bluetooth",
"not enough parameters passed");
912 if (
'h' == dbus_message_iter_get_arg_type(&it))
916 dbus_message_iter_get_basic(&it, &nDescriptor);
917 std::vector<Communicator*>* pCommunicators =
static_cast<std::vector<Communicator*>*
>(user_data);
921 (void)fcntl(nDescriptor, F_SETFL, fcntl(nDescriptor, F_GETFL) & ~O_NONBLOCK);
923 SAL_INFO(
"sdremote.bluetooth",
"connection accepted " << nDescriptor);
925 pCommunicators->push_back( pCommunicator );
930 DBusMessage* pRet = dbus_message_new_method_return(pMessage);
931 dbus_connection_send(pConnection, pRet,
nullptr);
932 dbus_message_unref(pRet);
939 return DBUS_HANDLER_RESULT_HANDLED;
942 else if (dbus_message_get_member(pMessage) == std::string_view(
"RequestDisconnection"))
944 return DBUS_HANDLER_RESULT_HANDLED;
947 SAL_WARN(
"sdremote.bluetooth",
"Couldn't handle message correctly.");
948 return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
953setupBluez5Profile1(DBusConnection* pConnection, std::vector<Communicator*>* pCommunicators)
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;
966 bErr = !dbus_connection_register_object_path(pConnection,
"/org/libreoffice/bluez/profile1", &aVTable, pCommunicators);
970 SAL_WARN(
"sdremote.bluetooth",
"Failed to register Bluez 5 Profile1 callback, bluetooth won't work.");
973 dbus_connection_flush( pConnection );
977unregisterBluez5Profile(DBusConnection* pConnection)
979 DBusMessage* pMsg = dbus_message_new_method_call(
"org.bluez",
"/org/bluez",
980 "org.bluez.ProfileManager1",
"UnregisterProfile");
982 dbus_message_iter_init_append(pMsg, &it);
984 const char *pPath =
"/org/libreoffice/bluez/profile1";
985 dbus_message_iter_append_basic(&it, DBUS_TYPE_OBJECT_PATH, &pPath);
987 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
990 dbus_message_unref(pMsg);
992 dbus_connection_unregister_object_path( pConnection,
"/org/libreoffice/bluez/profile1");
994 dbus_connection_flush(pConnection);
998registerBluez5Profile(DBusConnection* pConnection, std::vector<Communicator*>* pCommunicators)
1000 setupBluez5Profile1(pConnection, pCommunicators);
1005 pMsg = dbus_message_new_method_call(
"org.bluez",
"/org/bluez",
1006 "org.bluez.ProfileManager1",
"RegisterProfile");
1007 dbus_message_iter_init_append(pMsg, &it);
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";
1012 dbus_message_iter_append_basic(&it, DBUS_TYPE_STRING, &pUUID);
1014 DBusMessageIter aOptionsIter;
1015 dbus_message_iter_open_container(&it, DBUS_TYPE_ARRAY,
"{sv}", &aOptionsIter);
1017 DBusMessageIter aEntry;
1020 dbus_message_iter_open_container(&aOptionsIter, DBUS_TYPE_DICT_ENTRY,
nullptr, &aEntry);
1022 const char *pString =
"Name";
1023 dbus_message_iter_append_basic(&aEntry, DBUS_TYPE_STRING, &pString);
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);
1033 dbus_message_iter_close_container(&it, &aOptionsIter);
1041 bool bSuccess =
true;
1043 pMsg = sendUnrefAndWaitForReply( pConnection, pMsg );
1046 dbus_error_init(&aError);
1047 if (pMsg && dbus_set_error_from_message( &aError, pMsg ))
1051 "Failed to register our Profile1 with bluez ProfileManager "
1052 << (aError.message ? aError.message :
"<null>"));
1055 dbus_error_free(&aError);
1057 dbus_message_unref(pMsg);
1059 dbus_connection_flush(pConnection);
1067 : meWasDiscoverable(
UNKNOWN ),
1068 mpCommunicators( pCommunicators )
1070#ifdef LINUX_BLUETOOTH
1074 if (!dbus_threads_init_default()) {
1075 throw std::bad_alloc();
1078 mpImpl.reset(
new BluetoothServer::Impl());
1088#ifdef LINUX_BLUETOOTH
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 );
1102#ifdef LINUX_BLUETOOTH
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 );
1116#ifdef LINUX_BLUETOOTH
1117 if (!
spServer->mpImpl->mpConnection ||
1122 std::unique_ptr<DBusObject> pAdapter =
spServer->mpImpl->getAdapter();
1126 bool bDiscoverable = getDiscoverable(
spServer->mpImpl->mpConnection, pAdapter.get() );
1129 if( !bDiscoverable )
1130 setDiscoverable(
spServer->mpImpl->mpConnection, pAdapter.get(),
true );
1138#ifdef LINUX_BLUETOOTH
1139 std::unique_ptr<DBusObject> pAdapter =
spServer->mpImpl->getAdapter();
1142 setDiscoverable(
spServer->mpImpl->mpConnection, pAdapter.get(),
false );
1153 rpCommunicator->forceClose();
1160 SAL_INFO(
"sdremote.bluetooth",
"BluetoothServer::run called" );
1161 osl::Thread::setName(
"BluetoothServer");
1162#ifdef LINUX_BLUETOOTH
1163 DBusConnection *pConnection = dbusConnectToNameOnBus();
1170 if( dbus_connection_get_unix_fd( pConnection, &fd ) && fd >= 0 )
1173 aDBusFD.events = G_IO_IN | G_IO_PRI;
1174 g_main_context_add_poll( mpImpl->mpContext, &aDBusFD, G_PRIORITY_DEFAULT );
1177 SAL_WARN(
"sdremote.bluetooth",
"failed to poll for incoming dbus signals" );
1179 if (isBluez5Available(pConnection))
1181 SAL_INFO(
"sdremote.bluetooth",
"Using Bluez 5");
1183 mpImpl->mpConnection = pConnection;
1184 mpImpl->maBluezVersion = Impl::BluezVersion::BLUEZ5;
1195 aDBusFD.revents = 0;
1196 g_main_context_iteration( mpImpl->mpContext,
true );
1197 if( aDBusFD.revents )
1199 dbus_connection_read_write( pConnection, 0 );
1200 while (DBUS_DISPATCH_DATA_REMAINS == dbus_connection_get_dispatch_status( pConnection ))
1201 dbus_connection_dispatch( pConnection );
1207 unregisterBluez5Profile( pConnection );
1208 g_main_context_unref( mpImpl->mpContext );
1209 mpImpl->mpConnection =
nullptr;
1210 mpImpl->mpContext =
nullptr;
1215 mpImpl->maBluezVersion = Impl::BluezVersion::BLUEZ4;
1218 mpImpl->mpService = registerWithDefaultAdapter( pConnection );
1222 dbus_error_init( &aError );
1223 dbus_bus_add_match( pConnection,
"type='signal',interface='org.bluez.Manager'", &aError );
1224 dbus_connection_flush( pConnection );
1227 mpImpl->mpService = registerWithDefaultAdapter( pConnection );
1231 if( mpImpl->mpService )
1232 bluezCreateAttachListeningSocket( mpImpl->mpContext, &aSocketFD );
1234 mpImpl->mpConnection = pConnection;
1238 aDBusFD.revents = 0;
1239 aSocketFD.revents = 0;
1240 g_main_context_iteration( mpImpl->mpContext,
true );
1242 SAL_INFO(
"sdremote.bluetooth",
"main-loop spin "
1243 << aDBusFD.revents <<
" " << aSocketFD.revents );
1244 if( aDBusFD.revents )
1246 dbus_connection_read_write( pConnection, 0 );
1247 DBusMessage *pMsg = dbus_connection_pop_message( pConnection );
1250 if( dbus_message_is_signal( pMsg,
"org.bluez.Manager",
"AdapterRemoved" ) )
1252 SAL_WARN(
"sdremote.bluetooth",
"lost adapter - cleaning up sockets" );
1253 bluezDetachCloseSocket( mpImpl->mpContext, &aSocketFD );
1256 else if( dbus_message_is_signal( pMsg,
"org.bluez.Manager",
"AdapterAdded" ) ||
1257 dbus_message_is_signal( pMsg,
"org.bluez.Manager",
"DefaultAdapterChanged" ) )
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 );
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 ) );
1273 dbus_message_unref( pMsg );
1276 if( aSocketFD.revents )
1278 sockaddr_rc aRemoteAddr;
1279 socklen_t aRemoteAddrLen =
sizeof(aRemoteAddr);
1281 SAL_INFO(
"sdremote.bluetooth",
"performing accept" );
1282 int nClient = accept( aSocketFD.fd,
reinterpret_cast<sockaddr*
>(&aRemoteAddr), &aRemoteAddrLen);
1283 if ( nClient < 0 && errno != EAGAIN )
1285 SAL_WARN(
"sdremote.bluetooth",
"accept failed with errno " << errno );
1287 SAL_INFO(
"sdremote.bluetooth",
"connection accepted " << nClient );
1297 unregisterBluez5Profile( pConnection );
1298 g_main_context_unref( mpImpl->mpContext );
1299 mpImpl->mpConnection =
nullptr;
1300 mpImpl->mpContext =
nullptr;
1302#elif defined(_WIN32)
1303 WORD wVersionRequested;
1306 wVersionRequested = MAKEWORD(2, 2);
1308 if ( WSAStartup(wVersionRequested, &wsaData) )
1313 int aSocket = socket( AF_BTH, SOCK_STREAM, BTHPROTO_RFCOMM );
1320 aAddr.addressFamily = AF_BTH;
1322 aAddr.serviceClassId = GUID_NULL;
1323 aAddr.port = BT_PORT_ANY;
1324 if ( bind( aSocket,
reinterpret_cast<SOCKADDR*
>(&aAddr),
sizeof(aAddr) ) == SOCKET_ERROR )
1326 closesocket( aSocket );
1332 int aNameSize =
sizeof(
aName);
1333 getsockname( aSocket,
reinterpret_cast<SOCKADDR*
>(&
aName), &aNameSize );
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;
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)
1362 closesocket( aSocket );
1367 if ( listen( aSocket, 1 ) == SOCKET_ERROR )
1369 closesocket( aSocket );
1374 SOCKADDR_BTH aRemoteAddr;
1375 int aRemoteAddrLen =
sizeof(aRemoteAddr);
1379 if ( (socket = accept(aSocket,
reinterpret_cast<sockaddr*
>(&aRemoteAddr), &aRemoteAddrLen)) == INVALID_SOCKET )
1381 closesocket( aSocket );
1391#elif defined(MACOSX)
1397 NSAutoreleasePool* pool = [[NSAutoreleasePool alloc]
init];
1399 NSDictionary *dict =
1400 [NSDictionary dictionaryWithObjectsAndKeys:
1403 [NSArray arrayWithObject:
1404 [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16ServiceClassSerialPort]],
1405 @"0001 - ServiceClassIDList",
1408 [NSArray arrayWithObjects:
1409 [NSArray arrayWithObject: [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16L2CAP]],
1410 [NSArray arrayWithObjects:
1411 [IOBluetoothSDPUUID uuid16: kBluetoothL2CAPPSMRFCOMM],
1412 [NSDictionary dictionaryWithObjectsAndKeys:
1413 [NSNumber numberWithInt: 1],
1415 [NSNumber numberWithInt: 1],
1417 [NSNumber numberWithInt: 5],
1418 @"DataElementValue",
1422 @"0004 - Protocol descriptor list",
1425 [NSArray arrayWithObject:
1426 [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16ServiceClassPublicBrowseGroup]],
1427 @"0005 - BrowseGroupList",
1430 [NSArray arrayWithObjects:
1431 [NSData dataWithBytes:
"en" length: 2],
1432 [NSDictionary dictionaryWithObjectsAndKeys:
1433 [NSNumber numberWithInt: 2],
1435 [NSNumber numberWithInt: 1],
1437 [NSNumber numberWithInt: 0x006a],
1438 @"DataElementValue",
1440 [NSDictionary dictionaryWithObjectsAndKeys:
1441 [NSNumber numberWithInt: 2],
1443 [NSNumber numberWithInt: 1],
1445 [NSNumber numberWithInt: 0x0100],
1446 @"DataElementValue",
1449 @"0006 - LanguageBaseAttributeIDList",
1452 [NSArray arrayWithObject:
1453 [NSArray arrayWithObjects:
1454 [IOBluetoothSDPUUID uuid16: kBluetoothSDPUUID16ServiceClassSerialPort],
1455 [NSDictionary dictionaryWithObjectsAndKeys:
1456 [NSNumber numberWithInt: 2],
1458 [NSNumber numberWithInt: 1],
1460 [NSNumber numberWithInt: 0x0100],
1461 @"DataElementValue",
1464 @"0009 - BluetoothProfileDescriptorList",
1467 @"LibreOffice Impress Remote Control",
1468 @"0100 - ServiceName",
1469 @"The Document Foundation",
1470 @"0102 - ProviderName",
1474 IOBluetoothSDPServiceRecordRef serviceRecordRef;
1476 IOReturn rc = IOBluetoothAddServiceDict(
reinterpret_cast<CFDictionaryRef
>(dict), &serviceRecordRef);
1479 SAL_INFO(
"sdremote.bluetooth",
"IOBluetoothAddServiceDict returned " << rc);
1481 if (rc == kIOReturnSuccess)
1483 IOBluetoothSDPServiceRecord *serviceRecord =
1484 [IOBluetoothSDPServiceRecord withSDPServiceRecordRef: serviceRecordRef];
1486 BluetoothRFCOMMChannelID channelID;
1487 [serviceRecord getRFCOMMChannelID: &channelID];
1489 BluetoothSDPServiceRecordHandle serviceRecordHandle;
1490 [serviceRecord getServiceRecordHandle: &serviceRecordHandle];
1493 IOBluetoothRegisterForFilteredRFCOMMChannelOpenNotifications(
1497 kIOBluetoothUserNotificationChannelDirectionIncoming);
1499 [serviceRecord release];
const char *const bluetooth_service_record
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)
void cleanupCommunicators()
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.
osl::Condition mHaveBytes
EmbeddedObjectRef * pObject
#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