diff --git a/coap/coap-server1/common.c b/coap/coap-server1/common.c new file mode 100644 index 0000000..e54342c --- /dev/null +++ b/coap/coap-server1/common.c @@ -0,0 +1,48 @@ +/* minimal CoAP functions + * + * Copyright (C) 2018-2021 Olaf Bergmann + */ + +#include +#include +#include +#include + +#include "common.h" + +int +resolve_address(const char *host, const char *service, coap_address_t *dst) { + + struct addrinfo *res, *ainfo; + struct addrinfo hints; + int error, len=-1; + + memset(&hints, 0, sizeof(hints)); + memset(dst, 0, sizeof(*dst)); + hints.ai_socktype = SOCK_DGRAM; + hints.ai_family = AF_UNSPEC; + + error = getaddrinfo(host, service, &hints, &res); + + if (error != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(error)); + return error; + } + + for (ainfo = res; ainfo != NULL; ainfo = ainfo->ai_next) { + switch (ainfo->ai_family) { + case AF_INET6: + case AF_INET: + len = dst->size = ainfo->ai_addrlen; + memcpy(&dst->addr.sin6, ainfo->ai_addr, dst->size); + goto finish; + default: + ; + } + } + + finish: + freeaddrinfo(res); + return len; +} + diff --git a/coap/coap-server1/common.h b/coap/coap-server1/common.h new file mode 100644 index 0000000..cd31827 --- /dev/null +++ b/coap/coap-server1/common.h @@ -0,0 +1,8 @@ +/* minimal CoAP functions + * + * Copyright (C) 2018-2021 Olaf Bergmann + */ + +#include + +int resolve_address(const char *host, const char *service, coap_address_t *dst); diff --git a/coap/coap-server1/common.o b/coap/coap-server1/common.o new file mode 100644 index 0000000..042f31c Binary files /dev/null and b/coap/coap-server1/common.o differ diff --git a/coap/coap-server1/handlers.c b/coap/coap-server1/handlers.c new file mode 100644 index 0000000..da1bac9 --- /dev/null +++ b/coap/coap-server1/handlers.c @@ -0,0 +1,15 @@ +#include "handlers.h" +#include + +void hnd_get_hello(coap_resource_t *resource, + coap_session_t *session, + const coap_pdu_t *request, + const coap_string_t *query_string, + coap_pdu_t *response) +{ + coap_show_pdu(LOG_WARNING, request); + coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); + coap_add_data(response, 5, (const uint8_t *)"world"); + coap_show_pdu(LOG_WARNING, response); + return; +} \ No newline at end of file diff --git a/coap/coap-server1/handlers.h b/coap/coap-server1/handlers.h new file mode 100644 index 0000000..60268d4 --- /dev/null +++ b/coap/coap-server1/handlers.h @@ -0,0 +1,12 @@ + +#include +#include +#include +#include + +void hnd_get_hello( + coap_resource_t *resource, + coap_session_t *session, + const coap_pdu_t *resquest, + const coap_string_t *query_string, + coap_pdu_t* response); diff --git a/coap/coap-server1/handlers.o b/coap/coap-server1/handlers.o new file mode 100644 index 0000000..301f2ce Binary files /dev/null and b/coap/coap-server1/handlers.o differ diff --git a/coap/coap-server1/libcoap b/coap/coap-server1/libcoap new file mode 160000 index 0000000..1da37b9 --- /dev/null +++ b/coap/coap-server1/libcoap @@ -0,0 +1 @@ +Subproject commit 1da37b9abbe871675d5939395b498324ccc8ecfe diff --git a/coap/coap-server1/main.c b/coap/coap-server1/main.c new file mode 100644 index 0000000..e05a4a5 --- /dev/null +++ b/coap/coap-server1/main.c @@ -0,0 +1,77 @@ +/* minimal CoAP server + * + * Copyright (C) 2018-2021 Olaf Bergmann + * Edited by Hesham T. Banafa Mar 8, 2022 for ibs + */ + +#include +#include +#include +#include +#include +#include + +#include +#include +#include "common.h" +#include "handlers.h" + +int main(void) { + coap_context_t *ctx = NULL; + coap_address_t dst; + coap_resource_t *resource = NULL; + coap_endpoint_t *endpoint = NULL; + int result = EXIT_FAILURE;; + coap_str_const_t *ruri = coap_make_str_const("hello"); + coap_startup(); + + /* resolve destination address where server should be sent */ + + if (resolve_address("localhost", "5683", &dst) < 0) { + coap_log(LOG_CRIT, "failed to resolve address\n"); + goto finish; + } + + /* create CoAP context and a client session */ + ctx = coap_new_context(NULL); + + if (!ctx || !(endpoint = coap_new_endpoint(ctx, &dst, COAP_PROTO_UDP))) { + coap_log(LOG_EMERG, "cannot initialize context\n"); + goto finish; + } + + resource = coap_resource_init(ruri, 0); + // coap_register_handler(resource, COAP_REQUEST_GET, + // [](auto, auto, + // const coap_pdu_t *request, + // auto, + // coap_pdu_t *response) { + // coap_show_pdu(LOG_WARNING, request); + // coap_pdu_set_code(response, COAP_RESPONSE_CODE_CONTENT); + // coap_add_data(response, 5, + // (const uint8_t *)"world"); + // coap_show_pdu(LOG_WARNING, response); + // }); + + /* From what I understand, we need to implement the coap_method_handler_t + a supply a fucntion ptr to a function that takes args defined in + coap_method_handler_t typedef. + Holy crap this different. + This kind of typedef is similar to a java abstract class or interface.. + (somefunkystudd.txt) + Mar 8, 2022 - H.B. */ + + //coap_method_handler_t hnd = &hnd_get_hello; + coap_register_handler(resource, COAP_REQUEST_GET, &hnd_get_hello); + coap_add_resource(ctx, resource); + + while (true) { coap_io_process(ctx, COAP_IO_WAIT); } + + result = EXIT_SUCCESS; + finish: + + coap_free_context(ctx); + coap_cleanup(); + + return result; +} \ No newline at end of file diff --git a/coap/coap-server1/main.o b/coap/coap-server1/main.o new file mode 100644 index 0000000..1e47ec9 Binary files /dev/null and b/coap/coap-server1/main.o differ diff --git a/coap/coap-server1/somefunckystuff.txt b/coap/coap-server1/somefunckystuff.txt new file mode 100644 index 0000000..ed062b0 --- /dev/null +++ b/coap/coap-server1/somefunckystuff.txt @@ -0,0 +1,2 @@ +https://stackoverflow.com/questions/840501/how-do-function-pointers-in-c-work +https://stackoverflow.com/questions/3674200/what-does-a-typedef-with-parenthesis-like-typedef-int-fvoid-mean-is-it-a