From e633be8396ac0a8084df53252af3807032ba82b4 Mon Sep 17 00:00:00 2001 From: HeshamTB Date: Wed, 23 Mar 2022 03:55:29 +0300 Subject: [PATCH] coap: coap server using libcoap. also scrapped. Signed-off-by: HeshamTB --- coap/coap-server1/common.c | 48 ++++++++++++++++ coap/coap-server1/common.h | 8 +++ coap/coap-server1/common.o | Bin 0 -> 2240 bytes coap/coap-server1/handlers.c | 15 +++++ coap/coap-server1/handlers.h | 12 ++++ coap/coap-server1/handlers.o | Bin 0 -> 1728 bytes coap/coap-server1/libcoap | 1 + coap/coap-server1/main.c | 77 ++++++++++++++++++++++++++ coap/coap-server1/main.o | Bin 0 -> 3016 bytes coap/coap-server1/somefunckystuff.txt | 2 + 10 files changed, 163 insertions(+) create mode 100644 coap/coap-server1/common.c create mode 100644 coap/coap-server1/common.h create mode 100644 coap/coap-server1/common.o create mode 100644 coap/coap-server1/handlers.c create mode 100644 coap/coap-server1/handlers.h create mode 100644 coap/coap-server1/handlers.o create mode 160000 coap/coap-server1/libcoap create mode 100644 coap/coap-server1/main.c create mode 100644 coap/coap-server1/main.o create mode 100644 coap/coap-server1/somefunckystuff.txt 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 0000000000000000000000000000000000000000..042f31c853b5832ace72c59229c48f83c2034bb8 GIT binary patch literal 2240 zcmbu9&1(}u6u{pm>4&u~ReLDbLp)UKN0-)9EmG2!wWFw%LQW!Kn@rl;=7Y^vTCpeu zw969cMd(317tA5Y(1QdmwqEpK@FEDksHYr+`rhnJ+%CyQA57-WZ{F9=o5?fd?yZof zff5ZS;qOXOfLNPP7itW8p$Fp3`NoR-&zZBi!<@Bk=ES#B%#92UqY-nA`dID2*gZyJ zC8M@Ciu4`1Gw0yDm|bIQFN)d;dSD!ekR2HN!lrbu!b?ucG0MfwGT5W8 zQC8uc_skWgvQm%mbv~ncl~j=rO^=>pkP6mTJ$isz)s01p^^o0Gtaq(V3NY7))0QKC zU2u$DTB7oic9|O+U~YVimF`5C^NS&MzKGaI{c?Ypg0tdg71rKR#hA>i*ribMdWv1` z7YAgw3CB+#7P)PvQr2=dotuab6}kXsXJ#g%!?X9}m!o52`gNU74kdykii5rpHz5RC zjhGTmu2m2F<%afT9<=p7ZLqtea~n4=Ag4hxOU6^ZeJ?^YJ?-IX4NU}Z_Cux-_<7uH zFWmIz0E9)gCq3{B;jGVI)PU?fj=qzoVs)_vA8ElSTJWhB{B{dI*MdK6!6BK;WO7+O z2^KHpRv+^O^_3S2kl~pEx53wGSTvUt1>545m9wB=r%-`(9&dr2Ci7%|1JV}f>SjO! ztQ@naZrd;?^T>DK7QKAr+mEI$D^o|P(3snq8BzYwz&X09;i1jDb3=m zrf&0f8}yWEo1iaXJ9I1OEvR8Ui+kCe&2@ZJBjS+ddNI4E=dE0xTlNO}P$a&TNLyxx zH&XwP5=CeFRO*FaVsfG0h1!$HqLwg-8l7pZK|G~KO+oqLO+aT$zUpNUNPbCjX)Y{eyL>^H;eZ$(TX^uhM^98Ok~6Pq(mH|0@JhRG+E;^d6PJ;*=(s t8xaghI4MJI`rFL^P}XlZEIlRY{|DtsLSO&@ literal 0 HcmV?d00001 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 0000000000000000000000000000000000000000..301f2ce5b7fa2d54021a7322d38a025f8a047eaf GIT binary patch literal 1728 zcmbtUPiqrV5T8wItJs#*KOojaPSS!diKp6wL__Qzs)s_(%XatKMU&mIyP>Hd9t2?# zp`XK3KS3=Nw0?sgy$C&csJ9-3I-i_G0`hudM)^8nV`?bZ6y+Vk#mz1cJ$m<=KWP>(tus)9kNfN?;W zh{3*mw;LQVUT=Ugsu_#3GnaQUcn5JhBroaX@qF!lr9C%YQJ6tMxlr9H_;p?ut(Hy{ zpqkxwk}-?!wL*S&2gGjUsZFM4-qHp5>IFFXPT=)a6q_#ifoE^2#P(IM7lIo)13UJ^ zZF}Gia}NzW#soL?RE{{FXM0ZKz%t*3W=u2xeVz;TB_VtokKpv`Bo#cgN`h|?h(h@@ zcm#I|MA7TlGc>P5wZ>w`JN?Aj1T#*eT>D~0s^^%A8YN(6cQ-fV7|bZl9XIUvRe-S| zOq7Ycb3aZT_Z9nF!O$E;;Xp;n4sxiH^6eLq(^uox|6|mVOuIv~DoTqTd4Gw?{))ed z2F<7?0P7VV&WQc1sEI2pe2ekBj2C|1S>zMPb*1;ehY3{kU-?bsQq76CL|^*rbJ6=h zWIq25pqI-Y>+1rr7QW~wh>KlJJuEr}ThFEarM=bff5aU9UNV0vN$*QFb^aMAtVkkj zoljd>_WpZZP~Mr|{|X-A3trZnS~d8K9*~1V|4Y + * 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 0000000000000000000000000000000000000000..1e47ec9f7ff460ec6726425656589681ec9f78d8 GIT binary patch literal 3016 zcmbuB&u`pB6vxNeG%2M?Ln#PU4z3EQK(QJSRpC(Ohn!dtD3k`l1><-pSrf0lc)cMh zLLj0dR;!>~_zyTBQHebUklGbUNqZ>d1ZR+tK!HOg4mlu|_Z`n{Cf51_PnyiU&%7Vw znK!ffz@C}vGYqE0VCUJMrYK|1!M*XA7>==FHpFI~=5NmGpKq0&R{tZXXu@gQyQ3Xv z%WlJp`VHB4&~MpS5#3ok0L^J`{hsyqjcl-HZ#&KJy3?E8QD%6hvvOJ(uI?H~KE0(! z+PvOJsz@d*{`iX2>xiyWw4K%W+blilti8DHwCwgbVkONVv{iJ-q{A8y3zsXM<#)RI zH$|wu@H?+jh#S`sU-RD}NbNr0Ygu^tg{@i4wVmcqU2{_R`S1L-zGJLsR+*PrM$5kH zv}Sk9&J)MP2B!xEzWIHHhhfZ~IeYGD7RJ68R^ocf=Di@~WiyS z=zu9Pq>hh;9LqXpT)xPRe0xNmZ3ptNL=iQv8dz_g#; zzSC>2)U#3UEB`lPS0GJ7@u)3 zOijINUve*vPt4dZt3+iNxw3mF)|K#upq}!?t$0y6Znr);@m||*?$g50#5Hxmu~@*fZt6` z+5Zek0fCpsrdIx{y4E=ji1!)k7)d?#!>ew&a}p{ z2a3O;@uM1lQ{(q)yrFTtttyU5V+i&ctt(c2rJ8zk%%Url^%XTrc<5Q;7FcC3^_VqB z^=Kt=rucES%IQ8>^drG7dh4GQl_>Rvs9{kF*LaeykPjm9iaVcpRo+ege+DKw<5N+q zx{OIhz1M0^Zi`acaT?()$GRjNd?EhPpf^cw5^4#)>KEq$Y5%C?xLdk>n@=Ps^BDDp znE%y(lzb6$-&vBb^1nc1UGn+yK7gIkjc7a|1EQvMQU0o*ph-2>?H74MZBsXmeUcjX7; mtw5fh_d%5@Bc5P`Qau2 literal 0 HcmV?d00001 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