Skip to content

Commit

Permalink
s fixed sending of periodic PINGREQ messages to gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
haukepetersen committed Feb 27, 2017
1 parent e834a8c commit 8a71690
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 34 deletions.
21 changes: 13 additions & 8 deletions sys/include/net/emcute.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,10 @@
* @todo handle DISCONNECT messages initiated by the broker/gateway
* @todo support for pre-defined and short topic IDs
* @todo handle (previously) active subscriptions on reconnect/disconnect
* @todo handle/disconnect from unresponsive gateway (in case a number of
* ping requests get no answer)
* @todo some minor things -> look for 'todo' tags in the code...
*
* @todo periodic ping requests are broken (commented out)
* @todo handle re-connect/disconnect from unresponsive gateway (in case
* a number of ping requests are unanswered)
* @todo react only to incoming ping requests that are actually send by
* the gateway we are connected to
*
* @{
* @file
Expand All @@ -94,7 +93,7 @@ extern "C" {

#ifndef EMCUTE_DEFAULT_PORT
/**
* @brief Default UDP port to listen on (also used as SRC port
* @brief Default UDP port to listen on (also used as SRC port)
*/
#define EMCUTE_DEFAULT_PORT (1883U)
#endif
Expand Down Expand Up @@ -134,20 +133,26 @@ extern "C" {
*
* The node will communicate this interval to the gateway send a ping message
* every time when this amount of time has passed.
*
* For the default value, see spec v1.2, section 7.2 -> T_WAIT: > 5 min
*/
#define EMCUTE_KEEPALIVE (5) /* 600 -> 10 min*/
#define EMCUTE_KEEPALIVE (360) /* -> 6 min*/
#endif

#ifndef EMCUTE_T_RETRY
/**
* @brief Re-send interval [in seconds]
*
* For the default value, see spec v1.2, section 7.2 -> T_RETRY: 10 to 15 sec
*/
#define EMCUTE_T_RETRY (15U)
#define EMCUTE_T_RETRY (15U) /* -> 15 sec */
#endif

#ifndef EMCUTE_N_RETRY
/**
* @brief Number of retries when sending packets
*
* For the default value, see spec v1.2, section 7.2 -> N_RETRY: 3-5
*/
#define EMCUTE_N_RETRY (3U)
#endif
Expand Down
47 changes: 21 additions & 26 deletions sys/net/application_layer/emcute/emcute.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ static volatile uint8_t waiton = 0xff;
static volatile uint16_t waitonid = 0;
static volatile int result;


static inline uint16_t get_u16(const uint8_t *buf)
{
#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
Expand Down Expand Up @@ -206,7 +205,8 @@ static void on_publish(void)

static void on_pingreq(sock_udp_ep_t *remote)
{
/* all we do is to respond with a simple PINGRESP */
/* @todo respond with a PINGRESP only if the PINGREQ came from the
* connected gateway -> see spec v1.2, section 6.11 */
uint8_t buf[2] = { 2, PINGRESP };
sock_udp_send(&sock, &buf, 2, remote);
}
Expand All @@ -216,13 +216,13 @@ static void on_pingresp(void)
/** @todo: trigger update something like a 'last seen' value */
}

// static void send_ping(void)
// {
// if (gateway.port != 0) {
// uint8_t buf[2] = { 2, PINGREQ };
// sock_udp_send(&sock, &buf, 2, &gateway);
// }
// }
static void send_ping(void)
{
if (gateway.port != 0) {
uint8_t buf[2] = { 2, PINGREQ };
sock_udp_send(&sock, &buf, 2, &gateway);
}
}

int emcute_con(sock_udp_ep_t *remote, bool clean, const char *will_topic,
const void *will_msg, size_t will_msg_len, unsigned will_flags)
Expand Down Expand Up @@ -342,9 +342,6 @@ int emcute_reg(emcute_topic_t *topic)
int emcute_pub(emcute_topic_t *topic, const void *data, size_t len,
unsigned flags)
{

printf("flgas are %02x\n", (int)flags);

int res = EMCUTE_OK;

assert((topic->id != 0) && data && (len > 0) && !(flags & ~PUB_FLAGS));
Expand Down Expand Up @@ -438,7 +435,6 @@ int emcute_unsub(emcute_sub_t *sub)
set_u16(&tbuf[3], id_next);
waitonid = id_next++;
memcpy(&tbuf[5], sub->topic.name, strlen(sub->topic.name));
// set_u16(&tbuf[5], sub->topic.id);

int res = syncsend(UNSUBACK, (size_t)tbuf[0], false);
if (res == EMCUTE_OK) {
Expand Down Expand Up @@ -524,12 +520,11 @@ void emcute_run(uint16_t port, const char *id)
return;
}

// uint32_t start = xtimer_now_usec();
// uint32_t t_out = (EMCUTE_KEEPALIVE * US_PER_SEC);
uint32_t start = xtimer_now_usec();
uint32_t t_out = (EMCUTE_KEEPALIVE * US_PER_SEC);

while (1) {
// ssize_t len = sock_udp_recv(&sock, rbuf, sizeof(rbuf), t_out, &remote);
ssize_t len = sock_udp_recv(&sock, rbuf, sizeof(rbuf), SOCK_NO_TIMEOUT, &remote);
ssize_t len = sock_udp_recv(&sock, rbuf, sizeof(rbuf), t_out, &remote);

if ((len < 0) && (len != -ETIMEDOUT)) {
LOG_ERROR("[emcute] error while receiving UDP packet\n");
Expand Down Expand Up @@ -562,14 +557,14 @@ void emcute_run(uint16_t port, const char *id)
}
}

// uint32_t now = xtimer_now_usec() - start;
// if ((now - start) >= (EMCUTE_KEEPALIVE * US_PER_SEC)) {
// send_ping();
// start = now;
// t_out = (EMCUTE_KEEPALIVE * US_PER_SEC);
// }
// else {
// t_out -= (now - start);
// }
uint32_t now = xtimer_now_usec();
if ((now - start) >= (EMCUTE_KEEPALIVE * US_PER_SEC)) {
send_ping();
start = now;
t_out = (EMCUTE_KEEPALIVE * US_PER_SEC);
}
else {
t_out = (EMCUTE_KEEPALIVE * US_PER_SEC) - (now - start);
}
}
}

0 comments on commit 8a71690

Please sign in to comment.