Skip to content

Shared static variable in callback_value.h #2

@moritz-h

Description

@moritz-h

While trying to understand the callback mechanism, I noticed that there is a shared static state between multiple callbacks if they use the callback_value.h template and have the same type. I'm not sure how problematic this is.

Here is the static variable is_buffered_:

static bool is_buffered_ = false;

For example the voltage current bricklet is using this function template 3 times for different callbacks, but as it is 3 times the same type they are using the exact same function and therefore the same static variable.
https://github.com/Tinkerforge/voltage-current-v2-bricklet/blob/5014a1eeda48e745d6122213a45b560e59aec1ed/software/src/communication.c#L106-L116

So if i.e. the voltage callback fills the buffer, but cannot send the message, the buffered voltage message may is send during calling the power callback handler. As the message content is also buffered in a static variable at least no wrong message is sent, but the wrong callback handler returns true, which seems to be used for sorting the callback functions here:

for(uint32_t _ = 0; _ < COMMUNICATION_CALLBACK_HANDLER_NUM; _++) {
if(communication_callbacks[cb_index]()) {
communication_callback_handler_t current = communication_callbacks[cb_index];
for(uint32_t i = cb_index; i < COMMUNICATION_CALLBACK_HANDLER_NUM-1; i++) {
communication_callbacks[i] = communication_callbacks[i+1];
}
communication_callbacks[COMMUNICATION_CALLBACK_HANDLER_NUM-1] = current;
} else {
cb_index++;
}
}
and handling the actual intended callback is delayed by one tick loop.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions