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_1/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 namespace OHOS {
32 namespace USB {
33 
34 enum ACCESSORYMODE : int32_t {
35     ACC_NONE,
36     ACC_CONFIGURING,
37     ACC_START,
38     ACC_STOP,
39     ACC_SEND,
40 };
41 
42 class UsbAccessoryManager {
43 public:
44     UsbAccessoryManager();
45     ~UsbAccessoryManager();
46     void HandleEvent(int32_t status, bool delayProcess = true);
47     int32_t SetUsbd(const sptr<OHOS::HDI::Usb::V1_1::IUsbInterface> usbd);
48     void GetAccessoryList(const std::string &bundleName, std::vector<USBAccessory> &accessoryList);
49     int32_t GetAccessorySerialNumber(const USBAccessory &access, const std::string &bundleName,
50         std::string &serialValue);
51     int32_t OpenAccessory(int32_t &fd);
52     int32_t CloseAccessory(int32_t fd);
53 private:
54     void ProcessHandle(int32_t curAccStatus);
55     int32_t ProcessAccessoryStart(int32_t curFunc, int32_t curAccStatus);
56     int32_t ProcessAccessoryStop(int32_t curFunc, int32_t curAccStatus);
57     int32_t ProcessAccessorySend();
58     void InitBase64Map();
59     std::vector<uint8_t> Base64Decode(const std::string& encoded_string);
60     std::string SerialValueHash(const std::string &serialValue);
61     bool compare(const std::string &s1, const std::string &s2);
62     USBAccessory accessory;
63     int32_t accStatus_ {ACC_NONE};
64     int32_t eventStatus_;
65     int32_t curDeviceFunc_ = {UsbSrvSupport::FUNCTION_NONE};
66     int32_t lastDeviceFunc_ = {UsbSrvSupport::FUNCTION_NONE};
67     int32_t accFd_ = {0};
68     Utils::Timer accDelayTimer_ {"accDelayTimer"};
69     uint32_t accDelayTimerId_ {UINT32_MAX};
70     Utils::Timer antiShakeDelayTimer_ {"antiShakeDelayTimer"};
71     uint32_t antiShakeDelayTimerId_ {UINT32_MAX};
72     sptr<HDI::Usb::V1_1::IUsbInterface> usbdImpl_ = nullptr;
73     std::map<char, int> base64Map_;
74     std::mutex mutexHandleEvent_;
75 };
76 
77 } // USB
78 } // OHOS
79 #endif // USB_ACCESSORY_MANAGER_H
80