Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

l2cap.h

Go to the documentation of this file.
00001 // GENERAL PUBLIC LICENSE AGREEMENT
00002 // 
00003 // PLEASE READ THIS DOCUMENT CAREFULLY BEFORE UTILIZING THE PROGRAM
00004 // 
00005 // BY UTILIZING THIS PROGRAM, YOU AGREE TO BECOME BOUND BY THE TERMS OF
00006 // THIS LICENSE.  IF YOU DO NOT AGREE TO THE TERMS OF THIS LICENSE, DO
00007 // NOT USE THIS PROGRAM OR ANY PORTION THEREOF IN ANY FORM OR MANNER.
00008 // 
00009 // This Program is licensed, not sold to you by GEORGIA TECH RESEARCH
00010 // CORPORATION ("GTRC"), owner of all code and accompanying documentation
00011 // (hereinafter "Program"), for use only under the terms of this License,
00012 // and GTRC reserves any rights not expressly granted to you.
00013 // 
00014 // 1.  This License allows you to:
00015 // 
00016 // (a) make copies and distribute copies of the Program's source code
00017 // provide that any such copy clearly displays any and all appropriate
00018 // copyright notices and disclaimer of warranty as set forth in Article 5
00019 // and 6 of this License.  All notices that refer to this License and to
00020 // the absence of any warranty must be kept intact at all times.  A copy
00021 // of this License must accompany any and all copies of the Program
00022 // distributed to third parties.
00023 // 
00024 // A fee may be charged to cover the cost associated with the physical
00025 // act of transferring a copy to a third party.  At no time shall the
00026 // program be sold for commercial gain either alone or incorporated with
00027 // other program(s) without entering into a separate agreement with GTRC.
00028 //  
00029 // 
00030 // (b) modify the original copy or copies of the Program or any portion
00031 // thereof ("Modification(s)").  Modifications may be copied and
00032 // distributed under the terms and conditions as set forth above,
00033 // provided the following conditions are met:
00034 // 
00035 //     i) any and all modified files must be affixed with prominent
00036 // notices that you have changed the files and the date that the changes
00037 // occurred.
00038 //              
00039 //     ii) any work that you distribute, publish, or make available, that
00040 // in whole or in part contains portions of the Program or derivative
00041 // work thereof, must be licensed at no charge to all third parties under
00042 // the terms of this License.
00043 // 
00044 //    iii) if the modified program normally reads commands interactively
00045 // when run, you must cause it, when started running for such interactive
00046 // use in the most ordinary way, to display and/or print an announcement
00047 // with all appropriate copyright notices and disclaimer of warranty as
00048 // set forth in Article 5 and 6 of this License to be clearly displayed.
00049 // In addition, you must provide reasonable access to this License to the
00050 // user.
00051 // 
00052 // Any portion of a Modification that can be reasonably considered
00053 // independent of the Program and separate work in and of itself is not
00054 // subject to the terms and conditions set forth in this License as long
00055 // as it is not distributed with the Program or any portion thereof.
00056 // 
00057 // 
00058 // 2. This License further allows you to copy and distribute the Program
00059 //    or a work based on it, as set forth in Article 1 Section b in
00060 //    object code or executable form under the terms of Article 1 above
00061 //    provided that you also either:
00062 // 
00063 //    i) accompany it with complete corresponding machine-readable source
00064 // code, which must be distributed under the terms of Article 1, on a
00065 // medium customarily used for software interchange; or,
00066 // 
00067 //   ii) accompany it with a written offer, valid for no less than three
00068 // (3) years from the time of distribution, to give any third party, for
00069 // no consideration greater than the cost of physical transfer, a
00070 // complete machine-readable copy of the corresponding source code, to be
00071 // distributed under the terms of Article 1 on a medium customarily used
00072 // for software interchange; or,
00073 // 
00074 // 
00075 // 3.  Export Law Assurance.
00076 // 
00077 // You agree that the Software will not be shipped, transferred or
00078 // exported, directly into any country prohibited by the United States
00079 // Export Administration Act and the regulations thereunder nor will be
00080 // used for any purpose prohibited by the Act.
00081 //  
00082 // 4.  Termination.
00083 // 
00084 // If at anytime you are unable to comply with any portion of this
00085 // License you must immediately cease use of the Program and all
00086 // distribution activities involving the Program or any portion thereof.
00087 // 
00088 // 
00089 // 5.  Disclaimer of Warranties and Limitation on Liability.
00090 // 
00091 // YOU ACCEPT THE PROGRAM ON AN "AS IS" BASIS.  GTRC MAKES NO WARRANTY
00092 // THAT ALL ERRORS CAN BE OR HAVE BEEN ELIMINATED FROM PROGRAM.  GTRC
00093 // SHALL NOT BE RESPONSIBLE FOR LOSSES OF ANY KIND RESULTING FROM THE USE
00094 // OF PROGRAM AND ITS ACCOMPANYING DOCUMENT(S), AND CAN IN NO WAY PROVIDE
00095 // COMPENSATION FOR ANY LOSSES SUSTAINED, INCLUDING BUT NOT LIMITED TO
00096 // ANY OBLIGATION, LIABILITY, RIGHT, CLAIM OR REMEDY FOR TORT, OR FOR ANY
00097 // ACTUAL OR ALLEGED INFRINGEMENT OF PATENTS, COPYRIGHTS, TRADE SECRETS,
00098 // OR SIMILAR RIGHTS OF THIRD PARTIES, NOR ANY BUSINESS EXPENSE, MACHINE
00099 // DOWNTIME OR DAMAGES CAUSED TO YOU BY ANY DEFICIENCY, DEFECT OR ERROR
00100 // IN PROGRAM OR MALFUNCTION THEREOF, NOR ANY INCIDENTAL OR CONSEQUENTIAL
00101 // DAMAGES, HOWEVER CAUSED.  GTRC DISCLAIMS ALL WARRANTIES, BOTH EXPRESS
00102 // AND IMPLIED RESPECTING THE USE AND OPERATION OF PROGRAM AND ITS
00103 // ACCOMPANYING DOCUMENTATION, INCLUDING ALL IMPLIED WARRANTIES OF
00104 // MERCHANTABILITY AND FITNESS FOR PARTICULAR PURPOSE AND ANY IMPLIED
00105 // WARRANTY ARISING FROM COURSE OF PERFORMANCE, COURSE OF DEALING OR
00106 // USAGE OF TRADE.  GTRC MAKES NO WARRANTY THAT PROGRAM IS ADEQUATELY OR
00107 // COMPLETELY DESCRIBED IN, OR BEHAVES IN ACCORDANCE WITH ANY
00108 // ACCOMPANYING DOCUMENTATION.  THE USER OF PROGRAM IS EXPECTED TO MAKE
00109 // THE FINAL EVALUATION OF PROGRAM'S USEFULNESS IN USER'S OWN
00110 // ENVIRONMENT.
00111 // 
00112 // GTRC represents that, to the best of its knowledge, the software
00113 // furnished hereunder does not infringe any copyright or patent.
00114 // 
00115 // GTRC shall have no obligation for support or maintenance of Program.
00116 // 
00117 // 6.  Copyright Notice.
00118 // 
00119 // THE SOFTWARE AND ACCOMPANYING DOCUMENTATION ARE COPYRIGHTED WITH ALL
00120 // RIGHTS RESERVED BY GTRC.  UNDER UNITED STATES COPYRIGHT LAWS, THE
00121 // SOFTWARE AND ITS ACCOMPANYING DOCUMENTATION MAY NOT BE COPIED EXCEPT
00122 // AS GRANTED HEREIN.
00123 // 
00124 // You acknowledge that GTRC is the sole owner of Program, including all
00125 // copyrights subsisting therein.  Any and all copies or partial copies
00126 // of Program made by you shall bear the copyright notice set forth below
00127 // and affixed to the original version or such other notice as GTRC shall
00128 // designate.  Such notice shall also be affixed to all improvements or
00129 // enhancements of Program made by you or portions thereof in such a
00130 // manner and location as to give reasonable notice of GTRC's copyright
00131 // as set forth in Article 1.
00132 // 
00133 // Said copyright notice shall read as follows:
00134 // 
00135 // Copyright 2004
00136 // Dr. George F. Riley
00137 // Georgia Tech Research Corporation
00138 // Atlanta, Georgia 30332-0415
00139 // All Rights Reserved
00140 //
00141 
00142 
00143 // Georgia Tech Network Simulator - Bluetooth L2CAP class
00144 // George F. Riley, Georgia Tech, Spring 2004
00145 
00146 #ifndef __l2cap_h
00147 #define __l2cap_h
00148 
00149 #include <assert.h>
00150 
00151 #include <list>
00152 #include <iostream> //for cout
00153 #include "bluetus.h"
00154 #include "baseband.h"
00155 #include "bnep.h"
00156 
00157 //Signalling channel ID
00158 #define SIGNAL_CHANNEL                  0x0001
00159 //Connection-less channel ID
00160 #define CONNLESS_CHANNEL                0x0002
00161 
00162 //Command reject reason
00163 #define REASON_NOTUNDERSTAND    0x0000
00164 #define REASON_MTUEXCEED                0x0001
00165 #define REASON_INVALIDCID               0x0002
00166 
00167 //PSM values
00168 #define SDP                                     0x0001
00169 #define RFCOMM                          0x0003
00170 #define TCS_BIN                         0x0005
00171 #define TCS_BIN_CORDLESS        0x0007
00172 #define BNEP                            0x000F
00173 #define HID_CONTROL                     0x0011
00174 #define HID_INTERRUPT           0x0013
00175 #define UPNP                            0x0015
00176 #define AVCTP                           0x0017
00177 #define AVDTP                           0x0019
00178 #define UDI_C_PLANE                     0x001D
00179 
00180 
00181 //signalling command codes
00182 #define COMMAND_RESERVED                0x00 
00183 #define COMMAND_REJECT                  0x01
00184 #define COMMAND_CONN_REQ                0x02
00185 #define COMMAND_CONN_RESP               0x03
00186 #define COMMAND_CFG_REQ                 0x04
00187 #define COMMAND_CFG_RESP                0x05
00188 #define COMMAND_DISCONN_REQ             0x06
00189 #define COMMAND_DISCONN_RESP    0x07
00190 #define COMMAND_ECHO_REQ                0x08
00191 #define COMMAND_ECHO_RESP               0x09
00192 #define COMMAND_INFO_REQ                0x0A
00193 #define COMMAND_INFO_RESP               0x0B
00194 
00195 
00196 //Connection oriented channel state
00197 
00198 // The timer bound is 10, request can try 5 times
00199 // before giving up.
00200 //
00201 #define TIMEOUT_VALUE                   10 //10 seconds.
00202 #define MAX_TRY                                 5
00203 
00204 using namespace std;
00205 
00206 typedef enum {CLOSED, W4_L2CAP_CONNECT_RSP, W4_L2CA_CONNECT_RSP, CONFIG,
00207                 OPEN,  W4_L2CAP_DISCONNECT_RSP, W4_L2CA_DISCONNECT_RSP
00208              } L2CAP_state;
00209 
00210 
00211 //packet format of L2CAP within a connection-oriented channel
00212 //
00213 struct L2CAPPacket {
00214         uShort usLength;
00215         uShort usChannelID;
00216         uChar ucPayload[1];
00217 
00218         int HdrSize() {
00219                 return 2*sizeof(uShort);
00220         }
00221 };
00222 
00223 //structure related to Signalling
00224 struct CommandPacket {
00225         uChar ucCode;
00226         uChar ucID;
00227         uShort usLength;
00228         uChar ucData[1];
00229 
00230         int HdrSize(){
00231                 return 2*sizeof(uChar) + sizeof(uShort);
00232         }
00233 };
00234 
00235 struct CmdReject {
00236         uShort usReason;
00237         //optional data follows if any
00238 };
00239 //** COMMAND REJECT**
00240 // code: 0x01;     nLength>=0x0002; pData contains reason + data(optional)
00241 // Reason: 0x0000 command not understood; 0x0001 signalling MTU exceeded;
00242 //         0x0002 invalid CID in request; other reserved.
00243 // Data:   reason==0x0000 --> N/A
00244 //                 reason==0x0001 --> data length is 2 octs, actual MTU
00245 //                 reason==0x0002 --> data length is 4 octs, requested CID;
00246 
00247 struct CmdConnReq {
00248         uShort usPSM;
00249         uShort usSrcCID;
00250 };
00251 //** CONNECTION REQUEST**
00252 // code: 0x02;    nLength>=0x0004; pData contains PSM + SourceID
00253 // PSM:  (Protocol/Service Multiplexor) at least 2 octs.
00254 //                 0x0001 Service Discovery Protocol
00255 //                 0x0003 RFCOMM
00256 //                 0x0005 Telephony COntrol Protocol
00257 //                 <0x100 RESERVED.
00258 // SourceID: source local ID represents a channel endpoint on the device 
00259 //         sending the request.
00260 
00261 struct CmdConnResp {
00262         uShort usDstCID;
00263         uShort usSrcCID;
00264         uShort usResult;
00265         uShort usStatus;
00266 };
00267 //** CONNECTION RESPSONSE**
00268 // code: 0x03;    nLength=0x0008; pData contains Dest CID + SRC CID + Result +Status
00269 // Dest CID: contains the channel endpoint on the device receiving the request and
00270 //                       sending the response.
00271 // SRC CID:  contains the channel endpoint to receive this response packet.
00272 // Result:   2 octs
00273 //                   0x0000 connection successful
00274 //                       0x0001 connection pending
00275 //                       0x0002 connection refused --PSM not supported
00276 //                       0x0003 connection refused --Security block
00277 //                       0x0004 connection refused --no resources available
00278 //                       other  Reserved.
00279 // Status:   2 octs
00280 //                       0x0000 no further informatin available
00281 //                       0x0001 authentication pending
00282 //                       0x0002 authorization pending
00283 //                       other  Reserved.
00284 
00285 struct CmdCfgReq {
00286         uShort usDstCID;
00287         uShort usFlags;
00288         //options follows if any
00289 };
00290 
00291 struct CfgParamOpt {
00292         uChar ucType;
00293         uChar ucLength;
00294 };
00295 #define TYPE_MTU                0x01
00296 #define TYPE_FLUSHTIMEO 0x02
00297 #define TYPE_QOS                0x03
00298 
00299 struct QosOpt {
00300         uChar ucFlags; //reserved. set 0;
00301         uChar ucServiceType;
00302         
00303         uLong ulTokenRate;
00304         uLong ulTokenBucketSize;
00305         uLong ulPeakBandw;
00306         uLong ulLatency;
00307         uLong ulDelayVar;
00308 };
00309 //** CONFIGURATION REQUEST**
00310 //code: 0x04; nLength>=0x0004; pData contains Dest CID + Flags + Options
00311 //Dest CID:  2 octs,contains the channel end-point on the device receiving this Request
00312 //Flags:     2 octs,only LSB is used. C-- continuation flag.
00313 //Options:   the list of the parameters and their values to be negotiated.
00314 //              ##configuration formate: LSB---->MSB
00315 //                                                      type(1 oct) length(1 oct) option data(depends on length)
00316 //                                      MTU:    0x01        2             MTU
00317 //              Flush timeout:      0x02        2             flush timeout(ms)
00318 //                                      QoS:    0x03            22                        Flags(1)+ServiceType(1)+TokenRate(4)
00319 //                                                                                                        +TokenBucketSize(4)+PeakBandWidth(4)+
00320 //                                                                                                        Latency(4)+DelayVariation(4)
00321 //          in which ServiceType: 0x00 no traffic; 0x01 Best effort; 0x02 Guaranteed
00322 //                                                other reserved.
00323 //              
00324 
00325 struct CmdCfgResp {
00326         uShort usSrcCID;
00327         uShort usFlags;
00328         uShort usResult;
00329         uShort usConfig;
00330 };
00331 //** CONFIGURE RESPONSE**
00332 //code: 0x05;    nLength>=0x0006; pData contains SRC CID + Flags + Result + Config
00333 //SRC CID:   2 octs,contains the channel end-point on the device receiving this Response
00334 //Flags:     2 octs,only LSB is used. C-- more configurgation responses will follow 
00335 //                       when set to 1
00336 //Result:    2 octs, indicates whether or not the request was acceptable.
00337 //           0x0000 Success
00338 //           0x0001 Failure --unacceptable parameters
00339 //           0x0002 Failure --rejected(no reason provided)
00340 //           0x0003 Failure --unknown options
00341 //           other  Reserved
00342 //Config:    contains configuration options being negotiated.
00343 
00344 struct CmdDisconnReq {
00345         uShort usDstCID;
00346         uShort usSrcCID;
00347 };
00348 //** DISCONNECTION REQUEST**
00349 //code: 0x06;    nLength=0x0004; pData contains Dest CID + SRC CID
00350 //Dest CID:  2 octs,contains the end-point of the channel to be shutdown on the device 
00351 //                       receiving this request
00352 //SRC CID:   2 octs, contains the end-point of the channel on the device sending this 
00353 //               request
00354 
00355 struct CmdDisconnResp {
00356         uShort usDstCID;
00357         uShort usSrcCID;
00358 };
00359 //** DISCONNECTION RESPONSE**
00360 //code: 0x07;    nLength=0x0004; pData contains Dest CID + SRC CID
00361 //Dest CID:  2 octs, identifies the channel end-point on the device sending the response
00362 //SRC CID:   2 octs, identifies the channel end-point on the device receiving the resps.
00363 
00364 
00365 //** ECHO REQUEST**
00366 //code: 0x08;     nLength>=0; pData is optional
00367 
00368 //** ECHO RESPONSE**
00369 //code: 0x09;     nLength>=0; pData is optional
00370 
00371 struct CmdInfoReq {
00372         uShort usInfoType;
00373 };
00374 //** INFORMATION REQUEST**
00375 //code: 0x0a;     nLength=0x0002; pData contains Infotype
00376 //InfoType:   0x0001 connectionless MTU
00377 //                        other Reserved
00378 
00379 struct CmdInfoResp {
00380         uShort usInfoType;
00381         uShort usResult;
00382         //options follows
00383 };      
00384 //** INFORMATION RESPONSE**
00385 //code: 0x0b;     nLength>=0x0004; pData contains InfoType + Result + Data(optional)
00386 //Infotype:   2 octs, the same as Request
00387 //Result:     2 octs. 0x0000 Success
00388 //                                        0x0001 Not supported
00389 //                                        other  Reserved
00390 //Data:       0 or more octs. if infoType==0x0001, Data contains connectionless MTU and 
00391 //                        data length is 2 octs.
00392 
00393 #define MAX_QUEUE_LEN   128
00394 
00395 
00396 //Timer events
00397 class L2CAPTimerEvent : public TimerEvent {
00398         public:
00399                 // the types of the L2CAP timer events
00400                 typedef enum {CONNREQ_TIMEOUT, CFGREQ_TIMEOUT, DISCONNREQ_TIMEOUT
00401                                 } L2CAPTimerEvent_t;
00402         public:
00403                 L2CAPTimerEvent(Event_t ev, uChar ucId) 
00404                                 : TimerEvent(ev) {
00405                                                         ucRequestId = ucId;
00406                                 }
00407                 virtual ~L2CAPTimerEvent() {}
00408                 
00409                 uChar ucRequestId;       //the request command identifier.
00410 };
00411 
00412 typedef std::list<L2CAPPacket *> L2CAPPacketList;
00413 
00414 class L2capQueue {
00415 public:
00416         L2capQueue(){ usQueLen = 0;}
00417         bool Enque(L2CAPPacket *pPacket); //pPacket is pass-in parameter.
00418         bool DeQue(L2CAPPacket *pPacket); //pPacket is output parameter.
00419         ~L2capQueue() {
00420         }
00421 private:
00422         uShort usQueLen;
00423         L2CAPPacketList L2capPktList;
00424 };
00425 
00426 
00427 //L2CAP channel.
00428 class L2capChannel {
00429 public:
00430         L2capChannel(uShort usRemoteChannelID = 0x0000,
00431                                 uShort usLocalChannelID = 0x0000) {
00432                 pRxQueue = NULL;
00433                 this->usRemoteChannelID = usRemoteChannelID;
00434                 this->usLocalChannelID = usLocalChannelID;
00435                 pLocalAddr = NULL;
00436                 pRemoteAddr = NULL;
00437         }
00438         
00439         virtual ~L2capChannel() {
00440                 delete pLocalAddr;
00441                 delete pRemoteAddr;
00442                 if(pRxQueue) 
00443                         delete pRxQueue;
00444         }
00445         
00446         virtual uShort DataIndication(L2CAPPacket *pPacket) = 0;
00447         
00448         void SetLocalChannelID(uShort usLocalChannelID) {
00449                 this->usLocalChannelID = usLocalChannelID;
00450                 return;
00451         }
00452         
00453         uShort GetLocalChannelID() {
00454                 return usLocalChannelID;
00455         }
00456         void SetRemoteChannelID(uShort usRemoteChannelID) {
00457                 this->usRemoteChannelID = usRemoteChannelID;
00458                 return;
00459         }
00460         uShort GetRemoteChannelID() {
00461                 return usRemoteChannelID;
00462         }
00463 
00464         BdAddr * GetRemoteAddr() {
00465                 assert(pRemoteAddr!=NULL);
00466                 return pRemoteAddr;
00467         }
00468         void SetRemoteAddr(BdAddr addr) {
00469                 if(pRemoteAddr)
00470                         delete pRemoteAddr;
00471                 pRemoteAddr = new BdAddr;
00472                 memcpy(pRemoteAddr, &addr, sizeof(BdAddr));
00473                 return ;
00474         }
00475         
00476         BdAddr * GetLocalAddr() {
00477                 assert(pLocalAddr!=NULL);
00478                 return pLocalAddr;
00479         }
00480         void SetLocalAddr(BdAddr addr) {
00481                 if(pLocalAddr)
00482                         delete pLocalAddr;
00483                 pLocalAddr = new BdAddr;
00484                 memcpy(pLocalAddr, &addr, sizeof(BdAddr));
00485                 return ;
00486         }
00487         
00488         virtual uShort GetMTU() {
00489                 return 0;
00490         }       
00491 public:
00492         uShort usOutMTU; //outgoing MTU 
00493         uShort usInFlushTO;// incoming flush timeout
00494         
00495 private:
00496         uChar ucChannelType; //conn-oriented, conn-less and signalling
00497         
00498         BdAddr  *pLocalAddr;
00499         BdAddr  *pRemoteAddr; //remote device address.
00500 
00501         L2capQueue      *pRxQueue;
00502 
00503         //local and remote ChannelID;
00504         uShort usRemoteChannelID;
00505         uShort usLocalChannelID;
00506 
00507 };
00508 
00509 //360 seconds later, it can be reused.
00510 struct L2capCmdId {
00511         uChar ucIdentifier;
00512         //uChar ucState; // 1 "remote request" answered : 0 "remote request" not answered; 
00513                                                    // 3 "local request" answered : 2 "local request" not answered.
00514         double dExpire; //after this time it can be recyled.
00515 };
00516 
00517 typedef std::list<L2capCmdId> L2capCmdIdList;
00518 
00519 struct ConfigPara {
00520         uShort usCID;
00521         uShort usInMTU;
00522         void *pOutFlow;
00523         uShort usOutFlushTO;
00524         uShort usLinkTO;
00525         uShort *pInMTU_O;
00526         void *pOutFlow_O;
00527         uShort *pOutFlushTO_O;
00528 };      
00529         
00530 //
00531 //Class Signalling channel
00532 //
00533 class L2cap;
00534 class L2capConnChannel;
00535 
00536 class L2capSignalChannel: public L2capChannel{
00537 
00538 public: 
00539         //constructor
00540         L2capSignalChannel(L2cap *pL2CAP = NULL);
00541         //destructor
00542         ~L2capSignalChannel();
00543 
00544         //define an empty virtual function
00545         virtual uShort DataIndication(L2CAPPacket *pPacket) {return 0x0000;};
00546         //receive packet from lower layer.
00547         uShort DataIndication(L2CAPPacket *pPacket, L2capConnChannel *pChannel);
00548         
00549         //send signal packet, called by upper layer.
00550         uShort DataRequest(void *pData);
00551 
00552         //CmdID manage function.
00553         bool CmdIdRemove(L2capConnChannel *pChannel, uChar ucId);
00554         void CmdIdPurge(L2capConnChannel *pChannel);
00555         bool CmdIdLookup(L2capConnChannel *pChannel, uChar ucId);
00556         L2capCmdId CmdIdAlloc(L2capConnChannel *pChannel);
00557         bool CmdIdInsert(L2capConnChannel *pChannel, uChar ucID);
00558 
00559         virtual uShort GetMTU() {
00560                 return usMTU;
00561         }       
00562         //Helper function.
00563         uShort BuildCmd(uChar code, uChar identifier, 
00564                         uShort length, void *data, void *commandBuf);
00565 
00566         uShort L2CA_ConnectReq(uShort PSM, BdAddr BD_Addr, uShort *pLCID, 
00567                                         uChar ucId, uShort *pStatus, 
00568                                         L2capConnChannel *pChannel);
00569         uShort L2CA_ConnectRsp(BdAddr BD_Addr,uChar ucIdentifier,uShort 
00570                                         LCID, uShort response, uShort status);
00571         uShort L2CA_ConfigReq(uShort CID, uShort InMTU,void *pOutFlow, 
00572                                         uShort OutFlushTO, uShort LinkTO,uShort *pInMTU_O,
00573                                         void *pOutFlow_O,uShort *pOutFlushTO_O,uChar ucId,
00574                                         L2capConnChannel *pChannel);
00575         uShort L2CA_ConfigRsp(uShort LCID, uShort OutMTU,uChar ucIdentifier,
00576                                         uShort Result, void *pInFlow,
00577                                         L2capConnChannel *pChannel);
00578         uShort L2CA_DisconnectReq(uShort usLocalCID, uChar ucId);
00579         uShort L2CA_DisconnectRsp(uShort usLocalCID, uChar ucIdentifier);
00580 
00581         uShort Dispatch(CommandPacket *pCommand, L2capConnChannel *pChannel);
00582         //Action of receiving signalling.
00583         uShort RecvReject(CommandPacket *pCommand);
00584         uShort RecvConnReq(CommandPacket *pCommand);
00585         uShort RecvConnResp(CommandPacket *pCommand);
00586         uShort RecvCfgReq(CommandPacket *pCommand);
00587         uShort RecvCfgResp(CommandPacket *pCommand,L2capConnChannel *pChannel);
00588         uShort RecvDisconnReq(CommandPacket *pCommand);
00589         uShort RecvDisconnResp(CommandPacket *pCommand);
00590         
00591         void SaveCfgPara(uShort CID, uShort InMTU, void *pOutFlow,
00592                                         uShort OutFlushTO, uShort LinkTO, uShort *pInMTU_O,
00593                                         void *pOutFlow_O, uShort *pOutFlushTO_O) {
00594                 CfgParameter.usCID = CID;
00595                 CfgParameter.usInMTU = InMTU;
00596                 CfgParameter.pOutFlow = pOutFlow;
00597                 CfgParameter.usOutFlushTO = OutFlushTO;
00598                 CfgParameter.usLinkTO = LinkTO;
00599                 CfgParameter.pInMTU_O = pInMTU_O;
00600                 CfgParameter.pOutFlow_O = pOutFlow_O;
00601                 CfgParameter.pOutFlushTO_O = pOutFlushTO_O;
00602                 return;
00603         }
00604         
00605         ConfigPara GetCfgPara() {
00606                 return CfgParameter;
00607         }
00608         //ConfigureMTU
00609 private:
00610         L2cap *pL2CAP; //pointer back to L2CAP entity.
00611         uShort usMTU; 
00612         
00613         ConfigPara CfgParameter;
00614         L2capQueue      *pTxQueue;
00615 };
00616 
00617 //
00618 //Class Connection oriented channel
00619 //
00620 class L2capConnChannel: public L2capChannel , public TimerHandler {
00621 
00622 public:
00623 #define DEFAULT_MTU             672
00624 #define DEFAULT_TIMEO   0xFFFF //msec
00625         //constructor   
00626         L2capConnChannel(uShort usPSM = 0x0000,uShort usOutMTU = DEFAULT_MTU,
00627                                          uShort usInFlushTO = DEFAULT_TIMEO,
00628                                          uShort usInMTU = DEFAULT_MTU,
00629                                          uShort usOutFlushTO = DEFAULT_TIMEO);
00630         //destructor
00631         ~L2capConnChannel();
00632 
00633         //called by L2CAP::Demultiplxr  
00634         int Demultiplxr(L2CAPPacket *pPacket);
00635 
00636         //receive packet from lower layer.
00637         virtual uShort DataIndication(L2CAPPacket *pPacket);
00638         
00639         virtual uShort GetMTU() {
00640                 return usInMTU;
00641         }       
00642         //send packet
00643         uShort DataRequest(uShort usLength, void *pData);
00644 
00645         L2CAP_state usState; //channel state.
00646         uShort usCfgState; //configure state.
00647         
00648         L2cap *pL2CAP ; //pointer back to L2CAP entity.
00649         L2capSignalChannel *pSignalChannel; //pointer to signal channel
00650         
00651 #define ILLEGAL_COMMAND_ID      0x00
00652         uChar ucPendingCmdId; //Before processing the current request, no other allows!
00653         
00654         L2capCmdIdList CmdIdList; //command Identifier list for this channel.
00655         
00656 public:
00657         Timer timer; //timer for various timer event in signalling.
00658         
00659         L2CAPTimerEvent *pConnReqTimeout;
00660         L2CAPTimerEvent *pCfgReqTimeout;
00661         L2CAPTimerEvent *pDisconnReqTimeout;
00662 
00663         // Timer handle methods
00664         virtual void Timeout(TimerEvent*); // Called when timers expire 
00665         // Timer management
00666         void ScheduleTimer(Event_t, uChar, L2CAPTimerEvent*&, Time_t);
00667         void CancelTimer(L2CAPTimerEvent*&, bool delTimer = false);
00668         void CancelAllTimers(); 
00669 
00670 public:
00671         //max retransmit of a signalling request before terminate
00672         //the channel
00673         uChar ucMaxRetryReq; // 5 in our case.
00674         
00675 private:                
00676         L2capQueue      *pTxQueue;
00677         
00678         uShort usPSM; //Protocol/Service Multiplextor
00679 
00680 
00681         //options
00682         uShort usInMTU; //incoming MTU
00683         uShort usOutFlushTO;//outgoing flush timeout
00684 
00685         
00686         //QoS only for connection.
00687         QosOpt RequestQoS;  //local QoS, to negotiated with remote peer.
00688         QosOpt RequiredQoS; //remote QoS, proposed by remote entity
00689 
00690 };
00691 
00692 struct AssembleBuf {
00693         uShort usTotalLen;
00694         uShort usOccupied;
00695         uShort usChannelID;
00696         uChar *pBuf;
00697 };
00698 
00699 typedef std::list<L2capConnChannel *> L2capChannelList;
00700 
00701 class BaseBand;
00702 class Bnep;
00703 
00704 // class L2cap.
00705 class  L2cap {
00706 public:
00707         L2cap();
00708          
00709         ~L2cap();
00710                 
00711 public:
00712 
00713         uShort DataIndication(uChar ucLogicChannel, uChar ucFlow,
00714                                         uShort usLen, uChar *pData);
00715 
00716         void Segmentation(uShort usLen, uChar *pData);
00717         void Reassembly();
00718 
00719         void DataRequest(L2CAPPacket *pPacket);
00720         uShort DataRequest(uShort usLen, uShort usLocalCID, void *pData);
00721 
00722         //int Demultiplxr(L2capChannel *pChannel, L2CAPPacket *pPacket);
00723 
00724         //Build and send L2CapPacket.
00725         uShort SendL2CAPPacket(uShort usLength, uShort usChannelID,
00726                                 void * payload, L2capConnChannel *pChannel);
00727 
00728 
00729         uShort GetMTU(uShort usChannelID) {
00730                 L2capConnChannel *pChannel;
00731                 pChannel = ChannelLookupByLocalCID(usChannelID);
00732                 return pChannel->usOutMTU;
00733         }
00734         uShort GetFlushTimeout(uShort usChannelID) {
00735                 L2capConnChannel *pChannel;
00736                 pChannel = ChannelLookupByLocalCID(usChannelID);
00737                 return pChannel->usInFlushTO;
00738         }
00739         //routines related to channel id
00740         uShort AllocateChannelID();
00741         bool ChannelInsert(L2capConnChannel *pChannel);
00742         L2capConnChannel * ChannelLookupByLocalCID(uShort usLocalChannelID);
00743         L2capConnChannel * ChannelLookupByRemoteCID(uShort usRemoteChannelID);
00744         bool ChannelClose(uShort usChannelID);
00745 
00746         uShort L2CA_ConnectReq(uShort PSM, BdAddr BD_Addr,
00747                                 uShort *pLCID, uShort *pStatus);
00748         uShort L2CA_ConnectRsp(BdAddr BD_Addr,uChar ucIdentifier,
00749                             uShort LCID, uShort response, uShort status);
00750         
00751         uShort L2CA_ConfigReq(uShort CID, uShort InMTU, void *pOutFlow, 
00752                                         uShort OutFlushTO, uShort LinkTO, uShort *pInMTU_O,
00753                                         void *pOutFlow_O, uShort *pOutFlushTO_O);
00754         uShort L2CA_ConfigRsp(uShort LCID, uShort OutMTU, uChar ucId, void *pInFlow, 
00755                                         uShort result);
00756         uShort L2CA_DisconnectReq(uShort CID);
00757         uShort L2CA_DisconnectRsp(uShort usLocalCID, uChar ucIdentifier);
00758 
00759         void L2CA_DataWriteReq(uShort usCID, uShort usLength,
00760                         void *OutBuffer);
00761         
00762         uShort L2CA_DataWriteCfm(uLong *size );
00763         uShort L2CA_DataRead(uLong CID, uLong length, void *pInBuffer, uLong *pN);
00764         uShort L2CA_GroupCreate(uLong PSM);
00765         uShort L2CA_GroupClose(uLong CID);
00766         uShort L2CA_GroupAddMember(uLong CID, BdAddr BD_Addr);
00767         uShort L2CA_GroupRemoveMember(uLong CID, BdAddr BD_Addr);
00768         uShort L2CA_GroupMembership(uLong CID, uLong *pN,
00769                                                         void *BD_Addr_List);
00770         uShort L2CA_Ping(BdAddr BD_ADDR, uLong length,void *pEchoData,
00771                                                         uLong size);
00772         uShort L2CA_DisableCLT(uLong PSM);
00773         uShort L2CA_EnableCLT(uLong PSM);
00774   
00775         void AttachLMP(LMP *pLMPStack) {
00776                 if((pLMP==NULL)&&(pLMPStack!=NULL))
00777                         pLMP = pLMPStack;
00778                 else
00779                         cout<<"LMP already attached or parameter invalid."<<endl;
00780         }
00781 #define UPPER_PROTOCOL  Bnep
00782 #define LOWER_PROTOCOL  BaseBand
00783         Bnep  * pUpperProtocol;  // upper layer protocol pointer.
00784         BaseBand  * pLowerProtocol; // lower layer protocol pointer.
00785         LMP *pLMP; //point to LMP protocol object
00786          
00787 /*
00788 #define UPPER_PROTOCOL  Bnep
00789 #define LOWER_PROTOCOL  BaseBand
00790         UPPER_PROTOCOL  * pUpperProtocol;  // upper layer protocol pointer.
00791         LOWER_PROTOCOL  * pLowerProtocol; // lower layer protocol pointer.
00792 */
00793 
00794 private:
00795 
00796         AssembleBuf     assembleBuf;
00797         Node *  pNode; //local bluetooth node.
00798         BdAddr  localAddr; //can be obtained by pNode->GetLocalAddr;
00799         
00800         //receive event from lower layer
00801         int EventIndication(Event_t);
00802 
00803         //TODO: ACL link or link state?
00804         
00805         L2capSignalChannel *pSignalChannel; //Signal channel pointer.
00806         
00807         L2capChannelList ChannelList; //for connection-oriented channel only!
00808 };
00809 
00810 #endif

Generated on Wed Aug 27 16:17:13 2008 for Georgia Tech Network Simulator (GTNetS) by  doxygen 1.3.9.1