flexran_agent_net_comm.c 5.81 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
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 23
/*! \file flexran_agent_net_comm.c
 * \brief FlexRAN agent network interface abstraction 
24 25 26 27 28
 * \author Xenofon Foukas
 * \date 2016
 * \version 0.1
 */

29
#include "flexran_agent_net_comm.h"
30 31
#include "log.h"

32 33 34
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;
35

36
int flexran_agent_msg_send(mid_t mod_id, agent_id_t agent_id, void *data, int size, int priority) {
37
  /*Check if agent id is valid*/
38
  if (agent_id >= FLEXRAN_AGENT_MAX || agent_id < 0) {
39 40
    goto error;
  }
41
  flexran_agent_channel_t *channel;
42
  channel = agent_channel[mod_id][agent_id];
43 44 45 46 47 48 49 50 51
  
  /*Check if agent has a channel registered*/
  if (channel == NULL) {
    goto error;
  }

  return channel->msg_send(data, size, priority, channel->channel_info);
  
 error:
52
  LOG_E(FLEXRAN_AGENT, "No channel registered for agent with id %d\n", agent_id);
53 54 55
  return -1;
}

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

76
int flexran_agent_register_channel(mid_t mod_id, flexran_agent_channel_t *channel, agent_id_t agent_id) {
77 78 79 80 81 82
  int i;

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

83 84
  if (agent_id == FLEXRAN_AGENT_MAX) {
    for (i = 0; i < FLEXRAN_AGENT_MAX; i++) {
85
      agent_channel[mod_id][i] = channel;
86 87
    }
  } else {
88
    agent_channel[mod_id][agent_id] = channel;
89 90 91 92
  }
  return 0;
}

93
void flexran_agent_unregister_channel(mid_t mod_id, agent_id_t agent_id) {
94 95
  int i;

96 97
  if (agent_id == FLEXRAN_AGENT_MAX) {
    for (i = 0; i < FLEXRAN_AGENT_MAX; i++) {
98
      agent_channel[mod_id][i] = NULL;
99 100
    }
  } else {
101
    agent_channel[mod_id][agent_id] = NULL;
102 103 104
  }
}

105 106 107 108
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)) {
109
  
110 111
  int channel_id = ++flexran_agent_channel_id;
  flexran_agent_channel_t *channel = (flexran_agent_channel_t *) malloc(sizeof(flexran_agent_channel_t));
112 113 114 115 116 117 118
  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*/
119
  RB_INSERT(flexran_agent_channel_map, &channel_instance.flexran_agent_head, channel); 
120
  
shahab SHARIAT BAGHERI's avatar
shahab SHARIAT BAGHERI committed
121
  LOG_I(FLEXRAN_AGENT,"Created a new channel with id %d \n", channel->channel_id);
122 123 124 125
 
  return channel_id; 
}

126
int flexran_agent_destroy_channel(int channel_id) {
127
  int i, j;
128 129

  /*Check to see if channel exists*/
130 131 132
  struct flexran_agent_channel_s *e = NULL;
  struct flexran_agent_channel_s search;
  memset(&search, 0, sizeof(struct flexran_agent_channel_s));
133

134
  e = RB_FIND(flexran_agent_channel_map, &channel_instance.flexran_agent_head, &search);
135 136 137 138 139 140

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

  /*Unregister the channel from all agents*/
141
  for (i = 0; i < NUM_MAX_ENB; i++) {
142
    for (j = 0; j < FLEXRAN_AGENT_MAX; j++) {
143
      if (agent_channel[i][j] != NULL) {
144 145 146
        if (agent_channel[i][j]->channel_id == e->channel_id) {
            free(agent_channel[i][j]);
        }
147 148 149 150 151
      }
    }
  }

  /*Remove the channel from the tree and free memory*/
152
  RB_REMOVE(flexran_agent_channel_map, &channel_instance.flexran_agent_head, e);
153 154 155 156 157 158
  e->release(e);
  free(e);

  return 0;
}

159
err_code_t flexran_agent_init_channel_container(void) {
160
  int i, j;
161
  LOG_I(FLEXRAN_AGENT, "init RB tree for channel container\n");
162

163
  RB_INIT(&channel_instance.flexran_agent_head);
164
  
165
  for (i = 0; i < NUM_MAX_ENB; i++) {
166
    for (j = 0; j < FLEXRAN_AGENT_MAX; j++) {
167 168 169
      agent_channel[i][j] = malloc(sizeof(flexran_agent_channel_t));
      if (!agent_channel[i][j])
        return -1;
170
    }
171 172 173 174 175
  }

  return 0;
}

176
RB_GENERATE(flexran_agent_channel_map, flexran_agent_channel_s, entry, flexran_agent_compare_channel);
177

178
int flexran_agent_compare_channel(struct flexran_agent_channel_s *a, struct flexran_agent_channel_s *b) {
179 180 181 182 183 184 185
  if (a->channel_id < b->channel_id) return -1;
  if (a->channel_id > b->channel_id) return 1;

  // equal timers
  return 0;
}

186
flexran_agent_channel_t * get_channel(int channel_id) {
187
  
188 189
  struct flexran_agent_channel_s search;
  memset(&search, 0, sizeof(struct flexran_agent_channel_s));
190 191
  search.channel_id = channel_id;
  
192
  return  RB_FIND(flexran_agent_channel_map, &channel_instance.flexran_agent_head, &search);
193 194
  
}