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