libusb: require pkg-config support
[openocd.git] / src / helper / replacements.c
1 /***************************************************************************
2 * Copyright (C) 2006 by Dominic Rath *
3 * Dominic.Rath@gmx.de *
4 * *
5 * Copyright (C) 2007,2008 Øyvind Harboe *
6 * oyvind.harboe@zylin.com *
7 * *
8 * Copyright (C) 2008 by Spencer Oliver *
9 * spen@spen-soft.co.uk *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the GNU General Public License as published by *
13 * the Free Software Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * GNU General Public License for more details. *
20 * *
21 * You should have received a copy of the GNU General Public License *
22 * along with this program; if not, write to the *
23 * Free Software Foundation, Inc., *
24 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. *
25 ***************************************************************************/
26 /* DANGER!!!! These must be defined *BEFORE* replacements.h and the malloc() macro!!!! */
27
28 #include <stdlib.h>
29 #include <string.h>
30 /*
31 * clear_malloc
32 *
33 * will alloc memory and clear it
34 */
35 void *clear_malloc(size_t size)
36 {
37 void *t = malloc(size);
38 if (t != NULL)
39 memset(t, 0x00, size);
40 return t;
41 }
42
43 void *fill_malloc(size_t size)
44 {
45 void *t = malloc(size);
46 if (t != NULL) {
47 /* We want to initialize memory to some known bad state.
48 * 0 and 0xff yields 0 and -1 as integers, which often
49 * have meaningful values. 0x5555... is not often a valid
50 * integer and is quite easily spotted in the debugger
51 * also it is almost certainly an invalid address */
52 memset(t, 0x55, size);
53 }
54 return t;
55 }
56
57 #define IN_REPLACEMENTS_C
58 #ifdef HAVE_CONFIG_H
59 #include "config.h"
60 #endif
61 #ifdef HAVE_STRINGS_H
62 #include <strings.h>
63 #endif
64
65 #ifdef _WIN32
66 #include <io.h>
67 #endif
68
69 /* replacements for gettimeofday */
70 #ifndef HAVE_GETTIMEOFDAY
71
72 /* Windows */
73 #ifdef _WIN32
74
75 #ifndef __GNUC__
76 #define EPOCHFILETIME (116444736000000000i64)
77 #else
78 #define EPOCHFILETIME (116444736000000000LL)
79 #endif
80
81 int gettimeofday(struct timeval *tv, struct timezone *tz)
82 {
83 FILETIME ft;
84 LARGE_INTEGER li;
85 __int64 t;
86 static int tzflag;
87
88 if (tv) {
89 GetSystemTimeAsFileTime(&ft);
90 li.LowPart = ft.dwLowDateTime;
91 li.HighPart = ft.dwHighDateTime;
92 t = li.QuadPart; /* In 100-nanosecond intervals */
93 t -= EPOCHFILETIME; /* Offset to the Epoch time */
94 t /= 10; /* In microseconds */
95 tv->tv_sec = (long)(t / 1000000);
96 tv->tv_usec = (long)(t % 1000000);
97 }
98
99 if (tz) {
100 if (!tzflag) {
101 _tzset();
102 tzflag++;
103 }
104 tz->tz_minuteswest = _timezone / 60;
105 tz->tz_dsttime = _daylight;
106 }
107
108 return 0;
109 }
110 #endif /* _WIN32 */
111
112 #endif /* HAVE_GETTIMEOFDAY */
113
114 #ifndef HAVE_STRNLEN
115 size_t strnlen(const char *s, size_t maxlen)
116 {
117 const char *end = (const char *)memchr(s, '\0', maxlen);
118 return end ? (size_t) (end - s) : maxlen;
119 }
120 #endif
121
122 #ifndef HAVE_STRNDUP
123 char *strndup(const char *s, size_t n)
124 {
125 size_t len = strnlen(s, n);
126 char *new = (char *) malloc(len + 1);
127
128 if (new == NULL)
129 return NULL;
130
131 new[len] = '\0';
132 return (char *) memcpy(new, s, len);
133 }
134 #endif
135
136 #ifdef _WIN32
137 int win_select(int max_fd, fd_set *rfds, fd_set *wfds, fd_set *efds, struct timeval *tv)
138 {
139 DWORD ms_total, limit;
140 HANDLE handles[MAXIMUM_WAIT_OBJECTS];
141 int handle_slot_to_fd[MAXIMUM_WAIT_OBJECTS];
142 int n_handles = 0, i;
143 fd_set sock_read, sock_write, sock_except;
144 fd_set aread, awrite, aexcept;
145 int sock_max_fd = -1;
146 struct timeval tvslice;
147 int retcode;
148
149 #define SAFE_FD_ISSET(fd, set) (set != NULL && FD_ISSET(fd, set))
150
151 /* calculate how long we need to wait in milliseconds */
152 if (tv == NULL)
153 ms_total = INFINITE;
154 else {
155 ms_total = tv->tv_sec * 1000;
156 ms_total += tv->tv_usec / 1000;
157 }
158
159 FD_ZERO(&sock_read);
160 FD_ZERO(&sock_write);
161 FD_ZERO(&sock_except);
162
163 /* build an array of handles for non-sockets */
164 for (i = 0; i < max_fd; i++) {
165 if (SAFE_FD_ISSET(i, rfds) || SAFE_FD_ISSET(i, wfds) || SAFE_FD_ISSET(i, efds)) {
166 intptr_t handle = (intptr_t) _get_osfhandle(i);
167 handles[n_handles] = (HANDLE)handle;
168 if (handles[n_handles] == INVALID_HANDLE_VALUE) {
169 /* socket */
170 if (SAFE_FD_ISSET(i, rfds))
171 FD_SET(i, &sock_read);
172 if (SAFE_FD_ISSET(i, wfds))
173 FD_SET(i, &sock_write);
174 if (SAFE_FD_ISSET(i, efds))
175 FD_SET(i, &sock_except);
176 if (i > sock_max_fd)
177 sock_max_fd = i;
178 } else {
179 handle_slot_to_fd[n_handles] = i;
180 n_handles++;
181 }
182 }
183 }
184
185 if (n_handles == 0) {
186 /* plain sockets only - let winsock handle the whole thing */
187 return select(max_fd, rfds, wfds, efds, tv);
188 }
189
190 /* mixture of handles and sockets; lets multiplex between
191 * winsock and waiting on the handles */
192
193 FD_ZERO(&aread);
194 FD_ZERO(&awrite);
195 FD_ZERO(&aexcept);
196
197 limit = GetTickCount() + ms_total;
198 do {
199 retcode = 0;
200
201 if (sock_max_fd >= 0) {
202 /* overwrite the zero'd sets here; the select call
203 * will clear those that are not active */
204 aread = sock_read;
205 awrite = sock_write;
206 aexcept = sock_except;
207
208 tvslice.tv_sec = 0;
209 tvslice.tv_usec = 1000;
210
211 retcode = select(sock_max_fd + 1, &aread, &awrite, &aexcept, &tvslice);
212 }
213
214 if (n_handles > 0) {
215 /* check handles */
216 DWORD wret;
217
218 wret = MsgWaitForMultipleObjects(n_handles,
219 handles,
220 FALSE,
221 retcode > 0 ? 0 : 1,
222 QS_ALLEVENTS);
223
224 if (wret == WAIT_TIMEOUT) {
225 /* set retcode to 0; this is the default.
226 * select() may have set it to something else,
227 * in which case we leave it alone, so this branch
228 * does nothing */
229 ;
230 } else if (wret == WAIT_FAILED) {
231 if (retcode == 0)
232 retcode = -1;
233 } else {
234 if (retcode < 0)
235 retcode = 0;
236 for (i = 0; i < n_handles; i++) {
237 if (WAIT_OBJECT_0 == WaitForSingleObject(handles[i], 0)) {
238 if (SAFE_FD_ISSET(handle_slot_to_fd[i], rfds)) {
239 DWORD dwBytes;
240 intptr_t handle = (intptr_t) _get_osfhandle(
241 handle_slot_to_fd[i]);
242
243 if (PeekNamedPipe((HANDLE)handle, NULL, 0,
244 NULL, &dwBytes, NULL)) {
245 /* check to see if gdb pipe has data available */
246 if (dwBytes) {
247 FD_SET(handle_slot_to_fd[i], &aread);
248 retcode++;
249 }
250 } else {
251 FD_SET(handle_slot_to_fd[i], &aread);
252 retcode++;
253 }
254 }
255 if (SAFE_FD_ISSET(handle_slot_to_fd[i], wfds)) {
256 FD_SET(handle_slot_to_fd[i], &awrite);
257 retcode++;
258 }
259 if (SAFE_FD_ISSET(handle_slot_to_fd[i], efds)) {
260 FD_SET(handle_slot_to_fd[i], &aexcept);
261 retcode++;
262 }
263 }
264 }
265 }
266 }
267 } while (retcode == 0 && (ms_total == INFINITE || GetTickCount() < limit));
268
269 if (rfds)
270 *rfds = aread;
271 if (wfds)
272 *wfds = awrite;
273 if (efds)
274 *efds = aexcept;
275
276 return retcode;
277 }
278 #endif
279
280 #if defined HAVE_LIBUSB1 && !defined HAVE_LIBUSB_ERROR_NAME
281 #include <libusb.h>
282 /* Verbatim from git://git.libusb.org/libusb.git tag 1.0.9
283 * The libusb_error enum is compatible down to v0.9.1
284 */
285 const char *libusb_error_name(int error_code)
286 {
287 enum libusb_error error = error_code;
288 switch (error) {
289 case LIBUSB_SUCCESS:
290 return "LIBUSB_SUCCESS";
291 case LIBUSB_ERROR_IO:
292 return "LIBUSB_ERROR_IO";
293 case LIBUSB_ERROR_INVALID_PARAM:
294 return "LIBUSB_ERROR_INVALID_PARAM";
295 case LIBUSB_ERROR_ACCESS:
296 return "LIBUSB_ERROR_ACCESS";
297 case LIBUSB_ERROR_NO_DEVICE:
298 return "LIBUSB_ERROR_NO_DEVICE";
299 case LIBUSB_ERROR_NOT_FOUND:
300 return "LIBUSB_ERROR_NOT_FOUND";
301 case LIBUSB_ERROR_BUSY:
302 return "LIBUSB_ERROR_BUSY";
303 case LIBUSB_ERROR_TIMEOUT:
304 return "LIBUSB_ERROR_TIMEOUT";
305 case LIBUSB_ERROR_OVERFLOW:
306 return "LIBUSB_ERROR_OVERFLOW";
307 case LIBUSB_ERROR_PIPE:
308 return "LIBUSB_ERROR_PIPE";
309 case LIBUSB_ERROR_INTERRUPTED:
310 return "LIBUSB_ERROR_INTERRUPTED";
311 case LIBUSB_ERROR_NO_MEM:
312 return "LIBUSB_ERROR_NO_MEM";
313 case LIBUSB_ERROR_NOT_SUPPORTED:
314 return "LIBUSB_ERROR_NOT_SUPPORTED";
315 case LIBUSB_ERROR_OTHER:
316 return "LIBUSB_ERROR_OTHER";
317 }
318 return "**UNKNOWN**";
319 }
320 #endif

