Skip to content

Instantly share code, notes, and snippets.

@gabrielschulhof
Created May 25, 2016 19:47
Show Gist options
  • Save gabrielschulhof/bf5a9af0ed8f49e548144445cc793f55 to your computer and use it in GitHub Desktop.
Save gabrielschulhof/bf5a9af0ed8f49e548144445cc793f55 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <string.h>
#include <sol-oic-client.h>
#include <soletta.h>
static struct sol_oic_pending *pending = NULL;
struct sol_oic_client *client = NULL;
struct sol_oic_resource *theResource = NULL;
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
static void requestAnswered(void *data, enum sol_coap_response_code code,
struct sol_oic_client *client, const struct sol_network_link_addr *address,
const struct sol_oic_map_reader *reader) {
printf("Request answered\n");
sol_oic_resource_unref(theResource);
}
static void doRequest(struct sol_oic_resource *resource) {
struct sol_oic_request *request = sol_oic_client_request_new(SOL_COAP_METHOD_GET, resource);
struct sol_oic_map_writer *writer = sol_oic_client_request_get_writer(request);
struct sol_oic_repr_field field;
field.key = "ABC";
field.type = SOL_OIC_REPR_TYPE_TEXT_STRING;
field.v_slice.data = "DEF";
field.v_slice.len = 3;
if (sol_oic_map_append(writer, &field)) {
printf("map append failed\n");
} else {
printf("map append succeeded\n");
}
sol_oic_client_request(client, request, requestAnswered, NULL);
}
static bool resourceFound(void *nothing, struct sol_oic_client *client, struct sol_oic_resource *resource) {
printf("resourceFound: %.*s\n", SOL_STR_SLICE_PRINT(resource->path));
if (sol_argc() > 1 && !strncmp(sol_argv()[1], resource->path.data,
MIN(strlen(sol_argv()[1]), resource->path.len))) {
theResource = sol_oic_resource_ref(resource);
doRequest(theResource);
sol_oic_pending_cancel(pending);
resource = NULL;
}
return !!resource;
}
static void startup() {
client = sol_oic_client_new();
struct sol_network_link_addr address = {
.addr = {
.in6 = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
},
.family = SOL_NETWORK_FAMILY_INET,
.port = 5683
};
if (!sol_network_link_addr_from_str(&address, "224.0.1.187")) {
fprintf(stderr, "Failed to retrieve address");
return;
}
pending = sol_oic_client_find_resources(client, &address, "", "", resourceFound, NULL);
}
static void shutdown() {}
SOL_MAIN_DEFAULT(startup, shutdown);
#include <stdio.h>
#include <string.h>
#include <sol-oic-server.h>
#include <soletta.h>
static int getRequest(void *data, struct sol_oic_request *request) {
enum sol_oic_map_loop_status reason;
struct sol_oic_repr_field field;
struct sol_oic_map_reader iter;
struct sol_oic_map_reader *input = sol_oic_server_request_get_reader(request);
printf("getRequest\n");
SOL_OIC_MAP_LOOP(input, &field, &iter, reason) {
printf("key: %s\n", field.key);
sol_oic_repr_field_clear(&field);
}
printf("%d\n", reason);
return 0;
}
static void startup() {
struct sol_oic_resource_type resourceType = {
SOL_SET_API_VERSION(.api_version = SOL_OIC_RESOURCE_TYPE_API_VERSION,)
.get = { .handle = getRequest },
.resource_type = { .data = "core.light", .len = 10 },
.interface = { .data = "oic.if.baseline", .len = 15 },
.path = { .data = "/a/xyzzy", .len = 8 }
};
sol_oic_server_register_resource(&resourceType, NULL,
SOL_OIC_FLAG_DISCOVERABLE | SOL_OIC_FLAG_ACTIVE);
}
static void shutdown() {}
SOL_MAIN_DEFAULT(startup, shutdown);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment