新增telephony相关的ndk接口,获取默认主卡ID和网络制式

Signed-off-by: SJH <282229496@qq.com>
Change-Id: Ieb8502da8c4257349640ec7629f52891cd5d3d3b
This commit is contained in:
s00659936 2024-08-29 19:11:56 +08:00
parent 01431675aa
commit 582e204bf5
5 changed files with 460 additions and 0 deletions

View File

@ -80,6 +80,7 @@
"build": {
"group_type": {
"base_group": [
"//base/telephony/core_service/interfaces/kits/c/telephony_radio:telephony_radio"
],
"fwk_group": [
"//base/telephony/core_service/interfaces/innerkits:tel_core_service_api",

View File

@ -0,0 +1,60 @@
# Copyright (c) 2024 Huawei Device Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/ohos.gni")
CORE_SERVICE_DIR = "../../../../"
ohos_shared_library("telephony_radio") {
sanitize = {
cfi = true
cfi_cross_dso = true
debug = false
}
branch_protector_ret = "pac_ret"
output_extension = "so"
include_dirs =
[ "$CORE_SERVICE_DIR/interfaces/kits/c/telephony_radio/include" ]
sources = [ "$CORE_SERVICE_DIR/interfaces/kits/c/telephony_radio/src/telephony_radio.cpp" ]
cflags_cc = [
"-Wall",
"-Werror",
"-fstack-protector-strong",
"-D_FORTIFY_SOURCE=2",
"-O2",
]
deps = [
"$CORE_SERVICE_DIR/interfaces/innerkits:tel_core_service_api",
"$CORE_SERVICE_DIR/utils:libtel_common",
]
external_deps = [
"c_utils:utils",
"hilog:libhilog",
"init:libbegetutil",
]
defines = [
"TELEPHONY_LOG_TAG = \"CoreServiceRadioNdkApi\"",
"LOG_DOMAIN = 0xD001F04",
]
part_name = "core_service"
subsystem_name = "telephony"
relative_install_dir = "ndk"
}

View File

@ -0,0 +1,74 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NATIVE_TELEPHONY_RADIO_API_H
#define NATIVE_TELEPHONY_RADIO_API_H
/**
* @file telephony_radio.h
*
* @brief Provides C interface for the telephony radio.
*
* @kit TelephonyKit
* @syscap SystemCapability.Telephony.CoreService
* @library libtelephony_radio.so
* @since 13
*/
#include "telephony_radio_type.h"
#include "stdint.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief Obtains the radio network state.
*
* @param state Pointer to the network state.
* @return the result defines in {@link Telephony_RadioResult}.
* {@link TEL_RADIO_SUCCESS} Success.
* {@link TEL_RADIO_PERMISSION_DENIED} Permission denied.
* {@link TEL_RADIO_ERR_MARSHALLING_FAILED} Low probability Marshalling failed, try again later.
* {@link TEL_RADIO_ERR_SERVICE_CONNECTION_FAILED} Unable to connect to telephony service, try again later.
* {@link TEL_RADIO_ERR_OPERATION_FAILED} Operation failed in telephony service, try again later.
* {@link TEL_RADIO_ERR_INVALID_PARAM} Invalid parameter.
* @permission ohos.permission.GET_NETWORK_INFO
* @syscap SystemCapability.Telephony.CoreService
* @since 13
*/
Telephony_RadioResult OH_Telephony_GetNetworkState(Telephony_NetworkState *state);
/**
* @brief Obtains the radio network state for given slot id.
*
* @param slotId the number of slot, 0 for card slot 1, 1 for card slot 2.
* @param state Pointer to the network state.
* @return the result defines in {@link Telephony_RadioResult}.
* {@link TEL_RADIO_SUCCESS} Success.
* {@link TEL_RADIO_PERMISSION_DENIED} Permission denied.
* {@link TEL_RADIO_ERR_MARSHALLING_FAILED} Low probability Marshalling failed, try again later.
* {@link TEL_RADIO_ERR_SERVICE_CONNECTION_FAILED} Unable to connect to telephony service, try again later.
* {@link TEL_RADIO_ERR_OPERATION_FAILED} Operation failed in telephony service, try again later.
* {@link TEL_RADIO_ERR_INVALID_PARAM} Invalid parameter.
* @permission ohos.permission.GET_NETWORK_INFO
* @syscap SystemCapability.Telephony.CoreService
* @since 13
*/
Telephony_RadioResult OH_Telephony_GetNetworkStateForSlot(int32_t slotId, Telephony_NetworkState *state);
#ifdef __cplusplus
}
#endif
#endif // NATIVE_TELEPHONY_RADIO_API_H

View File

@ -0,0 +1,157 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef NATIVE_TELEPHONY_RADIO_TYPE_H
#define NATIVE_TELEPHONY_RADIO_TYPE_H
/**
* @file telephony_radio_type.h
*
* @brief Provides the data structures for the C APIs of the the telephony radio.
*
* @kit TelephonyKit
* @syscap SystemCapability.Telephony.CoreService
* @library libtelephony_radio.so
* @since 13
*/
#ifdef __cplusplus
extern "C" {
#endif
#define TELEPHONY_MAX_OPERATOR_LEN 64
#define TELEPHONY_MAX_PLMN_NUMERIC_LEN 6
/**
* @brief Result code.
*
* @since 13
*/
typedef enum Telephony_RadioResult {
/* @error success */
TEL_RADIO_SUCCESS = 0,
/* @error permission denied */
TEL_RADIO_PERMISSION_DENIED = 201,
/* @error invalid parameter */
TEL_RADIO_ERR_INVALID_PARAM = 401,
/* @error marshalling failed, this is a low probability error, try again later when get this error */
TEL_RADIO_ERR_MARSHALLING_FAILED = 8300001,
/* @error unable to connect to telephony service, try again later when get this error */
TEL_RADIO_ERR_SERVICE_CONNECTION_FAILED = 8300002,
/* @error operation failed in telephony service, try again later when get this error */
TEL_RADIO_ERR_OPERATION_FAILED = 8300003,
} Telephony_RadioResult;
/**
* @brief network registration status.
*
* @since 13
*/
typedef enum Telephony_RegState {
/* can not use any services */
TEL_REG_STATE_NO_SERVICE = 0,
/* can use services properly */
TEL_REG_STATE_IN_SERVICE = 1,
/* can use emergency call only */
TEL_REG_STATE_EMERGENCY_CALL_ONLY = 2,
/* radio power off */
TEL_REG_STATE_POWER_OFF = 3,
} Telephony_RegState;
/**
* @brief radio access technologies.
*
* @since 13
*/
typedef enum Telephony_RadioTechnology {
/* Unknown radio technology */
TEL_RADIO_TECHNOLOGY_UNKNOWN = 0,
/* Global System for Mobile Communication (GSM) */
TEL_RADIO_TECHNOLOGY_GSM = 1,
/* Single-Carrier Radio Transmission Technology (1XRTT) */
TEL_RADIO_TECHNOLOGY_1XRTT = 2,
/* Wideband Code Division Multiple Access (WCDMA) */
TEL_RADIO_TECHNOLOGY_WCDMA = 3,
/* High Speed Packet Access (HSPA) */
TEL_RADIO_TECHNOLOGY_HSPA = 4,
/* Evolved High Speed Packet Access (HSPA+) */
TEL_RADIO_TECHNOLOGY_HSPAP = 5,
/* Time Division-Synchronous Code Division Multiple Access(TD-SCDMA) */
TEL_RADIO_TECHNOLOGY_TD_SCDMA = 6,
/* Evolution-Data Optimized (EVDO) */
TEL_RADIO_TECHNOLOGY_EVDO = 7,
/* Evolved High Rate Package Data (EHRPD) */
TEL_RADIO_TECHNOLOGY_EHRPD = 8,
/* Long Term Evolution (LTE) */
TEL_RADIO_TECHNOLOGY_LTE = 9,
/* Long Term Evolution_Carrier Aggregation (LTE_CA) */
TEL_RADIO_TECHNOLOGY_LTE_CA = 10,
/* Industrial Wireless LAN (IWLAN) */
TEL_RADIO_TECHNOLOGY_IWLAN = 11,
/* New Radio (NR) */
TEL_RADIO_TECHNOLOGY_NR = 12,
} Telephony_RadioTechnology;
/**
* @brief NSA network state.
*
* @since 13
*/
typedef enum Telephony_NsaState {
/* The device is in idle or connected state in an LTE cell that does not support NSA */
TEL_NSA_STATE_NOT_SUPPORTED = 1,
/* The device is in the idle state in an LTE cell that supports NSA but not NR coverage detection */
TEL_NSA_STATE_NO_DETECTED = 2,
/* The device is connected to the LTE network in an LTE cell that supports NSA and NR coverage detection */
TEL_NSA_STATE_CONNECTED_DETECTED = 3,
/* The device is in the idle state in an LTE cell that supports NSA and NR coverage detection */
TEL_NSA_STATE_IDLE_DETECTED = 4,
/* The device is connected to the LTE/NR network in an LTE cell that supports NSA */
TEL_NSA_STATE_DUAL_CONNECTED = 5,
/* The device is idle or connected to the NG-RAN cell when being attached to the 5G Core */
TEL_NSA_STATE_SA_ATTACHED = 6,
} Telephony_NsaState;
/**
* @brief Network status.
*
* @since 13
*/
typedef struct Telephony_NetworkState {
/* Long carrier name of the registered network */
char longOperatorName_[TELEPHONY_MAX_OPERATOR_LEN];
/* Short carrier name of the registered network */
char shortOperatorName_[TELEPHONY_MAX_OPERATOR_LEN];
/* PLMN code of the registered network */
char plmnNumeric_[TELEPHONY_MAX_PLMN_NUMERIC_LEN];
/* Whether in roaming */
bool isRoaming_;
/* Network registration status */
Telephony_RegState regState_;
/* Radio technology. */
Telephony_RadioTechnology cfgTech_;
/* NSA state */
Telephony_NsaState nsaState_;
/* Whether Carrier Aggregation(CA) is active */
bool isCaActive_;
/* Whether in emergency call only */
bool isEmergency_;
} Telephony_NetworkState;
#ifdef __cplusplus
}
#endif
#endif // NATIVE_TELEPHONY_RADIO_TYPE_H

View File

@ -0,0 +1,168 @@
/*
* Copyright (c) 2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "telephony_radio.h"
#include "core_service_client.h"
#include "network_search_types.h"
#include "network_state.h"
#include "telephony_errors.h"
#include "telephony_log_wrapper.h"
#include "telephony_types.h"
#include "refbase.h"
#include "securec.h"
#include <string>
using namespace OHOS::Telephony;
static Telephony_RadioTechnology Conv2TelRadioTechnology(OHOS::Telephony::RadioTech radioTechType)
{
switch (radioTechType) {
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_GSM:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_GSM;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_LTE:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_LTE;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_WCDMA:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_WCDMA;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_1XRTT:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_1XRTT;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_HSPA:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_HSPA;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_HSPAP:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_HSPAP;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_TD_SCDMA:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_TD_SCDMA;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_EVDO:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_EVDO;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_EHRPD:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_EHRPD;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_LTE_CA:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_LTE_CA;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_IWLAN:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_IWLAN;
case OHOS::Telephony::RadioTech::RADIO_TECHNOLOGY_NR:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_NR;
default:
return Telephony_RadioTechnology::TEL_RADIO_TECHNOLOGY_UNKNOWN;
}
}
static Telephony_RegState Conv2TelRegState(OHOS::Telephony::RegServiceState regStateType)
{
switch (regStateType) {
case OHOS::Telephony::RegServiceState::REG_STATE_IN_SERVICE: {
return Telephony_RegState::TEL_REG_STATE_IN_SERVICE;
}
case OHOS::Telephony::RegServiceState::REG_STATE_EMERGENCY_ONLY: {
return Telephony_RegState::TEL_REG_STATE_EMERGENCY_CALL_ONLY;
}
case OHOS::Telephony::RegServiceState::REG_STATE_POWER_OFF: {
return Telephony_RegState::TEL_REG_STATE_POWER_OFF;
}
default:
return Telephony_RegState::TEL_REG_STATE_NO_SERVICE;
}
}
static Telephony_RadioResult TelephonyErrorCodeTrans(int32_t err)
{
switch (err) {
case OHOS::Telephony::TELEPHONY_ERR_SUCCESS:
return Telephony_RadioResult::TEL_RADIO_SUCCESS;
case OHOS::Telephony::TELEPHONY_ERR_PERMISSION_ERR:
return Telephony_RadioResult::TEL_RADIO_PERMISSION_DENIED;
case OHOS::Telephony::TELEPHONY_ERR_SLOTID_INVALID:
return Telephony_RadioResult::TEL_RADIO_ERR_INVALID_PARAM;
case OHOS::Telephony::TELEPHONY_ERR_WRITE_DESCRIPTOR_TOKEN_FAIL:
case OHOS::Telephony::TELEPHONY_ERR_WRITE_DATA_FAIL:
return Telephony_RadioResult::TEL_RADIO_ERR_MARSHALLING_FAILED;
case OHOS::Telephony::TELEPHONY_ERR_IPC_CONNECT_STUB_FAIL:
return Telephony_RadioResult::TEL_RADIO_ERR_SERVICE_CONNECTION_FAILED;
case OHOS::Telephony::TELEPHONY_ERR_LOCAL_PTR_NULL:
return Telephony_RadioResult::TEL_RADIO_ERR_OPERATION_FAILED;
default:
return Telephony_RadioResult::TEL_RADIO_ERR_OPERATION_FAILED;
}
}
static bool IsValidSlotId(int32_t slotId)
{
return ((slotId >= DEFAULT_SIM_SLOT_ID) && (slotId < SIM_SLOT_COUNT + 1));
}
Telephony_RadioResult Conv2TelNetworkState(const OHOS::sptr<NetworkState> &networkState, Telephony_NetworkState *state)
{
std::string name = networkState->GetLongOperatorName();
if (strcpy_s(state->longOperatorName_, TELEPHONY_MAX_OPERATOR_LEN, name.c_str()) != 0) {
TELEPHONY_LOGE("state->longOperatorName_ string copy failed");
return Telephony_RadioResult::TEL_RADIO_ERR_OPERATION_FAILED;
}
name = networkState->GetShortOperatorName();
if (strcpy_s(state->shortOperatorName_, TELEPHONY_MAX_OPERATOR_LEN, name.c_str()) != 0) {
TELEPHONY_LOGE("state->shortOperatorName_ string copy failed");
return Telephony_RadioResult::TEL_RADIO_ERR_OPERATION_FAILED;
}
name = networkState->GetPlmnNumeric();
if (strcpy_s(state->plmnNumeric_, TELEPHONY_MAX_PLMN_NUMERIC_LEN, name.c_str()) != 0) {
TELEPHONY_LOGE("state->plmnNumeric_ string copy failed");
return Telephony_RadioResult::TEL_RADIO_ERR_OPERATION_FAILED;
}
state->isRoaming_ = networkState->IsRoaming();
state->regState_ = Conv2TelRegState(networkState->GetRegStatus());
state->cfgTech_ = Conv2TelRadioTechnology(networkState->GetCfgTech());
state->nsaState_ = Telephony_NsaState::TEL_NSA_STATE_NOT_SUPPORTED;
state->isCaActive_ = false;
state->isEmergency_ = networkState->IsEmergency();
return Telephony_RadioResult::TEL_RADIO_SUCCESS;
}
Telephony_RadioResult OH_Telephony_GetNetworkStateForSlot(int32_t slotId, Telephony_NetworkState *state)
{
if (state == nullptr) {
TELEPHONY_LOGE("OH_Telephony_GetNetworkStateForSlot received invalid parameters");
return Telephony_RadioResult::TEL_RADIO_ERR_INVALID_PARAM;
}
if (!IsValidSlotId(slotId)) {
TELEPHONY_LOGE("GetNetworkState slotId is invalid");
return Telephony_RadioResult::TEL_RADIO_ERR_INVALID_PARAM;
}
OHOS::sptr<NetworkState> networkState = nullptr;
int32_t result = OHOS::Telephony::CoreServiceClient::GetInstance().GetNetworkState(slotId, networkState);
if (result != TELEPHONY_SUCCESS) {
TELEPHONY_LOGE("GetNetworkState errorCode = %{public}d", result);
return TelephonyErrorCodeTrans(result);
}
if (networkState == nullptr) {
TELEPHONY_LOGE("GetNetworkState networkState is nullptr");
return Telephony_RadioResult::TEL_RADIO_ERR_OPERATION_FAILED;
}
return Conv2TelNetworkState(networkState, state);
}
Telephony_RadioResult OH_Telephony_GetNetworkState(Telephony_NetworkState *state)
{
int32_t slotId = 0;
int32_t result = OHOS::Telephony::CoreServiceClient::GetInstance().GetPrimarySlotId(slotId);
if (result != TELEPHONY_SUCCESS) {
TELEPHONY_LOGE("GetPrimarySlotId errorCode = %{public}d", result);
return TelephonyErrorCodeTrans(result);
}
TELEPHONY_LOGD("GetDefaultSlotId = %{public}d", slotId);
return OH_Telephony_GetNetworkStateForSlot(slotId, state);
}