blob: f39af665484b9d004d3d11594f6a975806fd7e5c [file] [log] [blame]
keunyounge18e25d2015-08-28 15:57:19 -07001/*
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
keunyounge18e25d2015-08-28 15:57:19 -070023#include <binder/IInterface.h>
24#include <binder/IMemory.h>
25
Keun-young Park28dd4702015-11-19 18:06:04 -080026#include <utils/threads.h>
27#include <utils/Errors.h>
28#include <utils/List.h>
29#include <utils/RefBase.h>
30
keunyounge18e25d2015-08-28 15:57:19 -070031#include "IVehicleNetwork.h"
Keun-young Park28dd4702015-11-19 18:06:04 -080032#include "HandlerThread.h"
keunyounge18e25d2015-08-28 15:57:19 -070033
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;
Keun-young Park28dd4702015-11-19 18:06:04 -080047 virtual void onHalError(int32_t errorCode, int32_t property, int32_t operation) = 0;
48 virtual void onHalRestart(bool inMocking) = 0;
Pavel Maltsevb0324b42016-09-27 21:00:41 -070049 virtual void onPropertySet(const vehicle_prop_value_t& value) = 0;
keunyounge18e25d2015-08-28 15:57:19 -070050};
51
52// ----------------------------------------------------------------------------
Keun-young Park28dd4702015-11-19 18:06:04 -080053
54/** For internal event handling, not for client */
55class VehicleNetworkEventMessageHandler : public MessageHandler {
56 enum {
57 EVENT_EVENTS = 0,
58 EVENT_HAL_ERROR = 1,
59 EVENT_HAL_RESTART = 2,
Pavel Maltsevb0324b42016-09-27 21:00:41 -070060 EVENT_ON_SET = 3,
Keun-young Park28dd4702015-11-19 18:06:04 -080061 };
62public:
63 VehicleNetworkEventMessageHandler(const sp<Looper>& looper,
64 sp<VehicleNetworkListener>& listener);
65 virtual ~VehicleNetworkEventMessageHandler();
66
67 void handleHalEvents(sp<VehiclePropValueListHolder>& events);
68 void handleHalError(int32_t errorCode, int32_t property, int32_t operation);
Keun-young Parkaaeffaf2015-11-25 17:24:10 -080069 /**
70 * This error must be handled always. This can be called in vehicle network service's crash
71 * as well.
72 */
Keun-young Park28dd4702015-11-19 18:06:04 -080073 void handleHalRestart(bool inMocking);
74
Pavel Maltsevb0324b42016-09-27 21:00:41 -070075 void handleOnPropertySet(const vehicle_prop_value_t& value);
76
Keun-young Park28dd4702015-11-19 18:06:04 -080077private:
78 virtual void handleMessage(const Message& message);
79 void doHandleHalEvents();
80 void doHandleHalError();
81 void doHandleHalRestart();
Pavel Maltsevb0324b42016-09-27 21:00:41 -070082 void doHandleOnPropertySet();
Keun-young Park28dd4702015-11-19 18:06:04 -080083private:
84 mutable Mutex mLock;
85 sp<Looper> mLooper;
86 sp<VehicleNetworkListener>& mListener;
87 List<sp<VehiclePropValueListHolder>> mEvents;
88 List<VehicleHalError*> mHalErrors;
89 List<bool> mHalRestartEvents;
Pavel Maltsevb0324b42016-09-27 21:00:41 -070090 List<vehicle_prop_value_t*> mSetValueEvents;
Keun-young Park28dd4702015-11-19 18:06:04 -080091};
92
93// ----------------------------------------------------------------------------
94
keunyounge18e25d2015-08-28 15:57:19 -070095/**
96 * Vehicle network API for low level components like HALs to access / control car information.
97 * This is reference counted. So use with sp<>.
98 */
99class VehicleNetwork : public IBinder::DeathRecipient, public BnVehicleNetworkListener
100{
101public:
102 /**
103 * Factory method for VehicleNetwork. Client should use this method to create
104 * a new instance.
105 */
106 static sp<VehicleNetwork> createVehicleNetwork(sp<VehicleNetworkListener> &listener);
107
108 virtual ~VehicleNetwork();
109
110 /** Set int32 value */
111 status_t setInt32Property(int32_t property, int32_t value);
112 /** get int32 value */
113 status_t getInt32Property(int32_t property, int32_t* value, int64_t* timestamp);
114 status_t setInt64Property(int32_t property, int64_t value);
115 status_t getInt64Property(int32_t property, int64_t* value, int64_t* timestamp);
116 status_t setFloatProperty(int32_t property, float value);
117 status_t getFloatProperty(int32_t property, float* value, int64_t* timestamp);
118 status_t setStringProperty(int32_t property, const String8& value);
119 status_t getStringProperty(int32_t property, String8& value, int64_t* timestamp);
120 sp<VehiclePropertiesHolder> listProperties(int32_t property = 0);
121 /** For generic value setting. At least prop, value_type, and value should be set. */
122 status_t setProperty(const vehicle_prop_value_t& value);
123 /** For generic value getting. value->prop should be set. */
124 status_t getProperty(vehicle_prop_value_t* value);
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700125 status_t subscribe(int32_t property, float sampleRate, int32_t zones = 0,
126 int32_t flags = SubscribeFlags::DEFAULT);
keunyounge18e25d2015-08-28 15:57:19 -0700127 void unsubscribe(int32_t property);
128
Keun-young Park28dd4702015-11-19 18:06:04 -0800129 // Only for testing purpose
130 status_t injectEvent(const vehicle_prop_value_t& value);
131
132 // starting / stopping mocking not added yet.
133 status_t startMocking(const sp<IVehicleNetworkHalMock>& mock);
134 void stopMocking(const sp<IVehicleNetworkHalMock>& mock);
135
136 // only for testing
137 status_t injectHalError(int32_t errorCode, int32_t property, int32_t operation);
138
139 status_t startErrorListening();
140 void stopErrorListening();
141
keunyounge18e25d2015-08-28 15:57:19 -0700142 //IBinder::DeathRecipient, not for client
143 void binderDied(const wp<IBinder>& who);
144 // BnVehicleNetworkListener, not for client
Keun-young Park28dd4702015-11-19 18:06:04 -0800145 void onEvents(sp<VehiclePropValueListHolder>& events);
146 void onHalError(int32_t errorCode, int32_t property, int32_t operation);
147 void onHalRestart(bool inMocking);
Pavel Maltsevb0324b42016-09-27 21:00:41 -0700148 void onPropertySet(const vehicle_prop_value_t& value);
keunyounge18e25d2015-08-28 15:57:19 -0700149
150private:
151 VehicleNetwork(sp<IVehicleNetwork>& vehicleNetwork, sp<VehicleNetworkListener> &listener);
Keun-young Park28dd4702015-11-19 18:06:04 -0800152 // RefBase
153 virtual void onFirstRef();
Keun-young Parkaaeffaf2015-11-25 17:24:10 -0800154 sp<IVehicleNetwork> getService();
155 sp<VehicleNetworkEventMessageHandler> getEventHandler();
keunyounge18e25d2015-08-28 15:57:19 -0700156
157private:
158 sp<IVehicleNetwork> mService;
159 sp<VehicleNetworkListener> mClientListener;
160 Mutex mLock;
Keun-young Park28dd4702015-11-19 18:06:04 -0800161 sp<HandlerThread> mHandlerThread;
162 sp<VehicleNetworkEventMessageHandler> mEventHandler;
keunyounge18e25d2015-08-28 15:57:19 -0700163};
164
165// ----------------------------------------------------------------------------
166
167}; // namespace android
168
169#endif /* ANDROID_VEHICLE_NETWORK_H */
170