emm_main.c 48.7 KB
Newer Older
Cedric Roux's avatar
 
Cedric Roux committed
1
/*****************************************************************************
Cedric Roux's avatar
Cedric Roux committed
2 3
            Eurecom OpenAirInterface 3
            Copyright(c) 2012 Eurecom
Cedric Roux's avatar
 
Cedric Roux committed
4

Cedric Roux's avatar
Cedric Roux committed
5
Source      emm_main.c
Cedric Roux's avatar
 
Cedric Roux committed
6

Cedric Roux's avatar
Cedric Roux committed
7
Version     0.1
Cedric Roux's avatar
 
Cedric Roux committed
8

Cedric Roux's avatar
Cedric Roux committed
9
Date        2012/10/10
Cedric Roux's avatar
 
Cedric Roux committed
10

Cedric Roux's avatar
Cedric Roux committed
11
Product     NAS stack
Cedric Roux's avatar
 
Cedric Roux committed
12

Cedric Roux's avatar
Cedric Roux committed
13
Subsystem   EPS Mobility Management
Cedric Roux's avatar
 
Cedric Roux committed
14

Cedric Roux's avatar
Cedric Roux committed
15
Author      Frederic Maurel
Cedric Roux's avatar
 
Cedric Roux committed
16

Cedric Roux's avatar
Cedric Roux committed
17 18
Description Defines the EPS Mobility Management procedure call manager,
        the main entry point for elementary EMM processing.
Cedric Roux's avatar
 
Cedric Roux committed
19 20 21 22 23 24 25 26 27 28 29 30

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

#include "emm_main.h"
#include "nas_log.h"
#include "emmData.h"

#ifdef NAS_UE
#include "memory.h"
#include "usim_api.h"
#include "IdleMode.h"

Cedric Roux's avatar
Cedric Roux committed
31 32 33
#include <string.h> // memset, memcpy, strlen
#include <stdio.h>  // sprintf
#include <stdlib.h> // malloc, free
Cedric Roux's avatar
 
Cedric Roux committed
34 35
#endif

36 37 38 39
#if defined(EPC_BUILD) && defined(NAS_MME)
# include "mme_config.h"
#endif

Cedric Roux's avatar
 
Cedric Roux committed
40 41 42 43 44 45 46 47 48
/****************************************************************************/
/****************  E X T E R N A L    D E F I N I T I O N S  ****************/
/****************************************************************************/

/****************************************************************************/
/*******************  L O C A L    D E F I N I T I O N S  *******************/
/****************************************************************************/

#ifdef NAS_UE
Cedric Roux's avatar
Cedric Roux committed
49
static int _emm_main_get_imei(imei_t *imei, const char *imei_str);
Cedric Roux's avatar
 
Cedric Roux committed
50

Cedric Roux's avatar
Cedric Roux committed
51
static int _emm_main_imsi_cmp(imsi_t *imsi1, imsi_t *imsi2);
Cedric Roux's avatar
 
Cedric Roux committed
52

Cedric Roux's avatar
Cedric Roux committed
53 54
static const char *_emm_main_get_plmn(const plmn_t *plmn, int index,
                                      int format, size_t *size);
Cedric Roux's avatar
 
Cedric Roux committed
55

Cedric Roux's avatar
Cedric Roux committed
56
static int _emm_main_get_plmn_index(const char *plmn, int format);
Cedric Roux's avatar
 
Cedric Roux committed
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77

/*
 * USIM application data
 */
static usim_data_t _usim_data;

/*
 * Callback executed whenever a change in the network has to be notified
 * to the user application
 */
static emm_indication_callback_t _emm_main_user_callback;
static int _emm_main_callback(int);
#endif

/****************************************************************************/
/******************  E X P O R T E D    F U N C T I O N S  ******************/
/****************************************************************************/

#ifdef NAS_UE
/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
78
 ** Name:    emm_main_initialize()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
79 80 81
 **                                                                        **
 ** Description: Initializes EMM internal data                             **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
82 83 84 85
 ** Inputs:  cb:        The user notification callback             **
 **      imei:      The IMEI read from the UE's non-volatile   **
 **             memory                                     **
 **      Others:    _usim_data                                 **
Cedric Roux's avatar
 
Cedric Roux committed
86
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
87 88 89
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
90 91
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
92
void emm_main_initialize(emm_indication_callback_t cb, const char *imei)
Cedric Roux's avatar
 
