111fccf17Sopenharmony_ci/* 211fccf17Sopenharmony_ci * Copyright (C) 2021 Huawei Device Co., Ltd. 311fccf17Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 411fccf17Sopenharmony_ci * you may not use this file except in compliance with the License. 511fccf17Sopenharmony_ci * You may obtain a copy of the License at 611fccf17Sopenharmony_ci * 711fccf17Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 811fccf17Sopenharmony_ci * 911fccf17Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 1011fccf17Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 1111fccf17Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1211fccf17Sopenharmony_ci * See the License for the specific language governing permissions and 1311fccf17Sopenharmony_ci * limitations under the License. 1411fccf17Sopenharmony_ci */ 1511fccf17Sopenharmony_ci 1611fccf17Sopenharmony_ci#include "hril_base.h" 1711fccf17Sopenharmony_ci 1811fccf17Sopenharmony_ci#include <sstream> 1911fccf17Sopenharmony_ci#include <string_ex.h> 2011fccf17Sopenharmony_ci 2111fccf17Sopenharmony_ci#include "hril_manager.h" 2211fccf17Sopenharmony_ci 2311fccf17Sopenharmony_cinamespace OHOS { 2411fccf17Sopenharmony_cinamespace Telephony { 2511fccf17Sopenharmony_cistatic constexpr uint8_t HEX_OFFSET = 4; 2611fccf17Sopenharmony_cistatic constexpr char HEX_TABLE[] = "0123456789ABCDEF"; 2711fccf17Sopenharmony_ci 2811fccf17Sopenharmony_ciint32_t HRilBase::ConvertHexStringToInt(char **response, int32_t index, int32_t length) 2911fccf17Sopenharmony_ci{ 3011fccf17Sopenharmony_ci const int32_t hexBase = HRIL_INVALID_HEX_CHAR; 3111fccf17Sopenharmony_ci if ((response == nullptr) || (length <= index) || (response[index] == nullptr)) { 3211fccf17Sopenharmony_ci return HRIL_ERR_GENERIC_FAILURE; 3311fccf17Sopenharmony_ci } 3411fccf17Sopenharmony_ci return strtol(response[index], nullptr, hexBase); 3511fccf17Sopenharmony_ci} 3611fccf17Sopenharmony_ci 3711fccf17Sopenharmony_ciHRilNotiType HRilBase::ConvertIntToRadioNoticeType(int32_t indicationType) 3811fccf17Sopenharmony_ci{ 3911fccf17Sopenharmony_ci return (indicationType == (int32_t)ReportType::HRIL_NOTIFICATION) ? (HRilNotiType::HRIL_NOTIFICATION) : 4011fccf17Sopenharmony_ci (HRilNotiType::HRIL_NO_DEFINE); 4111fccf17Sopenharmony_ci} 4211fccf17Sopenharmony_ci 4311fccf17Sopenharmony_ciuint8_t HRilBase::ConvertHexCharToInt(uint8_t ch) 4411fccf17Sopenharmony_ci{ 4511fccf17Sopenharmony_ci if ((ch >= 'a' && ch <= 'f') || (ch >= 'A' && ch <= 'F')) { 4611fccf17Sopenharmony_ci return ((ch - 'A') % HRIL_UPPER_CASE_LETTERS_OFFSET + HRIL_DEC); 4711fccf17Sopenharmony_ci } else if (ch >= '0' && ch <= '9') { 4811fccf17Sopenharmony_ci return (ch - '0'); 4911fccf17Sopenharmony_ci } else { 5011fccf17Sopenharmony_ci return HRIL_INVALID_HEX_CHAR; 5111fccf17Sopenharmony_ci } 5211fccf17Sopenharmony_ci} 5311fccf17Sopenharmony_ci 5411fccf17Sopenharmony_ciuint8_t *HRilBase::ConvertHexStringToBytes(const void *response, size_t length) 5511fccf17Sopenharmony_ci{ 5611fccf17Sopenharmony_ci const int32_t SIZE_VALUE = 2; 5711fccf17Sopenharmony_ci const int32_t BIT_NUM = 4; 5811fccf17Sopenharmony_ci 5911fccf17Sopenharmony_ci if (response == nullptr) { 6011fccf17Sopenharmony_ci TELEPHONY_LOGE("response is null!!!"); 6111fccf17Sopenharmony_ci return nullptr; 6211fccf17Sopenharmony_ci } 6311fccf17Sopenharmony_ci 6411fccf17Sopenharmony_ci size_t bytesLen = length / SIZE_VALUE; 6511fccf17Sopenharmony_ci if (length % SIZE_VALUE != 0 || bytesLen <= 0) { 6611fccf17Sopenharmony_ci TELEPHONY_LOGE("invalid length: %{public}zu", length); 6711fccf17Sopenharmony_ci return nullptr; 6811fccf17Sopenharmony_ci } 6911fccf17Sopenharmony_ci uint8_t *bytes = (uint8_t *)calloc(bytesLen, sizeof(uint8_t)); 7011fccf17Sopenharmony_ci if (bytes == nullptr) { 7111fccf17Sopenharmony_ci TELEPHONY_LOGE("ConvertHexStringToBytes: cannot allocate memory for bytes string"); 7211fccf17Sopenharmony_ci return nullptr; 7311fccf17Sopenharmony_ci } 7411fccf17Sopenharmony_ci 7511fccf17Sopenharmony_ci uint8_t *hexStr = (uint8_t *)response; 7611fccf17Sopenharmony_ci size_t i = 0; 7711fccf17Sopenharmony_ci while (i < length) { 7811fccf17Sopenharmony_ci uint8_t hexCh1 = ConvertHexCharToInt(hexStr[i]); 7911fccf17Sopenharmony_ci uint8_t hexCh2 = ConvertHexCharToInt(hexStr[i + 1]); 8011fccf17Sopenharmony_ci if (hexCh1 == HRIL_INVALID_HEX_CHAR || hexCh2 == HRIL_INVALID_HEX_CHAR) { 8111fccf17Sopenharmony_ci free(bytes); 8211fccf17Sopenharmony_ci return nullptr; 8311fccf17Sopenharmony_ci } 8411fccf17Sopenharmony_ci bytes[i / SIZE_VALUE] = ((hexCh1 << BIT_NUM) | hexCh2); 8511fccf17Sopenharmony_ci i += SIZE_VALUE; 8611fccf17Sopenharmony_ci } 8711fccf17Sopenharmony_ci return bytes; 8811fccf17Sopenharmony_ci} 8911fccf17Sopenharmony_ci 9011fccf17Sopenharmony_cibool HRilBase::ConvertToString(char **dest, const std::string &srcStr) 9111fccf17Sopenharmony_ci{ 9211fccf17Sopenharmony_ci if (dest == nullptr) { 9311fccf17Sopenharmony_ci TELEPHONY_LOGE("ConvertToString dest is null"); 9411fccf17Sopenharmony_ci return false; 9511fccf17Sopenharmony_ci } 9611fccf17Sopenharmony_ci size_t size = srcStr.size(); 9711fccf17Sopenharmony_ci if (size == 0) { 9811fccf17Sopenharmony_ci *dest = nullptr; 9911fccf17Sopenharmony_ci return true; 10011fccf17Sopenharmony_ci } 10111fccf17Sopenharmony_ci size_t len = size + 1; 10211fccf17Sopenharmony_ci if (len <= 0) { 10311fccf17Sopenharmony_ci return false; 10411fccf17Sopenharmony_ci } 10511fccf17Sopenharmony_ci 10611fccf17Sopenharmony_ci *dest = (char *)calloc(len, sizeof(char)); 10711fccf17Sopenharmony_ci if (*dest == nullptr) { 10811fccf17Sopenharmony_ci TELEPHONY_LOGE("ConvertToString malloc fail"); 10911fccf17Sopenharmony_ci return false; 11011fccf17Sopenharmony_ci } 11111fccf17Sopenharmony_ci if (strncpy_s(*dest, len, reinterpret_cast<const char *>(srcStr.c_str()), size) != EOK) { 11211fccf17Sopenharmony_ci return false; 11311fccf17Sopenharmony_ci } 11411fccf17Sopenharmony_ci return true; 11511fccf17Sopenharmony_ci} 11611fccf17Sopenharmony_ci 11711fccf17Sopenharmony_civoid HRilBase::CopyToCharPoint(char **dest, const std::string &src) 11811fccf17Sopenharmony_ci{ 11911fccf17Sopenharmony_ci size_t size = src.size(); 12011fccf17Sopenharmony_ci if (size <= 0) { 12111fccf17Sopenharmony_ci TELEPHONY_LOGD("CopyToCharPoint src is null"); 12211fccf17Sopenharmony_ci return; 12311fccf17Sopenharmony_ci } 12411fccf17Sopenharmony_ci *dest = (char *)malloc((size + 1) * sizeof(char)); 12511fccf17Sopenharmony_ci if (*dest == nullptr) { 12611fccf17Sopenharmony_ci TELEPHONY_LOGE("CopyToCharPoint malloc content fail!"); 12711fccf17Sopenharmony_ci return; 12811fccf17Sopenharmony_ci } 12911fccf17Sopenharmony_ci if (memset_s(*dest, size + 1, 0, size + 1) != EOK) { 13011fccf17Sopenharmony_ci TELEPHONY_LOGE("CopyToCharPoint memset_s failed"); 13111fccf17Sopenharmony_ci SafeFrees(*dest); 13211fccf17Sopenharmony_ci return; 13311fccf17Sopenharmony_ci } 13411fccf17Sopenharmony_ci if (strcpy_s(*dest, size + 1, src.c_str()) != EOK) { 13511fccf17Sopenharmony_ci TELEPHONY_LOGE("CopyToCharPoint strcpy_s error"); 13611fccf17Sopenharmony_ci SafeFrees(*dest); 13711fccf17Sopenharmony_ci } 13811fccf17Sopenharmony_ci} 13911fccf17Sopenharmony_ci 14011fccf17Sopenharmony_ciReqDataInfo *HRilBase::CreateHRilRequest(int32_t serial, int32_t request) 14111fccf17Sopenharmony_ci{ 14211fccf17Sopenharmony_ci if (HRilManager::manager_ == nullptr) { 14311fccf17Sopenharmony_ci TELEPHONY_LOGE("manager_ is nullptr"); 14411fccf17Sopenharmony_ci return nullptr; 14511fccf17Sopenharmony_ci } 14611fccf17Sopenharmony_ci return HRilManager::manager_->CreateHRilRequest(serial, slotId_, request); 14711fccf17Sopenharmony_ci} 14811fccf17Sopenharmony_ci 14911fccf17Sopenharmony_ciHDI::Ril::V1_1::RilRadioResponseInfo HRilBase::BuildIHRilRadioResponseInfo( 15011fccf17Sopenharmony_ci const HDI::Ril::V1_1::RilRadioResponseInfo &responseInfo) 15111fccf17Sopenharmony_ci{ 15211fccf17Sopenharmony_ci HDI::Ril::V1_1::RilRadioResponseInfo iResponseInfo = { 0 }; 15311fccf17Sopenharmony_ci iResponseInfo.slotId = GetSlotId(); 15411fccf17Sopenharmony_ci iResponseInfo.flag = responseInfo.flag; 15511fccf17Sopenharmony_ci iResponseInfo.serial = responseInfo.serial; 15611fccf17Sopenharmony_ci iResponseInfo.error = (HDI::Ril::V1_1::RilErrType)responseInfo.error; 15711fccf17Sopenharmony_ci iResponseInfo.type = (HDI::Ril::V1_1::RilResponseTypes)responseInfo.type; 15811fccf17Sopenharmony_ci return iResponseInfo; 15911fccf17Sopenharmony_ci} 16011fccf17Sopenharmony_ci 16111fccf17Sopenharmony_civoid HRilBase::SetRilCallback(const sptr<HDI::Ril::V1_3::IRilCallback> &callback) 16211fccf17Sopenharmony_ci{ 16311fccf17Sopenharmony_ci std::lock_guard<std::mutex> mutexLock(mutex_); 16411fccf17Sopenharmony_ci callback_ = callback; 16511fccf17Sopenharmony_ci} 16611fccf17Sopenharmony_ci 16711fccf17Sopenharmony_cistd::string HRilBase::StringToHex(const char *data, int byteLength) 16811fccf17Sopenharmony_ci{ 16911fccf17Sopenharmony_ci std::stringstream ss; 17011fccf17Sopenharmony_ci for (int i = 0; i < byteLength; ++i) { 17111fccf17Sopenharmony_ci unsigned char temp = static_cast<unsigned char>(data[i]) >> HEX_OFFSET; 17211fccf17Sopenharmony_ci ss << HEX_TABLE[temp] << HEX_TABLE[static_cast<unsigned char>(data[i]) & 0xf]; 17311fccf17Sopenharmony_ci } 17411fccf17Sopenharmony_ci return ss.str(); 17511fccf17Sopenharmony_ci} 17611fccf17Sopenharmony_ci} // namespace Telephony 17711fccf17Sopenharmony_ci} // namespace OHOS