简介

介绍sig mesh蓝牙协议里面的各种安全密钥

网络密钥netkey

介绍:

由配网器生成并分配给节点的,一个节点可以加入多个网络中,所以可以有多个网络密钥!

SDK代码参考

以下代码是一些代码片段,需要了解完整内容的话,可以在工程里面进行全局搜索!

  1. 项目中暂时不怎么需要用到,所有暂时没了解~~~

设备密钥devicekey

介绍:

是在配网的过程中,由配网器和入网设备共同生成的,每个节点只有1个,专门给config模型使用的(配置用的)!服务端和客户端一致,才能入网。

流程:

  1. 客户端(gateway节点)
    1. 生成devicekey
    2. 获取devicekey(根据需要配置的节点“地址”来获取)
    3. 在config模型的publish绑定中使用
    4. 发送config数据
  2. 服务端(light节点)
    1. 生成devicekey
    2. 默认绑定config模型,可以查看
    3. 根据客户端发来的命令,回调处理

SDK代码参考

以下代码是一些代码片段,需要了解完整内容的话,可以在工程里面进行全局搜索!

客户端(gateway节点):
1.定义生成

  1. #define UI_DEVICE_UUID {0x25, 0x22, 0x42, 0x54, 0x50, 0x02, 0x10, 0x03, 0x22, 0x14, 0x52, 0x81, 0x11, 0x14, 0x90, 0x22}

2.获取devicekey

  1. retval = MS_access_cm_get_device_key_handle
  2. (
  3. publish_info.addr.addr,
  4. &dev_key_handle
  5. );

3.在设置publish时,用devicekey绑定定config模型

  1. retval = MS_access_cm_set_model_publication
  2. (
  3. model_handle,
  4. &publish_info
  5. );

整体参考:

  1. //void UI_set_publish_address(UINT16 addr, MS_ACCESS_MODEL_HANDLE model_handle,UINT8 config_mode)里面的片段
  2. if(config_mode)
  3. {
  4. publish_info.remote = MS_FALSE;
  5. retval = MS_access_cm_get_device_key_handle
  6. (
  7. publish_info.addr.addr,
  8. &dev_key_handle//重点
  9. );
  10. if (API_SUCCESS == retval)
  11. {
  12. publish_info.appkey_index = MS_CONFIG_LIMITS(MS_MAX_APPS) + dev_key_handle;//重点
  13. CONSOLE_OUT("DevKey -> AppKey Index: 0x%04X\n", publish_info.appkey_index);
  14. }
  15. }
  16. retval = MS_access_cm_set_model_publication
  17. (
  18. model_handle,
  19. &publish_info
  20. );

4.发送config数据(在publish中配置好了目标地址和devicekey,调用MS_config_api.h里面的API直接发送命令即可)

  1. /* ----------------------------------------- Functions */
  2. /* Model Client - Configuration Models */
  3. /* Send Config Composition Data Get */
  4. void UI_config_client_get_composition_data(UCHAR page)
  5. {
  6. API_RESULT retval;
  7. ACCESS_CONFIG_COMPDATA_GET_PARAM param;
  8. CONSOLE_OUT
  9. ("Send Config Composition Data Get\n");
  10. param.page = page;
  11. retval = MS_config_client_composition_data_get(&param);
  12. CONSOLE_OUT
  13. ("Retval - 0x%04X\n", retval);
  14. }

服务端(light节点):
1.定义生成

  1. /** Unprovisioned device identifier */
  2. //DECL_STATIC PROV_DEVICE_S UI_lprov_device =
  3. PROV_DEVICE_S UI_lprov_device =
  4. {
  5. /** UUID */
  6. {0x25, 0x22, 0x42, 0x54, 0x50, 0x02, 0x10, 0x03, 0x22, 0x14, 0x52, 0x81, 0x11, 0x14, 0x90, 0x22},
  7. /** OOB Flag */
  8. 0x00,
  9. /**
  10. Encoded URI Information
  11. For example, to give a web address, "https://www.abc.com"
  12. the URI encoded data would be -
  13. 0x17 0x2F 0x2F 0x77 0x77 0x77 0x2E 0x61 0x62 0x63 0x2E 0x63 0x6F 0x6D
  14. where 0x17 is the URI encoding for https:
  15. */
  16. NULL
  17. };

