diff --git a/software/include/mockturtle/libmockturtle.h b/software/include/mockturtle/libmockturtle.h index 03c3f5c246188ab35a393da778f56f0773613aed..f263a361eb43d7760ded464c6a99b0005e2c7b7b 100644 --- a/software/include/mockturtle/libmockturtle.h +++ b/software/include/mockturtle/libmockturtle.h @@ -66,6 +66,7 @@ enum trtl_error_number { ETRTL_MSG_SYNC_FAILED_INVAL, /**< Receive sync message failure: invalid */ + ETRTL_DEVICE_NAME_NOID, /**< The device name does not contain a valid ID */ __ETRTL_MAX, }; @@ -89,6 +90,8 @@ extern struct trtl_dev *trtl_open_by_id(uint32_t device_id); extern struct trtl_dev *trtl_open_by_lun(unsigned int lun) __attribute__((deprecated)); extern void trtl_close(struct trtl_dev *trtl); extern char *trtl_name_get(struct trtl_dev *trtl); +extern uint32_t trtl_id_get(struct trtl_dev *trtl); + const struct trtl_config_rom *trtl_config_get(struct trtl_dev *trtl); /**@}*/ diff --git a/software/lib/libmockturtle.c b/software/lib/libmockturtle.c index 701914652f40a6fce3a4527fe10b1b718ca551fd..e61c1da71b56fd0e51e183999f40623a4ca8515d 100644 --- a/software/lib/libmockturtle.c +++ b/software/lib/libmockturtle.c @@ -9,6 +9,7 @@ #include <sys/ioctl.h> #include <sys/stat.h> #include <unistd.h> +#include <inttypes.h> #include <stdlib.h> #include <stdio.h> #include <stdarg.h> @@ -50,6 +51,7 @@ static const char * const trtl_error_str[] = { "Failed to receive synchronous message: poll error", "Failed to receive synchronous message: timeout", "Failed to receive synchronous message: invalid sync id", + "Cannot extract device ID from device name", NULL, }; @@ -188,7 +190,7 @@ struct trtl_dev *trtl_open(const char *device) { struct trtl_desc *trtl; char path[256]; - int i, err, k, fd; + int i, err, k, fd, ret; struct stat sb; trtl = malloc(sizeof(struct trtl_desc)); @@ -196,6 +198,12 @@ struct trtl_dev *trtl_open(const char *device) return NULL; strncpy(trtl->name, device, TRTL_NAME_LEN); + ret = sscanf(trtl->name, "trtl-%"SCNx32, &trtl->devid); + if (ret != 1) { + errno = ETRTL_DEVICE_NAME_NOID; + goto out_id; + } + snprintf(path, sizeof(path), "/dev/%s", trtl->name); err = stat(path, &sb); if (!err) { @@ -263,6 +271,7 @@ out_hmq_fd: } out_cfg: out_stat: +out_id: free(trtl); return NULL; } @@ -277,20 +286,10 @@ out_stat: */ struct trtl_dev *trtl_open_by_id(uint32_t device_id) { - struct trtl_desc *wdesc; - struct trtl_dev *trtl; char name[12]; - snprintf(name, 12, "trtl-%04x", device_id); - trtl = trtl_open(name); - if (!trtl) - return NULL; - - /* Save the device ID */ - wdesc = (struct trtl_desc *)trtl; - wdesc->devid = device_id; - - return trtl; + snprintf(name, sizeof(name), "trtl-%04x", device_id); + return trtl_open(name); } @@ -836,6 +835,19 @@ char *trtl_name_get(struct trtl_dev *trtl) } +/** + * Return the device ID + * @param[in] trtl device token + * @return the device ID. If the ID can't be retrived, then + * it will return 0xFFFFFFFF + */ +uint32_t trtl_id_get(struct trtl_dev *trtl) +{ + struct trtl_desc *wdesc = (struct trtl_desc *)trtl; + + return trtl ? wdesc->devid : ~0; +} + /** * Return information about the synthesis configuration * @param[in] trtl device token