Skip to content

Commit 51515dd

Browse files
author
fancyxu
committed
Merge branch 'dev/qr-code-wifi-config'
2 parents afc0162 + 9a91236 commit 51515dd

File tree

5 files changed

+113
-40
lines changed

5 files changed

+113
-40
lines changed

samples/wifi_config/wifi_config_sample.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
#include "qcloud_wifi_config_internal.h"
3535

3636
#if !(WIFI_PROV_SOFT_AP_ENABLE || WIFI_PROV_SMART_CONFIG_ENABLE || WIFI_PROV_AIRKISS_CONFIG_ENABLE || \
37-
WIFI_PROV_SIMPLE_CONFIG_ENABLE || WIFI_PROV_BT_COMBO_CONFIG_ENABLE)
37+
WIFI_PROV_SIMPLE_CONFIG_ENABLE || WIFI_PROV_BT_COMBO_CONFIG_ENABLE || WIFI_PROV_QR_CODE_CONFIG_ENABLE)
3838

3939
#error "Please choose one set to 1 of them in qcloud_wifi_config.h"
4040
#endif
@@ -481,8 +481,8 @@ static bool qcloud_wifi_config_proc()
481481
int rc;
482482
Timer timer;
483483
#if WIFI_PROV_SOFT_AP_ENABLE
484-
WiFiConfigParams apConf = {"qcloud-softap", "12345678", 6};
485-
rc = qiot_wifi_config_start(WIFI_CONFIG_TYPE_SOFT_AP, &apConf, _wifi_config_result_cb);
484+
WiFiConfigParams apConf = {"qcloud-softap", "12345678", 6};
485+
rc = qiot_wifi_config_start(WIFI_CONFIG_TYPE_SOFT_AP, &apConf, _wifi_config_result_cb);
486486
countdown(&timer, 500);
487487
while ((rc == QCLOUD_RET_SUCCESS) && (false == wifi_config_result_success) && !expired(&timer)) {
488488
Log_d("wait wifi config result...");
@@ -545,6 +545,21 @@ static bool qcloud_wifi_config_proc()
545545
return true;
546546
}
547547
#endif // WIFI_PROV_BT_COMBO_CONFIG_ENABLE
548+
549+
#if WIFI_PROV_QR_CODE_CONFIG_ENABLE
550+
rc = qiot_wifi_config_start(WIFI_CONFIG_TYPE_QR_CODE,
551+
"{\"SSID\":\"your_ssid\",\"password\":\"your_password\",\"token\":\"your_token\"}",
552+
_wifi_config_result_cb);
553+
countdown(&timer, 500);
554+
while ((rc == QCLOUD_RET_SUCCESS) && (false == wifi_config_result_success) && !expired(&timer)) {
555+
Log_d("wait wifi config result...");
556+
HAL_SleepMs(1000);
557+
}
558+
qiot_wifi_config_stop();
559+
if (true == wifi_config_result_success) {
560+
return true;
561+
}
562+
#endif // WIFI_PROV_QR_CODE_CONFIG_ENABLE
548563
return false;
549564
}
550565

