Skip to content

Commit

Permalink
coap_startup: Make coap_startup() first function call in examples
Browse files Browse the repository at this point in the history
This then makes sure that all things libcoap are properly initialized
before use, and coders realize they need to do this.

Note: coap_new_context() continues to make sure that coap_startup()
is invoked if not done previously, but this may be too late.
  • Loading branch information
mrdeep1 committed Jul 25, 2023
1 parent 2f98643 commit 084c866
Show file tree
Hide file tree
Showing 21 changed files with 179 additions and 20 deletions.
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -1100,6 +1100,7 @@ man/coap_encryption.txt
man/coap_endpoint_client.txt
man/coap_endpoint_server.txt
man/coap_handler.txt
man/coap_init.txt
man/coap_io.txt
man/coap_keepalive.txt
man/coap_logging.txt
Expand Down
4 changes: 3 additions & 1 deletion examples/coap-client.c
Original file line number Diff line number Diff line change
Expand Up @@ -1606,6 +1606,9 @@ main(int argc, char **argv) {
struct sigaction sa;
#endif

/* Initialize libcoap library */
coap_startup();

while ((opt = getopt(argc, argv,
"a:b:c:e:f:h:j:k:l:m:no:p:rs:t:u:v:wA:B:C:E:G:H:J:K:L:M:NO:P:R:T:UV:X:")) != -1) {
switch (opt) {
Expand Down Expand Up @@ -1775,7 +1778,6 @@ main(int argc, char **argv) {
sigaction(SIGPIPE, &sa, NULL);
#endif

coap_startup();
coap_set_log_level(log_level);
coap_dtls_set_log_level(dtls_log_level);

Expand Down
4 changes: 3 additions & 1 deletion examples/coap-rd.c
Original file line number Diff line number Diff line change
Expand Up @@ -767,6 +767,9 @@ main(int argc, char **argv) {
struct sigaction sa;
#endif

/* Initialize libcoap library */
coap_startup();

while ((opt = getopt(argc, argv, "A:c:C:g:G:h:k:n:R:p:v:T:V:w:")) != -1) {
switch (opt) {
case 'A' :
Expand Down Expand Up @@ -834,7 +837,6 @@ main(int argc, char **argv) {
}
}

coap_startup();
coap_set_log_level(log_level);
coap_dtls_set_log_level(dtls_log_level);

Expand Down
4 changes: 3 additions & 1 deletion examples/coap-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -2734,6 +2734,9 @@ main(int argc, char **argv) {
struct sigaction sa;
#endif

/* Initialize libcoap library */
coap_startup();

clock_offset = time(NULL);

while ((opt = getopt(argc, argv,
Expand Down Expand Up @@ -2910,7 +2913,6 @@ main(int argc, char **argv) {
sigaction(SIGPIPE, &sa, NULL);
#endif

coap_startup();
coap_set_log_level(log_level);
coap_dtls_set_log_level(dtls_log_level);

Expand Down
4 changes: 4 additions & 0 deletions examples/contiki/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,9 @@ struct etimer dirty_timer;
PROCESS_THREAD(coap_server_process, ev, data) {
PROCESS_BEGIN();

/* Initialize libcoap library */
coap_startup();

clock_offset = clock_time();
init_coap_server(&coap_context);

Expand All @@ -182,6 +185,7 @@ PROCESS_THREAD(coap_server_process, ev, data) {
etimer_reset(&dirty_timer);
}
}
coap_cleanup();

PROCESS_END();
}
Expand Down
3 changes: 3 additions & 0 deletions examples/etsi_iot_01.c
Original file line number Diff line number Diff line change
Expand Up @@ -582,6 +582,9 @@ main(int argc, char **argv) {
coap_log_t log_level = COAP_LOG_WARN;
struct sigaction sa;

/* Initialize libcoap library */
coap_startup();

while ((opt = getopt(argc, argv, "A:p:v:")) != -1) {
switch (opt) {
case 'A' :
Expand Down
4 changes: 4 additions & 0 deletions examples/lwip/client-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,9 @@ client_coap_init(coap_lwip_input_wait_handler_t input_wait, void *input_arg,
const char *use_id = "abc";
coap_pdu_type_t pdu_type = COAP_MESSAGE_CON;

/* Initialize libcoap library */
coap_startup();

while ((opt = getopt(argc, argv, ":k:Nu:v:V:")) != -1) {
switch (opt) {
case 'k':
Expand Down Expand Up @@ -227,6 +230,7 @@ client_coap_finished(void) {
coap_delete_optlist(optlist);
coap_free_context(main_coap_context);
main_coap_context = NULL;
coap_cleanup();
}

int
Expand Down
4 changes: 4 additions & 0 deletions examples/lwip/server-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ server_coap_init(coap_lwip_input_wait_handler_t input_wait,
int have_ep = 0;
coap_str_const_t node;

/* Initialize libcoap library */
coap_startup();

while ((opt = getopt(argc, argv, ":k:v:V:")) != -1) {
switch (opt) {
case 'k':
Expand Down Expand Up @@ -197,6 +200,7 @@ void
server_coap_finished(void) {
coap_free_context(main_coap_context);
main_coap_context = NULL;
coap_cleanup();
}

void
Expand Down
4 changes: 3 additions & 1 deletion examples/oscore-interop-server.c
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,9 @@ main(int argc, char **argv) {
struct sigaction sa;
#endif

/* Initialize libcoap library */
coap_startup();

while ((opt = getopt(argc, argv, "g:G:l:p:rv:A:E:L:NX:")) != -1) {
switch (opt) {
case 'A' :
Expand Down Expand Up @@ -659,7 +662,6 @@ main(int argc, char **argv) {
sigaction(SIGPIPE, &sa, NULL);
#endif

coap_startup();
coap_dtls_set_log_level(log_level);
coap_set_log_level(log_level);

Expand Down
2 changes: 2 additions & 0 deletions examples/riot/examples_libcoap_client/client-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,9 @@ client_coap_init(int argc, char **argv) {
(void)argc;
(void)argv;

/* Initialize libcoap library */
coap_startup();

coap_set_log_level(COAP_MAX_LOGGING_LEVEL);

/* Parse the URI */
Expand Down
3 changes: 3 additions & 0 deletions examples/riot/examples_libcoap_server/server-coap.c
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,9 @@ void *
server_coap_run(void *arg) {
(void)arg;

/* Initialize libcoap library */
coap_startup();

coap_set_log_level(COAP_MAX_LOGGING_LEVEL);

if (!init_coap_context_endpoints(COAP_USE_PSK))
Expand Down
4 changes: 4 additions & 0 deletions examples/tiny.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,9 @@ main(int argc, char **argv) {
struct sigaction sa;
coap_context_t *ctx;

/* Initialize libcoap library */
coap_startup();

if (argc > 1 && strncmp(argv[1], "-h", 2) == 0) {
usage(argv[0]);
exit(1);
Expand Down Expand Up @@ -180,6 +183,7 @@ main(int argc, char **argv) {
}

coap_free_context(ctx);
coap_cleanup();

return 0;
}
2 changes: 2 additions & 0 deletions include/coap3/coap_net_internal.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,4 +416,6 @@ int coap_client_delay_first(coap_session_t *session);

/** @} */

extern int coap_started;

#endif /* COAP_NET_INTERNAL_H_ */
1 change: 1 addition & 0 deletions man/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ TXT3 = coap_address.txt \
coap_encryption.txt \
coap_endpoint_client.txt \
coap_handler.txt \
coap_init.txt \
coap_io.txt \
coap_keepalive.txt \
coap_logging.txt \
Expand Down
15 changes: 12 additions & 3 deletions man/coap_block.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -404,7 +404,8 @@ error:

}

int main(int argc, char *argv[]) {
int
main(int argc, char *argv[]) {
coap_context_t *context = NULL;
coap_session_t *session = NULL;
unsigned char *data = NULL;
Expand All @@ -413,6 +414,9 @@ int main(int argc, char *argv[]) {
(void)argc;
(void)argv;

/* Initialize libcoap library */
coap_startup();

/* ... Set up context, session etc. ... */

/* Set up using libcoap to do the block work */
Expand All @@ -428,6 +432,7 @@ int main(int argc, char *argv[]) {

/* ... Other code etc. ... */

coap_cleanup();
return 0;
}
----
Expand Down Expand Up @@ -494,12 +499,16 @@ const coap_pdu_t *request, const coap_string_t *query, coap_pdu_t *response) {
*/
}

int main(int argc, char *argv[]) {
int
main(int argc, char *argv[]) {
coap_context_t *context = NULL;
coap_resource_t *r;
coap_resource_t *time_resource;
int not_exit = 1;

/* Initialize libcoap library */
coap_startup();

(void)argc;
(void)argv;

Expand Down Expand Up @@ -545,7 +554,7 @@ int main(int argc, char *argv[]) {

SEE ALSO
--------
*coap_pdu_setup*(3), *coap_observe*(3), and *coap_resource*(3)
*coap_init*(3) *coap_pdu_setup*(3), *coap_observe*(3), and *coap_resource*(3)

FURTHER INFORMATION
-------------------
Expand Down
10 changes: 8 additions & 2 deletions man/coap_cache.txt.in
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,16 @@ hnd_put_example_data(coap_context_t *ctx,
coap_resource_notify_observers(resource, NULL);
}

int main(int argc, char* argv[]){
int
main(int argc, char* argv[]) {
coap_context_t *ctx = NULL; /* Set up as normal */
/* ... */
uint16_t cache_ignore_options[] = { COAP_OPTION_BLOCK1,
COAP_OPTION_BLOCK2 };

/* Initialize libcoap library */
coap_startup();

/* Remove (void) definition if variable is used */
(void)argc;
(void)argv;
Expand All @@ -392,13 +396,15 @@ int main(int argc, char* argv[]){
sizeof(cache_ignore_options)/sizeof(cache_ignore_options[0]));

/* ... */
coap_cleanup();

}
----

SEE ALSO
--------
*coap_block*(3), *coap_pdu_setup*(3), *coap_resource*(3) and *coap_string*(3)
*coap_block*(3), *coap_init*(3), *coap_pdu_setup*(3), *coap_resource*(3)
and *coap_string*(3)

FURTHER INFORMATION
-------------------
Expand Down
72 changes: 72 additions & 0 deletions man/coap_init.txt.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// -*- mode:doc; -*-
// vim: set syntax=asciidoc tw=0

coap_init(3)
============
:doctype: manpage
:man source: coap_init
:man version: @PACKAGE_VERSION@
:man manual: libcoap Manual

NAME
----
coap_init,
coap_startup,
coap_cleanup
- Work with CoAP initialization

SYNOPSIS
--------
*#include <coap@LIBCOAP_API_VERSION@/coap.h>*

*void coap_startup(void);*

*void coap_cleanup(void);*

For specific (D)TLS library support, link with
*-lcoap-@LIBCOAP_API_VERSION@-notls*, *-lcoap-@LIBCOAP_API_VERSION@-gnutls*,
*-lcoap-@LIBCOAP_API_VERSION@-openssl*, *-lcoap-@LIBCOAP_API_VERSION@-mbedtls*
or *-lcoap-@LIBCOAP_API_VERSION@-tinydtls*. Otherwise, link with
*-lcoap-@LIBCOAP_API_VERSION@* to get the default (D)TLS library support.

DESCRIPTION
-----------

The CoAP libcoap logic needs to be initialized before there is any use of the
libcoap public API. This is done by calling *coap_startup*().

FUNCTIONS
---------

*Function: coap_startup()*

The *coap_startup*() function must be called before any other *coap_**()
functions are called. It is used to initialize things like mutexes,
random number gererators, clocks, TLS libraries etc.

*NOTE:* This should be called after any other lower layer is initialized.
For example, for LwIP, lwip_init() must be called before *coap_startup*().

*Function: coap_cleanup()*

The *coap_cleanup*() function is used to cleanup / free any information set
up by the *coap_startup*() function and should be the last *coap_**() function
called.

FURTHER INFORMATION
-------------------
See

"https://rfc-editor.org/rfc/rfc7252[RFC7252: The Constrained Application Protocol (CoAP)]"

for further information.

BUGS
----
Please report bugs on the mailing list for libcoap:
[email protected] or raise an issue on GitHub at
https://github.com/obgm/libcoap/issues

AUTHORS
-------
The libcoap project <[email protected]>
Loading

0 comments on commit 084c866

Please sign in to comment.