c++ - Cryptoauthlib - 匿名联合只能有非静态数据成员 - 分段错误
问题描述
我正在尝试使用 cryptoauthlib 编译一个 c++ 程序,我在 atca_iface.h 中得到了这个(匿名联合只能有非静态数据成员)错误。我认为这是一个 c11 功能,与 c++ 不兼容:
未命名联合中的所有命名结构都会引发错误。我删除了以下结构的名称:
typedef struct
{
ATCAIfaceType iface_type; // active iface - how to interpret the union below
ATCADeviceType devtype; // explicit device type
union // each instance of an iface cfg defines a single type of interface
{
struct //ATCAI2C
{
uint8_t slave_address; // 8-bit slave address
uint8_t bus; // logical i2c bus number, 0-based - HAL will map this to a pin pair for SDA SCL
uint32_t baud; // typically 400000
} atcai2c;
struct //ATCASWI
{
uint8_t bus; // logical SWI bus - HAL will map this to a pin or uart port
} atcaswi;
struct //ATCAUART
{
int port; // logic port number
uint32_t baud; // typically 115200
uint8_t wordsize; // usually 8
uint8_t parity; // 0 == even, 1 == odd, 2 == none
uint8_t stopbits; // 0,1,2
} atcauart;
struct //ATCAHID
{
int idx; // HID enumeration index
uint32_t vid; // Vendor ID of kit (0x03EB for CK101)
uint32_t pid; // Product ID of kit (0x2312 for CK101)
uint32_t packetsize; // Size of the USB packet
uint8_t guid[16]; // The GUID for this HID device
} atcahid;
struct //ATCACUSTOM
{
ATCA_STATUS (*halinit)(void *hal, void *cfg);
ATCA_STATUS (*halpostinit)(void *iface);
ATCA_STATUS (*halsend)(void *iface, uint8_t *txdata, int txlength);
ATCA_STATUS (*halreceive)(void *iface, uint8_t* rxdata, uint16_t* rxlength);
ATCA_STATUS (*halwake)(void *iface);
ATCA_STATUS (*halidle)(void *iface);
ATCA_STATUS (*halsleep)(void *iface);
ATCA_STATUS (*halrelease)(void* hal_data);
} atcacustom;
};
uint16_t wake_delay; // microseconds of tWHI + tWLO which varies based on chip type
int rx_retries; // the number of retries to attempt for receiving bytes
void * cfg_data; // opaque data used by HAL in device discovery
} ATCAIfaceCfg;
我将这些结构更改为未命名的结构并编译,但不幸的是在调用 init 函数时出现分段错误ATCA_STATUS atinit(ATCAIface ca_iface);
......我不直接调用此函数。我打电话atcab_init(&cfg_ateccx08a_i2c_default);
有没有办法在不修改界面的情况下使用它,为什么会出现分段错误?
我的硬件设置是 CM3,ATECC608a 连接到 I2C 1。接口已启动,我可以查询它。我是否需要将默认接口修改为正确的设备类型和正确的 I2C 接口?当我这样做时,我会遇到相同的分段错误。
编辑:
示例代码
#include <iostream>
#include "cryptoauthlib.h"
int main(int argc, char *argv[])
{
uint8_t random_number;
atcab_init(&cfg_ateccx08a_i2c_default);
ATCA_STATUS status = atcab_random(&random_number);
std::cout << "status: " << (int)status << std::endl;
return 0;
}
编辑2:
github上有人告诉我,我必须使用 -DATCA_HAL_I2C 作为 C 编译器标志。但我现在得到undefined reference to hal_i2c_init
错误
编辑3:
问题似乎出在 cryptoauthlib 的 cmake 配置上,因为我在 windows 上为 armhf 进行交叉编译。
查看 cmake 配置
编辑4:
要重现最新的错误:
从这里获取 cryptoauthlib 从这里
获取 VisualGDB
安装 VS 2017 安装 VisualGDB 使用 VisualGDB 创建一个新项目 -> CMAKE 项目 从这里下载最新的工具链
在 VisualGDB 中使用它 添加 cryptoauthlib 作为对项目的引用 在之后添加以下 CMAKE命令项目的 CMakeLists.txt:project()
unset(WIN32)
unset(APPLE)
set(UNIX)
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -fms-extensions -std=c11 -DATCA_HAL_I2C=on -DATCAPRINTF=on")
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++17 -DATCA_HAL_I2C=on -DATCAPRINTF=on")
建造。
编辑5:
发现我必须set(ATCA_HAL_I2C TRUE)
在我的 CMAKE 项目中明确地做......不幸的是,作为一个选项它对我不起作用。
现在我在这里再次遇到分段错误:
ATCA_STATUS atcab_random(uint8_t *rand_out)
{
...
ATCACommand ca_cmd = _gDevice->mCommands;
...
}
我在 github 上将此作为问题打开。
解决方案
我发现了错误。硬件抽象层无法连接,因为 i2c 总线的总线默认设置.atcai2c.bus
为 2,必须为 1,否则将找不到 i2c 设备。
ATCAIfaceCfg cfg_ateccx08a_i2c_default = {
.iface_type = ATCA_I2C_IFACE,
.devtype = ATECC608A,
.atcai2c.slave_address = 0xC0,
.atcai2c.bus = 1,
.atcai2c.baud = 400000,
//.atcai2c.baud = 100000,
.wake_delay = 1500,
.rx_retries = 20
};
我现在可以使用以下内容从芯片中获取修订版、序列号和随机数:
#include <iostream>
#include "cryptoauthlib.h"
int main(int argc, char *argv[])
{
ATCAIfaceCfg *atca_cfg;
atca_cfg = &cfg_ateccx08a_i2c_default;
ATCA_STATUS status = atcab_init(atca_cfg);
std::cout << "status: " << (int)status << std::endl;
uint8_t revision[4];
status = atcab_info(revision);
if (status != ATCA_SUCCESS)
{
return -1;
}
std::cout << "revision: " << (int)revision << std::endl;
uint8_t serial[ATCA_SERIAL_NUM_SIZE];
status = atcab_read_serial_number(serial);
if (status != ATCA_SUCCESS)
{
return - 1;
}
std::cout << "serial: " << (int)serial << std::endl;
uint8_t num[32];
status = atcab_random(num);
if (status != ATCA_SUCCESS)
{
return - 1;
}
std::cout << "random: " << (int)num << std::endl;
return 0;
}
我将在 github 上继续这个。
推荐阅读
- java - 这个语法在 Kotlin 中是什么意思?
- html - 在多个多边形 SVG 中定位文本
- java - java.time.format.DateTimeParseException:无法解析文本“2019-02-16 09:29:32.959”,在索引 10 处找到未解析的文本
- vb.net - 对非共享成员的引用需要对象引用尝试在 VB.net 中传递对象
- php - 使用 preg 替换获取完整的图像链接
- apache-spark - Hive-On-Spark - 执行后运行查询时出错。引擎更改为 Spark
- bazel - 如何增加重试次数或延长抓取时间?
- java - 打印字符串数组中的所有名称
- apache - 在 Apache 上的 LDAP 身份验证后传递用户名
- jquery - 使用 html ids 查询 mongoDB 文档