enb_agent_handler.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
/*******************************************************************************
    OpenAirInterface
    Copyright(c) 1999 - 2014 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 32
/*! \file enb_agent_handler.c
 * \brief enb agent tx and rx message handler 
 * \author Navid Nikaein and Xenofon Foukas 
33 34 35 36 37 38 39 40 41 42 43 44
 * \date 2016
 * \version 0.1
 */


#include "enb_agent_common.h"
#include "enb_agent_mac.h"
#include "log.h"

#include "assertions.h"

enb_agent_message_decoded_callback messages_callback[][3] = {
45 46 47
  {enb_agent_hello, 0, 0}, /*PROTOCOL__PROGRAN_MESSAGE__MSG_HELLO_MSG*/
  {enb_agent_echo_reply, 0, 0}, /*PROTOCOL__PROGRAN_MESSAGE__MSG_ECHO_REQUEST_MSG*/
  {0, 0, 0}, /*PROTOCOL__PROGRAN_MESSAGE__MSG_ECHO_REPLY_MSG*/ //Must add handler when receiving echo reply
48
  {enb_agent_mac_handle_stats, 0, 0}, /*PROTOCOL__PROGRAN_MESSAGE__MSG_STATS_REQUEST_MSG*/
49
  {0,0,0}, /*PROTOCOL__PROGRAN_MESSAGE__MSG_STATS_REPLY_MSG*/
50 51 52

};

53 54 55 56
enb_agent_message_destruction_callback message_destruction_callback[] = {
  enb_agent_destroy_hello,
  enb_agent_destroy_echo_request,
  enb_agent_destroy_echo_reply,
57
  enb_agent_mac_destroy_stats_request,
58
  enb_agent_mac_destroy_stats_reply,
59
  enb_agent_mac_destroy_sr_info,
60 61
};

62 63 64 65 66 67 68 69
static const char *enb_agent_direction2String[] = {
  "", /* not_set  */
  "originating message", /* originating message */
  "successfull outcome", /* successfull outcome */
  "unsuccessfull outcome", /* unsuccessfull outcome */
};


70
Protocol__ProgranMessage* enb_agent_handle_message (mid_t mod_id,
71 72 73
						    uint8_t *data, 
						    uint32_t size){
  
74
  Protocol__ProgranMessage *decoded_message, *reply_message;
75 76 77
  err_code_t err_code;
  DevAssert(data != NULL);

78
  if (enb_agent_deserialize_message(data, size, &decoded_message) < 0) {
79 80 81 82
    err_code= PROTOCOL__PROGRAN_ERR__MSG_DECODING;
    goto error; 
  }
  
83 84
  if ((decoded_message->msg_case > sizeof(messages_callback) / (3*sizeof(enb_agent_message_decoded_callback))) || 
      (decoded_message->msg_dir > PROTOCOL__PROGRAN_DIRECTION__UNSUCCESSFUL_OUTCOME)){
85 86 87 88
    err_code= PROTOCOL__PROGRAN_ERR__MSG_NOT_HANDLED;
      goto error;
  }
    
89
  if (messages_callback[decoded_message->msg_case-1][decoded_message->msg_dir-1] == NULL) {
90 91 92 93 94
    err_code= PROTOCOL__PROGRAN_ERR__MSG_NOT_SUPPORTED;
    goto error;

  }

95
  err_code = ((*messages_callback[decoded_message->msg_case-1][decoded_message->msg_dir-1])(mod_id, (void *) decoded_message, &reply_message));
96 97 98 99
  if ( err_code < 0 ){
    goto error;
  }
  
100 101 102
  protocol__progran_message__free_unpacked(decoded_message, NULL);

  return reply_message;
103 104
  
error:
105 106
  LOG_E(ENB_AGENT,"errno %d occured\n",err_code);
  return NULL;
107 108 109 110 111

}



112
void * enb_agent_pack_message(Protocol__ProgranMessage *msg, 
113
			      uint32_t * size){
114 115 116 117 118 119 120 121 122 123

  void * buffer;
  err_code_t err_code = PROTOCOL__PROGRAN_ERR__NO_ERR;
  
  if (enb_agent_serialize_message(msg, &buffer, size) < 0 ) {
    err_code = PROTOCOL__PROGRAN_ERR__MSG_ENCODING;
    goto error;
  }
  
  // free the msg --> later keep this in the data struct and just update the values
124
  //TODO call proper destroy function
125
  err_code = ((*message_destruction_callback[msg->msg_case-1])(msg));
126 127
  
  DevAssert(buffer !=NULL);
128 129 130
  
  LOG_D(ENB_AGENT,"Serilized the enb mac stats reply (size %d)\n", *size);
  
131
  return buffer;
132 133 134 135
  
 error : 
  LOG_E(ENB_AGENT,"errno %d occured\n",err_code);
  
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
  return NULL;   
}

Protocol__ProgranMessage *enb_agent_handle_timed_task(void *args) {
  err_code_t err_code;
  enb_agent_timer_args_t *timer_args = (enb_agent_timer_args_t *) args;

  Protocol__ProgranMessage *timed_task, *reply_message;
  timed_task = timer_args->msg;
   err_code = ((*messages_callback[timed_task->msg_case-1][timed_task->msg_dir-1])(timer_args->mod_id, (void *) timed_task, &reply_message));
  if ( err_code < 0 ){
    goto error;
  }

  return reply_message;
151
  
152 153 154
 error:
  LOG_E(ENB_AGENT,"errno %d occured\n",err_code);
  return NULL;
155 156
}

157
Protocol__ProgranMessage* enb_agent_process_timeout(long timer_id, void* timer_args){
158
    
159 160 161 162
  struct enb_agent_timer_element_s *found = get_timer_entry(timer_id);
 
  if (found == NULL ) goto error;
  LOG_I(ENB_AGENT, "Found the entry (%p): timer_id is 0x%lx  0x%lx\n", found, timer_id, found->timer_id);
163 164 165 166
  
  if (timer_args == NULL)
    LOG_W(ENB_AGENT,"null timer args\n");
  
167
  return found->cb(timer_args);
168

169 170 171 172
 error:
  LOG_E(ENB_AGENT, "can't get the timer element\n");
  return TIMER_ELEMENT_NOT_FOUND;
}
173 174 175 176

err_code_t enb_agent_destroy_progran_message(Protocol__ProgranMessage *msg) {
  return ((*message_destruction_callback[msg->msg_case-1])(msg));
}