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