Package: bladerf / 0.2017.12~rc1-2

0011-host-libbladeRF_test-win32-fixes-for-test_config_fil.patch Patch series | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
From 3095d995c838405df0dd6ab49357a7cae00d4a3d Mon Sep 17 00:00:00 2001
From: Rey Tucker <git@reytucker.us>
Date: Wed, 7 Feb 2018 18:33:46 -0500
Subject: [PATCH 11/15] host: libbladeRF_test: win32 fixes for test_config_file
 (#546)

* host: common: add mkdtemp implementation for Win32

Windows does not have mkdtemp, which is something used by at least one
test case. This is a simple-enough implementation thereof.

* host: common/host_config: add more defs on Win32

chdir, getcwd, rmdir, unlink, and mkdir are all somewhat special on
Win32, so they need special handling.

* host: test_config_file: fix builds on Win32
---
 host/common/include/host_config.h.in               |   6 +
 host/common/include/windows/mkdtemp.h              |  59 +++++
 host/common/src/windows/mkdtemp.c                  | 250 +++++++++++++++++++++
 .../test_config_file/CMakeLists.txt                |  30 ++-
 .../libbladeRF_test/test_config_file/src/main.c    |  64 +++++-
 5 files changed, 393 insertions(+), 16 deletions(-)
 create mode 100644 host/common/include/windows/mkdtemp.h
 create mode 100644 host/common/src/windows/mkdtemp.c

diff --git a/host/common/include/host_config.h.in b/host/common/include/host_config.h.in
index 19cae4db..daa4782d 100644
--- a/host/common/include/host_config.h.in
+++ b/host/common/include/host_config.h.in
@@ -245,6 +245,7 @@
 /* Rename a few functions and types */
 #include <windows.h>
 #include <io.h>
+#include <direct.h>
 #define usleep(x)      Sleep((x+999)/1000)
 
 /* Changes specific to Microsoft Visual Studio and its C89-compliant ways */
@@ -262,6 +263,11 @@
 #   define fileno      _fileno
 #   define strdup      _strdup
 #   define access      _access
+#   define chdir       _chdir
+#   define getcwd      _getcwd
+#   define rmdir       _rmdir
+#   define unlink      _unlink
+#   define mkdir(n, m) _mkdir(n)
 
 /* ssize_t lives elsewhere */
 #   include <BaseTsd.h>
diff --git a/host/common/include/windows/mkdtemp.h b/host/common/include/windows/mkdtemp.h
new file mode 100644
index 00000000..0f8bbab0
--- /dev/null
+++ b/host/common/include/windows/mkdtemp.h
@@ -0,0 +1,59 @@
+/*
+ * This file is part of the bladeRF project:
+ *   http://www.github.com/Nuand/bladeRF
+ *
+ * Copyright (c) 2018 Nuand LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+#ifndef WIN_MKDTEMP_H_
+#define WIN_MKDTEMP_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#ifndef WIN32
+#error "This file is intended for use with WIN32 systems only."
+#endif
+
+/**
+ * Create a unique temporary directory
+ *
+ * Functionally equivalent to glibc's mkdtemp.
+ *
+ * The last six characters of template must be XXXXXX, and these are replaced
+ * with a string that makes the directory name unique. The directory is then
+ * created with 0700 permissions.
+ *
+ * The `template` parameter is modified in-place, and thus it should be
+ * declared as a character array and not a string constant.
+ *
+ * @param   template    filename template, ending in "XXXXXX"
+ *
+ * @returns pointer to modified `template` string on success, NULL on failure,
+ *          in which case `errno` is set appropriately.
+ */
+char *mkdtemp(char *template);
+
+#ifdef __cplusplus
+} /* extern "C" */
+#endif
+
+#endif
diff --git a/host/common/src/windows/mkdtemp.c b/host/common/src/windows/mkdtemp.c
new file mode 100644
index 00000000..0e2f804c
--- /dev/null
+++ b/host/common/src/windows/mkdtemp.c
@@ -0,0 +1,250 @@
+/*
+ * This file is part of the bladeRF project:
+ *   http://www.github.com/Nuand/bladeRF
+ *
+ * Copyright (c) 2018 Nuand LLC
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+// _CRT_RAND_S must be defined before including stdlib.h, to enable rand_s
+#define _CRT_RAND_S
+#include <stdlib.h>
+
+#ifdef WINDOWS_MKDTEMP_TEST_SUITE
+// Running in standalone test mode
+
+#include <stdbool.h>
+#include <stdio.h>
+#define __debug(...) fprintf(stderr, __VA_ARGS__)
+
+#ifndef WIN32
+// Running standalone test on non-Windows OS
+#include <time.h>
+#include <unistd.h>
+#define errno_t int
+errno_t rand_s(unsigned int *randomValue)
+{
+    *randomValue = rand();
+    return 0;
+}
+#endif  // WIN32
+
+#else
+// Building as part of a library
+
+#ifndef WIN32
+#error "This file is intended for use with WIN32 systems only."
+#endif  // WIN32
+
+#define __debug(...)
+
+#endif  // WINDOWS_MKDTEMP_TEST_SUITE
+
+#include <errno.h>
+#include <string.h>
+#include <sys/stat.h>
+#include "host_config.h"
+
+// The concepts of F_OK and S_IRWXU do not exist on Win32
+#ifndef F_OK
+#define F_OK 0
+#endif
+
+#ifndef S_IRWXU
+#define S_IRWXU 00700
+#endif
+
+/* h/t https://stackoverflow.com/a/14598879 */
+static int random_number(int min_num, int max_num)
+{
+    int result = 0, low_num = 0, hi_num = 0;
+    unsigned int randomValue;
+
+    if (min_num < max_num) {
+        low_num = min_num;
+        hi_num  = max_num + 1;  // include max_num in output
+    } else {
+        low_num = max_num + 1;  // include max_num in output
+        hi_num  = min_num;
+    }
+
+    // Use rand_s() on Windows, so that we don't have to deal with srand()
+    errno_t err = rand_s(&randomValue);
+    if (0 != err) {
+        return -1;
+    }
+
+    result = (randomValue % (hi_num - low_num)) + low_num;
+    return result;
+}
+
+char *mkdtemp(char *template)
+{
+    size_t const TEMPL_LEN = 6;
+    char const TEMPL_CHAR  = 'X';
+
+    if (strlen(template) <= TEMPL_LEN) {
+        // template is too short
+        errno = EINVAL;
+        goto error;
+    }
+
+    // Loop through the end of the template, replacing 'X' with random char
+    for (size_t i = strlen(template) - TEMPL_LEN; i < strlen(template); ++i) {
+        // The last TEMPL_LEN characters MUST be 'X'
+        if (template[i] != TEMPL_CHAR) {
+            errno = EINVAL;
+            goto error;
+        }
+
+        // Pick a random letter
+        if (random_number(0, 1)) {
+            template[i] = (char)random_number('A', 'Z');
+        } else {
+            template[i] = (char)random_number('a', 'z');
+        }
+    }
+
+    // Error out if the file already exists
+    if (access(template, F_OK) != -1) {
+        __debug("%s: failed: %s exists\n", __FUNCTION__, template);
+        errno = EEXIST;
+        goto error;
+    }
+
+    // Try to create the directory...
+    if (0 != mkdir(template, S_IRWXU)) {
+        int errsv = errno;
+        __debug("%s: mkdir() failed: %s\n", __FUNCTION__, strerror(errsv));
+        goto error;
+    }
+
+    // Success!
+    errno = 0;
+    return template;
+
+error:
+    return NULL;
+}
+
+#ifdef WINDOWS_MKDTEMP_TEST_SUITE
+/**
+ * These functions are intended to verify proper operation of the test suite.
+ */
+static bool test(char *template, bool expect_success)
+{
+    char *rv = mkdtemp(template);
+
+    if (NULL == rv) {
+        int errsv = errno;
+        printf("%s: mkdtemp failed: %s\n", __FUNCTION__, strerror(errsv));
+        return (false == expect_success);
+    } else {
+        printf("%s: mkdtemp created: %s\n", __FUNCTION__, rv);
+
+        if (0 != rmdir(rv)) {
+            int errsv = errno;
+            printf("%s: rmdir failed: %s\n", __FUNCTION__, strerror(errsv));
+        }
+
+        return (true == expect_success);
+    }
+}
+
+int main(int argc, char *argv[])
+{
+#ifndef WIN32
+    srand(time(NULL));
+#endif  // WIN32
+
+    int success = 0, failure = 0;
+
+    // Normal: should pass
+    char template1[] = "/tmp/asdf.XXXXXX";
+    if (test(template1, true)) {
+        printf("*** Test case 1: PASS\n");
+        ++success;
+    } else {
+        printf("*** Test case 1: FAIL\n");
+        ++failure;
+    }
+
+    // Too short: should fail
+    char template2[] = "XXXXXX";
+    if (test(template2, false)) {
+        printf("*** Test case 2: PASS\n");
+        ++success;
+    } else {
+        printf("*** Test case 2: FAIL\n");
+        ++failure;
+    }
+
+    // Not enough replacement Xs: should fail
+    char template3[] = "/tmp/asdf.XXXXX";
+    if (test(template3, false)) {
+        printf("*** Test case 3: PASS\n");
+        ++success;
+    } else {
+        printf("*** Test case 3: FAIL\n");
+        ++failure;
+    }
+
+    // Make sure it only replaces the end: should pass
+    char template4[] = "/tmp/asdfXXXXXX.XXXXXX";
+    if (test(template4, true)) {
+        printf("*** Test case 4: PASS\n");
+        ++success;
+    } else {
+        printf("*** Test case 4: FAIL\n");
+        ++failure;
+    }
+
+    // Really long: should fail
+    char template5[] = "/tmp/asdfaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+                       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+                       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+                       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+                       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+                       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+                       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
+                       "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaXXXXXX";
+    if (test(template5, false)) {
+        printf("*** Test case 5: PASS\n");
+        ++success;
+    } else {
+        printf("*** Test case 5: FAIL\n");
+        ++failure;
+    }
+
+    // Unwriteable path: should fail
+    char template6[] = "/asdfkjavblkjadv/asdf.XXXX";
+    if (test(template6, false)) {
+        printf("*** Test case 6: PASS\n");
+        ++success;
+    } else {
+        printf("*** Test case 6: FAIL\n");
+        ++failure;
+    }
+
+    printf("TEST SUMMARY: Success=%d, Failure=%d\n", success, failure);
+
+    return failure;
+}
+#endif  // WINDOWS_MKDTEMP_TEST_SUITE
diff --git a/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt b/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt
index 96499b27..548a65c4 100644
--- a/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt
+++ b/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt
@@ -1,7 +1,7 @@
 cmake_minimum_required(VERSION 2.8)
 project(libbladeRF_test_config_file C)
 
