user_api.c 35.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 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 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758
/*
 * Licensed to the OpenAirInterface (OAI) Software Alliance under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The OpenAirInterface Software Alliance licenses this file to You under
 * the OAI Public License, Version 1.1  (the "License"); you may not use this file
 * except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.openairinterface.org/?page_id=698
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *-------------------------------------------------------------------------------
 * For more information about the OpenAirInterface (OAI) Software Alliance:
 *      contact@openairinterface.org
 */

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

Source          user_api.c

Version         0.1

Date            2012/02/28

Product         NAS stack

Subsystem       Application Programming Interface

Author          Frederic Maurel

Description     Implements the API used by the NAS layer running in the UE
                to send/receive message to/from the user application layer

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


#include "user_api.h"
#include "nas_log.h"
#include "socket.h"
#include "device.h"
#include "nas_user.h"

#include "at_response.h"
#include "at_error.h"
#include "esm_ebr.h"

#include "user_indication.h"

#include <string.h> // strerror, memset
#include <netdb.h>  // gai_strerror
#include <errno.h>  // errno
#include <stdio.h>  // sprintf
#include <unistd.h> // gethostname

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

/*
 * Asynchronous notification procedure handlers
 */
static int _user_api_registration_handler(user_api_id_t *user_api_id, unsigned char id, const void* data, size_t size);
static int _user_api_location_handler(user_api_id_t *user_api_id, unsigned char id, const void* data, size_t size);
static int _user_api_network_handler(user_api_id_t *user_api_id, unsigned char id, const void* data, size_t size);
static int _user_api_pdn_connection_handler(user_api_id_t *user_api_id, unsigned char id, const void* data, size_t size);

static int _user_api_send(user_api_id_t *user_api_id, at_response_t* data);

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

/****************************************************************************
 **                                                                        **
 ** Name:        user_api_initialize()                                     **
 **                                                                        **
 ** Description: Initializes the user API from which the NAS layer         **
 **              will send/receive messages to/from the user application   **
 **              layer                                                     **
 **                                                                        **
 ** Inputs:      host:          The name of the host from which the user   **
 **                             application layer will connect             **
 **              port:          The local port number                      **
 **              Others:        None                                       **
 **                                                                        **
 ** Outputs:     Return:        RETURNerror, RETURNok                      **
 **              Others:        _user_api_id                               **
 **                                                                        **
 ***************************************************************************/
int user_api_initialize(user_api_id_t *user_api_id, const char* host, const char* port,
                        const char* devname, const char* devparams)
{
  LOG_FUNC_IN;

  gethostname(user_api_id->send_buffer, USER_API_SEND_BUFFER_SIZE);

  if (devname != NULL) {
    /* Initialize device handlers */
    user_api_id->open  = device_open;
    user_api_id->getfd = device_get_fd;
    user_api_id->recv  = device_read;
    user_api_id->send  = device_write;
    user_api_id->close = device_close;

    /* Initialize communication channel */
    user_api_id->endpoint = user_api_id->open(DEVICE, devname, devparams);

    if (user_api_id->endpoint == NULL) {
      LOG_TRACE(ERROR, "USR-API   - Failed to open connection endpoint, "
                "%s", strerror(errno));
      LOG_FUNC_RETURN (RETURNerror);
    }

    LOG_TRACE(INFO, "USR-API   - User's communication device %d is OPENED "
              "on %s/%s", user_api_get_fd(user_api_id), user_api_id->send_buffer, devname);
  } else {
    /* Initialize network socket handlers */
    user_api_id->open  = socket_udp_open;
    user_api_id->getfd = socket_get_fd;
    user_api_id->recv  = socket_recv;
    user_api_id->send  = socket_send;
    user_api_id->close = socket_close;

    /* Initialize communication channel */
    user_api_id->endpoint = user_api_id->open(SOCKET_SERVER, host, port);

    if (user_api_id->endpoint == NULL) {
      LOG_TRACE(ERROR, "USR-API   - Failed to open connection endpoint, "
                "%s", ( (errno < 0) ?gai_strerror(errno) : strerror(errno) ));
      LOG_FUNC_RETURN (RETURNerror);
    }

    LOG_TRACE(INFO, "USR-API   - User's UDP socket %d is BOUND to %s/%s",
              user_api_get_fd(user_api_id), user_api_id->send_buffer, port);
  }

  /* Register the asynchronous notification handlers */
  if (user_ind_register(USER_IND_REG, 0, _user_api_registration_handler) != RETURNok) {
    LOG_TRACE(WARNING, "USR-API   - "
              "Failed to register registration notification");
  } else if (user_ind_register(USER_IND_LOC, 0, _user_api_location_handler) != RETURNok) {
    LOG_TRACE(WARNING, "USR-API   - "
              "Failed to register location notification");
  } else if (user_ind_register(USER_IND_PLMN, 0, _user_api_network_handler) != RETURNok) {
    LOG_TRACE(WARNING, "USR-API   - "
              "Failed to register network notification");
  } else if (user_ind_register(USER_IND_PLMN, 0, NULL) != RETURNok) {
    LOG_TRACE(WARNING, "USR-API   - Failed to enable network notification");
  } else if (user_ind_register(USER_IND_PDN, 0, _user_api_pdn_connection_handler) != RETURNok) {
    LOG_TRACE(WARNING, "USR-API   - "
              "Failed to register PDN connection notification");
  } else if (user_ind_register(USER_IND_PDN, AT_CGACT, NULL) != RETURNok) {
    LOG_TRACE(WARNING, "USR-API   - "
              "Failed to enable PDN connection notification");
  } else {
    LOG_TRACE(INFO, "USR-API   - "
              "Notification handlers successfully registered");
  }

  LOG_FUNC_RETURN (RETURNok);
}

