1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
|
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "RemoteAccessService.h"
#include <VehicleUtils.h>
#include <aidl/android/hardware/automotive/vehicle/VehicleProperty.h>
#include <android-base/parseint.h>
#include <android-base/stringprintf.h>
#include <android/binder_status.h>
#include <grpc++/grpc++.h>
#include <private/android_filesystem_config.h>
#include <sys/stat.h>
#include <utils/Log.h>
#include <chrono>
#include <fstream>
#include <iostream>
#include <thread>
namespace android {
namespace hardware {
namespace automotive {
namespace remoteaccess {
namespace {
using ::aidl::android::hardware::automotive::remoteaccess::ApState;
using ::aidl::android::hardware::automotive::remoteaccess::IRemoteTaskCallback;
using ::aidl::android::hardware::automotive::vehicle::VehicleProperty;
using ::android::base::Error;
using ::android::base::ParseInt;
using ::android::base::Result;
using ::android::base::ScopedLockAssertion;
using ::android::base::StringAppendF;
using ::android::base::StringPrintf;
using ::android::frameworks::automotive::vhal::IVhalClient;
using ::android::hardware::automotive::vehicle::toInt;
using ::grpc::ClientContext;
using ::grpc::ClientReaderInterface;
using ::grpc::Status;
using ::grpc::StatusCode;
using ::ndk::ScopedAStatus;
const std::string WAKEUP_SERVICE_NAME = "com.google.vehicle.wakeup";
const std::string PROCESSOR_ID = "application_processor";
constexpr char COMMAND_SET_AP_STATE[] = "--set-ap-state";
constexpr char COMMAND_START_DEBUG_CALLBACK[] = "--start-debug-callback";
constexpr char COMMAND_STOP_DEBUG_CALLBACK[] = "--stop-debug-callback";
constexpr char COMMAND_SHOW_TASK[] = "--show-task";
constexpr char COMMAND_GET_VEHICLE_ID[] = "--get-vehicle-id";
constexpr char COMMAND_INJECT_TASK[] = "--inject-task";
constexpr char COMMAND_INJECT_TASK_NEXT_REBOOT[] = "--inject-task-next-reboot";
constexpr char COMMAND_STATUS[] = "--status";
constexpr char DEBUG_TASK_FILE[] = "/data/vendor/remoteaccess/debugTask";
std::vector<uint8_t> stringToBytes(std::string_view s) {
const char* data = s.data();
return std::vector<uint8_t>(data, data + s.size());
}
ScopedAStatus rpcStatusToScopedAStatus(const Status& status, const std::string& errorMsg) {
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
status.error_code(), (errorMsg + ", error: " + status.error_message()).c_str());
}
std::string printBytes(const std::vector<uint8_t>& bytes) {
std::string s;
for (size_t i = 0; i < bytes.size(); i++) {
StringAppendF(&s, "%02x", bytes[i]);
}
return s;
}
bool checkBoolFlag(const char* flag) {
return !strcmp(flag, "1") || !strcmp(flag, "0");
}
void dprintErrorStatus(int fd, const char* detail, const ScopedAStatus& status) {
dprintf(fd, "%s, code: %d, error: %s\n", detail, status.getStatus(), status.getMessage());
}
std::string boolToString(bool x) {
return x ? "true" : "false";
}
} // namespace
RemoteAccessService::RemoteAccessService(WakeupClient::StubInterface* grpcStub)
: mGrpcStub(grpcStub) {
std::ifstream debugTaskFile;
debugTaskFile.open(DEBUG_TASK_FILE, std::ios::in);
if (!debugTaskFile.is_open()) {
ALOGD("No debug task available");
return;
}
char buffer[1024] = {};
debugTaskFile.getline(buffer, sizeof(buffer));
std::string clientId = std::string(buffer);
debugTaskFile.getline(buffer, sizeof(buffer));
std::string taskData = std::string(buffer);
int latencyInSec;
debugTaskFile >> latencyInSec;
debugTaskFile.close();
ALOGD("Task for client: %s, data: [%s], latency: %d\n", clientId.c_str(), taskData.c_str(),
latencyInSec);
mInjectDebugTaskThread = std::thread([this, clientId, taskData, latencyInSec] {
std::this_thread::sleep_for(std::chrono::seconds(latencyInSec));
if (auto result = deliverRemoteTaskThroughCallback(clientId, taskData); !result.ok()) {
ALOGE("Failed to inject debug task, clientID: %s, taskData: %s, error: %s",
clientId.c_str(), taskData.c_str(), result.error().message().c_str());
return;
}
ALOGD("Task for client: %s, data: [%s] successfully injected\n", clientId.c_str(),
taskData.c_str());
});
}
RemoteAccessService::~RemoteAccessService() {
maybeStopTaskLoop();
if (mInjectDebugTaskThread.joinable()) {
mInjectDebugTaskThread.join();
}
}
void RemoteAccessService::maybeStartTaskLoop() {
std::lock_guard<std::mutex> lockGuard(mStartStopTaskLoopLock);
if (mTaskLoopRunning) {
return;
}
mThread = std::thread([this]() { runTaskLoop(); });
mTaskLoopRunning = true;
}
void RemoteAccessService::maybeStopTaskLoop() {
std::lock_guard<std::mutex> lockGuard(mStartStopTaskLoopLock);
if (!mTaskLoopRunning) {
return;
}
{
std::lock_guard<std::mutex> lockGuard(mLock);
// Try to stop the reading stream.
if (mGetRemoteTasksContext) {
mGetRemoteTasksContext->TryCancel();
// Don't reset mGetRemoteTaskContext here since the read stream might still be affective
// and might still be using it. This will cause reader->Read to return false and
// mGetRemoteTasksContext will be cleared after reader->Finish() is called.
}
mTaskWaitStopped = true;
mCv.notify_all();
}
if (mThread.joinable()) {
mThread.join();
}
mTaskLoopRunning = false;
}
void RemoteAccessService::updateGrpcConnected(bool connected) {
std::lock_guard<std::mutex> lockGuard(mLock);
mGrpcConnected = connected;
}
Result<void> RemoteAccessService::deliverRemoteTaskThroughCallback(const std::string& clientId,
std::string_view taskData) {
std::shared_ptr<IRemoteTaskCallback> callback;
{
std::lock_guard<std::mutex> lockGuard(mLock);
callback = mRemoteTaskCallback;
mClientIdToTaskCount[clientId] += 1;
}
if (callback == nullptr) {
return Error() << "No callback registered, task ignored";
}
ALOGD("Calling onRemoteTaskRequested callback for client ID: %s", clientId.c_str());
ScopedAStatus callbackStatus =
callback->onRemoteTaskRequested(clientId, stringToBytes(taskData));
if (!callbackStatus.isOk()) {
return Error() << "Failed to call onRemoteTaskRequested callback, status: "
<< callbackStatus.getStatus()
<< ", message: " << callbackStatus.getMessage();
}
return {};
}
void RemoteAccessService::runTaskLoop() {
GetRemoteTasksRequest request = {};
std::unique_ptr<ClientReaderInterface<GetRemoteTasksResponse>> reader;
while (true) {
{
std::lock_guard<std::mutex> lockGuard(mLock);
mGetRemoteTasksContext.reset(new ClientContext());
reader = mGrpcStub->GetRemoteTasks(mGetRemoteTasksContext.get(), request);
}
updateGrpcConnected(true);
GetRemoteTasksResponse response;
while (reader->Read(&response)) {
ALOGI("Receiving one task from remote task client");
if (auto result =
deliverRemoteTaskThroughCallback(response.clientid(), response.data());
!result.ok()) {
ALOGE("%s", result.error().message().c_str());
continue;
}
}
updateGrpcConnected(false);
Status status = reader->Finish();
mGetRemoteTasksContext.reset();
ALOGE("GetRemoteTasks stream breaks, code: %d, message: %s, sleeping for 10s and retry",
status.error_code(), status.error_message().c_str());
// The long lasting connection should not return. But if the server returns, retry after
// 10s.
{
std::unique_lock lk(mLock);
if (mCv.wait_for(lk, std::chrono::milliseconds(mRetryWaitInMs), [this] {
ScopedLockAssertion lockAssertion(mLock);
return mTaskWaitStopped;
})) {
// If the stopped flag is set, we are quitting, exit the loop.
break;
}
}
}
}
ScopedAStatus RemoteAccessService::getVehicleId(std::string* vehicleId) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
auto vhalClient = IVhalClient::tryCreate();
if (vhalClient == nullptr) {
ALOGE("Failed to connect to VHAL");
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
/*errorCode=*/0, "Failed to connect to VHAL to get vehicle ID");
}
return getVehicleIdWithClient(*vhalClient.get(), vehicleId);
#else
// Don't use VHAL client in fuzzing since IPC is not allowed.
return ScopedAStatus::ok();
#endif
}
ScopedAStatus RemoteAccessService::getVehicleIdWithClient(IVhalClient& vhalClient,
std::string* vehicleId) {
auto result = vhalClient.getValueSync(
*vhalClient.createHalPropValue(toInt(VehicleProperty::INFO_VIN)));
if (!result.ok()) {
return ScopedAStatus::fromServiceSpecificErrorWithMessage(
/*errorCode=*/0,
("failed to get INFO_VIN from VHAL: " + result.error().message()).c_str());
}
*vehicleId = (*result)->getStringValue();
return ScopedAStatus::ok();
}
ScopedAStatus RemoteAccessService::getProcessorId(std::string* processorId) {
*processorId = PROCESSOR_ID;
return ScopedAStatus::ok();
}
ScopedAStatus RemoteAccessService::getWakeupServiceName(std::string* wakeupServiceName) {
*wakeupServiceName = WAKEUP_SERVICE_NAME;
return ScopedAStatus::ok();
}
ScopedAStatus RemoteAccessService::setRemoteTaskCallback(
const std::shared_ptr<IRemoteTaskCallback>& callback) {
std::lock_guard<std::mutex> lockGuard(mLock);
mRemoteTaskCallback = callback;
return ScopedAStatus::ok();
}
ScopedAStatus RemoteAccessService::clearRemoteTaskCallback() {
std::lock_guard<std::mutex> lockGuard(mLock);
mRemoteTaskCallback.reset();
return ScopedAStatus::ok();
}
ScopedAStatus RemoteAccessService::notifyApStateChange(const ApState& newState) {
ClientContext context;
NotifyWakeupRequiredRequest request = {};
request.set_iswakeuprequired(newState.isWakeupRequired);
NotifyWakeupRequiredResponse response = {};
Status status = mGrpcStub->NotifyWakeupRequired(&context, request, &response);
if (!status.ok()) {
return rpcStatusToScopedAStatus(status, "Failed to notify isWakeupRequired");
}
if (newState.isReadyForRemoteTask) {
maybeStartTaskLoop();
} else {
maybeStopTaskLoop();
}
return ScopedAStatus::ok();
}
bool RemoteAccessService::checkDumpPermission() {
uid_t uid = AIBinder_getCallingUid();
return uid == AID_ROOT || uid == AID_SHELL || uid == AID_SYSTEM;
}
void RemoteAccessService::dumpHelp(int fd) {
dprintf(fd,
"RemoteAccess HAL debug interface, Usage: \n"
"%s [0/1](isReadyForRemoteTask) [0/1](isWakeupRequired): Set the new AP state\n"
"%s: Start a debug callback that will record the received tasks\n"
"%s: Stop the debug callback\n"
"%s: Show tasks received by debug callback\n"
"%s: Get vehicle id\n"
"%s [client_id] [task_data]: Inject a task\n"
"%s [client_id] [task_data] [latencyInSec]: "
"Inject a task on next reboot after latencyInSec seconds\n"
"%s: Show status\n",
COMMAND_SET_AP_STATE, COMMAND_START_DEBUG_CALLBACK, COMMAND_STOP_DEBUG_CALLBACK,
COMMAND_SHOW_TASK, COMMAND_GET_VEHICLE_ID, COMMAND_INJECT_TASK,
COMMAND_INJECT_TASK_NEXT_REBOOT, COMMAND_STATUS);
}
binder_status_t RemoteAccessService::dump(int fd, const char** args, uint32_t numArgs) {
if (!checkDumpPermission()) {
dprintf(fd, "Caller must be root, system or shell\n");
return STATUS_PERMISSION_DENIED;
}
if (numArgs == 0) {
dumpHelp(fd);
printCurrentStatus(fd);
return STATUS_OK;
}
if (!strcmp(args[0], COMMAND_SET_AP_STATE)) {
if (numArgs < 3) {
dumpHelp(fd);
return STATUS_OK;
}
ApState apState = {};
const char* remoteTaskFlag = args[1];
if (!strcmp(remoteTaskFlag, "1") && !strcmp(remoteTaskFlag, "0")) {
dumpHelp(fd);
return STATUS_OK;
}
if (!checkBoolFlag(args[1])) {
dumpHelp(fd);
return STATUS_OK;
}
if (!strcmp(args[1], "1")) {
apState.isReadyForRemoteTask = true;
}
if (!checkBoolFlag(args[2])) {
dumpHelp(fd);
return STATUS_OK;
}
if (!strcmp(args[2], "1")) {
apState.isWakeupRequired = true;
}
auto status = notifyApStateChange(apState);
if (!status.isOk()) {
dprintErrorStatus(fd, "Failed to set AP state", status);
} else {
dprintf(fd, "successfully set the new AP state\n");
}
} else if (!strcmp(args[0], COMMAND_START_DEBUG_CALLBACK)) {
mDebugCallback = ndk::SharedRefBase::make<DebugRemoteTaskCallback>();
setRemoteTaskCallback(mDebugCallback);
dprintf(fd, "Debug callback registered\n");
} else if (!strcmp(args[0], COMMAND_STOP_DEBUG_CALLBACK)) {
if (mDebugCallback) {
mDebugCallback.reset();
}
clearRemoteTaskCallback();
dprintf(fd, "Debug callback unregistered\n");
} else if (!strcmp(args[0], COMMAND_SHOW_TASK)) {
if (mDebugCallback) {
dprintf(fd, "%s", mDebugCallback->printTasks().c_str());
} else {
dprintf(fd, "Debug callback is not currently used, use \"%s\" first.\n",
COMMAND_START_DEBUG_CALLBACK);
}
} else if (!strcmp(args[0], COMMAND_GET_VEHICLE_ID)) {
std::string vehicleId;
auto status = getVehicleId(&vehicleId);
if (!status.isOk()) {
dprintErrorStatus(fd, "Failed to get vehicle ID", status);
} else {
dprintf(fd, "Vehicle Id: %s\n", vehicleId.c_str());
}
} else if (!strcmp(args[0], COMMAND_INJECT_TASK)) {
if (numArgs < 3) {
dumpHelp(fd);
return STATUS_OK;
}
debugInjectTask(fd, args[1], args[2]);
} else if (!strcmp(args[0], COMMAND_INJECT_TASK_NEXT_REBOOT)) {
if (numArgs < 4) {
dumpHelp(fd);
return STATUS_OK;
}
debugInjectTaskNextReboot(fd, args[1], args[2], args[3]);
} else if (!strcmp(args[0], COMMAND_STATUS)) {
printCurrentStatus(fd);
} else {
dumpHelp(fd);
}
return STATUS_OK;
}
void RemoteAccessService::printCurrentStatus(int fd) {
std::lock_guard<std::mutex> lockGuard(mLock);
dprintf(fd,
"\nRemoteAccess HAL status \n"
"Remote task callback registered: %s\n"
"Task receiving GRPC connection established: %s\n"
"Received task count by clientId: \n%s\n",
boolToString(mRemoteTaskCallback.get()).c_str(), boolToString(mGrpcConnected).c_str(),
clientIdToTaskCountToStringLocked().c_str());
}
void RemoteAccessService::debugInjectTask(int fd, std::string_view clientId,
std::string_view taskData) {
std::string clientIdCopy = std::string(clientId);
if (auto result = deliverRemoteTaskThroughCallback(clientIdCopy, taskData); !result.ok()) {
dprintf(fd, "Failed to inject task: %s\n", result.error().message().c_str());
return;
}
dprintf(fd, "Task for client: %s, data: [%s] successfully injected\n", clientId.data(),
taskData.data());
}
void RemoteAccessService::debugInjectTaskNextReboot(int fd, std::string_view clientId,
std::string_view taskData,
const char* latencyInSecStr) {
int latencyInSec;
if (!ParseInt(latencyInSecStr, &latencyInSec)) {
dprintf(fd, "The input latency in second is not a valid integer");
return;
}
std::ofstream debugTaskFile;
debugTaskFile.open(DEBUG_TASK_FILE, std::ios::out);
if (!debugTaskFile.is_open()) {
dprintf(fd,
"Failed to open debug task file, please run the command: "
"'adb shell touch %s' first\n",
DEBUG_TASK_FILE);
return;
}
if (taskData.find("\n") != std::string::npos) {
dprintf(fd, "Task data must not contain newline\n");
return;
}
debugTaskFile << clientId << "\n" << taskData << "\n" << latencyInSec;
debugTaskFile.close();
dprintf(fd,
"Task with clientId: %s, task data: %s, latency: %d sec scheduled for next reboot\n",
clientId.data(), taskData.data(), latencyInSec);
}
std::string RemoteAccessService::clientIdToTaskCountToStringLocked() {
// Print the table header
std::string output = "| ClientId | Count |\n";
for (const auto& [clientId, taskCount] : mClientIdToTaskCount) {
output += StringPrintf(" %-9s %-6zu\n", clientId.c_str(), taskCount);
}
return output;
}
ScopedAStatus DebugRemoteTaskCallback::onRemoteTaskRequested(const std::string& clientId,
const std::vector<uint8_t>& data) {
std::lock_guard<std::mutex> lockGuard(mLock);
mTasks.push_back({
.clientId = clientId,
.data = data,
});
return ScopedAStatus::ok();
}
std::string DebugRemoteTaskCallback::printTasks() {
std::lock_guard<std::mutex> lockGuard(mLock);
std::string s = StringPrintf("Received %zu tasks in %f seconds", mTasks.size(),
(android::uptimeMillis() - mStartTimeMillis) / 1000.);
for (size_t i = 0; i < mTasks.size(); i++) {
StringAppendF(&s, "Client Id: %s, Data: %s\n", mTasks[i].clientId.c_str(),
printBytes(mTasks[i].data).c_str());
}
return s;
}
} // namespace remoteaccess
} // namespace automotive
} // namespace hardware
} // namespace android
|