-set(INCLUDES
+set(TEST_CONFIG_FILE_INCLUDES
     ${libbladeRF_SOURCE_DIR}/include
     ${libbladeRF_SOURCE_DIR}/src
     ${BLADERF_FPGA_COMMON_INCLUDE_DIR}
@@ -10,10 +10,6 @@ set(INCLUDES
     ${CMAKE_CURRENT_SOURCE_DIR}/../common/include
 )
 
-if(MSVC)
-    set(INCLUDES ${INCLUDES} ${MSVC_C99_INCLUDES})
-endif()
-
 set(SRC
     src/main.c
     ../common/src/test_common.c
@@ -35,9 +31,29 @@ set(SRC
     ${BLADERF_FPGA_COMMON_SOURCE_DIR}/lms.c
 )
 
+set(TEST_CONFIG_FILE_LIBS libbladerf_shared)
+
+if(MSVC)
+    find_package(LibPThreadsWin32 REQUIRED)
+    set(TEST_CONFIG_FILE_INCLUDES
+        ${TEST_CONFIG_FILE_INCLUDES}
+        ${MSVC_C99_INCLUDES}
+        ${LIBPTHREADSWIN32_INCLUDE_DIRS}
+    )
+    set(SRC
+        ${SRC}
+        ${BLADERF_HOST_COMMON_SOURCE_DIR}/windows/clock_gettime.c
+        ${BLADERF_HOST_COMMON_SOURCE_DIR}/windows/mkdtemp.c
+    )
+    set(TEST_CONFIG_FILE_LIBS
+        ${TEST_CONFIG_FILE_LIBS}
+        ${LIBPTHREADSWIN32_LIBRARIES}
+    )
+endif()
+
 add_definitions(-DLIBBLADERF_SEARCH_PREFIX="${CMAKE_INSTALL_PREFIX}")
 add_definitions(-DLOGGING_ENABLED=1)
 
-include_directories(${INCLUDES})
+include_directories(${TEST_CONFIG_FILE_INCLUDES})
 add_executable(libbladeRF_test_config_file ${SRC})
-target_link_libraries(libbladeRF_test_config_file libbladerf_shared)
+target_link_libraries(libbladeRF_test_config_file ${TEST_CONFIG_FILE_LIBS})
diff --git a/host/libraries/libbladeRF_test/test_config_file/src/main.c b/host/libraries/libbladeRF_test/test_config_file/src/main.c
index 803549a9..e15b2316 100644
--- a/host/libraries/libbladeRF_test/test_config_file/src/main.c
+++ b/host/libraries/libbladeRF_test/test_config_file/src/main.c
@@ -47,13 +47,22 @@
 #include "config.h"
 #include "test_common.h"
 
+#ifdef WIN32
+#include "mkdtemp.h"
+#define MAX_FILENAME_LEN 256
+static char const DIR_DELIM[] = "\\";
+#else
+size_t const MAX_FILENAME_LEN = 256;
+static char const DIR_DELIM[] = "/";
+#endif  // WIN32
+
 /**
  * Structure for storing test state and conditions
  */
 typedef struct {
     struct bladerf_devinfo ident; /**< devinfo struct for this test */
     bladerf_fpga_size fpga_size;  /**< FPGA size */
-    unsigned int frequency;       /**< Expected frequency */
+    uint64_t frequency;       /**< Expected frequency */
 } test_state;
 
 /**
@@ -73,7 +82,7 @@ static const test_state states[] = {
             FIELD_INIT(.instance, 0)
         }),
         FIELD_INIT(.fpga_size, BLADERF_FPGA_40KLE),
-        FIELD_INIT(.frequency, 2e9),
+        FIELD_INIT(.frequency, 2000000000),
     },
     {
         FIELD_INIT(.ident, {
@@ -84,7 +93,7 @@ static const test_state states[] = {
             FIELD_INIT(.instance, 1)
         }),
         FIELD_INIT(.fpga_size, BLADERF_FPGA_115KLE),
-        FIELD_INIT(.frequency, 1.8e9),
+        FIELD_INIT(.frequency, 1800000000),
     },
     {
         FIELD_INIT(.ident, {
@@ -95,7 +104,7 @@ static const test_state states[] = {
             FIELD_INIT(.instance, 2)
         }),
         FIELD_INIT(.fpga_size, BLADERF_FPGA_40KLE),
-        FIELD_INIT(.frequency, 1.6e9),
+        FIELD_INIT(.frequency, 1600000000),
     },
 };
 // clang-format on
@@ -287,10 +296,39 @@ void bladerf_close(struct bladerf *dev)
  */
 int main(int argc, char *argv[])
 {
-    size_t const MAX_FILENAME_LEN = 256;
     char filename[MAX_FILENAME_LEN];
-    char tmpdir[] = "/tmp/libbladeRF_test_config_file.XXXXXX";
-    bool result   = true;
+    char tmpdir[MAX_FILENAME_LEN];
+    char tmpdirname[] = "libbladeRF_test_config_file.XXXXXX";
+    char *tmpbase     = NULL;
+    bool result       = true;
+    int rv;
+
+    /* Get the TEMP variable from the environment, if it exists */
+    tmpbase = getenv("TEMP");
+
+    if (NULL == tmpbase) {
+        /* It doesn't, so let's use the current working directory */
+        tmpbase = malloc(MAX_FILENAME_LEN);
+
+        if (NULL == tmpbase) {
+            perror("malloc failed");
+            exit(EXIT_FAILURE);
+        }
+
+        if (NULL == getcwd(tmpbase, MAX_FILENAME_LEN - 1)) {
+            perror("getcwd failed");
+            exit(EXIT_FAILURE);
+        }
+    }
+
+    /* Concatenate directory names */
+    rv = snprintf(tmpdir, MAX_FILENAME_LEN - 1, "%s%s%s", tmpbase, DIR_DELIM,
+                  tmpdirname);
+
+    if (rv < 0 || rv >= (int)(MAX_FILENAME_LEN - 1)) {
+        perror("snprintf failed");
+        exit(EXIT_FAILURE);
+    }
 
     /* Create a temporary directory for testing */
     if (NULL == mkdtemp(tmpdir)) {
@@ -299,8 +337,10 @@ int main(int argc, char *argv[])
     }
 
     /* Create a sample config file there */
-    if (0 > snprintf(filename, MAX_FILENAME_LEN - 1, "%s/%s", tmpdir,
-                     "bladeRF.conf")) {
+    rv = snprintf(filename, MAX_FILENAME_LEN - 1, "%s%s%s", tmpdir, DIR_DELIM,
+                  "bladeRF.conf");
+
+    if (rv < 0 || rv >= (int)(MAX_FILENAME_LEN - 1)) {
         perror("snprintf failed");
         exit(EXIT_FAILURE);
     }
@@ -363,6 +403,12 @@ int main(int argc, char *argv[])
         exit(EXIT_FAILURE);
     }
 
+    /* Change working directory */
+    if (0 > chdir(tmpbase)) {
+        perror("chdir failed");
+        exit(EXIT_FAILURE);
+    }
+
     if (0 > rmdir(tmpdir)) {
         perror("rmdir failed");
         exit(EXIT_FAILURE);
-- 
2.11.0