www.pudn.com > d4j.zip > ISDNChannel.java


// local.dialogic.ISDNChannel 
// Implements ISDN functionality 
// $Id: ISDNChannel.java,v 1.7 2003/11/13 11:47:22 cgm8 Exp $ 
/*  
 * Copyright (c) 1999 Carlos G Mendioroz. 
 * 
 *  This file is part of D4J. 
 * 
 *  D4J is free software; you can redistribute it and/or 
 *  modify it under the terms of the GNU Lesser General Public 
 *  License as published by the Free Software Foundation; either 
 *  version 2 of the License, or (at your option) any later version. 
 *   
 *  D4J 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 
 *  Lesser General Public License for more details. 
 *   
 *  You should have received a copy of the GNU Lesser General Public 
 *  License along with this library; if not, write to the 
 *  Free Software Foundation, Inc., 59 Temple Place - Suite 330, 
 *  Boston, MA  02111-1307, USA. 
 * 
 * Report problems and direct all questions to: 
 * 
 *	tron@acm.org 
 */ 
 
package local.dialogic; 
 
public class ISDNChannel extends Channel implements Runnable { 
 
    // Constants 
    private static final int RESET = 0; // NULL  
    private static final int IDLE = 1; // Aka UNBLOCKED 
    private static final int ICALL = 2;// OFFERED 
    private static final int RINGS = 3;// ACCEPTED 
    private static final int IN = 4;   // CONNECTED incoming 
    private static final int OFFH = 5; // DIALING 
    private static final int DIAL = 6; // ALERTING 
    private static final int OUT = 7;  // CONNECTED outgoing 
     
    // Constants from elsewhere 
/* 
 * Single-byte Codeset 0 information elements 
 */ 
    public static final int CODESET_SHIFT_IE         = 0x90;  /* Codeset shift - Type 1 */ 
    public static final int MORE_DATA_IE             = 0xA0;  /* More data - Type 2 */ 
    public static final int SENDING_COMPLETE_IE      = 0xA1;  /* Send complete - Type 2 */ 
    public static final int CONGEST_LEVEL_IE         = 0xB0;  /* Congestion level - Type 1 */ 
    public static final int REPEAT_IND_IE            = 0xD0;  /* Repeat indicator - Type 1 */    
 
/*                                     
 * Multi-byte Codeset 0 information elements 
 */ 
    public static final int SEGMENTED_MSG_IE         = 0x00;  /* Segmented message */ 
    public static final int CHANGE_STATUS_IE         = 0x01;  /* Change status maint. ie */ 
    public static final int BEARER_CAPABILITY_IE     = 0x04;  /* Bearer capability */ 
    public static final int CAUSE_IE                 = 0x08;  /* Cause elem */ 
    public static final int CONNECTED_NUM_IE         = 0x0C;  /* Connected number */ 
 
    public static final int CALL_IDENT_IE            = 0x10;  /* Call identity */ 
    public static final int CALL_STATE_IE            = 0x14;  /* Call state */ 
    public static final int CHANNEL_ID_IE            = 0x18;  /* Channel identification */ 
    public static final int FACILITY_IE              = 0x1C;  /* Facility element */ 
    public static final int PROGRESS_IND_IE          = 0x1E;  /* Progress indicator */ 
 
    public static final int NETWORK_FACILITY_IE      = 0x20;  /* Network-specific facility */ 
    public static final int NATIONAL_FACILITY_IE     = NETWORK_FACILITY_IE;      /* VN3 element */ 
    public static final int ENDPOINT_ID_IE           = 0x26;  /* Endpoint Identification */ 
    public static final int NOTIFY_IND_IE            = 0x27;  /* Notification indicator */ 
    public static final int DISPLAY_IE               = 0x28;  /* Display element */ 
    public static final int TIME_DATE_IE             = 0x29;  /* Time/Date element */ 
    public static final int KEYPAD_FACILITY_IE       = 0x2C;  /* Keypad facility */ 
 
/* 
 * Values used in CHAN_STATUS message. 
 */ 
    public static final int CHAN_STATUS_IE           = 0x30;  /* Channel status */ 
 
/* 
 * Values used in STATUS_UPDATE message 
 */ 
    public static final int UPDATE_IE                = 0x31;  /* update info */ 
 
    public static final int CONGESTION_IE            = 0x31;  /* Channel status */ 
 
    public static final int INFO_REQ_IE              = 0x32;  /* Information request */ 
    public static final int SIGNAL_IE                = 0x34;  /* Signal element */ 
    public static final int SWITCHHOOK_IE            = 0x36;  /* Switchhook element */ 
    public static final int FEATURE_ACTIV_IE         = 0x38;  /* Feature activation */ 
    public static final int FEATURE_IND_IE           = 0x39;  /* Feature indication */ 
 
    public static final int INFO_RATE_IE             = 0x40;  /* Information rate */ 
    public static final int TRANSIT_DELAY_EE_IE      = 0x42;  /* End to end transit delay */ 
    public static final int TRANSIT_DELAY_SEL_IE     = 0x43;  /* Transit delay select & indic */ 
    public static final int PACKET_BIN_PARMS_IE      = 0x44;  /* Packet layer binary parameters */ 
    public static final int PACKET_WINDOW_IE         = 0x45;  /* Packet layer window size */ 
    public static final int PACKET_SIZE_IE           = 0x46;  /* Packet size */ 
 
    public static final int CALLER_NUMBER_IE         = 0x6C;  /* Calling party number */ 
    public static final int CALLER_SUB_ADDR_IE       = 0x6D;  /* Calling party sub-address */ 
 
    public static final int CALLED_NUMBER_IE         = 0x70;  /* Called party number */ 
    public static final int CALLED_SUB_ADDR_IE       = 0x71;  /* Called party sub-address */ 
    public static final int ORIGINAL_CALLED_NUM_IE   = 0x73;  /* Original Called Number */ 
    public static final int REDIRECTING_NUM_IE       = 0x74;  /* Redirecting number */ 
    public static final int REDIRECTION_NUM_IE       = 0x76;  /* Redirection number */ 
    public static final int TRANSIT_NETWORK_SEL_IE   = 0x78;  /* Transit network selection */ 
    public static final int RESTART_IND_IE           = 0x79;  /* Re-start indication */ 
    public static final int USER_USER_FAC_IE         = 0x7A;  /* User-to-user facility */ 
    public static final int LOW_LAYER_COMPAT_IE      = 0x7C;  /* Low layer compatibility */ 
    public static final int HIGH_LAYER_COMPAT_IE     = 0x7D;  /* High layer compatibility */ 
    public static final int USER_USER_IE             = 0x7E;  /* User-to-User */ 
    public static final int ESCAPE_FOR_EXT_IE        = 0x7F;  /* Escape for extension */ 
 
    public static final int LOCKING_SHIFT_TO_6       = 0x96;  /* for VerifyIEs function */ 
    public static final int CODESET_6_UNLOCKED       = 0x86;  /* for VerifyIEs function */ 
    public static final int CODESET_6_LOCKED         = 0x06;  /* for VerifyIEs function */ 
 
    public static final int ADVICE_OF_CHARGE_IE      = 0x01;  /* NTT-KDD advice of charge */ 
                                  
/* 
 * Equates for supported value of Information Element 
 */ 
 
/* 
 *     Calling party number element 
 */ 
    public static final int ISDN_PI_RESTRICT         = 0x01;  /* presentation indicator - restricted */ 
    public static final int ISDN_PI_NOTAVAIL         = 0x02;  /* presentation indicator - not avail */ 
    public static final int ISDN_PI_RESRV            = 0x03;  /* presentation indicator - reserved  */ 
    public static final int ISDN_SI_USRVERSCRN       = 0x01;  /* screening indicator - verified and passed */ 
    public static final int ISDN_SI_USRVERFAIL       = 0x02;  /* screening indicator - verified and failed */ 
    public static final int ISDN_SI_NET              = 0x03;  /* screening indicator - network provided */ 
 
/* 
 *     Congestion Level element 
 */ 
    public static final int ISDN_RCV_READY           = 0x00;  /* ready to receive more user to user info */ 
    public static final int ISDN_RCV_NOT_READY       = 0x01;  /* not ready to receive more user to user info */ 
 
/* 
 *     Network-Specific facility element 
 */ 
    public static final int ISDN_FEATURE             = 0x00;  /* feature/service - feature */ 
    public static final int ISDN_SERVICE             = 0x01;  /* feature/service - service */ 
    public static final int ISDN_CPN_PREF            = 0x01;  /* facility coding - CPN perferred */ 
    public static final int ISDN_BN_PREF             = 0x02;  /* facility coding - BN preferred */ 
    public static final int ISDN_CPN                 = 0x03;  /* facility coding - CPN */ 
    public static final int ISDN_BN                  = 0x04;  /* facility coding - BN */ 
                                  
    public static final int ISDN_SDN                 = 0x01;  /* service coding - SDN */ 
    public static final int ISDN_MEGACOM800          = 0x02;  /* service coding - MEGACOM 800 */ 
    public static final int ISDN_MEGACOM             = 0x03;  /* service coding - MEGACOM */ 
    public static final int ISDN_WATS                = 0x04;  /* service coding - WATS */ 
    public static final int ISDN_TIE                 = 0x05;  /* service coding - TIE */ 
    public static final int ISDN_ACCUNET             = 0x06;  /* service coding - ACCUNET SDS */ 
    public static final int ISDN_LONG_DS             = 0x07;  /* service coding - Long distance */ 
    public static final int ISDN_INT_800             = 0x08;  /* service coding - internation 800 */ 
    public static final int ISDN_CA_TSC              = 0x09;  /* service coding - CA TSC */ 
    public static final int ISDN_ATT_MULTIQ          = 0x10;  /* service coding - ATT MultiQuest */ 
    public static final int ISDN_TNID_USER           = 0x00;  /* network id type - user specified */ 
    public static final int ISDN_TNID_NAT            = 0x02;  /* network id type - national */ 
    public static final int ISDN_TNID_INTER          = 0x03;  /* network id type - international */ 
    public static final int ISDN_NIDPLN_UNK          = 0x00;  /* network id plan - unknown */ 
    public static final int ISDN_NIDPLN_CIC          = 0x01;  /* network id plan - carrier id code */ 
    public static final int ISDN_NIDPLN_DNIC         = 0x03;  /* network id plan - datanetwork id co */ 
                                  
/* 
 * Information Transfer Capability 
 */ 
    public static final int BEAR_CAP_SPEECH          = 0x00;  /* Speech Bearer Capability */ 
    public static final int BEAR_CAP_UNREST_DIG      = 0x08;  /* Unrestricted Digital Capability */ 
    public static final int BEAR_CAP_REST_DIG        = 0x09;  /* Restricted Digital Capability */ 
    public static final int BEAR_CAP_3DOT1K_AUDIO    = 0x10;  /* 3.1KHz Audio Capability */ 
    public static final int BEAR_CAP_7K_AUDIO        = 0x11;  /* 7KHz Audio Capability */ 
    public static final int BEAR_CAP_VIDEO           = 0x18;  /* Video Capability */ 
 
