• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef NETWORK_UTILS_H
17 #define NETWORK_UTILS_H
18 
19 #include <any>
20 #include <memory>
21 #include <mutex>
22 #include <string>
23 #include <vector>
24 
25 #include "i_network_search.h"
26 #include "network_search_types.h"
27 #include "radio_event.h"
28 #include "securec.h"
29 #include "telephony_log_wrapper.h"
30 
31 namespace OHOS {
32 namespace Telephony {
33 struct NetworkSearchCallbackInfo {
34     int32_t param_;
35     sptr<INetworkSearchCallback> networkSearchItem_;
36 
NetworkSearchCallbackInfoNetworkSearchCallbackInfo37     NetworkSearchCallbackInfo(int32_t param, sptr<INetworkSearchCallback> callback)
38     {
39         param_ = param;
40         networkSearchItem_ = callback;
41     }
42 };
43 
44 class NetworkUtils {
45 public:
46     static PreferredNetworkMode GetNetworkModeFromRaf(int32_t raf);
47     static int32_t GetRafFromNetworkMode(PreferredNetworkMode PreferredNetworkMode);
48     static std::vector<std::string> SplitString(const std::string &inputString, const std::string &flag);
49 
50     static int64_t GetCallbackIndex64bit();
51     static std::shared_ptr<NetworkSearchCallbackInfo> FindNetworkSearchCallback(int64_t index);
52     static bool RemoveCallbackFromMap(int64_t index);
53     static bool AddNetworkSearchCallBack(int64_t index, std::shared_ptr<NetworkSearchCallbackInfo> &callback);
54 
55     template<typename... Args>
56     static std::string FormatString(const std::string &format, Args... args);
57 
58 private:
59     static std::unordered_map<int64_t, std::shared_ptr<NetworkSearchCallbackInfo>> networkSearchCacheMap_;
60     static std::mutex callbackMapMutex_;
61     static std::atomic<int64_t> callbackIndex64bit_;
62     static const int64_t MIN_INDEX = 0x100; // 256
63     static const int64_t MAX_INDEX = 0x7FFFFFFF;
64 };
65 
66 /**
67  * @brief format string
68  *
69  * @tparam Args Variable parameter type
70  * @param format A format string
71  * @param args Arguments referenced by the format specifiers in the format string
72  * @return std::string A formatted string. max size 100.
73  */
74 template<typename... Args>
FormatString(const std::string & format,Args...args)75 std::string NetworkUtils::FormatString(const std::string &format, Args... args)
76 {
77     const size_t size = 100;
78     std::unique_ptr<char[]> buf = std::make_unique<char[]>(size);
79     if (buf == nullptr) {
80         return "";
81     }
82     if (snprintf_s(buf.get(), size, size, format.c_str(), args...) < 0) {
83         return "";
84     }
85     return std::string(buf.get());
86 }
87 
88 class NetworkSearchManager;
89 class ITelRilManager;
90 class EventSender {
91 public:
EventSender(std::shared_ptr<ITelRilManager> & telRilManager,const std::weak_ptr<NetworkSearchManager> & networkSearchManager)92     EventSender(
93         std::shared_ptr<ITelRilManager> &telRilManager, const std::weak_ptr<NetworkSearchManager> &networkSearchManager)
94         : telRilManager_(telRilManager), networkSearchManager_(networkSearchManager)
95     {}
96     virtual ~EventSender() = default;
97 
98     /**
99      * @brief Mode of getting event .HandlerId is necessary , index and param are optional.see details in
100      * AppExecFwk::InnerEvent::Get().
101      *
102      */
103     enum class EventGetMode {
104         /**
105          * @brief AppExecFwk::InnerEvent::Get(radioEvent)
106          *
107          */
108         GET_EVENT_BY_HANDLERID,
109         /**
110          * @brief AppExecFwk::InnerEvent::Get(radioEvent,index)
111          *
112          */
113         GET_EVENT_BY_INDEX,
114         /**
115          * @brief AppExecFwk::InnerEvent::Get(radioEvent,param)
116          *
117          */
118         GET_EVENT_BY_PARAM,
119     };
120 
121     /**
122      * @brief send event to RilBaseManager
123      *
124      * @param slotId sim card id
125      * @param radioEvent see RadioEvent
126      * @return true success
127      * @return false fail
128      */
129     bool SendBase(int32_t slotId, RadioEvent radioEvent);
130 
131     /**
132      * @brief send event to RilBaseManager
133      *
134      * @param slotId sim card id
135      * @param radioEvent see RadioEvent
136      * @param param used for get event and call function
137      * @return true success
138      * @return false fail
139      */
140     bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t param);
141 
142     /**
143      * @brief send event to RilBaseManager
144      *
145      * @param slotId sim card id
146      * @param radioEvent see RadioEvent
147      * @param firstParam used for get event and call function
148      * @param secondParam used for call function
149      * @return true success
150      * @return false fail
151      */
152     bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t firstParam, int32_t secondParam);
153 
154     /**
155      * @brief send event to RilBaseManager
156      *
157      * @param slotId sim card id
158      * @param radioEvent see RadioEvent
159      * @param firstParam used for get event and call function
160      * @param secondParam used for call function
161      * @return true success
162      * @return false fail
163      */
164     bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t firstParam, std::string secondParam);
165 
166     /**
167      * @brief send event to RilBaseManager with callback
168      *
169      * @param slotId sim card id
170      * @param radioEvent see RadioEvent
171      * @param callback pointer to callback interface
172      * @return true success
173      * @return false fail
174      */
175     bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback);
176 
177     /**
178      * @brief send event to RilBaseManager with callback
179      *
180      * @param slotId sim card id
181      * @param radioEvent see RadioEvent
182      * @param callback pointer to callback interface
183      * @param param used for get event and call function
184      * @return true success
185      * @return false fail
186      */
187     bool SendCallback(
188         int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback, int32_t param);
189 
190     /**
191      * @brief send event to RilBaseManager with callback
192      *
193      * @param slotId sim card id
194      * @param radioEvent see RadioEvent
195      * @param callback pointer to callback interface
196      * @param param used for get event and call function
197      * @return true success
198      * @return false fail
199      */
200     bool SendCallbackEx(
201         int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback, int32_t param);
202 
203     /**
204      * @brief send event to RilBaseManager with callback
205      *
206      * @param slotId sim card id
207      * @param radioEvent see RadioEvent
208      * @param callback pointer to callback interface
209      * @param firstParam used for get event and param of fun
210      * @param secondParam param of fun
211      * @return true success
212      * @return false fail
213      */
214     bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback,
215         int32_t firstParam, int32_t secondParam);
216 
217     /**
218      * @brief
219      *
220      * @param radioEvent see RadioEvent
221      * @param callback pointer to callback interface
222      * @param firstParam param of fun
223      * @param secondParam param of fun
224      * @return true success
225      * @return false fail
226      */
227     bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback,
228         int32_t firstParam, std::string secondParam);
229 
230 private:
231     /**
232      * @brief Get the Ril Function Pointer From Map
233      *
234      * @tparam T function pointer type. see RilFunc_Event, RilFunc_Int_Event,
235      * RilFunc_Int_Int_Event, RilFunc_Int_String_Event
236      * @param radioEvent see RadioEvent
237      * @return T function pointer . if not found , it is nullptr
238      */
239     template<typename T>
240     T GetFunctionOfEvent(RadioEvent radioEvent);
241 
242     /**
243      * @brief Send event to model of TelRilManager
244      *
245      * @tparam eventGetMode see EventGetType.
246      * @tparam T pointer  type of class ITelRilManager's function. see
247      * RilFunc_Event,RilFunc_Int_Event,RilFunc_Int_Int_Event,RilFunc_Int_String_Event,RilFunc_Capability_Event.
248      * @tparam Args Variable parameters types.
249      * @param parameters tuple of input parameters.
250      * @param args parameters for function calling.
251      * @return true success
252      * @return false fail
253      */
254     template<EventGetMode eventGetMode, typename T, typename... Args>
255     bool Send(
256         std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> &parameters, Args... args);
257 
258     /**
259      * @brief Get the Event object
260      *
261      * @param slotId sim card id
262      * @param radioEvent see RadioEvent
263      * @param param parameter of class NetworkSearchCallbackInfo
264      * @param callback strong pointer class to class INetworkSearchCallback
265      * @return AppExecFwk::InnerEvent::Pointer
266      */
267     AppExecFwk::InnerEvent::Pointer GetEvent(
268         int32_t slotId, RadioEvent radioEvent, int32_t param, const sptr<INetworkSearchCallback> &callback);
269     AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent, int32_t param);
270     AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent);
271 
272     /**
273      * @brief map of function pointer
274      *
275      */
276     static const std::map<RadioEvent, std::any> mapFunctions_;
277     std::shared_ptr<ITelRilManager> telRilManager_ = nullptr;
278     std::weak_ptr<NetworkSearchManager> networkSearchManager_;
279 };
280 
281 template<typename T>
GetFunctionOfEvent(RadioEvent radioEvent)282 T EventSender::GetFunctionOfEvent(RadioEvent radioEvent)
283 {
284     auto itFunc = mapFunctions_.find(radioEvent);
285     if (itFunc != mapFunctions_.end()) {
286         TELEPHONY_LOGI("GetFunctionOfEvent find");
287         return std::any_cast<T>(itFunc->second);
288     }
289     TELEPHONY_LOGI("GetFunctionOfEvent nullptr");
290     return nullptr;
291 }
292 
293 template<EventSender::EventGetMode eventGetMode, typename T, typename... Args>
Send(std::tuple<int32_t,RadioEvent,int32_t,const sptr<INetworkSearchCallback> *,T> & parameters,Args...args)294 bool EventSender::Send(
295     std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> &parameters, Args... args)
296 {
297     if (telRilManager_ == nullptr) {
298         TELEPHONY_LOGE("EventSender::Send telRilManager is null.");
299         return false;
300     }
301     int32_t slotId = 0;
302     int32_t param = 0;
303     RadioEvent radioEvent = RadioEvent::RADIO_STATE_CHANGED;
304     const sptr<INetworkSearchCallback> *callback = nullptr;
305     T rilFuncPointer = nullptr;
306     std::tie(slotId, radioEvent, param, callback, rilFuncPointer) = parameters;
307 
308     if (rilFuncPointer == nullptr) {
309         TELEPHONY_LOGE("EventSender::Send rilFuncPointer is null.");
310         return false;
311     }
312     AppExecFwk::InnerEvent::Pointer event(nullptr, nullptr);
313     switch (eventGetMode) {
314         case EventGetMode::GET_EVENT_BY_HANDLERID: {
315             event = GetEvent(slotId, radioEvent);
316             break;
317         }
318         case EventGetMode::GET_EVENT_BY_INDEX: {
319             if (callback == nullptr) {
320                 return false;
321             }
322             event = GetEvent(slotId, radioEvent, param, *callback);
323             break;
324         }
325         case EventGetMode::GET_EVENT_BY_PARAM: {
326             event = GetEvent(slotId, radioEvent, param);
327             break;
328         }
329         default:
330             TELEPHONY_LOGE("EventSender::Send eventGetMode error.");
331             return false;
332     }
333     if (event == nullptr) {
334         TELEPHONY_LOGE("EventSender::Send event is null.");
335         return false;
336     }
337     if (telRilManager_.get() == nullptr) {
338         TELEPHONY_LOGE("EventSender::Send telRilManager_.get() is null.");
339         return false;
340     }
341     (telRilManager_.get()->*rilFuncPointer)(slotId, args..., event);
342     return true;
343 }
344 } // namespace Telephony
345 } // namespace OHOS
346 #endif // NETWORK_UTILS_H