Android Network-surfing Process

Source: Internet
Author: User

Network applications are mainly used for searching, Browsing webpages, sending emails, and sending MMS. For Android, some intermediate processes involved in these applications mainly involve WebKit, WAP, SMTP and other protocols, socket communication, TCPIP protocol stack of Linux kernel, and PPP protocol, the device interfaces such as ttys0 are followed by the modem to send data. In addition, the network interface of the mobile phone may not be a modem, but may be a network card or WiFi, so the corresponding interface can be added in Android.

However, wired NICs are rarely used. After all, large network ports are rarely used on mobile phones or tablets.

 

First, let's take a look at the network process.

The modem format is discussed here.

 

Application> trigger network connection (or connected)> Android local JNI socket function> BSD socket in kernel? -> TCP/IP> PPP>/dev/ttysx (modem data port ).

In the triggered network connection, if there is no connection, the dial will be performed. There are some initialization AT commands and a dialing command, ATD * 99 *** 1 #, these implementations are RIL. java and corresponding RIL. CPP file. After successful dialing, the PPP negotiation process will be conducted. After successful PPP negotiation, the mobile network will assign an IP address, Gateway and DNS address to the terminal. Then the network connection is successful. Then, the upper-layer application data is sent.

 

In our applications, the system will check whether the network is connected when the Internet access needs to be triggered. Of course, multiple interfaces will be polling, check which connection is available (whether there should be a priority here, first wired, then WiFi, and finally MODEM. After all, the modem fee is the highest)

 

Before PPP dialing, All implementations are included in phoneservice, that is, under the frameworks/base/telephony/Java/COM/Android/Internal/telephony/directory,

After all, dialing is still in the phone category.

First, in phoneapp. Java: oncreate

Phonefactory. makedefaphphones (this); // generate a basic form of telephone service

In phonefactory. Java: public static void makedefaultphone (context)

Int phonetype = getphonetype (networkmode); If (phonetype = phone. phone_type_gsm) {sproxyphone = new phoneproxy (New gsmphone (context, scommandsinterface, sphonenotifier); log. I (log_tag, "creating gsmphone");} else if (phonetype = phone. phone_type_cdma) {sproxyphone = new phoneproxy (New cdmaphone (context, scommandsinterface, sphonenotifier); log. I (log_tag, "creating cdmaphone");} // create phonepro XY, then in phoneproxy, A gsmphone is created. If the network is CDMA, A cdmaphone is created. The CDMA network is not discussed here.

In the phone. Java, constructor,

mDataConnection = new GsmDataConnectionTracker (this);

In gsmdataconnectiontracker. Java, the gsmdataconnectiontracker class inherits from dataconnectiontracker. The createallpdplist () is called in the gsmdataconnectiontracker constructor; the function is in gsmdataconnectiontracker. java. As follows:

Private void createallpdplist () {pdplist = new arraylist <dataconnection> (); dataconnection PDP; For (INT I = 0; I <pdp_connection_pool_size; I ++) {PDP = new pdpconnection (mgsmphone); pdplist. add (PDP) ;}}// create pdp_connection_pool_size pdpconnection (pdp_connection_pool_size is equal to 1)

When an application triggers a network to send data, such as onapnchanged, onroamingoff, and onroamingon, or when processing a message, it calls the trysetupdata function.

For the trysetupdata function, setupdata (reason) is just called for data connection.

Private Boolean setupdata (string reason) {apnsetting APN; pdpconnection PDP; APN = getnextapn (); If (APN = NULL) return false; PDP = findfreepdp (); if (PDP = NULL) {If (DBG) log ("setupdata: No free pdpconnection found! "); Return false;} mactiveapn = APN; mactivepdp = PDP; message MSG = obtainmessage (); MSG. what = event_data_setup_complete; MSG. OBJ = reason; PDP. connect (APN, MSG); setstate (state. initing); phone. notifydataconnection (reason); Return true;} // PDP is called. connect (APN, MSG ). When dialing, it should be sent to the RIL. Java layer.

Next, let's look at the pdpconnection. Java file, which inherits from dataconnection

This file implements connect, disconnect, and other methods.

In connect, The setupdatacall Interface Method

 phone.mCM.setupDataCall(Integer.toString(RILConstants.SETUP_DATA_TECH_GSM),                Integer.toString(RILConstants.DATA_PROFILE_DEFAULT), apn.apn, apn.user,                apn.password, Integer.toString(authType),                obtainMessage(EVENT_SETUP_DATA_CONNECTION_DONE));

The MCM type is commandsinterface, which is a common interface for telephone services. The implementation of this interface is the RIL class. The following describes RIL. Java

RIL. Java completes the conversion between the android telephone service and modem operations. That is, some telephone services are converted to the implemented at command and sent to the modem

In the RIL class, the setupdatacall method is implemented as follows:

 public void        setupDataCall(String radioTechnology, String profile, String apn,                String user, String password, String authType, Message result) {            RILRequest rr                    = RILRequest.obtain(RIL_REQUEST_SETUP_DATA_CALL, result);             rr.mp.writeInt(6);             rr.mp.writeString(radioTechnology);            rr.mp.writeString(profile);            rr.mp.writeString(apn);            rr.mp.writeString(user);            rr.mp.writeString(password);            rr.mp.writeString(authType);             if (RILJ_LOGD) riljLog(rr.serialString() + "> "                    + requestToString(rr.mRequest) + " " + radioTechnology + " "                    + profile + " " + apn + " " + user + " "                    + password + " " + authType);             send(rr);        }

This is through the socket method to RIL. CPP sends the relevant request, and sends the ril_request_setup_data_call request to the reference. CPP sends the ATD * 99 *** 1 # command to the modem through the serial port. Finally, return the AT command to return the result. If the result is successful, connect OK is returned. Then RIL. cpp will send a successful response to RIL. java. In the cycle of the run function in RIL. Java, call the processresponse method to parse the response.

 processResponse()->processSolicited ()->case RIL_REQUEST_SETUP_DATA_CALL: ret =  responseStrings(p); break;

Send the returned results to the previous module through RR. mresult. sendtotarget.

In this case, the system will call the PPP dialing program. The source code of the PPP dialing program is in the external/PPP directory.

Before Android 1.6, the system encapsulated the code for calling pppd in frameworks/base/telephony/Java/COM/Android/Internal/telephony/ppplink. java.

However, we will not have it any more. The introduction on the Internet uses the Qualcomm solution to implement the communication between pppd and system processes through Memory Sharing, therefore, the Java code that calls pppd is removed from the Code.

How to share? This is a problem. But it should be about the system.

You need to manually dial pppd.

When do I receive dialing? This is the key. After all, GPRS has traffic. Everyone prefers On-Demand dialing, which is equivalent to smart dialing. PPP already provides this function, just add the corresponding parameters.

However, in the new Android version, the PPP code has been simplified. Without the chat. c file, you naturally cannot use the pppd call XXX command without the chat command. If you want to use chat, you need to transplant the PPP program yourself.

Related Article

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.