Linking to existing account procedure

If you already have an account and want to add another login method you MUST first sign in with your existing account and then change URL to read https://review.openocd.org/login/?link to get to this page again but this time it'll work for linking. Thank you.

SSH host keys fingerprints

1024 SHA256:YKx8b7u5ZWdcbp7/4AeXNaqElP49m6QrwfXaqQGJAOk gerrit-code-review@openocd.zylin.com (DSA)
384 SHA256:jHIbSQa4REvwCFG4cq5LBlBLxmxSqelQPem/EXIrxjk gerrit-code-review@openocd.org (ECDSA)
521 SHA256:UAOPYkU9Fjtcao0Ul/Rrlnj/OsQvt+pgdYSZ4jOYdgs gerrit-code-review@openocd.org (ECDSA)
256 SHA256:A13M5QlnozFOvTllybRZH6vm7iSt0XLxbA48yfc2yfY gerrit-code-review@openocd.org (ECDSA)
256 SHA256:spYMBqEYoAOtK7yZBrcwE8ZpYt6b68Cfh9yEVetvbXg gerrit-code-review@openocd.org (ED25519)
+--[ED25519 256]--+
|=..              |
|+o..   .         |
|*.o   . .        |
|+B . . .         |
|Bo. = o S        |
|Oo.+ + =         |
|oB=.* = . o      |
| =+=.+   + E     |
|. .=o   . o      |
+----[SHA256]-----+
2048 SHA256:0Onrb7/PHjpo6iVZ7xQX2riKN83FJ3KGU0TvI0TaFG4 gerrit-code-review@openocd.zylin.com (RSA)