OpenSS7
SS7 for the
Common Man

© Copyright 1997-2004,OpenSS7 Corporation, All Rights Reserved.
Last modified:

Home Overview Status News Documentation Resources About
   
 Overview
 Status
 News
 Documentation
 Resources
 About

   
Home Index Prev Next More Download Info FAQ Mail   Home -> Resources -> Browse Source -> strss7/test/test-x400p-sdl.c


File /code/strss7/test/test-x400p-sdl.c



#ident "@(#) $RCSfile: test-x400p-sdl.c,v $ $Name:  $($Revision: 0.8.2.3 $) $Date: 2002/10/24 21:43:52 $"

static char const ident[] =
    "$RCSfile: test-x400p-sdl.c,v $ $Name:  $($Revision: 0.8.2.3 $) $Date: 2002/10/24 21:43:52 $";

#include <stropts.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <ss7/lmi.h>
#include <ss7/lmi_ioctl.h>
#include <ss7/sdli.h>
#include <ss7/sdli_ioctl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

#define BUFSIZE 280

const char *lmi_strreason(unsigned int reason)
{
	const char *r;
	switch (reason) {
	default:
	case LMI_UNSPEC:
		r = "Unknown or unspecified";
		break;
	case LMI_BADADDRESS:
		r = "Address was invalid";
		break;
	case LMI_BADADDRTYPE:
		r = "Invalid address type";
		break;
	case LMI_BADDIAL:
		r = "(not used)";
		break;
	case LMI_BADDIALTYPE:
		r = "(not used)";
		break;
	case LMI_BADDISPOSAL:
		r = "Invalid disposal parameter";
		break;
	case LMI_BADFRAME:
		r = "Defective SDU received";
		break;
	case LMI_BADPPA:
		r = "Invalid PPA identifier";
		break;
	case LMI_BADPRIM:
		r = "Unregognized primitive";
		break;
	case LMI_DISC:
		r = "Disconnected";
		break;
	case LMI_EVENT:
		r = "Protocol-specific event ocurred";
		break;
	case LMI_FATALERR:
		r = "Device has become unusable";
		break;
	case LMI_INITFAILED:
		r = "Link initialization failed";
		break;
	case LMI_NOTSUPP:
		r = "Primitive not supported by this device";
		break;
	case LMI_OUTSTATE:
		r = "Primitive was issued from invalid state";
		break;
	case LMI_PROTOSHORT:
		r = "M_PROTO block too short";
		break;
	case LMI_SYSERR:
		r = "UNIX system error";
		break;
	case LMI_WRITEFAIL:
		r = "Unitdata request failed";
		break;
	case LMI_CRCERR:
		r = "CRC or FCS error";
		break;
	case LMI_DLE_EOT:
		r = "DLE EOT detected";
		break;
	case LMI_FORMAT:
		r = "Format error detected";
		break;
	case LMI_HDLC_ABORT:
		r = "Aborted frame detected";
		break;
	case LMI_OVERRUN:
		r = "Input overrun";
		break;
	case LMI_TOOSHORT:
		r = "Frame too short";
		break;
	case LMI_INCOMPLETE:
		r = "Partial frame received";
		break;
	case LMI_BUSY:
		r = "Telephone was busy";
		break;
	case LMI_NOANSWER:
		r = "Connection went unanswered";
		break;
	case LMI_CALLREJECT:
		r = "Connection rejected";
		break;
	case LMI_HDLC_IDLE:
		r = "HDLC line went idle";
		break;
	case LMI_HDLC_NOTIDLE:
		r = "HDLC link no longer idle";
		break;
	case LMI_QUIESCENT:
		r = "Line being reassigned";
		break;
	case LMI_RESUMED:
		r = "Line has been reassigned";
		break;
	case LMI_DSRTIMEOUT:
		r = "Did not see DSR in time";
		break;
	case LMI_LAN_COLLISIONS:
		r = "LAN excessive collisions";
		break;
	case LMI_LAN_REFUSED:
		r = "LAN message refused";
		break;
	case LMI_LAN_NOSTATION:
		r = "LAN no such station";
		break;
	case LMI_LOSTCTS:
		r = "Lost Clear to Send signal";
		break;
	case LMI_DEVERR:
		r = "Start of device-specific error codes";
		break;
	}
	return r;
}

void do_lmi_get_msg(int fd)
{
	int ret, flags = 0;
	struct strbuf ctrl;
	struct strbuf data;
	char cbuf[BUFSIZE];
	char dbuf[BUFSIZE];
	union LMI_primitives *p = (union LMI_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = 0;
	ctrl.buf = cbuf;
	data.maxlen = BUFSIZE;
	data.len = 0;
	data.buf = dbuf;
      do_get_again:
	printf("\nAttempting getmsg\n");
	if ((ret = getmsg(fd, &ctrl, &data, &flags)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		return;
	} else
		printf("Getmsg succeeded!\n");
	if (ret == 0) {
		if (ctrl.len > 0) {
			switch ((long) p->lmi_primitive) {
			case LMI_INFO_ACK:
			{
				int ppalen = ctrl.len - sizeof(p->info_ack);
				printf("LMI_INFO_ACK:\n");
				printf("Version = 0x%08lx\n", p->info_ack.lmi_version);
				printf("State = %lu\n", p->info_ack.lmi_state);
				printf("Max sdu = %lu\n", p->info_ack.lmi_max_sdu);
				printf("Min sdu = %lu\n", p->info_ack.lmi_min_sdu);
				printf("Header len = %lu\n", p->info_ack.lmi_header_len);
				printf("PPA style = %lu\n", p->info_ack.lmi_ppa_style);
				printf("PPA length = %d\n", ppalen);
				return;
			}
			case LMI_OK_ACK:
				printf("LMI_OK_ACK:\n");
				printf("Correct primitive = %ld\n",
				       p->ok_ack.lmi_correct_primitive);
				printf("State = %ld\n", p->ok_ack.lmi_state);
				return;
			case LMI_ERROR_ACK:
				printf("LMI_ERROR_ACK:\n");
				printf("Error number = %lu\n", p->error_ack.lmi_errno);
				printf("Error string = %s\n", strerror(p->error_ack.lmi_errno));
				printf("Reason number = %lu\n", p->error_ack.lmi_reason);
				printf("Reason string = %s\n",
				       lmi_strreason(p->error_ack.lmi_reason));
				printf("Error primitive = %lu\n", p->error_ack.lmi_error_primitive);
				printf("State = %lu\n", p->error_ack.lmi_state);
				return;
			case LMI_ERROR_IND:
				printf("LMI_ERROR_IND:\n");
				printf("Error number = %lu\n", p->error_ind.lmi_errno);
				printf("Error string = %s\n", strerror(p->error_ind.lmi_errno));
				printf("Reason number = %lu\n", p->error_ind.lmi_reason);
				printf("Reason string = %s\n",
				       lmi_strreason(p->error_ind.lmi_reason));
				printf("State = %lu\n", p->error_ind.lmi_state);
				goto do_get_again;
			case LMI_ENABLE_CON:
				printf("LMI_ENABLE_CON:\n");
				printf("State = %lu\n", p->enable_con.lmi_state);
				return;
			case LMI_DISABLE_CON:
				printf("LMI_DISABLE_CON:\n");
				printf("State = %lu\n", p->disable_con.lmi_state);
				return;
			case LMI_STATS_IND:
				printf("LMI_STATS_IND:\n");
				return;
			case LMI_EVENT_IND:
				printf("LMI_EVENT_IND:\n");
				return;
			case SDL_BITS_FOR_TRANSMISSION_REQ:
				printf("SDL_BITS_FOR_TRANSMISSION_REQ:\n");
				return;
			case SDL_CONNECT_REQ:
				printf("SDL_CONNECT_REQ:\n");
				return;
			case SDL_DISCONNECT_REQ:
				printf("SDL_DISCONNECT_REQ:\n");
				return;
			case SDL_RECEIVED_BITS_IND:
				printf("SDL_RECEIVED_BITS_IND:\n");
				return;
			case SDL_DISCONNECT_IND:
				printf("SDL_DISCONNECT_IND:\n");
				return;
			default:
				printf("Unrecognized response primitive %ld!\n",
				       (long) p->lmi_primitive);
				goto do_get_again;
			}
		}
	}
}

void info_req(int fd)
{
	int ret;
	struct strbuf ctrl;
	char cbuf[BUFSIZE];
	union LMI_primitives *p = (union LMI_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = sizeof(p->info_req);
	ctrl.buf = cbuf;
	p->lmi_primitive = LMI_INFO_REQ;
	printf("\nAttempting info request\n");
	if ((ret = putmsg(fd, &ctrl, NULL, RS_HIPRI)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		exit(2);
	} else
		printf("Putmsg succeeded!\n");
	do_lmi_get_msg(fd);
}

void attach_req(int fd, uint16_t ppa)
{
	int ret;
	struct strbuf ctrl;
	char cbuf[BUFSIZE];
	union LMI_primitives *p = (union LMI_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = sizeof(p->attach_req) + sizeof(ppa);
	ctrl.buf = cbuf;
	p->attach_req.lmi_primitive = LMI_ATTACH_REQ;
	bcopy(&ppa, p->attach_req.lmi_ppa, sizeof(ppa));
	printf("\nAttempting attach request\n");
	if ((ret = putmsg(fd, &ctrl, NULL, RS_HIPRI)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		exit(2);
	} else
		printf("Putmsg succeeded!\n");
	do_lmi_get_msg(fd);
}

void detach_req(int fd)
{
	int ret;
	struct strbuf ctrl;
	char cbuf[BUFSIZE];
	union LMI_primitives *p = (union LMI_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = sizeof(p->detach_req);
	ctrl.buf = cbuf;
	p->lmi_primitive = LMI_DETACH_REQ;
	printf("\nAttempting detach request\n");
	if ((ret = putmsg(fd, &ctrl, NULL, RS_HIPRI)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		exit(2);
	} else
		printf("Putmsg succeeded!\n");
	do_lmi_get_msg(fd);
}

void enable_req(int fd)
{
	int ret;
	struct strbuf ctrl;
	char cbuf[BUFSIZE];
	union LMI_primitives *p = (union LMI_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = sizeof(p->enable_req);
	ctrl.buf = cbuf;
	p->lmi_primitive = LMI_ENABLE_REQ;
	printf("\nAttempting enable request\n");
	if ((ret = putmsg(fd, &ctrl, NULL, RS_HIPRI)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		exit(2);
	} else
		printf("Putmsg succeeded!\n");
	do_lmi_get_msg(fd);
}

void disable_req(int fd)
{
	int ret;
	struct strbuf ctrl;
	char cbuf[BUFSIZE];
	union LMI_primitives *p = (union LMI_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = sizeof(p->disable_req);
	ctrl.buf = cbuf;
	p->lmi_primitive = LMI_DISABLE_REQ;
	printf("\nAttempting disable request\n");
	if ((ret = putmsg(fd, &ctrl, NULL, RS_HIPRI)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		exit(2);
	} else
		printf("Putmsg succeeded!\n");
	do_lmi_get_msg(fd);
}

void sdl_connect(int fd)
{
	int ret;
	struct strbuf ctrl;
	char cbuf[BUFSIZE];
	union SDL_primitives *p = (union SDL_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = sizeof(p->connect_req);
	ctrl.buf = cbuf;
	p->sdl_primitive = SDL_CONNECT_REQ;
	p->connect_req.sdl_flags = SDL_RX_DIRECTION | SDL_TX_DIRECTION;
	printf("\nAttempting CONNECT request\n");
	if ((ret = putmsg(fd, &ctrl, NULL, RS_HIPRI)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		exit(2);
	} else
		printf("Putmsg succeeded!\n");
	// do_lmi_get_msg(fd);
}
void sdl_disconnect(int fd)
{
	int ret;
	struct strbuf ctrl;
	char cbuf[BUFSIZE];
	union SDL_primitives *p = (union SDL_primitives *) cbuf;
	ctrl.maxlen = BUFSIZE;
	ctrl.len = sizeof(p->disconnect_req);
	ctrl.buf = cbuf;
	p->sdl_primitive = SDL_DISCONNECT_REQ;
	p->connect_req.sdl_flags = SDL_RX_DIRECTION | SDL_TX_DIRECTION;
	printf("\nAttempting DISCONNECT request\n");
	if ((ret = putmsg(fd, &ctrl, NULL, RS_HIPRI)) < 0) {
		printf("error = %d\n", errno);
		perror("test");
		exit(2);
	} else
		printf("Putmsg succeeded!\n");
	// do_lmi_get_msg(fd);
}

void sdl_write(int fd)
{
	const size_t len = 64;
	const uint8_t buf[] = {
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
		0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08
	};
	int i, ret;
	i = 0;
	printf("\nAttempting write\n");
	for (;;) {
		ret = write(fd, buf, len);
		if (ret < 0) {
			if (errno == EINTR || errno == ERESTART) {
				if (i++ < 100)
					continue;
			}
			if (errno == EAGAIN) {
				return;
			}
			printf("error = %d\n", errno);
			perror("test");
			exit(2);
		} else
			printf("Write succeeded, wrote %d bytes!\n", ret);
		if (ret > 0) {
			int i;
			printf("Message[%d]: ", len);
			for (i = 0; i < len; i++) {
				printf("%02X ", buf[i]);
			}
			printf("\n");
		}
	}
}

void sdl_read(int fd)
{
	uint8_t buf[256];
	int i, ret;
	i = 0;
	printf("\nAttempting read\n");
	for (;;) {
		ret = read(fd, buf, 256);
		if (ret < 0) {
			if (errno == EAGAIN || errno == EINTR || errno == ERESTART) {
				if (i++ < 100)
					continue;
			}
			if (errno == EBADMSG) {
				do_lmi_get_msg(fd);
				continue;
			}
			printf("error = %d\n", errno);
			perror("test");
			break;
		} else
			printf("Read succeeded, read %d bytes!\n", ret);
		if (ret > 0) {
			int i;
			uint8_t *c = buf;
			printf("Message[%d]: ", ret);
			for (i = 0; i < ret; i++, c++) {
				printf("%02X ", *c);
			}
			printf("\n");
		}
	}
}

int sdl_ioctl(int fd, int cmd, void *arg, int len)
{
	struct strioctl ctl = { cmd, 0, len, arg };
	int ret;
	ret = ioctl(fd, I_STR, &ctl);
	if (ret < 0) {
		printf("return = %d\n", ret);
		printf("error = %d\n", errno);
		perror("sdl_ioctl");
		exit(2);
	}
	return ctl.ic_len;
}

void do_ioctl(int fd, int cmd, int arg)
{
	int ret;
	ret = ioctl(fd, cmd, arg);
	if (ret < 0) {
		printf("return = %d\n", ret);
		printf("error = %d\n", errno);
		perror("do_ioctl");
		exit(2);
	}
}

void sdl_config(int fd)
{
	unsigned char buf[256];
	sdl_config_t *c = (sdl_config_t *) buf;
	sdl_statem_t *s = (sdl_statem_t *) buf;
	printf("Getting configuration\n");
	sdl_ioctl(fd, SDL_IOCGCONFIG, buf, sizeof(sdl_config_t));
	printf("Config:\n");
	printf("  ifflags       = %lu\n", c->ifflags);
	printf("  iftype        = %lu\n", c->iftype);
	printf("  ifrate        = %lu\n", c->ifrate);
	printf("  ifgtype       = %lu\n", c->ifgtype);
	printf("  ifgrate       = %lu\n", c->ifgrate);
	printf("  ifgcrc        = %lu\n", c->ifgcrc);
	printf("  ifmode        = %lu\n", c->ifmode);
	printf("  ifclock       = %lu\n", c->ifclock);
	printf("  ifcoding      = %lu\n", c->ifcoding);
	printf("  ifframing     = %lu\n", c->ifframing);
	printf("  ifblksize     = %lu\n", c->ifblksize);
	printf("  ifleads       = %lu\n", c->ifleads);
	printf("  ifalarms      = %lu\n", c->ifalarms);
	printf("  ifrxlevel     = %lu\n", c->ifrxlevel);
	printf("  iftxlevel     = %lu\n", c->iftxlevel);
	printf("  ifsync        = %lu\n", c->ifsync);
	printf("  ifsyncsrc[0]  = %lu\n", c->ifsyncsrc[0]);
	printf("  ifsyncsrc[1]  = %lu\n", c->ifsyncsrc[1]);
	printf("  ifsyncsrc[2]  = %lu\n", c->ifsyncsrc[2]);
	printf("  ifsyncsrc[3]  = %lu\n", c->ifsyncsrc[3]);
	sdl_ioctl(fd, SDL_IOCGSTATEM, buf, sizeof(sdl_statem_t));
	printf("State:\n");
	printf("  tx_state      = %lu\n", s->tx_state);
	printf("  rx_state      = %lu\n", s->rx_state);
}

void sdl_stats(int fd)
{
	unsigned char buf[256];
	sdl_stats_t *s = (sdl_stats_t *) buf;
	printf("Attempting stats collection\n");
	sdl_ioctl(fd, SDL_IOCGSTATS, buf, sizeof(sdl_stats_t));
	printf("Stats:\n");
	printf("  rx_octets............ = %lu\n", s->rx_octets);
	printf("  tx_octets............ = %lu\n", s->tx_octets);
	printf("  rx_overruns.......... = %lu\n", s->rx_overruns);
	printf("  tx_underruns......... = %lu\n", s->tx_underruns);
	printf("  rx_buffer_overflows.. = %lu\n", s->rx_buffer_overflows);
	printf("  tx_buffer_overflows.. = %lu\n", s->tx_buffer_overflows);
	printf("  lead_cts_lost........ = %lu\n", s->lead_cts_lost);
	printf("  lead_dcd_lost........ = %lu\n", s->lead_dcd_lost);
	printf("  carrier_lost......... = %lu\n", s->carrier_lost);
}

int main()
{
	int i, pfd[2];
	uint16_t ppa[2] = { 0x0012, 0x0112 };
	printf("Opening one stream\n");
	if ((pfd[0] = open("/dev/x400p-sdl", O_NONBLOCK | O_RDWR)) < 0) {
		perror("open");
		exit(2);
	}
	printf("Opening other stream\n");
	if ((pfd[1] = open("/dev/x400p-sdl", O_NONBLOCK | O_RDWR)) < 0) {
		perror("open");
		exit(2);
	}

	// do_ioctl(pfd[0], I_SRDOPT, RMSGD);
	// do_ioctl(pfd[1], I_SRDOPT, RMSGD);

	do_ioctl(pfd[0], I_SETCLTIME, 0);
	do_ioctl(pfd[1], I_SETCLTIME, 0);

	info_req(pfd[0]);
	info_req(pfd[1]);

	attach_req(pfd[0], ppa[0]);
	attach_req(pfd[1], ppa[1]);

	sdl_config(pfd[0]);
	sdl_config(pfd[1]);
	sdl_stats(pfd[0]);
	sdl_stats(pfd[1]);

	enable_req(pfd[0]);
	enable_req(pfd[1]);

	sdl_config(pfd[0]);
	sdl_config(pfd[1]);
	sdl_stats(pfd[0]);
	sdl_stats(pfd[1]);

	sdl_connect(pfd[0]);
	sdl_connect(pfd[1]);

	sdl_config(pfd[0]);
	sdl_config(pfd[1]);
	sdl_stats(pfd[0]);
	sdl_stats(pfd[1]);

	// sleep(20);

	sdl_read(pfd[0]);
	sdl_read(pfd[1]);

	/* 
	 *  Do some reads and writes.
	 */
	for (i = 0; i < 100; i++) {
		sdl_write(pfd[0]);
		sdl_read(pfd[1]);
		sdl_write(pfd[1]);
		sdl_read(pfd[0]);
	}

	sdl_config(pfd[0]);
	sdl_config(pfd[1]);
	sdl_stats(pfd[0]);
	sdl_stats(pfd[1]);

	sdl_disconnect(pfd[1]);
	sdl_disconnect(pfd[0]);

	sdl_config(pfd[0]);
	sdl_config(pfd[1]);
	sdl_stats(pfd[0]);
	sdl_stats(pfd[1]);

	disable_req(pfd[1]);
	disable_req(pfd[0]);

	sdl_config(pfd[0]);
	sdl_config(pfd[1]);
	sdl_stats(pfd[0]);
	sdl_stats(pfd[1]);

	detach_req(pfd[1]);
	detach_req(pfd[0]);

	printf("Closing one side\n");
	close(pfd[0]);
	printf("Closing other side\n");
	close(pfd[1]);

	printf("Done\n");
	return (0);
}


Home Index Prev Next More Download Info FAQ Mail   Home -> Resources -> Browse Source -> strss7/test/test-x400p-sdl.c

OpenSS7
SS7 for the
Common Man
Home Overview Status News Documentation Resources About

© Copyright 1997-2004,OpenSS7 Corporation, All Rights Reserved.
Last modified: