/*
 *   capitest - listen to capi controllers
 *   Copyright (C) 2004  Enrik Berkhan <Enrik.Berkhan@inka.de>
 *
 *   This program is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU General Public License as published by
 *   the Free Software Foundation; either version 2 of the License, or
 *   (at your option) any later version.
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU General Public License for more details.
 *   You should have received a copy of the GNU General Public License
 *   along with this program; if not, write to the Free Software
 *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include <sys/types.h>
#include <linux/capi.h>
#include <capi20.h>
#include <stdio.h>

int
main(int argc, char **argv) {
	_cmsg cmsg;
	unsigned ret;
	unsigned appl = 0;
	int i;
	
	if (0 != (ret = capi20_isinstalled())) {
		fprintf(stderr, "CAPI not installed: 0x%X %s\n", ret, capi20_info2str(ret));
		return 1;
	}

	if (0 != (ret = capi20_register(16, 7, 128, &appl))) {
		fprintf(stderr, "capi20_register: 0x%X %s\n", ret, capi20_info2str(ret));
		return 1;
	}
	printf("CAPI application: %d\n", appl);

	for (i = 1; i <= 4; i++)
	{
		if (0 != (ret = LISTEN_REQ(&cmsg, appl, i, i, 0x17FF, 1, 0, "", ""))) {
			fprintf(stderr, "LISTEN_REQ: 0x%X %s\n", ret, capi20_info2str(ret));
	                return 1;
	        }
	}

	while(capi20_waitformessage(appl, 0) == 0)
	{
		if (0 != ( ret = capi20_get_cmsg(&cmsg, appl))) {
                        fprintf(stderr, "capi20_get_cmsg: 0x%X %s\n", ret, capi20_info2str(ret));
			return 1;
		}
		printf("CMSG: %s\n", capi20_cmsg2str(&cmsg));
	}

	if (0 != (ret = capi20_release(appl))) {
		fprintf(stderr, "capi20_release: 0x%X %s\n", ret, capi20_info2str(ret));
		return 1;
	}

	return 0;
}
