I have a code which communicates with external hardware in the system. As per the events sent by the external hardware the state in my C source will change. There are some handshakes happening during the system start and some informations need to be sent to other modules based on the handshakes. I have set some global variables in my callback but it is observed that external hardware is sending some events twice which makes some of the callbacks being called twice. This corrupts the information in the global variable.
int global_value = 0;
int eventcb()
{
if (some condition)
global_value = 1;
else if (some condition)
global_value = 2;
else
global_value = 0;
}
First time when the above code is called global_value = 1 let’s say ; next time when the call back is called the condition will not be present so global_value will become 0. I don’t want to use another global variable to keep track of the number of times this call back is called or even file based approach (creating a file in the file system). Is there any optimum way to handle this situation ? I do not have control on the external hardware to make it send the event only once.
Basically make a state transition change only if the global is in it’s initial state.
Of course you can add more conditions to your
ifstatements to make sure that all of your state transitions are covered properly.