blob: 4b89f5547809b2d7b21fa9e384f509307f87bb36 [file] [log] [blame]
Pavel Maltseve2603e32016-10-25 16:03:23 -07001/*
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 Maltsev2579fb72017-02-02 12:39:36 -080017#ifndef android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_
18#define android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_
Pavel Maltseve2603e32016-10-25 16:03:23 -070019
Enrico Granataadcb7c72017-01-25 12:07:15 -080020#include <memory>
21
Pavel Maltseve2603e32016-10-25 16:03:23 -070022#include <VehicleHal.h>
23#include <impl/DefaultConfig.h>
Steve Paik56c0c842017-01-24 18:08:38 -080024#include <sys/socket.h>
25#include <thread>
Pavel Maltseve2603e32016-10-25 16:03:23 -070026#include <utils/SystemClock.h>
Steve Paik56c0c842017-01-24 18:08:38 -080027#include <vehicle_hal_manager/Obd2SensorStore.h>
28#include "VehicleHalProto.pb.h"
29
Pavel Maltseve2603e32016-10-25 16:03:23 -070030
31namespace android {
32namespace hardware {
Pavel Maltsev2579fb72017-02-02 12:39:36 -080033namespace automotive {
Pavel Maltseve2603e32016-10-25 16:03:23 -070034namespace vehicle {
35namespace V2_0 {
36
37namespace impl {
38
39class DefaultVehicleHal : public VehicleHal {
40public:
Steve Paik56c0c842017-01-24 18:08:38 -080041 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 Maltseve2603e32016-10-25 16:03:23 -070058 std::vector<VehiclePropConfig> listProperties() override {
59 return std::vector<VehiclePropConfig>(std::begin(kVehicleProperties),
60 std::end(kVehicleProperties));
61 }
62
Pavel Maltsevdb179c52016-10-27 15:43:06 -070063 VehiclePropValuePtr get(const VehiclePropValue& requestedPropValue,
64 StatusCode* outStatus) override;
Pavel Maltseve2603e32016-10-25 16:03:23 -070065
Enrico Granataadcb7c72017-01-25 12:07:15 -080066 void onCreate() override;
67
Pavel Maltsevdb179c52016-10-27 15:43:06 -070068 StatusCode set(const VehiclePropValue& propValue) override;
Pavel Maltseve2603e32016-10-25 16:03:23 -070069
Steve Paik56c0c842017-01-24 18:08:38 -080070 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 Maltsevdb179c52016-10-27 15:43:06 -070073 return StatusCode::OK;
Pavel Maltseve2603e32016-10-25 16:03:23 -070074 }
75
Steve Paik56c0c842017-01-24 18:08:38 -080076 StatusCode unsubscribe(int32_t property) {
77 ALOGD("%s: not implemented: prop=0x%x", __FUNCTION__, property);
Pavel Maltsevdb179c52016-10-27 15:43:06 -070078 return StatusCode::OK;
Pavel Maltseve2603e32016-10-25 16:03:23 -070079 }
80
81private:
Steve Paik56c0c842017-01-24 18:08:38 -080082 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 Granata37e69d72017-02-08 18:09:45 -080088 void initObd2LiveFrame(VehiclePropConfig& propConfig);
89 void initObd2FreezeFrame(VehiclePropConfig& propConfig);
Steve Paik56c0c842017-01-24 18:08:38 -080090 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 Granata37e69d72017-02-08 18:09:45 -0800100 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 Maltseve2603e32016-10-25 16:03:23 -0700105private:
Steve Paik56c0c842017-01-24 18:08:38 -0800106 // 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 Granata37e69d72017-02-08 18:09:45 -0800110 std::unique_ptr<VehiclePropValue> mLiveObd2Frame {nullptr};
111 std::vector<std::unique_ptr<VehiclePropValue>> mFreezeObd2Frames;
Steve Paik56c0c842017-01-24 18:08:38 -0800112 std::mutex mPropsMutex;
113 int mSocket;
114 std::mutex mTxMutex;
115 std::thread mThread;
Pavel Maltseve2603e32016-10-25 16:03:23 -0700116};
117
118} // impl
119
120} // namespace V2_0
121} // namespace vehicle
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800122} // namespace automotive
Pavel Maltseve2603e32016-10-25 16:03:23 -0700123} // namespace hardware
124} // namespace android
125
126
Pavel Maltsev2579fb72017-02-02 12:39:36 -0800127#endif // android_hardware_automotive_vehicle_V2_0_impl_DefaultVehicleHal_H_