flexran_agent_common_internal.c 15.1 KB
Newer Older
1
2
3
4
5
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
Cedric Roux's avatar
Cedric Roux committed
6
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
7
8
9
10
11
12
13
14
15
16
17
18
19
20
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */ 
21

22
/*! \file flexran_agent_common_internal.c
23
 * \brief internal functions for common message primitves and utilities 
24
25
 * \author Xenofon Foukas and N. Nikaein
 * \date 2017
26
27
28
29
30
31
 * \version 0.1
 */

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

32
33
#include "flexran_agent_common_internal.h"
#include "flexran_agent_mac_internal.h"
34

35
36
37
38
/* the following is needed to soft-restart the lte-softmodem */
#include "targets/RT/USER/lte-softmodem.h"
#include "assertions.h"
#include "enb_app.h"
39

40
#if 0
41
42
43
44
void handle_reconfiguration(mid_t mod_id)
{
  /* NOTE: this function might be extended by using stop_modem()
   * to halt the modem so that it can later be resumed */
45
  int do_restart = 0;
46

47
  pthread_mutex_lock(&mutex_node_ctrl);
48
49
  if (ENB_NORMAL_OPERATION != node_control_state) {
    node_control_state = ENB_NORMAL_OPERATION;
50
    pthread_cond_broadcast(&cond_node_ctrl);
51
  } else {
52
53
54
55
56
57
58
    do_restart = 1;
  }
  pthread_mutex_unlock(&mutex_node_ctrl);

  if (do_restart) {
    clock_t start_ms = 1000 * clock();
    /* operator || enforces sequence points */
59
60
61
62
63
64
65
    if (stop_L1L2(mod_id) < 0 || restart_L1L2(mod_id) < 0) {
      LOG_E(ENB_APP, "could not restart, killing lte-softmodem\n");
      /* shutdown the whole lte-softmodem */
      itti_terminate_tasks(TASK_PHY_ENB);
      return;
    }
    enb_app_start_phy_rrc(mod_id, mod_id+1);
66
67
68
    MessageDef *msg_p = itti_alloc_new_message(TASK_ENB_APP, INITIALIZE_MESSAGE);
    itti_send_msg_to_task(TASK_L2L1, INSTANCE_DEFAULT, msg_p);

69
70
    int diff_ms = (1000 * clock() - start_ms) / CLOCKS_PER_SEC;
    LOG_I(ENB_APP, "lte-softmodem restart succeeded in %d ms\n", diff_ms);
71
72
  }
}
73
#endif
74

75
76
77
78
79
80
81
82
83
84
85
int apply_reconfiguration_policy(mid_t mod_id, const char *policy, size_t policy_length) {

  yaml_parser_t parser;
  yaml_event_t event;

  int done = 0;

  LOG_I(ENB_APP, "Time to apply a new policy \n");

  yaml_parser_initialize(&parser);

86
  yaml_parser_set_input_string(&parser, (unsigned char *) policy, strlen(policy));
87
88
89
90
91
92
93

  while (!done) {
    if (!yaml_parser_parse(&parser, &event))
      goto error;
 
    switch (event.type) {
    case YAML_STREAM_START_EVENT:
94
      break;
95
    case YAML_STREAM_END_EVENT:
96
      break;
97
    case YAML_DOCUMENT_START_EVENT:
98
      break;
99
100
101
102
103
104
105
106
    case YAML_DOCUMENT_END_EVENT:
      break;
    case YAML_MAPPING_START_EVENT:
      break;
    case YAML_MAPPING_END_EVENT:
      break;
    case YAML_SCALAR_EVENT:
      // Check the system name and call the proper handler
107
108
109
110
111
112
113
      // Check the system name and call the proper handler
      if (strcmp((char *) event.data.scalar.value, "enb") == 0) {
	LOG_I(ENB_APP, "This is intended for the enb system\n");
	// Call the enb handler
	if (parse_enb_id(mod_id, &parser) == -1) {
	  goto error;
	} else { // succeful parse and setting 
114
115
          /* TODO implement */
          //handle_reconfiguration(mod_id);
116
117
	}
      } else if (strcmp((char *) event.data.scalar.value, "mac") == 0) {
118
119
120
121
122
	LOG_D(ENB_APP, "This is intended for the mac system\n");
	// Call the mac handler
	if (parse_mac_config(mod_id, &parser) == -1) {
	  goto error;
	}
123
      } else if (strcmp((char *) event.data.scalar.value, "rlc") == 0) {
124
125
126
127
128
129
	// Call the RLC handler
	LOG_D(ENB_APP, "This is intended for the rlc system\n");
	// TODO : Just skip it for now
	if (skip_system_section(&parser) == -1) {
	  goto error;
	}
130
      } else if (strcmp((char *) event.data.scalar.value, "pdcp") == 0) {
131
132
133
134
135
136
	// Call the PDCP handler
	LOG_D(ENB_APP, "This is intended for the pdcp system\n");
	// TODO : Just skip it for now
	if (skip_system_section(&parser) == -1) {
	  goto error;
	}
137
      } else if (strcmp((char *) event.data.scalar.value, "rrc") == 0) {
138
139
140
141
142
	// Call the RRC handler
	LOG_D(ENB_APP, "This is intended for the rrc system\n");
	// TODO : Just skip it for now
	if (skip_system_section(&parser) == -1) {
	  goto error;
143
144
	} 
      } else { 
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
	goto error;
      }
      break;
    default:
      // We are not expecting to find any other type of event at this level
      // of the hierarchy
      yaml_event_delete(&event);
      goto error;
    }
    
    done = (event.type == YAML_STREAM_END_EVENT);

    yaml_event_delete(&event);
  }

  return 0;

 error:
  yaml_parser_delete(&parser);
  return -1;

}

