]> git.ipfire.org Git - thirdparty/freeswitch.git/commitdiff
add functions to initialize a trunk, and a public function to receive from layer...
authorMichael Jerris <mike@jerris.com>
Tue, 22 May 2007 01:25:35 +0000 (01:25 +0000)
committerMichael Jerris <mike@jerris.com>
Tue, 22 May 2007 01:25:35 +0000 (01:25 +0000)
git-svn-id: http://svn.openzap.org/svn/openzap/trunk@86 a93c3328-9c30-0410-af19-c9cd2b2d52af

libs/openzap/src/isdn/Q921.c
libs/openzap/src/isdn/include/Q921.h

index 27802d877d9e88d607065b31553db9e5481dfa8f..3f768e411655391928caabb7bf2eccd8f657164a 100644 (file)
-/*****************************************************************************
-
-  FileName:     q921.c
-
-  Description:  Contains the implementation of a Q.921 protocol on top of the
-                Comet Driver.
-
-                Most of the work required to execute a Q.921 protocol is 
-                taken care of by the Comet ship and it's driver. This layer
-                will simply configure and make use of these features to 
-                complete a Q.921 implementation.
-
-  Created:      27.dec.2000/JVB
-
-  License/Copyright:
-
-  Copyright (c) 2007, Jan Vidar Berger, Case Labs, Ltd. All rights reserved.
-  email:janvb@caselaboratories.com  
-
-  Redistribution and use in source and binary forms, with or without 
-  modification, are permitted provided that the following conditions are 
-  met:
-
-    * Redistributions of source code must retain the above copyright notice, 
-         this list of conditions and the following disclaimer.
-    * Redistributions in binary form must reproduce the above copyright notice, 
-         this list of conditions and the following disclaimer in the documentation 
-         and/or other materials provided with the distribution.
-    * Neither the name of the Case Labs, Ltd nor the names of its contributors 
-         may be used to endorse or promote products derived from this software 
-         without specific prior written permission.
-
-  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
-  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
-  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
-  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
-  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
-  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
-  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
-  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
-  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
-  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
-  POSSIBILITY OF SUCH DAMAGE.
-
-*****************************************************************************/
-#include "Q921.h"
-#include <stdlib.h>
-#include "mfifo.h"
-
-/*****************************************************************************
-  Global Tables & Variables.
-*****************************************************************************/
-Q921Data Q921DevSpace[Q921MAXTRUNK];
-int Q921HeaderSpace={0};
-
-int (*Q921Tx21Proc)(int dev, L2UCHAR *, int)={NULL};
-int (*Q921Tx23Proc)(int dev, L2UCHAR *, int)={NULL};
-
-/*****************************************************************************
-
-  Function:     Q921Init
-
-  Decription:   Initialize the Q.921 stack so it is ready for use. This 
-                function MUST be called as part of initializing the 
-                application.
-
-*****************************************************************************/
-void Q921Init()
-{
-    int x;
-    for(x=0; x<Q921MAXTRUNK;x++)
-    {
-        MFIFOCreate(Q921DevSpace[x].HDLCInQueue, Q921MAXHDLCSPACE, 10);
-        Q921DevSpace[x].vr=0;
-        Q921DevSpace[x].vs=0;
-        Q921DevSpace[x].state=0;;
-    }
-}
-
-void Q921SetHeaderSpace(int hspace)
-{
-    Q921HeaderSpace=hspace;
-}
-
-void Q921SetTx21CB(int (*callback)(int dev, L2UCHAR *, int))
-{
-    Q921Tx21Proc = callback;
-}
-
-void Q921SetTx23CB(int (*callback)(int dev, L2UCHAR *, int))
-{
-    Q921Tx23Proc = callback;
-}
-
-/*****************************************************************************
-
-  Function:     Q921QueueHDLCFrame
-
-  Description:  Called to receive and queue an incoming HDLC frame. Will
-                queue this in Q921HDLCInQueue. The called must either call
-                Q921Rx12 directly afterwards or signal Q921Rx12 to be called
-                later. Q921Rx12 will read from the same queue and process
-                the frame.
-
-                This function assumes that the message contains header 
-                space. This is removed for internal Q921 processing, but 
-                must be keept for I frames.
-
-  Parameters:   trunk   trunk #
-                b       ptr to frame;
-                size    size of frame in bytes
-
-*****************************************************************************/
-int Q921QueueHDLCFrame(int trunk, L2UCHAR *b, int size)
-{
-    return MFIFOWriteMes(Q921DevSpace[trunk].HDLCInQueue, b, size);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendI
-
-  Description:  Compose and Send I Frame to layer. Will receive an I frame
-                with space for L2 header and fill out that header before
-                it call Q921Tx21Proc.
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          P fiels octet 5
-                mes         ptr to I frame message.
-                size        size of message in bytes.
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-int Q921SendI(int trunk, L2UCHAR Sapi, char cr, L2UCHAR Tei, char pf, L2UCHAR *mes, int size)
-{
-    mes[Q921HeaderSpace+0] = (Sapi&0xfc) | ((cr<<1)&0x02);
-    mes[Q921HeaderSpace+1] = (Tei<<1) | 0x01;
-    mes[Q921HeaderSpace+2] = Q921DevSpace[trunk].vs<<1;
-    mes[Q921HeaderSpace+3] = (Q921DevSpace[trunk].vr<<1) | (pf & 0x01);
-    Q921DevSpace[trunk].vs++;
-
-    return Q921Tx21Proc(trunk, mes, size);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendRR
-
-  Description:  Compose and send Receive Ready.
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          P/F fiels octet 5
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-
-int Q921SendRR(int trunk, int Sapi, int cr, int Tei, int pf)
-{
-    L2UCHAR mes[400];
-
-    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));
-    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);
-    mes[Q921HeaderSpace+2] = (L2UCHAR)0x01;
-    mes[Q921HeaderSpace+3] = (L2UCHAR)((Q921DevSpace[trunk].vr<<1) | (pf & 0x01));
-
-    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+4);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendRNR
-
-  Description:  Compose and send Receive Nor Ready
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          P/F fiels octet 5
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-int Q921SendRNR(int trunk, int Sapi, int cr, int Tei, int pf)
-{
-    L2UCHAR mes[400];
-
-    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));
-    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);
-    mes[Q921HeaderSpace+2] = (L2UCHAR)0x05;
-    mes[Q921HeaderSpace+3] = (L2UCHAR)((Q921DevSpace[trunk].vr<<1) | (pf & 0x01));
-
-    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+4);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendREJ
-
-  Description:  Compose and Send Reject.
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          P/F fiels octet 5
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-int Q921SendREJ(int trunk, int Sapi, int cr, int Tei, int pf)
-{
-    L2UCHAR mes[400];
-
-    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));
-    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);
-    mes[Q921HeaderSpace+2] = (L2UCHAR)0x09;
-    mes[Q921HeaderSpace+3] = (L2UCHAR)((Q921DevSpace[trunk].vr<<1) | (pf & 0x01));
-
-    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+4);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendSABME
-
-  Description:  Compose and send SABME
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          P fiels octet 4
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-int Q921SendSABME(int trunk, int Sapi, int cr, int Tei, int pf)
-{
-    L2UCHAR mes[400];
-
-    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));
-    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);
-    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x6f | ((pf<<4)&0x10));
-
-    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendDM
-
-  Description:  Comose and Send DM (Disconnected Mode)
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          F fiels octet 4
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-int Q921SendDM(int trunk, int Sapi, int cr, int Tei, int pf)
-{
-    L2UCHAR mes[400];
-
-    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));
-    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);
-    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x0f | ((pf<<4)&0x10));
-
-    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendDISC
-
-  Description:  Compose and Send Disconnect
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          P fiels octet 4
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-int Q921SendDISC(int trunk, int Sapi, int cr, int Tei, int pf)
-{
-    L2UCHAR mes[400];
-
-    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));
-    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);
-    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x43 | ((pf<<4)&0x10));
-
-    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);
-}
-
-/*****************************************************************************
-
-  Function:     Q921SendUA
-
-  Description:  Compose and Send UA
-
-  Parameters:   trunk       trunk #
-                Sapi        Sapi
-                cr          C/R field.
-                Tei         Tei.
-                pf          F fiels octet 4
-
-  Return Value: 0 if failed, 1 if Send.
-
-*****************************************************************************/
-int Q921SendUA(int trunk, int Sapi, int cr, int Tei, int pf)
-{
-    L2UCHAR mes[400];
-
-    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));
-    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);
-    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x63 | ((pf<<4)&0x10));
-
-    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);
-}
-
-int Q921ProcSABME(int trunk, L2UCHAR *mes, int size)
-{
-       /* TODO:  Do we need these paramaters? */
-       (void)mes;
-       (void)size;
-
-    Q921DevSpace[trunk].vr=0;
-    Q921DevSpace[trunk].vs=0;
-
-    return 1;
-}
-
-/*****************************************************************************
-
-  Function:     Q921Rx12
-
-  Description:  Called to process a message frame from layer 1. Will 
-                identify the message and call the proper 'processor' for
-                layer 2 messages and forward I frames to the layer 3 entity.
-
-                Q921Rx12 will check the input fifo for a message, and if a 
-                message exist process one message before it exits. The caller
-                must either call Q921Rx12 polling or keep track on # 
-                messages in the queue.
-
-  Parameters:   trunk       trunk #.
-
-  Return Value: # messages processed (always 1 or 0).
-
-*****************************************************************************/
-int Q921Rx12(long trunk)
-{
-    L2UCHAR *mes;
-    int rs,size;     /* receive size & Q921 frame size*/
-    L2UCHAR *smes = MFIFOGetMesPtr(Q921DevSpace[trunk].HDLCInQueue, &size);
-    if(smes != NULL)
-    {
-        rs = size - Q921HeaderSpace;
-        mes = &smes[Q921HeaderSpace];
-        /* check for I frame */
-        if((mes[2] & 0x01) == 0)
-        {
-            if(Q921Tx23Proc(trunk, smes, size-2)) /* -2 to clip away CRC */
-            {
-                Q921DevSpace[trunk].vr++;
-                Q921SendRR(trunk, (mes[0]&0xfc)>>2, (mes[0]>>1)&0x01,mes[1]>>1, mes[3]&0x01);
-            }
-            else
-            {
-                /* todo: whatever*/
-            }
-        }
-
-        /* check for RR */
-        else if(mes[2] ==0x01)
-        {
-            /* todo: check if RR is responce to I */
-            Q921SendRR(trunk, (mes[0]&0xfc)>>2, (mes[0]>>1)&0x01,mes[1]>>1, mes[2]&0x01);
-        }
-
-        /* check for RNR */
-        /* check for REJ */
-        /* check for SABME */
-        else if((mes[2] & 0xef) == 0x6f)
-        {
-            Q921ProcSABME(trunk, mes, rs);
-            Q921SendUA(trunk, (mes[0]&0xfc)>>2, (mes[0]>>1)&0x01,mes[1]>>1, (mes[2]&0x10)>>4);
-        }
-
-        /* check for DM */
-        /* check for UI */
-        /* check for DISC */
-        /* check for UA */
-        /* check for FRMR */
-        /* check for XID */
-
-        else
-        {
-            /* what the ? Issue an error */
-                       /* Q921ErrorProc(trunk, Q921_UNKNOWNFRAME, mes, rs); */
-            /* todo: REJ or FRMR */
-        }
-
-        MFIFOKillNext(Q921DevSpace[trunk].HDLCInQueue);
-
-        return 1;
-    }
-    return 0;
-}
-
+/*****************************************************************************\r
+\r
+  FileName:     q921.c\r
+\r
+  Description:  Contains the implementation of a Q.921 protocol on top of the\r
+                Comet Driver.\r
+\r
+                Most of the work required to execute a Q.921 protocol is \r
+                taken care of by the Comet ship and it's driver. This layer\r
+                will simply configure and make use of these features to \r
+                complete a Q.921 implementation.\r
+\r
+  Created:      27.dec.2000/JVB\r
+\r
+  License/Copyright:\r
+\r
+  Copyright (c) 2007, Jan Vidar Berger, Case Labs, Ltd. All rights reserved.\r
+  email:janvb@caselaboratories.com  \r
+\r
+  Redistribution and use in source and binary forms, with or without \r
+  modification, are permitted provided that the following conditions are \r
+  met:\r
+\r
+    * Redistributions of source code must retain the above copyright notice, \r
+         this list of conditions and the following disclaimer.\r
+    * Redistributions in binary form must reproduce the above copyright notice, \r
+         this list of conditions and the following disclaimer in the documentation \r
+         and/or other materials provided with the distribution.\r
+    * Neither the name of the Case Labs, Ltd nor the names of its contributors \r
+         may be used to endorse or promote products derived from this software \r
+         without specific prior written permission.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" \r
+  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE \r
+  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE \r
+  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE \r
+  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR \r
+  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF \r
+  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS \r
+  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN \r
+  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) \r
+  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE \r
+  POSSIBILITY OF SUCH DAMAGE.\r
+\r
+*****************************************************************************/\r
+#include "Q921.h"\r
+#include <stdlib.h>\r
+#include "mfifo.h"\r
+\r
+/*****************************************************************************\r
+  Global Tables & Variables.\r
+*****************************************************************************/\r
+Q921Data Q921DevSpace[Q921MAXTRUNK];\r
+int Q921HeaderSpace={0};\r
+\r
+int (*Q921Tx21Proc)(int dev, L2UCHAR *, int)={NULL};\r
+int (*Q921Tx23Proc)(int dev, L2UCHAR *, int)={NULL};\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921Init\r
+\r
+  Decription:   Initialize the Q.921 stack so it is ready for use. This \r
+                function MUST be called as part of initializing the \r
+                application.\r
+\r
+*****************************************************************************/\r
+void Q921Init()\r
+{\r
+    int x;\r
+    for(x=0; x<Q921MAXTRUNK;x++)\r
+    {\r
+        MFIFOCreate(Q921DevSpace[x].HDLCInQueue, Q921MAXHDLCSPACE, 10);\r
+        Q921DevSpace[x].vr = 0;\r
+        Q921DevSpace[x].vs = 0;\r
+        Q921DevSpace[x].state = 0;\r
+               Q921DevSpace[x].sapi = 0;\r
+               Q921DevSpace[x].tei = 0;\r
+               Q921DevSpace[x].NetUser = Q921_TE;\r
+    }\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921_InitTrunk\r
+\r
+  Decription:   Initialize a Q.921 trunk so it is ready for use. This \r
+                function should be called before you call the rx functions\r
+                               if your trunk will not use hardcoded tei and sapi of 0 or\r
+                               if your trunk is not TE (user) mode (i.e. NET).\r
+\r
+*****************************************************************************/\r
+int Q921_InitTrunk(long trunk, int sapi, int tei, Q921NetUser_t NetUser)\r
+{\r
+       if (trunk > Q921MAXTRUNK)\r
+               return 0;\r
+\r
+       Q921DevSpace[trunk].sapi = sapi;\r
+       Q921DevSpace[trunk].tei = tei;\r
+       Q921DevSpace[trunk].NetUser = NetUser;\r
+       return 1;\r
+}\r
+\r
+void Q921SetHeaderSpace(int hspace)\r
+{\r
+    Q921HeaderSpace=hspace;\r
+}\r
+\r
+void Q921SetTx21CB(int (*callback)(int dev, L2UCHAR *, int))\r
+{\r
+    Q921Tx21Proc = callback;\r
+}\r
+\r
+void Q921SetTx23CB(int (*callback)(int dev, L2UCHAR *, int))\r
+{\r
+    Q921Tx23Proc = callback;\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921QueueHDLCFrame\r
+\r
+  Description:  Called to receive and queue an incoming HDLC frame. Will\r
+                queue this in Q921HDLCInQueue. The called must either call\r
+                Q921Rx12 directly afterwards or signal Q921Rx12 to be called\r
+                later. Q921Rx12 will read from the same queue and process\r
+                the frame.\r
+\r
+                This function assumes that the message contains header \r
+                space. This is removed for internal Q921 processing, but \r
+                must be keept for I frames.\r
+\r
+  Parameters:   trunk   trunk #\r
+                b       ptr to frame;\r
+                size    size of frame in bytes\r
+\r
+*****************************************************************************/\r
+int Q921QueueHDLCFrame(int trunk, L2UCHAR *b, int size)\r
+{\r
+    return MFIFOWriteMes(Q921DevSpace[trunk].HDLCInQueue, b, size);\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendI\r
+\r
+  Description:  Compose and Send I Frame to layer. Will receive an I frame\r
+                with space for L2 header and fill out that header before\r
+                it call Q921Tx21Proc.\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          P fiels octet 5\r
+                mes         ptr to I frame message.\r
+                size        size of message in bytes.\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+int Q921SendI(int trunk, L2UCHAR Sapi, char cr, L2UCHAR Tei, char pf, L2UCHAR *mes, int size)\r
+{\r
+    mes[Q921HeaderSpace+0] = (Sapi&0xfc) | ((cr<<1)&0x02);\r
+    mes[Q921HeaderSpace+1] = (Tei<<1) | 0x01;\r
+    mes[Q921HeaderSpace+2] = Q921DevSpace[trunk].vs<<1;\r
+    mes[Q921HeaderSpace+3] = (Q921DevSpace[trunk].vr<<1) | (pf & 0x01);\r
+    Q921DevSpace[trunk].vs++;\r
+\r
+    return Q921Tx21Proc(trunk, mes, size);\r
+}\r
+\r
+int Q921Rx32(long trunk, L2UCHAR * Mes, L2INT Size)\r
+{\r
+       return Q921SendI(trunk, \r
+                                       Q921DevSpace[x].sapi, \r
+                                       Q921DevSpace[x].NetUser == Q921_TE ? 0 : 1;\r
+                                       Q921DevSpace[x].tei, \r
+                                       0, \r
+                                       Mes, \r
+                                       Size);\r
+}\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendRR\r
+\r
+  Description:  Compose and send Receive Ready.\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          P/F fiels octet 5\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+\r
+int Q921SendRR(int trunk, int Sapi, int cr, int Tei, int pf)\r
+{\r
+    L2UCHAR mes[400];\r
+\r
+    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));\r
+    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);\r
+    mes[Q921HeaderSpace+2] = (L2UCHAR)0x01;\r
+    mes[Q921HeaderSpace+3] = (L2UCHAR)((Q921DevSpace[trunk].vr<<1) | (pf & 0x01));\r
+\r
+    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+4);\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendRNR\r
+\r
+  Description:  Compose and send Receive Nor Ready\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          P/F fiels octet 5\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+int Q921SendRNR(int trunk, int Sapi, int cr, int Tei, int pf)\r
+{\r
+    L2UCHAR mes[400];\r
+\r
+    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));\r
+    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);\r
+    mes[Q921HeaderSpace+2] = (L2UCHAR)0x05;\r
+    mes[Q921HeaderSpace+3] = (L2UCHAR)((Q921DevSpace[trunk].vr<<1) | (pf & 0x01));\r
+\r
+    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+4);\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendREJ\r
+\r
+  Description:  Compose and Send Reject.\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          P/F fiels octet 5\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+int Q921SendREJ(int trunk, int Sapi, int cr, int Tei, int pf)\r
+{\r
+    L2UCHAR mes[400];\r
+\r
+    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));\r
+    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);\r
+    mes[Q921HeaderSpace+2] = (L2UCHAR)0x09;\r
+    mes[Q921HeaderSpace+3] = (L2UCHAR)((Q921DevSpace[trunk].vr<<1) | (pf & 0x01));\r
+\r
+    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+4);\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendSABME\r
+\r
+  Description:  Compose and send SABME\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          P fiels octet 4\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+int Q921SendSABME(int trunk, int Sapi, int cr, int Tei, int pf)\r
+{\r
+    L2UCHAR mes[400];\r
+\r
+    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));\r
+    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);\r
+    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x6f | ((pf<<4)&0x10));\r
+\r
+    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendDM\r
+\r
+  Description:  Comose and Send DM (Disconnected Mode)\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          F fiels octet 4\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+int Q921SendDM(int trunk, int Sapi, int cr, int Tei, int pf)\r
+{\r
+    L2UCHAR mes[400];\r
+\r
+    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));\r
+    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);\r
+    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x0f | ((pf<<4)&0x10));\r
+\r
+    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendDISC\r
+\r
+  Description:  Compose and Send Disconnect\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          P fiels octet 4\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+int Q921SendDISC(int trunk, int Sapi, int cr, int Tei, int pf)\r
+{\r
+    L2UCHAR mes[400];\r
+\r
+    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));\r
+    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);\r
+    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x43 | ((pf<<4)&0x10));\r
+\r
+    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921SendUA\r
+\r
+  Description:  Compose and Send UA\r
+\r
+  Parameters:   trunk       trunk #\r
+                Sapi        Sapi\r
+                cr          C/R field.\r
+                Tei         Tei.\r
+                pf          F fiels octet 4\r
+\r
+  Return Value: 0 if failed, 1 if Send.\r
+\r
+*****************************************************************************/\r
+int Q921SendUA(int trunk, int Sapi, int cr, int Tei, int pf)\r
+{\r
+    L2UCHAR mes[400];\r
+\r
+    mes[Q921HeaderSpace+0] = (L2UCHAR)((Sapi&0xfc) | ((cr<<1)&0x02));\r
+    mes[Q921HeaderSpace+1] = (L2UCHAR)((Tei<<1) | 0x01);\r
+    mes[Q921HeaderSpace+2] = (L2UCHAR)(0x63 | ((pf<<4)&0x10));\r
+\r
+    return Q921Tx21Proc(trunk, mes, Q921HeaderSpace+3);\r
+}\r
+\r
+int Q921ProcSABME(int trunk, L2UCHAR *mes, int size)\r
+{\r
+       /* TODO:  Do we need these paramaters? */\r
+       (void)mes;\r
+       (void)size;\r
+\r
+    Q921DevSpace[trunk].vr=0;\r
+    Q921DevSpace[trunk].vs=0;\r
+\r
+    return 1;\r
+}\r
+\r
+/*****************************************************************************\r
+\r
+  Function:     Q921Rx12\r
+\r
+  Description:  Called to process a message frame from layer 1. Will \r
+                identify the message and call the proper 'processor' for\r
+                layer 2 messages and forward I frames to the layer 3 entity.\r
+\r
+                Q921Rx12 will check the input fifo for a message, and if a \r
+                message exist process one message before it exits. The caller\r
+                must either call Q921Rx12 polling or keep track on # \r
+                messages in the queue.\r
+\r
+  Parameters:   trunk       trunk #.\r
+\r
+  Return Value: # messages processed (always 1 or 0).\r
+\r
+*****************************************************************************/\r
+int Q921Rx12(long trunk)\r
+{\r
+    L2UCHAR *mes;\r
+    int rs,size;     /* receive size & Q921 frame size*/\r
+    L2UCHAR *smes = MFIFOGetMesPtr(Q921DevSpace[trunk].HDLCInQueue, &size);\r
+    if(smes != NULL)\r
+    {\r
+        rs = size - Q921HeaderSpace;\r
+        mes = &smes[Q921HeaderSpace];\r
+        /* check for I frame */\r
+        if((mes[2] & 0x01) == 0)\r
+        {\r
+            if(Q921Tx23Proc(trunk, smes, size-2)) /* -2 to clip away CRC */\r
+            {\r
+                Q921DevSpace[trunk].vr++;\r
+                Q921SendRR(trunk, (mes[0]&0xfc)>>2, (mes[0]>>1)&0x01,mes[1]>>1, mes[3]&0x01);\r
+            }\r
+            else\r
+            {\r
+                /* todo: whatever*/\r
+            }\r
+        }\r
+\r
+        /* check for RR */\r
+        else if(mes[2] ==0x01)\r
+        {\r
+            /* todo: check if RR is responce to I */\r
+            Q921SendRR(trunk, (mes[0]&0xfc)>>2, (mes[0]>>1)&0x01,mes[1]>>1, mes[2]&0x01);\r
+        }\r
+\r
+        /* check for RNR */\r
+        /* check for REJ */\r
+        /* check for SABME */\r
+        else if((mes[2] & 0xef) == 0x6f)\r
+        {\r
+            Q921ProcSABME(trunk, mes, rs);\r
+            Q921SendUA(trunk, (mes[0]&0xfc)>>2, (mes[0]>>1)&0x01,mes[1]>>1, (mes[2]&0x10)>>4);\r
+        }\r
+\r
+        /* check for DM */\r
+        /* check for UI */\r
+        /* check for DISC */\r
+        /* check for UA */\r
+        /* check for FRMR */\r
+        /* check for XID */\r
+\r
+        else\r
+        {\r
+            /* what the ? Issue an error */\r
+                       /* Q921ErrorProc(trunk, Q921_UNKNOWNFRAME, mes, rs); */\r
+            /* todo: REJ or FRMR */\r
+        }\r
+\r
+        MFIFOKillNext(Q921DevSpace[trunk].HDLCInQueue);\r
+\r
+        return 1;\r
+    }\r
+    return 0;\r
+}\r
+\r
index 26d4699c73f9a4618b0856e05a134a114e223a16..cec349820a6b591c5a591a0635818b0ecebd16b8 100644 (file)
-/*****************************************************************************
-
-  FileName:     q921.h
-
-  Description:  Contains headers of a Q.921 protocol on top of the Comet 
-                Driver.
-
-                Most of the work required to execute a Q.921 protocol is 
-                taken care of by the Comet ship and it's driver. This layer
-                will simply configure and make use of these features to 
-                complete a Q.921 implementation.
-
-  Note:         This header file is the only include file that should be 
-                acessed by users of the Q.921 stack.
-
-  Interface:    The Q.921 stack contains 2 layers. 
-
-                -   One interface layer.
-                -   One driver layer.
-
-                The interface layer contains the interface functions required 
-                for a layer 3 stack to be able to send and receive messages.
-
-                The driver layer will simply feed bytes into the ship as
-                required and queue messages received out from the ship.
-
-                Q921TimeTick        The Q.921 like any other blackbox 
-                                    modules contains no thread by it's own
-                                    and must therefore be called regularly 
-                                    by an external 'thread' to do maintenance
-                                    etc.
-
-                Q921Rx32            Receive message from layer 3. Called by
-                                    the layer 3 stack to send a message.
-
-                Q921Tx23            Send a message to layer 3. 
-
-                OnQ921Error         Function called every if an error is 
-                                    deteceted.
-
-                OnQ921Log           Function called if logging is active.
-
-
-                <TODO> Maintenance/Configuration interface
-
-  Created:      27.dec.2000/JVB
-
-  License/Copyright:
-
-  Copyright (c) 2007, Jan Vidar Berger, Case Labs, Ltd. All rights reserved.
-  email:janvb@caselaboratories.com  
-
-  Redistribution and use in source and binary forms, with or without 
-  modification, are permitted provided that the following conditions are 
-  met:
-
-    * Redistributions of source code must retain the above copyright notice, 
-         this list of conditions and the following disclaimer.
-    * Redistributions in binary form must reproduce the above copyright notice, 
-         this list of conditions and the following disclaimer in the documentation 
-         and/or other materials provided with the distribution.
-    * Neither the name of the Case Labs, Ltd nor the names of its contributors 
-         may be used to endorse or promote products derived from this software 
-         without specific prior written permission.
-
-  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
-  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
-  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
-  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
-  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
-  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
-  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
-  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
-  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
-  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
-  POSSIBILITY OF SUCH DAMAGE.
-
-*****************************************************************************/
-
-#ifndef _Q921
-#define _Q921
-
-#define Q921MAXTRUNK 4
-#define Q921MAXHDLCSPACE 3000
-
-/*****************************************************************************
-
-       Some speed optimization can be achieved by changing all variables to the 
-       word size of your processor. A 32 bit processor have to do a lot of extra 
-       work to read a packed 8 bit integer. Changing all fields to 32 bit integer 
-       will ressult in usage of some extra space, but speed up the stack.
-
-       The stack have been designed to allow L3UCHAR etc. to be any size of 8 bit
-       or larger.
-
-*****************************************************************************/
-
-#define L2UCHAR                unsigned char           /* Min 8 bit                                            */
-#define L2INT       int                 /* Min 16 bit signed                */
-
-typedef struct
-{
-    L2UCHAR HDLCInQueue[Q921MAXHDLCSPACE];
-    L2UCHAR vs;
-    L2UCHAR vr;
-    int state;
-}Q921Data;
-
-void Q921Init();
-void Q921SetHeaderSpace(int hspace);
-void Q921SetTx21CB(int (*callback)(int dev, L2UCHAR *, int));
-void Q921SetTx23CB(int (*callback)(int dev, L2UCHAR *, int));
-int Q921QueueHDLCFrame(int trunk, L2UCHAR *b, int size);
-int Q921Rx12(long trunk);
-
-#endif
-
+/*****************************************************************************\r
+\r
+  FileName:     q921.h\r
+\r
+  Description:  Contains headers of a Q.921 protocol on top of the Comet \r
+                Driver.\r
+\r
+                Most of the work required to execute a Q.921 protocol is \r
+                taken care of by the Comet ship and it's driver. This layer\r
+                will simply configure and make use of these features to \r
+                complete a Q.921 implementation.\r
+\r
+  Note:         This header file is the only include file that should be \r
+                acessed by users of the Q.921 stack.\r
+\r
+  Interface:    The Q.921 stack contains 2 layers. \r
+\r
+                -   One interface layer.\r
+                -   One driver layer.\r
+\r
+                The interface layer contains the interface functions required \r
+                for a layer 3 stack to be able to send and receive messages.\r
+\r
+                The driver layer will simply feed bytes into the ship as\r
+                required and queue messages received out from the ship.\r
+\r
+                Q921TimeTick        The Q.921 like any other blackbox \r
+                                    modules contains no thread by it's own\r
+                                    and must therefore be called regularly \r
+                                    by an external 'thread' to do maintenance\r
+                                    etc.\r
+\r
+                Q921Rx32            Receive message from layer 3. Called by\r
+                                    the layer 3 stack to send a message.\r
+\r
+                Q921Tx23            Send a message to layer 3. \r
+\r
+                OnQ921Error         Function called every if an error is \r
+                                    deteceted.\r
+\r
+                OnQ921Log           Function called if logging is active.\r
+\r
+\r
+                <TODO> Maintenance/Configuration interface\r
+\r
+  Created:      27.dec.2000/JVB\r
+\r
+  License/Copyright:\r
+\r
+  Copyright (c) 2007, Jan Vidar Berger, Case Labs, Ltd. All rights reserved.\r
+  email:janvb@caselaboratories.com  \r
+\r
+  Redistribution and use in source and binary forms, with or without \r
+  modification, are permitted provided that the following conditions are \r
+  met:\r
+\r
+    * Redistributions of source code must retain the above copyright notice, \r
+         this list of conditions and the following disclaimer.\r
+    * Redistributions in binary form must reproduce the above copyright notice, \r
+         this list of conditions and the following disclaimer in the documentation \r
+         and/or other materials provided with the distribution.\r
+    * Neither the name of the Case Labs, Ltd nor the names of its contributors \r
+         may be used to endorse or promote products derived from this software \r
+         without specific prior written permission.\r
+\r
+  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" \r
+  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE \r
+  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE \r
+  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE \r
+  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR \r
+  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF \r
+  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS \r
+  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN \r
+  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) \r
+  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE \r
+  POSSIBILITY OF SUCH DAMAGE.\r
+\r
+*****************************************************************************/\r
+\r
+#ifndef _Q921\r
+#define _Q921\r
+\r
+#define Q921MAXTRUNK 4\r
+#define Q921MAXHDLCSPACE 3000\r
+\r
+/*****************************************************************************\r
+\r
+       Some speed optimization can be achieved by changing all variables to the \r
+       word size of your processor. A 32 bit processor have to do a lot of extra \r
+       work to read a packed 8 bit integer. Changing all fields to 32 bit integer \r
+       will ressult in usage of some extra space, but speed up the stack.\r
+\r
+       The stack have been designed to allow L3UCHAR etc. to be any size of 8 bit\r
+       or larger.\r
+\r
+*****************************************************************************/\r
+\r
+#define L2UCHAR                unsigned char           /* Min 8 bit                                            */\r
+#define L2INT       int                 /* Min 16 bit signed                */\r
+\r
+typedef enum                                   /* Network/User Mode.                   */\r
+{\r
+       Q921_TE=0,                  /*  0 : User Mode                       */\r
+    Q921_NT=1                   /*  1 : Network Mode                    */\r
+} Q921NetUser_t;\r
+\r
+typedef struct\r
+{\r
+    L2UCHAR HDLCInQueue[Q921MAXHDLCSPACE];\r
+    L2UCHAR vs;\r
+    L2UCHAR vr;\r
+    L2INT state;\r
+       L2INT sapi;\r
+       L2INT tei;\r
+       Q921NetUser_t NetUser;\r
+\r
+}Q921Data;\r
+\r
+void Q921Init();\r
+int Q921_InitTrunk(long trunk, int sapi, int tei, Q921NetUser_t NetUser);\r
+void Q921SetHeaderSpace(int hspace);\r
+void Q921SetTx21CB(int (*callback)(int dev, L2UCHAR *, int));\r
+void Q921SetTx23CB(int (*callback)(int dev, L2UCHAR *, int));\r
+int Q921QueueHDLCFrame(int trunk, L2UCHAR *b, int size);\r
+int Q921Rx12(long trunk);\r
+\r
+#endif\r
+\r