changeset 239:ec81395bf08d

Solve name collisions and other problems caused by including Windows headers
author Daniele Nicolodi <daniele.nicolodi@obspm.fr>
date Thu, 12 Feb 2015 19:13:55 +0100
parents 78fdba868884
children 7fd5cb857d07
files data-provider.c data-provider.h file-data-provider.c future.h kk-data-provider.c logging.c logging.h
diffstat 7 files changed, 49 insertions(+), 43 deletions(-) [+]
line wrap: on
line diff
--- a/data-provider.c	Thu Jan 29 18:40:07 2015 +0100
+++ b/data-provider.c	Thu Feb 12 19:13:55 2015 +0100
@@ -11,14 +11,14 @@
 
 
 /* message callback */
-void CVICALLBACK MessageCB (void *msg)
+static void CVICALLBACK send_message_callback(void *msg)
 {
 	if (msg != NULL)
 		logmessage(INFO, msg);
 }
 
 
-void SendMessage(int threadId, const char *frmt, ...)
+void send_message(int thread, const char *frmt, ...)
 {
 	/* message buffer */
 	char *message = messages[messageid++ % DATA_PROVIDER_MSGNUM];
@@ -30,6 +30,6 @@
 	va_end(args);
 	
 	/* signal main thread */
-	PostDeferredCallToThread(MessageCB, message, threadId);
+	PostDeferredCallToThread(send_message_callback, message, thread);
 }
 
--- a/data-provider.h	Thu Jan 29 18:40:07 2015 +0100
+++ b/data-provider.h	Thu Feb 12 19:13:55 2015 +0100
@@ -9,9 +9,6 @@
 /* data queue */
 extern CmtTSQHandle dataQueue;
 
-/* callback receiving messages in the main thread */
-void CVICALLBACK MessageCB (void *callbackData);
-
 /* number of data channels */
 #define NCHAN 8
 
@@ -22,7 +19,7 @@
 };
 
 /* send messages to the main thread */
-void SendMessage(int threadId, const char *frmt, ...);
+void send_message(int threadid, const char *frmt, ...);
 
 /* data providers */
 int CVICALLBACK FakeDataProvider (void *functionData);
