1 /*
2  * Copyright (c) 2023-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 INTERFACES_KITS_USB_MANAGER_INCLUDE_USB_MANAGER_ADDON_H
17 #define INTERFACES_KITS_USB_MANAGER_INCLUDE_USB_MANAGER_ADDON_H
18 
19 #include "edm_constants.h"
20 #include "napi/native_api.h"
21 #include "napi/native_common.h"
22 #include "napi/native_node_api.h"
23 #include "napi_edm_common.h"
24 #include "napi_edm_error.h"
25 #include "usb_device_id.h"
26 #ifdef USB_EDM_ENABLE
27 #include "usb_interface_type.h"
28 #endif
29 #include "want.h"
30 
31 namespace OHOS {
32 namespace EDM {
33 struct AsyncSetUsbPolicyCallbackInfo : AsyncCallbackInfo {
34     OHOS::AppExecFwk::ElementName elementName;
35     int32_t policy;
36 };
37 
38 constexpr int32_t USB_POLICY[] = {
39     EdmConstants::STORAGE_USB_POLICY_READ_WRITE,
40     EdmConstants::STORAGE_USB_POLICY_READ_ONLY,
41     EdmConstants::STORAGE_USB_POLICY_DISABLED
42 };
43 
44 constexpr int32_t DESCRIPTOR[] = {
45     EdmConstants::USB_INTERFACE_DESCRIPTOR,
46     EdmConstants::USB_DEVICE_DESCRIPTOR
47 };
48 
49 class UsbManagerAddon {
50 public:
51     static napi_value Init(napi_env env, napi_value exports);
52 
53 private:
54     static napi_value SetUsbPolicy(napi_env env, napi_callback_info info);
55     static void CreateUsbPolicyEnum(napi_env env, napi_value value);
56     static void NativeSetUsbPolicy(napi_env env, void *data);
57     static napi_value DisableUsb(napi_env env, napi_callback_info info);
58     static napi_value IsUsbDisabled(napi_env env, napi_callback_info info);
59     static napi_value AddAllowedUsbDevices(napi_env env, napi_callback_info info);
60     static napi_value RemoveAllowedUsbDevices(napi_env env, napi_callback_info info);
61     static napi_value AddOrRemoveAllowedUsbDevices(napi_env env, napi_callback_info info, bool isAdd);
62     static bool ParseUsbDevicesArray(napi_env env, std::vector<UsbDeviceId> &usbDeviceIds, napi_value object);
63     static bool GetUsbDeviceIdFromNAPI(napi_env env, napi_value value, UsbDeviceId &usbDeviceId);
64     static napi_value GetAllowedUsbDevices(napi_env env, napi_callback_info info);
65     static napi_value UsbDeviceIdToJsObj(napi_env env, const UsbDeviceId &usbDeviceId);
66     static napi_value SetUsbStorageDeviceAccessPolicy(napi_env env, napi_callback_info info);
67     static napi_value GetUsbStorageDeviceAccessPolicy(napi_env env, napi_callback_info info);
68 
69     static void CreateDescriptorEnum(napi_env env, napi_value value);
70     static napi_value AddDisallowedUsbDevices(napi_env env, napi_callback_info info);
71     static napi_value RemoveDisallowedUsbDevices(napi_env env, napi_callback_info info);
72     static napi_value AddOrRemoveDisallowedUsbDevices(napi_env env, napi_callback_info info, bool isAdd);
73     static napi_value GetDisallowedUsbDevices(napi_env env, napi_callback_info info);
74 #ifdef USB_EDM_ENABLE
75     static bool ParseUsbDeviceTypesArray(napi_env env, std::vector<USB::UsbDeviceType> &usbDeviceTypes,
76         napi_value object);
77     static bool GetUsbDeviceTypeFromNAPI(napi_env env, napi_value value, USB::UsbDeviceType &usbDeviceType);
78     static napi_value UsbDeviceTypeToJsObj(napi_env env, const USB::UsbDeviceType &usbDeviceType);
79 #endif
80 };
81 } // namespace EDM
82 } // namespace OHOS
83 
84 #endif // INTERFACES_KITS_USB_MANAGER_INCLUDE_USB_MANAGER_ADDON_H
85