Home | History | Annotate | Download | only in target_platform
      1 /*
      2  * Copyright (C) 2016 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 #ifndef CHRE_PLATFORM_LINUX_PLATFORM_NANOAPP_BASE_H_
     18 #define CHRE_PLATFORM_LINUX_PLATFORM_NANOAPP_BASE_H_
     19 
     20 #include <cstdint>
     21 #include <string>
     22 
     23 #include "chre/platform/shared/nanoapp_support_lib_dso.h"
     24 
     25 #include "chre/util/entry_points.h"
     26 
     27 namespace chre {
     28 
     29 /**
     30  * Linux-specific nanoapp functionality.
     31  */
     32 class PlatformNanoappBase {
     33  public:
     34   /**
     35    * Associate this Nanoapp with a nanoapp included in a .so that is pre-loaded
     36    * onto the filesystem. Actually loading the .so into memory is done when
     37    * start() is called.
     38    *
     39    * @param filename The name of the .so file in /vendor/lib/dsp that holds this
     40    *        nanoapp. This string is not deep-copied, so the memory must remain
     41    *        valid for the lifetime of this Nanoapp instance.
     42    */
     43   void loadFromFile(const std::string& filename);
     44 
     45   /**
     46    * Associate this Nanoapp instance with a nanoapp that is statically built
     47    * into the CHRE binary with the given app info structure.
     48    */
     49   void loadStatic(const struct chreNslNanoappInfo *appInfo);
     50 
     51   /**
     52    * @return true if the app's binary data is resident in memory, i.e. a
     53    *         previous call to loadFromBuffer() or loadStatic() was successful
     54    */
     55   bool isLoaded() const;
     56 
     57  protected:
     58   //! The app ID we received in the metadata alongside the nanoapp binary. This
     59   //! is also included in (and checked against) mAppInfo.
     60   uint64_t mExpectedAppId;
     61 
     62   //! The dynamic shared object (DSO) handle returned by dlopen.
     63   void *mDsoHandle = nullptr;
     64 
     65   //! Pointer to the app info structure within this nanoapp
     66   const struct chreNslNanoappInfo *mAppInfo = nullptr;
     67 
     68   bool mIsStatic = false;
     69 
     70   //! If this is a pre-loaded, but non-static nanoapp (i.e. loaded from
     71   //! loadFromFile), this will be set to the filename string to pass to dlopen()
     72   std::string mFilename;
     73 
     74   /**
     75    * Calls through to openNanoappFromFile if the nanoapp was loaded from a
     76    * shared object or returns true if the nanoapp is static.
     77    *
     78    * @return true if the nanoapp was loaded successfully.
     79    */
     80   bool openNanoapp();
     81 
     82   /**
     83    * Calls dlopen on the app filename, and fetches and validates the app info
     84    * pointer. This will result in execution of any on-load handlers (e.g.
     85    * static global constructors) in the nanoapp.
     86    *
     87    * @return true if the app was opened successfully and the app info
     88    *         structure passed validation
     89    */
     90   bool openNanoappFromFile();
     91 
     92   /**
     93    * Releases the DSO handle if it was active, by calling dlclose(). This will
     94    * result in execution of any unload handlers in the nanoapp.
     95    */
     96   void closeNanoapp();
     97 };
     98 
     99 }  // namespace chre
    100 
    101 #endif  // CHRE_PLATFORM_LINUX_PLATFORM_NANOAPP_BASE_H_
    102