Skip to content

Commit

Permalink
Merge branch 'dev/qr-code-wifi-config'
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyxu committed Sep 7, 2022
2 parents afc0162 + 9a91236 commit 51515dd
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 40 deletions.
21 changes: 18 additions & 3 deletions samples/wifi_config/wifi_config_sample.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include "qcloud_wifi_config_internal.h"

#if !(WIFI_PROV_SOFT_AP_ENABLE || WIFI_PROV_SMART_CONFIG_ENABLE || WIFI_PROV_AIRKISS_CONFIG_ENABLE || \
WIFI_PROV_SIMPLE_CONFIG_ENABLE || WIFI_PROV_BT_COMBO_CONFIG_ENABLE)
WIFI_PROV_SIMPLE_CONFIG_ENABLE || WIFI_PROV_BT_COMBO_CONFIG_ENABLE || WIFI_PROV_QR_CODE_CONFIG_ENABLE)

#error "Please choose one set to 1 of them in qcloud_wifi_config.h"
#endif
Expand Down Expand Up @@ -481,8 +481,8 @@ static bool qcloud_wifi_config_proc()
int rc;
Timer timer;
#if WIFI_PROV_SOFT_AP_ENABLE
WiFiConfigParams apConf = {"qcloud-softap", "12345678", 6};
rc = qiot_wifi_config_start(WIFI_CONFIG_TYPE_SOFT_AP, &apConf, _wifi_config_result_cb);
WiFiConfigParams apConf = {"qcloud-softap", "12345678", 6};
rc = qiot_wifi_config_start(WIFI_CONFIG_TYPE_SOFT_AP, &apConf, _wifi_config_result_cb);
countdown(&timer, 500);
while ((rc == QCLOUD_RET_SUCCESS) && (false == wifi_config_result_success) && !expired(&timer)) {
Log_d("wait wifi config result...");
Expand Down Expand Up @@ -545,6 +545,21 @@ static bool qcloud_wifi_config_proc()
return true;
}
#endif // WIFI_PROV_BT_COMBO_CONFIG_ENABLE

#if WIFI_PROV_QR_CODE_CONFIG_ENABLE
rc = qiot_wifi_config_start(WIFI_CONFIG_TYPE_QR_CODE,
"{\"SSID\":\"your_ssid\",\"password\":\"your_password\",\"token\":\"your_token\"}",
_wifi_config_result_cb);
countdown(&timer, 500);
while ((rc == QCLOUD_RET_SUCCESS) && (false == wifi_config_result_success) && !expired(&timer)) {
Log_d("wait wifi config result...");
HAL_SleepMs(1000);
}
qiot_wifi_config_stop();
if (true == wifi_config_result_success) {
return true;
}
#endif // WIFI_PROV_QR_CODE_CONFIG_ENABLE
return false;
}

