From 85ae140ba9b2750b78ef4efff001a70833217653 Mon Sep 17 00:00:00 2001 From: Marc Schink Date: Thu, 25 Sep 2014 05:19:06 -0400 Subject: [PATCH] device: Add function to retrieve free memory size. --- libjaylink/device.c | 56 +++++++++++++++++++++++++++++++++++++++++++++++++ libjaylink/libjaylink.h | 4 ++++ 2 files changed, 60 insertions(+) diff --git a/libjaylink/device.c b/libjaylink/device.c index 31d6a91..18de63f 100644 --- a/libjaylink/device.c +++ b/libjaylink/device.c @@ -25,6 +25,7 @@ #define CMD_GET_VERSION 0x01 #define CMD_GET_HW_STATUS 0x07 +#define CMD_GET_FREE_MEMORY 0xd4 #define CMD_GET_CAPS 0xe8 #define CMD_GET_EXT_CAPS 0xed @@ -443,3 +444,58 @@ int jaylink_get_extended_caps(struct jaylink_device_handle *devh, uint8_t *caps) return JAYLINK_OK; } + +/** + * Retrieve the size of free memory of a device. + * + * @note This function must only be used if the device has the + * #JAYLINK_DEV_CAP_GET_FREE_MEMORY capability. + * + * @param[in,out] devh Device handle. + * @param[out] size Size of free memory in bytes on success, and undefined on + * failure. + * + * @retval JAYLINK_OK Success. + * @retval JAYLINK_ERR_ARG Invalid arguments. + * @retval JAYLINK_ERR_TIMEOUT A timeout occurred. + * @retval JAYLINK_ERR Other error conditions. + * + * @see jaylink_get_caps() to retrieve device capabilities. + */ +int jaylink_get_free_memory(struct jaylink_device_handle *devh, uint32_t *size) +{ + int ret; + struct jaylink_context *ctx; + uint8_t buf[4]; + + if (!devh || !size) + return JAYLINK_ERR_ARG; + + ctx = devh->dev->ctx; + ret = transport_start_write_read(devh, 1, 4, 1); + + if (ret != JAYLINK_OK) { + log_err(ctx, "transport_start_write_read() failed: %i.", ret); + return ret; + } + + buf[0] = CMD_GET_FREE_MEMORY; + + ret = transport_write(devh, buf, 1); + + if (ret != JAYLINK_OK) { + log_err(ctx, "transport_write() failed: %i.", ret); + return ret; + } + + ret = transport_read(devh, buf, 4); + + if (ret != JAYLINK_OK) { + log_err(ctx, "transport_read() failed: %i.", ret); + return ret; + } + + *size = buffer_get_u32(buf, 0); + + return JAYLINK_OK; +} diff --git a/libjaylink/libjaylink.h b/libjaylink/libjaylink.h index bbfc3f6..bbf7add 100644 --- a/libjaylink/libjaylink.h +++ b/libjaylink/libjaylink.h @@ -41,6 +41,8 @@ enum jaylink_log_level { /** Device capabilities. */ enum jaylink_device_capability { + /** Device supports retrieval of free memory size. */ + JAYLINK_DEV_CAP_GET_FREE_MEMORY = 11, /** Device supports retrieval of extended capabilities. */ JAYLINK_DEV_CAP_GET_EXT_CAPS = 31 }; @@ -107,4 +109,6 @@ int jaylink_get_caps(struct jaylink_device_handle *devh, uint8_t *caps); int jaylink_get_extended_caps(struct jaylink_device_handle *devh, uint8_t *caps); +int jaylink_get_free_memory(struct jaylink_device_handle *devh, uint32_t *size); + #endif /* LIBJAYLINK_LIBJAYLINK_H */ -- 2.11.4.GIT