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
20namespace OHOS {
21namespace NFC {
22namespace KITS {
23const static uint32_t HEX_BYTE_LEN = 2;
24const static uint32_t HEX_VALUE = 16;
25const static uint32_t HALF_BYTE_BITS = 4;
26static const uint32_t NFC_MANAGER_SYS_ABILITY_ID = 1140;
27static const std::string NFC_MANAGER_SYS_ABILITY_NAME = "nfc_service";
28
29enum 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
66enum NfcState { STATE_OFF = 1, STATE_TURNING_ON = 2, STATE_ON = 3, STATE_TURNING_OFF = 4 };
67
68enum NfcTask { TASK_TURN_ON = 101, TASK_TURN_OFF, TASK_INITIALIZE };
69
70enum FeatureType { HCE = 0, UICC = 1, ESE = 2 };
71
72/** NFC state changed for common event notification */
73const std::string COMMON_EVENT_NFC_ACTION_STATE_CHANGED = "usual.event.nfc.action.ADAPTER_STATE_CHANGED";
74const std::string NFC_EXTRA_STATE = "ohos.nfc.extra.ADAPTER_STATE";
75
76/** Payment type of card emulation */
77static const std::string TYPE_PAYMENT = "payment";
78
79/** Other type of card emulation */
80static const std::string TYPE_OHTER = "other";
81
82/** Payment type of card emulation metadata name */
83const std::string KEY_PAYMENT_AID = "payment-aid";
84
85/** Other type of card emulation metadata name */
86const std::string KEY_OHTER_AID = "other-aid";
87
88/** Action for tag application declared */
89const std::string ACTION_TAG_FOUND = "ohos.nfc.tag.action.TAG_FOUND";
90
91/** Action for HCE application declared */
92const std::string ACTION_HOST_APDU_SERVICE = "ohos.nfc.cardemulation.action.HOST_APDU_SERVICE";
93
94/** Action for off host*/
95const std::string ACTION_OFF_HOST_APDU_SERVICE = "ohos.nfc.cardemulation.action.OFF_HOST_APDU_SERVICE";
96
97/** Database key for nfc state. */
98const std::string NFC_DATA_URI_ID =
99    "/com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=data_key_nfc_state";
100const std::string NFC_DATA_ABILITY_PREFIX = "datashare://";
101const std::string NFC_DATA_URI = NFC_DATA_ABILITY_PREFIX + NFC_DATA_URI_ID;
102const std::string DATA_SHARE_KEY_STATE = "data_key_nfc_state";
103const std::string NFC_DATA_COLUMN_KEYWORD = "KEYWORD";
104const std::string NFC_DATA_COLUMN_VALUE = "VALUE";
105
106/** Database key for payment default app. */
107const std::string NFC_PAYMENT_DEFAULT_APP =
108    "/com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?Proxy=true&key=nfc_payment_default_app";
109const std::string NFC_DATA_URI_PAYMENT_DEFAULT_APP = NFC_DATA_ABILITY_PREFIX + NFC_PAYMENT_DEFAULT_APP;
110const std::string DATA_SHARE_KEY_NFC_PAYMENT_DEFAULT_APP = "nfc_payment_default_app";
111
112/** NFC field on/off common event */
113const std::string COMMON_EVENT_NFC_ACTION_RF_FIELD_ON_DETECTED = "usual.event.nfc.action.RF_FIELD_ON_DETECTED";
114const std::string COMMON_EVENT_NFC_ACTION_RF_FIELD_OFF_DETECTED = "usual.event.nfc.action.RF_FIELD_OFF_DETECTED";
115
116const int DATA_SHARE_INVALID_VALUE = -1;
117
118/** type const of hce napi on */
119const std::string EVENT_HCE_CMD = "hceCmd";
120
121/** type const of max apdu length */
122const uint32_t MAX_APDU_DATA_BYTE = 1024;
123const uint32_t MAX_APDU_DATA_HEX_STR = MAX_APDU_DATA_BYTE * 2;
124const uint32_t MAX_AID_LIST_NUM_PER_APP = 100;
125
126#ifdef VENDOR_APPLICATIONS_ENABLED
127const int VENDOR_APP_INIT_DONE = 1;
128const int VENDOR_APP_CHANGE = 2;
129#endif
130
131enum 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
140enum 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
153enum 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
163class NfcSdkCommon final {
164public:
165    static const int SHIFT_SIZE = 8;
166    static const int SHIFT_TIME = 4;
167
168public:
169    static bool IsLittleEndian();
170    static std::string BytesVecToHexString(const unsigned char* src, uint32_t length);
171    static std::string UnsignedCharToHexString(const unsigned char src);
172    static void HexStringToBytes(const std::string &src, std::vector<unsigned char> &bytes);
173    static unsigned char GetByteFromHexStr(const std::string src, uint32_t index);
174    static uint32_t GetHexStrBytesLen(const std::string src);
175    static uint32_t StringToInt(std::string src, bool bLittleEndian = true);
176    static std::string IntToHexString(uint32_t num);
177    static void StringToAsciiBytes(const std::string &src, std::vector<unsigned char> &bytes);
178    static std::string StringToHexString(const std::string &src);
179    static std::string HexStringToAsciiString(const std::string &src);
180    static std::string HexArrayToStringWithoutChecking(const std::string &src);
181    static uint64_t GetCurrentTime();
182    static uint64_t GetRelativeTime();
183    static std::string CodeMiddlePart(const std::string &src);
184};
185}  // namespace KITS
186}  // namespace NFC
187}  // namespace OHOS
188#endif  // NFC_SDK_COMMON_H
189