common.c 9.77 KB
Newer Older
1
/*******************************************************************************
nikaeinn's avatar
nikaeinn committed
2 3
    OpenAirInterface
    Copyright(c) 1999 - 2014 Eurecom
4

nikaeinn's avatar
nikaeinn committed
5 6 7 8
    OpenAirInterface is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.
9 10


nikaeinn's avatar
nikaeinn committed
11 12 13 14
    OpenAirInterface is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.
15

nikaeinn's avatar
nikaeinn committed
16 17 18 19
    You should have received a copy of the GNU General Public License
    along with OpenAirInterface.The full GNU General Public License is
    included in this distribution in the file called "COPYING". If not,
    see <http://www.gnu.org/licenses/>.
20 21

  Contact Information
nikaeinn's avatar
nikaeinn committed
22 23 24 25 26
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49

*******************************************************************************/

/*! \file OMG.c
* \brief Main function containing the OMG interface
* \author  M. Mahersi,  J. Harri, N. Nikaein,
* \date 2011
* \version 0.1
* \company Eurecom
* \email: 
* \note
* \warning
*/

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "omg.h"

#define frand() ((double) rand() / (RAND_MAX+1))
//#ifndef STANDALONE
50 51 52 53 54 55
mapping nodes_type[] = {
  {"eNB", eNB}
  ,
  {"UE", UE}
  ,
  {NULL, -1}
56 57
};

58 59 60 61 62 63
mapping nodes_status[] = {
  {"sleeping", 0}
  ,
  {"moving", 1}
  ,
  {NULL, -1}
64 65
};

66 67 68 69 70 71 72 73 74 75 76 77
mapping mob_type[] = {
  {"STATIC", STATIC}
  ,
  {"RWP", RWP}
  ,
  {"RWALK", RWALK}
  ,
  {"TRACE", TRACE}
  ,
  {"SUMO", SUMO}
  ,
  {NULL, -1}
78
};
79

80
//#endif
81 82 83 84 85 86 87

node_struct *
create_node (void)
{
  node_struct *ptr;
  ptr = calloc (1, sizeof (node_struct));
  return ptr;
88 89
}

90 91 92 93 94

void
delete_node (node_struct * node)
{
  free (node->mob);
95
  node->mob = NULL;
96
  free (node);
97 98
}

99 100 101
double
randomgen (double a, double b)
{
102

103
  return (rand () / (double) RAND_MAX) * (b - a) + a;
104 105 106
}


107 108 109 110 111 112
mobility_struct *
create_mobility (void)
{
  mobility_struct *ptr;
  ptr = calloc (1, sizeof (mobility_struct));
  return ptr;
113 114 115
}


116 117 118 119 120 121 122 123 124
node_list *
add_entry (node_struct * node, node_list * node_vector_end)
{
  node_list *entry = malloc (sizeof (struct node_list_struct));
  entry->node = node;
  entry->next = NULL;
  if (node_vector_end == NULL)
    {
      return entry;
125
    }
126 127 128 129
  else
    {
      node_list *tmp = node_vector_end;
      tmp->next = entry;
130
    }
131
  return entry;
132 133
}

134 135 136 137 138 139 140 141 142

node_list *
remove_node_entry (node_struct * node, node_list * node_vector)
{
  node_list *list = node_vector;
  node_list *tmp, *toremove;
  if (list == NULL)
    {
      return NULL;
143
    }
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
  if (list->node->id == node->id)
    {
      // TODO delete the entry
      toremove = list;
      LOG_D (OMG, "removed entry for node %d \n", list->node->id);
      if (list->next == NULL)
	{
	  node_vector = NULL;
	  return NULL;
	}
      else
	{
	  node_vector = list->next;
	}
    }
  else
    {
      while (list->next != NULL)
	{
	  tmp = list;
	  if (list->next->node->id == node->id)
	    {
	      toremove = tmp;	// TODO delete the entry
	      tmp = list->next->next;
	      if (tmp != NULL)
		{
		  list->next = tmp;
		}
	      else
		{
		  list->next = NULL;
		}
	    }
	}
178
    }
179
  return node_vector;
180 181 182 183
}


