From 1f8754766a751c3f9c548340ca361577acd43269 Mon Sep 17 00:00:00 2001 From: "carlosgp143@gmail.com" Date: Fri, 6 Apr 2018 15:16:42 +0200 Subject: [PATCH] Added macros for defining wake up/sleep behaviour depending on the platform --- examples/ipso-objects/zoul/module-macros.h | 16 ++++++- os/services/lwm2m/lwm2m-rd-client.c | 52 +++++++++++++--------- 2 files changed, 45 insertions(+), 23 deletions(-) diff --git a/examples/ipso-objects/zoul/module-macros.h b/examples/ipso-objects/zoul/module-macros.h index 5091ff08b..4db4e82fe 100644 --- a/examples/ipso-objects/zoul/module-macros.h +++ b/examples/ipso-objects/zoul/module-macros.h @@ -27,8 +27,22 @@ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED * OF THE POSSIBILITY OF SUCH DAMAGE. */ - /*---------------------------------------------------------------------------*/ /* Only sleep mode 1 on Zoul to enable full 32 KiB RAM */ #define LPM_CONF_MAX_PM 1 /*---------------------------------------------------------------------------*/ +/* Macros to enter sleep mode and wake up in the Zoul module. Sleep consists + * on turn off the radio and start a RTIMER to wake up, and wake up consists on + * turn on the radio again + */ +#define LWM2M_Q_MODE_WAKE_UP() do { \ + NETSTACK_MAC.on(); \ +} while(0) + +#define LWM2M_Q_MODE_SLEEP_MS(TIME_MS) do { \ + uint64_t aux = TIME_MS * RTIMER_SECOND; \ + NETSTACK_MAC.off(); \ + rtimer_arch_schedule(RTIMER_NOW() + (rtimer_clock_t)(aux / 1000)); \ +} while(0) + + diff --git a/os/services/lwm2m/lwm2m-rd-client.c b/os/services/lwm2m/lwm2m-rd-client.c index 61273797a..cf7c5deb3 100644 --- a/os/services/lwm2m/lwm2m-rd-client.c +++ b/os/services/lwm2m/lwm2m-rd-client.c @@ -43,7 +43,6 @@ * Joel Hoglund * Carlos Gonzalo Peces */ - #include "lwm2m-engine.h" #include "lwm2m-object.h" #include "lwm2m-device.h" @@ -58,15 +57,16 @@ #include #include #include -#if LWM2M_Q_MODE_ENABLED -#include "lwm2m-qmode-object.h" -#include "lwm2m-notification-queue.h" -#endif #if UIP_CONF_IPV6_RPL #include "rpl.h" #endif /* UIP_CONF_IPV6_RPL */ +#if LWM2M_Q_MODE_ENABLED +#include "lwm2m-qmode-object.h" +#include "lwm2m-notification-queue.h" +#endif /* LWM2M_Q_MODE_ENABLED */ + /* Log configuration */ #include "coap-log.h" #define LOG_MODULE "lwm2m-rd" @@ -131,10 +131,15 @@ static void (*rd_callback)(coap_request_state_t *state); static coap_timer_t block1_timer; #if LWM2M_Q_MODE_ENABLED -static coap_timer_t q_mode_client_awake_timer; /* Timer to control the client awake time */ -static uint8_t q_mode_client_awake; /* 1 - client is awake, 0 - client is sleeping */ +static coap_timer_t q_mode_client_awake_timer; /* Timer to control the client's + * awake time + */ +static uint8_t q_mode_client_awake; /* 1 - client is awake, + * 0 - client is sleeping + */ static uint16_t q_mode_client_awake_time; /* The time to be awake */ -static void q_mode_awake_timer_callback(coap_timer_t *timer); /* Callback for the client awake timer */ +/* Callback for the client awake timer */ +static void q_mode_awake_timer_callback(coap_timer_t *timer); #endif static void check_periodic_observations(); @@ -742,16 +747,19 @@ periodic_process(coap_timer_t *timer) coap_timer_set(&q_mode_client_awake_timer, q_mode_client_awake_time); break; case Q_MODE_SEND_UPDATE: -#if !STANDALONE - NETSTACK_MAC.on(); -#endif +/* Define this macro to make the necessary actions for waking up, + * depending on the platform + */ +#ifdef LWM2M_Q_MODE_WAKE_UP + LWM2M_Q_MODE_WAKE_UP(); +#endif /* LWM2M_Q_MODE_WAKE_UP */ prepare_update(request, rd_flags & FLAG_RD_DATA_UPDATE_TRIGGERED); coap_send_request(&rd_request_state, &session_info.server_ep, request, update_callback); last_rd_progress = coap_timer_uptime(); rd_state = UPDATE_SENT; break; -#endif +#endif /* LWM2M_Q_MODE_ENABLED */ case UPDATE_SENT: /* just wait until the callback kicks us to the next state... */ @@ -787,9 +795,10 @@ lwm2m_rd_client_init(const char *ep) /* default binding U = UDP, UQ = UDP Q-mode*/ #if LWM2M_Q_MODE_ENABLED session_info.binding = "UQ"; - session_info.lifetime = (LWM2M_Q_MODE_DEFAULT_CLIENT_SLEEP_TIME / 1000) * 2; /* Enough margin to ensure that the client is not unregistered (we - * do not know the time it can stay awake) - */ + /* Enough margin to ensure that the client is not unregistered (we + * do not know the time it can stay awake) + */ + session_info.lifetime = (LWM2M_Q_MODE_DEFAULT_CLIENT_SLEEP_TIME / 1000) * 2; #else session_info.binding = "U"; if(session_info.lifetime == 0) { @@ -836,12 +845,11 @@ q_mode_awake_timer_callback(coap_timer_t *timer) /* Timer has expired, no requests has been received, client can go to sleep */ LOG_DBG("Queue Mode: Client is SLEEPING at %lu\n", (unsigned long)coap_timer_uptime()); q_mode_client_awake = 0; -#if !STANDALONE - /* Turn off the radio and start sleeping timer. LPM will be entered since the radio - and the peripherals are off, and there is a sleep timer started for waking up */ - NETSTACK_MAC.off(); - rtimer_arch_schedule(RTIMER_NOW() + ((lwm2m_q_object_get_sleep_time() / 1000) * RTIMER_SECOND)); -#endif + +/* Define this macro to enter sleep mode depending on the platform */ +#ifdef LWM2M_Q_MODE_SLEEP_MS + LWM2M_Q_MODE_SLEEP_MS(lwm2m_q_object_get_sleep_time()); +#endif /* LWM2M_Q_MODE_SLEEP_MS */ rd_state = Q_MODE_SEND_UPDATE; coap_timer_set(&rd_timer, lwm2m_q_object_get_sleep_time()); } @@ -862,6 +870,6 @@ lwm2m_rd_client_fsm_execute_q_mode_update() periodic_process(&rd_timer); } /*---------------------------------------------------------------------------*/ -#endif +#endif /* LWM2M_Q_MODE_ENABLED */ /*---------------------------------------------------------------------------*/ /** @} */