support aliyun sdk on TencentOS tiny

sample: examples\aliyun_iotkit_csdk_mqtt
project: board\TencentOS_tiny_EVB_MX_Plus\KEIL\aliyun_iotkit_csdk_mqtt
This commit is contained in:
dcxajichu
2019-10-31 16:36:28 +08:00
parent 30ea36a7ab
commit 8c24d921b0
692 changed files with 199829 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
int awss_report_cloud(void);
/**
** @brief check reset flag in perisistent storage.
**
** @retval -1 : failure
** @retval 0 : sucess
** @note
** check reset flag in perisistent storage, if device failed to report reset message last time, retry it.
**/
int awss_check_reset(void);
/**
** @brief report reset to cloud.
**
** @retval -1 : failure
** @retval 0 : sucess
** @note
** device will save reset flag if device dosen't connect cloud, device will fails to send reset to cloud.
** when connection between device and cloud is ready, device will retry to report reset to cloud.
**/
int awss_report_reset(void);
/**
** @brief stop to report reset to cloud.
**
** @retval -1 : failure
** @retval 0 : sucess
** @note
** just stop report reset to cloud without any touch reset flag in flash.
**/
int awss_stop_report_reset(void);
int awss_bind_deinit(void);
/**
** @brief deinit bind operation.
**
** @retval -1 : failure
** @retval 0 : sucess
** @note
** stop report token to cloud and release coap topic and handler.
**/

View File

@@ -0,0 +1,43 @@
#include "infra_types.h"
#include "infra_defs.h"
#include "wrappers_defs.h"
#ifdef WIFI_PROVISION_ENABLED
#include "iot_import_awss.h"
#endif
int HAL_Snprintf(char *str, const int len, const char *fmt, ...);
int HAL_GetProductKey(char product_key[IOTX_PRODUCT_KEY_LEN]);
int HAL_GetProductSecret(char *product_secret);
int HAL_GetDeviceName(char device_name[IOTX_DEVICE_NAME_LEN]);
int HAL_GetDeviceSecret(char device_secret[IOTX_DEVICE_SECRET_LEN]);
void *HAL_Timer_Create(const char *name, void (*func)(void *), void *user_data);
int HAL_Timer_Stop(void *timer);
int HAL_Timer_Start(void *timer, int ms);
int HAL_Timer_Delete(void *timer);
char *HAL_Wifi_Get_Mac(char mac_str[HAL_MAC_LEN]);
void HAL_Srandom(uint32_t seed);
uint32_t HAL_Random(uint32_t region);
void HAL_Reboot();
void *HAL_MutexCreate(void);
void HAL_SleepMs(uint32_t ms);
void HAL_MutexDestroy(void *mutex);
void HAL_MutexLock(void *mutex);
void HAL_MutexUnlock(void *mutex);
void *HAL_Malloc(uint32_t size);
void HAL_Free(void *ptr);
int HAL_Sys_Net_Is_Ready();
uint64_t HAL_UptimeMs(void);
uint32_t HAL_Wifi_Get_IP(char ip_str[NETWORK_ADDR_LEN], const char *ifname);
#ifdef WIFI_PROVISION_ENABLED
int HAL_Wifi_Get_Ap_Info(char ssid[HAL_MAX_SSID_LEN],char passwd[HAL_MAX_PASSWD_LEN],uint8_t bssid[ETH_ALEN]);
int HAL_Awss_Connect_Ap(
_IN_ uint32_t connection_timeout_ms,
_IN_ char ssid[HAL_MAX_SSID_LEN],
_IN_ char passwd[HAL_MAX_PASSWD_LEN],
_IN_OPT_ enum AWSS_AUTH_TYPE auth,
_IN_OPT_ enum AWSS_ENC_TYPE encry,
_IN_OPT_ uint8_t bssid[ETH_ALEN],
_IN_OPT_ uint8_t channel);
#endif
/* dev reset */
int HAL_Kv_Set(const char *key, const void *val, int len, int sync);
int HAL_Kv_Get(const char *key, void *val, int *buffer_len);

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#ifdef WIFI_PROVISION_ENABLED
#ifndef AWSS_DISABLE_REGISTRAR
#include "awss_enrollee.h"
#endif
#endif
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
int awss_start_bind();
static void *awss_bind_mutex = NULL;
int awss_report_cloud()
{
if (awss_bind_mutex == NULL) {
awss_bind_mutex = HAL_MutexCreate();
if (awss_bind_mutex == NULL) {
return -1;
}
}
HAL_MutexLock(awss_bind_mutex);
awss_cmp_online_init();
HAL_MutexUnlock(awss_bind_mutex);
#ifdef DEVICE_MODEL_ENABLED
if(awss_check_reset()) {
return awss_report_reset_to_cloud();
}
#endif
awss_start_bind();
return 0;
}
int awss_start_bind()
{
static int awss_bind_inited = 0;
if (awss_bind_mutex == NULL) {
awss_bind_mutex = HAL_MutexCreate();
if (awss_bind_mutex == NULL)
return -1;
}
HAL_MutexLock(awss_bind_mutex);
if(awss_bind_inited == 1) {
HAL_MutexUnlock(awss_bind_mutex);
return 0;
}
awss_bind_inited = 1;
awss_report_token();
awss_cmp_local_init(AWSS_LC_INIT_BIND);
#ifndef DEV_BIND_DISABLE_NOTIFY
awss_dev_bind_notify_stop();
awss_dev_bind_notify();
#endif
#ifdef WIFI_PROVISION_ENABLED
#ifndef AWSS_DISABLE_REGISTRAR
awss_registrar_init();
#endif
AWSS_DISP_STATIS();
AWSS_REPORT_STATIS("dev_bind");
#endif
AWSS_DB_DISP_STATIS();
AWSS_DB_REPORT_STATIS("dev_bind");
HAL_MutexUnlock(awss_bind_mutex);
return 0;
}
int awss_bind_deinit()
{
if (awss_bind_mutex) {
HAL_MutexLock(awss_bind_mutex);
}
#ifdef DEVICE_MODEL_ENABLED
awss_stop_report_reset();
#endif
awss_stop_report_token();
awss_cmp_online_deinit();
awss_dev_bind_notify_stop();
awss_cmp_local_deinit(1);
#ifdef WIFI_PROVISION_ENABLED
#ifndef AWSS_DISABLE_REGISTRAR
awss_registrar_deinit();
#endif
AWSS_CLEAR_STATIS();
#endif
AWSS_DB_CLEAR_STATIS();
if (awss_bind_mutex) {
HAL_MutexUnlock(awss_bind_mutex);
HAL_MutexDestroy(awss_bind_mutex);
}
awss_bind_mutex = NULL;
return 0;
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,172 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#ifdef AWSS_SUPPORT_DEV_BIND_STATIS
static struct awss_statis_dev_bind_t g_db_statis = {0};
static uint32_t awss_statis_db_report_id = 0;
static uint32_t awss_statis_db_trace_id = 0;
static void *awss_statis_db_mutex = NULL;
#define DB_CNT g_db_statis.dev_bind_cnt
#define DB_SUC g_db_statis.dev_bind_suc
#define DB_TMEAN g_db_statis.dev_bind_time_mean
#define DB_TMIN g_db_statis.dev_bind_time_min
#define DB_TMAX g_db_statis.dev_bind_time_max
#define DB_START g_db_statis.dev_bind_start
#define DB_END g_db_statis.dev_bind_end
#define AWSS_STATIS_DB_BUF_LEN (512)
int awss_bind_report_statis(const char *module)
{
const char *elem_fmt = "[%s max:%u min:%u mean:%u cnt:%u suc:%u]";
int log_buf_len = AWSS_STATIS_DB_BUF_LEN + strlen(AWSS_STATIS_FMT) + 21;
char statis_topic[TOPIC_LEN_MAX] = {0};
char *log_content = NULL;
char id_str[21] = {0};
char *log_buf = NULL;
int len = 0;
int ret;
log_content = os_zalloc(AWSS_STATIS_DB_BUF_LEN + 1);
if (log_content == NULL) {
goto BIND_STATIS_ERR;
}
log_buf = os_zalloc(log_buf_len + 1);
if (log_buf == NULL) {
goto BIND_STATIS_ERR;
}
if (awss_build_topic(TOPIC_POST_STATIS, statis_topic, TOPIC_LEN_MAX) == NULL) {
awss_err("awss build statis topic fail\n");
goto BIND_STATIS_ERR;
}
if (awss_statis_db_mutex) {
HAL_MutexLock(awss_statis_db_mutex);
}
do {
if (DB_CNT == 0) {
break;
}
len += HAL_Snprintf(log_buf + len, log_buf_len - len, elem_fmt, "SyncToken",
DB_TMAX, DB_TMIN, DB_TMEAN, DB_CNT, DB_SUC);
HAL_Snprintf(log_content, AWSS_STATIS_DB_BUF_LEN, AWSS_STATIS_FMT, (uint32_t)HAL_UptimeMs(), "BIND_TRACE",
module == NULL ? "default" : module, awss_statis_db_trace_id, log_buf);
HAL_Snprintf(id_str, sizeof(id_str), "%u", ++ awss_statis_db_report_id);
awss_build_packet(AWSS_CMP_PKT_TYPE_REQ, id_str, ILOP_VER, METHOD_LOG_POST, log_content, 0,
log_buf, &log_buf_len);
awss_debug("%s\n", log_buf);
ret = awss_cmp_mqtt_send(statis_topic, log_buf, strlen(log_buf), 0);
awss_info("bind report statis %s\n", ret == 0 ? "success" : "fail");
} while (0);
if (awss_statis_db_mutex) {
HAL_MutexUnlock(awss_statis_db_mutex);
}
HAL_Free(log_buf);
HAL_Free(log_content);
return 0;
BIND_STATIS_ERR:
if (log_content) {
HAL_Free(log_content);
}
if (log_buf) {
HAL_Free(log_buf);
}
return -1;
}
void awss_bind_clear_statis()
{
if (awss_statis_db_mutex) {
HAL_MutexLock(awss_statis_db_mutex);
}
memset(&g_db_statis, 0, sizeof(g_db_statis));
awss_statis_db_trace_id = 0;
awss_statis_db_report_id = 0;
if (awss_statis_db_mutex) {
HAL_MutexUnlock(awss_statis_db_mutex);
HAL_MutexDestroy(awss_statis_db_mutex);
}
awss_statis_db_mutex = NULL;
}
void awss_bind_update_statis(int type)
{
uint32_t time = HAL_UptimeMs();
if (awss_statis_db_mutex == NULL) {
awss_statis_db_mutex = HAL_MutexCreate();
if (awss_statis_db_mutex == NULL) {
awss_debug("a-statis am fail\n");
return;
}
}
HAL_MutexLock(awss_statis_db_mutex);
if (type == AWSS_DB_STATIS_START) {
awss_statis_db_trace_id ++;
}
switch (type) {
case AWSS_DB_STATIS_START:
DB_CNT ++;
DB_START = time;
break;
case AWSS_DB_STATIS_SUC:
DB_END = time;
DB_SUC ++;
time = (uint32_t)(DB_END - DB_START);
if (DB_SUC > 0) {
DB_TMEAN = (DB_TMEAN + time) / DB_SUC;
} else {
DB_SUC = 1;
DB_TMEAN = time;
}
if (DB_TMIN == 0 || DB_TMIN > time) {
DB_TMIN = time;
}
if (DB_TMAX == 0 || DB_TMAX < time) {
DB_TMAX = time;
}
break;
default:
break;
}
HAL_MutexUnlock(awss_statis_db_mutex);
}
void awss_bind_disp_statis()
{
if (awss_statis_db_mutex) {
HAL_MutexLock(awss_statis_db_mutex);
}
awss_debug("--------------------------DEV BIND STATIS-----------------------------");
awss_debug("name\t\tmax\tmin\tmean\tcnt\tsuc");
awss_debug("SyncToken \t%u\t%u\t%u\t%u\t%u\t",
DB_TMAX, DB_TMIN, DB_TMEAN, DB_CNT, DB_SUC);
awss_debug("----------------------------------------------------------------------");
if (awss_statis_db_mutex) {
HAL_MutexUnlock(awss_statis_db_mutex);
}
}
#endif

View File

@@ -0,0 +1,45 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_BIND_STATIS_H__
#define __AWSS_BIND_STATIS_H__
#ifndef AWSS_SUPPORT_DEV_BIND_STATIS
#define AWSS_SUPPORT_DEV_BIND_STATIS
#endif
enum {
AWSS_DB_STATIS_START,
AWSS_DB_STATIS_SUC,
};
#ifdef AWSS_SUPPORT_DEV_BIND_STATIS
struct awss_statis_dev_bind_t {
uint32_t dev_bind_cnt; /* the count of token sync */
uint32_t dev_bind_suc; /* the successful count of token sync */
uint32_t dev_bind_time_mean; /* the mean time of token sync */
uint32_t dev_bind_time_max; /* the max time of token sync */
uint32_t dev_bind_time_min; /* the min time of token sync */
uint32_t dev_bind_start; /* the start time to sync token */
uint32_t dev_bind_end; /* the end time of token sync */
}; /* statistics for token sync */
int awss_bind_report_statis(const char *module);
void awss_bind_update_statis(int type);
void awss_bind_clear_statis();
void awss_bind_disp_statis();
#define AWSS_DB_UPDATE_STATIS(type) awss_bind_update_statis(type)
#define AWSS_DB_DISP_STATIS() awss_bind_disp_statis()
#define AWSS_DB_CLEAR_STATIS() awss_bind_clear_statis()
#define AWSS_DB_REPORT_STATIS(m) awss_bind_report_statis(m)
#else
#define AWSS_DB_UPDATE_STATIS(type)
#define AWSS_DB_DISP_STATIS()
#define AWSS_DB_CLEAR_STATIS()
#define AWSS_DB_REPORT_STATIS(m)
#endif
#endif

View File

@@ -0,0 +1,81 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_CMP_H__
#define __AWSS_CMP_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <stdint.h>
/**
* @brief this is a network address structure, including host(ip or host name) and port.
*/
typedef struct {
char host[16]; /**< host ip(dotted-decimal notation) or host name(string) */
uint16_t port; /**< udp port or tcp port */
} platform_netaddr_t;
enum {
AWSS_LC_INIT_ROUTER = 0x01,
AWSS_LC_INIT_PAP = 0x02,
AWSS_LC_INIT_DEV_AP = 0x04,
AWSS_LC_INIT_SUC = 0x08,
AWSS_LC_INIT_BIND = 0x100,
};
struct awss_cmp_couple {
int init_stage;
char *topic;
void *cb;
};
struct coap_session_ctx_t {
void *request;
void *remote;
char is_mcast;
};
int awss_cmp_local_init(int init_stage);
int awss_cmp_local_deinit(int force);
int awss_cmp_online_init();
int awss_cmp_online_deinit();
int awss_token_remain_time();
int awss_token_timeout();
int awss_update_token();
int awss_report_token();
int awss_stop_report_token();
int awss_cmp_coap_cancel_packet(uint16_t msgid);
int awss_cmp_coap_register_cb(char *topic, void *cb);
int awss_cmp_coap_send(void *buf, uint32_t len, void *sa, const char *uri, void *cb, uint16_t *msgid);
int awss_cmp_coap_send_resp(void *buf, uint32_t len, void *sa, const char *uri, void* req, void *cb, uint16_t *msgid, char qos);
int awss_cmp_coap_ob_send(void *buf, uint32_t len, void *sa, const char *uri, void *cb);
int awss_cmp_coap_deinit();
int awss_cmp_mqtt_register_cb(char *topic, void *cb);
int awss_cmp_mqtt_unregister_cb(char *topic);
int awss_cmp_mqtt_send(char *topic, void *pkt, int pkt_len, int qos);
int awss_release_coap_ctx(void *session);
void *awss_cpy_coap_ctx(void *request, void *remote, char mcast);
char *awss_cmp_get_coap_payload(void *request, int *payload_len);
uint8_t awss_cmp_get_coap_code(void *request);
int online_dev_bind_monitor(void *ctx, void *resource, void *remote, void *request);
void awss_enrollee_checkin(void *pcontext, void *pclient, void *msg);
void awss_online_switchap(void *pcontext, void *pclient, void *msg);
void awss_report_enrollee_reply(void *pcontext, void *pclient, void *msg);
void awss_get_cipher_reply(void *pcontext, void *pclient, void *msg);
void awss_report_token_reply(void *pcontext, void *pclient, void *msg);
int awss_cmp_mqtt_get_payload(void *mesg, char **payload, uint32_t *playload_len);
#ifdef __cplusplus
}
#endif
#endif