    public static final int ISDN_ITM_CIRCUIT         = 0x00;  /* info transfer mode - circuit mode */ 
    public static final int ISDN_ITM_PACKET          = 0x02;  /* info transfer mode - packet mode */ 
 
/*                                  
 * Information Transfer Rate 
 */ 
    public static final int BEAR_RATE_64KBPS         = 0x10;  /* B_CHANNEL_UNITS 1X64 */ 
    public static final int BEAR_RATE_128KBPS        = 0x11;  /* Non-standard 2X64       */ 
    public static final int BEAR_RATE_384KBPS        = 0x13;  /* H0_CHANNEL_UNITS 6X64   */ 
    public static final int BEAR_RATE_1536KBPS       = 0x15;  /* H11_CHANNEL_UNITS 24X64 */ 
    public static final int BEAR_RATE_1920KBPS       = 0x17;  /* H12_CHANNEL_UNITS 30X64 */ 
 
/* 
 *    Bearer Capability Element 
 */ 
    public static final int ISDN_UIL1_CCITTV110      = 0x01;  /* user info layer 1 - CCITT V.110/X.3 */ 
    public static final int ISDN_UIL1_G711ULAW       = 0x02;  /* user info layer 1 - G.711 u-law */ 
    public static final int ISDN_UIL1_G711ALAW       = 0x03;  /* user info layer 1 - G.711 A-law */ 
    public static final int ISDN_UIL1_G721ADCPM      = 0x04;  /* user info layer 1 - G.721 ADCPM */ 
    public static final int ISDN_UIL1_G722G725       = 0x05;  /* user info layer 1 - G.722 and G.725 */ 
    public static final int ISDN_UIL1_G722F725       = 0x05;  /* user info layer 1 - G.722 and G.725 */ 
    public static final int ISDN_UIL1_H261           = 0x06;  /* user info layer 1 - H.261 */ 
    public static final int ISDN_UIL1_NONCCITT       = 0x07;  /* user info layer 1 - non-CCITT */ 
    public static final int ISDN_UIL1_CCITTV120      = 0x08;  /* user info layer 1 - CCITT V.120 */ 
    public static final int ISDN_UIL1_CCITTX31       = 0x09;  /* user info layer 1 - CCITT X.31 */ 
    public static final int ISDN_UIL1_DEFAULT        = 0xFF;  /* user info layer 1 - Default Value */ 
 
    public static final int ISDN_UR_EINI460          = 0x00;  /* user rate - E bits in I.460 */ 
    public static final int ISDN_UR_600              = 0x01;  /* user rate - 0.6 kbits */ 
    public static final int ISDN_UR_1200             = 0x02;  /* user rate - 1.2 kbits */ 
    public static final int ISDN_UR_2400             = 0x03;  /* user rate - 2.4 kbits */ 
    public static final int ISDN_UR_3600             = 0x04;  /* user rate - 3.6 kbits */ 
    public static final int ISDN_UR_4800             = 0x05;  /* user rate - 4.8 kbits */ 
    public static final int ISDN_UR_7200             = 0x06;  /* user rate - 7.2 kbits */ 
    public static final int ISDN_UR_8000             = 0x07;  /* user rate - 8.0 kbits */ 
    public static final int ISDN_UR_9600             = 0x08;  /* user rate - 9.6 kbits */ 
    public static final int ISDN_UR_14400            = 0x09;  /* user rate - 14.4 kbits */ 
    public static final int ISDN_UR_16000            = 0x0A;  /* user rate - 16.0 kbits */ 
    public static final int ISDN_UR_19200            = 0x0B;  /* user rate - 19.2 kbits */ 
    public static final int ISDN_UR_32000            = 0x0C;  /* user rate - 32 kbits */ 
    public static final int ISDN_UR_48000            = 0x0E;  /* user rate - 48 kbits */ 
    public static final int ISDN_UR_56000            = 0x0F;  /* user rate - 56 kbits */ 
    public static final int ISDN_UR_64000            = 0x10;  /* user rate - 64 kbits */ 
    public static final int ISDN_UR_134              = 0x15;  /* user rate - .1345 kbits */ 
    public static final int ISDN_UR_100              = 0x16;  /* user rate - .100 kbits */ 
    public static final int ISDN_UR_75_1200          = 0x17;  /* user rate - .075/1200 kbits */ 
    public static final int ISDN_UR_1200_75          = 0x18;  /* user rate - 1200/.075 kbits */ 
    public static final int ISDN_UR_50               = 0x19;  /* user rate - .050 kbits */ 
    public static final int ISDN_UR_75               = 0x1A;  /* user rate - .075 kbits */ 
    public static final int ISDN_UR_110              = 0x1B;  /* user rate - .110 kbits */ 
    public static final int ISDN_UR_150              = 0x1C;  /* user rate - .150 kbits */ 
    public static final int ISDN_UR_200              = 0x1D;  /* user rate - .200 kbits */ 
    public static final int ISDN_UR_300              = 0x1E;  /* user rate - .300 kbits */ 
    public static final int ISDN_UR_12000            = 0x1F;  /* user rate - 12 kbits */ 
    public static final int ISDN_UR_DEFAULT          = 0xFF;  /* user rate - Default */ 
                                  
/* 
 * CALLER/CALLED_NUMBER_IE  
 */ 
 
/* defines for CLC (these take the place of 'type of number' in Called/Calling IEs: - 627a206 */ 
 