Cedric Roux committed
93 94 95 96 97
{
    LOG_FUNC_IN;

    /* USIM validity indicator */
    _emm_data.usim_is_valid = FALSE;
Cedric Roux's avatar
Cedric Roux committed
98 99
    /* The IMEI read from the UE's non-volatile memory  */
    _emm_data.imei = (imei_t *)malloc(sizeof(imei_t));
Cedric Roux's avatar
 
Cedric Roux committed
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
    _emm_data.imei->length = _emm_main_get_imei(_emm_data.imei, imei);
    /* The IMSI, valid only if USIM is present */
    _emm_data.imsi = NULL;
    /* EPS location information */
    _emm_data.guti = NULL;
    _emm_data.tai = NULL;
    _emm_data.ltai.n_tais = 0;
    /* EPS Connection Management status */
    _emm_data.ecm_status = ECM_IDLE;
    /* Network selection mode of operation */
    _emm_data.plmn_mode = EMM_DATA_PLMN_AUTO;
    /* Index of the PLMN manually selected by the user */
    _emm_data.plmn_index = -1;
    /* Selected Radio Access Technology */
    _emm_data.plmn_rat = NET_ACCESS_UNAVAILABLE;
    /* Selected PLMN */
    memset(&_emm_data.splmn, 0xFF, sizeof(plmn_t));
    _emm_data.is_rplmn = FALSE;
    _emm_data.is_eplmn = FALSE;
    /* Radio Access Technology of the serving cell */
    _emm_data.rat = NET_ACCESS_UNAVAILABLE;
    /* Network registration status */
    _emm_data.stat = NET_REG_STATE_OFF;
    _emm_data.is_attached = FALSE;
    _emm_data.is_emergency = FALSE;
    /* Location/Tracking area code */
Cedric Roux's avatar
Cedric Roux committed
126
    _emm_data.tac = 0;  // two byte in hexadecimal format
Cedric Roux's avatar
 
Cedric Roux committed
127
    /* Identifier of the serving cell */
Cedric Roux's avatar
Cedric Roux committed
128
    _emm_data.ci = 0;   // four byte in hexadecimal format
Cedric Roux's avatar
 
Cedric Roux committed
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
    /* List of operators present in the network */
    memset(_emm_data.plist.buffer, 0, EMM_DATA_BUFFER_SIZE + 1);
    /* Home PLMN */
    memset(&_emm_data.hplmn, 0xFF, sizeof(plmn_t));
    /* List of Forbidden PLMNs */
    _emm_data.fplmn.n_plmns = 0;
    /* List of Forbidden PLMNs for GPRS service */
    _emm_data.fplmn_gprs.n_plmns = 0;
    /* List of Equivalent HPLMNs */
    _emm_data.ehplmn.n_plmns = 0;
    /* List of user controlled PLMNs */
    _emm_data.plmn.n_plmns = 0;
    /* List of operator controlled PLMNs */
    _emm_data.oplmn.n_plmns = 0;
    /* List of operator network name records */
    _emm_data.n_opnns = 0;
    /* List of Forbidden Tracking Areas */
    _emm_data.ftai.n_tais = 0;
    /* List of Forbidden Tracking Areas for roaming */
    _emm_data.ftai_roaming.n_tais = 0;

    /*
     * Get USIM application data
     */
    if ( usim_api_read(&_usim_data) != RETURNok ) {
Cedric Roux's avatar
Cedric Roux committed
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 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 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 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
        /* The USIM application may not be present or not valid */
        LOG_TRACE(WARNING, "EMM-MAIN  - Failed to read USIM application data");
    } else {
        int i;

        /* The USIM application is present and valid */
        LOG_TRACE(INFO, "EMM-MAIN  - USIM application data successfully read");
        _emm_data.usim_is_valid = TRUE;

        /* Get the Home PLMN derived from the IMSI */
        _emm_data.hplmn.MCCdigit1 = _usim_data.imsi.u.num.digit1;
        _emm_data.hplmn.MCCdigit2 = _usim_data.imsi.u.num.digit2;
        _emm_data.hplmn.MCCdigit3 = _usim_data.imsi.u.num.digit3;
        _emm_data.hplmn.MNCdigit1 = _usim_data.imsi.u.num.digit4;
        _emm_data.hplmn.MNCdigit2 = _usim_data.imsi.u.num.digit5;
        _emm_data.hplmn.MNCdigit3 = _usim_data.imsi.u.num.digit6;

        /* Get the list of forbidden PLMNs */
        for (i=0; (i < EMM_DATA_FPLMN_MAX) && (i < USIM_FPLMN_MAX); i++) {
            if ( PLMN_IS_VALID(_usim_data.fplmn[i]) ) {
                _emm_data.fplmn.plmn[i] = _usim_data.fplmn[i];
                _emm_data.fplmn.n_plmns += 1;
            }
        }

        /* Get the list of Equivalent HPLMNs */
        for (i=0; (i < EMM_DATA_EHPLMN_MAX) && (i < USIM_EHPLMN_MAX); i++) {
            if ( PLMN_IS_VALID(_usim_data.ehplmn[i]) ) {
                _emm_data.ehplmn.plmn[i] = _usim_data.ehplmn[i];
                _emm_data.ehplmn.n_plmns += 1;
            }
        }

        /* Get the list of User controlled PLMN Selector */
        for (i=0; (i < EMM_DATA_PLMN_MAX) && (i < USIM_PLMN_MAX); i++) {
            if ( PLMN_IS_VALID(_usim_data.plmn[i].plmn) ) {
                _emm_data.plmn.plmn[i] = _usim_data.plmn[i].plmn;
                _emm_data.userAcT[i] = _usim_data.plmn[i].AcT;
                _emm_data.plmn.n_plmns += 1;
            }
        }

        /* Get the list of Operator controlled PLMN Selector */
        for (i=0; (i < EMM_DATA_OPLMN_MAX) && (i < USIM_OPLMN_MAX); i++) {
            if ( PLMN_IS_VALID(_usim_data.oplmn[i].plmn) ) {
                _emm_data.oplmn.plmn[i] = _usim_data.oplmn[i].plmn;
                _emm_data.operAcT[i] = _usim_data.oplmn[i].AcT;
                _emm_data.oplmn.n_plmns += 1;
            }
        }

        /* Get the list of Operator network name records */
        for (i=0; (i < EMM_DATA_OPNN_MAX) && (i < USIM_OPL_MAX); i++) {
            if ( PLMN_IS_VALID(_usim_data.opl[i].plmn) ) {
                int pnn_id = _usim_data.opl[i].record_id;
                _emm_data.opnn[i].plmn = &_usim_data.opl[i].plmn;
                _emm_data.opnn[i].fullname = (char *)_usim_data.pnn[pnn_id].fullname.value;
                _emm_data.opnn[i].shortname = (char *)_usim_data.pnn[pnn_id].shortname.value;
                _emm_data.n_opnns += 1;
            }
        }

        /* TODO: Get the Higher Priority PLMN search period parameter */

        /* Get the EPS location information */
        if (PLMN_IS_VALID(_usim_data.epsloci.guti.gummei.plmn)) {
            _emm_data.guti = &_usim_data.epsloci.guti;
        }
        if (TAI_IS_VALID(_usim_data.epsloci.tai)) {
            _emm_data.tai = &_usim_data.epsloci.tai;
        }
        _emm_data.status = _usim_data.epsloci.status;

        /* Get NAS configuration parameters */
        _emm_data.NAS_SignallingPriority =
            _usim_data.nasconfig.NAS_SignallingPriority.value[0];
        _emm_data.NMO_I_Behaviour = _usim_data.nasconfig.NMO_I_Behaviour.value[0];
        _emm_data.AttachWithImsi = _usim_data.nasconfig.AttachWithImsi.value[0];
        _emm_data.MinimumPeriodicSearchTimer =
            _usim_data.nasconfig.MinimumPeriodicSearchTimer.value[0];
        _emm_data.ExtendedAccessBarring =
            _usim_data.nasconfig.ExtendedAccessBarring.value[0];
        _emm_data.Timer_T3245_Behaviour =
            _usim_data.nasconfig.Timer_T3245_Behaviour.value[0];

        /*
         * Get EPS NAS security context
         */
        /* Create NAS security context */
        _emm_data.security =
            (emm_security_context_t *)malloc(sizeof(emm_security_context_t));
        if (_emm_data.security != NULL) {
            memset(_emm_data.security, 0, sizeof(emm_security_context_t));
            /* Type of security context */
            if (_usim_data.securityctx.KSIasme.value[0] !=
                    USIM_KSI_NOT_AVAILABLE) {
                _emm_data.security->type = EMM_KSI_NATIVE;
            } else {
                _emm_data.security->type = EMM_KSI_NOT_AVAILABLE;
            }
            /* EPS key set identifier */
            _emm_data.security->eksi = _usim_data.securityctx.KSIasme.value[0];
            /* ASME security key */
            _emm_data.security->kasme.length =
                _usim_data.securityctx.Kasme.length;
            _emm_data.security->kasme.value =
                (uint8_t *)malloc(_emm_data.security->kasme.length);
            if (_emm_data.security->kasme.value) {
                memcpy(_emm_data.security->kasme.value,
                       _usim_data.securityctx.Kasme.value,
                       _emm_data.security->kasme.length);
            }
            /* Downlink count parameter */
            if (_usim_data.securityctx.dlNAScount.length <= sizeof(UInt32_t)) {
                memcpy(&_emm_data.security->dl_count,
                       _usim_data.securityctx.dlNAScount.value,
                       _usim_data.securityctx.dlNAScount.length);
            }
            /* Uplink count parameter */
            if (_usim_data.securityctx.ulNAScount.length <= sizeof(UInt32_t)) {
                memcpy(&_emm_data.security->ul_count,
                       _usim_data.securityctx.ulNAScount.value,
                       _usim_data.securityctx.ulNAScount.length);
            }
            /* Ciphering algorithm */
            _emm_data.security->capability.encryption =
                ((_usim_data.securityctx.algorithmID.value[0] >> 4) & 0xf);
            /* Identity protection algorithm */
            _emm_data.security->capability.integrity =
                (_usim_data.securityctx.algorithmID.value[0] & 0xf);
            /* NAS integrity and cyphering keys are not available */
        } else {
            LOG_TRACE(WARNING,
                      "EMM-PROC  - Failed to create security context");
        }

        /*
         * Get EMM data from the UE's non-volatile memory
         */
        memset(&_emm_data.nvdata.rplmn, 0xFF, sizeof(plmn_t));
        _emm_data.nvdata.eplmn.n_plmns = 0;
        /* Get EMM data pathname */
        char *path = memory_get_path(EMM_NVRAM_DIRNAME, EMM_NVRAM_FILENAME);
        if (path == NULL) {
            LOG_TRACE(ERROR, "EMM-MAIN  - Failed to get EMM data pathname");
        } else {
            /* Get EMM data stored in the non-volatile memory device */
            int rc = memory_read(path, &_emm_data.nvdata, sizeof(emm_nvdata_t));
            if (rc != RETURNok) {
                LOG_TRACE(ERROR, "EMM-MAIN  - Failed to read %s", path);
            } else {
                /* Check the IMSI */
                LOG_TRACE(INFO, "EMM-MAIN  - EMM data successfully read");
                _emm_data.imsi = &_usim_data.imsi;
                int imsi_ok = _emm_main_imsi_cmp(&_emm_data.nvdata.imsi,
                                                 &_usim_data.imsi);
                if (!imsi_ok) {
                    LOG_TRACE(WARNING, "EMM-MAIN  - IMSI checking failed nvram: "
                              "%02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x, "
                              "usim: %02x.%02x.%02x.%02x.%02x.%02x.%02x.%02x",
                              _emm_data.nvdata.imsi.u.value[0],
                              _emm_data.nvdata.imsi.u.value[1],
                              _emm_data.nvdata.imsi.u.value[2],
                              _emm_data.nvdata.imsi.u.value[3],
                              _emm_data.nvdata.imsi.u.value[4],
                              _emm_data.nvdata.imsi.u.value[5],
                              _emm_data.nvdata.imsi.u.value[6],
                              _emm_data.nvdata.imsi.u.value[7],
                              _usim_data.imsi.u.value[0],
                              _usim_data.imsi.u.value[1],
                              _usim_data.imsi.u.value[2],
                              _usim_data.imsi.u.value[3],
                              _usim_data.imsi.u.value[4],
                              _usim_data.imsi.u.value[5],
                              _usim_data.imsi.u.value[6], _usim_data.imsi.u.value[7]);
                    memset(&_emm_data.nvdata.rplmn, 0xFF, sizeof(plmn_t));
                    _emm_data.nvdata.eplmn.n_plmns = 0;
                }
            }
            free(path);
        }
Cedric Roux's avatar
 
Cedric Roux committed
335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376
    }

    /*
     * Initialize EMM timers
     */
    T3410.id = NAS_TIMER_INACTIVE_ID;
    T3410.sec = T3410_DEFAULT_VALUE;
    T3411.id = NAS_TIMER_INACTIVE_ID;
    T3411.sec = T3411_DEFAULT_VALUE;
    T3402.id = NAS_TIMER_INACTIVE_ID;
    T3402.sec = T3402_DEFAULT_VALUE;
    T3416.id = NAS_TIMER_INACTIVE_ID;
    T3416.sec = T3416_DEFAULT_VALUE;
    T3417.id = NAS_TIMER_INACTIVE_ID;
    T3417.sec = T3417_DEFAULT_VALUE;
    T3418.id = NAS_TIMER_INACTIVE_ID;
    T3418.sec = T3418_DEFAULT_VALUE;
    T3420.id = NAS_TIMER_INACTIVE_ID;
    T3420.sec = T3420_DEFAULT_VALUE;
    T3421.id = NAS_TIMER_INACTIVE_ID;
    T3421.sec = T3421_DEFAULT_VALUE;
    T3423.id = NAS_TIMER_INACTIVE_ID;
    T3423.sec = T3423_DEFAULT_VALUE;
    T3430.id = NAS_TIMER_INACTIVE_ID;
    T3430.sec = T3430_DEFAULT_VALUE;

    /*
     * Initialize the user notification callback
     */
    _emm_main_user_callback = *cb;

    /*
     * Initialize EMM internal data used for UE in idle mode
     */
    IdleMode_initialize(&_emm_main_callback);

    LOG_FUNC_OUT;
}
#endif
#ifdef NAS_MME
/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
377
 ** Name:    emm_main_initialize()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
