mmap works for output waveforms now.
[comedilib.git] / lib / calib.c
index c6cce94dd75b6b3cc59e6334cf261863d38e5cfb..87fc6340484653692c58ecd8f2791a234de0b348 100644 (file)
 #include <stdlib.h>
 #include <stdio.h>
 #include <string.h>
-#include <comedilib.h>
-#include <libinternal.h>
+#include "libinternal.h"
 
-static int check_cal_file( comedi_t *dev, const struct calibration_file_contents *parsed_file )
+static int set_calibration( comedi_t *dev, const comedi_calibration_t *parsed_file,
+       unsigned int cal_index );
+
+static int check_cal_file( comedi_t *dev, const comedi_calibration_t *parsed_file )
 {
        if( strcmp( comedi_get_driver_name( dev ), parsed_file->driver_name ) )
        {
-               fprintf( stderr, "driver name does not match calibration file\n" );
+               COMEDILIB_DEBUG( 3, "driver name does not match '%s' from calibration file\n",
+                       parsed_file->driver_name );
                return -1;
        }
 
        if( strcmp( comedi_get_board_name( dev ), parsed_file->board_name ) )
        {
-               fprintf( stderr, "board name does not match calibration file\n" );
+               COMEDILIB_DEBUG( 3, "board name does not match '%s' from calibration file\n",
+                       parsed_file->board_name );
                return -1;
        }
 
        return 0;
 }
 
