Implementation of the LoginManager entity in the Device Access example

login_manager.c

#include "common.h"

#include <stdbool.h>

/* Server and its interfaces descriptions. */

#include <device_access/LoginManager.edl.h>

#include <device_access/ILoginManager.idl.h>

static const char *EntityName = "LoginManager";

/* Implementation of the ILoginManager interface. */

typedef struct ILoginManagerImpl {

ILoginManager base; /**< Base interface object */

const char* validUsername; /**< Valid user name */

} ILoginManagerImpl;

/* Implementation of the Login method of the ILoginManager interface. */

static nk_err_t Login(

ILoginManager *self,

const ILoginManager_Login_req *req,

const struct nk_arena* reqArena,

ILoginManager_Login_res* res,

struct nk_arena* resArena)

{

if (req == NK_NULL)

{

PRINT_ERR("req pointer is null!");

return NK_EBADMSG;

}

if (reqArena == NK_NULL)

{

PRINT_ERR("reqArena pointer is null!");

return NK_EBADMSG;

}

if (res == NK_NULL)

{

PRINT_ERR("res pointer is null!");

return NK_EBADMSG;

}

ILoginManagerImpl *impl = (ILoginManagerImpl *)self;

/* Gets the login from the request arena. */

nk_char_t *loginUsername;

nk_uint32_t loginLen = 0;

loginUsername = nk_arena_get(

nk_char_t, reqArena, &req->loginUsername, &loginLen);

if (loginLen < 0)

{

return NK_EPERM;

}

/**

* The loginUsername from the request is compared with the valid user name.

* If the authorization is successful, a response message will be sent,

* otherwise an error message will be sent.

*/

if (!rtl_strncmp(

loginUsername,

impl->validUsername,

ILoginManager_MaxStringSize))

{

/* Authorization is successful. Response message is sent. */

return NK_EOK;

}

else

{

/**

* Authorization is unsuccessful.

* An error message with a status code is sent.

*/

return NK_ELOGIC_MAKE(rcInvalidOperation);

}

}

/* Implementation of the Logout method of the ILoginManager interface. */

static nk_err_t Logout(

struct ILoginManager *self,

const ILoginManager_Logout_req *req,

const struct nk_arena* reqArena,

ILoginManager_Logout_res* res,

struct nk_arena* resArena)

{

return NK_EOK;

}

/* ILoginManagerImpl object constructor. */

static ILoginManager* CreateLoginManagerImpl(ILoginManagerImpl *impl)

{

if (impl == NK_NULL)

{

PRINT_ERR("impl pointer is null!");

return RTL_NULL;

}

/* ILoginManager interface method implementation table. */

static const ILoginManager_ops Ops = {

.Login = Login,

.Logout = Logout

};

impl->base.ops = &Ops;

impl->validUsername = "valid_username";

return &impl->base;

}

/* Entry point to the entity. */

int main(void)

{

if (setvbuf(stderr, RTL_NULL, _IONBF, 0))

{

PRINT_ERR("Error setting stderr parameters!");

return EXIT_FAILURE;

}

/**

* Gets the server IPC handle of the connection named

* "login_manager_connection".

*/

ServiceId iid;

Handle handle =

ServiceLocatorRegister("login_manager_connection", NULL, 0, &iid);

if (handle == INVALID_HANDLE)

{

PRINT_ERR("Can`t establish static IPC login_manager_connection!");

return EXIT_FAILURE;

}

/* Initializes IPC transport to client entities. */

NkKosTransport transport;

NkKosTransport_Init(&transport, handle, NK_NULL, 0);

/* Prepares request structures:fixed part and arena. */

union ILoginManager_req req;

char reqBuffer[ILoginManager_req_arena_size];

struct nk_arena reqArena =

NK_ARENA_INITIALIZER(reqBuffer, reqBuffer + sizeof(reqBuffer));

/* Prepares response structures:fixed part and arena. */

union ILoginManager_res res;

char resBuffer[ILoginManager_res_arena_size];

struct nk_arena resArena =

NK_ARENA_INITIALIZER(resBuffer, resBuffer + sizeof(resBuffer));

/**

* Creates and initializes the interface instance and

* Server entity dispatcher.

*/

ILoginManagerImpl impl;

LoginManager_entity entity;

LoginManager_entity_init(&entity, CreateLoginManagerImpl(&impl));

/* Implements a request loop. */

do

{

/* Resets the request and response buffers. */

nk_req_reset(&req);

nk_arena_reset(&reqArena);

nk_arena_reset(&resArena);

/* Waits for a request from the client. */

if (NK_EOK == nk_transport_recv(

&transport.base, &req.base_, &reqArena))

{

/**

* Processes the received request by calling implementations of

* the requested interface methods.

*/

LoginManager_entity_dispatch(

&entity, &req.base_, &reqArena, &res.base_, &resArena);

}

else

{

PRINT_ERR("NK transport error!");

}

/* Sends a response. */

if (NK_EOK != nk_transport_reply(

&transport.base, &res.base_, &resArena))

{

PRINT_ERR("NK transport error denied by KSM policy!");

}

}

while (true);

return EXIT_SUCCESS;

}

Page top