Add boardlink iot sdk support

This commit is contained in:
HuangXin 2018-08-10 10:22:55 +08:00
parent cbd2b80c4d
commit f737a816ab
10 changed files with 406 additions and 37 deletions

212
3partys/boardlink/blsdk.h Normal file
View File

@ -0,0 +1,212 @@
#ifndef __SDK_H
#define __SDK_H
/*
* 0. implement all callback's of this SDK
* 1. read wifi probe frame from driver
* 2. feed wifi probe frame to sdk_pkt_process
* 3. this sdk depends on no thirdparty libraries, and there is no internet connection with net.
*/
typedef unsigned char uint8;
typedef unsigned short int uint16;
typedef unsigned int uint32;
typedef char int8;
typedef short int int16;
typedef int int32;
#define SDK_SUCCESS (0)
#define SDK_FAIL (1)
#define SDK_NULL (0)
#define OUI_LEN (3)
#define OUI_TYPE (0)
#define OUI_TYPE_LEN (1)
#define MAC_LEN (6)
#define SSID_MAX_LEN (32)
#define PASSWORD_MAX_LEN (32)
#define SDK_SHAREDKEY_LEN (32)
#define SDK_FRAME_MAX_LEN (1460)
#define FASTCON_MASTER_ID_LEN (4)
#define SDK_PACKED __attribute__((packed))
typedef void* (*sdk_memset_t) (
void* ptr,
int32 value,
uint32 num);
typedef void* (*sdk_memcpy_t) (
void* dst,
const void* src,
uint32 num);
typedef int32 (*sdk_memcmp_t) (
const void* ptr1,
const void* ptr2,
uint32 num);
typedef int32 (*sdk_printf_t) (
const char* format,
...);
typedef struct sdk_device_info_t {
uint32 vendor_id;
uint32 product_id;
uint8 mac[MAC_LEN];
uint8 * device_info;
uint16 device_info_len;
uint8 * device_id;
uint16 device_id_len;
}sdk_product_info_t;
//发现设备
typedef int32 (*sdk_found_device_t) (sdk_product_info_t *product_info);
//对应2.1 云端身份校验接口请求
typedef int32 (*sdk_challenge_post_t)(
sdk_product_info_t *product_info,
uint8 * challenge,
uint16 challenge_len);
//对应2.2 设备身份声明接口请求
typedef int32 (*sdk_signature_post_t)(
sdk_product_info_t *product_info,
uint8 * signature,
uint16 signature_len);
//sdk将设备控制token传给上层
typedef int32 (*sdk_device_token_post_t)(
sdk_product_info_t *product_info,
uint8 * token,
uint16 token_len);
//上报配置结果
//目前只支持失败结果上报。
//成功的结果是由云端推送上线通知
//result 0:成功 -1:失败
typedef int32 (*sdk_config_result_post_t) (
sdk_product_info_t *product_info,
int8 result);
//发送802.11原始报文接口
typedef int32 (*sdk_raw_pkt_send_t)(
uint8 * frame,
uint16 frame_len);
typedef struct {
sdk_memset_t memset;
sdk_memcpy_t memcpy;
sdk_memcmp_t memcmp;
sdk_printf_t printf;
sdk_found_device_t found_device;
sdk_challenge_post_t challenge_post;
sdk_signature_post_t signature_post;
sdk_device_token_post_t device_token_post;
sdk_config_result_post_t config_result_post;
sdk_raw_pkt_send_t send_raw_pkt;
} sdk_config_t;
typedef struct {
//gateway信息
//包括当前路由器信道gateway macbssid
uint8 channel; //cur channel
//若mac地址为00-11-22-33-44-55则device_mac[] = {0x00,0x11,0x22,0x33,0x44,0x55};
uint8 device_mac[MAC_LEN];
//AP的mac地址格式等同device_mac
uint8 bssid[MAC_LEN];
//由dnakit后台分配用于区分不同平台固定4个字节
uint8 fastcon_master_id[FASTCON_MASTER_ID_LEN];
//0:probe req,1:probe rsp,2:action
//报文选择根据gateway 能力自行设置
uint8 send_wlan_type;
//回调接口注册
sdk_config_t sdk_config;
} SDK_PACKED sdk_user_init_t;
typedef enum {
SDK_STATE_PROBE_DEVICE,
SDK_STATE_LOCK_CHANNEL,
SDK_STATE_CONFIG,
SDK_STATE_CONNECTING,
SDK_STATE_DONE, //Finish
SDK_STATE_FAIL,
} sdk_state_e;
/**
* @brief sdk编译时间
* @return 0
* SDK_E_INIT_FAIL sdk未初始化
*/
extern int32 sdk_version(void);
/**
* @brief sdk
* @param init
* @return 0
* SDK_E_INIT_FAIL
*/
extern int32 sdk_init(sdk_user_init_t *init);
/**
* @brief 802.11
* @param frame 802.11
* @param frame_len
* @return sdk_state_e
SDK_E_INIT_FAIL
*/
extern int32 sdk_pkt_process(uint8 *frame, int16 frame_len);
/**
* @brief token bind成功报文
* @return 0
* SDK_E_INIT_FAIL
* SDK_E_STATE
*/
extern int32 sdk_device_token_bind_success(void);
/**
* @brief ssid和password信息发送
* @param sharedkey
* @param ssid
* @param ssid_len
* @param password
* @param password_len
* @return 0
* SDK_E_INIT_FAIL
* SDK_E_STATE
* @example ssid为"ssid"password为"password"
*
* @code
* uint8 ssid[] = "ssid";
* uint8 password[] = "password";
* sdk_wlan_params_set(sharedkey,ssid,strlen(ssid),password,strlen(password));
* @endcode
*/
extern int32 sdk_wlan_params_set(uint8 sharedkey[SDK_SHAREDKEY_LEN],uint8 *ssid,uint16 ssid_len,uint8 *password,uint16 password_len);
/**
* @brief challenge和signature信息发送
* @param challenge
* @param challenge_len
* @param signature
* @param signature_len
* @return 0
* SDK_E_INIT_FAIL
* SDK_E_STATE
*/
extern int32 sdk_challenge_and_signature_set(uint8 *challenge,uint16 challenge_len, uint8 *signature, uint16 signature_len);
/**
* @brief
* @return 0
* SDK_E_INIT_FAIL
* SDK_E_STATE
*/
extern int32 sdk_user_auth_to_start_config(void);
/**
* @brief sdk状态
* @return 0
* SDK_E_INIT_FAIL
*/
extern int32 sdk_deinit(void);
#endif

