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 Light Control for LEDs
|
|
|
|
* \author
|
|
|
|
* Joakim Eriksson <joakime@sics.se>
|
|
|
|
* Niclas Finne <nfi@sics.se>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "lwm2m-object.h"
|
|
|
|
#include "lwm2m-engine.h"
|
2017-10-26 21:22:44 +00:00
|
|
|
#include "ipso-control-template.h"
|
2015-12-01 03:14:46 +00:00
|
|
|
#include "dev/leds.h"
|
|
|
|
#include <stdint.h>
|
|
|
|
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#include <stdio.h>
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
|
2018-02-25 21:40:19 +00:00
|
|
|
#if LEDS_LEGACY_API
|
2015-12-21 12:02:34 +00:00
|
|
|
#if LEDS_ALL & LEDS_BLUE || LEDS_ALL & LEDS_RED || LEDS_ALL & LEDS_BLUE
|
|
|
|
#define LEDS_CONTROL_NUMBER (((LEDS_ALL & LEDS_BLUE) ? 1 : 0) + ((LEDS_ALL & LEDS_RED) ? 1 : 0) + ((LEDS_ALL & LEDS_GREEN) ? 1 : 0))
|
2015-12-01 03:14:46 +00:00
|
|
|
#else
|
|
|
|
#define LEDS_CONTROL_NUMBER 1
|
|
|
|
#endif
|
2018-02-25 21:40:19 +00:00
|
|
|
#else /* LEDS_LEGACY_API */
|
|
|
|
#define LEDS_CONTROL_NUMBER LEDS_COUNT
|
|
|
|
#endif /* LEDS_LEGACY_API */
|
2015-12-01 03:14:46 +00:00
|
|
|
|
2017-10-26 21:22:44 +00:00
|
|
|
typedef struct led_state {
|
|
|
|
ipso_control_t control;
|
2015-12-21 12:02:34 +00:00
|
|
|
uint8_t led_value;
|
2017-10-26 21:22:44 +00:00
|
|
|
} led_state_t;
|
2015-12-01 03:14:46 +00:00
|
|
|
|
2017-10-26 21:22:44 +00:00
|
|
|
static led_state_t leds_controls[LEDS_CONTROL_NUMBER];
|
2015-12-01 03:14:46 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2017-10-26 21:22:44 +00:00
|
|
|
static lwm2m_status_t
|
|
|
|
set_value(ipso_control_t *control, uint8_t value)
|
2015-12-01 03:14:46 +00:00
|
|
|
{
|
2018-02-25 21:40:19 +00:00
|
|
|
#if PLATFORM_HAS_LEDS || LEDS_COUNT
|
2017-10-26 21:22:44 +00:00
|
|
|
led_state_t *state;
|
2015-12-01 03:14:46 +00:00
|
|
|
|
2017-10-26 21:22:44 +00:00
|
|
|
state = (led_state_t *)control;
|
2015-12-01 03:14:46 +00:00
|
|
|
|
2017-10-26 21:22:44 +00:00
|
|
|
if(value) {
|
|
|
|
leds_on(state->led_value);
|
2015-12-01 03:14:46 +00:00
|
|
|
} else {
|
2017-10-26 21:22:44 +00:00
|
|
|
leds_off(state->led_value);
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
2017-10-26 21:22:44 +00:00
|
|
|
#endif /* PLATFORM_HAS_LEDS */
|
2015-12-01 03:14:46 +00:00
|
|
|
|
2017-10-26 21:22:44 +00:00
|
|
|
return LWM2M_STATUS_OK;
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-12-21 12:02:34 +00:00
|
|
|
static int
|
|
|
|
bit_no(int bit)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < 8; i++) {
|
|
|
|
if(LEDS_ALL & (1 << i)) {
|
|
|
|
if(bit == 0) {
|
|
|
|
/* matching bit */
|
|
|
|
return 1 << i;
|
|
|
|
} else {
|
|
|
|
/* matching but used */
|
|
|
|
bit--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2017-10-26 21:22:44 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2015-12-01 03:14:46 +00:00
|
|
|
void
|
|
|
|
ipso_leds_control_init(void)
|
|
|
|
{
|
2017-10-26 21:22:44 +00:00
|
|
|
ipso_control_t *c;
|
2015-12-01 03:14:46 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Initialize the instances */
|
|
|
|
for(i = 0; i < LEDS_CONTROL_NUMBER; i++) {
|
2017-10-26 21:22:44 +00:00
|
|
|
c = &leds_controls[i].control;
|
|
|
|
c->reg_object.object_id = 3311;
|
|
|
|
c->reg_object.instance_id = i;
|
|
|
|
c->set_value = set_value;
|
|
|
|
leds_controls[i].led_value = bit_no(i);
|
|
|
|
ipso_control_add(c);
|
2015-12-01 03:14:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRINTF("IPSO leds control initialized with %u instances\n",
|
|
|
|
LEDS_CONTROL_NUMBER);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/** @} */
|