kinect_upload_fw: support architectures that are not little-endian
authorAntonio Ospite <ospite@studenti.unina.it>
Mon, 3 Oct 2011 14:22:36 +0000 (16:22 +0200)
committerAntonio Ospite <ospite@studenti.unina.it>
Mon, 3 Oct 2011 14:36:09 +0000 (16:36 +0200)
Add an endian.c program which will be used at compile time to generate
an endian.h file with the information about the endianness of the
architecture. This way we do not depend on any particular build system
to retrieve this information (libfreenect knows the endianness with the
help of Cmake, for instance).

This is inspired from the rdesktop project[1] and looks more portable
than relying on the system endian.h as its use is relatively new and not
standardized yet, even though there are proposals in that direction[2].

Finally fn_le32() implementation is ripped off from libfreenect.

[1] http://www.rdesktop.org/archive/2001/msg00722.html
[2] http://austingroupbugs.net/view.php?id=162

kinect_upload_fw/Makefile
kinect_upload_fw/endian.c [new file with mode: 0644]
kinect_upload_fw/kinect_upload_fw.c

index 1c21c14..e917d59 100644 (file)
@@ -32,11 +32,21 @@ LDFLAGS += $(shell pkg-config --libs libusb-1.0)
 PREFIX ?= /usr/local
 bindir := $(PREFIX)/sbin
 
-kinect_upload_fw: kinect_upload_fw.o
+all: kinect_upload_fw
+
+
+endian: endian.o
+
+endian.h: endian
+       rm -f endian.h
+       ./endian > endian.h
+
+kinect_upload_fw: endian.h kinect_upload_fw.o
+
 
 install: kinect_upload_fw
        install -d $(DESTDIR)$(bindir)
        install -m 755 kinect_upload_fw $(DESTDIR)$(bindir)
 
 clean:
-       rm -rf *~ *.o kinect_upload_fw
+       rm -rf *~ *.o kinect_upload_fw endian endian.h
diff --git a/kinect_upload_fw/endian.c b/kinect_upload_fw/endian.c
new file mode 100644 (file)
index 0000000..89120ca
--- /dev/null
@@ -0,0 +1,25 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+static int litend(void) {
+       int i = 0;
+       ((char *) (&i))[0] = 1;
+       return (i == 1);
+}
+
+static int bigend(void) {
+       return !litend();
+}
+
+int main(void) {
+       printf("#ifndef __ENDIAN_H\n");
+       printf("#define __ENDIAN_H\n");
+       printf("\n");
+       printf("#define __LITTLE_ENDIAN 1234\n");
+       printf("#define __BIG_ENDIAN    4321\n");
+       printf("#define __BYTE_ORDER __%s_ENDIAN\n",
+              litend() ? "LITTLE" : "BIG");
+       printf("\n");
+       printf("#endif /* __ENDIAN_H */\n");
+       exit(0);
+}
index f9a1952..eb65113 100644 (file)
@@ -36,6 +36,8 @@
 #include <errno.h>
 #include <libusb.h>
 
+#include "endian.h"
+
 static libusb_device_handle *dev;
 unsigned int seq;
 
@@ -55,8 +57,15 @@ typedef struct {
 } status_code;
 
 #define LOG(...) printf(__VA_ARGS__)
+
+#if __BYTE_ORDER == __BIG_ENDIAN
+static inline uint32_t fn_le32(uint32_t d)
+{
+       return (d<<24) | ((d<<8)&0xFF0000) | ((d>>8)&0xFF00) | (d>>24);
+}
+#else
 #define fn_le32(x) (x)
-// TODO: support architectures that aren't little-endian
+#endif
 
 static void dump_bl_cmd(bootloader_command cmd) {
        int i;