add openhack files

This commit is contained in:
Ryan Peters
2022-11-03 16:41:13 -04:00
commit b2c9f7e29f
920 changed files with 118861 additions and 0 deletions

View File

@ -0,0 +1,178 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="14.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{812692D4-C638-4FD1-AEA7-407824B134A6}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>ObdLibUWP</RootNamespace>
<AssemblyName>ObdLibUWP</AssemblyName>
<DefaultLanguage>en-US</DefaultLanguage>
<TargetPlatformIdentifier>UAP</TargetPlatformIdentifier>
<TargetPlatformVersion>10.0.10586.0</TargetPlatformVersion>
<TargetPlatformMinVersion>10.0.10240.0</TargetPlatformMinVersion>
<MinimumVisualStudioVersion>14</MinimumVisualStudioVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{A5A43C5B-DE2A-4C0C-9213-0A381AF9435A};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x86'">
<PlatformTarget>x86</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x86\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x86'">
<PlatformTarget>x86</PlatformTarget>
<OutputPath>bin\x86\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\ARM\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|ARM'">
<PlatformTarget>ARM</PlatformTarget>
<OutputPath>bin\ARM\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|x64'">
<PlatformTarget>x64</PlatformTarget>
<DebugSymbols>true</DebugSymbols>
<OutputPath>bin\x64\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<NoWarn>;2008</NoWarn>
<DebugType>full</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Release|x64'">
<PlatformTarget>x64</PlatformTarget>
<OutputPath>bin\x64\Release\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
</PropertyGroup>
<ItemGroup>
<!-- A reference to the entire .Net Framework and Windows SDK are automatically included -->
<None Include="project.json" />
</ItemGroup>
<ItemGroup>
<Compile Include="ObdWrapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<EmbeddedResource Include="Properties\ObdLibUWP.rd.xml" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ObdShare\ObdShare.csproj">
<Project>{7a88da9b-a374-4de2-b46e-ed2c45434508}</Project>
<Name>ObdShare</Name>
</ProjectReference>
</ItemGroup>
<PropertyGroup Condition=" '$(VisualStudioVersion)' == '' or '$(VisualStudioVersion)' &lt; '14.0' ">
<VisualStudioVersion>14.0</VisualStudioVersion>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XTC|AnyCPU'">
<OutputPath>bin\XTC\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoStdLib>true</NoStdLib>
<DebugType>pdbonly</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XTC|x86'">
<OutputPath>bin\x86\XTC\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<NoStdLib>true</NoStdLib>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x86</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XTC|ARM'">
<OutputPath>bin\ARM\XTC\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<NoStdLib>true</NoStdLib>
<DebugType>pdbonly</DebugType>
<PlatformTarget>ARM</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XTC|x64'">
<OutputPath>bin\x64\XTC\</OutputPath>
<DefineConstants>TRACE;NETFX_CORE;WINDOWS_UWP</DefineConstants>
<Optimize>true</Optimize>
<NoWarn>;2008</NoWarn>
<NoStdLib>true</NoStdLib>
<DebugType>pdbonly</DebugType>
<PlatformTarget>x64</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<Import Project="$(MSBuildExtensionsPath)\Microsoft\WindowsXaml\v$(VisualStudioVersion)\Microsoft.Windows.UI.Xaml.CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.
Other similar extension points exist, see Microsoft.Common.targets.
<Target Name="BeforeBuild">
</Target>
<Target Name="AfterBuild">
</Target>
-->
</Project>

View File

