Skip to content

Commit

Permalink
Merge pull request #4 from BenBergeron/master
Browse files Browse the repository at this point in the history
Added GattServer
  • Loading branch information
brookpatten committed Jan 12, 2018
2 parents b6fcf5d + e73656e commit b4a1dfd
Show file tree
Hide file tree
Showing 17 changed files with 703 additions and 28 deletions.
8 changes: 5 additions & 3 deletions Mono.BlueZ.Console/BlendMicroBootstrap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ public void Run()
gattManager = GetObject<GattManager1>(Service,adapterPath);
var gattProfile = new BlendGattProfile();
_system.Register(gattProfilePath,gattProfile);
gattManager.RegisterProfile(gattProfilePath,new string[]{charRead},new Dictionary<string,object>());
gattManager.RegisterApplication(gattProfilePath, new Dictionary<string,object>());
System.Console.WriteLine("Registered gatt profile");

//assume discovery for ble
Expand Down Expand Up @@ -210,7 +210,7 @@ public void Run()
}
}
agentManager.UnregisterAgent (agentPath);
gattManager.UnregisterProfile (gattProfilePath);
gattManager.UnregisterApplication (gattProfilePath);
}
}

Expand Down Expand Up @@ -271,7 +271,9 @@ public BlendGattProfile()
{
}

public void Release()
public IList<string> UUIDs => throw new NotImplementedException();

public void Release()
{
System.Console.WriteLine ("GattProfile1.Release");
}
Expand Down
182 changes: 182 additions & 0 deletions Mono.BlueZ.Console/GattServer.cs
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);
}
}
}
4 changes: 4 additions & 0 deletions Mono.BlueZ.Console/Mono.BlueZ.Console.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@
<Compile Include="DemoAgent.cs" />
<Compile Include="PebbleBootstrap.cs" />
<Compile Include="BlendMicroBootstrap.cs" />
<Compile Include="GattServer.cs" />
<Compile Include="TestCharacteristic.cs" />
<Compile Include="TestDescriptor.cs" />
<Compile Include="TestService.cs" />
</ItemGroup>
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
<ItemGroup>
Expand Down
7 changes: 5 additions & 2 deletions Mono.BlueZ.Console/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@ class MainClass
public static void Main (string[] args)
{
AppDomain.CurrentDomain.UnhandledException += new UnhandledExceptionEventHandler (GlobalHandler);
var bootstrap = new BlendMicroBootstrap ();
bootstrap.Run ();
//var bootstrap = new BlendMicroBootstrap ();
//bootstrap.Run ();

//var bootstrap = new PebbleBootstrap ();
//bootstrap.Run (true, null);

var gattServer = new GattServer();
gattServer.Run();
}

static void GlobalHandler(object sender, UnhandledExceptionEventArgs args)
Expand Down
31 changes: 31 additions & 0 deletions Mono.BlueZ.Console/TestCharacteristic.cs
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;
}
}
}
29 changes: 29 additions & 0 deletions Mono.BlueZ.Console/TestDescriptor.cs
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;
}
}
}
17 changes: 17 additions & 0 deletions Mono.BlueZ.Console/TestService.cs
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()));
}
}
}
59 changes: 59 additions & 0 deletions Mono.BlueZ.DBus/Application.cs
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;
}
}
}
Loading

0 comments on commit b4a1dfd

Please sign in to comment.