-static inline int valid_channel( const struct calibration_file_contents *parsed_file,
+static inline int valid_channel( const comedi_calibration_t *parsed_file,
        unsigned int cal_index, unsigned int channel )
 {
        int num_channels, i;
 
-       num_channels = parsed_file->calibrations[ cal_index ].num_channels;
+       num_channels = parsed_file->settings[ cal_index ].num_channels;
        if( num_channels == 0 ) return 1;
        for( i = 0; i < num_channels; i++ )
        {
-               if( parsed_file->calibrations[ cal_index ].channels[ i ] == channel )
+               if( parsed_file->settings[ cal_index ].channels[ i ] == channel )
                        return 1;
        }
 
        return 0;
 }
 
-static inline int valid_range( const struct calibration_file_contents *parsed_file,
+static inline int valid_range( const comedi_calibration_t *parsed_file,
        unsigned int cal_index, unsigned int range )
 {
        int num_ranges, i;
 
-       num_ranges = parsed_file->calibrations[ cal_index ].num_ranges;
+       num_ranges = parsed_file->settings[ cal_index ].num_ranges;
        if( num_ranges == 0 ) return 1;
        for( i = 0; i < num_ranges; i++ )
        {
-               if( parsed_file->calibrations[ cal_index ].ranges[ i ] == range )
+               if( parsed_file->settings[ cal_index ].ranges[ i ] == range )
                        return 1;
        }
 
        return 0;
 }
 
-static inline int valid_aref( const struct calibration_file_contents *parsed_file,
+static inline int valid_aref( const comedi_calibration_t *parsed_file,
        unsigned int cal_index, unsigned int aref )
 {
        int num_arefs, i;
 
-       num_arefs = parsed_file->calibrations[ cal_index ].num_arefs;
+       num_arefs = parsed_file->settings[ cal_index ].num_arefs;
        if( num_arefs == 0 ) return 1;
        for( i = 0; i < num_arefs; i++ )
        {
-               if( parsed_file->calibrations[ cal_index ].arefs[ i ] == aref )
+               if( parsed_file->settings[ cal_index ].arefs[ i ] == aref )
                        return 1;
        }
 
        return 0;
 }
 
-static int find_calibration( const struct calibration_file_contents *parsed_file,
+static int apply_calibration( comedi_t *dev, const comedi_calibration_t *parsed_file,
        unsigned int subdev, unsigned int channel, unsigned int range, unsigned int aref )
 {
-       int num_cals, i;
+       int num_cals, i, retval;
+       int found_cal = 0;
 
-       num_cals = parsed_file->num_calibrations;
+       num_cals = parsed_file->num_settings;
 
        for( i = 0; i < num_cals; i++ )
        {
-               if( parsed_file->calibrations[ i ].subdevice != subdev ) continue;
+               if( parsed_file->settings[ i ].subdevice != subdev ) continue;
                if( valid_range( parsed_file, i, range ) == 0 ) continue;
                if( valid_channel( parsed_file, i, channel ) == 0 ) continue;
                if( valid_aref( parsed_file, i, aref ) == 0 ) continue;
-               break;
+
+               retval = set_calibration( dev, parsed_file, i );
+               if( retval < 0 ) return retval;
+               found_cal = 1;
+       }
+       if( found_cal == 0 )
+       {
+               COMEDILIB_DEBUG( 3, "failed to find matching calibration\n" );
+               return -1;
        }
-       if( i == num_cals ) return -1;
 
-       return i;
+       return 0;
 }
 
-static int set_calibration( comedi_t *dev, const struct calibration_file_contents *parsed_file,
+static int set_calibration( comedi_t *dev, const comedi_calibration_t *parsed_file,
        unsigned int cal_index )
 {
        int i, retval, num_caldacs;
 
-       num_caldacs = parsed_file->calibrations[ cal_index ].num_caldacs;
+       num_caldacs = parsed_file->settings[ cal_index ].num_caldacs;
+       COMEDILIB_DEBUG( 4, "num_caldacs %i\n", num_caldacs );
 
        for( i = 0; i < num_caldacs; i++ )
        {
-               struct caldac_setting caldac;
-
-               caldac = parsed_file->calibrations[ cal_index ].caldacs[ i ];
+               comedi_caldac_t caldac;
 
+               caldac = parsed_file->settings[ cal_index ].caldacs[ i ];
+               COMEDILIB_DEBUG( 4, "subdev %i, ch %i, val %i\n", caldac.subdevice,
+                       caldac.channel,caldac.value);
                retval = comedi_data_write( dev, caldac.subdevice, caldac.channel,
                        0, 0, caldac.value );
                if( retval < 0 ) return retval;
@@ -134,61 +148,97 @@ static int set_calibration( comedi_t *dev, const struct calibration_file_content
        return 0;
 }
 
-EXPORT_SYMBOL(comedi_apply_calibration,0.7.20);
-int comedi_apply_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel,
-       unsigned int range, unsigned int aref, const char *cal_file_path )
+EXPORT_ALIAS_DEFAULT(_comedi_apply_parsed_calibration,comedi_apply_parsed_calibration,0.7.20);
+int _comedi_apply_parsed_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel,
+       unsigned int range, unsigned int aref, const comedi_calibration_t *calibration )
 {
-       struct stat file_stats;
-       char file_path[ 1024 ];
        int retval;
-       int cal_index;
-       FILE *cal_file;
-       const struct calibration_file_contents *parsed_file;
 
-       if( cal_file_path )
-       {
-               strncpy( file_path, cal_file_path, sizeof( file_path ) );
-       }else
+       retval = check_cal_file( dev, calibration );
+       if( retval < 0 ) return retval;
+
+       retval = apply_calibration( dev, calibration, subdev, channel, range, aref );
+       return retval;
+}
+
+/* munge characters in board name that will cause problems with file paths */
+static void fixup_board_name( char *name )
+{
+       while( ( name = strchr( name, '/' ) ) )
        {
-               if( fstat( comedi_fileno( dev ), &file_stats ) < 0 )
+               if( name )
                {
-                       fprintf( stderr, "failed to get file stats of comedi device file\n" );
-                       return -1;
+                       *name = '-';
+                       name++;
                }
-
-               snprintf( file_path, sizeof( file_path ), "/etc/comedi/calibrations/%s_0x%lx",
-                       comedi_get_board_name( dev ),
-                       ( unsigned long ) file_stats.st_ino );
        }
+}
 
-       cal_file = fopen( file_path, "r" );
-       if( cal_file == NULL ) return -1;
-
-       parsed_file = parse_calibration_file( cal_file );
-       if( parsed_file == NULL ) return -1;
+EXPORT_ALIAS_DEFAULT(_comedi_get_default_calibration_path,comedi_get_default_calibration_path,0.7.20);
+char* _comedi_get_default_calibration_path( comedi_t *dev )
+{
+       struct stat file_stats;
+       char *file_path;
+       char *board_name, *temp;
+       char *driver_name;
 
-       fclose( cal_file );
+       if( fstat( comedi_fileno( dev ), &file_stats ) < 0 )
+       {
+               COMEDILIB_DEBUG( 3, "failed to get file stats of comedi device file\n" );
+               return NULL;
+       }
 
-       retval = check_cal_file( dev, parsed_file );
-       if( retval < 0 )
+       driver_name = comedi_get_driver_name( dev );
+       if( driver_name == NULL )
        {
-               cleanup_calibration_parse();
-               return retval;
+               return NULL;
        }
+       temp = comedi_get_board_name( dev );
+       if( temp == NULL )
+       {
+               return NULL;
+       }
+       board_name = strdup( temp );
+
+       fixup_board_name( board_name );
+       asprintf( &file_path, LOCALSTATEDIR "/lib/comedi/calibrations/%s_%s_comedi%li",
+               driver_name, board_name, ( unsigned long ) minor( file_stats.st_rdev ) );
+
+       free( board_name );
+       return file_path;
+}
+
+EXPORT_ALIAS_DEFAULT(_comedi_apply_calibration,comedi_apply_calibration,0.7.20);
+int _comedi_apply_calibration( comedi_t *dev, unsigned int subdev, unsigned int channel,
+       unsigned int range, unsigned int aref, const char *cal_file_path )
+{
+       char file_path[ 1024 ];
+       int retval;
+       comedi_calibration_t *parsed_file;
 
-       cal_index = find_calibration( parsed_file, subdev, channel, range, aref );
-       if( cal_index < 0 )
+       if( cal_file_path )
+       {
+               strncpy( file_path, cal_file_path, sizeof( file_path ) );
+       }else
        {
-               cleanup_calibration_parse();
-               return cal_index;
+               char *temp;
+
+               temp = comedi_get_default_calibration_path( dev );
+               if( temp == NULL ) return -1;
+               strncpy( file_path, temp, sizeof( file_path ) );
+               free( temp );
        }
 
-       retval = set_calibration( dev, parsed_file, cal_index );
-       if( retval < 0 );
+       parsed_file = comedi_parse_calibration_file( file_path );
+       if( parsed_file == NULL )
        {
-               cleanup_calibration_parse();
-               return retval;
+               COMEDILIB_DEBUG( 3, "failed to parse calibration file\n" );
+               return -1;
        }
 
-       return 0;
+       retval = comedi_apply_parsed_calibration( dev, subdev, channel, range, aref, parsed_file );
+
+       comedi_cleanup_calibration( parsed_file );
+
+       return retval;
 }