/*****************************************************************************
 *
 * Copyright (c) 1993-1994 Spider Systems Limited
 *
 * This Source Code is furnished under Licence, and may not be
 * copied or distributed without express written agreement.
 *
 * All rights reserved.  Made in Scotland.
 *
 * Authors: Neil McGill
 *
 * lmi.c of tune module
 *
 * SpiderISDN
 * Release 3.0.0 94/12/20
 * 
 * SpiderISDN Release 3 - Official Release.
 *
 *****************************************************************************/

/*
 * lmi.c
 */

#include <stdio.h>
#include <stdlib.h>
#include <windows.h>

#include "DsIoctl.h"
#include "dxbtypes.h"
#include "dsuser.h"

#ifndef	MAXIOCBSZ
#define MAXIOCBSZ 1024
#endif

#include "lmsi.h"
#include "lmi_errno.h"
#include "lmi.h"

#include "isdn_proto.h"
#include "il3_config.h"
#include "lapd_control.h"
#include "iwan_control.h"

#include "construct.h"

#define DigiDump(a,b)

#define AVL_COUNT	2
#define LMI_SIZE	200

#ifndef IL3_ERROR
#define IL3_ERROR 1
#endif

#ifndef NO_IL3_ERROR
#define NO_IL3_ERROR 0
#endif

#ifndef ERR_LMI
#define ERR_LMI   -22
#endif


int il3_set( HANDLE hFile,
             PVOID stream_id,
             uint8 attribute,
             char *pk,
             int pk_size,
             int *data,
             int size_data )
{
   BOOL bSuccess;
	list_info_t	lmi_request;

	int	lmi_size = LMI_SIZE;

	DigiDump( DIGICONFIG, ("lmi_request(attribute=%d,pk=%d)\n",attribute,pk) );

	if ( allocate_LMI_request( &lmi_request, lmi_size ) != LMI_OK ) 
	{
		return(ERR_LMI);
	}

	if ( construct_inst( IL3, AVL_COUNT, &lmi_request ) != LMI_OK )
	{
		return(ERR_LMI);
	}

	if ( construct_pk_AVL( pk, pk_size, &lmi_request) != LMI_OK ) 
	{
		return(ERR_LMI);
	}

	if ( construct_any_AVL( attribute,
                           (octet_t*)data,
		                     size_data,
                           &lmi_request ) != LMI_OK ) 
	{
		return(ERR_LMI);
	}

	bSuccess = DsIoctl( hFile, stream_id, LMSI_SET, lmi_request.base, lmi_request.size );
   if( !bSuccess  )
   {
      //
      // There was some sort of problem
      //
      printf( "DsIoctl LMSI_SET failed, GetLastError = 0x%x\n", GetLastError() );
		return ERR_LMI;
   } 

	skip_header(&lmi_request);
	skip_avl(&lmi_request);

	memcpy( (char*)data, LMI_VALUE(&lmi_request), size_data ); 

	deallocate_LMI_request( &lmi_request );
	return 0;
}


int il3_get( HANDLE hFile,
             PVOID stream_id,
             uint8 attribute,
             char *pk,
             int pk_size,
             int *data,
             int size_data )
{
   BOOL bSuccess;
	list_info_t	lmi_request;
	int	lmi_size = LMI_SIZE;

#ifdef LMI_DEBUG
	lmsi_debug = 1;
#endif

#ifdef DEBUG
	printf("lmi_request(attribute=%d,pk=%d)\n",attribute,pk);
#endif

	if ( allocate_LMI_request( &lmi_request, lmi_size ) != LMI_OK ) 
	{
		return(ERR_LMI);
	}

	if ( construct_inst( IL3, AVL_COUNT, &lmi_request ) != LMI_OK )
	{
		return(ERR_LMI);
	}

	if ( construct_pk_AVL( pk, pk_size, &lmi_request) != LMI_OK ) 
	{
		return(ERR_LMI);
	}

	if ( construct_zero_AVL( attribute, size_data, &lmi_request ) 
		!= LMI_OK ) 
	{
		return(ERR_LMI);
	}

	bSuccess = DsIoctl( hFile, stream_id, LMSI_GET, lmi_request.base, lmi_request.size );
   if( !bSuccess  )
   {
      //
      // There was some sort of problem
      //
      printf( "DsIoctl LMSI_GET failed, GetLastError = 0x%x\n", GetLastError() );
		return ERR_LMI;
   } 

	skip_header(&lmi_request);
	skip_avl(&lmi_request);

	memcpy( (char*)data, LMI_VALUE(&lmi_request), size_data ); 

	deallocate_LMI_request( &lmi_request );

	return(0);
}
