blob: 28184b508564df3a6a784f3b89543f78213eb9d4 [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 */
16package com.android.car.vehiclenetwork;
17
18import android.annotation.Nullable;
19import android.os.Handler;
20import android.os.Looper;
21import android.os.Message;
22import android.os.RemoteException;
23import android.os.ServiceManager;
24import android.util.Log;
25
26import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs;
27import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue;
28import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues;
29
30import java.lang.ref.WeakReference;
31
32/**
33 * System API to access Vehicle network. This is only for system services and applications should
keunyoung15882e52015-09-16 16:57:58 -070034 * not use this. All APIs will fail with security error if normal app tries this.
keunyounge18e25d2015-08-28 15:57:19 -070035 */
36public class VehicleNetwork {
37 public interface VehicleNetworkListener {
38 void onVehicleNetworkEvents(VehiclePropValues values);
39 }
40
keunyounge18e25d2015-08-28 15:57:19 -070041 private static final String TAG = VehicleNetwork.class.getSimpleName();
42
43 private final IVehicleNetwork mService;
44 private final VehicleNetworkListener mListener;
45 private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
46 private final EventHandler mEventHandler;
47
keunyoung15882e52015-09-16 16:57:58 -070048 public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener,
49 Looper looper) {
keunyounge18e25d2015-08-28 15:57:19 -070050 IVehicleNetwork service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
51 IVehicleNetwork.class.getCanonicalName()));
52 if (service == null) {
keunyoung15882e52015-09-16 16:57:58 -070053 throw new RuntimeException("Vehicle network service not available:" +
54 IVehicleNetwork.class.getCanonicalName());
keunyounge18e25d2015-08-28 15:57:19 -070055 }
56 return new VehicleNetwork(service, listener, looper);
57 }
58
59 private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
60 Looper looper) {
61 mService = service;
62 mListener = listener;
63 mEventHandler = new EventHandler(looper);
64 mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
65 }
66
keunyoung15882e52015-09-16 16:57:58 -070067 public VehiclePropConfigs listProperties() {
68 return listProperties(0 /* all */);
69 }
70
keunyounge18e25d2015-08-28 15:57:19 -070071 public VehiclePropConfigs listProperties(int property) {
72 try {
73 VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
74 if (parcelable != null) {
75 return parcelable.configs;
76 }
77 } catch (RemoteException e) {
78 handleRemoteException(e);
79 }
80 return null;
81 }
82
keunyoung15882e52015-09-16 16:57:58 -070083 public void setProperty(VehiclePropValue value) {
keunyounge18e25d2015-08-28 15:57:19 -070084 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
85 try {
keunyoung15882e52015-09-16 16:57:58 -070086 mService.setProperty(parcelable);
keunyounge18e25d2015-08-28 15:57:19 -070087 } catch (RemoteException e) {
88 handleRemoteException(e);
89 }
keunyounge18e25d2015-08-28 15:57:19 -070090 }
91
keunyoungfe30ba02015-09-17 17:56:35 -070092 public void setIntProperty(int property, int value) {
93 VehiclePropValue v = VehiclePropValue.newBuilder().
94 setProp(property).
95 setValueType(VehicleNetworkConsts.VehicleValueType.VEHICLE_VALUE_TYPE_INT32).
96 setInt32Value(value).
97 build();
98 setProperty(v);
99 }
100
101 public void setLongProperty(int property, long value) {
102 VehiclePropValue v = VehiclePropValue.newBuilder().
103 setProp(property).
104 setValueType(VehicleNetworkConsts.VehicleValueType.VEHICLE_VALUE_TYPE_INT64).
105 setInt64Value(value).
106 build();
107 setProperty(v);
108 }
109
110 public void setIntProperty(int property, float value) {
111 VehiclePropValue v = VehiclePropValue.newBuilder().
112 setProp(property).
113 setValueType(VehicleNetworkConsts.VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT).
114 setFloatValue(value).
115 build();
116 setProperty(v);
117 }
118
keunyounge18e25d2015-08-28 15:57:19 -0700119 public VehiclePropValue getProperty(int property) {
120 try {
121 VehiclePropValueParcelable parcelable = mService.getProperty(property);
122 if (parcelable != null) {
123 return parcelable.value;
124 }
125 } catch (RemoteException e) {
126 handleRemoteException(e);
127 }
128 return null;
129 }
130
keunyoungfe30ba02015-09-17 17:56:35 -0700131 public int getIntProperty(int property) {
132 VehiclePropValue v = getProperty(property);
133 if (v == null) {
134 throw new IllegalStateException();
135 }
136 if (v.getValueType() != VehicleNetworkConsts.VehicleValueType.VEHICLE_VALUE_TYPE_INT32) {
137 throw new IllegalArgumentException();
138 }
139 return v.getInt32Value();
140 }
141
142 public float getFloatProperty(int property) {
143 VehiclePropValue v = getProperty(property);
144 if (v == null) {
145 throw new IllegalStateException();
146 }
147 if (v.getValueType() != VehicleNetworkConsts.VehicleValueType.VEHICLE_VALUE_TYPE_FLOAT) {
148 throw new IllegalArgumentException();
149 }
150 return v.getFloatValue();
151 }
152
153 public long getLongProperty(int property) {
154 VehiclePropValue v = getProperty(property);
155 if (v == null) {
156 throw new IllegalStateException();
157 }
158 if (v.getValueType() != VehicleNetworkConsts.VehicleValueType.VEHICLE_VALUE_TYPE_INT64) {
159 throw new IllegalArgumentException();
160 }
161 return v.getInt64Value();
162 }
163
164 //TODO check UTF8 to java string conversion
165 public String getStringProperty(int property) {
166 VehiclePropValue v = getProperty(property);
167 if (v == null) {
168 throw new IllegalStateException();
169 }
170 if (v.getValueType() != VehicleNetworkConsts.VehicleValueType.VEHICLE_VALUE_TYPE_STRING) {
171 throw new IllegalArgumentException();
172 }
173 return v.getStringValue();
174 }
175
keunyoung15882e52015-09-16 16:57:58 -0700176 public void subscribe(int property, float sampleRate) {
keunyounge18e25d2015-08-28 15:57:19 -0700177 try {
keunyoung15882e52015-09-16 16:57:58 -0700178 mService.subscribe(mVehicleNetworkListener, property, sampleRate);
keunyounge18e25d2015-08-28 15:57:19 -0700179 } catch (RemoteException e) {
180 handleRemoteException(e);
181 }
keunyounge18e25d2015-08-28 15:57:19 -0700182 }
183
184 public void unsubscribe(int property) {
185 try {
186 mService.unsubscribe(mVehicleNetworkListener, property);
187 } catch (RemoteException e) {
188 handleRemoteException(e);
189 }
190 }
191
192 private void handleRemoteException(RemoteException e) {
193 throw new RuntimeException("Vehicle network service not working ", e);
194 }
195
196 private void handleVehicleNetworkEvents(VehiclePropValues values) {
197 mEventHandler.notifyEvents(values);
198 }
199
200 private void doHandleVehicleNetworkEvents(VehiclePropValues values) {
201 mListener.onVehicleNetworkEvents(values);
202 }
203
204 private class EventHandler extends Handler {
205 private static final int MSG_EVENTS = 0;
206
207 private EventHandler(Looper looper) {
208 super(looper);
209 }
210
211 private void notifyEvents(VehiclePropValues values) {
212 Message msg = obtainMessage(MSG_EVENTS, values);
213 sendMessage(msg);
214 }
215
216 @Override
217 public void handleMessage(Message msg) {
218 switch (msg.what) {
219 case MSG_EVENTS:
220 doHandleVehicleNetworkEvents((VehiclePropValues)msg.obj);
221 break;
222 default:
223 Log.w(TAG, "unown message:" + msg.what, new RuntimeException());
224 break;
225 }
226 }
227 }
228
229 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
230 private final WeakReference<VehicleNetwork> mVehicleNetwork;
231
232 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
233 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
234 }
235
236 @Override
237 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
238 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
239 if (vehicleNetwork != null) {
240 vehicleNetwork.handleVehicleNetworkEvents(values.values);
241 }
242 }
243 }
244}