libbladeRF: Load calibration tables or FPGA bitstream on device open
On Linux and OSX, the following directories will be searched,
in the order listed:
~/.config/Nuand/bladeRF
~/.Nuand/bladeRF
/etc/Nuand/bladeRF
In Windows, %appdata%/Nuand/bladeRF will searched.
The following files will be loaded, if found in these search paths:
FPGA image: hostedx40.rbf or hostedx115.rbf, depending on the
size of the FPGA reported by the device.
DC Calibration tables: <serial>_dc_<rx|tx>.tbl
libbladeRF: Added Windows support to file_find
This commit is contained in:
parent
6c20a6784e
commit
67938789f9
7 changed files with 268 additions and 24 deletions
|
|
@ -138,6 +138,7 @@
|
|||
/* Added here just to keep this out of the other source files. We'll have
|
||||
* a few Windows replacements for unistd.h items. */
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
|
||||
/*******************************************************************************
|
||||
* Miscellaneous OSX fixups
|
||||
|
|
@ -145,6 +146,7 @@
|
|||
#elif BLADERF_OS_OSX
|
||||
|
||||
#include <unistd.h>
|
||||
#include <pwd.h>
|
||||
|
||||
/*******************************************************************************
|
||||
* Miscellaneous Windows fixups
|
||||
|
|
@ -153,6 +155,7 @@
|
|||
|
||||
/* Rename a few functions and types */
|
||||
#include <windows.h>
|
||||
#include <io.h>
|
||||
#define usleep(x) Sleep((x+999)/1000)
|
||||
|
||||
/* Changes specific to Microsoft Visual Studio and its C89-compliant ways */
|
||||
|
|
@ -165,6 +168,7 @@
|
|||
# define strncasecmp _strnicmp
|
||||
# define fileno _fileno
|
||||
# define strdup _strdup
|
||||
# define access _access
|
||||
|
||||
/* ssize_t lives elsewhere */
|
||||
# include <BaseTsd.h>
|
||||
|
|
|
|||
|
|
@ -202,6 +202,7 @@ set(LIBBLADERF_SOURCE
|
|||
src/backend/backend.c
|
||||
src/bladerf.c
|
||||
src/bladerf_priv.c
|
||||
src/config.c
|
||||
src/dc_cal_table.c
|
||||
src/file_ops.c
|
||||
src/lms.c
|
||||
|
|
|
|||
|
|
@ -37,6 +37,7 @@
|
|||
#include "version.h" /* Generated at build time */
|
||||
#include "conversions.h"
|
||||
#include "dc_cal_table.h"
|
||||
#include "config.h"
|
||||
|
||||
/*------------------------------------------------------------------------------
|
||||
* Device discovery & initialization/deinitialization
|
||||
|
|
@ -135,8 +136,15 @@ int bladerf_open_with_devinfo(struct bladerf **opened_device,
|
|||
status = bladerf_is_fpga_configured(dev);
|
||||
if (status > 0) {
|
||||
status = bladerf_init_device(dev);
|
||||
if (status != 0) {
|
||||
goto error;
|
||||
}
|
||||
}
|
||||
|
||||
/* Load any configuration files or FPGA images that a user has stored
|
||||
* for this device in their bladerf config directory */
|
||||
status = config_load_all(dev);
|
||||
|
||||
error:
|
||||
if (status < 0) {
|
||||
bladerf_close(dev);
|
||||
|
|
|
|||
133
host/libraries/libbladeRF/src/config.c
Normal file
133
host/libraries/libbladeRF/src/config.c
Normal file
|
|
@ -0,0 +1,133 @@
|
|||
/*
|
||||
* This file is part of the bladeRF project:
|
||||
* http://www.github.com/nuand/bladeRF
|
||||
*
|
||||
* Copyright (C) 2014 Nuand LLC
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
#include <stdlib.h>
|
||||
#include "bladerf_priv.h"
|
||||
#include "dc_cal_table.h"
|
||||
#include "file_ops.h"
|
||||
#include "log.h"
|
||||
|
||||
static inline void load_dc_cal(struct bladerf *dev, const char *file)
|
||||
{
|
||||
int status;
|
||||
struct bladerf_image *img;
|
||||
|
||||
img = bladerf_alloc_image(BLADERF_IMAGE_TYPE_INVALID, 0, 0);
|
||||
if (img == NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
status = bladerf_image_read(img, file);
|
||||
if (status != 0) {
|
||||
log_debug("Failed to open image file (%s): %s\n",
|
||||
file, bladerf_strerror(status));
|
||||
return;
|
||||
}
|
||||
|
||||
switch (img->type) {
|
||||
case BLADERF_IMAGE_TYPE_RX_DC_CAL:
|
||||
dev->cal.dc_rx = dc_cal_tbl_load(img->data, img->length);
|
||||
break;
|
||||
|
||||
case BLADERF_IMAGE_TYPE_TX_DC_CAL:
|
||||
dev->cal.dc_tx = dc_cal_tbl_load(img->data, img->length);
|
||||
break;
|
||||
|
||||
default:
|
||||
log_debug("%s is not an RX DC calibration table.\n", file);
|
||||
}
|
||||
}
|
||||
|
||||
static inline int load_fpga(struct bladerf *dev)
|
||||
{
|
||||
int status;
|
||||
uint8_t *buf;
|
||||
size_t buf_len;
|
||||
|
||||
if (dev->fpga_size == BLADERF_FPGA_40KLE) {
|
||||
status = file_find_and_read("hostedx40.rbf", &buf, &buf_len);
|
||||
} else if (dev->fpga_size == BLADERF_FPGA_115KLE) {
|
||||
status = file_find_and_read("hostedx115.rbf", &buf, &buf_len);
|
||||
} else {
|
||||
/* Unexpected, but this will be complained about elsewhere */
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (status == 0) {
|
||||
log_verbose("Loading FPGA config directory\n");
|
||||
return dev->fn->load_fpga(dev, buf, buf_len);
|
||||
} else if (status == BLADERF_ERR_NO_FILE) {
|
||||
return 0;
|
||||
} else {
|
||||
return status;
|
||||
}
|
||||
}
|
||||
|
||||
static inline int load_dc_cals(struct bladerf *dev)
|
||||
{
|
||||
char *filename;
|
||||
char *full_path;
|
||||
|
||||
filename = calloc(1, FILENAME_MAX + 1);
|
||||
if (filename == NULL) {
|
||||
return BLADERF_ERR_MEM;
|
||||
}
|
||||
|
||||
strncat(filename, dev->ident.serial, FILENAME_MAX);
|
||||
strncat(filename, "_dc_rx.bin", FILENAME_MAX - BLADERF_SERIAL_LENGTH);
|
||||
full_path = file_find(filename);
|
||||
if (full_path != NULL) {
|
||||
log_verbose("Loading %s\n", full_path);
|
||||
load_dc_cal(dev, full_path);
|
||||
free(full_path);
|
||||
}
|
||||
|
||||
memset(filename, 0, FILENAME_MAX + 1);
|
||||
strncat(filename, dev->ident.serial, FILENAME_MAX);
|
||||
strncat(filename, "_dc_tx.bin", FILENAME_MAX - BLADERF_SERIAL_LENGTH);
|
||||
full_path = file_find(filename);
|
||||
if (full_path != NULL) {
|
||||
log_verbose("Loading %s\n", full_path);
|
||||
load_dc_cal(dev, full_path);
|
||||
free(full_path);
|
||||
}
|
||||
|
||||
free(filename);
|
||||
return 0;
|
||||
}
|
||||
|
||||
int config_load_all(struct bladerf *dev)
|
||||
{
|
||||
int status;
|
||||
|
||||
status = load_dc_cals(dev);
|
||||
if (status != 0) {
|
||||
return status;
|
||||
}
|
||||
|
||||
status = dev->fn->is_fpga_configured(dev);
|
||||
if (status == 0) {
|
||||
status = load_fpga(dev);
|
||||
} else if (status > 0) {
|
||||
status = 0;
|
||||
}
|
||||
|
||||
return status;
|
||||
}
|
||||
39
host/libraries/libbladeRF/src/config.h
Normal file
39
host/libraries/libbladeRF/src/config.h
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
/*
|
||||
* This file is part of the bladeRF project:
|
||||
* http://www.github.com/nuand/bladeRF
|
||||
*
|
||||
* Copyright (C) 2014 Nuand LLC
|
||||
*
|
||||
* This library is free software; you can redistribute it and/or
|
||||
* modify it under the terms of the GNU Lesser General Public
|
||||
* License as published by the Free Software Foundation; either
|
||||
* version 2.1 of the License, or (at your option) any later version.
|
||||
*
|
||||
* This library is distributed in the hope that it will be useful,
|
||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||
* Lesser General Public License for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU Lesser General Public
|
||||
* License along with this library; if not, write to the Free Software
|
||||
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
||||
*/
|
||||
|
||||
/* This file defines functionality related to libbladeRF configuration
|
||||
* directories and files */
|
||||
|
||||
#ifndef BLADERF_CONFIG_H_
|
||||
#define BLADERF_CONFIG_H_
|
||||
|
||||
|
||||
#include "config.h"
|
||||
|
||||
/**
|
||||
* Load all configurations files and FPGA images from a bladeRF config
|
||||
* directory, if available.
|
||||
*
|
||||
* @return 0 on success, BLADERF_ERR_* on failure
|
||||
*/
|
||||
int config_load_all(struct bladerf *dev);
|
||||
|
||||
#endif
|
||||
|
|
@ -31,6 +31,7 @@
|
|||
#include "file_ops.h"
|
||||
#include "minmax.h"
|
||||
#include "log.h"
|
||||
#include "rel_assert.h"
|
||||
|
||||
/* Paths to search for bladeRF files */
|
||||
struct search_path_entries {
|
||||
|
|
@ -111,7 +112,6 @@ int file_read_buffer(const char *filename, uint8_t **buf_ret, size_t *size_ret)
|
|||
f = fopen(filename, "rb");
|
||||
if (!f) {
|
||||
int errno_val = errno;
|
||||
log_verbose("fopen failed: %s\n", strerror(errno));
|
||||
return errno_val == ENOENT ? BLADERF_ERR_NO_FILE : BLADERF_ERR_IO;
|
||||
}
|
||||
|
||||
|
|
@ -148,10 +148,10 @@ out:
|
|||
|
||||
|
||||
#if BLADERF_OS_LINUX || BLADERF_OS_OSX
|
||||
#include <pwd.h>
|
||||
#define ACCESS_FILE_EXISTS F_OK
|
||||
|
||||
static const struct search_path_entries search_paths[] = {
|
||||
{ true, "/.config/nuand/bladeRF/" },
|
||||
{ true, "/.config/Nuand/bladeRF/" },
|
||||
{ true, "/.Nuand/bladeRF/" },
|
||||
{ false, "/etc/Nuand/bladeRF/" },
|
||||
};
|
||||
|
|
@ -165,41 +165,76 @@ static inline size_t get_home_dir(char *buf, size_t max_len)
|
|||
}
|
||||
|
||||
#elif BLADERF_OS_WINDOWS
|
||||
#error "TO DO"
|
||||
#define ACCESS_FILE_EXISTS 0
|
||||
#include <ShlObj.h>
|
||||
static const struct search_path_entries search_paths[] = {
|
||||
{ true, "/Nuand/bladeRF/" },
|
||||
};
|
||||
|
||||
static inline size_t get_home_dir(char *buf, size_t max_len)
|
||||
{
|
||||
const KNOWNFOLDERID folder_id = FOLDERID_RoamingAppData;
|
||||
PWSTR path;
|
||||
HRESULT status;
|
||||
|
||||
assert(max_len < INT_MAX);
|
||||
|
||||
status = SHGetKnownFolderPath(&folder_id, 0, NULL, &path);
|
||||
if (status == S_OK) {
|
||||
WideCharToMultiByte(CP_ACP, 0, path, -1, buf, (int)max_len, NULL, NULL);
|
||||
CoTaskMemFree(path);
|
||||
}
|
||||
|
||||
return strlen(buf);
|
||||
}
|
||||
|
||||
#else
|
||||
#error "Unknown OS or missing BLADERF_OS_* definition"
|
||||
#endif
|
||||
|
||||
int file_find_and_read(const char *filename, uint8_t **buf, size_t *size)
|
||||
/* We're not using functions that use the *nix PATH_MAX (which is known to be problematic),
|
||||
* or the WIN32 MAX_PATH. Therefore, we'll just use this arbitrary, but "sufficiently"
|
||||
* large max buffer size for paths */
|
||||
#define PATH_MAX_LEN 4096
|
||||
|
||||
char *file_find(const char *filename)
|
||||
{
|
||||
int status;
|
||||
size_t i;
|
||||
size_t max_len;
|
||||
char *full_path;
|
||||
|
||||
*buf = NULL;
|
||||
*size = 0;
|
||||
|
||||
full_path = malloc(PATH_MAX);
|
||||
if (full_path == NULL) {
|
||||
return BLADERF_ERR_MEM;
|
||||
}
|
||||
size_t i, max_len;
|
||||
char *full_path = (char*) calloc(1, PATH_MAX_LEN + 1);
|
||||
|
||||
for (i = 0; i < ARRAY_SIZE(search_paths); i++) {
|
||||
memset(full_path, 0, PATH_MAX);
|
||||
max_len = PATH_MAX - 1;
|
||||
memset(full_path, 0, PATH_MAX_LEN);
|
||||
max_len = PATH_MAX_LEN - 1;
|
||||
|
||||
if (search_paths[i].prepend_home) {
|
||||
max_len -= get_home_dir(full_path, max_len);
|
||||
}
|
||||
|
||||
strncat(full_path, search_paths[i].path, max_len);
|
||||
status = file_read_buffer(full_path, buf, size);
|
||||
if (status == 0) {
|
||||
goto out;
|
||||
max_len = PATH_MAX_LEN - strlen(full_path);
|
||||
strncat(full_path, filename, max_len);
|
||||
|
||||
if (access(full_path, ACCESS_FILE_EXISTS) != -1) {
|
||||
return full_path;
|
||||
}
|
||||
}
|
||||
|
||||
out:
|
||||
free(full_path);
|
||||
return status;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
int file_find_and_read(const char *filename, uint8_t **buf, size_t *size)
|
||||
{
|
||||
int status;
|
||||
char *full_path = file_find(filename);
|
||||
*buf = NULL;
|
||||
*size = 0;
|
||||
|
||||
if (full_path != NULL) {
|
||||
status = file_read_buffer(full_path, buf, size);
|
||||
free(full_path);
|
||||
return status;
|
||||
} else {
|
||||
return BLADERF_ERR_NO_FILE;
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -29,6 +29,7 @@
|
|||
#ifndef FILE_OPS_H__
|
||||
#define FILE_OPS_H__
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdint.h>
|
||||
|
||||
/**
|
||||
|
|
@ -76,9 +77,32 @@ int file_read(FILE *f, char *buf, size_t len);
|
|||
*
|
||||
* @param[in] f Open file stream.
|
||||
*
|
||||
* @return poisitive size of file on success, negative BLADERF_ERR_* value on
|
||||
* @return positive size of file on success, negative BLADERF_ERR_* value on
|
||||
* failure
|
||||
*/
|
||||
ssize_t file_size(FILE *f);
|
||||
|
||||
/**
|
||||
* Search for the specified filename in bladeRF config directories. If found,
|
||||
* the full path is returned. There is a chance that the file will be removed
|
||||
* in between this call indicating it exists and attempting to open it.
|
||||
*
|
||||
* @param filename File to search for
|
||||
*
|
||||
* @return Full path if the file is found, NULL otherwise.
|
||||
*/
|
||||
char *file_find(const char *filename);
|
||||
|
||||
/**
|
||||
* Search for the specified filename in bladeRF config directories. If found,
|
||||
* the file will be read and contents provided
|
||||
*
|
||||
* @param[in] f Filename
|
||||
* @param[out] buf Written with file data
|
||||
* @param[out] size Updated with file size
|
||||
*
|
||||
* @return 0 on success, negative BLADERF_ERR_* value on failure
|
||||
*/
|
||||
int file_find_and_read(const char *filename, uint8_t **buf, size_t *size);
|
||||
|
||||
#endif
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue