Source code: com/synchrona/jred/TinyTP.java
1 /*
2 **************************************************************************
3 ** $Header: /cvsroot/jred/jred/src/com/synchrona/jred/TinyTP.java,v 1.1.1.1 2000/07/05 04:41:52 mpatters Exp $
4 **
5 ** Copyright (C) 2000 Synchrona, Inc. All rights reserved.
6 **
7 ** This file is part of JRed, a 100% Java implementation of the IrDA
8 ** infrared communications protocols.
9 **
10 ** This file may be distributed under the terms of the Synchrona Public
11 ** License as defined by Synchrona, Inc. and appearing in the file
12 ** LICENSE included in the packaging of this file. The Synchrona Public
13 ** License is based on the Q Public License as defined by Troll Tech AS
14 ** of Norway; it differs only in its use of the courts of Florida, USA
15 ** rather than those of Oslo, Norway.
16 **************************************************************************
17 */
18 package com.synchrona.jred;
19
20 import com.synchrona.util.Log;
21 import com.synchrona.util.Utilities;
22 import java.io.PrintWriter;
23
24 /**
25 ***
26 **/
27 public class TinyTP implements iIrLMPService {
28 public final static int PDU_HAS_PARAMETERS = 0x00000080;
29 public final static int PDU_HAS_NO_PARAMETERS = 0x00000000;
30 public final static int PDU_INITIAL_CREDIT = 0x0000007F;
31
32 private Test m_irlmp;
33 private Log m_log;
34 private IrOBEX m_obex;
35 private byte m_yDestination;
36 private byte m_ySource;
37
38 public TinyTP(Log log, Test irlmp, IrOBEX obex) {
39 m_irlmp = irlmp;
40 m_log = log;
41 m_obex = obex;
42 }
43
44 public Test getIrLMP() {
45 return m_irlmp;
46 }
47
48 //-------------------------------------------------------------------
49 //
50 //-------------------------------------------------------------------
51
52 //-------------------------------------------------------------------
53 // Implement IrLMPService
54 //-------------------------------------------------------------------
55
56 public void clientConfirmsConnection(Client client) {
57 m_log.debug("TinyTP", "clientConfirmsConnection: " + client);
58 }
59
60 public void clientConfirmsDisconnect(Client client) {
61 m_log.debug("TinyTP", "clientConfirmsDisconnect: " + client);
62 }
63
64 public void clientDiscovered(Client client) {
65 m_log.debug("TinyTP", "clientDiscovered: " + client);
66 }
67
68 public void confirmConnectionRequest(Client client) {
69 boolean okayToConnect = false;
70 byte [] myServiceData = new byte[1];
71 m_log.debug("TinyTP", "respondToConnectionRequest: " + client);
72
73 byte [] serviceData = client.getServiceData();
74 if ( null == serviceData ) {
75 m_log.debug("TinyTP", "User data was null, TinyTP expects to see at least one byte.");
76 } else {
77 int hasParameters = (serviceData[0] & PDU_HAS_PARAMETERS);
78 if ( hasParameters != 0 ) {
79 m_log.error("TinyTP", "Parameter-Carrying Connect TTP-PDU's are not implemented yet.");
80 } else {
81 // It's our responsibility to give away Tx credits -- give away
82 // 127, the maximum. Also this user data means no support for
83 // SAR.
84 myServiceData[0]= (byte) (PDU_HAS_NO_PARAMETERS | PDU_INITIAL_CREDIT);
85 okayToConnect = true;
86 }
87 }
88
89 if ( okayToConnect ) {
90 client.confirmConnectionRequest(myServiceData);
91 } else {
92 client.denyConnectionRequest();
93 }
94 }
95
96 public void clientRequestsDisconnect(Client client) {
97 client.confirmDisconnectRequest();
98 }
99
100 public void clientSendsData(Client client, byte [] data, int offset, int length) {
101 }
102
103 //-------------------------------------------------------------------
104 // Implement iIrLMPService
105 //-------------------------------------------------------------------
106
107 public void connectRequest(Test irlmp, byte yDestinationLSAP, byte ySourceLSAP, byte [] ayUserData) {
108 m_log.debug("TinyTP", "(connectRequest) " + Utilities.bytesToString(ayUserData));
109
110 if ( null == ayUserData ) {
111 m_log.debug("TinyTP", "User data was null, TinyTP expects to see at least one byte.");
112 } else {
113 // Mask off the high bit of the user data to see if this client
114 // expects to set TinyTP parameters. We don't support them.
115 int nP = (ayUserData[0] & 0x80);
116 if ( nP != 0 ) {
117 m_log.debug("TinyTP", "Parameter-Carrying Connect TTP-PDU's are not implemented yet.");
118 }
119
120 // Mask off the lower 7 bits to get our initial transmit credits.
121 int nInitialCredit = (ayUserData[0] & 0x7F);
122 m_log.debug("TinyTP", "nInitialCredit: " + nInitialCredit);
123
124 // It's our responsibility to give away Tx credits -- give away
125 // 127, the maximum. Also this user data means no support for
126 // SAR.
127 byte [] ayMyUserData = new byte[1];
128 ayMyUserData[0] = (byte) 0x7F;
129
130 irlmp.connectConfirm(ySourceLSAP, yDestinationLSAP, ayMyUserData);
131 }
132 }
133
134 public void dataRequest(byte [] ayData, int nOffset, int nLength) {
135 m_log.debug("TinyTP", "(dataRequest)");
136 byte [] ayRequest = new byte[nLength + 1];
137 ayRequest[0] = 0x7F;
138 for (int i = 0; i < nLength; i++) {
139 ayRequest[i + 1] = ayData[nOffset + i];
140 }
141
142 m_irlmp.sendData(m_ySource, m_yDestination, ayRequest, 0, ayRequest.length);
143 }
144
145 public void serviceRequest(Test irlmp, IrLMPConnection conn, byte [] ayRequest, int nOffset, int nLength) throws Exception {
146 m_log.debug("TinyTP", "(serviceRequest) " + Utilities.bytesToString(ayRequest, nOffset, nLength));
147
148 m_irlmp = irlmp;
149 m_yDestination = (byte) (ayRequest[nOffset] & 0x7F);
150 m_ySource = (byte) (ayRequest[nOffset + 1] & 0x7F);
151
152 m_obex.serviceRequest(this, conn, ayRequest, nOffset + 3, nLength - 3);
153 }
154
155 public void setLog(PrintWriter log) {
156 }
157
158 public void setLogging(boolean bDoLogging) {
159 }
160 }