Home | History | Annotate | Download | only in citadeld
      1 /*
      2  * Copyright (C) 2017 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 <limits>
     18 #include <thread>
     19 #include <mutex>
     20 
     21 #include <android-base/logging.h>
     22 #include <binder/IPCThreadState.h>
     23 #include <binder/IServiceManager.h>
     24 
     25 #include <nos/device.h>
     26 #include <nos/NuggetClient.h>
     27 
     28 #include <android/hardware/citadel/BnCitadeld.h>
     29 
     30 using ::android::OK;
     31 using ::android::defaultServiceManager;
     32 using ::android::sp;
     33 using ::android::status_t;
     34 using ::android::IPCThreadState;
     35 using ::android::IServiceManager;
     36 using ::android::ProcessState;
     37 
     38 using ::android::binder::Status;
     39 
     40 using ::nos::NuggetClient;
     41 
     42 using ::android::hardware::citadel::BnCitadeld;
     43 using ::android::hardware::citadel::ICitadeld;
     44 
     45 namespace {
     46 
     47 class CitadelProxy : public BnCitadeld {
     48 public:
     49     CitadelProxy(NuggetClient& client) : _client{client} {}
     50     ~CitadelProxy() override = default;
     51 
     52     Status callApp(const int32_t _appId, const int32_t _arg, const std::vector<uint8_t>& request,
     53                    std::vector<uint8_t>* const response, int32_t* const _aidl_return) override {
     54         // AIDL doesn't support integers less than 32-bit so validate it before casting
     55         if (_appId < 0 || _appId > kMaxAppId) {
     56             LOG(ERROR) << "App ID " << _appId << " is outside the app ID range";
     57             return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
     58         }
     59         if (_arg < 0 || _arg > std::numeric_limits<uint16_t>::max()) {
     60             LOG(ERROR) << "Argument " << _arg << " is outside the unsigned 16-bit range";
     61             return Status::fromExceptionCode(Status::EX_ILLEGAL_ARGUMENT);
     62         }
     63 
     64         const uint8_t appId = static_cast<uint32_t>(_appId);
     65         const uint16_t arg =  static_cast<uint16_t>(_arg);
     66         uint32_t* const appStatus = reinterpret_cast<uint32_t*>(_aidl_return);
     67 
     68         // Make the call to the app while holding the lock for that app
     69         std::unique_lock<std::mutex> lock(_appLocks[appId]);
     70         *appStatus = _client.CallApp(appId, arg, request, response);
     71         return Status::ok();
     72     }
     73 
     74     Status reset(bool* const _aidl_return) override {
     75         // This doesn't use the transport API to talk to any app so doesn't need
     76         // to hold any app locks.
     77         const nos_device& device = *_client.Device();
     78         *_aidl_return = (device.ops.reset(device.ctx) == 0);
     79         return Status::ok();
     80     }
     81 
     82 private:
     83     static constexpr auto kMaxAppId = std::numeric_limits<uint8_t>::max();
     84 
     85     NuggetClient& _client;
     86     std::mutex _appLocks[kMaxAppId + 1];
     87 };
     88 
     89 [[noreturn]] void CitadelEventDispatcher(const nos_device& device) {
     90     LOG(INFO) << "Event dispatcher startup.";
     91     while(1) {
     92         if (device.ops.wait_for_interrupt(device.ctx, -1) > 0) {
     93             LOG(INFO) << "Citadel has dispatched an event";
     94         } else {
     95             LOG(INFO) << "Citadel did something unexpected";
     96         }
     97 
     98         // This is a placeholder for the message handling that gives citadel a
     99         // chance to deassert CTLD_AP_IRQ, so this doesn't spam the logs.
    100         // TODO(b/62713383) Replace this with the code to contact citadel.
    101         sleep(1);
    102     }
    103 }
    104 
    105 } // namespace
    106 
    107 int main() {
    108     LOG(INFO) << "Starting citadeld";
    109 
    110     // Connect to Citadel
    111     NuggetClient citadel;
    112     citadel.Open();
    113     if (!citadel.IsOpen()) {
    114         LOG(FATAL) << "Failed to open Citadel client";
    115     }
    116 
    117     // Citadel HALs will communicate with this daemon via /dev/vndbinder as this
    118     // is vendor code
    119     ProcessState::initWithDriver("/dev/vndbinder");
    120 
    121     sp<CitadelProxy> proxy = new CitadelProxy(citadel);
    122     const status_t status = defaultServiceManager()->addService(ICitadeld::descriptor, proxy);
    123     if (status != OK) {
    124         LOG(FATAL) << "Failed to register citadeld as a service (status " << status << ")";
    125     }
    126 
    127     // Handle interrupts triggered by Citadel and dispatch any events to
    128     // registered listeners.
    129     std::thread event_dispatcher(CitadelEventDispatcher, *citadel.Device());
    130 
    131     // Start handling binder requests with multiple threads
    132     ProcessState::self()->startThreadPool();
    133     IPCThreadState::self()->joinThreadPool();
    134     return 0;
    135 }
    136