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