blob: 61fa20861bf8faa48351111f9db6fcaa738a3588 [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#define LOG_TAG "VehicleNetwork"
18
19#include <memory>
20
21#include <binder/IPCThreadState.h>
22#include <private/android_filesystem_config.h>
23
24#include <utils/Log.h>
25
26#include <IVehicleNetwork.h>
27#include <VehicleNetworkProto.pb.h>
28
keunyoung15882e52015-09-16 16:57:58 -070029#include "BinderUtil.h"
keunyounge18e25d2015-08-28 15:57:19 -070030#include "VehicleNetworkProtoUtil.h"
31
32namespace android {
33
34enum {
35 LIST_PROPERTIES = IBinder::FIRST_CALL_TRANSACTION,
36 SET_PROPERTY,
37 GET_PROPERTY,
38 SUBSCRIBE,
39 UNSUBSCRIBE,
keunyoung1ab8e182015-09-24 09:25:22 -070040 INJECT_EVENT,
41 START_MOCKING,
42 STOP_MOCKING,
keunyounge18e25d2015-08-28 15:57:19 -070043};
44
45// ----------------------------------------------------------------------------
46
keunyoung15882e52015-09-16 16:57:58 -070047const char IVehicleNetwork::SERVICE_NAME[] = "com.android.car.vehiclenetwork.IVehicleNetwork";
keunyounge18e25d2015-08-28 15:57:19 -070048
49// ----------------------------------------------------------------------------
50
51class BpVehicleNetwork : public BpInterface<IVehicleNetwork> {
52public:
53 BpVehicleNetwork(const sp<IBinder> & impl)
54 : BpInterface<IVehicleNetwork>(impl) {
55 }
56
57 virtual sp<VehiclePropertiesHolder> listProperties(int32_t property) {
58 sp<VehiclePropertiesHolder> holder;
59 Parcel data, reply;
60 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
61 data.writeInt32(property);
62 status_t status = remote()->transact(LIST_PROPERTIES, data, &reply);
63 if (status == NO_ERROR) {
keunyoung15882e52015-09-16 16:57:58 -070064 reply.readExceptionCode(); // for compatibility with java
keunyounge18e25d2015-08-28 15:57:19 -070065 if (reply.readInt32() == 0) { // no result
66 return holder;
67 }
68 ReadableBlobHolder blob(new Parcel::ReadableBlob());
69 if (blob.blob == NULL) {
70 ALOGE("listProperties, no memory");
71 return holder;
72 }
73 int32_t size = reply.readInt32();
74 status = reply.readBlob(size, blob.blob);
75 if (status != NO_ERROR) {
76 ALOGE("listProperties, cannot read blob %d", status);
77 return holder;
78 }
79 //TODO make this more memory efficient
80 std::unique_ptr<VehiclePropConfigs> configs(new VehiclePropConfigs());
81 if (configs.get() == NULL) {
82 return holder;
83 }
84 if(!configs->ParseFromArray(blob.blob->data(), size)) {
85 ALOGE("listProperties, cannot parse reply");
86 return holder;
87 }
keunyoungd32f4e62015-09-21 11:33:06 -070088 holder = new VehiclePropertiesHolder();
89 ASSERT_OR_HANDLE_NO_MEMORY(holder.get(), return);
90 status = VehicleNetworkProtoUtil::fromVehiclePropConfigs(*configs.get(),
91 holder->getList());
keunyounge18e25d2015-08-28 15:57:19 -070092 if (status != NO_ERROR) {
93 ALOGE("listProperties, cannot convert VehiclePropConfigs %d", status);
94 return holder;
95 }
keunyoungd32f4e62015-09-21 11:33:06 -070096
keunyounge18e25d2015-08-28 15:57:19 -070097 }
98 return holder;
99 }
100
101 virtual status_t setProperty(const vehicle_prop_value_t& value) {
102 Parcel data, reply;
103 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
keunyoung7d74e6d2015-10-14 15:43:10 -0700104 status_t status = VehiclePropValueBinderUtil::writeToParcel(data, value);
105 if (status != NO_ERROR) {
106 return status;
107 }
108 status = remote()->transact(SET_PROPERTY, data, &reply);
keunyounge18e25d2015-08-28 15:57:19 -0700109 return status;
110 }
111
112 virtual status_t getProperty(vehicle_prop_value_t* value) {
113 Parcel data, reply;
keunyoung7d74e6d2015-10-14 15:43:10 -0700114 if (value == NULL) {
115 return BAD_VALUE;
116 }
keunyounge18e25d2015-08-28 15:57:19 -0700117 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
keunyoung7d74e6d2015-10-14 15:43:10 -0700118 status_t status = VehiclePropValueBinderUtil::writeToParcel(data, *value);
119 if (status != NO_ERROR) {
120 ALOGE("getProperty, cannot write");
121 return status;
122 }
123 status = remote()->transact(GET_PROPERTY, data, &reply);
keunyounge18e25d2015-08-28 15:57:19 -0700124 if (status == NO_ERROR) {
keunyoung15882e52015-09-16 16:57:58 -0700125 reply.readExceptionCode(); // for compatibility with java
keunyoung7d74e6d2015-10-14 15:43:10 -0700126 status = VehiclePropValueBinderUtil::readFromParcel(reply, value);
keunyounge18e25d2015-08-28 15:57:19 -0700127 }
128 return status;
129 }
130
131 virtual status_t subscribe(const sp<IVehicleNetworkListener> &listener, int32_t property,
132 float sampleRate) {
133 Parcel data, reply;
134 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
135 data.writeStrongBinder(IInterface::asBinder(listener));
136 data.writeInt32(property);
137 data.writeFloat(sampleRate);
138 status_t status = remote()->transact(SUBSCRIBE, data, &reply);
139 return status;
140 }
141
142 virtual void unsubscribe(const sp<IVehicleNetworkListener> &listener, int32_t property) {
143 Parcel data, reply;
144 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
145 data.writeStrongBinder(IInterface::asBinder(listener));
146 data.writeInt32(property);
147 status_t status = remote()->transact(UNSUBSCRIBE, data, &reply);
148 if (status != NO_ERROR) {
149 ALOGI("unsubscribing property %d failed %d", property, status);
150 }
151 }
keunyoung1ab8e182015-09-24 09:25:22 -0700152
153 virtual status_t injectEvent(const vehicle_prop_value_t& value) {
154 Parcel data, reply;
155 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
156 data.writeInt32(1); // 0 means no value. For compatibility with aidl based code.
157 std::unique_ptr<VehiclePropValue> v(new VehiclePropValue());
158 ASSERT_OR_HANDLE_NO_MEMORY(v.get(), return NO_MEMORY);
159 VehicleNetworkProtoUtil::toVehiclePropValue(value, *v.get());
160 int size = v->ByteSize();
161 WritableBlobHolder blob(new Parcel::WritableBlob());
162 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
163 data.writeInt32(size);
164 data.writeBlob(size, false, blob.blob);
165 v->SerializeToArray(blob.blob->data(), size);
166 status_t status = remote()->transact(INJECT_EVENT, data, &reply);
167 return status;
168 }
169
170 virtual status_t startMocking(const sp<IVehicleNetworkHalMock>& mock) {
171 Parcel data, reply;
172 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
173 data.writeStrongBinder(IInterface::asBinder(mock));
174 status_t status = remote()->transact(START_MOCKING, data, &reply);
175 return status;
176 }
177
178 virtual void stopMocking(const sp<IVehicleNetworkHalMock>& mock) {
179 Parcel data, reply;
180 data.writeInterfaceToken(IVehicleNetwork::getInterfaceDescriptor());
181 data.writeStrongBinder(IInterface::asBinder(mock));
182 status_t status = remote()->transact(STOP_MOCKING, data, &reply);
183 if (status != NO_ERROR) {
184 ALOGI("stop mocking failed %d", status);
185 }
186 }
keunyounge18e25d2015-08-28 15:57:19 -0700187};
188
189IMPLEMENT_META_INTERFACE(VehicleNetwork, IVehicleNetwork::SERVICE_NAME);
190
191// ----------------------------------------------------------------------
192
193static bool isSystemUser() {
194 uid_t uid = IPCThreadState::self()->getCallingUid();
195 switch (uid) {
196 // This list will be expanded. Only those UIDs are allowed to access vehicle network
197 // for now. There can be per property based UID check built-in as well.
198 case AID_ROOT:
199 case AID_SYSTEM:
200 case AID_AUDIO: {
201 return true;
202 } break;
keunyoung15882e52015-09-16 16:57:58 -0700203 default: {
204 ALOGE("non-system user tried access, uid %d", uid);
205 } break;
keunyounge18e25d2015-08-28 15:57:19 -0700206 }
207 return false;
208}
209
210status_t BnVehicleNetwork::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
211 uint32_t flags) {
212 if (!isSystemUser()) {
213 return PERMISSION_DENIED;
214 }
215 status_t r;
216 switch (code) {
217 case LIST_PROPERTIES: {
218 CHECK_INTERFACE(IVehicleNetwork, data, reply);
219 int32_t property = data.readInt32();
220 sp<VehiclePropertiesHolder> holder = listProperties(property);
221 if (holder.get() == NULL) { // given property not found
keunyoung15882e52015-09-16 16:57:58 -0700222 BinderUtil::fillObjectResultReply(reply, false /* isValid */);
223 return NO_ERROR;
keunyounge18e25d2015-08-28 15:57:19 -0700224 }
225 std::unique_ptr<VehiclePropConfigs> configs(new VehiclePropConfigs());
226 ASSERT_OR_HANDLE_NO_MEMORY(configs.get(), return NO_MEMORY);
keunyoungd32f4e62015-09-21 11:33:06 -0700227 VehicleNetworkProtoUtil::toVehiclePropConfigs(holder->getList(), *configs.get());
keunyounge18e25d2015-08-28 15:57:19 -0700228 int size = configs->ByteSize();
229 WritableBlobHolder blob(new Parcel::WritableBlob());
230 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
keunyoung15882e52015-09-16 16:57:58 -0700231 BinderUtil::fillObjectResultReply(reply, true);
keunyounge18e25d2015-08-28 15:57:19 -0700232 reply->writeInt32(size);
233 reply->writeBlob(size, false, blob.blob);
234 configs->SerializeToArray(blob.blob->data(), size);
235 return NO_ERROR;
236 } break;
237 case SET_PROPERTY: {
238 CHECK_INTERFACE(IVehicleNetwork, data, reply);
keunyounge18e25d2015-08-28 15:57:19 -0700239 ScopedVehiclePropValue value;
keunyoung7d74e6d2015-10-14 15:43:10 -0700240 r = VehiclePropValueBinderUtil::readFromParcel(data, &value.value,
241 false /* deleteMembers */);
keunyounge18e25d2015-08-28 15:57:19 -0700242 if (r != NO_ERROR) {
keunyounge18e25d2015-08-28 15:57:19 -0700243 return r;
244 }
keunyounge18e25d2015-08-28 15:57:19 -0700245 r = setProperty(value.value);
keunyoung15882e52015-09-16 16:57:58 -0700246 BinderUtil::fillNoResultReply(reply);
keunyounge18e25d2015-08-28 15:57:19 -0700247 return r;
248 } break;
249 case GET_PROPERTY: {
250 CHECK_INTERFACE(IVehicleNetwork, data, reply);
251 ScopedVehiclePropValue value;
keunyoung7d74e6d2015-10-14 15:43:10 -0700252 r = VehiclePropValueBinderUtil::readFromParcel(data, &value.value,
253 false /* deleteMembers */, true /*canIgnoreNoData*/);
254 if (r != NO_ERROR) {
255 ALOGE("getProperty cannot read %d", r);
256 return r;
257 }
keunyounge18e25d2015-08-28 15:57:19 -0700258 r = getProperty(&(value.value));
259 if (r == NO_ERROR) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700260 reply->writeNoException();
261 r = VehiclePropValueBinderUtil::writeToParcel(*reply, value.value);
keunyounge18e25d2015-08-28 15:57:19 -0700262 }
263 return r;
264 } break;
265 case SUBSCRIBE: {
266 CHECK_INTERFACE(IVehicleNetwork, data, reply);
267 sp<IVehicleNetworkListener> listener =
268 interface_cast<IVehicleNetworkListener>(data.readStrongBinder());
269 int32_t property = data.readInt32();
270 float sampleRate = data.readFloat();
271 r = subscribe(listener, property, sampleRate);
keunyoung15882e52015-09-16 16:57:58 -0700272 BinderUtil::fillNoResultReply(reply);
keunyounge18e25d2015-08-28 15:57:19 -0700273 return r;
274 } break;
275 case UNSUBSCRIBE: {
276 CHECK_INTERFACE(IVehicleNetwork, data, reply);
277 sp<IVehicleNetworkListener> listener =
278 interface_cast<IVehicleNetworkListener>(data.readStrongBinder());
279 int32_t property = data.readInt32();
280 unsubscribe(listener, property);
keunyoung15882e52015-09-16 16:57:58 -0700281 BinderUtil::fillNoResultReply(reply);
keunyounge18e25d2015-08-28 15:57:19 -0700282 return NO_ERROR;
283 } break;
keunyoung1ab8e182015-09-24 09:25:22 -0700284 case INJECT_EVENT: {
285 CHECK_INTERFACE(IVehicleNetwork, data, reply);
286 if (data.readInt32() == 0) { // java side allows passing null with this.
287 return BAD_VALUE;
288 }
289 ScopedVehiclePropValue value;
290 ReadableBlobHolder blob(new Parcel::ReadableBlob());
291 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
292 int32_t size = data.readInt32();
293 r = data.readBlob(size, blob.blob);
294 if (r != NO_ERROR) {
295 ALOGE("injectEvent:service, cannot read blob");
296 return r;
297 }
298 std::unique_ptr<VehiclePropValue> v(new VehiclePropValue());
299 ASSERT_OR_HANDLE_NO_MEMORY(v.get(), return NO_MEMORY);
300 if (!v->ParseFromArray(blob.blob->data(), size)) {
301 ALOGE("injectEvent:service, cannot parse data");
302 return BAD_VALUE;
303 }
304 r = VehicleNetworkProtoUtil::fromVehiclePropValue(*v.get(), value.value);
305 if (r != NO_ERROR) {
306 ALOGE("injectEvent:service, cannot convert data");
307 return BAD_VALUE;
308 }
309 r = injectEvent(value.value);
310 BinderUtil::fillNoResultReply(reply);
311 return r;
312 } break;
313 case START_MOCKING: {
314 CHECK_INTERFACE(IVehicleNetwork, data, reply);
315 sp<IVehicleNetworkHalMock> mock =
316 interface_cast<IVehicleNetworkHalMock>(data.readStrongBinder());
317 r = startMocking(mock);
318 BinderUtil::fillNoResultReply(reply);
319 return r;
320 } break;
321 case STOP_MOCKING: {
322 CHECK_INTERFACE(IVehicleNetwork, data, reply);
323 sp<IVehicleNetworkHalMock> mock =
324 interface_cast<IVehicleNetworkHalMock>(data.readStrongBinder());
325 stopMocking(mock);
326 BinderUtil::fillNoResultReply(reply);
327 return NO_ERROR;
328 } break;
keunyounge18e25d2015-08-28 15:57:19 -0700329 default:
330 return BBinder::onTransact(code, data, reply, flags);
331 }
332}
333
334}; // namespace android