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 #include "Obd2SensorStore.h"
18
19 #include <utils/SystemClock.h>
20 #include "VehicleUtils.h"
21
22 namespace android {
23 namespace hardware {
24 namespace automotive {
25 namespace vehicle {
26 namespace V2_0 {
27
BitmaskInVector(size_t numBits)28 Obd2SensorStore::BitmaskInVector::BitmaskInVector(size_t numBits) {
29 resize(numBits);
30 }
31
resize(size_t numBits)32 void Obd2SensorStore::BitmaskInVector::resize(size_t numBits) {
33 mStorage = std::vector<uint8_t>((numBits + 7) / 8, 0);
34 }
35
set(size_t index,bool value)36 void Obd2SensorStore::BitmaskInVector::set(size_t index, bool value) {
37 const size_t byteIndex = index / 8;
38 const size_t bitIndex = index % 8;
39 const uint8_t byte = mStorage[byteIndex];
40 uint8_t newValue = value ? (byte | (1 << bitIndex)) : (byte & ~(1 << bitIndex));
41 mStorage[byteIndex] = newValue;
42 }
43
get(size_t index) const44 bool Obd2SensorStore::BitmaskInVector::get(size_t index) const {
45 const size_t byteIndex = index / 8;
46 const size_t bitIndex = index % 8;
47 const uint8_t byte = mStorage[byteIndex];
48 return (byte & (1 << bitIndex)) != 0;
49 }
50
getBitmask() const51 const std::vector<uint8_t>& Obd2SensorStore::BitmaskInVector::getBitmask() const {
52 return mStorage;
53 }
54
Obd2SensorStore(size_t numVendorIntegerSensors,size_t numVendorFloatSensors)55 Obd2SensorStore::Obd2SensorStore(size_t numVendorIntegerSensors, size_t numVendorFloatSensors) {
56 // because the last index is valid *inclusive*
57 const size_t numSystemIntegerSensors =
58 toInt(DiagnosticIntegerSensorIndex::LAST_SYSTEM_INDEX) + 1;
59 const size_t numSystemFloatSensors = toInt(DiagnosticFloatSensorIndex::LAST_SYSTEM_INDEX) + 1;
60 mIntegerSensors = std::vector<int32_t>(numSystemIntegerSensors + numVendorIntegerSensors, 0);
61 mFloatSensors = std::vector<float>(numSystemFloatSensors + numVendorFloatSensors, 0);
62 mSensorsBitmask.resize(mIntegerSensors.size() + mFloatSensors.size());
63 }
64
setIntegerSensor(DiagnosticIntegerSensorIndex index,int32_t value)65 StatusCode Obd2SensorStore::setIntegerSensor(DiagnosticIntegerSensorIndex index, int32_t value) {
66 return setIntegerSensor(toInt(index), value);
67 }
setFloatSensor(DiagnosticFloatSensorIndex index,float value)68 StatusCode Obd2SensorStore::setFloatSensor(DiagnosticFloatSensorIndex index, float value) {
69 return setFloatSensor(toInt(index), value);
70 }
71
setIntegerSensor(size_t index,int32_t value)72 StatusCode Obd2SensorStore::setIntegerSensor(size_t index, int32_t value) {
73 mIntegerSensors[index] = value;
74 mSensorsBitmask.set(index, true);
75 return StatusCode::OK;
76 }
77
setFloatSensor(size_t index,float value)78 StatusCode Obd2SensorStore::setFloatSensor(size_t index, float value) {
79 mFloatSensors[index] = value;
80 mSensorsBitmask.set(index + mIntegerSensors.size(), true);
81 return StatusCode::OK;
82 }
83
getIntegerSensors() const84 const std::vector<int32_t>& Obd2SensorStore::getIntegerSensors() const {
85 return mIntegerSensors;
86 }
87
getFloatSensors() const88 const std::vector<float>& Obd2SensorStore::getFloatSensors() const {
89 return mFloatSensors;
90 }
91
getSensorsBitmask() const92 const std::vector<uint8_t>& Obd2SensorStore::getSensorsBitmask() const {
93 return mSensorsBitmask.getBitmask();
94 }
95
fillPropValue(const std::string & dtc,VehiclePropValue * propValue) const96 void Obd2SensorStore::fillPropValue(const std::string& dtc, VehiclePropValue* propValue) const {
97 propValue->timestamp = elapsedRealtimeNano();
98 propValue->value.int32Values = getIntegerSensors();
99 propValue->value.floatValues = getFloatSensors();
100 propValue->value.bytes = getSensorsBitmask();
101 propValue->value.stringValue = dtc;
102 }
103
104 } // namespace V2_0
105 } // namespace vehicle
106 } // namespace automotive
107 } // namespace hardware
108 } // namespace android
109