summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorEnrico Granata2017-03-21 19:31:23 -0500
committerEnrico Granata2017-03-22 12:55:25 -0500
commit87c0545b6f0814657df555902300d7b3dfe6b96f (patch)
tree8fd48224b22e024d6dd941e6c40d8c5da3f6bfa9 /obd2-lib
parent0cad3236e5f46abeb211cca11b5ba3f68f679d6a (diff)
downloadplatform-packages-services-car-87c0545b6f0814657df555902300d7b3dfe6b96f.tar.gz
platform-packages-services-car-87c0545b6f0814657df555902300d7b3dfe6b96f.tar.xz
platform-packages-services-car-87c0545b6f0814657df555902300d7b3dfe6b96f.zip
Add logic to obd2_lib to generate a live OBD2 frame from a connection.
This commit adds a few main moving parts: - logic to intersect the sensor types supported by obd2_lib with those supported by a vehicle; - logic to collect all such supported sensors in one operation, and store the result in a JSON object; - logic to create a VehiclePropValue from JSON data. Test: runtest -x packages/services/Car/tests/obd2_test/src/com/android/car/obd2/test/Obd2LiveFrameGeneratorTest.java Change-Id: Ic89cd8b615b11a1ba136df1ac6f336e7882ef1c6
Diffstat (limited to 'obd2-lib')
-rw-r--r--obd2-lib/Android.mk2
-rw-r--r--obd2-lib/src/com/android/car/obd2/Obd2Command.java4
-rw-r--r--obd2-lib/src/com/android/car/obd2/Obd2LiveFrameGenerator.java105
3 files changed, 111 insertions, 0 deletions
diff --git a/obd2-lib/Android.mk b/obd2-lib/Android.mk
index 544dfc14..e992680c 100644
--- a/obd2-lib/Android.mk
+++ b/obd2-lib/Android.mk
@@ -26,6 +26,8 @@ LOCAL_SRC_FILES := $(call all-java-files-under, src)
26LOCAL_MODULE := com.android.car.obd2 26LOCAL_MODULE := com.android.car.obd2
27LOCAL_JAVA_LANGUAGE_VERSION := 1.8 27LOCAL_JAVA_LANGUAGE_VERSION := 1.8
28 28
29LOCAL_STATIC_JAVA_LIBRARIES += vehicle-hal-support-lib \
30
29include $(BUILD_STATIC_JAVA_LIBRARY) 31include $(BUILD_STATIC_JAVA_LIBRARY)
30 32
31endif #TARGET_BUILD_PDK 33endif #TARGET_BUILD_PDK
diff --git a/obd2-lib/src/com/android/car/obd2/Obd2Command.java b/obd2-lib/src/com/android/car/obd2/Obd2Command.java
index e02c6af5..e3366cfd 100644
--- a/obd2-lib/src/com/android/car/obd2/Obd2Command.java
+++ b/obd2-lib/src/com/android/car/obd2/Obd2Command.java
@@ -129,6 +129,10 @@ public abstract class Obd2Command<ValueType> {
129 129
130 public abstract Optional<ValueType> run(Obd2Connection connection) throws Exception; 130 public abstract Optional<ValueType> run(Obd2Connection connection) throws Exception;
131 131
132 public int getPid() {
133 return mSemanticHandler.getPid();
134 }
135
132 public static final <T> LiveFrameCommand<T> getLiveFrameCommand(OutputSemanticHandler handler) { 136 public static final <T> LiveFrameCommand<T> getLiveFrameCommand(OutputSemanticHandler handler) {
133 return new LiveFrameCommand<>(handler); 137 return new LiveFrameCommand<>(handler);
134 } 138 }
diff --git a/obd2-lib/src/com/android/car/obd2/Obd2LiveFrameGenerator.java b/obd2-lib/src/com/android/car/obd2/Obd2LiveFrameGenerator.java
new file mode 100644
index 00000000..5fb45391
--- /dev/null
+++ b/obd2-lib/src/com/android/car/obd2/Obd2LiveFrameGenerator.java
@@ -0,0 +1,105 @@
1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.car.obd2;
18
19import android.os.SystemClock;
20import android.util.JsonWriter;
21import android.util.Log;
22import com.android.car.obd2.Obd2Command.LiveFrameCommand;
23import java.io.IOException;
24import java.util.ArrayList;
25import java.util.List;
26import java.util.Optional;
27import java.util.Set;
28
29public class Obd2LiveFrameGenerator {
30 public static final String TAG = Obd2LiveFrameGenerator.class.getSimpleName();
31
32 private final Obd2Connection mConnection;
33 private final List<LiveFrameCommand<Integer>> mIntegerCommands = new ArrayList<>();
34 private final List<LiveFrameCommand<Float>> mFloatCommands = new ArrayList<>();
35
36 public Obd2LiveFrameGenerator(Obd2Connection connection)
37 throws IOException, InterruptedException {
38 mConnection = connection;
39 Set<Integer> connectionPids = connection.getSupportedPIDs();
40 Set<Integer> apiIntegerPids = Obd2Command.getSupportedIntegerCommands();
41 Set<Integer> apiFloatPids = Obd2Command.getSupportedFloatCommands();
42 apiIntegerPids
43 .stream()
44 .filter(connectionPids::contains)
45 .forEach(
46 (Integer pid) ->
47 mIntegerCommands.add(
48 Obd2Command.getLiveFrameCommand(
49 Obd2Command.getIntegerCommand(pid))));
50 apiFloatPids
51 .stream()
52 .filter(connectionPids::contains)
53 .forEach(
54 (Integer pid) ->
55 mFloatCommands.add(
56 Obd2Command.getLiveFrameCommand(
57 Obd2Command.getFloatCommand(pid))));
58 }
59
60 public JsonWriter generate(JsonWriter jsonWriter) throws IOException {
61 return generate(jsonWriter, SystemClock.elapsedRealtimeNanos());
62 }
63
64 public JsonWriter generate(JsonWriter jsonWriter, long timestamp) throws IOException {
65 jsonWriter.beginObject();
66 jsonWriter.name("timestamp").value(timestamp);
67 jsonWriter.name("intValues").beginArray();
68 for (LiveFrameCommand<Integer> command : mIntegerCommands) {
69 try {
70 Optional<Integer> result = command.run(mConnection);
71 if (result.isPresent()) {
72 jsonWriter.beginObject();
73 jsonWriter.name("id").value(command.getPid());
74 jsonWriter.name("value").value(result.get());
75 jsonWriter.endObject();
76 }
77 } catch (IOException | InterruptedException e) {
78 Log.w(TAG, String.format("unable to retrieve OBD2 pid %d due to exception: %s",
79 command.getPid(), e));
80 // skip this entry
81 }
82 }
83 jsonWriter.endArray();
84
85 jsonWriter.name("floatValues").beginArray();
86 for (LiveFrameCommand<Float> command : mFloatCommands) {
87 try {
88 Optional<Float> result = command.run(mConnection);
89 if (result.isPresent()) {
90 jsonWriter.beginObject();
91 jsonWriter.name("id").value(command.getPid());
92 jsonWriter.name("value").value(result.get());
93 jsonWriter.endObject();
94 }
95 } catch (IOException | InterruptedException e) {
96 Log.w(TAG, String.format("unable to retrieve OBD2 pid %d due to exception: %s",
97 command.getPid(), e));
98 // skip this entry
99 }
100 }
101 jsonWriter.endArray();
102
103 return jsonWriter.endObject();
104 }
105}