1/*
2    File:       ttp.h
3
4    Contains:   Tiny TP
5
6*/
7
8#ifndef _TTP_
9#define _TTP_
10
11#include "CIrLSAP.h"
12#include "IrQOS.h"
13
14class TIrGlue;
15
16//********** Things probably still need fixing!!
17#define kMaxTTPRequests 50      // size of TTP event request pool
18typedef UInt32 TTPSAP;      // TinyTP Service Access Point (lsap id)
19#define TTPBuf CBufferSegment   // this might change, hide CBufferSegment
20#define BufUsed(x) (x->Position())      // mark - base
21#define BufSize(x) (x->GetSize())       // logical size of buffer (end-base)
22#define BufBase(x) (x->GetBufferPtr())
23#define BufHideStart(x, n) (x->Hide(n, kPosBeg))
24#define BufHideEnd(x, n) (x->Hide(n, kPosEnd))
25#define BufSeekStart(x) (x->Seek(0, kPosBeg))
26#define BufGet(x)  (x->Get())
27#define BufPut(x, y) (x->Put(y))
28#define BufAlloc(x) (CBufferSegment::New(x))
29#define BufFree(x)  (x->Delete())
30//************************************************************
31
32enum FlowOnOff { FlowOn, FlowOff};
33typedef FlowOnOff TFlowOnOff;
34enum TTP_Read_Status {TTP_Data_Ok, TTP_Data_Truncated};
35typedef TTP_Read_Status TReadStatus;
36
37
38//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
39//  these don't belong here ... move 'em out!
40struct SDU {
41    TTPBuf *sarbuf;         // the current reassembly buffer
42    Boolean busy;           // controls consumption of rcv queue.
43};
44
45enum TTPMsg {               // actions for stuff on my queue
46    TTP_Disconnect=10,      // Disconnect action
47    TTP_Segment=11,         // first/middle data segment    (M=1)
48    TTP_Segment_Last=12};   // first/last data segment      (M=0)
49
50struct TTPq {               // silly TTP queue structure, match QElem pls
51    TTPq        *qLink;     // link to next
52    short       qType;      // TTPMsg
53    short       reason;     // usually reason, could be anything
54    TTPBuf      *buf;       // the buffer (or zero if none)
55};
56typedef TTPq TTPq;
57//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
58
59// From OSUtils.h
60struct QElem {
61    struct QElem *                  qLink;
62    short                           qType;
63    short                           qData[1];
64};
65typedef struct QElem QElem;
66
67typedef QElem * QElemPtr;
68struct QHdr {
69	short           qFlags;
70	QElemPtr        qHead;
71	QElemPtr        qTail;
72};
73typedef struct QHdr QHdr;
74
75typedef QHdr *QHdrPtr;
76//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**//**
77
78class TTinyTP  : public CIrLSAP // hook up to IrGlue/IrStream/IrEtAl
79{
80    OSDeclareAbstractStructors(TTinyTP);
81
82public:
83    void        free();
84
85
86    Boolean TTPInitialize(              // allocate a bunch-o-buffers and initialize
87	TIrGlue *irda,                  // irda glue
88	UInt32 credit,                  // initial advertised credit
89	UInt32 desiredLSAPId,           // use kAssignDynamicLSAPId if notlisten
90	UInt8 * className,              // client's LSAP name to register w/IAS
91	ULong   hints = 0);             // hint bits (if any) to set
92
93					//******** TTP Client-callable primitives
94
95    void    DoDiscoverRequest (             // look for peer
96	    int     slots);                 // use 8
97
98    void    DoLookupRequest (               // do an IAS lookup
99	    unsigned char   *classname,     // name to look for
100	    UInt32          remoteAddr );   // device to query
101
102    void    DoListenRequest(                // new --- hang a listen
103	    TTPBuf  *userdata);
104
105    void    DoConnectRequest (              // connect to a remote peer
106	    UInt32  remoteAddr,             // peer address
107	    TTPSAP  SAP,                    // remote LSAP id
108	    TIrQOS *QoS,                    // requested QoS (unimpld)
109	    int     MaxSduSize,             // max reassembly space
110	    TTPBuf  *UserData);             // user-level connect payload (max 50 bytes or so)
111
112    //void  DoConnectRequest (              // connect to a remote peer
113    //      TTPSAP  SAP,                    // remote LSAP id
114    //      TIrQOS *QoS,                    // requested QoS (unimpld)
115    //      int     MaxSduSize,             // max reassembly space
116    //      TTPBuf  *UserData);             // user-level connect payload (max 50 bytes or so)
117
118    void    DoConnectResponse (             // reply to a connect indication (i.e. accept call)
119	    TTPSAP  SAP,                    // calling TTP SAP
120	    int     MaxSduSize,             // called MaxSduSize
121	    TTPBuf  *UserData);             // called UserData
122
123    void    DoDisconnectRequest (           // hangup
124	    TTPBuf  *UserData);             // optional userdata (unimpl'd)
125
126    void    DoDataRequest (                 // put
127	    TTPBuf  *UserData);             // data to send
128
129    void    DoUdataRequest (                // udata put
130	    TTPBuf  *UserData);             // data to send
131
132    void    SetLocalFlow (                  // flow control
133	    TFlowOnOff      onOff);         // start/stop flow
134
135    int     TTPXmitQueueSize(int maxPacketSize);    // Flow control - number of xmits we'll currently accept
136    void    TTPRxDone();                    // Flow control - let TTP know client is done w/buffer
137    void    TTPDiscardPendingPuts(void);    // discard any pending puts (zombie)
138
139    UInt32  GetPeerAddr(void);              // Get peer address and SAP
140    UInt32  GetPeerSAP(void);
141
142					    //*******
143					    //******* TTP Client supplied virtual callbacks
144					    //*******
145
146    virtual void TTPDiscoverComplete (      // Discover has completed
147	int     numFound,                   // number of peers discovered
148	IrDAErr result      ) = 0;          // result of discovery
149
150    virtual void TTPLookupComplete (        // Lookup completed
151	IrDAErr result,
152	UInt32  peerLSAPId) = 0;
153
154    virtual void TTPConnectIndication (     // Listen complete
155	IrDAErr result,
156	TTPSAP  SAP,                        // calling TTP SAP
157	TIrQOS *ourQOS,                     // our QoS (post negotiation)
158	TIrQOS *peerQOS,                    // peer QoS (post negotiation)
159	int     MaxSduSize,                 // calling MaxSduSize
160	TTPBuf  *UserData) = 0;             // calling UserData
161
162    virtual void TTPConnectConfirm (        // Connect complete
163	TTPSAP  SAP,                        // called TTP SAP
164	TIrQOS *ourQOS,                     // our QoS (post negotiation)
165	TIrQOS *peerQOS,                    // peer QoS (post negotiation)
166	int     MaxSduSize,                 // called MaxSduSize
167	TTPBuf  *UserData) = 0;             // called UserData
168
169    virtual void TTPDisconnectIndication (  // Disconnect complete
170	int     reason,                     // passed up from IrLMP (not)
171	TTPBuf  *UserData) = 0;
172
173    virtual void TTPDataIndication (        // Read complete
174	TTPBuf  *UserData,                  // data read
175	TTP_Read_Status status) = 0;        // Ok or Truncated
176
177    virtual void TTPUDataIndication (       // UData Read complete (unimpld)
178	TTPBuf  *UserData) = 0;                 // data read
179
180    virtual void TTPAcceptDoneIndication(   // accept done (we're really open now)
181	IrDAErr result) = 0;                    // just result code, buffer copied during accept call
182
183    virtual void TTPBackEnable(void) = 0;       // Called when more TTP buffers available
184
185
186private:                        // TinyTP Internal interfaces
187
188				// These are named funny so they closely match the TinyTP spec
189    int AvailCredit;            // credit available to advance to peer TTP entity
190    int RemoteCredit;           // credit held by peer TTP entity
191    int SendCredit;             // credit held by local TTP entity
192    Boolean Connected;          // reflects state of LM-MUX connection
193    QHdr AvailQueue;            // Queue of available TTPq elements (ahem)
194    QHdr TxQueue;               // FIFO queue for TTP_Segments and TTP_Disconnect requests
195    QHdr RxQueue;               // FIFO queue for inbound TTP_Segments and TTP_Disconnect requests
196    int MaxSegSize;             // Max size of segment in an outbound TTP-PDU
197    int TxMaxSduSize;           // Received from peer
198    int RxMaxSduSize;           // Sent to peer.  Used to police size of inbound SDUs
199    struct SDU RxSdu;           // The current incoming SDU
200    Boolean discoverPending;    // true if have a discover request pending already
201    int txQDepth;                   // jdg, keep count of entries on tx queue (avoid race condition)
202    TTPq ttpqpool[kMaxTTPRequests]; // memory pool for ttq
203    //Boolean putPending;           // jdg test: let a put/put complete finish before new put (needed?)
204    //TTinyTP *nextTTP;             // testing: keep a list of the ttp's
205    //mblk_t        *fPendingGet;   // allocated msg for pending reads
206    /////////////////////////////////////////////////////////////////////////////////////
207					// Support data and methods
208    UInt32  fPeerAddr;                  // address of our peer
209    UInt32  fPeerSAP;                   // lsap id of our peer
210
211    int initial_credit;                 // number of packet buffers to alloc at init time
212    void AppendTail(QHdr *, TTPMsg, int, TTPBuf *); // add event to end of my list
213    void FlushQueue(QHdr *);                        // remove events from my queue
214    TTPBuf *GetSegment(int, TTPBuf *);              // extract segment
215    void CheckTheQueues();              // do pending work
216    Boolean CheckTxQueue();
217    Boolean CheckRxQueue();
218    void Reassemble(TTPBuf *dest, TTPBuf *src); // append TTPBuf to SAR buffer
219    void SendDataless();                // send off a dataless data PDU
220    ////////////////////////////////////////////////////////////////////////////////////
221
222
223
224					//*****
225					// TTP is supplying these virtual functions to CIrLSAP
226					//*****
227    void DiscoverComplete (                 // discovery has finished callback
228		UInt32  numFound,           // number of peers discovered
229		IrDAErr result );           // result of discovery
230
231    void LSAPLookupComplete (           // an IAS query has finished
232		IrDAErr result,             // result of the lookup
233		UInt32  peerLSAPId);        // peer's LSAP id of the service
234
235    void ConnectComplete (              // a connect request has completed
236		IrDAErr result,             // result of the connect request
237		TIrQOS *myQOS,              // my qos ... (requested?)
238		TIrQOS *peerQOS,            // peer's qos .. (result?)
239		CBufferSegment *data);      // data payload from connect msg
240
241    void DisconnectComplete (void );        // you've been disconnected
242
243    void DataPutComplete (
244		IrDAErr result,             // result code
245		CBufferSegment *data);      // data that was sent
246
247    void DataGetComplete (
248		IrDAErr result,             // result code
249		CBufferSegment *data);      // data
250
251    void ListenComplete (                   // check me
252		IrDAErr result,
253		UInt32  peerAddr,           // address of connecting peer
254		UInt32  peerLSAPId,         // LSAP id of connecting peer
255		TIrQOS  *myQOS,             // my qos ... (requested?)
256		TIrQOS  *peerQOS,           // peer's qos .. (result?)
257		CBufferSegment *data);      // data payload from connect msg
258
259    void AcceptComplete (                   // check me
260		IrDAErr result,
261		CBufferSegment *data);      // data payload in connect msg
262
263    void CancelGetsComplete (               // all pending gets have been canceled
264		    IrDAErr result);
265
266    void CancelPutsComplete (               // all pending puts have been canceled
267		    IrDAErr result);
268
269
270typedef TTPSAP  LMSAP;                      // just a little leftover callback glue
271    void TTPHandleConnectIndication (       // between the above LSAP callbacks and TTP
272	IrDAErr result,
273	LMSAP   sap,
274	TIrQOS  *ourQOS,                    // our QoS
275	TIrQOS  *peerQOS,                   // peer QoS
276	TTPBuf  *UserData);
277
278    void TTPHandleConnectConfirm (
279	LMSAP   sap,
280	TIrQOS  *ourQOS,                    // our QoS
281	TIrQOS  *peerQOS,                   // peer QoS
282	TTPBuf  *UserData);
283
284    void TTPHandleDisconnectIndication (
285	int     reason,
286	TTPBuf  *UserData);
287
288    void TTPHandleDataIndication (
289	TTPBuf  *userData);
290
291    void TTPHandleUDataIndication (
292	TTPBuf  *userData);
293
294    void TTPHandleAcceptComplete (
295	IrDAErr result,
296	TTPBuf  *userdata);                 // buffer send in with accept call
297
298};  // class TTinyTP
299
300// Inlines
301inline UInt32 TTinyTP::GetPeerAddr() { return fPeerAddr; };
302inline UInt32 TTinyTP::GetPeerSAP() { return fPeerSAP; };
303
304#endif  // _TTP_
305
306
307
308
309