GCF commconnection code analysis (detailed analysis)

Source: Internet
Author: User

In the previous article: GCF commconnection conector code analysis, the architecture of commconnection implementation is roughly analyzed. during the development process, you need to learn more details and encapsulate your own library based on this structure. Here, we will summarize this part of code.

 

1. Interface relationship diagram;

2. Serial implementation details; com. Sun. MIDP. Io. Comm. Protocol code analysis

3. com. Sun. MIDP. Io. bufferedconnectionadapter class code analysis

4. com. Sun. MIDP. Io. connectionbaseadapter class code analysis

5. connector class code analysis

6. Some questions and attempts

 

1. Interface Relationship Diagram

Serial Port operations involve the following interfaces:

Commconnection, streamconnection, inputconnection, outputconnection, connection

Commconnection: inherited from streamconnection; two methods are available: getbaudrate and setbaudrate;

Streamconnection: inherited from inputconnection and outputconnection; supports input and output. This is the feature of stream connection;

Inputconnection: inherited from connection; Method: openinputstream. You can use this method to obtain the stream input interface;

Outputconnection: inherited from connection; Method: openoutpuststream. You can use this method to obtain the stream output interface;

Connection: there is a method to close. Use this method to close the connection;

 

2. com. Sun. MIDP. Io. Comm. Protocol code analysis

Some local methods are called in this class:

Private Static native int native_openbynumber (INT port, int Baud, int flags) throws ioexception; <br/> Private Static native int native_openbyname (string name, int Baud, int flags) throws ioexception; <br/> Private Static native void native_configureport (INT port, int Baud, int flags) throws ioexception; <br/> Private Static native void native_close (INT hport) throws ioexception; <br/> private native void registercleanup (); <br/> Private Static native int native_readbytes (INT hport, byte B [], int off, int Len) throws ioexception; <br/> Private Static native int native_writebytes (INT hport, byte B [], int off, int Len) throws ioexception; <br/> private native void finalize (); 

Native_configureport supports changing the serial port attributes by setting the flag after enabling the serial port;

The hport parameter in the native_readbytes and native_writebytes methods is obtained through native_open;

There are two read operations: readbytesnonblocking and nonbufferedread: The write operation has one method: writebytes;

Read operation:

The difference between readbytesnonblocking and nonbufferedread is that the former is non-blocking read, and the latter can be blocking read. It should be noted that the blocking and non-blocking types are implemented in Java, the local function native_readbytes will not be blocked. In the nonbufferedread implementation, if native_readbytes returns 0 and the current operation is blocked, this method will be blocked;

Nonbufferedread is used to implement the available method. When you need to know how much data is readable in the serial port, call the available method. The available method reads data from the serial port into a fixed buffer zone through nonbufferedread, until no data is readable or the buffer is full; the length returned by available cannot exceed the size of the fixed buffer. In midp2.0, this value is 256 bytes and can be obtained through "com. sun. MIDP. io. j2s. comm. buffersize "configuration; available will also affect subsequent read operations, because the data is not actually read, and subsequent read must first be read from the buffer until the buffer becomes empty; for example, if available is used, the returned result is 100. The first read-only request contains 99 bytes, and the remaining 1 byte is returned. The second request reads 1000 bytes, even if the serial port contains 999 bytes, the second read can only get 1 byte; Available also has advantages, for example, when using is. after available obtains the length, it allocates a Buffer Based on the length, and then reads data into the buffer using read. At this time, you do not need to judge the return value of read;

Readbytesnonblocking is used to implement the read method. when the length of the data to be read exceeds the fixed buffer and the fixed buffer is empty, the data will be directly read to the passed buffer. Otherwise, first, read the data into the fixed buffer and then copy the data. Here is the details: If no data is readable, the return value of the local method is 0. When the attribute block of the serial port is true, it will be blocked. If the attribute block of the serial port is false, the read value will be returned as-1; this-1 is the converted value in connectionbaseadapter; if the returned value of the local method is-1, it indicates that an exception has occurred. This-1 is not the other-1;

Write operation:

Writebytes In The protocl class is a direct call to the local write method. The local method returns the written length. In the connectionbaseadapter, writebytes is called cyclically until all the data to be written is written; when writing a serial port, the serial port driver buffer is full, and the returned length of writebytes may be inconsistent with the input length.

Open the serial port:

It mainly resolves incoming parameters and calls local methods;

Close the serial port:

Call the local method;

Set the serial port baud rate:

Call the native_configport method;

 

3.com. Sun. MIDP. Io. bufferedconnectionadapter class Parsing

It mainly refers to the operation and encapsulation of fixed buffer during read operations, and the implementation of available and readbytes methods;

 

4. parse com. Sun. MIDP. Io. connectionbaseadapter class

The connectionbaseinterface interface is implemented, and the connection class is returned. The baseinputstream and baseoutputstream private classes are implemented internally;This separation of connection and input/output is the essence of the GCF architecture;

The read/write and close operations of baseinputstream and baseoutputstream must be implemented through the parent class:

Public int read (byte B [], int off, int Len) throws ioexception {<br/> int test; <br/> ensureopen (); <br/> If (LEN = 0) {<br/> return 0; <br/>}< br/>/* <br/> * test the parameters so the subclass will not have. <br/> * This will avoid crashes in the native code <br/> */<br/> test = B [off] + B [Len-1] + B [Off + len-1]; <br/> return parent. readbytes (B, off, Len); <br/>} 

Public void close () throws ioexception {<br/> If (parent! = NULL) {<br/> parent. closeinputstream (); <br/> parent = NULL; <br/>}< br/>} 

The connectionbaseadapter mainly checks the parameters and status and controls permissions, as well as the loop write operations mentioned above;

It should be noted that the flush method has nothing to do;

Connectionbaseadapter calls its sub-classes bufferedconnectionadapter and protocol by defining some abstract methods;

 

 

5. connector class code analysis

Connector. what open does is parse the input parameters, find the corresponding protocol, and find the corresponding protocol processing class at a fixed position. For example, the serial port is com. sun. MIDP. io. tckcomm. protocol; on the simulator, the location is com. sun. kvem. io. comm. protocol, and finally call the connectionbaseinterface method openprim transmission parameter;

 

6. Some questions and attempts:

Interruptedioexception explanation: Io operations are interrupted because Io is disabled during operations;

Connectionbaseadapter and other classes are all implemented in MIDP. Can I operate on it directly?

Yes. The test code is as follows:

String STR = system. getproperty ("javax. microedition. io. connector. protocolpath "); <br/> class clazz = <br/> class. forname (STR + <br/> ". "+" corner stone "+ <br/> ". "+" Comm "+ ". protocol "); </P> <p>/* construct a new instance of the Protocol */<br/> connectionbaseinterface UC = <br/> (connectionbaseinterface) clazz. newinstance (); </P> <p> commconnection cc = (commconnection) UC. openprim ("com3", 3, true); <br/> datainputstream is = cc. opendatainputstream (); <br/> int iret = is. read (); 

However, this does not make sense. Because the returned values of openprim have been specified as connection and commconnection interfaces, I can only use this interface for operations, and connection is defined in cldc; commconnection is defined in MIDP; it can be seen that the structure design of MIDP and cldc is reasonable;

 

Note: The connector class is defined in javax. microedition. Io and belongs to cldc;

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.