ThingWorx Edge C SDK > Interacting with ThingWorx > Server-Initiated Interaction > Service Callbacks
  
Service Callbacks
Service callbacks are the functions that are called when a request comes from ThingWorx platform to execute a service on a particular entity. These functions have the following signature:
typedef enum msgCodeEnum (*service_cb) (
const char * entityName,
const char * serviceName,
twInfoTable * params,
twInfoTable ** content);
The following table defines the parameters:
Parameters for msgCodeEnum()
Parameter
Type
Description
entityName
Input
Pointer to a character array. The name is represented as a modified UTF-8 string with the name of the entity targeted in this request. This parameter is guaranteed not to be NULL.
serviceName
Input
Pointer to a character array. This is the name of the service to be executed, represented in modified UTF-8. This parameter is guaranteed not to be NULL.
params
Input
Pointer to a twInfoTable. This is a pointer to an infotable that contains all of the parameters specified for this invocation of the service. This pointer may be NULL if the service in question has no input parameters. The API owns this pointer and will manage any memory associated with it.
content
Output
Pointer to a pointer to a twInfoTable. This is used to return any data the service returns back to the server. The callback function should create a twInfoTable as described previously and pass a pointer to that structure to *content. If the service does not return any data it is OK to set *content to NULL. The API will assume ownership of the pointer in *value, so the callback function does not need to worry about memory management of any infotables passed in or created and returned as values.
userdata
Input
The same pointer value that was passed in when this property was registered. This pointer can be used for anything, a typical use is to specify the ‘this’ pointer when using C++ class wrappers.
The return value of the callback is an indicator of the success or failure of the service call. You are free to choose any of the return codes defined in the msgCodeEnum enumeration type, defined in src/api/twDefinitions.h, starting with SUCCESS or any applicable larger value. Here is an example of a service handler callback:
enum msgCodeEnum addNumbersService(const char * entityName,
const char * serviceName,
twInfoTable * params,
twInfoTable ** content,
void * userdata) {
double a, b, res;
TW_LOG(TW_TRACE,"addNumbersService - Function called");
if (!params || !content) {
TW_LOG(TW_ERROR,"addNumbersService - NULL params or content pointer");
return BAD_REQUEST;
}
if (twInfoTable_GetNumber(params, "a", 0, &a) || twInfoTable_GetNumber(params, "b", 0, &b)) {
TW_LOG(TW_ERROR,"addNumbersService – Missing parameter data");
return BAD_REQUEST;
}
res = a + b;
*content = twInfoTable_CreateFromNumber("result", res);
if (*content) return SUCCESS;
else return INTERNAL_SERVER_ERROR;
}