Created
September 29, 2015 22:21
-
-
Save gabrielschulhof/de8bdffb3120f5d61dba to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <string.h> | |
#include <signal.h> | |
#include <glib.h> | |
#include <ocstack.h> | |
#include <ocpayload.h> | |
static int observationCount = 0; | |
static GMainLoop *loop = NULL; | |
static guint timeout_id = 0; | |
static char *sampleUri = "/a/light"; | |
static gboolean useSpecificConnType = FALSE; | |
static void deleteContextNoop( void *context ) {} | |
static void cleanupAndExit( int whatSignal ) { | |
g_message( "Cleaning up and exiting" ); | |
g_source_remove( timeout_id ); | |
g_message( "OCStop: %d", OCStop() ); | |
g_main_loop_quit( loop ); | |
} | |
static gboolean run_OCProcess( void *nothingHere ) { | |
g_message( "OCProcess: %d", OCProcess() ); | |
return TRUE; | |
} | |
static void dumpResponse( const char *prefix, OCClientResponse *response ) { | |
_Bool state; | |
int64_t power; | |
printf( "%s: response:\n", prefix ); | |
printf( "%s: ->result: %d\n", prefix, response->result ); | |
printf( "%s: ->devAddr:\n", prefix ); | |
printf( "%s: ->devAddr.adapter: %d\n", prefix, response->devAddr.adapter ); | |
printf( "%s: ->devAddr.flags: %d\n", prefix, response->devAddr.flags ); | |
printf( "%s: ->devAddr.interface: %d\n", prefix, response->devAddr.interface ); | |
printf( "%s: ->devAddr.port: %d\n", prefix, response->devAddr.port ); | |
printf( "%s: ->devAddr.addr: %s\n", prefix, response->devAddr.addr ); | |
printf( "%s: response->payload: %s\n", prefix, response->payload ? "present": "absent" ); | |
if ( response->payload ) { | |
printf( "%s: response->payload->type: %d\n", prefix, response->payload->type ); | |
if ( response->payload->type == PAYLOAD_TYPE_DISCOVERY ) { | |
OCDiscoveryPayload *discoveryPayload = ( OCDiscoveryPayload * )( response->payload ); | |
if ( discoveryPayload->resources ) { | |
printf( "%s: response->payload->resources->uri: %s\n", prefix, discoveryPayload->resources->uri ); | |
} | |
} else if ( response->payload->type == PAYLOAD_TYPE_REPRESENTATION ) { | |
if ( OCRepPayloadGetPropBool( ( OCRepPayload * )( response->payload ), "state", &state ) && | |
OCRepPayloadGetPropInt( ( OCRepPayload * )( response->payload ), "power", &power ) ) { | |
printf( "%s: payload values: { state: %s, power: %d }\n", prefix, state ? "true" : "false", (int)power ); | |
} else { | |
printf( "%s: Failed to retrieve payload values\n", prefix ); | |
} | |
} | |
} | |
} | |
static OCStackApplicationResult observeCallback( void *nothingHere, OCDoHandle handle, OCClientResponse *response ) { | |
dumpResponse( "observe", response ); | |
if ( observationCount++ >= 10 ) { | |
g_message( "OCCancel: %d", OCCancel( handle, OC_HIGH_QOS, NULL, 0 ) ); | |
} | |
return OC_STACK_KEEP_TRANSACTION; | |
} | |
static OCStackApplicationResult discoverCallback( void *nothingHere, OCDoHandle handle, OCClientResponse *response ) { | |
OCResourcePayload *iter = 0; | |
OCConnectivityType connType = ( useSpecificConnType ? ( CT_ADAPTER_IP | CT_IP_USE_V6 ) : CT_DEFAULT ); | |
OCCallbackData observeData = { NULL, observeCallback, deleteContextNoop }; | |
OCDoHandle observeHandle = NULL; | |
dumpResponse( "discovery", response ); | |
g_message( "OCConnectivityType for observation: %d", connType ); | |
if ( response->payload->type == PAYLOAD_TYPE_DISCOVERY ) { | |
for ( iter = ( ( OCDiscoveryPayload * )response->payload )->resources ; iter; iter = iter->next ) { | |
if ( iter->uri && !strcmp( iter->uri, sampleUri ) ) { | |
g_message( "OCDoResource(observation): %d", OCDoResource( | |
&observeHandle, | |
OC_REST_OBSERVE, | |
sampleUri, | |
response->addr, | |
NULL, | |
connType, | |
OC_HIGH_QOS, | |
&observeData, | |
NULL, | |
0 ) ); | |
return OC_STACK_DELETE_TRANSACTION; | |
} | |
} | |
} | |
return OC_STACK_KEEP_TRANSACTION; | |
} | |
int main( int argc, char **argv ) { | |
OCDoHandle discoverHandle; | |
OCCallbackData discoverData = { NULL, discoverCallback, deleteContextNoop }; | |
useSpecificConnType = ( argc > 1 ); | |
loop = g_main_loop_new( NULL, false ); | |
timeout_id = g_timeout_add( 1000, run_OCProcess, NULL ); | |
signal( SIGINT, cleanupAndExit ); | |
g_message( "OCInit: %d", OCInit( NULL, 0, OC_CLIENT ) ); | |
g_message( "OCDoResource(discovery): %d", OCDoResource( | |
&discoverHandle, | |
OC_REST_DISCOVER, | |
"/oic/res", | |
NULL, | |
NULL, | |
CT_DEFAULT, | |
OC_HIGH_QOS, | |
&discoverData, | |
NULL, | |
0 ) ); | |
g_main_loop_run( loop ); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment