From ed59987e25a4600fcb1f7192a2cd6dc459a2ffad Mon Sep 17 00:00:00 2001
From: Michael Jerris <mike@jerris.com>
Date: Sat, 19 May 2007 05:05:29 +0000
Subject: [PATCH] windows build building again.  most of the functions filled
 in for windows.  more to come!

git-svn-id: http://svn.openzap.org/svn/openzap/trunk@37 a93c3328-9c30-0410-af19-c9cd2b2d52af
---
 libs/openzap/src/include/openzap.h   |   3 +
 libs/openzap/src/include/zap_types.h |   1 +
 libs/openzap/src/openzap.c           |   8 +--
 libs/openzap/src/zap_wanpipe.c       | 101 ++++++++++++++++++++++++---
 4 files changed, 98 insertions(+), 15 deletions(-)

diff --git a/libs/openzap/src/include/openzap.h b/libs/openzap/src/include/openzap.h
index c6e4e79343..d2a6fc08e6 100644
--- a/libs/openzap/src/include/openzap.h
+++ b/libs/openzap/src/include/openzap.h
@@ -61,6 +61,9 @@
 #ifndef strcasecmp
 #define strcasecmp(s1, s2) _stricmp(s1, s2)
 #endif
+#ifndef strncasecmp
+#define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
+#endif
 #ifndef snprintf
 #define snprintf _snprintf
 #endif
diff --git a/libs/openzap/src/include/zap_types.h b/libs/openzap/src/include/zap_types.h
index b8613e613b..1d274de2b6 100644
--- a/libs/openzap/src/include/zap_types.h
+++ b/libs/openzap/src/include/zap_types.h
@@ -37,6 +37,7 @@
 #ifdef WIN32
 #include <windows.h>
 typedef HANDLE zap_socket_t;
+typedef unsigned __int32 uint32_t;
 #else
 #include <stdint.h>
 typedef int zap_socket_t;
