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,61 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProductVersion>8.0.30703</ProductVersion>
<SchemaVersion>2.0</SchemaVersion>
<ProjectGuid>{8B136D60-8E68-4871-BF25-52E27C61E8E9}</ProjectGuid>
<ProjectTypeGuids>{FEACFBD2-3405-455C-9665-78FE426C6842};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<OutputType>Library</OutputType>
<RootNamespace>ObdLibiOS</RootNamespace>
<IPhoneResourcePrefix>Resources</IPhoneResourcePrefix>
<AssemblyName>ObdLibiOS</AssemblyName>
</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>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>full</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release</OutputPath>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
<ConsolePause>false</ConsolePause>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'XTC|AnyCPU'">
<OutputPath>bin\XTC\</OutputPath>
<DefineConstants>__UNIFIED__;__MOBILE__;__IOS__;</DefineConstants>
<Optimize>true</Optimize>
<DebugType>full</DebugType>
<PlatformTarget>AnyCPU</PlatformTarget>
<UseVSHostingProcess>false</UseVSHostingProcess>
<ErrorReport>prompt</ErrorReport>
<CodeAnalysisRuleSet>MinimumRecommendedRules.ruleset</CodeAnalysisRuleSet>
</PropertyGroup>
<ItemGroup>
<Reference Include="System" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="Xamarin.iOS" />
</ItemGroup>
<ItemGroup>
<Folder Include="Resources\" />
</ItemGroup>
<ItemGroup>
<Compile Include="ObdWrapper.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<ProjectReference Include="..\ObdShare\ObdShare.csproj">
<Project>{7a88da9b-a374-4de2-b46e-ed2c45434508}</Project>
<Name>ObdShare</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildExtensionsPath)\Xamarin\iOS\Xamarin.iOS.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 System.Text;
using System.Net;
using System.Net.Sockets;
using System.Threading.Tasks;
using System.IO;
using System.Net.NetworkInformation;
namespace ObdLibiOS
{
public class ObdWrapper
{
const uint BufSize = 1024;
const int Interval = 100;
const string DefValue = "-255";
private readonly Object _lock = new Object();
private bool connected = true;
private Dictionary<string, string> data;
private IPAddress ipAddress;
private IPEndPoint ipEndPoint;
private Dictionary<string, string> piDs;
private int port;
private bool running = true;
private bool simulatormode;
private Socket socket;
private Stream stream;
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;
}
bool isObdReaderAvailable = false;
foreach (var netInterface in NetworkInterface.GetAllNetworkInterfaces())
{
if (netInterface.NetworkInterfaceType == NetworkInterfaceType.Wireless80211
|| netInterface.NetworkInterfaceType == NetworkInterfaceType.Ethernet)
{
foreach (var addrInfo in netInterface.GetIPProperties().UnicastAddresses)
{
var ipaddr = addrInfo.Address;
if (ipaddr.ToString().StartsWith("192.168.0"))
{
isObdReaderAvailable = true;
break;
}
}
}
}
if (!isObdReaderAvailable)
{
socket = null;
running = false;
connected = false;
return false;
}
if (!ConnectSocket())
{
socket = null;
running = false;
connected = false;
return false;
}
if (connected)
{
//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;
}
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 void PollObd()
{
try
{
string s;
if (simulatormode)
s = "SIMULATORIPHONE12";
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 (stream != null)
{
stream.Close();
stream = null;
}
if (socket != null)
{
socket.Close();
socket = null;
}
}
}
private async Task<string> GetVIN()
{
var result = await SendAndReceive("0902\r");
if (result.StartsWith("49"))
{
while (!result.Contains("49 02 05"))
{
string tmp = await ReceiveAsync();
result += tmp;
}
}
return ObdShare.ObdUtil.ParseVINMsg(result);
}
private bool ConnectSocket()
{
//setup the connection via socket
ipAddress = IPAddress.Parse("192.168.0.10"); //hard coded in obdlink MX
port = 35000; //hard coded in obdlink MX
ipEndPoint = new IPEndPoint(ipAddress, port);
socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
try
{
socket.Connect(ipEndPoint);
stream = new NetworkStream(socket);
connected = true;
}
catch (Exception)
{
connected = false;
return false;
}
return true;
}
private async Task<string> SendAndReceive(string msg)
{
try
{
await WriteAsync(msg);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
System.Threading.Thread.Sleep(100);
try
{
string s = await ReceiveAsync();
System.Diagnostics.Debug.WriteLine("Received: " + s);
s = s.Replace("SEARCHING...\r\n", "");
return s;
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
return "";
}
private async Task WriteAsync(string msg)
{
System.Diagnostics.Debug.WriteLine(msg);
byte[] buffer = Encoding.ASCII.GetBytes(msg);
await stream.WriteAsync(buffer, 0, buffer.Length);
stream.Flush();
}
private async Task<string> ReceiveAsync()
{
string ret = await ReceiveAsyncRaw();
while (!ret.Trim().EndsWith(">"))
{
string tmp = await ReceiveAsyncRaw();
ret = ret + tmp;
}
return ret;
}
private async Task<string> ReceiveAsyncRaw()
{
byte[] buffer = new byte[BufSize];
var bytes = await stream.ReadAsync(buffer, 0, buffer.Length);
var s = Encoding.ASCII.GetString(buffer, 0, bytes);
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 (stream != null)
{
stream.Dispose();
stream.Close();
stream = null;
}
if (socket != null)
{
try
{
socket.Shutdown(SocketShutdown.Both);
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine(ex.Message);
}
socket.Close();
socket = null;
}
}
}
}

View File

@ -0,0 +1,42 @@
// 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("ObdLibiOS")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("ObdLibiOS")]
[assembly: AssemblyCopyright("Copyright © 2016")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("8b136d60-8e68-4871-bf25-52e27c61e8e9")]
// 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")]