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 */
16
17package com.android.car.hal;
18
19
20import android.annotation.Nullable;
21import android.hardware.automotive.vehicle.V2_0.VehiclePropConfig;
22import android.hardware.automotive.vehicle.V2_0.VehiclePropValue;
23
24import java.io.PrintWriter;
25import java.util.Collection;
26import java.util.LinkedList;
27import java.util.List;
28
29/**
30 * Common interface for all HAL service like sensor HAL.
31 * Each HAL service is connected with XyzService supporting XyzManager,
32 * and will translate HAL data into car api specific format.
33 */
34public abstract class HalServiceBase {
35    /** For dispatching events. Kept here to avoid alloc every time */
36    private final LinkedList<VehiclePropValue> mDispatchList = new LinkedList<VehiclePropValue>();
37
38    final static int NOT_SUPPORTED_PROPERTY = -1;
39
40    public List<VehiclePropValue> getDispatchList() {
41        return mDispatchList;
42    }
43
44    /** initialize */
45    public abstract void init();
46
47    /** release and stop operation */
48    public abstract void release();
49
50    /**
51     * return supported properties among all properties.
52     * @return null if no properties are supported
53     */
54    /**
55     * Take supported properties from given allProperties and return List of supported properties.
56     * @param allProperties
57     * @return null if no properties are supported.
58     */
59    @Nullable
60    public Collection<VehiclePropConfig> takeSupportedProperties(
61            Collection<VehiclePropConfig> allProperties) {
62        return null;
63    }
64
65    public abstract void handleHalEvents(List<VehiclePropValue> values);
66
67    public void handlePropertySetError(int property, int area) {}
68
69    public abstract void dump(PrintWriter writer);
70
71    /**
72     * Helper class that maintains bi-directional mapping between manager's property
73     * Id (public or system API) and vehicle HAL property Id.
74     *
75     * <p>This class is supposed to be immutable. Use {@link #create(int[])} factory method to
76     * instantiate this class.
77     */
78    static class ManagerToHalPropIdMap {
79        private final BidirectionalSparseIntArray mMap;
80
81        /**
82         * Creates {@link ManagerToHalPropIdMap} for provided [manager prop Id, hal prop Id] pairs.
83         *
84         * <p> The input array should have an odd number of elements.
85         */
86        static ManagerToHalPropIdMap create(int... mgrToHalPropIds) {
87            return new ManagerToHalPropIdMap(BidirectionalSparseIntArray.create(mgrToHalPropIds));
88        }
89
90        private ManagerToHalPropIdMap(BidirectionalSparseIntArray map) {
91            mMap = map;
92        }
93
94        int getHalPropId(int managerPropId) {
95            return mMap.getValue(managerPropId, NOT_SUPPORTED_PROPERTY);
96        }
97
98        int getManagerPropId(int halPropId) {
99            return mMap.getKey(halPropId, NOT_SUPPORTED_PROPERTY);
100        }
101    }
102}
103