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