1acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine/*
2acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * Copyright (C) 2009 The Android Open Source Project
3acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine *
4acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * Licensed under the Apache License, Version 2.0 (the "License");
5acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * you may not use this file except in compliance with the License.
6acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * You may obtain a copy of the License at
7acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine *
8acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine *      http://www.apache.org/licenses/LICENSE-2.0
9acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine *
10acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * Unless required by applicable law or agreed to in writing, software
11acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * distributed under the License is distributed on an "AS IS" BASIS,
12acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * See the License for the specific language governing permissions and
14acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine * limitations under the License.
15acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine */
16acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
17acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine// AdbWinUsbApi.cpp : Implementation of DLL Exports.
18acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
19acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine#include "stdafx.h"
20acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine#include "adb_winusb_interface.h"
21acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
22acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkineclass CAdbWinApiModule : public CAtlDllModuleT< CAdbWinApiModule > {
23acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkinepublic:
24acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine};
25acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
26acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkineCAdbWinApiModule _AtlModule;
27acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
28acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine// DLL Entry Point
29acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkineextern "C" BOOL WINAPI DllMain(HINSTANCE instance,
30acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine                               DWORD reason,
31acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine                               LPVOID reserved) {
32acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    return _AtlModule.DllMain(reason, reserved);
33acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine}
34acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
35acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine/** \brief Instantiates interface instance that uses WinUsb API to communicate
36acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine  with USB driver.
37acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
38acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine  This is the only exported routine from this DLL. This routine instantiates an
39acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine  object of AdbWinUsbInterfaceObject on request from AdbWinApi.dll when it is
40acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine  detected that underlying USB driver is WinUsb.sys.
41acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine  @param[in] interface_name Name of the interface.
42acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine  @return AdbInterfaceObject - casted instance of AdbWinUsbInterfaceObject
43acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine          object on success, or NULL on failure with GetLastError providing
44acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine          information on an error that occurred.
45acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine*/
46acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkineextern "C" __declspec(dllexport)
47acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkineAdbInterfaceObject* __cdecl InstantiateWinUsbInterface(
48acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    const wchar_t* interface_name) {
49acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    // Validate parameter.
50acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    if (NULL == interface_name) {
51acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine        return NULL;
52acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    }
53acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine
54acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    // Instantiate requested object.
55acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    try {
56acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine        return new AdbWinUsbInterfaceObject(interface_name);
57acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    } catch (...) {
58acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine        // We expect only OOM exceptions here.
59acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine        SetLastError(ERROR_OUTOFMEMORY);
60acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine        return NULL;
61acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine    }
62acc6f826433e639b1ba00c021ab5f9161eb56e59vchtchetkine}
63