Contiki-NG
Loading...
Searching...
No Matches
ipso-leds-control.c
Go to the documentation of this file.
1/*
2 * Copyright (c) 2015, Yanzi Networks AB.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of the copyright holder nor the names of its
14 * contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDER AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
20 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
21 * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
28 * OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31/**
32 * \addtogroup ipso-objects
33 * @{
34 *
35 */
36
37/**
38 * \file
39 * Implementation of OMA LWM2M / IPSO Light Control for LEDs
40 * \author
41 * Joakim Eriksson <joakime@sics.se>
42 * Niclas Finne <nfi@sics.se>
43 */
44
45#include "lwm2m-object.h"
46#include "lwm2m-engine.h"
48#include "dev/leds.h"
49#include <stdint.h>
50
51#define DEBUG 0
52#if DEBUG
53#include <stdio.h>
54#define PRINTF(...) printf(__VA_ARGS__)
55#else
56#define PRINTF(...)
57#endif
58
59#if LEDS_LEGACY_API
60#if LEDS_ALL & LEDS_BLUE || LEDS_ALL & LEDS_RED || LEDS_ALL & LEDS_BLUE
61#define LEDS_CONTROL_NUMBER (((LEDS_ALL & LEDS_BLUE) ? 1 : 0) + ((LEDS_ALL & LEDS_RED) ? 1 : 0) + ((LEDS_ALL & LEDS_GREEN) ? 1 : 0))
62#else
63#define LEDS_CONTROL_NUMBER 1
64#endif
65#else /* LEDS_LEGACY_API */
66#define LEDS_CONTROL_NUMBER LEDS_COUNT
67#endif /* LEDS_LEGACY_API */
68
69typedef struct led_state {
70 ipso_control_t control;
71 uint8_t led_value;
72} led_state_t;
73
74static led_state_t leds_controls[LEDS_CONTROL_NUMBER];
75/*---------------------------------------------------------------------------*/
76static lwm2m_status_t
77set_value(ipso_control_t *control, uint8_t value)
78{
79#if PLATFORM_HAS_LEDS || LEDS_COUNT
80 led_state_t *state;
81
82 state = (led_state_t *)control;
83
84 if(value) {
85 leds_on(state->led_value);
86 } else {
87 leds_off(state->led_value);
88 }
89#endif /* PLATFORM_HAS_LEDS */
90
91 return LWM2M_STATUS_OK;
92}
93/*---------------------------------------------------------------------------*/
94static int
95bit_no(int bit)
96{
97 int i;
98 for(i = 0; i < 8; i++) {
99 if(LEDS_ALL & (1 << i)) {
100 if(bit == 0) {
101 /* matching bit */
102 return 1 << i;
103 } else {
104 /* matching but used */
105 bit--;
106 }
107 }
108 }
109 return 0;
110}
111/*---------------------------------------------------------------------------*/
112void
113ipso_leds_control_init(void)
114{
115 ipso_control_t *c;
116 int i;
117
118 /* Initialize the instances */
119 for(i = 0; i < LEDS_CONTROL_NUMBER; i++) {
120 c = &leds_controls[i].control;
121 c->reg_object.object_id = 3311;
122 c->reg_object.instance_id = i;
123 c->set_value = set_value;
124 leds_controls[i].led_value = bit_no(i);
125 ipso_control_add(c);
126 }
127
128 PRINTF("IPSO leds control initialized with %u instances\n",
129 LEDS_CONTROL_NUMBER);
130}
131/*---------------------------------------------------------------------------*/
132/** @} */
#define LEDS_ALL
The OR mask representation of all device LEDs.
Definition leds.h:195
Implementation of OMA LWM2M / IPSO sensor template.
Header file for the LED HAL.
Header file for the Contiki OMA LWM2M engine.
Header file for the LWM2M object API.