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 OHOS_HDI_USB_V1_1_USBD_ACCESSORY_H
17 #define OHOS_HDI_USB_V1_1_USBD_ACCESSORY_H
18 
19 #include <fcntl.h>
20 #include <sys/types.h>
21 #include <sys/ioctl.h>
22 #include <vector>
23 #include <string>
24 #include <map>
25 namespace OHOS {
26 namespace HDI {
27 namespace Usb {
28 namespace V1_1 {
29 
30 /* ioctls for retrieving strings set by the host */
31 #define ACCESSORY_GET_STRING_MANUFACTURER   _IOW('M', 1, char[256])
32 #define ACCESSORY_GET_STRING_MODEL          _IOW('M', 2, char[256])
33 #define ACCESSORY_GET_STRING_DESCRIPTION    _IOW('M', 3, char[256])
34 #define ACCESSORY_GET_STRING_VERSION        _IOW('M', 4, char[256])
35 #define ACCESSORY_GET_STRING_URI            _IOW('M', 5, char[256])
36 #define ACCESSORY_GET_STRING_SERIAL         _IOW('M', 6, char[256])
37 /* returns 1 if there is a start request pending */
38 #define ACCESSORY_IS_START_REQUESTED        _IO('M', 7)
39 /* returns audio mode (set via the ACCESSORY_SET_AUDIO_MODE control request) */
40 #define ACCESSORY_GET_AUDIO_MODE            _IO('M', 8)
41 #define AOA_IOCTL_EXTRA_DATA                0xC0
42 #define AOA_GET_EXTRA_DATA                  _IOW('M', AOA_IOCTL_EXTRA_DATA, char[256])
43 
44 
45 class UsbdAccessory {
46 public:
47     static UsbdAccessory &GetInstance();
48     int32_t GetAccessoryInfo(std::vector<std::string> &accessoryInfo);
49     int32_t OpenAccessory(int32_t &fd);
50     int32_t CloseAccessory(int32_t fd);
51     void HandleEvent(int32_t state);
52 private:
53     int32_t GetAccessoryString(int32_t fd, int32_t cmd, std::string &accInfoString);
54     int32_t ExtraToString(char* buffer, int32_t len, std::string &extraData);
55     void init_base64_char_map();
56     std::string base64_encode(char* buffer, int32_t len);
57     std::map<char, int> base64_char_map;
58     int32_t accFd = {0};
59 };
60 
61 } // namespace V1_1
62 } // namespace Usb
63 } // namespace HDI
64 } // namespace OHOS
65 #endif // OHOS_HDI_USB_V1_1_USBD_ACCESSORY_H
66