Callback Functions

Callback functions are used in a number of cases when writing an OCSDK application. If your application is designed to process specific events, you can register a callback function that will be invoked asynchronously when the event occurs. 

In most OCSDK sample applications, the local module terminates all events that the stack generates. This hides the series of event notifications that are happening (e.g. as the mote joins). Instead, the module can post semaphores when the mote is joined and/or has a service, simplifying your code. The OCSDK provides hooks for registering callback functions for certain events:

  • Data packets received from the network
  • Time notification - Contains the timestamp associated with a TIMEn trigger
  • Event notification - Note that if you register an Event callback, the local module cannot join for you since your application consumes the boot event. 
  • Advertisement received in Search mode (new in OCSDK 1.2.0)
  • TxDone notification (new in OCSDK 1.2.0) - tells your app that the packet with a given sequence number has been sent

In addition, a callback function is associated with an OSTimer and is triggered when the timer expires. 

Callback functions are not ISRs, but they have some things in common. In general, a callback function executes at higher priority than your application's tasks, so the following rules of thumb apply:

  • Keep the code in the callback to a minimum
  • Post a semaphore to an application task to process the event instead of handling it directly in the callback
  • Use globals to pass variables - don't use static variables in a callback
  • Don't use the critical section macros from uCOS-II

They differ from ISRs in that:

  • Interrupts cannot be suspended in a callback
  • A callback executes on a task stack that is associated with the trigger event.