OSDN Git Service

記録パス設定 記録CH設定 ファイル書込タイミング
[scilog/scilog.git] / conf.c
1 #include <pthread.h>
2 #include <semaphore.h> 
3 #include <string.h>
4 #include <syslog.h>
5 #include <stdio.h>
6
7 #include "conf.h"
8 #include "debug_print.h"
9 #include "ad_ring.h"
10
11 // ミューテックス
12 static pthread_mutex_t  mutex_conf = PTHREAD_MUTEX_INITIALIZER;
13
14 //
15 /**** 記録周波数 ***************************************************
16 */
17 // 50,25,10,5,2,1
18 static int      freq;
19
20 void conf_freq_set(int f)
21 {
22 pthread_mutex_lock(&mutex_conf);
23         freq = f;
24 pthread_mutex_unlock(&mutex_conf);
25 }
26 int conf_freq_get(void)
27 {
28         int     f;
29 pthread_mutex_lock(&mutex_conf);
30         f = freq;
31 pthread_mutex_unlock(&mutex_conf);
32         return f;
33 }
34 //
35 /**** Gain ***************************************************
36 */
37 /*
38         0=1/8, 1=1/4, 2=1/2, 3=1, 4=2, 5=4
39         6=8, 7=16, 8=32, 9=64, 10=128
40 */
41 static int      gain;
42
43 void conf_gain_set(int f)
44 {
45 pthread_mutex_lock(&mutex_conf);
46         gain = f;
47 pthread_mutex_unlock(&mutex_conf);
48 }
49 int conf_gain_get(void)
50 {
51         int     f;
52 pthread_mutex_lock(&mutex_conf);
53         f = gain;
54 pthread_mutex_unlock(&mutex_conf);
55         return f;
56 }
57
58 /**** Linux時刻セット ***************************************************
59 */
60 // 1=ON
61 static int linux_time_set;
62 void conf_linux_time_set_set(int val)
63 {
64         linux_time_set = val;
65 }
66 int conf_linux_time_set_get(void)
67 {
68         return linux_time_set;
69 }
70
71 // 記録パス
72 static char     recpath[CONF_RECPATH_MAX];
73 void conf_recpath_set(char *s)
74 {
75         strncpy(recpath, s, sizeof(recpath));
76 }
77 char* conf_recpath_getp(void)
78 {
79         return recpath;
80 }
81
82 // 記録CH
83 static int      recchnum;
84 void conf_recchn_set(int chn)
85 {
86         recchnum = chn;
87 }
88 int conf_recchn_get(void)
89 {
90         return recchnum;
91 }
92
93 //
94 /**** 設定ファイル *******************************************
95 */
96 /*
97         文字列の前から空白を取り除く
98 */
99 static void trims_space(char *name)
100 {
101         char    c;
102         char    *name0;
103         int     len;
104         
105         name0 = name;
106         len = strlen(name);
107         
108         // 最初の非空白文字までポインタ移動
109         while (*name) {
110                 c = *name;
111                 if (c != ' ') break;
112                 name++;
113                 len--;
114         }
115         if (len < 0) return;
116         // 移動
117         if (name0 != name) memmove(name0, name, len);
118         // 末尾に0入れる
119         name0[len] = 0;
120 }
121 /*
122         文字列の後ろから空白を取り除く
123 */
124 static void trime_space(char *name)
125 {
126         char    c;
127         int     len, i;
128
129         // 文字列末尾
130         len = strlen(name);
131         name += (len - 1);
132         // 最初の非空白文字までポインタ移動
133         for(i = len - 1; i >= 0; i--) {
134                 c = *name;
135                 if (c != ' ') break;
136                 name--;
137         }
138         // 末尾に0入れる
139         *(name + 1) = 0;
140 }
141 /*
142         文字列の前後から空白を取り除く
143 */
144 static void trim_space(char *name)
145 {
146         if (name == NULL) return;
147         trims_space(name);
148         trime_space(name);
149 }
150 /*
151         文字列の末尾からCR LFを除く
152 */
153 static void trim_crlf(char *name)
154 {
155         int     i;
156         int     len;
157         char    *src, *dst;
158         
159         if (name == NULL) return;
160         len = strlen(name);
161         src = name;
162         dst = name;
163         for(i = 0; i < len; i++) {
164                 if (*src != 0x0d && *src != 0x0a) {
165                         *dst = *src;
166                         dst++;
167                 }
168                 src++;
169         }
170         *dst = 0;
171 }
172
173 /*
174         読み込み
175
176 return
177         -1=ERR
178         0=OK
179 */
180 int conf_read(void)
181 {
182         FILE    *fp;
183         char    buf[256];
184 //      char    buf2[256];
185         char    buf2[CONF_RECPATH_MAX];
186         int     f;
187         int     val;
188
189         fp = fopen(CONF_FILE, "rt");
190         if (fp == NULL) {
191                 syslog(LOG_ERR, "conf_read(): conf file not found. %s", CONF_FILE);
192                 return -1;
193         }
194
195         syslog(LOG_INFO, "conf_read():");
196         while(fgets(buf, sizeof(buf), fp)) {
197                 // CR LF除く
198                 trim_crlf(buf);
199                 trim_space(buf);
200                 PDEBUG(buf);
201                 PDEBUG("\n");
202                 // 記録周波数
203                 if (sscanf(buf, "freq = %d", &f) == 1) {
204                         conf_freq_set(f);
205                         syslog(LOG_INFO, "freq=%d", conf_freq_get());
206                 } 
207                 // Gain
208                 if (sscanf(buf, "gain = %d", &f) == 1) {
209                         conf_gain_set(f);
210                         syslog(LOG_INFO, "gain=%d", conf_gain_get());
211                 } 
212                 // Linux時刻セット
213                 if (sscanf(buf, "linux_time_set = %d", &val) == 1) {
214                         conf_linux_time_set_set(val);
215                         syslog(LOG_INFO, "linux_time_set=%d", conf_linux_time_set_get());
216                 } 
217                 // 記録パス
218                 if (sscanf(buf, "rec_path = %s", buf2) == 1) {
219                         trim_space(buf2);
220                         conf_recpath_set(buf2);
221                         syslog(LOG_INFO, "rec_path=%s", conf_recpath_getp());
222                 } 
223                 // 記録CH
224                 if (sscanf(buf, "rec_chn = %d", &f) == 1) {
225                         if (f < 1 || f > AD_CHNUM) f = AD_CHNUM;
226                         conf_recchn_set(f);
227                 }
228         }
229         fclose(fp);
230         return 0;
231 }
232
233