-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from rosetree/feature/rd-registration
Feature/rd registration
- Loading branch information
Showing
6 changed files
with
286 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ | |
* @brief CoAP endpoint for the SAUL registry | ||
* | ||
* @author Micha Rosenbaum <[email protected]> | ||
* | ||
* @author Matthias Bräuer <[email protected]> | ||
* @} | ||
*/ | ||
|
||
|
@@ -290,3 +290,4 @@ void saul_coap_init(void) | |
{ | ||
gcoap_register_listener(&_listener); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,156 @@ | ||
/** | ||
* Copyright (C) 2019 HAW Hamburg | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
* | ||
* @ingroup pkg | ||
* @{ | ||
* | ||
* @file | ||
* @brief Register SAUL registry to resource directory | ||
* | ||
* @author Matthias Bräuer <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
|
||
#include "saul_cord_ep.h" | ||
|
||
#define ENABLE_DEBUG (0) | ||
#include "debug.h" | ||
|
||
/* stack configuration */ | ||
#define STACKSIZE (THREAD_STACKSIZE_DEFAULT) | ||
#define PRIO (THREAD_PRIORITY_MAIN - 1) | ||
#define TNAME "cord_ep" | ||
|
||
#define UPDATE_TIMEOUT (0xe537) | ||
|
||
#define TIMEOUT_US ((uint64_t)(CORD_UPDATE_INTERVAL * US_PER_SEC)) | ||
|
||
static char _stack[STACKSIZE]; | ||
|
||
static xtimer_t _timer; | ||
static kernel_pid_t _runner_pid; | ||
static msg_t _msg; | ||
|
||
static saul_cord_ep_cb_t _cb = NULL; | ||
|
||
// The ipv6 address (and port) of the resource directory | ||
static const char *ip = NULL; | ||
|
||
static void _set_timer(void) | ||
{ | ||
xtimer_set_msg64(&_timer, TIMEOUT_US, &_msg, _runner_pid); | ||
} | ||
|
||
/** | ||
* @brief Execute the callback function if present. | ||
* | ||
* @param[in] event The event that occurred | ||
*/ | ||
static void _notify(saul_cord_ep_event_t event) { | ||
if (_cb != NULL) { | ||
_cb(event); | ||
} | ||
} | ||
|
||
/** | ||
* @brief Create an UDP socket. | ||
* | ||
* @param[out] ep Will contain the enpoint of the IPv6 address | ||
* @param[in] addr The IPv6 address to be used for the socket | ||
* @return `0` on success, else `-1` | ||
*/ | ||
static int make_sock_ep(sock_udp_ep_t *ep, const char *addr) | ||
{ | ||
ep->port = 0; | ||
if (sock_udp_str2ep(ep, addr) < 0) { | ||
return -1; | ||
} | ||
ep->family = AF_INET6; | ||
ep->netif = SOCK_ADDR_ANY_NETIF; | ||
if (ep->port == 0) { | ||
ep->port = COAP_PORT; | ||
} | ||
return 0; | ||
} | ||
|
||
/** | ||
* @brief Try to register to resource directory service. | ||
*/ | ||
static int register_rd(void) { | ||
sock_udp_ep_t remote; | ||
char *regif = NULL; | ||
|
||
if (make_sock_ep(&remote, ip) < 0) { | ||
puts("error: unable to parse address\n"); | ||
return 1; | ||
} | ||
|
||
puts("Registering with RD now, this may take a short while..."); | ||
|
||
if (cord_ep_register(&remote, regif) != CORD_EP_OK) { | ||
_notify(SAUL_CORD_EP_DEREGISTERED); | ||
return 1; | ||
} | ||
|
||
_notify(SAUL_CORD_EP_REGISTERED); | ||
|
||
return 0; | ||
} | ||
|
||
static void saul_cord_ep_register(void) { | ||
printf("DEBUG: RD-ADDRESS: %s\n", ip); | ||
|
||
while (register_rd()) { | ||
puts("Try again to register to RD deamon"); | ||
} | ||
|
||
cord_ep_dump_status(); | ||
} | ||
|
||
static void *_reg_runner(void *arg) | ||
{ | ||
(void)arg; | ||
msg_t in; | ||
|
||
/* prepare context and message */ | ||
_runner_pid = thread_getpid(); | ||
_msg.type = UPDATE_TIMEOUT; | ||
|
||
while (1) { | ||
msg_receive(&in); | ||
if (in.type == UPDATE_TIMEOUT) { | ||
if (cord_ep_update() == CORD_EP_OK) { | ||
_set_timer(); | ||
_notify(SAUL_CORD_EP_UPDATED); | ||
} | ||
else { | ||
_notify(SAUL_CORD_EP_DEREGISTERED); | ||
saul_cord_ep_register(); | ||
_set_timer(); | ||
} | ||
} | ||
} | ||
|
||
return NULL; /* should never be reached */ | ||
} | ||
|
||
void saul_cord_ep_register_cb(saul_cord_ep_cb_t cb) { | ||
_cb = cb; | ||
} | ||
|
||
void saul_cord_ep_create(const char* ip_address) | ||
{ | ||
ip = ip_address; | ||
thread_create(_stack, sizeof(_stack), PRIO, THREAD_CREATE_STACKTEST, | ||
_reg_runner, NULL, TNAME); | ||
} | ||
|
||
void saul_cord_ep_run(void) { | ||
_set_timer(); | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright (C) 2019 HAW Hamburg | ||
* | ||
* This file is subject to the terms and conditions of the GNU Lesser | ||
* General Public License v2.1. See the file LICENSE in the top level | ||
* directory for more details. | ||
* | ||
* @ingroup pkg | ||
* @{ | ||
* | ||
* @file | ||
* @brief Register SAUL registry to resource directory and update it periodically. | ||
* | ||
* @author Matthias Bräuer <[email protected]> | ||
* | ||
* @} | ||
*/ | ||
#ifndef SAUL_CORD_EP_H | ||
#define SAUL_CORD_EP_H | ||
|
||
#include "net/nanocoap.h" | ||
#include "net/cord/config.h" | ||
#include "net/cord/ep.h" | ||
#include "net/sock/util.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
/** | ||
* @brief Possible types of events when interacting with a Resource Directory. | ||
*/ | ||
typedef enum { | ||
SAUL_CORD_EP_REGISTERED, | ||
SAUL_CORD_EP_DEREGISTERED, | ||
SAUL_CORD_EP_UPDATED, | ||
} saul_cord_ep_event_t; | ||
|
||
/** | ||
* @brief Callback function signature for RD endpoint state synchronization. | ||
* | ||
* The registered callback function is executed in the context of the dedicated | ||
* RD endpoint's thread. | ||
* | ||
* @param[in] event Type of event | ||
*/ | ||
typedef void (*saul_cord_ep_cb_t)(saul_cord_ep_event_t event); | ||
|
||
/** | ||
* @brief Create the saul-cord thread. | ||
* | ||
* @param[in] ip_address The IPv6 address of the resource directory service | ||
* | ||
* @warning This function must only be called once (typically during system | ||
* initialization) | ||
*/ | ||
void saul_cord_ep_create(const char *ip_address); | ||
|
||
/** | ||
* @brief Run the saul-cord thread. | ||
* | ||
* @warning This function must only be called once (typically during system | ||
* initialization) | ||
*/ | ||
void saul_cord_ep_run(void); | ||
|
||
/** | ||
* @brief Register a callback to be notified about RD endpoint state changes. | ||
* | ||
* Only a single callback can be active at any point in time, so setting a new | ||
* callback will override the existing one. | ||
* | ||
* @pre @p cb != NULL | ||
* | ||
* @param[in] cb The callback to be executed on RD endpoint state changes | ||
*/ | ||
void saul_cord_ep_register_cb(saul_cord_ep_cb_t cb); | ||
|
||
|
||
#ifdef __cplusplus | ||
} | ||
#endif | ||
|
||
#endif /* SAUL_CORD_EP_H */ | ||
|