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 22namespace android { 23namespace hardware { 24namespace automotive { 25namespace vehicle { 26namespace V2_1 { 27 28Obd2SensorStore::BitmaskInVector::BitmaskInVector(size_t numBits) 29{ 30 resize(numBits); 31} 32 33void Obd2SensorStore::BitmaskInVector::resize(size_t numBits) { 34 mStorage = std::vector<uint8_t>((numBits+7)/8, 0); 35} 36 37void 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 46bool 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 53const std::vector<uint8_t>& Obd2SensorStore::BitmaskInVector::getBitmask() const { 54 return mStorage; 55} 56 57Obd2SensorStore::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 69V2_0::StatusCode Obd2SensorStore::setIntegerSensor(Obd2IntegerSensorIndex index, 70 int32_t value) { 71 return setIntegerSensor(V2_0::toInt(index), value); 72} 73V2_0::StatusCode Obd2SensorStore::setFloatSensor(Obd2FloatSensorIndex index, 74 float value) { 75 return setFloatSensor(V2_0::toInt(index), value); 76} 77 78V2_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 84V2_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 90const std::vector<int32_t>& Obd2SensorStore::getIntegerSensors() const { 91 return mIntegerSensors; 92} 93 94const std::vector<float>& Obd2SensorStore::getFloatSensors() const { 95 return mFloatSensors; 96} 97 98const std::vector<uint8_t>& Obd2SensorStore::getSensorsBitmask() const { 99 return mSensorsBitmask.getBitmask(); 100} 101 102void 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