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 #include <string>
18 #include <vector>
19 
20 namespace OHOS {
21 namespace NFC {
22 namespace KITS {
23 const static uint32_t HEX_BYTE_LEN = 2;
24 const static uint32_t HEX_VALUE = 16;
25 const static uint32_t HALF_BYTE_BITS = 4;
26 static const uint32_t NFC_MANAGER_SYS_ABILITY_ID = 1140;
27 static const std::string NFC_MANAGER_SYS_ABILITY_NAME = "nfc_service";
28 
29 enum ErrorCode : const int {
30     ERR_NONE = 0,
31 
32     ERR_NO_PERMISSION = 201,
33     ERR_NOT_SYSTEM_APP = 202,
34 
35     // error for nfc state operations
36     ERR_NFC_BASE = 3100100,
37     ERR_NFC_PARAMETERS,
38     ERR_NFC_STATE_UNBIND,
39     ERR_NFC_STATE_INVALID,
40 
41     // error for tag I/O operations
42     ERR_TAG_BASE = 3100200,
43     ERR_TAG_STATE_IO_FAILED,
44     ERR_TAG_PARAMETERS,
45     ERR_TAG_STATE_NFC_CLOSED,
46     ERR_TAG_STATE_LOST,
47     ERR_TAG_STATE_DISCONNECTED,
48     ERR_TAG_STATE_UNBIND,
49     ERR_TAG_APP_NOT_FOREGROUND,
50     ERR_TAG_APP_NOT_REGISTERED,
51 
52     // error for card emulation operations
53     ERR_CE_BASE = 3100300,
54     ERR_HCE_PARAMETERS,
55     ERR_HCE_STATE_NFC_CLOSED,
56     ERR_HCE_STATE_LOST,
57     ERR_HCE_STATE_DISCONNECTED,
58     ERR_HCE_STATE_IO_FAILED,
59     ERR_HCE_STATE_UNBIND,
60     ERR_HCE_NOT_GET_PAYMENT_SERVICES,
61 
62     // error for nfc database operations
63     ERR_NFC_DATABASE_RW = 3100400,
64 };
65 
66 enum NfcState { STATE_OFF = 1, STATE_TURNING_ON = 2, STATE_ON = 3, STATE_TURNING_OFF = 4 };
67 
68 enum NfcTask { TASK_TURN_ON = 101, TASK_TURN_OFF, TASK_INITIALIZE };
69 
70 enum FeatureType { HCE = 0, UICC = 1, ESE = 2 };
71 
72 /** NFC state changed for common event notification */
73 const std::string COMMON_EVENT_NFC_ACTION_STATE_CHANGED = "usual.event.nfc.action.ADAPTER_STATE_CHANGED";
74 const std::string NFC_EXTRA_STATE = "ohos.nfc.extra.ADAPTER_STATE";
75 
76 /** Payment type of card emulation */
77 static const std::string TYPE_PAYMENT = "payment";
78 
79 /** Other type of card emulation */
80 static const std::string TYPE_OHTER = "other";
81 
82 /** Payment type of card emulation metadata name */
83 const std::string KEY_PAYMENT_AID = "payment-aid";
84 
85 /** Other type of card emulation metadata name */
86 const std::string KEY_OHTER_AID = "other-aid";
87 
88 /** Action for tag application declared */
89 const std::string ACTION_TAG_FOUND = "ohos.nfc.tag.action.TAG_FOUND";
90 
91 /** Action for HCE application declared */
92 const std::string ACTION_HOST_APDU_SERVICE = "ohos.nfc.cardemulation.action.HOST_APDU_SERVICE";
93 
94 /** Action for off host*/
95 const std::string ACTION_OFF_HOST_APDU_SERVICE = "ohos.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE";
96 
97 /** Database key for nfc state. */
98 const std::string NFC_DATA_URI_ID =
99     "/com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=data_key_nfc_state";
100 const std::string NFC_DATA_ABILITY_PREFIX = "datashare://";
101 const std::string NFC_DATA_URI = NFC_DATA_ABILITY_PREFIX + NFC_DATA_URI_ID;
102 const std::string DATA_SHARE_KEY_STATE = "data_key_nfc_state";
103 const std::string NFC_DATA_COLUMN_KEYWORD = "KEYWORD";
104 const std::string NFC_DATA_COLUMN_VALUE = "VALUE";
105 
106 /** Database key for payment default app. */
107 const std::string NFC_PAYMENT_DEFAULT_APP =
108     "/com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=nfc_payment_default_app";
109 const std::string NFC_DATA_URI_PAYMENT_DEFAULT_APP = NFC_DATA_ABILITY_PREFIX + NFC_PAYMENT_DEFAULT_APP;
110 const std::string DATA_SHARE_KEY_NFC_PAYMENT_DEFAULT_APP = "nfc_payment_default_app";
111 
112 /** NFC field on/off common event */
113 const std::string COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED = "usual.event.nfc.action.RF_FIELD_ON_DETECTED";
114 const std::string COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED = "usual.event.nfc.action.RF_FIELD_OFF_DETECTED";
115 
116 const int DATA_SHARE_INVALID_VALUE = -1;
117 
118 /** type const of hce napi on */
119 const std::string EVENT_HCE_CMD = "hceCmd";
120 
121 /** type const of max apdu length */
122 const uint32_t MAX_APDU_DATA_BYTE = 1024;
123 const uint32_t MAX_APDU_DATA_HEX_STR = MAX_APDU_DATA_BYTE * 2;
124 const uint32_t MAX_AID_LIST_NUM_PER_APP = 100;
125 
126 #ifdef VENDOR_APPLICATIONS_ENABLED
127 const int VENDOR_APP_INIT_DONE = 1;
128 const int VENDOR_APP_CHANGE = 2;
129 #endif
130 
131 enum class DefaultPaymentType {
132     TYPE_HCE = 0,
133     TYPE_UICC = 1,
134     TYPE_ESE = 2,
135     TYPE_EMPTY = 3,
136     TYPE_UNINSTALLED = 4,
137     TYPE_UNKNOWN = 0xFF,
138 };
139 
140 enum class TagTechnology {
141     NFC_INVALID_TECH = 0,
142     NFC_A_TECH = 1,
143     NFC_B_TECH = 2,
144     NFC_ISODEP_TECH = 3,
145     NFC_F_TECH = 4, // Felica
146     NFC_V_TECH = 5, // ISO15693
147     NFC_NDEF_TECH = 6,
148     NFC_NDEF_FORMATABLE_TECH = 7,
149     NFC_MIFARE_CLASSIC_TECH = 8,
150     NFC_MIFARE_ULTRALIGHT_TECH = 9
151 };
152 
153 enum EmNfcForumType {
154     NFC_FORUM_TYPE_UNKNOWN = 0,
155     NFC_FORUM_TYPE_1 = 1,
156     NFC_FORUM_TYPE_2 = 2,
157     NFC_FORUM_TYPE_3 = 3,
158     NFC_FORUM_TYPE_4 = 4,
159     MIFARE_CLASSIC = 101,
160     ICODE_SLI = 102
161 };
162 
163 enum NotifyVendorEvent : int {
164     DEF_PAYMENT_APP_CHANGE_KEY = 1114,
165     TAG_DISPATCH_KEY = 1115,
166     READERMODE_APP_KEY = 1120,
167     FOREGROUND_APP_KEY = 1121,
168     NFC_SWITCH_KEY = 1122,
169     DEF_PAYMENT_APP_REMOVED_KEY = 1124,
170     DEF_PAYMENT_APP_ADDED_KEY = 1125,
171 };
172 
173 class NfcSdkCommon final {
174 public:
175     static const int SHIFT_SIZE = 8;
176     static const int SHIFT_TIME = 4;
177 
178 public:
179     static bool IsLittleEndian();
180     static std::string BytesVecToHexString(const unsigned char* src, uint32_t length);
181     static std::string UnsignedCharToHexString(const unsigned char src);
182     static void HexStringToBytes(const std::string &src, std::vector<unsigned char> &bytes);
183     static unsigned char GetByteFromHexStr(const std::string src, uint32_t index);
184     static uint32_t GetHexStrBytesLen(const std::string src);
185     static uint32_t StringToInt(std::string src, bool bLittleEndian = true);
186     static std::string IntToHexString(uint32_t num);
187     static void StringToAsciiBytes(const std::string &src, std::vector<unsigned char> &bytes);
188     static std::string StringToHexString(const std::string &src);
189     static std::string HexStringToAsciiString(const std::string &src);
190     static std::string HexArrayToStringWithoutChecking(const std::string &src);
191     static uint64_t GetCurrentTime();
192     static uint64_t GetRelativeTime();
193     static std::string CodeMiddlePart(const std::string &src);
194 };
195 }  // namespace KITS
196 }  // namespace NFC
197 }  // namespace OHOS
198 #endif  // NFC_SDK_COMMON_H
199