168
169
170
171
int parse_enb_id(mid_t mod_id, yaml_parser_t *parser) {
  yaml_event_t event;
  
  char *endptr;
shahab's avatar
shahab committed
172
  // int is_array;
173
174
175
176
177
178
179
180
181
182
183
184
  
  int done = 0;
  int mapping_started = 0;

  while (!done) {
    
    if (!yaml_parser_parse(parser, &event))
      goto error;

    switch (event.type) {
      // We are expecting a mapping of parameters
    case YAML_SEQUENCE_START_EVENT:
shahab's avatar
shahab committed
185
      // is_array = 1;
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
      break;
    case YAML_MAPPING_START_EVENT:
      LOG_D(ENB_APP, "The mapping of the parameters started\n");
      mapping_started = 1;
      break;
    case YAML_MAPPING_END_EVENT:
      LOG_D(ENB_APP, "The mapping of the parameters ended\n");
      mapping_started = 0;
      break;
    case YAML_SCALAR_EVENT:
      if (!mapping_started) {
	goto error;
      }
      // Check what key needs to be set
      // use eNB egistered
      if (mac_agent_registered[mod_id]) {
	LOG_I(ENB_APP, "Setting parameter for eNB %s\n", event.data.scalar.value);
	if (strcmp((char *) event.data.scalar.tag, YAML_INT_TAG) == 0) { // if int 
	  if ((strtol((char *) event.data.scalar.value, &endptr, 10))== mod_id ) { // enb_id == mod_id: right enb instance to be configured
	    if (parse_enb_config_parameters(mod_id, parser) == -1) {
	      goto error;
	    } 
	  }
	  else{
	    goto error; // not the expected type
	  }
	}
      }
      break;
    default:
      goto error;
    }

    done = (event.type == YAML_MAPPING_END_EVENT);
    yaml_event_delete(&event);
  }

  return 0;
  
 error:
  yaml_event_delete(&event);
  return -1;
}

