1 /*
2 * Copyright (C) 2017 Texas Instruments Incorporated - http://www.ti.com/
3 * Copyright (C) 2016 CC-Link Partner Association -http://am.cc-link.org/
4 *
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 *
10 * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 *
13 * Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the
16 * distribution.
17 *
18 * Neither the name of Texas Instruments Incorporated nor the names of
19 * its contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 *
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 *
34 */
36 #include "SLMP.h"
37 #include "CCIEF_BASIC_SLAVE.h"
38 #include "SLMP_SERVER.h"
39 #include "TIMER.h"
40 #include "USER_SAMPLE.h"
42 /************************************************************************************/
43 /* The following is an user defined main program. This main program is one of a */
44 /* sample in the Windows OS and Intel x86 CPU. Please rewrite if necessary. */
45 /* This main program is one of a sample in the Linux. Please rewrite if necessary. */
46 /************************************************************************************/
48 #include <stdio.h>
49 #ifdef _WIN32
50 #include <stdint.h>
51 #include <winsock2.h>
52 #include <Ws2tcpip.h>
53 #include <windows.h>
54 #include <iphlpapi.h>
55 #include <conio.h>
56 #include <direct.h>
57 #include <sys/stat.h>
58 #elif __linux__
59 #include <string.h>
60 #include <sys/socket.h>
61 #include <sys/types.h>
62 #include <sys/ioctl.h>
63 #include <net/if.h>
64 #include <netinet/in.h>
65 #include <arpa/inet.h>
66 #include <net/route.h>
67 #include <limits.h>
68 #endif
69 #include <time.h>
71 /*[ Definition for sample program ]*/
72 #define USER_PARAMETER_FILE_READ 1 /* Read the parameter file */
73 #define USER_PARAMETER_FILE_NOT_READ 0 /* Not read the parameter file */
74 #define USER_DISPLAY_INTERVAL_TIME 5000 /* Interval time for the display of the cyclic state [ms] */
75 #ifdef _WIN32
76 #elif __linux__
77 #define MAX_INTERFACE 20
78 #define MAX_PATH 260
79 #define SOCKET_NOT_OPEN 0
80 #define DIR_PROC_ROUTE "/proc/net/route"
82 /*[ Structure of sample program ]*/
83 typedef struct
84 {
85 char IPAddr[INET_ADDRSTRLEN];
86 char IPMask[INET_ADDRSTRLEN];
87 char MACAddr[18];
88 char Defo[1028];
89 struct ifreq USER_ifreq;
90 } USER_NETWORK_INFO;
92 typedef struct
93 {
94 char cIface[IF_NAMESIZE];
95 struct in_addr cDst;
96 struct in_addr cGateway;
97 unsigned short int iFlag;
98 int iRecCnt;
99 int iUse;
100 short int iMetric;
101 struct in_addr cMask;
102 unsigned long int iMTU;
103 unsigned long int iWindow;
104 unsigned short int iIRTT;
105 } t_RouteInfo;
106 #endif
108 typedef struct
109 {
110 uint8_t aucMacAddress[6]; /* MAC Address */
111 uint32_t ulIpAddress; /* IP Address */
112 uint32_t ulSubnetMask; /* Subnet Mask */
113 uint32_t ulDefaultGatewayIPAddress; /* Default Gateway IP Address */
114 } USER_ADAPTER_INFO;
116 /************************************************************************************/
117 /* This is an user defined data for the CCIEF-BASIC Slave sample. */
118 /************************************************************************************/
119 static USER_SLAVE_PARAMETER UserSlaveParameter = { 0, 0, 0, 1, 0 }; /* Parameter of user setting */
120 static char acParameterFilePath[MAX_PATH]; /* Path of the parameter file */
121 static int iParameterFileRead; /* Flag of the parameter read */
123 /************************************************************************************/
124 /* This is an user defined data for the SLMP Server sample. */
125 /************************************************************************************/
126 /* Memory */
127 static uint16_t ausUserMemory[10000]; /* User Memory */
129 /************************************************************************************/
130 /* This is an user defined data for user sample. */
131 /************************************************************************************/
132 static USER_ADAPTER_INFO AdapterInfo; /* Information of the Network adapter */
133 static int iReset; /* Flag of user reset */
134 static unsigned long ulCyclicDataCount = 0; /* Count of the cyclic data at intervals */
136 /* Definition of function of sample program */
137 static void user_callback_recv_cyclic_data( int iCyclicState, int iOccupiedStationNumber );
138 static void user_callback_cyclic_disconnection( void );
139 static void user_callback_set_ip_address_basic( uint32_t ulIpAddress, uint32_t ulSubnetMask );
140 static int user_callback_parameter_get( uint16_t usId, uint16_t *pusSize, uint8_t **ppucData );
141 static int user_callback_parameter_set( uint16_t usId, uint16_t usSize, uint8_t *pucData );
142 static int user_callback_parameter_set_end( void );
143 static int user_callback_remote_reset( void );
144 static int32_t user_parameter_file_read( char *file_path, USER_SLAVE_PARAMETER *pParameter, USER_ADAPTER_INFO *pAdapterInfo );
145 static int32_t user_parameter_file_write( char *file_path, USER_SLAVE_PARAMETER *pParameter );
146 static void user_display_cyclic_information( void );
147 static int user_get_adapter_info( USER_ADAPTER_INFO *pGetAdapterInfo );
148 static int user_set_adapter_info( USER_ADAPTER_INFO *pSetAdapterInfo );
150 /************************************************************************************/
151 /* This is an user defined function for main function. */
152 /* The following is one of a sample in the Windows OS. Please rewrite if necessary. */
153 /* The following is one of a sample in the Linux. Please rewrite if necessary. */
154 /************************************************************************************/
155 void main( int argc, char *argv[] )
156 {
157 CCIEF_BASIC_SLAVE_INFO Slave;
158 SLMP_SERVER_INFO ServerInfo;
159 int iErrCode = 0;
160 int iResult;
161 struct in_addr addr;
162 char Ipaddr[16];
163 #ifdef _WIN32
164 WSADATA wsaData;
166 /* Initialize Winsock */
167 iResult = WSAStartup( MAKEWORD(2,2), &wsaData );
168 if ( iResult != 0 )
169 {
170 printf( "WSAStartup failed with error: %d\n", iResult );
171 goto end;
172 }
173 #elif __linux__
174 #endif
176 /* Get the Network adapter information */
177 iErrCode = user_get_adapter_info( &AdapterInfo );
178 if ( iErrCode != USER_ERR_OK )
179 {
180 goto end;
181 }
183 /* Get command line argument */
184 if ( 1 < argc )
185 {
186 #ifdef _WIN32
187 strncpy_s( acParameterFilePath, sizeof( acParameterFilePath ), argv[1], sizeof( acParameterFilePath ) );
188 #elif __linux__
189 strncpy( acParameterFilePath, argv[1], sizeof( acParameterFilePath ) );
190 #endif
191 /* Read the parameter file */
192 iParameterFileRead = USER_PARAMETER_FILE_READ;
193 }
194 else
195 {
196 printf( "Not found the parameter file!\n" );
197 printf( "\nStarting with the default parameter.\n" );
198 /* Not read the parameter file */
199 iParameterFileRead = USER_PARAMETER_FILE_NOT_READ;
200 }
202 reset:
203 iReset = USER_RESET_NONE;
205 /* Check the parameter refresh */
206 if ( iParameterFileRead == USER_PARAMETER_FILE_READ )
207 {
208 /* Read the parameter file */
209 iErrCode = user_parameter_file_read( acParameterFilePath, &UserSlaveParameter, &AdapterInfo );
210 if ( iErrCode != USER_ERR_OK )
211 {
212 goto end;
213 }
214 /* Not read the parameter file */
215 iParameterFileRead = USER_PARAMETER_FILE_NOT_READ;
216 }
218 /* Set ip address */
219 iErrCode = user_set_adapter_info( &AdapterInfo );
220 if ( iErrCode != USER_ERR_OK )
221 {
222 goto end;
223 }
225 /****************************************************************************/
226 /* Initialization of the Timer. */
227 /****************************************************************************/
228 /* Initialization the timer environment */
229 timer_initialize();
231 /****************************************************************************/
232 /* Initialization of the CCIEF-BASIC Slave. */
233 /****************************************************************************/
234 /* Initialization of the CCIEF-BASIC Slave */
235 Slave.usVenderCode = USER_PROFILE_VENDOR_CODE;
236 Slave.ulModelCode = USER_PROFILE_MODEL_CODE;
237 Slave.usMachineVersion = USER_PROFILE_MACHINE_VERSION;
238 Slave.ulIpAddress = AdapterInfo.ulIpAddress;
239 #ifdef _WIN32
240 #elif __linux__
241 Slave.ulSubnetMask = AdapterInfo.ulSubnetMask;
242 #endif
243 Slave.iOccupiedStationNumber = (int)UserSlaveParameter.usOccupiedStationNumber;
244 iErrCode = ccief_basic_slave_initialize( &Slave, user_callback_recv_cyclic_data,
245 user_callback_cyclic_disconnection );
246 if ( iErrCode != CCIEF_BASIC_SLAVE_ERR_OK )
247 {
248 printf( "\nCCIEF-BASIC Slave Station initialization error!\n" );
249 goto end;
250 }
252 /* Show the parameter of the slave */
253 printf( "\nStart CC-Link IE Field Basic Slave Station!\n" );
254 #ifdef _WIN32
255 addr.S_un.S_addr = htonl(AdapterInfo.ulIpAddress);
256 #elif __linux__
257 addr.s_addr = htonl(AdapterInfo.ulIpAddress);
258 #endif
259 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
260 printf( "\n IP Address:\t\t\t\t%s\n", Ipaddr );
261 #ifdef _WIN32
262 addr.S_un.S_addr = htonl(AdapterInfo.ulSubnetMask);
263 #elif __linux__
264 addr.s_addr = htonl(AdapterInfo.ulSubnetMask);
265 #endif
266 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
267 printf( " Subnet mask:\t\t\t%s\n", Ipaddr );
268 #ifdef _WIN32
269 addr.S_un.S_addr = htonl(AdapterInfo.ulDefaultGatewayIPAddress);
270 #elif __linux__
271 addr.s_addr = htonl(AdapterInfo.ulDefaultGatewayIPAddress);
272 #endif
273 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
274 printf( " Default GW IP address:\t\t%s\n", Ipaddr );
275 printf( " Number of Occupied Stations:\t%d\n", UserSlaveParameter.usOccupiedStationNumber );
276 printf( " Cyclic response wait time:\t\t%d[ms] (0:Not wait)\n", UserSlaveParameter.ulCyclicResponseWaitTime );
278 /****************************************************************************/
279 /* Initialization of the SLMP Server. */
280 /****************************************************************************/
281 /* Set the SLMP Server Information */
282 ServerInfo.usVenderCode = Slave.usVenderCode;
283 ServerInfo.ulModelCode = Slave.ulModelCode;
284 ServerInfo.usMachineVersion = Slave.usMachineVersion;
285 memcpy( &ServerInfo.aucMacAddress, AdapterInfo.aucMacAddress, sizeof( ServerInfo.aucMacAddress ) );
286 ServerInfo.ulIpAddress = AdapterInfo.ulIpAddress;
287 ServerInfo.ulSubnetMask = AdapterInfo.ulSubnetMask;
288 ServerInfo.ulDefaultGatewayIPAddress = AdapterInfo.ulDefaultGatewayIPAddress;
289 ServerInfo.usPortNumber = USER_SERVER_PORT_NUMBER;
290 #ifdef _WIN32
291 strncpy_s( (char*)ServerInfo.acHostname, sizeof(ServerInfo.acHostname), USER_SERVER_HOSTNAME, 64 );
292 #elif __linux__
293 strncpy( (char*)ServerInfo.acHostname, USER_SERVER_HOSTNAME, 64 );
294 #endif
295 ServerInfo.usStatus = 0x0000;
296 memcpy( &ServerInfo.acTypeName, USER_TYPE_NAME, sizeof(ServerInfo.acTypeName) );
297 ServerInfo.usTypeNameCode = USER_TYPE_NAME_CODE;
298 ServerInfo.pusMemory = ausUserMemory;
299 ServerInfo.uiMemorySize = sizeof( ausUserMemory ) / sizeof( uint16_t );
301 /* Initialization of the SLMP Server */
302 iErrCode = slmp_server_initialize( &ServerInfo, &user_callback_set_ip_address_basic, &user_callback_parameter_get, &user_callback_parameter_set,
303 &user_callback_parameter_set_end, &user_callback_remote_reset );
304 if ( iErrCode != SLMP_SERVER_ERR_OK )
305 {
306 printf( "\nSLMP Server initialization error!\n" );
307 goto end;
308 }
310 /* Set the slave information data */
311 ccief_basic_slave_set_err_code( USER_ERR_OK );
312 ccief_basic_slave_set_unit_info( CCIEF_BASIC_UNIT_INFO_APPLICATION_STOP );
313 ccief_basic_slave_set_unit_data( 0x00000000 );
315 /* Show the information for display of the cyclic state */
316 printf( "\n Show the cyclic information at %d-second intervals.\n", ( USER_DISPLAY_INTERVAL_TIME / 1000 ));
317 printf( "\n" );
318 printf( " Master | Cyclic Data | Link scan time | Time Data\n" );
319 printf( "\n" );
320 printf( " Master - The control master station.(Master ID / Group Number)\n" );
321 printf( " Cyclic Data - Count of the receive Cyclic Data at intervals.\n" );
322 printf( " Link scan time - The link scan time at intervals.[ms]\n" );
323 printf( " Time Data - The time from the master information data.\n" );
324 printf( "\n" );
326 /****************************************************************************/
327 /* Main loop of sample code. */
328 /****************************************************************************/
329 while ( 1 )
330 {
331 /* CCIEF-BASIC Slave */
332 iErrCode = ccief_basic_slave_main();
333 if ( iErrCode != CCIEF_BASIC_SLAVE_ERR_OK )
334 {
335 break;
336 }
338 /* SLMP Server */
339 iErrCode = slmp_server_main();
340 if ( iErrCode != SLMP_SERVER_ERR_OK )
341 {
342 break;
343 }
345 /* Timer */
346 timer_main();
348 /* Check reset */
349 if ( iReset == USER_RESET )
350 {
351 break;
352 }
354 /* Display of the cyclic information */
355 user_display_cyclic_information();
356 }
358 /* Termination of CCIEF-BASIC Slave */
359 ccief_basic_slave_terminate();
361 /* Termination of SLMP Server */
362 slmp_server_terminate();
364 /* Termination of the timer environment */
365 timer_terminate();
367 /* Check the reset */
368 if ( iReset == USER_RESET )
369 {
370 printf( "\nReset the unit!\n" );
371 goto reset;
372 }
374 /* Exit the application */
375 end:
376 #ifdef _WIN32
377 /* WinSock clean up */
378 WSACleanup();
379 #elif __linux__
380 #endif
381 printf( "\nApplication has exited.(please any press)\n" );
382 #ifdef _WIN32
383 _getch();
384 #elif __linux__
385 getchar();
386 #endif
387 return;
388 }
390 /************************************************************************************/
391 /* This is an user defined function for callback function for receiving */
392 /* the Cyclic Data from the master. */
393 /************************************************************************************/
394 void user_callback_recv_cyclic_data( int iCyclicState, int iOccupiedStationNumber )
395 {
396 uint16_t *pusRWw, *pusRY, *pusRX, *pusRWr;
397 uint32_t ulTimerStart, ulTimerEnd;
399 /*[ Example: Sample code is data transfer from receiving data to sending data. ]*/
400 /* Check the cyclic state. */
401 if ( iCyclicState == CCIEF_BASIC_CYCLIC_DATA_VALID )
402 {
403 /* Please write here receiving the cyclic data from the master. */
405 /* Getting the start pointer of RWw */
406 pusRWw = ccief_basic_slave_get_pointer( CCIEF_BASIC_DEVICE_TYPE_RWW );
407 /* Getting the start pointer of RY */
408 pusRY = ccief_basic_slave_get_pointer( CCIEF_BASIC_DEVICE_TYPE_RY );
410 /* Getting the start pointer of RWr */
411 pusRWr = ccief_basic_slave_get_pointer( CCIEF_BASIC_DEVICE_TYPE_RWR );
412 /* Getting the start pointer of RX */
413 pusRX = ccief_basic_slave_get_pointer( CCIEF_BASIC_DEVICE_TYPE_RX );
415 /* Data transfer from RY to RX */
416 memcpy( pusRX, pusRY, iOccupiedStationNumber * CCIEF_BASIC_RX_RY_SIZE );
417 /* Data transfer from RWw to RWr */
418 memcpy( pusRWr, pusRWw, iOccupiedStationNumber * CCIEF_BASIC_RWW_RWR_SIZE );
419 }
421 /* Increase the counter of Cyclic Data. */
422 ulCyclicDataCount ++;
424 /*[ Example: Wait for cyclic response for sample code.(for debug the slow scan time) ]*/
425 if ( UserSlaveParameter.ulCyclicResponseWaitTime != 0 )
426 {
427 ulTimerStart = timer_get_time();
428 while ( 1 )
429 {
430 ulTimerEnd = timer_get_time();
431 if(ulTimerEnd < ulTimerStart)
432 {
433 ulTimerEnd = ulTimerEnd + (((int64_t)LONG_MAX * 1000 / CLOCKS_PER_SEC) - ulTimerStart);
434 if ( UserSlaveParameter.ulCyclicResponseWaitTime < ulTimerEnd )
435 {
436 break;
437 }
438 }
439 else
440 {
441 if ( UserSlaveParameter.ulCyclicResponseWaitTime < ( ulTimerEnd - ulTimerStart ))
442 {
443 break;
444 }
445 }
446 }
447 }
449 /*[ Example: Set the unit information data. ]*/
450 ccief_basic_slave_set_unit_info( CCIEF_BASIC_UNIT_INFO_APPLICATION_RUNNING );
452 return;
453 }
455 /************************************************************************************/
456 /* This is an user defined function for cyclic disconnection timeout. */
457 /************************************************************************************/
458 void user_callback_cyclic_disconnection( void )
459 {
460 /*[ Example: Set the unit information data. ]*/
461 ccief_basic_slave_set_unit_info( CCIEF_BASIC_UNIT_INFO_APPLICATION_STOP );
463 /* Please write for disconnection of cyclic operateion here */
465 return;
466 }
468 /************************************************************************************/
469 /* This is an user defined function for callback function for set the ip address. */
470 /* For CC-Link IEF Basic use. */
471 /************************************************************************************/
472 void user_callback_set_ip_address_basic( uint32_t ulIpAddress, uint32_t ulSubnetMask )
473 {
474 /* check the subnet mask */
475 if ( ulSubnetMask == 0xFFFFFFFF )
476 {
477 /* Not setting */
478 ulSubnetMask = 0;
479 }
481 /* set the ip address */
482 AdapterInfo.ulIpAddress = ulIpAddress;
483 AdapterInfo.ulSubnetMask = ulSubnetMask;
484 UserSlaveParameter.ulIpAddress = ulIpAddress;
485 UserSlaveParameter.ulSubnetMask = ulSubnetMask;
487 /* Write the parameter file */
488 (void)user_parameter_file_write( acParameterFilePath, &UserSlaveParameter );
490 iReset = USER_RESET;
491 return;
492 }
494 /************************************************************************************/
495 /* This is an user defined function for callback function for parameter get. */
496 /************************************************************************************/
497 int user_callback_parameter_get( uint16_t usId, uint16_t *pusSize, uint8_t **ppucData )
498 {
499 struct in_addr addr;
500 char *addrStr;
501 char Ipaddr[16];
503 /* Check the parameter id */
504 switch ( usId )
505 {
506 /* Getting of the ip address */
507 case USER_PARAMETER_ID_IP_ADDRESS:
508 #ifdef _WIN32
509 addr.S_un.S_addr = htonl(UserSlaveParameter.ulIpAddress);
510 #elif __linux__
511 addr.s_addr = htonl(UserSlaveParameter.ulIpAddress);
512 #endif
513 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
514 addrStr = Ipaddr;
515 *pusSize = (uint16_t)strlen( addrStr );
516 *ppucData = (uint8_t*)addrStr;
517 break;
518 /* Getting of the subnet mask */
519 case USER_PARAMETER_ID_SUBNET_MASK:
520 #ifdef _WIN32
521 addr.S_un.S_addr = htonl(UserSlaveParameter.ulSubnetMask);
522 #elif __linux__
523 addr.s_addr = htonl(UserSlaveParameter.ulSubnetMask);
524 #endif
525 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
526 addrStr = Ipaddr;
527 *pusSize = (uint16_t)strlen( addrStr );
528 *ppucData = (uint8_t*)addrStr;
529 break;
530 /* Getting of the default gateway ip address */
531 case USER_PARAMETER_ID_DEFAULT_GW_IP_ADDRESS:
532 #ifdef _WIN32
533 addr.S_un.S_addr = htonl(UserSlaveParameter.ulDefaultGatewayIPAddress);
534 #elif __linux__
535 addr.s_addr = htonl(UserSlaveParameter.ulDefaultGatewayIPAddress);
536 #endif
537 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
538 addrStr = Ipaddr;
539 *pusSize = (uint16_t)strlen( addrStr );
540 *ppucData = (uint8_t*)addrStr;
541 break;
542 /* Getting of the occupied station number */
543 case USER_PARAMETER_ID_OCCUPIED_STATION_NUMBER:
544 *pusSize = sizeof( uint16_t );
545 *ppucData = (uint8_t*)&UserSlaveParameter.usOccupiedStationNumber;
546 break;
547 /* Getting of the cyclic response wait time */
548 case USER_PARAMETER_ID_CYCLIC_RESPONSE_WAIT_TIME:
549 *pusSize = sizeof( uint16_t );
550 *ppucData = (uint8_t*)&UserSlaveParameter.ulCyclicResponseWaitTime;
551 break;
553 /* Please write user parameters here. */
555 default:
556 return SLMP_ERR_NO_EXIST_PARAM_ID;
557 }
559 return SLMP_ERR_OK;
560 }
562 /************************************************************************************/
563 /* This is an user defined function for callback function for parameter set. */
564 /************************************************************************************/
565 int user_callback_parameter_set( uint16_t usId, uint16_t usSize, uint8_t *pucData )
566 {
567 uint32_t ulIpaddr;
568 int iResult;
570 /* Check the parameter id */
571 switch ( usId )
572 {
573 /* Getting of the ip address */
574 case USER_PARAMETER_ID_IP_ADDRESS:
575 iResult = inet_pton( AF_INET, (char*)pucData, &ulIpaddr );
576 if( iResult == 1 )
577 {
578 UserSlaveParameter.ulIpAddress = htonl(ulIpaddr);
579 }
580 else
581 {
582 UserSlaveParameter.ulIpAddress = 0;
583 }
584 break;
585 /* Getting of the subnet mask */
586 case USER_PARAMETER_ID_SUBNET_MASK:
587 inet_pton( AF_INET, (char*)pucData, &ulIpaddr );
588 iResult = inet_pton( AF_INET, (char*)pucData, &ulIpaddr );
589 if( iResult == 1 )
590 {
591 UserSlaveParameter.ulSubnetMask = htonl(ulIpaddr);
592 }
593 else
594 {
595 UserSlaveParameter.ulSubnetMask = 0;
596 }
597 break;
598 /* Getting of the default gateway ip address */
599 case USER_PARAMETER_ID_DEFAULT_GW_IP_ADDRESS:
600 iResult = inet_pton( AF_INET, (char*)pucData, &ulIpaddr );
601 if( iResult == 1 )
602 {
603 UserSlaveParameter.ulDefaultGatewayIPAddress = htonl(ulIpaddr);
604 }
605 else
606 {
607 UserSlaveParameter.ulDefaultGatewayIPAddress = 0;
608 }
609 break;
610 /* Getting of the occupied station number */
611 case USER_PARAMETER_ID_OCCUPIED_STATION_NUMBER:
612 memcpy( &UserSlaveParameter.usOccupiedStationNumber, pucData, sizeof( UserSlaveParameter.usOccupiedStationNumber ) );
613 break;
614 /* Getting of the cyclic response wait time */
615 case USER_PARAMETER_ID_CYCLIC_RESPONSE_WAIT_TIME:
616 memcpy( &UserSlaveParameter.ulCyclicResponseWaitTime, pucData, sizeof( UserSlaveParameter.ulCyclicResponseWaitTime ) );
617 break;
619 /* Please write user parameters here. */
621 default:
622 return SLMP_ERR_NO_EXIST_PARAM_ID;
623 }
625 return SLMP_ERR_OK;
626 }
628 /************************************************************************************/
629 /* This is an user defined function for callback function for parameter set end. */
630 /************************************************************************************/
631 int user_callback_parameter_set_end( void )
632 {
633 /* Write the parameter file */
634 (void)user_parameter_file_write( acParameterFilePath, &UserSlaveParameter );
636 /* Read the parameter file */
637 iParameterFileRead = USER_PARAMETER_FILE_READ;
639 return SLMP_ERR_OK;
640 }
642 /************************************************************************************/
643 /* This is an user defined function for callback function for remote reset */
644 /************************************************************************************/
645 int user_callback_remote_reset( void )
646 {
647 /* Reset the operation */
648 iReset = USER_RESET;
650 return SLMP_ERR_OK;
651 }
653 /************************************************************************************/
654 /* This is an user defined function for read of the parameter file (csv format). */
655 /************************************************************************************/
656 int user_parameter_file_read( char *file_path, USER_SLAVE_PARAMETER *pParameter, USER_ADAPTER_INFO *pAdapterInfo )
657 {
658 uint32_t ulIpaddr;
659 FILE *fp;
660 #ifdef _WIN32
661 errno_t error;
662 #elif __linux__
663 #endif
664 static char aucLine[32];
665 static char aucId[32];
666 static char aucData[32];
667 int iId, iResult;
668 int iErrCode = USER_ERR_OK;
670 #ifdef _WIN32
671 error = fopen_s( &fp, file_path, "r" );
672 if ( error != 0 )
673 #elif __linux__
674 fp = fopen( file_path, "r" );
675 if ( fp == NULL )
676 #endif
677 {
678 printf( "Could not open file \"%s\"\n", file_path );
679 return USER_ERR_NG;
680 }
682 /* Start analyze the parameter file */
683 while ( fgets( aucLine, sizeof( aucLine ), fp ) != NULL )
684 {
685 memset( aucId, 0, sizeof( aucId ) );
686 memset( aucData, 0, sizeof( aucData ) );
687 #ifdef _WIN32
688 sscanf_s( aucLine, "%[^,],%[^,]", aucId, sizeof(aucId), aucData, sizeof(aucData) );
689 #elif __linux__
690 sscanf( aucLine, "%[^,],%[^,]", aucId, aucData );
691 #endif
692 iId = strtol( aucId, NULL, 0 );
693 switch ( iId )
694 {
695 case USER_PARAMETER_ID_IP_ADDRESS:
696 iResult = inet_pton( AF_INET, aucData, &ulIpaddr );
697 if ( iResult == 1 )
698 {
699 pParameter->ulIpAddress = htonl(ulIpaddr);
700 }
701 /* Check the ip address */
702 if ( pParameter->ulIpAddress != 0 )
703 {
704 /* Setting the adapter ip address */
705 pAdapterInfo->ulIpAddress = pParameter->ulIpAddress;
706 }
707 break;
708 case USER_PARAMETER_ID_SUBNET_MASK:
709 iResult = inet_pton( AF_INET, aucData, &ulIpaddr );
710 if ( iResult == 1 )
711 {
712 pParameter->ulSubnetMask = htonl(ulIpaddr);
713 }
714 /* Check the subnet mask */
715 if ( pParameter->ulSubnetMask != 0 )
716 {
717 /* Setting the adapter subnet mask */
718 pAdapterInfo->ulSubnetMask = pParameter->ulSubnetMask;
719 }
720 break;
721 case USER_PARAMETER_ID_DEFAULT_GW_IP_ADDRESS:
722 iResult = inet_pton( AF_INET, aucData, &ulIpaddr );
723 if ( iResult == 1 )
724 {
725 pParameter->ulDefaultGatewayIPAddress = htonl(ulIpaddr);
726 }
727 /* Check the default gateway ip address */
728 if ( pParameter->ulDefaultGatewayIPAddress != 0 )
729 {
730 /* Setting the adapter default gateway ip address */
731 pAdapterInfo->ulDefaultGatewayIPAddress = pParameter->ulDefaultGatewayIPAddress;
732 }
733 break;
734 case USER_PARAMETER_ID_OCCUPIED_STATION_NUMBER:
735 pParameter->usOccupiedStationNumber = (uint16_t)strtoul( aucData, NULL, 0 );
736 /* Check the occupied station number */
737 if (( pParameter->usOccupiedStationNumber <= 0 ) || ( CCIEF_BASIC_MAX_NUMBER_OF_OCCUPIED_STATIONS_GROUP < pParameter->usOccupiedStationNumber ))
738 {
739 printf( "Parameter Error: Invalid number of occupied stations! %d (range: 1-64)\n", pParameter->usOccupiedStationNumber );
740 iErrCode = USER_ERR_NG;
741 goto end;
742 }
743 break;
744 case USER_PARAMETER_ID_CYCLIC_RESPONSE_WAIT_TIME:
745 pParameter->ulCyclicResponseWaitTime = strtoul( aucData, NULL, 0 );
746 break;
748 /* Please write user parameters here. */
750 default:
751 break;
752 }
753 }
755 end:
756 fclose( fp );
758 return USER_ERR_OK;
759 }
761 /************************************************************************************/
762 /* This is an user defined function for write of the parameter file (csv format). */
763 /************************************************************************************/
764 int user_parameter_file_write( char *file_path, USER_SLAVE_PARAMETER *pParameter )
765 {
766 static char aucLine[1024];
767 struct in_addr addr;
768 FILE *fp;
769 #ifdef _WIN32
770 errno_t error;
771 #elif __linux__
772 #endif
773 char Ipaddr[16];
775 #ifdef _WIN32
776 error = fopen_s( &fp, file_path, "w" );
777 if ( error != 0 )
778 #elif __linux__
779 fp = fopen( file_path, "w" );
780 if ( fp == NULL )
781 #endif
782 {
783 printf( "Could not open file \"%s\"\n", file_path );
784 return USER_ERR_NG;
785 }
787 #ifdef _WIN32
788 sprintf_s( aucLine, sizeof(aucLine), ",,\n" );
789 #elif __linux__
790 sprintf( aucLine, ",,\n" );
791 #endif
792 fputs( aucLine, fp );
793 #ifdef _WIN32
794 sprintf_s( aucLine, sizeof(aucLine), "CCIEF-SIM Slave Sample Parameter,,\n" );
795 #elif __linux__
796 sprintf( aucLine, "CCIEF-SIM Slave Sample Parameter,,\n" );
797 #endif
798 fputs( aucLine, fp );
799 #ifdef _WIN32
800 sprintf_s( aucLine, sizeof(aucLine), ",,\n" );
801 #elif __linux__
802 sprintf( aucLine, ",,\n" );
803 #endif
804 fputs( aucLine, fp );
805 #ifdef _WIN32
806 sprintf_s( aucLine, sizeof(aucLine),"ID,DATA,COMMENT\n" );
807 #elif __linux__
808 sprintf( aucLine, "ID,DATA,COMMENT\n" );
809 #endif
810 fputs( aucLine, fp );
812 if ( pParameter->ulIpAddress != 0 )
813 {
814 #ifdef _WIN32
815 addr.S_un.S_addr = htonl(pParameter->ulIpAddress);
816 #elif __linux__
817 addr.s_addr = htonl(pParameter->ulIpAddress);
818 #endif
819 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
820 #ifdef _WIN32
821 sprintf_s( aucLine, sizeof(aucLine), "1,%s,IP address\n", Ipaddr );
822 #elif __linux__
823 sprintf( aucLine, "1,%s,IP address\n", Ipaddr );
824 #endif
825 }
826 else
827 {
828 #ifdef _WIN32
829 sprintf_s( aucLine, sizeof(aucLine), "1,0,IP address\n" );
830 #elif __linux__
831 sprintf( aucLine, "1,0,IP address\n" );
832 #endif
833 }
834 fputs( aucLine, fp );
835 if ( pParameter->ulSubnetMask != 0 )
836 {
837 #ifdef _WIN32
838 addr.S_un.S_addr = htonl(pParameter->ulSubnetMask);
839 #elif __linux__
840 addr.s_addr = htonl(pParameter->ulSubnetMask);
841 #endif
842 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
843 #ifdef _WIN32
844 sprintf_s( aucLine, sizeof(aucLine), "2,%s,Subnet mask\n", Ipaddr );
845 #elif __linux__
846 sprintf( aucLine, "2,%s,Subnet mask\n", Ipaddr );
847 #endif
848 }
849 else
850 {
851 #ifdef _WIN32
852 sprintf_s( aucLine, sizeof(aucLine), "2,0,Subnet mask\n" );
853 #elif __linux__
854 sprintf( aucLine, "2,0,Subnet mask\n" );
855 #endif
856 }
857 fputs( aucLine, fp );
858 if ( pParameter->ulDefaultGatewayIPAddress != 0 )
859 {
860 #ifdef _WIN32
861 addr.S_un.S_addr = htonl(pParameter->ulDefaultGatewayIPAddress);
862 #elif __linux__
863 addr.s_addr = htonl(pParameter->ulDefaultGatewayIPAddress);
864 #endif
865 inet_ntop(AF_INET, &addr, Ipaddr, sizeof(Ipaddr) );
866 #ifdef _WIN32
867 sprintf_s( aucLine, sizeof(aucLine), "3,%s,Default gateway IP address\n", Ipaddr );
868 #elif __linux__
869 sprintf( aucLine, "3,%s,Default gateway IP address\n", Ipaddr );
870 #endif
871 }
872 else
873 {
874 #ifdef _WIN32
875 sprintf_s( aucLine, sizeof(aucLine), "3,0,Default gateway IP address\n" );
876 #elif __linux__
877 sprintf( aucLine, "3,0,Default gateway IP address\n" );
878 #endif
879 }
880 fputs( aucLine, fp );
881 #ifdef _WIN32
882 sprintf_s( aucLine, sizeof(aucLine), "4,%d,Number of occupied stations\n", pParameter->usOccupiedStationNumber );
883 #elif __linux__
884 sprintf( aucLine, "4,%d,Number of occupied stations\n", pParameter->usOccupiedStationNumber );
885 #endif
886 fputs( aucLine, fp );
887 #ifdef _WIN32
888 sprintf_s( aucLine, sizeof(aucLine), "5,%lu,Cyclic response wait time\n", pParameter->ulCyclicResponseWaitTime );
889 #elif __linux__
890 sprintf( aucLine, "5,%lu,Cyclic response wait time\n", pParameter->ulCyclicResponseWaitTime );
891 #endif
892 fputs( aucLine, fp );
894 /* Please write user parameters here. */
896 fclose( fp );
898 return USER_ERR_OK;
899 }
901 /************************************************************************************/
902 /* This is an user defined function for display the cyclic state. */
903 /************************************************************************************/
904 void user_display_cyclic_information( void )
905 {
906 static CCIEF_BASIC_SLAVE_MASTER_INFO MasterInfo;
907 TIMER_TIME_DATA TimeData;
908 static uint32_t ulDisplayTimerStart = 0; /* Start time of the display timer */
909 uint32_t ulDisplayTimerEnd; /* End time of the display timer */
910 int64_t llTimeData;
911 uint32_t ulInterval;
914 if ( ulDisplayTimerStart == 0 ) {
915 ulDisplayTimerStart = timer_get_time();
916 }
918 ulDisplayTimerEnd = timer_get_time();
919 if(ulDisplayTimerEnd < ulDisplayTimerStart)
920 {
921 ulInterval = ulDisplayTimerEnd + (((int64_t)LONG_MAX * 1000 / CLOCKS_PER_SEC) - ulDisplayTimerStart);
922 }
923 else
924 {
925 ulInterval = ulDisplayTimerEnd - ulDisplayTimerStart;
926 }
928 /* Check the display time. */
929 if ( USER_DISPLAY_INTERVAL_TIME < ulInterval )
930 {
931 ulDisplayTimerStart = ulDisplayTimerEnd;
933 /* Getting the master information */
934 ccief_basic_slave_get_master_info( &MasterInfo );
936 /* Show the current cyclic state */
937 if ( MasterInfo.ulId == 0 )
938 {
939 printf( "CCIEF-BASIC: Master(Unknown) | Cyclic Data=0 | 0.000[ms] |\n" );
940 }
941 else
942 {
943 if ( ulCyclicDataCount == 0 )
944 {
945 printf( "CCIEF-BASIC: Master(0x%08lX/%d) | Cyclic Data=%lu | 0.000[ms] |\n", MasterInfo.ulId,
946 ulCyclicDataCount, MasterInfo.ucGroupNumber );
947 }
948 else
949 {
950 printf( "CCIEF-BASIC: Master(0x%08lX/%d) | Cyclic Data=%lu | %01.3f[ms] | ", MasterInfo.ulId,
951 MasterInfo.ucGroupNumber, ulCyclicDataCount, ((float)USER_DISPLAY_INTERVAL_TIME / ulCyclicDataCount) );
952 memcpy( &llTimeData, MasterInfo.NotifyInfo.aucTimeData, sizeof( llTimeData ) );
953 timer_analyze_time_data( llTimeData, &TimeData );
954 printf( "%d-%02d-%02d %02d:%02d:%02d\n", TimeData.usYear, TimeData.usMonth, TimeData.usDay, TimeData.usHour,
955 TimeData.usMinute, TimeData.usSecond );
956 }
957 }
959 /* Clear the count of cyclic data */
960 ulCyclicDataCount = 0;
961 }
963 return;
964 }
966 #define MALLOC(x) HeapAlloc(GetProcessHeap(), 0, (x))
967 #define FREE(x) HeapFree(GetProcessHeap(), 0, (x))
969 /************************************************************************************/
970 /* This is an user defined function for get the adapter information. */
971 /* The following is one of a sample in the Windows OS. Please rewrite if necessary. */
972 /* The following is one of a sample in the Linux. Please rewrite if necessary. */
973 /************************************************************************************/
974 int user_get_adapter_info( USER_ADAPTER_INFO *pGetAdapterInfo )
975 {
976 #ifdef _WIN32
977 ULONG ulOutBufLen;
978 ULONG ulIpaddr, ulSubNetMask, ulDefGateway;
979 DWORD dwRetVal;
980 IP_ADAPTER_INFO *pAdapterInfo;
981 IP_ADAPTER_INFO *pAdapter;
982 int iAdapterNum;
983 int iKey;
984 int iInputNumber;
985 int i;
986 int iResult;
988 /* GetAdaptersInfo */
989 pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof( IP_ADAPTER_INFO ));
990 if ( pAdapterInfo == NULL )
991 {
992 printf( "Error allocating memory needed to call GetAdapterInfo\n" );
993 return USER_ERR_NG;
994 }
995 ulOutBufLen = sizeof( IP_ADAPTER_INFO );
997 if ( GetAdaptersInfo( pAdapterInfo, &ulOutBufLen ) == ERROR_BUFFER_OVERFLOW )
998 {
999 FREE( pAdapterInfo );
1000 pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC( ulOutBufLen );
1001 if ( pAdapterInfo == NULL )
1002 {
1003 printf( "Error allocating memory needed to call GetAdapterInfo\n" );
1004 return USER_ERR_NG;
1005 }
1006 }
1008 if (( dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen )) != NO_ERROR )
1009 {
1010 printf( "GetAdaptersInfo failed with error %d\n", dwRetVal );
1011 if ( pAdapterInfo )
1012 {
1013 FREE( pAdapterInfo );
1014 }
1015 return USER_ERR_NG;
1016 }
1018 pAdapter = pAdapterInfo;
1019 iAdapterNum = 0;
1021 /* Check the Multiple Adapter */
1022 if ( pAdapter->Next != NULL )
1023 {
1024 printf( "Found the multiple Network adapter!\n\n");
1025 while ( pAdapter )
1026 {
1027 printf( "%2d: \tAdapter desc: \t\t%s\n", iAdapterNum+1, pAdapter->Description );
1028 printf( "\tMAC address: \t\t%02X:%02X:%02X:%02X:%02X:%02X\n",
1029 pAdapter->Address[0], pAdapter->Address[1], pAdapter->Address[2],
1030 pAdapter->Address[3], pAdapter->Address[4], pAdapter->Address[5] );
1031 printf( "\tIP address: \t\t%s\n",
1032 pAdapter->IpAddressList.IpAddress.String );
1033 printf( "\tSubnet mask: \t\t%s\n", pAdapter->IpAddressList.IpMask.String );
1034 printf( "\tDefault GW IP address: \t%s\n", pAdapter->GatewayList.IpAddress.String );
1035 printf( "\n");
1036 iAdapterNum ++;
1037 pAdapter = pAdapter->Next;
1038 }
1039 printf( "Please select the adapter number (Press 'Enter' Key after select) [1-%d]: ", iAdapterNum );
1041 iInputNumber = 0;
1043 while ( 1 )
1044 {
1045 iKey = _getch();
1046 if ( iKey != 0x0d )
1047 {
1048 if (( 0x30 <= iKey ) && ( iKey <= 0x39 ))
1049 {
1050 printf( "%c",iKey);
1051 iInputNumber = (iInputNumber*10)+(iKey-0x30);
1052 }
1053 }
1054 else
1055 {
1056 printf( "\n");
1057 if (( iInputNumber == 0 ) || ( iAdapterNum < iInputNumber ))
1058 {
1059 printf( "Wrong number select!\n");
1060 iInputNumber = 0;
1061 printf( "Please select the adapter number (Press 'Enter' Key after select) [1-%d]: ", iAdapterNum );
1062 }
1063 else
1064 {
1065 break;
1066 }
1067 }
1068 }
1069 pAdapter = pAdapterInfo;
1070 for ( i = 1; i < iInputNumber; i++ )
1071 {
1072 pAdapter = pAdapter->Next;
1073 }
1074 }
1075 else
1076 {
1077 iAdapterNum = 1;
1078 }
1079 printf( "\n" );
1081 /* Conversion of endian */
1082 AdapterInfo.aucMacAddress[0] = pAdapter->Address[5];
1083 AdapterInfo.aucMacAddress[1] = pAdapter->Address[4];
1084 AdapterInfo.aucMacAddress[2] = pAdapter->Address[3];
1085 AdapterInfo.aucMacAddress[3] = pAdapter->Address[2];
1086 AdapterInfo.aucMacAddress[4] = pAdapter->Address[1];
1087 AdapterInfo.aucMacAddress[5] = pAdapter->Address[0];
1088 iResult = inet_pton( AF_INET, pAdapter->IpAddressList.IpAddress.String, &ulIpaddr );
1089 if ( iResult == 1)
1090 {
1091 AdapterInfo.ulIpAddress = htonl(ulIpaddr);
1092 }
1093 else
1094 {
1095 printf("Adapter IP address error.\n");
1096 return USER_ERR_NG;
1097 }
1098 iResult = inet_pton( AF_INET, pAdapter->IpAddressList.IpMask.String, &ulSubNetMask );
1099 if ( iResult == 1)
1100 {
1101 AdapterInfo.ulSubnetMask = htonl(ulSubNetMask);
1102 }
1103 else
1104 {
1105 printf("Adapter sub-net mask error.\n");
1106 return USER_ERR_NG;
1107 }
1108 iResult = inet_pton( AF_INET, pAdapter->GatewayList.IpAddress.String, &ulDefGateway );
1109 if ( iResult == 1)
1110 {
1111 AdapterInfo.ulDefaultGatewayIPAddress = htonl(ulDefGateway);
1112 }
1113 else
1114 {
1115 printf("Adapter default gateway IP address error.\n");
1116 return USER_ERR_NG;
1117 }
1119 FREE( pAdapterInfo );
1121 /* Copy the argument pointer */
1122 memcpy( pGetAdapterInfo, &AdapterInfo, sizeof(USER_ADAPTER_INFO) );
1124 return USER_ERR_OK;
1125 #elif __linux__
1126 struct ifconf Ifc_Get;
1127 struct ifreq Ifreq_Size[MAX_INTERFACE];
1128 struct sockaddr_in get_addr[MAX_INTERFACE];
1129 USER_NETWORK_INFO User_Adapter[MAX_INTERFACE];
1130 USER_NETWORK_INFO User_Get_Info;
1131 unsigned char *pMAC_addr;
1132 int sock_network;
1133 int i;
1134 int ikey;
1135 int iNet_Number=0;
1136 FILE *pFile;
1137 t_RouteInfo RouteInfo_Defo[MAX_INTERFACE];
1138 char input_line[128];
1140 Ifc_Get.ifc_len = sizeof( Ifreq_Size );
1141 Ifc_Get.ifc_ifcu.ifcu_buf=(void *)Ifreq_Size;
1143 /* Socket open */
1144 sock_network = socket( AF_INET, SOCK_DGRAM, 0 );
1145 if( sock_network < SOCKET_NOT_OPEN )
1146 {
1147 perror("Socket not open");
1148 return USER_ERR_NG;
1149 }
1151 /* Network interface list acquisition */
1152 if( ioctl( sock_network, SIOCGIFCONF, &Ifc_Get ) == -1 )
1153 {
1154 perror("Error can not get the interface list");
1155 close( sock_network );
1156 return USER_ERR_NG;
1157 }
1159 /* Calculate the number that came back from the kernel */
1160 iNet_Number = Ifc_Get.ifc_len / ( int )sizeof( struct ifreq );
1161 for(i=0;i<iNet_Number;i++)
1162 {
1163 /* Interface designation */
1164 strncpy( User_Adapter[i].USER_ifreq.ifr_name,Ifreq_Size[i].ifr_name,IFNAMSIZ-1 );
1166 /* IP address (IPv4) acquisition */
1167 if ( ioctl( sock_network, SIOCGIFADDR, &User_Adapter[i].USER_ifreq ) == -1 )
1168 {
1169 perror("Error can not get the IP address");
1170 close( sock_network );
1171 return USER_ERR_NG;
1172 }
1173 memcpy( &get_addr[i], &User_Adapter[i].USER_ifreq.ifr_addr, sizeof( struct sockaddr_in ));
1174 memcpy( &User_Adapter[i].IPAddr, inet_ntoa( get_addr[i].sin_addr ), sizeof( User_Adapter[i].IPAddr ));
1176 /* Mask address acquisition */
1177 if ( ioctl( sock_network, SIOCGIFNETMASK, &User_Adapter[i].USER_ifreq ) == -1 )
1178 {
1179 perror("Error can not get the Mask address");
1180 close( sock_network );
1181 return USER_ERR_NG;
1182 }
1183 memcpy( &get_addr[i], &User_Adapter[i].USER_ifreq.ifr_netmask, sizeof( struct sockaddr_in ));
1184 memcpy( &User_Adapter[i].IPMask, inet_ntoa( get_addr[i].sin_addr ), sizeof( User_Adapter[i].IPMask ));
1186 /* Mac address acquisition */
1187 if ( ioctl( sock_network, SIOCGIFHWADDR, &User_Adapter[i].USER_ifreq ) == -1 )
1188 {
1189 perror("Error can not get the Mac address");
1190 close( sock_network );
1191 return USER_ERR_NG;
1192 }
1193 pMAC_addr=(unsigned char *)&User_Adapter[i].USER_ifreq.ifr_hwaddr.sa_data;
1194 sprintf( User_Adapter[i].MACAddr, "%02x:%02x:%02x:%02x:%02x:%02x", *pMAC_addr, *(pMAC_addr+1), *(pMAC_addr+2), *(pMAC_addr+3), *(pMAC_addr+4), *(pMAC_addr+5));
1196 /* Get the default gateway from the system file */
1197 pFile = fopen( DIR_PROC_ROUTE , "r" );
1198 if( pFile == NULL )
1199 {
1200 printf("Not systemfile read");
1201 close( sock_network );
1202 return USER_ERR_NG;
1203 }
1205 while(1)
1206 {
1207 if ( fgets( User_Adapter[i].Defo, sizeof( User_Adapter[i].Defo ), pFile ) == 0)
1208 {
1209 memset( User_Adapter[i].Defo,0x00,sizeof( User_Adapter[i].Defo ));
1210 fclose( pFile );
1211 break;
1212 }
1213 //Route information acquisition
1214 memset( &RouteInfo_Defo[i], 0x00, sizeof( RouteInfo_Defo[i] ));
1215 if ( sscanf( User_Adapter[i].Defo, "%s%x%x%d%d%d%d%x%d%d%d",
1216 &RouteInfo_Defo[i].cIface,
1217 &RouteInfo_Defo[i].cDst,
1218 &RouteInfo_Defo[i].cGateway,
1219 &RouteInfo_Defo[i].iFlag,
1220 &RouteInfo_Defo[i].iRecCnt,
1221 &RouteInfo_Defo[i].iUse,
1222 &RouteInfo_Defo[i].iMetric,
1223 &RouteInfo_Defo[i].cMask,
1224 &RouteInfo_Defo[i].iMTU,
1225 &RouteInfo_Defo[i].iWindow,
1226 &RouteInfo_Defo[i].iIRTT) == 0 )
1227 {
1228 printf("Not file read");
1229 fclose( pFile );
1230 close( sock_network );
1231 return USER_ERR_NG;
1232 }
1233 /* device name and default GW flag check */
1234 if (( strstr( RouteInfo_Defo[i].cIface,User_Adapter[i].USER_ifreq.ifr_name ) != NULL ) && ( RouteInfo_Defo[i].iFlag == ( RTF_UP | RTF_GATEWAY )))
1235 {
1236 memcpy( &User_Adapter[i].Defo, inet_ntoa( RouteInfo_Defo[i].cGateway ), INET_ADDRSTRLEN );
1237 fclose( pFile );
1238 break;
1239 }
1240 memset( User_Adapter[i].Defo,0x00,sizeof( User_Adapter[i].Defo ));
1241 }
1242 }
1246 for(i=0;i<iNet_Number;i++)
1247 {
1248 printf( "%2d: \tAdapter desc: \t\t%s\n", i+1, User_Adapter[i].USER_ifreq.ifr_name );
1249 printf( "\tMAC address: \t\t%s\n", User_Adapter[i].MACAddr );
1250 printf( "\tIP address: \t\t%s\n",User_Adapter[i].IPAddr );
1251 printf( "\tSubnet mask: \t\t%s\n", User_Adapter[i].IPMask );
1252 printf( "\tDefault GW IP address: \t%s\n", User_Adapter[i].Defo );
1253 printf( "\n");
1254 }
1255 printf( "Please select the adapter number (Press 'enter' Key after select) [1-%d]: ", iNet_Number );
1256 while ( 1 )
1257 {
1258 fgets( input_line,sizeof( input_line ),stdin );
1259 sscanf( input_line,"%d",&ikey );
1260 if (( 1 > ikey ) || ( iNet_Number < ikey ))
1261 {
1262 printf( "Wrong number select!\n");
1263 printf( "Please select the adapter number (Press 'Enter' Key after select) [1-%d]: ", iNet_Number );
1264 }
1265 else
1266 {
1267 break;
1268 }
1269 }
1271 User_Get_Info = User_Adapter[ikey-1];
1273 /* Conversion of endian */
1274 AdapterInfo.aucMacAddress[0] = ( uint8_t ) User_Get_Info.USER_ifreq.ifr_hwaddr.sa_data[5];
1275 AdapterInfo.aucMacAddress[1] = ( uint8_t ) User_Get_Info.USER_ifreq.ifr_hwaddr.sa_data[4];
1276 AdapterInfo.aucMacAddress[2] = ( uint8_t ) User_Get_Info.USER_ifreq.ifr_hwaddr.sa_data[3];
1277 AdapterInfo.aucMacAddress[3] = ( uint8_t ) User_Get_Info.USER_ifreq.ifr_hwaddr.sa_data[2];
1278 AdapterInfo.aucMacAddress[4] = ( uint8_t ) User_Get_Info.USER_ifreq.ifr_hwaddr.sa_data[1];
1279 AdapterInfo.aucMacAddress[5] = ( uint8_t ) User_Get_Info.USER_ifreq.ifr_hwaddr.sa_data[0];
1280 AdapterInfo.ulIpAddress = htonl( inet_addr( User_Get_Info.IPAddr ));
1281 AdapterInfo.ulSubnetMask = htonl( inet_addr( User_Get_Info.IPMask ));
1282 AdapterInfo.ulDefaultGatewayIPAddress = htonl(inet_addr( User_Get_Info.Defo ));
1284 close( sock_network );
1286 return USER_ERR_OK;
1287 #endif
1288 }
1290 /************************************************************************************/
1291 /* This is an user defined function for set the ip address. */
1292 /* The following is one of a sample in the Windows OS. Please rewrite if necessary. */
1293 /* The following is one of a sample in the Linux. Please rewrite if necessary. */
1294 /************************************************************************************/
1295 int user_set_adapter_info( USER_ADAPTER_INFO *pSetAdapterInfo )
1296 {
1297 #ifdef _WIN32
1298 static char str[2000];
1299 static char szAdapterName[BUFSIZ];
1300 uint8_t aucMacAddress[6];
1301 PIP_ADAPTER_ADDRESSES pAdapterAddresses, pAdapterAddress;
1302 DWORD dwRet, dwSize;
1303 int len;
1304 ULONG ulOutBufLen;
1305 ULONG ulIpaddr, ulSubNetMask, ulDefGateway;
1306 DWORD dwRetVal;
1307 IP_ADAPTER_INFO *pAdapterInfo;
1308 IP_ADAPTER_INFO *pAdapter;
1309 static int iKey, iFirst = 0;
1310 struct in_addr addrIpAddress, addrSubnetMask, addrDefaultGatewayIPAddress;
1311 char Ipaddr[16],SubNetMask[16],Gateway[16];
1313 /* GetAdaptersAddresses */
1314 dwRet = GetAdaptersAddresses( AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, NULL, &dwSize );
1315 if ( dwRet != ERROR_BUFFER_OVERFLOW )
1316 {
1317 printf( "Error call GetAdaptersAddresses\n" );
1318 return USER_ERR_NG;
1319 }
1321 pAdapterAddresses = (PIP_ADAPTER_ADDRESSES)MALLOC( dwSize );
1322 if (pAdapterAddresses == NULL)
1323 {
1324 printf( "Error allocating memory needed to call GetAdaptersAddresses\n" );
1325 return USER_ERR_NG;
1326 }
1328 dwRet = GetAdaptersAddresses( AF_UNSPEC, GAA_FLAG_INCLUDE_PREFIX, NULL, pAdapterAddresses, &dwSize );
1329 if (dwRet != ERROR_SUCCESS)
1330 {
1331 printf( "Error call GetAdaptersAddresses\n" );
1332 return USER_ERR_NG;
1333 }
1335 /* Conversion of endian */
1336 aucMacAddress[0] = pSetAdapterInfo->aucMacAddress[5];
1337 aucMacAddress[1] = pSetAdapterInfo->aucMacAddress[4];
1338 aucMacAddress[2] = pSetAdapterInfo->aucMacAddress[3];
1339 aucMacAddress[3] = pSetAdapterInfo->aucMacAddress[2];
1340 aucMacAddress[4] = pSetAdapterInfo->aucMacAddress[1];
1341 aucMacAddress[5] = pSetAdapterInfo->aucMacAddress[0];
1343 /* Get Adapter Name */
1344 for ( pAdapterAddress = pAdapterAddresses; pAdapterAddress; pAdapterAddress = pAdapterAddress->Next )
1345 {
1346 if ( memcmp( pAdapterAddress->PhysicalAddress, aucMacAddress, pAdapterAddress->PhysicalAddressLength ) == 0 )
1347 {
1348 len = WideCharToMultiByte(CP_ACP, 0,
1349 pAdapterAddress->FriendlyName, wcslen(pAdapterAddress->FriendlyName),
1350 szAdapterName, sizeof( szAdapterName ), NULL, NULL);
1351 if ( len == 0 )
1352 {
1353 printf( "Error Adapter Name\n" );
1354 return USER_ERR_NG;
1355 }
1357 szAdapterName[len] = '\0';
1358 break;
1359 }
1360 }
1362 FREE( pAdapterAddresses );
1364 /* GetAdaptersInfo */
1365 pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC(sizeof( IP_ADAPTER_INFO ));
1366 if ( pAdapterInfo == NULL )
1367 {
1368 printf( "Error allocating memory needed to call GetAdapterInfo\n" );
1369 return USER_ERR_NG;
1370 }
1371 ulOutBufLen = sizeof( IP_ADAPTER_INFO );
1373 if ( GetAdaptersInfo( pAdapterInfo, &ulOutBufLen ) == ERROR_BUFFER_OVERFLOW )
1374 {
1375 FREE( pAdapterInfo );
1376 pAdapterInfo = (IP_ADAPTER_INFO *) MALLOC( ulOutBufLen );
1377 if ( pAdapterInfo == NULL )
1378 {
1379 printf( "Error allocating memory needed to call GetAdapterInfo\n" );
1380 return USER_ERR_NG;
1381 }
1382 }
1384 if (( dwRetVal = GetAdaptersInfo( pAdapterInfo, &ulOutBufLen )) != NO_ERROR )
1385 {
1386 printf( "GetAdaptersInfo failed with error %d\n", dwRetVal );
1387 if ( pAdapterInfo )
1388 {
1389 FREE( pAdapterInfo );
1390 }
1391 return USER_ERR_NG;
1392 }
1394 pAdapter = pAdapterInfo;
1396 /* Check the adapter */
1397 if ( pAdapter != NULL )
1398 {
1399 while ( pAdapter )
1400 {
1401 if ( memcmp( pAdapter->Address, aucMacAddress, pAdapter->AddressLength ) == 0 )
1402 {
1403 inet_pton( AF_INET, pAdapter->IpAddressList.IpAddress.String, &ulIpaddr );
1404 inet_pton( AF_INET, pAdapter->IpAddressList.IpMask.String, &ulSubNetMask );
1405 inet_pton( AF_INET, pAdapter->GatewayList.IpAddress.String, &ulDefGateway );
1407 if (( pSetAdapterInfo->ulIpAddress == htonl(ulIpaddr))
1408 && ( pSetAdapterInfo->ulSubnetMask == htonl(ulSubNetMask))
1409 && ( pSetAdapterInfo->ulDefaultGatewayIPAddress == htonl(ulDefGateway)))
1410 {
1411 /* Nothing */
1412 }
1413 else
1414 {
1415 addrIpAddress.S_un.S_addr = htonl(pSetAdapterInfo->ulIpAddress);
1416 inet_ntop(AF_INET, &addrIpAddress, Ipaddr, sizeof(Ipaddr) );
1417 addrSubnetMask.S_un.S_addr = htonl(pSetAdapterInfo->ulSubnetMask);
1418 inet_ntop(AF_INET, &addrSubnetMask, SubNetMask, sizeof(SubNetMask) );
1419 addrDefaultGatewayIPAddress.S_un.S_addr = htonl(pSetAdapterInfo->ulDefaultGatewayIPAddress);
1420 inet_ntop(AF_INET, &addrDefaultGatewayIPAddress, Gateway, sizeof(Gateway) );
1421 /* Check the setting first */
1422 if ( iFirst == 0 )
1423 {
1424 printf( "Change the Network adapter setting ?\n" );
1425 printf( "\n ""\"%s\"""\n", szAdapterName );
1426 printf( "\tIP address:\t\t%s --> %s\n", pAdapter->IpAddressList.IpAddress.String, Ipaddr );
1427 printf( "\tSubnet mask:\t\t%s --> %s\n", pAdapter->IpAddressList.IpMask.String, SubNetMask );
1428 printf( "\tDefault GW IP address:\t%s --> %s\n", pAdapter->GatewayList.IpAddress.String, Gateway );
1429 while ( 1 )
1430 {
1431 printf( "\nPlease Press Key if change the Network adapter setting Yes('Y') or No('N')\n" );
1432 iKey = _getch();
1433 if (( iKey == 'Y' ) || ( iKey == 'y' ))
1434 {
1435 break;
1436 }
1437 else if (( iKey == 'N' ) || ( iKey == 'n' ))
1438 {
1439 FREE( pAdapterInfo );
1441 return USER_ERR_NG;
1442 }
1443 }
1444 }
1445 /* Change the Network adapter setting */
1446 sprintf_s( str, sizeof(str), "\nnetsh interface ip set address ""\"%s\""" static addr=%s mask=",
1447 szAdapterName, Ipaddr );
1448 strcat_s( str, sizeof(str), SubNetMask );
1449 if( addrDefaultGatewayIPAddress.S_un.S_addr != 0 )
1450 {
1451 strcat_s( str, sizeof(str), " gateway=" );
1452 strcat_s( str, sizeof(str), Gateway );
1453 }
1454 strcat_s( str, sizeof(str), " gwmetric=1" );
1455 printf( str );
1456 system( str );
1458 /* Wait for changing the ip address */
1459 Sleep(200);
1460 NotifyAddrChange( NULL, NULL );
1461 }
1462 iFirst = 1;
1463 break;
1464 }
1465 pAdapter = pAdapter->Next;
1466 }
1467 }
1469 FREE( pAdapterInfo );
1471 return USER_ERR_OK;
1472 #elif __linux__
1473 static struct ifconf Ifc_Get;
1474 struct ifreq Ifreq_Size[MAX_INTERFACE];
1475 struct in_addr addrip_get,addrMask_get,addrdg_get;
1476 struct in_addr addrip_set,addrMask_set,addrdg_set;
1477 struct ifreq ifr_set;
1478 struct sockaddr_in *s_in;
1479 struct sockaddr_in get_addr;
1480 USER_NETWORK_INFO User_Network[MAX_INTERFACE];
1481 USER_ADAPTER_INFO Get_NetworkInfo;
1482 FILE *pFile;
1483 t_RouteInfo RouteInfo_Defo;
1484 int sock_network;
1485 int iErrCode;
1486 int i;
1487 int iNet_Number;
1488 int Interface_Number;
1489 int NotGateway=0;
1490 char system_call[256];
1491 char iKey;
1492 uint8_t aucUser_MacAddress[6];
1493 unsigned char *pMAC_addr;
1495 Interface_Number=0;
1497 Ifc_Get.ifc_len = sizeof( Ifreq_Size );
1498 Ifc_Get.ifc_ifcu.ifcu_buf=(void *)Ifreq_Size;
1500 sock_network = socket( AF_INET, SOCK_DGRAM, 0 );
1501 if( sock_network < SOCKET_NOT_OPEN )
1502 {
1503 perror("Socket not open");
1504 return USER_ERR_NG;
1505 }
1507 if( ioctl ( sock_network, SIOCGIFCONF, &Ifc_Get ) == -1 )
1508 {
1509 perror("Error can not get the interface list");
1510 close( sock_network );
1511 return USER_ERR_NG;
1512 }
1514 /* Calculate the number that came back from the kernel */
1515 iNet_Number = Ifc_Get.ifc_len / ( int )sizeof( struct ifreq );
1517 for(i=0;i<iNet_Number;i++)
1518 {
1519 /* Interface specification */
1520 strncpy ( User_Network[i].USER_ifreq.ifr_name,Ifreq_Size[i].ifr_name,IFNAMSIZ-1 );
1522 /* Mac address acquisition */
1523 if( ioctl ( sock_network, SIOCGIFHWADDR, &User_Network[i].USER_ifreq ) == -1 )
1524 {
1525 perror("Error can not get the Mac address");
1526 close( sock_network );
1527 return USER_ERR_NG;
1528 }
1530 pMAC_addr=( unsigned char *)&User_Network[i].USER_ifreq.ifr_hwaddr.sa_data;
1531 sprintf( User_Network[i].MACAddr, "%02x:%02x:%02x:%02x:%02x:%02x", *pMAC_addr, *( pMAC_addr+1 ), *( pMAC_addr+2 ), *( pMAC_addr+3 ), *( pMAC_addr+4 ), *( pMAC_addr+5 ));
1532 aucUser_MacAddress[0] = ( uint8_t ) User_Network[i].USER_ifreq.ifr_hwaddr.sa_data[5];
1533 aucUser_MacAddress[1] = ( uint8_t ) User_Network[i].USER_ifreq.ifr_hwaddr.sa_data[4];
1534 aucUser_MacAddress[2] = ( uint8_t ) User_Network[i].USER_ifreq.ifr_hwaddr.sa_data[3];
1535 aucUser_MacAddress[3] = ( uint8_t ) User_Network[i].USER_ifreq.ifr_hwaddr.sa_data[2];
1536 aucUser_MacAddress[4] = ( uint8_t ) User_Network[i].USER_ifreq.ifr_hwaddr.sa_data[1];
1537 aucUser_MacAddress[5] = ( uint8_t ) User_Network[i].USER_ifreq.ifr_hwaddr.sa_data[0];
1539 if( memcmp(pSetAdapterInfo->aucMacAddress,aucUser_MacAddress,sizeof( aucUser_MacAddress )) == 0 )
1540 {
1541 Interface_Number=i;
1542 break;
1543 }
1544 }
1546 /* IP address (IPv4) acquisition */
1547 if ( ioctl ( sock_network, SIOCGIFADDR, &User_Network[Interface_Number].USER_ifreq ) == -1 )
1548 {
1549 perror( "Error can not get the IP address" );
1550 close( sock_network );
1551 return USER_ERR_NG;
1552 }
1553 memcpy( &get_addr, &User_Network[Interface_Number].USER_ifreq.ifr_addr, sizeof( struct sockaddr_in ));
1554 memcpy( &User_Network[Interface_Number].IPAddr, inet_ntoa( get_addr.sin_addr ), sizeof(User_Network[Interface_Number].IPAddr ));
1556 /* Mask address acquisition */
1557 if ( ioctl ( sock_network, SIOCGIFNETMASK, &User_Network[Interface_Number].USER_ifreq ) == -1 )
1558 {
1559 perror( "Error can not get the Mask address" );
1560 close( sock_network );
1561 return USER_ERR_NG;
1562 }
1563 memcpy( &get_addr, &User_Network[Interface_Number].USER_ifreq.ifr_netmask, sizeof( struct sockaddr_in ));
1564 memcpy( &User_Network[Interface_Number].IPMask, inet_ntoa( get_addr.sin_addr ), sizeof( User_Network[Interface_Number].IPMask ));
1566 /* Get the default gateway from the system file */
1567 pFile = fopen(DIR_PROC_ROUTE , "r" );
1568 if( pFile == NULL )
1569 {
1570 printf( "Not systemfile read" );
1571 close( sock_network );
1572 return USER_ERR_NG;
1573 }
1574 while(1)
1575 {
1576 if ( fgets( User_Network[Interface_Number].Defo, sizeof( User_Network[Interface_Number].Defo ), pFile ) == 0 )
1577 {
1578 memset( User_Network[Interface_Number].Defo,0x00,sizeof( User_Network[Interface_Number].Defo ));
1579 fclose( pFile );
1580 NotGateway = 1;
1581 break;
1582 }
1583 //Route information acquisition
1584 memset( &RouteInfo_Defo, 0x00, sizeof( RouteInfo_Defo ));
1585 if ( sscanf(User_Network[Interface_Number].Defo, "%s%x%x%d%d%d%d%x%d%d%d",
1586 &RouteInfo_Defo.cIface,
1587 &RouteInfo_Defo.cDst,
1588 &RouteInfo_Defo.cGateway,
1589 &RouteInfo_Defo.iFlag,
1590 &RouteInfo_Defo.iRecCnt,
1591 &RouteInfo_Defo.iUse,
1592 &RouteInfo_Defo.iMetric,
1593 &RouteInfo_Defo.cMask,
1594 &RouteInfo_Defo.iMTU,
1595 &RouteInfo_Defo.iWindow,
1596 &RouteInfo_Defo.iIRTT) == 0 )
1597 {
1598 printf( "Not file read" );
1599 close( sock_network );
1600 fclose( pFile );
1601 return USER_ERR_NG;
1602 }
1604 /* device name and default GW flag check */
1605 if (( strstr ( RouteInfo_Defo.cIface,User_Network[Interface_Number].USER_ifreq.ifr_name ) != NULL ) && ( RouteInfo_Defo.iFlag == ( RTF_UP | RTF_GATEWAY )))
1606 {
1607 memcpy( &User_Network[Interface_Number].Defo, inet_ntoa( RouteInfo_Defo.cGateway ), INET_ADDRSTRLEN );
1608 fclose( pFile );
1609 break;
1610 }
1611 memset( User_Network[Interface_Number].Defo,0x00,sizeof( User_Network[Interface_Number].Defo ));
1612 }
1614 Get_NetworkInfo.ulIpAddress = htonl( inet_addr(User_Network[Interface_Number].IPAddr ));
1615 Get_NetworkInfo.ulSubnetMask = htonl( inet_addr( User_Network[Interface_Number].IPMask ));
1616 Get_NetworkInfo.ulDefaultGatewayIPAddress = htonl( inet_addr(User_Network[Interface_Number].Defo ));
1618 /* Check that the network is the same */
1619 if(( pSetAdapterInfo->ulIpAddress == Get_NetworkInfo.ulIpAddress ) &&
1620 ( pSetAdapterInfo->ulSubnetMask == Get_NetworkInfo.ulSubnetMask ) &&
1621 ( pSetAdapterInfo->ulDefaultGatewayIPAddress == Get_NetworkInfo.ulDefaultGatewayIPAddress ))
1622 {
1623 return USER_ERR_OK;
1624 }
1625 else
1626 {
1627 addrip_get.s_addr=htonl( Get_NetworkInfo.ulIpAddress );
1628 addrip_set.s_addr=htonl( pSetAdapterInfo->ulIpAddress );
1629 addrMask_get.s_addr=htonl( Get_NetworkInfo.ulSubnetMask );
1630 addrMask_set.s_addr=htonl( pSetAdapterInfo->ulSubnetMask );
1631 addrdg_get.s_addr=htonl( Get_NetworkInfo.ulDefaultGatewayIPAddress );
1632 addrdg_set.s_addr=htonl( pSetAdapterInfo->ulDefaultGatewayIPAddress );
1633 printf( "Change the Network adapter setting ?\n" );
1634 printf( "%s", User_Network[Interface_Number].USER_ifreq.ifr_name );
1635 printf( "\tIP address: \t\t%s", inet_ntoa( addrip_get ));
1636 printf( "--> %s\n",inet_ntoa( addrip_set ));
1637 printf( "\tSubnet mask: \t\t%s",inet_ntoa( addrMask_get ));
1638 printf( " --> %s\n",inet_ntoa( addrMask_set ));
1639 printf( "\tDefault GW IP address: \t%s",inet_ntoa( addrdg_get ));
1640 printf( " --> %s\n",inet_ntoa ( addrdg_set ));
1641 while ( 1 )
1642 {
1643 printf( "\nPlease Press Key if change the Network adapter setting Yes('Y') or No('N')\n" );
1644 iKey= ( char )getchar();
1645 if (( iKey == 'Y' ) || ( iKey == 'y' ))
1646 {
1647 break;
1648 }
1649 else if (( iKey == 'N' ) || ( iKey == 'n' ))
1650 {
1651 return USER_ERR_NG;
1652 }
1653 }
1654 }
1656 /* Change the IP address */
1657 s_in = (struct sockaddr_in *)&ifr_set.ifr_addr;
1658 s_in->sin_family = AF_INET;
1659 s_in->sin_addr.s_addr = htonl( pSetAdapterInfo->ulIpAddress );
1660 strncpy( ifr_set.ifr_name,User_Network[Interface_Number].USER_ifreq.ifr_name,IFNAMSIZ-1 );
1661 if ( ioctl( sock_network,SIOCSIFADDR,&ifr_set ) == -1 )
1662 {
1663 perror("Error can not set the IP address");
1664 return USER_ERR_NG;
1665 }
1666 /* Change the address mask */
1667 s_in->sin_addr.s_addr = htonl( pSetAdapterInfo->ulSubnetMask );
1668 strncpy( ifr_set.ifr_name,User_Network[Interface_Number].USER_ifreq.ifr_name,IFNAMSIZ-1 );
1669 if ( ioctl( sock_network,SIOCSIFNETMASK,&ifr_set ) == -1 )
1670 {
1671 perror("Error can not set the Mask address");
1672 return USER_ERR_NG;
1673 }
1674 if( NotGateway == 1 )
1675 {
1676 /* Nothing */
1677 }
1678 else
1679 {
1680 /* Delete the default gateway */
1681 sprintf( system_call,"route del default gw %s",inet_ntoa( addrdg_get ));
1682 system(system_call);
1683 }
1684 /* add the default GW */
1685 sprintf( system_call,"route add default gw %s",inet_ntoa( addrdg_set ));
1686 system(system_call);
1687 /* Wait for changing the ip address */
1688 sleep(1);
1689 return USER_ERR_OK;
1690 #endif
1691 }