/****************************************************************************
 **                                                                        **
 ** Name:        user_api_get_fd()                                         **
 **                                                                        **
 ** Description: Get the file descriptor of the connection endpoint used   **
 **              to send/receive messages to/from the user application     **
 **              layer                                                     **
 **                                                                        **
 ** Inputs:      None                                                      **
 **              Others:        _user_api_id                               **
 **                                                                        **
 ** Outputs:     Return:        The file descriptor of the connection end- **
 **                             point used by the user application layer   **
 **              Others:        None                                       **
 **                                                                        **
 ***************************************************************************/
int user_api_get_fd(user_api_id_t *user_api_id)
{
  LOG_FUNC_IN;
  LOG_FUNC_RETURN (user_api_id->getfd(user_api_id->endpoint));
}

/****************************************************************************
 **                                                                        **
 ** Name:        user_api_get_data()                                       **
 **                                                                        **
 ** Description: Get a generic pointer to the user data structure at the   **
 **              given index. Casting to the proper type is necessary      **
 **              before its usage.                                         **
 **                                                                        **
 ** Inputs:      index:         Index of the user data structure to get    **
 **                                                                        **
 ** Outputs:     Return:        A generic pointer to the user data         **
 **                             structure                                  **
 **              Others:        None                                       **
 **                                                                        **
 ***************************************************************************/
const void* user_api_get_data(user_at_commands_t *commands, int index)
{
  LOG_FUNC_IN;

  if (index < commands->n_cmd) {
    LOG_FUNC_RETURN ((void*)(&commands->cmd[index]));
  }

  LOG_FUNC_RETURN (NULL);
}

/****************************************************************************
 **                                                                        **
 ** Name:        user_api_read_data()                                      **
 **                                                                        **
 ** Description: Read data received from the user application layer        **
 **                                                                        **
 **          Others:            _user_api_id                               **
 **                                                                        **
 ** Outputs: Return:            The number of bytes read when success;     **
 **                             RETURNerror Otherwise                      **
 **          Others:            user_api_id->recv_buffer, _user_api_id        **
 **                                                                        **
 ***************************************************************************/
int user_api_read_data(user_api_id_t *user_api_id)
{
  LOG_FUNC_IN;

  int rbytes;

  memset(user_api_id->recv_buffer, 0, USER_API_RECV_BUFFER_SIZE);

  /* Receive data from the user application layer */
  rbytes = user_api_id->recv(user_api_id->endpoint, user_api_id->recv_buffer, USER_API_RECV_BUFFER_SIZE);

  if (rbytes == RETURNerror) {
    LOG_TRACE(ERROR, "USR-API   - recv() failed, %s", strerror(errno));
    LOG_FUNC_RETURN (RETURNerror);
  } else if (rbytes == 0) {
    //LOG_TRACE(WARNING, "USR-API   - A signal was caught");
  } else {
    LOG_TRACE(INFO, "USR-API   - %d bytes received "
              "from the user application layer", rbytes);
    LOG_DUMP(user_api_id->recv_buffer, rbytes);
  }

  LOG_FUNC_RETURN (rbytes);
}