378 379 380
 **                                                                        **
 ** Description: Initializes EMM internal data                             **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
381 382
 ** Inputs:  None                                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
383
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
384 385 386
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
387 388
 **                                                                        **
 ***************************************************************************/
389 390 391
#if defined(EPC_BUILD)
void emm_main_initialize(mme_config_t *mme_config_p)
#else
Cedric Roux's avatar
 
Cedric Roux committed
392
void emm_main_initialize(void)
393
#endif
Cedric Roux's avatar
 
Cedric Roux committed
394 395 396 397
{
    LOG_FUNC_IN;

    /* Retreive MME supported configuration data */
398 399 400 401 402 403
#if defined(EPC_BUILD)
    if (mme_api_get_emm_config(&_emm_data.conf, mme_config_p) != RETURNok)
#else
    if (mme_api_get_emm_config(&_emm_data.conf) != RETURNok)
#endif
    {
Cedric Roux's avatar
Cedric Roux committed
404
        LOG_TRACE(ERROR, "EMM-MAIN  - Failed to get MME configuration data");
Cedric Roux's avatar
 
Cedric Roux committed
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426
    }

#if defined(EPC_BUILD)
    RB_INIT(&_emm_data.ctx_map);
#endif

    /*
     * Initialize EMM timers
     */
    T3450.id = NAS_TIMER_INACTIVE_ID;
    T3450.sec = T3450_DEFAULT_VALUE;
    T3460.id = NAS_TIMER_INACTIVE_ID;
    T3460.sec = T3460_DEFAULT_VALUE;
    T3470.id = NAS_TIMER_INACTIVE_ID;
    T3470.sec = T3470_DEFAULT_VALUE;

    LOG_FUNC_OUT;
}
#endif

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
427
 ** Name:    emm_main_cleanup()                                        **