// display list of nodes
184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
void
display_node_list (node_list * node_vector)
{
  node_list *tmp = node_vector;
  if (tmp == NULL)
    {
#ifdef STANDALONE
      printf ("Empty Node_list\n");
#else
      LOG_I (OMG, "Empty Node_list\n");
#endif
    }
  while (tmp != NULL)
    {
      /*LOG_I(OMG,"[%s][%s] Node of ID %d is %s. Now, it is at location (%.3f, %.3f)\n", 
         map_int_to_str(mob_type, tmp->node->generator),
         map_int_to_str(nodes_type, tmp->node->type),  
         tmp->node->ID,
         map_int_to_str(nodes_status, tmp->node->mobile), 
         tmp->node->X_pos,
         tmp->node->Y_pos ); */
      LOG_I (OMG, "[%s %d][%s] \t at (%.3f, %.3f)\n",
	     		map_int_to_str(nodes_type, tmp->node->type),  
          tmp->node->id,
					map_int_to_str(mob_type, tmp->node->generator),
          tmp->node->x_pos, tmp->node->y_pos);
			
				// two options: view node mobility of one node during the entire simulation or just a snapshot of all nodes for different timestamps
		// note:gnu plot requiredifferent files for each timestamp
			//use a python script to postprocess the positions, check the
	
// support: view node mobility of one node during the entire simulation OR only one snapshot
		LOG_F(OMG,"%d; %.3f; %.3f; %.3f\n",
					tmp->node->id,
		      tmp->node->x_pos, tmp->node->y_pos);

      //LOG_I(OMG, "node number %d\tstatus(fix/mobile) %d\tX_pos %.2f\tY_pos %.2f\tnode_type(eNB, UE)%d\t", tmp->node->ID,tmp->node->mobile, tmp->node->X_pos,tmp->node->Y_pos, tmp->node->type);
221 222 223 224 225
      //LOG_D(OMG, "mob->X_from %.3f\tmob->Y_from %.3f\tmob->X_to %.3f\tmob->Y_to %.3f\t", tmp->node->mob->X_from,tmp->node->mob->Y_from, tmp->node->mob->X_to, tmp->node->mob->Y_to );
      tmp = tmp->next;
    }
}

226 227 228 229 230 231 232 233 234
void
display_node_position (int id, int generator, int type, int mobile, double x,
		       double y)
{
  LOG_I (OMG,
	 "[%s][%s] Node of ID %d is %s. Now, it is at location (%.2f, %.2f) \n",
	 map_int_to_str (mob_type, generator), map_int_to_str (nodes_type,
							       type), id,
	 map_int_to_str (nodes_status, mobile), x, y);
235 236
}

237 238 239 240 241
node_list *
filter (node_list * vector, int node_type)
{
  node_list *tmp1, *tmp2;
  tmp1 = vector;
242
  tmp2 = NULL;
243 244 245 246 247 248 249
  while (tmp1 != NULL)
    {
      if (tmp1->node->type == node_type)
	{
	  tmp2 = add_entry (tmp1->node, tmp2);
	}
      tmp1 = tmp1->next;
250 251 252 253
    }
  return tmp2;
}

254 255 256 257 258 259 260 261

void
delete_node_entry (node_list * entry)
{
  node_struct *node = entry->node;
  delete_node (node);
  entry->node = NULL;
  free (entry);
262 263
}

264 265 266 267
node_list *
remove_node (node_list * list, int nid, int node_type)
{

268
  int found;
269
  node_list *current, *previous;
270 271
  //int cond=0;
  //int i=0;
272 273 274
  if (list == NULL)
    {
      found = 1;		//false
275 276
      return NULL;
    }
277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
  else
    {				//start search
      current = list;
      while ((current != NULL)
	     && ((current->node->id != nid)
		 || (current->node->type != node_type)))
	{
	  previous = current;	//  previous hobbles after
	  current = current->next;
	}
      //holds: current = NULL or  type != node_type or.., but not both
      if (current == NULL)
	{
	  found = 1;
	  LOG_E (OMG, " Element to remove is not found\n ");
	  return NULL;
	}			//value not found
      else
	{
	  found = 0;		// true                value found
	  if (current == list)
	    {
	      list = current->next;
	      LOG_D (OMG, "Element to remove is found at beginning\n");
	    }

	  else
	    {
	      previous->next = current->next;

	    }
	  delete_node_entry (current);	// freeing memory
	  current = NULL;
310 311

	}
312 313
      return list;
    }
314 315
}

