• 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 // the command id defined for IPC, from kits to system ability.
25 constexpr int COMMAND_ID = 100;
26 constexpr int COMMAND_GET_STATE = COMMAND_ID + 1;
27 constexpr int COMMAND_TURN_ON = COMMAND_ID + 2;
28 constexpr int COMMAND_TURN_OFF = COMMAND_ID + 3;
29 constexpr int COMMAND_ON_NOTIFY = COMMAND_ID + 4;
30 constexpr int COMMAND_REGISTER_CALLBACK = COMMAND_ID + 5;
31 constexpr int COMMAND_UNREGISTER_CALLBACK = COMMAND_ID + 6;
32 constexpr int COMMAND_IS_NFC_OPEN = COMMAND_ID + 7;
33 constexpr int COMMAND_GET_TAG_INTERFACE = COMMAND_ID + 8;
34 
35 constexpr int TAG_SESSION_START_ID = 200;
36 constexpr int COMMAND_CONNECT = TAG_SESSION_START_ID + 1;
37 constexpr int COMMAND_RECONNECT = TAG_SESSION_START_ID + 2;
38 constexpr int COMMAND_DISCONNECT = TAG_SESSION_START_ID + 3;
39 constexpr int COMMAND_GET_TECHLIST = TAG_SESSION_START_ID + 4;
40 constexpr int COMMAND_IS_PRESENT = TAG_SESSION_START_ID + 5;
41 constexpr int COMMAND_IS_NDEF = TAG_SESSION_START_ID + 6;
42 constexpr int COMMAND_SEND_RAW_FRAME = TAG_SESSION_START_ID + 7;
43 constexpr int COMMAND_NDEF_READ = TAG_SESSION_START_ID + 8;
44 constexpr int COMMAND_NDEF_WRITE = TAG_SESSION_START_ID + 9;
45 constexpr int COMMAND_NDEF_MAKE_READ_ONLY = TAG_SESSION_START_ID + 10;
46 constexpr int COMMAND_FORMAT_NDEF = TAG_SESSION_START_ID + 11;
47 constexpr int COMMAND_CAN_MAKE_READ_ONLY = TAG_SESSION_START_ID + 12;
48 constexpr int COMMAND_GET_MAX_TRANSCEIVE_LENGTH = TAG_SESSION_START_ID + 13;
49 constexpr int COMMAND_IS_SUPPORTED_APDUS_EXTENDED = TAG_SESSION_START_ID + 14;
50 constexpr int COMMAND_SET_TIMEOUT = TAG_SESSION_START_ID + 15;
51 constexpr int COMMAND_GET_TIMEOUT = TAG_SESSION_START_ID + 16;
52 
53 const static uint32_t HEX_BYTE_LEN = 2;
54 const static uint32_t HEX_VALUE = 16;
55 const static uint32_t HALF_BYTE_BITS = 4;
56 
57 enum ErrorCode : const int {
58     ERR_NONE = 0,
59 
60     ERR_NO_PERMISSION = 201,
61 
62     // error for nfc state operations
63     ERR_NFC_BASE = 3100100,
64     ERR_NFC_PARAMETERS,
65     ERR_NFC_STATE_UNBIND,
66 
67     // error for tag I/O operations
68     ERR_TAG_BASE = 3100200,
69     ERR_TAG_PARAMETERS,
70     ERR_TAG_STATE_NFC_CLOSED,
71     ERR_TAG_STATE_LOST,
72     ERR_TAG_STATE_DISCONNECTED,
73     ERR_TAG_STATE_IO_FAILED,
74     ERR_TAG_STATE_UNBIND,
75 
76     // error for card emulation operations
77     ERR_CE_BASE = 3100300,
78 };
79 
80 enum NfcState { STATE_OFF = 1, STATE_TURNING_ON = 2, STATE_ON = 3, STATE_TURNING_OFF = 4 };
81 
82 enum NfcTask { TASK_TURN_ON = 101, TASK_TURN_OFF, TASK_INITIALIZE, TASK_START_POLLING_LOOP };
83 
84 enum FeatureType { HCE = 0, UICC = 1, ESE = 2 };
85 
86 /** Payment type of card emulation */
87 static const std::string TYPE_PAYMENT = "payment";
88 
89 /** Other type of card emulation */
90 static const std::string TYPE_OHTER = "other";
91 
92 enum class TagTechnology {
93     NFC_INVALID_TECH = 0,
94     NFC_A_TECH = 1,
95     NFC_B_TECH = 2,
96     NFC_ISODEP_TECH = 3,
97     NFC_F_TECH = 4, // Felica
98     NFC_V_TECH = 5, // ISO15693
99     NFC_NDEF_TECH = 6,
100     NFC_NDEF_FORMATABLE_TECH = 7,
101     NFC_MIFARE_CLASSIC_TECH = 8,
102     NFC_MIFARE_ULTRALIGHT_TECH = 9
103 };
104 
105 class NfcSdkCommon final {
106 public:
107     static const int SHIFT_SIZE = 8;
108     static const int SHIFT_TIME = 4;
109 
110 public:
111     static bool IsLittleEndian();
112     static std::string BytesVecToHexString(const unsigned char* charArray, uint32_t length);
113     static std::string UnsignedCharToHexString(const unsigned char charArray);
114     static void HexStringToBytes(std::string &src, std::vector<unsigned char> &bytes);
115     static unsigned char GetByteFromHexStr(const std::string src, uint32_t index);
116     static uint32_t GetHexStrBytesLen(const std::string src);
117     static uint32_t StringToInt(std::string src, bool bLittleEndian = true);
118     static std::string IntToHexString(uint32_t num);
119     static void StringToAsciiBytes(const std::string &src, std::vector<unsigned char> &bytes);
120     static std::string StringToHexString(const std::string &src);
121 };
122 }  // namespace KITS
123 }  // namespace NFC
124 }  // namespace OHOS
125 #endif  // NFC_SDK_COMMON_H
126