Contiki-NG
sensors.c
1 /*
2  * Copyright (c) 2009, Swedish Institute of Computer Science
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 Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  */
32 
33 #include <string.h>
34 
35 #include "contiki.h"
36 
37 #include "lib/sensors.h"
38 
39 extern const struct sensors_sensor *sensors[];
40 extern unsigned char sensors_flags[];
41 
42 #define FLAG_CHANGED 0x80
43 
44 process_event_t sensors_event;
45 
46 static unsigned char num_sensors;
47 
48 PROCESS(sensors_process, "Sensors");
49 
50 /*---------------------------------------------------------------------------*/
51 static int
52 get_sensor_index(const struct sensors_sensor *s)
53 {
54  int i;
55  for(i = 0; i < num_sensors; ++i) {
56  if(sensors[i] == s) {
57  return i;
58  }
59  }
60  return i;
61 }
62 /*---------------------------------------------------------------------------*/
63 const struct sensors_sensor *
64 sensors_first(void)
65 {
66  return sensors[0];
67 }
68 /*---------------------------------------------------------------------------*/
69 const struct sensors_sensor *
70 sensors_next(const struct sensors_sensor *s)
71 {
72  return sensors[get_sensor_index(s) + 1];
73 }
74 /*---------------------------------------------------------------------------*/
75 void
76 sensors_changed(const struct sensors_sensor *s)
77 {
78  sensors_flags[get_sensor_index(s)] |= FLAG_CHANGED;
79  process_poll(&sensors_process);
80 }
81 /*---------------------------------------------------------------------------*/
82 const struct sensors_sensor *
83 sensors_find(const char *prefix)
84 {
85  int i;
86  unsigned short len;
87 
88  /* Search through all processes and search for the specified process
89  name. */
90  len = strlen(prefix);
91 
92  for(i = 0; i < num_sensors; ++i) {
93  if(strncmp(prefix, sensors[i]->type, len) == 0) {
94  return sensors[i];
95  }
96  }
97  return NULL;
98 }
99 /*---------------------------------------------------------------------------*/
100 PROCESS_THREAD(sensors_process, ev, data)
101 {
102  static int i;
103  static int events;
104 
105  PROCESS_BEGIN();
106 
107  sensors_event = process_alloc_event();
108 
109  for(i = 0; sensors[i] != NULL; ++i) {
110  sensors_flags[i] = 0;
111  sensors[i]->configure(SENSORS_HW_INIT, 0);
112  }
113  num_sensors = i;
114 
115  while(1) {
116 
118 
119  do {
120  events = 0;
121  for(i = 0; i < num_sensors; ++i) {
122  if(sensors_flags[i] & FLAG_CHANGED) {
123  if(process_post(PROCESS_BROADCAST, sensors_event, (void *)sensors[i]) == PROCESS_ERR_OK) {
124  PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event);
125  }
126  sensors_flags[i] &= ~FLAG_CHANGED;
127  events++;
128  }
129  }
130  } while(events);
131  }
132 
133  PROCESS_END();
134 }
135 /*---------------------------------------------------------------------------*/
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PROCESS_WAIT_EVENT_UNTIL(c)
Wait for an event to be posted to the process, with an extra condition.
Definition: process.h:157
#define PROCESS_ERR_OK
Return value indicating that an operation was successful.
Definition: process.h:74
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
process_event_t process_alloc_event(void)
Allocate a global event number.
Definition: process.c:93
int process_post(struct process *p, process_event_t ev, process_data_t data)
Post an asynchronous event.
Definition: process.c:322
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1110