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 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753
|
From 254e831d2499714c2d8af95de50e9a18b4f1cab7 Mon Sep 17 00:00:00 2001
From: Rey Tucker <git@reytucker.us>
Date: Thu, 1 Feb 2018 15:04:53 -0500
Subject: [PATCH 08/15] host: libbladeRF: Add documentation and test program
for config file handling (#539)
* host: libbladeRF: fix typo of "bandwidth" cfg file key
* host: libbladeRF: add documentation for config files
* host: libbladeRF: config.c: minor typo fix
* host: libbladeRF_test: add test case for config file parsing
Creates a surrogate config file with a few different scope restrictions,
then makes sure the configuration is applied appropriately in the end.
---
.../libbladeRF/doc/doxygen/configfile.dox | 185 ++++++++++
host/libraries/libbladeRF/doc/doxygen/mainpage.dox | 3 +
.../libbladeRF/doc/examples/example_bladeRF.conf | 45 +++
host/libraries/libbladeRF/src/config.c | 4 +-
host/libraries/libbladeRF_test/CMakeLists.txt | 1 +
.../test_config_file/CMakeLists.txt | 43 +++
.../libbladeRF_test/test_config_file/src/main.c | 375 +++++++++++++++++++++
7 files changed, 654 insertions(+), 2 deletions(-)
create mode 100644 host/libraries/libbladeRF/doc/doxygen/configfile.dox
create mode 100644 host/libraries/libbladeRF/doc/examples/example_bladeRF.conf
create mode 100644 host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt
create mode 100644 host/libraries/libbladeRF_test/test_config_file/src/main.c
diff --git a/host/libraries/libbladeRF/doc/doxygen/configfile.dox b/host/libraries/libbladeRF/doc/doxygen/configfile.dox
new file mode 100644
index 00000000..782a5c00
--- /dev/null
+++ b/host/libraries/libbladeRF/doc/doxygen/configfile.dox
@@ -0,0 +1,185 @@
+/**
+\page configfile Configuration File
+
+libbladeRF supports an optional configuration file. This feature is useful for
+specifying preferred default settings, or for configuring options that aren't
+yet supported by a third-party application. If found, the configuration file is
+parsed immediately after a bladeRF is opened and initialized by libbladeRF.
+
+A configuration file may be named either `bladeRF.conf` (preferred) or
+`bladerf.conf`. libbladeRF uses the same search path it uses for finding FPGA
+images and calibration tables, defined by file_find() in file_ops.c:
+
+<b>Linux and OSX:</b>
+<ul>
+ <li>The current working directory</li>
+ <li>The directory containing the program being executed</li>
+ <li><code>~/.config/Nuand/bladeRF</code></li>
+ <li><code>~/.Nuand/bladeRF</code></li>
+ <li><code>/etc/Nuand/bladeRF</code></li>
+ <li><code>/usr/share/Nuand/bladeRF</code></li>
+</ul>
+
+<b>Windows:</b>
+<ul>
+ <li>The current working directory</li>
+ <li>The directory containing the program being executed</li>
+ <li><code>C:\\Users\\USERNAME\\AppData\\Roaming\\Nuand\\bladeRF</code>
+ (via <a class="el" href="https://msdn.microsoft.com/en-us/library/windows/desktop/dd378457%28v=vs.85%29.aspx">FOLDERID_RoamingAppData</a>)</li>
+ <li>The installation path, via <code>HKEY_LOCAL_MACHINE\\Software\\Nuand LLC\\Path</code></li>
+</ul>
+
+<strong>Note:</strong> libbladeRF will only find and process the first
+configuration file it finds. Any other files appearing later in the search path
+will be ignored.
+
+A valid configuration file may contain:
+
+<ul>
+ <li><em>Comments</em> are lines starting with a `#`, and are ignored.
+ <li><em>Restrictions</em> are lines starting with a `[` and ending with a
+ `]`, and are used to apply a scope to a subset of options.</li>
+ <li><em>Options</em> themselves are specified by an option key, a space,
+ and then a value.</li>
+</ul>
+
+The below example loads an FPGA image from `/home/user/hostedx40.rbf`, set the
+trim DAC value to 592, and set the frequency to 2.4 GHz, when <em>any</em>
+bladeRF is opened:
+
+\snippet example_bladeRF.conf basic_usage
+
+<hr>
+<h2>Restrictions</h2>
+
+Options may be restricted to bladeRFs that match a particular description. A
+restriction applies to all options following it, until the next restriction (or
+the end of the file). When iterating through the configuration file, a given
+option will be applied if the target bladeRF matches the most recent restriction
+(or if there are no restrictions). If it does not, the option will be silently
+ignored.
+
+<h3>Matching by FPGA size</h3>
+
+The following restrictions are based on the FPGA size on the bladeRF board:
+
+<ul>
+ <li>`[x40]` - Only boards matching ::BLADERF_FPGA_40KLE</li>
+ <li>`[x115]` - Only boards matching ::BLADERF_FPGA_115KLE</li>
+</ul>
+
+The following example would program the FPGA with `/home/user/hostedx40.rbf`
+when initializing a bladeRF x40, but would program it with
+`/home/user/hostedx115.rbf` if the bladeRF is an x115 model.
+
+\snippet example_bladeRF.conf match_fpga
+
+<h3>Matching by Device Identifier String</h3>
+
+A restriction of `[<device identifier string>]` will limit the following options
+to bladeRFs matching that string. Under the hood, any square-bracketed string
+that is not otherwise recognized gets passed to bladerf_devstr_matches() as a
+possible device identifier string.
+
+The general form of a device identifier string is:
+
+ <backend>:[device=<bus>:<addr>] [instance=<n>] [serial=<serial>]
+
+<strong>Note:</strong> Please see bladerf_open() for a complete description of
+device identifier strings.
+
+The below example is similar to the above examples. However, it will apply
+different trimdac values to two specific boards, which are identified by serial
+number.
+
+\snippet example_bladeRF.conf match_devstr
+
+<h3>Matching any device</h3>
+
+Finally, a wildcard (`[*]`) or empty (`[]`) restriction will match any device,
+and may be used to "cancel" a previous restriction.
+
+<hr>
+<h2>Options</h2>
+
+Options (unless ignored due to a restriction) will be applied sequentially,
+immediately following initialization of the bladeRF.
+
+<strong>Note:</strong> If the same option key is specified more than once, it
+will be applied more than once; e.g. specifying `frequency 2.4G` and then
+`frequency 400M` will first tune the frequency to 2.4 GHz, and then to 400 MHz.
+
+<ul>
+ <li><b>`fpga <rbf_filename>`</b></li>
+
+ Loads the specified FPGA image via bladerf_load_fpga().
+
+ <li><b>`frequency <Hz>`</b></li>
+
+ Sets the bladeRF's RX and TX frequencies to a given frequency in Hz.
+ Suffixes are supported.
+
+ This option causes libbladeRF to call bladerf_set_frequency() on both
+ ::BLADERF_MODULE_RX and ::BLADERF_MODULE_TX.
+
+ <li><b>`samplerate <rate>`</b></li>
+
+ Sets the sample rate for the RX ADC and TX to the given rate in samples
+ per second. Suffixes are supported.
+
+ This option causes libbladeRF to call bladerf_set_rational_sample_rate()
+ on both ::BLADERF_MODULE_RX and ::BLADERF_MODULE_TX.
+
+ <li><b>`bandwidth <Hz>`</b></li>
+
+ Sets the RF bandwidth to a given value in Hz. Suffixes are supported.
+
+ This option causes libbladeRF to call bladerf_set_bandwidth() on both
+ ::BLADERF_MODULE_RX and ::BLADERF_MODULE_TX.
+
+ <li><b>`agc <bool>`</b></li>
+
+ Enables or disables automatic gain control mode, via
+ bladerf_set_gain_mode().
+
+ A true value (e.g. `on`) selects ::BLADERF_GAIN_AUTOMATIC, while a false
+ value (e.g. `off`) selects ::BLADERF_GAIN_MANUAL.
+
+ <li><b>`gpio <uint>`</b></li>
+
+ Sets the GPIO register to a given value, using
+ bladerf_config_gpio_write().
+
+ <li><b>`sampling <internal|external>`</b></li>
+
+ Sets the sampling source by calling bladerf_set_sampling().
+
+ Choices are `internal` or `external`.
+
+ <strong>See ::bladerf_sampling for important information.</strong>
+
+ <li><b>`trimdac <uint>`</b></li>
+
+ Sets the trim DAC value via bladerf_dac_write().
+
+ <li><b>`vctcxo_tamer <disabled|1pps|10mhz>`</b></li>
+
+ Sets the VCTCXO tamer method using bladerf_set_vctcxo_tamer_mode().
+
+ Choices are `disabled`, `1PPS`, and `10MHz`.
+
+ <strong>See ::bladerf_vctcxo_tamer_mode for important
+ information.</strong>
+
+</ul>
+
+<h3>Suffixes</h3>
+Where noted above, the following suffixes are supported:
+
+<ul>
+ <li>`G`, `GHz`: multiply by 1000*1000*1000</li>
+ <li>`M`, `MHz`: multiply by 1000*1000</li>
+ <li>`k`, `kHz`: multiply by 1000</li>
+</ul>
+
+*/
diff --git a/host/libraries/libbladeRF/doc/doxygen/mainpage.dox b/host/libraries/libbladeRF/doc/doxygen/mainpage.dox
index 4c6d7765..87b7da46 100644
--- a/host/libraries/libbladeRF/doc/doxygen/mainpage.dox
+++ b/host/libraries/libbladeRF/doc/doxygen/mainpage.dox
@@ -101,6 +101,9 @@ The following pages provide examples and more detailed usage information:
<li>
<a class="el" href="envvars.html">Environment variables</a>
</li>
+ <li>
+ <a class="el" href="configfile.html">Configuration file</a>
+ </li>
</ul>
*/
diff --git a/host/libraries/libbladeRF/doc/examples/example_bladeRF.conf b/host/libraries/libbladeRF/doc/examples/example_bladeRF.conf
new file mode 100644
index 00000000..b13e66cb
--- /dev/null
+++ b/host/libraries/libbladeRF/doc/examples/example_bladeRF.conf
@@ -0,0 +1,45 @@
+# [basic_usage]
+
+# Load the hostedx40.rbf FPGA image, set the trim DAC, and
+# configure the frequency
+fpga /home/user/hostedx40.rbf
+trimdac 592
+frequency 2.4G
+
+# [basic_usage]
+
+###
+
+# [match_fpga]
+
+trimdac 592
+frequency 2.4G
+
+[x40]
+fpga /home/user/hostedx40.rbf
+
+[x115]
+fpga /home/user/hostedx115.rbf
+
+# [match_fpga]
+
+###
+
+# [match_devstr]
+
+trimdac 592
+frequency 2.4G
+
+[x40]
+fpga /home/user/hostedx40.rbf
+
+[x115]
+fpga /home/user/hostedx115.rbf
+
+[*:serial=febe585efb9e3f36c87c3e4db1b2adee]
+trimdac 440
+
+[*:serial=07dea50449172fcd63e711d7e5fe9bb4]
+trimdac 498
+
+# [match_devstr]
diff --git a/host/libraries/libbladeRF/src/config.c b/host/libraries/libbladeRF/src/config.c
index 1ddd2712..e50617ac 100644
--- a/host/libraries/libbladeRF/src/config.c
+++ b/host/libraries/libbladeRF/src/config.c
@@ -181,7 +181,7 @@ static int apply_config_options(struct bladerf *dev, struct config_options opt)
status = bladerf_set_rational_sample_rate(dev, BLADERF_MODULE_TX,
&rate, &actual);
- } else if (!strcasecmp(opt.key, "bandwdith")) {
+ } else if (!strcasecmp(opt.key, "bandwidth")) {
bw = str2uint_suffix( opt.value,
BLADERF_BANDWIDTH_MIN, BLADERF_BANDWIDTH_MAX,
freq_suffixes, NUM_FREQ_SUFFIXES, &ok);
@@ -242,7 +242,7 @@ static int apply_config_options(struct bladerf *dev, struct config_options opt)
}
if (status < 0)
- log_warning("Error massage for option (%s) on line %d:\n%s\n",
+ log_warning("Error message for option (%s) on line %d:\n%s\n",
opt.key, opt.lineno, bladerf_strerror(status));
return status;
diff --git a/host/libraries/libbladeRF_test/CMakeLists.txt b/host/libraries/libbladeRF_test/CMakeLists.txt
index c8c7bd98..48f93d94 100644
--- a/host/libraries/libbladeRF_test/CMakeLists.txt
+++ b/host/libraries/libbladeRF_test/CMakeLists.txt
@@ -3,6 +3,7 @@ cmake_minimum_required(VERSION 2.8)
add_subdirectory(test_async)
add_subdirectory(test_bootloader_recovery)
add_subdirectory(test_c)
+add_subdirectory(test_config_file)
add_subdirectory(test_cpp)
add_subdirectory(test_ctrl)
add_subdirectory(test_freq_hop)
diff --git a/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt b/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt
new file mode 100644
index 00000000..96499b27
--- /dev/null
+++ b/host/libraries/libbladeRF_test/test_config_file/CMakeLists.txt
@@ -0,0 +1,43 @@
+cmake_minimum_required(VERSION 2.8)
+project(libbladeRF_test_config_file C)
+
+set(INCLUDES
+ ${libbladeRF_SOURCE_DIR}/include
+ ${libbladeRF_SOURCE_DIR}/src
+ ${BLADERF_FPGA_COMMON_INCLUDE_DIR}
+ ${BLADERF_FW_COMMON_INCLUDE_DIR}
+ ${BLADERF_HOST_COMMON_INCLUDE_DIRS}
+ ${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
+ ${libbladeRF_SOURCE_DIR}/src/bladerf_priv.c
+ ${libbladeRF_SOURCE_DIR}/src/config.c
+ ${libbladeRF_SOURCE_DIR}/src/dc_cal_table.c
+ ${libbladeRF_SOURCE_DIR}/src/file_ops.c
+ ${libbladeRF_SOURCE_DIR}/src/flash.c
+ ${libbladeRF_SOURCE_DIR}/src/flash_fields.c
+ ${libbladeRF_SOURCE_DIR}/src/fpga.c
+ ${libbladeRF_SOURCE_DIR}/src/si5338.c
+ ${libbladeRF_SOURCE_DIR}/src/tuning.c
+ ${libbladeRF_SOURCE_DIR}/src/version_compat.c
+ ${libbladeRF_SOURCE_DIR}/src/xb.c
+ ${BLADERF_HOST_COMMON_SOURCE_DIR}/conversions.c
+ ${BLADERF_HOST_COMMON_SOURCE_DIR}/log.c
+ ${BLADERF_HOST_COMMON_SOURCE_DIR}/parse.c
+ ${BLADERF_FPGA_COMMON_SOURCE_DIR}/band_select.c
+ ${BLADERF_FPGA_COMMON_SOURCE_DIR}/lms.c
+)
+
+add_definitions(-DLIBBLADERF_SEARCH_PREFIX="${CMAKE_INSTALL_PREFIX}")
+add_definitions(-DLOGGING_ENABLED=1)
+
+include_directories(${INCLUDES})
+add_executable(libbladeRF_test_config_file ${SRC})
+target_link_libraries(libbladeRF_test_config_file libbladerf_shared)
diff --git a/host/libraries/libbladeRF_test/test_config_file/src/main.c b/host/libraries/libbladeRF_test/test_config_file/src/main.c
new file mode 100644
index 00000000..803549a9
--- /dev/null
+++ b/host/libraries/libbladeRF_test/test_config_file/src/main.c
@@ -0,0 +1,375 @@
+/*
+ * This file is part of the bladeRF project:
+ * http://www.github.com/nuand/bladeRF
+ *
+ * Copyright (C) 2017 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.
+ */
+
+/**
+ * Test program for libbladeRF configuration file parsing
+ *
+ * Creates a config file with a few different scope restrictions, then
+ * makes sure the proper activity happens.
+ *
+ * Currently, it tests:
+ * Option keys:
+ * frequency
+ * Restrictions:
+ * none
+ * x40
+ * x115
+ * serial number
+ */
+
+#include <libbladeRF.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "bladerf_priv.h"
+#include "config.h"
+#include "test_common.h"
+
+/**
+ * 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 */
+} test_state;
+
+/**
+ * Test cases
+ *
+ * .ident and .fpga_size are inputs, and the remainder are expected end
+ * states for their respective keys.
+ */
+// clang-format off
+static const test_state states[] = {
+ {
+ FIELD_INIT(.ident, {
+ FIELD_INIT(.backend, BLADERF_BACKEND_DUMMY),
+ FIELD_INIT(.serial, "9f9f90dbe3e5ee1218c86b8839db1995"),
+ FIELD_INIT(.usb_bus, 1),
+ FIELD_INIT(.usb_addr, 2),
+ FIELD_INIT(.instance, 0)
+ }),
+ FIELD_INIT(.fpga_size, BLADERF_FPGA_40KLE),
+ FIELD_INIT(.frequency, 2e9),
+ },
+ {
+ FIELD_INIT(.ident, {
+ FIELD_INIT(.backend, BLADERF_BACKEND_DUMMY),
+ FIELD_INIT(.serial, "df34f5f71a4e812327ac9b04538386af"),
+ FIELD_INIT(.usb_bus, 1),
+ FIELD_INIT(.usb_addr, 3),
+ FIELD_INIT(.instance, 1)
+ }),
+ FIELD_INIT(.fpga_size, BLADERF_FPGA_115KLE),
+ FIELD_INIT(.frequency, 1.8e9),
+ },
+ {
+ FIELD_INIT(.ident, {
+ FIELD_INIT(.backend, BLADERF_BACKEND_DUMMY),
+ FIELD_INIT(.serial, "742330d6617e449e7bb460e802d50701"),
+ FIELD_INIT(.usb_bus, 2),
+ FIELD_INIT(.usb_addr, 1),
+ FIELD_INIT(.instance, 2)
+ }),
+ FIELD_INIT(.fpga_size, BLADERF_FPGA_40KLE),
+ FIELD_INIT(.frequency, 1.6e9),
+ },
+};
+// clang-format on
+
+
+/**
+ * Contents of test configuration file...
+ */
+static char const *test_config[] = {
+ "frequency 1.0G", // All boards: set frequency to 1.0 GHz
+ "frequency 1.4G", // All boards: set frequency to 1.4 GHz
+ "[x40]",
+ "frequency 1.6G", // If x40 FPGA: set frequency to 1.6 GHz
+ "[x115]",
+ "frequency 1.8G", // If x115 FPGA: set frequency to 1.8 GHz
+ "[*:serial=9f9f90dbe3e5ee1218c86b8839db1995]",
+ "frequency 2.0G", // If serial 9f9f90db..., set frequency to 2.0 GHz
+};
+
+/**
+ * Global test history variable, used by {update,check}_test_state
+ */
+static test_state HISTORY[ARRAY_SIZE(states)];
+
+/**
+ * Writes a config file
+ *
+ * @param[in] filename Filename to write to
+ * @param[in] config Array of lines to write to config file
+ * @param[in] len Number of elements in config
+ *
+ * @return 0 on success, or value from \ref RETCODES list on failure
+ */
+int create_config(char const *filename, char const **config, size_t len)
+{
+ FILE *f = fopen(filename, "w");
+ if (NULL == f) {
+ perror("fopen failed");
+ return BLADERF_ERR_IO;
+ }
+
+ for (size_t i = 0; i < len; ++i) {
+ fprintf(f, "%s\n", config[i]);
+ }
+
+ fclose(f);
+
+ printf("Created config file: %s\n", filename);
+
+ return 0;
+}
+
+/**
+ * Creates a fake test bladeRF device
+ *
+ * @param[out] device Update with device handle on success
+ * @param[in] devinfo Device specification. If NULL, any available
+ * device will be opened.
+ *
+ * @return 0 on success, or value from \ref RETCODES list on failure
+ */
+int create_test_device(struct bladerf **device,
+ struct bladerf_devinfo const *devinfo)
+{
+ struct bladerf *dev;
+
+ dev = (struct bladerf *)calloc(1, sizeof(struct bladerf));
+ if (dev == NULL) {
+ return BLADERF_ERR_MEM;
+ }
+
+ MUTEX_INIT(&dev->ctrl_lock);
+ MUTEX_INIT(&dev->sync_lock[BLADERF_MODULE_RX]);
+ MUTEX_INIT(&dev->sync_lock[BLADERF_MODULE_TX]);
+
+ dev->fpga_version.describe = calloc(1, BLADERF_VERSION_STR_MAX + 1);
+ if (dev->fpga_version.describe == NULL) {
+ free(dev);
+ return BLADERF_ERR_MEM;
+ }
+
+ dev->fw_version.describe = calloc(1, BLADERF_VERSION_STR_MAX + 1);
+ if (dev->fw_version.describe == NULL) {
+ free((void *)dev->fpga_version.describe);
+ free(dev);
+ return BLADERF_ERR_MEM;
+ }
+
+ dev->capabilities = 0;
+ dev->ident = *devinfo;
+
+ *device = dev;
+
+ return 0;
+}
+
+/**
+ * Update the test state for a given device and key
+ *
+ * @param[in] dev Device handle
+ * @param[in] key Key to update
+ * @param[in] value New value
+ *
+ * @return 0 on success, or value from \ref RETCODES list on failure
+ */
+int update_test_state(struct bladerf *dev, char const *key, uint64_t value)
+{
+ if (NULL == dev || NULL == key) {
+ return BLADERF_ERR_INVAL;
+ }
+
+ for (size_t i = 0; i < ARRAY_SIZE(states); ++i) {
+ test_state const *t = &states[i];
+
+ if (bladerf_devinfo_matches(&dev->ident, &t->ident)) {
+ if (0 == strcmp(key, "frequency")) {
+ HISTORY[i].frequency = value;
+ return 0;
+ }
+
+ return BLADERF_ERR_INVAL;
+ }
+ }
+
+ return BLADERF_ERR_NODEV;
+}
+
+/**
+ * Checks the current test value for a given device and key against the
+ * expected values.
+ *
+ * @param[in] dev Device handle
+ * @param[in] key Key to test
+ *
+ * @return true if the value is as expected, false otherwise
+ */
+bool check_test_state(struct bladerf *dev, char const *key)
+{
+ if (NULL == dev || NULL == key) {
+ return false;
+ }
+
+ for (size_t i = 0; i < ARRAY_SIZE(states); ++i) {
+ test_state const *t = &states[i];
+
+ if (bladerf_devinfo_matches(&dev->ident, &t->ident)) {
+ if (0 == strcmp(key, "frequency")) {
+ return states[i].frequency == HISTORY[i].frequency;
+ }
+ }
+ }
+
+ return false;
+}
+
+/**
+ * Surrogate bladerf_set_frequency handler
+ *
+ * @param dev Device handle
+ * @param module Module to configure
+ * @param frequency Desired frequency
+ *
+ * @return 0 on success, or value from \ref RETCODES list on failure
+ */
+int bladerf_set_frequency(struct bladerf *dev,
+ bladerf_module module,
+ unsigned int frequency)
+{
+ return update_test_state(dev, "frequency", frequency);
+}
+
+/**
+ * Surrogate bladerf_close handler
+ *
+ * @param dev Device handle
+ */
+void bladerf_close(struct bladerf *dev)
+{
+ if (dev) {
+ free((void *)dev->fpga_version.describe);
+ free((void *)dev->fw_version.describe);
+
+ free(dev);
+ }
+}
+
+/**
+ * Main program
+ */
+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;
+
+ /* Create a temporary directory for testing */
+ if (NULL == mkdtemp(tmpdir)) {
+ perror("mkdtemp failed");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Create a sample config file there */
+ if (0 > snprintf(filename, MAX_FILENAME_LEN - 1, "%s/%s", tmpdir,
+ "bladeRF.conf")) {
+ perror("snprintf failed");
+ exit(EXIT_FAILURE);
+ }
+
+ if (0 > create_config(filename, test_config, ARRAY_SIZE(test_config))) {
+ perror("create_config failed");
+ exit(EXIT_FAILURE);
+ }
+
+ /* Change working directory */
+ if (0 > chdir(tmpdir)) {
+ perror("chdir failed");
+ exit(EXIT_FAILURE);
+ }
+
+ bladerf_log_set_verbosity(BLADERF_LOG_LEVEL_DEBUG);
+
+ /* Iterate through the test cases */
+ for (size_t i = 0; i < ARRAY_SIZE(states); ++i) {
+ test_state const *t = &states[i];
+ struct bladerf *dev = NULL;
+ bool check = false;
+
+ int status;
+
+ status = create_test_device(&dev, &t->ident);
+ if (status != 0) {
+ fprintf(stderr, "Unable to open device: %s\n",
+ bladerf_strerror(status));
+ exit(EXIT_FAILURE);
+ }
+
+ dev->fpga_size = states[i].fpga_size;
+
+ printf("%s: fpga=%i backend=%d device=%d:%d instance=%d\n",
+ dev->ident.serial, dev->fpga_size, dev->ident.backend,
+ dev->ident.usb_bus, dev->ident.usb_addr, dev->ident.instance);
+
+ status = config_load_options_file(dev);
+ if (status != 0) {
+ fprintf(stderr, "Unable to load options file: %s\n",
+ bladerf_strerror(status));
+ exit(EXIT_FAILURE);
+ }
+
+ check = check_test_state(dev, "frequency");
+ printf("\tfrequency=%s\n", check ? "pass" : "FAIL");
+ if (!check) {
+ result = false;
+ }
+
+ printf("\n");
+
+ bladerf_close(dev);
+ }
+
+ /* Remove the temporary directory */
+ if (0 > unlink(filename)) {
+ perror("unlink failed");
+ exit(EXIT_FAILURE);
+ }
+
+ if (0 > rmdir(tmpdir)) {
+ perror("rmdir failed");
+ exit(EXIT_FAILURE);
+ }
+
+ printf("\nEnd Result: %s\n", result ? "PASS" : "FAIL");
+ exit(result ? 0 : EXIT_FAILURE);
+
+ return 0;
+}
--
2.11.0
|