linux/oshw.c
Go to the documentation of this file.
1 /*
2  * Licensed under the GNU General Public License version 2 with exceptions. See
3  * LICENSE file in the project root for full license information
4  */
5 
6 #include <sys/ioctl.h>
7 #include <net/if.h>
8 #include <sys/socket.h>
9 #include <arpa/inet.h>
10 #include <stdio.h>
11 #include <stdlib.h>
12 #include <string.h>
13 #include "oshw.h"
14 
22 {
23  uint16 network = htons (host);
24  return network;
25 }
26 
34 {
35  uint16 host = ntohs (network);
36  return host;
37 }
38 
43 {
44  int i;
45  int string_len;
46  struct if_nameindex *ids;
47  ec_adaptert * adapter;
48  ec_adaptert * prev_adapter;
49  ec_adaptert * ret_adapter = NULL;
50 
51 
52  /* Iterate all devices and create a local copy holding the name and
53  * description.
54  */
55 
56  ids = if_nameindex ();
57  for(i = 0; ids[i].if_index != 0; i++)
58  {
59  adapter = (ec_adaptert *)malloc(sizeof(ec_adaptert));
60  /* If we got more than one adapter save link list pointer to previous
61  * adapter.
62  * Else save as pointer to return.
63  */
64  if (i)
65  {
66  prev_adapter->next = adapter;
67  }
68  else
69  {
70  ret_adapter = adapter;
71  }
72 
73  /* fetch description and name, in Linux we use the same on both */
74  adapter->next = NULL;
75 
76  if (ids[i].if_name)
77  {
78  string_len = strlen(ids[i].if_name);
79  if (string_len > (EC_MAXLEN_ADAPTERNAME - 1))
80  {
81  string_len = EC_MAXLEN_ADAPTERNAME - 1;
82  }
83  strncpy(adapter->name, ids[i].if_name,string_len);
84  adapter->name[string_len] = '\0';
85  strncpy(adapter->desc, ids[i].if_name,string_len);
86  adapter->desc[string_len] = '\0';
87  }
88  else
89  {
90  adapter->name[0] = '\0';
91  adapter->desc[0] = '\0';
92  }
93 
94  prev_adapter = adapter;
95  }
96 
97  if_freenameindex (ids);
98 
99  return ret_adapter;
100 }
101 
107 {
108  ec_adaptert * next_adapter;
109  /* Iterate the linked list and free all elements holding
110  * adapter information
111  */
112  if(adapter)
113  {
114  next_adapter = adapter->next;
115  free (adapter);
116  while (next_adapter)
117  {
118  adapter = next_adapter;
119  next_adapter = adapter->next;
120  free (adapter);
121  }
122  }
123 }
EC_MAXLEN_ADAPTERNAME
#define EC_MAXLEN_ADAPTERNAME
Definition: ethercatmain.h:39
ec_adapter
Definition: ethercatmain.h:44
ec_adapter::next
ec_adaptert * next
Definition: ethercatmain.h:48
ec_adapter::desc
char desc[EC_MAXLEN_ADAPTERNAME]
Definition: ethercatmain.h:47
oshw_find_adapters
ec_adaptert * oshw_find_adapters(void)
Definition: linux/oshw.c:42
oshw_free_adapters
void oshw_free_adapters(ec_adaptert *adapter)
Definition: linux/oshw.c:106
oshw_htons
uint16 oshw_htons(uint16 host)
Definition: linux/oshw.c:21
oshw_ntohs
uint16 oshw_ntohs(uint16 network)
Definition: linux/oshw.c:33
uint16
uint16_t uint16
Definition: osal.h:29
oshw.h
Headerfile for ethercatbase.c.
ec_adapter::name
char name[EC_MAXLEN_ADAPTERNAME]
Definition: ethercatmain.h:46


soem
Author(s): Arthur Ketels and M.J.G. van den Molengraft
autogenerated on Wed Mar 2 2022 01:01:49