From 763576612d16b2044ca8199a111c947f6ff3b13a Mon Sep 17 00:00:00 2001 From: tao.dong Date: Sat, 11 Oct 2014 10:46:22 +0000 Subject: PD#98297:add looper to filter msg.Squashed commit of the following: commit 4ce337ff73a9f1f5295c2123499582ffca9594e6 Author: xy.ding Date: Fri Oct 10 20:09:09 2014 +0800 PD#98297: move isEthAdded, add isNetAdded func Change-Id: Id797cfa73f922467e88bb4cd401866d2f25e4712 commit 1ede323057829f73b982a70704b51e9aa440326f Author: xy.ding Date: Wed Oct 8 20:18:11 2014 +0800 PD#98297: add looper to filter msg Change-Id: I572640b22ff1d9cb320704312a378ee678a0c352 Change-Id: I37953f4c1198b94285a4378c0bf4ce423c8af328 --- diff --git a/jni/pppoe_jni.cpp b/jni/pppoe_jni.cpp index cdfe6c2..385c100 100755 --- a/jni/pppoe_jni.cpp +++ b/jni/pppoe_jni.cpp @@ -44,6 +44,7 @@ struct fields_t { static struct fields_t fields; +extern int get_eth0_updown(); extern int get_pppoe_status( const char *phy_if_name); static char pppoe_connect_cmd[PPPOE_CONNECT_CMD_LEN_MAX]; @@ -228,6 +229,21 @@ jboolean com_amlogic_PppoeOperation_terminate return 1; } +jint com_amlogic_PppoeOperation_isNetAdded +(JNIEnv *env, jobject obj, jstring jstr_if_name) +{ + char *p_ifname; + int status; + + __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG,"ppp.isNetAdded\n"); + + p_ifname = (char *)env->GetStringUTFChars(jstr_if_name, NULL); + status = get_net_updown(p_ifname); + + env->ReleaseStringUTFChars(jstr_if_name, p_ifname); + + return status; +} jint com_amlogic_PppoeOperation_status (JNIEnv *env, jobject obj, jstring jstr_if_name) @@ -248,9 +264,10 @@ jint com_amlogic_PppoeOperation_status static JNINativeMethod gPppoeJNIMethods[] = { /* name, signature, funcPtr */ - { "connect", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z", (void*) com_amlogic_PppoeOperation_connect }, - { "disconnect", "()Z", (void*) com_amlogic_PppoeOperation_disconnect }, - { "terminate", "()Z", (void*) com_amlogic_PppoeOperation_terminate }, + { "_connect", "(Ljava/lang/String;Ljava/lang/String;Ljava/lang/String;)Z", (void*) com_amlogic_PppoeOperation_connect }, + { "_disconnect", "()Z", (void*) com_amlogic_PppoeOperation_disconnect }, + { "_terminate", "()Z", (void*) com_amlogic_PppoeOperation_terminate }, + { "isNetAdded", "(Ljava/lang/String;)I", (void*) com_amlogic_PppoeOperation_isNetAdded }, { "status", "(Ljava/lang/String;)I", (void*) com_amlogic_PppoeOperation_status }, }; diff --git a/jni/src/pppoe_status.c b/jni/src/pppoe_status.c index f542921..6417d22 100755 --- a/jni/src/pppoe_status.c +++ b/jni/src/pppoe_status.c @@ -54,14 +54,35 @@ done: #define PPP_IF_NAME "ppp0" -int get_pppoe_status( const char *phy_if_name) +int get_net_updown(const char *phy_if_name) +{ + int ret; + + ret = if_is_up(phy_if_name); + if(ret < 0){ + if(ENODEV == -ret) + __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "No such device(%s)\n", phy_if_name); + return 0; + } + + if(0 == ret) { + __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "%s is DOWN\n", phy_if_name); + return 0; + } + else { + __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, "%s is UP\n", phy_if_name); + return 1; + } +} + +int get_pppoe_status(const char *phy_if_name) { int ret; ret = if_is_up(phy_if_name); if (ret < 0){ - if ( ENODEV == -ret ) - PRINTF( "No such device(%s)\n", phy_if_name ); + if (ENODEV == -ret) + PRINTF("No such device(%s)\n", phy_if_name); PRINTF("ppp_status: DISCONNECTED\n"); __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, @@ -70,8 +91,8 @@ int get_pppoe_status( const char *phy_if_name) return PPP_STATUS_DISCONNECTED; } - if (0 == ret ) { - PRINTF( "%s is DOWN\n", phy_if_name ); + if (0 == ret) { + PRINTF("%s is DOWN\n", phy_if_name); PRINTF("ppp_status: DISCONNECTED\n"); __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, @@ -81,8 +102,8 @@ int get_pppoe_status( const char *phy_if_name) ret = if_is_up(PPP_IF_NAME); if (ret < 0){ - if ( ENODEV == -ret ) - PRINTF( "No such device(%s)\n", PPP_IF_NAME ); + if (ENODEV == -ret) + PRINTF("No such device(%s)\n", PPP_IF_NAME); PRINTF("ppp_status: DISCONNECTED\n"); __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, @@ -90,7 +111,7 @@ int get_pppoe_status( const char *phy_if_name) return PPP_STATUS_DISCONNECTED; } - if (0 == ret ) { + if (0 == ret) { PRINTF("ppp_status: CONNECTING\n"); __android_log_print(ANDROID_LOG_ERROR, LOCAL_TAG, diff --git a/jni/src/pppoe_status.h b/jni/src/pppoe_status.h index 8762614..3ae2979 100755 --- a/jni/src/pppoe_status.h +++ b/jni/src/pppoe_status.h @@ -14,8 +14,8 @@ extern "C" { #endif -int get_pppoe_status( const char *ether_if_name); - +int get_net_updown(const char *phy_if_name); +int get_pppoe_status(const char *phy_if_name); #ifdef __cplusplus } diff --git a/src/com/amlogic/pppoe/LooperThread.java b/src/com/amlogic/pppoe/LooperThread.java new file mode 100755 index 0000000..af7fa3b --- a/dev/null +++ b/src/com/amlogic/pppoe/LooperThread.java @@ -0,0 +1,97 @@ +package com.amlogic.pppoe; + +import android.util.Log; +import android.os.Handler; +import android.os.Looper; +import android.os.Message; +import android.os.MessageQueue; + +import java.util.LinkedList; + +public class LooperThread extends Thread { + private static final String TAG = "LooperThread"; + private LinkedList mQueue = new LinkedList(); + private Impl mHandler = null; + private Looper mLooper = null; + + public LooperThread() { + setName("PPPOE.LooperThread"); + } + + public void run() { + mLooper = Looper.myLooper(); + Looper.prepare(); + mHandler = new Impl(); + Looper.loop(); + } + + public void post(RunnableImpl runnableimpl) { + if(runnableimpl == null) { + Log.w(TAG, "post runnableimpl is null!"); + return; + } + synchronized (mQueue) { + mQueue.add(runnableimpl); + if (mQueue.size() == 1) { + scheduleNextLocked(); + } + } + } + + public RunnableImpl getLast() { + synchronized (mQueue) { + if (mQueue.size() > 0) { + return mQueue.getLast(); + } + else { + return null; + } + } + } + + public void cancelRunnable(RunnableImpl runnableimpl) { + synchronized (mQueue) { + while (mQueue.remove(runnableimpl)) { } + } + } + + public void cancel() { + synchronized (mQueue) { + mQueue.clear(); + } + } + + private void scheduleNextLocked() { + if (mQueue.size() > 0) { + if(mHandler == null) { + Log.w(TAG, "scheduleNextLocked, mHandler is null!"); + return; + } + mHandler.sendEmptyMessage(1); + } + } + + public void stopThread(){ + if (null != mLooper){ + mLooper.quit(); + } + } + + private class Impl extends Handler { + public void handleMessage(Message msg) { + RunnableImpl r; + synchronized (mQueue) { + if (mQueue.size() == 0) { + Log.w(TAG, "mQueue is empty, don't do nothing!"); + return; + } + r = mQueue.removeFirst(); + } + if((r != null) && (r.mRunnable != null)) + r.mRunnable.run(); + synchronized (mQueue) { + scheduleNextLocked(); + } + } + } +} diff --git a/src/com/amlogic/pppoe/PppoeOperation.java b/src/com/amlogic/pppoe/PppoeOperation.java index 4c2cc30..d7be254 100755 --- a/src/com/amlogic/pppoe/PppoeOperation.java +++ b/src/com/amlogic/pppoe/PppoeOperation.java @@ -1,16 +1,106 @@ package com.amlogic.pppoe; + +import android.util.Log; public class PppoeOperation { - public static final int PPP_STATUS_CONNECTED = 0x10; - public static final int PPP_STATUS_DISCONNECTED = 0x20; - public static final int PPP_STATUS_CONNECTING = 0x40; + public static final String TAG = "PppoeOperation"; + public static final int PPP_STATUS_CONNECTED = 0x10; + public static final int PPP_STATUS_DISCONNECTED = 0x20; + public static final int PPP_STATUS_CONNECTING = 0x40; - public native boolean connect(String ifname, String account, String passwd); - public native boolean disconnect(); - public native boolean terminate(); - public native int status(String ifname); - static { + private native boolean _connect(String ifname, String account, String passwd); + private native boolean _disconnect(); + private native boolean _terminate(); + public native int isNetAdded(String ifname); + public native int status(String ifname); + private static LooperThread mLooper = null; + static { System.loadLibrary("pppoejni"); - } + Log.d(TAG, "LooperThread init!"); + mLooper = new LooperThread(); + mLooper.start(); + } + + public PppoeOperation() { + Log.d(TAG, "PppoeOperation init!"); + } + + public boolean connect(String ifname, String account, String passwd) { + String func = "connect"; + Log.d(TAG, func + ", ifname: " + ifname + " account: " + account + " passwd: " + passwd); + + if((ifname == null) || (isNetAdded(ifname) == 0)) { + Log.w(TAG, "The " + ifname + " is down or ifname is null, don't do connect!"); + return false; + } + RunnableImpl mLast = mLooper.getLast(); + if((mLast != null) && func.equals(mLast.mName)) { + Log.w(TAG, "the last action is connect, don't do again!"); + return false; + } + final String Ifname = ifname; + final String Account = account; + final String Passwd = passwd; + RunnableImpl mCur = new RunnableImpl(); + mCur.mName = func; + mCur.mRunnable = new Runnable() { + public void run() { + _connect(Ifname, Account, Passwd); + } + }; + mLooper.post(mCur); + return true; + } + + public boolean disconnect() { + String func = "disconnect"; + + Log.d(TAG, func); + RunnableImpl mLast = mLooper.getLast(); + if((mLast != null) && func.equals(mLast.mName)) { + Log.w(TAG, "the last action is disconnect, don't do again!"); + return false; + } + RunnableImpl mCur = new RunnableImpl(); + mCur.mName = func; + mCur.mRunnable = new Runnable() { + public void run() { + _disconnect(); + } + }; + mLooper.post(mCur); + return true; + } + + public boolean terminate() { + String func = "terminate"; + String connect = "connect"; + + Log.d(TAG, func); + if((isNetAdded("eth0") == 0) && (isNetAdded("wlan0") == 0) && (isNetAdded("usbnet0") == 0)) { + Log.w(TAG, "The eht0/wlan0/usbnet0 is down, don't do terminate!"); + return false; + } + RunnableImpl mLast = mLooper.getLast(); + if(mLast != null) { + if(func.equals(mLast.mName)) { + Log.w(TAG, "the last action is terminate, don't do again!"); + return false; + } + else if(connect.equals(mLast.mName)) { + Log.w(TAG, "the last action is connect, don't do terminate!"); + return false; + } + } + RunnableImpl mCur = new RunnableImpl(); + mCur.mName = func; + mCur.mRunnable = new Runnable() { + public void run() { + _terminate(); + } + }; + mLooper.post(mCur); + return true; + } } diff --git a/src/com/amlogic/pppoe/RunnableImpl.java b/src/com/amlogic/pppoe/RunnableImpl.java new file mode 100755 index 0000000..37f9545 --- a/dev/null +++ b/src/com/amlogic/pppoe/RunnableImpl.java @@ -0,0 +1,8 @@ +package com.amlogic.pppoe; + +public class RunnableImpl { + public String mName; + public Runnable mRunnable; + + public RunnableImpl() {}; +} -- cgit