1 //
2 // Copyright 2015 Ettus Research LLC
3 // Copyright 2018 Ettus Research, a National Instruments Company
4 //
5 // SPDX-License-Identifier: GPL-3.0-or-later
6 //
7
8 #include <uhd/error.h>
9 #include <uhd/usrp/dboard_eeprom.h>
10 #include <string.h>
11 #include <boost/format.hpp>
12
uhd_dboard_eeprom_make(uhd_dboard_eeprom_handle * h)13 uhd_error uhd_dboard_eeprom_make(uhd_dboard_eeprom_handle* h)
14 {
15 UHD_SAFE_C(*h = new uhd_dboard_eeprom_t;)
16 }
17
uhd_dboard_eeprom_free(uhd_dboard_eeprom_handle * h)18 uhd_error uhd_dboard_eeprom_free(uhd_dboard_eeprom_handle* h)
19 {
20 UHD_SAFE_C(delete *h; *h = NULL;)
21 }
22
uhd_dboard_eeprom_get_id(uhd_dboard_eeprom_handle h,char * id_out,size_t strbuffer_len)23 uhd_error uhd_dboard_eeprom_get_id(
24 uhd_dboard_eeprom_handle h, char* id_out, size_t strbuffer_len)
25 {
26 UHD_SAFE_C_SAVE_ERROR(
27 h, std::string dboard_id_cpp = h->dboard_eeprom_cpp.id.to_string();
28 strncpy(id_out, dboard_id_cpp.c_str(), strbuffer_len);)
29 }
30
uhd_dboard_eeprom_set_id(uhd_dboard_eeprom_handle h,const char * id)31 uhd_error uhd_dboard_eeprom_set_id(uhd_dboard_eeprom_handle h, const char* id)
32 {
33 UHD_SAFE_C_SAVE_ERROR(
34 h, h->dboard_eeprom_cpp.id = uhd::usrp::dboard_id_t::from_string(id);)
35 }
36
uhd_dboard_eeprom_get_serial(uhd_dboard_eeprom_handle h,char * id_out,size_t strbuffer_len)37 uhd_error uhd_dboard_eeprom_get_serial(
38 uhd_dboard_eeprom_handle h, char* id_out, size_t strbuffer_len)
39 {
40 UHD_SAFE_C_SAVE_ERROR(h, std::string dboard_serial_cpp = h->dboard_eeprom_cpp.serial;
41 strncpy(id_out, dboard_serial_cpp.c_str(), strbuffer_len);)
42 }
43
uhd_dboard_eeprom_set_serial(uhd_dboard_eeprom_handle h,const char * serial)44 uhd_error uhd_dboard_eeprom_set_serial(uhd_dboard_eeprom_handle h, const char* serial)
45 {
46 UHD_SAFE_C_SAVE_ERROR(h, h->dboard_eeprom_cpp.serial = serial;)
47 }
48
49 //! Convert a string into an int. If that doesn't work, craft our own exception
50 // instead of using the Boost exception. We need to put this separate from the
51 // caller function because of macro expansion.
_convert_rev_with_exception(const std::string & rev_str)52 int _convert_rev_with_exception(const std::string& rev_str)
53 {
54 try {
55 return std::stoi(rev_str);
56 } catch (const std::invalid_argument&) {
57 throw uhd::lookup_error(
58 str(boost::format("Error retrieving revision from string `%s`") % rev_str));
59 } catch (const std::out_of_range&) {
60 throw uhd::lookup_error(
61 str(boost::format("Error retrieving revision from string `%s`") % rev_str));
62 }
63 }
64
uhd_dboard_eeprom_get_revision(uhd_dboard_eeprom_handle h,int * revision_out)65 uhd_error uhd_dboard_eeprom_get_revision(uhd_dboard_eeprom_handle h, int* revision_out)
66 {
67 UHD_SAFE_C_SAVE_ERROR(
68 h, *revision_out = _convert_rev_with_exception(h->dboard_eeprom_cpp.revision);)
69 }
70
uhd_dboard_eeprom_set_revision(uhd_dboard_eeprom_handle h,int revision)71 uhd_error uhd_dboard_eeprom_set_revision(uhd_dboard_eeprom_handle h, int revision)
72 {
73 UHD_SAFE_C_SAVE_ERROR(h, h->dboard_eeprom_cpp.revision = std::to_string(revision);)
74 }
75
uhd_dboard_eeprom_last_error(uhd_dboard_eeprom_handle h,char * error_out,size_t strbuffer_len)76 uhd_error uhd_dboard_eeprom_last_error(
77 uhd_dboard_eeprom_handle h, char* error_out, size_t strbuffer_len)
78 {
79 UHD_SAFE_C(memset(error_out, '\0', strbuffer_len);
80 strncpy(error_out, h->last_error.c_str(), strbuffer_len);)
81 }
82