FastCon配网命令

1 概述

本文档描述的是支持FastCon功能的模块,通过APP进行配网的时候,和APP报文交互的流程的描述。

FastCon底层支持两种配网:

因为业务的需要,在上述两种主要的配网方式的基础上,应用层和APP对于不同场景下进行的配网交互,重新定义了好几套不同的交互格式。

框架图如下:

graph TD; APP-.-推送零配; APP-.-主动零配; APP-.-推送子设备添加; APP-.-主动子设备添加; 推送零配---FastCon零配; 主动零配---FastCon零配; 推送子设备添加---FastCon子设备添加; 主动子设备添加---FastCon子设备添加;

2 推送零配/子设备添加格式

推送零配和推送子设备添加这两种方式是共用一套交互格式的。通过协议格式中字段数据配置字内容的不同,执行不同的流程。

2.1 推送零配流程

此流程中,state=5表示下发ssid和password成功,后续device是否连接上AP,以及是否连接上云端,master是无法获取到这些状态的。

可能存在state=5了,但是由于device在后续连接AP或者连接云端出现异常,导致device又重新退回到配网状态的情况。

sequenceDiagram participant device participant master participant APP participant server device->>master:消息推送 master->>server:消息推送 server->>APP:消息推送 APP->>master:2.2.1推送零配确认(config字段为1) master->>APP:2.2.1应答返回 APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为1) Note over device,master:双向认证 APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为2) Note over device,master:master下发ssid和password APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为3~4) Note over device,master:device上报token成功,,device开始去连接AP APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为5) APP->>master:2.2.3推送配网结束 master->>APP:2.2.3应答返回 Note over APP:若配网超时,APP下发2.2.3推送配网结束命令

2.1 推送子设备添加流程

sequenceDiagram participant device participant master participant APP participant server device->>master:消息推送 master->>server:消息推送 server->>APP:消息推送 APP->>master:2.2.1推送子设备添加(config字段为2) master->>APP:2.2.1应答返回 APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为1) Note over device,master:双向认证 APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为2) Note over device,master:master下发ssid和password APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为3~4) Note over device,master:device上报token APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为5) Note over device,master:device登陆master,device上线 APP->>master:2.2.2推送配网查询 master->>APP:2.2.2应答返回(返回json中的state为6) APP->>master:2.2.3推送配网结束 master->>APP:2.2.3应答返回 Note over APP:若配网超时,APP下发2.2.3推送配网结束命令

2.2 相关接口

APP下发走的是106透传的接口,使用标准的DNA串口头进行下发。

APP 通过 APPSDK 如下接口下发命令到 master:

	/**
     * Control device with data string composed by yourself.
     *
     * @param did Control wifi device did
     * @param sDid Control sub device did. If sdid = nil, just control wifi device.
     * @param dataStr Control data string composed by yourself
     * @param cmd Control command composed by yourself
     * @param configParam Config param
     * @return Control result
     */
    public static String dnaControl(String did, String sDid, String dataStr, String cmd, BLConfigParam configParam);
	/**
	 Control device with data string composed by yourself.

	 @param did             Control wifi device did
	 @param sDid            Control sub device did. If sdid = nil, just control wifi device.
	 @param dataStr         Control data string composed by yourself
	 @param command         Control command composed by yourself
	 @param scriptPath      Control device script. If scriptPath = nil, find device script in default store path.
	 @return                Control result
	 */
	- (NSString *_Nonnull)dnaControl:(NSString *_Nonnull)did subDevDid:(NSString *_Nullable)sDid dataStr:(NSString *_Nonnull)dataStr command:(NSString *_Nonnull)command scriptPath:(NSString *_Nullable)scriptPath;

2.2.1 配网确认

APP下发

cmd : “fastcon_device_add”

dataStr :

{
    "config":1,         //1:配网    2:子设备添加
    "timeout":100,      //单位为秒,配网超时时间
    "did":"00000000000000000000112233445566"    //一共32个字节的字符串,后面12个字节为mac地址,前面0补齐
}

master返回

{
    "state":1
    "fastcon_enable":1      //0:未开启fastcon功能,1:开启fastcon功能
}
state
-1 master正在配网中或者master不支持,无法处理配网确认命令
0 master为空闲
1 master确认配网/确认子设备添加
2 master双向认证
3 master获取token
4 master等待绑定
5 配网/子设备添加成功(推送零配到这边结束,后续待配设备自行连接AP路由器)
6 子设备登陆成功(推送子设备添加到这边才算成功)

2.2.2 查询配网状态

APP下发

cmd : “fastcon_device_add_status_query”

dataStr :

{
    "did":"00000000000000000000112233445566"    //一共32个字节的字符串,后面12个字节为mac地址,前面0补齐
}

master返回

{
    "state":1,
    "fastcon_enable":1,
    ["token":"xxxxxxxx"]
}