View File

@ -0,0 +1,19 @@
#ifndef __SDK_ERRNO_H
#define __SDK_ERRNO_H
#define SDK_E_INIT_FAIL (-1) //SDK初始化失败
#define SDK_E_INVPARAMS (-2) //参数异常
#define SDK_E_STATE (-3) //sdk状态异常
//only use by sdk_config_result_post_t
#define SDK_E_CONNECT_AP_TIMEOUT (-101) //连接ap超时
#define SDK_E_PKT_SEND (-102) //发送数据包失败
#define SDK_E_OVER_RETRY_COUNT (-103) //超过最大重试次数
#define SDK_E_VERIFY_SIGNATURE (-104) //校验网关端身份失败
#define SDK_E_SSID_PWD (-105) //ssid和pwd解析异常
#define SDK_E_MSG_NOT_SUPPORT (-106) //接收到不支持的消息类型
#define SDK_E_CONFIG_TIMEOUT (-107) //配置超时超过10分钟配置时间
#endif

Binary file not shown.

View File

@ -10,6 +10,8 @@
#include <uthash/utstring.h>
#include <linux/netlink.h>
#include <linux/socket.h>
#include <linux/nl80211.h>
#include <sys/socket.h>
#include "log.h"
#include "libuv_dbus.h"
@ -1594,15 +1596,16 @@ static void __dlThreadRuntimeCb(void *pParams)
static void __uvThreadTest(void *pParams)
{
#ifndef PLATFORM_CPU
for(int i = 0; i < 100; i++)
{
char buf[256];
memset(buf, 0, 256);
sprintf(buf, "Send %d times\n", i);
BL_SendMessage(buf, strlen(buf));
//BL_SendMessage(buf, strlen(buf));
sleep(1);
}
#endif
#if 0
for(int i = 0; i < 100000; i++)
{
@ -1797,25 +1800,97 @@ void test_netlink(void)
}
else
{
LOG_EX(LOG_Debug, "Received message: %s\n",(char *) NLMSG_DATA(nlh));
//LOG_EX(LOG_Debug, "Received message: %s\n",(char *) NLMSG_DATA(nlh));
print_hex_dump_bytes("Msg", 0, NLMSG_DATA(nlh), retval);
}
}
close(sock_fd);
}
#ifndef PLATFORM_CPU
static void __uvThreadNetlinkSend(void *pParams)
{
char* pData = "0123456789abcdef";
int lastSt = MSG_IN_DISCOVERY_MODE;
for(int i = 0; i < 100; i++)
{
char buf[256];
memset(buf, 0, 256);
sprintf(buf, "Send %d times", i);
//LOG_EX(LOG_Debug, "Send [%s] ret = %d\n", buf, BL_SendMessage(buf, strlen(buf)));
BL_SendMessage(buf, strlen(buf));
BL_IOT_MSG blMsg;
memset(&blMsg, 0, sizeof(BL_IOT_MSG));
strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS);
memcpy(blMsg.msgData, pData, strlen(pData));
blMsg.msglen = strlen(pData);
blMsg.msgType = MSG_BYPASS_MODE;
LOG_EX(LOG_Debug, "Send Message : %d\n", i);
BL_SendBLMsg(MSG_GET_WLAN_INFO, NULL, 0);
if(i % 10 == 0)
{
BL_SendBLMsg(lastSt, NULL, 0);
if(lastSt == MSG_IN_DISCOVERY_MODE)
{
lastSt = MSG_OUT_DISCOVERY_MODE;
}
else
{
lastSt = MSG_IN_DISCOVERY_MODE;
}
}
//BL_SendMessage((unsigned char*)&blMsg, BL_IOT_MSG_LEN(blMsg.msglen));
sleep(1);
}
}
#endif
static int expectedId;
static int nlCallback(void* msg, void* arg)
{
struct ucred * p = (struct ucred *)msg;
LOG_EX(LOG_Debug, "receive message\n");
}
#if 0
void test_nl80211(void)
{
int ret;
//allocate socket
nl_sock* sk = nl_socket_alloc();
//connect to generic netlink
genl_connect(sk);
//find the nl80211 driver ID
expectedId = genl_ctrl_resolve(sk, "nl80211");
//attach a callback
nl_socket_modify_cb(sk, NL_CB_VALID, NL_CB_CUSTOM,
nlCallback, NULL);
//allocate a message
nl_msg* msg = nlmsg_alloc();
nl80211_commands cmd = NL80211_CMD_GET_INTERFACE;
int ifIndex = if_nametoindex("wlan0");
int flags = 0;
// setup the message
genlmsg_put(msg, 0, 0, expectedId, 0, flags, cmd, 0);
//add message attributes
NLA_PUT_U32(msg, NL80211_ATTR_IFINDEX, ifIndex);
//send the messge (this frees it)
ret = nl_send_auto_complete(sk, msg);
//block for message to return
nl_recvmsgs_default(sk);
}
#endif
int main(int argc, char **argv)
{
@ -1825,7 +1900,7 @@ int main(int argc, char **argv)
uv_loop_t* pLoop = GetDBusDefaultLoop();
int modIdx = -1;
char buf[256];
BL_IOT_MSG blMsg;
#if 0
i = GetServerModeFromCC(PUBLISH_MODE, &ret);
@ -1901,28 +1976,16 @@ int main(int argc, char **argv)
}
//test_netlink();
BL_Init();
memset(&blMsg, 0, sizeof(BL_IOT_MSG));
strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS);
if(modIdx % 2 == 0)
{
blMsg.msgType = MSG_IN_DISCOVERY_MODE;
}
else
{
blMsg.msgType = MSG_OUT_DISCOVERY_MODE;
}
#ifndef PLATFORM_CPU
BL_Init(NULL);
BL_SendMessage((unsigned char*)&blMsg, sizeof(BL_IOT_MSG));
//L_SendMessage((unsigned char*)&blMsg, sizeof(BL_IOT_MSG));
//test_netlink();
//__uvThreadTimerV2(pLoop);
//test_task_new(__uvThreadNetlinkSend, NULL);
test_task_new(__uvThreadNetlinkSend, NULL);
#endif
//DumpCurServerAddr("Default");
#if 0

