1 /* 2 * Copyright (C) 2024 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 16 #ifndef SIM_SE_VENDOR_ADAPTIONS_H 17 #define SIM_SE_VENDOR_ADAPTIONS_H 18 19 #include "v1_0/isecure_element_interface.h" 20 #include <dlfcn.h> 21 #include <hdf_log.h> 22 #include <memory> 23 #include <string> 24 25 #include "remote_death_recipient.h" 26 27 enum SIM_SECURE_ELEMENT_RET { 28 SIM_SECURE_ELEMENT_RET_OK = 0, 29 SIM_SECURE_ELEMENT_RET_CONTEXT_FAIL = 1, 30 SIM_SECURE_ELEMENT_RET_MEMSET_FAIL, 31 SIM_SECURE_ELEMENT_RET_TEE_UNINITED, 32 SIM_SECURE_ELEMENT_RET_ESE_CONFIG_FAIL, 33 SIM_SECURE_ELEMENT_RET_LOAD_FAIL, 34 SIM_SECURE_ELEMENT_RET_END, 35 }; 36 37 namespace OHOS { 38 namespace HDI { 39 namespace SecureElement { 40 namespace SimSecureElement { 41 namespace V1_0 { 42 class SimSeVendorAdaptions { 43 public: 44 SimSeVendorAdaptions(); 45 ~SimSeVendorAdaptions(); 46 47 int32_t init(const sptr<OHOS::HDI::SecureElement::SimSecureElement::V1_0::ISecureElementCallback>& clientCallback, 48 OHOS::HDI::SecureElement::SimSecureElement::V1_0::SecureElementStatus& status); 49 50 int32_t getAtr(std::vector<uint8_t>& response); 51 52 int32_t isSecureElementPresent(bool& present); 53 54 int32_t openLogicalChannel(const std::vector<uint8_t>& aid, uint8_t p2, std::vector<uint8_t>& response, 55 uint8_t& channelNumber, OHOS::HDI::SecureElement::SimSecureElement::V1_0::SecureElementStatus& status); 56 57 int32_t openBasicChannel(const std::vector<uint8_t>& aid, uint8_t p2, std::vector<uint8_t>& response, 58 OHOS::HDI::SecureElement::SimSecureElement::V1_0::SecureElementStatus& status); 59 60 int32_t closeChannel(uint8_t channelNumber, 61 OHOS::HDI::SecureElement::SimSecureElement::V1_0::SecureElementStatus& status); 62 63 int32_t transmit(const std::vector<uint8_t>& command, std::vector<uint8_t>& response, 64 OHOS::HDI::SecureElement::SimSecureElement::V1_0::SecureElementStatus& status); 65 66 int32_t reset(OHOS::HDI::SecureElement::SimSecureElement::V1_0::SecureElementStatus& status); 67 private: 68 void OnRemoteDied(const wptr<IRemoteObject> &object); 69 int32_t AddSecureElementDeathRecipient(const sptr<ISecureElementCallback> &callbackObj); 70 int32_t RemoveSecureElementDeathRecipient(const sptr<ISecureElementCallback> &callbackObj); 71 72 sptr<RemoteDeathRecipient> remoteDeathRecipient_ = nullptr; 73 private: 74 class DynamicLoad { 75 public: 76 explicit DynamicLoad(const std::string &lib); 77 ~DynamicLoad(); 78 DynamicLoad(const DynamicLoad &) = delete; 79 DynamicLoad &operator=(DynamicLoad &) = delete; 80 bool LoadLib(); 81 bool CloseLib(); 82 template <typename T> FindTheFunc(const std::string & func)83 T FindTheFunc(const std::string &func) 84 { 85 if (!handle_) { 86 HDF_LOGE("fail handle is null"); 87 return nullptr; 88 } 89 T newFunc = reinterpret_cast<T>(dlsym(handle_, func.c_str())); 90 if (!newFunc) { 91 HDF_LOGE("find func:%{public}s in %{public}s fail", func.c_str(), libPath_.c_str()); 92 return nullptr; 93 } 94 HDF_LOGI("find func:%{public}s in %{public}s success", func.c_str(), libPath_.c_str()); 95 return newFunc; 96 } 97 98 private: 99 void *handle_{ nullptr }; 100 std::string libPath_; 101 }; 102 using VendorSimSecureElementInitT = int (*)(void); 103 using VendorSimSecureElementUninitT = int (*)(void); 104 using VendorSimSecureElementIsCardPresentT = bool (*)(void); 105 using VendorSimSecureElementGetAtrT = int (*)(uint8_t *rsp, uint32_t *rspLen); 106 using VendorSimSecureElementOpenLogicalChannelT = int (*)(uint8_t *aid, uint32_t len, uint8_t p2, uint8_t *rsp, 107 uint32_t *rspLen, uint32_t *channelNum, int *status); 108 using VendorSimSecureElementOpenBasicChannelT = int (*)(uint8_t *aid, uint32_t len, uint8_t *rsp, 109 uint32_t *rspLen, int *status); 110 using VendorSimSecureElementCloseChannelT = int (*)(uint32_t channelNum, int *status); 111 using VendorSimSecureElementTransmitT = int (*)(uint8_t *cmd, uint32_t cmdLen, uint8_t *rsp, 112 uint32_t *rspLen, int *status); 113 const char *const LIB_NAME = "libsim_secure_element.z.so"; 114 const char *const SIM_INIT_SYMBOL = "VendorSimSecureElementInit"; 115 const char *const SIM_UNINIT_SYMBOL = "VendorSimSecureElementUninit"; 116 const char *const SIM_IS_CARD_PRESENT_SYMBOL = "VendorSimSecureElementIsCardPresent"; 117 const char *const SIM_GET_ATR_SYMBOL = "VendorSimSecureElementGetAtr"; 118 const char *const SIM_OPEN_LOGICAL_SYMBOL = "VendorSimSecureElementOpenLogicalChannel"; 119 const char *const SIM_OPEN_BASIC_SYMBOL = "VendorSimSecureElementOpenBasicChannel"; 120 const char *const SIM_CLOSE_SYMBOL = "VendorSimSecureElementCloseChannel"; 121 const char *const SIM_TRANS_SYMBOL = "VendorSimSecureElementTransmit"; 122 123 void InitFunc(); 124 VendorSimSecureElementInitT vendorSimSecureElementInitFunc_{nullptr}; 125 VendorSimSecureElementUninitT vendorSimSecureElementUninitFunc_{nullptr}; 126 VendorSimSecureElementIsCardPresentT vendorSimSecureElementIsCardPresentFunc_{nullptr}; 127 VendorSimSecureElementGetAtrT vendorSimSecureElementGetAtrFunc_{nullptr}; 128 VendorSimSecureElementOpenLogicalChannelT vendorSimSecureElementOpenLogicalChannelFunc_{nullptr}; 129 VendorSimSecureElementOpenBasicChannelT vendorSimSecureElementOpenBasicChannelFunc_{nullptr}; 130 VendorSimSecureElementCloseChannelT vendorSimSecureElementCloseChannelFunc_{nullptr}; 131 VendorSimSecureElementTransmitT vendorSimSecureElementTransmitFunc_{nullptr}; 132 static inline std::unique_ptr<DynamicLoad> loader_; 133 134 int VendorSimSecureElementInit(); 135 int VendorSimSecureElementUninit(); 136 int VendorSimSecureElementGetAtr(uint8_t *rsp, uint32_t *rspLen); 137 int VendorSimSecureElementOpenLogicalChannel(const std::vector<uint8_t>& aid, uint8_t p2, 138 std::vector<uint8_t>& response, uint32_t *channelNum, int *status); 139 int VendorSimSecureElementOpenBasicChannel(uint8_t *aid, uint32_t len, uint8_t *rsp, uint32_t *rspLen, int *status); 140 int VendorSimSecureElementCloseChannel(uint32_t channelNum, int *status); 141 int VendorSimSecureElementTransmit(uint8_t *cmd, uint32_t cmdLen, uint8_t *rsp, uint32_t *rspLen, int *status); 142 }; 143 } // V1_0 144 } // SimSecureElement 145 } // SecureElement 146 } // HDI 147 } // OHOS 148 149 #endif // SIM_SE_VENDOR_ADAPTIONS_H 150