Property Access Callbacks
Property access callbacks are the functions that are called when a request comes from the server to either read or write a specific property. These functions have the following signature:
enum msgCodeEnum myPropCallback (
const char * entityName,
const char * propertyName,
twInfoTable ** value,
char isWrite,
void * userdata
)
The following table describes the parameters:
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.
propertyName
Input
Pointer to a character array. This is the name of the property, represented in modified UTF-8. This value may be null or ‘*” which means the request is to return the value of all properties registered for this entity.
value
Input/Output
Pointer to a pointer to a twInfoTable. If this is a request to read the value of a property a new twInfoTable structure should be created and it pointer should assigned to value. If this is a write, the value will contain a pointer to the infotable that contains the data to be written. This pointer is guaranteed to be non-NULL. In either case, the calling function 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.
isWrite
Input
A Boolean value describing whether this is a read (FALSE) or write (true) request for the property.
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 function. 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.
Below is a simple example of a property handler callback function.
enum msgCodeEnum propertyHandler(const char * entityName,
const char * propertyName,
twInfoTable ** value,
char isWrite,
void * userdata) {
char * asterisk = “*”;
if (!propertyName) propertyName = asterisk;
TW_LOG(TW_TRACE,"propertyHandler - Function called for Entity %s, Property %s", entityName, propertyName);
if (value) {
if (isWrite && *value) {
/* Property Writes */
if (strcmp(propertyName, "TemperatureLimit") == 0) {
twInfoTable_GetNumber(*value, propertyName, 0, &properties.TemperatureLimit);
} else return NOT_FOUND;
return SUCCESS;
} else {
/* Property Reads */
if (strcmp(propertyName, "TemperatureLimit") == 0) {*value = twInfoTable_CreateFromNumber(propertyName, properties.TemperatureLimit);
} else return NOT_FOUND;
}
return SUCCESS;
} else {
TW_LOG(TW_ERROR,"propertyHandler - NULL pointer for value");
return BAD_REQUEST;
}
}
Was this helpful?