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