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 
17 #ifndef android_hardware_automotive_vehicle_V2_1_Obd2SensorStore_H_
18 #define android_hardware_automotive_vehicle_V2_1_Obd2SensorStore_H_
19 
20 #include <vector>
21 
22 #include <android/hardware/automotive/vehicle/2.1/types.h>
23 
24 namespace android {
25 namespace hardware {
26 namespace automotive {
27 namespace vehicle {
28 namespace V2_1 {
29 
30 // This class wraps all the logic required to create an OBD2 frame.
31 // It allows storing sensor values, setting appropriate bitmasks as needed,
32 // and returning appropriately laid out storage of sensor values suitable
33 // for being returned via a VehicleHal implementation.
34 class Obd2SensorStore {
35 public:
36     // Creates a sensor storage with a given number of vendor-specific sensors.
37     Obd2SensorStore(size_t numVendorIntegerSensors,
38                     size_t numVendorFloatSensors);
39 
40     // Stores an integer-valued sensor.
41     V2_0::StatusCode setIntegerSensor(Obd2IntegerSensorIndex index, int32_t value);
42     // Stores an integer-valued sensor.
43     V2_0::StatusCode setIntegerSensor(size_t index, int32_t value);
44 
45     // Stores a float-valued sensor.
46     V2_0::StatusCode setFloatSensor(Obd2FloatSensorIndex index, float value);
47     // Stores a float-valued sensor.
48     V2_0::StatusCode setFloatSensor(size_t index, float value);
49 
50     // Returns a vector that contains all integer sensors stored.
51     const std::vector<int32_t>& getIntegerSensors() const;
52     // Returns a vector that contains all float sensors stored.
53     const std::vector<float>& getFloatSensors() const;
54     // Returns a vector that contains a bitmask for all stored sensors.
55     const std::vector<uint8_t>& getSensorsBitmask() const;
56 
57     // Given a stringValue, fill in a VehiclePropValue
58     void fillPropValue(const std::string& dtc, V2_0::VehiclePropValue *propValue) const;
59 
60 private:
61     class BitmaskInVector {
62     public:
63         BitmaskInVector(size_t numBits = 0);
64         void resize(size_t numBits);
65         bool get(size_t index) const;
66         void set(size_t index, bool value);
67 
68         const std::vector<uint8_t>& getBitmask() const;
69 
70     private:
71         std::vector<uint8_t> mStorage;
72     };
73 
74     std::vector<int32_t> mIntegerSensors;
75     std::vector<float> mFloatSensors;
76     BitmaskInVector mSensorsBitmask;
77 };
78 
79 }  // namespace V2_1
80 }  // namespace vehicle
81 }  // namespace automotive
82 }  // namespace hardware
83 }  // namespace android
84 
85 #endif  // android_hardware_automotive_vehicle_V2_0_Obd2SensorStore_H_
86