316 317 318 319 320 321 322 323 324 325 326 327 328 329 330
int
length (char *s)
{
  int count = 0;
  while (s[count] != '\0')
    {
      ++count;
    }
  return count;
}

node_struct *
find_node (node_list * list, int nid, int node_type)
{

331
  int found;
332 333 334 335 336 337
  node_list *current;

  if (list == NULL)
    {
      printf (" Node_LIST for nodeID %d is NULL \n ", nid);
      return NULL;
338
    }
339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
  else
    {				//start search
      current = list;
      while ((current != NULL)
	     && ((current->node->id != nid)
		 || (current->node->type != node_type)))
	{
	  current = current->next;
	}
      //holds: current = NULL or  type != node_type or.., but not both
      if (current == NULL)
	{
	  found = 1;
	  LOG_D (OMG,
		 " Element to find in Node_Vector with ID: %d could not be found\n ",
		 nid);
	  return NULL;
	}			//value not found
      else
	{
	  //printf(" found a node for nodeID %d  \n ",nID);
	  return current->node;
	}
362 363 364 365
      return NULL;
    }
}

366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
node_list *
clear_node_list (node_list * list)
{
  node_list *tmp;

  if (list == NULL)
    {
      return NULL;
    }
  else
    {
      while (list->next != NULL)
	{
	  tmp = list;
	  list = list->next;
	  delete_node_entry (tmp);
	}
      delete_node_entry (list);	// clearing the last one
    }
385 386 387 388
  return NULL;
}

// TODO rewrite this part...not working correctly
389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
node_list *
reset_node_list (node_list * list)
{
  node_list *tmp;
  node_list *last = list;
  if (list == NULL)
    {
      //printf("Node_list is NULL\n");
      return NULL;
    }
  else
    {
      while (list->next != NULL)
	{
	  tmp = list;
	  list = list->next;
	  tmp->node = NULL;
	  //free(tmp);
	}
      list->node = NULL;	// clearing the last one | JHNOTE: dangerous here: the pointer is not NULL, but node is...that leads to segfault...
      //free(last);
    }
411 412 413 414
  return list;
}

// TODO rewrite this part...not working correctly
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
string_list *
clear_string_list (string_list * list)
{
  string_list *tmp;

  if (list == NULL)
    {
      return NULL;
    }
  else
    {
      while (list->next != NULL)
	{
	  tmp = list;
	  list = list->next;
	  free (tmp->string);
	  tmp->string = NULL;
	  free (tmp);
	}
      list->string = NULL;	// clearing the last one | JHNOTE: dangerous here: the pointer is not NULL, but node is...that leads to segfault...
    }
436 437 438 439 440 441 442 443 444
  return list;
}

#ifdef STANDALONE
/*
 * for the two functions below, the passed array must have a final entry
 * with string value NULL
 */
/* map a string to an int. Takes a mapping array and a string as arg */
445 446 447 448 449 450 451 452 453 454 455 456 457 458
int
map_str_to_int (mapping * map, const char *str)
{
  while (1)
    {
      if (map->name == NULL)
	{
	  return (-1);
	}
      if (!strcmp (map->name, str))
	{
	  return (map->value);
	}
      map++;
459 460 461 462
    }
}

/* map an int to a string. Takes a mapping array and a value */
463 464 465 466 467 468 469 470 471 472 473 474 475 476
char *
map_int_to_str (mapping * map, int val)
{
  while (1)
    {
      if (map->name == NULL)
	{
	  return NULL;
	}
      if (map->value == val)
	{
	  return map->name;
	}
      map++;
477 478 479 480 481 482
    }
}




483 484

#endif