storage.c
#include "common.h"
#include <stdbool.h>
/* Server and its interfaces descriptions. */
#include <device_access/Storage.edl.h>
#include <device_access/IStorage.idl.h>
static const char *EntityName = "Storage";
/* Implementation of the IStorage interface. */
typedef struct IStorageImpl
{
IStorage base; /**< Base interface object */
const char* privateData; /**< Private user data */
} IStorageImpl;
/* Implementation of the GetInfo method of the IStorage interface. */
static nk_err_t GetInfo(
IStorage *self,
const IStorage_GetInfo_req *req,
const struct nk_arena* reqArena,
IStorage_GetInfo_res* res,
struct nk_arena* resArena)
{
if (res == NK_NULL)
{
PRINT_ERR("res pointer is null!");
return NK_EBADMSG;
}
if (resArena == NK_NULL)
{
PRINT_ERR("resArena pointer is null!");
return NK_EBADMSG;
}
IStorageImpl *impl = (IStorageImpl *)self;
/* Copies private data to the server response. */
nk_char_t *info;
info = nk_arena_alloc(
nk_char_t,
resArena,
&res->info,
rtl_strnlen(impl->privateData, IStorage_MaxStringSize) + 1);
rtl_strncpy((char *)info, impl->privateData, IStorage_MaxStringSize);
return NK_EOK;
}
/* IStorageImpl object constructor. */
static IStorage* CreateStorageImpl(IStorageImpl *impl)
{
if (impl == NK_NULL)
{
PRINT_ERR("impl pointer is null!");
return RTL_NULL;
}
/* IStorageManager interface method implementation table. */
static const IStorage_ops Ops =
{
.GetInfo = GetInfo,
};
impl->base.ops = &Ops;
impl->privateData = "file1.txt file2.txt";
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
* "storage_connection".
*/
ServiceId iid;
Handle handle =
ServiceLocatorRegister("storage_connection", NULL, 0, &iid);
if (handle == INVALID_HANDLE)
{
PRINT_ERR("Can`t establish static IPC storage_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 IStorage_req req;
char req_buffer[IStorage_req_arena_size];
struct nk_arena reqArena =
NK_ARENA_INITIALIZER(req_buffer, req_buffer + sizeof(req_buffer));
/* Prepares response structures:fixed part and arena. */
union IStorage_res res;
char resBuffer[IStorage_res_arena_size];
struct nk_arena resArena =
NK_ARENA_INITIALIZER(resBuffer, resBuffer + sizeof(resBuffer));
/**
* Creates and initializes the interface instance and
* Server entity dispatcher.
*/
IStorageImpl impl;
Storage_entity entity;
Storage_entity_init(&entity, CreateStorageImpl(&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.
*/
Storage_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 or denied by KSM policy!");
}
}
while (true);
return EXIT_SUCCESS;
}
Page top