blob: 0a9372114a3bd174107149fbcd8b4640cddb2e24 [file] [log] [blame]
keunyoung1ab8e182015-09-24 09:25:22 -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 <IVehicleNetworkHalMock.h>
28#include <VehicleNetworkProto.pb.h>
29
30#include "BinderUtil.h"
31#include "VehicleNetworkProtoUtil.h"
32
33namespace android {
34
35enum {
36 ON_LIST_PROPERTIES = IBinder::FIRST_CALL_TRANSACTION,
37 ON_PROPERTY_SET,
38 ON_PROPERTY_GET,
39 ON_SUBSCRIBE,
40 ON_UNSUBSCRIBE,
41};
42
43// ----------------------------------------------------------------------------
44
45const char IVehicleNetworkHalMock::SERVICE_NAME[] =
46 "com.android.car.vehiclenetwork.IVehicleNetworkHalMock";
47
48// ----------------------------------------------------------------------------
49
50class BpVehicleNetworkHalMock : public BpInterface<IVehicleNetworkHalMock> {
51public:
52 BpVehicleNetworkHalMock(const sp<IBinder> & impl)
53 : BpInterface<IVehicleNetworkHalMock>(impl) {
54 }
55
56 virtual sp<VehiclePropertiesHolder> onListProperties() {
57 sp<VehiclePropertiesHolder> holder;
58 Parcel data, reply;
59 data.writeInterfaceToken(IVehicleNetworkHalMock::getInterfaceDescriptor());
60 status_t status = remote()->transact(ON_LIST_PROPERTIES, data, &reply);
61 if (status == NO_ERROR) {
62 reply.readExceptionCode(); // for compatibility with java
63 if (reply.readInt32() == 0) { // no result
64 return holder;
65 }
66 ReadableBlobHolder blob(new Parcel::ReadableBlob());
67 if (blob.blob == NULL) {
68 ALOGE("listProperties, no memory");
69 return holder;
70 }
71 int32_t size = reply.readInt32();
72 status = reply.readBlob(size, blob.blob);
73 if (status != NO_ERROR) {
74 ALOGE("listProperties, cannot read blob %d", status);
75 return holder;
76 }
77 //TODO make this more memory efficient
78 std::unique_ptr<VehiclePropConfigs> configs(new VehiclePropConfigs());
79 if (configs.get() == NULL) {
80 return holder;
81 }
82 if(!configs->ParseFromArray(blob.blob->data(), size)) {
83 ALOGE("listProperties, cannot parse reply");
84 return holder;
85 }
86 holder = new VehiclePropertiesHolder();
87 ASSERT_OR_HANDLE_NO_MEMORY(holder.get(), return);
88 status = VehicleNetworkProtoUtil::fromVehiclePropConfigs(*configs.get(),
89 holder->getList());
90 if (status != NO_ERROR) {
91 ALOGE("listProperties, cannot convert VehiclePropConfigs %d", status);
92 return holder;
93 }
94
95 }
96 return holder;
97 }
98
99 virtual status_t onPropertySet(const vehicle_prop_value_t& value) {
100 Parcel data, reply;
101 data.writeInterfaceToken(IVehicleNetworkHalMock::getInterfaceDescriptor());
keunyoung7d74e6d2015-10-14 15:43:10 -0700102 status_t status = VehiclePropValueBinderUtil::writeToParcel(data, value);
103 if (status != NO_ERROR) {
104 return status;
105 }
106 status = remote()->transact(ON_PROPERTY_SET, data, &reply);
keunyoung1ab8e182015-09-24 09:25:22 -0700107 return status;
108 }
109
110 virtual status_t onPropertyGet(vehicle_prop_value_t* value) {
keunyoung7d74e6d2015-10-14 15:43:10 -0700111 if (value == NULL) {
112 return BAD_VALUE;
113 }
keunyoung1ab8e182015-09-24 09:25:22 -0700114 Parcel data, reply;
115 data.writeInterfaceToken(IVehicleNetworkHalMock::getInterfaceDescriptor());
keunyoung7d74e6d2015-10-14 15:43:10 -0700116 status_t status = VehiclePropValueBinderUtil::writeToParcel(data, *value);
117 if (status != NO_ERROR) {
118 return status;
119 }
120 status = remote()->transact(ON_PROPERTY_GET, data, &reply);
keunyoung1ab8e182015-09-24 09:25:22 -0700121 if (status == NO_ERROR) {
122 reply.readExceptionCode(); // for compatibility with java
keunyoung7d74e6d2015-10-14 15:43:10 -0700123 status = VehiclePropValueBinderUtil::readFromParcel(reply, value);
keunyoung1ab8e182015-09-24 09:25:22 -0700124 }
125 return status;
126 }
127
128 virtual status_t onPropertySubscribe(int32_t property, float sampleRate) {
129 Parcel data, reply;
130 data.writeInterfaceToken(IVehicleNetworkHalMock::getInterfaceDescriptor());
131 data.writeInt32(property);
132 data.writeFloat(sampleRate);
133 status_t status = remote()->transact(ON_SUBSCRIBE, data, &reply);
134 return status;
135 }
136
137 virtual void onPropertyUnsubscribe(int32_t property) {
138 Parcel data, reply;
139 data.writeInterfaceToken(IVehicleNetworkHalMock::getInterfaceDescriptor());
140 data.writeInt32(property);
141 status_t status = remote()->transact(ON_UNSUBSCRIBE, data, &reply);
142 if (status != NO_ERROR) {
143 ALOGI("onPropertyUnsubscribe property %d failed %d", property, status);
144 }
145 }
146};
147
148IMPLEMENT_META_INTERFACE(VehicleNetworkHalMock, IVehicleNetworkHalMock::SERVICE_NAME);
149
150// ----------------------------------------------------------------------
151
152static bool isSystemUser() {
153 uid_t uid = IPCThreadState::self()->getCallingUid();
154 switch (uid) {
155 // This list will be expanded. Only those UIDs are allowed to access vehicle network
156 // for now. There can be per property based UID check built-in as well.
157 case AID_ROOT:
158 case AID_SYSTEM:
159 case AID_AUDIO: {
160 return true;
161 } break;
162 default: {
163 ALOGE("non-system user tried access, uid %d", uid);
164 } break;
165 }
166 return false;
167}
168
169status_t BnVehicleNetworkHalMock::onTransact(uint32_t code, const Parcel& data, Parcel* reply,
170 uint32_t flags) {
171 if (!isSystemUser()) {
172 return PERMISSION_DENIED;
173 }
174 status_t r;
175 switch (code) {
176 case ON_LIST_PROPERTIES: {
177 CHECK_INTERFACE(IVehicleNetworkHalMock, data, reply);
178 sp<VehiclePropertiesHolder> holder = onListProperties();
179 if (holder.get() == NULL) { // given property not found
180 BinderUtil::fillObjectResultReply(reply, false /* isValid */);
181 return NO_ERROR;
182 }
183 std::unique_ptr<VehiclePropConfigs> configs(new VehiclePropConfigs());
184 ASSERT_OR_HANDLE_NO_MEMORY(configs.get(), return NO_MEMORY);
185 VehicleNetworkProtoUtil::toVehiclePropConfigs(holder->getList(), *configs.get());
186 int size = configs->ByteSize();
187 WritableBlobHolder blob(new Parcel::WritableBlob());
188 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
189 BinderUtil::fillObjectResultReply(reply, true);
190 reply->writeInt32(size);
191 reply->writeBlob(size, false, blob.blob);
192 configs->SerializeToArray(blob.blob->data(), size);
193 return NO_ERROR;
194 } break;
195 case ON_PROPERTY_SET: {
196 CHECK_INTERFACE(IVehicleNetworkHalMock, data, reply);
197 if (data.readInt32() == 0) { // java side allows passing null with this.
198 return BAD_VALUE;
199 }
200 ScopedVehiclePropValue value;
201 ReadableBlobHolder blob(new Parcel::ReadableBlob());
202 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
203 int32_t size = data.readInt32();
204 r = data.readBlob(size, blob.blob);
205 if (r != NO_ERROR) {
206 ALOGE("setProperty:service, cannot read blob");
207 return r;
208 }
209 std::unique_ptr<VehiclePropValue> v(new VehiclePropValue());
210 ASSERT_OR_HANDLE_NO_MEMORY(v.get(), return NO_MEMORY);
211 if (!v->ParseFromArray(blob.blob->data(), size)) {
212 ALOGE("setProperty:service, cannot parse data");
213 return BAD_VALUE;
214 }
215 r = VehicleNetworkProtoUtil::fromVehiclePropValue(*v.get(), value.value);
216 if (r != NO_ERROR) {
217 ALOGE("setProperty:service, cannot convert data");
218 return BAD_VALUE;
219 }
220 r = onPropertySet(value.value);
221 BinderUtil::fillNoResultReply(reply);
222 return r;
223 } break;
224 case ON_PROPERTY_GET: {
225 CHECK_INTERFACE(IVehicleNetworkHalMock, data, reply);
226 ScopedVehiclePropValue value;
227 value.value.prop = data.readInt32();
228 r = onPropertyGet(&(value.value));
229 if (r == NO_ERROR) {
230 BinderUtil::fillObjectResultReply(reply, true);
231 std::unique_ptr<VehiclePropValue> v(new VehiclePropValue());
232 ASSERT_OR_HANDLE_NO_MEMORY(v.get(), return NO_MEMORY);
233 VehicleNetworkProtoUtil::toVehiclePropValue(value.value, *v.get());
234 int size = v->ByteSize();
235 WritableBlobHolder blob(new Parcel::WritableBlob());
236 ASSERT_OR_HANDLE_NO_MEMORY(blob.blob, return NO_MEMORY);
237 reply->writeInt32(size);
238 reply->writeBlob(size, false, blob.blob);
239 v->SerializeToArray(blob.blob->data(), size);
240 }
241 return r;
242 } break;
243 case ON_SUBSCRIBE: {
244 CHECK_INTERFACE(IVehicleNetworkHalMock, data, reply);
245 int32_t property = data.readInt32();
246 float sampleRate = data.readFloat();
247 r = onPropertySubscribe(property, sampleRate);
248 BinderUtil::fillNoResultReply(reply);
249 return r;
250 } break;
251 case ON_UNSUBSCRIBE: {
252 CHECK_INTERFACE(IVehicleNetworkHalMock, data, reply);
253 int32_t property = data.readInt32();
254 onPropertyUnsubscribe(property);
255 BinderUtil::fillNoResultReply(reply);
256 return NO_ERROR;
257 } break;
258 default:
259 return BBinder::onTransact(code, data, reply, flags);
260 }
261}
262
263}; // namespace android