View File

@ -1,3 +1,4 @@
#ifndef PLATFORM_CPU
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@ -11,6 +12,8 @@
#include "log.h"
#include "libuv_dbus.h"
#include "boardlink_iot.h"
#include "blsdk.h"
#include "blsdk_errno.h"
#else
#include <uvdbus/log.h>
#include <uvdbus/libuv_dbus.h>
@ -22,6 +25,7 @@
static int g_nlSock = -1;
static unsigned char g_MsgSendBuf[BL_MSG_BUF_MAX];
static BlMsgCb g_blMsgCb = NULL;
static void __recvMsgThread(void *pParam)
{
@ -56,7 +60,13 @@ static void __recvMsgThread(void *pParam)
}
else
{
LOG_EX(LOG_Debug, "Recv Message: %s\n", NLMSG_DATA(pMsgHdr));
PBL_IOT_MSG pMsg = (PBL_IOT_MSG)NLMSG_DATA(pMsgHdr);
//LOG_EX(LOG_Debug, "Recv Message: %s\n", NLMSG_DATA(pMsgHdr));
print_hex_dump_bytes("Msg", 0, pMsg, BL_IOT_MSG_LEN(pMsg->msglen));
if(g_blMsgCb)
{
g_blMsgCb(pMsg);
}
}
}
}
@ -93,7 +103,7 @@ static int __nlCreateSocket(void)
return 0;
}
int BL_SendMessage(unsigned char* pData, unsigned int len)
static int __sendMessage(unsigned char* pData, unsigned int len)
{
struct iovec iov;
struct sockaddr_nl dstAddr;
@ -117,7 +127,42 @@ int BL_SendMessage(unsigned char* pData, unsigned int len)
return sendmsg(g_nlSock, &msgHdr, 0);
}
int BL_Init(void)
int BL_SendBLMsg(BL_IOT_MSG_TYPE msgType, unsigned char* pData, unsigned int len)
{
BL_IOT_MSG blMsg;
memset(&blMsg, 0, sizeof(BL_IOT_MSG));
blMsg.msgType = msgType;
blMsg.msglen = len;
strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS);
if(pData && len > 0)
{
memcpy(blMsg.msgData, pData, len);
}
return __sendMessage((unsigned char*)&blMsg, BL_IOT_MSG_LEN(len));
}
int BL_SendBLMsgTo(BL_IOT_MSG_TYPE msgType, unsigned char* pData, unsigned int len, unsigned char dstMac[ETH_ALEN])
{
BL_IOT_MSG blMsg;
memset(&blMsg, 0, sizeof(BL_IOT_MSG));
blMsg.msgType = msgType;
blMsg.msglen = len;
memcpy(blMsg.dstMac, dstMac, ETH_ALEN);
strcpy(blMsg.msgTags, BL_IOT_MSG_TAGS);
if(pData && len > 0)
{
memcpy(blMsg.msgData, pData, len);
}
return __sendMessage((unsigned char*)&blMsg, BL_IOT_MSG_LEN(len));
}
int BL_Init(BlMsgCb cbOnMsg)
{
int ret = 0;
uv_thread_t uvRecvThread;
@ -129,7 +174,10 @@ int BL_Init(void)
return ret;
}
g_blMsgCb = cbOnMsg;
uv_thread_create(&uvRecvThread, __recvMsgThread, NULL);
return 0;
}
#endif