int parse_enb_config_parameters(mid_t mod_id, yaml_parser_t *parser) {
  yaml_event_t event;
  
  char *endptr;
  
  int done = 0;
  int mapping_started = 0;

  while (!done) {
    
    if (!yaml_parser_parse(parser, &event))
      goto error;

    switch (event.type) {
      // We are expecting a mapping of parameters
    case YAML_MAPPING_START_EVENT:
      LOG_D(ENB_APP, "The mapping of the parameters started\n");
      mapping_started = 1;
      break;
    case YAML_MAPPING_END_EVENT:
      LOG_D(ENB_APP, "The mapping of the parameters ended\n");
      mapping_started = 0;
      break;
    case YAML_SCALAR_EVENT:
      if (!mapping_started) {
	goto error;
      }
      // Check what key needs to be set
      if (strcmp((char *) event.data.scalar.value, "dl_freq") == 0) {
259
260
        if (!yaml_parser_parse(parser, &event))
          goto error;
261
262
263
	flexran_agent_set_operating_dl_freq(mod_id,
					    0,
					    strtol((char *) event.data.scalar.value, &endptr, 10));
264
        LOG_I(ENB_APP, "Setting dl_freq to %s\n", event.data.scalar.value);
265
      } else if (strcmp((char *) event.data.scalar.value, "ul_freq_offset") == 0) {
266
267
        if (!yaml_parser_parse(parser, &event))
          goto error;
268
269
270
	flexran_agent_set_operating_ul_freq(mod_id,
					    0,
					    strtol((char *) event.data.scalar.value, &endptr, 10));
271
        LOG_I(ENB_APP, "Setting ul_freq_offset to %s\n", event.data.scalar.value);
272
      } else if (strcmp((char *) event.data.scalar.value, "bandwidth") == 0) {
273
274
        if (!yaml_parser_parse(parser, &event))
          goto error;
275
276
277
	flexran_agent_set_operating_bandwidth(mod_id,
					    0,
					    strtol((char *) event.data.scalar.value, &endptr, 10));
278
        LOG_I(ENB_APP, "Setting bandwidth to %s\n", event.data.scalar.value);
279
      } else if (strcmp((char *) event.data.scalar.value, "frame_type") == 0) {
280
281
        if (!yaml_parser_parse(parser, &event))
          goto error;
282
283
284
	flexran_agent_set_operating_frame_type (mod_id,
					    0,
					    strtol((char *) event.data.scalar.value, &endptr, 10));
285
        LOG_I(ENB_APP, "Setting frame_type to %s\n", event.data.scalar.value);
286
287
288
289
290
291
292
293
294
295
296
297
298
      }else { // not supported tag  
	goto error;
      }
      
      break;
    default:
      goto error;
    }

    done = (event.type == YAML_MAPPING_END_EVENT);
    yaml_event_delete(&event);
  }

299
  /* TODO: reflect in RAN API */
300

301
302
303
304
305
306
307
  return 0;
  
 error:
  yaml_event_delete(&event);
  return -1;
}

308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
int skip_system_section(yaml_parser_t *parser) {
  yaml_event_t event;
  
  int done = 0;

  int sequence_started = 0;
  int mapping_started = 0;

  while (!done) {
    if (!yaml_parser_parse(parser, &event))
      goto error;
   
    switch (event.type) {
      
      case YAML_SEQUENCE_START_EVENT:
      LOG_D(ENB_APP, "A sequence just started as expected\n");
      sequence_started = 1;
      break;
    case YAML_SEQUENCE_END_EVENT:
      LOG_D(ENB_APP, "A sequence ended\n");
      sequence_started = 0;
      break;
    case YAML_MAPPING_START_EVENT:
      if (!sequence_started) {
	goto error;
      }
      LOG_D(ENB_APP, "A mapping started\n");
      mapping_started = 1;
      break;
    case YAML_MAPPING_END_EVENT:
      if (!mapping_started) {
	goto error;
      }
      LOG_D(ENB_APP, "A mapping ended\n");
      mapping_started = 0;
      break;
    case YAML_SCALAR_EVENT:
      if (!mapping_started) {
	goto error;
      }
      // Entered a subsystem section. Let's skip it
      if (skip_subsystem_section(parser) == -1) {
	goto error;
      }
352
353
    default:
      break;
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
    }
    done = (event.type == YAML_SEQUENCE_END_EVENT);

    yaml_event_delete(&event);
    
  }

  return 0;

 error:
  yaml_event_delete(&event);
  return -1;
}

int skip_subsystem_section(yaml_parser_t *parser) {
  
  yaml_event_t event;

  int done = 0;
  int mapping_started = 0;

  while (!done) {
    if (!yaml_parser_parse(parser, &event))
      goto error;

    switch (event.type) {
    case YAML_MAPPING_START_EVENT:
      LOG_D(ENB_APP, "The mapping of the subsystem started\n");
      mapping_started = 1;
      break;
    case YAML_MAPPING_END_EVENT:
      LOG_D(ENB_APP, "The mapping of the subsystem ended\n");
      mapping_started = 0;
      break;
    case YAML_SCALAR_EVENT:
      if (!mapping_started) {
	goto error;
      }
      // Check what key needs to be set
393
      if (strcmp((char *) event.data.scalar.value, "behavior") == 0) {
394
395
396
397
398
399
400
401
402
403
	LOG_D(ENB_APP, "Skipping the behavior attribute\n");
	yaml_event_delete(&event);
	if (!yaml_parser_parse(parser, &event)) {
	  goto error;
	}
	if (event.type == YAML_SCALAR_EVENT) {
	  break;
	} else {
	  goto error;
	}
404
      } else if (strcmp((char *) event.data.scalar.value, "parameters") == 0) {
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
	LOG_D(ENB_APP, "Skipping the parameters for this subsystem\n");
	if (skip_subsystem_parameters_config(parser) == -1) {
	  goto error;
	}
      }
      break;
    default:
      goto error;
    }
    
    done = (event.type == YAML_MAPPING_END_EVENT);
    yaml_event_delete(&event);
  }

  return 0;

 error:
  yaml_event_delete(&event);
  return -1;
}