diff --git a/libs/openzap/src/openzap.c b/libs/openzap/src/openzap.c
index 37a39c71cf..eb32bb35ec 100644
--- a/libs/openzap/src/openzap.c
+++ b/libs/openzap/src/openzap.c
@@ -703,7 +703,7 @@ ZINT_CODEC_FUNCTION(zint_alaw2ulaw)
 zap_status_t zap_channel_read(zap_channel_t *zchan, void *data, zap_size_t *datalen)
 {
 	zap_status_t status = ZAP_FAIL;
-	zint_codec_t codec_func;
+	zint_codec_t codec_func = NULL;
 	zap_size_t max = *datalen;
 
 	assert(zchan != NULL);
@@ -772,7 +772,7 @@ zap_status_t zap_channel_read(zap_channel_t *zchan, void *data, zap_size_t *data
 			sln = sln_buf;
 		}
 
-		teletone_dtmf_detect (&zchan->dtmf_detect, sln, slen);
+		teletone_dtmf_detect(&zchan->dtmf_detect, sln, (int)slen);
 		teletone_dtmf_get(&zchan->dtmf_detect, digit_str, sizeof(digit_str));
 		if(digit_str[0]) {
 			zint_event_cb_t event_callback = NULL;
@@ -806,7 +806,7 @@ zap_status_t zap_channel_read(zap_channel_t *zchan, void *data, zap_size_t *data
 zap_status_t zap_channel_write(zap_channel_t *zchan, void *data, zap_size_t *datalen)
 {
 	zap_status_t status = ZAP_FAIL;
-	zint_codec_t codec_func;
+	zint_codec_t codec_func = NULL;
 	zap_size_t dtmf_blen, max = *datalen;
 	
 	assert(zchan != NULL);
@@ -844,7 +844,7 @@ zap_status_t zap_channel_write(zap_channel_t *zchan, void *data, zap_size_t *dat
 	if (zchan->dtmf_buffer && (dtmf_blen = zap_buffer_inuse(zchan->dtmf_buffer))) {
 		zap_size_t dlen = *datalen;
 		uint8_t auxbuf[1024];
-		uint32_t len, br;
+		zap_size_t len, br;
 
 		if (zchan->native_codec != ZAP_CODEC_SLIN) {
 			dlen *= 2;
diff --git a/libs/openzap/src/zap_wanpipe.c b/libs/openzap/src/zap_wanpipe.c
index cc86622b57..b3fb8b297f 100644
--- a/libs/openzap/src/zap_wanpipe.c
+++ b/libs/openzap/src/zap_wanpipe.c
@@ -330,7 +330,7 @@ static ZINT_CLOSE_FUNCTION(wanpipe_close)
 static ZINT_COMMAND_FUNCTION(wanpipe_command)
 {
 	wanpipe_tdm_api_t tdm_api;
-	int err;
+	int err = 0;
 
 	ZINT_COMMAND_MUZZLE;
 	
@@ -367,7 +367,74 @@ static ZINT_COMMAND_FUNCTION(wanpipe_command)
 	return ZAP_SUCCESS;
 }
 
-static ZINT_WAIT_FUNCTION(wanpipe_wait)
+#ifdef __WINDOWS__
+
+static ZINT_WAIT_FUNCTION(wanpipe_wait_windows)
+{
+	API_POLL_STRUCT	api_poll;
+	zap_wait_flag_t inflags = *flags;
+
+	memset(&api_poll, 0x00, sizeof(API_POLL_STRUCT));
+	
+	api_poll.user_flags_bitmap = inflags;
+	api_poll.timeout = to;
+
+	if(DoApiPollCommand(zchan->sockfd, &api_poll)){
+		snprintf(zchan->last_error, sizeof(zchan->last_error), "Poll failed");
+		return ZAP_FAIL;
+	}
+
+	*flags = ZAP_NO_FLAGS;
+
+	switch(api_poll.operation_status)
+	{
+		case SANG_STATUS_RX_DATA_AVAILABLE:
+			break;
+
+		default:
+			snprintf(zchan->last_error, sizeof(zchan->last_error), "Unknown Operation Status: %d\n", api_poll.operation_status);
+			return ZAP_FAIL;
+	}
+
+	if(api_poll.poll_events_bitmap == 0){
+		snprintf(zchan->last_error, sizeof(zchan->last_error), "invalid Poll Events bitmap: 0x%X\n", api_poll.poll_events_bitmap);
+		return ZAP_FAIL;
+	}
+
+	if (api_poll.poll_events_bitmap & POLL_EVENT_TIMEOUT) {
+		return ZAP_TIMEOUT;
+	}
+
+	if (api_poll.poll_events_bitmap & POLL_EVENT_RX_DATA) {
+		*flags |= ZAP_READ;
+	}
+
+	if (api_poll.poll_events_bitmap & POLL_EVENT_TX_READY) {
+		*flags |= ZAP_WRITE;
+	}
+
+	if (api_poll.poll_events_bitmap & POLL_EVENT_LINK_STATE) {
+		*flags |= ZAP_ERROR;
+	}
+
+	return ZAP_SUCCESS;
+}
+
+static ZINT_READ_FUNCTION(wanpipe_read_windows)
+{
+	ZINT_READ_MUZZLE;
+	return ZAP_FAIL;
+}
+
+static ZINT_WRITE_FUNCTION(wanpipe_write_windows)
+{
+	ZINT_WRITE_MUZZLE;
+	return ZAP_FAIL;
+}
+
+#else
+
+static ZINT_WAIT_FUNCTION(wanpipe_wait_unix)
 {
 	fd_set read_fds, write_fds, error_fds, *r = NULL, *w = NULL, *e = NULL;
 	zap_wait_flag_t inflags = *flags;
@@ -430,8 +497,6 @@ static ZINT_WAIT_FUNCTION(wanpipe_wait)
 	return ZAP_SUCCESS;
 }
 
-#ifndef __WINDOWS__
-
 static ZINT_READ_FUNCTION(wanpipe_read_unix)
 {
 	int rx_len = 0;
@@ -502,8 +567,9 @@ static ZINT_WRITE_FUNCTION(wanpipe_write_unix)
 
 static ZINT_READ_FUNCTION(wanpipe_read)
 {
-
-#ifndef WIN32
+#ifdef __WINDOWS__
+	return wanpipe_read_windows(zchan, data, datalen);
+#else
 	return wanpipe_read_unix(zchan, data, datalen);
 #endif
 
@@ -511,13 +577,20 @@ static ZINT_READ_FUNCTION(wanpipe_read)
 
 static ZINT_WRITE_FUNCTION(wanpipe_write)
 {
-	ZINT_WRITE_MUZZLE;
-
-#ifndef WIN32
+#ifdef __WINDOWS__
+	return wanpipe_write_windows(zchan, data, datalen);
+#else
 	return wanpipe_write_unix(zchan, data, datalen);
 #endif
+}
 
-	return ZAP_FAIL;
+static ZINT_WAIT_FUNCTION(wanpipe_wait)
+{
+#ifdef __WINDOWS__
+	return wanpipe_wait_windows(zchan, flags, to);
+#else
+	return wanpipe_wait_unix(zchan, flags, to);
+#endif
 }
 
 zap_status_t wanpipe_init(zap_software_interface_t **zint)
@@ -539,6 +612,12 @@ zap_status_t wanpipe_init(zap_software_interface_t **zint)
 	return ZAP_SUCCESS;
 }
 
+#if defined(__WINDOWS__)
+#define close(handle) CloseHandle(handle)
+#endif
+
+#define zap_wanpipe_socket_close(it) if (it != WP_INVALID_SOCKET) { close(it); it = WP_INVALID_SOCKET;}
+
 zap_status_t wanpipe_destroy(void)
 {
 	unsigned int i,j;
@@ -551,7 +630,7 @@ zap_status_t wanpipe_destroy(void)
 				zap_channel_t *cur_chan = &cur_span->channels[j];
 				if (zap_test_flag(cur_chan, ZAP_CHANNEL_CONFIGURED)) {
 					zap_log(ZAP_LOG_INFO, "Closing channel %u:%u fd:%d\n", cur_chan->span_id, cur_chan->chan_id, cur_chan->sockfd);
-					zap_socket_close(cur_chan->sockfd);
+					zap_wanpipe_socket_close(cur_chan->sockfd);
 				}
 			}
 		}