summaryrefslogtreecommitdiff
path: root/ecos/packages/net/common/current/tests/set_mac_address.c
blob: e02d3e23c48d93f2653f7d26ff5bd1d792c406b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
//==========================================================================
//
//      tests/set_mac_address.c
//
//      Set_Mac_Address Utility - this carefully does NOTHING unless you
//      edit this source file to confirm that you really want to do it.
//
//==========================================================================
// ####BSDALTCOPYRIGHTBEGIN####                                             
// -------------------------------------------                              
// Portions of this software may have been derived from FreeBSD, OpenBSD,   
// or other sources, and if so are covered by the appropriate copyright     
// and license included herein.                                             
// -------------------------------------------                              
// ####BSDALTCOPYRIGHTEND####                                               
//==========================================================================
//#####DESCRIPTIONBEGIN####
//
// Author(s):    hmt
// Contributors: 
// Date:         2000-05-03
// Purpose:      
// Description:  
//              
//
//####DESCRIPTIONEND####
//
//==========================================================================

#include <pkgconf/system.h>
#ifdef CYGBLD_DEVS_ETH_DEVICE_H    // Get the device config if it exists
#include CYGBLD_DEVS_ETH_DEVICE_H
#endif


// SET_MAC_ADDRESS test code

#include <network.h>

#include <netinet/if_ether.h>

#define NUMTHREADS 1
#define STACK_SIZE (CYGNUM_HAL_STACK_SIZE_TYPICAL + 0x1000)
static char thread_stack[NUMTHREADS][STACK_SIZE];
static cyg_thread thread_data[NUMTHREADS];
static cyg_handle_t thread_handle[NUMTHREADS];

extern void
cyg_test_exit(void);

void
pexit(char *s)
{
    perror(s);
    cyg_test_exit();
}

// ------------------------------------------------------------------------
// remove NT to make this utility be useful ;-)
#define DONT_SET_ETH0
#define DONT_SET_ETH1

// These are commented out to make sure you choose a value:
#ifdef DO_SET_ETH0
//static cyg_uint8 new_eth0_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xdd};
//static cyg_uint8 new_eth0_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xdb};
#endif
#ifdef DO_SET_ETH1
//static cyg_uint8 new_eth1_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xde};
//static cyg_uint8 new_eth1_addr[6]={ 0x0,0x90,0x27,0x8c,0x57,0xdc};
#endif

// ------------------------------------------------------------------------

int
set_mac_address( const char *interface, char *mac_address )
{
    int s, i;
    struct ifreq ifr;

    s = socket(AF_INET, SOCK_DGRAM, 0);
    if (s < 0) {
        perror("socket");
        return false;
    }

    diag_printf( "%s socket is %d:\n", interface, s );

    strcpy(ifr.ifr_name, interface);

    for ( i = 0; i < ETHER_ADDR_LEN; i++ )
        ifr.ifr_hwaddr.sa_data[i] = mac_address[i];

    diag_printf( "Mac addr %02x:%02x:%02x:%02x:%02x:%02x\n",
                 ifr.ifr_hwaddr.sa_data[0],
                 ifr.ifr_hwaddr.sa_data[1],
                 ifr.ifr_hwaddr.sa_data[2],
                 ifr.ifr_hwaddr.sa_data[3],
                 ifr.ifr_hwaddr.sa_data[4],
                 ifr.ifr_hwaddr.sa_data[5] );

    if (ioctl(s, SIOCSIFHWADDR, &ifr)) {
        perror("SIOCSIFHWADDR");
        close( s );
        return false;
    }

    diag_printf( "%s ioctl(SIOCSIFHWADDR) succeeded\n", interface );

    close( s );

    return true;
}

// ------------------------------------------------------------------------
void
net_test(cyg_addrword_t param)
{
    int results = 0;
    diag_printf("Start set_mac_address\n");
#ifdef CYGHWR_NET_DRIVER_ETH0
#ifdef DO_SET_ETH0
    diag_printf("Setting MAC of eth0 to %02x:%02x:%02x:%02x:%02x:%02x\n",
                new_eth0_addr[0],new_eth0_addr[1],
                new_eth0_addr[2],new_eth0_addr[3],
                new_eth0_addr[4],new_eth0_addr[5] );
    results += set_mac_address( "eth0", new_eth0_addr );
#endif
#endif
#ifdef CYGHWR_NET_DRIVER_ETH1
#ifdef DO_SET_ETH1
    diag_printf("Setting MAC of eth1 to %02x:%02x:%02x:%02x:%02x:%02x\n",
                new_eth1_addr[0],new_eth1_addr[1],
                new_eth1_addr[2],new_eth1_addr[3],
                new_eth1_addr[4],new_eth1_addr[5] );
    results += set_mac_address( "eth1", new_eth1_addr );
#endif
#endif

    if ( 0 == results )
        diag_printf( "**** Did not set any MAC addresses ****\n" );

    diag_printf("Init Network Interfaces\n");
    init_all_network_interfaces();
    diag_printf("After init.\n");

    cyg_test_exit();
}

// ------------------------------------------------------------------------
void
cyg_start(void)
{
    // Create a main thread, so we can run the scheduler and have time 'pass'
    cyg_thread_create(10,                // Priority - just a number
                      net_test,          // entry
                      0,                 // entry parameter
                      "Network test",    // Name
                     &thread_stack[0][0], // Stack
                      STACK_SIZE,        // Size
                      &thread_handle[0], // Handle
                      &thread_data[0]    // Thread data structure
            );
    cyg_thread_resume(thread_handle[0]);  // Start it

    cyg_scheduler_start();
}

// EOF set_mac_address.c