简介
网络密钥netkey
介绍:
由配网器生成并分配给节点的,一个节点可以加入多个网络中,所以可以有多个网络密钥!
SDK代码参考
以下代码是一些代码片段,需要了解完整内容的话,可以在工程里面进行全局搜索!
项目中暂时不怎么需要用到,所有暂时没了解~~~
设备密钥devicekey
介绍:
是在配网的过程中,由配网器和入网设备共同生成的,每个节点只有1个,专门给config模型使用的(配置用的)!服务端和客户端一致,才能入网。
流程:
- 客户端(gateway节点)
- 生成devicekey
- 获取devicekey(根据需要配置的节点“地址”来获取)
- 在config模型的publish绑定中使用
- 发送config数据
- 服务端(light节点)
- 生成devicekey
- 默认绑定config模型,可以查看
- 根据客户端发来的命令,回调处理
SDK代码参考
以下代码是一些代码片段,需要了解完整内容的话,可以在工程里面进行全局搜索!
客户端(gateway节点):
1.定义生成
#define UI_DEVICE_UUID {0x25, 0x22, 0x42, 0x54, 0x50, 0x02, 0x10, 0x03, 0x22, 0x14, 0x52, 0x81, 0x11, 0x14, 0x90, 0x22}
2.获取devicekey
retval = MS_access_cm_get_device_key_handle
(
publish_info.addr.addr,
&dev_key_handle
);
3.在设置publish时,用devicekey绑定定config模型
retval = MS_access_cm_set_model_publication
(
model_handle,
&publish_info
);
整体参考:
//void UI_set_publish_address(UINT16 addr, MS_ACCESS_MODEL_HANDLE model_handle,UINT8 config_mode)里面的片段
if(config_mode)
{
publish_info.remote = MS_FALSE;
retval = MS_access_cm_get_device_key_handle
(
publish_info.addr.addr,
&dev_key_handle//重点
);
if (API_SUCCESS == retval)
{
publish_info.appkey_index = MS_CONFIG_LIMITS(MS_MAX_APPS) + dev_key_handle;//重点
CONSOLE_OUT("DevKey -> AppKey Index: 0x%04X\n", publish_info.appkey_index);
}
}
retval = MS_access_cm_set_model_publication
(
model_handle,
&publish_info
);
4.发送config数据(在publish中配置好了目标地址和devicekey,调用MS_config_api.h里面的API直接发送命令即可)
/* ----------------------------------------- Functions */
/* Model Client - Configuration Models */
/* Send Config Composition Data Get */
void UI_config_client_get_composition_data(UCHAR page)
{
API_RESULT retval;
ACCESS_CONFIG_COMPDATA_GET_PARAM param;
CONSOLE_OUT
("Send Config Composition Data Get\n");
param.page = page;
retval = MS_config_client_composition_data_get(¶m);
CONSOLE_OUT
("Retval - 0x%04X\n", retval);
}
服务端(light节点):
1.定义生成
/** Unprovisioned device identifier */
//DECL_STATIC PROV_DEVICE_S UI_lprov_device =
PROV_DEVICE_S UI_lprov_device =
{
/** UUID */
{0x25, 0x22, 0x42, 0x54, 0x50, 0x02, 0x10, 0x03, 0x22, 0x14, 0x52, 0x81, 0x11, 0x14, 0x90, 0x22},
/** OOB Flag */
0x00,
/**
Encoded URI Information
For example, to give a web address, "https://www.abc.com"
the URI encoded data would be -
0x17 0x2F 0x2F 0x77 0x77 0x77 0x2E 0x61 0x62 0x63 0x2E 0x63 0x6F 0x6D
where 0x17 is the URI encoding for https:
*/
NULL
};
2.绑定
默认绑定config模型
3.回调处理
API_RESULT UI_app_config_server_callback
应用密钥appkey
介绍:
给应用层其他各种“功能模型”用的,相当于给模型添加”登录密码”,客户端模型和服务端模型的”登录密码”相同了,才能进行连接操作,模型可以有多个密钥,用来区分一些特定的需求。
流程
- 客户端(gateway节点)
- 存储appkey
- 获取appkey
- 自身模型绑定appkey,用于认证“对端设备”的appkey(这里的绑定类似于给功能模型加上密码)
- 在publish绑定里面使用appkey,用于给对端设备作认证(相当于填密码)
- 发送publish。
- 服务端(light节点)
- 获取appkey
- 绑定到模型上
- 根据客户端发来的命令,回调处理
SDK代码参考
以下代码是一些代码片段,需要了解完整内容的话,可以在工程里面进行全局搜索!
客户端(gateway节点):
1.添加appkey到本地数据库
MS_access_cm_add_appkey
(
0, /* subnet_handle */
param.appkey_index, /* appkey_index */
¶m.appkey[0] /* app_key */
);
2.通过句柄handle获取appkey数据(这里只是用来打印显示)
retval = MS_access_cm_get_app_key
(
handle,
&key,
&aid
);
CONSOLE_OUT("App Key[0x%02X]: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
handle, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
key[8], key[9], key[10], key[11], key[12], key[13], key[14], key[15]);
3.通过句柄handle来绑定第1步存储本地数据库里面的那个appkey(服务端的模型也要绑定)
retval=MS_access_bind_model_app(UI_generic_onoff_client_model_handle, handle);
#ifdef USE_VENDORMODEL
retval=MS_access_bind_model_app(UI_vendor_defined_client_model_handle, handle);
CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_vendor_defined_client_model_handle,handle);
#endif
4.在注册publish时,用appkey绑定应用模型(只需调用索引即可)
/* Set the Publish address for onoff and vendor model Client */
UI_prov_data.uaddr = 0xCFFF;
UI_set_publish_address(UI_prov_data.uaddr, UI_generic_onoff_client_model_handle,MS_FALSE);
UI_set_publish_address(UI_prov_data.uaddr, UI_vendor_defined_client_model_handle,MS_FALSE);
if(config_mode)
{
publish_info.remote = MS_FALSE;
retval = MS_access_cm_get_device_key_handle
(
publish_info.addr.addr,
&dev_key_handle
);
if (API_SUCCESS == retval)
{
publish_info.appkey_index = MS_CONFIG_LIMITS(MS_MAX_APPS) + dev_key_handle;
CONSOLE_OUT("DevKey -> AppKey Index: 0x%04X\n", publish_info.appkey_index);
}
}
else
{
publish_info.remote = MS_TRUE;
publish_info.appkey_index = 0;//重点
CONSOLE_OUT("AppKey Index: 0x%04X\n", publish_info.appkey_index);
}
//重点
retval = MS_access_cm_set_model_publication
(
model_handle,
&publish_info
);
5.发送publish
void UI_generic_onoff_set(UCHAR state)
{
API_RESULT retval;
MS_GENERIC_ONOFF_SET_STRUCT param;
CONSOLE_OUT
("Send Generic Onoff Set\n");
param.onoff = state;
param.tid = 0;
param.optional_fields_present = 0x00;
retval = MS_generic_onoff_set(¶m);//重点
CONSOLE_OUT
("Retval - 0x%04X\n", retval);
}
#define MS_generic_onoff_set(param) \
MS_generic_onoff_client_send_reliable_pdu \
(\
MS_ACCESS_GENERIC_ONOFF_SET_OPCODE,\
param,\
MS_ACCESS_GENERIC_ONOFF_STATUS_OPCODE\
)
服务端(light节点):
1.获取appkey
retval = MS_access_cm_get_app_key
(
handle,
&key,
&aid
);
2.绑定appkey
retval=MS_access_bind_model_app(UI_generic_onoff_server_model_handle, handle);
#ifdef USE_LIGHTNESS
retval=MS_access_bind_model_app(UI_light_lightness_server_model_handle, handle);
CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_light_lightness_server_model_handle,handle);
#endif
#ifdef USE_CTL
retval=MS_access_bind_model_app(UI_light_ctl_server_model_handle, handle);
CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_light_ctl_server_model_handle,handle);
#endif
#ifdef USE_HSL
retval=MS_access_bind_model_app(UI_light_hsl_server_model_handle, handle);
CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_light_hsl_server_model_handle,handle);
#endif
#ifdef USE_SCENE
retval=MS_access_bind_model_app(UI_scene_server_model_handle, handle);
CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_scene_server_model_handle,handle);
#endif
#ifdef USE_VENDORMODEL
retval=MS_access_bind_model_app(UI_vendor_defined_server_model_handle, handle);
CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_vendor_defined_server_model_handle,handle);
#endif
3.接收回调处理
static API_RESULT UI_generic_onoff_server_cb
(
/* IN */ MS_ACCESS_MODEL_REQ_MSG_CONTEXT* ctx,
/* IN */ MS_ACCESS_MODEL_REQ_MSG_RAW* msg_raw,
/* IN */ MS_ACCESS_MODEL_REQ_MSG_T* req_type,
/* IN */ MS_ACCESS_MODEL_STATE_PARAMS* state_params,
/* IN */ MS_ACCESS_MODEL_EXT_PARAMS* ext_params
)
{
MS_STATE_GENERIC_ONOFF_STRUCT param;
MS_ACCESS_MODEL_STATE_PARAMS current_state_params;
API_RESULT retval;
retval = API_SUCCESS;
/* Check message type */
if (MS_ACCESS_MODEL_REQ_MSG_T_GET == req_type->type)
{
CONSOLE_OUT("[GENERIC_ONOFF] GET Request.\n");
UI_generic_onoff_model_state_get(state_params->state_type, 0, ¶m, 0);
current_state_params.state_type = state_params->state_type;
current_state_params.state = ¶m;
/* Using same as target state and remaining time as 0 */
}
else if (MS_ACCESS_MODEL_REQ_MSG_T_SET == req_type->type)
{
CONSOLE_OUT("[GENERIC_ONOFF] SET Request.\n");
retval = UI_generic_onoff_model_state_set(state_params->state_type, 0, (MS_STATE_GENERIC_ONOFF_STRUCT*)state_params->state, 0);
current_state_params.state_type = state_params->state_type;
current_state_params.state = (MS_STATE_GENERIC_ONOFF_STRUCT*)state_params->state;
}
/* See if to be acknowledged */
if (0x01 == req_type->to_be_acked)
{
CONSOLE_OUT("[GENERIC_ONOFF] Sending Response.\n");
/* Parameters: Request Context, Current State, Target State (NULL: to be ignored), Remaining Time (0: to be ignored), Additional Parameters (NULL: to be ignored) */
retval = MS_generic_onoff_server_state_update(ctx, ¤t_state_params, NULL, 0, NULL);
}
return retval;
}