1/* 2 * Copyright (C) 2024 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#include "nfc_param_util.h" 16 17#include "loghelper.h" 18#include "nfc_sdk_common.h" 19#include "parameter.h" 20 21namespace OHOS { 22namespace NFC { 23void NfcParamUtil::UpdateNfcStateToParam(int newState) 24{ 25 if (newState != KITS::STATE_ON && newState != KITS::STATE_OFF) { 26 ErrorLog("illegal state [%{public}d].", newState); 27 return; 28 } 29 InfoLog("UpdateNfcStateToParam, new nfc state[%{public}d]", newState); 30 int errCode = SetParameter(NFC_SWITCH_STATE_PARAM_NAME, std::to_string(newState).c_str()); 31 if (errCode < 0) { 32 ErrorLog("fail to set nfc switch param, errCode[%{public}d]", errCode); 33 } 34} 35 36int NfcParamUtil::GetNfcStateFromParam() 37{ 38 char nfcState[PROPERTY_VALUE_MAX] = {0}; 39 int errCode = GetParameter(NFC_SWITCH_STATE_PARAM_NAME, "", nfcState, PROPERTY_VALUE_MAX); 40 if (errCode != NFC_SWITCH_PARAM_LEN) { 41 ErrorLog("failed to get nfc switch state, errCode[%{public}d]", errCode); 42 return 0; // return invalid nfc state 43 } 44 InfoLog("GetNfcStateFromParam, nfc state[%{publib}d]", nfcState); 45 errno = 0; 46 char *endptr = nullptr; 47 long int num = std::strtol(nfcState, &endptr, DECIMAL_NOTATION); 48 if (errno == ERANGE) { 49 ErrorLog("strtol errno = ERANGE"); 50 return 0; // return invalid nfc state 51 } 52 return static_cast<int>(num); 53} 54} // namespace NFC 55} // namespace OHOS