@ -0,0 +1,360 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Windows.Devices.Enumeration;
using Windows.Devices.Bluetooth.Rfcomm;
using Windows.Networking.Sockets;
using Windows.Storage.Streams;
namespace ObdLibUWP
{
public class ObdWrapper
{
const int Interval = 100;
const string DefValue = "-255";
private readonly Object _lock = new Object();
private bool connected = true;
private Dictionary<string, string> data;
private DataReader dataReaderObject;
private DataWriter dataWriterObject;
private Dictionary<string, string> piDs;
private bool running = true;
private RfcommDeviceService service;
private bool simulatormode;
private StreamSocket socket;
public async Task<bool> Init(bool simulatormode = false)
{
running = true;
//initialize _data
data = new Dictionary<string, string> {{"vin", DefValue}};
//VIN
piDs = ObdShare.ObdUtil.GetPIDs();
foreach (var v in piDs.Values)
{
data.Add(v, DefValue);
}
this.simulatormode = simulatormode;
if (simulatormode)
{
PollObd();
return true;
}
DeviceInformationCollection deviceInfoCollection =
await DeviceInformation.FindAllAsync(RfcommDeviceService.GetDeviceSelector(RfcommServiceId.SerialPort));
var numDevices = deviceInfoCollection.Count();
DeviceInformation device = null;
foreach (DeviceInformation info in deviceInfoCollection)
{
if (info.Name.ToLower().Contains("obd"))
{
device = info;
}
}
if (device == null)
return false;
try
{
service = await RfcommDeviceService.FromIdAsync(device.Id);
// Disposing the socket with close it and release all resources associated with the socket
socket?.Dispose();
socket = new StreamSocket();
try
{
// Note: If either parameter is null or empty, the call will throw an exception
await socket.ConnectAsync(service.ConnectionHostName, service.ConnectionServiceName);
connected = true;
}
catch (Exception ex)
{
connected = false;
System.Diagnostics.Debug.WriteLine("Connect:" + ex.Message);
}
// If the connection was successful, the RemoteAddress field will be populated
if (connected)
{
string msg = String.Format("Connected to {0}!", socket.Information.RemoteAddress.DisplayName);
System.Diagnostics.Debug.WriteLine(msg);
dataWriterObject = new DataWriter(socket.OutputStream);
dataReaderObject = new DataReader(socket.InputStream);
//initialize the device
string s;
s = await SendAndReceive("ATZ\r");
s = await SendAndReceive("ATE0\r");
s = await SendAndReceive("ATL1\r");
s = await SendAndReceive("ATSP00\r");
PollObd();
return true;
}
else
return false;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Overall Connect: " + ex.Message);
if (dataReaderObject != null)
{
dataReaderObject.Dispose();
dataReaderObject = null;
}
if (dataWriterObject != null)
{
dataWriterObject.Dispose();
dataWriterObject = null;
}
if (socket != null)
{
socket.Dispose();
socket = null;
}
return false;
}
}
private async void PollObd()
{
try
{
string s;
if (simulatormode)
s = "SIMULATORWINPHONE";
else
s = await GetVIN();
lock (_lock)
{
data["vin"] = s;
}
while (true)
{
foreach (var cmd in piDs.Keys)
{
var key = piDs[cmd];
if (simulatormode)
s = ObdShare.ObdUtil.GetEmulatorValue(cmd);
else
s = await RunCmd(cmd);
if (s != "ERROR")
lock (_lock)
{
data[key] = s;
}
if (!running)
return;
}
await Task.Delay(Interval);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
running = false;
if (dataReaderObject != null)
{
dataReaderObject.Dispose();
dataReaderObject = null;
}
if (dataWriterObject != null)
{
dataWriterObject.Dispose();
dataWriterObject = null;
}
if (socket != null)
{
socket.Dispose();
socket = null;
}
}
}
private async Task<string> ReadAsync()
{
string ret = await ReadAsyncRaw();
while (!ret.Trim().EndsWith(">"))
{
string tmp = await ReadAsyncRaw();
ret = ret + tmp;
}
return ret;
}
public async Task<string> GetSpeed()
{
if (simulatormode)
{
var r = new Random();
return r.Next().ToString();
}
var result = await SendAndReceive("010D\r");
return ObdShare.ObdUtil.ParseObd01Msg(result);
}
public async Task<string> GetVIN()
{
var result = await SendAndReceive("0902\r");
if (result.StartsWith("49"))
{
while (!result.Contains("49 02 05"))
{
string tmp = await ReadAsync();
result += tmp;
}
}
return ObdShare.ObdUtil.ParseVINMsg(result);
}
public Dictionary<string, string> Read()
{
if (!simulatormode && socket == null)
{
//if there is no connection
return null;
}
var ret = new Dictionary<string, string>();
lock (_lock)
{
foreach (var key in data.Keys)
{
ret.Add(key, data[key]);
}
foreach (var v in piDs.Values)
{
data[v] = DefValue;
}
}
return ret;
}
private async Task<string> SendAndReceive(string msg)
{
await WriteAsync(msg);
string s = await ReadAsync();
System.Diagnostics.Debug.WriteLine("Received: " + s);
s = s.Replace("SEARCHING...\r\n", "");
return s;
}
private async void Send(string msg)
{
try
{
if (socket.OutputStream != null)
{
//Launch the WriteAsync task to perform the write
await WriteAsync(msg);
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("Send(): " + ex.Message);
}
finally
{
// Cleanup once complete
if (dataWriterObject != null)
{
dataWriterObject.DetachStream();
dataWriterObject = null;
}
}
}
private async Task WriteAsync(string msg)
{
Task<UInt32> storeAsyncTask;
if (msg.Length != 0)
{
// Load the text from the sendText input text box to the dataWriter object
dataWriterObject.WriteString(msg);
// Launch an async task to complete the write operation
storeAsyncTask = dataWriterObject.StoreAsync().AsTask();
UInt32 bytesWritten = await storeAsyncTask;
if (bytesWritten > 0)
{
string statusText = msg + ", ";
statusText += bytesWritten.ToString();
statusText += " bytes written successfully!";
System.Diagnostics.Debug.WriteLine(statusText);
}
}
}
private async Task<string> ReadAsyncRaw()
{
uint readBufferLength = 1024;
// Set InputStreamOptions to complete the asynchronous read operation when one or more bytes is available
dataReaderObject.InputStreamOptions = InputStreamOptions.Partial;
// Create a task object to wait for data on the serialPort.InputStream
var loadAsyncTask = dataReaderObject.LoadAsync(readBufferLength).AsTask();
// Launch the task and wait
UInt32 bytesRead = await loadAsyncTask;
if (bytesRead > 0)
{
try
{
string recvdtxt = dataReaderObject.ReadString(bytesRead);
System.Diagnostics.Debug.WriteLine(recvdtxt);
return recvdtxt;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("ReadAsync: " + ex.Message);
return "";
}
}
return "";
}
private async Task<string> RunCmd(string cmd)
{
var result = await SendAndReceive(cmd + "\r");
return ObdShare.ObdUtil.ParseObd01Msg(result);
}
public async Task Disconnect()
{
running = false;
if (dataReaderObject != null)
{
dataReaderObject.Dispose();
dataReaderObject = null;
}
if (dataWriterObject != null)
{
dataWriterObject.Dispose();
dataWriterObject = null;
}
if (socket != null)
{
try
{
await socket.CancelIOAsync();
socket.Dispose();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
socket = null;
}
}
}
}

View File

@ -0,0 +1,33 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
using System.Reflection;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("ObdLibUWP")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ObdLibUWP")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: ComVisible(false)]

View File

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
This file contains Runtime Directives, specifications about types your application accesses
through reflection and other dynamic code patterns. Runtime Directives are used to control the
.NET Native optimizer and ensure that it does not remove code accessed by your library. If your
library does not do any reflection, then you generally do not need to edit this file. However,
if your library reflects over types, especially types passed to it or derived from its types,
then you should write Runtime Directives.
The most common use of reflection in libraries is to discover information about types passed
to the library. Runtime Directives have three ways to express requirements on types passed to
your library.
1. Parameter, GenericParameter, TypeParameter, TypeEnumerableParameter
Use these directives to reflect over types passed as a parameter.
2. SubTypes
Use a SubTypes directive to reflect over types derived from another type.
3. AttributeImplies
Use an AttributeImplies directive to indicate that your library needs to reflect over
types or methods decorated with an attribute.
For more information on writing Runtime Directives for libraries, please visit
http://go.microsoft.com/fwlink/?LinkID=391919
-->
<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata">
<Library Name="ObdLibUWP">
<!-- add directives for your library here -->
</Library>
</Directives>

View File

@ -0,0 +1,16 @@
{
"dependencies": {
"Microsoft.NETCore.UniversalWindowsPlatform": "5.0.0"
},
"frameworks": {
"uap10.0": {}
},
"runtimes": {
"win10-arm": {},
"win10-arm-aot": {},
"win10-x86": {},
"win10-x86-aot": {},
"win10-x64": {},
"win10-x64-aot": {}
}
}