Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 | |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 17 | #ifndef android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_ |
| 18 | #define android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_ |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 19 | |
Enrico Granata | adcb7c7 | 2017-01-25 12:07:15 -0800 | [diff] [blame] | 20 | #include <memory> |
| 21 | |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 22 | #include <VehicleHal.h> |
| 23 | #include <impl/DefaultConfig.h> |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | #include <thread> |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 26 | #include <utils/SystemClock.h> |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 27 | #include <vehicle_hal_manager/Obd2SensorStore.h> |
| 28 | #include "VehicleHalProto.pb.h" |
| 29 | |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 30 | |
| 31 | namespace android { |
| 32 | namespace hardware { |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 33 | namespace automotive { |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 34 | namespace vehicle { |
| 35 | namespace V2_0 { |
| 36 | |
| 37 | namespace impl { |
| 38 | |
| 39 | class DefaultVehicleHal : public VehicleHal { |
| 40 | public: |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 41 | DefaultVehicleHal() : mThread() {} |
| 42 | ~DefaultVehicleHal() override { |
| 43 | // Notify thread to finish and wait for it to terminate |
| 44 | mExit = 1; |
| 45 | |
| 46 | // Close emulator socket if it is open |
| 47 | { |
| 48 | std::lock_guard<std::mutex> lock(mTxMutex); |
| 49 | if (mCurSocket != -1) { |
| 50 | close(mCurSocket); |
| 51 | mCurSocket = -1; |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | mThread.join(); |
| 56 | } |
| 57 | |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 58 | std::vector<VehiclePropConfig> listProperties() override { |
| 59 | return std::vector<VehiclePropConfig>(std::begin(kVehicleProperties), |
| 60 | std::end(kVehicleProperties)); |
| 61 | } |
| 62 | |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 63 | VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue, |
| 64 | StatusCode* outStatus) override; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 65 | |
Enrico Granata | adcb7c7 | 2017-01-25 12:07:15 -0800 | [diff] [blame] | 66 | void onCreate() override; |
| 67 | |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 68 | StatusCode set(const VehiclePropValue& propValue) override; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 69 | |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 70 | StatusCode subscribe(int32_t property, int32_t areas, float sampleRate) { |
| 71 | ALOGD("%s: not implemented: prop=0x%x, areas=0x%x, rate=%f", __FUNCTION__, property, |
| 72 | areas, sampleRate); |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 73 | return StatusCode::OK; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 74 | } |
| 75 | |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 76 | StatusCode unsubscribe(int32_t property) { |
| 77 | ALOGD("%s: not implemented: prop=0x%x", __FUNCTION__, property); |
Pavel Maltsev | db179c5 | 2016-10-27 15:43:06 -0700 | [diff] [blame] | 78 | return StatusCode::OK; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | private: |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 82 | void doGetConfig(emulator::EmulatorMessage& rxMsg, emulator::EmulatorMessage& respMsg); |
| 83 | void doGetConfigAll(emulator::EmulatorMessage& rxMsg, emulator::EmulatorMessage& respMsg); |
| 84 | void doGetProperty(emulator::EmulatorMessage& rxMsg, emulator::EmulatorMessage& respMsg); |
| 85 | void doGetPropertyAll(emulator::EmulatorMessage& rxMsg, emulator::EmulatorMessage& respMsg); |
| 86 | void doSetProperty(emulator::EmulatorMessage& rxMsg, emulator::EmulatorMessage& respMsg); |
| 87 | VehiclePropValue* getVehiclePropValueLocked(int32_t propId, int32_t areaId); |
Enrico Granata | 37e69d7 | 2017-02-08 18:09:45 -0800 | [diff] [blame^] | 88 | void initObd2LiveFrame(VehiclePropConfig& propConfig); |
| 89 | void initObd2FreezeFrame(VehiclePropConfig& propConfig); |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 90 | void parseRxProtoBuf(std::vector<uint8_t>& msg); |
| 91 | void populateProtoVehicleConfig(emulator::VehiclePropConfig* protoCfg, |
| 92 | const VehiclePropConfig& cfg); |
| 93 | void populateProtoVehiclePropValue(emulator::VehiclePropValue* protoVal, |
| 94 | const VehiclePropValue* val); |
| 95 | void setDefaultValue(VehiclePropValue* prop); |
| 96 | void rxMsg(void); |
| 97 | void rxThread(void); |
| 98 | void txMsg(emulator::EmulatorMessage& txMsg); |
| 99 | StatusCode updateProperty(const VehiclePropValue& propValue); |
Enrico Granata | 37e69d7 | 2017-02-08 18:09:45 -0800 | [diff] [blame^] | 100 | StatusCode fillObd2LiveFrame(VehiclePropValue* v); |
| 101 | StatusCode fillObd2FreezeFrame(const VehiclePropValue& requestedPropValue, |
| 102 | VehiclePropValue* v); |
| 103 | StatusCode fillObd2DtcInfo(VehiclePropValue *v); |
| 104 | StatusCode clearObd2FreezeFrames(const VehiclePropValue& propValue); |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 105 | private: |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 106 | // TODO: Use a hashtable to support indexing props |
| 107 | std::vector<std::unique_ptr<VehiclePropValue>> mProps; |
| 108 | std::atomic<int> mCurSocket; |
| 109 | std::atomic<int> mExit; |
Enrico Granata | 37e69d7 | 2017-02-08 18:09:45 -0800 | [diff] [blame^] | 110 | std::unique_ptr<VehiclePropValue> mLiveObd2Frame {nullptr}; |
| 111 | std::vector<std::unique_ptr<VehiclePropValue>> mFreezeObd2Frames; |
Steve Paik | 56c0c84 | 2017-01-24 18:08:38 -0800 | [diff] [blame] | 112 | std::mutex mPropsMutex; |
| 113 | int mSocket; |
| 114 | std::mutex mTxMutex; |
| 115 | std::thread mThread; |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 116 | }; |
| 117 | |
| 118 | } // impl |
| 119 | |
| 120 | } // namespace V2_0 |
| 121 | } // namespace vehicle |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 122 | } // namespace automotive |
Pavel Maltsev | e2603e3 | 2016-10-25 16:03:23 -0700 | [diff] [blame] | 123 | } // namespace hardware |
| 124 | } // namespace android |
| 125 | |
| 126 | |
Pavel Maltsev | 2579fb7 | 2017-02-02 12:39:36 -0800 | [diff] [blame] | 127 | #endif // android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_ |