备注:只有当state大于3的时候,token才会有

2.2.3 配网结束

APP下发

cmd : “fastcon_cancel”

dataStr :

{
    "did":"00000000000000000000112233445566"    //一共32个字节的字符串,后面12个字节为mac地址,前面0补齐
}

master返回

{
    "state":0
    "fastcon_enable":1      //0:未开启fastcon功能,1:开启fastcon功能
}

2.2.4 子设备控制

APP下发

cmd : “fastcon_client_control”

dataStr :

{
    "did" : "00000000000000000000112233445566",  //控制子设备的did
    "cmd" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"	 //子设备控制命令,透传指令,可以通过 sdk 接口 dev_data 命令获取
}

master返回

{
    "status":0,                                     //控制返回状态
    "did" : "00000000000000000000112233445566",     //控制子设备的did
    "cmd" : "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"	    //子设备控制命令返回数据,同样可以通过 sdk 接口转换成 key-value 格式
}

3 主动零配格式

3.1 主要流程

此流程主要描述APP和master之间的交互,中间双向认证等其他流程有所省略。

sequenceDiagram participant device participant master participant APP Note over master:master在运行过程中会去发现周围待配网的device APP->>master:3.2.1主动获取待配网设备列表 master->>APP:3.2.1应答返回 APP->>master:3.2.2主动零配 master->>APP:3.2.2应答返回 APP->>master:3.2.3获取配网状态 master->>APP:3.2.3应答返回(status:1) Note over device,master:双向认证 APP->>master:3.2.3获取配网状态 master->>APP:3.2.3应答返回(status:1) Note over device,master:master下发ssid和password,上报token成功,device开始去连接AP APP->>master:3.2.3获取配网状态 master->>APP:3.2.3获取配网状态(status:2) APP->>master:2.2.3推送配网结束 master->>APP:2.2.3应答返回 Note over APP:若配网超时,APP下发2.2.3推送配网结束命令

3.2 相关接口

APP 通过 APPSDK 如下接口下发命令到 master:

	/**
     * Control device with data string composed by yourself.
     *
     * @param did Control wifi device did
     * @param sDid Control sub device did. If sdid = nil, just control wifi device.
     * @param dataStr Control data string composed by yourself
     * @param cmd Control command composed by yourself
     * @param configParam Config param
     * @return Control result
     */
    public static String dnaControl(String did, String sDid, String dataStr, String cmd, BLConfigParam configParam);
	/**
	 Control device with data string composed by yourself.

	 @param did             Control wifi device did
	 @param sDid            Control sub device did. If sdid = nil, just control wifi device.
	 @param dataStr         Control data string composed by yourself
	 @param command         Control command composed by yourself
	 @param scriptPath      Control device script. If scriptPath = nil, find device script in default store path.
	 @return                Control result
	 */
	- (NSString *_Nonnull)dnaControl:(NSString *_Nonnull)did subDevDid:(NSString *_Nullable)sDid dataStr:(NSString *_Nonnull)dataStr command:(NSString *_Nonnull)command scriptPath:(NSString *_Nullable)scriptPath;

command : “fastcon_no_config”

dataStr 为 JSON String. 其中 act 参数代表零配的几个步骤,具体如下:

act
0 获取待配网设备列表
1 批量配网
2 获取配网状态

3.2.1 获取待配网设备列表

APP下发

{
	"did":"xx",					//普通模块did
	"act" : 0, 
	"count": xx, 				//查询数量
	"index": xx, 				//查询开始索引
}

master返回

{
	"did":"xx",					//普通模块did
	"status": 0, 					//非0表示失败
	"total": xx,  					//设备总数
	"index": xx, 					//查询开始索引
	"devlist" : [
		{"pid": "xx", "did":"xx"},
		{"pid": "xx", "did":"xx"},
	    {"pid": "xx", "did":"xx"},
	]
}

3.2.2 批量配网

APP下发

{
	"did":"xx",					//普通模块did
	"act" : 1, 
	"devlist" : [
		"did0","did1","did2"		//待配网设备的did列表
		]
}

master返回

{
	"did":"xx",					//普通模块did
	"status": 0, 					//非0表示失败
}

说明:最多支持8个设备同时配网

3.2.3 获取配网状态

APP下发

{
	"did":"xx",					//普通模块did
	"act" : 2, 
	"devlist" : [
		"did0","did1","did2"		//待配网设备的did列表
		]
}

master返回

{
	"did":"xx",					//普通模块did
	"status": 0, 					//非0表示失败

	"devlist":[
		{"did":"xx", "status":xx},
		{"did":"xx", "status":xx},
	    {"did":"xx", "status":xx},
	]
}

配网状态定义:

status
0 等待配网
1 正在配网
2 配网成功
3 配网超时
4 设备离线(设备不存在)

4 主动添加子设备

主动添加子设备是兼容了原先433添加子设备的流程。

详细定义在 网关及子设备功能