blob: 8ed4433d2a18efcb45d3a8e3cdf53275ade63a4c [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
34 * not use this.
35 */
36public class VehicleNetwork {
37 public interface VehicleNetworkListener {
38 void onVehicleNetworkEvents(VehiclePropValues values);
39 }
40
41 public static final int NO_ERROR = 0;
42 public static final int ERROR_UNKNOWN = -1;
43
44 private static final String TAG = VehicleNetwork.class.getSimpleName();
45
46 private final IVehicleNetwork mService;
47 private final VehicleNetworkListener mListener;
48 private final IVehicleNetworkListenerImpl mVehicleNetworkListener;
49 private final EventHandler mEventHandler;
50
51 public VehicleNetwork createVehicleNetwork(VehicleNetworkListener listener, Looper looper) {
52 IVehicleNetwork service = IVehicleNetwork.Stub.asInterface(ServiceManager.getService(
53 IVehicleNetwork.class.getCanonicalName()));
54 if (service == null) {
55 throw new RuntimeException("Vehicle network service not available");
56 }
57 return new VehicleNetwork(service, listener, looper);
58 }
59
60 private VehicleNetwork(IVehicleNetwork service, VehicleNetworkListener listener,
61 Looper looper) {
62 mService = service;
63 mListener = listener;
64 mEventHandler = new EventHandler(looper);
65 mVehicleNetworkListener = new IVehicleNetworkListenerImpl(this);
66 }
67
68 public VehiclePropConfigs listProperties(int property) {
69 try {
70 VehiclePropConfigsParcelable parcelable = mService.listProperties(property);
71 if (parcelable != null) {
72 return parcelable.configs;
73 }
74 } catch (RemoteException e) {
75 handleRemoteException(e);
76 }
77 return null;
78 }
79
80 public int setProperty(VehiclePropValue value) {
81 VehiclePropValueParcelable parcelable = new VehiclePropValueParcelable(value);
82 try {
83 int r = mService.setProperty(parcelable);
84 return r;
85 } catch (RemoteException e) {
86 handleRemoteException(e);
87 }
88 return ERROR_UNKNOWN;
89 }
90
91 public VehiclePropValue getProperty(int property) {
92 try {
93 VehiclePropValueParcelable parcelable = mService.getProperty(property);
94 if (parcelable != null) {
95 return parcelable.value;
96 }
97 } catch (RemoteException e) {
98 handleRemoteException(e);
99 }
100 return null;
101 }
102
103 public int subscribe(int property, float sampleRate) {
104 try {
105 int r = mService.subscribe(mVehicleNetworkListener, property, sampleRate);
106 return r;
107 } catch (RemoteException e) {
108 handleRemoteException(e);
109 }
110 return ERROR_UNKNOWN;
111 }
112
113 public void unsubscribe(int property) {
114 try {
115 mService.unsubscribe(mVehicleNetworkListener, property);
116 } catch (RemoteException e) {
117 handleRemoteException(e);
118 }
119 }
120
121 private void handleRemoteException(RemoteException e) {
122 throw new RuntimeException("Vehicle network service not working ", e);
123 }
124
125 private void handleVehicleNetworkEvents(VehiclePropValues values) {
126 mEventHandler.notifyEvents(values);
127 }
128
129 private void doHandleVehicleNetworkEvents(VehiclePropValues values) {
130 mListener.onVehicleNetworkEvents(values);
131 }
132
133 private class EventHandler extends Handler {
134 private static final int MSG_EVENTS = 0;
135
136 private EventHandler(Looper looper) {
137 super(looper);
138 }
139
140 private void notifyEvents(VehiclePropValues values) {
141 Message msg = obtainMessage(MSG_EVENTS, values);
142 sendMessage(msg);
143 }
144
145 @Override
146 public void handleMessage(Message msg) {
147 switch (msg.what) {
148 case MSG_EVENTS:
149 doHandleVehicleNetworkEvents((VehiclePropValues)msg.obj);
150 break;
151 default:
152 Log.w(TAG, "unown message:" + msg.what, new RuntimeException());
153 break;
154 }
155 }
156 }
157
158 private static class IVehicleNetworkListenerImpl extends IVehicleNetworkListener.Stub {
159 private final WeakReference<VehicleNetwork> mVehicleNetwork;
160
161 private IVehicleNetworkListenerImpl(VehicleNetwork vehicleNewotk) {
162 mVehicleNetwork = new WeakReference<VehicleNetwork>(vehicleNewotk);
163 }
164
165 @Override
166 public void onVehicleNetworkEvents(VehiclePropValuesParcelable values) {
167 VehicleNetwork vehicleNetwork = mVehicleNetwork.get();
168 if (vehicleNetwork != null) {
169 vehicleNetwork.handleVehicleNetworkEvents(values.values);
170 }
171 }
172 }
173}