user_settings.c 7.57 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
/***********************************************************************************************
* MODULE:     user_settings.c
* PURPOSE:    user setting loading and storing
* WRITTEN BY: NDA
* COPYRIGHT:  CAEN S.p.A all rights reserved
* USAGE:      
*             compiled with gcc ?!?!?
*             platform Linux
************************************************************************************************/
////////////////////////////////////////////
// File includes
////////////////////////////////////////////
#include <malloc.h>
#include <stdlib.h>
#include <string.h>
#include "common_defs.h"
#include "user_settings.h"
#include "./include/N957Lib.h"

////////////////////////////////////////////
// File local defines
////////////////////////////////////////////
23
//JULIO#define DEF_LOG_FILENAME					"data.log"					/* read board values default filename */
24

25
#define DEF_CONFIG_FILENAME					"N957Run.conf"				/* configuration default filename */
26

27
//JULIO#define DEF_GNU_PLOT_PATH					"../gnuplot/"				/* GNUPLOT default path */
28
29
30
31
32
33
34

#define DEF_MAX_NUM_SAMPLES					-1							/* default number of samples */

#define DEF_BLDIM							32768						/* default value for bldim */

#define DEF_MODE							N957ControlModeAuto			/* default value for acquisition mode */

35
//JULIO#define DEF_GNU_PLOT_REFRESH				500							/* default gnu plot refresh rate (msec) */
36

37
//JULIO#define DEF_GNU_PLOT_X_SCALE				1.0							/* default gnu plot X conversion factor */
38
39
40
41
42
43
44
45
46


////////////////////////////////////////////
// File local variables declaration
////////////////////////////////////////////

////////////////////////////////////////////
// File local methods declaration
////////////////////////////////////////////
47
//static BOOL parse_config_file( user_setting_data* p_data);
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
static char* trim( char charset[], int n_chars, const char* string);

////////////////////////////////////////////
// Global visible variables declaration
////////////////////////////////////////////

/**************************************************
**************************************************/
BOOL user_settings_open( user_setting_data* p_data)
{
	// module variable init
	p_data->m_log_filename= (char*)malloc( MAX_FILENAME_LENGHT);
	p_data->m_config_filename= (char*)malloc( MAX_FILENAME_LENGHT);

	if( p_data->m_config_filename== NULL)
63
		TRACE("ME12:user setting module: insufficient memory !\n");
64
65
66
67
68
69
70
71
72
	else
		strcpy( p_data->m_config_filename, DEF_CONFIG_FILENAME);

	p_data->m_max_num_samples= DEF_MAX_NUM_SAMPLES;
	p_data->m_debug= N957_FALSE;
	p_data->m_bldim= DEF_BLDIM;
	p_data->m_mode= DEF_MODE;
	p_data->m_N957_handle= N957_INVALID_HANDLE_VALUE;
	p_data->m_log_to_file= N957_FALSE;
73
	//p_data->m_bd_num;
74

75
76
77
78
79
80
81
82
83
84
85
86
87
88
89

	return TRUE;
}

/**************************************************
**************************************************/
BOOL user_settings_close( user_setting_data* p_data) {
	//
	// Release board resources
	if( p_data->m_N957_handle!= N957_INVALID_HANDLE_VALUE) {
		N957_End( p_data->m_N957_handle);
	}

	// module variable free
	if( p_data->m_log_filename)
90
	 	free( p_data->m_log_filename);
91
92
93
94
95
	if( p_data->m_config_filename)
		free( p_data->m_config_filename);
	// setting invalid values ...
	p_data->m_log_filename= NULL;
	p_data->m_config_filename= NULL;
96
	// JULIO p_data->m_gnu_plot_path= NULL;
97
98
99
100
101
102

	return TRUE;
}

