2015-03-12 04:45:16 +08:00
|
|
|
/*
|
2017-03-29 13:16:44 +08:00
|
|
|
* Licensed under the GNU General Public License version 2 with exceptions. See
|
|
|
|
* LICENSE file in the project root for full license information
|
2014-11-25 23:10:29 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include "oshw.h"
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Host to Network byte order (i.e. to big endian).
|
|
|
|
*
|
|
|
|
* Note that Ethercat uses little endian byte order, except for the Ethernet
|
|
|
|
* header which is big endian as usual.
|
|
|
|
*/
|
|
|
|
uint16 oshw_htons(uint16 host)
|
|
|
|
{
|
|
|
|
uint16 network = htons (host);
|
|
|
|
return network;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Network (i.e. big endian) to Host byte order.
|
|
|
|
*
|
|
|
|
* Note that Ethercat uses little endian byte order, except for the Ethernet
|
|
|
|
* header which is big endian as usual.
|
|
|
|
*/
|
|
|
|
uint16 oshw_ntohs(uint16 network)
|
|
|
|
{
|
|
|
|
uint16 host = ntohs (network);
|
|
|
|
return host;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Create list over available network adapters.
|
|
|
|
* @return First element in linked list of adapters
|
|
|
|
*/
|
|
|
|
ec_adaptert * oshw_find_adapters(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int string_len;
|
|
|
|
struct if_nameindex *ids;
|
|
|
|
ec_adaptert * adapter;
|
|
|
|
ec_adaptert * prev_adapter;
|
|
|
|
ec_adaptert * ret_adapter = NULL;
|
|
|
|
|
|
|
|
|
|
|
|
/* Iterate all devices and create a local copy holding the name and
|
2019-01-29 18:32:32 +08:00
|
|
|
* description.
|
2014-11-25 23:10:29 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
ids = if_nameindex ();
|
|
|
|
for(i = 0; ids[i].if_index != 0; i++)
|
|
|
|
{
|
|
|
|
adapter = (ec_adaptert *)malloc(sizeof(ec_adaptert));
|
|
|
|
/* If we got more than one adapter save link list pointer to previous
|
|
|
|
* adapter.
|
|
|
|
* Else save as pointer to return.
|
|
|
|
*/
|
|
|
|
if (i)
|
|
|
|
{
|
|
|
|
prev_adapter->next = adapter;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret_adapter = adapter;
|
|
|
|
}
|
|
|
|
|
2019-01-29 18:32:32 +08:00
|
|
|
/* fetch description and name, in Linux we use the same on both */
|
2014-11-25 23:10:29 +08:00
|
|
|
adapter->next = NULL;
|
|
|
|
|
|
|
|
if (ids[i].if_name)
|
|
|
|
{
|
|
|
|
string_len = strlen(ids[i].if_name);
|
|
|
|
if (string_len > (EC_MAXLEN_ADAPTERNAME - 1))
|
|
|
|
{
|
|
|
|
string_len = EC_MAXLEN_ADAPTERNAME - 1;
|
|
|
|
}
|
|
|
|
strncpy(adapter->name, ids[i].if_name,string_len);
|
|
|
|
adapter->name[string_len] = '\0';
|
|
|
|
strncpy(adapter->desc, ids[i].if_name,string_len);
|
|
|
|
adapter->desc[string_len] = '\0';
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
adapter->name[0] = '\0';
|
|
|
|
adapter->desc[0] = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
prev_adapter = adapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
if_freenameindex (ids);
|
|
|
|
|
|
|
|
return ret_adapter;
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Free memory allocated memory used by adapter collection.
|
|
|
|
* @param[in] adapter = First element in linked list of adapters
|
|
|
|
* EC_NOFRAME.
|
|
|
|
*/
|
|
|
|
void oshw_free_adapters(ec_adaptert * adapter)
|
|
|
|
{
|
|
|
|
ec_adaptert * next_adapter;
|
2019-01-29 18:32:32 +08:00
|
|
|
/* Iterate the linked list and free all elements holding
|
2014-11-25 23:10:29 +08:00
|
|
|
* adapter information
|
|
|
|
*/
|
|
|
|
if(adapter)
|
|
|
|
{
|
|
|
|
next_adapter = adapter->next;
|
|
|
|
free (adapter);
|
|
|
|
while (next_adapter)
|
|
|
|
{
|
|
|
|
adapter = next_adapter;
|
|
|
|
next_adapter = adapter->next;
|
|
|
|
free (adapter);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|