提交 f0834d47 authored 作者: Michael Jerris's avatar Michael Jerris

implementations for the cross platform read and write in sangoma_tdm_api.h.

move zap_wanpipe.c to use the read and write from sangoma_tdm_api.h.
cleanups and lots of comments added to sangoma_tdm_api.h
some fixes to sangoma_tdm_api.h to make the api more consistent between windows and nix.

git-svn-id: http://svn.openzap.org/svn/openzap/trunk@51 a93c3328-9c30-0410-af19-c9cd2b2d52af
上级 d3efe9d8
......@@ -311,134 +311,14 @@ static ZINT_COMMAND_FUNCTION(wanpipe_command)
return ZAP_SUCCESS;
}
#ifdef __WINDOWS__
static ZINT_READ_FUNCTION(wanpipe_read_windows)
{
zap_size_t rx_len = 0;
zap_status_t status = ZAP_FAIL;
/* should we just pass in abuffer big enough in the first place instead of having to use rx_data and memcpy here? */
static RX_DATA_STRUCT rx_data;
if(DoReadCommand(zchan->sockfd, &rx_data)) {
snprintf(zchan->last_error, sizeof(zchan->last_error), "Error: DoReadCommand() failed! Check messages log.\n");
goto done;
}
switch(rx_data.api_header.operation_status)
{
case SANG_STATUS_RX_DATA_AVAILABLE:
if(rx_data.api_header.data_length > *datalen){
snprintf(zchan->last_error, sizeof(zchan->last_error), "Buffer overrun.\n");
break;
}
memcpy(data, rx_data.data, rx_data.api_header.data_length);
rx_len = rx_data.api_header.data_length;
status = ZAP_SUCCESS;
break;
case SANG_STATUS_TDM_EVENT_AVAILABLE:
memcpy(data, rx_data.data, rx_data.api_header.data_length);
rx_len = rx_data.api_header.data_length;
status = ZAP_SUCCESS;
break;
case SANG_STATUS_RX_DATA_TIMEOUT:
snprintf(zchan->last_error, sizeof(zchan->last_error), "Error: Timeout on read.\n");
break;
case SANG_STATUS_BUFFER_TOO_SMALL:
snprintf(zchan->last_error, sizeof(zchan->last_error), "Error: Received data longer than buffer passed to API.\n");
break;
case SANG_STATUS_LINE_DISCONNECTED:
snprintf(zchan->last_error, sizeof(zchan->last_error), "Error: Line disconnected.\n");
break;
default:
snprintf(zchan->last_error, sizeof(zchan->last_error), "Rx:Unknown Operation Status: %d\n", rx_data.api_header.operation_status);
break;
}
done:
*datalen = rx_len;
return status;
}
static ZINT_WRITE_FUNCTION(wanpipe_write_windows)
{
static TX_DATA_STRUCT local_tx_data;
/* why don't we just provide the big enough buffer to start with so we can avoid the memcpy ? */
memcpy(local_tx_data.data, data, *datalen);
/* queue data for transmission */
if(DoWriteCommand(zchan->sockfd, &local_tx_data)) {
snprintf(zchan->last_error, sizeof(zchan->last_error), "Error: DoWriteCommand() failed!! Check messages log.\n");
*datalen = 0;
return ZAP_FAIL;
}
if (local_tx_data.api_header.operation_status == SANG_STATUS_SUCCESS) {
return ZAP_SUCCESS;
}
*datalen = 0;
switch(local_tx_data.api_header.operation_status)
{
case SANG_STATUS_TX_TIMEOUT:
snprintf(zchan->last_error, sizeof(zchan->last_error), "****** Error: SANG_STATUS_TX_TIMEOUT ******\n");
break;
case SANG_STATUS_TX_DATA_TOO_LONG:
snprintf(zchan->last_error, sizeof(zchan->last_error), "****** SANG_STATUS_TX_DATA_TOO_LONG ******\n");
break;
case SANG_STATUS_TX_DATA_TOO_SHORT:
snprintf(zchan->last_error, sizeof(zchan->last_error), "****** SANG_STATUS_TX_DATA_TOO_SHORT ******\n");
break;
case SANG_STATUS_LINE_DISCONNECTED:
snprintf(zchan->last_error, sizeof(zchan->last_error), "****** SANG_STATUS_LINE_DISCONNECTED ******\n");
break;
default:
snprintf(zchan->last_error, sizeof(zchan->last_error), "Unknown return code (0x%X) on transmission!\n", local_tx_data.api_header.operation_status);
break;
}
return ZAP_FAIL;
}
#else
static ZINT_READ_FUNCTION(wanpipe_read_unix)
static ZINT_READ_FUNCTION(wanpipe_read)
{
int rx_len = 0;
struct msghdr msg;
struct iovec iov[2];
wp_tdm_api_rx_hdr_t hdrframe;
memset(&msg, 0, sizeof(msg));
memset(&hdrframe, 0, sizeof(hdrframe));
memset(iov, 0, sizeof(iov[0])*2);
iov[0].iov_len = sizeof(hdrframe);
iov[0].iov_base = &hdrframe;
iov[1].iov_len = *datalen;
iov[1].iov_base = data;
msg.msg_iovlen = 2;
msg.msg_iov = iov;
rx_len = read(zchan->sockfd, &msg, iov[1].iov_len + iov[0].iov_len);
if (rx_len > 0) {
rx_len -= sizeof(hdrframe);
}
rx_len = tdmv_api_readmsg_tdm(zchan->sockfd, &hdrframe, (int)sizeof(hdrframe), data, (int)*datalen);
*datalen = rx_len;
......@@ -450,37 +330,24 @@ static ZINT_READ_FUNCTION(wanpipe_read_unix)
return ZAP_SUCCESS;
}
static ZINT_WRITE_FUNCTION(wanpipe_write_unix)
static ZINT_WRITE_FUNCTION(wanpipe_write)
{
int bsent;
struct msghdr msg;
struct iovec iov[2];
wp_tdm_api_tx_hdr_t hdrframe;
memset(&msg, 0, sizeof(msg));
/* Do we even need the headerframe here? on windows, we don't even pass it to the driver */
memset(&hdrframe, 0, sizeof(hdrframe));
memset(iov, 0, sizeof(iov[0])*2);
iov[0].iov_len = sizeof(hdrframe);
iov[0].iov_base = &hdrframe;
iov[1].iov_len = *datalen;
iov[1].iov_base = data;
msg.msg_iovlen = 2;
msg.msg_iov = iov;
bsent = write(zchan->sockfd, &msg, iov[1].iov_len + iov[0].iov_len);
if (bsent > 0){
bsent -= sizeof(wp_tdm_api_tx_hdr_t);
}
bsent = tdmv_api_writemsg_tdm(zchan->sockfd, &hdrframe, (int)sizeof(hdrframe), data, (unsigned short)(*datalen));
/* should we be checking if bsent == *datalen here? */
if (bsent > 0) {
*datalen = bsent;
return ZAP_SUCCESS;
}
return ZAP_FAIL;
}
#endif
static ZINT_WAIT_FUNCTION(wanpipe_wait)
{
......@@ -527,13 +394,8 @@ zap_status_t wanpipe_init(zap_software_interface_t **zint)
wanpipe_interface.close = wanpipe_close;
wanpipe_interface.command = wanpipe_command;
wanpipe_interface.wait = wanpipe_wait;
#ifdef __WINDOWS__
wanpipe_interface.read = wanpipe_read_windows;
wanpipe_interface.write = wanpipe_write_windows;
#else
wanpipe_interface.read = wanpipe_read_unix;
wanpipe_interface.write = wanpipe_write_unix;
#endif
wanpipe_interface.read = wanpipe_read;
wanpipe_interface.write = wanpipe_write;
*zint = &wanpipe_interface;
return ZAP_SUCCESS;
......
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论