Skip to content

Commit

Permalink
fixed struct field offsets to work for raspbian / mono build
Browse files Browse the repository at this point in the history
  • Loading branch information
root committed Jul 9, 2015
1 parent 16e141c commit 1829bd3
Show file tree
Hide file tree
Showing 27 changed files with 466 additions and 344 deletions.
21 changes: 7 additions & 14 deletions InTheHand.Net.Personal/InTheHand.Net.Personal.FX2.csproj
@@ -1,10 +1,8 @@
<?xml version="1.0" encoding="utf-8"?> <?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="12.0"> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003" ToolsVersion="4.0">
<PropertyGroup> <PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration> <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform> <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>9.0.30729</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{98BDDE14-8F8C-40CC-9E45-5BAC73573EF4}</ProjectGuid> <ProjectGuid>{98BDDE14-8F8C-40CC-9E45-5BAC73573EF4}</ProjectGuid>
<OutputType>Library</OutputType> <OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder> <AppDesignerFolder>Properties</AppDesignerFolder>
Expand Down Expand Up @@ -44,8 +42,6 @@
<DefineConstants>TRACE;DEBUG;WinXP;V2;BlueZ;FX3_5</DefineConstants> <DefineConstants>TRACE;DEBUG;WinXP;V2;BlueZ;FX3_5</DefineConstants>
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel> <WarningLevel>4</WarningLevel>
<DocumentationFile>
</DocumentationFile>
<PlatformTarget>AnyCPU</PlatformTarget> <PlatformTarget>AnyCPU</PlatformTarget>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' "> <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
Expand All @@ -68,23 +64,20 @@
<ErrorReport>prompt</ErrorReport> <ErrorReport>prompt</ErrorReport>
<NoWarn>1591</NoWarn> <NoWarn>1591</NoWarn>
<CodeAnalysisRules>-Microsoft.Globalization#CA1304;-Microsoft.Globalization#CA1305;-Microsoft.Performance#CA1822</CodeAnalysisRules> <CodeAnalysisRules>-Microsoft.Globalization#CA1304;-Microsoft.Globalization#CA1305;-Microsoft.Performance#CA1822</CodeAnalysisRules>
<WarningLevel>4</WarningLevel>
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<Reference Include="mscorlib" /> <Reference Include="mscorlib" />
<Reference Include="NDesk.DBus, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f6716e4f9b2ed099, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\ExternalLibs\Mono etc\NDesk.DBus.dll</HintPath>
<Private>False</Private>
</Reference>
<Reference Include="System" /> <Reference Include="System" />
<Reference Include="System.configuration" /> <Reference Include="System.Core" />
<Reference Include="System.Core">
<RequiredTargetFramework>3.5</RequiredTargetFramework>
</Reference>
<Reference Include="System.Data" /> <Reference Include="System.Data" />
<Reference Include="System.Drawing" /> <Reference Include="System.Drawing" />
<Reference Include="System.Windows.Forms" /> <Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" /> <Reference Include="System.Xml" />
<Reference Include="System.Configuration" />
<Reference Include="NDesk.DBus">
<HintPath>NDesk.DBus.dll</HintPath>
</Reference>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<Compile Include="FakeAttributes.cs" /> <Compile Include="FakeAttributes.cs" />
Expand Down
10 changes: 10 additions & 0 deletions InTheHand.Net.Personal/Net.Bluetooth.BlueZ/BluezClient.cs
Expand Up @@ -233,7 +233,17 @@ List<int> IUsesBluetoothConnectorImplementsServiceLookup.EndServiceDiscovery(IAs
throw new InvalidOperationException("Begin not called."); throw new InvalidOperationException("Begin not called.");
} }
var records = _sdpQuery.EndQuery(ar); var records = _sdpQuery.EndQuery(ar);

foreach (var record in records) {
Console.WriteLine ("Service Record:" + record.ToString());
}

var ports = BluetoothConnector.ListAllRfcommPortsInRecords(records); var ports = BluetoothConnector.ListAllRfcommPortsInRecords(records);

foreach (var port in ports) {
Console.WriteLine ("Port:" + port);
}

return ports; return ports;
} }


