Home | History | Annotate | Download | only in core
      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 #ifndef CHRE_CORE_WWAN_REQUEST_MANAGER_H_
     18 #define CHRE_CORE_WWAN_REQUEST_MANAGER_H_
     19 
     20 #include <cstdint>
     21 
     22 #include "chre/core/nanoapp.h"
     23 #include "chre/platform/platform_wwan.h"
     24 #include "chre/util/non_copyable.h"
     25 #include "chre/util/optional.h"
     26 
     27 namespace chre {
     28 
     29 /**
     30  * The WwanRequestManager handles requests from nanoapps for WWAN data. This
     31  * includes multiplexing multiple requests into one for the platform to handle.
     32  *
     33  * This class is effectively a singleton as there can only be one instance of
     34  * the PlatformWwan instance.
     35  */
     36 class WwanRequestManager : public NonCopyable {
     37  public:
     38   /**
     39    * Initializes the underlying platform-specific WWAN module. Must be called
     40    * prior to invoking any other methods in this class.
     41    */
     42   void init();
     43 
     44   /**
     45    * @return the WWAN capabilities exposed by this platform.
     46    */
     47   uint32_t getCapabilities();
     48 
     49   /**
     50    * Performs a request for cell neighbor info for the given nanoapp.
     51    *
     52    * @param nanoapp The nanoapp requesting the cell info.
     53    * @param cookie A cookie provided by the nanoapp to supply context in the
     54    *        asynchronous result event.
     55    * @return true if the request was accepted.
     56    */
     57   bool requestCellInfo(Nanoapp *nanoapp, const void *cookie);
     58 
     59   /**
     60    * Handles the result of a cell info request.
     61    *
     62    * @param result the results of a cell info request.
     63    */
     64   void handleCellInfoResult(chreWwanCellInfoResult *result);
     65 
     66   /**
     67    * Prints state in a string buffer. Must only be called from the context of
     68    * the main CHRE thread.
     69    *
     70    * @param buffer Pointer to the start of the buffer.
     71    * @param bufferPos Pointer to buffer position to start the print (in-out).
     72    * @param size Size of the buffer in bytes.
     73    *
     74    * @return true if entire log printed, false if overflow or error.
     75    */
     76   bool logStateToBuffer(char *buffer, size_t *bufferPos,
     77                         size_t bufferSize) const;
     78 
     79  private:
     80   //! The instance of the platform WWAN interface.
     81   PlatformWwan mPlatformWwan;
     82 
     83   // TODO: Support multiple requests for cell info by enqueuing them and
     84   // requesting one after another.
     85   //! The nanoapp that is currently requesting cell info. At this time only one
     86   //! nanoapp can have a pending request for cell info.
     87   Optional<uint32_t> mCellInfoRequestingNanoappInstanceId;
     88 
     89   //! The cookie passed in by a nanoapp making a request for cell info. Note
     90   //! that this will only be valid if the mCellInfoRequestingNanoappInstanceId
     91   //! is set.
     92   const void *mCellInfoRequestingNanoappCookie;
     93 
     94   /**
     95    * Handles the result of a request for cell info. See handleCellInfoResult
     96    * which may be called from any thread. This thread is intended to be invoked
     97    * on the CHRE event loop thread.
     98    *
     99    * @param result the result of the request for cell info.
    100    */
    101   void handleCellInfoResultSync(chreWwanCellInfoResult *result);
    102 
    103   /**
    104    * Handles the releasing of a WWAN cell info result and unsubscribes the
    105    * nanoapp who made the request for cell info from cell info events.
    106    *
    107    * @param result The cell info result to release.
    108    */
    109   void handleFreeCellInfoResult(chreWwanCellInfoResult *result);
    110 
    111   /**
    112    * Releases a cell info result after nanoapps have consumed it.
    113    *
    114    * @param eventType the type of event being freed.
    115    * @param eventData a pointer to the scan event to release.
    116    */
    117   static void freeCellInfoResultCallback(uint16_t eventType, void *eventData);
    118 };
    119 
    120 }  // namespace chre
    121 
    122 #endif  // CHRE_CORE_WWAN_REQUEST_MANAGER_H_
    123