View File

@@ -0,0 +1,235 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#ifdef WIFI_PROVISION_ENABLED
#if defined(AWSS_SUPPORT_ADHA) || defined(AWSS_SUPPORT_AHA)
#include "awss_wifimgr.h"
#endif
#endif
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
static void *g_coap_ctx = NULL;
int awss_release_coap_ctx(void *session)
{
struct coap_session_ctx_t *ctx = (struct coap_session_ctx_t *)session;
if (ctx == NULL) {
return 0;
}
if (ctx->request) {
void *payload = ((struct CoAPMessage *)ctx->request)->payload;
if (payload) {
HAL_Free(payload);
}
HAL_Free(ctx->request);
}
if (ctx->remote) {
HAL_Free(ctx->remote);
}
HAL_Free(ctx);
return 0;
}
void *awss_cpy_coap_ctx(void *request, void *remote, char mcast)
{
struct coap_session_ctx_t *ctx = os_zalloc(sizeof(struct coap_session_ctx_t));
if (ctx == NULL) {
goto CPY_CTX_FAIL;
}
ctx->request = os_zalloc(sizeof(struct CoAPMessage));
if (ctx->request == NULL) {
goto CPY_CTX_FAIL;
}
memcpy(ctx->request, request, sizeof(struct CoAPMessage));
do {
char *payload = NULL;
int len = 0;
struct CoAPMessage *req = (struct CoAPMessage *)ctx->request;
payload = awss_cmp_get_coap_payload(request, &len);
req->payloadlen = len;
if (payload == NULL) {
break;
}
req->payload = os_zalloc(len + 1);
if (req->payload == NULL) {
goto CPY_CTX_FAIL;
}
memcpy(req->payload, payload, len);
} while (0);
ctx->remote = os_zalloc(sizeof(platform_netaddr_t));
if (ctx->remote == NULL) {
goto CPY_CTX_FAIL;
}
memcpy(ctx->remote, remote, sizeof(platform_netaddr_t));
ctx->is_mcast = mcast;
return ctx;
CPY_CTX_FAIL:
awss_release_coap_ctx(ctx);
return NULL;
}
uint8_t awss_cmp_get_coap_code(void *request)
{
struct CoAPMessage *msg = NULL;
if (request == NULL) {
return 0x60;
}
msg = (struct CoAPMessage *)request;
return msg->header.code;
}
char *awss_cmp_get_coap_payload(void *request, int *payload_len)
{
struct CoAPMessage *msg = (struct CoAPMessage *)request;
if (request == NULL) {
return NULL;
}
msg = (struct CoAPMessage *)request;
if (payload_len) {
*payload_len = msg->payloadlen;
}
return (char *)msg->payload;
}
int awss_cmp_coap_register_cb(char *topic, void *cb)
{
if (g_coap_ctx == NULL) {
g_coap_ctx = (void *)CoAPServer_init();
}
if (g_coap_ctx == NULL) {
return -1;
}
if (topic == NULL) {
return -1;
}
CoAPServer_register(g_coap_ctx, (const char *)topic, (CoAPRecvMsgHandler)cb);
return 0;
}
int awss_cmp_coap_cancel_packet(uint16_t msgid)
{
if (g_coap_ctx == NULL) {
return -1;
}
return CoAPMessageId_cancel(g_coap_ctx, msgid);
}
int awss_cmp_coap_send(void *buf, uint32_t len, void *sa, const char *uri, void *cb, uint16_t *msgid)
{
if (g_coap_ctx == NULL) {
g_coap_ctx = (void *)CoAPServer_init();
} else {
CoAPMessageId_cancel(g_coap_ctx, *msgid);
}
return CoAPServerMultiCast_send(g_coap_ctx, (NetworkAddr *)sa, uri, (uint8_t *)buf,
(uint16_t)len, (CoAPSendMsgHandler)cb, msgid);
}
int awss_cmp_coap_send_resp(void *buf, uint32_t len, void *sa, const char *uri, void *req, void *cb, uint16_t *msgid,
char qos)
{
if (g_coap_ctx == NULL) {
g_coap_ctx = (void *)CoAPServer_init();
}
return CoAPServerResp_send(g_coap_ctx, (NetworkAddr *)sa, (uint8_t *)buf, (uint16_t)len, req, uri,
(CoAPSendMsgHandler)cb, msgid, qos);
}
int awss_cmp_coap_ob_send(void *buf, uint32_t len, void *sa, const char *uri, void *cb)
{
if (g_coap_ctx == NULL) {
g_coap_ctx = (void *)CoAPServer_init();
}
return CoAPObsServer_notify(g_coap_ctx, uri, (uint8_t *)buf, (uint16_t)len, cb);
}
int awss_cmp_coap_deinit()
{
void *coap_ctx = g_coap_ctx;
g_coap_ctx = NULL;
if (coap_ctx) {
CoAPServer_deinit(coap_ctx);
}
return 0;
}
const struct awss_cmp_couple awss_local_couple[] = {
#ifdef WIFI_PROVISION_ENABLED
#if defined(AWSS_SUPPORT_ADHA) || defined(AWSS_SUPPORT_AHA)
{AWSS_LC_INIT_PAP, TOPIC_AWSS_SWITCHAP, wifimgr_process_switch_ap_request},
{AWSS_LC_INIT_PAP, TOPIC_AWSS_WIFILIST, wifimgr_process_get_wifilist_request},
{AWSS_LC_INIT_ROUTER | AWSS_LC_INIT_PAP, TOPIC_AWSS_GETDEVICEINFO_MCAST, wifimgr_process_mcast_get_device_info},
{AWSS_LC_INIT_ROUTER | AWSS_LC_INIT_PAP, TOPIC_AWSS_GETDEVICEINFO_UCAST, wifimgr_process_ucast_get_device_info},
#endif
#ifdef AWSS_SUPPORT_DEV_AP
{AWSS_LC_INIT_DEV_AP, TOPIC_AWSS_DEV_AP_SWITCHAP, wifimgr_process_dev_ap_switchap_request},
#endif
{AWSS_LC_INIT_SUC, TOPIC_AWSS_GET_CONNECTAP_INFO_MCAST, awss_process_mcast_get_connectap_info},
{AWSS_LC_INIT_SUC, TOPIC_AWSS_GET_CONNECTAP_INFO_UCAST, awss_process_ucast_get_connectap_info},
#ifndef AWSS_DISABLE_REGISTRAR
{AWSS_LC_INIT_BIND, TOPIC_NOTIFY, online_dev_bind_monitor},
{AWSS_LC_INIT_BIND, TOPIC_AWSS_CONNECTAP_NOTIFY, online_dev_bind_monitor},
#endif
#endif
{AWSS_LC_INIT_SUC | AWSS_LC_INIT_BIND, TOPIC_GETDEVICEINFO_MCAST, online_mcast_get_device_info},
{AWSS_LC_INIT_SUC | AWSS_LC_INIT_BIND, TOPIC_GETDEVICEINFO_UCAST, online_ucast_get_device_info},
};
int awss_cmp_local_init(int init_stage)
{
char topic[TOPIC_LEN_MAX] = {0};
int i;
for (i = 0; i < sizeof(awss_local_couple) / sizeof(awss_local_couple[0]); i ++) {
if ((awss_local_couple[i].init_stage & init_stage) == 0) {
continue;
}
memset(topic, 0, sizeof(topic));
awss_build_topic(awss_local_couple[i].topic, topic, TOPIC_LEN_MAX);
awss_cmp_coap_register_cb(topic, awss_local_couple[i].cb);
}
return 0;
}
int awss_cmp_local_deinit(int force)
{
if (g_coap_ctx == NULL) {
return 0;
}
#ifdef WIFI_PROVISION_ENABLED
#if defined(AWSS_SUPPORT_ADHA) || defined(AWSS_SUPPORT_AHA)
awss_devinfo_notify_stop();
#endif
awss_suc_notify_stop();
#endif
if (force) {
awss_cmp_coap_deinit();
}
return 0;
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,111 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
static char online_init = 0;
int awss_cmp_mqtt_register_cb(char *topic, void *cb)
{
if (topic == NULL) {
return -1;
}
return IOT_MQTT_Subscribe(NULL, topic, 0, (iotx_mqtt_event_handle_func_fpt)cb, NULL);
}
int awss_cmp_mqtt_unregister_cb(char *topic)
{
return IOT_MQTT_Unsubscribe(NULL, topic);
}
int awss_cmp_mqtt_send(char *topic, void *data, int len, int qos)
{
return IOT_MQTT_Publish_Simple(NULL, topic, qos, data, len); /* IOTX_MQTT_QOS1 or IOTX_MQTT_QOS1 */
}
const struct awss_cmp_couple awss_online_couple[] = {
{-1, TOPIC_MATCH_REPORT_REPLY, awss_report_token_reply},
#ifdef WIFI_PROVISION_ENABLED
#ifndef AWSS_DISABLE_REGISTRAR
{-1, TOPIC_ZC_CHECKIN, awss_enrollee_checkin},
{-1, TOPIC_ZC_ENROLLEE_REPLY, awss_report_enrollee_reply},
{-1, TOPIC_ZC_CIPHER_REPLY, awss_get_cipher_reply},
#endif
{-1, TOPIC_SWITCHAP, awss_online_switchap}
#endif
};
int awss_cmp_online_init()
{
int i;
char topic[TOPIC_LEN_MAX] = {0};
if (online_init) {
return 0;
}
for (i = 0; i < sizeof(awss_online_couple) / sizeof(awss_online_couple[0]); i ++) {
int res = -1;
memset(topic, 0, sizeof(topic));
awss_build_topic(awss_online_couple[i].topic, topic, TOPIC_LEN_MAX);
res = awss_cmp_mqtt_register_cb(topic, awss_online_couple[i].cb);
awss_debug("sub %s %s\n", topic, res < 0 ? "fail" : "success");
}
online_init = 1;
return 0;
}
int awss_cmp_online_deinit()
{
uint8_t i;
char topic[TOPIC_LEN_MAX] = {0};
if (!online_init) {
return 0;
}
awss_dev_bind_notify_stop();
for (i = 0; i < sizeof(awss_online_couple) / sizeof(awss_online_couple[0]); i ++) {
memset(topic, 0, sizeof(topic));
awss_build_topic(awss_online_couple[i].topic, topic, TOPIC_LEN_MAX);
awss_cmp_mqtt_unregister_cb(topic);
}
online_init = 0;
return 0;
}
int awss_cmp_mqtt_get_payload(void *mesg, char **payload, uint32_t *playload_len)
{
iotx_mqtt_event_msg_pt msg;
iotx_mqtt_topic_info_pt ptopic_info;
if (mesg == NULL || payload == NULL || playload_len == NULL) {
return - 1;
}
msg = (iotx_mqtt_event_msg_pt)mesg;
ptopic_info = (iotx_mqtt_topic_info_pt) msg->msg;
switch (msg->event_type) {
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
*playload_len = ptopic_info->payload_len;
*payload = (char *)ptopic_info->payload;
break;
default:
return -1;
}
return 0;
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,29 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
extern int iotx_event_post(int event);
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
int awss_event_post(int event)
{
int ret = 0;
void *cb = NULL;
ret = iotx_event_post(event);
cb = (void *)iotx_event_callback(ITE_AWSS_STATUS);
if (cb) {
ret = ((int (*)(int))cb)(event);
}
return ret;
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,19 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_EVENT_H__
#define __AWSS_EVENT_H__
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
int awss_event_post(int event);
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View File

@@ -0,0 +1,160 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#define AWSS_CONNAP_MONITOR_TIMEOUT_MS (60 * 1000)
static char g_awss_connectap_info_avaliable = 0;
static void *connectap_monitor_timer = NULL;
static void *connectap_monitor_mutex = NULL;
static void awss_release_connectap_monitor()
{
if (connectap_monitor_timer) {
HAL_Timer_Stop(connectap_monitor_timer);
HAL_Timer_Delete(connectap_monitor_timer);
connectap_monitor_timer = NULL;
}
if (connectap_monitor_mutex) {
HAL_MutexUnlock(connectap_monitor_mutex);
HAL_MutexDestroy(connectap_monitor_mutex);
connectap_monitor_mutex = NULL;
}
}
static void awss_connectap_monitor(void *param)
{
if (connectap_monitor_mutex) {
HAL_MutexLock(connectap_monitor_mutex);
}
g_awss_connectap_info_avaliable = 0;
awss_release_connectap_monitor();
}
int awss_stop_connectap_monitor()
{
awss_connectap_monitor(NULL);
return 0;
}
int awss_start_connectap_monitor()
{
if (connectap_monitor_timer) {
awss_debug("connap-m exist");
return 0;
}
if (connectap_monitor_mutex == NULL) {
connectap_monitor_mutex = HAL_MutexCreate();
if (connectap_monitor_mutex == NULL) {
awss_err("connap alloc-m fail");
goto CONNAP_M_FAIL;
}
}
HAL_MutexLock(connectap_monitor_mutex);
connectap_monitor_timer = HAL_Timer_Create("connap_monitor",
awss_connectap_monitor, NULL);
if (connectap_monitor_timer == NULL) {
awss_err("connap alloc-t fail");
goto CONNAP_M_FAIL;
}
g_awss_connectap_info_avaliable = 1;
HAL_Timer_Stop(connectap_monitor_timer);
HAL_Timer_Start(connectap_monitor_timer, AWSS_CONNAP_MONITOR_TIMEOUT_MS);
HAL_MutexUnlock(connectap_monitor_mutex);
return 0;
CONNAP_M_FAIL:
awss_release_connectap_monitor();
return -1;
}
int process_get_device_info(void *ctx, void *resource, void *remote, void *request, char is_mcast, int type)
{
char *buf = NULL;
char *dev_info = NULL;
int len = 0, id_len = 0;
char *msg = NULL, *id = NULL;
const char *topic_fmt = NULL;
char topic[TOPIC_LEN_MAX] = {0};
char req_msg_id[MSG_REQ_ID_LEN] = {0};
buf = os_zalloc(DEV_INFO_LEN_MAX);
if (!buf) {
goto DEV_INFO_ERR;
}
dev_info = os_zalloc(DEV_INFO_LEN_MAX);
if (!dev_info) {
goto DEV_INFO_ERR;
}
msg = awss_cmp_get_coap_payload(request, &len);
id = json_get_value_by_name(msg, len, "id", &id_len, 0);
if (id && id_len < MSG_REQ_ID_LEN) {
memcpy(req_msg_id, id, id_len);
}
if (type == AWSS_NOTIFY_DEV_RAND_SIGN) {
topic_fmt = is_mcast ? TOPIC_AWSS_GETDEVICEINFO_MCAST : TOPIC_AWSS_GETDEVICEINFO_UCAST;
} else if (type == AWSS_NOTIFY_SUCCESS) {
topic_fmt = is_mcast ? TOPIC_AWSS_GET_CONNECTAP_INFO_MCAST : TOPIC_AWSS_GET_CONNECTAP_INFO_UCAST;
} else {
goto DEV_INFO_ERR;
}
awss_build_dev_info(type, buf, DEV_INFO_LEN_MAX);
HAL_Snprintf(dev_info, DEV_INFO_LEN_MAX - 1, "{%s}", buf);
memset(buf, 0x00, DEV_INFO_LEN_MAX);
HAL_Snprintf(buf, DEV_INFO_LEN_MAX - 1, AWSS_ACK_FMT, req_msg_id, 200, dev_info);
HAL_Free(dev_info);
awss_info("tx msg to app: %s", buf);
awss_build_topic(topic_fmt, topic, TOPIC_LEN_MAX);
if (0 != awss_cmp_coap_send_resp(buf, strlen(buf), remote, topic, request, NULL, NULL, 0)) {
awss_err("tx dev info rsp fail.");
}
HAL_Free(buf);
return 0;
DEV_INFO_ERR:
if (buf) {
HAL_Free(buf);
}
if (dev_info) {
HAL_Free(dev_info);
}
return -1;
}
int awss_process_mcast_get_connectap_info(void *ctx, void *resource, void *remote, void *request)
{
if (g_awss_connectap_info_avaliable == 0) {
return -1;
}
return process_get_device_info(ctx, resource, remote, request, 1, AWSS_NOTIFY_SUCCESS);
}
int awss_process_ucast_get_connectap_info(void *ctx, void *resource, void *remote, void *request)
{
if (g_awss_connectap_info_avaliable == 0) {
return -1;
}
return process_get_device_info(ctx, resource, remote, request, 0, AWSS_NOTIFY_SUCCESS);
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,24 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_INFO_H__
#define __AWSS_INFO_H__
#include <stdio.h>
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
int process_get_device_info(void *ctx, void *resource, void *remote, void *request, char is_mcast, int type);
int awss_process_mcast_get_connectap_info(void *ctx, void *resource, void *remote, void *request);
int awss_process_ucast_get_connectap_info(void *ctx, void *resource, void *remote, void *request);
int awss_stop_connectap_monitor();
int awss_start_connectap_monitor();
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View File

@@ -0,0 +1,19 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_LOG_H__
#define __AWSS_LOG_H__
#include "infra_log.h"
#define awss_flow(...) log_flow("awss", __VA_ARGS__)
#define awss_debug(...) log_debug("awss", __VA_ARGS__)
#define awss_info(...) log_info("awss", __VA_ARGS__)
#define awss_warn(...) log_warning("awss", __VA_ARGS__)
#define awss_err(...) log_err("awss", __VA_ARGS__)
#define awss_crit(...) log_crit("awss", __VA_ARGS__)
#define awss_emerg(...) log_emerg("awss", __VA_ARGS__)
#define awss_trace(...) log_crit("awss", __VA_ARGS__)
#endif

View File

@@ -0,0 +1,745 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#define AWSS_CHECK_RESP_TIME (300)
#define AWSS_NOTIFY_PORT (5683)
#define AWSS_NOTIFY_HOST "255.255.255.255"
#define AWSS_DEV_NOTIFY_FMT "{\"id\":\"%u\",\"version\":\"1.0\",\"method\":\"%s\",\"params\":{%s}}"
struct notify_map_t {
uint8_t notify_type;
char *notify_method;
char *notify_topic;
void *cb;
};
static uint8_t g_notify_id;
static char awss_notify_resp[AWSS_NOTIFY_TYPE_MAX] = {0};
static uint16_t g_notify_msg_id[AWSS_NOTIFY_TYPE_MAX] = {0};
#ifdef WIFI_PROVISION_ENABLED
static void *success_notify_timer = NULL;
static void *devinfo_notify_timer = NULL;
static void *success_notify_mutex = NULL;
static void *devinfo_notify_mutex = NULL;
#endif
static void *dev_bind_notify_timer = NULL;
static void *get_devinfo_timer = NULL;
static void *dev_bind_notify_mutex = NULL;
extern char awss_report_token_suc;
extern char awss_report_token_cnt;
static int awss_dev_bind_notify_resp(void *context, int result,
void *userdata, void *remote,
void *message);
#ifdef WIFI_PROVISION_ENABLED
static int awss_devinfo_notify_resp(void *context, int result,
void *userdata, void *remote,
void *message);
static int awss_suc_notify_resp(void *context, int result,
void *userdata, void *remote,
void *message);
int awss_devinfo_notify();
int awss_suc_notify();
#endif
static int awss_notify_response(int type, int result, void *message);
static int awss_process_get_devinfo();
int awss_dev_bind_notify();
static const struct notify_map_t notify_map[] = {
{AWSS_NOTIFY_DEV_BIND_TOKEN, METHOD_DEV_INFO_NOTIFY, TOPIC_NOTIFY, awss_dev_bind_notify_resp},
#ifdef WIFI_PROVISION_ENABLED
{AWSS_NOTIFY_DEV_RAND_SIGN, METHOD_AWSS_DEV_INFO_NOTIFY, TOPIC_AWSS_NOTIFY, awss_devinfo_notify_resp},
{AWSS_NOTIFY_SUCCESS, METHOD_AWSS_CONNECTAP_NOTIFY, TOPIC_AWSS_CONNECTAP_NOTIFY, awss_suc_notify_resp}
#endif
};
/*
* {
* "id": "123",
* "code": 200,
* "data": {}
* }
*/
static int awss_dev_bind_notify_resp(void *context, int result,
void *userdata, void *remote,
void *message)
{
int res = awss_notify_response(AWSS_NOTIFY_DEV_BIND_TOKEN, result, message);
if (res == 1) {
awss_update_token();
#ifdef DEV_BIND_TEST
HAL_Reboot();
#endif
}
return res;
}
#ifdef WIFI_PROVISION_ENABLED
static int awss_devinfo_notify_resp(void *context, int result,
void *userdata, void *remote,
void *message)
{
return awss_notify_response(AWSS_NOTIFY_DEV_RAND_SIGN, result, message);
}
static int awss_suc_notify_resp(void *context, int result,
void *userdata, void *remote,
void *message)
{
return awss_notify_response(AWSS_NOTIFY_SUCCESS, result, message);
}
#endif
static int awss_notify_response(int type, int result, void *message)
{
uint8_t i = 0;
awss_flow("%s, type:%d,result:%u\r\n", __func__, type, result);
if (message == NULL) {
return -1;
}
if (result != 0) {
return 0;
}
if (awss_cmp_get_coap_code(message) >= 0x60) {
return 0;
}
do {
int val = 0;
int len = 0, mlen = 0;
char *payload = NULL, *elem = NULL;
if ((payload = awss_cmp_get_coap_payload(message, &len)) == NULL ||
len > 0x40 || len == 0) {
return 0;
}
awss_debug("payload:%s\r\n", payload);
elem = json_get_value_by_name(payload, len, AWSS_JSON_ID, &mlen, 0);
if (elem == NULL) {
return 0;
}
val = atoi(elem);
if (val != 123 && val > g_notify_id) {
return 0;
}
elem = json_get_value_by_name(payload, len, AWSS_JSON_CODE, &mlen, 0);
if (elem == NULL) {
return 0;
}
val = atoi(elem);
if (val != 200) {
return 0;
}
} while (0);
for (i = 0; i < sizeof(notify_map) / sizeof(notify_map[0]); i ++) {
if (notify_map[i].notify_type != type) {
continue;
}
awss_notify_resp[type] = 1;
break;
}
return awss_notify_resp[type];
}
/**
* @brief alternate bcast addr
*
* @param bcast_addr[out] bcast addr output
* @param mask_level 0 ~ 4(255), 1 ~ 3(255), 2 ~ 2(255), 3 ~ 1(255), others ~ invalid,
*/
static int awss_get_broadcast_addr(platform_netaddr_t *bcast_addr)
{
static uint8_t mask_level = 3;
char ip[20] = {0};
uint8_t level = 0;
if (bcast_addr == NULL) {
return -1;
}
/* update mask_level */
if (++mask_level >= 4) {
mask_level = 0;
}
/* setup port */
bcast_addr->port = AWSS_NOTIFY_PORT;
/* setup ip */
HAL_Wifi_Get_IP(ip, NULL);
if (ip[0] != '\0' && mask_level != 0) {
uint8_t i = 0;
for (i=0; i<strlen(ip); i++) {
bcast_addr->host[i] = ip[i];
if (ip[i] == '.') {
if (++level == mask_level) {
break;
}
}
}
if (mask_level == 1) {
if (i + strlen("255.255.255") < 16) {
memcpy(bcast_addr->host + strlen(bcast_addr->host), "255.255.255", strlen("255.255.255"));
return 0;
}
}
else if (mask_level == 2) {
if (i + strlen("255.255") < 16) {
memcpy(bcast_addr->host + strlen(bcast_addr->host), "255.255", strlen("255.255"));
return 0;
}
}
else if (mask_level == 3) {
if (i + strlen("255") < 16) {
memcpy(bcast_addr->host + strlen(bcast_addr->host), "255", strlen("255"));
return 0;
}
}
}
memcpy(bcast_addr->host, AWSS_NOTIFY_HOST, strlen(AWSS_NOTIFY_HOST));
return 0;
}
int awss_notify_dev_info(int type, int count)
{
char *buf = NULL;
char *dev_info = NULL;
int i;
platform_netaddr_t notify_sa;
memset(&notify_sa, 0, sizeof(notify_sa));
do {
void *cb = NULL;
char *method = NULL, *topic = NULL;
for (i = 0; i < sizeof(notify_map) / sizeof(notify_map[0]); i ++) {
if (notify_map[i].notify_type != type) {
continue;
}
method = notify_map[i].notify_method;
topic = notify_map[i].notify_topic;
cb = notify_map[i].cb;
break;
}
if (method == NULL || topic == NULL) {
awss_err("parametes invalid");
break;
}
buf = os_zalloc(DEV_INFO_LEN_MAX);
dev_info = os_zalloc(DEV_INFO_LEN_MAX);
if (buf == NULL || dev_info == NULL) {
awss_err("alloc mem fail");
break;
}
memset(&notify_sa, 0, sizeof(notify_sa));
awss_get_broadcast_addr(&notify_sa);
awss_info("bcast ip = %s\n", notify_sa.host);
awss_build_dev_info(type, dev_info, DEV_INFO_LEN_MAX);
HAL_Snprintf(buf, DEV_INFO_LEN_MAX - 1, AWSS_DEV_NOTIFY_FMT, ++ g_notify_id, method, dev_info);
awss_info("topic:%s\n", topic);
awss_debug("payload:%s\n", buf);
for (i = 0; i < count; i ++) {
int ret = awss_cmp_coap_send(buf, strlen(buf), &notify_sa, topic, cb, &g_notify_msg_id[type]);
awss_info("send notify %s", ret == 0 ? "success" : "fail");
if (count > 1) {
HAL_SleepMs(200 + 100 * i);
}
if (awss_notify_resp[type]) {
break;
}
}
} while (0);
if (buf) {
HAL_Free(buf);
}
if (dev_info) {
HAL_Free(dev_info);
}
return awss_notify_resp[type];
}
#define AWSS_NOTIFY_CNT_MAX (30)
static void *coap_session_ctx = NULL;
static int awss_process_get_devinfo()
{
char *buf = NULL;
char *dev_info = NULL;
if (awss_report_token_suc == 0) {
awss_debug("try to report token to cloud");
HAL_Timer_Start(get_devinfo_timer, AWSS_CHECK_RESP_TIME);
return 0;
}
if (coap_session_ctx == NULL) {
awss_debug("no get req");
return -1;
}
do {
int len = 0, id_len = 0;
char *msg = NULL, *id = NULL;
char req_msg_id[MSG_REQ_ID_LEN + 1];
char topic[TOPIC_LEN_MAX] = { 0 };
struct coap_session_ctx_t *ctx = (struct coap_session_ctx_t *)coap_session_ctx;
buf = os_zalloc(DEV_INFO_LEN_MAX);
if (buf == NULL) {
goto GET_DEV_INFO_ERR;
}
dev_info = os_zalloc(DEV_INFO_LEN_MAX);
if (dev_info == NULL) {
goto GET_DEV_INFO_ERR;
}
msg = awss_cmp_get_coap_payload(ctx->request, &len);
if (msg == NULL) {
goto GET_DEV_INFO_ERR;
}
id = json_get_value_by_name(msg, len, "id", &id_len, 0);
memset(req_msg_id, 0, sizeof(req_msg_id));
if(id_len > MSG_REQ_ID_LEN) {
goto GET_DEV_INFO_ERR;
}
memcpy(req_msg_id, id, id_len);
awss_build_dev_info(AWSS_NOTIFY_DEV_BIND_TOKEN, buf, DEV_INFO_LEN_MAX);
HAL_Snprintf(dev_info, DEV_INFO_LEN_MAX - 1, "{%s}", buf);
memset(buf, 0x00, DEV_INFO_LEN_MAX);
HAL_Snprintf(buf, DEV_INFO_LEN_MAX - 1, AWSS_ACK_FMT, req_msg_id, 200, dev_info);
HAL_Free(dev_info);
awss_info("sending message to app: %s", buf);
if (ctx->is_mcast) {
awss_build_topic((const char *)TOPIC_GETDEVICEINFO_MCAST, topic, TOPIC_LEN_MAX);
} else {
awss_build_topic((const char *)TOPIC_GETDEVICEINFO_UCAST, topic, TOPIC_LEN_MAX);
}
/*before tx to app, clear token suc flag*/
awss_update_token();
if (0 != awss_cmp_coap_send_resp(buf, strlen(buf), ctx->remote, topic, ctx->request, NULL, NULL, 0)) {
awss_err("sending failed.");
}
HAL_Free(buf);
awss_release_coap_ctx(coap_session_ctx);
coap_session_ctx = NULL;
awss_stop_timer(get_devinfo_timer);
get_devinfo_timer = NULL;
} while (0);
return 0;
GET_DEV_INFO_ERR:
awss_release_coap_ctx(coap_session_ctx);
coap_session_ctx = NULL;
awss_stop_timer(get_devinfo_timer);
get_devinfo_timer = NULL;
if (buf) {
HAL_Free(buf);
}
if (dev_info) {
HAL_Free(dev_info);
}
return -1;
}
static int online_get_device_info(void *ctx, void *resource, void *remote,
void *request, char is_mcast)
{
int timeout = 0;
if(awss_check_reset()) {
return -1;
}
/*
* if the last one is not finished, drop current request
*/
if (coap_session_ctx != NULL) {
awss_debug("no req");
return -1;
}
/*
* copy coap session context
*/
coap_session_ctx = awss_cpy_coap_ctx(request, remote, is_mcast);
if (coap_session_ctx == NULL) {
awss_err("cpy req ctx fail");
return -1;
}
timeout = awss_token_timeout();
if (timeout) {
produce_random(aes_random, sizeof(aes_random));
awss_report_token();
}
if (get_devinfo_timer == NULL) {
get_devinfo_timer = HAL_Timer_Create("get_devinfo", (void (*)(void *))awss_process_get_devinfo, NULL);
}
HAL_Timer_Stop(get_devinfo_timer);
HAL_Timer_Start(get_devinfo_timer, timeout ? AWSS_CHECK_RESP_TIME : 1);
return 0;
}
int online_mcast_get_device_info(void *ctx, void *resource, void *remote, void *request)
{
return online_get_device_info(ctx, resource, remote, request, 1);
}
int online_ucast_get_device_info(void *ctx, void *resource, void *remote, void *request)
{
return online_get_device_info(ctx, resource, remote, request, 0);
}
static int dev_bind_interval = 0;
static char dev_bind_cnt = 0;
static int __awss_dev_bind_notify()
{
/*
* wait for token is sent to cloud and rx reply from cloud
*/
if (awss_report_token_suc == 0) {
if (dev_bind_notify_timer == NULL) {
dev_bind_notify_timer = HAL_Timer_Create("dev_bind", (void (*)(void *))__awss_dev_bind_notify, NULL);
}
HAL_Timer_Stop(dev_bind_notify_timer);
HAL_Timer_Start(dev_bind_notify_timer, AWSS_CHECK_RESP_TIME);
return 0;
}
if (dev_bind_notify_mutex == NULL) {
dev_bind_notify_mutex = HAL_MutexCreate();
if (dev_bind_notify_mutex == NULL) {
return -1;
}
}
if (dev_bind_cnt == 0) {
awss_event_post(IOTX_AWSS_BIND_NOTIFY);
}
HAL_MutexLock(dev_bind_notify_mutex);
do {
uint8_t i = 0;
if (awss_notify_resp[AWSS_NOTIFY_DEV_BIND_TOKEN] != 0) {
break;
}
for (i = 0; i < RANDOM_MAX_LEN; i ++)
if (aes_random[i] != 0x00) {
break;
}
if (i >= RANDOM_MAX_LEN) {
produce_random(aes_random, sizeof(aes_random));
}
if (awss_token_timeout() == 0) {
awss_notify_dev_info(AWSS_NOTIFY_DEV_BIND_TOKEN, 1);
dev_bind_interval += 100;
dev_bind_cnt ++;
}
#ifdef DEV_BIND_TEST
if (dev_bind_cnt > 3) {
HAL_Reboot();
}
#endif
if (dev_bind_cnt < AWSS_NOTIFY_CNT_MAX &&
awss_notify_resp[AWSS_NOTIFY_DEV_BIND_TOKEN] == 0) {
if (dev_bind_notify_timer == NULL) {
dev_bind_notify_timer = HAL_Timer_Create("dev_bind", (void (*)(void *))awss_dev_bind_notify, NULL);
}
HAL_Timer_Stop(dev_bind_notify_timer);
HAL_Timer_Start(dev_bind_notify_timer, dev_bind_interval);
HAL_MutexUnlock(dev_bind_notify_mutex);
return 0;
}
} while (0);
awss_cmp_coap_cancel_packet(g_notify_msg_id[AWSS_NOTIFY_DEV_BIND_TOKEN]);
g_notify_msg_id[AWSS_NOTIFY_DEV_BIND_TOKEN] = 0;
awss_notify_resp[AWSS_NOTIFY_DEV_BIND_TOKEN] = 0;
dev_bind_interval = 0;
dev_bind_cnt = 0;
if (dev_bind_notify_timer) {
awss_stop_timer(dev_bind_notify_timer);
dev_bind_notify_timer = NULL;
}
if (dev_bind_notify_mutex) {
HAL_MutexUnlock(dev_bind_notify_mutex);
HAL_MutexDestroy(dev_bind_notify_mutex);
dev_bind_notify_mutex = NULL;
}
return 1;
}
int awss_dev_bind_notify()
{
dev_bind_cnt = 0;
dev_bind_interval = 0;
awss_notify_resp[AWSS_NOTIFY_DEV_BIND_TOKEN] = 0;
return __awss_dev_bind_notify();
}
int awss_dev_bind_notify_stop()
{
if (dev_bind_notify_mutex) {
HAL_MutexLock(dev_bind_notify_mutex);
}
do {
awss_notify_resp[AWSS_NOTIFY_DEV_BIND_TOKEN] = 1;
dev_bind_cnt = AWSS_NOTIFY_CNT_MAX;
if (dev_bind_notify_timer == NULL) {
break;
}
awss_stop_timer(dev_bind_notify_timer);
dev_bind_notify_timer = NULL;
} while (0);
awss_cmp_coap_cancel_packet(g_notify_msg_id[AWSS_NOTIFY_DEV_BIND_TOKEN]);
g_notify_msg_id[AWSS_NOTIFY_DEV_BIND_TOKEN] = 0;
if (dev_bind_notify_mutex) {
HAL_MutexUnlock(dev_bind_notify_mutex);
HAL_MutexDestroy(dev_bind_notify_mutex);
dev_bind_notify_mutex = NULL;
}
return 0;
}
#ifdef WIFI_PROVISION_ENABLED
static int suc_interval = 0;
static char suc_cnt = 0;
static int __awss_suc_notify()
{
awss_debug("resp:%d\r\n", awss_notify_resp[AWSS_NOTIFY_SUCCESS]);
if (success_notify_mutex == NULL) {
success_notify_mutex = HAL_MutexCreate();
if (success_notify_mutex == NULL) {
return -1;
}
}
if (suc_cnt == 0) {
awss_event_post(IOTX_AWSS_SUC_NOTIFY);
}
HAL_MutexLock(success_notify_mutex);
do {
if (awss_notify_resp[AWSS_NOTIFY_SUCCESS] != 0) {
break;
}
awss_notify_dev_info(AWSS_NOTIFY_SUCCESS, 1);
suc_interval += 100;
if (suc_cnt ++ < AWSS_NOTIFY_CNT_MAX &&
awss_notify_resp[AWSS_NOTIFY_SUCCESS] == 0) {
if (success_notify_timer == NULL) {
success_notify_timer = HAL_Timer_Create("awss_suc", (void (*)(void *))__awss_suc_notify, NULL);
}
HAL_Timer_Stop(success_notify_timer);
HAL_Timer_Start(success_notify_timer, suc_interval);
HAL_MutexUnlock(success_notify_mutex);
return 0;
}
} while (0);
awss_cmp_coap_cancel_packet(g_notify_msg_id[AWSS_NOTIFY_SUCCESS]);
g_notify_msg_id[AWSS_NOTIFY_SUCCESS] = 0;
awss_notify_resp[AWSS_NOTIFY_SUCCESS] = 0;
suc_interval = 0;
suc_cnt = 0;
if (success_notify_timer) {
awss_stop_timer(success_notify_timer);
success_notify_timer = NULL;
}
if (success_notify_mutex) {
HAL_MutexUnlock(success_notify_mutex);
HAL_MutexDestroy(success_notify_mutex);
success_notify_mutex = NULL;
}
return 1;
}
int awss_suc_notify()
{
suc_cnt = 0;
suc_interval = 0;
awss_notify_resp[AWSS_NOTIFY_SUCCESS] = 0;
return __awss_suc_notify();
}
int awss_suc_notify_stop()
{
if (success_notify_mutex) {
HAL_MutexLock(success_notify_mutex);
}
do {
awss_notify_resp[AWSS_NOTIFY_SUCCESS] = 1;
suc_cnt = AWSS_NOTIFY_CNT_MAX;
if (success_notify_timer == NULL) {
break;
}
awss_stop_timer(success_notify_timer);
success_notify_timer = NULL;
} while (0);
awss_cmp_coap_cancel_packet(g_notify_msg_id[AWSS_NOTIFY_SUCCESS]);
g_notify_msg_id[AWSS_NOTIFY_SUCCESS] = 0;
if (success_notify_mutex) {
HAL_MutexUnlock(success_notify_mutex);
HAL_MutexDestroy(success_notify_mutex);
success_notify_mutex = NULL;
}
return 0;
}
static int devinfo_interval = 0;
static char devinfo_cnt = 0;
static int __awss_devinfo_notify()
{
if (devinfo_notify_mutex == NULL) {
devinfo_notify_mutex = HAL_MutexCreate();
if (devinfo_notify_mutex == NULL) {
return -1;
}
}
HAL_MutexLock(devinfo_notify_mutex);
do {
if (awss_notify_resp[AWSS_NOTIFY_DEV_RAND_SIGN] != 0) {
break;
}
awss_notify_dev_info(AWSS_NOTIFY_DEV_RAND_SIGN, 1);
devinfo_interval += 100;
if (devinfo_cnt ++ < AWSS_NOTIFY_CNT_MAX &&
awss_notify_resp[AWSS_NOTIFY_DEV_RAND_SIGN] == 0) {
if (devinfo_notify_timer == NULL) {
devinfo_notify_timer = HAL_Timer_Create("devinfo", (void (*)(void *))__awss_devinfo_notify, NULL);
}
HAL_Timer_Stop(devinfo_notify_timer);
HAL_Timer_Start(devinfo_notify_timer, devinfo_interval);
HAL_MutexUnlock(devinfo_notify_mutex);
return 0;
}
} while (0);
awss_cmp_coap_cancel_packet(g_notify_msg_id[AWSS_NOTIFY_DEV_RAND_SIGN]);
g_notify_msg_id[AWSS_NOTIFY_DEV_RAND_SIGN] = 0;
awss_notify_resp[AWSS_NOTIFY_DEV_RAND_SIGN] = 0;
devinfo_interval = 0;
devinfo_cnt = 0;
if (devinfo_notify_timer) {
awss_stop_timer(devinfo_notify_timer);
devinfo_notify_timer = NULL;
}
if (devinfo_notify_mutex) {
HAL_MutexUnlock(devinfo_notify_mutex);
HAL_MutexDestroy(devinfo_notify_mutex);
devinfo_notify_mutex = NULL;
}
return 1;
}
int awss_devinfo_notify()
{
devinfo_cnt = 0;
devinfo_interval = 0;
awss_notify_resp[AWSS_NOTIFY_DEV_RAND_SIGN] = 0;
return __awss_devinfo_notify();
}
int awss_devinfo_notify_stop()
{
if (devinfo_notify_mutex) {
HAL_MutexLock(devinfo_notify_mutex);
}
do {
awss_notify_resp[AWSS_NOTIFY_DEV_RAND_SIGN] = 1;
devinfo_cnt = AWSS_NOTIFY_CNT_MAX;
if (devinfo_notify_timer == NULL) {
break;
}
awss_stop_timer(devinfo_notify_timer);
devinfo_notify_timer = NULL;
} while (0);
awss_cmp_coap_cancel_packet(g_notify_msg_id[AWSS_NOTIFY_DEV_RAND_SIGN]);
g_notify_msg_id[AWSS_NOTIFY_DEV_RAND_SIGN] = 0;
if (devinfo_notify_mutex) {
HAL_MutexUnlock(devinfo_notify_mutex);
HAL_MutexDestroy(devinfo_notify_mutex);
devinfo_notify_mutex = NULL;
}
return 0;
}
#endif
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,38 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_NOTIFY_H__
#define __AWSS_NOTIFY_H__
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
enum {
AWSS_NOTIFY_DEV_INFO = 0,
AWSS_NOTIFY_DEV_BIND_TOKEN,
AWSS_NOTIFY_DEV_RAND_SIGN,
AWSS_NOTIFY_SUCCESS,
AWSS_NOTIFY_TYPE_MAX,
};
#ifdef WIFI_PROVISION_ENABLED
int awss_suc_notify();
int awss_devinfo_notify();
int awss_suc_notify_stop();
int awss_devinfo_notify_stop();
#endif
int awss_dev_bind_notify();
int awss_dev_bind_notify_stop();
int awss_notify_dev_info(int type, int count);
int online_mcast_get_device_info(void *ctx, void *resource, void *remote, void *request);
int online_ucast_get_device_info(void *ctx, void *resource, void *remote, void *request);
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View File

@@ -0,0 +1,208 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#define AWSS_DEV_RAND_SIGN_FMT ",\"random\":\"%s\",\"signMethod\":%d,\"sign\":\"%s\""
#define AWSS_DEV_BIND_TOKEN_FMT ",\"token\":\"%s\",\"remainTime\":%d,\"type\":%d"
#define AWSS_SUCCESS_FMT ",\"type\":%d"
#define AWSS_DEV_INFO_FMT "\"awssVer\":%s,\"productKey\":\"%s\",\"deviceName\":\"%s\",\"mac\":\"%s\",\"ip\":\"%s\",\"cipherType\":%d"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
static inline int bind_get_encrypt_type()
{
return 3;
}
static void *awss_get_dev_info(void *dev_info, int len)
{
char dev_name[IOTX_DEVICE_NAME_LEN + 1] = {0};
char mac_str[HAL_MAC_LEN + 1] = {0};
char pk[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char ip_str[OS_IP_LEN + 1] = {0};
if (dev_info == NULL || len <= 0) {
return NULL;
}
HAL_GetProductKey(pk);
HAL_GetDeviceName(dev_name);
os_wifi_get_mac_str(mac_str);
HAL_Wifi_Get_IP(ip_str, NULL);
#if 0
awss_dict_crypt(NOTIFY_ENCODE_TABLE, (uint8_t *)pk, strlen(pk));
awss_dict_crypt(NOTIFY_ENCODE_TABLE, (uint8_t *)dev_name, strlen(dev_name));
#endif
HAL_Snprintf(dev_info, len - 1, AWSS_DEV_INFO_FMT, AWSS_VER, pk, dev_name, mac_str, ip_str,
bind_get_encrypt_type());
return dev_info;
}
void *awss_build_dev_info(int type, void *dev_info, int info_len)
{
int len = 0;
char *buf = NULL;
if (dev_info == NULL || info_len <= 0) {
return NULL;
}
buf = os_zalloc(DEV_INFO_LEN_MAX);
if (buf == NULL) {
return NULL;
}
len += HAL_Snprintf((char *)dev_info + len, info_len - len - 1, "%s", (char *)awss_get_dev_info(buf, DEV_INFO_LEN_MAX));
HAL_Free(buf);
buf = NULL;
switch (type) {
case AWSS_NOTIFY_DEV_BIND_TOKEN: {
char rand_str[(RANDOM_MAX_LEN << 1) + 1] = {0};
utils_hex_to_str(aes_random, RANDOM_MAX_LEN, rand_str, sizeof(rand_str));
len += HAL_Snprintf((char *)dev_info + len, info_len - len - 1, AWSS_DEV_BIND_TOKEN_FMT, rand_str,
awss_token_remain_time(), 0);
break;
}
#ifdef WIFI_PROVISION_ENABLED
case AWSS_NOTIFY_SUCCESS: {
len += HAL_Snprintf((char *)dev_info + len, info_len - len - 1, AWSS_SUCCESS_FMT, 0);
break;
}
case AWSS_NOTIFY_DEV_RAND_SIGN: {
char sign_str[DEV_SIGN_SIZE * 2 + 1] = {0};
char rand_str[(RANDOM_MAX_LEN << 1) + 1] = {0};
{
int txt_len = 80;
char txt[80] = {0};
char key[IOTX_DEVICE_SECRET_LEN + 1] = {0};
uint8_t sign[DEV_SIGN_SIZE + 1] = {0};
if (bind_get_encrypt_type() == 3) { /* aes-key per product */
HAL_GetProductSecret(key);
} else { /* aes-key per device */
HAL_GetDeviceSecret(key);
}
awss_build_sign_src(txt, &txt_len);
produce_signature(sign, (uint8_t *)txt, txt_len, key);
utils_hex_to_str(sign, DEV_SIGN_SIZE, sign_str, sizeof(sign_str));
}
utils_hex_to_str(aes_random, RANDOM_MAX_LEN, rand_str, sizeof(rand_str));
len += HAL_Snprintf((char *)dev_info + len, info_len - len - 1, AWSS_DEV_RAND_SIGN_FMT, rand_str, 0, sign_str);
break;
}
#endif
default:
break;
}
return dev_info;
}
#ifdef WIFI_PROVISION_ENABLED
char *awss_build_sign_src(char *sign_src, int *sign_src_len)
{
char *pk = NULL, *dev_name = NULL;
int dev_name_len, pk_len, text_len;
if (sign_src == NULL || sign_src_len == NULL) {
goto build_sign_src_err;
}
pk = os_zalloc(IOTX_PRODUCT_KEY_LEN + 1);
dev_name = os_zalloc(IOTX_DEVICE_NAME_LEN + 1);
if (pk == NULL || dev_name == NULL) {
goto build_sign_src_err;
}
HAL_GetProductKey(pk);
HAL_GetDeviceName(dev_name);
pk_len = strlen(pk);
dev_name_len = strlen(dev_name);
text_len = RANDOM_MAX_LEN + dev_name_len + pk_len;
if (*sign_src_len < text_len) {
goto build_sign_src_err;
}
*sign_src_len = text_len;
memcpy(sign_src, aes_random, RANDOM_MAX_LEN);
memcpy(sign_src + RANDOM_MAX_LEN, dev_name, dev_name_len);
memcpy(sign_src + RANDOM_MAX_LEN + dev_name_len, pk, pk_len);
HAL_Free(pk);
HAL_Free(dev_name);
return sign_src;
build_sign_src_err:
if (pk) {
HAL_Free(pk);
}
if (dev_name) {
HAL_Free(dev_name);
}
return NULL;
}
#endif
const char *awss_build_topic(const char *topic_fmt, char *topic, uint32_t tlen)
{
char pk[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char dev_name[IOTX_DEVICE_NAME_LEN + 1] = {0};
if (topic == NULL || topic_fmt == NULL || tlen == 0) {
return NULL;
}
HAL_GetProductKey(pk);
HAL_GetDeviceName(dev_name);
HAL_Snprintf(topic, tlen - 1, topic_fmt, pk, dev_name);
return topic;
}
int awss_build_packet(int type, void *id, void *ver, void *method, void *data, int code, void *packet, int *packet_len)
{
int len;
if (packet_len == NULL || data == NULL || packet == NULL) {
return -1;
}
len = *packet_len;
if (len <= 0) {
return -1;
}
if (type == AWSS_CMP_PKT_TYPE_REQ) {
if (ver == NULL || method == NULL) {
return -1;
}
len = HAL_Snprintf(packet, len - 1, AWSS_REQ_FMT, (char *)id, (char *)ver, (char *)method, (char *)data);
return 0;
} else if (type == AWSS_CMP_PKT_TYPE_RSP) {
len = HAL_Snprintf(packet, len - 1, AWSS_ACK_FMT, (char *)id, code, (char *)data);
return 0;
}
return -1;
}
void produce_random(uint8_t *random, uint32_t len)
{
int i = 0;
int time = HAL_UptimeMs();
HAL_Srandom(time);
for (i = 0; i < len; i ++) {
random[i] = HAL_Random(0xFF);
}
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,70 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_PACKET_H__
#define __AWSS_PACKET_H__
#include "infra_sha1.h"
#define MSG_REQ_ID_LEN (16)
#define TOPIC_LEN_MAX (128)
#define DEV_INFO_LEN_MAX (512)
#define DEV_SIGN_SIZE (SHA1_DIGEST_SIZE)
#define ILOP_VER "1.0"
#define AWSS_VER "{\"smartconfig\":\"2.0\",\"zconfig\":\"2.0\",\"router\":\"2.0\",\"ap\":\"2.0\",\"softap\":\"2.0\"}"
#define TOPIC_AWSS_WIFILIST "/sys/%s/%s/awss/event/wifilist/get"
#define TOPIC_AWSS_SWITCHAP "/sys/%s/%s/awss/device/switchap"
#define TOPIC_AWSS_GETDEVICEINFO_MCAST "/sys/awss/device/info/get"
#define TOPIC_AWSS_GETDEVICEINFO_UCAST "/sys/%s/%s/awss/device/info/get"
#define TOPIC_AWSS_GET_CONNECTAP_INFO_MCAST "/sys/awss/device/connectap/info/get"
#define TOPIC_AWSS_GET_CONNECTAP_INFO_UCAST "/sys/%s/%s/awss/device/connectap/info/get"
#define TOPIC_GETDEVICEINFO_MCAST "/sys/device/info/get"
#define TOPIC_GETDEVICEINFO_UCAST "/sys/%s/%s/device/info/get"
#define TOPIC_POST_STATIS "/sys/%s/%s/thing/log/post"
#define TOPIC_AWSS_NOTIFY "/sys/awss/device/info/notify"
#define TOPIC_AWSS_CONNECTAP_NOTIFY "/sys/awss/event/connectap/notify"
#define TOPIC_NOTIFY "/sys/device/info/notify"
#define TOPIC_SWITCHAP "/sys/%s/%s/thing/awss/device/switchap"
#define TOPIC_SWITCHAP_REPLY "/sys/%s/%s/thing/awss/device/switchap_reply"
#define TOPIC_ZC_ENROLLEE "/sys/%s/%s/thing/awss/enrollee/found"
#define TOPIC_ZC_ENROLLEE_REPLY "/sys/%s/%s/thing/awss/enrollee/found_reply"
#define TOPIC_ZC_CHECKIN "/sys/%s/%s/thing/awss/enrollee/checkin"
#define TOPIC_ZC_CHECKIN_REPLY "/sys/%s/%s/thing/awss/enrollee/checkin_reply"
#define TOPIC_ZC_CIPHER "/sys/%s/%s/thing/cipher/get"
#define TOPIC_ZC_CIPHER_REPLY "/sys/%s/%s/thing/cipher/get_reply"
#define TOPIC_MATCH_REPORT "/sys/%s/%s/thing/awss/enrollee/match"
#define TOPIC_MATCH_REPORT_REPLY "/sys/%s/%s/thing/awss/enrollee/match_reply"
#define TOPIC_AWSS_DEV_AP_SWITCHAP "/sys/awss/device/softap/switchap"
#define METHOD_DEV_INFO_NOTIFY "device.info.notify"
#define METHOD_AWSS_DEV_INFO_NOTIFY "awss.device.info.notify"
#define METHOD_AWSS_CONNECTAP_NOTIFY "awss.event.connectap.notify"
#define METHOD_AWSS_DEV_AP_SWITCHAP "awss.device.softap.switchap"
#define METHOD_EVENT_ZC_SWITCHAP "thing.awss.device.switchap"
#define METHOD_EVENT_ZC_ENROLLEE "thing.awss.enrollee.found"
#define METHOD_EVENT_ZC_CHECKIN "thing.awss.enrollee.checkin"
#define METHOD_EVENT_ZC_CIPHER "thing.cipher.get"
#define METHOD_MATCH_REPORT "thing.awss.enrollee.match"
#define METHOD_LOG_POST "things.log.post"
#define AWSS_ACK_FMT "{\"id\":%s,\"code\":%d,\"data\":%s}"
#define AWSS_REQ_FMT "{\"id\":%s,\"version\":\"%s\",\"method\":\"%s\",\"params\":%s}"
#define AWSS_JSON_PARAM "params"
#define AWSS_JSON_CODE "code"
#define AWSS_JSON_ID "id"
#define AWSS_STATIS_FMT "{\"template\":\"timestamp logLevel module traceContext logContent\",\"contents\":[\"%u %s %s %u %s\"]}"
enum {
AWSS_CMP_PKT_TYPE_REQ = 1,
AWSS_CMP_PKT_TYPE_RSP,
};
void produce_random(uint8_t *random, uint32_t len);
char *awss_build_sign_src(char *sign_src, int *sign_src_len);
void *awss_build_dev_info(int type, void *dev_info, int info_len);
const char *awss_build_topic(const char *topic_fmt, char *topic, uint32_t tlen);
int awss_build_packet(int type, void *id, void *ver, void *method, void *data, int code, void *pkt, int *pkt_len);
#endif

View File

@@ -0,0 +1,379 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#define AWSS_REPORT_LEN_MAX (256)
#define AWSS_TOKEN_TIMEOUT_MS (45 * 1000)
#define MATCH_MONITOR_TIMEOUT_MS (10 * 1000)
#define MATCH_REPORT_CNT_MAX (2)
volatile char awss_report_token_suc = 0;
volatile char awss_report_token_cnt = 0;
static char awss_report_id = 0;
#ifdef WIFI_PROVISION_ENABLED
static uint8_t switchap_bssid[ETH_ALEN] = {0};
static char switchap_ssid[OS_MAX_SSID_LEN] = {0};
static char switchap_passwd[OS_MAX_PASSWD_LEN] = {0};
static void *switchap_timer = NULL;
#endif
static uint32_t awss_report_token_time = 0;
static void *report_token_timer = NULL;
static int awss_report_token_to_cloud();
#ifdef WIFI_PROVISION_ENABLED
static int awss_switch_ap_online();
static int awss_reboot_system();
#endif
int awss_token_remain_time()
{
int remain = 0;
uint32_t cur = os_get_time_ms();
uint32_t diff = (uint32_t)(cur - awss_report_token_time);
if (awss_report_token_suc == 0) {
return remain;
}
if (diff < AWSS_TOKEN_TIMEOUT_MS) {
remain = AWSS_TOKEN_TIMEOUT_MS - diff;
}
return remain;
}
int awss_update_token()
{
awss_report_token_time = 0;
awss_report_token_cnt = 0;
awss_report_token_suc = 0;
produce_random(aes_random, sizeof(aes_random));
if (report_token_timer == NULL) {
report_token_timer = HAL_Timer_Create("rp_token", (void (*)(void *))awss_report_token_to_cloud, NULL);
}
HAL_Timer_Stop(report_token_timer);
HAL_Timer_Start(report_token_timer, 10);
awss_info("update token");
return 0;
}
int awss_token_timeout()
{
uint32_t cur;
if (awss_report_token_time == 0) {
return 1;
}
cur = os_get_time_ms();
if ((uint32_t)(cur - awss_report_token_time) > AWSS_TOKEN_TIMEOUT_MS) {
return 1;
}
return 0;
}
void awss_report_token_reply(void *pcontext, void *pclient, void *msg)
{
int ret, len;
char *payload;
char *id = NULL;
char reply_id = 0;
uint32_t payload_len;
ret = awss_cmp_mqtt_get_payload(msg, &payload, &payload_len);
if (ret != 0 || payload == NULL || payload_len == 0) {
return;
}
id = json_get_value_by_name(payload, payload_len, AWSS_JSON_ID, &len, NULL);
if (id == NULL) {
return;
}
reply_id = atoi(id);
if (reply_id + 1 < awss_report_id) {
return;
}
awss_info("%s\r\n", __func__);
awss_report_token_suc = 1;
awss_stop_timer(report_token_timer);
report_token_timer = NULL;
AWSS_DB_UPDATE_STATIS(AWSS_DB_STATIS_SUC);
AWSS_DB_DISP_STATIS();
return;
}
#ifdef WIFI_PROVISION_ENABLED
void awss_online_switchap(void *pcontext, void *pclient, void *msg)
{
#define SWITCHAP_RSP_LEN (64)
#define AWSS_BSSID_STR_LEN (17)
#define AWSS_SSID "ssid"
#define AWSS_PASSWD "passwd"
#define AWSS_BSSID "bssid"
#define AWSS_SWITCH_MODE "switchMode"
#define AWSS_TIMEOUT "timeout"
int len = 0, timeout = 0;
char *packet = NULL, *awss_info = NULL, *elem = NULL;
int packet_len = SWITCHAP_RSP_LEN, awss_info_len = 0;
uint32_t payload_len;
char *payload;
int ret;
ret = awss_cmp_mqtt_get_payload(msg, &payload, &payload_len);
if (ret != 0) {
goto ONLINE_SWITCHAP_FAIL;
}
if (payload == NULL || payload_len == 0) {
goto ONLINE_SWITCHAP_FAIL;
}
awss_debug("online switchap len:%u, payload:%s\r\n", payload_len, payload);
packet = os_zalloc(packet_len + 1);
if (packet == NULL) {
goto ONLINE_SWITCHAP_FAIL;
}
awss_info = json_get_value_by_name(payload, payload_len, AWSS_JSON_PARAM, &awss_info_len, NULL);
if (awss_info == NULL || awss_info_len == 0) {
goto ONLINE_SWITCHAP_FAIL;
}
/*
* get SSID , PASSWD, BSSID of router
*/
elem = json_get_value_by_name(awss_info, awss_info_len, AWSS_SSID, &len, NULL);
if (elem == NULL || len <= 0 || len >= OS_MAX_SSID_LEN) {
goto ONLINE_SWITCHAP_FAIL;
}
memset(switchap_ssid, 0, sizeof(switchap_ssid));
memcpy(switchap_ssid, elem, len);
len = 0;
elem = json_get_value_by_name(awss_info, awss_info_len, AWSS_PASSWD, &len, NULL);
if (elem == NULL || len <= 0 || len >= OS_MAX_PASSWD_LEN) {
goto ONLINE_SWITCHAP_FAIL;
}
memset(switchap_passwd, 0, sizeof(switchap_passwd));
memcpy(switchap_passwd, elem, len);
len = 0;
memset(switchap_bssid, 0, sizeof(switchap_bssid));
elem = json_get_value_by_name(awss_info, awss_info_len, AWSS_BSSID, &len, NULL);
if (elem != NULL && len == AWSS_BSSID_STR_LEN) {
uint8_t i = 0;
char *bssid_str = elem;
/* convert bssid string to bssid value */
while (i < OS_ETH_ALEN) {
switchap_bssid[i ++] = (uint8_t)strtol(bssid_str, &bssid_str, 16);
++ bssid_str;
/*
* fix the format of bssid string is not legal.
*/
if ((uint32_t)((unsigned long)bssid_str - (unsigned long)elem) > AWSS_BSSID_STR_LEN) {
memset(switchap_bssid, 0, sizeof(switchap_bssid));
break;
}
}
}
len = 0;
elem = json_get_value_by_name(awss_info, awss_info_len, AWSS_SWITCH_MODE, &len, NULL);
if (elem != NULL && (elem[0] == '0' || elem[0] == 0)) {
elem = json_get_value_by_name(awss_info, awss_info_len, AWSS_TIMEOUT, &len, NULL);
if (elem) {
timeout = (int)strtol(elem, &elem, 16);
}
}
do {
/* reduce stack used */
char *id = NULL;
char id_str[MSG_REQ_ID_LEN] = {0};
id = json_get_value_by_name(payload, payload_len, AWSS_JSON_ID, &len, NULL);
memcpy(id_str, id, len > MSG_REQ_ID_LEN - 1 ? MSG_REQ_ID_LEN - 1 : len);
awss_build_packet(AWSS_CMP_PKT_TYPE_RSP, id_str, ILOP_VER, METHOD_EVENT_ZC_SWITCHAP, "{}", 200, packet, &packet_len);
} while (0);
do {
char reply[TOPIC_LEN_MAX] = {0};
awss_build_topic(TOPIC_SWITCHAP_REPLY, reply, TOPIC_LEN_MAX);
awss_cmp_mqtt_send(reply, packet, packet_len, 1);
HAL_Free(packet);
} while (0);
/*
* make sure the response would been received
*/
if (timeout < 1000) {
timeout = 1000;
}
do {
uint8_t bssid[ETH_ALEN] = {0};
char ssid[OS_MAX_SSID_LEN + 1] = {0}, passwd[OS_MAX_PASSWD_LEN + 1] = {0};
HAL_Wifi_Get_Ap_Info(ssid, passwd, bssid);
/*
* switch ap when destination ap is differenct from current ap
*/
if (strncmp(ssid, switchap_ssid, sizeof(ssid)) ||
memcmp(bssid, switchap_bssid, sizeof(bssid)) ||
strncmp(passwd, switchap_passwd, sizeof(passwd))) {
if (switchap_timer == NULL) {
switchap_timer = HAL_Timer_Create("swichap_online", (void (*)(void *))awss_switch_ap_online, NULL);
}
HAL_Timer_Stop(switchap_timer);
HAL_Timer_Start(switchap_timer, timeout);
}
} while (0);
return;
ONLINE_SWITCHAP_FAIL:
awss_warn("ilop online switchap failed");
memset(switchap_ssid, 0, sizeof(switchap_ssid));
memset(switchap_bssid, 0, sizeof(switchap_bssid));
memset(switchap_passwd, 0, sizeof(switchap_passwd));
if (packet) {
HAL_Free(packet);
}
return;
}
static void *reboot_timer = NULL;
static int awss_switch_ap_online()
{
HAL_Awss_Connect_Ap(WLAN_CONNECTION_TIMEOUT_MS, switchap_ssid, switchap_passwd,
AWSS_AUTH_TYPE_INVALID, AWSS_ENC_TYPE_INVALID, switchap_bssid, 0);
awss_stop_timer(switchap_timer);
switchap_timer = NULL;
memset(switchap_ssid, 0, sizeof(switchap_ssid));
memset(switchap_bssid, 0, sizeof(switchap_bssid));
memset(switchap_passwd, 0, sizeof(switchap_passwd));
reboot_timer = HAL_Timer_Create("rb_timer", (void (*)(void *))awss_reboot_system, NULL);
HAL_Timer_Start(reboot_timer, 1000);;
return 0;
}
static int awss_reboot_system()
{
awss_stop_timer(reboot_timer);
reboot_timer = NULL;
HAL_Reboot();
return 0;
}
#endif
static int awss_report_token_to_cloud()
{
int packet_len, ret;
char *packet;
char topic[TOPIC_LEN_MAX] = {0};
#define REPORT_TOKEN_PARAM_LEN (64)
if (awss_report_token_suc) { /* success ,no need to report */
return 0;
}
AWSS_DB_UPDATE_STATIS(AWSS_DB_STATIS_START);
/*
* it is still failed after try to report token MATCH_REPORT_CNT_MAX times
*/
if (awss_report_token_cnt ++ > MATCH_REPORT_CNT_MAX) {
awss_stop_timer(report_token_timer);
report_token_timer = NULL;
awss_info("try %d times fail", awss_report_token_cnt);
return -2;
}
if (report_token_timer == NULL) {
report_token_timer = HAL_Timer_Create("rp_token", (void (*)(void *))awss_report_token_to_cloud, NULL);
}
HAL_Timer_Stop(report_token_timer);
HAL_Timer_Start(report_token_timer, 3 * 1000);
packet_len = AWSS_REPORT_LEN_MAX;
packet = os_zalloc(packet_len + 1);
if (packet == NULL) {
awss_err("alloc mem(%d) failed", packet_len);
return -1;
}
do {
/* reduce stack used */
uint8_t i;
char id_str[MSG_REQ_ID_LEN] = {0};
char param[REPORT_TOKEN_PARAM_LEN] = {0};
char token_str[(RANDOM_MAX_LEN << 1) + 1] = {0};
for (i = 0; i < sizeof(aes_random); i ++) /* check aes_random is initialed or not */
if (aes_random[i] != 0x00) {
break;
}
if (i >= sizeof(aes_random)) { /* aes_random needs to be initialed */
produce_random(aes_random, sizeof(aes_random));
}
awss_report_token_time = os_get_time_ms();
HAL_Snprintf(id_str, MSG_REQ_ID_LEN - 1, "\"%u\"", awss_report_id ++);
utils_hex_to_str(aes_random, RANDOM_MAX_LEN, token_str, sizeof(token_str) - 1);
HAL_Snprintf(param, REPORT_TOKEN_PARAM_LEN - 1, "{\"token\":\"%s\"}", token_str);
awss_build_packet(AWSS_CMP_PKT_TYPE_REQ, id_str, ILOP_VER, METHOD_MATCH_REPORT, param, 0, packet, &packet_len);
} while (0);
awss_debug("report token:%s\r\n", packet);
awss_build_topic(TOPIC_MATCH_REPORT, topic, TOPIC_LEN_MAX);
ret = awss_cmp_mqtt_send(topic, packet, packet_len, 1);
awss_info("report token res:%d\r\n", ret);
HAL_Free(packet);
return ret;
}
int awss_report_token()
{
awss_report_token_cnt = 0;
awss_report_token_suc = 0;
return awss_report_token_to_cloud();
}
int awss_stop_report_token()
{
if (report_token_timer) {
awss_stop_timer(report_token_timer);
report_token_timer = NULL;
}
memset(aes_random, 0x00, sizeof(aes_random));
return 0;
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,39 @@
#ifndef __AWSS_RESET__
#define __AWSS_RESET__
#define AWSS_RESET_PKT_LEN (256)
#define AWSS_RESET_TOPIC_LEN (128)
#define AWSS_RESET_MSG_ID_LEN (16)
#define TOPIC_RESET_REPORT "/sys/%s/%s/thing/reset"
#define TOPIC_RESET_REPORT_REPLY "/sys/%s/%s/thing/reset_reply"
#define METHOD_RESET_REPORT "thing.reset"
#define AWSS_RESET_REQ_FMT "{\"id\":%s, \"version\":\"1.0\", \"method\":\"%s\", \"params\":%s}"
#define AWSS_KV_RST "awss.rst"
int awss_check_reset();
/**
* @brief report reset to cloud.
*
* @retval -1 : failure
* @retval 0 : sucess
* @note
* device will save reset flag if device dosen't connect cloud, device will fails to send reset to cloud.
* when connection between device and cloud is ready, device will retry to report reset to cloud.
*/
int awss_report_reset();
/**
* @brief stop to report reset to cloud.
*
* @retval -1 : failure
* @retval 0 : sucess
* @note
* just stop report reset to cloud without any touch reset flag in flash.
*/
int awss_stop_report_reset();
#endif

View File

@@ -0,0 +1,186 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#define AWSS_RESET_PKT_LEN (256)
#define AWSS_RESET_TOPIC_LEN (128)
#define AWSS_RESET_MSG_ID_LEN (16)
#define TOPIC_RESET_REPORT "/sys/%s/%s/thing/reset"
#define TOPIC_RESET_REPORT_REPLY "/sys/%s/%s/thing/reset_reply"
#define METHOD_RESET_REPORT "thing.reset"
#define AWSS_RESET_REQ_FMT "{\"id\":%s, \"version\":\"1.0\", \"method\":\"%s\", \"params\":%s}"
#define AWSS_KV_RST "awss.rst"
#ifdef DEV_BIND_ENABLED
extern int awss_start_bind();
#endif
static uint8_t awss_report_reset_suc = 0;
static uint16_t awss_report_reset_id = 0;
static void *report_reset_timer = NULL;
int awss_report_reset_to_cloud();
void awss_report_reset_reply(void *pcontext, void *pclient, void *mesg)
{
char rst = 0;
iotx_mqtt_event_msg_pt msg = (iotx_mqtt_event_msg_pt)mesg;
switch (msg->event_type) {
case IOTX_MQTT_EVENT_PUBLISH_RECEIVED:
break;
default:
return;
}
devrst_debug("[RST]", "%s\r\n", __func__);
awss_report_reset_suc = 1;
HAL_Kv_Set(AWSS_KV_RST, &rst, sizeof(rst), 0);
HAL_Timer_Stop(report_reset_timer);
HAL_Timer_Delete(report_reset_timer);
report_reset_timer = NULL;
#ifdef INFRA_EVENT
iotx_event_post(IOTX_RESET); /* for old version of event */
do { /* for new version of event */
void *cb = NULL;
cb = (void *)iotx_event_callback(ITE_AWSS_STATUS);
if (cb == NULL) {
break;
}
((int (*)(int))cb)(IOTX_RESET);
} while (0);
#endif
#ifdef DEV_BIND_ENABLED
awss_start_bind();
#endif
}
int awss_report_reset_to_cloud()
{
int ret = -1;
int final_len = 0;
char *topic = NULL;
char *packet = NULL;
int packet_len = AWSS_RESET_PKT_LEN;
int topic_len = AWSS_RESET_TOPIC_LEN;
if (awss_report_reset_suc) {
return 0;
}
if (report_reset_timer == NULL) {
report_reset_timer = HAL_Timer_Create("report_rst", (void (*)(void *))awss_report_reset_to_cloud, NULL);
}
HAL_Timer_Stop(report_reset_timer);
HAL_Timer_Start(report_reset_timer, 3000);
do {
char pk[IOTX_PRODUCT_KEY_LEN + 1] = {0};
char dn[IOTX_DEVICE_NAME_LEN + 1] = {0};
HAL_GetProductKey(pk);
HAL_GetDeviceName(dn);
topic = (char *)devrst_malloc(topic_len + 1);
if (topic == NULL) {
goto REPORT_RST_ERR;
}
memset(topic, 0, topic_len + 1);
HAL_Snprintf(topic, topic_len, TOPIC_RESET_REPORT_REPLY, pk, dn);
ret = IOT_MQTT_Subscribe(NULL, topic, IOTX_MQTT_QOS0,
(iotx_mqtt_event_handle_func_fpt)awss_report_reset_reply, NULL);
if (ret < 0) {
goto REPORT_RST_ERR;
}
memset(topic, 0, topic_len + 1);
HAL_Snprintf(topic, topic_len, TOPIC_RESET_REPORT, pk, dn);
} while (0);
packet = devrst_malloc(packet_len + 1);
if (packet == NULL) {
ret = -1;
goto REPORT_RST_ERR;
}
memset(packet, 0, packet_len + 1);
do {
char id_str[AWSS_RESET_MSG_ID_LEN + 1] = {0};
HAL_Snprintf(id_str, AWSS_RESET_MSG_ID_LEN, "\"%u\"", awss_report_reset_id ++);
final_len = HAL_Snprintf(packet, packet_len, AWSS_RESET_REQ_FMT, id_str, METHOD_RESET_REPORT, "{}");
} while (0);
devrst_debug("[RST]", "report reset:%s\r\n", packet);
ret = IOT_MQTT_Publish_Simple(NULL, topic, IOTX_MQTT_QOS0, packet, final_len);
devrst_debug("[RST]", "report reset result:%d\r\n", ret);
REPORT_RST_ERR:
if (packet) {
devrst_free(packet);
}
if (topic) {
devrst_free(topic);
}
return ret;
}
int awss_report_reset()
{
char rst = 0x01;
awss_report_reset_suc = 0;
HAL_Kv_Set(AWSS_KV_RST, &rst, sizeof(rst), 0);
return awss_report_reset_to_cloud();
}
int awss_check_reset()
{
int len = 1;
char rst = 0;
HAL_Kv_Get(AWSS_KV_RST, &rst, &len);
if (rst != 0x01) { /* reset flag is not set */
devrst_debug("[RST]", "no rst\r\n");
return 0;
}
log_info("[RST]", "need report rst\r\n");
awss_report_reset_suc = 0;
return 1;
}
int awss_stop_report_reset()
{
if (report_reset_timer == NULL) {
return 0;
}
HAL_Timer_Stop(report_reset_timer);
HAL_Timer_Delete(report_reset_timer);
report_reset_timer = NULL;
return 0;
}
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,21 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_DEV_RESET_H__
#define __AWSS_DEV_RESET_H__
#include <stdio.h>
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
int awss_report_reset_to_cloud();
int awss_report_reset();
int awss_check_reset();
int awss_stop_report_reset();
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View File

@@ -0,0 +1,27 @@
#ifndef _AWSS_DEV_RESET_INTERNAL_H_
#define _AWSS_DEV_RESET_INTERNAL_H_
#include "os.h"
#ifdef INFRA_LOG
#include "infra_log.h"
#define devrst_err(...) log_err("devrst", __VA_ARGS__)
#define devrst_info(...) log_info("devrst", __VA_ARGS__)
#define devrst_debug(...) log_debug("devrst", __VA_ARGS__)
#else
#define devrst_info(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define devrst_err(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#define devrst_debug(...) do{HAL_Printf(__VA_ARGS__);HAL_Printf("\r\n");}while(0)
#endif
#ifdef INFRA_MEM_STATS
#include "infra_mem_stats.h"
#define devrst_malloc(size) LITE_malloc(size, MEM_MAGIC, "devrst")
#define devrst_free(ptr) LITE_free(ptr)
#else
#define devrst_malloc(size) HAL_Malloc(size)
#define devrst_free(ptr) {HAL_Free((void *)ptr);ptr = NULL;}
#endif
#endif

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
int awss_stop_timer(void *timer)
{
if (timer == NULL)
return 0;
HAL_Timer_Stop(timer);
HAL_Timer_Delete(timer);
return 0;
}
#if 0
int awss_start_timer(void **timer, const char *name, void *func, void *user_data, int ms)
{
if (timer == NULL)
return -1;
*timer = HAL_Timer_Create(name, (void (*)(void *))func, user_data);
if (*timer == NULL)
return -1;
HAL_Timer_Stop(*timer);
HAL_Timer_Start(*timer, ms);
return 0;
}
#endif
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,19 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_TIMER_H__
#define __AWSS_TIMER_H__
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
int awss_stop_timer(void *timer);
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View File

@@ -0,0 +1,51 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_UTILS_H__
#define __AWSS_UTILS_H__
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C" {
#endif
#include "os.h"
#if 0
#include "iotx_utils.h"
#endif
#include "infra_string.h"
/**
* @brief string to hex
*
* @param[in] str: input hex string
* @param[in] str_len: length of input hex string
* @param[out] out: output hex byte stream
* @param[in/out] output_len: [in] for output buffer size, [out] for
* output hex byte len
* @Note None.
*
* @retval return num of hex bytes converted, 0 means error.
*/
#define utils_str_to_hex LITE_hexstr_convert
/**
* @brief hex to string
*
* @param[in] buf: input hex byte stream
* @param[in] buf_len: input stream length in byte
* @param[out] str: encoded hex string
* @param[in/out] str_len: [in] for str buffer size, [out] for
* encoded string length
* @Note output str buffer is NULL-terminated(if str_buf_len is longer enough)
*
* @retval return length of str converted, 0 means error.
*/
#define utils_hex_to_str(buf, buf_len, str, str_buf_len) LITE_hexbuf_convert(buf, str, buf_len, 1)
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View File

@@ -0,0 +1,26 @@
#include "infra_config.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include "awss_event.h"
#include "awss_timer.h"
#include "awss_log.h"
#include "passwd.h"
#include "awss_utils.h"
#include "infra_compat.h"
#include "awss_packet.h"
#include "awss_notify.h"
#include "awss_cmp.h"
#include "awss_cmp.h"
#include "infra_json_parser.h"
#include "mqtt_api.h"
#include "awss_dev_reset.h"
#include "awss_dev_reset_internal.h"
#include "awss_info.h"
#include "awss_bind_statis.h"
#include "dev_bind_wrapper.h"
#include "coap_api.h"
#include "iotx_coap.h"
#ifdef WIFI_PROVISION_ENABLED
#include "awss_statis.h"
#endif

View File

@@ -0,0 +1,110 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __IOT_IMPORT_AWSS_H__
#define __IOT_IMPORT_AWSS_H__
#include "infra_types.h"
#include "infra_defs.h"
#ifdef __cplusplus
extern "C" {
#endif
#ifndef _IN_
#define _IN_
#endif
#ifndef _OU_
#define _OU_
#endif
#ifndef _IN_OPT_
#define _IN_OPT_
#endif
/* link type */
enum AWSS_LINK_TYPE {
/* rtos HAL choose this type */
AWSS_LINK_TYPE_NONE,
/* linux HAL may choose the following type */
AWSS_LINK_TYPE_PRISM,
AWSS_LINK_TYPE_80211_RADIO,
AWSS_LINK_TYPE_80211_RADIO_AVS,
AWSS_LINK_TYPE_HT40_CTRL /* for espressif HAL, see struct ht40_ctrl */
};
struct HAL_Ht40_Ctrl {
uint16_t length;
uint8_t filter;
signed char rssi;
};
typedef int (*awss_recv_80211_frame_cb_t)(char *buf, int length,
enum AWSS_LINK_TYPE link_type, int with_fcs, signed char rssi);
/* auth type */
enum AWSS_AUTH_TYPE {
AWSS_AUTH_TYPE_OPEN,
AWSS_AUTH_TYPE_SHARED,
AWSS_AUTH_TYPE_WPAPSK,
AWSS_AUTH_TYPE_WPA8021X,
AWSS_AUTH_TYPE_WPA2PSK,
AWSS_AUTH_TYPE_WPA28021X,
AWSS_AUTH_TYPE_WPAPSKWPA2PSK,
AWSS_AUTH_TYPE_MAX = AWSS_AUTH_TYPE_WPAPSKWPA2PSK,
AWSS_AUTH_TYPE_INVALID = 0xff,
};
/* encryt type */
enum AWSS_ENC_TYPE {
AWSS_ENC_TYPE_NONE,
AWSS_ENC_TYPE_WEP,
AWSS_ENC_TYPE_TKIP,
AWSS_ENC_TYPE_AES,
AWSS_ENC_TYPE_TKIPAES,
AWSS_ENC_TYPE_MAX = AWSS_ENC_TYPE_TKIPAES,
AWSS_ENC_TYPE_INVALID = 0xff,
};
typedef int (*awss_wifi_scan_result_cb_t)(
const char ssid[HAL_MAX_SSID_LEN],
const uint8_t bssid[ETH_ALEN],
enum AWSS_AUTH_TYPE auth,
enum AWSS_ENC_TYPE encry,
uint8_t channel, signed char rssi,
int is_last_ap);
/* 80211 frame type */
typedef enum HAL_Awss_Frame_Type {
FRAME_ACTION,
FRAME_BEACON,
FRAME_PROBE_REQ,
FRAME_PROBE_RESPONSE,
FRAME_DATA
} HAL_Awss_Frame_Type_t;
#define FRAME_ACTION_MASK (1 << FRAME_ACTION)
#define FRAME_BEACON_MASK (1 << FRAME_BEACON)
#define FRAME_PROBE_REQ_MASK (1 << FRAME_PROBE_REQ)
#define FRAME_PROBE_RESP_MASK (1 << FRAME_PROBE_RESPONSE)
#define FRAME_DATA_MASK (1 << FRAME_DATA)
typedef void (*awss_wifi_mgmt_frame_cb_t)(_IN_ uint8_t *buffer, _IN_ int len,
_IN_ signed char rssi_dbm, _IN_ int buffer_type);
typedef struct {
enum AWSS_AUTH_TYPE auth;
enum AWSS_ENC_TYPE encry;
uint8_t channel;
signed char rssi_dbm;
char ssid[HAL_MAX_SSID_LEN];
uint8_t mac[ETH_ALEN];
} awss_ap_info_t;
#ifdef __cplusplus
}
#endif
#endif /* __IOT_IMPORT_AWSS_H__ */

View File

@@ -0,0 +1,73 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_OS_H__
#define __AWSS_OS_H__
#ifdef __cplusplus
extern "C" {
#endif
#include <ctype.h>
#include "infra_defs.h"
#include <stdio.h>
#include <string.h>
#include "infra_compat.h"
#ifndef _IN_OPT_
#define _IN_OPT_
#endif
#ifndef _OUT_
#define _OUT_
#endif
#ifndef _OUT_OPT_
#define _OUT_OPT_
#endif
#ifndef _INOUT_
#define _INOUT_
#endif
#ifndef _INOUT_OPT_
#define _INOUT_OPT_
#endif
/** @defgroup group_os os
* @{
*/
#define OS_MAC_LEN HAL_MAC_LEN
#define OS_ETH_ALEN ETH_ALEN
#define OS_IP_LEN (NETWORK_ADDR_LEN)
#define OS_MAX_PASSWD_LEN HAL_MAX_PASSWD_LEN
#define OS_MAX_SSID_LEN HAL_MAX_SSID_LEN
#define OS_PRODUCT_KEY_LEN IOTX_PRODUCT_KEY_LEN
#define OS_PRODUCT_SECRET_LEN IOTX_PRODUCT_SECRET_LEN
#define OS_DEVICE_NAME_LEN IOTX_DEVICE_NAME_LEN
#define OS_DEVICE_SECRET_LEN IOTX_DEVICE_SECRET_LEN
#define PLATFORM_AES_DECRYPTION HAL_AES_DECRYPTION
#define PLATFORM_AES_ENCRYPTION HAL_AES_ENCRYPTION
#define PLATFORM_MAX_SSID_LEN HAL_MAX_SSID_LEN
#define PLATFORM_MAX_PASSWD_LEN HAL_MAX_PASSWD_LEN
typedef void *p_HAL_Aes128_t;
#define p_aes128_t p_HAL_Aes128_t
char *os_wifi_get_mac_str(char mac_str[HAL_MAC_LEN]);
char *os_wifi_str2mac(char mac_str[HAL_MAC_LEN], char mac[ETH_ALEN]);
uint8_t *os_wifi_get_mac(uint8_t mac[ETH_ALEN]);
uint32_t os_get_time_ms(void);
int os_is_big_endian(void);
uint16_t os_htobe16(uint16_t data);
uint16_t os_htole16(uint16_t data);
uint16_t os_be16toh(uint16_t data);
uint16_t os_le16toh(uint16_t data);
uint32_t os_le32toh(uint32_t data);
uint16_t os_get_unaligned_be16(uint8_t *ptr);
uint16_t os_get_unaligned_le16(uint8_t *ptr);
uint32_t os_get_unaligned_be32(uint8_t *ptr);
uint32_t os_get_unaligned_le32(uint8_t *ptr);
void *os_zalloc(uint32_t size);
uint32_t time_elapsed_ms_since(uint32_t start_timestamp);
#ifdef __cplusplus
}
#endif
#endif /* SRC_OSA_ABSTRAC_H_ */

View File

@@ -0,0 +1,161 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
/****** Convert values between host and big-/little-endian byte order ******/
/* reverse byte order */
static uint16_t reverse_16bit(uint16_t data)
{
return (data >> 8) | (data << 8);
}
/* host byte order to big endian */
uint16_t os_htobe16(uint16_t data)
{
if (os_is_big_endian()) {
return data;
}
return reverse_16bit(data);
}
/* host byte order to little endian */
uint16_t os_htole16(uint16_t data)
{
if (os_is_big_endian()) {
return reverse_16bit(data);
}
return data;
}
/* big endian to host byte order */
uint16_t os_be16toh(uint16_t data)
{
return os_htobe16(data);
}
/* little endian to host byte order */
uint16_t os_le16toh(uint16_t data)
{
return os_htole16(data);
}
/* get unaligned data in big endian. */
uint16_t os_get_unaligned_be16(uint8_t * ptr)
{
uint16_t res;
memcpy(&res, ptr, sizeof(uint16_t));
return os_be16toh(res);
}
/* get unaligned data in little endian. */
uint16_t os_get_unaligned_le16(uint8_t * ptr)
{
uint16_t res;
memcpy(&res, ptr, sizeof(uint16_t));
return os_le16toh(res);
}
/* format mac string uppercase */
char *os_wifi_get_mac_str(char mac_str[OS_MAC_LEN])
{
char *str;
int colon_num = 0, i;
str = HAL_Wifi_Get_Mac(mac_str);
/* sanity check */
while (str) {
str = strchr(str, ':');
if (str) {
colon_num ++;
str ++; /* eating char ':' */
}
}
/* convert to capital letter */
for (i = 0; i < OS_MAC_LEN && mac_str[i]; i ++) {
if ('a' <= mac_str[i] && mac_str[i] <= 'z') {
mac_str[i] -= 'a' - 'A';
}
}
return mac_str;
}
char *os_wifi_str2mac(char mac_str[OS_MAC_LEN], char mac[OS_ETH_ALEN])
{
int i = 0;
char *ptr = mac_str;
char mac_addr[OS_ETH_ALEN] = {0};
if (ptr == NULL)
return NULL;
while (isxdigit(*ptr) && i < OS_ETH_ALEN) {
mac_addr[i ++] = (uint8_t)strtol(ptr, &ptr, 16);
++ ptr;
}
if (i < OS_ETH_ALEN) /* don't touch mac when fail */
return NULL;
if (mac) memcpy(mac, mac_addr, OS_ETH_ALEN);
return mac;
}
uint8_t *os_wifi_get_mac(uint8_t mac[OS_ETH_ALEN])
{
char mac_str[OS_MAC_LEN] = {0};
os_wifi_get_mac_str(mac_str);
return (uint8_t *)os_wifi_str2mac(mac_str, (char *)mac);
}
void *os_zalloc(uint32_t size)
{
void *ptr = HAL_Malloc(size);
if (ptr != NULL) {
memset(ptr, 0, size);
}
return ptr;
}
uint32_t os_get_time_ms(void)
{
static uint32_t fixed_delta;
if (!fixed_delta) {
fixed_delta = (uint32_t)HAL_UptimeMs() - 0xFFFF0000;
}
/* add a big offset, for easier caught time overflow bug */
return (uint32_t)HAL_UptimeMs() - fixed_delta;
}
uint32_t time_elapsed_ms_since(uint32_t start_timestamp)
{
uint32_t now = os_get_time_ms();
return now - start_timestamp;
}
int os_is_big_endian(void)
{
uint32_t data = 0xFF000000;
if (0xFF == *(uint8_t *) & data) {
return 1; /* big endian */
}
return 0; /* little endian */
}

View File

@@ -0,0 +1,126 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#include "dev_bind_internal.h"
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
uint8_t aes_random[RANDOM_MAX_LEN] = {0};
#ifdef WIFI_PROVISION_ENABLED
/*
* 1. place 0 @ 0, because of java modified-UTF8
* 2. translation follow utf8 stardard
*/
static const uint8_t ssid_dict_decode_table[] = {
0x00, 0x0e, 0x6c, 0x3a, 0x6d, 0x44, 0x2a, 0x6f,
0x4d, 0x05, 0x6b, 0x28, 0x08, 0x25, 0x5f, 0x2d,
0x64, 0x76, 0x78, 0x37, 0x58, 0x60, 0x53, 0x31,
0x36, 0x79, 0x43, 0x1a, 0x11, 0x72, 0x03, 0x59,
0x50, 0x02, 0x71, 0x7c, 0x34, 0x3e, 0x23, 0x24,
0x26, 0x5b, 0x73, 0x0f, 0x5e, 0x12, 0x54, 0x0b,
0x61, 0x35, 0x3c, 0x57, 0x48, 0x55, 0x63, 0x4a,
0x13, 0x75, 0x45, 0x70, 0x47, 0x0c, 0x2f, 0x21,
0x17, 0x2e, 0x62, 0x49, 0x4b, 0x5c, 0x19, 0x51,
0x69, 0x3b, 0x7e, 0x0d, 0x3d, 0x67, 0x2c, 0x22,
0x14, 0x42, 0x5a, 0x7f, 0x32, 0x01, 0x07, 0x7b,
0x15, 0x4f, 0x16, 0x29, 0x30, 0x27, 0x20, 0x18,
0x65, 0x06, 0x1c, 0x3f, 0x68, 0x2b, 0x4c, 0x0a,
0x1e, 0x46, 0x5d, 0x1f, 0x10, 0x6e, 0x56, 0x7a,
0x1b, 0x09, 0x52, 0x38, 0x66, 0x7d, 0x41, 0x40,
0x04, 0x6a, 0x39, 0x77, 0x33, 0x1d, 0x74, 0x4e,
0xaf, 0xa6, 0x8c, 0xbd, 0x89, 0xa2, 0xa9, 0x9e,
0xa1, 0x91, 0xb9, 0xad, 0xbf, 0xb7, 0x95, 0xa8,
0xa5, 0x82, 0xaa, 0xa3, 0x94, 0x92, 0xb8, 0x87,
0x88, 0xb1, 0x93, 0xbc, 0x80, 0xb5, 0xba, 0x99,
0xab, 0xbe, 0x90, 0x8e, 0x83, 0x9f, 0x9a, 0x86,
0x85, 0x98, 0xa4, 0xa0, 0xac, 0x9c, 0x96, 0x81,
0xb0, 0x8d, 0xbb, 0xb2, 0x9d, 0xae, 0x84, 0x9b,
0xb4, 0x8b, 0x97, 0xa7, 0xb3, 0x8a, 0x8f, 0xb6,
0xc5, 0xc0, 0xc8, 0xd7, 0xde, 0xc4, 0xd1, 0xd2,
0xd9, 0xcb, 0xcd, 0xd5, 0xcc, 0xc7, 0xdb, 0xdf,
0xdc, 0xdd, 0xcf, 0xc6, 0xda, 0xc2, 0xc3, 0xc9,
0xc1, 0xca, 0xd6, 0xd8, 0xce, 0xd3, 0xd0, 0xd4,
0xe9, 0xe5, 0xe8, 0xe2, 0xe6, 0xeb, 0xe3, 0xec,
0xed, 0xe7, 0xe1, 0xe4, 0xea, 0xef, 0xee, 0xe0,
0xf6, 0xf0, 0xf4, 0xf5, 0xf2, 0xf3, 0xf7, 0xf1,
0xfb, 0xf9, 0xfa, 0xf8, 0xfc, 0xfd, 0xfe, 0xff
};
static const uint8_t notify_encode_table[] = {
0x00, 0x71, 0x21, 0x1e, 0x78, 0x09, 0x61, 0x56,
0x0c, 0x55, 0x67, 0x2f, 0x3d, 0x4b, 0x01, 0x2b,
0x6c, 0x1c, 0x1b, 0x38, 0x50, 0x58, 0x5a, 0x40,
0x5f, 0x46, 0x2d, 0x70, 0x62, 0x7d, 0x68, 0x6b,
0x5e, 0x3f, 0x4f, 0x65, 0x27, 0x0d, 0x28, 0x5d,
0x0b, 0x5b, 0x06, 0x26, 0x4e, 0x0f, 0x41, 0x3e,
0x5c, 0x17, 0x54, 0x7c, 0x32, 0x31, 0x18, 0x13,
0x73, 0x7a, 0x03, 0x49, 0x24, 0x4c, 0x25, 0x63,
0x77, 0x76, 0x51, 0x1a, 0x05, 0x08, 0x69, 0x3c,
0x34, 0x43, 0x37, 0x44, 0x66, 0x3a, 0x7f, 0x59,
0x20, 0x47, 0x72, 0x16, 0x2e, 0x35, 0x2c, 0x33,
0x14, 0x1f, 0x52, 0x29, 0x45, 0x6a, 0x6e, 0x0e,
0x15, 0x30, 0x42, 0x36, 0x10, 0x60, 0x74, 0x07,
0x64, 0x48, 0x79, 0x0a, 0x02, 0x04, 0x6d, 0x4d,
0x3b, 0x22, 0x1d, 0x2a, 0x7e, 0x39, 0x1a, 0x7b,
0x12, 0x19, 0x6f, 0x57, 0x23, 0x75, 0x41, 0x53,
0x9c, 0xaf, 0x91, 0xa4, 0xb6, 0xb1, 0xa7, 0x97,
0x98, 0x84, 0xbd, 0xb9, 0x82, 0xa8, 0xa3, 0xbe,
0xa2, 0x89, 0x95, 0x9a, 0x94, 0x8e, 0xae, 0xba,
0xa9, 0x9f, 0xa6, 0xb7, 0xad, 0xb4, 0x87, 0xa5,
0xab, 0x88, 0x85, 0x93, 0xaa, 0x90, 0x81, 0xbb,
0x8f, 0x86, 0x92, 0xa0, 0xac, 0x8b, 0xb5, 0x80,
0xb0, 0x99, 0xb3, 0xbc, 0xb8, 0x9d, 0xbf, 0x8d,
0x96, 0x8a, 0x9e, 0xb2, 0x9b, 0x83, 0xa1, 0x8c,
0xc1, 0xd8, 0xd5, 0xd6, 0xc5, 0xc0, 0xd3, 0xcd,
0xc2, 0xd7, 0xd9, 0xc9, 0xcc, 0xca, 0xdc, 0xd2,
0xde, 0xc6, 0xc7, 0xdd, 0xdf, 0xcb, 0xda, 0xc3,
0xdb, 0xc8, 0xd4, 0xce, 0xd0, 0xd1, 0xc4, 0xcf,
0xef, 0xea, 0xe3, 0xe6, 0xeb, 0xe1, 0xe4, 0xe9,
0xe2, 0xe0, 0xec, 0xe5, 0xe7, 0xe8, 0xee, 0xed,
0xf1, 0xf7, 0xf4, 0xf5, 0xf2, 0xf3, 0xf0, 0xf6,
0xfb, 0xf9, 0xfa, 0xf8, 0xfc, 0xfd, 0xfe, 0xff
};
int awss_dict_crypt(char tab_idx, uint8_t *data, uint8_t len)
{
uint8_t i = 0;
uint8_t *table = NULL;
switch (tab_idx) {
case SSID_DECODE_TABLE:
table = (uint8_t *)ssid_dict_decode_table;
break;
case NOTIFY_ENCODE_TABLE:
table = (uint8_t *)notify_encode_table;
break;
default:
table = NULL;
break;
}
if (table == NULL || data == NULL)
return -1;
for (i = 0; i < len; i ++)
data[i] = table[data[i]];
return 0;
}
int produce_signature(uint8_t *sign, uint8_t *txt,
uint32_t txt_len, const char *key)
{
if (sign == NULL || txt == NULL || txt_len == 0 || key == NULL)
return -1;
/* TODO */
utils_hmac_sha1_hex((const char *)txt, (int)txt_len,
(char *)sign, key, strlen(key));
return 0;
}
#endif
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif

View File

@@ -0,0 +1,34 @@
/*
* Copyright (C) 2015-2018 Alibaba Group Holding Limited
*/
#ifndef __AWSS_PASSWD_H__
#define __AWSS_PASSWD_H__
#define KEY_MAX_LEN (40)
#define AES128_KEY_LEN (16)
#define RANDOM_MAX_LEN (16)
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
extern "C"
{
#endif
enum {
SSID_DECODE_TABLE = 0x0,
NOTIFY_ENCODE_TABLE,
DICT_CRYPT_TABLE_IDX_MAX,
};
int awss_dict_crypt(char tab_idx, uint8_t *data, uint8_t len);
#ifdef WIFI_PROVISION_ENABLED
int produce_signature(uint8_t *sign, uint8_t *txt, uint32_t txt_len, const char *key);
#endif
extern uint8_t aes_random[RANDOM_MAX_LEN];
#if defined(__cplusplus) /* If this is a C++ compiler, use C linkage */
}
#endif
#endif

View File

@@ -0,0 +1,6 @@
LIBA_TARGET := libiot_dev_bind.a
LIB_SRCS_PATTERN := *.c
$(call Append_Conditional, LIB_SRCS_PATTERN, impl/*.c, DEV_BIND_ENABLED)
$(call Append_Conditional, LIB_SRCS_PATTERN, impl/os/*.c, DEV_BIND_ENABLED)
$(call Append_Conditional, LIB_SRCS_PATTERN, impl/awss_reset/*.c, DEV_BIND_ENABLED)