2.绑定

  1. 默认绑定config模型

3.回调处理

  1. API_RESULT UI_app_config_server_callback

应用密钥appkey

介绍:

给应用层其他各种“功能模型”用的,相当于给模型添加”登录密码”,客户端模型和服务端模型的”登录密码”相同了,才能进行连接操作,模型可以有多个密钥,用来区分一些特定的需求。
image.png
流程

  1. 客户端(gateway节点)
    1. 存储appkey
    2. 获取appkey
    3. 自身模型绑定appkey,用于认证“对端设备”的appkey(这里的绑定类似于给功能模型加上密码)
    4. 在publish绑定里面使用appkey,用于给对端设备作认证(相当于填密码)
    5. 发送publish。
  2. 服务端(light节点)
    1. 获取appkey
    2. 绑定到模型上
    3. 根据客户端发来的命令,回调处理

SDK代码参考

以下代码是一些代码片段,需要了解完整内容的话,可以在工程里面进行全局搜索!

客户端(gateway节点):
1.添加appkey到本地数据库

  1. MS_access_cm_add_appkey
  2. (
  3. 0, /* subnet_handle */
  4. param.appkey_index, /* appkey_index */
  5. &param.appkey[0] /* app_key */
  6. );

2.通过句柄handle获取appkey数据(这里只是用来打印显示)

  1. retval = MS_access_cm_get_app_key
  2. (
  3. handle,
  4. &key,
  5. &aid
  6. );
  7. CONSOLE_OUT("App Key[0x%02X]: %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X %02X\r\n",
  8. handle, key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
  9. key[8], key[9], key[10], key[11], key[12], key[13], key[14], key[15]);

3.通过句柄handle来绑定第1步存储本地数据库里面的那个appkey(服务端的模型也要绑定)

  1. retval=MS_access_bind_model_app(UI_generic_onoff_client_model_handle, handle);
  2. #ifdef USE_VENDORMODEL
  3. retval=MS_access_bind_model_app(UI_vendor_defined_client_model_handle, handle);
  4. CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_vendor_defined_client_model_handle,handle);
  5. #endif

4.在注册publish时,用appkey绑定应用模型(只需调用索引即可)

  1. /* Set the Publish address for onoff and vendor model Client */
  2. UI_prov_data.uaddr = 0xCFFF;
  3. UI_set_publish_address(UI_prov_data.uaddr, UI_generic_onoff_client_model_handle,MS_FALSE);
  4. UI_set_publish_address(UI_prov_data.uaddr, UI_vendor_defined_client_model_handle,MS_FALSE);
  5. if(config_mode)
  6. {
  7. publish_info.remote = MS_FALSE;
  8. retval = MS_access_cm_get_device_key_handle
  9. (
  10. publish_info.addr.addr,
  11. &dev_key_handle
  12. );
  13. if (API_SUCCESS == retval)
  14. {
  15. publish_info.appkey_index = MS_CONFIG_LIMITS(MS_MAX_APPS) + dev_key_handle;
  16. CONSOLE_OUT("DevKey -> AppKey Index: 0x%04X\n", publish_info.appkey_index);
  17. }
  18. }
  19. else
  20. {
  21. publish_info.remote = MS_TRUE;
  22. publish_info.appkey_index = 0;//重点
  23. CONSOLE_OUT("AppKey Index: 0x%04X\n", publish_info.appkey_index);
  24. }
  25. //重点
  26. retval = MS_access_cm_set_model_publication
  27. (
  28. model_handle,
  29. &publish_info
  30. );

5.发送publish

  1. void UI_generic_onoff_set(UCHAR state)
  2. {
  3. API_RESULT retval;
  4. MS_GENERIC_ONOFF_SET_STRUCT param;
  5. CONSOLE_OUT
  6. ("Send Generic Onoff Set\n");
  7. param.onoff = state;
  8. param.tid = 0;
  9. param.optional_fields_present = 0x00;
  10. retval = MS_generic_onoff_set(&param);//重点
  11. CONSOLE_OUT
  12. ("Retval - 0x%04X\n", retval);
  13. }
  14. #define MS_generic_onoff_set(param) \
  15. MS_generic_onoff_client_send_reliable_pdu \
  16. (\
  17. MS_ACCESS_GENERIC_ONOFF_SET_OPCODE,\
  18. param,\
  19. MS_ACCESS_GENERIC_ONOFF_STATUS_OPCODE\
  20. )

