|
1 /* |
|
2 * Copyright (c) 2008 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: ?Description |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #ifndef NATIVECOMMCONNECTION_H |
|
20 #define NATIVECOMMCONNECTION_H |
|
21 |
|
22 #include <stdlib.h> |
|
23 #include <string> |
|
24 #include <map> |
|
25 #include <stdio.h> |
|
26 #include <c32comm.h> |
|
27 |
|
28 #include "nativestreambase.h" |
|
29 #include "functionserver.h" |
|
30 |
|
31 |
|
32 typedef std::pair<std::string, std::string> TPair; |
|
33 |
|
34 namespace java |
|
35 { |
|
36 |
|
37 namespace comm |
|
38 { |
|
39 |
|
40 class NativeCommConnection: public NativeStreamBase |
|
41 { |
|
42 |
|
43 public: |
|
44 |
|
45 /** |
|
46 * Constructor |
|
47 */ |
|
48 NativeCommConnection(java::util::FunctionServer *aFuncServ); |
|
49 |
|
50 /** |
|
51 * Destructor |
|
52 */ |
|
53 ~NativeCommConnection(); |
|
54 |
|
55 /** |
|
56 * openCommPortConnection is called by CJavaCommConnection. |
|
57 * Refer to the CJavaCommConnection::openCommPortConnection() for more details. |
|
58 */ |
|
59 int openCommPortConnection(const std::string aJavaPortName, |
|
60 int aPortNumber, int *aNumericOptions); |
|
61 |
|
62 /** |
|
63 * writeBytes is called by StreamConnectionBase when the output stream |
|
64 * buffer in java overflows |
|
65 * It is also called if flush() is called at the Java side. |
|
66 * @param[in] aJavaBuffer: A byte array of length aLength |
|
67 * @return the number of bytes written. If error, then the |
|
68 * value of errno should be negated and returned. (return -errno) |
|
69 */ |
|
70 int writeBytes(JNIEnv& aJni, jbyteArray aJavaBuffer, int aOffset, |
|
71 int aLength); |
|
72 |
|
73 /** |
|
74 * ReadBytes is called by StreamConnectionBase when the buffer at |
|
75 * input stream buffer runs out of data. |
|
76 * @param[in] aJavaBuffer: Byte array to which the data read is copied into |
|
77 * @return the number of bytes read. If error, then the |
|
78 * value of errno should be negated and returned. (return -errno) |
|
79 */ |
|
80 int readBytes(JNIEnv& aJni, jbyteArray aJavaBuffer); |
|
81 |
|
82 /** |
|
83 * StopReading is called by StreamConnectionBase once InputStream is closed |
|
84 * from the Application. |
|
85 */ |
|
86 void stopReading(); |
|
87 |
|
88 /** |
|
89 * StopWriting is called by StreamConnectionBase OutputStream is closed |
|
90 * from the Application. |
|
91 */ |
|
92 |
|
93 void stopWriting(); |
|
94 |
|
95 /** |
|
96 * getBaudRate is called by CJavaCommConnection. |
|
97 * Refer to the CJavaCommConnection::getBaudRate() for more details. |
|
98 */ |
|
99 int getBaudRate(); |
|
100 |
|
101 /** |
|
102 * setBaudRate is called by CJavaCommConnection. |
|
103 * Refer to the CJavaCommConnection::setBaudRate() for more details. |
|
104 */ |
|
105 |
|
106 int setBaudRate(const int); |
|
107 |
|
108 /** |
|
109 * close is called by CJavaCommConnection class to close the connection to |
|
110 * the serial port. |
|
111 */ |
|
112 void close(); |
|
113 |
|
114 int doWrite(int aNativeCommConnectionHandle, int aBufferHandle); |
|
115 |
|
116 int doRead(JNIEnv& aJni, jbyteArray aJavaBuffer); |
|
117 |
|
118 void doOpenConnnection(int portNameHandle); |
|
119 |
|
120 int openPortConnection(std::string aJavaPortName, int aPortNumber, |
|
121 int *aNumericOptions); |
|
122 |
|
123 int doSetBaudRate(const int aBaudRate); |
|
124 |
|
125 int doGetBaudRate(); |
|
126 |
|
127 void doClose(); |
|
128 |
|
129 int available(); |
|
130 |
|
131 int doAvailable(); |
|
132 |
|
133 void doStopWriting(); |
|
134 |
|
135 void doStopReading(); |
|
136 |
|
137 private: |
|
138 |
|
139 /** |
|
140 * TBpsToTInt is internal function called by getBaudRate() function. |
|
141 * In Symbian, the speed(baudrate) of any logical port is specfied using |
|
142 * TBps datatype. The java api should return the integer value to the |
|
143 * application. This function is used to convert TBps to integer. |
|
144 * @param aBaudRate: The platform specific baud rate unit to be converted. |
|
145 * @return the speed of the port as integer value. |
|
146 */ |
|
147 |
|
148 int TBpsToTInt(TBps aRate); |
|
149 |
|
150 /** |
|
151 * TIntToTBps is internal function called by setBaudRate() function. |
|
152 * In Symbian, the speed(baudrate) of any logical port is specfied using |
|
153 * TBps datatype. The java setBaudRate() api provides the speed as an |
|
154 * integer. This function is used to convert integer to TBps. |
|
155 * @param aRate: The int value of speed to be converted. |
|
156 * @return the speed of the port as TBps unit. |
|
157 */ |
|
158 TBps TIntToTBps(const TInt aRate); |
|
159 |
|
160 /** |
|
161 * initializePortSettings is an internal function called by |
|
162 * openCommPortConnection() function. |
|
163 * This function initializes the port with all the options specified by the |
|
164 * Connector.open URI. |
|
165 * @param aNumericOptions: A array of options to be set for this serial |
|
166 * port which is opened. |
|
167 * aNumericOptions[0]: value of the option "baudrate" |
|
168 * aNumericOptions[1]: value of the option "bitsperchar" |
|
169 * aNumericOptions[2]: value of the option "stopbits" |
|
170 * aStringOptions[3]: value of the option "parity" |
|
171 * aStringOptions[4]: value of the option "blocking" |
|
172 * aStringOptions[5]: value of the option "autocts" |
|
173 * aStringOptions[6]: value of the option "autorts" |
|
174 */ |
|
175 void initializePortSettings(int *aNumericOptions); |
|
176 |
|
177 /** |
|
178 * connectToCommServer is an internal function called by |
|
179 * openCommPortConnection() to get the RCommServer object. |
|
180 * @return 0 on success or error value. |
|
181 */ |
|
182 int connectToCommServer(); |
|
183 |
|
184 /** |
|
185 * mapToSymbianPortName is an internal function called by |
|
186 * openCommPortConnection() to get the symbian platform |
|
187 * specific port. |
|
188 * @return mapped port string |
|
189 * |
|
190 */ |
|
191 std::basic_string<char> |
|
192 mapToSymbianPortName(const std::string aJavaPortName); |
|
193 |
|
194 private: |
|
195 // hash map of valid port names |
|
196 std::map<std::string, std::string> mPortNames; |
|
197 // port name |
|
198 std::basic_string<char> mNativePortName; |
|
199 RCommServ mCommServer; |
|
200 bool mConnectionIsOpen; |
|
201 RComm mComm; |
|
202 TCommConfigV01 mCommConfig; |
|
203 TInt mPortLowIndex; |
|
204 HBufC8* mInternalBuf; |
|
205 int mInternalBuflength; |
|
206 int mReadInProgress; |
|
207 int mWriteInProgress; |
|
208 public: |
|
209 java::util::FunctionServer *iFuncServ; |
|
210 |
|
211 }; |
|
212 |
|
213 } // end comm namespace |
|
214 |
|
215 } // java namespace |
|
216 |
|
217 #endif // NATIVECOMMCONNECTION_H |