coap: coap server using libcoap. also scrapped.

Signed-off-by: HeshamTB <hishaminv@gmail.com>
This commit is contained in:
HeshamTB 2022-03-23 03:55:29 +03:00
parent 8094b1fc77
commit e633be8396
Signed by: Hesham
GPG Key ID: 74876157D199B09E
10 changed files with 163 additions and 0 deletions

View File

@ -0,0 +1,48 @@
/* minimal CoAP functions
*
* Copyright (C) 2018-2021 Olaf Bergmann <bergmann@tzi.org>
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#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;
}

View File

@ -0,0 +1,8 @@
/* minimal CoAP functions
*
* Copyright (C) 2018-2021 Olaf Bergmann <bergmann@tzi.org>
*/
#include <coap3/coap.h>
int resolve_address(const char *host, const char *service, coap_address_t *dst);

BIN
coap/coap-server1/common.o Normal file

Binary file not shown.

View File

@ -0,0 +1,15 @@
#include "handlers.h"
#include <coap3/resource.h>
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;
}

View File

@ -0,0 +1,12 @@
#include <coap3/coap.h>
#include <coap3/coap_forward_decls.h>
#include <coap3/resource.h>
#include <coap3/str.h>
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);

Binary file not shown.

@ -0,0 +1 @@
Subproject commit 1da37b9abbe871675d5939395b498324ccc8ecfe

77
coap/coap-server1/main.c Normal file
View File

@ -0,0 +1,77 @@
/* minimal CoAP server
*
* Copyright (C) 2018-2021 Olaf Bergmann <bergmann@tzi.org>
* Edited by Hesham T. Banafa Mar 8, 2022 for ibs
*/
#include <gsl/gsl_matrix_uint.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <sys/socket.h>
#include <gsl/gsl_matrix.h>
#include <coap3/coap.h>
#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;
}

BIN
coap/coap-server1/main.o Normal file

Binary file not shown.

View File

@ -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