Skip to content

Commit

Permalink
Esptouch v0.3.4.2 only support Espressif's Smart Config v2.4
Browse files Browse the repository at this point in the history
  • Loading branch information
afunx committed Aug 14, 2015
1 parent d652ce3 commit cde76ef
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 31 deletions.
27 changes: 17 additions & 10 deletions src/com/espressif/iot/esptouch/EsptouchTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
import com.espressif.iot.esptouch.task.IEsptouchTaskParameter;
import com.espressif.iot.esptouch.task.__EsptouchTask;


public class EsptouchTask implements IEsptouchTask {

public __EsptouchTask _mEsptouchTask;
private IEsptouchTaskParameter _mParameter;

/**
* Constructor of EsptouchTask
*
Expand All @@ -28,11 +27,13 @@ public class EsptouchTask implements IEsptouchTask {
* @param context
* the Context of the Application
*/
public EsptouchTask(String apSsid, String apBssid, String apPassword,boolean isSsidHidden, Context context) {
public EsptouchTask(String apSsid, String apBssid, String apPassword,
boolean isSsidHidden, Context context) {
_mParameter = new EsptouchTaskParameter();
_mEsptouchTask = new __EsptouchTask(apSsid, apBssid, apPassword, context, _mParameter, isSsidHidden);
_mEsptouchTask = new __EsptouchTask(apSsid, apBssid, apPassword,
context, _mParameter, isSsidHidden);
}

/**
* Constructor of EsptouchTask
*
Expand All @@ -44,18 +45,19 @@ public EsptouchTask(String apSsid, String apBssid, String apPassword,boolean isS
* the Ap's password
* @param isSsidHidden
* whether the Ap's ssid is hidden
* @param timeoutMillisecond(it should be >= 10000+8000)
* millisecond of total timeout
* @param timeoutMillisecond
* (it should be >= 15000+6000) millisecond of total timeout
* @param context
* the Context of the Application
*/
public EsptouchTask(String apSsid, String apBssid, String apPassword,boolean isSsidHidden,int timeoutMillisecond, Context context) {
public EsptouchTask(String apSsid, String apBssid, String apPassword,
boolean isSsidHidden, int timeoutMillisecond, Context context) {
_mParameter = new EsptouchTaskParameter();
_mParameter.setWaitUdpTotalMillisecond(timeoutMillisecond);
_mEsptouchTask = new __EsptouchTask(apSsid, apBssid, apPassword, context, _mParameter, isSsidHidden);
_mEsptouchTask = new __EsptouchTask(apSsid, apBssid, apPassword,
context, _mParameter, isSsidHidden);
}


@Override
public void interrupt() {
_mEsptouchTask.interrupt();
Expand All @@ -79,4 +81,9 @@ public List<IEsptouchResult> executeForResults(int expectTaskResultCount)
}
return _mEsptouchTask.executeForResults(expectTaskResultCount);
}

@Override
public void setEsptouchListener(IEsptouchListener esptouchListener) {
_mEsptouchTask.setEsptouchListener(esptouchListener);
}
}
12 changes: 12 additions & 0 deletions src/com/espressif/iot/esptouch/IEsptouchListener.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.espressif.iot.esptouch;

public interface IEsptouchListener {
/**
* when new esptouch result is added, the listener will call
* onEsptouchResultAdded callback
*
* @param result
* the Esptouch result
*/
void onEsptouchResultAdded(IEsptouchResult result);
}
10 changes: 8 additions & 2 deletions src/com/espressif/iot/esptouch/IEsptouchTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@

public interface IEsptouchTask {

/**
* set the esptouch listener, when one device is connected to the Ap, it will be called back
* @param esptouchListener when one device is connected to the Ap, it will be called back
*/
void setEsptouchListener(IEsptouchListener esptouchListener);

/**
* Interrupt the Esptouch Task when User tap back or close the Application.
*/
Expand All @@ -13,7 +19,7 @@ public interface IEsptouchTask {
* Note: !!!Don't call the task at UI Main Thread or RuntimeException will
* be thrown Execute the Esptouch Task and return the result
*
* Smart Config v2.2 support the API
* Smart Config v2.4 support the API
*
* @return the IEsptouchResult
* @throws RuntimeException
Expand All @@ -24,7 +30,7 @@ public interface IEsptouchTask {
* Note: !!!Don't call the task at UI Main Thread or RuntimeException will
* be thrown Execute the Esptouch Task and return the result
*
* Smart Config v2.2 support the API
* Smart Config v2.4 support the API
*
* It will be blocked until the client receive result count >= expectTaskResultCount.
* If it fail, it will return one fail result will be returned in the list.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.util.List;

import com.espressif.iot.esptouch.EsptouchTask;
import com.espressif.iot.esptouch.IEsptouchListener;
import com.espressif.iot.esptouch.IEsptouchResult;
import com.espressif.iot.esptouch.IEsptouchTask;
import com.espressif.iot.esptouch.task.__IEsptouchTask;
Expand All @@ -24,6 +25,7 @@
import android.widget.Spinner;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.Toast;

public class EsptouchDemoActivity extends Activity implements OnClickListener {

Expand Down Expand Up @@ -190,6 +192,26 @@ protected void onPostExecute(IEsptouchResult result) {
}
}

private void onEsptoucResultAddedPerform(final IEsptouchResult result) {
runOnUiThread(new Runnable() {

@Override
public void run() {
String text = result.getBssid() + " is connected to the wifi";
Toast.makeText(EsptouchDemoActivity.this, text,
Toast.LENGTH_LONG).show();
}

});
}

private IEsptouchListener myListener = new IEsptouchListener() {

@Override
public void onEsptouchResultAdded(final IEsptouchResult result) {
onEsptoucResultAddedPerform(result);
}
};

private class EsptouchAsyncTask3 extends AsyncTask<String, Void, List<IEsptouchResult>> {

Expand Down Expand Up @@ -250,6 +272,7 @@ protected List<IEsptouchResult> doInBackground(String... params) {
taskResultCount = Integer.parseInt(taskResultCountStr);
mEsptouchTask = new EsptouchTask(apSsid, apBssid, apPassword,
isSsidHidden, EsptouchDemoActivity.this);
mEsptouchTask.setEsptouchListener(myListener);
}
List<IEsptouchResult> resultList = mEsptouchTask.executeForResults(taskResultCount);
return resultList;
Expand Down
46 changes: 27 additions & 19 deletions src/com/espressif/iot/esptouch/task/__EsptouchTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.concurrent.atomic.AtomicBoolean;

import com.espressif.iot.esptouch.EsptouchResult;
import com.espressif.iot.esptouch.IEsptouchListener;
import com.espressif.iot.esptouch.IEsptouchResult;
import com.espressif.iot.esptouch.protocol.EsptouchGenerator;
import com.espressif.iot.esptouch.udp.UDPSocketClient;
Expand All @@ -26,7 +27,7 @@ public class __EsptouchTask implements __IEsptouchTask {
* one indivisible data contain 3 9bits info
*/
private static final int ONE_DATA_LEN = 3;

private static final String TAG = "EsptouchTask";

private volatile List<IEsptouchResult> mEsptouchResultList;
Expand All @@ -43,9 +44,11 @@ public class __EsptouchTask implements __IEsptouchTask {
private AtomicBoolean mIsCancelled;
private IEsptouchTaskParameter mParameter;
private volatile Map<String, Integer> mBssidTaskSucCountMap;
private IEsptouchListener mEsptouchListener;

public __EsptouchTask(String apSsid, String apBssid, String apPassword, Context context, IEsptouchTaskParameter parameter
,boolean isSsidHidden) {
public __EsptouchTask(String apSsid, String apBssid, String apPassword,
Context context, IEsptouchTaskParameter parameter,
boolean isSsidHidden) {
if (TextUtils.isEmpty(apSsid)) {
throw new IllegalArgumentException(
"the apSsid should be null or empty");
Expand All @@ -61,8 +64,7 @@ public __EsptouchTask(String apSsid, String apBssid, String apPassword, Context
mSocketClient = new UDPSocketClient();
mParameter = parameter;
mSocketServer = new UDPSocketServer(mParameter.getPortListening(),
mParameter.getWaitUdpTotalMillisecond(),
context);
mParameter.getWaitUdpTotalMillisecond(), context);
mIsSsidHidden = isSsidHidden;
mEsptouchResultList = new ArrayList<IEsptouchResult>();
mBssidTaskSucCountMap = new HashMap<String, Integer>();
Expand Down Expand Up @@ -104,13 +106,16 @@ private void __putEsptouchResult(boolean isSuc, String bssid,
if (__IEsptouchTask.DEBUG) {
Log.d(TAG, "__putEsptouchResult(): put one more result");
}
IEsptouchResult esptouchResult = new EsptouchResult(isSuc,
final IEsptouchResult esptouchResult = new EsptouchResult(isSuc,
bssid, inetAddress);
mEsptouchResultList.add(esptouchResult);
if (mEsptouchListener != null) {
mEsptouchListener.onEsptouchResultAdded(esptouchResult);
}
}
}
}

private List<IEsptouchResult> __getEsptouchResultList() {
synchronized (mEsptouchResultList) {
if (mEsptouchResultList.isEmpty()) {
Expand All @@ -119,16 +124,17 @@ private List<IEsptouchResult> __getEsptouchResultList() {
esptouchResultFail.setIsCancelled(mIsCancelled.get());
mEsptouchResultList.add(esptouchResultFail);
}

return mEsptouchResultList;
}
}

private synchronized void __interrupt() {
if (!mIsInterrupt) {
mIsInterrupt = true;
mSocketClient.interrupt();
mSocketServer.interrupt();
// interrupt the current Thread which is used to wait for udp response
// interrupt the current Thread which is used to wait for udp response
Thread.currentThread().interrupt();
}
}
Expand Down Expand Up @@ -227,7 +233,7 @@ private boolean __execute(IEsptouchGenerator generator) {

byte[][] gcBytes2 = generator.getGCBytes2();
byte[][] dcBytes2 = generator.getDCBytes2();

int index = 0;
while (!mIsInterrupt) {
if (currentTime - lastTime >= mParameter.getTimeoutTotalCodeMillisecond()) {
Expand Down Expand Up @@ -273,7 +279,7 @@ private void __checkTaskValid() {
}
this.mIsExecuted = true;
}

@Override
public IEsptouchResult executeForResult() throws RuntimeException {
return executeForResults(1).get(0);
Expand All @@ -288,9 +294,9 @@ public boolean isCancelled() {
public List<IEsptouchResult> executeForResults(int expectTaskResultCount)
throws RuntimeException {
__checkTaskValid();

mParameter.setExpectTaskResultCount(expectTaskResultCount);

if (__IEsptouchTask.DEBUG) {
Log.d(TAG, "execute()");
}
Expand All @@ -315,18 +321,15 @@ public List<IEsptouchResult> executeForResults(int expectTaskResultCount)
return __getEsptouchResultList();
}
}

// wait the udp response without sending udp broadcast
try {
Thread.sleep(mParameter.getWaitUdpReceivingMillisecond());
} catch (InterruptedException e) {
// receive the udp broadcast or the user interrupt the task
if (this.mIsSuc)
{
if (this.mIsSuc) {
return __getEsptouchResultList();
}
else
{
} else {
this.__interrupt();
return __getEsptouchResultList();
}
Expand All @@ -335,4 +338,9 @@ public List<IEsptouchResult> executeForResults(int expectTaskResultCount)
return __getEsptouchResultList();
}

@Override
public void setEsptouchListener(IEsptouchListener esptouchListener) {
mEsptouchListener = esptouchListener;
}

}
7 changes: 7 additions & 0 deletions src/com/espressif/iot/esptouch/task/__IEsptouchTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;

import com.espressif.iot.esptouch.IEsptouchListener;
import com.espressif.iot.esptouch.IEsptouchResult;

/**
Expand All @@ -13,6 +14,12 @@
*
*/
public interface __IEsptouchTask {

/**
* set the esptouch listener, when one device is connected to the Ap, it will be called back
* @param esptouchListener when one device is connected to the Ap, it will be called back
*/
void setEsptouchListener(IEsptouchListener esptouchListener);

/**
* Interrupt the Esptouch Task when User tap back or close the Application.
Expand Down

0 comments on commit cde76ef

Please sign in to comment.