/****************************************************************************
 **                                                                        **
 ** Name:        user_api_set_data()                                       **
 **                                                                        **
 ** Description: Set content of data received buffer to allow loop back    **
 **                                                                        **
 ** Inputs:         message:    Message to set into the received buffer    **
 **                                                                        **
 ** Outputs:         Return:    The number of bytes write when success;    **
 **                             RETURNerror Otherwise                      **
 **                  Others:    user_api_id->recv_buffer                      **
 **                                                                        **
 ***************************************************************************/
int user_api_set_data(user_api_id_t *user_api_id, char *message)
{
  LOG_FUNC_IN;

  int rbytes;

  memset(user_api_id->recv_buffer, 0, USER_API_RECV_BUFFER_SIZE);

  strncpy(user_api_id->recv_buffer, message, USER_API_RECV_BUFFER_SIZE);
  rbytes = strlen(user_api_id->recv_buffer);

  LOG_TRACE(INFO, "USR-API   - %d bytes write", rbytes);
  LOG_DUMP(user_api_id->recv_buffer, rbytes);

  LOG_FUNC_RETURN (rbytes);
}

/****************************************************************************
 **                                                                        **
** Name:        user_api_send_data()                                      **
 **                                                                        **
 ** Description: Send data to the user application layer                   **
 **                                                                        **
 **              length:        Number of bytes to send                    **
 **                                                                        **
 ** Outputs:     Return:        The number of bytes sent when success;     **
 **                             RETURNerror Otherwise                      **
 **              Others:        None                                       **
 **                                                                        **
 ***************************************************************************/
static int _user_api_send_data(user_api_id_t *user_api_id, int length)
{
  int sbytes = user_api_id->send(user_api_id->endpoint, user_api_id->send_buffer, length);

  if (sbytes == RETURNerror) {
    LOG_TRACE(ERROR, "USR-API   - send() failed, %s", strerror(errno));
    return RETURNerror;
  } else if (sbytes == 0) {
    LOG_TRACE(WARNING, "USR-API   - A signal was caught");
  } else {
    LOG_TRACE(INFO, "USR-API   - %d bytes sent "
              "to the user application layer", sbytes);
    LOG_DUMP(user_api_id->send_buffer, sbytes);
  }

  return sbytes;
}

/****************************************************************************
 **                                                                        **
 ** Name:        user_api_close()                                          **
 ***************************************************************************/
 int user_api_send_data(user_api_id_t *user_api_id, int length)
{
  LOG_FUNC_IN;

  /* Send data to the user application layer */
  int sbytes = 0;

  if (length > 0) {
    sbytes = _user_api_send_data(user_api_id, length);
  }

  LOG_FUNC_RETURN (sbytes);
}

/****************************************************************************
 **                                                                        **
 ** Name:        user_api_close()                                          **
 **                                                                        **
 ** Description: Close the user API from which the NAS layer sent/received **
 **              messages to/from the user application layer               **
 **                                                                        **
 **              Others:        None                                       **
 **                                                                        **
 ** Outputs:     Return:        None                                       **
 **                                                                        **
 ***************************************************************************/
void user_api_close(user_api_id_t *user_api_id)
{
  LOG_FUNC_IN;

  /* Cleanup the connection endpoint */
  user_api_id->close(user_api_id->endpoint) ;
  user_api_id->endpoint = NULL;

  LOG_FUNC_OUT;
}

/****************************************************************************
 **                                                                        **
 ** Name:  user_api_decode_data()                                    **
 **                                                                        **
 ** Description: Parses the message received from the user application     **
 **    (mainly AT commands) and fills the user data structure.   **
 **    Returns an AT syntax error code to the user application   **
 **    layer when the AT command failed to be decoded.           **
 **                                                                        **
 ** Inputs:  length:  Number of bytes to decode                  **
 **                                                                        **
 ** Outputs:   Return:  The number of AT commands succeessfully    **
 **       decoded                                    **
 **                                                                        **
 ***************************************************************************/
