Contiki 2.5
registration.c
Go to the documentation of this file.
1 /**
2  * \addtogroup bprocess
3  */
4 
5 /**
6  * \file
7  *
8  * \author Georg von Zengen <vonzeng@ibr.cs.tu-bs.de>
9  * \author Wolf-Bastian Poettner <poettner@ibr.cs.tu-bs.de>
10  */
11 
12 #include <stdio.h>
13 #include <string.h>
14 
15 #include "lib/memb.h"
16 #include "lib/list.h"
17 #include "logging.h"
18 
19 #include "api.h"
20 #include "agent.h"
21 
22 #include "registration.h"
23 
24 /* Maximum number of registered services */
25 #ifdef CONF_MAX_REGISTRATIONS
26 #define MAX_REGISTRATIONS CONF_MAX_REGISTRATIONS
27 #else
28 #define MAX_REGISTRATIONS 5
29 #endif
30 
31 LIST(registration_list);
32 MEMB(registration_mem, struct registration, MAX_REGISTRATIONS);
33 
35 {
36  /* Initialize the lists */
37  memb_init(&registration_mem);
38  list_init(registration_list);
39 
40  /* Set the pointer for "outsiders" */
41  reg_list = registration_list;
42 
43  LOG(LOGD_DTN, LOG_AGENT, LOGL_INF, "Registration init ok");
44 }
45 
46 int registration_new_application(uint32_t app_id, struct process *application_process, uint32_t node_id)
47 {
48  struct registration * n = NULL;
49 
50  if( node_id == 0 ) {
51  node_id = dtn_node_id;
52  }
53 
54  LOG(LOGD_DTN, LOG_AGENT, LOGL_DBG, "Registering app_id %lu on node_id %lu", app_id, node_id);
55 
56  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
57  if (n->node_id == node_id && n->app_id == app_id){
58  return 0;
59  }
60  }
61 
62  if( n != NULL ) {
63  return -1;
64  }
65 
66  n = memb_alloc(&registration_mem);
67  if( n == NULL ) {
68  return -1;
69  }
70 
71  n->node_id = node_id;
72  n->app_id = app_id;
73  n->status = APP_ACTIVE;
74  n->application_process = application_process;
75 
76  list_add(registration_list, n);
77 
78  return 1;
79 }
80 
81 struct process * registration_get_process(uint32_t app_id, uint32_t node_id)
82 {
83  struct registration * n = NULL;
84 
85  if( node_id == 0 ) {
86  node_id = dtn_node_id;
87  }
88 
89  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
90  if(n->node_id == node_id && n->app_id == app_id) {
91  return n->application_process;
92  }
93  }
94 
95  return NULL;
96 }
97 
98 void registration_remove_application(uint32_t app_id, uint32_t node_id)
99 {
100  struct registration * n = NULL;
101 
102  if( node_id == 0 ) {
103  node_id = dtn_node_id;
104  }
105 
106  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
107  if(n->node_id == node_id && n->app_id == app_id) {
108  list_remove(registration_list, n);
109  memb_free(&registration_mem, n);
110  }
111  }
112 }
113 
114 int registration_set_active(uint32_t app_id, uint32_t node_id) {
115  struct registration * n = NULL;
116 
117  if( node_id == 0 ) {
118  node_id = dtn_node_id;
119  }
120 
121  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
122  if(n->node_id == node_id && n->app_id == app_id) {
123  n->status = APP_ACTIVE;
124  break;
125  }
126  }
127 
128  if(n == NULL) {
129  return -1;
130  }
131 
132  return n->status;
133 }
134 
135 int registration_set_passive(uint32_t app_id, uint32_t node_id)
136 {
137  struct registration * n = NULL;
138 
139  if( node_id == 0 ) {
140  node_id = dtn_node_id;
141  }
142 
143  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
144  if(n->node_id == node_id && n->app_id == app_id) {
145  n->status = APP_PASSIVE;
146  break;
147  }
148  }
149 
150  if(n == NULL) {
151  return -1;
152  }
153 
154  return n->status;
155 }
156 
157 int registration_return_status(uint32_t app_id, uint32_t node_id)
158 {
159  struct registration * n = NULL;
160 
161  if( node_id == 0 ) {
162  node_id = dtn_node_id;
163  }
164 
165  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
166  if(n->node_id == node_id && n->app_id == app_id) {
167  return n->status;
168  }
169  }
170 
171  return -1;
172 }
173 
174 uint32_t registration_get_application_id(struct process * application_process)
175 {
176  struct registration * n = NULL;
177 
178  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
179  if( n->application_process == application_process ) {
180  return n->app_id;
181  }
182  }
183 
184  return 0xFFFF;
185 }
186 
187 uint8_t registration_is_local(uint32_t app_id, uint32_t node_id)
188 {
189  struct registration * n = NULL;
190 
191  if( node_id == 0 ) {
192  node_id = dtn_node_id;
193  }
194 
195  for(n = list_head(registration_list); n != NULL; n = list_item_next(n)) {
196  if(n->node_id == node_id && n->app_id == app_id) {
197  return 1;
198  }
199  }
200 
201  return 0;
202 }
203 
204 /** @} */