    public static final int	DPNSS_CLC_UNKNOWN	= 0x00; 
    public static final int	DPNSS_CLC_ORD		= 0x01; 
    public static final int	DPNSS_CLC_DEC		= 0x02; 
    public static final int	DPNSS_CLC_ISDN		= 0x03; 
    public static final int	DPNSS_CLC_PSTN		= 0x04; 
    public static final int	DPNSS_CLC_MF5		= 0x05; 
    public static final int	DPNSS_CLC_OP		= 0x06; 
    public static final int	DPNSS_CLC_NET		= 0x07; 
 
/* 
 * CALLER/CALLED_NUMBER_IE  
 */ 
 
/* 
 * Number types 
 */ 
    public static final int EN_BLOC_NUMBER           = 0x00;  /* Number is sent en-bloc */ 
    public static final int INTL_NUMBER              = 0x01;  /* International number */ 
    public static final int NAT_NUMBER               = 0x02;  /* National number */ 
    public static final int LOC_NUMBER               = 0x04;  /* Loacl (directory) number */ 
    public static final int OVERLAP_NUMBER           = 0x05;  /* Number is sent overlap */ 
 
/* 
 * Numbering plans 
 */ 
    public static final int UNKNOWN_NUMB_PLAN        = 0x00;  /* Unknown plan */ 
    public static final int ISDN_NUMB_PLAN           = 0x01;  /* ISDN numb. plan E.164 */ 
    public static final int TELEPHONY_NUMB_PLAN      = 0x02;  /* Telephony numb. plan E.163 */ 
    public static final int PRIVATE_NUMB_PLAN        = 0x09;  /* Private numbering plan */ 
 
/* 
 * Presentation indicators 
 */ 
    public static final int PRESENTATION_ALLOWED     = 0x00;  /* Default */ 
 
/* 
 * Screening indicators 
 */ 
    public static final int USER_PROVIDED            = 0x00;  /* Default */ 
 
/* 
 * CALLER_SUB_ADDR_IE and CALLED_SUB_ADDR_IE 
 */ 
    public static final int OSI_SUB_ADDR             = 0x00;  /* OSI Sub-address */ 
    public static final int USER_SPECIFIC_SUB_ADDR   = 0x02;  /* User-specific Sub-address */ 
    public static final int IA_5_FORMAT              = 0x50;  /* IA 5 sub-address digit format */ 
    public static final int MAX_SUB_ADDR_LEN         = 23;    /* Japanese Len > CCITT default of 23 */ 
 
/* 
 * CAUSE_IE 
 */ 
    public static final int CAUSE_LENGTH             = 0x02;  /* Internal length = 2 */ 
    public static final int DIAG_LEN                 = 0x03;  /* Maximum diagnostic length */ 
    public static final int NO_DIAG                  = 0x00;  /* No CAUSE diagnostic specified */ 
 
/* 
 * Cause values 
 * 
 * NOTE: Not all cause values are universally supported across switch types. 
 *       Before a particular cause value is used, its validity should be 
 *       compared with the appropriate switch vendor specs. 
 */ 
    public static final int UNASSIGNED_NUMBER        = 0x01;  /* Cause 01 */ 
    public static final int NO_ROUTE                 = 0x02;  /* Cause 02 */ 
    public static final int CHANNEL_UNACCEPTABLE     = 0x06;  /* Cause 06 */ 
    public static final int NORMAL_CLEARING          = 0x10;  /* Cause 16 */ 
    public static final int USER_BUSY                = 0x11;  /* Cause 17 */ 
    public static final int NO_USER_RESPONDING       = 0x12;  /* Cause 18 */ 
    public static final int NO_ANSWER_FROM_USER      = 0x13;  /* Cause 19 */        
    public static final int CALL_REJECTED            = 0x15;  /* Cause 21 */ 
    public static final int NUMBER_CHANGED           = 0x16;  /* Cause 22 */ 
    public static final int DEST_OUT_OF_ORDER        = 0x1B;  /* Cause 27 */ 
    public static final int INVALID_NUMBER_FORMAT    = 0x1C;  /* Cause 28 */ 
    public static final int FACILITY_REJECTED        = 0x1D;  /* Cause 29 */ 
    public static final int RESP_TO_STAT_ENQ         = 0x1E;  /* Cause 30 */ 
    public static final int UNSPECIFIED_CAUSE        = 0x1F;  /* Cause 31 */ 
    public static final int NO_CIRCUIT_AVAILABLE     = 0x22;  /* Cause 34 */ 
    public static final int NETWORK_OUT_OF_ORDER     = 0x26;  /* Cause 38 */ 
    public static final int TEMPORARY_FAILURE        = 0x29;  /* Cause 41 */ 
    public static final int NETWORK_CONGESTION       = 0x2A;  /* Cause 42 */ 
    public static final int ACCESS_INFO_DISCARDED    = 0x2B;  /* Cause 43 */ 
    public static final int REQ_CHANNEL_NOT_AVAIL    = 0x2C;  /* Cause 44 */ 
    public static final int PRE_EMPTED               = 0x2D;  /* Cause 45 */ 
    public static final int FACILITY_NOT_SUBSCRIBED  = 0x32;  /* Cause 50 */ 
    public static final int OUTGOING_CALL_BARRED     = 0x34;  /* Cause 52 */ 
    public static final int INCOMING_CALL_BARRED     = 0x36;  /* Cause 54 */ 
    public static final int BEAR_CAP_NOT_AVAIL       = 0x3A;  /* Cause 58 */ 
    public static final int SERVICE_NOT_AVAIL        = 0x3F;  /* Cause 63 */ 
    public static final int CAP_NOT_IMPLEMENTED      = 0x41;  /* Cause 65 */ 
    public static final int CHAN_NOT_IMPLEMENTED     = 0x42;  /* Cause 66 */ 
    public static final int FACILITY_NOT_IMPLEMENT   = 0x45;  /* Cause 69 */ 
    public static final int INVALID_CALL_REF         = 0x51;  /* Cause 81 */ 
    public static final int CHAN_DOES_NOT_EXIST      = 0x52;  /* Cause 82 */ 
    public static final int INCOMPATIBLE_DEST        = 0x58;  /* Cause 88 */ 
    public static final int INVALID_MSG_UNSPEC       = 0x5F;  /* Cause 95 */ 
    public static final int MANDATORY_IE_MISSING     = 0x60;  /* Cause 96 */ 
    public static final int NONEXISTENT_MSG          = 0x61;  /* Cause 97 */ 
    public static final int WRONG_MESSAGE            = 0x62;  /* Cause 98 */ 
    public static final int BAD_INFO_ELEM            = 0x63;  /* Cause 99 */ 
    public static final int INVALID_ELEM_CONTENTS    = 0x64;  /* Cause 100 */ 
    public static final int WRONG_MSG_FOR_STATE      = 0x65;  /* Cause 101 */ 
    public static final int TIMER_EXPIRY             = 0x66;  /* Cause 102 */ 
    public static final int MANDATORY_IE_LEN_ERR     = 0x67;  /* Cause 103 */ 
    public static final int PROTOCOL_ERROR           = 0x6F;  /* Cause 111 */ 
    public static final int INTERWORKING_UNSPEC      = 0x7F;  /* Cause 127 */ 
 
/*  
   The Cause value greater than 127 is allowed to define here. There is 
   no conflict with those universal defines which ranges 0 - 127  
*/ 
    public static final int RESTART_ON_NON_PRIMARY = 0x80;                         /* Cause 128 */ 
 
 
/* 
 * CHANNEL_ID_IE 
 */ 
    public static final int  ISDN_CHAN_EXCLUSIVE     = 0x01; 
 
/* 
 * B-Channel choices for the Channel Identification IE. 
 */ 
    public static final int B_CHANNEL_UNITS          = 3;     /* Chan type -   single B channel */ 
    public static final int B_CHANNEL                = B_CHANNEL_UNITS;     /* Borrow the ID */ 
    public static final int CHANNEL_1X64             = B_CHANNEL;           /* BEAR_RATE_64KBPS */ 
 
/* 
 * CONGEST_LEVEL_IE 
 */ 
    public static final int NOT_CONGESTED            = 0;     /* Level = NOT_CONGESTED */ 
    public static final int CONGESTED                = 0x0F;  /* Level = CONGESTED */ 
 
 
/* 
 * HIGH_LAYER_COMPAT_IE 
 */ 
 
/* 
 * Coding Standards  
 */ 
 
