NCS BLE mesh memory safety of mesh send and publish functions

I am working on a BLE mesh project, and I have created my own BLE mesh publish and send functions,

int vendor_model_send(uint16_t addr, uint8_t * p_data, uint16_t length)
{
    int err = 0;

    // create a dummy context
    struct bt_mesh_msg_ctx context = 
    {
        .addr     = addr,
        .app_idx  = 0,
        .net_idx  = 0,
        .send_rel = false,
    };

    struct net_buf_simple *msg = NET_BUF_SIMPLE(VND_MSG_BUFFER_SIZE);
    bt_mesh_model_msg_init(msg, BT_MESH_MODEL_OP_3(0x04, CID_ZEPHYR));

    uint16_t old_size = msg->len;
    net_buf_simple_add(msg, length);
    (void)memcpy(msg->data + old_size, p_data, length);

    err = bt_mesh_model_send(&vnd_models[0], &context, msg, NULL, NULL);
    if (err != 0)
    {
        printk("\rUnable to send VENDOR Status response\n");
    }
    else
    {
        // do nothing, sending was successful
    }

    return err;
}

int vendor_model_publish(enum vendor_model_opcodes opcode, uint8_t *p_data, uint16_t length)
{
    int err = 0;

    bt_mesh_model_msg_init(vnd_models[0].pub->msg, BT_MESH_MODEL_OP_3((uint32_t)opcode, CID_ZEPHYR));

    for (uint16_t packet_num = 0; packet_num < length; packet_num++)
    {
        uint8_t packet_data = 0;
        (void)memcpy(&packet_data, &p_data[packet_num], sizeof(packet_data));
        net_buf_simple_add_u8(vnd_models[0].pub->msg, packet_data);
    }

    err = bt_mesh_model_publish(&vnd_models[0]);
    if (err != 0)
    {
        printk("bt_mesh_model_publish: err: %d\n", err);
    }
    else
    {
        // do nothing, publish was successful
    }

    return err;
}

My concern is, I am creating buffers using the bt_mesh_model_msg_init() and NET_BUF_SIMPLE() functions, which I suspect might perform dynamic allocation. And I'm not freeing these variables. Can anyone kindly confirm if that is not the case? Or tell me how I can find that out for myself?

Any help would be much appreciated.

Best regards,

Parents
  • Hello,

    To me, it looks like bt_mesh_model_msg_init() is used by most of our mesh examples, so I think this one is safe to use. The same goes for NET_BUF_SIMPLE_DEFINE(), if that is what you meant? If you mean NET_BUF_SIMPLE(), can you please let me know where you found it being used, and where you found the implementation of it (if you found it). 

    Or tell me how I can find that out for myself?

    I suggest looking at our samples that are closest to what you want to acheive, such as the mesh samples in this case. When you are using Bluetooth Mesh, or the "normal" BLE samples which both uses the SoftDevice Controller (the Bluetooth Low Energy Stack), these buffers are usually copied locally to the bluetooth stack, and handled (released) when they are done with that message.

    Both of the functions (function and macro) that you refer to, are just creating buffers that are being used in the function where they are being called, and the content of the buffer is copied into the softdevice controller, as far as I can tell.

    Best regards,

    Edvin

  • I did make them from the BLE mesh vendor model example provided. Sounds like it is safe to use. 

Reply Children
No Data
Related