1/*
2 * Copyright (C) 2015 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 */
16package com.android.car.hal;
17
18import android.car.CarInfoManager;
19import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
20import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
21import android.hardware.automotive.vehicle.V2_0.VehicleProperty;
22import android.os.Bundle;
23import android.util.Log;
24
25import com.android.car.CarLog;
26
27import java.io.PrintWriter;
28import java.util.Collection;
29import java.util.LinkedList;
30import java.util.List;
31
32public class InfoHalService extends HalServiceBase {
33
34    private final VehicleHal mHal;
35    private Bundle mBasicInfo = new Bundle();
36
37    public InfoHalService(VehicleHal hal) {
38        mHal = hal;
39    }
40
41    @Override
42    public void init() {
43        //nothing to do
44    }
45
46    @Override
47    public synchronized void release() {
48        mBasicInfo = new Bundle();
49    }
50
51    @Override
52    public synchronized Collection<VehiclePropConfig> takeSupportedProperties(
53            Collection<VehiclePropConfig> allProperties) {
54        List<VehiclePropConfig> supported = new LinkedList<>();
55        for (VehiclePropConfig p: allProperties) {
56            switch (p.prop) {
57                case VehicleProperty.INFO_MAKE:
58                    readPropertyToBundle(p.prop, CarInfoManager.BASIC_INFO_KEY_MANUFACTURER);
59                    break;
60                case VehicleProperty.INFO_MODEL:
61                    readPropertyToBundle(p.prop, CarInfoManager.BASIC_INFO_KEY_MODEL);
62                    break;
63                case VehicleProperty.INFO_MODEL_YEAR:
64                    readPropertyToBundle(p.prop, CarInfoManager.BASIC_INFO_KEY_MODEL_YEAR);
65                    break;
66                default: // not supported
67                    break;
68            }
69        }
70        return supported;
71    }
72
73    private void readPropertyToBundle(int prop, String key) {
74        String value = "";
75        try {
76            value = mHal.get(String.class, prop);
77        } catch (PropertyTimeoutException e) {
78            Log.e(CarLog.TAG_INFO, "Unable to read property", e);
79        }
80        mBasicInfo.putString(key, value);
81    }
82
83    @Override
84    public void handleHalEvents(List<VehiclePropValue> values) {
85        for (VehiclePropValue v : values) {
86            logUnexpectedEvent(v.prop);
87        }
88    }
89
90    @Override
91    public void dump(PrintWriter writer) {
92        writer.println("*InfoHal*");
93        writer.println("**BasicInfo:" + mBasicInfo);
94    }
95
96    public synchronized Bundle getBasicInfo() {
97        return mBasicInfo;
98    }
99
100    private void logUnexpectedEvent(int property) {
101       Log.w(CarLog.TAG_INFO, "unexpected HAL event for property 0x" +
102               Integer.toHexString(property));
103    }
104}
105