    public static final int CCITT_STANDARD            = 0;    /* Coding standard */ 
    public static final int CEPT_NATIONAL_STANDARD    = 2;    /* CEPT/National standard */ 
 
    public static final int NATIONAL_INTERPRETATION   = 0;    /* Interpretation value */ 
    public static final int CCITT_CEPT_INTERPRETATION = 4;    /* Interpretation value */ 
    public static final int PROFILE_PRESENTATION      = 1;    /* Presentation value */ 
 
/* 
 * CCITT Teleservices 
 */ 
    public static final int TELEPHONY_SERVICE        = 0x01;  /* Teleservice */ 
    public static final int GR3_FAX_SERVICE          = 0x04;  /* Teleservice */ 
    public static final int TELETEX_MIXED_SERVICE    = 0x24;  /* Teleservice */ 
    public static final int GR4_FAX_SERVICE          = 0x21;  /* Teleservice */ 
    public static final int TELETEX_CHAR_SERVICE     = 0x31;  /* Teleservice */ 
    public static final int ALPHA_MOS_VID_SERVICE    = 0x32;  /* Teleservice */ 
    public static final int TELEX_SERVICE            = 0x35;  /* Teleservice */ 
    public static final int MHS_SERVICE              = 0x38;  /* Teleservice */ 
    public static final int OSI_APPL_SERVICE         = 0x41;  /* Teleservice */ 
                                  
/* 
 * CEPT Teleservices 
 */ 
    public static final int UNKNOWN_SERVICE          = 0x00;  /* Teleservice (interworking) */ 
 
/* 
 * National Standard Teleservices 
 */ 
    public static final int AUDIOSCRIPT_SERVICE      = 0x40;  /* Teleservice */ 
    public static final int AUDIOVIDEOTEX_SERVICE    = 0x48;  /* Teleservice */ 
    public static final int NON_STD_APPL_SERVICE     = 0x7F;  /* Teleservice */ 
 
/* 
 * PROGRESS_IND_IE  
 */ 
 
/* 
 * Progress values 
 */ 
    public static final int CALL_NOT_END_TO_END_ISDN = 1;     /* Progress indicator value */ 
    public static final int IN_BAND_INFO             = 8;     /* Progress indicator value */ 
 
 
/* 
 * USER_USER_IE  
 */ 
    public static final int USER_SPEC_PROTO          = 0x00;  /* User-specific protocol */ 
    public static final int OSI_HIGH_LAYER_PROTO     = 0x01;  /* OSI high layer protocol */ 
    public static final int X244_PROTO               = 0x02;  /* X.244 protocol */ 
    public static final int MAINT_MGMT_PROTO         = 0x03;  /* Maintenance/management protocol */ 
    public static final int IA5_CHAR_PROTO           = 0x04;  /* IA5 character protocol */ 
    public static final int V120_RATE_ADPT_PROTO     = 0x07;  /* V.120 rate adaption protocol */ 
    public static final int Q931_USER_NET_PROTO      = 0x08;  /* Q.931 user-network protocol */ 
    public static final int NT2_NT2_FAC_PROTO        = 0x40;  /* VN2 NT2-NT2 facilities protocol */ 
    public static final int MINI_MSG_SERV_PROTO      = 0x41;  /* VN2 mini-msg handling protocol */ 
    public static final int MAX_UUI_LEN              = 131;   /* A network-dependent value */ 
 
/* 
 * "update" values 
 */ 
    public static final int DATA_LINK_DOWN           = 0x01;  /* CC->Host data link failure report */ 
    public static final int DATA_LINK_UP             = 0x02;  /* CC->Host data link restore report */ 
    public static final int EST_DATA_LINK            = 0x03;  /* Host->CC data link restore request */ 
    public static final int REL_DATA_LINK            = 0x04;  /* Host->CC data link restore request */ 
    public static final int DATA_LINK_RESET          = 0x05;  /* CC->Host data link reset for DASS2 */ 
 
/* 
 * vari-a-bill 
 */ 
    public static final int FLEX_BILLING_AVAILABLE      = 0x01; 
    public static final int FLEX_BILLING_UNAVAILABLE    = 0x02; 
    public static final int FLEX_BILLING_ACCEPTED       = 0x03; 
    public static final int FLEX_BILLING_BAD_OPERATION  = 0x04; 
    public static final int FLEX_BILLING_BAD_ARGUMENT   = 0x05; 
    public static final int FLEX_BILLING_RETURN_ERR     = 0x06; 
    public static final int FLEX_BILLING_IE_ERR         = 0x07; 
    public static final int NO_FLEX_BILLING_INFO        = 0x08; 
 
/* 
 * definitions for flex billing billing type field 
 */ 
    public static final int BILLING_NEW_RATE            = 0x10; 
    public static final int BILLING_FLAT_RATE           = 0x11; 
    public static final int BILLING_PREMIUM_CHARGE      = 0x12; 
    public static final int BILLING_PREMIUM_CREDIT      = 0x13; 
    public static final int BILLING_FREE_CALL           = 0x18; 
/* 
 * DPNSS_IE SINGLE BYTE IE 
 */ 
    public static final int BUSY_IE                     = 0xE0;     /* Busy IE */ 
    public static final int DIVERSION_BYPASS_IE         = 0xE1;     /* Diversion bypass IE */ 
    public static final int ENQUIRY_IE                  = 0xE2;     /* Enquiry IE */ 
    public static final int VIRTUALCALL_IE              = 0xE3;     /* Virtual call IE */ 
    public static final int DIVERSION_VALIDATION_IE     = 0xE4;     /* Diversion validation IE */ 
    public static final int DPNSS_SIMULATED_ANSWER_IE		= 0xE6;     /* 627a206 - new DPNSS define */ 
    public static final int DPNSS_EXPLICIT_CALL_TRANSFER_IE	= 0xE7;	 /* 627a206 - new DPNSS define */ 
 
/* 
 * DPNSS_IE MULITIPLE BYTES IE 
 */ 
    public static final int DIVERSION_IE                = 0x58;     /* Diversion IE */ 
    public static final int INTRUDE_IE                  = 0x59;     /* Intrude IE */ 
    public static final int INTRUSION_IE                = 0x5A;     /* Intrusion IE */ 
    public static final int NSI_IE                      = 0x5B;     /* NSI IE */ 
    public static final int TRANSFER_IE                 = 0x5C;     /* Transfer IE */ 
    public static final int TRANSIT_IE                  = 0x5D;     /* Transit IE */ 
    public static final int EXTENSION_STATUS_IE         = 0xE5;     /* Extension Status IE */ 
    public static final int TEXT_DISPLAY_IE		= 0x5E;     /* Text Display IE */ 
 
/* 
 *     Diversion Information Element 
 */ 
    public static final int DIVERT_IMMEDIATE            = 0x01; 
    public static final int DIVERT_ON_BUSY              = 0x02; 
    public static final int DIVERT_NO_REPLY             = 0x03; 
 
