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 OHOS_HRIL_BASE_H
17 #define OHOS_HRIL_BASE_H
18
19 #include <any>
20 #include <cstdlib>
21 #include <map>
22 #include <securec.h>
23
24 #include "hdf_remote_service.h"
25 #include "hdf_sbuf_ipc.h"
26 #include "hril_types.h"
27 #include "telephony_log_wrapper.h"
28 #include "v1_3/iril.h"
29 #include "v1_3/iril_callback.h"
30 #include "hril_notification.h"
31
32 namespace OHOS {
33 namespace Telephony {
34 class IHRilReporter {
35 public:
36 virtual ReqDataInfo *CreateHRilRequest(int32_t serial, int32_t slotId, int32_t request) = 0;
37 virtual void ReleaseHRilRequest(int32_t request, ReqDataInfo *requestInfo) = 0;
38 };
39
40 class HRilBase {
41 public:
42 // The "reply" event processing entry.
43 template<typename T>
44 int32_t ProcessResponse(
45 int32_t code, HDI::Ril::V1_1::RilRadioResponseInfo &responseInfo, const void *response, size_t responseLen);
46 // The "Active reporting" event processing entry.
47 template<typename T>
48 int32_t ProcessNotify(
49 int32_t notifyType, const struct ReportInfo *reportInfo, const void *response, size_t responseLen);
50 void SetRilCallback(const sptr<HDI::Ril::V1_3::IRilCallback> &callback);
51 std::string StringToHex(const char *data, int byteLength);
52
53 protected:
HRilBase(int32_t slotId)54 HRilBase(int32_t slotId) : slotId_(slotId) {}
~HRilBase()55 virtual ~HRilBase() {}
56 HRilNotiType ConvertIntToRadioNoticeType(int32_t indicationType);
57 uint8_t ConvertHexCharToInt(uint8_t c);
58 uint8_t *ConvertHexStringToBytes(const void *response, size_t responseLen);
59 bool ConvertToString(char **dest, const std::string &src);
60 void CopyToCharPoint(char **a, const std::string &temp);
61 HDI::Ril::V1_1::RilRadioResponseInfo BuildIHRilRadioResponseInfo(
62 const HDI::Ril::V1_1::RilRadioResponseInfo &responseInfo);
SafeFrees()63 inline void SafeFrees() {}
64 template<typename M, typename... Ms>
SafeFrees(M & m,Ms &...ms)65 inline void SafeFrees(M &m, Ms &...ms)
66 {
67 if (m != nullptr) {
68 free(m);
69 m = nullptr;
70 }
71 SafeFrees(ms...);
72 }
73
74 template<typename FuncType, typename... ParamTypes>
75 inline int32_t Response(
76 HDI::Ril::V1_1::RilRadioResponseInfo &responseInfo, FuncType &&_func, ParamTypes &&... _args);
77 template<typename FuncType, typename... ParamTypes>
78 inline int32_t Notify(int32_t notifyType, const HRilErrNumber error, FuncType &&_func, ParamTypes &&... _args);
79 int32_t ConvertHexStringToInt(char **response, int32_t index, int32_t length);
StringToCString(const std::string & src)80 inline char *StringToCString(const std::string &src)
81 {
82 return static_cast<char *>(const_cast<char *>(src.c_str()));
83 }
84
85 // get slotid
GetSlotId()86 int32_t GetSlotId() const
87 {
88 return slotId_;
89 }
90 ReqDataInfo *CreateHRilRequest(int32_t serial, int32_t request);
91 template<typename ReqFuncSet, typename FuncPointer, typename... ValueTypes>
92 int32_t RequestVendor(
93 int32_t serial, int32_t requestId, ReqFuncSet reqFuncSet, FuncPointer func, ValueTypes &&... vals);
94
95 protected:
96 using RespFunc = std::function<int32_t(int32_t requestNum, HDI::Ril::V1_1::RilRadioResponseInfo &responseInfo,
97 const void *response, size_t responseLen)>;
98 using NotiFunc =
99 std::function<int32_t(int32_t notifyType, HRilErrNumber error, const void *response, size_t responseLen)>;
100 std::map<uint32_t, RespFunc> respMemberFuncMap_;
101 std::map<uint32_t, NotiFunc> notiMemberFuncMap_;
102 sptr<HDI::Ril::V1_3::IRilCallback> callback_ = nullptr;
103
104 private:
105 // Get the function pointer of the event handler.
106 template<typename F>
107 F GetFunc(std::map<uint32_t, F> &funcs, uint32_t code);
108
109 private:
110 int32_t slotId_;
111 };
112
113 template<typename ReqFuncSet, typename FuncPointer, typename... ValueTypes>
RequestVendor(int32_t serial,int32_t requestId,ReqFuncSet reqFuncSet,FuncPointer func,ValueTypes &&...vals)114 int32_t HRilBase::RequestVendor(
115 int32_t serial, int32_t requestId, ReqFuncSet reqFuncSet, FuncPointer func, ValueTypes &&... vals)
116 {
117 if (reqFuncSet == nullptr || (reqFuncSet->*func) == nullptr) {
118 TELEPHONY_LOGE("reqFunSet or reqFuncSet->*fun is null");
119 if (callback_ == nullptr) {
120 TELEPHONY_LOGE("callback is null");
121 return HRIL_ERR_NULL_POINT;
122 }
123 HDI::Ril::V1_1::RilRadioResponseInfo responseInfo = { 0 };
124 responseInfo.slotId = GetSlotId();
125 responseInfo.serial = serial;
126 responseInfo.error = HDI::Ril::V1_1::RilErrType::RIL_ERR_VENDOR_NOT_IMPLEMENT;
127 callback_->CommonErrorResponse(responseInfo);
128 return HRIL_ERR_NULL_POINT;
129 }
130
131 ReqDataInfo *requestInfo = CreateHRilRequest(serial, requestId);
132 if (requestInfo == nullptr) {
133 TELEPHONY_LOGE("requestInfo == nullptr: serial=%{public}d, request=%{public}d", serial, requestId);
134 return HRIL_ERR_MEMORY_FULL;
135 }
136
137 (reqFuncSet->*func)(requestInfo, std::forward<ValueTypes>(vals)...);
138 return HRIL_ERR_SUCCESS;
139 }
140
141 template<typename F>
GetFunc(std::map<uint32_t,F> & funcs,uint32_t code)142 F HRilBase::GetFunc(std::map<uint32_t, F> &funcs, uint32_t code)
143 {
144 auto itFunc = funcs.find(code);
145 if (itFunc != funcs.end()) {
146 return itFunc->second;
147 }
148 if (code != HNOTI_NETWORK_RESTRICTED_STATE_UPDATED) {
149 TELEPHONY_LOGE("Can not find Request code in func map: %{public}d", code);
150 }
151 return nullptr;
152 }
153
154 template<typename T>
ProcessResponse(int32_t code,HDI::Ril::V1_1::RilRadioResponseInfo & responseInfo,const void * response,size_t responseLen)155 int32_t HRilBase::ProcessResponse(
156 int32_t code, HDI::Ril::V1_1::RilRadioResponseInfo &responseInfo, const void *response, size_t responseLen)
157 {
158 auto func = GetFunc<RespFunc>(respMemberFuncMap_, code);
159 if (func != nullptr) {
160 return func(code, responseInfo, response, responseLen);
161 }
162 return HRIL_ERR_INVALID_PARAMETER;
163 }
164
165 template<typename T>
ProcessNotify(int32_t notifyType,const struct ReportInfo * reportInfo,const void * response,size_t responseLen)166 int32_t HRilBase::ProcessNotify(
167 int32_t notifyType, const struct ReportInfo *reportInfo, const void *response, size_t responseLen)
168 {
169 if (reportInfo == nullptr) {
170 return HRIL_ERR_INVALID_PARAMETER;
171 }
172 int32_t code = reportInfo->notifyId;
173 HRilErrNumber error = (HRilErrNumber)reportInfo->error;
174 auto func = GetFunc<NotiFunc>(notiMemberFuncMap_, code);
175 if (func != nullptr) {
176 return func(notifyType, error, response, responseLen);
177 }
178 return HRIL_ERR_INVALID_PARAMETER;
179 }
180
181 template<typename FuncType, typename... ParamTypes>
Response(HDI::Ril::V1_1::RilRadioResponseInfo & responseInfo,FuncType && _func,ParamTypes &&..._args)182 inline int32_t HRilBase::Response(HDI::Ril::V1_1::RilRadioResponseInfo &responseInfo, FuncType &&_func,
183 ParamTypes &&... _args)
184 {
185 if (callback_ == nullptr || _func == nullptr) {
186 TELEPHONY_LOGE("callback_ or _func is null");
187 return HRIL_ERR_NULL_POINT;
188 }
189 (callback_->*(_func))(BuildIHRilRadioResponseInfo(responseInfo), std::forward<ParamTypes>(_args)...);
190 return HRIL_ERR_SUCCESS;
191 }
192
193 template<typename FuncType, typename... ParamTypes>
Notify(int32_t notifyType,const HRilErrNumber error,FuncType && _func,ParamTypes &&..._args)194 inline int32_t HRilBase::Notify(int32_t notifyType, const HRilErrNumber error, FuncType &&_func, ParamTypes &&... _args)
195 {
196 if (callback_ == nullptr) {
197 TELEPHONY_LOGE("callback_ is null");
198 return HRIL_ERR_NULL_POINT;
199 }
200 HDI::Ril::V1_1::RilRadioResponseInfo mResponseInfo = { 0 };
201 mResponseInfo.slotId = GetSlotId();
202 mResponseInfo.type = (HDI::Ril::V1_1::RilResponseTypes)notifyType;
203 (callback_->*(_func))(mResponseInfo, std::forward<ParamTypes>(_args)...);
204 return HRIL_ERR_SUCCESS;
205 }
206 } // namespace Telephony
207 } // namespace OHOS
208 #endif // OHOS_HRIL_UTILS_H
209