/**************************************************
**************************************************/
103
/*BOOL user_settings_parse_input_param( user_setting_data* p_data, int argc, char* argv[]) {
Julio Tambara's avatar
Julio Tambara committed
104
	//BOOL help_to_show= FALSE;
105
	int i;
106
	/*for( i= 1; i< argc; i++)
107
108
109
110
	{
		if( !strcmp( argv[i], "-h")||
			!strcmp( argv[i], "-H"))
		{
Julio Tambara's avatar
Julio Tambara committed
111
			//JULIOhelp_to_show= TRUE;
112
113
114
115
116
117
118
		} 
		else if( !strncmp( argv[i], "-f", 2)||
			!strncmp( argv[i], "-F", 2))
		{
			// config filename
			strcpy( p_data->m_config_filename, argv[i]+ 2);
		}
Julio Tambara's avatar
Julio Tambara committed
119
		/*JULIOelse
120
121
122
123
124
125
		{
			// Unknown parameter
			TRACE1("\nuser settings: ignored command line parameter '%s'", argv[i]);
			TRACE("\n  restart with -h flag for help on usage\n");
			help_to_show= TRUE;

126
		}
127
128
129
130
131
132
133
134
	}
	if( !parse_config_file( p_data))
		return FALSE;
	return TRUE;
}
/**************************************************
**************************************************/
BOOL parse_config_file( user_setting_data* p_data) {
135
	//char tmp_filename[ MAX_FILENAME_LENGHT];
136
137
138
139
140
141
	char line[ 200];
	char *str;
	FILE *p_conf_file= NULL;
	BOOL ret_val= TRUE;
	N957ErrorCodes N957_error_code;					// returned error code
	char trim_space_charset[]= {' ', '\t'};
142
	//char trim_text_charset[]= {' ', '\t', '"'};
143
144
145


	if( ( p_conf_file= fopen( p_data->m_config_filename, "r"))== NULL){
146
147
	    TRACE1("Can't open configuration file %s\n", p_data->m_config_filename);
	    return FALSE;
148
	}
149
150
151
152
153
154
	if( (N957_Init( (short)p_data->m_bd_num, &p_data->m_N957_handle)))
	{	
		ret_val= FALSE;
		goto exit_point;
	}
	
155
156
157
158
159
160
161
	while(!feof( p_conf_file)) {
		fscanf( p_conf_file, "%s", line);  // read one string from the file
		str= trim( trim_space_charset, sizeof( trim_space_charset), line);
		if ( str[0] == '#') {
			fgets( line, sizeof( line), p_conf_file);  // skip the whole line (comment)
			continue;
		} 
162

163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
		if (strstr(str, "MAX_NUM_SAMPLES")!=NULL) {
			fscanf(p_conf_file, "%ld", &p_data->m_max_num_samples);
			continue;
		}
		// Data Block dim
		if (strstr(str, "DATA_BLOCK_DIM")!=NULL) {
			long tmp= DEF_BLDIM;
			fscanf(p_conf_file, "%ld", &tmp);
			if(( tmp> 65536)|| (tmp<= 0))
			{
				TRACE1( "\nuser settings: block dim samples out of range [1..65536]: defaulting to %i", DEF_BLDIM);
				tmp= DEF_BLDIM;
			}
			p_data->m_bldim= ( N957_UINT16)tmp;
			continue;
		}
		// enable debugging
		if (strstr(str, "DEBUG")!=NULL) {
			int tmp= 0;
			fscanf(p_conf_file, "%d", &tmp);
			p_data->m_debug= tmp!= 0;
			continue;
		}
		// Acquisition mode
		if (strstr(str, "ACQ_MODE")!=NULL) {
			int tmp= DEF_MODE;
			fscanf(p_conf_file, "%d", &tmp);
			switch( tmp)
			{
			case N957ControlModeExtGate:
			case N957ControlModeAuto:
				break;
			default:
				TRACE2( "\nuser settings: Unhandled Mode %lu: defaulting to %i", (long)tmp, DEF_MODE);
				tmp= DEF_MODE;
				break;
			}
			p_data->m_mode= ( N957ControlModes) tmp;
			continue;
		}
 
		// Generic Write
		if (strstr(str, "WRITE_REGISTER")!=NULL) {
			int addr, data;
			fscanf(p_conf_file, "%x", &addr);
			fscanf(p_conf_file, "%x", &data);
			if ( N957_WriteReg( p_data->m_N957_handle, ( N957_UINT16)addr, ( N957_UINT16)data)!= N957Success) {
				TRACE1("Write failure at address %08lX\n", (long)addr);
				goto exit_point;
			}                
		}
		//Set LLD Threshold
		if (strstr(str, "LLD_THR_VAL")!=NULL) {
			fscanf(p_conf_file, "%d", &p_data->m_lld);
			if( ( N957_error_code= N957_SetLLD( p_data->m_N957_handle, p_data->m_lld))!=N957Success){
				TRACE( N957_DecodeError( N957_error_code));
				ret_val= FALSE;
				goto exit_point;
			}
			continue;
		}

	}
exit_point:
	fclose( p_conf_file);
	return ret_val;
}

char* trim( char charset[], int n_chars, const char* string){
	int i;
	char* str= (char* )string;
	BOOL found_char;
	// Trim leading chars
	found_char= TRUE;
	while( found_char&& strlen( str) ) {
		found_char= FALSE;
		for( i= 0; i< n_chars; i++) {
			if( str[0]== charset[ i]){
				found_char= TRUE;
				++str;
				break;
			}
		}
	}
	// Trim trailing chars
	found_char= TRUE;
	while( found_char&& strlen( str) ) {
		found_char= FALSE;
		for( i= 0; i< n_chars; i++) {
			if( str[ strlen( str)- 1]== charset[ i]){
				found_char= TRUE;
				str[ strlen( str)- 1]= '\0';
				break;
			}
		}
	}
	return str;
}