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
17#ifndef ANDROID_VEHICLE_NETWORK_H
18#define ANDROID_VEHICLE_NETWORK_H
19
20#include <stdint.h>
21#include <sys/types.h>
22
23#include <binder/IInterface.h>
24#include <binder/IMemory.h>
25
26#include <utils/threads.h>
27#include <utils/Errors.h>
28#include <utils/List.h>
29#include <utils/RefBase.h>
30
31#include "IVehicleNetwork.h"
32#include "HandlerThread.h"
33
34namespace android {
35
36// ----------------------------------------------------------------------------
37
38/**
39 * Listener for client to implement to get events from Vehicle network service.
40 */
41class VehicleNetworkListener : public RefBase
42{
43public:
44    VehicleNetworkListener() {};
45    virtual ~VehicleNetworkListener() {};
46    virtual void onEvents(sp<VehiclePropValueListHolder>& events) = 0;
47    virtual void onHalError(int32_t errorCode, int32_t property, int32_t operation) = 0;
48    virtual void onHalRestart(bool inMocking) = 0;
49};
50
51// ----------------------------------------------------------------------------
52
53/** For internal event handling, not for client */
54class VehicleNetworkEventMessageHandler : public MessageHandler {
55    enum {
56        EVENT_EVENTS = 0,
57        EVENT_HAL_ERROR = 1,
58        EVENT_HAL_RESTART = 2,
59    };
60public:
61    VehicleNetworkEventMessageHandler(const sp<Looper>& looper,
62            sp<VehicleNetworkListener>& listener);
63    virtual ~VehicleNetworkEventMessageHandler();
64
65    void handleHalEvents(sp<VehiclePropValueListHolder>& events);
66    void handleHalError(int32_t errorCode, int32_t property, int32_t operation);
67    /**
68     * This error must be handled always. This can be called in vehicle network service's crash
69     * as well.
70     */
71    void handleHalRestart(bool inMocking);
72
73private:
74    virtual void handleMessage(const Message& message);
75    void doHandleHalEvents();
76    void doHandleHalError();
77    void doHandleHalRestart();
78private:
79    mutable Mutex mLock;
80    sp<Looper> mLooper;
81    sp<VehicleNetworkListener>& mListener;
82    List<sp<VehiclePropValueListHolder>> mEvents;
83    List<VehicleHalError*> mHalErrors;
84    List<bool> mHalRestartEvents;
85};
86
87// ----------------------------------------------------------------------------
88
89/**
90 * Vehicle network API for low level components like HALs to access / control car information.
91 * This is reference counted. So use with sp<>.
92 */
93class VehicleNetwork : public IBinder::DeathRecipient, public BnVehicleNetworkListener
94{
95public:
96    /**
97     * Factory method for VehicleNetwork. Client should use this method to create
98     * a new instance.
99     */
100    static sp<VehicleNetwork> createVehicleNetwork(sp<VehicleNetworkListener> &listener);
101
102    virtual ~VehicleNetwork();
103
104    /** Set int32 value */
105    status_t setInt32Property(int32_t property, int32_t value);
106    /** get int32 value */
107    status_t getInt32Property(int32_t property, int32_t* value, int64_t* timestamp);
108    status_t setInt64Property(int32_t property, int64_t value);
109    status_t getInt64Property(int32_t property, int64_t* value, int64_t* timestamp);
110    status_t setFloatProperty(int32_t property, float value);
111    status_t getFloatProperty(int32_t property, float* value, int64_t* timestamp);
112    status_t setStringProperty(int32_t property, const String8& value);
113    status_t getStringProperty(int32_t property, String8& value, int64_t* timestamp);
114    sp<VehiclePropertiesHolder> listProperties(int32_t property = 0);
115    /** For generic value setting. At least prop, value_type, and value should be set. */
116    status_t setProperty(const vehicle_prop_value_t& value);
117    /** For generic value getting. value->prop should be set. */
118    status_t getProperty(vehicle_prop_value_t* value);
119    status_t subscribe(int32_t property, float sampleRate, int32_t zones = 0);
120    void unsubscribe(int32_t property);
121
122    // Only for testing purpose
123    status_t injectEvent(const vehicle_prop_value_t& value);
124
125    // starting / stopping mocking not added yet.
126    status_t startMocking(const sp<IVehicleNetworkHalMock>& mock);
127    void stopMocking(const sp<IVehicleNetworkHalMock>& mock);
128
129    // only for testing
130    status_t injectHalError(int32_t errorCode, int32_t property, int32_t operation);
131
132    status_t startErrorListening();
133    void stopErrorListening();
134
135    //IBinder::DeathRecipient, not for client
136    void binderDied(const wp<IBinder>& who);
137    // BnVehicleNetworkListener, not for client
138    void onEvents(sp<VehiclePropValueListHolder>& events);
139    void onHalError(int32_t errorCode, int32_t property, int32_t operation);
140    void onHalRestart(bool inMocking);
141
142private:
143    VehicleNetwork(sp<IVehicleNetwork>& vehicleNetwork, sp<VehicleNetworkListener> &listener);
144    // RefBase
145    virtual void onFirstRef();
146    sp<IVehicleNetwork> getService();
147    sp<VehicleNetworkEventMessageHandler> getEventHandler();
148
149private:
150    sp<IVehicleNetwork> mService;
151    sp<VehicleNetworkListener> mClientListener;
152    Mutex mLock;
153    sp<HandlerThread> mHandlerThread;
154    sp<VehicleNetworkEventMessageHandler> mEventHandler;
155};
156
157// ----------------------------------------------------------------------------
158
159}; // namespace android
160
161#endif /* ANDROID_VEHICLE_NETWORK_H */
162
163