--- a/file-data-provider.c	Thu Jan 29 18:40:07 2015 +0100
+++ b/file-data-provider.c	Thu Feb 12 19:13:55 2015 +0100
@@ -13,8 +13,8 @@
 
 int CVICALLBACK FileDataProvider (void *functionData)
 {
-	int mainThreadId;
-	char dataFileName[MAX_PATHNAME_LEN];
+	int main_thread_id;
+	char datafile[MAX_PATHNAME_LEN];
 	int fd;
 	int read;
 	int year, month, day, hour, min, sec, msec;
@@ -24,19 +24,19 @@
 	double now;
 	
 	/* get main thread id to post messages to it */
-	mainThreadId = CmtGetMainThreadID();
+	main_thread_id = CmtGetMainThreadID();
 	
 	/* guess current data log file name */
 	GetCurrentDateTime(&now);
 	GetDateTimeElements(now, NULL, NULL, NULL, &month, &day, &year);
-	snprintf(dataFileName, sizeof(dataFileName) - 1, 
+	snprintf(datafile, sizeof(datafile) - 1, 
 		"%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year % 100, month, day);
 	
 	/* open file */
-	fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
+	fd = OpenFile(datafile, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
 	if (fd < 0) {
 		/* notify error to the main thread */
-		SendMessage(mainThreadId, "error opening data file '%s'", dataFileName);
+		send_message(main_thread_id, "error opening data file '%s'", datafile);
 	}
 	
 	/* seek to file end */
@@ -57,7 +57,7 @@
 			&event.data[0], &event.data[1], &event.data[2], &event.data[3]);
 		if (read != 11) {
 			/* notify error to the main thread */
-			SendMessage(mainThreadId, "error parsing data log line");
+			send_message(main_thread_id, "error parsing data log line");
 			/* skip line */
 			continue;
 		}
@@ -90,7 +90,7 @@
 		if ((hour == 23) && (min == 59) && (sec == 59)) {
 				
 			/* next data file name */
-			snprintf(dataFileName, sizeof(dataFileName) - 1, 
+			snprintf(datafile, sizeof(datafile) - 1, 
 				"%s\\%02d%02d%02d_Frequ.txt", LOGFILEPATH, year, month, day + 1);
 				
 			/* close data file */
@@ -99,7 +99,7 @@
 			/* wait for new data file to appear */
 			int retry = 20;
 			while (retry--) {
-				fd = OpenFile(dataFileName, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
+				fd = OpenFile(datafile, VAL_READ_ONLY, VAL_OPEN_AS_IS, VAL_ASCII);
 				if (fd != -1)
 					break;
 				Delay(0.01);
--- a/future.h	Thu Jan 29 18:40:07 2015 +0100
+++ b/future.h	Thu Feb 12 19:13:55 2015 +0100
@@ -1,23 +1,27 @@
 #ifndef __FUTURE_H__
 #define __FUTURE_H__
 
+/* required for time_t definition */
+#include <time.h>
+
 #define PTR_TO_INT(p) ((int) ((intptr_t)(p)))
 #define INT_TO_PTR(u) ((void *) ((intptr_t)(u)))
 
 double round(double x);
 
-typedef unsigned int time_t;
-typedef long int suseconds_t;
-
-struct timeval {
-	time_t tv_sec;			/* seconds since 1970-01-01 */
-	suseconds_t tv_usec;	/* microseconds */
+/* struct timeval is defined in `WinSock2.h` but we cannot safely include 
+ * this header because it includes `Windows.h` and CVI does not like to
+ * have that included after his includes. resort to this ugly hack to
+ * avoid to include windows headers and to avoid name clashes */
+struct __timeval {
+	time_t tv_sec;
+	long tv_usec;
 };
 
-struct timezone {
-	int tz_minuteswest;
-	int tz_dsttime;
-};
+#define timeval __timeval
+
+/* we never use struct timezone so leave it as on opaque struct */
+struct timezone;
 
 int gettimeofday(struct timeval *tp, struct timezone *tzp);
 
--- a/kk-data-provider.c	Thu Jan 29 18:40:07 2015 +0100
+++ b/kk-data-provider.c	Thu Feb 12 19:13:55 2015 +0100
@@ -46,7 +46,7 @@
 
 int CVICALLBACK KKDataProvider (void *functionData)
 {
-	int mainThreadId;
+	int main_thread_id;
 	int rv;
 	char *resp;
 	struct event event;
@@ -54,7 +54,7 @@
 	char port[256];
 	
 	/* get main thread id to post messages to it */
-	mainThreadId = CmtGetMainThreadID();
+	main_thread_id = CmtGetMainThreadID();
 	
 	/* configuration file path */ 
 	char path[MAX_PATHNAME_LEN];
@@ -83,50 +83,50 @@
 	/* connect to KK FX80E counter */
 	rv = FX_Open(port);
 	if (! rv) {
-		SendMessage(mainThreadId, FX_Error());
+		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
 
 	/* get counter hardware version string */
 	resp = FX_Command(0x81, 0xFFFF, 0x7001);
 	if (! resp) {
-		SendMessage(mainThreadId, FX_Error());
+		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
-	SendMessage(mainThreadId, "KK Counter version: %s", resp + 4);
+	send_message(main_thread_id, "KK Counter version: %s", resp + 4);
 	
 	/* set report interval 1sec */
 	resp = FX_Command(0x29, 0x0F00, 0x0900);
 	if (! resp) {
-		SendMessage(mainThreadId, FX_Error());
+		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
 	
 	/* set mode to instantaneous frequency measurement */
 	resp = FX_Command(0x42, 0x6F00, 0x2900);
 	if (! resp) {
-		SendMessage(mainThreadId, FX_Error());
+		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
 
 	/* disable scrambler */
 	resp = FX_Command(0x50, 0x6FFF, 0x2900);
 	if (! resp) {
- 		SendMessage(mainThreadId, FX_Error());
+ 		send_message(main_thread_id, FX_Error());
  		goto error;
  	}
 	
 	/* read nchan channels */
 	resp = FX_Command(0x30 + nchan, 0x6FFF, 0x2900);
 	if (! resp) {
-		SendMessage(mainThreadId, FX_Error());
+		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
 
 	/* enable synchronization */
 	resp = FX_Command(0x0F, 0x00, 0x00);
 	if (! resp) {
-		SendMessage(mainThreadId, FX_Error());
+		send_message(main_thread_id, FX_Error());
 		goto error;
 	}
 	
@@ -134,7 +134,7 @@
 		/* receive data from counter */
 		FX_Recv(&resp, TIMEOUT);
 		if (! resp) {
-			SendMessage(mainThreadId, FX_Error());
+			send_message(main_thread_id, FX_Error());
 			break;
 		}
 		
@@ -149,7 +149,7 @@
 				&event.data[0], &event.data[1], &event.data[2], &event.data[3],
 				&event.data[4], &event.data[5], &event.data[6], &event.data[7]);
 			if (rv != nchan) {
-				SendMessage(mainThreadId, "KK Counter: data conversion error: %d != %d", rv, nchan);
+				send_message(main_thread_id, "KK Counter: data conversion error: %d != %d", rv, nchan);
 				goto error;
 			}
 			
@@ -169,13 +169,13 @@
 		} else if (strneq(resp, "7020", 4)) {
 			/* undocumented packet. it probably reports the header for
 			   subsequent data packets */
-			SendMessage(mainThreadId, "KK Counter packet header: %s", resp + 7);
+			send_message(main_thread_id, "KK Counter packet header: %s", resp + 7);
 		} else if (strneq(resp, "7F51", 4)) {
 			/* measurement interval synchronized */
-			SendMessage(mainThreadId, "KK Counter measurement interval synchronized");
+			send_message(main_thread_id, "KK Counter measurement interval synchronized");
 		} else {
 			/* send message to the main thread */
-			SendMessage(mainThreadId, "KK Counter: %s", resp);
+			send_message(main_thread_id, "KK Counter: %s", resp);
 		}
 	}
 	
--- a/logging.c	Thu Jan 29 18:40:07 2015 +0100
+++ b/logging.c	Thu Feb 12 19:13:55 2015 +0100
@@ -51,10 +51,10 @@
 void __logmessage(struct logger *l, enum loglevel level, const char *frmt, ...)
 {
 	static const char *levels[] = {
+		"ERROR",
 		"DEBUG",
 		"INFO",
 		"WARNING",
-		"ERROR",
 	};
 
 	char msg[1024];
--- a/logging.h	Thu Jan 29 18:40:07 2015 +0100
+++ b/logging.h	Thu Feb 12 19:13:55 2015 +0100
@@ -11,11 +11,16 @@
 	void(*onerror)(int, const char *);
 } __logger;
 
+/* an ERROR macro is defined in `WinGDI.h` which is included
+ * by some other header file included before this. to be on
+ * the safe side we define the enum lable to the same value */
+#undef ERROR
+
 enum loglevel {
+	ERROR = 0,
 	DEBUG,
 	INFO,
 	WARNING,
-	ERROR,
 };
 
 int __logger_init(struct logger *l, void(*onerror)(int, const char *));