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