oshw.c
Go to the documentation of this file.
00001 /******************************************************************************
00002  *                *          ***                    ***
00003  *              ***          ***                    ***
00004  * ***  ****  **********     ***        *****       ***  ****          *****
00005  * *********  **********     ***      *********     ************     *********
00006  * ****         ***          ***              ***   ***       ****   ***
00007  * ***          ***  ******  ***      ***********   ***        ****   *****
00008  * ***          ***  ******  ***    *************   ***        ****      *****
00009  * ***          ****         ****   ***       ***   ***       ****          ***
00010  * ***           *******      ***** **************  *************    *********
00011  * ***             *****        ***   *******   **  **  ******         *****
00012  *                           t h e  r e a l t i m e  t a r g e t  e x p e r t s
00013  *
00014  * http://www.rt-labs.com
00015  * Copyright (C) 2009. rt-labs AB, Sweden. All rights reserved.
00016  *------------------------------------------------------------------------------
00017  * $Id: oshw.c 282 2012-09-25 10:52:42Z rtlaka $
00018  *------------------------------------------------------------------------------
00019  */
00020 
00021 #include <sys/ioctl.h>
00022 #include <net/if.h>
00023 #include <sys/socket.h>
00024 #include <arpa/inet.h>
00025 #include <stdio.h>
00026 #include <stdlib.h>
00027 #include <string.h>
00028 #include "ethercat_soem/oshw.h"
00029 
00036 uint16 oshw_htons(uint16 host)
00037 {
00038    uint16 network = htons (host);
00039    return network;
00040 }
00041 
00048 uint16 oshw_ntohs(uint16 network)
00049 {
00050    uint16 host = ntohs (network);
00051    return host;
00052 }
00053 
00057 ec_adaptert * oshw_find_adapters(void)
00058 {
00059    int i;
00060    int string_len;
00061    struct if_nameindex *ids;
00062    ec_adaptert * adapter;
00063    ec_adaptert * prev_adapter;
00064    ec_adaptert * ret_adapter = NULL;
00065 
00066 
00067    /* Iterate all devices and create a local copy holding the name and
00068     * decsription.
00069     */
00070 
00071    ids = if_nameindex ();
00072    for(i = 0; ids[i].if_index != 0; i++)
00073    {
00074       adapter = (ec_adaptert *)malloc(sizeof(ec_adaptert));
00075       /* If we got more than one adapter save link list pointer to previous
00076        * adapter.
00077        * Else save as pointer to return.
00078        */
00079       if (i)
00080       {
00081          prev_adapter->next = adapter;
00082       }
00083       else
00084       {
00085          ret_adapter = adapter;
00086       }
00087 
00088       /* fetch description and name, in linux we use the same on both */
00089       adapter->next = NULL;
00090 
00091       if (ids[i].if_name)
00092       {
00093           string_len = strlen(ids[i].if_name);
00094           if (string_len > (EC_MAXLEN_ADAPTERNAME - 1))
00095           {
00096              string_len = EC_MAXLEN_ADAPTERNAME - 1;
00097           }
00098           strncpy(adapter->name, ids[i].if_name,string_len);
00099           adapter->name[string_len] = '\0';
00100           strncpy(adapter->desc, ids[i].if_name,string_len);
00101           adapter->desc[string_len] = '\0';
00102       }
00103       else
00104       {
00105          adapter->name[0] = '\0';
00106          adapter->desc[0] = '\0';
00107       }
00108 
00109       prev_adapter = adapter;
00110    }
00111 
00112    if_freenameindex (ids);
00113 
00114    return ret_adapter;
00115 }
00116 
00121 void oshw_free_adapters(ec_adaptert * adapter)
00122 {
00123    ec_adaptert * next_adapter;
00124    /* Iterate the linked list and free all elemnts holding
00125     * adapter information
00126     */
00127    if(adapter)
00128    {
00129       next_adapter = adapter->next;
00130       free (adapter);
00131       while (next_adapter)
00132       {
00133          adapter = next_adapter;
00134          next_adapter = adapter->next;
00135          free (adapter);
00136       }
00137    }
00138 }


ethercat_soem
Author(s): Arthur Ketels, M.J.G. van de Molengraft
autogenerated on Wed Aug 26 2015 11:32:40