keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 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 | package com.android.car.vehiclenetwork; |
| 17 | |
| 18 | import android.annotation.Nullable; |
| 19 | import android.os.Handler; |
| 20 | import android.os.Looper; |
| 21 | import android.os.Message; |
| 22 | import android.os.RemoteException; |
| 23 | import android.os.ServiceManager; |
| 24 | import android.util.Log; |
| 25 | |
| 26 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropConfigs; |
| 27 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValue; |
| 28 | import com.android.car.vehiclenetwork.VehicleNetworkProto.VehiclePropValues; |
| 29 | |
| 30 | import java.lang.ref.WeakReference; |
| 31 | |
| 32 | /** |
| 33 | * System API to access Vehicle network. This is only for system services and applications should |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 34 | * not use this. All APIs will fail with security error if normal app tries this. |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 35 | */ |
| 36 | public class VehicleNetwork { |
| 37 | public interface VehicleNetworkListener { |
| 38 | void onVehicleNetworkEvents(VehiclePropValues values); |
| 39 | } |
| 40 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 41 | 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 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 48 | public static VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener, |
| 49 | Looper looper) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 50 | IVehicleNetwork service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService( |
| 51 | IVehicleNetwork.class.getCanonicalName())); |
| 52 | if (service == null) { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 53 | throw new RuntimeException("Vehicle network service not available:" + |
| 54 | IVehicleNetwork.class.getCanonicalName()); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 55 | } |
| 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 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 67 | public VehiclePropConfigs listProperties() { |
| 68 | return listProperties(0 /* all */); |
| 69 | } |
| 70 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 71 | 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 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 83 | public void setProperty(VehiclePropValue value) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 84 | VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value); |
| 85 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 86 | mService.setProperty(parcelable); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 87 | } catch (RemoteException e) { |
| 88 | handleRemoteException(e); |
| 89 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 90 | } |
| 91 | |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame^] | 92 | 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 | |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 119 | 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 | |
keunyoung | fe30ba0 | 2015-09-17 17:56:35 -0700 | [diff] [blame^] | 131 | 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 | |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 176 | public void subscribe(int property, float sampleRate) { |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 177 | try { |
keunyoung | 15882e5 | 2015-09-16 16:57:58 -0700 | [diff] [blame] | 178 | mService.subscribe(mVehicleNetworkListener, property, sampleRate); |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 179 | } catch (RemoteException e) { |
| 180 | handleRemoteException(e); |
| 181 | } |
keunyoung | e18e25d | 2015-08-28 15:57:19 -0700 | [diff] [blame] | 182 | } |
| 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 | } |