    public static final int DIVERT_LOCAL                = 0x01; 
    public static final int DIVERT_REMOTE               = 0x02; 
 
/* 
 *     Intrusion Information Element 
 */ 
    public static final int INTRUDE_PRIOR_VALIDATION    = 0x01; 
    public static final int INTRUDE_NORMAL              = 0x02; 
 
    public static final int INTRUSION_LEVEL_1           = 0x01; 
    public static final int INTRUSION_LEVEL_2           = 0x02; 
    public static final int INTRUSION_LEVEL_3           = 0x03; 
 
/* 
 *     Intrude Information Element 
 */ 
    public static final int INTRUDE                     = 0x01; 
    public static final int INTRUDE_WITHDRAW            = 0x02; 
 
/* 
 *     Transfer Information Element 
 */ 
    public static final int TRANSFER_ORIG               = 0x01; 
    public static final int TRANSFER_TERM               = 0x02; 
 
/* 
 *     NSI Information Element 
 */ 
    public static final int NSI_EEM                     = 0x01; 
    public static final int NSI_LLM                     = 0x02; 
 
/* 
 *     Text Display Information Element 
 */ 
    public static final int TEXT_TYPE_NOT_PRESENT	= 0x00; 
    public static final int TEXT_TYPE_NAME		   = 0x01; 
    public static final int TEXT_TYPE_MESSAGE		   = 0x02; 
    public static final int TEXT_TYPE_REASON		   = 0x03; 
 
    public static final int MAX_DN_SIZE             = (16+1); 
    public static final int MAX_SPID_SIZE           = (20+1); 
 
    public static final int USER_SIDE               = 0;        /* User side of ISDN protocol */ 
    public static final int NETWORK_SIDE            = 1;        /* Network side of ISDN protocol */ 
 
    public static final int LAYER_2_ONLY            = 0;        /* ISDN access @ layer 2 */ 
    public static final int FULL_ISDN_STACK         = 1;        /* ISDN access @ L3 call control */ 
 
    public static final int AUTO_INIT_TERMINAL      = 0;        /* Auto Initializing terminal */ 
    public static final int NON_INIT_TERMINAL       = 1;        /* Non Initilizing Terminal   */ 
 
    public static final int AUTO_TEI_TERMINAL       = 0;        /* auto TEI assigning Terminal */ 
    public static final int FIXED_TEI_TERMINAL      = 1;        /* Non-auto TEI assigning term.*/ 
 
    public static final int TMR_DEFAULT             = 0;        /* Use default values of timers */ 
    public static final int TMR_DISABLE             = -1;       /* Do not use the timer */ 
 
