• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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 #ifndef NFC_SDK_COMMON_H
16 #define NFC_SDK_COMMON_H
17 
18 #include <string>
19 #include <vector>
20 
21 namespace OHOS {
22 namespace NFC {
23 namespace KITS {
24 const static uint32_t HEX_BYTE_LEN = 2;
25 const static uint32_t HEX_VALUE = 16;
26 const static uint32_t HALF_BYTE_BITS = 4;
27 
28 enum ErrorCode : const int {
29     ERR_NONE = 0,
30 
31     ERR_NO_PERMISSION = 201,
32 
33     // error for nfc state operations
34     ERR_NFC_BASE = 3100100,
35     ERR_NFC_PARAMETERS,
36     ERR_NFC_STATE_UNBIND,
37 
38     // error for tag I/O operations
39     ERR_TAG_BASE = 3100200,
40     ERR_TAG_PARAMETERS,
41     ERR_TAG_STATE_NFC_CLOSED,
42     ERR_TAG_STATE_LOST,
43     ERR_TAG_STATE_DISCONNECTED,
44     ERR_TAG_STATE_IO_FAILED,
45     ERR_TAG_STATE_UNBIND,
46 
47     // error for card emulation operations
48     ERR_CE_BASE = 3100300,
49 };
50 
51 enum NfcState { STATE_OFF = 1, STATE_TURNING_ON = 2, STATE_ON = 3, STATE_TURNING_OFF = 4 };
52 
53 enum NfcTask { TASK_TURN_ON = 101, TASK_TURN_OFF, TASK_INITIALIZE };
54 
55 enum FeatureType { HCE = 0, UICC = 1, ESE = 2 };
56 
57 /** Payment type of card emulation */
58 static const std::string TYPE_PAYMENT = "payment";
59 
60 /** Other type of card emulation */
61 static const std::string TYPE_OHTER = "other";
62 
63 /** Action for tag application declared */
64 const std::string ACTION_TAG_FOUND = "ohos.nfc.tag.action.TAG_FOUND";
65 
66 /** Action for HCE application declared */
67 const std::string ACTION_HOST_APDU_SERVICE = "ohos.nfc.cardemulation.action.HOST_APDU_SERVICE";
68 
69 enum class TagTechnology {
70     NFC_INVALID_TECH = 0,
71     NFC_A_TECH = 1,
72     NFC_B_TECH = 2,
73     NFC_ISODEP_TECH = 3,
74     NFC_F_TECH = 4, // Felica
75     NFC_V_TECH = 5, // ISO15693
76     NFC_NDEF_TECH = 6,
77     NFC_NDEF_FORMATABLE_TECH = 7,
78     NFC_MIFARE_CLASSIC_TECH = 8,
79     NFC_MIFARE_ULTRALIGHT_TECH = 9
80 };
81 
82 class NfcSdkCommon final {
83 public:
84     static const int SHIFT_SIZE = 8;
85     static const int SHIFT_TIME = 4;
86 
87 public:
88     static bool IsLittleEndian();
89     static std::string BytesVecToHexString(const unsigned char* charArray, uint32_t length);
90     static std::string UnsignedCharToHexString(const unsigned char charArray);
91     static void HexStringToBytes(std::string &src, std::vector<unsigned char> &bytes);
92     static unsigned char GetByteFromHexStr(const std::string src, uint32_t index);
93     static uint32_t GetHexStrBytesLen(const std::string src);
94     static uint32_t StringToInt(std::string src, bool bLittleEndian = true);
95     static std::string IntToHexString(uint32_t num);
96     static void StringToAsciiBytes(const std::string &src, std::vector<unsigned char> &bytes);
97     static std::string StringToHexString(const std::string &src);
98     static uint64_t GetCurrentTime();
99 };
100 }  // namespace KITS
101 }  // namespace NFC
102 }  // namespace OHOS
103 #endif  // NFC_SDK_COMMON_H
104