enb_agent.c 4.72 KB
Newer Older
nikaeinn's avatar
nikaeinn committed
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
197
198
199
200
201
202
/*******************************************************************************
    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.

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

/*! \file 
 * \brief 
 * \author 
 * \date 2016
 * \version 0.1
 */


#include <stdio.h>
#include <stdlib.h>
#include <pthread.h>
#include <string.h>
#include <unistd.h>

#include "enb_agent_common.h"
#include "link_manager.h"
#include "log.h"
#include "enb_agent.h"

typedef uint8_t xid_t;  

// tx and rx shared context 
typedef struct {
  message_queue_t       *tx_mq;
  message_queue_t       *rx_mq;
  xid_t                 tx_xid;
  xid_t                 rx_xid; 
} msg_context_t;
msg_context_t shared_ctxt;

void *send_thread(void *arg);
void *receive_thread(void *arg);
pthread_t new_thread(void *(*f)(void *), void *b);

void *send_thread(void *arg) {

  msg_context_t         *d = arg;
  void                  *data;
  int                   size;
  int                   priority;

  
  while (1) {
    // need logic for the timer, and 
    usleep(10);
    if (message_put(d->tx_mq, data, size, priority)) goto error;
  }

  return NULL;

error:
  printf("receive_thread: there was an error\n");
  return NULL;
}

void *receive_thread(void *arg) {

  msg_context_t         *d = arg;
  void                  *data;
  int                   size;
  int                   priority;
  err_code_t             err_code;

  Protocol__ProgranMessage *msg;
  
  while (1) {
    if (message_get(d->rx_mq, &data, &size, &priority)){
      err_code = PROTOCOL__PROGRAN_ERR__MSG_DEQUEUING;
      goto error;
    }
    LOG_D(ENB_APP,"received message with size %d\n", size);
  
    
    msg=enb_agent_handle_message(d->rx_xid, data, size);

    free(data);
    
    d->rx_xid = ((d->rx_xid)+1)%4;
    d->tx_xid = d->rx_xid;
  
    // check if there is something to send back to the controller
    if (msg != NULL){
      data=enb_agent_send_message(d->tx_xid,msg,&size);
     
      if (message_put(d->tx_mq, data, size, priority)){
	err_code = PROTOCOL__PROGRAN_ERR__MSG_ENQUEUING;
	goto error;
      }
      LOG_D(ENB_APP,"sent message with size %d\n", size);
    }
    
  }

  return NULL;

error:
  printf("receive_thread: error %d occured\n",err_code);
  return NULL;
}


/* utility function to create a thread */
pthread_t new_thread(void *(*f)(void *), void *b) {
  pthread_t t;
  pthread_attr_t att;

  if (pthread_attr_init(&att)){ 
    fprintf(stderr, "pthread_attr_init err\n"); 
    exit(1); 
  }
  if (pthread_attr_setdetachstate(&att, PTHREAD_CREATE_DETACHED)) { 
    fprintf(stderr, "pthread_attr_setdetachstate err\n"); 
    exit(1); 
  }
  if (pthread_create(&t, &att, f, b)) { 
    fprintf(stderr, "pthread_create err\n"); 
    exit(1); 
  }
  if (pthread_attr_destroy(&att)) { 
    fprintf(stderr, "pthread_attr_destroy err\n"); 
    exit(1); 
  }

  return t;
}

int enb_agent_start(){

  socket_link_t   *link;
  message_queue_t *send_queue;
  message_queue_t *receive_queue;
  link_manager_t  *manager;

  LOG_I(ENB_APP,"starting enb agent client\n");

  link = new_link_client("127.0.0.1", 2210);
  if (link == NULL) goto error;

  send_queue = new_message_queue();
  if (send_queue == NULL) goto error;
  receive_queue = new_message_queue();
  if (receive_queue == NULL) goto error;

  manager = create_link_manager(send_queue, receive_queue, link);
  if (manager == NULL) goto error;


  memset(&shared_ctxt, 0, sizeof(msg_context_t));
  
  shared_ctxt.tx_mq = send_queue;
  shared_ctxt.rx_mq = receive_queue;

  new_thread(receive_thread, &shared_ctxt);
  
  //  new_thread(send_thread, &shared_ctxt);

  // while (1) pause();

  printf("client ends\n");
  return 0;

error:
  printf("there was an error\n");
  return 1;

}



int enb_agent_stop(){


}