API/USER: move _user_api_id to nas_user_t

This commit is contained in:
Frédéric Leroy
2017-01-25 13:24:30 +01:00
parent 53fa94785a
commit a99e1a95af
23 changed files with 195 additions and 162 deletions
+56 -87
View File
@@ -45,7 +45,6 @@ Description Implements the API used by the NAS layer running in the UE
#include "device.h"
#include "nas_user.h"
#include "at_command.h"
#include "at_response.h"
#include "at_error.h"
#include "esm_ebr.h"
@@ -69,42 +68,12 @@ Description Implements the API used by the NAS layer running in the UE
/*
* Asynchronous notification procedure handlers
*/
static int _user_api_registration_handler(unsigned char id, const void* data, size_t size);
static int _user_api_location_handler(unsigned char id, const void* data, size_t size);
static int _user_api_network_handler(unsigned char id, const void* data, size_t size);
static int _user_api_pdn_connection_handler(unsigned char id, const void* data, size_t size);
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(at_response_t* data);
/* -------------------
* Connection endpoint
* -------------------
* The connection endpoint is used to send/receive data to/from the
* user application layer. Its definition depends on the underlaying
* mechanism chosen to communicate (network socket, I/O terminal device).
* A connection endpoint is handled using an identifier, and functions
* used to retreive the file descriptor actually allocated by the system,
* to receive data, to send data, and to perform clean up when connection
* is shut down.
* Only one single end to end connection with the user is managed at a
* time.
*/
static struct {
/* Connection endpoint reference */
void* endpoint;
/* Connection endpoint handlers */
void* (*open) (int, const char*, const char*);
int (*getfd)(const void*);
ssize_t (*recv) (void*, char*, size_t);
ssize_t (*send) (const void*, const char*, size_t);
void (*close)(void*);
} _user_api_id;
#define USER_API_OPEN(a, b, c) _user_api_id.open(a, b, c)
#define USER_API_GETFD() _user_api_id.getfd(_user_api_id.endpoint)
#define USER_API_RECV(a, b) _user_api_id.recv(_user_api_id.endpoint, a, b)
#define USER_API_SEND(a, b) _user_api_id.send(_user_api_id.endpoint, a, b)
#define USER_API_CLOSE() _user_api_id.close(_user_api_id.endpoint)
static int _user_api_send(user_api_id_t *user_api_id, at_response_t* data);
/*
* The buffer used to receive data from the user application layer
@@ -141,7 +110,7 @@ static char _user_api_send_buffer[USER_API_SEND_BUFFER_SIZE];
** Others: _user_api_id **
** **
***************************************************************************/
int user_api_initialize(const char* host, const char* port,
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;
@@ -150,35 +119,35 @@ int user_api_initialize(const char* host, const char* port,
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;
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_OPEN(DEVICE, devname, devparams);
user_api_id->endpoint = user_api_id->open(DEVICE, devname, devparams);
if (_user_api_id.endpoint == NULL) {
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_send_buffer, devname);
"on %s/%s", user_api_get_fd(user_api_id), _user_api_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;
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_OPEN(SOCKET_SERVER, host, port);
user_api_id->endpoint = user_api_id->open(SOCKET_SERVER, host, port);
if (_user_api_id.endpoint == NULL) {
if (user_api_id->endpoint == NULL) {
const char* error = ( (errno < 0) ?
gai_strerror(errno) : strerror(errno) );
LOG_TRACE(ERROR, "USR-API - Failed to open connection endpoint, "
@@ -187,7 +156,7 @@ int user_api_initialize(const char* host, const char* port,
}
LOG_TRACE(INFO, "USR-API - User's UDP socket %d is BOUND to %s/%s",
user_api_get_fd(), _user_api_send_buffer, port);
user_api_get_fd(user_api_id), _user_api_send_buffer, port);
}
/* Register the asynchronous notification handlers */
@@ -232,10 +201,10 @@ int user_api_initialize(const char* host, const char* port,
** Others: None **
** **
***************************************************************************/
int user_api_get_fd(void)
int user_api_get_fd(user_api_id_t *user_api_id)
{
LOG_FUNC_IN;
LOG_FUNC_RETURN (USER_API_GETFD());
LOG_FUNC_RETURN (user_api_id->getfd(user_api_id->endpoint));
}
/****************************************************************************
@@ -247,7 +216,6 @@ int user_api_get_fd(void)
** before its usage. **
** **
** Inputs: index: Index of the user data structure to get **
** Others: _user_data **
** **
** Outputs: Return: A generic pointer to the user data **
** structure **
@@ -280,14 +248,14 @@ const void* user_api_get_data(user_at_commands_t *commands, int index)
** Others: _user_api_recv_buffer, _user_api_id **
** **
***************************************************************************/
int user_api_read_data(int fd)
int user_api_read_data(user_api_id_t *user_api_id, int fd)
{
LOG_FUNC_IN;
int rbytes;
/* Sanity check */
int sfd = user_api_get_fd();
int sfd = user_api_get_fd(user_api_id);
if (fd != sfd) {
LOG_TRACE(ERROR, "USR-API - Endpoint %d is not the one created for communication with the user application layer (%d)", fd, sfd);
@@ -297,7 +265,7 @@ int user_api_read_data(int fd)
memset(_user_api_recv_buffer, 0, USER_API_RECV_BUFFER_SIZE);
/* Receive data from the user application layer */
rbytes = USER_API_RECV(_user_api_recv_buffer, USER_API_RECV_BUFFER_SIZE);
rbytes = user_api_id->recv(user_api_id->endpoint, _user_api_recv_buffer, USER_API_RECV_BUFFER_SIZE);
if (rbytes == RETURNerror) {
LOG_TRACE(ERROR, "USR-API - recv() failed, %s", strerror(errno));
@@ -345,7 +313,7 @@ int user_api_set_data(char *message)
/****************************************************************************
** **
** Name: user_api_send_data() **
** Name: user_api_send_data() **
** **
** Description: Send data to the user application layer **
** **
@@ -359,9 +327,9 @@ int user_api_set_data(char *message)
** Others: None **
** **
***************************************************************************/
static int _user_api_send_data(int length)
static int _user_api_send_data(user_api_id_t *user_api_id, int length)
{
int sbytes = USER_API_SEND(_user_api_send_buffer, length);
int sbytes = user_api_id->send(user_api_id->endpoint, _user_api_send_buffer, length);
if (sbytes == RETURNerror) {
LOG_TRACE(ERROR, "USR-API - send() failed, %s", strerror(errno));
@@ -376,12 +344,12 @@ static int _user_api_send_data(int length)
return sbytes;
}
int user_api_send_data(int fd, int length)
int user_api_send_data(user_api_id_t *user_api_id, int fd, int length)
{
LOG_FUNC_IN;
/* Sanity check */
int sfd = user_api_get_fd();
int sfd = user_api_get_fd(user_api_id);
if (fd != sfd) {
LOG_TRACE(ERROR, "USR-API - Endpoint %d is not the one created for communication with the user application layer (%d)", fd, sfd);
@@ -392,7 +360,7 @@ int user_api_send_data(int fd, int length)
int sbytes = 0;
if (length > 0) {
sbytes = _user_api_send_data(length);
sbytes = _user_api_send_data(user_api_id, length);
}
LOG_FUNC_RETURN (sbytes);
@@ -414,12 +382,12 @@ int user_api_send_data(int fd, int length)
** Others: _user_api_id **
** **
***************************************************************************/
void user_api_close(int fd)
void user_api_close(user_api_id_t *user_api_id, int fd)
{
LOG_FUNC_IN;
/* Sanity check */
int sfd = user_api_get_fd();
int sfd = user_api_get_fd(user_api_id);
if (fd != sfd) {
LOG_TRACE(ERROR, "USR-API - Endpoint %d is not the one created for communication with the user application layer (%d)", fd, sfd);
@@ -428,8 +396,8 @@ void user_api_close(int fd)
}
/* Cleanup the connection endpoint */
USER_API_CLOSE();
_user_api_id.endpoint = NULL;
user_api_id->close(user_api_id->endpoint) ;
user_api_id->endpoint = NULL;
LOG_FUNC_OUT;
}
@@ -451,7 +419,7 @@ void user_api_close(int fd)
** Others: _user_api_send_buffer, _user_data **
** **
***************************************************************************/
int user_api_decode_data(user_at_commands_t *commands, int length)
int user_api_decode_data(user_api_id_t *user_api_id, user_at_commands_t *commands, int length)
{
LOG_FUNC_IN;
@@ -479,8 +447,9 @@ int user_api_decode_data(user_at_commands_t *commands, int length)
bytes = at_error_encode(_user_api_send_buffer, AT_ERROR_SYNTAX,
AT_ERROR_OPERATION_NOT_SUPPORTED);
// FIXME move _user_data call
/* Send the syntax error code message */
(void) _user_api_send_data(bytes);
_user_api_send_data(user_api_id, bytes);
}
LOG_FUNC_RETURN (commands->n_cmd);
@@ -561,7 +530,7 @@ int user_api_encode_data(const void* data, int success_code)
** Others: None **
** **
***************************************************************************/
int user_api_emm_callback(Stat_t stat, tac_t tac, ci_t ci, AcT_t AcT,
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;
@@ -573,14 +542,14 @@ int user_api_emm_callback(Stat_t stat, tac_t tac, ci_t ci, AcT_t AcT,
* The list of available operators present in the network has to be
* displayed to the user application
*/
rc = user_ind_notify(USER_IND_PLMN, (void*)data, size);
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_IND_REG, (void*)&ind, 0);
rc = user_ind_notify(user_api_id, USER_IND_REG, (void*)&ind, 0);
}
if (rc != RETURNerror) {
@@ -590,7 +559,7 @@ int user_api_emm_callback(Stat_t stat, tac_t tac, ci_t ci, AcT_t AcT,
ind.notification.loc.tac = tac;
ind.notification.loc.ci = ci;
ind.notification.loc.AcT = AcT;
rc = user_ind_notify(USER_IND_LOC, (void*)&ind, 0);
rc = user_ind_notify(user_api_id, USER_IND_LOC, (void*)&ind, 0);
}
}
@@ -618,7 +587,7 @@ int user_api_emm_callback(Stat_t stat, tac_t tac, ci_t ci, AcT_t AcT,
** Others: None **
** **
***************************************************************************/
int user_api_esm_callback(int cid, network_pdn_state_t state)
int user_api_esm_callback(user_api_id_t *user_api_id, int cid, network_pdn_state_t state)
{
LOG_FUNC_IN;
@@ -628,7 +597,7 @@ int user_api_esm_callback(int cid, network_pdn_state_t state)
ind.notification.pdn.cid = cid;
ind.notification.pdn.status = state;
/* The status of the specified PDN connection has changed */
rc = user_ind_notify(USER_IND_PDN, (void*)&ind, 0);
rc = user_ind_notify(user_api_id, USER_IND_PDN, (void*)&ind, 0);
LOG_FUNC_RETURN (rc);
}
@@ -651,7 +620,7 @@ int user_api_esm_callback(int cid, network_pdn_state_t state)
** Others: None **
** **
***************************************************************************/
static int _user_api_send(at_response_t* data)
static int _user_api_send(user_api_id_t *user_api_id, at_response_t* data)
{
LOG_FUNC_IN;
@@ -660,7 +629,7 @@ static int _user_api_send(at_response_t* data)
/* Send the AT command response message to the user application */
if (bytes != RETURNerror) {
bytes = _user_api_send_data(bytes);
bytes = _user_api_send_data(user_api_id, bytes);
}
LOG_FUNC_RETURN (bytes);
@@ -687,7 +656,7 @@ static int _user_api_send(at_response_t* data)
** Others: None **
** **
***************************************************************************/
static int _user_api_registration_handler(unsigned char id, const void* data,
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;
@@ -704,7 +673,7 @@ static int _user_api_registration_handler(unsigned char id, const void* data,
at_response.response.cereg.stat = reg->status;
/* Encode and send the AT command response message to the user */
int bytes = _user_api_send(&at_response);
int bytes = _user_api_send(user_api_id, &at_response);
LOG_FUNC_RETURN (bytes);
}
@@ -729,7 +698,7 @@ static int _user_api_registration_handler(unsigned char id, const void* data,
** Others: None **
** **
***************************************************************************/
static int _user_api_location_handler(unsigned char id, const void* data,
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;
@@ -753,7 +722,7 @@ static int _user_api_location_handler(unsigned char id, const void* data,
}
/* Encode and send the AT command response message to the user */
int bytes = _user_api_send(&at_response);
int bytes = _user_api_send(user_api_id, &at_response);
LOG_FUNC_RETURN (bytes);
}
@@ -776,7 +745,7 @@ static int _user_api_location_handler(unsigned char id, const void* data,
** Others: None **
** **
***************************************************************************/
static int _user_api_network_handler(unsigned char id, const void* data,
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;
@@ -789,7 +758,7 @@ static int _user_api_network_handler(unsigned char id, const void* data,
at_response.response.cops.tst.size = size;
/* Encode and send the AT command response message to the user */
int bytes = _user_api_send(&at_response);
int bytes = _user_api_send(user_api_id, &at_response);
LOG_FUNC_RETURN (bytes);
}
@@ -814,7 +783,7 @@ static int _user_api_network_handler(unsigned char id, const void* data,
** Others: None **
** **
***************************************************************************/
static int _user_api_pdn_connection_handler(unsigned char id, const void* data,
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;
@@ -831,7 +800,7 @@ static int _user_api_pdn_connection_handler(unsigned char id, const void* data,
at_response.response.cgev.code = pdn->status;
/* Encode and send the AT command response message to the user */
int bytes = _user_api_send(&at_response);
int bytes = _user_api_send(user_api_id, &at_response);
LOG_FUNC_RETURN (bytes);
}