enb_agent_net_comm.c 5.49 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 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 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 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
/*******************************************************************************
    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.

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

/*! \file enb_agent_net_comm.c
 * \brief enb agent network interface abstraction 
 * \author Xenofon Foukas
 * \date 2016
 * \version 0.1
 */

#include "enb_agent_net_comm.h"
#include "log.h"

enb_agent_channel_t *agent_channel[ENB_AGENT_MAX];
enb_agent_channel_instance_t channel_instance;
int enb_agent_channel_id = 0;

int enb_agent_msg_send(void *data, int size, int priority, agent_id_t agent_id) {
  /*Check if agent id is valid*/
  if (agent_id >= ENB_AGENT_MAX || agent_id < 0) {
    goto error;
  }
  enb_agent_channel_t *channel;
  channel = agent_channel[agent_id];
  
  /*Check if agent has a channel registered*/
  if (channel == NULL) {
    goto error;
  }

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

int enb_agent_msg_recv(void **data, int *size, int *priority, agent_id_t agent_id) {
  /*Check if agent id is valid*/
  if (agent_id >= ENB_AGENT_MAX || agent_id < 0) {
    goto error;
  }
  enb_agent_channel_t *channel;
  channel = agent_channel[agent_id];
  
  /*Check if agent has a channel registered*/
  if (channel == NULL) {
    goto error;
  }
  
  return channel->msg_recv(data, size, priority, channel->channel_info);
  
 error:
  LOG_E(ENB_AGENT, "No channel registered for agent with id %d\n", agent_id);
  return -1;
}

int enb_agent_register_channel(enb_agent_channel_t *channel, agent_id_t agent_id) {
  int i;

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

  if (agent_id == ENB_AGENT_MAX) {
    for (i = 0; i < ENB_AGENT_MAX; i++) {
      agent_channel[i] = channel;
    }
  } else {
    agent_channel[agent_id] = channel;
  }
  return 0;
}

void enb_agent_unregister_channel(agent_id_t agent_id) {
  int i;

  if (agent_id == ENB_AGENT_MAX) {
    for (i = 0; i < ENB_AGENT_MAX; i++) {
      agent_channel[i] = NULL;
    }
  } else {
    agent_channel[agent_id] = NULL;
  }
}

int enb_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)(enb_agent_channel_t *channel)) {
  
  int channel_id = ++enb_agent_channel_id;
  enb_agent_channel_t *channel = (enb_agent_channel_t *) malloc(sizeof(enb_agent_channel_t));
  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*/
  RB_INSERT(enb_agent_channel_map, &channel_instance.enb_agent_head, channel); 
  
  LOG_I(ENB_AGENT,"Created a new channel with id 0x%lx\n", channel->channel_id);
 
  return channel_id; 
}

int enb_agent_destroy_channel(int channel_id) {
  int i;

  /*Check to see if channel exists*/
  struct enb_agent_channel_s *e = NULL;
  struct enb_agent_channel_s search;
  memset(&search, 0, sizeof(struct enb_agent_channel_s));

  e = RB_FIND(enb_agent_channel_map, &channel_instance.enb_agent_head, &search);

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

  /*Unregister the channel from all agents*/
  for (i = 0; i < ENB_AGENT_MAX; i++) {
    if (agent_channel[i] != NULL) {
      if (agent_channel[i]->channel_id == e->channel_id) {
	agent_channel[i] == NULL;
      }
    }
  }

  /*Remove the channel from the tree and free memory*/
  RB_REMOVE(enb_agent_channel_map, &channel_instance.enb_agent_head, e);
  e->release(e);
  free(e);

  return 0;
}

err_code_t enb_agent_init_channel_container(void) {
  int i;
  LOG_I(ENB_AGENT, "init RB tree for channel container\n");

  RB_INIT(&channel_instance.enb_agent_head);

  for (i = 0; i < ENB_AGENT_MAX; i++) {
    agent_channel[i] == NULL;
  }

  return 0;
}

RB_GENERATE(enb_agent_channel_map,enb_agent_channel_s, entry, enb_agent_compare_channel);

int enb_agent_compare_channel(struct enb_agent_channel_s *a, struct enb_agent_channel_s *b) {
  if (a->channel_id < b->channel_id) return -1;
  if (a->channel_id > b->channel_id) return 1;

  // equal timers
  return 0;
}

enb_agent_channel_t * get_channel(int channel_id) {
  
  struct enb_agent_channel_s search;
  memset(&search, 0, sizeof(struct enb_agent_channel_s));
  search.channel_id = channel_id;
  
  return  RB_FIND(enb_agent_channel_map, &channel_instance.enb_agent_head, &search);
  
}