int user_api_decode_data(user_api_id_t *user_api_id, user_at_commands_t *commands, int length)
{
  LOG_FUNC_IN;

  /* Parse the AT command line */
  LOG_TRACE(INFO, "USR-API   - Decode user data: %s", user_api_id->recv_buffer);
  commands->n_cmd = at_command_decode(user_api_id->recv_buffer, length,
                                       commands->cmd);

  if (commands->n_cmd > 0) {
    /* AT command data received from the user application layer
     * has been successfully decoded */
    LOG_TRACE(INFO, "USR-API   - %d AT command%s ha%s been successfully "
              "decoded", commands->n_cmd,
              (commands->n_cmd > 1) ? "s" : "",
              (commands->n_cmd > 1) ? "ve" : "s");
  } else {
    int bytes;

    /* Failed to decode AT command data received from the user
     * application layer; Return syntax error code message */
    LOG_TRACE(ERROR, "USR-API   - Syntax error: Failed to decode "
              "AT command data %s", user_api_id->recv_buffer);

    /* Encode the syntax error code message */
    bytes = at_error_encode(user_api_id->send_buffer, AT_ERROR_SYNTAX,
                            AT_ERROR_OPERATION_NOT_SUPPORTED);

    // FIXME move _user_data call
    /* Send the syntax error code message */
    _user_api_send_data(user_api_id, bytes);
  }

  LOG_FUNC_RETURN (commands->n_cmd);
}

/****************************************************************************
 **                                                                        **
 ** Name:  user_api_encode_data()                                    **
 **                                                                        **
 ** Description: Encodes AT command response message                       **
 **                                                                        **
 ** Inputs   data:    Generic pointer to the data to encode      **
 **      success_code:  Indicates whether success code has to be   **
 **       displayed or not (covers the case where    **
 **       more than one AT command is executed in    **
 **       the same user command line).               **
 **      Others:  None                                       **
 **                                                                        **
 ** Outputs:   Return:  The number of characters that have been    **
 **       successfully encoded;                      **
 **       RETURNerror otherwise.                     **
 **                                                                        **
 ***************************************************************************/
int user_api_encode_data(user_api_id_t *user_api_id, const void* data, int success_code)
{
  LOG_FUNC_IN;

  const at_response_t* user_data = (at_response_t*)(data);
  int bytes;

  /* Encode AT command error message */
  if (user_data->cause_code != AT_ERROR_SUCCESS) {
    bytes = at_error_encode(user_api_id->send_buffer, AT_ERROR_CME,
                            user_data->cause_code);
  }
  /* Encode AT command response message */
  else {
    bytes = at_response_encode(user_api_id->send_buffer, user_data);

    /* Add success result code */
    if ( (success_code) && (bytes != RETURNerror) ) {
      bytes += at_error_encode(&user_api_id->send_buffer[bytes],
                               AT_ERROR_OK, 0);
    }
  }

  if (bytes != RETURNerror) {
    LOG_TRACE(INFO, "USR-API   - %d bytes encoded", bytes);
  } else {
    LOG_TRACE(ERROR, "USR-API   - Syntax error: Failed to encode AT "
              "response data (%d)", user_data->id);
  }

  LOG_FUNC_RETURN (bytes);
}

/****************************************************************************
 **                                                                        **
 ** Name:  user_api_callback()                                       **
 **                                                                        **
 ** Description: Notifies the user application that asynchronous notifica- **
 **    tion has been received from the EPS Mobility Management   **
 **    sublayer.                                                 **
 **                                                                        **
 ** Inputs:  stat:    Network registration status                **
 **    tac:   Location/Tracking Area Code                **
 **    ci:    Indentifier of the serving cell            **
 **    AcT:   Access Technology supported by the cell    **
 **    data:    Data string to display to the user         **
 **    size:    Size of the notification data (only used   **
 **       to display string information to the user  **
 **       application)                               **
 **      Others:  None                                       **
 **                                                                        **
 ** Outputs:   None                                                      **
 **    Return:  RETURNok, RETURNerror                      **
 **      Others:  None                                       **
 **                                                                        **
 ***************************************************************************/
int user_api_emm_callback(user_api_id_t *user_api_id, Stat_t stat, tac_t tac, ci_t ci, AcT_t AcT,
                          const char* data, size_t size)
{
  LOG_FUNC_IN;

  int rc = RETURNok;

  if (size > 1) {
    /*
     * The list of available operators present in the network has to be
     * displayed to the user application
     */
    rc = user_ind_notify(user_api_id, USER_IND_PLMN, (void*)data, size);
  } else {
    user_indication_t ind;
    ind.notification.reg.status = stat;

    if (size > 0) {
      /* The UE's network registration status has changed */
      rc = user_ind_notify(user_api_id, USER_IND_REG, (void*)&ind, 0);
    }

    if (rc != RETURNerror) {
      /* The UE's location area has changed or,
       * the UE's network registration status has changed and
       * only location information notification is enabled */
      ind.notification.loc.tac = tac;
      ind.notification.loc.ci  = ci;
      ind.notification.loc.AcT = AcT;
      rc = user_ind_notify(user_api_id, USER_IND_LOC, (void*)&ind, 0);
    }
  }

  if (rc != RETURNerror) {
    LOG_FUNC_RETURN (RETURNok);
  }

  LOG_FUNC_RETURN (rc);
}

