Skip to content

Commit 7f8108a

Browse files
committed
Merge branch 'feature/sec2_provisioning' into 'master'
Support for provisioning using Security 2. See merge request idf/esp-idf-provisioning-android!59
2 parents c6fb3a9 + 519c0a8 commit 7f8108a

37 files changed

+4003
-194
lines changed

README.md

+17-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ To get this app please clone this repository using the below command:
2929
- [x] Provision device.
3030
- [x] Scan for available Wi-Fi networks.
3131
- [x] Support for exchanging custom data.
32+
- [x] Support for security version 2.
3233

3334
## Requirements
3435

@@ -47,7 +48,7 @@ To get this app please clone this repository using the below command:
4748
```
4849
And add a dependency code to your app module's `build.gradle` file.
4950
```
50-
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.0.14'
51+
implementation 'com.github.espressif:esp-idf-provisioning-android:lib-2.1.0'
5152
```
5253

5354
## Using Provisioning Library
@@ -75,7 +76,7 @@ Payload information :
7576
| name | Name of the device. | PROV_XXXXXX | Yes |
7677
| pop | Proof of possession. | POP value of the device like abcd1234 | Optional. Considered empty string if not available in QR code data. |
7778
| transport | Wi-Fi provisioning transport type. | It can be softap or ble. | Yes |
78-
| security | Security for device communication. | It can be 0 or 1 int value. | Optional. Considered Sec1 if not available in QR code data. |
79+
| security | Security for device communication. | It can be 0, 1 or 2 int value. | Optional. Considered Sec2 if not available in QR code data. |
7980
| password | Password of SoftAP device. | Password to connect with SoftAP device. | Optional |
8081

8182
In provisioning library, there are two options for QR code scanning API.
@@ -152,6 +153,20 @@ ESPProvisionManager.getInstance(context).createESPDevice(TransportType transport
152153
```
153154
For both transport app can listen device connected / disconnected events by registering for events.
154155

156+
157+
After device connection, app needs to get Proof of Possession from user, if device has pop capability.
158+
App needs to set proof of possession for the device.
159+
160+
```java
161+
espDevice.setProofOfPossession(pop)
162+
```
163+
164+
For security version 2, app needs to provide username as shown below :
165+
166+
```java
167+
espDevice.setUserName(username)
168+
```
169+
155170

156171
## Provisioning
157172

app/build.gradle

+2-2
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ android {
1717
applicationId "com.espressif.wifi_provisioning"
1818
minSdkVersion 23
1919
targetSdkVersion 30
20-
versionCode 15
21-
versionName "2.0.14 - ${getGitHash()}"
20+
versionCode 16
21+
versionName "2.1.0 - ${getGitHash()}"
2222
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
2323
}
2424

app/src/main/java/com/espressif/AppConstants.java

+7
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,18 @@ public class AppConstants {
2727
public static final String KEY_WIFI_PASSWORD = "password";
2828
public static final String KEY_DEVICE_TYPES = "device_types";
2929
public static final String KEY_SECURITY_TYPE = "security_type";
30+
public static final String KEY_USER_NAME = "sec2_username";
3031

3132
public static final String ESP_PREFERENCES = "Esp_Preferences";
3233

3334
public static final String DEVICE_TYPE_SOFTAP = "softap";
3435
public static final String DEVICE_TYPE_BLE = "ble";
3536
public static final String DEVICE_TYPE_BOTH = "both";
3637
public static final String DEVICE_TYPE_DEFAULT = DEVICE_TYPE_BOTH;
38+
39+
public static final int SEC_TYPE_0 = 0;
40+
public static final int SEC_TYPE_1 = 1;
41+
public static final int SEC_TYPE_2 = 2;
42+
public static final int SEC_TYPE_DEFAULT = SEC_TYPE_2;
43+
public static final String DEFAULT_USER_NAME = "wifiprov";
3744
}

app/src/main/java/com/espressif/ui/activities/AddDeviceActivity.java

+129-38
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,8 @@
5555
import org.greenrobot.eventbus.EventBus;
5656
import org.greenrobot.eventbus.Subscribe;
5757
import org.greenrobot.eventbus.ThreadMode;
58+
import org.json.JSONException;
59+
import org.json.JSONObject;
5860

5961
import java.util.ArrayList;
6062

