blob: 394edc304715bd585a20af487176cd4cc3b1df05 [file] [log] [blame]
Xavier Ducroheta09fbd12009-05-20 17:33:53 -07001/*
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
Xavier Ducroheta09fbd12009-05-20 17:33:53 -070019#include <stdio.h>
Xavier Ducroheta481d092009-05-21 17:47:43 -070020
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"
Xavier Ducroheta09fbd12009-05-20 17:33:53 -070031#include "adb.h"
32
Xavier Ducroheta481d092009-05-21 17:47:43 -070033#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
Xavier Ducrohetde6f62a2009-07-24 14:14:56 -070042// Samsung's USB Vendor ID
43#define VENDOR_ID_SAMSUNG 0x04e8
44// Motorola's USB Vendor ID
45#define VENDOR_ID_MOTOROLA 0x22b8
Mike Lockwood6ae92562009-09-21 08:09:59 -040046// LG's USB Vendor ID
47#define VENDOR_ID_LGE 0x1004
48// Huawei's USB Vendor ID
49#define VENDOR_ID_HUAWEI 0x12D1
Xavier Ducroheta481d092009-05-21 17:47:43 -070050
51/** built-in vendor list */
52int builtInVendorIds[] = {
53 VENDOR_ID_GOOGLE,
54 VENDOR_ID_HTC,
Xavier Ducrohetde6f62a2009-07-24 14:14:56 -070055 VENDOR_ID_SAMSUNG,
56 VENDOR_ID_MOTOROLA,
Mike Lockwood6ae92562009-09-21 08:09:59 -040057 VENDOR_ID_LGE,
58 VENDOR_ID_HUAWEI,
Xavier Ducroheta481d092009-05-21 17:47:43 -070059};
60
61#define BUILT_IN_VENDOR_COUNT (sizeof(builtInVendorIds)/sizeof(builtInVendorIds[0]))
62
63/* max number of supported vendor ids (built-in + 3rd party). increase as needed */
64#define VENDOR_COUNT_MAX 128
65
66int vendorIds[VENDOR_COUNT_MAX];
Xavier Ducroheta09fbd12009-05-20 17:33:53 -070067unsigned vendorIdCount = 0;
68
Xavier Ducroheta481d092009-05-21 17:47:43 -070069int get_adb_usb_ini(char* buff, size_t len);
70
71void usb_vendors_init(void)
72{
73 if (VENDOR_COUNT_MAX < BUILT_IN_VENDOR_COUNT) {
74 fprintf(stderr, "VENDOR_COUNT_MAX not big enough for built-in vendor list.\n");
75 exit(2);
76 }
77
78 /* add the built-in vendors at the beginning of the array */
79 memcpy(vendorIds, builtInVendorIds, sizeof(builtInVendorIds));
80
81 /* default array size is the number of built-in vendors */
82 vendorIdCount = BUILT_IN_VENDOR_COUNT;
83
84 if (VENDOR_COUNT_MAX == BUILT_IN_VENDOR_COUNT)
85 return;
86
87 char temp[PATH_MAX];
88 if (get_adb_usb_ini(temp, sizeof(temp)) == 0) {
89 FILE * f = fopen(temp, "rt");
90
91 if (f != NULL) {
92 /* The vendor id file is pretty basic. 1 vendor id per line.
93 Lines starting with # are comments */
94 while (fgets(temp, sizeof(temp), f) != NULL) {
95 if (temp[0] == '#')
96 continue;
97
98 long value = strtol(temp, NULL, 0);
99 if (errno == EINVAL || errno == ERANGE || value > INT_MAX || value < 0) {
100 fprintf(stderr, "Invalid content in %s. Quitting.\n", ANDROID_ADB_INI);
101 exit(2);
102 }
103
104 vendorIds[vendorIdCount++] = (int)value;
105
106 /* make sure we don't go beyond the array */
107 if (vendorIdCount == VENDOR_COUNT_MAX) {
108 break;
109 }
110 }
111 }
112 }
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700113}
114
Xavier Ducroheta481d092009-05-21 17:47:43 -0700115/* Utils methods */
116
117/* builds the path to the adb vendor id file. returns 0 if success */
118int build_path(char* buff, size_t len, const char* format, const char* home)
119{
120 if (snprintf(buff, len, format, home, ANDROID_PATH, ANDROID_ADB_INI) >= len) {
121 return 1;
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700122 }
Xavier Ducroheta481d092009-05-21 17:47:43 -0700123
124 return 0;
125}
126
127/* fills buff with the path to the adb vendor id file. returns 0 if success */
128int get_adb_usb_ini(char* buff, size_t len)
129{
130#ifdef _WIN32
131 const char* home = getenv("ANDROID_SDK_HOME");
132 if (home != NULL) {
133 return build_path(buff, len, "%s\\%s\\%s", home);
134 } else {
135 char path[MAX_PATH];
136 SHGetFolderPath( NULL, CSIDL_PROFILE, NULL, 0, path);
137 return build_path(buff, len, "%s\\%s\\%s", path);
138 }
139#else
140 const char* home = getenv("HOME");
141 if (home == NULL)
142 home = "/tmp";
143
144 return build_path(buff, len, "%s/%s/%s", home);
145#endif
Xavier Ducroheta09fbd12009-05-20 17:33:53 -0700146}