Cedric Roux's avatar
 
Cedric Roux committed
428 429 430
 **                                                                        **
 ** Description: Performs the EPS Mobility Management clean up procedure   **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
431 432
 ** Inputs:  None                                                      **
 **          Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
433
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
434 435 436
 ** Outputs:     None                                                      **
 **          Return:    None                                       **
 **          Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
437 438 439 440 441 442 443 444 445
 **                                                                        **
 ***************************************************************************/
void emm_main_cleanup(void)
{
    LOG_FUNC_IN;

#ifdef NAS_UE

    if (_emm_data.usim_is_valid) {
Cedric Roux's avatar
Cedric Roux committed
446 447 448
        /*
         * TODO: Update USIM application data
         */
Cedric Roux's avatar
 
Cedric Roux committed
449
#if 0
Cedric Roux's avatar
Cedric Roux committed
450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483
        int i;
        /* Update the list of Forbidden PLMNs */
        for (i=0; (i < _emm_data.fplmn.n_plmns) && (i < USIM_FPLMN_MAX); i++) {
            _usim_data.fplmn[i] = _emm_data.fplmn.plmn[i];
        }
        /* Update the list of Equivalent HPLMNs */
        for (i=0; (i < _emm_data.ehplmn.n_plmns) && (i < USIM_EHPLMN_MAX); i++) {
            _usim_data.ehplmn[i] = _emm_data.ehplmn.plmn[i];
        }
        /* Update the GUTI */
        if (_emm_data.guti) {
            _usim_data.epsloci.guti = *(_emm_data.guti);
        }
        /* Update the last visited registered TAI */
        if (_emm_data.tai) {
            _usim_data.epsloci.tai = *(_emm_data.tai);
        }
        /* Update the EPS location information */
        _usim_data.epsloci.status = _emm_data.status;

        if (_emm_data.security && (_emm_data.security->type == EMM_KSI_NATIVE)) {
            /* TODO: Update the EPS security context parameters from the full
             * native EPS security context */
        }

        /*
         * Store USIM application data
         * - List of forbidden PLMNs
         */
        if ( usim_api_write(&_usim_data) != RETURNok ) {
            /* The USIM application may not be present or not valid */
            LOG_TRACE(WARNING, "EMM-MAIN  - "
                      "Failed to write USIM application data");
        }
Cedric Roux's avatar
 
Cedric Roux committed
484 485 486 487 488 489 490 491
#endif
    }

    /*
     * Store EMM data into the UE's non-volatile memory
     * - Registered PLMN
     * - List of equivalent PLMNs
     */
Cedric Roux's avatar
Cedric Roux committed
492
    char *path = memory_get_path(EMM_NVRAM_DIRNAME, EMM_NVRAM_FILENAME);
Cedric Roux's avatar
 
Cedric Roux committed
493
    if (path == NULL) {
Cedric Roux's avatar
Cedric Roux committed
494 495 496 497 498 499
        LOG_TRACE(ERROR, "EMM-MAIN  - Failed to get EMM data pathname");
    } else {
        int rc = memory_write(path, &_emm_data.nvdata, sizeof(emm_nvdata_t));
        if (rc != RETURNok) {
            LOG_TRACE(ERROR, "EMM-MAIN  - Failed to write %s", path);
        }
Cedric Roux's avatar
 
Cedric Roux committed
500 501 502 503
    }

    /* Release dynamically allocated memory */
    if (_emm_data.imei) {
Cedric Roux's avatar
Cedric Roux committed
504 505
        free(_emm_data.imei);
        _emm_data.imei = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
506 507
    }
    if (_emm_data.security) {
Cedric Roux's avatar
Cedric Roux committed
508 509 510 511 512 513 514 515 516 517 518 519
        emm_security_context_t *security = _emm_data.security;
        if (security->kasme.value) {
            free(security->kasme.value);
        }
        if (security->knas_enc.value) {
            free(security->knas_enc.value);
        }
        if (security->knas_int.value) {
            free(security->knas_int.value);
        }
        free(_emm_data.security);
        _emm_data.security = NULL;
Cedric Roux's avatar
 
Cedric Roux committed
520 521 522 523 524 525 526 527 528 529
    }

#endif // NAS_UE

    LOG_FUNC_OUT;
}

#ifdef NAS_UE
/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
530
 ** Name:    emm_main_get_imsi()                                       **
Cedric Roux's avatar
 
Cedric Roux committed
531 532 533
 **                                                                        **
 ** Description: Get the International Mobile Subscriber Identity number   **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
534 535
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
536
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
537 538 539
 ** Outputs:     None                                                      **
 **      Return:    Pointer to the IMSI                        **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
540 541
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
542
const imsi_t *emm_main_get_imsi(void)
Cedric Roux's avatar
 
Cedric Roux committed
543 544 545 546 547 548 549
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (&_emm_data.nvdata.imsi);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
550
 ** Name:    emm_main_get_msisdn()                                     **
Cedric Roux's avatar
 
Cedric Roux committed
551 552 553
 **                                                                        **
 ** Description: Get the Mobile Subscriber Dialing Number from the USIM    **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
554 555
 ** Inputs:  None                                                      **
 **      Others:    _usim_data                                 **
Cedric Roux's avatar
 
Cedric Roux committed
556
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
557 558 559
 ** Outputs:     None                                                      **
 **      Return:    Pointer to the subscriber dialing number   **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
560 561
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
562
const msisdn_t *emm_main_get_msisdn(void)
Cedric Roux's avatar
 
Cedric Roux committed
563 564 565 566 567 568 569
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (&_usim_data.msisdn.number);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
570
 ** Name:    emm_main_set_plmn_selection_mode()                        **
Cedric Roux's avatar
 
Cedric Roux committed
571 572
 **                                                                        **
 ** Description: Set the network selection mode of operation to the given  **
Cedric Roux's avatar
Cedric Roux committed
573 574
 **      mode and update the manually selected network selection   **
 **      data                                                      **
Cedric Roux's avatar
 
Cedric Roux committed
575
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
576 577 578 579 580 581 582
 ** Inputs:  mode:      The specified network selection mode of    **
 **             operation                                  **
 **      format:    The representation format of the PLMN      **
 **             identifier                                 **
 **      plmn:      Identifier of the selected PLMN            **
 **      rat:       The selected Radio Access Techonology      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
583
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
584 585 586
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
587 588 589
 **                                                                        **
 ***************************************************************************/
int emm_main_set_plmn_selection_mode(int mode, int format,
Cedric Roux's avatar
Cedric Roux committed
590
                                     const network_plmn_t *plmn, int rat)
Cedric Roux's avatar
 
Cedric Roux committed
591 592 593 594 595 596
{
    LOG_FUNC_IN;

    int index;

    LOG_TRACE(INFO, "EMM-MAIN  - PLMN selection: mode=%d, format=%d, plmn=%s, "
Cedric Roux's avatar
Cedric Roux committed
597
              "rat=%d", mode, format, (const char *)&plmn->id, rat);
Cedric Roux's avatar
 
Cedric Roux committed
598 599 600 601

    _emm_data.plmn_mode = mode;

    if (mode != EMM_DATA_PLMN_AUTO) {
Cedric Roux's avatar
Cedric Roux committed
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618
        /* Get the index of the PLMN in the list of available PLMNs */
        index = _emm_main_get_plmn_index((const char *)&plmn->id, format);
        if (index < 0) {
            LOG_TRACE(WARNING, "EMM-MAIN  - PLMN %s not available",
                      (const char *)&plmn->id);
        } else {
            /* Update the manually selected network selection data */
            _emm_data.plmn_index = index;
            _emm_data.plmn_rat = rat;
        }
    } else {
        /*
         * Get the index of the last PLMN the UE already tried to automatically
         * register to when switched on; the equivalent PLMNs list shall not be
         * applied to the user reselection in Automatic Network Selection Mode.
         */
        index = IdleMode_get_hplmn_index();
Cedric Roux's avatar
 
Cedric Roux committed
619 620 621 622 623 624 625
    }

    LOG_FUNC_RETURN (index);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
626
 ** Name:    emm_main_get_plmn_selection_mode()                        **
Cedric Roux's avatar
 
Cedric Roux committed
627 628
 **                                                                        **
 ** Description: Get the current value of the network selection mode of    **
Cedric Roux's avatar
Cedric Roux committed
629
 **      operation                                                 **
Cedric Roux's avatar
 
Cedric Roux committed
630
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
631 632
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
633
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
634 635 636
 ** Outputs:     None                                                      **
 **      Return:    The value of the network selection mode    **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
637 638 639 640 641 642 643 644 645 646
 **                                                                        **
 ***************************************************************************/
int emm_main_get_plmn_selection_mode(void)
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (_emm_data.plmn_mode);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
647
 ** Name:    emm_main_get_plmn_list()                                  **
Cedric Roux's avatar
 
Cedric Roux committed
648 649 650
 **                                                                        **
 ** Description: Get the list of available PLMNs                           **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
651 652
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
653
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
654 655 656
 ** Outputs:     plist:     Pointer to the list of available PLMNs     **
 **      Return:    The size of the list in bytes              **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
657 658
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
659
int emm_main_get_plmn_list(const char **plist)
Cedric Roux's avatar
 
Cedric Roux committed
660 661 662 663 664 665 666 667 668 669 670
{
    LOG_FUNC_IN;

    int size = IdleMode_update_plmn_list(0);
    *plist = _emm_data.plist.buffer;

    LOG_FUNC_RETURN (size);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
671
 ** Name:    emm_main_get_selected_plmn()                              **
Cedric Roux's avatar
 
Cedric Roux committed
672 673 674
 **                                                                        **
 ** Description: Get the identifier of the currently selected PLMN         **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
675 676 677
 ** Inputs:  format:    The requested format of the string repre-  **
 **             sentation of the PLMN identifier           **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
678
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
679 680 681 682 683
 ** Outputs:     plmn:      The selected PLMN identifier coded in the  **
 **             requested format                           **
 **      Return:    A pointer to the string representation of  **
 **             the selected PLMN                          **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
684 685
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
686
const char *emm_main_get_selected_plmn(network_plmn_t *plmn, int format)
Cedric Roux's avatar
 
Cedric Roux committed
687 688 689 690 691 692 693 694 695
{
    LOG_FUNC_IN;

    size_t size = 0;
    /*
     * Get the identifier of the selected PLMN in the list of available PLMNs
     */
    int index = IdleMode_get_splmn_index();
    if ( !(index < 0) ) {
Cedric Roux's avatar
Cedric Roux committed
696 697 698 699 700
        const char *name = _emm_main_get_plmn(&_emm_data.splmn, index,
                                              format, &size);
        if (size > 0) {
            LOG_FUNC_RETURN ((char *) memcpy(&plmn->id, name, size));
        }
Cedric Roux's avatar
 
Cedric Roux committed
701 702 703 704 705 706 707
    }

    LOG_FUNC_RETURN (NULL);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
708
 ** Name:    emm_main_get_registered_plmn()                            **
Cedric Roux's avatar
 
Cedric Roux committed
709 710 711
 **                                                                        **
 ** Description: Get the identifier of the currently registered PLMN       **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
712 713 714
 ** Inputs:  format:    The requested format of the string repre-  **
 **             sentation of the PLMN identifier           **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
715
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
716 717 718 719 720
 ** Outputs:     plmn:      The registered PLMN identifier coded in    **
 **             the requested format                       **
 **      Return:    A pointer to the string representation of  **
 **             the registered PLMN                        **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
721 722
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
723
const char *emm_main_get_registered_plmn(network_plmn_t *plmn, int format)
Cedric Roux's avatar
 
Cedric Roux committed
724 725 726 727 728 729 730 731 732 733
{
    LOG_FUNC_IN;

    size_t size = 0;

    /*
     * Get the identifier of the registered PLMN in the list of available PLMNs
     */
    int index = IdleMode_get_rplmn_index();
    if ( !(index < 0) ) {
Cedric Roux's avatar
Cedric Roux committed
734 735 736 737 738
        const char *name = _emm_main_get_plmn(&_emm_data.nvdata.rplmn,
                                              index, format, &size);
        if (size > 0) {
            LOG_FUNC_RETURN ((char *) memcpy(&plmn->id, name, size));
        }
Cedric Roux's avatar
 
Cedric Roux committed
739 740 741 742 743 744 745
    }

    LOG_FUNC_RETURN (NULL);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
746
 ** Name:    emm_main_get_plmn_status()                                **
Cedric Roux's avatar
 
Cedric Roux committed
747 748
 **                                                                        **
 ** Description: Get the value of the network registration status which    **
Cedric Roux's avatar
Cedric Roux committed
749 750
 **      shows whether the network has currently indicated the     **
 **      registration of the UE                                    **
Cedric Roux's avatar
 
Cedric Roux committed
751
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
752 753
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
754
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
755 756 757
 ** Outputs:     None                                                      **
 **      Return:    The current network registration status    **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
758 759 760 761 762 763 764 765 766 767
 **                                                                        **
 ***************************************************************************/
Stat_t emm_main_get_plmn_status(void)
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (_emm_data.stat);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
768
 ** Name:    emm_main_get_plmn_tac()                                   **
Cedric Roux's avatar
 
Cedric Roux committed
769 770
 **                                                                        **
 ** Description: Get the code of the Tracking area the registered PLMN     **
Cedric Roux's avatar
Cedric Roux committed
771
 **      belongs to                                                **
Cedric Roux's avatar
 
Cedric Roux committed
772
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
773 774
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
775
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
776 777 778
 ** Outputs:     None                                                      **
 **      Return:    The Location/Tracking area code            **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
779 780 781 782 783 784 785 786 787 788
 **                                                                        **
 ***************************************************************************/
tac_t emm_main_get_plmn_tac(void)
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (_emm_data.tac);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
789
 ** Name:    emm_main_get_plmn_ci()                                    **
Cedric Roux's avatar
 
Cedric Roux committed
790 791 792
 **                                                                        **
 ** Description: Get the identifier of the serving cell                    **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
793 794
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
795
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
796 797 798
 ** Outputs:     None                                                      **
 **      Return:    The serving cell identifier                **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
799 800 801 802 803 804 805 806 807 808
 **                                                                        **
 ***************************************************************************/
ci_t emm_main_get_plmn_ci(void)
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (_emm_data.ci);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
809
 ** Name:    emm_main_get_plmn_rat()                                   **
Cedric Roux's avatar
 
Cedric Roux committed
810 811
 **                                                                        **
 ** Description: Get the value of the Radio Access Technology of the ser-  **
Cedric Roux's avatar
Cedric Roux committed
812
 **      ving cell                                                 **
Cedric Roux's avatar
 
Cedric Roux committed
813
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
814 815
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
816
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
817 818 819 820
 ** Outputs:     None                                                      **
 **      Return:    The value of the Radio Access Technology   **
 **             of the serving cell                        **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
821 822 823 824 825 826 827 828 829 830
 **                                                                        **
 ***************************************************************************/
AcT_t emm_main_get_plmn_rat(void)
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (_emm_data.rat);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
831
 ** Name:    emm_main_is_attached()                                    **
Cedric Roux's avatar
 
Cedric Roux committed
832 833
 **                                                                        **
 ** Description: Indicates whether the UE is currently attached to the     **
Cedric Roux's avatar
Cedric Roux committed
834
 **      network for EPS services or emergency service only        **
Cedric Roux's avatar
 
Cedric Roux committed
835
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
836 837
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
838
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
839 840 841 842
 ** Outputs:     None                                                      **
 **      Return:    TRUE if the UE is currently attached to    **
 **             the network; FALSE otherwise.              **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
843 844 845 846 847 848 849 850 851 852
 **                                                                        **
 ***************************************************************************/
int emm_main_is_attached(void)
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (_emm_data.is_attached);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
853
 ** Name:    emm_main_get_plmn_rat()                                   **
Cedric Roux's avatar
 
Cedric Roux committed
854 855
 **                                                                        **
 ** Description: Indicates whether the UE is currently attached to the     **
Cedric Roux's avatar
Cedric Roux committed
856
 **      network for emergency bearer services                     **
Cedric Roux's avatar
 
Cedric Roux committed
857
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
858 859
 ** Inputs:  None                                                      **
 **      Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
860
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
861 862 863 864 865
 ** Outputs:     None                                                      **
 **      Return:    TRUE if the UE is currently attached or is **
 **             attempting to attach to the network for    **
 **             emergency bearer services; FALSE otherwise **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
 **                                                                        **
 ***************************************************************************/
int emm_main_is_emergency(void)
{
    LOG_FUNC_IN;
    LOG_FUNC_RETURN (_emm_data.is_attached && _emm_data.is_emergency);
}
#endif // NAS_UE

/****************************************************************************/
/*********************  L O C A L    F U N C T I O N S  *********************/
/****************************************************************************/

#ifdef NAS_UE
/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
882
 ** Name:    _emm_main_callback()                                      **
Cedric Roux's avatar
 
Cedric Roux committed
883 884
 **                                                                        **
 ** Description: Forwards the network indication to the upper control la-  **
Cedric Roux's avatar
Cedric Roux committed
885 886
 **      yer (user API) to notify that network registration and/or **
 **      location information has changed.                         **
Cedric Roux's avatar
 
Cedric Roux committed
887
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
888 889 890 891 892
 ** Inputs:  size:      Size in byte of the list of operators      **
 **             present in the network. The list has to be **
 **             displayed to the user application when     **
 **             size > 0.                                  **
 **          Others:    _emm_data                                  **
Cedric Roux's avatar
 
Cedric Roux committed
893
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
894 895 896
 ** Outputs:     None                                                      **
 **      Return:    RETURNok, RETURNerror                      **
 **          Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
897 898 899 900 901 902 903 904
 **                                                                        **
 ***************************************************************************/
static int _emm_main_callback(int size)
{
    LOG_FUNC_IN;

    /* Forward the notification to the user API */
    int rc = (*_emm_main_user_callback)(_emm_data.stat, _emm_data.tac,
Cedric Roux's avatar
Cedric Roux committed
905 906
                                        _emm_data.ci, _emm_data.rat,
                                        _emm_data.plist.buffer, size);
Cedric Roux's avatar
 
Cedric Roux committed
907 908 909 910 911 912

    LOG_FUNC_RETURN (rc);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
913
 ** Name:    _emm_main_get_imei()                                      **
Cedric Roux's avatar
 
Cedric Roux committed
914 915
 **                                                                        **
 ** Description: Returns the International Mobile Equipment Identity con-  **
Cedric Roux's avatar
Cedric Roux committed
916
 **      tained in the given string representation                 **
Cedric Roux's avatar
 
Cedric Roux committed
917
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
918 919
 ** Inputs:  imei:      The string representation of the IMEI      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
920
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
921 922 923
 ** Outputs:     imei:      The IMEI of the UE                         **
 **      Return:    The number of digits in the IMEI           **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
924 925
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
926
static int _emm_main_get_imei(imei_t *imei, const char *imei_str)
Cedric Roux's avatar
 
Cedric Roux committed
927 928 929
{
    int len = strlen(imei_str);
    if (len % 2) {
Cedric Roux's avatar
Cedric Roux committed
930
        imei->u.num.parity = ODD_PARITY;
Cedric Roux's avatar
 
Cedric Roux committed
931
    } else {
Cedric Roux's avatar
Cedric Roux committed
932
        imei->u.num.parity = EVEN_PARITY;
Cedric Roux's avatar
 
Cedric Roux committed
933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953
    }
    imei->u.num.digit1 = imei_str[0] - '0';
    imei->u.num.digit2 = imei_str[1] - '0';
    imei->u.num.digit3 = imei_str[2] - '0';
    imei->u.num.digit4 = imei_str[3] - '0';
    imei->u.num.digit5 = imei_str[4] - '0';
    imei->u.num.digit6 = imei_str[5] - '0';
    imei->u.num.digit7 = imei_str[6] - '0';
    imei->u.num.digit8 = imei_str[7] - '0';
    imei->u.num.digit9 = imei_str[8] - '0';
    imei->u.num.digit10 = imei_str[9] - '0';
    imei->u.num.digit11 = imei_str[10] - '0';
    imei->u.num.digit12 = imei_str[11] - '0';
    imei->u.num.digit13 = imei_str[12] - '0';
    imei->u.num.digit14 = imei_str[13] - '0';
    imei->u.num.digit15 = imei_str[14] - '0';
    return (len);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
954
 ** Name:    _emm_main_imsi_cmp()                                      **
Cedric Roux's avatar
 
Cedric Roux committed
955 956 957
 **                                                                        **
 ** Description: Compares two International Mobile Subscriber Identifiers  **
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
958 959 960
 ** Inputs:  imsi1:     The first IMSI                             **
 **      imsi2:     The second IMSI to compare to              **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
961
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
962 963 964 965
 ** Outputs:     None                                                      **
 **      Return:    TRUE if the first IMSI is found to match   **
 **             the second; FALSE otherwise.               **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
966 967
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
968
static int _emm_main_imsi_cmp(imsi_t *imsi1, imsi_t *imsi2)
Cedric Roux's avatar
 
Cedric Roux committed
969 970
{
    if (imsi1->length != imsi2->length) {
Cedric Roux's avatar
Cedric Roux committed
971
        return FALSE;
Cedric Roux's avatar
 
Cedric Roux committed
972 973
    }
    for (int i = 0; i < imsi1->length; i++) {
Cedric Roux's avatar
Cedric Roux committed
974 975 976
        if (imsi1->u.value[i] != imsi2->u.value[i]) {
            return FALSE;
        }
Cedric Roux's avatar
 
Cedric Roux committed
977 978 979 980 981 982
    }
    return TRUE;
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
983
 ** Name:    _emm_main_get_plmn()                                      **
Cedric Roux's avatar
 
Cedric Roux committed
984 985
 **                                                                        **
 ** Description: Get the identifier of the PLMN at the given index in the  **
Cedric Roux's avatar
Cedric Roux committed
986
 **      list of available PLMNs.                                  **
Cedric Roux's avatar
 
Cedric Roux committed
987
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
988 989 990 991 992
 ** Inputs:  plmn:      The PLMN to search for                     **
 **      index:     The index of the PLMN in the list of PLMNs **
 **      format:    The requested representation format of the **
 **             PLMN identifier                            **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
993
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
994 995 996 997
 ** Outputs:     size:      The size in bytes of the PLMN identifier   **
 **             coded in the requested format              **
 **      Return:    A pointer to the identifier of the PLMN    **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
998 999
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1000 1001
static const char *_emm_main_get_plmn(const plmn_t *plmn, int index,
                                      int format, size_t *size)
Cedric Roux's avatar
 
Cedric Roux committed
1002 1003
{
    if ( PLMN_IS_VALID(*plmn) ) {
Cedric Roux's avatar
Cedric Roux committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025
        switch (format) {
            case NET_FORMAT_LONG:
                /* Get the long alpha-numeric representation of the PLMN */
                return IdleMode_get_plmn_fullname(plmn, index, size);
                break;

            case NET_FORMAT_SHORT:
                /* Get the short alpha-numeric representation of the PLMN */
                return IdleMode_get_plmn_shortname(plmn, index, size);
                break;

            case NET_FORMAT_NUM:
                /* Get the numeric representation of the PLMN */
                return IdleMode_get_plmn_id(plmn, index, size);
                break;

            default:
                LOG_TRACE(WARNING, "EMM-MAIN  - Format is not valid (%d)",
                          format);
                *size = 0;
                break;
        }
Cedric Roux's avatar
 
Cedric Roux committed
1026 1027 1028 1029 1030 1031
    }
    return (NULL);
}

/****************************************************************************
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1032
 ** Name:    _emm_main_get_plmn_index()                                **
Cedric Roux's avatar
 
Cedric Roux committed
1033 1034
 **                                                                        **
 ** Description: Get the index of the given PLMN in the ordered list of    **
Cedric Roux's avatar
Cedric Roux committed
1035
 **      available PLMNs                                           **
Cedric Roux's avatar
 
Cedric Roux committed
1036
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1037 1038 1039 1040
 ** Inputs:  plmn:      Identifier of the PLMN                     **
 **      format:    The representation format of the PLMN      **
 **             identifier                                 **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1041
 **                                                                        **
Cedric Roux's avatar
Cedric Roux committed
1042 1043 1044 1045 1046
 ** Outputs:     None                                                      **
 **      Return:    The index of the selected PLMN in the list **
 **             of available PLMNs; -1 if the PLMN is not  **
 **             found                                      **
 **      Others:    None                                       **
Cedric Roux's avatar
 
Cedric Roux committed
1047 1048
 **                                                                        **
 ***************************************************************************/
Cedric Roux's avatar
Cedric Roux committed
1049
static int _emm_main_get_plmn_index(const char *plmn, int format)
Cedric Roux's avatar
 
Cedric Roux committed
1050 1051
{
    int index = -1;
Cedric Roux's avatar
Cedric Roux committed
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070
    switch (format) {
        case NET_FORMAT_LONG:
            /* Get the index of the long alpha-numeric PLMN identifier */
            index = IdleMode_get_plmn_fullname_index(plmn);
            break;

        case NET_FORMAT_SHORT:
            /* Get the index of the short alpha-numeric PLMN identifier */
            index = IdleMode_get_plmn_shortname_index(plmn);
            break;

        case NET_FORMAT_NUM:
            /* Get the index of the numeric PLMN identifier */
            index = IdleMode_get_plmn_id_index(plmn);
            break;

        default:
            LOG_TRACE(WARNING, "EMM-MAIN  - Format is not valid (%d)", format);
            break;
Cedric Roux's avatar
 
Cedric Roux committed
1071 1072 1073 1074 1075
    }
    return (index);
}
#endif // NAS_UE