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,66 @@
<?xml version="1.0" encoding="utf-8"?>
<Project DefaultTargets="Build" ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{9A377DF7-3E28-4B4B-8BD2-61416F7AEA1F}</ProjectGuid>
<ProjectTypeGuids>{EFBA0AD7-5A72-4C68-AF49-83D382785DCF};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<RootNamespace>ObdLibAndroid</RootNamespace>
<AssemblyName>ObdLibAndroid</AssemblyName>
<TargetFrameworkVersion>v7.1</TargetFrameworkVersion>
<AndroidResgenFile>Resources\Resource.designer.cs</AndroidResgenFile>
<AndroidResgenClass>Resource</AndroidResgenClass>
<MonoAndroidResourcePrefix>Resources</MonoAndroidResourcePrefix>
<MonoAndroidAssetsPrefix>Assets</MonoAndroidAssetsPrefix>
<AndroidUseLatestPlatformSdk>true</AndroidUseLatestPlatformSdk>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug</OutputPath>
<DefineConstants>DEBUG;</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<AndroidLinkMode>None</AndroidLinkMode>
<EmbedAssembliesIntoApk>
</EmbedAssembliesIntoApk>
<AndroidFastDeploymentType>
</AndroidFastDeploymentType>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
<AndroidUseSharedRuntime>false</AndroidUseSharedRuntime>
<EmbedAssembliesIntoApk>
</EmbedAssembliesIntoApk>
<AndroidFastDeploymentType>
</AndroidFastDeploymentType>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Mono.Android" />
</ItemGroup>
<ItemGroup>
<Compile Include="Resources\Resource.designer.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="ObdWrapper.cs" />
</ItemGroup>
<ItemGroup>
<None Include="Resources\AboutResources.txt" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ObdShare\ObdShare.csproj">
<Project>{7a88da9b-a374-4de2-b46e-ed2c45434508}</Project>
<Name>ObdShare</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\Android\Xamarin.Android.CSharp.targets" />
</Project>

View File

@ -0,0 +1,293 @@
// 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 Android.Bluetooth;
using Java.Util;
using System.IO;
using System.Threading.Tasks;
namespace ObdLibAndroid
{
public class ObdWrapper
{
const int Interval = 100;
const string DefValue = "-255";
private static readonly UUID SppUuid = UUID.FromString("00001101-0000-1000-8000-00805F9B34FB");
private readonly Object _lock = new Object();
private BluetoothAdapter bluetoothAdapter;
private BluetoothDevice bluetoothDevice;
private BluetoothSocket bluetoothSocket;
private bool connected = true;
private Dictionary<string, string> data;
private Dictionary<string, string> piDs;
private Stream reader;
private bool running = true;
private bool simulatormode;
private Stream writer;
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)
{
return true;
}
bluetoothAdapter = BluetoothAdapter.DefaultAdapter;
if (bluetoothAdapter == null)
{
System.Diagnostics.Debug.WriteLine("Bluetooth is not available");
return false;
}
try
{
var ba = bluetoothAdapter.BondedDevices;
foreach (var bd in ba)
{
if (bd.Name.ToLower().Contains("obd"))
bluetoothDevice = bd;
}
if (bluetoothDevice == null)
{
return false;
}
bluetoothSocket = bluetoothDevice.CreateRfcommSocketToServiceRecord(SppUuid);
await bluetoothSocket.ConnectAsync();
connected = true;
}
catch (Java.IO.IOException)
{
// Close the socket
try
{
connected = false;
bluetoothSocket.Close();
}
catch (Java.IO.IOException)
{
}
catch (Exception)
{
}
return false;
}
catch (Exception)
{
}
if (connected)
{
reader = bluetoothSocket.InputStream;
writer = bluetoothSocket.OutputStream;
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;
}
public Dictionary<string, string> Read()
{
var ret = new Dictionary<string, string>();
string s;
if (simulatormode)
{
s = "SIMULATORANDROID1";
ret.Add("vin", s);
foreach (var cmd in piDs.Keys)
{
var key = piDs[cmd];
s = ObdShare.ObdUtil.GetEmulatorValue(cmd);
ret.Add(key, s);
}
return ret;
}
if (!simulatormode && bluetoothSocket == null)
{
//if there is no connection
return null;
}
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 void PollObd()
{
try
{
string s;
if (simulatormode)
s = "SIMULATOR12345678";
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 (reader != null)
{
reader.Close();
reader = null;
}
if (writer != null)
{
writer.Close();
writer = null;
}
if (bluetoothSocket != null)
{
bluetoothSocket.Close();
bluetoothSocket = null;
}
}
}
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);
}
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 Task WriteAsync(string msg)
{
System.Diagnostics.Debug.WriteLine(msg);
byte[] buffer = GetBytes(msg);
await writer.WriteAsync(buffer, 0, buffer.Length);
}
private byte[] GetBytes(string str)
{
byte[] bytes = new byte[str.Length*sizeof (char)];
Buffer.BlockCopy(str.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytes;
}
private async Task<string> ReadAsync()
{
string ret = await ReadAsyncRaw();
while (!ret.Trim().EndsWith(">"))
{
string tmp = await ReadAsyncRaw();
ret = ret + tmp;
}
return ret;
}
private async Task<string> ReadAsyncRaw()
{
byte[] buffer = new byte[1024];
var bytes = await reader.ReadAsync(buffer, 0, buffer.Length);
var s1 = new Java.Lang.String(buffer, 0, bytes);
var s = s1.ToString();
System.Diagnostics.Debug.WriteLine(s);
return s;
}
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 (reader != null)
{
reader.Close();
reader = null;
}
if (writer != null)
{
writer.Close();
writer = null;
}
if (bluetoothSocket != null)
{
try
{
bluetoothSocket.Close();
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
bluetoothSocket = null;
}
}
}
}