@@ -194,17 +196,12 @@ public void onEvent(DeviceConnectionEvent event) {
194196

195197
case ESPConstants.EVENT_DEVICE_CONNECTED:
196198

197-
Log.e(TAG, "Device Connected Event Received");
198-
ArrayList<String> deviceCaps = espDevice.getDeviceCapabilities();
199-
200-
if (deviceCaps.contains("wifi_scan")) {
201-
202-
goToWiFiScanActivity();
203-
204-
} else {
205-
206-
goToWiFiConfigActivity();
199+
if (TextUtils.isEmpty(provisionManager.getEspDevice().getUserName())) {
200+
String userName = sharedPreferences.getString(AppConstants.KEY_USER_NAME, AppConstants.DEFAULT_USER_NAME);
201+
provisionManager.getEspDevice().setUserName(userName);
207202
}
203+
Log.e(TAG, "Device Connected Event Received");
204+
setSecurityTypeFromVersionInfo();
208205
break;
209206

210207
case ESPConstants.EVENT_DEVICE_DISCONNECTED:
@@ -214,18 +211,20 @@ public void onEvent(DeviceConnectionEvent event) {
214211
Toast.makeText(AddDeviceActivity.this, "Device disconnected", Toast.LENGTH_LONG).show();
215212
finish();
216213
} else {
217-
askForManualDeviceConnection();
214+
if (!isFinishing()) {
215+
askForManualDeviceConnection();
216+
}
218217
}
219218
break;
220219

221220
case ESPConstants.EVENT_DEVICE_CONNECTION_FAILED:
222221

223222
if (espDevice != null && espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_BLE)) {
224-
225-
Toast.makeText(AddDeviceActivity.this, "Failed to connect with device", Toast.LENGTH_LONG).show();
226-
finish();
223+
alertForDeviceNotSupported("Failed to connect with device");
227224
} else {
228-
askForManualDeviceConnection();
225+
if (!isFinishing()) {
226+
askForManualDeviceConnection();
227+
}
229228
}
230229
break;
231230
}
@@ -375,18 +374,14 @@ public void run() {
375374
if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE)) {
376375

377376
if (espDevice != null && espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_SOFTAP)) {
378-
379-
Toast.makeText(AddDeviceActivity.this, "Error! Device not supported", Toast.LENGTH_LONG).show();
380-
finish();
377+
alertForDeviceNotSupported(getString(R.string.error_device_transport_not_supported));
381378
} else {
382379
device.connectToDevice();
383380
}
384381
} else if (deviceType.equals(AppConstants.DEVICE_TYPE_SOFTAP)) {
385382

386383
if (espDevice != null && espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_BLE)) {
387-
388-
Toast.makeText(AddDeviceActivity.this, "Error! Device not supported", Toast.LENGTH_LONG).show();
389-
finish();
384+
alertForDeviceNotSupported(getString(R.string.error_device_transport_not_supported));
390385
} else {
391386

392387
if (espDevice.getTransportType().equals(ESPConstants.TransportType.TRANSPORT_SOFTAP)
@@ -517,10 +512,18 @@ public void onClick(DialogInterface dialog, int which) {
517512

518513
dialog.dismiss();
519514
if (espDevice != null) {
520-
if (espDevice.getSecurityType().equals(ESPConstants.SecurityType.SECURITY_0)) {
521-
goToWiFiProvisionLandingActivity(0);
522-
} else {
523-
goToWiFiProvisionLandingActivity(1);
515+
516+
switch (espDevice.getSecurityType()) {
517+
case SECURITY_0:
518+
goToWiFiProvisionLandingActivity(AppConstants.SEC_TYPE_0);
519+
break;
520+
case SECURITY_1:
521+
goToWiFiProvisionLandingActivity(AppConstants.SEC_TYPE_1);
522+
break;
523+
case SECURITY_2:
524+
default:
525+
goToWiFiProvisionLandingActivity(AppConstants.SEC_TYPE_2);
526+
break;
524527
}
525528
} else {
526529
finish();
@@ -545,27 +548,26 @@ public void onClick(DialogInterface dialog, int which) {
545548
private void startProvisioningFlow() {
546549

547550
String deviceType = sharedPreferences.getString(AppConstants.KEY_DEVICE_TYPES, AppConstants.DEVICE_TYPE_DEFAULT);
548-
final boolean isSec1 = sharedPreferences.getBoolean(AppConstants.KEY_SECURITY_TYPE, true);
551+
final boolean isSecure = sharedPreferences.getBoolean(AppConstants.KEY_SECURITY_TYPE, true);
549552
Log.d(TAG, "Device Types : " + deviceType);
550-
Log.d(TAG, "isSec1 : " + isSec1);
551-
int securityType = 0;
552-
if (isSec1) {
553-
securityType = 1;
553+
int securityType = AppConstants.SEC_TYPE_0;
554+
if (isSecure) {
555+
securityType = AppConstants.SEC_TYPE_DEFAULT;
554556
}
555557

556558
if (deviceType.equals(AppConstants.DEVICE_TYPE_BLE)) {
557559

558-
if (isSec1) {
559-
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_BLE, ESPConstants.SecurityType.SECURITY_1);
560+
if (isSecure) {
561+
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_BLE, ESPConstants.SecurityType.SECURITY_2);
560562
} else {
561563
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_BLE, ESPConstants.SecurityType.SECURITY_0);
562564
}
563565
goToBLEProvisionLandingActivity(securityType);
564566

565567
} else if (deviceType.equals(AppConstants.DEVICE_TYPE_SOFTAP)) {
566568

567-
if (isSec1) {
568-
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_SOFTAP, ESPConstants.SecurityType.SECURITY_1);
569+
if (isSecure) {
570+
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_SOFTAP, ESPConstants.SecurityType.SECURITY_2);
569571
} else {
570572
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_SOFTAP, ESPConstants.SecurityType.SECURITY_0);
571573
}
@@ -586,8 +588,8 @@ public void onClick(DialogInterface dialog, int position) {
586588
switch (position) {
587589
case 0:
588590

589-
if (isSec1) {
590-
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_BLE, ESPConstants.SecurityType.SECURITY_1);
591+
if (isSecure) {
592+
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_BLE, ESPConstants.SecurityType.SECURITY_2);
591593
} else {
592594
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_BLE, ESPConstants.SecurityType.SECURITY_0);
593595
}
@@ -597,8 +599,8 @@ public void onClick(DialogInterface dialog, int position) {
597599

598600
case 1:
599601

600-
if (isSec1) {
601-
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_SOFTAP, ESPConstants.SecurityType.SECURITY_1);
602+
if (isSecure) {
603+
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_SOFTAP, ESPConstants.SecurityType.SECURITY_2);
602604
} else {
603605
provisionManager.createESPDevice(ESPConstants.TransportType.TRANSPORT_SOFTAP, ESPConstants.SecurityType.SECURITY_0);
604606
}
@@ -613,6 +615,71 @@ public void onClick(DialogInterface dialog, int position) {
613615
}
614616
}
615617

618+
private void setSecurityTypeFromVersionInfo() {
619+
620+
boolean isSecure = sharedPreferences.getBoolean(AppConstants.KEY_SECURITY_TYPE, true);
621+
String protoVerStr = provisionManager.getEspDevice().getVersionInfo();
622+
int securityType = AppConstants.SEC_TYPE_2;
623+
624+
try {
625+
JSONObject jsonObject = new JSONObject(protoVerStr);
626+
JSONObject provInfo = jsonObject.getJSONObject("prov");
627+
628+
if (provInfo.has("sec_ver")) {
629+
630+
int serVer = provInfo.optInt("sec_ver");
631+
Log.d(TAG, "Security Version : " + serVer);
632+
633+
switch (serVer) {
634+
case AppConstants.SEC_TYPE_0:
635+
securityType = AppConstants.SEC_TYPE_0;
636+
provisionManager.getEspDevice().setSecurityType(ESPConstants.SecurityType.SECURITY_0);
637+
break;
638+
case AppConstants.SEC_TYPE_1:
639+
securityType = AppConstants.SEC_TYPE_1;
640+
provisionManager.getEspDevice().setSecurityType(ESPConstants.SecurityType.SECURITY_1);
641+
break;
642+
case AppConstants.SEC_TYPE_2:
643+
default:
644+
securityType = AppConstants.SEC_TYPE_2;
645+
provisionManager.getEspDevice().setSecurityType(ESPConstants.SecurityType.SECURITY_2);
646+
break;
647+
}
648+
} else {
649+
if (securityType == AppConstants.SEC_TYPE_2) {
650+
securityType = AppConstants.SEC_TYPE_1;
651+
provisionManager.getEspDevice().setSecurityType(ESPConstants.SecurityType.SECURITY_1);
652+
}
653+
}
654+
} catch (JSONException e) {
655+
e.printStackTrace();
656+
Log.d(TAG, "Capabilities JSON not available.");
657+
}
658+
659+
if (isSecure) {
660+
if (securityType == AppConstants.SEC_TYPE_0) {
661+
alertForDeviceNotSupported(getString(R.string.error_security_mismatch));
662+
} else {
663+
processDeviceCapabilities();
664+
}
665+
} else {
666+
if (securityType != AppConstants.SEC_TYPE_0) {
667+
alertForDeviceNotSupported(getString(R.string.error_security_mismatch));
668+
} else {
669+
processDeviceCapabilities();
670+
}
671+
}
672+
}
673+
674+
private void processDeviceCapabilities() {
675+
ArrayList<String> deviceCaps = espDevice.getDeviceCapabilities();
676+
if (deviceCaps.contains("wifi_scan")) {
677+
goToWiFiScanActivity();
678+
} else {
679+
goToWiFiConfigActivity();
680+
}
681+
}
682+
616683
private void goToBLEProvisionLandingActivity(int securityType) {
617684

618685
finish();
@@ -646,4 +713,28 @@ private String getWifiSsid() {
646713
}
647714
return ssid;
648715
}
716+
717+
private void alertForDeviceNotSupported(String msg) {
718+
719+
AlertDialog.Builder builder = new AlertDialog.Builder(this);
720+
builder.setCancelable(false);
721+
722+
builder.setTitle(R.string.error_title);
723+
builder.setMessage(msg);
724+
725+
// Set up the buttons
726+
builder.setPositiveButton(R.string.btn_ok, new DialogInterface.OnClickListener() {
727+
728+
@Override
729+
public void onClick(DialogInterface dialog, int which) {
730+
if (provisionManager.getEspDevice() != null) {
731+
provisionManager.getEspDevice().disconnectDevice();
732+
}
733+
dialog.dismiss();
734+
finish();
735+
}
736+
});
737+
738+
builder.show();
739+
}
649740
}

0 commit comments

Comments
 (0)