    public static final int USE_DEFAULT_USID_TID    = 0;        /* Use values by Telnetworks */  
    public static final int MATCH_USID_AND_TID      = 0;        /* Match Usid and Tid */ 
    public static final int MATCH_USID_BUT_NOT_TID  = 1;        /* match usid but tid shouldn't match */ 
 
/* 
 * BIT MASKS FOR D CHANNEL CONFIGURATION FEATURE CONTROL BYTE  
*/ 
    public static final int SENDING_COMPLETE_ATTACH  = 0x04;     /* Add SENDING_COMPLETE IE to  
                                             SETUP message */ 
    public static final int HOST_CONTROLLED_RELEASE  = 0x10;     /* Delay RELEASE reply until host 
                                             issues cc_ReleaseCall */ 
    public static final int USER_PERST_L2_ACT	 = 0x08;	  /* Persistent L2 activation on 
					     user side */ 
 
/* BRI PCM TONE SETTINGS IN FEATURE CONTROL BYTE */ 
    public static final int ULAW_PCM_TONE          = 0x01;      /* Use Mu-Law for tone */ 
    public static final int ALAW_PCM_TONE          = 0x02;      /* Use A-Law for tone  */ 
 
     
     
     
    // Variables 
    private Voice voiceDev = null; 
    private ISDNLine lineDev = null; 
    private int linestate = RESET; 
    private boolean blocked = true; 
    private String dnisPref = ""; 
    private long crn = 0; 
    private int ringsSent = 0; 
    private boolean ringPhase = true; 
    private String voiceName; 
	private String dtiName; 
     
    public ISDNChannel(String dtiName, String voiceName) { 
        this (dtiName, voiceName, ""); 
    } 
     
    public ISDNChannel(String dtiName, String voiceName, String dnisPreffix) { 
        super(); 
         
        lineDev = new ISDNLine(this, dtiName); 
        voiceDev = new Voice(this, voiceName); 
 
        // link 
        lineDev.listen(voiceDev); 
        voiceDev.listen(lineDev); 
 
        linestate = RESET; 
        dnisPref = dnisPreffix; 
        lineDev.restart(); 
        this.voiceName = voiceName; 
        this.dtiName = dtiName; 
 
        group = new ThreadGroup(dtiName + "/" + voiceName + " group"); 
 
        serviceThread = new Thread(group, this, dtiName + "/" + voiceName + " service"); 
        serviceThread.start(); 
    } 
 
    public void close() 
    { 
        super.clear(); 
        if (serviceThread != null) { 
            Channel.stopGroup(this); 
            serviceThread.interrupt(); 
            try { 
                serviceThread.join(500); 
            } 
            catch (InterruptedException ie) {}; 
            serviceThread = null; 
        } 
        if (lineDev != null) { 
            lineDev.close(); 
            lineDev = null; 
        } 
        if (voiceDev != null) { 
            voiceDev.close(); 
            voiceDev = null; 
        } 
    } 
 
    public Voice getVoice() { 
        return voiceDev; 
    } 
 
    public Device getNetwork() { 
        return lineDev; 
    } 
 
