nas_ue_task.c 10.4 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/*
 * 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
 * the OAI Public License, Version 1.0  (the "License"); you may not use this file
 * 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
 */

22
#include "utils.h"
winckel's avatar
winckel committed
23
#if defined(ENABLE_ITTI)
24
# include "assertions.h"
winckel's avatar
winckel committed
25
26
# include "intertask_interface.h"
# include "nas_ue_task.h"
27
# include "UTIL/LOG/log.h"
28

29
# include "user_defs.h"
30
# include "user_api.h"
31
# include "nas_parser.h"
32
# include "nas_proc.h"
33
# include "msc.h"
34
# include "memory.h"
35

36
37
#include "nas_user.h"

38
// FIXME make command line option for NAS_UE_AUTOSTART
39
# define NAS_UE_AUTOSTART 1
winckel's avatar
winckel committed
40

41
// FIXME review these externs
winckel's avatar
winckel committed
42
extern unsigned char NB_eNB_INST;
43
extern unsigned char NB_UE_INST;
winckel's avatar
winckel committed
44

45
46
47
char *make_port_str_from_ueid(const char *base_port_str, int ueid);

static int nas_ue_process_events(nas_user_container_t *users, struct epoll_event *events, int nb_events)
48
49
50
{
  int event;
  int exit_loop = FALSE;
winckel's avatar
winckel committed
51

52
  LOG_I(NAS, "[UE] Received %d events\n", nb_events);
53

54
  for (event = 0; event < nb_events; event++) {
55
    if (events[event].events != 0) {
56
      /* If the event has not been yet been processed (not an itti message) */
57
58
      nas_user_t *user = find_user_from_fd(users, events[event].data.fd);
      if ( user != NULL ) {
59
        exit_loop = nas_user_receive_and_process(user, NULL);
60
      } else {
61
        LOG_E(NAS, "[UE] Received an event from an unknown fd %d!\n", events[event].data.fd);
62
63
64
      }
    }
  }
winckel's avatar
winckel committed
65

66
67
  return (exit_loop);
}
winckel's avatar
winckel committed
68

69
70
71
72
// Initialize user api id and port number
void nas_user_api_id_initialize(nas_user_t *user) {
  user_api_id_t *user_api_id = calloc_or_fail(sizeof(user_api_id_t));
  user->user_api_id = user_api_id;
73
74
75
76
77
78
  char *port = make_port_str_from_ueid(NAS_PARSER_DEFAULT_USER_PORT_NUMBER, user->ueid);
  if ( port == NULL ) {
      LOG_E(NAS, "[UE %d] can't get port from ueid %d !", user->ueid);
      exit (EXIT_FAILURE);
  }
  if (user_api_initialize (user_api_id, NAS_PARSER_DEFAULT_USER_HOSTNAME, port, NULL,
79
80
81
82
              NULL) != RETURNok) {
      LOG_E(NAS, "[UE %d] user interface initialization failed!", user->ueid);
      exit (EXIT_FAILURE);
  }
83
  free(port);
84
85
86
  itti_subscribe_event_fd (TASK_NAS_UE, user_api_get_fd(user_api_id));
}

