Skip to content

Commit

Permalink
Add gh66 : Adding additional callback register function for handling …
Browse files Browse the repository at this point in the history
…GET request
  • Loading branch information
kanjoe24 committed Feb 4, 2025
1 parent d710c52 commit ef4672b
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 128 deletions.
38 changes: 10 additions & 28 deletions include/ut_control_plane.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,48 +45,33 @@ typedef struct
int32_t value;
} ut_control_keyStringMapping_t;

typedef enum
{
POST = 0,
GET,
INVALID
}eRestApi_t;

typedef void ut_controlPlane_instance_t; /*!< Handle to a control plane instance */

/**
* @brief Callback function type for handling REST_API from POST triggers.
* @brief Callback function type for handling POST triggers.
*
* This callback function is invoked when a POST request is received
* at a registered endpoint and the triggerKey is matched on the incoming data.
* at the ut control server and the triggerKey is matched on the incoming data.
*
* @param triggerKey The trigger key that was matched.
* @param instance The key-value pair instance containing the incoming data.
* @param userData User-defined data passed to the callback function.
*/
typedef void (*ut_control_REST_API_POST_callback_t)(char *triggerKey, ut_kvp_instance_t *instance, void *userData);
typedef void (*ut_control_on_message_callback_t)(char *triggerKey, ut_kvp_instance_t *instance, void *userData);

/**
* @brief Callback function for handling REST_API from GET triggers.
*
* This callback function is invoked when a GET request is received
* at a registered endpoint. It allows the user to implement the
* REST API call.
* at UT control server and the rest api name of the GET request matches the restAPI parameter
* registered with the UT control server.
*
* @param restAPI The name of the REST API being called.
* @param userData User-defined data passed to the callback function.
*
* @returns A character string containing the result of the API call, in JSON or YAML format
*/
typedef char *(*ut_control_REST_API_GET_callback_t)(char *restAPI, void *userData);

typedef struct
{
ut_control_REST_API_GET_callback_t callbackFunctionGET;
ut_control_REST_API_POST_callback_t callbackFunctionPOST;
eRestApi_t restApiType; // POST = 0, GET = 1
void *userData; // user-defined data
} ut_control_rest_api_handler_t;
typedef char *(*ut_control_endpoint_callback_t)(char *restAPI, void *userData);

/**
* @brief Initializes a control plane instance.
Expand All @@ -101,18 +86,16 @@ ut_controlPlane_instance_t* UT_ControlPlane_Init( uint32_t monitorPort );
* @param key - Null-terminated string representing the message key to trigger the callback.
* @param callbackFunction - Callback function to be invoked when the key is received.
* @param userData - Handle to the caller instance.
* @param restApiType - Type of REST API to be registered.(POST = 0, GET = 1)
* @returns Status of the registration operation (`ut_control_plane_status_t`).
* @retval UT_CONTROL_PLANE_STATUS_OK - Success
* @retval UT_CONTROL_PLANE_STATUS_INVALID_HANDLE - Invalid control plane instance handle.
* @retval UT_CONTROL_PLANE_STATUS_INVALID_PARAM - Invalid parameter passed
* @retval UT_CONTROL_PLANE_STATUS_CALLBACK_LIST_FULL - Callback list is full
* *******************NOTE: This function will be deprecated in future major releases.*******************
*/
ut_control_plane_status_t UT_ControlPlane_RegisterCallbackOnMessage(ut_controlPlane_instance_t *pInstance,
char *key,
ut_control_REST_API_POST_callback_t callbackFunction,
void *userData, eRestApi_t restApiType);
ut_control_on_message_callback_t callbackFunction,
void *userData);

/**
* @brief Registers a callback function for REST API endpoint.
Expand All @@ -126,10 +109,9 @@ ut_control_plane_status_t UT_ControlPlane_RegisterCallbackOnMessage(ut_controlPl
*
* @returns A status code indicating the success or failure of the registration.
*/
ut_control_plane_status_t UT_ControlPlane_RegisterAPIEndpointHandler(
ut_control_plane_status_t UT_ControlPlane_RegisterEndPointCallback(
ut_controlPlane_instance_t *pInstance,
char *restAPI,
ut_control_rest_api_handler_t *handler);
char *restAPI, ut_control_endpoint_callback_t callbackFunction, void *userData);