/****************************************************************************
 **                                                                        **
 ** Name:  user_api_esm_callback()                                   **
 **                                                                        **
 ** Description: Notifies the user application that asynchronous notifica- **
 **    tion has been received from the EPS Session Management    **
 **    sublayer.                                                 **
 **                                                                        **
 ** Inputs:  cid:   PDN connection identifier                  **
 **    state:   PDN connection status                      **
 **      Others:  None                                       **
 **                                                                        **
 ** Outputs:   None                                                      **
 **    Return:  RETURNok, RETURNerror                      **
 **      Others:  None                                       **
 **                                                                        **
 ***************************************************************************/
int user_api_esm_callback(user_api_id_t *user_api_id, int cid, network_pdn_state_t state)
{
  LOG_FUNC_IN;

  int rc = RETURNok;

  user_indication_t ind;
  ind.notification.pdn.cid = cid;
  ind.notification.pdn.status = state;
  /* The status of the specified PDN connection has changed */
  rc = user_ind_notify(user_api_id, USER_IND_PDN, (void*)&ind, 0);

  LOG_FUNC_RETURN (rc);
}

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

/****************************************************************************
 **                                                                        **
 ** Name:  _user_api_send()                                          **
 **                                                                        **
 ** Description: Encodes and sends data to the user application layer      **
 **                                                                        **
 ** Inputs:  data:    The data to send                           **
 **                                                                        **
 ** Outputs:   Return:  The number of bytes sent when success;     **
 **       RETURNerror Otherwise                      **
 **      Others:  None                                       **
 **                                                                        **
 ***************************************************************************/
static int _user_api_send(user_api_id_t *user_api_id, at_response_t* data)
{
  LOG_FUNC_IN;

  /* Encode AT command response message */
  int bytes = at_response_encode(user_api_id->send_buffer, data);

  /* Send the AT command response message to the user application */
  if (bytes != RETURNerror) {
    bytes = _user_api_send_data(user_api_id, bytes);
  }

  LOG_FUNC_RETURN (bytes);
}

/****************************************************************************
 **                                                                        **
 ** Name:  _user_api_registration_handler()                          **
 **                                                                        **
 ** Description: Procedure executed upon receiving registration notifica-  **
 **    tion whenever there is a change in the UE's network re-   **
 **    gistration status in GERAN/UTRAN/E-UTRAN.                 **
 **    The network registration data are then displayed to the   **
 **    user.                                                     **
 **                                                                        **
 ** Inputs:  id:    Network registration AT command identifier **
 **    data:    Generic pointer to the registration data   **
 **    size:    Not used                                   **
 **      Others:  None                                       **
 **                                                                        **
 ** Outputs:   None                                                      **
 **    Return:  The number of bytes actually sent to the   **
 **       user application                           **
 **      Others:  None                                       **
 **                                                                        **
 ***************************************************************************/
static int _user_api_registration_handler(user_api_id_t *user_api_id, unsigned char id, const void* data,
    size_t size)
{
  LOG_FUNC_IN;

  /* Get registration notification data */
  user_ind_reg_t* reg = (user_ind_reg_t*)data;

  /* Setup AT command response message for AT+CEREG? read command */
  at_response_t at_response;
  at_response.id = id; // may be +CREG, +CGREG, +CEREG
  at_response.type = AT_COMMAND_GET;
  at_response.mask = AT_RESPONSE_NO_PARAM;
  at_response.response.cereg.n = AT_CEREG_ON;
  at_response.response.cereg.stat = reg->status;

  /* Encode and send the AT command response message to the user */
  int bytes = _user_api_send(user_api_id, &at_response);

  LOG_FUNC_RETURN (bytes);
}

/****************************************************************************
 **                                                                        **
 ** Name:  _user_api_location_handler()                              **
 **                                                                        **
 ** Description: Procedure executed upon receiving location notification   **
 **    whenever there is a change in the network serving cell    **
 **    in GERAN/UTRAN/E-UTRAN.                                   **
 **    The location data are then displayed to the user.         **
 **                                                                        **
 ** Inputs:  id:    Network registration AT command identifier **
 **    data:    Generic pointer to the registration data   **
 **    size:    Not used                                   **
 **      Others:  None                                       **
 **                                                                        **
 ** Outputs:   None                                                      **
 **    Return:  The number of bytes actually sent to the   **
 **       user application                           **
 **      Others:  None                                       **
 **                                                                        **
 ***************************************************************************/
