Added API to get libftdi library version.
authorThomas Jarosch <thomas.jarosch@intra2net.com>
Sun, 25 Sep 2011 10:08:55 +0000 (12:08 +0200)
committerThomas Jarosch <thomas.jarosch@intra2net.com>
Sun, 25 Sep 2011 10:08:55 +0000 (12:08 +0200)
Supports version number as integer, string
and possible git snapshot revision.

Based upon idea by Martin Zenzes.

examples/simple.c
src/CMakeLists.txt
src/ftdi.c
src/ftdi.h

index 145e931..9fac121 100644 (file)
@@ -13,12 +13,18 @@ int main(void)
 {
     int ret;
     struct ftdi_context ftdic;
+    struct ftdi_version_info version;
     if (ftdi_init(&ftdic) < 0)
     {
         fprintf(stderr, "ftdi_init failed\n");
         return EXIT_FAILURE;
     }
 
+    version = ftdi_get_library_version();
+    printf("Initialized libftdi %s (major: %d, minor: %d, micro: %d, snapshot ver: %s)\n",
+        version.version_str, version.major, version.minor, version.micro,
+        version.snapshot_str);
+
     if ((ret = ftdi_usb_open(&ftdic, 0x0403, 0x6001)) < 0)
     {
         fprintf(stderr, "unable to open ftdi device: %d (%s)\n", ret, ftdi_get_error_string(&ftdic));
index 0816341..b7ae085 100644 (file)
@@ -3,6 +3,20 @@ include_directories(BEFORE ${CMAKE_CURRENT_BINARY_DIR}
                      ${CMAKE_CURRENT_SOURCE_DIR}
                      )
 
+# Version information
+set(SNAPSHOT_VERSION "unknown")
+execute_process(COMMAND git describe
+                OUTPUT_VARIABLE GIT_DESCRIBE_OUTPUT
+                RESULT_VARIABLE GIT_DESCRIBE_RESULT
+                OUTPUT_STRIP_TRAILING_WHITESPACE
+                )
+if(${GIT_DESCRIBE_RESULT} STREQUAL 0)
+    set(SNAPSHOT_VERSION ${GIT_DESCRIBE_OUTPUT})
+endif(${GIT_DESCRIBE_RESULT} STREQUAL 0)
+message(STATUS "Detected git snapshot version: ${SNAPSHOT_VERSION}")
+
+configure_file(ftdi_version_i.h.in "${CMAKE_CURRENT_BINARY_DIR}/ftdi_version_i.h" @ONLY)
+
 # Targets
 set(c_sources     ftdi.c ftdi_stream.c)
 set(c_headers     ftdi.h)
index fba5288..de5ded1 100644 (file)
@@ -35,6 +35,7 @@
 #include <stdlib.h>
 
 #include "ftdi.h"
+#include "ftdi_version_i.h"
 
 #define ftdi_error_return(code, str) do {  \
         ftdi->error_str = str;             \
@@ -257,6 +258,23 @@ void ftdi_set_usbdev (struct ftdi_context *ftdi, libusb_device_handle *usb)
     ftdi->usb_dev = usb;
 }
 
+/**
+ * @brief Get libftdi library version
+ *
+ * @return ftdi_version_info Library version information
+ **/
+struct ftdi_version_info ftdi_get_library_version()
+{
+    struct ftdi_version_info ver;
+
+    ver.major = FTDI_MAJOR_VERSION;
+    ver.minor = FTDI_MINOR_VERSION;
+    ver.micro = FTDI_MICRO_VERSION;
+    ver.version_str = FTDI_VERSION_STRING;
+    ver.snapshot_str = FTDI_SNAPSHOT_VERSION;
+
+    return ver;
+}
 
 /**
     Finds all ftdi devices with given VID:PID on the usb bus. Creates a new
index 4e3880e..759e61b 100644 (file)
@@ -498,6 +498,23 @@ typedef struct
 typedef int (FTDIStreamCallback)(uint8_t *buffer, int length,
                                  FTDIProgressInfo *progress, void *userdata);
 
+/**
+ * Provide libftdi version information
+ * major: Library major version
+ * minor: Library minor version
+ * micro: Currently unused, ight get used for hotfixes.
+ * version_str: Version as (static) string
+ * snapshot_str: Git snapshot version if known. Otherwise "unknown" or empty string.
+*/
+struct ftdi_version_info
+{
+    int major;
+    int minor;
+    int micro;
+    const char *version_str;
+    const char *snapshot_str;
+};
+
 
 #ifdef __cplusplus
 extern "C"
@@ -512,6 +529,8 @@ extern "C"
     void ftdi_free(struct ftdi_context *ftdi);
     void ftdi_set_usbdev (struct ftdi_context *ftdi, struct libusb_device_handle *usbdev);
 
+    struct ftdi_version_info ftdi_get_library_version();
+
     int ftdi_usb_find_all(struct ftdi_context *ftdi, struct ftdi_device_list **devlist,
                           int vendor, int product);
     void ftdi_list_free(struct ftdi_device_list **devlist);