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