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 class EventSender {
92 public:
EventSender(std::shared_ptr<ITelRilManager> & telRilManager,const std::weak_ptr<NetworkSearchManager> & networkSearchManager)93 EventSender(
94 std::shared_ptr<ITelRilManager> &telRilManager, const std::weak_ptr<NetworkSearchManager> &networkSearchManager)
95 : telRilManager_(telRilManager), networkSearchManager_(networkSearchManager)
96 {}
97 virtual ~EventSender() = default;
98
99 /**
100 * @brief Mode of getting event .HandlerId is necessary , index and param are optional.see details in
101 * AppExecFwk::InnerEvent::Get().
102 *
103 */
104 enum class EventGetMode {
105 /**
106 * @brief AppExecFwk::InnerEvent::Get(radioEvent)
107 *
108 */
109 GET_EVENT_BY_HANDLERID,
110 /**
111 * @brief AppExecFwk::InnerEvent::Get(radioEvent,index)
112 *
113 */
114 GET_EVENT_BY_INDEX,
115 /**
116 * @brief AppExecFwk::InnerEvent::Get(radioEvent,param)
117 *
118 */
119 GET_EVENT_BY_PARAM,
120 };
121
122 /**
123 * @brief send event to RilBaseManager
124 *
125 * @param slotId sim card id
126 * @param radioEvent see RadioEvent
127 * @return true success
128 * @return false fail
129 */
130 bool SendBase(int32_t slotId, RadioEvent radioEvent);
131
132 /**
133 * @brief send event to RilBaseManager
134 *
135 * @param slotId sim card id
136 * @param radioEvent see RadioEvent
137 * @param param used for get event and call function
138 * @return true success
139 * @return false fail
140 */
141 bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t param);
142
143 /**
144 * @brief send event to RilBaseManager
145 *
146 * @param slotId sim card id
147 * @param radioEvent see RadioEvent
148 * @param firstParam used for get event and call function
149 * @param secondParam used for call function
150 * @return true success
151 * @return false fail
152 */
153 bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t firstParam, int32_t secondParam);
154
155 /**
156 * @brief send event to RilBaseManager
157 *
158 * @param slotId sim card id
159 * @param radioEvent see RadioEvent
160 * @param firstParam used for get event and call function
161 * @param secondParam used for call function
162 * @return true success
163 * @return false fail
164 */
165 bool SendBase(int32_t slotId, RadioEvent radioEvent, int32_t firstParam, std::string secondParam);
166
167 /**
168 * @brief send event to RilBaseManager with callback
169 *
170 * @param slotId sim card id
171 * @param radioEvent see RadioEvent
172 * @param callback pointer to callback interface
173 * @return true success
174 * @return false fail
175 */
176 bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback);
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 * @param param used for get event and call function
185 * @return true success
186 * @return false fail
187 */
188 bool SendCallback(
189 int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback, int32_t param);
190
191 /**
192 * @brief send event to RilBaseManager with callback
193 *
194 * @param slotId sim card id
195 * @param radioEvent see RadioEvent
196 * @param callback pointer to callback interface
197 * @param param used for get event and call function
198 * @return true success
199 * @return false fail
200 */
201 bool SendCallbackEx(
202 int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback, int32_t param);
203
204 /**
205 * @brief send event to RilBaseManager with callback
206 *
207 * @param slotId sim card id
208 * @param radioEvent see RadioEvent
209 * @param callback pointer to callback interface
210 * @param firstParam used for get event and param of fun
211 * @param secondParam param of fun
212 * @return true success
213 * @return false fail
214 */
215 bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback,
216 int32_t firstParam, int32_t secondParam);
217
218 /**
219 * @brief
220 *
221 * @param radioEvent see RadioEvent
222 * @param callback pointer to callback interface
223 * @param firstParam param of fun
224 * @param secondParam param of fun
225 * @return true success
226 * @return false fail
227 */
228 bool SendCallback(int32_t slotId, RadioEvent radioEvent, const sptr<INetworkSearchCallback> *callback,
229 int32_t firstParam, std::string secondParam);
230
231 private:
232 /**
233 * @brief Get the Ril Function Pointer From Map
234 *
235 * @tparam T function pointer type. see RilFunc_Event, RilFunc_Int_Event,
236 * RilFunc_Int_Int_Event, RilFunc_Int_String_Event
237 * @param radioEvent see RadioEvent
238 * @return T function pointer . if not found , it is nullptr
239 */
240 template<typename T>
241 T GetFunctionOfEvent(RadioEvent radioEvent);
242
243 /**
244 * @brief Send event to model of TelRilManager
245 *
246 * @tparam eventGetMode see EventGetType.
247 * @tparam T pointer type of class ITelRilManager's function. see
248 * RilFunc_Event,RilFunc_Int_Event,RilFunc_Int_Int_Event,RilFunc_Int_String_Event,RilFunc_Capability_Event.
249 * @tparam Args Variable parameters types.
250 * @param parameters tuple of input parameters.
251 * @param args parameters for function calling.
252 * @return true success
253 * @return false fail
254 */
255 template<EventGetMode eventGetMode, typename T, typename... Args>
256 bool Send(
257 std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> ¶meters, Args... args);
258
259 /**
260 * @brief Get the Event object
261 *
262 * @param slotId sim card id
263 * @param radioEvent see RadioEvent
264 * @param param parameter of class NetworkSearchCallbackInfo
265 * @param callback strong pointer class to class INetworkSearchCallback
266 * @return AppExecFwk::InnerEvent::Pointer
267 */
268 AppExecFwk::InnerEvent::Pointer GetEvent(
269 int32_t slotId, RadioEvent radioEvent, int32_t param, const sptr<INetworkSearchCallback> &callback);
270 AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent, int32_t param);
271 AppExecFwk::InnerEvent::Pointer GetEvent(int32_t slotId, RadioEvent radioEvent);
272
273 /**
274 * @brief map of function pointer
275 *
276 */
277 static const std::map<RadioEvent, std::any> mapFunctions_;
278 std::shared_ptr<ITelRilManager> telRilManager_ = nullptr;
279 std::weak_ptr<NetworkSearchManager> networkSearchManager_;
280 };
281
282 template<typename T>
GetFunctionOfEvent(RadioEvent radioEvent)283 T EventSender::GetFunctionOfEvent(RadioEvent radioEvent)
284 {
285 auto itFunc = mapFunctions_.find(radioEvent);
286 if (itFunc != mapFunctions_.end() && itFunc->second.has_value()) {
287 TELEPHONY_LOGI("GetFunctionOfEvent find");
288 return std::any_cast<T>(itFunc->second);
289 }
290 TELEPHONY_LOGI("GetFunctionOfEvent nullptr");
291 return nullptr;
292 }
293
294 template<EventSender::EventGetMode eventGetMode, typename T, typename... Args>
Send(std::tuple<int32_t,RadioEvent,int32_t,const sptr<INetworkSearchCallback> *,T> & parameters,Args...args)295 bool EventSender::Send(
296 std::tuple<int32_t, RadioEvent, int32_t, const sptr<INetworkSearchCallback> *, T> ¶meters, Args... args)
297 {
298 if (telRilManager_ == nullptr) {
299 TELEPHONY_LOGE("EventSender::Send telRilManager is null.");
300 return false;
301 }
302 int32_t slotId = 0;
303 int32_t param = 0;
304 RadioEvent radioEvent = RadioEvent::RADIO_STATE_CHANGED;
305 const sptr<INetworkSearchCallback> *callback = nullptr;
306 T rilFuncPointer = nullptr;
307 std::tie(slotId, radioEvent, param, callback, rilFuncPointer) = parameters;
308
309 if (rilFuncPointer == nullptr) {
310 TELEPHONY_LOGE("EventSender::Send rilFuncPointer is null.");
311 return false;
312 }
313 AppExecFwk::InnerEvent::Pointer event(nullptr, nullptr);
314 switch (eventGetMode) {
315 case EventGetMode::GET_EVENT_BY_HANDLERID: {
316 event = GetEvent(slotId, radioEvent);
317 break;
318 }
319 case EventGetMode::GET_EVENT_BY_INDEX: {
320 if (callback == nullptr) {
321 return false;
322 }
323 event = GetEvent(slotId, radioEvent, param, *callback);
324 break;
325 }
326 case EventGetMode::GET_EVENT_BY_PARAM: {
327 event = GetEvent(slotId, radioEvent, param);
328 break;
329 }
330 default:
331 TELEPHONY_LOGE("EventSender::Send eventGetMode error.");
332 return false;
333 }
334 if (event == nullptr) {
335 TELEPHONY_LOGE("EventSender::Send event is null.");
336 return false;
337 }
338 if (telRilManager_.get() == nullptr) {
339 TELEPHONY_LOGE("EventSender::Send telRilManager_.get() is null.");
340 return false;
341 }
342 if ((telRilManager_.get()->*rilFuncPointer)(slotId, args..., event) != TELEPHONY_ERR_SUCCESS) {
343 TELEPHONY_LOGE("EventSender::Send process ril function error.");
344 return false;
345 }
346 return true;
347 }
348 } // namespace Telephony
349 } // namespace OHOS
350 #endif // NETWORK_UTILS_H