Skip to content

Instantly share code, notes, and snippets.

@gabrielschulhof
Last active September 8, 2017 06:39
Show Gist options
  • Save gabrielschulhof/517b3518353e95a45b08eb21d4315840 to your computer and use it in GitHub Desktop.
Save gabrielschulhof/517b3518353e95a45b08eb21d4315840 to your computer and use it in GitHub Desktop.
#include <glib.h>
#include <ocstack.h>
#include <unistd.h>
#include "boilerplate.h"
static gboolean
do_OCProcess(gpointer NOTHING) {
OCStackResult result = OCProcess();
if (result != OC_STACK_OK) {
printf("OCProcess: %d\n", result);
}
return true;
}
static FILE *my_open(const char *filename, const char *mode) {
gboolean needFree = FALSE;
char *processedFileName = processFileName(filename, &needFree);
FILE *returnValue = fopen(processedFileName, mode);
if (needFree) {
g_free(processedFileName);
}
return returnValue;
}
static int my_unlink(const char *filename) {
gboolean needFree = FALSE;
char *processedFileName = processFileName(filename, &needFree);
int returnValue = unlink(processedFileName);
if (needFree) {
g_free(processedFileName);
}
return returnValue;
}
int
main(int argc, char **argv) {
GMainLoop *loop = g_main_loop_new(NULL, FALSE);
OCPersistentStorage storage = {
.open = my_open,
.read = fread,
.write = fwrite,
.close = fclose,
.unlink = my_unlink
};
printf("OCRegisterPersistentStorageHandler: %d\n",
OCRegisterPersistentStorageHandler(&storage));
printf("OCInit: %d\n",
OCInit(NULL, 0, OC_CLIENT_SERVER));
g_timeout_add(100, do_OCProcess, NULL);
doIoT();
g_main_loop_run(loop);
return 0;
}
#ifndef __BOILERPLATE_H__
#define __BOILERPLATE_H__
void doIoT();
char *processFileName(const char *filename, gboolean *needsFree);
#endif /* ndef __BOILERPLATE_H__ */
#include <string.h>
#include <glib.h>
#include <ocstack.h>
#define DESIRED_RESOURCE "/a/high-level-example"
static char *desiredUrl = NULL;
static OCStackApplicationResult
requestCallback(void *NOTHING, OCDoHandle handle, OCClientResponse *response) {
printf("response->result: %d\n", response ? response->result : -1);
return OC_STACK_KEEP_TRANSACTION;
}
static gboolean
performRequest(gpointer NOTHING) {
OCDoHandle handle = NULL;
OCCallbackData callback = {
.context = NULL,
.cb = requestCallback,
.cd = NULL,
};
printf("OCDoResource(request) to %s: %d\n", desiredUrl,
OCDoResource(&handle, OC_REST_GET, desiredUrl, NULL,
NULL, CT_DEFAULT, OC_HIGH_QOS, &callback, NULL, 0));
g_free(desiredUrl);
return false;
}
static OCStackApplicationResult
discoverCallback(void *NOTHING, OCDoHandle handle, OCClientResponse *response) {
OCResourcePayload *resource = NULL;
if (response && response->result == OC_STACK_OK && response->payload &&
response->payload->type == PAYLOAD_TYPE_DISCOVERY) {
for (resource = ((OCDiscoveryPayload *)(response->payload))->resources;
resource; resource = resource->next) {
if (resource->uri && !g_strcmp0(resource->uri, DESIRED_RESOURCE)) {
for (OCEndpointPayload *ep = resource->eps; ep; ep = ep->next) {
if (!desiredUrl && !strcmp(ep->tps, "coaps")) {
char *percent = strstr(ep->addr, "%");
if (percent) {
*percent = 0;
}
desiredUrl = g_strdup_printf("%s://%s%s%s:%d%s",
ep->tps,
(ep->family & OC_IP_USE_V6 ? "[" : ""),
ep->addr,
(ep->family & OC_IP_USE_V6 ? "]" : ""),
ep->port,
DESIRED_RESOURCE);
}
printf("Resource endpoint: %s://%s%s%s:%d\n",
ep->tps,
(ep->family & OC_IP_USE_V6 ? "[" : ""),
ep->addr,
(ep->family & OC_IP_USE_V6 ? "]" : ""),
ep->port);
}
printf("Resource found\n");
g_idle_add(performRequest, NULL);
return OC_STACK_DELETE_TRANSACTION;
}
}
}
return OC_STACK_KEEP_TRANSACTION;
}
static gboolean
discoverResource(gpointer NOTHING) {
OCDoHandle handle = NULL;
OCCallbackData callback = {
.context = NULL,
.cb = discoverCallback,
.cd = NULL
};
printf("OCDoResource(discovery): %d\n",
OCDoResource(&handle, OC_REST_DISCOVER, OC_MULTICAST_DISCOVERY_URI,
NULL, NULL, CT_DEFAULT, OC_HIGH_QOS, &callback, NULL, 0));
return false;
}
void doIoT() {
g_idle_add(discoverResource, NULL);
}
char *processFileName(const char *filename, gboolean *needFree) {
*needFree = true;
return g_strdup_printf("client.%s", filename);
}
{
"acl": {
"aclist2": [
{
"aceid": 1,
"subject": { "conntype": "anon-clear" },
"resources": [
{ "href": "/oic/res" },
{ "href": "/oic/d" },
{ "href": "/oic/p" },
{ "href": "/oic/sec/doxm" }
],
"permission": 2
},
{
"aceid": 2,
"subject": { "conntype": "auth-crypt" },
"resources": [
{ "href": "/oic/res" },
{ "href": "/oic/d" },
{ "href": "/oic/p" },
{ "href": "/oic/sec/doxm" }
],
"permission": 2
}
],
"rowneruuid" : "32323232-3232-3232-3232-323232323232"
},
"pstat": {
"dos": {"s": 3, "p": false},
"isop": true,
"rowneruuid": "32323232-3232-3232-3232-323232323232",
"cm": 0,
"tm": 0,
"om": 4,
"sm": 4
},
"doxm": {
"oxms": [0],
"oxmsel": 0,
"sct": 1,
"owned": true,
"deviceuuid": "32323232-3232-3232-3232-323232323232",
"devowneruuid": "32323232-3232-3232-3232-323232323232",
"rowneruuid": "32323232-3232-3232-3232-323232323232"
},
"cred": {
"creds": [
{
"credid": 1,
"subjectuuid": "31313131-3131-3131-3131-313131313131",
"credtype": 1,
"privatedata": {
"data": "AAAAAAAAAAAAAAAA",
"encoding": "oic.sec.encoding.raw"
}
}
]
}
}
IOTIVITY_PREFIX = /home/nix/iot/iotivity-node/iotivity-native
BIN_PATH = out/linux/x86_64/debug
INCLUDES = \
-I$(IOTIVITY_PREFIX)/resource/csdk/stack/include \
-I$(IOTIVITY_PREFIX)/resource/csdk/include \
-I$(IOTIVITY_PREFIX)/resource/c_common
all: client server server.oic_svr_db.dat client.oic_svr_db.dat
.PHONY: all
client: client.c boilerplate.c
cc -g -Wall -O0 `pkg-config --cflags --libs glib-2.0` \
-DROUTING_EP \
-D__WITH_DTLS__ \
$(INCLUDES) \
-loctbstack \
-L$(IOTIVITY_PREFIX)/$(BIN_PATH)/resource/csdk/stack \
-Wl,-rpath $(IOTIVITY_PREFIX)/$(BIN_PATH)/resource/csdk/stack \
client.c boilerplate.c -o client
server: server.c boilerplate.c
cc -g -Wall -O0 `pkg-config --cflags --libs glib-2.0` \
-DROUTING_EP \
-D__WITH_DTLS__ \
$(INCLUDES) \
-loctbstack \
-L$(IOTIVITY_PREFIX)/$(BIN_PATH)/resource/csdk/stack \
-Wl,-rpath $(IOTIVITY_PREFIX)/$(BIN_PATH)/resource/csdk/stack \
server.c boilerplate.c -o server
%.dat: %.json
$(IOTIVITY_PREFIX)/$(BIN_PATH)/resource/csdk/security/tool/json2cbor $< $@
clean:
rm -rf *dat client server
.PHONY: clean
#include <glib.h>
#include <ocstack.h>
#include "boilerplate.h"
static OCEntityHandlerResult
entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest *request,
void *NOTHING) {
OCEntityHandlerResponse response = {
.requestHandle = request->requestHandle,
.resourceHandle = request->resource,
.ehResult = OC_EH_OK,
.payload = NULL,
.numSendVendorSpecificHeaderOptions = 0,
.sendVendorSpecificHeaderOptions = {},
.resourceUri = "/a/high-level-example",
.persistentBufferFlag = 1
};
printf("flag: %d, request->method: %d\n", flag, request->method);
printf("OCDoResponse: %d\n", OCDoResponse(&response));
return OC_EH_OK;
}
static gboolean
setupResource(gpointer NOTHING) {
OCResourceHandle handle = 0;
printf("OCCreateResource: %d\n",
OCCreateResource(&handle, "core.light", "oic.if.baseline",
"/a/high-level-example", entityHandler, NULL,
OC_DISCOVERABLE | OC_OBSERVABLE | OC_SECURE));
return false;
}
char *processFileName(const char *filename, gboolean *needFree) {
*needFree = TRUE;
return g_strdup_printf("server.%s", filename);
}
void doIoT() {
g_idle_add(setupResource, NULL);
}
{
"acl": {
"aclist2": [
{
"aceid": 1,
"subject": { "conntype": "anon-clear" },
"resources": [
{ "href": "/oic/res" },
{ "href": "/oic/d" },
{ "href": "/oic/p" },
{ "href": "/oic/sec/doxm" }
],
"permission": 2
},
{
"aceid": 2,
"subject": { "conntype": "auth-crypt" },
"resources": [
{ "href": "/oic/res" },
{ "href": "/oic/d" },
{ "href": "/oic/p" },
{ "href": "/oic/sec/doxm" }
],
"permission": 2
},
{
"aceid": 3,
"subject": { "conntype": "auth-crypt" },
"resources": [{ "href": "/a/high-level-example" }],
"permission": 7
},
{
"aceid": 4,
"subject": { "conntype": "anon-clear" },
"resources": [{ "href": "/a/high-level-example" }],
"permission": 7
}
],
"rowneruuid" : "31313131-3131-3131-3131-313131313131"
},
"pstat": {
"dos": {"s": 3, "p": false},
"isop": true,
"rowneruuid": "31313131-3131-3131-3131-313131313131",
"cm": 0,
"tm": 0,
"om": 4,
"sm": 4
},
"doxm": {
"oxms": [0],
"oxmsel": 0,
"sct": 1,
"owned": true,
"deviceuuid": "31313131-3131-3131-3131-313131313131",
"rowneruuid": "31313131-3131-3131-3131-313131313131"
},
"cred": {
"creds": [
{
"credid": 1,
"subjectuuid": "32323232-3232-3232-3232-323232323232",
"credtype": 1,
"period": "20150630T060000/20990920T220000",
"privatedata": {
"data": "AAAAAAAAAAAAAAAA",
"encoding": "oic.sec.encoding.raw"
}
}
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment