uhd/host/lib/usrp_clock/usrp_clock_c.cpp

136 lines
4.6 KiB
C++
Raw Normal View History

/*
* Copyright 2015 Ettus Research LLC
* Copyright 2018 Ettus Research, a National Instruments Company
*
* SPDX-License-Identifier: GPL-3.0-or-later
*/
/* C-Interface for multi_usrp_clock */
#include <uhd/usrp_clock/multi_usrp_clock.hpp>
#include <uhd/usrp_clock/usrp_clock.h>
2020-01-23 18:32:59 +00:00
#include <uhd/utils/static.hpp>
#include <string.h>
#include <map>
uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
2021-07-06 14:51:55 +00:00
#include <mutex>
/****************************************************************************
* Registry / Pointer Management
***************************************************************************/
/* Public structs */
2020-01-23 18:32:59 +00:00
struct uhd_usrp_clock
{
size_t usrp_clock_index;
std::string last_error;
};
/* Not public: We use this for our internal registry */
2020-01-23 18:32:59 +00:00
struct usrp_clock_ptr
{
uhd::usrp_clock::multi_usrp_clock::sptr ptr;
static size_t usrp_clock_counter;
};
size_t usrp_clock_ptr::usrp_clock_counter = 0;
typedef struct usrp_clock_ptr usrp_clock_ptr;
/* Prefer map, because the list can be discontiguous */
typedef std::map<size_t, usrp_clock_ptr> usrp_clock_ptrs;
UHD_SINGLETON_FCN(usrp_clock_ptrs, get_usrp_clock_ptrs);
/* Shortcut for accessing the underlying USRP clock sptr from a uhd_usrp_clock_handle* */
#define USRP_CLOCK(h_ptr) (get_usrp_clock_ptrs()[h_ptr->usrp_clock_index].ptr)
/****************************************************************************
* Generate / Destroy API calls
***************************************************************************/
uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
2021-07-06 14:51:55 +00:00
static std::mutex _usrp_clock_find_mutex;
2020-01-23 18:32:59 +00:00
uhd_error uhd_usrp_clock_find(const char* args, uhd_string_vector_t* devices_out)
{
UHD_SAFE_C(
uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
2021-07-06 14:51:55 +00:00
std::lock_guard<std::mutex> lock(_usrp_clock_find_mutex);
2020-01-23 18:32:59 +00:00
uhd::device_addrs_t devs =
uhd::device::find(std::string(args), uhd::device::CLOCK);
devices_out->string_vector_cpp.clear();
2020-01-23 18:32:59 +00:00
for (const uhd::device_addr_t& dev
: devs) { devices_out->string_vector_cpp.push_back(dev.to_string()); })
}
uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
2021-07-06 14:51:55 +00:00
static std::mutex _usrp_clock_make_mutex;
2020-01-23 18:32:59 +00:00
uhd_error uhd_usrp_clock_make(uhd_usrp_clock_handle* h, const char* args)
{
uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
2021-07-06 14:51:55 +00:00
UHD_SAFE_C(std::lock_guard<std::mutex> lock(_usrp_clock_make_mutex);
2020-01-23 18:32:59 +00:00
size_t usrp_clock_count = usrp_clock_ptr::usrp_clock_counter;
usrp_clock_ptr::usrp_clock_counter++;
2020-01-23 18:32:59 +00:00
// Initialize USRP Clock
uhd::device_addr_t device_addr(args);
usrp_clock_ptr P;
P.ptr = uhd::usrp_clock::multi_usrp_clock::make(device_addr);
2020-01-23 18:32:59 +00:00
// Dump into registry
get_usrp_clock_ptrs()[usrp_clock_count] = P;
2020-01-23 18:32:59 +00:00
// Update handle
(*h) = new uhd_usrp_clock;
(*h)->usrp_clock_index = usrp_clock_count;)
}
uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
2021-07-06 14:51:55 +00:00
static std::mutex _usrp_clock_free_mutex;
2020-01-23 18:32:59 +00:00
uhd_error uhd_usrp_clock_free(uhd_usrp_clock_handle* h)
{
uhd: Replace Boost mutexes and locks with standard options This is a very mechanical task that could almost have been done with sed. Boost versions of mutexes and locks were removed, and replaced with std:: versions. The replacement tables are as follows: == Mutexes == - boost::mutex -> std::mutex - boost::recursive_mutex -> std::recursive_mutex Mutexes behave identically between Boost and std:: and have the same API. == Locks == C++11 has only two types of lock that we use/need in UHD: - std::lock_guard: Identical to boost::lock_guard - std::unique_lock: Identical to boost::unique_lock Boost also has boost::mutex::scoped_lock, which is a typedef for boost::unique_lock<>. However, we often have used scoped_lock where we meant to use lock_guard<>. The name is a bit misleading, "scoped lock" sounding a bit like an RAII mechanism. Therefore, some previous boost::mutex::scoped_lock are now std::lock_guard<>. std::unique_lock is required when doing more than RAII locking (i.e., unlocking, relocking, usage with condition variables, etc.). == Condition Variables == Condition variables were out of the scope of this lock/mutex change, but in UHD, we inconsistently use boost::condition vs. boost::condition_variable. The former is a templated version of the latter, and thus works fine with std::mutex'es. Therefore, some boost::condition_variable where changed to boost::condition. All locks and mutexes use `#include <mutex>`. The corresponding Boost includes were removed. In some cases, this exposed issues with implicit Boost includes elsewhere. The missing explicit includes were added.
2021-07-06 14:51:55 +00:00
UHD_SAFE_C(std::lock_guard<std::mutex> lock(_usrp_clock_free_mutex);
2020-01-23 18:32:59 +00:00
if (!get_usrp_clock_ptrs().count((*h)->usrp_clock_index)) {
return UHD_ERROR_INVALID_DEVICE;
}
get_usrp_clock_ptrs()
.erase((*h)->usrp_clock_index);
delete *h;
*h = NULL;)
}
uhd_error uhd_usrp_clock_last_error(
2020-01-23 18:32:59 +00:00
uhd_usrp_clock_handle h, char* error_out, size_t strbuffer_len)
{
UHD_SAFE_C(memset(error_out, '\0', strbuffer_len);
strncpy(error_out, h->last_error.c_str(), strbuffer_len);)
}
uhd_error uhd_usrp_clock_get_pp_string(
2020-01-23 18:32:59 +00:00
uhd_usrp_clock_handle h, char* pp_string_out, size_t strbuffer_len)
{
UHD_SAFE_C_SAVE_ERROR(h, memset(pp_string_out, '\0', strbuffer_len); strncpy(
pp_string_out, USRP_CLOCK(h)->get_pp_string().c_str(), strbuffer_len);)
}
2020-01-23 18:32:59 +00:00
uhd_error uhd_usrp_clock_get_num_boards(uhd_usrp_clock_handle h, size_t* num_boards_out)
{
UHD_SAFE_C_SAVE_ERROR(h, *num_boards_out = USRP_CLOCK(h)->get_num_boards();)
}
uhd_error uhd_usrp_clock_get_time(
2020-01-23 18:32:59 +00:00
uhd_usrp_clock_handle h, size_t board, uint32_t* clock_time_out)
{
UHD_SAFE_C_SAVE_ERROR(h, *clock_time_out = USRP_CLOCK(h)->get_time(board);)
}
2020-01-23 18:32:59 +00:00
uhd_error uhd_usrp_clock_get_sensor(uhd_usrp_clock_handle h,
const char* name,
size_t board,
2020-01-23 18:32:59 +00:00
uhd_sensor_value_handle* sensor_value_out)
{
UHD_SAFE_C_SAVE_ERROR(h, delete (*sensor_value_out)->sensor_value_cpp;
(*sensor_value_out)->sensor_value_cpp = new uhd::sensor_value_t(
USRP_CLOCK(h)->get_sensor(name, board));)
}
uhd_error uhd_usrp_clock_get_sensor_names(
2020-01-23 18:32:59 +00:00
uhd_usrp_clock_handle h, size_t board, uhd_string_vector_handle* sensor_names_out)
{
UHD_SAFE_C_SAVE_ERROR(
h,
(*sensor_names_out)->string_vector_cpp = USRP_CLOCK(h)->get_sensor_names(board);)
}