This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Demonstrates usage of the EIBnetmux layer 7 API.
/* * layer7 - work with physical knx devices * * eibnetmux - eibnet/ip multiplexer * Copyright (C) 2006-2009 Urs Zurbuchen <going_nuts@users.sourceforge.net> * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <stdio.h> #include <stdint.h> #include <stdlib.h> #include <libgen.h> #include <string.h> #include <getopt.h> #include <eibnetmux/enmx_lib.h> #include <mylib.h> #define FALSE 0 #define TRUE 1 /* * Global variables */ ENMX_HANDLE sock_con = 0; unsigned char conn_state = 0; /* * local function declarations */ static void Usage( char *prog ); static void Usage( char *prog ) { fprintf( stderr, "%s [options] <device address> <function>\n", basename( prog )); fprintf( stderr, "options:\n" ); fprintf( stderr, " -s server name of eibnetmux server (default: <search>)\n" ); fprintf( stderr, " -u user name of user (default: -)\n" ); fprintf( stderr, " -q no verbose output (default: no)\n\n" ); fprintf( stderr, "<device address> is knx physical address either as x.y.z or 16-bit integer\n" ); fprintf( stderr, "<function> is one of the following\n" ); fprintf( stderr, " 1 - reset device\n" ); fprintf( stderr, " 2 - read first 100 bytes of device memory\n" ); } int main( int argc, char **argv ) { int show_usage; uint16_t knxaddress = 0; int enmx_version; int quiet = 0; int c; int tmp; int offset; int length; int function; int line; int area; int device; unsigned char *buf; char *user = NULL; char pwd[255]; char *server = NULL; // get parameters show_usage = FALSE; opterr = 0; while( ( c = getopt( argc, argv, "qs:u:" )) != -1 ) { switch( c ) { case 's': server = strdup( optarg ); break; case 'u': user = strdup( optarg ); break; case 'q': quiet = 1; break; default: fprintf( stderr, "Invalid option: %c\n", c ); Usage( argv[0] ); exit( -1 ); } } if( optind + 2 != argc ) { show_usage = TRUE; } else { // knx device address if( strchr( argv[optind +0], '.' ) == NULL ) { if( sscanf( argv[optind +0], "%d", &tmp ) != 1 ) { show_usage = TRUE; } else { knxaddress = tmp & 0xffff; } } else { if( sscanf( argv[optind +0], "%d.%d.%d", &line, &area, &device ) != 3 ) { show_usage = TRUE; } else { knxaddress = (line << 12) + (area << 8) + device; } } // function if( sscanf( argv[optind +1], "%d", &function ) != 1 || function < 1 || function > 2 ) { show_usage = TRUE; } } if( show_usage == TRUE ) { Usage( basename( argv[0] )); exit( -1 ); } // access bus enmx_version = enmx_init(); sock_con = enmx_open( server, "layer7" ); if( sock_con < 0 ) { fprintf( stderr, "Connect to eibnetmux failed: %s\n", enmx_errormessage( sock_con )); exit( -2 ); } if( quiet == 0 ) printf( "Connecting to eibnetmux server on '%s'\n", enmx_gethost( sock_con )); // authenticate if( user != NULL ) { if( getpassword( pwd ) != 0 ) { fprintf( stderr, "Error reading password - cannot continue\n" ); exit( -6 ); } if( enmx_auth( sock_con, user, pwd ) != 0 ) { fprintf( stderr, "Authentication failure\n" ); exit( -3 ); } } if( quiet == 0 ) printf( "Connection to eibnetmux established\n" ); if( (c = enmx_L7_connect( sock_con, knxaddress )) != 0 ) { fprintf( stderr, "Unable to establish connection with %d.%d.%d: %d-%s\n", line, area, device, enmx_geterror( sock_con ), (c == ENMX_E_NO_CONNECTION) ? "invalid connection handle" : enmx_errormessage( enmx_geterror( sock_con ))); exit( -8 ); } switch( function ) { case 1: if( enmx_L7_reset( sock_con, knxaddress ) != 0 ) { fprintf( stderr, "Unable to reset device %d.%d.%d: %s\n", line, area, device, enmx_errormessage( sock_con )); exit( -9 ); } break; case 2: if( (buf = malloc( 0x100 )) == NULL ) { fprintf( stderr, "Out of memory\n" ); exit( -10 ); } offset = 0x100; length = 0x100; c = enmx_L7_readmemory( sock_con, knxaddress, offset, length, buf ); if( c < 0 ) { fprintf( stderr, "Unable to read memory of device %d.%d.%d: %s\n", line, area, device, enmx_errormessage( sock_con )); exit( -9 ); } else { if( quiet == 0 ) printf( "Read %d bytes of memory\n", c ); } if( quiet == 0 ) { printf( "Memory dump of %d.%d.%d\n", line, area, device ); tmp = 0; while( tmp < c ) { printf( "%04x %s\n", offset + tmp, hexdump( buf + tmp, ((c - tmp) > 0x10) ? 0x10 : c - tmp, 1 )); tmp += 0x10; } } free( buf ); break; } if( enmx_L7_disconnect( sock_con, knxaddress ) != 0 ) { fprintf( stderr, "Unable to disconnect from %d.%d.%d: %s\n", line, area, device, enmx_errormessage( sock_con )); exit( -11 ); } enmx_close( sock_con ); if( quiet == 0 ) printf( "Connection to eibnetmux closed\n" ); exit( 0 ); }