Expand Down
4 changes: 0 additions & 4 deletions InTheHand.Net.Personal/Net.Bluetooth.BlueZ/BluezDbus.cs
Expand Up @@ -145,7 +145,6 @@ internal BluezDbusInterface.Device GetDevice(ObjectPath devicePath)
{ {
a = GetDefaultAdapter(); a = GetDefaultAdapter();
var addrStrIn = FromBluetoothAddress(address); var addrStrIn = FromBluetoothAddress(address);
Console.WriteLine("gonna FindDevice");
ObjectPath devicePath; ObjectPath devicePath;
try { try {
devicePath = a.FindDevice(addrStrIn); devicePath = a.FindDevice(addrStrIn);
Expand All @@ -161,7 +160,6 @@ internal BluezDbusInterface.Device GetDevice(ObjectPath devicePath)
} }
} }
objectPath = devicePath; objectPath = devicePath;
Console.WriteLine("gonna Get {0}", devicePath);
var d = FactoryBus.GetObject<BluezDbusInterface.Device>(Service, devicePath); var d = FactoryBus.GetObject<BluezDbusInterface.Device>(Service, devicePath);
return d; return d;
} }
Expand Down Expand Up @@ -263,7 +261,6 @@ internal BluezDbusInterface.Adapter GetDefaultAdapter()
BluetoothAddress localAddress, out ObjectPath objectPath) BluetoothAddress localAddress, out ObjectPath objectPath)
{ {
var mgr = FactoryBus.GetObject<BluezDbusInterface.Manager>(Service, ObjectPath.Root); var mgr = FactoryBus.GetObject<BluezDbusInterface.Manager>(Service, ObjectPath.Root);
Console.WriteLine("got Manager");
ObjectPath adapterPath = mgr.FindAdapter(FromBluetoothAddress(localAddress)); ObjectPath adapterPath = mgr.FindAdapter(FromBluetoothAddress(localAddress));
objectPath = adapterPath; objectPath = adapterPath;
return GetAdapter(adapterPath); return GetAdapter(adapterPath);
Expand All @@ -272,7 +269,6 @@ internal BluezDbusInterface.Adapter GetDefaultAdapter()
internal BluezDbusInterface.Adapter GetAdapter(ObjectPath adapterPath) internal BluezDbusInterface.Adapter GetAdapter(ObjectPath adapterPath)
{ {
var a = FactoryBus.GetObject<BluezDbusInterface.Adapter>(Service, adapterPath); var a = FactoryBus.GetObject<BluezDbusInterface.Adapter>(Service, adapterPath);
Console.WriteLine("got Adapter");
return a; return a;
} }


Expand Down
100 changes: 75 additions & 25 deletions InTheHand.Net.Personal/Net.Bluetooth.BlueZ/BluezSdpQuery.cs
Expand Up @@ -21,6 +21,7 @@
using InTheHand.Net.Bluetooth.AttributeIds; using InTheHand.Net.Bluetooth.AttributeIds;
using System.Net.Sockets; using System.Net.Sockets;
using System.Diagnostics.CodeAnalysis; using System.Diagnostics.CodeAnalysis;
using System.ComponentModel;


namespace InTheHand.Net.Bluetooth.BlueZ namespace InTheHand.Net.Bluetooth.BlueZ
{ {
Expand Down Expand Up @@ -82,7 +83,10 @@ internal List<ServiceRecord> EndQuery(IAsyncResult ar)
if (session.IsInvalid) { if (session.IsInvalid) {
//BluezUtils.Throw((BluezError)(-1), "sdp_connect"); //BluezUtils.Throw((BluezError)(-1), "sdp_connect");
const int WSASERVICE_NOT_FOUND = 10108; const int WSASERVICE_NOT_FOUND = 10108;
throw new Win32Exception(Marshal.GetLastWin32Error());

throw new SocketException(WSASERVICE_NOT_FOUND); throw new SocketException(WSASERVICE_NOT_FOUND);

} }
try try
{ {
Expand All @@ -102,6 +106,7 @@ internal List<ServiceRecord> EndQuery(IAsyncResult ar)
List<ServiceRecord> DoSdpQuery(NativeMethods.SdpSessionSafeHandle session, List<ServiceRecord> DoSdpQuery(NativeMethods.SdpSessionSafeHandle session,
Structs.uuid_t svcUuid, bool rfcommOnly) Structs.uuid_t svcUuid, bool rfcommOnly)
{ {
Console.WriteLine("Size ="+Marshal.SizeOf(typeof(Structs.uuid_t)));
var listAllocs = new List<IntPtr>(); var listAllocs = new List<IntPtr>();
IntPtr searchList = BluezUtils.sdp_list_append(IntPtr.Zero, svcUuid, listAllocs); IntPtr searchList = BluezUtils.sdp_list_append(IntPtr.Zero, svcUuid, listAllocs);


Expand All @@ -122,28 +127,34 @@ internal List<ServiceRecord> EndQuery(IAsyncResult ar)
} }


// Query // Query
Console.WriteLine("sdp_service_search_attr_req in:" // Console.WriteLine("sdp_service_search_attr_req in:"
+ " {0}, attrid_list: {1}", // + " {0}, attrid_list: {1}",
searchList, attridList); // searchList, attridList);
IntPtr pResponseList; IntPtr pResponseList;
BluezError ret = NativeMethods.sdp_service_search_attr_req(session, BluezError ret = NativeMethods.sdp_service_search_attr_req(session,
searchList, searchList,
reqType, attridList, reqType, attridList,
out pResponseList); out pResponseList);
Console.WriteLine("sdp_service_search_attr_req ret: {0}, result: {1}", //Console.WriteLine("sdp_service_search_attr_req ret: {0}, result: {1}",ret, pResponseList);
ret, pResponseList);
BluezUtils.CheckAndThrow(ret, "sdp_service_search_attr_req"); BluezUtils.CheckAndThrow(ret, "sdp_service_search_attr_req");

var rList = BuildRecordList(pResponseList);
var rList = BuildRecordList(pResponseList);
return rList; foreach (var record in rList) {
string output = ServiceRecordUtilities.Dump (record);
Console.WriteLine (output);
}


return rList;
} }


//---- //----
internal List<ServiceRecord> BuildRecordList(IntPtr pResponseList) internal List<ServiceRecord> BuildRecordList(IntPtr pResponseList)
{ {
Console.WriteLine ("BuildRecordList");
var list = new List<ServiceRecord>(); var list = new List<ServiceRecord>();
if (pResponseList == IntPtr.Zero) { if (pResponseList == IntPtr.Zero) {
Console.WriteLine("Empty responseList."); //Console.WriteLine("Empty responseList.");
return list; return list;
} }
int count = 0; int count = 0;
Expand All @@ -152,74 +163,107 @@ internal List<ServiceRecord> BuildRecordList(IntPtr pResponseList)
++count; ++count;
var item = (Structs.sdp_list_t)Marshal.PtrToStructure( var item = (Structs.sdp_list_t)Marshal.PtrToStructure(
pCurR, typeof(Structs.sdp_list_t)); pCurR, typeof(Structs.sdp_list_t));
//Console.WriteLine(" item next: 0x{0:X8}, data 0x{1:X8}", item.next, item.data); //Console.WriteLine("\t"+count+" Record List item next: 0x{0:X8}, data 0x{1:X8}", item.next, item.data);
var r = (Structs.sdp_record_t)Marshal.PtrToStructure( var r = (Structs.sdp_record_t)Marshal.PtrToStructure(
item.data, typeof(Structs.sdp_record_t)); item.data, typeof(Structs.sdp_record_t));
//Console.WriteLine("rcd.Handle: 0x{0:X}", r.handle); //Console.WriteLine("rcd.Handle: 0x{0:X}", r.handle);
var x = BuildRecord(r); var x = BuildRecord(r);
list.Add(x); list.Add(x);
pCurR = item.next; pCurR = item.next;
if (pCurR == IntPtr.Zero) { if (pCurR == IntPtr.Zero) {
break; break;
} }
}//while }//while
Console.WriteLine("record count: {0}", count); //Console.WriteLine("record count: {0}", count);
return list; return list;
} }


internal ServiceRecord BuildRecord(Structs.sdp_record_t rcdData) internal ServiceRecord BuildRecord(Structs.sdp_record_t rcdData)
{ {
Console.WriteLine ("BuildRecord");
var attrList = new List<ServiceAttribute>(); var attrList = new List<ServiceAttribute>();
int count = 0; int count = 0;
IntPtr pCurAttr = rcdData.attrlist; IntPtr pCurAttr = rcdData.attrlist;
for (int HACK = 0; HACK < RestrictRunawayCount; ++HACK) { for (int HACK = 0; HACK < RestrictRunawayCount; ++HACK) {
++count; ++count;
var item = (Structs.sdp_list_t)Marshal.PtrToStructure( var item = (Structs.sdp_list_t)Marshal.PtrToStructure(
pCurAttr, typeof(Structs.sdp_list_t)); pCurAttr, typeof(Structs.sdp_list_t));
//Console.WriteLine(" item next: 0x{0:X8}, data 0x{1:X8}", item.next, item.data);
var attrData = (Structs.sdp_data_struct__Bytes)Marshal.PtrToStructure(
item.data, typeof(Structs.sdp_data_struct__Bytes));
//Console.WriteLine(" attrId: 0x{0:X}", attrData.attrId);
var attr = BuildAttribute(item.data); var attr = BuildAttribute(item.data);
attrList.Add(attr); attrList.Add(attr);
// Next // Next
pCurAttr = item.next; pCurAttr = item.next;
if (pCurAttr == IntPtr.Zero) {
if (pCurAttr == IntPtr.Zero) {
break; break;
} }
}//while }//while
Console.WriteLine("attr count: {0}", count); Console.WriteLine("attr count: {0}", count);
var r = new ServiceRecord(attrList); var r = new ServiceRecord(attrList);
return r; return r;
} }


private ServiceAttribute BuildAttribute(IntPtr pAttrData) private ServiceAttribute BuildAttribute(IntPtr pAttrData)
{ {
Console.WriteLine ("BuildAttribute");
var attrData = (Structs.sdp_data_struct__Bytes)Marshal.PtrToStructure( var attrData = (Structs.sdp_data_struct__Bytes)Marshal.PtrToStructure(
pAttrData, typeof(Structs.sdp_data_struct__Bytes)); pAttrData, typeof(Structs.sdp_data_struct__Bytes));
ushort attrId0 = attrData.attrId; //ushort attrId0 = attrData.attrId;
var attrId = unchecked((ServiceAttributeId)attrId0); //var attrId = unchecked((ServiceAttributeId)attrId0);
//Console.WriteLine("attrId: {0} = 0x{1:X}", attrId, attrId0); //Console.WriteLine("attrId: {0} = 0x{1:X}", attrId, attrId0);
var elem = BuildElement(pAttrData);
return new ServiceAttribute(attrId0, elem); var elem = BuildElement(pAttrData);
Console.WriteLine ("Child Element of Attribute " + attrData.attrId + " is " + elem.ElementTypeDescriptor);


//return new ServiceAttribute(attrId0, elem);
return new ServiceAttribute(attrData.attrId, elem);
} }


private ServiceElement BuildElement(IntPtr pElemData) private ServiceElement BuildElement(IntPtr pElemData)
{ {
Console.WriteLine ("BuildElement");
var elemData = (Structs.sdp_data_struct__Bytes)Marshal.PtrToStructure( var elemData = (Structs.sdp_data_struct__Bytes)Marshal.PtrToStructure(
pElemData, typeof(Structs.sdp_data_struct__Bytes)); pElemData, typeof(Structs.sdp_data_struct__Bytes));
var debug = (Structs.sdp_data_struct__Debug)Marshal.PtrToStructure( var debug = (Structs.sdp_data_struct__Debug)Marshal.PtrToStructure(
pElemData, typeof(Structs.sdp_data_struct__Debug)); pElemData, typeof(Structs.sdp_data_struct__Debug));

//Console.WriteLine ("dtd=" + elemData.dtd);

//Console.WriteLine ("ServiceElement length in bytes is " + debug.all.Length);
Console.WriteLine ("sdp_data_struct__Bytes length is measured at " + Marshal.SizeOf (typeof(Structs.sdp_data_struct__Bytes)));
string debugString = "";
foreach (var b in debug.all) {
debugString += b + ":";
}
Console.WriteLine (debugString);

string valString = "";
foreach (var b in elemData.val) {
valString += b + ":";
}


Console.WriteLine ("Debug sdp_data_struct__Bytes at "+pElemData.ToString()+" " + elemData.dtd + "," + elemData.attrId + ","+valString+"," + elemData.next.ToString() + "," + elemData.unitSize);


// //
ElementTypeDescriptor etd; SizeIndex sizeIndex; ElementTypeDescriptor etd; SizeIndex sizeIndex;
Map(elemData.dtd, out etd, out sizeIndex); Map(elemData.dtd, out etd, out sizeIndex);
//Console.WriteLine("BE: dtd: {0}, unitSize: {1}", //Console.WriteLine("BE: dtd: {0}, unitSize: {1}",elemData.dtd, elemData.unitSize);
// elemData.dtd, elemData.unitSize);
// //
if (etd == ElementTypeDescriptor.ElementSequence if (etd == ElementTypeDescriptor.ElementSequence
|| etd == ElementTypeDescriptor.ElementAlternative) { || etd == ElementTypeDescriptor.ElementAlternative) {
Console.WriteLine ("ElementTypeDescriptor = ElementSequence or ElementAlternative");

var list = new List<ServiceElement> ();
IntPtr pCur = elemData.ReadIntPtr(); IntPtr pCur = elemData.ReadIntPtr();
var list = DoSequence(pCur); if (pCur != IntPtr.Zero) {
list = DoSequence (pCur);
Console.WriteLine ("Element Child List Length=" + list.Count);
} else {
Console.WriteLine ("Element Child is null");
}
//Console.WriteLine ("list length=" + list.Count);
#if DEBUG #if DEBUG
ElementTypeDescriptor cover; ElementTypeDescriptor cover;
if (etd == ElementTypeDescriptor.ElementAlternative) { if (etd == ElementTypeDescriptor.ElementAlternative) {
Expand All @@ -238,11 +282,13 @@ private ServiceElement BuildElement(IntPtr pElemData)
byte[] data; byte[] data;
if (etd == ElementTypeDescriptor.TextString if (etd == ElementTypeDescriptor.TextString
|| etd == ElementTypeDescriptor.Url) { || etd == ElementTypeDescriptor.Url) {
Console.WriteLine ("ElementTypeDescriptor = TextString or Url");
buf = new byte[elemData.unitSize]; buf = new byte[elemData.unitSize];
IntPtr pStr = elemData.ReadIntPtr(); IntPtr pStr = elemData.ReadIntPtr();
Marshal.Copy(pStr, buf, 0, buf.Length - 1); Marshal.Copy(pStr, buf, 0, buf.Length - 1);
//---- //----
} else if (etd == ElementTypeDescriptor.Uuid) { } else if (etd == ElementTypeDescriptor.Uuid) {
Console.WriteLine ("ElementTypeDescriptor = Uuid");
var elemDataUuid = (Structs.sdp_data_struct__uuid_t)Marshal.PtrToStructure( var elemDataUuid = (Structs.sdp_data_struct__uuid_t)Marshal.PtrToStructure(
pElemData, typeof(Structs.sdp_data_struct__uuid_t)); pElemData, typeof(Structs.sdp_data_struct__uuid_t));
Debug.Assert(elemDataUuid.dtd == elemDataUuid.val.type, "uuid type"); Debug.Assert(elemDataUuid.dtd == elemDataUuid.val.type, "uuid type");
Expand All @@ -252,6 +298,8 @@ private ServiceElement BuildElement(IntPtr pElemData)
Array.Copy(data, buf, buf.Length); Array.Copy(data, buf, buf.Length);
//---- //----
} else { } else {
Console.WriteLine ("ElementTypeDescriptor = ?");

int len = FromSizeIndex(sizeIndex); int len = FromSizeIndex(sizeIndex);
buf = new byte[len]; buf = new byte[len];
data = elemData.val; data = elemData.val;
Expand All @@ -268,6 +316,7 @@ private ServiceElement BuildElement(IntPtr pElemData)


private List<ServiceElement> DoSequence(IntPtr pCur) private List<ServiceElement> DoSequence(IntPtr pCur)
{ {
Console.WriteLine ("DoSequence");
List<ServiceElement> list = new List<ServiceElement>(); List<ServiceElement> list = new List<ServiceElement>();
if (pCur != IntPtr.Zero) { if (pCur != IntPtr.Zero) {
for (int HACK = 0; HACK < Math.Min(4, RestrictRunawayCount); ++HACK) { for (int HACK = 0; HACK < Math.Min(4, RestrictRunawayCount); ++HACK) {
Expand All @@ -283,6 +332,7 @@ private List<ServiceElement> DoSequence(IntPtr pCur)
} }
}//for }//for
} }
//Console.WriteLine ("DoSequence list length=" + list.Count);
return list; return list;
} }


Expand Down
6 changes: 5 additions & 1 deletion InTheHand.Net.Personal/Net.Bluetooth.BlueZ/BluezUtils.cs
Expand Up @@ -13,6 +13,7 @@
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Net.Sockets; using System.Net.Sockets;
using System.Linq; using System.Linq;
using System.ComponentModel;


namespace InTheHand.Net.Bluetooth.BlueZ namespace InTheHand.Net.Bluetooth.BlueZ
{ {
Expand All @@ -36,7 +37,10 @@ internal static void CheckAndThrow(BluezError ret, string descr)
{ {
if (ret >= 0) if (ret >= 0)
return; return;
Throw(ret, descr); var ex = new Win32Exception (Marshal.GetLastWin32Error());
Console.WriteLine ("BluezUtils.CheckAndThrow:" + ex.Message);
throw ex;
//Throw(ret, descr);
} }


[DebuggerNonUserCode] [DebuggerNonUserCode]
Expand Down

0 comments on commit 1829bd3

Please sign in to comment.