Skip to content

Commit

Permalink
fix: sock_raw_mmaped receive thread added
Browse files Browse the repository at this point in the history
  • Loading branch information
robert-burger committed Sep 10, 2024
1 parent 2846d67 commit 9108db1
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 8 deletions.
4 changes: 3 additions & 1 deletion include/libethercat/hw_sock_raw_mmaped.h
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,12 @@ typedef struct hw_sock_raw_mmaped {
/*!
* \param[in] phw_sock_raw_mmaped Pointer to sock_raw_mmmaped hw handle.
* \param[in] devname Null-terminated string to EtherCAT hw device name.
* \param[in] prio Priority for receiver thread.
* \param[in] cpu_mask CPU mask for receiver thread.
*
* \return 0 or negative error code
*/
int hw_device_sock_raw_mmaped_open(struct hw_sock_raw_mmaped *phw_sock_raw_mmaped, const osal_char_t *devname);
int hw_device_sock_raw_mmaped_open(struct hw_sock_raw_mmaped *phw_sock_raw_mmaped, const osal_char_t *devname, int prio, int cpumask);

#endif // LIBETHERCAT_HW_SOCK_RAW_MMAPED_H

Expand Down
46 changes: 43 additions & 3 deletions src/hw_sock_raw_mmaped.c
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ void hw_device_sock_raw_mmaped_send_finished(struct hw_common *phw);
int hw_device_sock_raw_mmaped_get_tx_buffer(struct hw_common *phw, ec_frame_t **ppframe);
int hw_device_sock_raw_mmaped_close(struct hw_common *phw);

static void *hw_device_sock_raw_mmaped_rx_thread(void *arg);

// this need the grant_cap_net_raw kernel module
// see https://gitlab.com/fastflo/open_ethercat
#define GRANT_CAP_NET_RAW_PROCFS "/proc/grant_cap_net_raw"
Expand Down Expand Up @@ -103,10 +105,12 @@ static int try_grant_cap_net_raw_init(void) {
/*!
* \param[in] phw Pointer to hw handle.
* \param[in] devname Null-terminated string to EtherCAT hw device name.
* \param[in] prio Priority for receiver thread.
* \param[in] cpu_mask CPU mask for receiver thread.
*
* \return 0 or negative error code
*/
int hw_device_sock_raw_mmaped_open(struct hw_sock_raw_mmaped *phw_sock_raw_mmaped, const osal_char_t *devname) {
int hw_device_sock_raw_mmaped_open(struct hw_sock_raw_mmaped *phw_sock_raw_mmaped, const osal_char_t *devname, int prio, int cpumask) {
int ret = EC_OK;
struct ifreq ifr;
int ifindex;
Expand Down Expand Up @@ -234,6 +238,16 @@ int hw_device_sock_raw_mmaped_open(struct hw_sock_raw_mmaped *phw_sock_raw_mmape
sll.sll_protocol = htons(ETH_P_ECAT);
bind(phw_sock_raw_mmaped->sockfd, (struct sockaddr *) &sll, sizeof(sll));
}

if (ret == EC_OK) {
phw_sock_raw_mmaped->rxthreadrunning = 1;
osal_task_attr_t attr;
attr.policy = OSAL_SCHED_POLICY_FIFO;
attr.priority = prio;
attr.affinity = cpumask;
(void)strcpy(&attr.task_name[0], "ecat.rx");
osal_task_create(&phw_sock_raw_mmaped->rxthread, &attr, hw_device_sock_raw_mmaped_rx_thread, phw_sock_raw_mmaped);
}

return ret;
}
Expand All @@ -248,10 +262,12 @@ int hw_device_sock_raw_mmaped_close(struct hw_common *phw) {
int ret = 0;

struct hw_sock_raw_mmaped *phw_sock_raw_mmaped = container_of(phw, struct hw_sock_raw_mmaped, common);

phw_sock_raw_mmaped->rxthreadrunning = 0;
osal_task_join(&phw_sock_raw_mmaped->rxthread, NULL);

close(phw_sock_raw_mmaped->sockfd);

// TODO some more close of sbuf things???

return ret;
}

Expand Down Expand Up @@ -296,6 +312,30 @@ int hw_device_sock_raw_mmaped_recv(struct hw_common *phw) {
return EC_OK;
}

//! receiver thread
void *hw_device_sock_raw_mmaped_rx_thread(void *arg) {
// cppcheck-suppress misra-c2012-11.5
struct hw_sock_raw_mmaped *phw_sock_raw_mmaped = (struct hw_sock_raw_mmaped *) arg;

assert(phw_sock_raw_mmaped != NULL);

osal_task_sched_priority_t rx_prio;
if (osal_task_get_priority(&phw_sock_raw_mmaped->rxthread, &rx_prio) != OSAL_OK) {
rx_prio = 0;
}

ec_log(10, "HW_SOCK_RAW_MMAPED_RX", "receive thread running (prio %d)\n", rx_prio);

while (phw_sock_raw_mmaped->rxthreadrunning != 0) {
(void)hw_device_sock_raw_mmaped_recv(&phw_sock_raw_mmaped->common);
}

ec_log(10, "HW_SOCK_RAW_MMAPED_RX", "receive thread stopped\n");

return NULL;
}


static struct tpacket_hdr *hw_get_next_tx_buffer(struct hw_common *phw) {
struct tpacket_hdr *header;
struct pollfd pollset;
Expand Down
2 changes: 1 addition & 1 deletion tools/eepromtool/eepromtool.c
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ int main(int argc, char **argv) {
intf = &intf[16];

ec_log(10, "HW_OPEN", "Opening interface as mmaped SOCK_RAW: %s\n", intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf, base_prio - 1, base_affinity);

if (ret == 0) {
phw = &hw_sock_raw_mmaped.common;
Expand Down
2 changes: 1 addition & 1 deletion tools/ethercatdiag/ethercatdiag.c
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ int main(int argc, char **argv) {
intf = &intf[16];

ec_log(10, "HW_OPEN", "Opening interface as mmaped SOCK_RAW: %s\n", intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf, base_prio - 1, base_affinity);

if (ret == 0) {
phw = &hw_sock_raw_mmaped.common;
Expand Down
2 changes: 1 addition & 1 deletion tools/example_with_dc/example_with_dc.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ int main(int argc, char **argv) {
intf = &intf[16];

ec_log(10, "HW_OPEN", "Opening interface as mmaped SOCK_RAW: %s\n", intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf, base_prio - 1, base_affinity);

if (ret == 0) {
phw = &hw_sock_raw_mmaped.common;
Expand Down
2 changes: 1 addition & 1 deletion tools/foe_tool/foe_tool.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ int main(int argc, char **argv) {
intf = &intf[16];

ec_log(10, "HW_OPEN", "Opening interface as mmaped SOCK_RAW: %s\n", intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf);
ret = hw_device_sock_raw_mmaped_open(&hw_sock_raw_mmaped, intf, base_prio - 1, base_affinity);

if (ret == 0) {
phw = &hw_sock_raw_mmaped.common;
Expand Down

0 comments on commit 9108db1

Please sign in to comment.