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