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   void logStateToBuffer(char *buffer, size_t *bufferPos,
75                         size_t bufferSize) const;
76 
77  private:
78   //! The instance of the platform WWAN interface.
79   PlatformWwan mPlatformWwan;
80 
81   // TODO: Support multiple requests for cell info by enqueuing them and
82   // requesting one after another.
83   //! The nanoapp that is currently requesting cell info. At this time only one
84   //! nanoapp can have a pending request for cell info.
85   Optional<uint32_t> mCellInfoRequestingNanoappInstanceId;
86 
87   //! The cookie passed in by a nanoapp making a request for cell info. Note
88   //! that this will only be valid if the mCellInfoRequestingNanoappInstanceId
89   //! is set.
90   const void *mCellInfoRequestingNanoappCookie;
91 
92   /**
93    * Handles the result of a request for cell info. See handleCellInfoResult
94    * which may be called from any thread. This thread is intended to be invoked
95    * on the CHRE event loop thread.
96    *
97    * @param result the result of the request for cell info.
98    */
99   void handleCellInfoResultSync(chreWwanCellInfoResult *result);
100 
101   /**
102    * Handles the releasing of a WWAN cell info result and unsubscribes the
103    * nanoapp who made the request for cell info from cell info events.
104    *
105    * @param result The cell info result to release.
106    */
107   void handleFreeCellInfoResult(chreWwanCellInfoResult *result);
108 
109   /**
110    * Releases a cell info result after nanoapps have consumed it.
111    *
112    * @param eventType the type of event being freed.
113    * @param eventData a pointer to the scan event to release.
114    */
115   static void freeCellInfoResultCallback(uint16_t eventType, void *eventData);
116 };
117 
118 }  // namespace chre
119 
120 #endif  // CHRE_CORE_WWAN_REQUEST_MANAGER_H_
121