beginnings of comedi_set_calibration()
[comedilib.git] / lib / calib.c
1 /*
2     lib/calib.c
3     functions for setting calibration
4
5     Copyright (C) 2003 Frank Mori Hess <fmhess@users.sourceforge.net
6
7     This library is free software; you can redistribute it and/or
8     modify it under the terms of the GNU Lesser General Public
9     License as published by the Free Software Foundation, version 2.1
10     of the License.
11
12     This library is distributed in the hope that it will be useful,
13     but WITHOUT ANY WARRANTY; without even the implied warranty of
14     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15     Lesser General Public License for more details.
16
17     You should have received a copy of the GNU Lesser General Public
18     License along with this library; if not, write to the Free Software
19     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
20     USA.
21 */
22
23 #define _GNU_SOURCE
24
25 #include <stdlib.h>
26 #include <comedilib.h>
27 #include <libinternal.h>
28
29 static int ph_extract_element( const char *file_path, const char *element,
30         char *result, unsigned int result_size )
31 {
32         char perl_prog[ 1024 ];
33         FILE *perl_stdout;
34         int retval;
35
36         snprintf( perl_prog, sizeof( perl_prog ),
37                 "perl -e '"
38                 "use strict;"
39                 "use warnings;"
40                 "my $hash;"
41                 "my $cal;"
42                 "$hash = `cat %s`;"
43                 "eval \"\\$cal = $hash;\";"
44                 "print $cal->%s;"
45                 "'",
46                 file_path, element );
47
48         perl_stdout = popen( perl_prog, "r");
49         if( perl_stdout == NULL )
50         {
51                 fprintf( stderr, "popen() failed in ph_extract_element()\n" );
52                 return -1;
53         }
54
55         if( fgets( result, result_size, perl_stdout ) == NULL )
56         {
57                 fprintf( stderr, "fgets() returned NULL in ph_extract_element()\n" );
58                 return -1;
59         }
60
61         retval = pclose( perl_stdout );
62         if( retval )
63         {
64                 fprintf( stderr, "perl returned error %i\n in ph_extract_element()", retval );
65                 return -1;
66         }
67
68         return 0;
69 }
70
71 //EXPORT_SYMBOL(comedi_set_calibration,0.7.20);
72 int comedi_set_calibration( comedi_t *dev, const char *cal_file_path,
73         unsigned int subdev, unsigned int channel, unsigned int range, unsigned int aref )
74 {
75         struct stat file_stats;
76         FILE *cal_file;
77
78         if( cal_file_path )
79                 cal_file = fopen( cal_file_path, "r" );
80         else
81         {
82                 char *file_path;
83
84                 if( fstat( comedi_fileno( dev ), &file_stats ) < 0 )
85                 {
86                         fprintf( stderr, "failed to get file stats of comedi device file\n" );
87                         return -1;
88                 }
89
90                 asprintf( &file_path, "/etc/comedi/calibrations/%s_0x%lx",
91                         comedi_get_board_name( dev ),
92                         ( unsigned long ) file_stats.st_ino );
93                 cal_file = fopen( file_path, "r" );
94                 free( file_path );
95         }
96
97         if( cal_file == NULL )
98         {
99                 fprintf( stderr, "failed to open calibration file\n" );
100                 return -1;
101         }
102
103         return 0;
104 }