2015-12-01 03:14:46 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2015, Yanzi Networks AB.
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \addtogroup ipso-objects
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \file
|
|
|
|
* Implementation of OMA LWM2M / IPSO button as a digital input
|
2017-10-25 00:27:14 +00:00
|
|
|
* NOTE: only works with a Contiki Button Sensor (not for Standalone)
|
2015-12-01 03:14:46 +00:00
|
|
|
* \author
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "lwm2m-object.h"
|
|
|
|
#include "lwm2m-engine.h"
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2017-10-25 00:27:14 +00:00
|
|
|
#define IPSO_INPUT_STATE 5500
|
|
|
|
#define IPSO_INPUT_COUNTER 5501
|
|
|
|
#define IPSO_INPUT_DEBOUNCE 5503
|
|
|
|
#define IPSO_INPUT_EDGE_SEL 5504
|
|
|
|
#define IPSO_INPUT_CTR_RESET 5505
|
|
|
|
#define IPSO_INPUT_SENSOR_TYPE 5751
|
|
|
|
|
2015-12-01 03:14:46 +00:00
|
|
|
#if PLATFORM_HAS_BUTTON
|
2017-11-05 22:38:45 +00:00
|
|
|
#if PLATFORM_SUPPORTS_BUTTON_HAL
|
|
|
|
#include "dev/button-hal.h"
|
2017-10-25 00:27:14 +00:00
|
|
|
#else
|
2017-11-05 22:38:45 +00:00
|
|
|
#include "dev/button-sensor.h"
|
2017-10-25 00:27:14 +00:00
|
|
|
#define IPSO_BUTTON_SENSOR button_sensor
|
2017-11-05 22:38:45 +00:00
|
|
|
static struct etimer timer;
|
2017-10-25 00:27:14 +00:00
|
|
|
#endif
|
|
|
|
|
2015-12-01 03:14:46 +00:00
|
|
|
PROCESS(ipso_button_process, "ipso-button");
|
|
|
|
#endif /* PLATFORM_HAS_BUTTON */
|
|
|
|
|
2017-10-25 00:27:14 +00:00
|
|
|
|
|
|
|
static lwm2m_status_t lwm2m_callback(lwm2m_object_instance_t *object,
|
|
|
|
lwm2m_context_t *ctx);
|
|
|
|
|
2015-12-01 03:14:46 +00:00
|
|
|
static int input_state = 0;
|
|
|
|
static int32_t counter = 0;
|
2017-10-25 00:27:14 +00:00
|
|
|
static int32_t edge_selection = 3; /* both */
|
2015-12-01 03:14:46 +00:00
|
|
|
static int32_t debounce_time = 10;
|
2017-10-25 00:27:14 +00:00
|
|
|
|
|
|
|
static const lwm2m_resource_id_t resources[] = {
|
2018-03-17 22:38:14 +00:00
|
|
|
RO(IPSO_INPUT_STATE), RO(IPSO_INPUT_COUNTER),
|
2017-10-25 00:27:14 +00:00
|
|
|
RW(IPSO_INPUT_DEBOUNCE), RW(IPSO_INPUT_EDGE_SEL), EX(IPSO_INPUT_CTR_RESET),
|
|
|
|
RO(IPSO_INPUT_SENSOR_TYPE)
|
|
|
|
};
|
|
|
|
|
|
|
|
/* Only support for one button for now */
|
|
|
|
static lwm2m_object_instance_t reg_object = {
|
|
|
|
.object_id = 3200,
|
|
|
|
.instance_id = 0,
|
|
|
|
.resource_ids = resources,
|
|
|
|
.resource_count = sizeof(resources) / sizeof(lwm2m_resource_id_t),
|
|
|
|
.callback = lwm2m_callback,
|
|
|
|
};
|
|
|
|
|
2015-12-01 03:14:46 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
2017-10-25 00:27:14 +00:00
|
|
|
read_state(void)
|
2015-12-01 03:14:46 +00:00
|
|
|
{
|
2018-03-17 22:38:14 +00:00
|
|
|
PRINTF("Read button state: %d\n", input_state);
|
|
|
|
return input_state;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-25 00:27:14 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static lwm2m_status_t
|
|
|
|
lwm2m_callback(lwm2m_object_instance_t *object,
|
|
|
|
lwm2m_context_t *ctx)
|
2015-12-01 03:14:46 +00:00
|
|
|
{
|
2017-10-25 00:27:14 +00:00
|
|
|
if(ctx->operation == LWM2M_OP_READ) {
|
|
|
|
switch(ctx->resource_id) {
|
|
|
|
case IPSO_INPUT_STATE:
|
|
|
|
lwm2m_object_write_int(ctx, read_state());
|
|
|
|
break;
|
|
|
|
case IPSO_INPUT_COUNTER:
|
|
|
|
lwm2m_object_write_int(ctx, counter);
|
|
|
|
break;
|
|
|
|
case IPSO_INPUT_DEBOUNCE:
|
|
|
|
lwm2m_object_write_int(ctx, debounce_time);
|
|
|
|
break;
|
|
|
|
case IPSO_INPUT_EDGE_SEL:
|
|
|
|
lwm2m_object_write_int(ctx, edge_selection);
|
|
|
|
break;
|
|
|
|
case IPSO_INPUT_SENSOR_TYPE:
|
|
|
|
lwm2m_object_write_string(ctx, "button", strlen("button"));
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
return LWM2M_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
} else if(ctx->operation == LWM2M_OP_EXECUTE) {
|
|
|
|
if(ctx->resource_id == IPSO_INPUT_CTR_RESET) {
|
|
|
|
counter = 0;
|
|
|
|
} else {
|
|
|
|
return LWM2M_STATUS_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return LWM2M_STATUS_OK;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
ipso_button_init(void)
|
|
|
|
{
|
|
|
|
/* register this device and its handlers - the handlers automatically
|
|
|
|
sends in the object to handle */
|
2017-10-25 00:27:14 +00:00
|
|
|
lwm2m_engine_add_object(®_object);
|
2015-12-01 03:14:46 +00:00
|
|
|
|
|
|
|
#if PLATFORM_HAS_BUTTON
|
|
|
|
process_start(&ipso_button_process, NULL);
|
|
|
|
#endif /* PLATFORM_HAS_BUTTON */
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#if PLATFORM_HAS_BUTTON
|
|
|
|
PROCESS_THREAD(ipso_button_process, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
2017-11-05 22:38:45 +00:00
|
|
|
#if !PLATFORM_SUPPORTS_BUTTON_HAL
|
2017-10-25 00:27:14 +00:00
|
|
|
SENSORS_ACTIVATE(IPSO_BUTTON_SENSOR);
|
2017-11-05 22:38:45 +00:00
|
|
|
#endif
|
2015-12-01 03:14:46 +00:00
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT();
|
|
|
|
|
2017-11-05 22:38:45 +00:00
|
|
|
#if PLATFORM_SUPPORTS_BUTTON_HAL
|
2018-03-17 22:38:14 +00:00
|
|
|
if(ev == button_hal_press_event) {
|
|
|
|
input_state = 1;
|
|
|
|
counter++;
|
|
|
|
if((edge_selection & 2) != 0) {
|
|
|
|
lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE);
|
|
|
|
}
|
2017-11-05 22:38:45 +00:00
|
|
|
lwm2m_notify_object_observers(®_object, IPSO_INPUT_COUNTER);
|
2018-03-17 22:38:14 +00:00
|
|
|
} else if(ev == button_hal_release_event) {
|
|
|
|
input_state = 0;
|
|
|
|
if((edge_selection & 1) != 0) {
|
|
|
|
lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE);
|
|
|
|
}
|
2017-11-05 22:38:45 +00:00
|
|
|
}
|
2018-03-17 22:38:14 +00:00
|
|
|
#else /* PLATFORM_SUPPORTS_BUTTON_HAL */
|
2017-10-25 00:27:14 +00:00
|
|
|
if(ev == sensors_event && data == &IPSO_BUTTON_SENSOR) {
|
2015-12-01 03:14:46 +00:00
|
|
|
if(!input_state) {
|
2017-11-05 22:38:45 +00:00
|
|
|
int32_t time;
|
2015-12-01 03:14:46 +00:00
|
|
|
input_state = 1;
|
|
|
|
counter++;
|
|
|
|
if((edge_selection & 2) != 0) {
|
2017-10-25 00:27:14 +00:00
|
|
|
lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE);
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
2017-10-25 00:27:14 +00:00
|
|
|
lwm2m_notify_object_observers(®_object, IPSO_INPUT_COUNTER);
|
2015-12-01 03:14:46 +00:00
|
|
|
|
|
|
|
time = (debounce_time * CLOCK_SECOND / 1000);
|
|
|
|
if(time < 1) {
|
|
|
|
time = 1;
|
|
|
|
}
|
|
|
|
etimer_set(&timer, (clock_time_t)time);
|
|
|
|
}
|
|
|
|
} else if(ev == PROCESS_EVENT_TIMER && data == &timer) {
|
|
|
|
if(!input_state) {
|
|
|
|
/* Button is not in pressed state */
|
2017-10-25 00:27:14 +00:00
|
|
|
} else if(IPSO_BUTTON_SENSOR.value(0) != 0) {
|
2015-12-01 03:14:46 +00:00
|
|
|
/* Button is still pressed */
|
|
|
|
etimer_reset(&timer);
|
|
|
|
} else {
|
|
|
|
input_state = 0;
|
|
|
|
if((edge_selection & 1) != 0) {
|
2017-10-25 00:27:14 +00:00
|
|
|
lwm2m_notify_object_observers(®_object, IPSO_INPUT_STATE);
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-17 22:38:14 +00:00
|
|
|
#endif /* PLATFORM_SUPPORTS_BUTTON_HAL */
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
#endif /* PLATFORM_HAS_BUTTON */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|