1 /* 2 * Copyright (c) Huawei Technologies Co., Ltd. 2024. All rights reserved. 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 USB_ACCESSORY_MANAGER_H 17 #define USB_ACCESSORY_MANAGER_H 18 #include <map> 19 #include <string> 20 #include <vector> 21 #include "timer.h" 22 23 #include "usb_accessory.h" 24 #include "usb_common.h" 25 #include "usb_srv_support.h" 26 #include "v1_2/iusb_interface.h" 27 #include "v1_0/iusb_interface.h" 28 #include "v1_0/iusbd_subscriber.h" 29 #include "delayed_sp_singleton.h" 30 #include "usb_right_manager.h" 31 #include "usb_timer_wraper.h" 32 #ifdef USB_MANAGER_V2_0 33 #include "v2_0/iusb_device_interface.h" 34 #endif // USB_MANAGER_V2_0 35 namespace OHOS { 36 namespace USB { 37 38 enum ACCESSORYMODE : int32_t { 39 ACC_NONE, 40 ACC_CONFIGURING, 41 ACC_START, 42 ACC_STOP, 43 ACC_SEND, 44 }; 45 46 class UsbAccessoryManager { 47 public: 48 UsbAccessoryManager(); 49 void HandleEvent(int32_t status, bool delayProcess = true); 50 int32_t SetUsbd(const sptr<OHOS::HDI::Usb::V1_2::IUsbInterface> usbd); 51 void GetAccessoryList(const std::string &bundleName, std::vector<USBAccessory> &accessoryList); 52 int32_t GetAccessorySerialNumber(const USBAccessory &access, const std::string &bundleName, 53 std::string &serialValue); 54 int32_t OpenAccessory(int32_t &fd); 55 int32_t CloseAccessory(int32_t fd); 56 #ifdef USB_MANAGER_V2_0 57 bool InitUsbAccessoryInterface(); 58 #endif // USB_MANAGER_V2_0 59 private: 60 void GetAccessoryInfo(std::vector<std::string> &accessorys); 61 int32_t SetCurrentFunctions(int32_t funcs); 62 int32_t GetCurrentFunctions(int32_t &funcs); 63 void ProcessHandle(int32_t curAccStatus); 64 int32_t ProcessAccessoryStart(int32_t curFunc, int32_t curAccStatus); 65 int32_t ProcessAccessoryStop(int32_t curFunc, int32_t curAccStatus); 66 int32_t ProcessAccessorySend(); 67 void InitBase64Map(); 68 std::vector<uint8_t> Base64Decode(const std::string& encoded_string); 69 std::string SerialValueHash(const std::string &serialValue); 70 bool compare(const std::string &s1, const std::string &s2); 71 USBAccessory accessory; 72 int32_t accStatus_ {ACC_NONE}; 73 int32_t eventStatus_ = 0; 74 int32_t curDeviceFunc_ = {UsbSrvSupport::FUNCTION_NONE}; 75 int32_t lastDeviceFunc_ = {UsbSrvSupport::FUNCTION_NONE}; 76 int32_t accFd_ = {0}; 77 uint32_t accDelayTimerId_ {UINT32_MAX}; 78 uint32_t antiShakeDelayTimerId_ {UINT32_MAX}; 79 sptr<HDI::Usb::V1_2::IUsbInterface> usbdImpl_ = nullptr; 80 std::map<char, int> base64Map_; 81 std::mutex mutexHandleEvent_; 82 #ifdef USB_MANAGER_V2_0 83 sptr<HDI::Usb::V2_0::IUsbDeviceInterface> usbDeviceInterface_ = nullptr; 84 #endif // USB_MANAGER_V2_0 85 }; 86 87 } // USB 88 } // OHOS 89 #endif // USB_ACCESSORY_MANAGER_H 90