1 /*
2  * Copyright (C) 2020 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 package android.car.vms;
18 
19 import android.annotation.CallbackExecutor;
20 import android.annotation.NonNull;
21 import android.annotation.RequiresPermission;
22 import android.annotation.SystemApi;
23 import android.car.Car;
24 import android.car.CarManagerBase;
25 import android.car.annotation.RequiredFeature;
26 import android.os.IBinder;
27 import android.os.RemoteException;
28 import android.util.ArrayMap;
29 import android.util.Log;
30 
31 import com.android.internal.annotations.GuardedBy;
32 
33 import java.util.Map;
34 import java.util.Objects;
35 import java.util.concurrent.Executor;
36 
37 /**
38  * Car Service manager for connecting clients to the Vehicle Map Service.
39  *
40  * @hide
41  */
42 @RequiredFeature(Car.VEHICLE_MAP_SERVICE)
43 @SystemApi
44 public final class VmsClientManager extends CarManagerBase {
45     private static final boolean DBG = false;
46     private static final String TAG = VmsClientManager.class.getSimpleName();
47 
48     /**
49      * Callback interface for Vehicle Map Service clients.
50      */
51     public interface VmsClientCallback {
52         /**
53          * Invoked when a Vehicle Map Service connection has been established for the callback.
54          *
55          * @param client API client
56          */
onClientConnected(@onNull VmsClient client)57         void onClientConnected(@NonNull VmsClient client);
58 
59         /**
60          * Invoked when the availability of data layers has changed.
61          *
62          * @param availableLayers Current layer availability
63          */
onLayerAvailabilityChanged(@onNull VmsAvailableLayers availableLayers)64         void onLayerAvailabilityChanged(@NonNull VmsAvailableLayers availableLayers);
65 
66         /**
67          * Invoked when any subscriptions to data layers have changed.
68          *
69          * @param subscriptionState Current subscription state
70          */
onSubscriptionStateChanged(@onNull VmsSubscriptionState subscriptionState)71         void onSubscriptionStateChanged(@NonNull VmsSubscriptionState subscriptionState);
72 
73         /**
74          * Invoked whenever a packet is received for this client's subscriptions.
75          *
76          * @param providerId  Packet provider
77          * @param layer       Packet layer
78          * @param packet      Packet data
79          */
onPacketReceived(int providerId, @NonNull VmsLayer layer, @NonNull byte[] packet)80         void onPacketReceived(int providerId, @NonNull VmsLayer layer, @NonNull byte[] packet);
81     }
82 
83     private final IVmsBrokerService mBrokerService;
84 
85     private final Object mLock = new Object();
86     @GuardedBy("mLock")
87     private final Map<VmsClientCallback, VmsClient> mClients = new ArrayMap<>();
88 
89     /**
90      * @hide
91      */
VmsClientManager(Car car, IBinder service)92     public VmsClientManager(Car car, IBinder service) {
93         super(car);
94         mBrokerService = IVmsBrokerService.Stub.asInterface(service);
95     }
96 
97     /**
98      * Registers new Vehicle Map Service client for the given callback.
99      *
100      * If the callback is already registered, no action is taken.
101      *
102      * @param executor Executor to run callback operations
103      * @param callback Callback to register for new client
104      */
105     @RequiresPermission(anyOf = {Car.PERMISSION_VMS_PUBLISHER, Car.PERMISSION_VMS_SUBSCRIBER})
registerVmsClientCallback( @onNull @allbackExecutor Executor executor, @NonNull VmsClientCallback callback)106     public void registerVmsClientCallback(
107             @NonNull @CallbackExecutor Executor executor,
108             @NonNull VmsClientCallback callback) {
109         registerVmsClientCallback(executor, callback, false);
110     }
111 
112     /**
113      * @hide
114      */
115     @RequiresPermission(anyOf = {Car.PERMISSION_VMS_PUBLISHER, Car.PERMISSION_VMS_SUBSCRIBER})
registerVmsClientCallback( @onNull @allbackExecutor Executor executor, @NonNull VmsClientCallback callback, boolean legacyClient)116     void registerVmsClientCallback(
117             @NonNull @CallbackExecutor Executor executor,
118             @NonNull VmsClientCallback callback,
119             boolean legacyClient) {
120         Objects.requireNonNull(executor, "executor cannot be null");
121         Objects.requireNonNull(callback, "callback cannot be null");
122         VmsClient client;
123         synchronized (mLock) {
124             if (mClients.containsKey(callback)) {
125                 Log.w(TAG, "VmsClient already registered");
126                 return;
127             }
128 
129             client = new VmsClient(mBrokerService, executor, callback, legacyClient,
130                     /* autoCloseMemory */ true,
131                     this::handleRemoteExceptionFromCarService);
132             mClients.put(callback, client);
133             if (DBG) Log.d(TAG, "Client count: " + mClients.size());
134         }
135 
136         try {
137             if (DBG) Log.d(TAG, "Registering VmsClient");
138             client.register();
139         } catch (RemoteException e) {
140             Log.e(TAG, "Error while registering", e);
141             synchronized (mLock) {
142                 mClients.remove(callback);
143             }
144             handleRemoteExceptionFromCarService(e);
145             return;
146         }
147 
148         if (DBG) Log.d(TAG, "Triggering callbacks for new VmsClient");
149         executor.execute(() -> {
150             callback.onClientConnected(client);
151             if (!legacyClient) {
152                 callback.onLayerAvailabilityChanged(client.getAvailableLayers());
153                 callback.onSubscriptionStateChanged(client.getSubscriptionState());
154             }
155         });
156     }
157 
158     /**
159      * Unregisters the Vehicle Map Service client associated with the given callback.
160      *
161      * If the callback is not registered, no action is taken.
162      *
163      * @param callback
164      */
165     @RequiresPermission(anyOf = {Car.PERMISSION_VMS_PUBLISHER, Car.PERMISSION_VMS_SUBSCRIBER})
unregisterVmsClientCallback(@onNull VmsClientCallback callback)166     public void unregisterVmsClientCallback(@NonNull VmsClientCallback callback) {
167         VmsClient client;
168         synchronized (mLock) {
169             client = mClients.remove(callback);
170         }
171         if (client == null) {
172             Log.w(TAG, "Unregister called for unknown callback");
173             return;
174         }
175 
176         if (DBG) Log.d(TAG, "Unregistering VmsClient");
177         try {
178             client.unregister();
179         } catch (RemoteException e) {
180             handleRemoteExceptionFromCarService(e);
181         }
182     }
183 
184     /**
185      * @hide
186      */
187     @Override
onCarDisconnected()188     protected void onCarDisconnected() {
189         synchronized (mLock) {
190             Log.w(TAG, "Car disconnected with " + mClients.size() + " active clients");
191             mClients.clear();
192         }
193     }
194 }
195