Contiki 2.5
nodes.c
1 /*
2  * Copyright (c) 2004, 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  * Author: Adam Dunkels <adam@sics.se>
32  *
33  * $Id: nodes.c,v 1.8 2008/05/14 19:22:58 adamdunkels Exp $
34  */
35 #include <signal.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 
40 #include "nodes.h"
41 
42 
43 static int numnodes;
44 
45 static struct nodes_node nodes[2000];
46 
47 int nodes_base_node_port = 0;
48 /*---------------------------------------------------------------------------*/
49 void
50 nodes_init(void)
51 {
52  numnodes = 0;
53 }
54 /*---------------------------------------------------------------------------*/
55 void
56 nodes_add(int pid, int x, int y, int port, int id)
57 {
58  nodes[numnodes].pid = pid;
59  nodes[numnodes].x = x;
60  nodes[numnodes].y = y;
61  nodes[numnodes].port = port;
62  nodes[numnodes].leds = 0;
63  nodes[numnodes].done = 0;
64  nodes[numnodes].id = id;
65  ++numnodes;
66 }
67 /*---------------------------------------------------------------------------*/
68 void
69 nodes_kill(void)
70 {
71  int i;
72  for(i = 0; i < numnodes; ++i) {
73  kill(nodes[i].pid, SIGTERM);
74  }
75 }
76 /*---------------------------------------------------------------------------*/
77 int
78 nodes_num(void)
79 {
80  return numnodes;
81 }
82 /*---------------------------------------------------------------------------*/
83 struct nodes_node *
84 nodes_node(int num)
85 {
86  if(num > numnodes) {
87  fprintf(stderr, "nodes_node: request for %d > %d\n", num, numnodes);
88  abort();
89  }
90  return &nodes[num];
91 }
92 /*---------------------------------------------------------------------------*/
93 static struct nodes_node *
94 find_node(int x, int y)
95 {
96  int i;
97 
98  for(i = numnodes; i >= 0; --i) {
99  if(nodes[i].x == x && nodes[i].y == y) {
100  return &nodes[i];
101  }
102  }
103  return &nodes[0];
104 }
105 /*---------------------------------------------------------------------------*/
106 void
107 nodes_set_leds(int x, int y, int leds)
108 {
109  find_node(x, y)->leds = leds;
110 }
111 /*---------------------------------------------------------------------------*/
112 void
113 nodes_set_text(int x, int y, char *text)
114 {
115  strncpy(find_node(x, y)->text, text, NODES_TEXTLEN);
116 }
117 /*---------------------------------------------------------------------------*/
118 void
119 nodes_set_radio_status(int x, int y, int radio_status)
120 {
121  find_node(x, y)->radio_status = radio_status;
122 }
123 /*---------------------------------------------------------------------------*/
124 void
125 nodes_set_line(int x, int y, int linex, int liney)
126 {
127  struct nodes_node *n;
128 
129  n = find_node(x, y);
130  n->linex = linex;
131  n->liney = liney;
132 }
133 /*---------------------------------------------------------------------------*/
134 struct nodes_node *
135 nodes_find_pid(pid_t pid)
136 {
137  int i;
138  printf("Nofodes %d\n", numnodes);
139  for(i = 0; i < numnodes; ++i) {
140  printf("%d == %d\n", pid, nodes[i].pid); fflush(NULL);
141  if(nodes[i].pid == pid) {
142  return &nodes[i];
143  }
144  }
145  return NULL;
146 }
147 /*---------------------------------------------------------------------------*/
148 void
149 nodes_done(int id)
150 {
151  int i;
152  int num_done = 0;
153 
154  for(i = numnodes; i >= 0; --i) {
155  if(nodes[i].id == id) {
156  nodes[i].done = 1;
157  }
158  if(nodes[i].done != 0) {
159  num_done++;
160  }
161  }
162 
163  if(num_done == numnodes) {
164  exit(0);
165  }
166 }
167 /*---------------------------------------------------------------------------*/