1/* 2 * Copyright (c) 2021 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 16import { paramMock } from '../utils'; 17 18export function mockUsb() { 19 const USBDevice = { 20 busNum: '[PC Preview] unknow busNum', 21 devAddress: '[PC Preview] unknow devAddress', 22 serial: '[PC Preview] unknow serial', 23 name: '[PC Preview] unknow name', 24 manufacturerName: '[PC Preview] unknow maunfactureName', 25 productName: '[PC Preview] unknow productName', 26 version: '[PC Preview] unknow version', 27 vendorId: '[PC Preview] unknow vendorId', 28 productId: '[PC Preview] unknow productId', 29 clazz: '[PC Preview] unknow clazz', 30 subClass: '[PC Preview] unknow subClass', 31 protocol: '[PC Preview] unknow protocol', 32 configs: '[PC Preview] unknow configs', 33 }; 34 35 const USBConfig = { 36 id: '[PC Preview] unknow id', 37 attributes: '[PC Preview] unknow attributes', 38 maxPower: '[PC Preview] unknow maxPower', 39 name: '[PC Preview] unknow name', 40 isRemoteWakeup: '[PC Preview] unknow isRemoteWakeup', 41 isSelfPowered: '[PC Preview] unknow isSelfPowered', 42 interfaces: '[PC Preview] unknow interfaces', 43 }; 44 45 const USBInterface = { 46 id: '[PC Preview] unknow id', 47 protocol: '[PC Preview] unknow protocol', 48 clazz: '[PC Preview] unknow clazz', 49 subClass: '[PC Preview] unknow subClass', 50 alternateSetting: '[PC Preview] unknow alternateSetting', 51 name: '[PC Preview] unknow name', 52 endpoints: '[PC Preview] unknow endpoints', 53 }; 54 55 const USBEndpoint = { 56 address: '[PC Preview] unknow address', 57 attributes: '[PC Preview] unknow attributes', 58 interval: '[PC Preview] unknow interval', 59 maxPacketSize: '[PC Preview] unknow maxPacketSize', 60 direction: '[PC Preview] unknow direction', 61 number: '[PC Preview] unknow number', 62 type: '[PC Preview] unknow type', 63 interfaceId: '[PC Preview] unknow interfaceId', 64 }; 65 66 const USBDevicePipe = { 67 busNum: '[PC Preview] unknow busNum', 68 devAddress: '[PC Preview] unknow devAddress', 69 }; 70 71 const PowerRoleType = { 72 NONE: '[PC Preview] unknow NONE', 73 SOURCE: '[PC Preview] unknow SOURCE', 74 SINK: '[PC Preview] unknow SINK', 75 }; 76 77 const DataRoleType = { 78 NONE: '[PC Preview] unknow NONE', 79 HOST: '[PC Preview] unknow HOST', 80 DEVICE: '[PC Preview] unknow DEVICE', 81 }; 82 83 const USBPort = { 84 id: '[PC Preview] unknow id', 85 supportedModes: '[PC Preview] unknow supportedModes', 86 status: '[PC Preview] unknow status', 87 }; 88 89 const PortModeType = { 90 NONE: '[PC Preview] unknow NONE', 91 UFP: '[PC Preview] unknow UFP', 92 DFP: '[PC Preview] unknow DFP', 93 DRP: '[PC Preview] unknow DRP', 94 NUM_MODES: '[PC Preview] unknow NUM_MODES', 95 }; 96 97 const USBPortStatus = { 98 currentMode: '[PC Preview] unknow currentMode', 99 currentPowerRole: '[PC Preview] unknow currentPowerRole', 100 currentDataRole: '[PC Preview] unknow currentDataRole', 101 }; 102 103 const USBControlParams = { 104 request: '[PC Preview] unknow request', 105 target: '[PC Preview] unknow target', 106 reqType: '[PC Preview] unknow reqType', 107 direction: '[PC Preview] unknow direction', 108 value: '[PC Preview] unknow value', 109 index: '[PC Preview] unknow index', 110 data: '[PC Preview] unknow data', 111 }; 112 113 const USBRequestTargetType = { 114 USB_REQUEST_TARGET_DEVICE: '[PC Preview] unknow USB_REQUEST_TARGET_DEVICE', 115 USB_REQUEST_TARGET_INTERFACE: '[PC Preview] unknow USB_REQUEST_TARGET_INTERFACE', 116 USB_REQUEST_TARGET_ENDPOINT: '[PC Preview] unknow USB_REQUEST_TARGET_ENDPOINT', 117 USB_REQUEST_TARGET_OTHER: '[PC Preview] unknow USB_REQUEST_TARGET_OTHER', 118 }; 119 120 const USBControlRequestType = { 121 USB_REQUEST_TYPE_STANDARD: '[PC Preview] unknow USB_REQUEST_TYPE_STANDARD', 122 USB_REQUEST_TYPE_CLASS: '[PC Preview] unknow USB_REQUEST_TYPE_CLASS', 123 USB_REQUEST_TYPE_VENDOR: '[PC Preview] unknow USB_REQUEST_TYPE_VENDOR', 124 }; 125 126 const USBRequestDirection = { 127 USB_REQUEST_DIR_TO_DEVICE: '[PC Preview] unknow USB_REQUEST_DIR_TO_DEVICE', 128 USB_REQUEST_DIR_FROM_DEVICE: '[PC Preview] unknow USB_REQUEST_DIR_FROM_DEVICE', 129 }; 130 131 const FunctionType = { 132 NONE: '[PC Preview] unknow NONE', 133 ACM: '[PC Preview] unknow ACM', 134 ECM: '[PC Preview] unknow ECM', 135 HDC: '[PC Preview] unknow HDC', 136 MTP: '[PC Preview] unknow MTP', 137 PTP: '[PC Preview] unknow PTP', 138 RNDIS: '[PC Preview] unknow RNDIS', 139 MIDI: '[PC Preview] unknow MIDI', 140 AUDIO_SOURCE: '[PC Preview] unknow AUDIO_SOURCE', 141 NCM: '[PC Preview] unknow NCM', 142 }; 143 144 const usb = { 145 getVersion: function (...args) { 146 console.warn('usb.getVersion interface mocked in the Previewer. How this interface works on the' 147 + ' Previewer may be different from that on a real device.'); 148 return paramMock.paramStringMock; 149 }, 150 getDevices: function (...args) { 151 console.warn('usb.getDevices interface mocked in the Previewer. How this interface works on the' 152 + ' Previewer may be different from that on a real device.'); 153 return [USBDevice]; 154 }, 155 connectDevice: function (...args) { 156 console.warn('usb.connectDevice interface mocked in the Previewer. How this interface works on the' 157 + ' Previewer may be different from that on a real device.'); 158 return USBDevicePipe; 159 }, 160 getPorts: function (...args) { 161 console.warn('usb.getPorts interface mocked in the Previewer. How this interface works on the' 162 + ' Previewer may be different from that on a real device.'); 163 return [USBPort]; 164 }, 165 getSupportedModes: function (...args) { 166 console.warn('usb.getSupportedModes interface mocked in the Previewer. How this interface works on the' 167 + ' Previewer may be different from that on a real device.'); 168 return PortModeType; 169 }, 170 requestRight: function (...args) { 171 console.warn('usb.requestRight interface mocked in the Previewer. How this interface works on the' 172 + ' Previewer may be different from that on a real device.'); 173 const len = args.length; 174 if (len > 0 && typeof args[len - 1] === 'function') { 175 args[len - 1].call(this, paramMock.businessErrorMock); 176 } else { 177 return new Promise((resolve, reject) => { 178 resolve(paramMock.paramBooleanMock); 179 }); 180 } 181 }, 182 getCurrentFunctions: function (...args) { 183 console.warn('usb.getCurrentFunctions interface mocked in the Previewer. How this interface works on the' 184 + ' Previewer may be different from that on a real device.'); 185 return FunctionType; 186 }, 187 setCurrentFunctions: function (...args) { 188 console.warn('usb.setCurrentFunctions interface mocked in the Previewer. How this interface works on the' 189 + ' Previewer may be different from that on a real device.'); 190 const len = args.length; 191 if (len > 0 && typeof args[len - 1] === 'function') { 192 args[len - 1].call(this, paramMock.businessErrorMock); 193 } else { 194 return new Promise((resolve, reject) => { 195 resolve(paramMock.paramBooleanMock); 196 }); 197 } 198 }, 199 hasRight: function (...args) { 200 console.warn('usb.hasRight interface mocked in the Previewer. How this interface works on the' 201 + ' Previewer may be different from that on a real device.'); 202 return paramMock.paramBooleanMock; 203 }, 204 setPortRoles: function (...args) { 205 console.warn('usb.setPortRoles interface mocked in the Previewer. How this interface works on the' 206 + ' Previewer may be different from that on a real device.'); 207 const len = args.length; 208 if (len > 0 && typeof args[len - 1] === 'function') { 209 args[len - 1].call(this, paramMock.businessErrorMock); 210 } else { 211 return new Promise((resolve, reject) => { 212 resolve(paramMock.paramBooleanMock); 213 }); 214 } 215 }, 216 getRawDescriptor: function (...args) { 217 console.warn('usb.getRawDescriptor interface mocked in the Previewer. How this interface works on the' 218 + ' Previewer may be different from that on a real device.'); 219 return paramMock.paramArrayMock; 220 }, 221 closePipe: function (...args) { 222 console.warn('usb.closePipe interface mocked in the Previewer. How this interface works on the' 223 + ' Previewer may be different from that on a real device.'); 224 return paramMock.paramNumberMock; 225 }, 226 claimInterface: function (...args) { 227 console.warn('usb.claimInterface interface mocked in the Previewer. How this interface works on the' 228 + ' Previewer may be different from that on a real device.'); 229 return paramMock.paramNumberMock; 230 }, 231 releaseInterface: function (...args) { 232 console.warn('usb.releaseInterface interface mocked in the Previewer. How this interface works on the' 233 + ' Previewer may be different from that on a real device.'); 234 return paramMock.paramNumberMock; 235 }, 236 setInterface: function (...args) { 237 console.warn('usb.setInterface interface mocked in the Previewer. How this interface works on the' 238 + ' Previewer may be different from that on a real device.'); 239 return paramMock.paramNumberMock; 240 }, 241 getFileDescriptor: function (...args) { 242 console.warn('usb.getFileDescriptor interface mocked in the Previewer. How this interface works on the' 243 + ' Previewer may be different from that on a real device.'); 244 return paramMock.paramNumberMock; 245 }, 246 usbFunctionsFromString: function (...args) { 247 console.warn('usb.usbFunctionsFromString interface mocked in the Previewer. How this interface works on the' 248 + ' Previewer may be different from that on a real device.'); 249 return paramMock.paramNumberMock; 250 }, 251 usbFunctionsToString: function (...args) { 252 console.warn('usb.usbFunctionsToString interface mocked in the Previewer. How this interface works on the' 253 + ' Previewer may be different from that on a real device.'); 254 return paramMock.paramStringMock; 255 }, 256 controlTransfer: function (...args) { 257 console.warn('usb.controlTransfer interface mocked in the Previewer. How this interface works on the' 258 + ' Previewer may be different from that on a real device.'); 259 const len = args.length; 260 if (len > 0 && typeof args[len - 1] === 'function') { 261 args[len - 1].call(this, paramMock.businessErrorMock); 262 } else { 263 return new Promise((resolve, reject) => { 264 resolve(paramMock.paramNumberMock); 265 }); 266 } 267 }, 268 setConfiguration: function (...args) { 269 console.warn('usb.setConfiguration interface mocked in the Previewer. How this interface works on the' 270 + ' Previewer may be different from that on a real device.'); 271 return paramMock.paramNumberMock; 272 }, 273 bulkTransfer: function (...args) { 274 console.warn('usb.bulkTransfer interface mocked in the Previewer. How this interface works on the' 275 + ' Previewer may be different from that on a real device.'); 276 const len = args.length; 277 if (len > 0 && typeof args[len - 1] === 'function') { 278 args[len - 1].call(this, paramMock.businessErrorMock); 279 } else { 280 return new Promise((resolve, reject) => { 281 resolve(paramMock.paramNumberMock); 282 }); 283 } 284 }, 285 }; 286 return usb; 287} 288 289