87
88
void *nas_ue_task(void *args_p)
{
89
90
91
92
93
94
  int                   nb_events;
  struct epoll_event   *events;
  MessageDef           *msg_p;
  const char           *msg_name;
  instance_t            instance;
  unsigned int          Mod_id;
95
  int                   result;
96
  nas_user_container_t *users=args_p;
winckel's avatar
winckel committed
97

98
  itti_mark_task_ready (TASK_NAS_UE);
99
  MSC_START_USE();
100
  /* Initialize UE NAS (EURECOM-NAS) */
101
  for (int i=0; i < users->count; i++)
102
  {
103
104
105
    nas_user_t *user = &users->item[i];
    user->ueid=i;

106
    /* Get USIM data application filename */
107
    user->usim_data_store = memory_get_path_from_ueid(USIM_API_NVRAM_DIRNAME, USIM_API_NVRAM_FILENAME, user->ueid);
108
    if ( user->usim_data_store == NULL ) {
109
      LOG_E(NAS, "[UE %d] - Failed to get USIM data application filename", user->ueid, user->ueid);
110
111
112
      exit(EXIT_FAILURE);
    }

113
    /* Get UE's data pathname */
114
    user->user_nvdata_store = memory_get_path_from_ueid(USER_NVRAM_DIRNAME, USER_NVRAM_FILENAME, user->ueid);
115
    if ( user->user_nvdata_store == NULL ) {
116
117
118
119
120
      LOG_E(NAS, "[UE %d] - Failed to get USIM nvdata filename", user->ueid);
      exit(EXIT_FAILURE);
    }

    /* Get EMM data pathname */
121
    user->emm_nvdata_store = memory_get_path_from_ueid(EMM_NVRAM_DIRNAME, EMM_NVRAM_FILENAME, user->ueid);
122
123
    if ( user->emm_nvdata_store == NULL ) {
      LOG_E(NAS, "[UE %d] - Failed to get EMM nvdata filename", user->ueid);
124
125
126
      exit(EXIT_FAILURE);
    }

127
    /* Initialize user interface (to exchange AT commands with user process) */
128
    nas_user_api_id_initialize(user);
129
    /* allocate needed structures */
130
131
132
    user->user_at_commands = calloc_or_fail(sizeof(user_at_commands_t));
    user->at_response = calloc_or_fail(sizeof(at_response_t));
    user->lowerlayer_data = calloc_or_fail(sizeof(lowerlayer_data_t));
133
    /* Initialize NAS user */
134
    nas_user_initialize (user, &user_api_emm_callback, &user_api_esm_callback, FIRMWARE_VERSION);
135
136
  }

137
  /* Set UE activation state */
138
139
  for (instance = NB_eNB_INST; instance < (NB_eNB_INST + NB_UE_INST); instance++) {
    MessageDef *message_p;
140

141
142
    message_p = itti_alloc_new_message(TASK_NAS_UE, DEACTIVATE_MESSAGE);
    itti_send_msg_to_task(TASK_L2L1, instance, message_p);
143
144
  }

145
146
147
148
149
150
151
152
  while(1) {
    // Wait for a message or an event
    itti_receive_msg (TASK_NAS_UE, &msg_p);

    if (msg_p != NULL) {
      msg_name = ITTI_MSG_NAME (msg_p);
      instance = ITTI_MSG_INSTANCE (msg_p);
      Mod_id = instance - NB_eNB_INST;
153
154
155
156
157
      if (instance == INSTANCE_DEFAULT) {
        printf("%s:%d: FATAL: instance is INSTANCE_DEFAULT, should not happen.\n",
               __FILE__, __LINE__);
        abort();
      }
158
      nas_user_t *user = &users->item[Mod_id];
159
160

      switch (ITTI_MSG_ID(msg_p)) {
161
162
      case INITIALIZE_MESSAGE:
        LOG_I(NAS, "[UE %d] Received %s\n", Mod_id, msg_name);
163
#if (NAS_UE_AUTOSTART != 0)
164
165
166
        {
          /* Send an activate modem command to NAS like UserProcess should do it */
          char *user_data = "at+cfun=1\r";
167

168
          nas_user_receive_and_process (user, user_data);
169
        }
170
#endif
171
        break;
172

173
174
175
      case TERMINATE_MESSAGE:
        itti_exit_task ();
        break;
176

177
178
179
      case MESSAGE_TEST:
        LOG_I(NAS, "[UE %d] Received %s\n", Mod_id, msg_name);
        break;
180

181
182
183
      case NAS_CELL_SELECTION_CNF:
        LOG_I(NAS, "[UE %d] Received %s: errCode %u, cellID %u, tac %u\n", Mod_id, msg_name,
              NAS_CELL_SELECTION_CNF (msg_p).errCode, NAS_CELL_SELECTION_CNF (msg_p).cellID, NAS_CELL_SELECTION_CNF (msg_p).tac);
184

185
186
        {
          int cell_found = (NAS_CELL_SELECTION_CNF (msg_p).errCode == AS_SUCCESS);
187

188
          nas_proc_cell_info (user, cell_found, NAS_CELL_SELECTION_CNF (msg_p).tac,
189
190
191
192
                              NAS_CELL_SELECTION_CNF (msg_p).cellID, NAS_CELL_SELECTION_CNF (msg_p).rat,
                              NAS_CELL_SELECTION_CNF (msg_p).rsrq, NAS_CELL_SELECTION_CNF (msg_p).rsrp);
        }
        break;
193

194
195
196
      case NAS_CELL_SELECTION_IND:
        LOG_I(NAS, "[UE %d] Received %s: cellID %u, tac %u\n", Mod_id, msg_name,
              NAS_CELL_SELECTION_IND (msg_p).cellID, NAS_CELL_SELECTION_IND (msg_p).tac);
197

198
199
        /* TODO not processed by NAS currently */
        break;
200

201
202
203
      case NAS_PAGING_IND:
        LOG_I(NAS, "[UE %d] Received %s: cause %u\n", Mod_id, msg_name,
              NAS_PAGING_IND (msg_p).cause);
204

205
206
        /* TODO not processed by NAS currently */
        break;
207

208
209
210
      case NAS_CONN_ESTABLI_CNF:
        LOG_I(NAS, "[UE %d] Received %s: errCode %u, length %u\n", Mod_id, msg_name,
              NAS_CONN_ESTABLI_CNF (msg_p).errCode, NAS_CONN_ESTABLI_CNF (msg_p).nasMsg.length);
211

212
213
        if ((NAS_CONN_ESTABLI_CNF (msg_p).errCode == AS_SUCCESS)
            || (NAS_CONN_ESTABLI_CNF (msg_p).errCode == AS_TERMINATED_NAS)) {
214
          nas_proc_establish_cnf(user, NAS_CONN_ESTABLI_CNF (msg_p).nasMsg.data, NAS_CONN_ESTABLI_CNF (msg_p).nasMsg.length);
215

216
217
218
219
          /* TODO checks if NAS will free the nas message, better to do it there anyway! */
          // result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), NAS_CONN_ESTABLI_CNF(msg_p).nasMsg.data);
          // AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
        }
220

221
        break;
222

223
224
225
      case NAS_CONN_RELEASE_IND:
        LOG_I(NAS, "[UE %d] Received %s: cause %u\n", Mod_id, msg_name,
              NAS_CONN_RELEASE_IND (msg_p).cause);
226

227
        nas_proc_release_ind (user, NAS_CONN_RELEASE_IND (msg_p).cause);
228
        break;
229

230
231
232
      case NAS_UPLINK_DATA_CNF:
        LOG_I(NAS, "[UE %d] Received %s: UEid %u, errCode %u\n", Mod_id, msg_name,
              NAS_UPLINK_DATA_CNF (msg_p).UEid, NAS_UPLINK_DATA_CNF (msg_p).errCode);
233

234
        if (NAS_UPLINK_DATA_CNF (msg_p).errCode == AS_SUCCESS) {
235
          nas_proc_ul_transfer_cnf (user);
236
        } else {
237
          nas_proc_ul_transfer_rej (user);
238
        }
239

240
        break;
241

242
243
244
245
      case NAS_DOWNLINK_DATA_IND:
        LOG_I(NAS, "[UE %d] Received %s: UEid %u, length %u\n", Mod_id, msg_name,
              NAS_DOWNLINK_DATA_IND (msg_p).UEid, NAS_DOWNLINK_DATA_IND (msg_p).nasMsg.length);

246
        nas_proc_dl_transfer_ind (user, NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data, NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.length);
247
248

        if (0) {
249
          /* TODO checks if NAS will free the nas message, better to do it there anyway! */
250
          result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), NAS_DOWNLINK_DATA_IND(msg_p).nasMsg.data);
251
          AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
252
        }
253

254
255
256
257
258
        break;

      default:
        LOG_E(NAS, "[UE %d] Received unexpected message %s\n", Mod_id, msg_name);
        break;
259
260
      }

261
262
      result = itti_free (ITTI_MSG_ORIGIN_ID(msg_p), msg_p);
      AssertFatal (result == EXIT_SUCCESS, "Failed to free memory (%d)!\n", result);
263
      msg_p = NULL;
264
    }
265
266

    nb_events = itti_get_events(TASK_NAS_UE, &events);
267

268
    if ((nb_events > 0) && (events != NULL)) {
269
      if (nas_ue_process_events(users, events, nb_events) == TRUE) {
270
        LOG_E(NAS, "[UE] Received exit loop\n");
271
272
      }
    }
winckel's avatar
winckel committed
273
  }
274
275
276

  free(users);
  return NULL;
winckel's avatar
winckel committed
277
}
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305

nas_user_t *find_user_from_fd(nas_user_container_t *users, int fd) {
  for (int i=0; i<users->count; i++) {
    nas_user_t *user = &users->item[i];
    if (fd == user_api_get_fd(user->user_api_id)) {
      return user;
    }
  }
  return NULL;
}

char *make_port_str_from_ueid(const char *base_port_str, int ueid) {
  int port;
  int base_port;
  char *endptr = NULL;

  base_port = strtol(base_port_str, &endptr, 10);
  if ( base_port_str == endptr ) {
    return NULL;
  }

  port = base_port + ueid;
  if ( port<1 || port > 65535 ) {
    return NULL;
  }

  return itoa(port);
}
winckel's avatar
winckel committed
306
#endif