1/*
2    File:       CIrLSAP.h
3
4*/
5
6#ifndef __CIRLSAP__
7#define __CIRLSAP__
8
9#include "IrStream.h"
10#include "IrDiscovery.h"
11#include "IrDscInfo.h"
12#include "IrEvent.h"
13
14enum CIrLSAPStates
15{
16    kIrLSAPDisconnected,        // nothing going on
17
18    kIrLSAPDisconnectStart,     // these all transition to disconnected
19    kIrLSAPDiscoverStart,       // when the request is done
20    kIrLSAPLookupStart,
21
22    kIrLSAPConnectStart,        // connect goes to connected or disconnected
23    kIrLSAPConnected,
24
25    kIrLSAPListenStart,         // listen goes to listencomplete or disconnected
26    kIrLSAPListenComplete,
27    kIrLSAPAcceptStart          // transitions to connected when finished
28};
29
30typedef struct DiscoveryInfo
31{
32    UInt8               name[kMaxNicknameLen+1];
33    UInt32              addr,
34			serviceHints;
35} DiscoveryInfo;
36
37class TIASClient;
38class TIrGlue;
39class TIrLMP;
40//class CIrDiscovery;
41class TIrDscInfo;
42class CBufferSegment;
43
44class CIrLSAP : public TIrStream
45{
46			    OSDeclareAbstractStructors(CIrLSAP);
47public:
48    Boolean                 Init(TIrGlue *irda, UInt32 desiredLSAPId, UInt8 * className, UInt8 * attributeName, ULong hints=0);
49    void                    free();
50
51    void                    SetPeerLAPAddr( UInt32 addr );      // For those who already know
52    UInt32                  GetPeerLAPAddr( void );             // For info only
53    UInt32                  GetMyLSAPId(void);
54    UInt32                  GetState (void);
55
56    IrDAErr                 Discover    ( UInt32 slots );
57    IrDAErr                 LSAPLookup  ( UInt8 * className, UInt8 * attributeName, UInt32 remoteAddr );
58    IrDAErr                 Connect     ( CBufferSegment *connectData );
59    IrDAErr                 Connect     ( UInt32 lsapID, CBufferSegment *connectData );
60    IrDAErr                 Connect     ( UInt32 remoteAddr, UInt32 lsapID, CBufferSegment *connectData );
61    IrDAErr                 DataPut     ( CBufferSegment * data );
62    IrDAErr                 DataGet     ( CBufferSegment * data );
63    IrDAErr                 Listen      ( CBufferSegment *connectData );
64    IrDAErr                 Accept      ( CBufferSegment *connectData );
65    IrDAErr                 CancelGets  ( void );               // abort all pending gets
66    IrDAErr                 CancelPuts  ( void );               // abort all pending puts
67    void                    Disconnect  ( void );
68
69    void                    NextState( UInt32 event );          // called with new event (by IrStream/Glue)
70
71    Boolean                 Connected( void ) { return fConnected; };
72
73						// client needs to subclass and provide these
74    virtual void DiscoverComplete (             // discovery has finished callback
75		    UInt32  numFound,           // number of peers discovered
76		    IrDAErr result  ) = 0 ;     // Any error returned
77
78    virtual void LSAPLookupComplete (           // an IAS query has finished
79		    IrDAErr result,             // result of the lookup
80		    UInt32  peerLSAPId) = 0 ;   // peer's LSAP id of the service
81
82    virtual void ConnectComplete (              // a connect request has completed
83		    IrDAErr result,             // result of the connect request
84		    TIrQOS *myQOS,              // my qos ... (requested?)
85		    TIrQOS *peerQOS,            // peer's qos .. (result?)
86		    CBufferSegment *data) = 0 ; // data payload from connect msg
87
88    virtual void DisconnectComplete ( void ) = 0 ;  // you've been disconnected
89						    // hmmph.  where's the reason code?
90
91    virtual void DataPutComplete (
92		    IrDAErr result,                 // result code
93		    CBufferSegment *data) = 0 ;     // data that was sent
94
95    virtual void DataGetComplete (
96		    IrDAErr result,                 // result code
97		    CBufferSegment *data) = 0 ;     // data
98
99    virtual void ListenComplete (                   // check me
100		    IrDAErr result,
101		    UInt32  peerAddr,               // address of connecting peer
102		    UInt32  peerLSAPId,             // LSAP id of connecting peer
103		    TIrQOS  *myQOS,                 // my qos ... (requested?)
104		    TIrQOS  *peerQOS,               // peer's qos .. (result?)
105		    CBufferSegment *data) = 0 ;     // data payload from connect msg
106
107    virtual void AcceptComplete (                   // check me
108		    IrDAErr result,
109		    CBufferSegment *data) = 0 ;     // data payload in connect msg
110
111    virtual void CancelGetsComplete (               // all pending gets have been canceled
112		    IrDAErr result) = 0;
113
114    virtual void CancelPutsComplete (               // all pending puts have been canceled
115		    IrDAErr result) = 0;
116
117
118
119protected:
120    TIrDscInfo          GetDiscoveryInfo                ( void );
121    void                SetState                        (UInt32 newState);
122
123    void                HandleDisconnectComplete        ( void );       // These are the internal routines called by NextState()
124    void                HandleDiscoverComplete          ( void );
125
126    void                HandleLSAPLookupComplete        ( void );
127    void                HandleNameServerConnectComplete ( void );
128    void                HandleNameServerLookupComplete  ( void );
129    void                HandleNameServerReleaseComplete ( void );
130
131    void                HandleConnectComplete           ( void );
132    void                HandleListenComplete            ( void );
133    void                HandleAcceptComplete            ( void );
134    void                HandleDataGetComplete           ( void );
135    void                HandleDataPutComplete           ( void );
136    void                HandleCancelPutComplete         ( void );
137    void                HandleCancelGetComplete         ( void );
138
139    UInt32              fState;
140    UInt32              fPeerAddr;                  // peer address, saved by LSAPLookup and set by SetPeerLAPAddr()
141    UInt32              fMyLSAPId;                  // local lsap id (port number)
142    UInt32              fPeerLSAPId;                // remote lsap id (port number)
143
144    UInt8               fClassName[64];             // IAS class name for this LSAP
145    UInt8               fAttrName[64];              // IAS attribute name for this LSAP
146    UInt8               fConnectClassName[64];      // IAS Lookup class name
147    UInt8               fAttributeName[64];         // IAS Lookup attribute name
148    ULong               fHints;                     // discovery hints set/cleared by this LSAP
149
150    Boolean             fConnected;
151
152    UInt32              fDiscoverCount;
153    DiscoveryInfo       fDiscoverInfo[kMaxDiscoverSlots];
154    TIrDscInfo      *   fDscInfo;                   // our own discovery info, from glue
155
156private:
157    TIASClient      *   fNameClient;                // we alloc and free this IAS stream client
158    TLSAPConn       *   fLSAP;                      // our lsap connection.  glue makes this, we free it
159    CIrDiscovery    *   fDiscovery;                 // discovery client, from glue
160    Boolean             fPendingDisconnect;         // disconnect request has been deferred
161    TIrDisconnectRequest    *fDisconnectRequest;    // disconnect request event (for free check)
162
163    CBufferSegment *    fLastListenBuffer;      // temp debugging
164    CBufferSegment *    fLastPutBuffer;         // temp debugging
165};
166
167inline UInt32   CIrLSAP::GetState() { return fState; };
168inline void     CIrLSAP::SetState(UInt32 newState) {fState = newState;};
169inline UInt32   CIrLSAP::GetPeerLAPAddr() { return fPeerAddr; };
170inline UInt32   CIrLSAP::GetMyLSAPId() { return fMyLSAPId; };       // may not be valid yet
171
172#endif  // __CIRLSAP__
173
174
175
176