int skip_subsystem_parameters_config(yaml_parser_t *parser) {
  yaml_event_t event;
  
  int done = 0;
  int mapping_started = 0;

  while (!done) {
    
    if (!yaml_parser_parse(parser, &event))
      goto error;

    switch (event.type) {
      // We are expecting a mapping of parameters
    case YAML_MAPPING_START_EVENT:
      LOG_D(ENB_APP, "The mapping of the parameters started\n");
      mapping_started = 1;
      break;
    case YAML_MAPPING_END_EVENT:
      LOG_D(ENB_APP, "The mapping of the parameters ended\n");
      mapping_started = 0;
      break;
    case YAML_SCALAR_EVENT:
      if (!mapping_started) {
	goto error;
      }
      // Check what key needs to be set
      LOG_D(ENB_APP, "Skipping parameter %s\n", event.data.scalar.value);
      if (skip_parameter_modification(parser) == -1 ) {
	goto error;
      }
      break;
    default:
      goto error;
    }

    done = (event.type == YAML_MAPPING_END_EVENT);
    yaml_event_delete(&event);
  }

  return 0;
  
 error:
  yaml_event_delete(&event);
  return -1;
}


int skip_parameter_modification(yaml_parser_t *parser) {
  yaml_event_t event;
  
  int done = 0;
  int is_array = 0;
  int i = 0;

  while (!done) {
    if (!yaml_parser_parse(parser, &event)) {
      goto error;
    }
    
    // Expecting either a scalar or a sequence
    switch (event.type) {
    case YAML_SEQUENCE_START_EVENT:
      is_array = 1;
      break;
    case YAML_SCALAR_EVENT:
491
492
493
494
      if ((strcmp((char *) event.data.scalar.tag, YAML_INT_TAG) == 0) ||
	  (strcmp((char *) event.data.scalar.tag, YAML_FLOAT_TAG) == 0) ||
	  (strcmp((char *) event.data.scalar.tag, YAML_STR_TAG) == 0) ||
	  (strcmp((char *) event.data.scalar.tag, YAML_BOOL_TAG) == 0)) {
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
	// Do nothing
      } else {
	// No other type is supported at the moment, so it should be considered an error
	// if we reach here
	goto error;
      }
      if (is_array) {
	i++;
      } else {
	done = 1;
      }
      break;
    case YAML_SEQUENCE_END_EVENT:
      done = 1;
      break;
    default:
      goto error;
    }
    yaml_event_delete(&event);
  }

  return 0;

 error:
  yaml_event_delete(&event);
  return -1;
}

int apply_parameter_modification(void *parameter, yaml_parser_t *parser) {
  yaml_event_t event;
  
  int done = 0;
  int is_array = 0;
  int i = 0;

  char *endptr;

  while (!done) {
    if (!yaml_parser_parse(parser, &event)) {
      goto error;
    }
    
    // Expecting either a scalar or a sequence
    switch (event.type) {
    case YAML_SEQUENCE_START_EVENT:
      is_array = 1;
      break;
    case YAML_SCALAR_EVENT:
543
544
545
546
547
548
549
550
      if (strcmp((char *) event.data.scalar.tag, YAML_INT_TAG) == 0) {
	((int *) parameter)[i] = strtol((char *) event.data.scalar.value, &endptr, 10);
      } else if (strcmp((char *) event.data.scalar.tag, YAML_FLOAT_TAG) == 0) {
	((float *) parameter)[i] = strtof((char *) event.data.scalar.value, &endptr);
      } else if (strcmp((char *) event.data.scalar.tag, YAML_STR_TAG) == 0) {
	strncpy(&((char *) parameter)[i], (char *) event.data.scalar.value, event.data.scalar.length);
      } else if (strcmp((char *) event.data.scalar.tag, YAML_BOOL_TAG) == 0) {
	if (strcmp((char *) event.data.scalar.value, "true") == 0) {
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
	  ((int *) parameter)[i] = 1;
	} else {
	  ((int *) parameter)[i] = 0;
	}
      } else {
	// No other type is supported at the moment, so it should be considered an error
	// if we reach here
	goto error;
      }
      if (is_array) {
	i++;
      } else {
	done = 1;
      }
      break;
    case YAML_SEQUENCE_END_EVENT:
      done = 1;
      break;
    default:
      goto error;
    }
    yaml_event_delete(&event);
  }

  return 0;

 error:
  yaml_event_delete(&event);
  return -1;
  
}