FileDocCategorySizeDatePackage
PppLink.javaAPI DocAndroid 1.5 API7176Wed May 06 22:42:02 BST 2009com.android.internal.telephony.gsm

PppLink

public final class PppLink extends DataLink implements DataLinkInterface
Represents a PPP link. Ideally this would be managed by the RIL implementation, but we currently have implementations where this is not the case. {@hide}

Fields Summary
private static final String
LOG_TAG
static final String
PATH_PPP_OPERSTATE
static final String
SERVICE_PPPD_GPRS
static final String
PROPERTY_PPPD_EXIT_CODE
static final int
POLL_SYSFS_MILLIS
static final int
EVENT_POLL_DATA_CONNECTION
static final int
EVENT_PPP_OPERSTATE_CHANGED
static final int
EVENT_PPP_PIDFILE_CHANGED
static final byte[]
UP_ASCII_STRING
static final byte[]
DOWN_ASCII_STRING
static final byte[]
UNKNOWN_ASCII_STRING
private final byte[]
mCheckPPPBuffer
int
lastPppdExitCode
Constructors Summary
PppLink(DataConnectionTracker dc)



      
        super(dc);
    
Methods Summary
private voidcheckPPP()

        boolean connecting = (dataConnection.state == State.CONNECTING);

        try {
            RandomAccessFile file = new RandomAccessFile(PATH_PPP_OPERSTATE, "r");
            file.read(mCheckPPPBuffer);
            file.close();

            // Unfortunately, we're currently seeing operstate
            // "unknown" where one might otherwise expect "up"
            if (ArrayUtils.equals(mCheckPPPBuffer, UP_ASCII_STRING, UP_ASCII_STRING.length)
                    || ArrayUtils.equals(mCheckPPPBuffer, UNKNOWN_ASCII_STRING,
                            UNKNOWN_ASCII_STRING.length) 
                            && dataConnection.state == State.CONNECTING) {

                Log.i(LOG_TAG, 
                "found ppp interface. Notifying GPRS connected");

                if (mLinkChangeRegistrant != null) {
                    mLinkChangeRegistrant.notifyResult(LinkState.LINK_UP);
                }

                connecting = false;
            } else if (dataConnection.state == State.CONNECTED 
                    && ArrayUtils.equals(mCheckPPPBuffer, DOWN_ASCII_STRING,
                            DOWN_ASCII_STRING.length)) {

                Log.i(LOG_TAG, 
                "ppp interface went down. Reconnecting...");

                if (mLinkChangeRegistrant != null) {
                    mLinkChangeRegistrant.notifyResult(LinkState.LINK_DOWN);
                }
            }                                    
        } catch (IOException ex) {
            if (! (ex instanceof FileNotFoundException)) {
                Log.i(LOG_TAG, "Poll ppp0 ex " + ex.toString());
            }

            if (dataConnection.state == State.CONNECTED &&
                    mLinkChangeRegistrant != null) {
                mLinkChangeRegistrant.notifyResult(LinkState.LINK_DOWN);
            }
        }

        // CONNECTING means pppd has started but negotiation is not complete
        // If we're still CONNECTING here, check to see if pppd has
        // already exited
        if (connecting) {
            String exitCode;

            exitCode = SystemProperties.get(PROPERTY_PPPD_EXIT_CODE, "");

            if (!exitCode.equals("")) {
                // pppd has exited. Let's figure out why
                lastPppdExitCode = Integer.parseInt(exitCode);

                Log.d(LOG_TAG,"pppd exited with " + exitCode);

                if (mLinkChangeRegistrant != null) {
                    mLinkChangeRegistrant.notifyResult(LinkState.LINK_EXITED);
                }
            }
        }

    
public voidconnect()

        // Clear any previous exit code
        SystemProperties.set(PROPERTY_PPPD_EXIT_CODE, "");
        SystemService.start(SERVICE_PPPD_GPRS);
        removeMessages(EVENT_POLL_DATA_CONNECTION);
        Message poll = obtainMessage();
        poll.what = EVENT_POLL_DATA_CONNECTION;
        sendMessageDelayed(poll, POLL_SYSFS_MILLIS);
    
public voiddisconnect()

        SystemService.stop(SERVICE_PPPD_GPRS);
    
public intgetLastLinkExitCode()

        return lastPppdExitCode;
    
public voidhandleMessage(android.os.Message msg)


        switch (msg.what) {

            case EVENT_POLL_DATA_CONNECTION:
                checkPPP();

                // keep polling in case interface goes down
                if (dataConnection.state != State.IDLE) {                    
                    Message poll = obtainMessage();
                    poll.what = EVENT_POLL_DATA_CONNECTION;
                    sendMessageDelayed(poll, POLL_SYSFS_MILLIS);
                }
                break;
        }
    
public voidsetPasswordInfo(android.database.Cursor cursor)

        StringBuilder builder = new StringBuilder();
        FileOutputStream output = null;

        try {
            output = new FileOutputStream("/etc/ppp/pap-secrets");
            if (cursor.moveToFirst()) {
                do {
                    builder.append(cursor.getString(cursor.getColumnIndex("user")));
                    builder.append(" ");
                    builder.append(cursor.getString(cursor.getColumnIndex("server")));
                    builder.append(" ");
                    builder.append(cursor.getString(cursor.getColumnIndex("password")));
                    builder.append("\n");
                } while (cursor.moveToNext());
            }

            output.write(builder.toString().getBytes());
        } catch (java.io.IOException e) {
            Log.e(LOG_TAG, "Could not create '/etc/ppp/pap-secrets'", e);
        } finally {
            try {
                if (output != null) output.close();
            } catch (java.io.IOException e) {
                Log.e(LOG_TAG, "Error closing '/etc/ppp/pap-secrets'", e);
            }
        }