Expand Down
15 changes: 9 additions & 6 deletions sdk_src/internal_inc/qcloud_wifi_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,15 @@ extern "C" {
#endif

#define WIFI_ERR_LOG_POST 1
#define WIFI_LOG_UPLOAD 1
#define WIFI_LOG_UPLOAD 0

#define WIFI_PROV_SOFT_AP_ENABLE 1 ///< wifi provisioning method: device AP, need Wechat Applets
#define WIFI_PROV_SMART_CONFIG_ENABLE 1 ///< wifi provisioning method: smart config, need Wechat Applets
#define WIFI_PROV_AIRKISS_CONFIG_ENABLE 1 ///< wifi provisioning method: airkiss, need Wechat Applets
#define WIFI_PROV_SIMPLE_CONFIG_ENABLE 1 ///< wifi provisioning method: simple config, need Wechat Applets
#define WIFI_PROV_BT_COMBO_CONFIG_ENABLE 1 ///< wifi provisioning method: bt combo config, need Wechat Applets
#define WIFI_PROV_SOFT_AP_ENABLE 0 ///< wifi provisioning method: device AP, need Wechat Applets
#define WIFI_PROV_SMART_CONFIG_ENABLE 0 ///< wifi provisioning method: smart config, need Wechat Applets
#define WIFI_PROV_AIRKISS_CONFIG_ENABLE 0 ///< wifi provisioning method: airkiss, need Wechat Applets
#define WIFI_PROV_SIMPLE_CONFIG_ENABLE 0 ///< wifi provisioning method: simple config, need Wechat Applets
#define WIFI_PROV_BT_COMBO_CONFIG_ENABLE 0 ///< wifi provisioning method: bt combo config, need Wechat Applets
#define WIFI_PROV_QR_CODE_CONFIG_ENABLE \
1 ///< wifi provisioning method: qr code, {"SSID":"x","password":"x","token":"x"}

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

typedef enum {
Expand Down
1 change: 1 addition & 0 deletions sdk_src/internal_inc/qcloud_wifi_config_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ typedef struct {

int qiot_comm_service_start(void);
void qiot_comm_service_stop(void);
int qiot_comm_parse_json_token(const char *json);

int qiot_device_bind(void);

Expand Down
65 changes: 34 additions & 31 deletions sdk_src/wifi_config/qcloud_wifi_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,23 @@ typedef struct {
int (*config_stop)(void);
} WiFiConfigMethod;

static int _qr_code_config_start(void *json, WifiConfigEventCallBack callback)
{
int rc = qiot_comm_parse_json_token((const char *)json);
callback(rc == 1 ? RESULT_WIFI_CONFIG_SUCCESS : RESULT_WIFI_CONFIG_FAILED, NULL);
return !(rc == 1);
}

static WiFiConfigMethod sg_wifi_config_methods[] = {
{HAL_SoftApProvision_Start, HAL_SoftApProvision_Stop}, // WIFI_CONFIG_TYPE_SOFT_AP
{HAL_SmartConfig_Start, HAL_SmartConfig_Stop}, // WIFI_CONFIG_TYPE_SMART_CONFIG
{HAL_AirkissConfig_Start, HAL_AirkissConfig_Stop}, // WIFI_CONFIG_TYPE_AIRKISS
{HAL_SimpleConfig_Start, HAL_SimpleConfig_Stop}, // WIFI_CONFIG_TYPE_SIMPLE_CONFIG
{HAL_BTComboConfig_Start, HAL_BTComboConfig_Stop} // WIFI_CONFIG_TYPE_BTCombo_CONFIG
[WIFI_CONFIG_TYPE_SOFT_AP] = {HAL_SoftApProvision_Start, HAL_SoftApProvision_Stop},
[WIFI_CONFIG_TYPE_SMART_CONFIG] = {HAL_SmartConfig_Start, HAL_SmartConfig_Stop},
[WIFI_CONFIG_TYPE_AIRKISS] = {HAL_AirkissConfig_Start, HAL_AirkissConfig_Stop},
[WIFI_CONFIG_TYPE_SIMPLE_CONFIG] = {HAL_SimpleConfig_Start, HAL_SimpleConfig_Stop},
[WIFI_CONFIG_TYPE_BT_COMBO] = {HAL_BTComboConfig_Start, HAL_BTComboConfig_Stop},
[WIFI_CONFIG_TYPE_QR_CODE] = {_qr_code_config_start, NULL},
};

static WiFiConfigMethod * sg_wifi_config_method_now = NULL;
static WiFiConfigMethod *sg_wifi_config_method_now = NULL;
static WifiConfigResultCallBack sg_wifi_config_result_cb = NULL;

static void _qiot_wifi_config_event_cb(eWiFiConfigEvent event, void *usr_data)
Expand Down Expand Up @@ -86,30 +94,25 @@ static void _qiot_wifi_config_event_cb(eWiFiConfigEvent event, void *usr_data)
int qiot_wifi_config_start(eWiFiConfigType type, void *params, WifiConfigResultCallBack result_cb)
{
memset(&g_publish_token_info, 0, sizeof(publish_token_info_t));
if (type < WIFI_CONFIG_TYPE_SOFT_AP || type > WIFI_CONFIG_TYPE_BT_COMBO) {
Log_e("Unknown wifi config type!");
return ERR_UNKNOWN_WIFI_CONFIG_TYPE;
} else {
switch (type) {
case WIFI_CONFIG_TYPE_SOFT_AP:
strncpy(g_publish_token_info.pairTime.type, "SoftAP", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_AIRKISS:
strncpy(g_publish_token_info.pairTime.type, "AirKiss", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_SMART_CONFIG:
strncpy(g_publish_token_info.pairTime.type, "SmartConfig", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_SIMPLE_CONFIG:
strncpy(g_publish_token_info.pairTime.type, "SimpleConfig", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_BT_COMBO:
strncpy(g_publish_token_info.pairTime.type, "BTCombo", MAX_TYPE_LENGTH);
break;
default:
strncpy(g_publish_token_info.pairTime.type, "Unknow", MAX_TYPE_LENGTH);
break;
}
switch (type) {
case WIFI_CONFIG_TYPE_SOFT_AP:
strncpy(g_publish_token_info.pairTime.type, "SoftAP", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_AIRKISS:
strncpy(g_publish_token_info.pairTime.type, "AirKiss", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_SMART_CONFIG:
strncpy(g_publish_token_info.pairTime.type, "SmartConfig", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_SIMPLE_CONFIG:
strncpy(g_publish_token_info.pairTime.type, "SimpleConfig", MAX_TYPE_LENGTH);
break;
case WIFI_CONFIG_TYPE_BT_COMBO:
strncpy(g_publish_token_info.pairTime.type, "BTCombo", MAX_TYPE_LENGTH);
break;
default:
strncpy(g_publish_token_info.pairTime.type, "Unknow", MAX_TYPE_LENGTH);
break;
}

if (init_dev_log_queue()) {
Expand Down Expand Up @@ -137,7 +140,7 @@ int qiot_wifi_config_start(eWiFiConfigType type, void *params, WifiConfigResultC
return ERR_WIFI_CONFIG_START_FAILED;
}

#if !WIFI_PROV_BT_COMBO_CONFIG_ENABLE
#if !WIFI_PROV_BT_COMBO_CONFIG_ENABLE && !WIFI_PROV_QR_CODE_CONFIG_ENABLE
if (qiot_comm_service_start()) {
sg_wifi_config_method_now->config_stop();
sg_wifi_config_method_now = NULL;
Expand Down
51 changes: 51 additions & 0 deletions sdk_src/wifi_config/qcloud_wifi_config_comm_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ extern publish_token_info_t g_publish_token_info;

static int _app_reply_dev_info(comm_peer_t *peer, eWiFiConfigCmd cmd)
{
if (!peer) {
return -1;
}

int ret;
DeviceInfo devinfo;
cJSON_Hooks memoryHook;
Expand Down Expand Up @@ -261,6 +265,48 @@ static void _app_handle_broadcast_local_ipv4(int socket_id)
}
}

static int _parse_json_token(const char *json, comm_peer_t *peer)
{
int ret;
cJSON *root = cJSON_Parse(json);
cJSON *ssid_json = cJSON_GetObjectItem(root, "ssid");
cJSON *psw_json = cJSON_GetObjectItem(root, "password");
cJSON *token_json = cJSON_GetObjectItem(root, "token");

if (ssid_json && psw_json && token_json) {
// parse token and connect to ap
g_publish_token_info.pairTime.getSSID = HAL_GetTimeMs();
qiot_device_bind_set_token(token_json->valuestring);
_app_reply_dev_info(peer, CMD_DEVICE_REPLY);
// sleep a while before changing to STA mode
HAL_SleepMs(3000);
Log_i("STA to connect SSID:%s PASSWORD:%s", ssid_json->valuestring, psw_json->valuestring);
PUSH_LOG("SSID:%s|PSW:%s|TOKEN:%s", ssid_json->valuestring, psw_json->valuestring, token_json->valuestring);
ret = HAL_Wifi_StaConnect(ssid_json->valuestring, psw_json->valuestring, 0);
if (ret) {
Log_e("wifi_sta_connect failed: %d", ret);
PUSH_LOG("wifi_sta_connect failed: %d", ret);
app_send_error_log(peer, CUR_ERR, ERR_WIFI_AP_STA, ret);
cJSON_Delete(root);
#if WIFI_PROV_SOFT_AP_ENABLE
set_soft_ap_config_result(WIFI_CONFIG_FAIL);
#endif
return -1;
} else {
Log_d("wifi_sta_connect success");
#if WIFI_PROV_SOFT_AP_ENABLE
set_soft_ap_config_result(WIFI_CONFIG_SUCCESS);
#endif
}
cJSON_Delete(root);

/* return 1 as device alreay switch to STA mode and unable to recv cmd anymore
* 1: Everything OK and we've finished the job */
return 1;
}
return -1;
}

static int _app_handle_recv_data(comm_peer_t *peer, char *pdata, int len)
{
int ret = 0;
Expand Down Expand Up @@ -490,3 +536,8 @@ void qiot_comm_service_stop(void)
{
sg_comm_task_run = false;
}

int qiot_comm_parse_json_token(const char *json)
{
return _parse_json_token(json, NULL);
}

0 comments on commit 51515dd

Please sign in to comment.