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
16bc2ed2b3Sopenharmony_ci#ifndef ISODEP_CARD_HANDLER_H
17bc2ed2b3Sopenharmony_ci#define ISODEP_CARD_HANDLER_H
18bc2ed2b3Sopenharmony_ci
19bc2ed2b3Sopenharmony_ci#include <string>
20bc2ed2b3Sopenharmony_ci#include <vector>
21bc2ed2b3Sopenharmony_ci
22bc2ed2b3Sopenharmony_ci#include "inci_tag_interface.h"
23bc2ed2b3Sopenharmony_ci
24bc2ed2b3Sopenharmony_cinamespace OHOS {
25bc2ed2b3Sopenharmony_cinamespace NFC {
26bc2ed2b3Sopenharmony_cinamespace TAG {
27bc2ed2b3Sopenharmony_cistruct TransportCardInfo {
28bc2ed2b3Sopenharmony_ci    std::string name;
29bc2ed2b3Sopenharmony_ci    std::string aid;
30bc2ed2b3Sopenharmony_ci    std::vector<std::string> checkApdus;
31bc2ed2b3Sopenharmony_ci    std::vector<std::string> balanceApdus;
32bc2ed2b3Sopenharmony_ci    std::string rspContain;
33bc2ed2b3Sopenharmony_ci};
34bc2ed2b3Sopenharmony_ci
35bc2ed2b3Sopenharmony_cistatic const uint8_t INVALID_CARD_INDEX = 0xFF;
36bc2ed2b3Sopenharmony_cistatic const int INVALID_BALANCE = -1;
37bc2ed2b3Sopenharmony_cistatic const int APDU_RSP_OK_STR_LEN = 4;
38bc2ed2b3Sopenharmony_cistatic const int APDU_RSP_BALANCE_STR_LEN = 8;
39bc2ed2b3Sopenharmony_cistatic const int APDU_RSP_BALANCE_BYTES_LEN = 4;
40bc2ed2b3Sopenharmony_cistatic const int MAX_APDU_ARRAY_SIZE = 2;
41bc2ed2b3Sopenharmony_cistatic const int MAX_CARD_INFO_VEC_LEN = 7;
42bc2ed2b3Sopenharmony_ci
43bc2ed2b3Sopenharmony_cistatic const std::string KEY_CARD_INFO_LEN = "cardInfoLength";
44bc2ed2b3Sopenharmony_cistatic const std::string KEY_CARD_INFO = "cardInfo";
45bc2ed2b3Sopenharmony_cistatic const std::string KEY_APDU_NAME = "name";
46bc2ed2b3Sopenharmony_cistatic const std::string KEY_APDU_AID = "aid";
47bc2ed2b3Sopenharmony_cistatic const std::string KEY_APDU_CHECK_APDUS = "checkApdus";
48bc2ed2b3Sopenharmony_cistatic const std::string KEY_APDU_BALANCE_APDUS = "balanceApdus";
49bc2ed2b3Sopenharmony_cistatic const std::string KEY_APDU_RSP_CONTAINS = "rspContains";
50bc2ed2b3Sopenharmony_ci
51bc2ed2b3Sopenharmony_ciclass IsodepCardHandler {
52bc2ed2b3Sopenharmony_cipublic:
53bc2ed2b3Sopenharmony_ci    explicit IsodepCardHandler(std::weak_ptr<NCI::INciTagInterface> nciTagProxy);
54bc2ed2b3Sopenharmony_ci    ~IsodepCardHandler();
55bc2ed2b3Sopenharmony_ci    IsodepCardHandler(const IsodepCardHandler&) = delete;
56bc2ed2b3Sopenharmony_ci    IsodepCardHandler& operator=(const IsodepCardHandler&) = delete;
57bc2ed2b3Sopenharmony_ci
58bc2ed2b3Sopenharmony_ci    void InitTransportCardInfo(void);
59bc2ed2b3Sopenharmony_ci    bool IsSupportedTransportCard(uint32_t rfDiscId, uint8_t &cardIndex);
60bc2ed2b3Sopenharmony_ci    void GetBalance(uint32_t rfDiscId, uint8_t cardIndex, int &balance);
61bc2ed2b3Sopenharmony_ci    void GetCardName(uint8_t cardIndex, std::string &cardName);
62bc2ed2b3Sopenharmony_ci
63bc2ed2b3Sopenharmony_ciprivate:
64bc2ed2b3Sopenharmony_ci    bool MatchCity(uint32_t rfDiscId, uint8_t cardIndex);
65bc2ed2b3Sopenharmony_ci    bool CheckApduResponse(const std::string &response, uint8_t cardIndex);
66bc2ed2b3Sopenharmony_ci    bool CheckApduResponse(const std::string &response);
67bc2ed2b3Sopenharmony_ci    void GetBalanceValue(const std::string &balanceStr, int &balanceValue);
68bc2ed2b3Sopenharmony_ci    bool DoJsonRead();
69bc2ed2b3Sopenharmony_ci
70bc2ed2b3Sopenharmony_ci    std::weak_ptr<NCI::INciTagInterface> nciTagProxy_ {};
71bc2ed2b3Sopenharmony_ci
72bc2ed2b3Sopenharmony_ci    // transport card info
73bc2ed2b3Sopenharmony_ci    std::vector<TransportCardInfo> cardInfoVec_;
74bc2ed2b3Sopenharmony_ci    bool isInitialized_ = false;
75bc2ed2b3Sopenharmony_ci
76bc2ed2b3Sopenharmony_ci    static const int BYTE_ZERO = 0;
77bc2ed2b3Sopenharmony_ci    static const int BYTE_ONE = 1;
78bc2ed2b3Sopenharmony_ci    static const int BYTE_TWO = 2;
79bc2ed2b3Sopenharmony_ci    static const int BYTE_THREE = 3;
80bc2ed2b3Sopenharmony_ci    static const int THREE_BYTES_SHIFT = 24;
81bc2ed2b3Sopenharmony_ci    static const int TWO_BYTES_SHIFT = 16;
82bc2ed2b3Sopenharmony_ci    static const int ONE_BYTES_SHIFT = 8;
83bc2ed2b3Sopenharmony_ci
84bc2ed2b3Sopenharmony_ci    const std::string NFC_CARD_APDU_JSON_FILEPATH = "system/etc/nfc/nfc_card_apdu.json";
85bc2ed2b3Sopenharmony_ci    const std::string APDU_RSP_OK = "9000";
86bc2ed2b3Sopenharmony_ci    const std::string APDU_RSP_PREFIX = "9F0C";
87bc2ed2b3Sopenharmony_ci};
88bc2ed2b3Sopenharmony_ci}  // namespace TAG
89bc2ed2b3Sopenharmony_ci}  // namespace NFC
90bc2ed2b3Sopenharmony_ci}  // namespace OHOS
91bc2ed2b3Sopenharmony_ci#endif  // ISODEP_CARD_HANDLER_H
92