Contiki 2.5
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  * @(#)$Id: sensors.c,v 1.5 2010/01/14 20:04:38 nifi Exp $
32  */
33 /* exeperimental code, will be renamed to sensors.c when done */
34 
35 
36 #include <string.h>
37 
38 #include "contiki.h"
39 
40 #include "lib/sensors.h"
41 
42 extern struct sensors_sensor *sensors[];
43 extern unsigned char sensors_flags[];
44 
45 #define FLAG_CHANGED 0x80
46 
47 process_event_t sensors_event;
48 
49 static unsigned char num_sensors;
50 
51 PROCESS(sensors_process, "Sensors");
52 
53 /*---------------------------------------------------------------------------*/
54 static int
55 get_sensor_index(const struct sensors_sensor *s)
56 {
57  int i;
58  for(i = 0; i < num_sensors; ++i) {
59  if(sensors[i] == s) {
60  return i;
61  }
62  }
63  return i;
64 }
65 /*---------------------------------------------------------------------------*/
66 struct sensors_sensor *
67 sensors_first(void)
68 {
69  return sensors[0];
70 }
71 /*---------------------------------------------------------------------------*/
72 struct sensors_sensor *
73 sensors_next(const struct sensors_sensor *s)
74 {
75  return sensors[get_sensor_index(s) + 1];
76 }
77 /*---------------------------------------------------------------------------*/
78 void
79 sensors_changed(const struct sensors_sensor *s)
80 {
81  sensors_flags[get_sensor_index(s)] |= FLAG_CHANGED;
82  process_poll(&sensors_process);
83 }
84 /*---------------------------------------------------------------------------*/
85 struct sensors_sensor *
86 sensors_find(const char *prefix)
87 {
88  int i;
89  unsigned short len;
90 
91  /* Search through all processes and search for the specified process
92  name. */
93  len = strlen(prefix);
94 
95  for(i = 0; i < num_sensors; ++i) {
96  if(strncmp(prefix, sensors[i]->type, len) == 0) {
97  return sensors[i];
98  }
99  }
100  return NULL;
101 }
102 /*---------------------------------------------------------------------------*/
103 PROCESS_THREAD(sensors_process, ev, data)
104 {
105  static int i;
106  static int events;
107 
108  PROCESS_BEGIN();
109 
110  sensors_event = process_alloc_event();
111 
112  for(i = 0; sensors[i] != NULL; ++i) {
113  sensors_flags[i] = 0;
114  sensors[i]->configure(SENSORS_HW_INIT, 0);
115  }
116  num_sensors = i;
117 
118  while(1) {
119 
121 
122  do {
123  events = 0;
124  for(i = 0; i < num_sensors; ++i) {
125  if(sensors_flags[i] & FLAG_CHANGED) {
126  if(process_post(PROCESS_BROADCAST, sensors_event, sensors[i]) == PROCESS_ERR_OK) {
127  PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event);
128  }
129  sensors_flags[i] &= ~FLAG_CHANGED;
130  events++;
131  }
132  }
133  } while(events);
134  }
135 
136  PROCESS_END();
137 }
138 /*---------------------------------------------------------------------------*/