sdk_src/internal_inc/qcloud_wifi_config.h

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ extern "C" {
2020
#endif
2121

2222
#define WIFI_ERR_LOG_POST 1
23-
#define WIFI_LOG_UPLOAD 1
23+
#define WIFI_LOG_UPLOAD 0
2424

25-
#define WIFI_PROV_SOFT_AP_ENABLE 1 ///< wifi provisioning method: device AP, need Wechat Applets
26-
#define WIFI_PROV_SMART_CONFIG_ENABLE 1 ///< wifi provisioning method: smart config, need Wechat Applets
27-
#define WIFI_PROV_AIRKISS_CONFIG_ENABLE 1 ///< wifi provisioning method: airkiss, need Wechat Applets
28-
#define WIFI_PROV_SIMPLE_CONFIG_ENABLE 1 ///< wifi provisioning method: simple config, need Wechat Applets
29-
#define WIFI_PROV_BT_COMBO_CONFIG_ENABLE 1 ///< wifi provisioning method: bt combo config, need Wechat Applets
25+
#define WIFI_PROV_SOFT_AP_ENABLE 0 ///< wifi provisioning method: device AP, need Wechat Applets
26+
#define WIFI_PROV_SMART_CONFIG_ENABLE 0 ///< wifi provisioning method: smart config, need Wechat Applets
27+
#define WIFI_PROV_AIRKISS_CONFIG_ENABLE 0 ///< wifi provisioning method: airkiss, need Wechat Applets
28+
#define WIFI_PROV_SIMPLE_CONFIG_ENABLE 0 ///< wifi provisioning method: simple config, need Wechat Applets
29+
#define WIFI_PROV_BT_COMBO_CONFIG_ENABLE 0 ///< wifi provisioning method: bt combo config, need Wechat Applets
30+
#define WIFI_PROV_QR_CODE_CONFIG_ENABLE \
31+
1 ///< wifi provisioning method: qr code, {"SSID":"x","password":"x","token":"x"}
3032

3133
#define MAX_SSID_LEN 32 // max ssid len
3234
#define MAX_PSK_LEN 65 // max psk len
@@ -37,6 +39,7 @@ typedef enum {
3739
WIFI_CONFIG_TYPE_AIRKISS = 2, /* Airkiss */
3840
WIFI_CONFIG_TYPE_SIMPLE_CONFIG = 3, /* Simple config */
3941
WIFI_CONFIG_TYPE_BT_COMBO = 4, /* BT Combo config */
42+
WIFI_CONFIG_TYPE_QR_CODE = 5, /* QR Code config */
4043
} eWiFiConfigType;
4144

4245
typedef enum {

sdk_src/internal_inc/qcloud_wifi_config_internal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ typedef struct {
138138

139139
int qiot_comm_service_start(void);
140140
void qiot_comm_service_stop(void);
141+
int qiot_comm_parse_json_token(const char *json);
141142

142143
int qiot_device_bind(void);
143144

sdk_src/wifi_config/qcloud_wifi_config.c

Lines changed: 34 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,23 @@ typedef struct {
3333
int (*config_stop)(void);
3434
} WiFiConfigMethod;
3535

36+
static int _qr_code_config_start(void *json, WifiConfigEventCallBack callback)
37+
{
38+
int rc = qiot_comm_parse_json_token((const char *)json);
39+
callback(rc == 1 ? RESULT_WIFI_CONFIG_SUCCESS : RESULT_WIFI_CONFIG_FAILED, NULL);
40+
return !(rc == 1);
41+
}
42+
3643
static WiFiConfigMethod sg_wifi_config_methods[] = {
37-
{HAL_SoftApProvision_Start, HAL_SoftApProvision_Stop}, // WIFI_CONFIG_TYPE_SOFT_AP
38-
{HAL_SmartConfig_Start, HAL_SmartConfig_Stop}, // WIFI_CONFIG_TYPE_SMART_CONFIG
39-
{HAL_AirkissConfig_Start, HAL_AirkissConfig_Stop}, // WIFI_CONFIG_TYPE_AIRKISS
40-
{HAL_SimpleConfig_Start, HAL_SimpleConfig_Stop}, // WIFI_CONFIG_TYPE_SIMPLE_CONFIG
41-
{HAL_BTComboConfig_Start, HAL_BTComboConfig_Stop} // WIFI_CONFIG_TYPE_BTCombo_CONFIG
44+
[WIFI_CONFIG_TYPE_SOFT_AP] = {HAL_SoftApProvision_Start, HAL_SoftApProvision_Stop},
45+
[WIFI_CONFIG_TYPE_SMART_CONFIG] = {HAL_SmartConfig_Start, HAL_SmartConfig_Stop},
46+
[WIFI_CONFIG_TYPE_AIRKISS] = {HAL_AirkissConfig_Start, HAL_AirkissConfig_Stop},
47+
[WIFI_CONFIG_TYPE_SIMPLE_CONFIG] = {HAL_SimpleConfig_Start, HAL_SimpleConfig_Stop},
48+
[WIFI_CONFIG_TYPE_BT_COMBO] = {HAL_BTComboConfig_Start, HAL_BTComboConfig_Stop},
49+
[WIFI_CONFIG_TYPE_QR_CODE] = {_qr_code_config_start, NULL},
4250
};
4351

44-
static WiFiConfigMethod * sg_wifi_config_method_now = NULL;
52+
static WiFiConfigMethod *sg_wifi_config_method_now = NULL;
4553
static WifiConfigResultCallBack sg_wifi_config_result_cb = NULL;
4654

4755
static void _qiot_wifi_config_event_cb(eWiFiConfigEvent event, void *usr_data)
@@ -86,30 +94,25 @@ static void _qiot_wifi_config_event_cb(eWiFiConfigEvent event, void *usr_data)
8694
int qiot_wifi_config_start(eWiFiConfigType type, void *params, WifiConfigResultCallBack result_cb)
8795
{
8896
memset(&g_publish_token_info, 0, sizeof(publish_token_info_t));
89-
if (type < WIFI_CONFIG_TYPE_SOFT_AP || type > WIFI_CONFIG_TYPE_BT_COMBO) {
90-
Log_e("Unknown wifi config type!");
91-
return ERR_UNKNOWN_WIFI_CONFIG_TYPE;
92-
} else {
93-
switch (type) {
94-
case WIFI_CONFIG_TYPE_SOFT_AP:
95-
strncpy(g_publish_token_info.pairTime.type, "SoftAP", MAX_TYPE_LENGTH);
96-
break;
97-
case WIFI_CONFIG_TYPE_AIRKISS:
98-
strncpy(g_publish_token_info.pairTime.type, "AirKiss", MAX_TYPE_LENGTH);
99-
break;
100-
case WIFI_CONFIG_TYPE_SMART_CONFIG:
101-
strncpy(g_publish_token_info.pairTime.type, "SmartConfig", MAX_TYPE_LENGTH);
102-
break;
103-
case WIFI_CONFIG_TYPE_SIMPLE_CONFIG:
104-
strncpy(g_publish_token_info.pairTime.type, "SimpleConfig", MAX_TYPE_LENGTH);
105-
break;
106-
case WIFI_CONFIG_TYPE_BT_COMBO:
107-
strncpy(g_publish_token_info.pairTime.type, "BTCombo", MAX_TYPE_LENGTH);
108-
break;
109-
default:
110-
strncpy(g_publish_token_info.pairTime.type, "Unknow", MAX_TYPE_LENGTH);
111-
break;
112-
}
97+
switch (type) {
98+
case WIFI_CONFIG_TYPE_SOFT_AP:
99+
strncpy(g_publish_token_info.pairTime.type, "SoftAP", MAX_TYPE_LENGTH);
100+
break;
101+
case WIFI_CONFIG_TYPE_AIRKISS:
102+
strncpy(g_publish_token_info.pairTime.type, "AirKiss", MAX_TYPE_LENGTH);
103+
break;
104+
case WIFI_CONFIG_TYPE_SMART_CONFIG:
105+
strncpy(g_publish_token_info.pairTime.type, "SmartConfig", MAX_TYPE_LENGTH);
106+
break;
107+
case WIFI_CONFIG_TYPE_SIMPLE_CONFIG:
108+
strncpy(g_publish_token_info.pairTime.type, "SimpleConfig", MAX_TYPE_LENGTH);
109+
break;
110+
case WIFI_CONFIG_TYPE_BT_COMBO:
111+
strncpy(g_publish_token_info.pairTime.type, "BTCombo", MAX_TYPE_LENGTH);
112+
break;
113+
default:
114+
strncpy(g_publish_token_info.pairTime.type, "Unknow", MAX_TYPE_LENGTH);
115+
break;
113116
}
114117

115118
if (init_dev_log_queue()) {
@@ -137,7 +140,7 @@ int qiot_wifi_config_start(eWiFiConfigType type, void *params, WifiConfigResultC
137140
return ERR_WIFI_CONFIG_START_FAILED;
138141
}
139142

140-
#if !WIFI_PROV_BT_COMBO_CONFIG_ENABLE
143+
#if !WIFI_PROV_BT_COMBO_CONFIG_ENABLE && !WIFI_PROV_QR_CODE_CONFIG_ENABLE
141144
if (qiot_comm_service_start()) {
142145
sg_wifi_config_method_now->config_stop();
143146
sg_wifi_config_method_now = NULL;

sdk_src/wifi_config/qcloud_wifi_config_comm_service.c

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ extern publish_token_info_t g_publish_token_info;
2828

2929
static int _app_reply_dev_info(comm_peer_t *peer, eWiFiConfigCmd cmd)
3030
{
31+
if (!peer) {
32+
return -1;
33+
}
34+
3135
int ret;
3236
DeviceInfo devinfo;
3337
cJSON_Hooks memoryHook;
@@ -261,6 +265,48 @@ static void _app_handle_broadcast_local_ipv4(int socket_id)
261265
}
262266
}
263267

268+
static int _parse_json_token(const char *json, comm_peer_t *peer)
269+
{
270+
int ret;
271+
cJSON *root = cJSON_Parse(json);
272+
cJSON *ssid_json = cJSON_GetObjectItem(root, "ssid");
273+
cJSON *psw_json = cJSON_GetObjectItem(root, "password");
274+
cJSON *token_json = cJSON_GetObjectItem(root, "token");
275+
276+
if (ssid_json && psw_json && token_json) {
277+
// parse token and connect to ap
278+
g_publish_token_info.pairTime.getSSID = HAL_GetTimeMs();
279+
qiot_device_bind_set_token(token_json->valuestring);
280+
_app_reply_dev_info(peer, CMD_DEVICE_REPLY);
281+
// sleep a while before changing to STA mode
282+
HAL_SleepMs(3000);
283+
Log_i("STA to connect SSID:%s PASSWORD:%s", ssid_json->valuestring, psw_json->valuestring);
284+
PUSH_LOG("SSID:%s|PSW:%s|TOKEN:%s", ssid_json->valuestring, psw_json->valuestring, token_json->valuestring);
285+
ret = HAL_Wifi_StaConnect(ssid_json->valuestring, psw_json->valuestring, 0);
286+
if (ret) {
287+
Log_e("wifi_sta_connect failed: %d", ret);
288+
PUSH_LOG("wifi_sta_connect failed: %d", ret);
289+
app_send_error_log(peer, CUR_ERR, ERR_WIFI_AP_STA, ret);
290+
cJSON_Delete(root);
291+
#if WIFI_PROV_SOFT_AP_ENABLE
292+
set_soft_ap_config_result(WIFI_CONFIG_FAIL);
293+
#endif
294+
return -1;
295+
} else {
296+
Log_d("wifi_sta_connect success");
297+
#if WIFI_PROV_SOFT_AP_ENABLE
298+
set_soft_ap_config_result(WIFI_CONFIG_SUCCESS);
299+
#endif
300+
}
301+
cJSON_Delete(root);
302+
303+
/* return 1 as device alreay switch to STA mode and unable to recv cmd anymore
304+
* 1: Everything OK and we've finished the job */
305+
return 1;
306+
}
307+
return -1;
308+
}
309+
264310
static int _app_handle_recv_data(comm_peer_t *peer, char *pdata, int len)
265311
{
266312
int ret = 0;
@@ -490,3 +536,8 @@ void qiot_comm_service_stop(void)
490536
{
491537
sg_comm_task_run = false;
492538
}
539+
540+
int qiot_comm_parse_json_token(const char *json)
541+
{
542+
return _parse_json_token(json, NULL);
543+
}

0 commit comments

Comments
 (0)