/**
* @brief Starts the control plane listening for incoming messages.
Expand Down
58 changes: 22 additions & 36 deletions src/ut_control_plane.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,16 @@

typedef struct
{
char key[UT_KVP_MAX_ELEMENT_SIZE];
ut_control_REST_API_POST_callback_t pCallback;
ut_control_REST_API_GET_callback_t pStringCallback;
void* userData;
eRestApi_t pRestApi;
}CallbackEntry_t;
ut_control_on_message_callback_t pCallback;
ut_control_endpoint_callback_t pStringCallback;
}Callback_type_t;

typedef struct
{
char key[UT_KVP_MAX_ELEMENT_SIZE];
Callback_type_t pCallbackType;
void *userData;
} CallbackEntry_t;

typedef enum
{
Expand Down Expand Up @@ -170,7 +174,7 @@ static void call_callback_on_match(cp_message_t *mssg, ut_cp_instance_internal_t
if (UT_KVP_STATUS_SUCCESS == ut_kvp_getStringField(pkvpInstance, entry.key, result_kvp, UT_KVP_MAX_ELEMENT_SIZE))
{
// call callback
entry.pCallback(entry.key, pkvpInstance, entry.userData);
entry.pCallbackType.pCallback(entry.key, pkvpInstance, entry.userData);
}
}
ut_kvp_destroyInstance(pkvpInstance);
Expand Down Expand Up @@ -321,7 +325,7 @@ static char* create_response(ut_cp_instance_internal_t *pInternal, const char* k

if (compareStrings(entry.key, key) == 0)
{
kvpData = entry.pStringCallback((char *)key, entry.userData);
kvpData = entry.pCallbackType.pStringCallback((char *)key, entry.userData);

pkvpInstance = ut_kvp_createInstance();
// The `kvpData` memory passed gets freed as part of destroy instance
Expand Down Expand Up @@ -712,7 +716,7 @@ void UT_ControlPlane_Stop( ut_controlPlane_instance_t *pInstance )
pInternal->state_machine_thread_handle = 0;
}

ut_control_plane_status_t UT_ControlPlane_RegisterCallbackOnMessage(ut_controlPlane_instance_t *pInstance, char *key, ut_control_REST_API_POST_callback_t callbackFunction, void *userData, eRestApi_t restApiType)
ut_control_plane_status_t UT_ControlPlane_RegisterCallbackOnMessage(ut_controlPlane_instance_t *pInstance, char *key, ut_control_on_message_callback_t callbackFunction, void *userData)
{
ut_cp_instance_internal_t *pInternal = (ut_cp_instance_internal_t *)pInstance;

Expand Down Expand Up @@ -740,30 +744,20 @@ ut_control_plane_status_t UT_ControlPlane_RegisterCallbackOnMessage(ut_controlPl
return UT_CONTROL_PLANE_STATUS_INVALID_PARAM;
}

if ( restApiType == INVALID )
{
UT_CONTROL_PLANE_ERROR("Invalid Rest API\n");
return UT_CONTROL_PLANE_STATUS_INVALID_PARAM;
}

if ( pInternal->callback_entry_index >= UT_CONTROL_PLANE_MAX_CALLBACK_ENTRIES )
{
return UT_CONTROL_PLANE_STATUS_LIST_FULL;
}
strncpy(pInternal->callbackEntryList[pInternal->callback_entry_index].key, key,UT_KVP_MAX_ELEMENT_SIZE);
pInternal->callbackEntryList[pInternal->callback_entry_index].pCallback = callbackFunction;
pInternal->callbackEntryList[pInternal->callback_entry_index].pCallbackType.pCallback = callbackFunction;
pInternal->callbackEntryList[pInternal->callback_entry_index].userData = userData;
pInternal->callbackEntryList[pInternal->callback_entry_index].pStringCallback = NULL;
pInternal->callbackEntryList[pInternal->callback_entry_index].pRestApi = restApiType;
pInternal->callbackEntryList[pInternal->callback_entry_index].pCallbackType.pStringCallback = NULL;
pInternal->callback_entry_index++;
UT_CONTROL_PLANE_DEBUG("callback_entry_index : %d\n", pInternal->callback_entry_index);
return UT_CONTROL_PLANE_STATUS_OK;
}

ut_control_plane_status_t UT_ControlPlane_RegisterAPIEndpointHandler(
ut_controlPlane_instance_t *pInstance,
char *restAPI,
ut_control_rest_api_handler_t *handler)
ut_control_plane_status_t UT_ControlPlane_RegisterEndPointCallback(ut_controlPlane_instance_t *pInstance, char *restAPI, ut_control_endpoint_callback_t callbackFunction, void *userData)

{
ut_cp_instance_internal_t *pInternal = (ut_cp_instance_internal_t *)pInstance;
Expand All @@ -780,33 +774,25 @@ ut_control_plane_status_t UT_ControlPlane_RegisterAPIEndpointHandler(
return UT_CONTROL_PLANE_STATUS_INVALID_PARAM;
}

if (handler->callbackFunctionGET == NULL && handler->callbackFunctionPOST == NULL)
if (callbackFunction == NULL)
{
UT_CONTROL_PLANE_ERROR("NULL callbackFunctions\n");
UT_CONTROL_PLANE_ERROR("NULL callbackFunction\n");
return UT_CONTROL_PLANE_STATUS_INVALID_PARAM;
}

if (handler->userData == NULL && handler->restApiType == POST)
if (userData == NULL)
{
UT_CONTROL_PLANE_ERROR("NULL userData\n");
return UT_CONTROL_PLANE_STATUS_INVALID_PARAM;
}

if (handler->restApiType == INVALID)
{
UT_CONTROL_PLANE_ERROR("Invalid Rest API\n");
return UT_CONTROL_PLANE_STATUS_INVALID_PARAM;
}

if (pInternal->callback_entry_index >= UT_CONTROL_PLANE_MAX_CALLBACK_ENTRIES)
{
return UT_CONTROL_PLANE_STATUS_LIST_FULL;
}
strncpy(pInternal->callbackEntryList[pInternal->callback_entry_index].key, restAPI, UT_KVP_MAX_ELEMENT_SIZE);
pInternal->callbackEntryList[pInternal->callback_entry_index].pCallback = handler->callbackFunctionPOST;
pInternal->callbackEntryList[pInternal->callback_entry_index].userData = handler->userData;
pInternal->callbackEntryList[pInternal->callback_entry_index].pStringCallback = handler->callbackFunctionGET;
pInternal->callbackEntryList[pInternal->callback_entry_index].pRestApi = handler->restApiType;
pInternal->callbackEntryList[pInternal->callback_entry_index].pCallbackType.pCallback = NULL;
pInternal->callbackEntryList[pInternal->callback_entry_index].userData = userData;
pInternal->callbackEntryList[pInternal->callback_entry_index].pCallbackType.pStringCallback = callbackFunction;
pInternal->callback_entry_index++;
UT_CONTROL_PLANE_DEBUG("callback_entry_index : %d\n", pInternal->callback_entry_index);
return UT_CONTROL_PLANE_STATUS_OK;
Expand Down
Loading

0 comments on commit ef4672b

Please sign in to comment.