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_1 {
27
BitmaskInVector(size_t numBits)28 Obd2SensorStore::BitmaskInVector::BitmaskInVector(size_t numBits)
29 {
30 resize(numBits);
31 }
32
resize(size_t numBits)33 void Obd2SensorStore::BitmaskInVector::resize(size_t numBits) {
34 mStorage = std::vector<uint8_t>((numBits+7)/8, 0);
35 }
36
set(size_t index,bool value)37 void Obd2SensorStore::BitmaskInVector::set(size_t index, bool value) {
38 const size_t byteIndex = index / 8;
39 const size_t bitIndex = index % 8;
40 const uint8_t byte = mStorage[byteIndex];
41 uint8_t newValue = value ? (byte | (1 << bitIndex)) :
42 (byte & ~(1 << bitIndex));
43 mStorage[byteIndex] = newValue;
44 }
45
get(size_t index) const46 bool Obd2SensorStore::BitmaskInVector::get(size_t index) const {
47 const size_t byteIndex = index / 8;
48 const size_t bitIndex = index % 8;
49 const uint8_t byte = mStorage[byteIndex];
50 return (byte & (1 << bitIndex)) != 0;
51 }
52
getBitmask() const53 const std::vector<uint8_t>& Obd2SensorStore::BitmaskInVector::getBitmask() const {
54 return mStorage;
55 }
56
Obd2SensorStore(size_t numVendorIntegerSensors,size_t numVendorFloatSensors)57 Obd2SensorStore::Obd2SensorStore(size_t numVendorIntegerSensors,
58 size_t numVendorFloatSensors) {
59 // because the last index is valid *inclusive*
60 const size_t numSystemIntegerSensors = V2_0::toInt(Obd2IntegerSensorIndex::LAST_SYSTEM_INDEX)+1;
61 const size_t numSystemFloatSensors = V2_0::toInt(Obd2FloatSensorIndex::LAST_SYSTEM_INDEX)+1;
62 mIntegerSensors = std::vector<int32_t>(
63 numSystemIntegerSensors+numVendorIntegerSensors, 0);
64 mFloatSensors = std::vector<float>(
65 numSystemFloatSensors+numVendorFloatSensors, 0);
66 mSensorsBitmask.resize(mIntegerSensors.size()+mFloatSensors.size());
67 }
68
setIntegerSensor(Obd2IntegerSensorIndex index,int32_t value)69 V2_0::StatusCode Obd2SensorStore::setIntegerSensor(Obd2IntegerSensorIndex index,
70 int32_t value) {
71 return setIntegerSensor(V2_0::toInt(index), value);
72 }
setFloatSensor(Obd2FloatSensorIndex index,float value)73 V2_0::StatusCode Obd2SensorStore::setFloatSensor(Obd2FloatSensorIndex index,
74 float value) {
75 return setFloatSensor(V2_0::toInt(index), value);
76 }
77
setIntegerSensor(size_t index,int32_t value)78 V2_0::StatusCode Obd2SensorStore::setIntegerSensor(size_t index, int32_t value) {
79 mIntegerSensors[index] = value;
80 mSensorsBitmask.set(index, true);
81 return V2_0::StatusCode::OK;
82 }
83
setFloatSensor(size_t index,float value)84 V2_0::StatusCode Obd2SensorStore::setFloatSensor(size_t index, float value) {
85 mFloatSensors[index] = value;
86 mSensorsBitmask.set(index + mIntegerSensors.size(), true);
87 return V2_0::StatusCode::OK;
88 }
89
getIntegerSensors() const90 const std::vector<int32_t>& Obd2SensorStore::getIntegerSensors() const {
91 return mIntegerSensors;
92 }
93
getFloatSensors() const94 const std::vector<float>& Obd2SensorStore::getFloatSensors() const {
95 return mFloatSensors;
96 }
97
getSensorsBitmask() const98 const std::vector<uint8_t>& Obd2SensorStore::getSensorsBitmask() const {
99 return mSensorsBitmask.getBitmask();
100 }
101
fillPropValue(const std::string & dtc,V2_0::VehiclePropValue * propValue) const102 void Obd2SensorStore::fillPropValue(const std::string& dtc,
103 V2_0::VehiclePropValue *propValue) const {
104 propValue->timestamp = elapsedRealtimeNano();
105 propValue->value.int32Values = getIntegerSensors();
106 propValue->value.floatValues = getFloatSensors();
107 propValue->value.bytes = getSensorsBitmask();
108 propValue->value.stringValue = dtc;
109 }
110
111
112
113 } // namespace V2_0
114 } // namespace vehicle
115 } // namespace automotive
116 } // namespace hardware
117 } // namespace android
118