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