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