usb_vendors.c revision a481d096feba7cf6e61d54e416a68e2d46498df1
1/*
2 * Copyright (C) 2009 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "usb_vendors.h"
18
19#include <stdio.h>
20
21#ifdef _WIN32
22#  define WIN32_LEAN_AND_MEAN
23#  include "windows.h"
24#  include "shlobj.h"
25#else
26#  include <unistd.h>
27#  include <sys/stat.h>
28#endif
29
30#include "sysdeps.h"
31#include "adb.h"
32
33#define ANDROID_PATH            ".android"
34#define ANDROID_ADB_INI         "adb_usb.ini"
35
36#define TRACE_TAG               TRACE_USB
37
38// Google's USB Vendor ID
39#define VENDOR_ID_GOOGLE        0x18d1
40// HTC's USB Vendor ID
41#define VENDOR_ID_HTC           0x0bb4
42
43/** built-in vendor list */
44int builtInVendorIds[] = {
45    VENDOR_ID_GOOGLE,
46    VENDOR_ID_HTC,
47};
48
49#define BUILT_IN_VENDOR_COUNT    (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
50
51/* max number of supported vendor ids (built-in + 3rd party). increase as needed */
52#define VENDOR_COUNT_MAX         128
53
54int vendorIds[VENDOR_COUNT_MAX];
55unsigned vendorIdCount = 0;
56
57int get_adb_usb_ini(char* buff, size_t len);
58
59void usb_vendors_init(void)
60{
61    if (VENDOR_COUNT_MAX < BUILT_IN_VENDOR_COUNT) {
62        fprintf(stderr, "VENDOR_COUNT_MAX not big enough for built-in vendor list.\n");
63        exit(2);
64    }
65
66    /* add the built-in vendors at the beginning of the array */
67    memcpy(vendorIds, builtInVendorIds, sizeof(builtInVendorIds));
68
69    /* default array size is the number of built-in vendors */
70    vendorIdCount = BUILT_IN_VENDOR_COUNT;
71
72    if (VENDOR_COUNT_MAX == BUILT_IN_VENDOR_COUNT)
73        return;
74
75    char temp[PATH_MAX];
76    if (get_adb_usb_ini(temp, sizeof(temp)) == 0) {
77        FILE * f = fopen(temp, "rt");
78
79        if (f != NULL) {
80            /* The vendor id file is pretty basic. 1 vendor id per line.
81               Lines starting with # are comments */
82            while (fgets(temp, sizeof(temp), f) != NULL) {
83                if (temp[0] == '#')
84                    continue;
85
86                long value = strtol(temp, NULL, 0);
87                if (errno == EINVAL || errno == ERANGE || value > INT_MAX || value < 0) {
88                    fprintf(stderr, "Invalid content in %s. Quitting.\n", ANDROID_ADB_INI);
89                    exit(2);
90                }
91
92                vendorIds[vendorIdCount++] = (int)value;
93
94                /* make sure we don't go beyond the array */
95                if (vendorIdCount == VENDOR_COUNT_MAX) {
96                    break;
97                }
98            }
99        }
100    }
101}
102
103/* Utils methods */
104
105/* builds the path to the adb vendor id file. returns 0 if success */
106int build_path(char* buff, size_t len, const char* format, const char* home)
107{
108    if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= len) {
109        return 1;
110    }
111
112    return 0;
113}
114
115/* fills buff with the path to the adb vendor id file. returns 0 if success */
116int get_adb_usb_ini(char* buff, size_t len)
117{
118#ifdef _WIN32
119    const char* home = getenv("ANDROID_SDK_HOME");
120    if (home != NULL) {
121        return build_path(buff, len, "%s\\%s\\%s", home);
122    } else {
123        char path[MAX_PATH];
124        SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, path);
125        return build_path(buff, len, "%s\\%s\\%s", path);
126    }
127#else
128    const char* home = getenv("HOME");
129    if (home == NULL)
130        home = "/tmp";
131
132    return build_path(buff, len, "%s/%s/%s", home);
133#endif
134}
135