• 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_errors.h"
30 #include "telephony_log_wrapper.h"
31 
32 namespace OHOS {
33 namespace Telephony {
34 struct NetworkSearchCallbackInfo {
35     int32_t param_ = 0;
36     sptr<INetworkSearchCallback> networkSearchItem_ = nullptr;
37 
NetworkSearchCallbackInfoNetworkSearchCallbackInfo38     NetworkSearchCallbackInfo(int32_t param, sptr<INetworkSearchCallback> callback)
39     {
40         param_ = param;
41         networkSearchItem_ = callback;
42     }
43 };
44 
45 class NetworkUtils {
46 public:
47     static PreferredNetworkMode GetNetworkModeFromRaf(int32_t raf);
48     static int32_t GetRafFromNetworkMode(PreferredNetworkMode PreferredNetworkMode);
49     static std::vector<std::string> SplitString(const std::string &inputString, const std::string &flag);
50 
51     static int64_t GetCallbackIndex64bit();
52     static std::shared_ptr<NetworkSearchCallbackInfo> FindNetworkSearchCallback(int64_t index);
53     static bool RemoveCallbackFromMap(int64_t index);
54     static bool AddNetworkSearchCallBack(int64_t index, std::shared_ptr<NetworkSearchCallbackInfo> &callback);
55 
56     template<typename... Args>
57     static std::string FormatString(const std::string &format, Args... args);
58 
59 private:
60     static std::unordered_map<int64_t, std::shared_ptr<NetworkSearchCallbackInfo>> networkSearchCacheMap_;
61     static std::mutex callbackMapMutex_;
62     static std::atomic<int64_t> callbackIndex64bit_;
63     static const int64_t MIN_INDEX = 0x100; // 256
64     static const int64_t MAX_INDEX = 0x7FFFFFFF;
65 };
66 
67 /**
68  * @brief format string
69  *
70  * @tparam Args Variable parameter type
71  * @param format A format string
72  * @param args Arguments referenced by the format specifiers in the format string
73  * @return std::string A formatted string. max size 100.
74  */
75 template<typename... Args>
FormatString(const std::string & format,Args...args)76 std::string NetworkUtils::FormatString(const std::string &format, Args... args)
77 {
78     const size_t size = 100;
79     std::unique_ptr<char[]> buf = std::make_unique<char[]>(size);
80     if (buf == nullptr) {
81         return "";
82     }
83     if (snprintf_s(buf.get(), size, size, format.c_str(), args...) < 0) {
84         return "";
85     }
86     return std::string(buf.get());
87 }
88 
89 class NetworkSearchManager;
90 class ITelRilManager;
91 /**
92  * @brief function pointer of class ITelRilManager.
93  *
94  */
95 using RilFunc_Event = std::function<int32_t(ITelRilManager *, int32_t, const AppExecFwk::InnerEvent::Pointer &)>;
96 using RilFunc_Int_Event =
97     std::function<int32_t(ITelRilManager *, int32_t, int32_t, const AppExecFwk::InnerEvent::Pointer &)>;
98 using RilFunc_Int_Int_Event =
99     std::function<int32_t(ITelRilManager *, int32_t, int32_t, int32_t, const AppExecFwk::InnerEvent::Pointer &)>;
100 using RilFunc_Int_String_Event =
101     std::function<int32_t(ITelRilManager *, int32_t, int32_t, std::string, const AppExecFwk::InnerEvent::Pointer &)>;
102 class EventSender {
103 public:
EventSender(std::shared_ptr<ITelRilManager> & telRilManager,const std::weak_ptr<NetworkSearchManager> & networkSearchManager)104     EventSender(
105         std::shared_ptr<ITelRilManager> &telRilManager, const std::weak_ptr<NetworkSearchManager> &networkSearchManager)
106         : telRilManager_(telRilManager), networkSearchManager_(networkSearchManager)
107     {}
108     virtual ~EventSender() = default;
109 
110     /**
111      * @brief Mode of getting event .HandlerId is necessary , index and param are optional.see details in
112      * AppExecFwk::InnerEvent::Get().
113      *
114      */
115     enum class EventGetMode {
116         /**
117          * @brief AppExecFwk::InnerEvent::Get(radioEvent)
118          *
119          */
120         GET_EVENT_BY_HANDLERID,
121         /**
122          * @brief AppExecFwk::InnerEvent::Get(radioEvent,index)
123          *
124          */
125         GET_EVENT_BY_INDEX,
126         /**
127          * @brief AppExecFwk::InnerEvent::Get(radioEvent,param)
128          *
129          */
130         GET_EVENT_BY_PARAM,
131     };
132 
133     /**
134      * @brief send event to RilBaseManager
135      *
136      * @param slotId sim card id
137      * @param radioEvent see RadioEvent
138      * @return true success
139      * @return false fail
140      */
141     bool SendBase(int32_t slotId, RadioEvent radioEvent);
142 
143     /**
144      * @brief send event to RilBaseManager
145      *
146      * @param slotId sim card id
147      * @param radioEvent see RadioEvent
148      * @param param used for get event and call function
149      * @return true success
150      * @return false fail
151      */
152     bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t param);
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, int32_t secondParam);
165 
166     /**
167      * @brief send event to RilBaseManager
168      *
169      * @param slotId sim card id
170      * @param radioEvent see RadioEvent
171      * @param firstParam used for get event and call function
172      * @param secondParam used for call function
173      * @return true success
174      * @return false fail
175      */
176     bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t firstParam, std::string secondParam);
177 
178     /**
179      * @brief send event to RilBaseManager with callback
180      *
181      * @param slotId sim card id
182      * @param radioEvent see RadioEvent
183      * @param callback pointer to callback interface
184      * @return true success
185      * @return false fail
186      */
187     bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback);
188 
189     /**
190      * @brief send event to RilBaseManager with callback
191      *
192      * @param slotId sim card id
193      * @param radioEvent see RadioEvent
194      * @param callback pointer to callback interface
195      * @param param used for get event and call function
196      * @return true success
197      * @return false fail
198      */
199     bool SendCallback(
200         int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback, int32_t param);
201 
202     /**
203      * @brief send event to RilBaseManager with callback
204      *
205      * @param slotId sim card id
206      * @param radioEvent see RadioEvent
207      * @param callback pointer to callback interface
208      * @param param used for get event and call function
209      * @return true success
210      * @return false fail
211      */
212     bool SendCallbackEx(
213         int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback, int32_t param);
214 
215     /**
216      * @brief send event to RilBaseManager with callback
217      *
218      * @param slotId sim card id
219      * @param radioEvent see RadioEvent
220      * @param callback pointer to callback interface
221      * @param firstParam used for get event and param of fun
222      * @param secondParam param of fun
223      * @return true success
224      * @return false fail
225      */
226     bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback,
227         int32_t firstParam, int32_t secondParam);
228 
229     /**
230      * @brief
231      *
232      * @param radioEvent see RadioEvent
233      * @param callback pointer to callback interface
234      * @param firstParam param of fun
235      * @param secondParam param of fun
236      * @return true success
237      * @return false fail
238      */
239     bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback,
240         int32_t firstParam, std::string secondParam);
241 
242     /**
243      * @brief send event to RilBaseManager with callback
244      *
245      * @param slotId sim card id
246      * @param radioEvent see RadioEvent
247      * @param callback pointer to callback interface
248      * @param isChipsetNetworkExtSupported indicates whether the chipset supports networkExt
249      * @return true success
250      * @return false fail
251      */
252     bool SendCallbackNetworkExt(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback,
253         bool isChipsetNetworkExtSupported);
254 
255 private:
256     /**
257      * @brief Get the Ril Function Pointer From Map
258      *
259      * @tparam T function pointer type. see RilFunc_Event, RilFunc_Int_Event,
260      * RilFunc_Int_Int_Event, RilFunc_Int_String_Event
261      * @param funcs map of function pointer
262      * @param radioEvent see RadioEvent
263      * @return T function pointer . if not found , it is nullptr
264      */
265     template<typename T>
266     T GetFunctionOfEvent(const std::map<RadioEvent, T> &funcs, RadioEvent radioEvent);
267 
268     /**
269      * @brief Send event to model of TelRilManager
270      *
271      * @tparam eventGetMode see EventGetType.
272      * @tparam T pointer  type of class ITelRilManager's function. see
273      * RilFunc_Event,RilFunc_Int_Event,RilFunc_Int_Int_Event,RilFunc_Int_String_Event,RilFunc_Capability_Event.
274      * @tparam Args Variable parameters types.
275      * @param parameters tuple of input parameters.
276      * @param args parameters for function calling.
277      * @return true success
278      * @return false fail
279      */
280     template<EventGetMode eventGetMode, typename T, typename... Args>
281     bool Send(
282         std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> &parameters, Args... args);
283 
284     /**
285      * @brief Get the Event object
286      *
287      * @param slotId sim card id
288      * @param radioEvent see RadioEvent
289      * @param param parameter of class NetworkSearchCallbackInfo
290      * @param callback strong pointer class to class INetworkSearchCallback
291      * @return AppExecFwk::InnerEvent::Pointer
292      */
293     AppExecFwk::InnerEvent::Pointer GetEvent(
294         int32_t slotId, RadioEvent radioEvent, int32_t param, const sptr<INetworkSearchCallback> &callback);
295     AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent, int32_t param);
296     AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent);
297 
298     /**
299      * @brief map of function pointer
300      *
301      */
302     static const std::map<RadioEvent, RilFunc_Event> mapFunctions_;
303     static const std::map<RadioEvent, RilFunc_Int_Event> mapFunctionsInt_;
304     static const std::map<RadioEvent, RilFunc_Int_Int_Event> mapFunctionsIntInt_;
305     static const std::map<RadioEvent, RilFunc_Int_String_Event> mapFunctionsIntString_;
306 
307     std::shared_ptr<ITelRilManager> telRilManager_ = nullptr;
308     std::weak_ptr<NetworkSearchManager> networkSearchManager_;
309 };
310 
311 template<typename T>
GetFunctionOfEvent(const std::map<RadioEvent,T> & funcs,RadioEvent radioEvent)312 T EventSender::GetFunctionOfEvent(const std::map<RadioEvent, T> &funcs, RadioEvent radioEvent)
313 {
314     auto itFunc = funcs.find(radioEvent);
315     if (itFunc != funcs.end()) {
316         TELEPHONY_LOGD("GetFunctionOfEvent find");
317         return itFunc->second;
318     }
319     TELEPHONY_LOGI("GetFunctionOfEvent nullptr , radioEvent: %{public}d", radioEvent);
320     return nullptr;
321 }
322 
323 template<EventSender::EventGetMode eventGetMode, typename T, typename... Args>
Send(std::tuple<int32_t,RadioEvent,int32_t,const sptr<INetworkSearchCallback> *,T> & parameters,Args...args)324 bool EventSender::Send(
325     std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> &parameters, Args... args)
326 {
327     if (telRilManager_ == nullptr) {
328         TELEPHONY_LOGE("EventSender::Send telRilManager is null.");
329         return false;
330     }
331     int32_t slotId = 0;
332     int32_t param = 0;
333     RadioEvent radioEvent = RadioEvent::RADIO_STATE_CHANGED;
334     const sptr<INetworkSearchCallback> *callback = nullptr;
335     T rilFuncPointer = nullptr;
336     std::tie(slotId, radioEvent, param, callback, rilFuncPointer) = parameters;
337 
338     if (rilFuncPointer == nullptr) {
339         TELEPHONY_LOGE("EventSender::Send rilFuncPointer is null.");
340         return false;
341     }
342     AppExecFwk::InnerEvent::Pointer event(nullptr, nullptr);
343     switch (eventGetMode) {
344         case EventGetMode::GET_EVENT_BY_HANDLERID: {
345             event = GetEvent(slotId, radioEvent);
346             break;
347         }
348         case EventGetMode::GET_EVENT_BY_INDEX: {
349             if (callback == nullptr) {
350                 return false;
351             }
352             event = GetEvent(slotId, radioEvent, param, *callback);
353             break;
354         }
355         case EventGetMode::GET_EVENT_BY_PARAM: {
356             event = GetEvent(slotId, radioEvent, param);
357             break;
358         }
359         default:
360             TELEPHONY_LOGE("EventSender::Send eventGetMode error.");
361             return false;
362     }
363     if (event == nullptr) {
364         TELEPHONY_LOGE("EventSender::Send event is null.");
365         return false;
366     }
367     if (telRilManager_.get() == nullptr) {
368         TELEPHONY_LOGE("EventSender::Send telRilManager_.get() is null.");
369         return false;
370     }
371     if (rilFuncPointer(telRilManager_.get(), slotId, args..., event) != TELEPHONY_ERR_SUCCESS) {
372         TELEPHONY_LOGE("EventSender::Send process ril function error.");
373         return false;
374     }
375     return true;
376 }
377 } // namespace Telephony
378 } // namespace OHOS
379 #endif // NETWORK_UTILS_H