flexran_agent_net_comm.c 5.92 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2016 Eurecom

    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.


    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.

    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/>.

  Contact Information
  OpenAirInterface Admin: openair_admin@eurecom.fr
  OpenAirInterface Tech : openair_tech@eurecom.fr
  OpenAirInterface Dev  : openair4g-devel@lists.eurecom.fr

  Address      : Eurecom, Compus SophiaTech 450, route des chappes, 06451 Biot, France.

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

30
31
/*! \file flexran_agent_net_comm.c
 * \brief FlexRAN agent network interface abstraction 
32
33
34
35
36
 * \author Xenofon Foukas
 * \date 2016
 * \version 0.1
 */

37
#include "flexran_agent_net_comm.h"
38
39
#include "log.h"

40
41
42
flexran_agent_channel_t *agent_channel[NUM_MAX_ENB][FLEXRAN_AGENT_MAX];
flexran_agent_channel_instance_t channel_instance;
int flexran_agent_channel_id = 0;
43

44
int flexran_agent_msg_send(mid_t mod_id, agent_id_t agent_id, void *data, int size, int priority) {
45
  /*Check if agent id is valid*/
46
  if (agent_id >= FLEXRAN_AGENT_MAX || agent_id < 0) {
47
48
    goto error;
  }
49
  flexran_agent_channel_t *channel;
50
  channel = agent_channel[mod_id][agent_id];
51
52
53
54
55
56
57
58
59
  
  /*Check if agent has a channel registered*/
  if (channel == NULL) {
    goto error;
  }

  return channel->msg_send(data, size, priority, channel->channel_info);
  
 error:
60
  LOG_E(FLEXRAN_AGENT, "No channel registered for agent with id %d\n", agent_id);
61
62
63
  return -1;
}

64
int flexran_agent_msg_recv(mid_t mod_id, agent_id_t agent_id, void **data, int *size, int *priority) {
65
  /*Check if agent id is valid*/
66
  if (agent_id >= FLEXRAN_AGENT_MAX || agent_id < 0) {
67
68
    goto error;
  }
69
  flexran_agent_channel_t *channel;
70
  channel = agent_channel[mod_id][agent_id];
71
72
73
74
75
76
77
78
79
  
  /*Check if agent has a channel registered*/
  if (channel == NULL) {
    goto error;
  }
  
  return channel->msg_recv(data, size, priority, channel->channel_info);
  
 error:
80
  LOG_E(FLEXRAN_AGENT, "No channel registered for agent with id %d\n", agent_id);
81
82
83
  return -1;
}

84
int flexran_agent_register_channel(mid_t mod_id, flexran_agent_channel_t *channel, agent_id_t agent_id) {
85
86
87
88
89
90
  int i;

  if (channel == NULL) {
    return -1;
  }

91
92
  if (agent_id == FLEXRAN_AGENT_MAX) {
    for (i = 0; i < FLEXRAN_AGENT_MAX; i++) {
93
      agent_channel[mod_id][i] = channel;
94
95
    }
  } else {
96
    agent_channel[mod_id][agent_id] = channel;
97
98
99
100
  }
  return 0;
}

101
void flexran_agent_unregister_channel(mid_t mod_id, agent_id_t agent_id) {
102
103
  int i;

104
105
  if (agent_id == FLEXRAN_AGENT_MAX) {
    for (i = 0; i < FLEXRAN_AGENT_MAX; i++) {
106
      agent_channel[mod_id][i] = NULL;
107
108
    }
  } else {
109
    agent_channel[mod_id][agent_id] = NULL;
110
111
112
  }
}

113
114
115
116
int flexran_agent_create_channel(void *channel_info,
				 int (*msg_send)(void *data, int size, int priority, void *channel_info),
				 int (*msg_recv)(void **data, int *size, int *priority, void *channel_info),
				 void (*release)(flexran_agent_channel_t *channel)) {
117
  
118
119
  int channel_id = ++flexran_agent_channel_id;
  flexran_agent_channel_t *channel = (flexran_agent_channel_t *) malloc(sizeof(flexran_agent_channel_t));
120
121
122
123
124
125
126
  channel->channel_id = channel_id;
  channel->channel_info = channel_info;
  channel->msg_send = msg_send;
  channel->msg_recv = msg_recv;
  channel->release = release;
  
  /*element should be a real pointer*/
127
  RB_INSERT(flexran_agent_channel_map, &channel_instance.flexran_agent_head, channel); 
128
  
129
  LOG_I(FLEXRAN_AGENT,"Created a new channel with id 0x%lx\n", channel->channel_id);
130
131
132
133
 
  return channel_id; 
}

134
int flexran_agent_destroy_channel(int channel_id) {
135
  int i, j;
136
137

  /*Check to see if channel exists*/
138
139
140
  struct flexran_agent_channel_s *e = NULL;
  struct flexran_agent_channel_s search;
  memset(&search, 0, sizeof(struct flexran_agent_channel_s));
141

142
  e = RB_FIND(flexran_agent_channel_map, &channel_instance.flexran_agent_head, &search);
143
144
145
146
147
148

  if (e == NULL) {
    return -1;
  }

  /*Unregister the channel from all agents*/
149
  for (i = 0; i < NUM_MAX_ENB; i++) {
150
    for (j = 0; j < FLEXRAN_AGENT_MAX; j++) {
151
152
153
154
      if (agent_channel[i][j] != NULL) {
	if (agent_channel[i][j]->channel_id == e->channel_id) {
	  agent_channel[i][j] == NULL;
	}
155
156
157
158
159
      }
    }
  }

  /*Remove the channel from the tree and free memory*/
160
  RB_REMOVE(flexran_agent_channel_map, &channel_instance.flexran_agent_head, e);
161
162
163
164
165
166
  e->release(e);
  free(e);

  return 0;
}

167
err_code_t flexran_agent_init_channel_container(void) {
168
  int i, j;
169
  LOG_I(FLEXRAN_AGENT, "init RB tree for channel container\n");
170

171
  RB_INIT(&channel_instance.flexran_agent_head);
172
  
173
  for (i = 0; i < NUM_MAX_ENB; i++) {
174
    for (j = 0; j < FLEXRAN_AGENT_MAX; j++) {
175
176
    agent_channel[i][j] == NULL;
    }
177
178
179
180
181
  }

  return 0;
}

182
RB_GENERATE(flexran_agent_channel_map, flexran_agent_channel_s, entry, flexran_agent_compare_channel);
183

184
int flexran_agent_compare_channel(struct flexran_agent_channel_s *a, struct flexran_agent_channel_s *b) {
185
186
187
188
189
190
191
  if (a->channel_id < b->channel_id) return -1;
  if (a->channel_id > b->channel_id) return 1;

  // equal timers
  return 0;
}

192
flexran_agent_channel_t * get_channel(int channel_id) {
193
  
194
195
  struct flexran_agent_channel_s search;
  memset(&search, 0, sizeof(struct flexran_agent_channel_s));
196
197
  search.channel_id = channel_id;
  
198
  return  RB_FIND(flexran_agent_channel_map, &channel_instance.flexran_agent_head, &search);
199
200
  
}