How to bind app key to local server

zacwbond
Posts: 1
Joined: Thu May 21, 2020 9:46 pm

How to bind app key to local server

Postby zacwbond » Thu May 21, 2020 10:05 pm

I am creating a very simple BLE mesh network. I have enabled both the provisioner and node roles on all devices. When I press a button on one device, it will begin provisioning all other devices within range. This part works fine.

Each device has a single vendor server. When the device state changes, any device is to publish a message to the all nodes address. The provisioner uses its configuration client to bind an app key to the servers on the other nodes, and also to set the publication address.

What I don't understand is what I need to do to get the provisioner device also publishing in exactly the same way. Here's what I've tried, extracted from the provisioner event handler:
  1. case ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT:
  2.             ESP_LOGI(TAG,
  3.                      "ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT, "
  4.                      "err_code %d",
  5.                      param->provisioner_add_app_key_comp.err_code);
  6.             if (param->provisioner_add_app_key_comp.err_code == 0)
  7.             {
  8.                 prov_key.app_idx = param->provisioner_add_app_key_comp.app_idx;
  9.                 esp_err_t err =
  10.                     esp_ble_mesh_provisioner_bind_app_key_to_local_model(PROV_OWN_ADDR,
  11.                                                                          prov_key
  12.                                                                              .app_idx,
  13.                                                                          MODEL_ID,
  14.                                                                          COMPANY_ID);
  15.  
  16.                 if (err != ESP_OK)
  17.                 {
  18.                     ESP_LOGE(TAG, "Failed to bind AppKey to vendor client");
  19.                 }
  20.             }
  21.             break;
  22.  
  23.         case ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT:
  24.             ESP_LOGI(TAG,
  25.                      "ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, "
  26.                      "err_code %d",
  27.                      param->provisioner_add_app_key_comp.err_code);
  28.             if (param->provisioner_bind_app_key_to_model_comp.err_code == 0)
  29.             {
  30.                 // Configure our local node to publish to 0xFFFF.
  31.                 myPub.publish_addr = 0xFFFF;
  32.                 myPub.retransmit   = 0;
  33.                 myPub.ttl          = 7;
  34.                 myPub.period       = 0;
  35.                 myPub.app_idx =
  36.                     param->provisioner_bind_app_key_to_model_comp.app_idx;
  37.             }
  38.             break;
But when I call esp_ble_mesh_model_publish(), it expects a role. If I give it ROLE_NODE, I get an error that there's no appkey (even though I just bound it). If I give it ROLE_PROVISIONER, I get the error
E (970394) BLE_MESH: bt_mesh_set_client_model_role, Invalid parameter
E (970394) BLE_MESH: btc_ble_mesh_model_call_handler, Failed to set model role
Why does publish care what role I'm in? Isn't it just sending a message?

Any suggestions on getting this to work? I think I need to add the app key to the list of keys used when it publishes as a node, but I don't know how to do that locally (as opposed to using a configuration client on another node to set it up).

esp_liu
Posts: 35
Joined: Wed Nov 28, 2018 4:12 am

Re: How to bind app key to local server

Postby esp_liu » Fri May 22, 2020 7:40 am

zacwbond wrote:
Thu May 21, 2020 10:05 pm
I am creating a very simple BLE mesh network. I have enabled both the provisioner and node roles on all devices. When I press a button on one device, it will begin provisioning all other devices within range. This part works fine.

Each device has a single vendor server. When the device state changes, any device is to publish a message to the all nodes address. The provisioner uses its configuration client to bind an app key to the servers on the other nodes, and also to set the publication address.

What I don't understand is what I need to do to get the provisioner device also publishing in exactly the same way. Here's what I've tried, extracted from the provisioner event handler:
  1. case ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT:
  2.             ESP_LOGI(TAG,
  3.                      "ESP_BLE_MESH_PROVISIONER_ADD_LOCAL_APP_KEY_COMP_EVT, "
  4.                      "err_code %d",
  5.                      param->provisioner_add_app_key_comp.err_code);
  6.             if (param->provisioner_add_app_key_comp.err_code == 0)
  7.             {
  8.                 prov_key.app_idx = param->provisioner_add_app_key_comp.app_idx;
  9.                 esp_err_t err =
  10.                     esp_ble_mesh_provisioner_bind_app_key_to_local_model(PROV_OWN_ADDR,
  11.                                                                          prov_key
  12.                                                                              .app_idx,
  13.                                                                          MODEL_ID,
  14.                                                                          COMPANY_ID);
  15.  
  16.                 if (err != ESP_OK)
  17.                 {
  18.                     ESP_LOGE(TAG, "Failed to bind AppKey to vendor client");
  19.                 }
  20.             }
  21.             break;
  22.  
  23.         case ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT:
  24.             ESP_LOGI(TAG,
  25.                      "ESP_BLE_MESH_PROVISIONER_BIND_APP_KEY_TO_MODEL_COMP_EVT, "
  26.                      "err_code %d",
  27.                      param->provisioner_add_app_key_comp.err_code);
  28.             if (param->provisioner_bind_app_key_to_model_comp.err_code == 0)
  29.             {
  30.                 // Configure our local node to publish to 0xFFFF.
  31.                 myPub.publish_addr = 0xFFFF;
  32.                 myPub.retransmit   = 0;
  33.                 myPub.ttl          = 7;
  34.                 myPub.period       = 0;
  35.                 myPub.app_idx =
  36.                     param->provisioner_bind_app_key_to_model_comp.app_idx;
  37.             }
  38.             break;
But when I call esp_ble_mesh_model_publish(), it expects a role. If I give it ROLE_NODE, I get an error that there's no appkey (even though I just bound it). If I give it ROLE_PROVISIONER, I get the error
E (970394) BLE_MESH: bt_mesh_set_client_model_role, Invalid parameter
E (970394) BLE_MESH: btc_ble_mesh_model_call_handler, Failed to set model role
Why does publish care what role I'm in? Isn't it just sending a message?

Any suggestions on getting this to work? I think I need to add the app key to the list of keys used when it publishes as a node, but I don't know how to do that locally (as opposed to using a configuration client on another node to set it up).
Hi zacwbond,

First of all, because currently in the mesh stack, we use different spaces to store AppKeys for node and Provisioner, so when publishing a message, we need the device role to get the right AppKey.

The above event for the completion of binding AppKey is for Provisioner. And for node, it should wait for the Config Model App Bind from the Provisioner, then the AppKey can be used by the model within the node.

But since this way is not expected, maybe you can directly add the AppKey Index to the list of the corresponding model after the AppKey is added, it will work. Later we may add an API for the node to complete such operations, although it is not a recommended and standard operation.

And thanks for the question, we find an issue when a client model within Provisioner tries to publish a message, we have fixed it.

Thanks

Who is online

Users browsing this forum: Bing [Bot] and 250 guests