1bc2ed2b3Sopenharmony_ci/*
2bc2ed2b3Sopenharmony_ci * Copyright (C) 2024 Huawei Device Co., Ltd.
3bc2ed2b3Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4bc2ed2b3Sopenharmony_ci * you may not use this file except in compliance with the License.
5bc2ed2b3Sopenharmony_ci * You may obtain a copy of the License at
6bc2ed2b3Sopenharmony_ci *
7bc2ed2b3Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8bc2ed2b3Sopenharmony_ci *
9bc2ed2b3Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10bc2ed2b3Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11bc2ed2b3Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12bc2ed2b3Sopenharmony_ci * See the License for the specific language governing permissions and
13bc2ed2b3Sopenharmony_ci * limitations under the License.
14bc2ed2b3Sopenharmony_ci */
15bc2ed2b3Sopenharmony_ci#include "nfc_param_util.h"
16bc2ed2b3Sopenharmony_ci
17bc2ed2b3Sopenharmony_ci#include "loghelper.h"
18bc2ed2b3Sopenharmony_ci#include "nfc_sdk_common.h"
19bc2ed2b3Sopenharmony_ci#include "parameter.h"
20bc2ed2b3Sopenharmony_ci
21bc2ed2b3Sopenharmony_cinamespace OHOS {
22bc2ed2b3Sopenharmony_cinamespace NFC {
23bc2ed2b3Sopenharmony_civoid NfcParamUtil::UpdateNfcStateToParam(int newState)
24bc2ed2b3Sopenharmony_ci{
25bc2ed2b3Sopenharmony_ci    if (newState != KITS::STATE_ON && newState != KITS::STATE_OFF) {
26bc2ed2b3Sopenharmony_ci        ErrorLog("illegal state [%{public}d].", newState);
27bc2ed2b3Sopenharmony_ci        return;
28bc2ed2b3Sopenharmony_ci    }
29bc2ed2b3Sopenharmony_ci    InfoLog("UpdateNfcStateToParam, new nfc state[%{public}d]", newState);
30bc2ed2b3Sopenharmony_ci    int errCode = SetParameter(NFC_SWITCH_STATE_PARAM_NAME, std::to_string(newState).c_str());
31bc2ed2b3Sopenharmony_ci    if (errCode < 0) {
32bc2ed2b3Sopenharmony_ci        ErrorLog("fail to set nfc switch param, errCode[%{public}d]", errCode);
33bc2ed2b3Sopenharmony_ci    }
34bc2ed2b3Sopenharmony_ci}
35bc2ed2b3Sopenharmony_ci
36bc2ed2b3Sopenharmony_ciint NfcParamUtil::GetNfcStateFromParam()
37bc2ed2b3Sopenharmony_ci{
38bc2ed2b3Sopenharmony_ci    char nfcState[PROPERTY_VALUE_MAX] = {0};
39bc2ed2b3Sopenharmony_ci    int errCode = GetParameter(NFC_SWITCH_STATE_PARAM_NAME, "", nfcState, PROPERTY_VALUE_MAX);
40bc2ed2b3Sopenharmony_ci    if (errCode != NFC_SWITCH_PARAM_LEN) {
41bc2ed2b3Sopenharmony_ci        ErrorLog("failed to get nfc switch state, errCode[%{public}d]", errCode);
42bc2ed2b3Sopenharmony_ci        return 0; // return invalid nfc state
43bc2ed2b3Sopenharmony_ci    }
44bc2ed2b3Sopenharmony_ci    InfoLog("GetNfcStateFromParam, nfc state[%{publib}d]", nfcState);
45bc2ed2b3Sopenharmony_ci    errno = 0;
46bc2ed2b3Sopenharmony_ci    char *endptr = nullptr;
47bc2ed2b3Sopenharmony_ci    long int num = std::strtol(nfcState, &endptr, DECIMAL_NOTATION);
48bc2ed2b3Sopenharmony_ci    if (errno == ERANGE) {
49bc2ed2b3Sopenharmony_ci        ErrorLog("strtol errno = ERANGE");
50bc2ed2b3Sopenharmony_ci        return 0; // return invalid nfc state
51bc2ed2b3Sopenharmony_ci    }
52bc2ed2b3Sopenharmony_ci    return static_cast<int>(num);
53bc2ed2b3Sopenharmony_ci}
54bc2ed2b3Sopenharmony_ci}  // namespace NFC
55bc2ed2b3Sopenharmony_ci}  // namespace OHOS