-
Notifications
You must be signed in to change notification settings - Fork 224
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix #540, add event callback framework #541
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -57,6 +57,12 @@ struct OS_shared_global_vars | |
int32 MicroSecPerTick; | ||
int32 TicksPerSecond; | ||
|
||
/* | ||
* The event handler is an application-defined callback | ||
* that gets invoked as resources are created/configured/deleted. | ||
*/ | ||
OS_EventHandler_t EventHandler; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can there only be one? It might be useful to follow the observer/observable pattern and have a list of observers, so that apps can monitor events as well to get an idea of the state of the system. I am not fully briefed on the use-case for this work, so this thought may be beyond scope. |
||
|
||
#ifdef OSAL_CONFIG_DEBUG_PRINTF | ||
uint8 DebugLevel; | ||
#endif | ||
|
@@ -69,6 +75,16 @@ struct OS_shared_global_vars | |
*/ | ||
extern OS_SharedGlobalVars_t OS_SharedGlobalVars; | ||
|
||
/*--------------------------------------------------------------------------------------- | ||
Name: OS_NotifyEvent | ||
Purpose: Notify the user application of a change in the state of an OSAL resource | ||
returns: OS_SUCCESS on success, or relevant error code | ||
---------------------------------------------------------------------------------------*/ | ||
int32 OS_NotifyEvent(OS_Event_t event, osal_id_t object_id, void *data); | ||
|
||
|
||
/*--------------------------------------------------------------------------------------- | ||
Name: OS_API_Impl_Init | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -65,11 +65,36 @@ OS_SharedGlobalVars_t OS_SharedGlobalVars = | |
.ShutdownFlag = 0, | ||
.MicroSecPerTick = 0, /* invalid, _must_ be set by implementation init */ | ||
.TicksPerSecond = 0, /* invalid, _must_ be set by implementation init */ | ||
.EventHandler = NULL, | ||
#if defined(OSAL_CONFIG_DEBUG_PRINTF) | ||
.DebugLevel = 1, | ||
#endif | ||
}; | ||
|
||
|
||
/*---------------------------------------------------------------- | ||
* | ||
* Function: OS_NotifyEvent | ||
* | ||
* Purpose: Helper function to invoke the user-defined event handler | ||
* | ||
*-----------------------------------------------------------------*/ | ||
int32 OS_NotifyEvent(OS_Event_t event, osal_id_t object_id, void *data) | ||
{ | ||
int32 status; | ||
|
||
if (OS_SharedGlobalVars.EventHandler != NULL) | ||
{ | ||
status = OS_SharedGlobalVars.EventHandler(event, object_id, data); | ||
} | ||
else | ||
{ | ||
status = OS_SUCCESS; | ||
} | ||
|
||
return status; | ||
} | ||
|
||
/* | ||
********************************************************************************* | ||
* PUBLIC API (application-callable functions) | ||
|
@@ -199,6 +224,24 @@ int32 OS_API_Init(void) | |
return(return_code); | ||
} /* end OS_API_Init */ | ||
|
||
/*---------------------------------------------------------------- | ||
* | ||
* Function: OS_RegisterEventHandler | ||
* | ||
* Purpose: Implemented per public OSAL API | ||
* See description in API and header file for detail | ||
* | ||
*-----------------------------------------------------------------*/ | ||
int32 OS_RegisterEventHandler (OS_EventHandler_t handler) | ||
{ | ||
if (handler == NULL) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it an error if null is passed in? The effect would be to say "I am no longer interested in notifications," which seems like would not be an error. |
||
{ | ||
return OS_INVALID_POINTER; | ||
} | ||
|
||
OS_SharedGlobalVars.EventHandler = handler; | ||
return OS_SUCCESS; | ||
} | ||
|
||
/*---------------------------------------------------------------- | ||
* | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might make sense to have a bitmask so the caller can specify which events are of interest. This idea also scales forward as more events are added. It could be 32-bit or 64-bit number which gives that many extra bits for future use. Perhaps 0 means "all events."