Commit 3aac8f1f authored by Federico Vaga's avatar Federico Vaga

lib: export open/close functions

Signed-off-by: 's avatarFederico Vaga <federico.vaga@gmail.com>
parent 1f73ca64
......@@ -17,12 +17,21 @@
static inline int wrtd_in_send_and_receive_sync(struct wrtd_desc *wrtd,
struct wrnc_msg *msg)
{
int err;
err = wrnc_hmq_open(wrtd->wrnc, WRTD_IN_TDC_CONTROL, 0);
if (err)
return err;
/* Send the message and get answer */
return wrnc_slot_send_and_receive_sync(wrtd->wrnc,
WRTD_IN_TDC_CONTROL,
WRTD_OUT_TDC_CONTROL,
msg,
WRTD_DEFAULT_TIMEOUT);
err = wrnc_slot_send_and_receive_sync(wrtd->wrnc,
WRTD_IN_TDC_CONTROL,
WRTD_OUT_TDC_CONTROL,
msg,
WRTD_DEFAULT_TIMEOUT);
wrnc_hmq_close(wrtd->wrnc, WRTD_IN_TDC_CONTROL, 0);
return err;
}
......
......@@ -17,11 +17,21 @@ static inline int wrtd_out_send_and_receive_sync(struct wrtd_desc *wrtd,
struct wrnc_msg *msg)
{
/* Send the message and get answer */
return wrnc_slot_send_and_receive_sync(wrtd->wrnc,
WRTD_IN_FD_CONTROL,
WRTD_OUT_FD_CONTROL,
msg,
WRTD_DEFAULT_TIMEOUT);
int err;
err = wrnc_hmq_open(wrtd->wrnc, WRTD_IN_FD_CONTROL, 0);
if (err)
return err;
err = wrnc_slot_send_and_receive_sync(wrtd->wrnc,
WRTD_IN_FD_CONTROL,
WRTD_OUT_FD_CONTROL,
msg,
WRTD_DEFAULT_TIMEOUT);
wrnc_hmq_close(wrtd->wrnc, WRTD_IN_FD_CONTROL, 0);
return err;
}
/**
......
......@@ -26,6 +26,7 @@ static char *wrnc_error_str[] = {
"Cannot parse data from sysfs attribute",
"Invalid slot",
"Operation not yet implemented",
"The HMQ slot is close",
NULL,
};
......@@ -189,16 +190,14 @@ void wrnc_close(struct wrnc_dev *wrnc)
close(wdesc->fd_dev);
for (i = 0; i < WRNC_MAX_CPU; ++i)
if (wdesc->fd_cpu[i] < 0)
if (wdesc->fd_cpu[i] > 0)
close(wdesc->fd_cpu[i]);
for (i = 0; i < WRNC_MAX_HMQ_SLOT / 2; ++i)
if (wdesc->fd_hmq_in[i] < 0)
close(wdesc->fd_hmq_in[i]);
wrnc_hmq_close(wrnc, i, 1);
for (i = 0; i < WRNC_MAX_HMQ_SLOT / 2; ++i)
if (wdesc->fd_hmq_in[i] < 0 )
close(wdesc->fd_hmq_in[i]);
wrnc_hmq_close(wrnc, i, 0);
free(wdesc);
}
......@@ -549,9 +548,10 @@ int wrnc_cpu_dump_application_file(struct wrnc_dev *wrnc,
* @param[in] dir direction of the slot (1 input, 0 output)
* @return 0 on success, -1 on error and errno is set appropriately
*/
static int wrnc_hmq_open(struct wrnc_desc *wdesc, unsigned int index,
unsigned int dir)
int wrnc_hmq_open(struct wrnc_dev *wrnc, unsigned int index,
unsigned int dir)
{
struct wrnc_desc *wdesc = (struct wrnc_desc *)wrnc;
char path[64];
int *fd;
......@@ -575,6 +575,25 @@ static int wrnc_hmq_open(struct wrnc_desc *wdesc, unsigned int index,
}
/**
* It closes a HMQ slot
* @param[in] wdesc device token
* @param[in] index HMQ index
* @param[in] dir direction of the slot (1 input, 0 output)
* @return 0 on success, -1 on error and errno is set appropriately
*/
void wrnc_hmq_close(struct wrnc_dev *wrnc, unsigned int index,
unsigned int dir)
{
struct wrnc_desc *wdesc = (struct wrnc_desc *)wrnc;
int *fd;
fd = dir ? wdesc->fd_hmq_in : wdesc->fd_hmq_out;
if (fd[index] > 0)
close(fd[index]);
}
/**
* It sends a synchronous message. The slots are uni-directional, so you must
* specify where write the message and where the answer is expected.
......@@ -596,19 +615,19 @@ int wrnc_slot_send_and_receive_sync(struct wrnc_dev *wrnc,
struct wrnc_msg_sync smsg;
int err;
if (wdesc->fd_hmq_in[index_in] < 0) {
errno = EWRNC_HMQ_CLOSE;
return -1;
}
/* Build the message */
smsg.index_in = index_in;
smsg.index_out = index_out;
smsg.timeout_ms = timeout_ms;
memcpy(&smsg.msg, msg, sizeof(struct wrnc_msg));
err = wrnc_hmq_open(wdesc, smsg.index_in, 1);
if (err)
return err;
/* Send the message */
err = ioctl(wdesc->fd_hmq_in[smsg.index_in],
WRNC_IOCTL_MSG_SYNC, &smsg);
err = ioctl(wdesc->fd_hmq_in[index_in], WRNC_IOCTL_MSG_SYNC, &smsg);
if (err)
return -1;
......@@ -640,11 +659,9 @@ int wrnc_slot_poll(struct wrnc_dev *wrnc, struct pollfd *p, nfds_t nfds,
lp[i].events = p[i].events;
index = p[i].fd;
if (p[i].events & POLLIN) {
wrnc_hmq_open(wdesc, index, 0);
lp[i].fd = wdesc->fd_hmq_out[index];
}
if (p[i].events & POLLOUT) {
wrnc_hmq_open(wdesc, index, 1);
lp[i].fd = wdesc->fd_hmq_in[index];
}
}
......@@ -829,18 +846,19 @@ int wrnc_cpu_disable(struct wrnc_dev *wrnc, unsigned int index)
* It allocates and returns a message from an output message queue slot.
* The user of this function is in charge to release the memory.
* @param[in] wrnc device to use
* @param[in] index CPU to enable
* @param[in] index HMQ slot to read
* @return a WRNC message, NULL on error and errno is set appropriately
*/
struct wrnc_msg *wrnc_slot_receive(struct wrnc_dev *wrnc, unsigned int index)
{
struct wrnc_desc *wdesc = (struct wrnc_desc *)wrnc;
struct wrnc_msg *msg;
int err, n;
int n;
err = wrnc_hmq_open(wdesc, index, 0);
if (err)
if (wdesc->fd_hmq_out[index] < 0) {
errno = EWRNC_HMQ_CLOSE;
return NULL;
}
msg = malloc(sizeof(struct wrnc_msg));
if (!msg)
......
......@@ -33,6 +33,7 @@ enum wrnc_error_number {
EWRNC_INVAL_PARSE = 83630, /**< cannot parse data from sysfs */
EWRNC_INVAL_SLOT, /**< invalid slot */
EWRNC_NO_IMPLEMENTATION, /**< a prototype is not implemented */
EWRNC_HMQ_CLOSE, /**< The HMQ is closed */
__EWRNC_MAX,
};
......@@ -94,6 +95,10 @@ extern int wrnc_cpu_disable(struct wrnc_dev *wrnc, unsigned int index);
extern int wrnc_cpu_start(struct wrnc_dev *wrnc, unsigned int index);
extern int wrnc_cpu_stop(struct wrnc_dev *wrnc, unsigned int index);
extern int wrnc_hmq_open(struct wrnc_dev *wrnc, unsigned int index,
unsigned int dir);
extern void wrnc_hmq_close(struct wrnc_dev *wrnc, unsigned int index,
unsigned int dir);
extern struct wrnc_msg *wrnc_slot_receive(struct wrnc_dev *wrnc,
unsigned int index);
extern int wrnc_slot_send(struct wrnc_dev *wrnc, unsigned int index,
......
......@@ -108,6 +108,12 @@ void *dump_thread(void *arg)
/* Build the polling structures */
for (i = 0; i < idx_valid[idx]; ++i) {
err = wrnc_hmq_open(wrnc, slot_index[idx][i], 0);
if (err) {
fprintf(stderr, "Cannot open HMQ: %s\n",
wrnc_strerror(errno));
goto out;
}
p[i].fd = slot_index[idx][i];
p[i].events = POLLIN | POLLERR;
}
......@@ -139,7 +145,9 @@ void *dump_thread(void *arg)
break;
}
}
out:
for (i = 0; i < idx_valid[idx]; ++i)
wrnc_hmq_close(wrnc, slot_index[idx][i], 0);
wrnc_close(wrnc);
return NULL;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment