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

gauthier's avatar
 
gauthier committed
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*****************************************************************************
Source      nas_user.c

Version     0.1

Date        2012/03/09

Product     NAS stack

Subsystem   NAS main process

Author      Frederic Maurel

Description NAS procedure functions triggered by the user

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


40
#include "user_defs.h"
gauthier's avatar
 
gauthier committed
41 42 43 44 45 46 47 48
#include "nas_log.h"
#include "memory.h"

#include "at_command.h"
#include "at_response.h"
#include "at_error.h"
#include "user_indication.h"
#include "nas_proc.h"
49
#include "nas_user.h"
50
#include "utils.h"
gauthier's avatar
 
gauthier committed
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68
#include "user_api.h"

#include <string.h> // memset, strncpy, strncmp
#include <stdlib.h> // free

/****************************************************************************/
/****************  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  *******************/
/****************************************************************************/

/*
 * ---------------------------------------------------------------------
 *  Functions executed upon receiving AT command from the user
 * ---------------------------------------------------------------------
 */
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89
static int _nas_user_proc_cgsn    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgmi    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgmm    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgmr    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cimi    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cfun    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cpin    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_csq     (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cesq    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cops    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgatt   (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_creg    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgreg   (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cereg   (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgdcont (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgact   (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cmee    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_clck    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cgpaddr (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_cnum    (nas_user_t *user, const at_command_t *data);
static int _nas_user_proc_clac    (nas_user_t *user, const at_command_t *data);
gauthier's avatar
 
gauthier committed
90 91

/* NAS procedures applicable to AT commands */
92
typedef int (*_nas_user_procedure_t) (nas_user_t *, const at_command_t *);
gauthier's avatar
 
gauthier committed
93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
static const _nas_user_procedure_t _nas_user_procedure[AT_COMMAND_ID_MAX] = {
    NULL,
    _nas_user_proc_cgsn, /* CGSN    */
    _nas_user_proc_cgmi, /* CGMI    */
    _nas_user_proc_cgmm, /* CGMM    */
    _nas_user_proc_cgmr, /* CGMR    */
    _nas_user_proc_cimi, /* CIMI    */
    _nas_user_proc_cfun, /* CFUN    */
    _nas_user_proc_cpin, /* CPIN    */
    _nas_user_proc_csq, /* CSQ     */
    _nas_user_proc_cesq, /* CESQ    */
    _nas_user_proc_clac, /* CLAC    */
    _nas_user_proc_cmee, /* CMEE    */
    _nas_user_proc_cnum, /* CNUM    */
    _nas_user_proc_clck, /* CLCK    */
    _nas_user_proc_cops, /* COPS    */
    _nas_user_proc_creg, /* CREG    */
    _nas_user_proc_cgatt, /* CGATT   */
    _nas_user_proc_cgreg, /* CGREG   */
    _nas_user_proc_cereg, /* CEREG   */
    _nas_user_proc_cgdcont, /* CGDCONT */
    _nas_user_proc_cgact, /* CGACT   */
    _nas_user_proc_cgpaddr, /* CGPADDR */
gauthier's avatar
 
gauthier committed
117 118 119 120 121 122 123
};

/*
 * ---------------------------------------------------------------------
 *              Local UE context
 * ---------------------------------------------------------------------
 */
124

125
static const char *const _nas_user_sim_status_str[] = {"READY", "SIM PIN", "SIM PUK", "PH-SIM PIN"};
gauthier's avatar
 
gauthier committed
126 127 128 129 130

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

131 132 133 134 135 136
void _nas_user_context_initialize(nas_user_context_t *nas_user_context, const char *version) {
  nas_user_context->version = version;
  nas_user_context->sim_status = NAS_USER_SIM_PIN;
  nas_user_context->fun = AT_CFUN_FUN_DEFAULT;
}

gauthier's avatar
 
gauthier committed
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_initialize()                                     **
 **                                                                        **
 ** Description: Initializes user internal data                            **
 **                                                                        **
 ** Inputs:  emm_cb:    Mobility Management indication callback    **
 **      esm_cb:    Session Management indication callback     **
 **          version:   Firmware version                           **
 **          Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    None                                       **
 **                                                                        **
 ***************************************************************************/
152
void nas_user_initialize(nas_user_t *user, emm_indication_callback_t emm_cb,
gauthier's avatar
 
gauthier committed
153 154 155 156
                         esm_indication_callback_t esm_cb, const char *version)
{
  LOG_FUNC_IN;

157
  user->nas_user_nvdata = calloc_or_fail(1, sizeof(user_nvdata_t));
158

gauthier's avatar
 
gauthier committed
159
  /* Get UE data stored in the non-volatile memory device */
160
  int rc = memory_read(user->user_nvdata_store, user->nas_user_nvdata, sizeof(user_nvdata_t));
161
  if (rc != RETURNok) {
162
    LOG_TRACE(ERROR, "USR-MAIN  - Failed to read non volatile memory");
163
    abort();
gauthier's avatar
 
gauthier committed
164 165
  }

166
  user->nas_user_context = calloc_or_fail(1, sizeof(nas_user_context_t));
167
  _nas_user_context_initialize(user->nas_user_context, version);
gauthier's avatar
 
gauthier committed
168 169

  /* Initialize the internal NAS processing data */
170
  nas_proc_initialize(user, emm_cb, esm_cb, user->nas_user_nvdata->IMEI);
gauthier's avatar
 
gauthier committed
171 172 173 174 175 176 177 178 179 180 181 182 183 184

  LOG_FUNC_OUT;
}

/****************************************************************************
 **                                                                        **
 ** Name:        nas_user_receive_and_process()                            **
 **                                                                        **
 ** Description: Receives and process messages from user application       **
 **                                                                        **
 ** Inputs:      fd:            File descriptor of the connection endpoint **
 **                             from which data have been received         **
 **              Others:        None                                       **
 **                                                                        **
185
 ** Outputs:     Return:        false, true                                **
gauthier's avatar
 
gauthier committed
186 187
 **                                                                        **
 ***************************************************************************/
188
bool nas_user_receive_and_process(nas_user_t *user, char *message)
gauthier's avatar
 
gauthier committed
189 190 191 192 193 194 195
{
  LOG_FUNC_IN;

  int ret_code;
  int nb_command;
  int bytes;
  int i;
196
  user_api_id_t *user_api_id = user->user_api_id;
gauthier's avatar
 
gauthier committed
197 198 199

  if (message != NULL) {
    /* Set the message in receive buffer (Use to simulate reception of data from UserProcess) */
200
    bytes = user_api_set_data(user_api_id, message);
gauthier's avatar
 
gauthier committed
201 202
  } else {
    /* Read the user data message */
203
    bytes = user_api_read_data (user_api_id);
gauthier's avatar
 
gauthier committed
204 205 206 207 208 209

    if (bytes == RETURNerror) {
      /* Failed to read data from the user application layer;
       * exit from the receiving loop */
      LOG_TRACE (ERROR, "UE-MAIN   - "
                 "Failed to read data from the user application layer");
210
      LOG_FUNC_RETURN(true);
gauthier's avatar
 
gauthier committed
211 212 213 214 215
    }
  }

  if (bytes == 0) {
    /* A signal was caught before any data were available */
216
    LOG_FUNC_RETURN(false);
gauthier's avatar
 
gauthier committed
217 218 219
  }

  /* Decode the user data message */
220
  nb_command = user_api_decode_data (user_api_id, user->user_at_commands, bytes);
gauthier's avatar
 
gauthier committed
221 222 223

  for (i = 0; i < nb_command; i++) {
    /* Get the user data to be processed */
224
    const void *data = user_api_get_data (user->user_at_commands, i);
gauthier's avatar
 
gauthier committed
225 226 227 228 229 230 231 232 233 234 235

    if (data == NULL) {
      /* Failed to get user data at the given index;
       * go ahead and process the next user data */
      LOG_TRACE (ERROR, "UE-MAIN   - "
                 "Failed to get user data at index %d",
                 i);
      continue;
    }

    /* Process the user data message */
236
    ret_code = nas_user_process_data (user, data);
gauthier's avatar
 
gauthier committed
237 238 239 240 241 242 243 244 245 246 247 248 249

    if (ret_code != RETURNok) {
      /* The user data message has not been successfully
       * processed; cause code will be encoded and sent back
       * to the user */
      LOG_TRACE
      (WARNING, "UE-MAIN   - "
       "The user procedure call failed");
    }

    /* Send response to UserProcess (If not in simulated reception) */
    if (message == NULL) {
      /* Encode the user data message */
250
      bytes = user_api_encode_data (user->user_api_id, nas_user_get_data (user), i == nb_command - 1);
gauthier's avatar
 
gauthier committed
251 252 253 254 255 256 257 258

      if (bytes == RETURNerror) {
        /* Failed to encode the user data message;
         * go ahead and process the next user data */
        continue;
      }

      /* Send the data message to the user */
259
      bytes = user_api_send_data (user_api_id, bytes);
gauthier's avatar
 
gauthier committed
260 261 262 263 264 265

      if (bytes == RETURNerror) {
        /* Failed to send data to the user application layer;
         * exit from the receiving loop */
        LOG_TRACE (ERROR, "UE-MAIN   - "
                   "Failed to send data to the user application layer");
266
        LOG_FUNC_RETURN(true);
gauthier's avatar
 
gauthier committed
267 268 269 270
      }
    }
  }

271
  LOG_FUNC_RETURN(false);
gauthier's avatar
 
gauthier committed
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_process_data()                                   **
 **                                                                        **
 ** Description: Executes AT command operations received from the user and **
 **      calls applicable NAS procedure function.                  **
 **                                                                        **
 ** Inputs:  data:      Generic pointer to data structure that has **
 **             to be processed                            **
 **          Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok if the command has been success-  **
 **             fully executed; RETURNerror otherwise      **
 **                                                                        **
 ***************************************************************************/
290
int nas_user_process_data(nas_user_t *user, const void *data)
gauthier's avatar
 
gauthier committed
291 292
{
  LOG_FUNC_IN;
293
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311

  int ret_code = RETURNerror;

  if (data) {
    const at_command_t *user_data = (at_command_t *)(data);
    _nas_user_procedure_t nas_procedure;

    LOG_TRACE(INFO, "USR-MAIN  - Process %s AT command %d",
              (user_data->type == AT_COMMAND_ACT) ? "action" :
              (user_data->type == AT_COMMAND_SET) ? "set parameter" :
              (user_data->type == AT_COMMAND_GET) ? "get parameter" :
              (user_data->type == AT_COMMAND_TST) ? "test parameter"
              : "unknown type", user_data->id);

    /* Call NAS procedure applicable to the AT command */
    nas_procedure = _nas_user_procedure[user_data->id];

    if (nas_procedure != NULL) {
312
      ret_code = (*nas_procedure)(user, user_data);
gauthier's avatar
 
gauthier committed
313 314
    } else {
      /* AT command related to result format only */
315 316 317 318
      at_response->id = user_data->id;
      at_response->type = user_data->type;
      at_response->mask = user_data->mask;
      at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
319 320 321 322
      ret_code = RETURNok;
    }
  } else {
    LOG_TRACE(ERROR, "USR-MAIN  - Data to be processed is null");
323
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_get_data()                                       **
 **                                                                        **
 ** Description: Get a generic pointer to the internal representation of   **
 **      the data structure returned as the result of NAS proce-   **
 **      dure function call.                                       **
 **      Casting to the proper type is necessary before its usage. **
 **                                                                        **
 ** Inputs:  None                                                      **
 **          Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **          Return:    A generic pointer to the data structure    **
 **          Others:    None                                       **
 **                                                                        **
 ***************************************************************************/
346
const void *nas_user_get_data(nas_user_t *user)
gauthier's avatar
 
gauthier committed
347 348
{
  LOG_FUNC_IN;
349
  LOG_FUNC_RETURN ((void *) user->at_response);
gauthier's avatar
 
gauthier committed
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372
}

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

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgsn()                                      **
 **                                                                        **
 ** Description: Executes the AT CGSN command operations:                  **
 **          The AT CGSN command returns information text intended to  **
 **      permit the user to identify the individual Mobile         **
 **      Equipment to which it is connected to.                    **
 **      Typically, the text will consist of a single line contai- **
 **      ning the IMEI.                                            **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
373
static int _nas_user_proc_cgsn(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
374 375
{
  LOG_FUNC_IN;
376
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
377 378

  int ret_code = RETURNok;
379
  at_cgsn_resp_t *cgsn = &at_response->response.cgsn;
gauthier's avatar
 
gauthier committed
380 381
  memset(cgsn, 0, sizeof(at_cgsn_resp_t));

382 383 384 385
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CGSN_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
386 387 388 389

  switch (data->type) {
  case AT_COMMAND_ACT:
    /* Get the Product Serial Number Identification (IMEI) */
390
    strncpy(cgsn->sn, user->nas_user_nvdata->IMEI,
gauthier's avatar
 
gauthier committed
391 392 393 394 395 396 397 398 399 400
            AT_RESPONSE_INFO_TEXT_SIZE);
    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGSN command type %d is not supported",
              data->type);
401
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgmi()                                      **
 **                                                                        **
 ** Description: Executes the AT CGMI command operations:                  **
 **      The AT CGMI command returns information text intended to  **
 **      permit the user to identify the manufacturer of the Mobi- **
 **      le Equipment to which it is connected to.                 **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
424
static int _nas_user_proc_cgmi(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
425 426
{
  LOG_FUNC_IN;
427
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
428 429

  int ret_code = RETURNok;
430
  at_cgmi_resp_t *cgmi = &at_response->response.cgmi;
gauthier's avatar
 
gauthier committed
431 432
  memset(cgmi, 0, sizeof(at_cgmi_resp_t));

433 434 435 436
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CGMI_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
437 438 439 440

  switch (data->type) {
  case AT_COMMAND_ACT:
    /* Get the Manufacturer identifier */
441
    strncpy(cgmi->manufacturer, user->nas_user_nvdata->manufacturer,
gauthier's avatar
 
gauthier committed
442 443 444 445 446 447 448 449 450 451
            AT_RESPONSE_INFO_TEXT_SIZE);
    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGMI command type %d is not supported",
              data->type);
452
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgmm()                                      **
 **                                                                        **
 ** Description: Executes the AT CGMM command operations:                  **
 **      The AT CGMM command returns information text intended to  **
 **      permit the user to identify specific model of the Mobile  **
 **      Equipment to which it is connected to.                    **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
475
static int _nas_user_proc_cgmm(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
476 477
{
  LOG_FUNC_IN;
478
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
479 480

  int ret_code = RETURNok;
481
  at_cgmm_resp_t *cgmm = &at_response->response.cgmm;
gauthier's avatar
 
gauthier committed
482 483
  memset(cgmm, 0, sizeof(at_cgmm_resp_t));

484 485 486 487
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CGMM_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
488 489 490 491

  switch (data->type) {
  case AT_COMMAND_ACT:
    /* Get the Model identifier */
492
    strncpy(cgmm->model, user->nas_user_nvdata->model,
gauthier's avatar
 
gauthier committed
493 494 495 496 497 498 499 500 501 502
            AT_RESPONSE_INFO_TEXT_SIZE);
    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGMM command type %d is not supported",
              data->type);
503
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgmr()                                      **
 **                                                                        **
 ** Description: Executes the AT CGMR command operations:                  **
 **      The AT CGMR command returns information text intended to  **
 **      permit the user to identify the version, revision level   **
 **      or date, or other pertinent information of the Mobile     **
 **      Equipment to which it is connected to.                    **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
527
static int _nas_user_proc_cgmr(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
528 529
{
  LOG_FUNC_IN;
530
  nas_user_context_t *nas_user_context = user->nas_user_context;
531
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
532 533

  int ret_code = RETURNok;
534
  at_cgmr_resp_t *cgmr = &at_response->response.cgmr;
gauthier's avatar
 
gauthier committed
535 536
  memset(cgmr, 0, sizeof(at_cgmr_resp_t));

537 538 539 540
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CGMR_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
541 542 543 544

  switch (data->type) {
  case AT_COMMAND_ACT:
    /* Get the revision identifier */
545
    strncpy(cgmr->revision, nas_user_context->version,
gauthier's avatar
 
gauthier committed
546 547 548 549 550 551 552 553 554 555
            AT_RESPONSE_INFO_TEXT_SIZE);
    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGMR command type %d is not supported",
              data->type);
556
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cimi()                                      **
 **                                                                        **
 ** Description: Executes the AT CIMI command operations:                  **
 **      The AT CIMI command returns information text intended to  **
 **      permit the user to identify the individual SIM card or    **
 **      active application in the UICC (GSM or USIM) which is     **
 **      attached to the Mobile Equipment to which it is connected **
 **      to.                                                       **
 **      Typically, the text will consist of a single line contai- **
 **      ning the IMSI.                                            **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
583
static int _nas_user_proc_cimi(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
584 585
{
  LOG_FUNC_IN;
586
  nas_user_context_t *nas_user_context = user->nas_user_context;
587
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
588 589

  int ret_code = RETURNok;
590
  at_cimi_resp_t *cimi = &at_response->response.cimi;
gauthier's avatar
 
gauthier committed
591 592
  memset(cimi, 0, sizeof(at_cimi_resp_t));

593 594 595 596
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CIMI_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
597 598 599

  switch (data->type) {
  case AT_COMMAND_ACT:
600
    if (nas_user_context->sim_status != NAS_USER_READY) {
601
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
602 603 604 605
      LOG_FUNC_RETURN(RETURNerror);
    }

    /* Get the International Mobile Subscriber Identity (IMSI) */
606
    ret_code = nas_proc_get_imsi(user->emm_data, cimi->IMSI);
gauthier's avatar
 
gauthier committed
607 608 609

    if (ret_code != RETURNok) {
      LOG_TRACE(ERROR, "USR-MAIN  - Failed to get IMSI number");
610
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
611 612 613 614 615 616 617 618 619 620 621
    }

    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CIMI command type %d is not supported",
              data->type);
622
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cfun()                                      **
 **                                                                        **
 ** Description: Executes the AT CFUN command operations:                  **
 **      The AT CFUN command is used to set the Mobile Equipment   **
 **      to different power consumption states.                    **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
644
static int _nas_user_proc_cfun(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
645 646
{
  LOG_FUNC_IN;
647
  nas_user_context_t *nas_user_context = user->nas_user_context;
648
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
649 650

  int ret_code = RETURNok;
651
  at_cfun_resp_t *cfun = &at_response->response.cfun;
gauthier's avatar
 
gauthier committed
652 653 654 655
  memset(cfun, 0, sizeof(at_cfun_resp_t));

  int fun = AT_CFUN_FUN_DEFAULT;

656 657 658 659
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CFUN_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
660 661 662 663 664 665 666 667 668 669 670 671 672 673 674

  switch (data->type) {
  case AT_COMMAND_SET:

    /*
     * Set command selects the level of functionality in the MT
     */
    if (data->mask & AT_CFUN_RST_MASK) {
      if (data->command.cfun.rst == AT_CFUN_RST) {
        /* TODO: Reset the MT before setting it to <fun> power level */
      } else if (data->command.cfun.rst != AT_CFUN_NORST) {
        /* The value of the reset parameter is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <rst> parameter is not valid"
                  " (%d)", data->command.cfun.rst);
675
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
676 677 678 679 680 681 682 683 684 685 686 687
        ret_code = RETURNerror;
        break;
      }
    }

    if (data->mask & AT_CFUN_FUN_MASK) {
      if ( (data->command.cfun.fun < AT_CFUN_MIN) ||
           (data->command.cfun.fun > AT_CFUN_MAX) ) {
        /* The value of the functionality level parameter
         * is not valid; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <fun> parameter is not valid"
                  " (%d)", data->command.cfun.fun);
688
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
        ret_code = RETURNerror;
        break;
      }

      /* Set to the selected parameter value */
      fun = data->command.cfun.fun;
    }

    switch (fun) {
    case AT_CFUN_MIN:
      /* TODO: Shutdown ??? */
      break;

    case AT_CFUN_FULL:
      /* Notify the NAS procedure call manager that the UE is
       * operational */
705
      ret_code = nas_proc_enable_s1_mode(user);
gauthier's avatar
 
gauthier committed
706 707 708 709 710 711 712 713 714
      break;

    default:
      /* TODO: Disable the radio ??? */
      break;
    }

    if (ret_code != RETURNerror) {
      /* Update the functionality level */
715
      nas_user_context->fun = fun;
gauthier's avatar
 
gauthier committed
716 717 718 719 720 721
    }

    break;

  case AT_COMMAND_GET:
    /* Get the MT's functionality level */
722
    cfun->fun = nas_user_context->fun;
gauthier's avatar
 
gauthier committed
723 724 725 726 727 728 729 730 731 732 733
    break;

  case AT_COMMAND_TST:
    /*
     * Test command returns values supported as a compound value
     */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CFUN command type %d is not supported",
              data->type);
734
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cpin()                                      **
 **                                                                        **
 ** Description: Executes the AT CPIN command operations:                  **
 **      The AT CPIN command is used to enter the Mobile Equipment **
 **      passwords which are needed before any other functionality **
 **      can be operated.                                          **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
757
static int _nas_user_proc_cpin(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
758 759
{
  LOG_FUNC_IN;
760
  nas_user_context_t *nas_user_context = user->nas_user_context;
761
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
762 763

  int ret_code = RETURNok;
764
  at_cpin_resp_t *cpin = &at_response->response.cpin;
gauthier's avatar
 
gauthier committed
765 766
  memset(cpin, 0, sizeof(at_cpin_resp_t));

767 768 769 770
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CPIN_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
771 772 773 774 775 776 777 778

  switch (data->type) {
  case AT_COMMAND_SET:

    /*
     * Set command sends to the MT a password which is necessary
     * before it can be operated
     */
779
    if (nas_user_context->sim_status == NAS_USER_SIM_PIN) {
gauthier's avatar
 
gauthier committed
780
      /* The MT is waiting for PIN password; check the PIN code */
781
      if (strncmp(user->nas_user_nvdata->PIN,
gauthier's avatar
 
gauthier committed
782 783 784 785
                  data->command.cpin.pin, USER_PIN_SIZE) != 0) {
        /* The PIN code is NOT matching; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - PIN code is not correct "
                  "(%s)", data->command.cpin.pin);
786
        at_response->cause_code = AT_ERROR_INCORRECT_PASSWD;
gauthier's avatar
 
gauthier committed
787 788 789 790
        ret_code = RETURNerror;
      } else {
        /* The PIN code is matching; update the user's PIN
         * pending status */
791
        nas_user_context->sim_status = NAS_USER_READY;
gauthier's avatar
 
gauthier committed
792 793 794 795
      }
    } else {
      /* The MT is NOT waiting for PIN password;
       * return an error message */
796
      at_response->cause_code = AT_ERROR_OPERATION_NOT_ALLOWED;
gauthier's avatar
 
gauthier committed
797 798 799 800 801 802 803 804 805 806 807
      ret_code = RETURNerror;
    }

    break;

  case AT_COMMAND_GET:
    /*
     * Read command returns an alphanumeric string indicating
     * whether some password is required or not.
     */
    strncpy(cpin->code,
808
            _nas_user_sim_status_str[nas_user_context->sim_status],
gauthier's avatar
 
gauthier committed
809 810 811 812 813 814 815 816 817 818 819
            AT_CPIN_RESP_SIZE);
    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    /* Other types of AT CPIN command are not valid */
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CPIN command type %d is not supported",
              data->type);
820
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_csq()                                       **
 **                                                                        **
 ** Description: Executes the AT CSQ command operations:                   **
 **      The AT CSQ command returns received signal strength       **
 **      indication and channel bit error rate from the Mobile     **
 **      Equipment.                                                **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
843
static int _nas_user_proc_csq(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
844 845
{
  LOG_FUNC_IN;
846
  nas_user_context_t *nas_user_context = user->nas_user_context;
847
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
848 849

  int ret_code = RETURNok;
850
  at_csq_resp_t *csq = &at_response->response.csq;
gauthier's avatar
 
gauthier committed
851 852
  memset(csq, 0, sizeof(at_csq_resp_t));

853 854 855 856
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CSQ_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
857 858 859

  switch (data->type) {
  case AT_COMMAND_ACT:
860
    if (nas_user_context->sim_status != NAS_USER_READY) {
861
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * Execution command returns received signal strength indication
     * and channel bit error rate <ber> from the MT
     */
    csq->rssi = AT_CESQ_RSSI_UNKNOWN;
    csq->ber  = AT_CESQ_BER_UNKNOWN;
    break;

  case AT_COMMAND_TST:
    /*
     * Test command returns values supported as a compound value
     */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CSQ command type %d is not supported",
              data->type);
882
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cesq()                                      **
 **                                                                        **
 ** Description: Executes the AT CESQ command operations:                  **
 **      The AT CESQ command returns received signal quality para- **
 **      meters.                                                   **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
904
static int _nas_user_proc_cesq(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
905 906
{
  LOG_FUNC_IN;
907
  nas_user_context_t *nas_user_context = user->nas_user_context;
908
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
909 910

  int ret_code = RETURNok;
911
  at_cesq_resp_t *cesq = &at_response->response.cesq;
gauthier's avatar
 
gauthier committed
912 913
  memset(cesq, 0, sizeof(at_cesq_resp_t));

914 915 916 917
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CESQ_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
918 919 920

  switch (data->type) {
  case AT_COMMAND_ACT:
921
    if (nas_user_context->sim_status != NAS_USER_READY) {
922
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
923 924 925 926 927 928 929 930 931 932
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * Execution command returns received signal quality parameters
     */
    cesq->rssi = AT_CESQ_RSSI_UNKNOWN;
    cesq->ber  = AT_CESQ_BER_UNKNOWN;
    cesq->rscp = AT_CESQ_RSCP_UNKNOWN;
    cesq->ecno = AT_CESQ_ECNO_UNKNOWN;
933
    ret_code = nas_proc_get_signal_quality(user, &cesq->rsrq, &cesq->rsrp);
gauthier's avatar
 
gauthier committed
934 935 936 937 938 939 940 941 942 943 944
    break;

  case AT_COMMAND_TST:
    /*
     * Test command returns values supported as a compound value
     */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CESQ command type %d is not supported",
              data->type);
945
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cops()                                      **
 **                                                                        **
 ** Description: Executes the AT COPS command operations:                  **
 **      The AT COPS command forces an attempt to select and       **
 **      register the GSM/UMTS/EPS network operator using the      **
 **      SIM/USIM card installed in the currently selected card    **
 **      slot.                                                     **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
969
static int _nas_user_proc_cops(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
970 971
{
  LOG_FUNC_IN;
972
  nas_user_context_t *nas_user_context = user->nas_user_context;
973
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
974 975

  int ret_code = RETURNok;
976
  at_cops_resp_t *cops = &at_response->response.cops;
gauthier's avatar
 
gauthier committed
977 978 979 980 981 982 983 984 985 986
  memset(cops, 0, sizeof(at_cops_resp_t));

  static int read_format = AT_COPS_FORMAT_DEFAULT;

  int mode = AT_COPS_MODE_DEFAULT;
  int format = AT_COPS_FORMAT_DEFAULT;
  int AcT = AT_COPS_ACT_DEFAULT;
  char oper_buffer[NET_FORMAT_MAX_SIZE], *oper = oper_buffer;
  memset(oper, 0, NET_FORMAT_MAX_SIZE);

987
  bool oper_is_selected;
gauthier's avatar
 
gauthier committed
988

989 990 991 992
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_NO_PARAM;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
993 994 995

  switch (data->type) {
  case AT_COMMAND_SET:
996
    if (nas_user_context->sim_status != NAS_USER_READY) {
997
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * Set command forces an attempt to select and register the
     * GSM/UMTS/EPS network operator using the SIM/USIM card
     * installed in the currently selected card slot.
     */
    if (data->mask & AT_COPS_MODE_MASK) {
      mode = data->command.cops.mode;

      switch (mode) {
      case AT_COPS_AUTO:
        /*
         * Register in automatic mode
         */
        break;

      case AT_COPS_MANUAL:

        /*
         * Register in manual mode
         */

        /** break is intentionaly missing */

      case AT_COPS_MANAUTO:

        /*
         * Register in manual/automatic mode
         */

        /* <oper> field shall be present */
        if ( !(data->mask & AT_COPS_OPER_MASK) ) {
          LOG_TRACE(ERROR, "USR-MAIN  - <oper> parameter is not present");
1033
          at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1034 1035 1036 1037 1038 1039 1040
          ret_code = RETURNerror;
          break;
        }

        /* <format> field shall be present */
        if ( !(data->mask & AT_COPS_FORMAT_MASK) ) {
          LOG_TRACE(ERROR, "USR-MAIN  - <format> parameter is not present");
1041
          at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1042 1043 1044 1045 1046 1047 1048 1049 1050 1051
          ret_code = RETURNerror;
          break;
        }

        /* Set the operator's representation format */
        if ( (data->command.cops.format < AT_COPS_FORMAT_MIN) ||
             (data->command.cops.format > AT_COPS_FORMAT_MAX) ) {
          /* The value of <format> field is not valid */
          LOG_TRACE(ERROR, "USR-MAIN  - <format> parameter is not valid (%d)",
                    data->command.cops.format);
1052
          at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065
          ret_code = RETURNerror;
          break;
        }

        read_format = format = data->command.cops.format;

        /* Set the access technology */
        if (data->mask & AT_COPS_ACT_MASK) {
          if ( (data->command.cops.AcT < AT_COPS_ACT_MIN) ||
               (data->command.cops.AcT > AT_COPS_ACT_MAX) ) {
            /* The value of <AcT> field is not valid */
            LOG_TRACE(ERROR, "USR-MAIN  - <AcT> parameter is not valid (%d)",
                      data->command.cops.AcT);
1066
            at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093
            ret_code = RETURNerror;
            break;
          }

          AcT = data->command.cops.AcT;
        }

        break;

      case AT_COPS_DEREG:
        /*
         * Deregister from network
         */
        /* Nothing to do there */
        break;

      case AT_COPS_FORMAT:

        /*
         * Set only <format> for read command +COPS?
         */
        if (data->mask & AT_COPS_FORMAT_MASK) {
          if ( (data->command.cops.format < AT_COPS_FORMAT_MIN) ||
               (data->command.cops.format > AT_COPS_FORMAT_MAX) ) {
            /* The value of <format> field is not valid */
            LOG_TRACE(ERROR, "USR-MAIN  - <format> parameter is not valid (%d)",
                      data->command.cops.format);
1094
            at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109
            ret_code = RETURNerror;
            break;
          }

          /* Set to the selected representation format */
          read_format = data->command.cops.format;
        } else {
          /* Format is not present; set to default */
          read_format = AT_COPS_FORMAT_DEFAULT;
        }

        break;

      default:
        LOG_TRACE(ERROR, "USR-MAIN  - <mode> parameter is not supported (%d)", mode);
1110
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121
        ret_code = RETURNerror;
        break;
      }
    }

    /*
     * Performs network registration
     */
    if (ret_code != RETURNerror) {
      if (mode == AT_COPS_DEREG) {
        /* Force an attempt to deregister from the network */
1122
        ret_code = nas_proc_deregister(user);
gauthier's avatar
 
gauthier committed
1123 1124 1125

        if (ret_code != RETURNok) {
          LOG_TRACE(ERROR, "USR-MAIN  - Network deregistration failed");
1126
          at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1127 1128 1129 1130 1131
          break;
        }
      } else if (mode != AT_COPS_FORMAT) {
        /* Force an attempt to automatically/manualy select
         * and register the GSM/UMTS/EPS network operator */
1132
        ret_code = nas_proc_register(user, mode, format,
gauthier's avatar
 
gauthier committed
1133 1134 1135 1136
                                     &data->command.cops.plmn, AcT);

        if (ret_code != RETURNok) {
          LOG_TRACE(ERROR, "USR-MAIN  - Network registration failed (<mode>=%d)", mode);
1137
          at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
          break;
        }
      }
    }

    break;

  case AT_COMMAND_GET:
    /*
     * Read command returns the current mode, the currently selected
     * network operator and the current Access Technology.
     */

    /* Get the current network registration data */
1152
    ret_code = nas_proc_get_reg_data(user, &mode,
gauthier's avatar
 
gauthier committed
1153 1154 1155 1156 1157 1158
                                     &oper_is_selected, read_format,
                                     &cops->get.plmn, &cops->get.AcT);

    if (ret_code != RETURNok) {
      LOG_TRACE(ERROR, "USR-MAIN  - Failed to get registration data (<mode>=%d)",
                mode);
1159
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
      break;
    }

    /* Set the network selection operating mode */
    if (mode == NET_PLMN_AUTO) {
      cops->get.mode = AT_COPS_AUTO;
    } else if (mode == NET_PLMN_MANUAL) {
      cops->get.mode = AT_COPS_MANUAL;
    } else {
      cops->get.mode = AT_COPS_MANAUTO;
    }

    /* Set optional parameter bitmask */
    if (oper_is_selected) {
      cops->get.format = read_format;
1175
      at_response->mask |= (AT_COPS_RESP_FORMAT_MASK |
gauthier's avatar
 
gauthier committed
1176 1177 1178
                              AT_COPS_RESP_OPER_MASK);

      if (cops->get.AcT != NET_ACCESS_UNAVAILABLE) {
1179
        at_response->mask |= AT_COPS_RESP_ACT_MASK;
gauthier's avatar
 
gauthier committed
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
      }
    }

    break;

  case AT_COMMAND_TST:
    /*
     * Test command returns a set of parameters, each representing
     * an operator present in the network.
     */
1190
    cops->tst.size = nas_proc_get_oper_list(user, &cops->tst.data);
gauthier's avatar
 
gauthier committed
1191 1192 1193 1194 1195
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+COPS command type %d is not supported",
              data->type);
1196
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgatt()                                     **
 **                                                                        **
 ** Description: Executes the AT CGATT command operations:                 **
 **      The AT CGATT command is used to attach the MT to,         **
 **      or detach the MT from, the EPS service.                   **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
1218
static int _nas_user_proc_cgatt(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
1219 1220
{
  LOG_FUNC_IN;
1221
  nas_user_context_t *nas_user_context = user->nas_user_context;
1222
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
1223 1224

  int ret_code = RETURNok;
1225
  at_cgatt_resp_t *cgatt = &at_response->response.cgatt;
gauthier's avatar
 
gauthier committed
1226 1227
  memset(cgatt, 0, sizeof(at_cgatt_resp_t));

1228 1229 1230 1231
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CGATT_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
1232 1233 1234

  switch (data->type) {
  case AT_COMMAND_SET:
1235
    if (nas_user_context->sim_status != NAS_USER_READY) {
1236
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * The execution command is used to attach the MT to, or detach the
     * MT from, the EPS service
     */
    if (data->mask & AT_CGATT_STATE_MASK) {
      if ( (data->command.cgatt.state < AT_CGATT_STATE_MIN) ||
           (data->command.cgatt.state > AT_CGATT_STATE_MAX) ) {
        /* The value of the EPS attachment code is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <state> parameter is not valid (%d)",
                  data->command.cgatt.state);
1251
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261
        ret_code = RETURNerror;
        break;
      }

      /*
       * Perform EPS service attach/detach
       */
      ret_code = RETURNerror;

      if (data->command.cgatt.state == AT_CGATT_ATTACHED) {
1262
        ret_code = nas_proc_attach(user);
gauthier's avatar
 
gauthier committed
1263
      } else if (data->command.cgatt.state == AT_CGATT_DETACHED) {
1264
        ret_code = nas_proc_detach(user, false);
gauthier's avatar
 
gauthier committed
1265 1266 1267 1268 1269 1270
      }

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to attach/detach "
                  "to/from EPS service (<state>=%d)",
                  data->command.cgatt.state);
1271
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
      }
    }

    break;

  case AT_COMMAND_GET:

    /*
     * Read command returns the current EPS service state.
     */
1282
    if (nas_proc_get_attach_status(user) != true) {
gauthier's avatar
 
gauthier committed
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299
      cgatt->state = AT_CGATT_DETACHED;
    } else {
      cgatt->state = AT_CGATT_ATTACHED;
    }

    break;

  case AT_COMMAND_TST:
    /*
     * Test command is used for requesting information on the supported
     * EPS service states
     */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGATT command type %d is not supported",
              data->type);
1300
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_creg()                                      **
 **                                                                        **
 ** Description: Executes the AT CREG command operations:                  **
 **      The AT CREG command returns the Mobile Equipment's        **
 **      circuit mode network registration status and optionnally  **
 **      location information in GERA/UTRA/E-UTRA Network.         **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
1323
static int _nas_user_proc_creg(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
1324 1325
{
  LOG_FUNC_IN;
1326
  nas_user_context_t *nas_user_context = user->nas_user_context;
1327
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
1328 1329

  int ret_code = RETURNok;
1330
  at_creg_resp_t *creg = &at_response->response.creg;
gauthier's avatar
 
gauthier committed
1331 1332 1333 1334
  memset(creg, 0, sizeof(at_creg_resp_t));

  static int n = AT_CREG_N_DEFAULT;

1335 1336 1337 1338
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_NO_PARAM;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
1339 1340 1341

  switch (data->type) {
  case AT_COMMAND_SET:
1342
    if (nas_user_context->sim_status != NAS_USER_READY) {
1343
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * The set command controls the presentation of an unsolicited
     * result code when there is a change in the MT's circuit mode
     * network registration status, or when there is a change of the
     * network cell in GERAN/UTRAN/E-UTRAN.
     */
    if (data->mask & AT_CREG_N_MASK) {
      if ( (data->command.creg.n < AT_CREG_N_MIN) ||
           (data->command.creg.n > AT_CREG_N_MAX) ) {
        /* The value of the unsolicited result code is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <n> parameter is not valid"
                  " (%d)", data->command.creg.n);
1360
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385
        ret_code = RETURNerror;
        break;
      }

      /* Set to the selected parameter value */
      n = data->command.creg.n;
    } else {
      /* The numeric parameter is not present; set to default */
      n = AT_CREG_N_DEFAULT;
    }

    /* Disable/Enable network logging */
    switch (n) {
    case AT_CREG_OFF:
      /* Disable logging of network registration status */
      ret_code = user_ind_deregister(USER_IND_REG);

      if (ret_code != RETURNerror) {
        /* Disable logging of location information */
        ret_code = user_ind_deregister(USER_IND_LOC);
      }

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR,
                  "USR-MAIN  - Failed to disable logging of network notification");
1386
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400
      }

      break;

    case AT_CREG_BOTH:

      /* Location Area Code (lac) is not available */
    case AT_CREG_ON:
      /* Enable logging of the MT's circuit mode network
       * registration status in GERAN/UTRAN/E_UTRAN */
      ret_code = user_ind_register(USER_IND_REG, AT_CREG, NULL);

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to enable logging of registration status");
1401
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427
      }

      break;

    default:
      break;
    }

    break;

  case AT_COMMAND_GET:
    /*
     * The read command returns the status of result code presentation,
     * and indication of whether the network has currently indicated
     * the registration of the MT, and available location information
     * elements when the MT is registered in the network.
     */
    creg->n = n;

    switch (n) {
    case AT_CREG_BOTH:

      /* Location Area Code (lac) is not available */
    case AT_CREG_OFF:
    case AT_CREG_ON:
      /* Get network registration status */
1428
      ret_code = nas_proc_get_reg_status(user, &creg->stat);
gauthier's avatar
 
gauthier committed
1429 1430 1431

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to get registration status");
1432
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448
      }

      break;

    default:
      break;
    }

    break;

  case AT_COMMAND_TST:
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CREG command type %d is not supported",
              data->type);
1449
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgreg()                                     **
 **                                                                        **
 ** Description: Executes the AT CGREG command operations:                 **
 **      The AT CGREG command returns the Mobile Equipment's GPRS  **
 **      network registration status and optionnally location      **
 **      information in GERA/UTRA Network.                         **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
1472
static int _nas_user_proc_cgreg(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
1473 1474
{
  LOG_FUNC_IN;
1475
  nas_user_context_t *nas_user_context = user->nas_user_context;
1476
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
1477 1478

  int ret_code = RETURNok;
1479
  at_cgreg_resp_t *cgreg = &at_response->response.cgreg;
gauthier's avatar
 
gauthier committed
1480 1481 1482 1483
  memset(cgreg, 0, sizeof(at_cgreg_resp_t));

  static int n = AT_CGREG_N_DEFAULT;

1484 1485 1486 1487
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_NO_PARAM;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
1488 1489 1490

  switch (data->type) {
  case AT_COMMAND_SET:
1491
    if (nas_user_context->sim_status != NAS_USER_READY) {
1492
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * The set command controls the presentation of an unsolicited
     * result code when there is a change in the MT's GPRS network
     * registration status, or when there is a change of the network
     * cell in GERAN/UTRAN.
     */
    if (data->mask & AT_CGREG_N_MASK) {
      if ( (data->command.cgreg.n < AT_CGREG_N_MIN) ||
           (data->command.cgreg.n > AT_CGREG_N_MAX) ) {
        /* The value of the unsolicited result code is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <n> parameter is not valid"
                  " (%d)", data->command.cgreg.n);
1509
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534
        ret_code = RETURNerror;
        break;
      }

      /* Set to the selected parameter value */
      n = data->command.cgreg.n;
    } else {
      /* The numeric parameter is not present; set to default */
      n = AT_CGREG_N_DEFAULT;
    }

    /* Disable/Enable network logging */
    switch (n) {
    case AT_CGREG_OFF:
      /* Disable logging of network registration status */
      ret_code = user_ind_deregister(USER_IND_REG);

      if (ret_code != RETURNerror) {
        /* Disable logging of location information */
        ret_code = user_ind_deregister(USER_IND_LOC);
      }

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR,
                  "USR-MAIN  - Failed to disable logging of network notification");
1535
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549
      }

      break;

    case AT_CGREG_BOTH:

      /* Location Area Code (lac) is not available */
    case AT_CGREG_ON:
      /* Enable logging of the MT's GPRS network registration
       * status in GERAN/UTRAN/E_UTRAN */
      ret_code = user_ind_register(USER_IND_REG, AT_CGREG, NULL);

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to enable logging of registration status");
1550
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576
      }

      break;

    default:
      break;
    }

    break;

  case AT_COMMAND_GET:
    /*
     * The read command returns the status of result code presentation,
     * and indication of whether the network has currently indicated
     * the registration of the MT, and available location information
     * elements when the MT is registered in the network.
     */
    cgreg->n = n;

    switch (n) {
    case AT_CGREG_BOTH:

      /* Location Area Code (lac) is not available */
    case AT_CGREG_OFF:
    case AT_CGREG_ON:
      /* Get network registration status */
1577
      ret_code = nas_proc_get_reg_status(user, &cgreg->stat);
gauthier's avatar
 
gauthier committed
1578 1579 1580

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to get registration status");
1581
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597
      }

      break;

    default:
      break;
    }

    break;

  case AT_COMMAND_TST:
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGREG command type %d is not supported",
              data->type);
1598
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cereg()                                     **
 **                                                                        **
 ** Description: Executes the AT CEREG command operations:                 **
 **      The AT CEREG command returns the Mobile Equipment's EPS   **
 **      services registration status and optionnally location     **
 **      information in E-UTRA Network.                            **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
1621
static int _nas_user_proc_cereg(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
1622 1623
{
  LOG_FUNC_IN;
1624
  nas_user_context_t *nas_user_context = user->nas_user_context;
1625
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
1626 1627

  int ret_code = RETURNok;
1628
  at_cereg_resp_t *cereg = &at_response->response.cereg;
gauthier's avatar
 
gauthier committed
1629 1630 1631 1632
  memset(cereg, 0, sizeof(at_cereg_resp_t));

  static int n = AT_CEREG_N_DEFAULT;

1633 1634 1635 1636
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_NO_PARAM;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
1637 1638 1639

  switch (data->type) {
  case AT_COMMAND_SET:
1640
    if (nas_user_context->sim_status != NAS_USER_READY) {
1641
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * The set command controls the presentation of an unsolicited
     * result code when there is a change in the MT's EPS network
     * registration status, or when there is a change of the network
     * cell in E-UTRAN.
     */
    if (data->mask & AT_CEREG_N_MASK) {
      if ( (data->command.cereg.n < AT_CEREG_N_MIN) ||
           (data->command.cereg.n > AT_CEREG_N_MAX) ) {
        /* The value of the unsolicited result code is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <n> parameter is not valid"
                  " (%d)",  data->command.cereg.n);
1658
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683
        ret_code = RETURNerror;
        break;
      }

      /* Set to the selected parameter value */
      n = data->command.cereg.n;
    } else {
      /* The numeric parameter is not present; set to default */
      n = AT_CEREG_N_DEFAULT;
    }

    /* Disable/Enable network logging */
    switch (n) {
    case AT_CEREG_OFF:
      /* Disable logging of network registration status */
      ret_code = user_ind_deregister(USER_IND_REG);

      if (ret_code != RETURNerror) {
        /* Disable logging of location information */
        ret_code = user_ind_deregister(USER_IND_LOC);
      }

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR,
                  "USR-MAIN  - Failed to disable logging of network notification");
1684
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1685 1686 1687 1688 1689 1690 1691 1692 1693 1694
      }

      break;

    case AT_CEREG_BOTH:
      /* Enable logging of the location information in E-UTRAN */
      ret_code = user_ind_register(USER_IND_LOC, AT_CEREG, NULL);

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to enable logging of location information");
1695
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706
      }

      break;

    case AT_CEREG_ON:
      /* Enable logging of the MT's EPS network registration
       * status in E-UTRAN */
      ret_code = user_ind_register(USER_IND_REG, AT_CEREG, NULL);

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to enable logging of registration status");
1707
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729
      }

      break;

    default:
      break;
    }

    break;

  case AT_COMMAND_GET:
    /*
     * The read command returns the status of result code presentation,
     * and indication of whether the network has currently indicated
     * the registration of the MT, and available location information
     * elements when the MT is registered in the network.
     */
    cereg->n = n;

    switch (n) {
    case AT_CEREG_BOTH:
      /* Get EPS location information  */
1730
      ret_code = nas_proc_get_loc_info(user, cereg->tac, cereg->ci,
gauthier's avatar
 
gauthier committed
1731 1732 1733 1734
                                       &cereg->AcT);

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to get location information");
1735
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1736 1737 1738 1739
        break;
      }

      if (cereg->tac[0] != 0) {
1740
        at_response->mask |= (AT_CEREG_RESP_TAC_MASK |
gauthier's avatar
 
gauthier committed
1741 1742 1743
                                AT_CEREG_RESP_CI_MASK);

        if (cereg->AcT != NET_ACCESS_UNAVAILABLE) {
1744
          at_response->mask |= (AT_CEREG_RESP_ACT_MASK);
gauthier's avatar
 
gauthier committed
1745 1746 1747 1748 1749 1750 1751 1752
        }
      }

      /** break is intentionaly missing */

    case AT_CEREG_OFF:
    case AT_CEREG_ON:
      /* Get network registration status */
1753
      ret_code = nas_proc_get_reg_status(user, &cereg->stat);
gauthier's avatar
 
gauthier committed
1754 1755 1756

      if (ret_code != RETURNok) {
        LOG_TRACE(ERROR, "USR-MAIN  - Failed to get registration status");
1757
        at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773
      }

      break;

    default:
      break;
    }

    break;

  case AT_COMMAND_TST:
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CEREG command type %d is not supported",
              data->type);
1774
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgdcont()                                   **
 **                                                                        **
 ** Description: Executes the AT CGDCONT command operations:               **
 **      The AT CGDCONT command specifies PDP context parameter    **
 **      values for a PDP context identified by the local context  **
 **      identification parameter (cid).                           **
 **                                                                        **
 **      There is a 1 to 1 mapping between EPS bearer context and  **
 **      PDP context. Therefore a PDP context used for UMTS/GPRS   **
 **      designates a PDN connection and its associated EPS de-    **
 **      fault bearer and traffic flows in EPS.                    **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
1802
static int _nas_user_proc_cgdcont(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
1803 1804
{
  LOG_FUNC_IN;
1805
  nas_user_context_t *nas_user_context = user->nas_user_context;
1806
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
1807 1808

  int ret_code = RETURNok;
1809
  at_cgdcont_get_t *cgdcont = &at_response->response.cgdcont.get;
gauthier's avatar
 
gauthier committed
1810 1811 1812 1813 1814 1815 1816 1817 1818
  memset(cgdcont, 0, sizeof(at_cgdcont_resp_t));

  int cid = AT_CGDCONT_CID_DEFAULT;
  int pdn_type = NET_PDN_TYPE_IPV4;
  const char *apn = NULL;
  int ipv4_addr_allocation = AT_CGDCONT_IPV4_DEFAULT;
  int emergency = AT_CGDCONT_EBS_DEFAULT;
  int p_cscf = AT_CGDCONT_PCSCF_DEFAULT;
  int im_cn_signalling = AT_CGDCONT_IM_CM_DEFAULT;
1819
  bool reset_pdn = true;
gauthier's avatar
 
gauthier committed
1820

1821 1822 1823 1824
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_NO_PARAM;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
1825 1826 1827

  switch (data->type) {
  case AT_COMMAND_SET:
1828
    if (nas_user_context->sim_status != NAS_USER_READY) {
1829
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * Set command specifies PDN connection and its default
     * EPS bearer context parameter values
     */
    if (data->mask & AT_CGDCONT_CID_MASK) {
      if (data->command.cgdcont.cid < AT_CGDCONT_CID_MIN) {
        /* The value of the PDP context identifier is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <cid> parameter is not valid"
                  " (%d)", data->command.cgdcont.cid);
1843
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862
        ret_code = RETURNerror;
        break;
      }

      cid = data->command.cgdcont.cid;
    }

    if (data->mask & AT_CGDCONT_PDP_TYPE_MASK) {
      if (strcmp(data->command.cgdcont.PDP_type, "IP") == 0) {
        pdn_type = NET_PDN_TYPE_IPV4;
      } else if (strcmp(data->command.cgdcont.PDP_type, "IPV6") == 0) {
        pdn_type = NET_PDN_TYPE_IPV6;
      } else if (strcmp(data->command.cgdcont.PDP_type,"IPV4V6") == 0) {
        pdn_type = NET_PDN_TYPE_IPV4V6;
      } else {
        /* The value of the PDP type is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <PDN_type> parameter is not "
                  "valid (%s)", data->command.cgdcont.PDP_type);
1863
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1864 1865 1866 1867
        ret_code = RETURNerror;
        break;
      }

1868
      reset_pdn = false;
gauthier's avatar
 
gauthier committed
1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881
    }

    if (data->mask & AT_CGDCONT_APN_MASK) {
      apn = data->command.cgdcont.APN;
    }

    if (data->mask & AT_CGDCONT_D_COMP_MASK) {
      if ( (data->command.cgdcont.d_comp < AT_CGDCONT_D_COMP_MIN) ||
           (data->command.cgdcont.d_comp > AT_CGDCONT_D_COMP_MAX) ) {
        /* The value of the PDP data compression parameter is
         * not valid; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <d_comp> parameter is not "
                  "valid (%d)", data->command.cgdcont.d_comp);
1882
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896
        ret_code = RETURNerror;
        break;
      }

      /* Not supported: Applicable for SNDCP only */
    }

    if (data->mask & AT_CGDCONT_H_COMP_MASK) {
      if ( (data->command.cgdcont.h_comp < AT_CGDCONT_H_COMP_MIN) ||
           (data->command.cgdcont.h_comp > AT_CGDCONT_H_COMP_MAX) ) {
        /* The value of the PDP header compression parameter is
         * not valid; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <h_comp> parameter is not "
                  "valid (%d)", data->command.cgdcont.h_comp);
1897
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912
        ret_code = RETURNerror;
        break;
      }

      /* Not supported */
    }

    if (data->mask & AT_CGDCONT_IPV4ADDRALLOC_MASK) {
      if ( (data->command.cgdcont.IPv4AddrAlloc < AT_CGDCONT_IPV4_MIN)
           || (data->command.cgdcont.IPv4AddrAlloc > AT_CGDCONT_IPV4_MAX) ) {
        /* The value of the IPv4 address allocation parameter is
         * not valid; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <IPv4AddrAlloc> parameter "
                  "is not valid (%d)",
                  data->command.cgdcont.IPv4AddrAlloc);
1913
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928
        ret_code = RETURNerror;
        break;
      }

      ipv4_addr_allocation = data->command.cgdcont.IPv4AddrAlloc;
    }

    if (data->mask & AT_CGDCONT_EMERGECY_INDICATION_MASK) {
      if ( (data->command.cgdcont.emergency_indication < AT_CGDCONT_EBS_MIN)
           || (data->command.cgdcont.emergency_indication > AT_CGDCONT_EBS_MAX) ) {
        /* The value of the emergency indication parameter  is
         * not valid; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <emergency indication> "
                  "parameter is not valid (%d)",
                  data->command.cgdcont.emergency_indication);
1929
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944
        ret_code = RETURNerror;
        break;
      }

      emergency = data->command.cgdcont.emergency_indication;
    }

    if (data->mask & AT_CGDCONT_P_CSCF_DISCOVERY_MASK) {
      if ( (data->command.cgdcont.P_CSCF_discovery < AT_CGDCONT_PCSCF_MIN)
           || (data->command.cgdcont.P_CSCF_discovery > AT_CGDCONT_PCSCF_MAX) ) {
        /* The value of the P-CSCF address discovery parameter  is
         * not valid; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <P-CSCF_discovery> "
                  "parameter is not valid (%d)",
                  data->command.cgdcont.P_CSCF_discovery);
1945
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
        ret_code = RETURNerror;
        break;
      }

      p_cscf = data->command.cgdcont.P_CSCF_discovery;
    }

    if (data->mask & AT_CGDCONT_IM_CN_SIGNALLING_FLAG_IND_MASK) {
      if ( (data->command.cgdcont.IM_CN_Signalling_Flag_Ind < AT_CGDCONT_IM_CM_MIN)
           || (data->command.cgdcont.IM_CN_Signalling_Flag_Ind > AT_CGDCONT_IM_CM_MAX) ) {
        /* The value of the IM CN subsystem-related signalling
         * support indication is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <IM_CN_Signalling_Flag_Ind> "
                  "parameter is not valid (%d)",
                  data->command.cgdcont.IM_CN_Signalling_Flag_Ind);
1962
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975
        ret_code = RETURNerror;
        break;
      }

      im_cn_signalling = data->command.cgdcont.IM_CN_Signalling_Flag_Ind;
    }

    /*
     * Setup PDN context
     */
    if (reset_pdn) {
      /* A special form of the set command, +CGDCONT=<cid> causes
       * the values for context number <cid> to become undefined */
1976
      ret_code = nas_proc_reset_pdn(user, cid);
gauthier's avatar
 
gauthier committed
1977 1978
    } else {
      /* Define a new PDN connection */
1979
      ret_code = nas_proc_set_pdn(user, cid, pdn_type, apn,
gauthier's avatar
 
gauthier committed
1980 1981 1982 1983 1984 1985 1986
                                  ipv4_addr_allocation, emergency,
                                  p_cscf, im_cn_signalling);
    }

    if (ret_code != RETURNok) {
      LOG_TRACE(ERROR, "USR-MAIN  - Failed to setup PDN connection "
                "(<cid>=%d)", data->command.cgdcont.cid);
1987
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
1988 1989 1990 1991 1992 1993 1994 1995 1996
    }

    break;

  case AT_COMMAND_GET:
    /*
     * Read command returns the current settings for each
     * defined PDN connection/default EPS bearer context
     */
1997
    cgdcont->n_pdns = nas_proc_get_pdn_param(user->esm_data, cgdcont->cid,
gauthier's avatar
 
gauthier committed
1998 1999 2000 2001 2002 2003
                      cgdcont->PDP_type,
                      cgdcont->APN,
                      AT_CGDCONT_RESP_SIZE);

    if (cgdcont->n_pdns == 0) {
      LOG_TRACE(ERROR, "USR-MAIN  - No any PDN context is defined");
2004
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2005 2006 2007 2008 2009 2010 2011 2012 2013 2014
    }

    break;

  case AT_COMMAND_TST:
    /*
     * Test command returns values supported as a compound value
     */
  {
    /* Get the maximum value of a PDN context identifier */
2015
    int cid_max = nas_proc_get_pdn_range(user->esm_data);
gauthier's avatar
 
gauthier committed
2016 2017 2018

    if (cid_max > AT_CGDCONT_RESP_SIZE) {
      /* The range is defined by the user interface */
2019
      at_response->response.cgdcont.tst.n_cid =
gauthier's avatar
 
gauthier committed
2020 2021 2022
        AT_CGDCONT_RESP_SIZE;
    } else {
      /* The range is defined by the ESM sublayer application */
2023
      at_response->response.cgdcont.tst.n_cid = cid_max;
gauthier's avatar
 
gauthier committed
2024 2025 2026 2027 2028 2029 2030
    }
  }
  break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGDCONT command type %d is not supported",
              data->type);
2031
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgact()                                     **
 **                                                                        **
 ** Description: Executes the AT CGACT command operations:                 **
 **      The AT CGACT command is used to activate or deactivate    **
 **      the specified PDP context(s) or PDN/EPS bearer context(s) **
 **      for E-UTRAN                                               **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
2054
static int _nas_user_proc_cgact(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
2055 2056
{
  LOG_FUNC_IN;
2057
  nas_user_context_t *nas_user_context = user->nas_user_context;
2058
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
2059 2060

  int ret_code = RETURNok;
2061
  at_cgact_resp_t *cgact = &at_response->response.cgact;
gauthier's avatar
 
gauthier committed
2062 2063 2064 2065 2066
  memset(cgact, 0, sizeof(at_cgact_resp_t));

  int cid = -1;
  int state = AT_CGACT_STATE_DEFAULT;

2067 2068 2069 2070
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CGACT_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
2071 2072 2073

  switch (data->type) {
  case AT_COMMAND_SET:
2074
    if (nas_user_context->sim_status != NAS_USER_READY) {
2075
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * The execution command is used to activate or deactivate
     * the specified PDN/EPS bearer context(s).
     */
    if (data->mask & AT_CGACT_STATE_MASK) {
      if ( (data->command.cgact.state < AT_CGACT_STATE_MIN) ||
           (data->command.cgact.state > AT_CGACT_STATE_MAX) ) {
        /* The value of the PDP context activation status is
         * not valid; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <state> parameter is "
                  "not valid (%d)",  data->command.cgact.state);
2090
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103
        ret_code = RETURNerror;
        break;
      }

      state = data->command.cgact.state;
    }

    if (data->mask & AT_CGACT_CID_MASK) {
      if (data->command.cgact.cid < AT_CGACT_CID_MIN) {
        /* The value of the PDP context identifier is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <cid> parameter is "
                  "not valid (%d)",  data->command.cgact.cid);
2104
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117
        ret_code = RETURNerror;
        break;
      }

      cid = data->command.cgact.cid;
    }

    /*
     * Activate/Deactivate PDN context
     */
    ret_code = RETURNerror;

    if (state == AT_CGACT_DEACTIVATED) {
2118
      ret_code = nas_proc_deactivate_pdn(user, cid);
gauthier's avatar
 
gauthier committed
2119
    } else if (state == AT_CGACT_ACTIVATED) {
2120
      ret_code = nas_proc_activate_pdn(user, cid);
gauthier's avatar
 
gauthier committed
2121 2122 2123 2124 2125 2126 2127
    }

    if (ret_code != RETURNok) {
      LOG_TRACE(ERROR, "USR-MAIN  - Failed to %s PDN context "
                "(<state>=%d,<cid>=%d)",
                (state != AT_CGACT_ACTIVATED)? "deactivate" :
                "activate", state, cid);
2128
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2129 2130 2131 2132 2133 2134 2135 2136 2137
    }

    break;

  case AT_COMMAND_GET:
    /*
     * The read command returns the current activation states for
     * all the defined PDN/EPS bearer contexts
     */
2138
    cgact->n_pdns = nas_proc_get_pdn_status(user, cgact->cid, cgact->state,
gauthier's avatar
 
gauthier committed
2139 2140 2141 2142
                                            AT_CGACT_RESP_SIZE);

    if (cgact->n_pdns == 0) {
      LOG_TRACE(ERROR, "USR-MAIN  - No any PDN context is defined");
2143
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157
    }

    break;

  case AT_COMMAND_TST:
    /*
     * The test command is used for requesting information on the
     * supported PDN/EPS bearer context activation states.
     */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGACT command type %d is not supported",
              data->type);
2158
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cmee()                                      **
 **                                                                        **
 ** Description: Executes the AT CMEE command operations:                  **
 **      The AT CMEE command disables or enables the use of final  **
 **      result code +CME ERROR: <err> as an indication of an er-  **
 **      ror relating to the functionality of the MT.              **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **          Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
2182
static int _nas_user_proc_cmee(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
2183 2184 2185 2186
{
  LOG_FUNC_IN;

  int ret_code = RETURNok;
2187 2188
  at_response_t *at_response = user->at_response;
  at_cmee_resp_t *cmee = &at_response->response.cmee;
gauthier's avatar
 
gauthier committed
2189 2190 2191 2192
  memset(cmee, 0, sizeof(at_cmee_resp_t));

  int n = AT_CMEE_N_DEFAULT;

2193 2194 2195 2196
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CMEE_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213

  switch (data->type) {
  case AT_COMMAND_ACT:    /* ATV0, ATV1 response format commands */
  case AT_COMMAND_SET:

    /*
     * The set command controls the presentation of final result code
     * +CME ERROR: <err> as an indication of an error relating to the
     * functionality of the MT.
     */
    if (data->mask & AT_CMEE_N_MASK) {
      if ( (data->command.cmee.n < AT_CMEE_N_MIN) ||
           (data->command.cmee.n > AT_CMEE_N_MAX) ) {
        /* The value of the numeric parameter is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <n> parameter is not valid"
                  " (%d)", data->command.cmee.n);
2214
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257
        ret_code = RETURNerror;
        break;
      }

      /* Set to the selected parameter value */
      n = data->command.cmee.n;
    }

    /* Disable/Enable final result code logging */
    switch (n) {
    case AT_CMEE_OFF:
      /* Disable logging of final result code */
      at_error_set_format(AT_ERROR_OFF);
      break;

    case AT_CMEE_NUMERIC:
      /* Enable logging of numeric final result code */
      at_error_set_format(AT_ERROR_NUMERIC);
      break;

    case AT_CMEE_VERBOSE:
      /* Enable logging of verbose final result code */
      at_error_set_format(AT_ERROR_VERBOSE);
      break;

    default:
      break;
    }

    break;

  case AT_COMMAND_GET:
    /*
     * Read command returns the status of the final result code
     * presentation.
     */
    n = at_error_get_format();
    cmee->n = ( (n == AT_ERROR_OFF)? AT_CMEE_OFF :
                (n == AT_ERROR_NUMERIC)? AT_CMEE_NUMERIC :
                (n == AT_ERROR_VERBOSE)? AT_CMEE_VERBOSE : RETURNerror);

    if (cmee->n == RETURNerror) {
      LOG_TRACE(ERROR, "USR-MAIN  - Failed to get format of the final result code");
2258
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271
    }

    break;

  case AT_COMMAND_TST:
    /*
     * Test command returns values supported as a compound value
     */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CMEE command type %d is not supported",
              data->type);
2272
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_clck()                                      **
 **                                                                        **
 ** Description: Executes the AT CLCK command operations:                  **
 **      The AT CLCK command locks, unlocks or interrogates a MT   **
 **      or a network facility.                                    **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **          Others:    None                                       **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
2295
static int _nas_user_proc_clck(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
2296 2297
{
  LOG_FUNC_IN;
2298
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
2299 2300

  int ret_code = RETURNok;
2301
  at_clck_resp_t *clck = &at_response->response.clck;
gauthier's avatar
 
gauthier committed
2302 2303
  memset(clck, 0, sizeof(at_clck_resp_t));

2304 2305 2306 2307
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CLCK_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322

  switch (data->type) {
  case AT_COMMAND_SET:

    /*
     * Execution command locks, unlocks or returns status of a network
     * facility
     */

    /* Check facility parameter */
    if (strncmp(data->command.clck.fac, AT_CLCK_SC,
                AT_CLCK_FAC_SIZE) != 0) {
      /* Facilities other than SIM is not supported */
      LOG_TRACE(ERROR, "USR-MAIN  - Facility is not supported (%s)",
                data->command.clck.fac);
2323
      at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
2324 2325 2326 2327 2328 2329 2330
      ret_code = RETURNerror;
      break;
    }

    /* Check password parameter */
    if (data->mask & AT_CLCK_PASSWD_MASK) {
      /* Check the PIN code */
2331
      if (strncmp(user->nas_user_nvdata->PIN,
gauthier's avatar
 
gauthier committed
2332 2333 2334 2335
                  data->command.clck.passwd, USER_PIN_SIZE) != 0) {
        /* The PIN code is NOT matching; return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - Password is not correct "
                  "(%s)", data->command.clck.passwd);
2336
        at_response->cause_code = AT_ERROR_INCORRECT_PASSWD;
gauthier's avatar
 
gauthier committed
2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350
        ret_code = RETURNerror;
        break;
      }
    }

    /* Execute the command with specified mode of operation */
    switch (data->command.clck.mode) {
    case AT_CLCK_UNLOCK:

      /* unlock facility */
      if ( !(data->mask & AT_CLCK_PASSWD_MASK) ) {
        /* unlock requires password */
        LOG_TRACE(ERROR, "USR-MAIN  - unlock mode of operation "
                  "requires a password");
2351
        at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
2352 2353 2354 2355 2356 2357
        ret_code = RETURNerror;
        break;
      }

      LOG_TRACE(ERROR, "USR-MAIN  - unlock mode of operation "
                "is not supported");
2358
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2359 2360 2361 2362 2363 2364 2365 2366 2367 2368
      ret_code = RETURNerror;
      break;

    case AT_CLCK_LOCK:

      /* lock facility */
      if ( !(data->mask & AT_CLCK_PASSWD_MASK) ) {
        /* unlock requires password */
        LOG_TRACE(ERROR, "USR-MAIN  - lock mode of operation "
                  "requires a password");
2369
        at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
2370 2371 2372 2373 2374 2375
        ret_code = RETURNerror;
        break;
      }

      LOG_TRACE(ERROR, "USR-MAIN  - lock mode of operation "
                "is not supported");
2376
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387
      ret_code = RETURNerror;
      break;

    case AT_CLCK_STATUS:
      /* Read facility status */
      clck->status = AT_CLCK_RESP_STATUS_NOT_ACTIVE;
      break;

    default:
      LOG_TRACE(ERROR, "USR-MAIN  - <mode> parameter is not valid"
                " (%d)", data->command.clck.mode);
2388
      at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403
      ret_code = RETURNerror;
      break;
    }

    break;

  case AT_COMMAND_TST:
    /*
     * Test command returns values supported as a compound value
     */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CLCK command type %d is not supported",
              data->type);
2404
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cgpaddr()                                   **
 **                                                                        **
 ** Description: Executes the AT CGPADDR command operations:               **
 **      The AT CGPADDR command returns a list of PDP addresses    **
 **      for the specified context identifiers                     **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
2426
static int _nas_user_proc_cgpaddr(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
2427 2428
{
  LOG_FUNC_IN;
2429
  nas_user_context_t *nas_user_context = user->nas_user_context;
2430
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
2431 2432

  int ret_code = RETURNok;
2433
  at_cgpaddr_resp_t *cgpaddr = &at_response->response.cgpaddr;
gauthier's avatar
 
gauthier committed
2434 2435 2436 2437
  memset(cgpaddr, 0, sizeof(at_cgpaddr_resp_t));

  int cid = -1;

2438 2439 2440 2441
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CGPADDR_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
2442 2443 2444

  switch (data->type) {
  case AT_COMMAND_SET:
2445
    if (nas_user_context->sim_status != NAS_USER_READY) {
2446
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459
      LOG_FUNC_RETURN(RETURNerror);
    }

    /*
     * The execution command returns a list of PDP addresses for
     * the specified context identifiers
     */
    if (data->mask & AT_CGPADDR_CID_MASK) {
      if (data->command.cgpaddr.cid < AT_CGPADDR_CID_MIN) {
        /* The value of the PDP context identifier is not valid;
         * return an error message */
        LOG_TRACE(ERROR, "USR-MAIN  - <cid> parameter is "
                  "not valid (%d)",  data->command.cgpaddr.cid);
2460
        at_response->cause_code = AT_ERROR_INCORRECT_PARAMETERS;
gauthier's avatar
 
gauthier committed
2461 2462 2463 2464 2465 2466 2467 2468 2469 2470
        ret_code = RETURNerror;
        break;
      }

      cid = data->command.cgpaddr.cid;
    }

    /*
     * Get the PDP addresses
     */
2471
    cgpaddr->n_pdns = nas_proc_get_pdn_addr(user, cid, cgpaddr->cid,
gauthier's avatar
 
gauthier committed
2472 2473 2474 2475 2476 2477
                                            cgpaddr->PDP_addr_1,
                                            cgpaddr->PDP_addr_2,
                                            AT_CGPADDR_RESP_SIZE);

    if (cgpaddr->n_pdns == 0) {
      LOG_TRACE(ERROR, "USR-MAIN  - No any PDN context is defined");
2478
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2479 2480 2481 2482 2483 2484 2485 2486
    }

    break;

  case AT_COMMAND_TST:
    /*
     * The test command returns a list of defined <cid>s.
     */
2487
    cgpaddr->n_pdns = nas_proc_get_pdn_addr(user, cid, cgpaddr->cid,
gauthier's avatar
 
gauthier committed
2488 2489 2490 2491 2492 2493 2494 2495
                                            cgpaddr->PDP_addr_1,
                                            cgpaddr->PDP_addr_2,
                                            AT_CGPADDR_RESP_SIZE);
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CGPADDR command type %d is "
              "not supported", data->type);
2496
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_cnum()                                      **
 **                                                                        **
 ** Description: Executes the AT CNUM command operations:                  **
 **      The AT CNUM command returns the MSISDNs related to the    **
 **      subscriber.                                               **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
2518
static int _nas_user_proc_cnum(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
2519 2520
{
  LOG_FUNC_IN;
2521
  nas_user_context_t *nas_user_context = user->nas_user_context;
2522
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
2523 2524

  int ret_code = RETURNok;
2525
  at_cnum_resp_t *cnum = &at_response->response.cnum;
gauthier's avatar
 
gauthier committed
2526 2527
  memset(cnum, 0, sizeof(at_cnum_resp_t));

2528 2529 2530 2531
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CNUM_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
2532 2533 2534

  switch (data->type) {
  case AT_COMMAND_ACT:
2535
    if (nas_user_context->sim_status != NAS_USER_READY) {
2536
      at_response->cause_code = AT_ERROR_SIM_PIN_REQUIRED;
gauthier's avatar
 
gauthier committed
2537 2538 2539 2540
      LOG_FUNC_RETURN(RETURNerror);
    }

    /* Get the International Mobile Subscriber Identity (IMSI) */
2541
    ret_code = nas_proc_get_msisdn(user, cnum->number, &cnum->type);
gauthier's avatar
 
gauthier committed
2542 2543 2544

    if (ret_code != RETURNok) {
      LOG_TRACE(ERROR, "USR-MAIN  - Failed to get MS dialing number");
2545
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556
    }

    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CNUM command type %d is "
              "not supported", data->type);
2557
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}

/****************************************************************************
 **                                                                        **
 ** Name:    nas_user_proc_clac()                                      **
 **                                                                        **
 ** Description: Executes the AT CLAC command operations:                  **
 **      The AT CLAC command returns the list of AT Commands that  **
 **      are available for the user.                               **
 **                                                                        **
 ** Inputs:  data:      Pointer to the AT command data structure   **
 **                                                                        **
 ** Outputs:     None                                                      **
 **      Return:    RETURNok; RETURNerror;                     **
 **                                                                        **
 ***************************************************************************/
2579
static int _nas_user_proc_clac(nas_user_t *user, const at_command_t *data)
gauthier's avatar
 
gauthier committed
2580 2581
{
  LOG_FUNC_IN;
2582
  at_response_t *at_response = user->at_response;
gauthier's avatar
 
gauthier committed
2583
  int ret_code = RETURNok;
2584
  at_clac_resp_t *clac = &at_response->response.clac;
gauthier's avatar
 
gauthier committed
2585 2586
  memset(clac, 0, sizeof(at_clac_resp_t));

2587 2588 2589 2590
  at_response->id = data->id;
  at_response->type = data->type;
  at_response->mask = AT_RESPONSE_CLAC_MASK;
  at_response->cause_code = AT_ERROR_SUCCESS;
gauthier's avatar
 
gauthier committed
2591 2592 2593 2594 2595 2596 2597 2598 2599

  switch (data->type) {
  case AT_COMMAND_ACT:
    /* Get the list of supported AT commands */
    clac->n_acs = at_command_get_list(clac->ac, AT_CLAC_RESP_SIZE);

    if (clac->n_acs == 0) {
      LOG_TRACE(ERROR, "USR-MAIN  - Failed to get the list of "
                "supported AT commands");
2600
      at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611
    }

    break;

  case AT_COMMAND_TST:
    /* Nothing to do */
    break;

  default:
    LOG_TRACE(ERROR, "USR-MAIN  - AT+CLAC command type %d is "
              "not supported", data->type);
2612
    at_response->cause_code = AT_ERROR_OPERATION_NOT_SUPPORTED;
gauthier's avatar
 
gauthier committed
2613 2614 2615 2616 2617 2618 2619
    ret_code = RETURNerror;
    break;
  }

  LOG_FUNC_RETURN (ret_code);
}