static int _user_api_location_handler(user_api_id_t *user_api_id, unsigned char id, const void* data,
                                      size_t size)
{
  LOG_FUNC_IN;

  /* Get location notification data */
  user_ind_loc_t* loc = (user_ind_loc_t*)data;

  /* Setup AT command response message for AT+CEREG? read command */
  at_response_t at_response;
  at_response.id = id; // may be +CREG, +CGREG, +CEREG
  at_response.type = AT_COMMAND_GET;
  at_response.mask = (AT_CEREG_RESP_TAC_MASK | AT_CEREG_RESP_CI_MASK);
  at_response.response.cereg.n = AT_CEREG_BOTH;
  at_response.response.cereg.stat = loc->status;
  sprintf(at_response.response.cereg.tac, "%.4x", loc->tac);  // two byte
  sprintf(at_response.response.cereg.ci, "%.8x", loc->ci);  // four byte

  if (at_response.response.cereg.AcT != NET_ACCESS_UNAVAILABLE) {
    at_response.response.cereg.AcT = loc->AcT;
    at_response.mask |= AT_CEREG_RESP_ACT_MASK;
  }

  /* Encode and send the AT command response message to the user */
  int bytes = _user_api_send(user_api_id, &at_response);

  LOG_FUNC_RETURN (bytes);
}

/****************************************************************************
 **                                                                        **
 ** Name:  _user_api_network_handler()                               **
 **                                                                        **
 ** Description: Procedure executed whenever the list of operators present **
 **    in the network has to be displayed to the user.           **
 **                                                                        **
 ** Inputs:  id:    Not used                                   **
 **    data:    Generic pointer to the list of operators   **
 **    size:    The size of the data to display            **
 **      Others:  None                                       **
 **                                                                        **
 ** Outputs:   None                                                      **
 **    Return:  The number of bytes actually sent to the   **
 **       user application                           **
 **      Others:  None                                       **
 **                                                                        **
 ***************************************************************************/
static int _user_api_network_handler(user_api_id_t *user_api_id, unsigned char id, const void* data,
                                     size_t size)
{
  LOG_FUNC_IN;

  /* Setup AT command response message for AT+COPS=? test command */
  at_response_t at_response;
  at_response.id = AT_COPS;
  at_response.type = AT_COMMAND_TST;
  at_response.response.cops.tst.data = (char*)data;
  at_response.response.cops.tst.size = size;

  /* Encode and send the AT command response message to the user */
  int bytes = _user_api_send(user_api_id, &at_response);

  LOG_FUNC_RETURN (bytes);
}

/****************************************************************************
 **                                                                        **
 ** Name:  _user_api_pdn_connection_handler()                        **
 **                                                                        **
 ** Description: Procedure executed upon receiving PDN connection notifi-  **
 **    cation whenever the user or the network has activated or  **
 **    desactivated a PDN connection.                            **
 **    The PDN connection data are then displayed to the user.   **
 **                                                                        **
 ** Inputs:  id:    Not used                                   **
 **    data:    Generic pointer to the PDN connection data **
 **    size:    Not used                                   **
 **      Others:  None                                       **
 **                                                                        **
 ** Outputs:   None                                                      **
 **    Return:  The number of bytes actually sent to the   **
 **       user application                           **
 **      Others:  None                                       **
 **                                                                        **
 ***************************************************************************/
static int _user_api_pdn_connection_handler(user_api_id_t *user_api_id, unsigned char id, const void* data,
    size_t size)
{
  LOG_FUNC_IN;

  /* Get PDN connection notification data */
  user_ind_pdn_t* pdn = (user_ind_pdn_t*)data;

  /* Setup AT command unsolicited result response message for +CGEV */
  at_response_t at_response;
  at_response.id = AT_CGEV;
  at_response.type = AT_COMMAND_GET;
  at_response.mask = AT_RESPONSE_CGEV_MASK;
  at_response.response.cgev.cid = pdn->cid;
  at_response.response.cgev.code = pdn->status;

  /* Encode and send the AT command response message to the user */
  int bytes = _user_api_send(user_api_id, &at_response);

  LOG_FUNC_RETURN (bytes);
}