服务端(light节点):
1.获取appkey

  1. retval = MS_access_cm_get_app_key
  2. (
  3. handle,
  4. &key,
  5. &aid
  6. );

2.绑定appkey

  1. retval=MS_access_bind_model_app(UI_generic_onoff_server_model_handle, handle);
  2. #ifdef USE_LIGHTNESS
  3. retval=MS_access_bind_model_app(UI_light_lightness_server_model_handle, handle);
  4. CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_light_lightness_server_model_handle,handle);
  5. #endif
  6. #ifdef USE_CTL
  7. retval=MS_access_bind_model_app(UI_light_ctl_server_model_handle, handle);
  8. CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_light_ctl_server_model_handle,handle);
  9. #endif
  10. #ifdef USE_HSL
  11. retval=MS_access_bind_model_app(UI_light_hsl_server_model_handle, handle);
  12. CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_light_hsl_server_model_handle,handle);
  13. #endif
  14. #ifdef USE_SCENE
  15. retval=MS_access_bind_model_app(UI_scene_server_model_handle, handle);
  16. CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_scene_server_model_handle,handle);
  17. #endif
  18. #ifdef USE_VENDORMODEL
  19. retval=MS_access_bind_model_app(UI_vendor_defined_server_model_handle, handle);
  20. CONSOLE_OUT("BINDING App Key %04x (%04x %04x)\n",retval,UI_vendor_defined_server_model_handle,handle);
  21. #endif

3.接收回调处理

  1. static API_RESULT UI_generic_onoff_server_cb
  2. (
  3. /* IN */ MS_ACCESS_MODEL_REQ_MSG_CONTEXT* ctx,
  4. /* IN */ MS_ACCESS_MODEL_REQ_MSG_RAW* msg_raw,
  5. /* IN */ MS_ACCESS_MODEL_REQ_MSG_T* req_type,
  6. /* IN */ MS_ACCESS_MODEL_STATE_PARAMS* state_params,
  7. /* IN */ MS_ACCESS_MODEL_EXT_PARAMS* ext_params
  8. )
  9. {
  10. MS_STATE_GENERIC_ONOFF_STRUCT param;
  11. MS_ACCESS_MODEL_STATE_PARAMS current_state_params;
  12. API_RESULT retval;
  13. retval = API_SUCCESS;
  14. /* Check message type */
  15. if (MS_ACCESS_MODEL_REQ_MSG_T_GET == req_type->type)
  16. {
  17. CONSOLE_OUT("[GENERIC_ONOFF] GET Request.\n");
  18. UI_generic_onoff_model_state_get(state_params->state_type, 0, &param, 0);
  19. current_state_params.state_type = state_params->state_type;
  20. current_state_params.state = &param;
  21. /* Using same as target state and remaining time as 0 */
  22. }
  23. else if (MS_ACCESS_MODEL_REQ_MSG_T_SET == req_type->type)
  24. {
  25. CONSOLE_OUT("[GENERIC_ONOFF] SET Request.\n");
  26. retval = UI_generic_onoff_model_state_set(state_params->state_type, 0, (MS_STATE_GENERIC_ONOFF_STRUCT*)state_params->state, 0);
  27. current_state_params.state_type = state_params->state_type;
  28. current_state_params.state = (MS_STATE_GENERIC_ONOFF_STRUCT*)state_params->state;
  29. }
  30. /* See if to be acknowledged */
  31. if (0x01 == req_type->to_be_acked)
  32. {
  33. CONSOLE_OUT("[GENERIC_ONOFF] Sending Response.\n");
  34. /* Parameters: Request Context, Current State, Target State (NULL: to be ignored), Remaining Time (0: to be ignored), Additional Parameters (NULL: to be ignored) */
  35. retval = MS_generic_onoff_server_state_update(ctx, &current_state_params, NULL, 0, NULL);
  36. }
  37. return retval;
  38. }