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.util.Log;
20
21import com.android.car.CarLog;
22import com.android.car.vehiclenetwork.VehicleNetworkConsts;
23import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfig;
24import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
25
26import java.io.PrintWriter;
27import java.util.HashMap;
28import java.util.LinkedList;
29import java.util.List;
30
31public class InfoHalService extends HalServiceBase {
32
33    private final VehicleHal mHal;
34    private final HashMap<String, VehiclePropConfig> mInfoNameToHalPropertyMap =
35            new HashMap<String, VehiclePropConfig>();
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        mInfoNameToHalPropertyMap.clear();
49    }
50
51    @Override
52    public synchronized List<VehiclePropConfig> takeSupportedProperties(
53            List<VehiclePropConfig> allProperties) {
54        List<VehiclePropConfig> supported = new LinkedList<VehiclePropConfig>();
55        for (VehiclePropConfig p: allProperties) {
56            String infoName = getInfoStringFromProperty(p.getProp());
57            if (infoName != null) {
58                supported.add(p);
59                mInfoNameToHalPropertyMap.put(infoName, p);
60            }
61        }
62        return supported;
63    }
64
65    @Override
66    public void handleHalEvents(List<VehiclePropValue> values) {
67        for (VehiclePropValue v : values) {
68            logUnexpectedEvent(v.getProp());
69        }
70    }
71
72    @Override
73    public void dump(PrintWriter writer) {
74        writer.println("*InfoHal*");
75        writer.println("**Supported properties**");
76        for (VehiclePropConfig p : mInfoNameToHalPropertyMap.values()) {
77            //TODO fix toString
78            writer.println(p.toString());
79        }
80    }
81
82    public int[] getInt(String key) {
83        VehiclePropConfig prop = getHalPropertyFromInfoString(key);
84        if (prop == null) {
85            return null;
86        }
87        // no lock here as get can take time and multiple get should be possible.
88        int v = mHal.getVehicleNetwork().getIntProperty(prop.getProp());
89        return new int[] { v };
90    }
91
92    public long[] getLong(String key) {
93        VehiclePropConfig prop = getHalPropertyFromInfoString(key);
94        if (prop == null) {
95            return null;
96        }
97        // no lock here as get can take time and multiple get should be possible.
98        long v = mHal.getVehicleNetwork().getLongProperty(prop.getProp());
99        return new long[] { v };
100    }
101
102    public float[] getFloat(String key) {
103        VehiclePropConfig prop = getHalPropertyFromInfoString(key);
104        if (prop == null) {
105            return null;
106        }
107        // no lock here as get can take time and multiple get should be possible.
108        float v = mHal.getVehicleNetwork().getFloatProperty(prop.getProp());
109        return new float[] { v };
110    }
111
112    public String getString(String key) {
113        VehiclePropConfig prop = getHalPropertyFromInfoString(key);
114        if (prop == null) {
115            return null;
116        }
117        // no lock here as get can take time and multiple get should be possible.
118        return mHal.getVehicleNetwork().getStringProperty(prop.getProp());
119    }
120
121    private synchronized VehiclePropConfig getHalPropertyFromInfoString(String key) {
122        return mInfoNameToHalPropertyMap.get(key);
123    }
124
125    private void logUnexpectedEvent(int property) {
126       Log.w(CarLog.TAG_INFO, "unexpected HAL event for property 0x" +
127               Integer.toHexString(property));
128    }
129
130    private static String getInfoStringFromProperty(int property) {
131        switch (property) {
132            case VehicleNetworkConsts.VEHICLE_PROPERTY_INFO_MAKE:
133                return CarInfoManager.KEY_MANUFACTURER;
134            case VehicleNetworkConsts.VEHICLE_PROPERTY_INFO_MODEL:
135                return CarInfoManager.KEY_MODEL;
136            case VehicleNetworkConsts.VEHICLE_PROPERTY_INFO_MODEL_YEAR:
137                return CarInfoManager.KEY_MODEL_YEAR;
138            case VehicleNetworkConsts.VEHICLE_PROPERTY_INFO_VIN:
139                return CarInfoManager.KEY_VEHICLE_ID;
140            //TODO add more properties
141            default:
142                return null;
143        }
144    }
145}
146