View File

@ -724,6 +724,7 @@ static void __dBusDeameonCb(MODULE_NAME modName, int status)
static void __assistantSyncThread(void *pParam)
{
int ret, i = 0, isSynced = -1;
struct tm* pTmNextYear;
LOG_EX(LOG_Debug, "Beging Sync holiday Database\n");
while(TRUE)
@ -826,6 +827,9 @@ static void __assistantSyncThread(void *pParam)
LOG_EX(LOG_Debug, "Send CMD_REMAIND_SYNC_REQ Command: %d\n", ret);
#endif
while(TRUE)
{
}
pthread_detach(pthread_self());
}
@ -852,6 +856,7 @@ int main(int argc, char **argv)
AlarmTimerInit(pLoop);
uv_thread_create(&uvSyncThread, __assistantSyncThread, NULL);
RunUVLoop(pLoop);
while(TRUE)

View File

@ -1041,6 +1041,7 @@ static void __otaDownloadImageCb(void *pParams)
pthread_detach(pthread_self());
}
#if 0
static void __dBusDeameonCb(MODULE_NAME modName, int status)
{
if(status)
@ -1059,6 +1060,7 @@ static void __dBusDeameonCb(MODULE_NAME modName, int status)
__otaRspStatus(OTA_CURRENT_REBOOT_TIME, g_iplInfo.setupStatus);
}
}
#endif
static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t* pLoop, DBusConnection* pConn, PDBUS_MSG_PACK pMsg)
{
@ -1112,6 +1114,9 @@ static PDBUS_MSG_PACK __dBusOnMessage(uv_loop_t* pLoop, DBusConnection* pConn, P
{
__otaRspStatus(OTA_SUCCESED, 1);
}
__otaRspStatus(OTA_CURRENT_VERSION, g_iplInfo.curVersion);
__otaRspStatus(OTA_CURRENT_SETUP_MODE, g_iplInfo.setupMode);
__otaRspStatus(OTA_CURRENT_REBOOT_TIME, g_iplInfo.setupStatus);
break;
case CMD_SYSTEM_STANDBY:
@ -1276,7 +1281,7 @@ int main(int argc, char **argv)
pBus = DBusWithLibuvInit(g_pMainLoop,
g_pModInfoTable[MODULE_OTA].modAliase,
__dBusOnMessage,
__dBusDeameonCb,
NULL,
NULL,
&ret);

View File

@ -1,2 +1,2 @@
COMMON_R16_LIBS := -ldbus-1 -luv -lcrypto -lcjson -ls2json -lsqlite3 -lnghttp2 -lquickmail -lz -luuid -lconfig -lcurl -lssl -lpthread
COMMON_R16_LIBS := -ldbus-1 -luv -lcrypto -lcjson -ls2json -lsqlite3 -lnghttp2 -lquickmail -lz -luuid -lconfig -lcurl -lssl -lpthread ../3partys/boardlink/libfastcon.a
COMMON_LINUX_LIBS := -ldbus-1 -luv -lcrypto -lcurl -lm -lnghttp2 -lsqlite3 -lquickmail -lz -luuid -lconfig -lssl -lpthread

View File

@ -58,14 +58,14 @@ PLAT_LINUX_SRCS := $(PLAT_R16_SRCS) \
# gcc CFLAGS
PLAT_R16_CFLAGS := -I../include -fPIC
PLAT_R16_CFLAGS := -I../include -fPIC -I../3partys/boardlink/
PLAT_LINUX_CFLAGS := $(PLAT_R16_CFLAGS) -I/usr/include/dbus-1.0/ -I/usr/lib/x86_64-linux-gnu/dbus-1.0/include/ -I../linux32/inc/ -I../linux32/inc/cjson/
PLAT_LINUX_CFLAGS += -I../linux32/inc/uthash/
PLAT_R16_LDFLAGS := -shared -fPIC
PLAT_LINUX_LDFLAGS := $(PLAT_R16_LDFLAGS)
R16_LIBS := $(COMMON_R16_LIBS)
R16_LIBS := $(COMMON_R16_LIBS)
LINUX_LIBS := $(COMMON_LINUX_LIBS)
# this line must be at below of thus, because of...

View File

@ -1,13 +1,15 @@
#ifndef BOARDLINK_IOT_H
#define BOARDLINK_IOT_H
#ifndef PLATFORM_CPU
#define BL_IOT_MSG_TAGS ("BLNLINK")
#define BL_MAX_MSG (1024)
#define ETH_ALEN (6)
typedef enum
{
MSG_IN_DISCOVERY_MODE = 0,
MSG_OUT_DISCOVERY_MODE,
MSG_GET_WLAN_INFO,
MSG_BYPASS_MODE,
} BL_IOT_MSG_TYPE;
@ -16,12 +18,27 @@ typedef enum
typedef struct
{
unsigned char msgTags[8];
unsigned char dstMac[ETH_ALEN];
unsigned char msgType;
unsigned int msglen;
unsigned char msgData[BL_MAX_MSG];
} BL_IOT_MSG, *PBL_IOT_MSG;
typedef struct
{
unsigned char channel;
unsigned char dev_mac[ETH_ALEN];
unsigned char bssid[ETH_ALEN];
unsigned char ssid[32];
} WL_INFO, *PWL_INFO;
#pragma pack (pop)
int BL_Init(void);
int BL_SendMessage(unsigned char* pData, unsigned int len);
#define BL_IOT_MSG_LEN(len) (sizeof(BL_IOT_MSG) - BL_MAX_MSG + len)
typedef void (*BlMsgCb)(PBL_IOT_MSG pMsg);
int BL_Init(BlMsgCb cbOnMsg);
int BL_SendBLMsg(BL_IOT_MSG_TYPE msgType, unsigned char* pData, unsigned int len);
int BL_SendBLMsgTo(BL_IOT_MSG_TYPE msgType, unsigned char* pData, unsigned int len, unsigned char dstMac[ETH_ALEN]);
#endif
#endif