root/src/isdn/include/Q921priv.h

/* [<][>][^][v][top][bottom][index][help] */

INCLUDED FROM


   1 /*****************************************************************************
   2 
   3   FileName:     Q921priv.h
   4 
   5   Description:  Private declarations
   6 
   7   Created:      08.Aug.2008/STKN
   8 
   9   License/Copyright:
  10 
  11   Copyright (c) 2007, Jan Vidar Berger, Case Labs, Ltd. All rights reserved.
  12   email:janvb@caselaboratories.com  
  13 
  14   Copyright (c) 2008, Stefan Knoblich, axsentis GmbH. All rights reserved.
  15   email:s.knoblich@axsentis.de  
  16 
  17   Redistribution and use in source and binary forms, with or without 
  18   modification, are permitted provided that the following conditions are 
  19   met:
  20 
  21     * Redistributions of source code must retain the above copyright notice, 
  22           this list of conditions and the following disclaimer.
  23     * Redistributions in binary form must reproduce the above copyright notice, 
  24           this list of conditions and the following disclaimer in the documentation 
  25           and/or other materials provided with the distribution.
  26     * Neither the name of the Case Labs, Ltd nor the names of its contributors 
  27           may be used to endorse or promote products derived from this software 
  28           without specific prior written permission.
  29 
  30   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
  31   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
  32   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
  33   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
  34   LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
  35   CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
  36   SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
  37   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
  38   CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
  39   ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
  40   POSSIBILITY OF SUCH DAMAGE.
  41 
  42 *****************************************************************************/
  43 #ifndef _Q921_PRIV_H_
  44 #define _Q921_PRIV_H_
  45 
  46 #ifdef _MSC_VER
  47 #ifndef __inline__
  48 #define __inline__ __inline
  49 #endif
  50 #if (_MSC_VER >= 1400)                  /* VC8+ */
  51 #ifndef _CRT_SECURE_NO_DEPRECATE
  52 #define _CRT_SECURE_NO_DEPRECATE
  53 #endif
  54 #ifndef _CRT_NONSTDC_NO_DEPRECATE
  55 #define _CRT_NONSTDC_NO_DEPRECATE
  56 #endif
  57 #endif
  58 #ifndef strcasecmp
  59 #define strcasecmp(s1, s2) _stricmp(s1, s2)
  60 #endif
  61 #ifndef strncasecmp
  62 #define strncasecmp(s1, s2, n) _strnicmp(s1, s2, n)
  63 #endif
  64 #ifndef snprintf
  65 #define snprintf _snprintf
  66 #endif
  67 #endif
  68 
  69 typedef enum                    /* Q.921 States */
  70 {
  71         Q921_STATE_STOPPED = 0,                 /* Trunk stopped */
  72         Q921_STATE_TEI_UNASSIGNED = 1,          /* TEI unassigned */
  73         Q921_STATE_TEI_AWAITING,                /* Assign awaiting TEI */
  74         Q921_STATE_TEI_ESTABLISH,               /* Establish awaiting TEI */
  75         Q921_STATE_TEI_ASSIGNED,                /* TEI assigned */
  76         Q921_STATE_AWAITING_ESTABLISHMENT,      /* Awaiting establishment */
  77         Q921_STATE_AWAITING_RELEASE,            /* Awaiting release */
  78         Q921_STATE_MULTIPLE_FRAME_ESTABLISHED,  /* Multiple frame established */
  79         Q921_STATE_TIMER_RECOVERY               /* Timer recovery */
  80 } Q921State_t;
  81 
  82 /*
  83  * Flags
  84  */
  85 enum Q921_Flags {
  86         Q921_FLAG_L3_INITIATED = (1 << 0),
  87 
  88         Q921_FLAG_UI_FRAME_QUEUED = (1 << 1),
  89         Q921_FLAG_I_FRAME_QUEUED  = (1 << 2),
  90 
  91         Q921_FLAG_ACK_PENDING = (1 << 3),
  92         Q921_FLAG_REJECT      = (1 << 4),
  93 
  94         Q921_FLAG_RECV_BUSY      = (1 << 5),
  95         Q921_FLAG_PEER_RECV_BUSY = (1 << 6)
  96 };
  97 
  98 #define Q921_SET_FLAG(x, f)     ((x)->flags |= f)
  99 #define Q921_CHECK_FLAG(x, f)   ((x)->flags & f)
 100 #define Q921_CLEAR_FLAG(x, f)   ((x)->flags &= ~f)
 101 
 102 
 103 /*
 104  * dynamic TEI handling
 105  */
 106 #define Q921_SAPI_TEI           63      /* SAPI for all TEI Messages */
 107 #define Q921_LAYER_ENT_ID_TEI   0x0f    /* UN Layer Management Entity ID for TEI Mgmt */
 108 #define Q921_LAYER_ENT_ID_Q931  0x08    /* Q.931 Layer Management Entity ID */
 109 
 110 
 111 typedef enum {
 112         Q921_TEI_ID_REQUEST = 1,
 113         Q921_TEI_ID_ASSIGNED,
 114         Q921_TEI_ID_DENIED,
 115         Q921_TEI_ID_CHECKREQ,
 116         Q921_TEI_ID_CHECKRESP,
 117         Q921_TEI_ID_REMOVE,
 118         Q921_TEI_ID_VERIFY
 119 } Q921TeiMessageType_t;
 120 
 121 
 122 /**
 123  * Per-Datalink context
 124  */
 125 struct Q921_Link {
 126         L2UCHAR tei;            /*!< This endpoint's TEI */
 127 
 128         L2UCHAR va;
 129         L2UCHAR vs;
 130         L2UCHAR vr;
 131 
 132         L2INT flags;
 133         Q921State_t state;
 134 
 135         L2ULONG N202;           /*!< PTMP TE mode retransmit counter */
 136         L2ULONG N200;           /*!< retransmit counter (per-TEI in PTMP NT mode) */
 137 
 138         L2ULONG TM01;           /*!< Datalink inactivity disconnect timer */
 139 
 140         L2ULONG T200;
 141         L2ULONG T201;           /*!< PTMP NT mode timer */
 142         L2ULONG T203;
 143 
 144         L2USHORT ri;            /*!< random id for TEI request mgmt */
 145 
 146         /* I + UI Frame queue */
 147         L2UCHAR UIFrameQueue[Q921MAXHDLCSPACE];
 148         L2UCHAR  IFrameQueue[Q921MAXHDLCSPACE];
 149         L2UCHAR  IFrameResendQueue[Q921MAXHDLCSPACE];
 150 };
 151 
 152 
 153 #define Q921_LINK_CONTEXT(tr, tei) \
 154         (Q921_IS_PTMP_NT(tr) && tei != Q921_TEI_BCAST) ? ((struct Q921_Link *)&(tr)->context[tei]) : (tr)->context
 155 
 156 #define Q921_TRUNK_CONTEXT(tr) \
 157         (tr)->context
 158 
 159 #define Q921_LOGBUFSIZE         2000
 160 #define INITIALIZED_MAGIC       42
 161 
 162 /*
 163  * Helper macros
 164  */
 165 #define Q921_INC_COUNTER(x)             (x = (x + 1) % 128)
 166 #define Q921_DEC_COUNTER(x)             (x = (x) ? (x - 1) : 127)
 167 
 168 #define Q921_UFRAME_HEADER_SIZE         3
 169 #define Q921_UFRAME_DATA_OFFSET(tr)     ((tr)->Q921HeaderSpace + Q921_UFRAME_HEADER_SIZE)
 170 
 171 #define Q921_SFRAME_HEADER_SIZE         4
 172 #define Q921_SFRAME_DATA_OFFSET(tr)     ((tr)->Q921HeaderSpace + Q921_SFRAME_HEADER_SIZE)
 173 
 174 #define Q921_IFRAME_HEADER_SIZE         4
 175 #define Q921_IFRAME_DATA_OFFSET(tr)     ((tr)->Q921HeaderSpace + Q921_IFRAME_HEADER_SIZE)
 176 
 177 #define Q921_IS_TE(x)                   ((x)->NetUser == Q921_TE)
 178 #define Q921_IS_NT(x)                   ((x)->NetUser == Q921_NT)
 179 
 180 #define Q921_IS_STOPPED(tr)             ((tr)->state == Q921_STATE_STOPPED)
 181 
 182 /* TODO: rework this one */
 183 #define Q921_IS_READY(tr)               ((tr)->state >= Q921_STATE_TEI_ASSIGNED)
 184 
 185 #define Q921_IS_PTMP(x)                 ((x)->NetType == Q921_PTMP)
 186 #define Q921_IS_PTMP_TE(x)              ((x)->NetType == Q921_PTMP && (x)->NetUser == Q921_TE)
 187 #define Q921_IS_PTMP_NT(x)              ((x)->NetType == Q921_PTMP && (x)->NetUser == Q921_NT)
 188 
 189 #define Q921_IS_PTP(x)                  ((x)->NetType == Q921_PTP)
 190 #define Q921_IS_PTP_TE(x)               ((x)->NetType == Q921_PTP && (x)->NetUser == Q921_TE)
 191 #define Q921_IS_PTP_NT(x)               ((x)->NetType == Q921_PTP && (x)->NetUser == Q921_NT)
 192 
 193 /* Make life a little easier */
 194 #define Q921_COMMAND(x)                 ((x)->NetUser == Q921_TE ? 0 : 1)
 195 #define Q921_RESPONSE(x)                ((x)->NetUser == Q921_TE ? 1 : 0)
 196 
 197 #define Q921_IS_COMMAND(tr, x)          ((x) == (Q921_IS_TE(tr) ? 1 : 0))
 198 #define Q921_IS_RESPONSE(tr, x)         ((x) == (Q921_IS_TE(tr) ? 0 : 1))
 199 
 200 
 201 /*******************************************************************************
 202  * Private functions
 203  *******************************************************************************/
 204 
 205 /*
 206  * L1 / L2 Interface
 207  */
 208 static int Q921Tx21Proc(L2TRUNK trunk, L2UCHAR *Msg, L2INT size);
 209 static int Q921Tx23Proc(L2TRUNK trunk, Q921DLMsg_t ind, L2UCHAR tei, L2UCHAR *Msg, L2INT size);
 210 
 211 
 212 /*
 213  * Timers
 214  */
 215 static L2ULONG Q921GetTime(void);
 216 
 217 static void Q921T200TimerStart(L2TRUNK trunk, L2UCHAR tei);
 218 static void Q921T200TimerStop(L2TRUNK trunk, L2UCHAR tei);
 219 static void Q921T200TimerReset(L2TRUNK trunk, L2UCHAR tei);
 220 static void Q921T200TimerExpire(L2TRUNK trunk, L2UCHAR tei);
 221 
 222 static void Q921T201TimerStart(L2TRUNK trunk, L2UCHAR tei);
 223 static void Q921T201TimerStop(L2TRUNK trunk, L2UCHAR tei);
 224 /* static void Q921T201TimerReset(L2TRUNK trunk, L2UCHAR tei); - Unused for now */
 225 static void Q921T201TimerExpire(L2TRUNK trunk, L2UCHAR tei);
 226 
 227 static void Q921T202TimerStart(L2TRUNK trunk);
 228 static void Q921T202TimerStop(L2TRUNK trunk);
 229 static void Q921T202TimerReset(L2TRUNK trunk);
 230 static void Q921T202TimerExpire(L2TRUNK trunk);
 231 
 232 static void Q921T203TimerStart(L2TRUNK trunk, L2UCHAR tei);
 233 static void Q921T203TimerStop(L2TRUNK trunk, L2UCHAR tei);
 234 static void Q921T203TimerReset(L2TRUNK trunk, L2UCHAR tei);
 235 static void Q921T203TimerExpire(L2TRUNK trunk, L2UCHAR tei);
 236 
 237 static void Q921TM01TimerStart(L2TRUNK trunk, L2UCHAR tei);
 238 /* static void Q921TM01TimerStop(L2TRUNK trunk, L2UCHAR tei); - Unused for now */
 239 static void Q921TM01TimerReset(L2TRUNK trunk, L2UCHAR tei);
 240 /* static void Q921TM01TimerExpire(L2TRUNK trunk, L2UCHAR tei); - Unused for now */
 241 
 242 /*
 243  * Frame encoding
 244  */
 245 static int Q921SendS(L2TRUNK trunk, L2UCHAR Sapi, char cr, L2UCHAR Tei, char pf, L2UCHAR sv, L2UCHAR *mes, L2INT size);
 246 static int Q921SendU(L2TRUNK trunk, L2UCHAR Sapi, char cr, L2UCHAR Tei, char pf, L2UCHAR m, L2UCHAR *mes, L2INT size);
 247 
 248 static int Q921SendRNR(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf);
 249 static int Q921SendREJ(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf);
 250 static int Q921SendSABME(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf);
 251 static int Q921SendDM(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf);
 252 static int Q921SendDISC(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf);
 253 static int Q921SendUA(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf);
 254 static int Q921SendUN(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf, L2UCHAR *mes, L2INT size);
 255 static int Q921SendRR(L2TRUNK trunk, int Sapi, int cr, int Tei, int pf);
 256 
 257 /*
 258  * Frame decoding
 259  */
 260 static int Q921ProcIFrame(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 261 static int Q921ProcSFrame(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 262 static int Q921ProcUFrame(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 263 
 264 static int Q921ProcSABME(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 265 static int Q921ProcDM(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 266 static int Q921ProcUA(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 267 static int Q921ProcDISC(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 268 static int Q921ProcRR(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 269 static int Q921ProcRNR(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 270 static int Q921ProcREJ(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 271 
 272 
 273 /*
 274  * (Common) procedures defined in the Q.921 SDL
 275  */
 276 static int Q921SendEnquiry(L2TRUNK trunk, L2UCHAR tei);
 277 static int Q921SendEnquiryResponse(L2TRUNK trunk, L2UCHAR tei);
 278 static void Q921ResetExceptionConditions(L2TRUNK trunk, L2UCHAR tei);
 279 static int Q921EstablishDataLink(L2TRUNK trunk, L2UCHAR tei);
 280 static int Q921NrErrorRecovery(L2TRUNK trunk, L2UCHAR tei);
 281 static int Q921InvokeRetransmission(L2TRUNK trunk, L2UCHAR tei, L2UCHAR nr);
 282 static int Q921AcknowledgePending(L2TRUNK trunk, L2UCHAR tei);
 283 /*
 284 static int Q921SetReceiverBusy(L2TRUNK trunk);
 285 static int Q921ClearReceiverBusy(L2TRUNK trunk);
 286 */
 287 
 288 /*
 289  * Queueing
 290  */
 291 static int Q921SendQueuedIFrame(L2TRUNK trunk, L2UCHAR tei);
 292 static int Q921EnqueueI(L2TRUNK trunk, L2UCHAR Sapi, char cr, L2UCHAR Tei, char pf, L2UCHAR *mes, L2INT size);
 293 
 294 /*
 295  * TEI management
 296  */
 297 static int Q921TeiSendAssignRequest(L2TRUNK trunk);
 298 static int Q921TeiProcAssignResponse(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 299 static int Q921TeiSendVerifyRequest(L2TRUNK trunk);
 300 static int Q921TeiProcCheckRequest(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 301 static int Q921TeiProcRemoveRequest(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 302 static int Q921TeiProcAssignRequest(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 303 static int Q921TeiProcCheckResponse(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 304 static int Q921TeiProcVerifyRequest(L2TRUNK trunk, L2UCHAR *mes, L2INT size);
 305 static int Q921TeiSendRemoveRequest(L2TRUNK trunk, L2UCHAR tei);
 306 static int Q921TeiSendDenyResponse(L2TRUNK trunk, L2UCHAR tei, L2USHORT ri);
 307 static int Q921TeiSendAssignedResponse(L2TRUNK trunk, L2UCHAR tei, L2USHORT ri);
 308 static int Q921TeiSendCheckRequest(L2TRUNK trunk, L2UCHAR tei);
 309 
 310 /*
 311  * Logging
 312  */
 313 static int Q921Log(L2TRUNK trunk, Q921LogLevel_t level, const char *fmt, ...);
 314 static int Q921LogMesg(L2TRUNK trunk, Q921LogLevel_t level, L2UCHAR received, L2UCHAR *mes, L2INT size, const char *fmt, ...);
 315 
 316 /*
 317  * State handling
 318  */
 319 static int Q921ChangeState(L2TRUNK trunk, Q921State_t state, L2UCHAR tei);
 320 
 321 #endif

/* [<][>][^][v][top][bottom][index][help] */