├── README.md
├── pom.xml
└── src
└── main
└── java
└── so
└── mwil
└── obdsim
├── Main.java
├── OBDBluetoothServer.java
├── OBDProtocol.java
├── OBDServer.java
├── obdpid
├── EngineRpm.java
├── IOBDPID.java
└── IntakeManifoldAbsolutePressure.java
└── utilities
└── ResponseUtils.java
/README.md:
--------------------------------------------------------------------------------
1 | # OBD Simulator #
2 |
3 | On-board diagnostics (OBD) is an automotive term referring to a vehicle's self-diagnostic and reporting capability.
4 | This app will simulate a WiFi OBD dongle, allowing development of companion mobile apps, without the need to sit in your car!
5 |
6 | * Creates a TCP socket on your machine on port 35000.
7 | * Currently will only simulate wifi connections, we need to figure out how to add bluetooth functionality
8 |
9 | ### How do I get set up? ###
10 |
11 | * You need to install java 7 jdk
12 | * Maven is used to download dependencies and build
13 | * Developed in IntelliJ Comminity Edition
14 |
15 | ### Supported PIDS ###
16 | Currently the simulator supports these OBD PIDS:
17 |
18 | * 0B - Intake Manifold Absolute Pressure
19 | * 0C - Engine RPM
20 |
21 | full list of pids can be viewed on wikipedia - http://en.wikipedia.org/wiki/OBD-II_PIDs#Mode_01
22 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | so.mwil
8 | obdsim
9 | 1.0-SNAPSHOT
10 |
11 |
12 |
13 | pyx4me-web-snapshot
14 | http://www.pyx4me.com/maven2-snapshot
15 |
16 | true
17 |
18 |
19 | false
20 |
21 |
22 |
23 |
24 |
25 |
26 | net.sf.bluecove
27 | bluecove
28 | 2.1.1-SNAPSHOT
29 |
30 |
31 | net.sf.bluecove
32 | bluecove-gpl
33 | 2.1.1-SNAPSHOT
34 | runtime
35 |
36 |
37 |
38 |
39 |
40 |
41 | org.codehaus.mojo
42 | exec-maven-plugin
43 | 1.3.2
44 |
45 | so.mwil.obdsim.Main
46 |
47 |
48 |
49 |
50 | java
51 |
52 |
53 |
54 |
55 |
56 |
57 |
58 |
59 |
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/Main.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim;
2 |
3 | /**
4 | * Created by matthew on 30/10/14.
5 | */
6 | public class Main {
7 |
8 | public static boolean USE_BLUETOOTH = false;
9 | public static int SERVER_PORT = 35000;
10 |
11 | public static void main(String[] args) {
12 | int restartCount = 10;
13 |
14 | while(restartCount > 0) {
15 | restartCount--;
16 |
17 | if(USE_BLUETOOTH) {
18 | OBDBluetoothServer server = new OBDBluetoothServer();
19 | server.start();
20 | } else {
21 | OBDServer server = new OBDServer();
22 | server.start(SERVER_PORT);
23 | }
24 | }
25 | }
26 |
27 | }
28 |
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/OBDBluetoothServer.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim;
2 |
3 | import so.mwil.obdsim.utilities.ResponseUtils;
4 |
5 | import javax.bluetooth.DiscoveryAgent;
6 | import javax.bluetooth.LocalDevice;
7 | import javax.bluetooth.UUID;
8 | import javax.microedition.io.Connector;
9 | import javax.microedition.io.StreamConnection;
10 | import javax.microedition.io.StreamConnectionNotifier;
11 | import java.io.BufferedReader;
12 | import java.io.InputStream;
13 | import java.io.InputStreamReader;
14 | import java.io.PrintWriter;
15 |
16 | /**
17 | * Created by matthew on 07/11/14.
18 | */
19 | public class OBDBluetoothServer {
20 |
21 | public void start() {
22 | // Retrieve the local Bluetooth device object
23 | LocalDevice local = null;
24 |
25 | StreamConnectionNotifier notifier;
26 | StreamConnection connection = null;
27 |
28 | // Setup the server to listen for connection
29 | try {
30 | local = LocalDevice.getLocalDevice();
31 | local.setDiscoverable(DiscoveryAgent.GIAC);
32 |
33 | UUID uuid = new UUID(80087355); // "04c6093b-0000-1000-8000-00805f9b34fb"
34 | String url = "btspp://localhost:" + uuid.toString() + ";name=RemoteBluetooth";
35 | notifier = (StreamConnectionNotifier) Connector.open(url);
36 | } catch (Exception e) {
37 | e.printStackTrace();
38 | return;
39 | }
40 |
41 | // Waiting for connection
42 | while(true) {
43 | try {
44 | System.out.println("waiting for connection...");
45 | connection = notifier.acceptAndOpen();
46 |
47 | processConnection(connection);
48 |
49 | } catch (Exception e) {
50 | e.printStackTrace();
51 | return;
52 | }
53 | }
54 | }
55 |
56 | public void processConnection(StreamConnection connection) {
57 | try {
58 | PrintWriter out = new PrintWriter(connection.openOutputStream(), true);
59 | BufferedReader in = new BufferedReader(new InputStreamReader(connection.openInputStream()));
60 |
61 | String inputLine, outputLine;
62 |
63 | OBDProtocol obdp = new OBDProtocol();
64 |
65 | System.out.println("Connected to client");
66 |
67 | while ((inputLine = in.readLine()) != null) {
68 |
69 | outputLine = obdp.processInput(inputLine);
70 |
71 | if(outputLine != null) {
72 | out.println(outputLine);
73 | out.println(ResponseUtils.buildOBDEndResponse());
74 | }
75 | }
76 | } catch (Exception e) {
77 | e.printStackTrace();
78 | }
79 | }
80 |
81 | }
82 |
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/OBDProtocol.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim;
2 |
3 | import so.mwil.obdsim.obdpid.EngineRpm;
4 | import so.mwil.obdsim.obdpid.IOBDPID;
5 | import so.mwil.obdsim.obdpid.IntakeManifoldAbsolutePressure;
6 |
7 | import java.util.ArrayList;
8 | import java.util.List;
9 |
10 | /**
11 | * Created by matthew on 30/10/14.
12 | */
13 | public class OBDProtocol {
14 |
15 | private final List supportedPIDS;
16 |
17 | public OBDProtocol() {
18 | supportedPIDS = new ArrayList();
19 |
20 | // Add some PIDs
21 | supportedPIDS.add(new IntakeManifoldAbsolutePressure());
22 | supportedPIDS.add(new EngineRpm());
23 | }
24 |
25 | public String processInput(String inputLine) {
26 |
27 | if(inputLine != null && inputLine.length() == 4) {
28 | final String mode = getModeFromRequest(inputLine);
29 | final String code = getCodeFromRequest(inputLine);
30 |
31 | System.out.println("Received Command: " + inputLine);
32 |
33 | for(IOBDPID pid : supportedPIDS) {
34 | if(pid.getCode().equals(code)) {
35 | //simulateDelay(500);
36 | return pid.generateResponse(mode);
37 | }
38 | }
39 | }
40 |
41 | return null;
42 | }
43 |
44 | private void simulateDelay(int milliseconds) {
45 | try {
46 | Thread.sleep(milliseconds);
47 | } catch (InterruptedException e) {
48 | e.printStackTrace();
49 | }
50 | }
51 |
52 | private String getModeFromRequest(String inputLine) {
53 | return inputLine.substring(0, 2);
54 | }
55 |
56 | private String getCodeFromRequest(String inputLine) {
57 | return inputLine.substring(2, 4);
58 | }
59 | }
60 |
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/OBDServer.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim;
2 |
3 | import so.mwil.obdsim.utilities.ResponseUtils;
4 |
5 | import java.io.BufferedReader;
6 | import java.io.IOException;
7 | import java.io.InputStreamReader;
8 | import java.io.PrintWriter;
9 | import java.net.InetAddress;
10 | import java.net.ServerSocket;
11 | import java.net.Socket;
12 |
13 | /**
14 | * Created by matthew on 30/10/14.
15 | */
16 | public class OBDServer {
17 |
18 | public void start(int portNumber) {
19 | try {
20 | ServerSocket serverSocket = new ServerSocket(portNumber);
21 |
22 | InetAddress IP = InetAddress.getLocalHost();
23 | System.out.println("Simulator is running on : " + IP.getHostAddress() + " port number : " + portNumber);
24 |
25 | Socket clientSocket = serverSocket.accept();
26 | PrintWriter out = new PrintWriter(clientSocket.getOutputStream(), true);
27 | BufferedReader in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
28 |
29 | String inputLine, outputLine;
30 |
31 | OBDProtocol obdp = new OBDProtocol();
32 |
33 | System.out.println("Connected to client");
34 |
35 | while ((inputLine = in.readLine()) != null) {
36 |
37 | outputLine = obdp.processInput(inputLine);
38 |
39 | if(outputLine != null) {
40 | out.println(outputLine);
41 | out.println(ResponseUtils.buildOBDEndResponse());
42 | } else {
43 | out.println("No response for command");
44 | }
45 | }
46 |
47 | clientSocket.close();
48 | serverSocket.close();
49 |
50 | } catch (IOException e) {
51 | e.printStackTrace();
52 | }
53 | }
54 | }
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/obdpid/EngineRpm.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim.obdpid;
2 |
3 | import so.mwil.obdsim.utilities.ResponseUtils;
4 |
5 | /**
6 | * Mode 01 - Engine RPM
7 | * Min Value: 0
8 | * Max Value: 16,383.75
9 | * Units: rpm
10 | *
11 | * Created by amouly on 6/17/15.
12 | */
13 | public class EngineRpm implements IOBDPID {
14 |
15 | // Set standard values
16 | private final String unit = "rpm";
17 | private final String code = "0C";
18 | private final String minValue = "0";
19 | private final String maxValue = "16,383.75";
20 |
21 | // Set random value
22 | private final Integer value = 1200;
23 |
24 | @Override
25 | public String getCode() {
26 | return code;
27 | }
28 |
29 | @Override
30 | public String generateResponse(String mode) {
31 | return ResponseUtils.buildOBDResponse(mode, getCode(), Integer.toHexString(value));
32 | }
33 |
34 | @Override
35 | public String getUnit() {
36 | return unit;
37 | }
38 |
39 | @Override
40 | public String getMinValue() {
41 | return minValue;
42 | }
43 |
44 | @Override
45 | public String getMaxValue() {
46 | return maxValue;
47 | }
48 |
49 | @Override
50 | public String toString() {
51 | return "Engine RPM";
52 | }
53 | }
54 |
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/obdpid/IOBDPID.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim.obdpid;
2 |
3 | /**
4 | * Created by matthew on 01/11/14.
5 | */
6 | public interface IOBDPID {
7 |
8 | String getCode();
9 |
10 | String generateResponse(String mode);
11 |
12 | String getUnit();
13 |
14 | String getMinValue();
15 |
16 | String getMaxValue();
17 | }
18 |
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/obdpid/IntakeManifoldAbsolutePressure.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim.obdpid;
2 |
3 | import so.mwil.obdsim.utilities.ResponseUtils;
4 |
5 | /**
6 | * Mode 01 - Intake manifold absolute pressure
7 | * Min Value: 0
8 | * Max Value: 255
9 | * Units: kPa (absolute)
10 | *
11 | * Created by matthew on 01/11/14.
12 | */
13 | public class IntakeManifoldAbsolutePressure implements IOBDPID {
14 |
15 | // Set standard values
16 | private final String unit = "kPa";
17 | private final String code = "0B";
18 | private final String minValue = "0";
19 | private final String maxValue = "255";
20 |
21 | private int value = 96;
22 | private int offBoost = 0;
23 |
24 | @Override
25 | public String getCode() {
26 | return code;
27 | }
28 |
29 | @Override
30 | public String generateResponse(String mode) {
31 |
32 | if(offBoost > 0) {
33 | offBoost--;
34 |
35 | return ResponseUtils.buildOBDResponse(mode, getCode(), Integer.toHexString(96));
36 | } else {
37 | value++;
38 |
39 | if(value >= 337) {
40 | offBoost = 200;
41 | value = 96;
42 | }
43 |
44 | return ResponseUtils.buildOBDResponse(mode, getCode(), Integer.toHexString(value));
45 | }
46 | }
47 |
48 | @Override
49 | public String getUnit() {
50 | return unit;
51 | }
52 |
53 | @Override
54 | public String getMinValue() {
55 | return minValue;
56 | }
57 |
58 | @Override
59 | public String getMaxValue() {
60 | return maxValue;
61 | }
62 |
63 | @Override
64 | public String toString() {
65 | return "Intake manifold absolute pressure";
66 | }
67 | }
68 |
--------------------------------------------------------------------------------
/src/main/java/so/mwil/obdsim/utilities/ResponseUtils.java:
--------------------------------------------------------------------------------
1 | package so.mwil.obdsim.utilities;
2 |
3 | /**
4 | * Created by matthew on 01/11/14.
5 | */
6 | public class ResponseUtils {
7 |
8 | public static String buildOBDResponse(String mode, String command, String value) {
9 | String responseMode = convertRequestModeToResponseMode(mode);
10 |
11 | String response = responseMode + " " + command + " " + value;
12 |
13 | return mode + command + "\r\r\n" + response + "\r\n";
14 | }
15 |
16 | public static String buildOBDEndResponse() {
17 | return ">";
18 | }
19 |
20 | private static String convertRequestModeToResponseMode(String requestMode) {
21 | return requestMode.replace("0", "4");
22 | }
23 |
24 | }
25 |
--------------------------------------------------------------------------------