-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from BenBergeron/master
Added GattServer
- Loading branch information
Showing
17 changed files
with
703 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,182 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading; | ||
|
||
using DBus; | ||
using Mono.BlueZ.DBus; | ||
using org.freedesktop.DBus; | ||
|
||
namespace Mono.BlueZ.Console | ||
{ | ||
public class GattServer | ||
{ | ||
private Bus system; | ||
private ManualResetEvent _started = new ManualResetEvent(false); | ||
public Exception _startupException { get; private set; } | ||
private const string SERVICE = "org.bluez"; | ||
|
||
public void Run() | ||
{ | ||
StartMessageLoopDBus(); | ||
|
||
GattManager1 gattManager = null; | ||
ObjectPath appObjectPath = null; | ||
|
||
try | ||
{ | ||
System.Console.WriteLine("Fetching objects"); | ||
|
||
// 2. Find adapter | ||
var adapterFound = FindAdapter(); | ||
|
||
if (adapterFound == null) | ||
{ | ||
System.Console.WriteLine("Couldn't find adapter that supports LE"); | ||
return; | ||
} | ||
|
||
gattManager = GetObject<GattManager1>(SERVICE, adapterFound); | ||
|
||
if (gattManager == null) | ||
{ | ||
System.Console.WriteLine("Couldn't find Gatt manager."); | ||
return; | ||
} | ||
else | ||
{ | ||
System.Console.WriteLine("Found Gatt manager."); | ||
} | ||
|
||
var application = new Application(system); | ||
application.AddService(new TestService(system, 0)); | ||
|
||
appObjectPath = application.GetPath(); | ||
var options = new Dictionary<string, object>(); | ||
gattManager.RegisterApplication(appObjectPath, options); | ||
|
||
while(true) | ||
{ | ||
// Gatt server is running. Do nothing here. | ||
} | ||
|
||
//Thread.Sleep(30000); | ||
} | ||
catch (Exception exception) | ||
{ | ||
System.Console.WriteLine(exception); | ||
} | ||
finally | ||
{ | ||
gattManager.UnregisterApplication(appObjectPath); | ||
} | ||
} | ||
|
||
private void StartMessageLoopDBus() | ||
{ | ||
// Run a message loop for DBus on a new thread. | ||
var t = new Thread(DBusLoop) | ||
{ | ||
IsBackground = true | ||
}; | ||
t.Start(); | ||
_started.WaitOne(15 * 1000); | ||
_started.Close(); | ||
if (_startupException != null) | ||
{ | ||
throw _startupException; | ||
} | ||
else | ||
{ | ||
System.Console.WriteLine("Bus connected at " + system.UniqueName); | ||
} | ||
} | ||
|
||
private void DBusLoop() | ||
{ | ||
try | ||
{ | ||
system = Bus.System; | ||
} | ||
catch (Exception ex) | ||
{ | ||
_startupException = ex; | ||
return; | ||
} | ||
finally | ||
{ | ||
_started.Set(); | ||
} | ||
|
||
while (true) | ||
{ | ||
system.Iterate(); | ||
} | ||
} | ||
|
||
private ObjectManager GetCopyObjectManager() | ||
{ | ||
//get a copy of the object manager so we can browse the "tree" of bluetooth items | ||
ObjectManager manager = null; | ||
|
||
manager = GetObject<ObjectManager>(SERVICE, ObjectPath.Root); | ||
|
||
if (manager != null) | ||
{ | ||
System.Console.WriteLine("Registering interface events"); | ||
|
||
//register these events so we can tell when things are added/removed (eg: discovery) | ||
manager.InterfacesAdded += (p, i) => | ||
{ | ||
System.Console.WriteLine(p + " Discovered"); | ||
}; | ||
manager.InterfacesRemoved += (p, i) => | ||
{ | ||
System.Console.WriteLine(p + " Lost"); | ||
}; | ||
|
||
System.Console.WriteLine("Done"); | ||
} | ||
|
||
return manager; | ||
} | ||
|
||
private ObjectPath FindAdapter() | ||
{ | ||
var manager = GetCopyObjectManager(); | ||
//get the bluetooth object tree | ||
var managedObjects = manager.GetManagedObjects(); | ||
//find our adapter | ||
ObjectPath adapterPath = null; | ||
foreach (var obj in managedObjects.Keys) | ||
{ | ||
System.Console.WriteLine("Checking " + obj); | ||
if (managedObjects[obj].ContainsKey(typeof(LEAdvertisingManager1).DBusInterfaceName())) | ||
{ | ||
System.Console.WriteLine("Adapter found at" + obj + " that supports LE"); | ||
adapterPath = obj; | ||
break; | ||
} | ||
} | ||
|
||
return adapterPath; | ||
} | ||
|
||
private T GetObject<T>(string busName, ObjectPath path) | ||
{ | ||
if (system != null) | ||
{ | ||
var obj = system.GetObject<T>(busName, path); | ||
|
||
if (obj == null) | ||
{ | ||
System.Console.WriteLine("Object is NULL"); | ||
} | ||
|
||
return obj; | ||
} | ||
|
||
System.Console.WriteLine("System is NULL"); | ||
return default(T); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using System; | ||
using DBus; | ||
|
||
namespace Mono.BlueZ.Console | ||
{ | ||
public class TestCharacteristic : Characteristic | ||
{ | ||
private const string testCharacteristicUUID = "12345678-1234-5678-1234-56789abcdef1"; | ||
private static readonly string[] flags = { "read", "write", "writable-auxiliaries" }; | ||
private byte[] data; | ||
|
||
|
||
public TestCharacteristic(Bus bus, int index, ObjectPath service) : base(bus, index, testCharacteristicUUID, flags, service) | ||
{ | ||
AddDescriptor(new TestDescriptor(bus, 0, GetPath())); | ||
data = new byte[0]; | ||
} | ||
|
||
public override byte[] ReadValue(System.Collections.Generic.IDictionary<string, object> options) | ||
{ | ||
System.Console.WriteLine("Characteristic ReadValue: " + BitConverter.ToString(data)); | ||
return data; | ||
} | ||
|
||
public override void WriteValue(byte[] value, System.Collections.Generic.IDictionary<string, object> options) | ||
{ | ||
System.Console.WriteLine("Characteristic WriteValue: " + BitConverter.ToString(value)); | ||
data = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
using System; | ||
using DBus; | ||
|
||
namespace Mono.BlueZ.Console | ||
{ | ||
public class TestDescriptor : Descriptor | ||
{ | ||
private const string testDescriptorUUID = "12345678-1234-5678-1234-56789abcdef2"; | ||
private static readonly string[] flags = { "read", "write" }; | ||
private byte[] data; | ||
|
||
public TestDescriptor(Bus bus, int index, ObjectPath characteristic) : base(bus, index, testDescriptorUUID, flags, characteristic) | ||
{ | ||
data = new byte[] { 0x61, 0x62, 0x63, 0x64 }; | ||
} | ||
|
||
public override byte[] ReadValue(System.Collections.Generic.IDictionary<string, object> flags) | ||
{ | ||
System.Console.WriteLine("Descriptor ReadValue: " + BitConverter.ToString(data)); | ||
return data; | ||
} | ||
|
||
public override void WriteValue(byte[] value, System.Collections.Generic.IDictionary<string, object> flags) | ||
{ | ||
System.Console.WriteLine("Descriptor ReadValue: " + BitConverter.ToString(value)); | ||
data = value; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
using System; | ||
|
||
using DBus; | ||
|
||
namespace Mono.BlueZ.Console | ||
{ | ||
public class TestService : Service | ||
{ | ||
private const bool primary = true; | ||
private const string testServiceUUID = "12345678-1234-5678-1234-56789abcdef0"; | ||
|
||
public TestService(Bus bus, int index) : base(bus, index, testServiceUUID, primary) | ||
{ | ||
AddCharacteristic(new TestCharacteristic(bus, 0, GetPath())); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Collections.Generic; | ||
|
||
using DBus; | ||
using org.freedesktop.DBus; | ||
|
||
namespace Mono.BlueZ | ||
{ | ||
public class Application : ObjectManager | ||
{ | ||
private readonly Bus bus; | ||
private string path = "/"; | ||
private List<Service> services; | ||
|
||
public event InterfacesAddedHandler InterfacesAdded; | ||
public event InterfacesRemovedHandler InterfacesRemoved; | ||
|
||
public Application(Bus bus) | ||
{ | ||
this.bus = bus; | ||
services = new List<Service>(); | ||
bus.Register(new ObjectPath(path), this); | ||
} | ||
|
||
public ObjectPath GetPath() | ||
{ | ||
return new ObjectPath(path); | ||
} | ||
|
||
public void AddService(Service service) | ||
{ | ||
services.Add(service); | ||
} | ||
|
||
[return: Argument("objects")] | ||
public IDictionary<ObjectPath, IDictionary<string, IDictionary<string, object>>> GetManagedObjects() | ||
{ | ||
var manageObjets = new Dictionary<ObjectPath, IDictionary<string, IDictionary<string, object>>>(); | ||
|
||
foreach (var service in services) | ||
{ | ||
manageObjets.Add(service.GetPath(), service.GetProperties()); | ||
var characteristics = service.GetCharacteristics(); | ||
|
||
foreach (var characteristic in characteristics) | ||
{ | ||
manageObjets.Add(characteristic.GetPath(), characteristic.GetProperties()); | ||
var descriptors = characteristic.GetDescriptors(); | ||
|
||
foreach (var descriptor in descriptors) | ||
{ | ||
manageObjets.Add(descriptor.GetPath(), descriptor.GetProperties()); | ||
} | ||
} | ||
} | ||
|
||
return manageObjets; | ||
} | ||
} | ||
} |
Oops, something went wrong.