Home | History | Annotate | Download | only in nanoapp
      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 #include "chre/platform/shared/nanoapp_support_lib_dso.h"
     18 
     19 #include <chre.h>
     20 #include <dlfcn.h>
     21 
     22 #include "chre/util/macros.h"
     23 
     24 /**
     25  * @file
     26  * The Nanoapp Support Library (NSL) that gets built with nanoapps to act as an
     27  * intermediary to the reference CHRE implementation. It provides hooks so the
     28  * app can be registered with the system, and also provides a layer where we can
     29  * implement cross-version compatibility features as needed.
     30  */
     31 
     32 #ifdef CHRE_SLPI_UIMG_ENABLED
     33 constexpr int kIsTcmNanoapp = 1;
     34 #else
     35 constexpr int kIsTcmNanoapp = 0;
     36 #endif  // CHRE_SLPI_UIMG_ENABLED
     37 
     38 DLL_EXPORT extern "C" const struct chreNslNanoappInfo _chreNslDsoNanoappInfo = {
     39   .magic = CHRE_NSL_NANOAPP_INFO_MAGIC,
     40   .structMinorVersion = CHRE_NSL_NANOAPP_INFO_STRUCT_MINOR_VERSION,
     41   .targetApiVersion = CHRE_API_VERSION,
     42 
     43   // These values are supplied by the build environment
     44   .vendor = NANOAPP_VENDOR_STRING,
     45   .name = NANOAPP_NAME_STRING,
     46   .isSystemNanoapp = NANOAPP_IS_SYSTEM_NANOAPP,
     47   .isTcmNanoapp = kIsTcmNanoapp,
     48   .appId = NANOAPP_ID,
     49   .appVersion = NANOAPP_VERSION,
     50 
     51   .entryPoints = {
     52     .start = nanoappStart,
     53     .handleEvent = nanoappHandleEvent,
     54     .end = nanoappEnd,
     55   },
     56 };
     57 
     58 // The code section below provides default implementations for new symbols
     59 // introduced in CHRE API v1.2 to provide binary compatibility with previous
     60 // CHRE implementations. Note that we don't presently include symbols for v1.1,
     61 // as the current known set of CHRE platforms that use this NSL implementation
     62 // are all v1.1+.
     63 // If a nanoapp knows that it is only targeting v1.2+ platforms, it can define
     64 // the CHRE_NANOAPP_DISABLE_BACKCOMPAT flag, so this indirection will be avoided
     65 // at the expense of a nanoapp not being able to load at all on prior
     66 // implementations.
     67 
     68 #ifndef CHRE_NANOAPP_DISABLE_BACKCOMPAT
     69 
     70 /**
     71  * Lazily calls dlsym to find the function pointer for a given function
     72  * (provided without quotes) in another library (i.e. the CHRE platform DSO),
     73  * caching and returning the result.
     74  */
     75 #define CHRE_NSL_LAZY_LOOKUP(functionName) ({         \
     76     static bool lookupPerformed = false;              \
     77     static decltype(functionName) *fptr = nullptr;    \
     78     if (!lookupPerformed) {                           \
     79       fptr = reinterpret_cast<decltype(fptr)>(        \
     80           dlsym(RTLD_NEXT, STRINGIFY(functionName))); \
     81       lookupPerformed = true;                         \
     82     }                                                 \
     83     fptr; })
     84 
     85 WEAK_SYMBOL
     86 bool chreAudioGetSource(uint32_t handle, struct chreAudioSource *audioSource) {
     87   auto *fptr = CHRE_NSL_LAZY_LOOKUP(chreAudioGetSource);
     88   return (fptr != nullptr) ? fptr(handle, audioSource) : false;
     89 }
     90 
     91 WEAK_SYMBOL
     92 bool chreAudioConfigureSource(uint32_t handle, bool enable,
     93                               uint64_t bufferDuration,
     94                               uint64_t deliveryInterval) {
     95   auto *fptr = CHRE_NSL_LAZY_LOOKUP(chreAudioConfigureSource);
     96   return (fptr != nullptr) ?
     97       fptr(handle, enable, bufferDuration, deliveryInterval) : false;
     98 }
     99 
    100 WEAK_SYMBOL
    101 bool chreAudioGetStatus(uint32_t handle, struct chreAudioSourceStatus *status) {
    102   auto *fptr = CHRE_NSL_LAZY_LOOKUP(chreAudioGetStatus);
    103   return (fptr != nullptr) ? fptr(handle, status) : false;
    104 }
    105 
    106 WEAK_SYMBOL
    107 void chreConfigureHostSleepStateEvents(bool enable) {
    108   auto *fptr = CHRE_NSL_LAZY_LOOKUP(chreConfigureHostSleepStateEvents);
    109   if (fptr != nullptr) {
    110     fptr(enable);
    111   }
    112 }
    113 
    114 WEAK_SYMBOL
    115 bool chreIsHostAwake(void) {
    116   auto *fptr = CHRE_NSL_LAZY_LOOKUP(chreIsHostAwake);
    117   return (fptr != nullptr) ? fptr() : false;
    118 }
    119 
    120 WEAK_SYMBOL
    121 bool chreGnssConfigureLocationMonitor(bool enable) {
    122   auto *fptr = CHRE_NSL_LAZY_LOOKUP(chreGnssConfigureLocationMonitor);
    123   return (fptr != nullptr) ? fptr(enable) : false;
    124 }
    125 
    126 WEAK_SYMBOL
    127 bool chreWifiRequestRangingAsync(const struct chreWifiRangingParams *params,
    128                                  const void *cookie) {
    129   auto *fptr = CHRE_NSL_LAZY_LOOKUP(chreWifiRequestRangingAsync);
    130   return (fptr != nullptr) ? fptr(params, cookie) : false;
    131 }
    132 
    133 #endif  // CHRE_NANOAPP_DISABLE_BACKCOMPAT
    134