    public void run() { 
 
        while(true) try { 
            clear(); 
            beginService(); 
       	    while(true) { 
           	    EVT evt = serviceWaitEvent(); 
           	    if (evt.type == EVT.CCEV_OFFERED) { 
               	    	linestate = ICALL; 
           	        setState(INCOMING); 
           	        crn = evt.crn; 
           	        if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0) 
                   	    System.out.println(new java.util.Date().toString().substring(11,19) +": Incoming CRN: " + crn); 
           	        call = new Call(ISDNChannel.this); 
           	        byte dnis[] = new byte[32]; 
           	        Dialogic.cc_GetDNIS(crn, dnis); 
           	        // ASCIIZ, find end and convert to string 
           	        int dnisl = 0; while(dnis[dnisl] != 0 && dnisl < (dnis.length-1)) dnisl++;   
           	        call.dnis = dnisPref + new String(dnis, 0, dnisl); 
           	         
               	    if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0) 
               	        System.out.println(new java.util.Date().toString().substring(11,19) +":Call to " + call.dnis); 
                   	if (handler != null) { 
                   	    endService(); 
       	                handler.handleCall(call); 
       	            } 
               	} else { 
               	    if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0) 
                   	    System.out.println(new java.util.Date().toString().substring(11,19) +":Event " + evt); 
               	} 
       	    } 
       	}  
       	catch (Exception e) { 
       		if (linestate == DIAL) 
       			// Someone asking me to leave... 
       			return; 
       	    System.err.println(this.toString() + " service loop: " + e); 
       	} 
       	finally { 
       	    endService(); 
       	} 
    } 
 
    public String toString() { 
        return "ISDNChannel on " + lineDev + "/" + voiceDev; 
    } 
 
    static TNGEN ringLead = new TNGEN(200, -40, 20) ; 
    static TNGEN ring = new TNGEN(440, -10, 100) ; 
    static TNGEN ringSilence = new TNGEN(200, -40, 200); 
    static TPT stdTPT = new TPT(); 
 
    void accept() { 
        if (linestate != ICALL) 
            throw new ChannelException("accept(): wrong state"); 
        try { 
           	if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0) 
                System.out.println(new java.util.Date().toString().substring(11,19) +": ISDN accept()"); 
            beginService(); 
            Dialogic.cc_AcceptCall(crn, 0, Dialogic.EV_ASYNC); 
            EVT evt = serviceWaitEvent(); 
            if (evt.type != EVT.CCEV_ACCEPT) 
                throw new ChannelException("Accept error:" + evt); 
            linestate = RINGS; 
            ringsSent = 0; 
       	    voiceDev.playtone(ringLead, stdTPT); 
            ringPhase = true; 
        } finally { 
            endService(); 
        } 
    } 
 
    void answer(int rings) { 
        // It is ok to answer w/o accept... 
        if (!(linestate == RINGS || linestate == ICALL)) 
            throw new ChannelException("answer(): wrong state"); 
        if ((Dialogic.debug & Dialogic.DEBUG_ISDN) != 0) 
            System.out.println(new java.util.Date().toString().substring(11,19) +": ISDN answer(" + rings + ")"); 
        if (linestate == ICALL)  
            accept(); 
        // Ring cycle 
        EVT evt = null; 
        long now = System.currentTimeMillis(); 
        long conn = call.startTime() + rings * 3000; // 3 secs each 
        try { 
            beginService(); 
            if (conn > now)  
                evt = serviceWaitEvent(conn - now); 
            if (evt == null) {     
                Dialogic.cc_AnswerCall(crn, 0, Dialogic.EV_ASYNC); 
                evt = serviceWaitEvent(); 
                if (evt.type != EVT.CCEV_ANSWERED) 
                    throw new ChannelException("Answer error:" + evt); 
                linestate = IN; 
                voiceDev.stop(); 
            } else { 
                linestate = RESET; 
                setState(OOS); 
                throw new ChannelException("answer():" + evt); 
            } 
        } finally { 
            endService(); 
        } 
    } 
 
    void reject(int reason) { 
        if (linestate != ICALL) 
            throw new ChannelException("reject(): wrong state"); 
        throw new ChannelException("reject: not implemented!"); 
    } 
 
    void dial(Call call, String number) { 
        if (linestate != IDLE) 
            throw new ChannelException("dial(): wrong state"); 
        if (blocked)  
            throw new ChannelException("dial(): blocked"); 
		linestate = DIAL; 
		if (serviceThread != null) { 
			Channel.stopGroup(this); 
			serviceThread.interrupt(); 
			/* Service thread will normally die,  
			 * but simultaneous seizure may prevent that from happening... */ 
			try { 
				serviceThread.join(500); 
			} 
			catch(InterruptedException ie) { 
				Thread.currentThread().interrupt(); 
			} 
			// Give up if it did not quit... 
			if (serviceThread.isAlive()) { 
				System.err.println("Seizure failure: service not quitting"); 
				throw new ChannelException("dial(): seizure failure"); 
			} 
			serviceThread = null; 
		} 
        this.call = call; 
        setState(OUTGOING); 
        crn = lineDev.dial(number, 5); 
        return; 
    } 
 
    String ani() { 
        String ani = ""; 
        if (crn == 0) 
            return ""; 
        byte anis[] = new byte[32]; 
        try { 
            Dialogic.cc_GetANI(crn,anis); 
           	// ASCIIZ, find end and convert to string 
           	int anisl = 0; while(anis[anisl] != 0 && anisl < (anis.length-1)) anisl++;   
            ani = new String(anis, 0, anisl); 
        } catch (Exception e) { 
            // Runtime exception thrown by CC layer if ani not available 
            ani = ""; 
        } 
        return ani; 
    } 
 
    int pulses() { 
        throw new ChannelException("pulses: not implemented!"); 
    } 
 
	synchronized void clear() { 
        if (linestate == RESET) 
            return; 
        linestate = RESET; 
        super.clear(); 
        if (voiceDev != null) { 
          voiceDev.stop(); 
          voiceDev.clear(); 
        } 
        lineDev.listen(voiceDev); 
        voiceDev.listen(lineDev); 
        if (crn != 0) { 
            Dialogic.cc_DropCall(crn, NORMAL_CLEARING, Dialogic.EV_ASYNC); 
        }  
        flush(); 
		if (serviceThread == null) { 
			serviceThread = new Thread(group, this, dtiName + "/" + voiceName + " service"); 
			serviceThread.start(); 
		} 
    } 
 
    // Our line state service fn 
    protected EVT service(EVT evt) { 
        if (lineDev.source(evt)) {         
            switch (evt.type) { 
            case EVT.CCEV_RESTARTFAIL: 
            case EVT.CCEV_BLOCKED:  
                blocked = true; 
                linestate = RESET; 
                evt = null; 
                setState(OOS); 
                break; 
            case EVT.CCEV_RESTART: 
            case EVT.CCEV_UNBLOCKED:  
                blocked = false; 
                evt = null; 
                lineDev.waitCall(); 
                if (linestate == RESET) { 
                    linestate = IDLE; 
                    setState(FREE); 
                } 
                break;             
            case EVT.CCEV_DISCONNECTED: 
                if (call != null) call.drop(); 
                if (linestate != RESET) { 
					clear(); 
					setState(OOS); 
	                throw new HangUpException(); 
				} 
	            break; 
			case EVT.CCEV_CONNECTED: 
				// Remote answer 
				if (call != null) call.connect(); 
				break; // Let the event go up 
            case EVT.CCEV_DROPCALL: 
                Dialogic.cc_ReleaseCallEx(crn, Dialogic.EV_ASYNC); 
                if (call != null) call.drop(); 
                crn = 0; 
                evt = null; 
                linestate = RESET; 
                setState(OOS); 
            case EVT.CCEV_RELEASECALL: 
                evt = null; 
                linestate = IDLE; 
                setState(FREE); 
                break; 
            } 
        } else if (voiceDev.source(evt)) { 
            if (evt.type == EVT.TDX_PLAYTONE) { 
                if (linestate == RINGS) { 
                    if (ringPhase) { 
   	                    voiceDev.playtone(ring, stdTPT); 
   	                    ringPhase = false; 
   	                    ringsSent++; 
   	                } else { 
   	                    voiceDev.playtone(ringSilence, stdTPT); 
   	                    ringPhase = true; 
   	                } 
   	                return null; 
                } 
            } 
        } 
        return evt; 
    } 
}