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 private:
243     /**
244      * @brief Get the Ril Function Pointer From Map
245      *
246      * @tparam T function pointer type. see RilFunc_Event, RilFunc_Int_Event,
247      * RilFunc_Int_Int_Event, RilFunc_Int_String_Event
248      * @param funcs map of function pointer
249      * @param radioEvent see RadioEvent
250      * @return T function pointer . if not found , it is nullptr
251      */
252     template<typename T>
253     T GetFunctionOfEvent(const std::map<RadioEvent, T> &funcs, RadioEvent radioEvent);
254 
255     /**
256      * @brief Send event to model of TelRilManager
257      *
258      * @tparam eventGetMode see EventGetType.
259      * @tparam T pointer  type of class ITelRilManager's function. see
260      * RilFunc_Event,RilFunc_Int_Event,RilFunc_Int_Int_Event,RilFunc_Int_String_Event,RilFunc_Capability_Event.
261      * @tparam Args Variable parameters types.
262      * @param parameters tuple of input parameters.
263      * @param args parameters for function calling.
264      * @return true success
265      * @return false fail
266      */
267     template<EventGetMode eventGetMode, typename T, typename... Args>
268     bool Send(
269         std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> &parameters, Args... args);
270 
271     /**
272      * @brief Get the Event object
273      *
274      * @param slotId sim card id
275      * @param radioEvent see RadioEvent
276      * @param param parameter of class NetworkSearchCallbackInfo
277      * @param callback strong pointer class to class INetworkSearchCallback
278      * @return AppExecFwk::InnerEvent::Pointer
279      */
280     AppExecFwk::InnerEvent::Pointer GetEvent(
281         int32_t slotId, RadioEvent radioEvent, int32_t param, const sptr<INetworkSearchCallback> &callback);
282     AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent, int32_t param);
283     AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent);
284 
285     /**
286      * @brief map of function pointer
287      *
288      */
289     static const std::map<RadioEvent, RilFunc_Event> mapFunctions_;
290     static const std::map<RadioEvent, RilFunc_Int_Event> mapFunctionsInt_;
291     static const std::map<RadioEvent, RilFunc_Int_Int_Event> mapFunctionsIntInt_;
292     static const std::map<RadioEvent, RilFunc_Int_String_Event> mapFunctionsIntString_;
293 
294     std::shared_ptr<ITelRilManager> telRilManager_ = nullptr;
295     std::weak_ptr<NetworkSearchManager> networkSearchManager_;
296 };
297 
298 template<typename T>
GetFunctionOfEvent(const std::map<RadioEvent,T> & funcs,RadioEvent radioEvent)299 T EventSender::GetFunctionOfEvent(const std::map<RadioEvent, T> &funcs, RadioEvent radioEvent)
300 {
301     auto itFunc = funcs.find(radioEvent);
302     if (itFunc != funcs.end()) {
303         TELEPHONY_LOGD("GetFunctionOfEvent find");
304         return itFunc->second;
305     }
306     TELEPHONY_LOGI("GetFunctionOfEvent nullptr , radioEvent: %{public}d", radioEvent);
307     return nullptr;
308 }
309 
310 template<EventSender::EventGetMode eventGetMode, typename T, typename... Args>
Send(std::tuple<int32_t,RadioEvent,int32_t,const sptr<INetworkSearchCallback> *,T> & parameters,Args...args)311 bool EventSender::Send(
312     std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> &parameters, Args... args)
313 {
314     if (telRilManager_ == nullptr) {
315         TELEPHONY_LOGE("EventSender::Send telRilManager is null.");
316         return false;
317     }
318     int32_t slotId = 0;
319     int32_t param = 0;
320     RadioEvent radioEvent = RadioEvent::RADIO_STATE_CHANGED;
321     const sptr<INetworkSearchCallback> *callback = nullptr;
322     T rilFuncPointer = nullptr;
323     std::tie(slotId, radioEvent, param, callback, rilFuncPointer) = parameters;
324 
325     if (rilFuncPointer == nullptr) {
326         TELEPHONY_LOGE("EventSender::Send rilFuncPointer is null.");
327         return false;
328     }
329     AppExecFwk::InnerEvent::Pointer event(nullptr, nullptr);
330     switch (eventGetMode) {
331         case EventGetMode::GET_EVENT_BY_HANDLERID: {
332             event = GetEvent(slotId, radioEvent);
333             break;
334         }
335         case EventGetMode::GET_EVENT_BY_INDEX: {
336             if (callback == nullptr) {
337                 return false;
338             }
339             event = GetEvent(slotId, radioEvent, param, *callback);
340             break;
341         }
342         case EventGetMode::GET_EVENT_BY_PARAM: {
343             event = GetEvent(slotId, radioEvent, param);
344             break;
345         }
346         default:
347             TELEPHONY_LOGE("EventSender::Send eventGetMode error.");
348             return false;
349     }
350     if (event == nullptr) {
351         TELEPHONY_LOGE("EventSender::Send event is null.");
352         return false;
353     }
354     if (telRilManager_.get() == nullptr) {
355         TELEPHONY_LOGE("EventSender::Send telRilManager_.get() is null.");
356         return false;
357     }
358     if (rilFuncPointer(telRilManager_.get(), slotId, args..., event) != TELEPHONY_ERR_SUCCESS) {
359         TELEPHONY_LOGE("EventSender::Send process ril function error.");
360         return false;
361     }
362     return true;
363 }
364 } // namespace Telephony
365 } // namespace OHOS
366 #endif // NETWORK_UTILS_H