View File

@ -0,0 +1,32 @@
// 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 Android.App;
// Information about this assembly is defined by the following attributes.
// Change them to the values specific to your project.
[assembly: AssemblyTitle("ObdLibAndroid")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("")]
[assembly: AssemblyCopyright("Microsoft")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// The assembly version has the format "{Major}.{Minor}.{Build}.{Revision}".
// The form "{Major}.{Minor}.*" will automatically update the build and revision,
// and "{Major}.{Minor}.{Build}.*" will update just the revision.
[assembly: AssemblyVersion("1.0.0")]
// The following attributes are used to specify the signing key for the assembly,
// if desired. See the Mono documentation for more information about signing.
//[assembly: AssemblyDelaySign(false)]
//[assembly: AssemblyKeyFile("")]
[assembly: UsesPermission(Android.Manifest.Permission.Bluetooth)]
[assembly: UsesFeature("android.hardware.bluetooth", Required = false)]

View File

@ -0,0 +1,44 @@
Images, layout descriptions, binary blobs and string dictionaries can be included
in your application as resource files. Various Android APIs are designed to
operate on the resource IDs instead of dealing with images, strings or binary blobs
directly.
For example, a sample Android app that contains a user interface layout (main.axml),
an internationalization string table (strings.xml) and some icons (drawable-XXX/icon.png)
would keep its resources in the "Resources" directory of the application:
Resources/
drawable/
icon.png
layout/
main.axml
values/
strings.xml
In order to get the build system to recognize Android resources, set the build action to
"AndroidResource". The native Android APIs do not operate directly with filenames, but
instead operate on resource IDs. When you compile an Android application that uses resources,
the build system will package the resources for distribution and generate a class called "R"
(this is an Android convention) that contains the tokens for each one of the resources
included. For example, for the above Resources layout, this is what the R class would expose:
public class R {
public class drawable {
public const int icon = 0x123;
}
public class layout {
public const int main = 0x456;
}
public class strings {
public const int first_string = 0xabc;
public const int second_string = 0xbcd;
}
}
You would then use R.drawable.icon to reference the drawable/icon.png file, or R.layout.main
to reference the layout/main.axml file, or R.strings.first_string to reference the first
string in the dictionary file values/strings.xml.

View File