aboutsummaryrefslogtreecommitdiff
path: root/obd2-lib/src/com/android/car/obd2/Obd2Command.java
blob: 3cbbf58e6f7d484f8a39de15865b297990e76a87 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
/*
 * Copyright (C) 2017 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.car.obd2;

import com.android.car.obd2.commands.AmbientAirTemperature;
import com.android.car.obd2.commands.EngineOilTemperature;
import java.io.IOException;
import java.util.HashMap;
import java.util.Optional;
import java.util.Set;

/**
 * Base class of OBD2 command objects that query a "vehicle" and return an individual data point
 * represented as a Java type.
 *
 * @param <ValueType> The Java type that represents the value of this command's output.
 */
public abstract class Obd2Command<ValueType> {

    /**
     * Abstract representation of an object whose job it is to receive the bytes read from the OBD2
     * connection and return a Java representation of a command's value.
     *
     * @param <ValueType>
     */
    public interface OutputSemanticHandler<ValueType> {
        int getPid();

        Optional<ValueType> consume(IntegerArrayStream data);
    }

    public static final int LIVE_FRAME = 1;
    public static final int FREEZE_FRAME = 2;

    private static final HashMap<Integer, OutputSemanticHandler<Integer>>
            SUPPORTED_INTEGER_COMMANDS = new HashMap<>();
    private static final HashMap<Integer, OutputSemanticHandler<Float>> SUPPORTED_FLOAT_COMMANDS =
            new HashMap<>();

    private static void addSupportedIntegerCommand(
            OutputSemanticHandler<Integer> integerOutputSemanticHandler) {
        SUPPORTED_INTEGER_COMMANDS.put(
                integerOutputSemanticHandler.getPid(), integerOutputSemanticHandler);
    }

    private static void addSupportedFloatCommand(
            OutputSemanticHandler<Float> floatOutputSemanticHandler) {
        SUPPORTED_FLOAT_COMMANDS.put(
                floatOutputSemanticHandler.getPid(), floatOutputSemanticHandler);
    }

    public static Set<Integer> getSupportedIntegerCommands() {
        return SUPPORTED_INTEGER_COMMANDS.keySet();
    }

    public static Set<Integer> getSupportedFloatCommands() {
        return SUPPORTED_FLOAT_COMMANDS.keySet();
    }

    public static OutputSemanticHandler<Integer> getIntegerCommand(int pid) {
        return SUPPORTED_INTEGER_COMMANDS.get(pid);
    }

    public static OutputSemanticHandler<Float> getFloatCommand(int pid) {
        return SUPPORTED_FLOAT_COMMANDS.get(pid);
    }

    static {
        addSupportedFloatCommand(new AmbientAirTemperature());
        addSupportedIntegerCommand(new EngineOilTemperature());
    }

    protected final int mMode;
    protected final OutputSemanticHandler<ValueType> mSemanticHandler;

    Obd2Command(int mode, OutputSemanticHandler<ValueType> semanticHandler) {
        mMode = mode;
        mSemanticHandler = semanticHandler;
    }

    public abstract Optional<ValueType> run(Obd2Connection connection) throws Exception;

    public static final <T> LiveFrameCommand<T> getLiveFrameCommand(OutputSemanticHandler handler) {
        return new LiveFrameCommand<>(handler);
    }

    public static final <T> FreezeFrameCommand<T> getFreezeFrameCommand(
            OutputSemanticHandler handler, int frameId) {
        return new FreezeFrameCommand<>(handler, frameId);
    }

    /**
     * An OBD2 command that returns live frame data.
     *
     * @param <ValueType> The Java type that represents the command's result type.
     */
    public static class LiveFrameCommand<ValueType> extends Obd2Command<ValueType> {
        private static final int RESPONSE_MARKER = 0x41;

        LiveFrameCommand(OutputSemanticHandler<ValueType> semanticHandler) {
            super(LIVE_FRAME, semanticHandler);
        }

        public Optional<ValueType> run(Obd2Connection connection)
                throws IOException, InterruptedException {
            String command = String.format("%02X%02X", mMode, mSemanticHandler.getPid());
            int[] data = connection.run(command);
            IntegerArrayStream stream = new IntegerArrayStream(data);
            if (stream.expect(RESPONSE_MARKER, mSemanticHandler.getPid())) {
                return mSemanticHandler.consume(stream);
            }
            return Optional.empty();
        }
    }

    /**
     * An OBD2 command that returns freeze frame data.
     *
     * @param <ValueType> The Java type that represents the command's result type.
     */
    public static class FreezeFrameCommand<ValueType> extends Obd2Command<ValueType> {
        private static final int RESPONSE_MARKER = 0x2;

        private int mFrameId;

        FreezeFrameCommand(OutputSemanticHandler<ValueType> semanticHandler, int frameId) {
            super(FREEZE_FRAME, semanticHandler);
            mFrameId = frameId;
        }

        public Optional<ValueType> run(Obd2Connection connection)
                throws IOException, InterruptedException {
            String command =
                    String.format("%02X%02X %02X", mMode, mSemanticHandler.getPid(), mFrameId);
            int[] data = connection.run(command);
            IntegerArrayStream stream = new IntegerArrayStream(data);
            if (stream.expect(RESPONSE_MARKER, mSemanticHandler.getPid(), mFrameId)) {
                return mSemanticHandler.consume(stream);
            }
            return Optional.empty();
        }
    }
}