Last active
October 11, 2017 07:45
-
-
Save gabrielschulhof/706648360cb2a92d9f8d2ba4870ddf24 to your computer and use it in GitHub Desktop.
This file contains 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 <stdarg.h> | |
#include <string.h> | |
#include <ocstack.h> | |
#include <stdlib.h> | |
#include "boilerplate.h" | |
#ifdef __unix__ | |
# include <unistd.h> | |
#elif defined _WIN32 | |
# include <windows.h> | |
#define sleep(x) Sleep(1000 * x) | |
#endif | |
static FILE *my_open(const char *filename, const char *mode) { | |
bool needFree = false; | |
char *processedFileName = processFileName(filename, &needFree); | |
FILE *returnValue = fopen(processedFileName, mode); | |
if (needFree) { | |
free(processedFileName); | |
} | |
return returnValue; | |
} | |
static int my_unlink(const char *filename) { | |
bool needFree = false; | |
char *processedFileName = processFileName(filename, &needFree); | |
int returnValue = unlink(processedFileName); | |
if (needFree) { | |
free(processedFileName); | |
} | |
return returnValue; | |
} | |
char *strdup_printf(char *format, ...) { | |
char *return_value = NULL; | |
va_list list; | |
int size; | |
va_start(list, format); | |
size = vsnprintf(NULL, 0, format, list); | |
va_end(list); | |
return_value = malloc(size + 1); | |
va_start(list, format); | |
vsnprintf(return_value, size + 1, format, list); | |
va_end(list); | |
return_value[size] = 0; | |
return return_value; | |
} | |
int | |
main(int argc, char **argv) { | |
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)); | |
doIoT(); | |
while (true) { | |
OCStackResult result = OCProcess(); | |
if (result != OC_STACK_OK) { | |
printf("OCProcess: %d\n", result); | |
} | |
sleep(1); | |
} | |
return 0; | |
} |
This file contains 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
#ifndef __BOILERPLATE_H__ | |
#define __BOILERPLATE_H__ | |
void doIoT(); | |
char *strdup_printf(char *format, ...); | |
char *processFileName(const char *filename, bool *needsFree); | |
#endif /* ndef __BOILERPLATE_H__ */ |
This file contains 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 <string.h> | |
#include <stdlib.h> | |
#include <ocstack.h> | |
#include "boilerplate.h" | |
#define DESIRED_RESOURCE "/a/high-level-example" | |
static OCStackApplicationResult | |
requestCallback(void *url, OCDoHandle handle, OCClientResponse *response) { | |
printf("%s: response->result: %d\n", (char *)url, | |
response ? response->result : -1); | |
return OC_STACK_KEEP_TRANSACTION; | |
} | |
static void | |
performRequest(const char *desiredUrl) { | |
OCDoHandle handle = NULL; | |
OCCallbackData callback = { | |
.context = desiredUrl, | |
.cb = requestCallback, | |
.cd = free, | |
}; | |
printf("OCDoResource(request) to %s: %d\n", desiredUrl, | |
OCDoResource(&handle, OC_REST_GET, desiredUrl, NULL, | |
NULL, CT_DEFAULT, OC_HIGH_QOS, &callback, NULL, 0)); | |
} | |
static char *epToUrl(OCEndpointPayload *ep) { | |
char *percent = strstr(ep->addr, "%"); | |
if (percent) { | |
*percent = 0; | |
} | |
return 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); | |
} | |
static OCStackApplicationResult | |
discoverCallback(void *NOTHING, OCDoHandle handle, OCClientResponse *response) { | |
char *coapsUrl = NULL, *coapUrl = NULL; | |
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 && !strcmp(resource->uri, DESIRED_RESOURCE)) { | |
for (OCEndpointPayload *ep = resource->eps; ep; ep = ep->next) { | |
if (!coapsUrl && !strcmp(ep->tps, "coaps")) { | |
coapsUrl = epToUrl(ep); | |
} else if (!coapUrl && !strcmp(ep->tps, "coap")) { | |
coapUrl = epToUrl(ep); | |
} | |
if (coapsUrl && coapUrl) { | |
break; | |
} | |
} | |
if (coapsUrl) { | |
performRequest(coapsUrl); | |
} | |
if (coapUrl) { | |
performRequest(coapUrl); | |
} | |
printf("Resource found\n"); | |
return OC_STACK_DELETE_TRANSACTION; | |
} | |
} | |
} | |
return OC_STACK_KEEP_TRANSACTION; | |
} | |
void doIoT() { | |
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)); | |
} | |
char *processFileName(const char *filename, bool *needFree) { | |
*needFree = true; | |
return strdup_printf("client.%s", filename); | |
} |
This file contains 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
{ | |
"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" | |
} | |
} | |
] | |
} | |
} |
This file contains 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 <ocstack.h> | |
#include "boilerplate.h" | |
static OCEntityHandlerResult | |
entityHandler(OCEntityHandlerFlag flag, OCEntityHandlerRequest *request, | |
void *NOTHING) { | |
OCEntityHandlerResponse response = { | |
request->requestHandle, | |
request->resource, | |
OC_EH_OK, | |
NULL, | |
0, | |
{{0, 0}}, | |
"/a/high-level-example", | |
1 | |
}; | |
printf("flag: %d, request->method: %d\n", flag, request->method); | |
printf("OCDoResponse: %d\n", OCDoResponse(&response)); | |
return OC_EH_OK; | |
} | |
char *processFileName(const char *filename, bool *needFree) { | |
*needFree = true; | |
return strdup_printf("server.%s", filename); | |
} | |
void doIoT() { | |
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)); | |
} |
This file contains 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
{ | |
"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