OSDN Git Service

GPS valid=07にならなくても記録を開始してしまうバグ修正
[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
10 // ミューテックス
11 static pthread_mutex_t  mutex_conf = PTHREAD_MUTEX_INITIALIZER;
12
13 //
14 /**** 記録周波数 ***************************************************
15 */
16
17 static int      freq;
18
19 void conf_freq_set(int f)
20 {
21 pthread_mutex_lock(&mutex_conf);
22         freq = f;
23 pthread_mutex_unlock(&mutex_conf);
24 }
25 int conf_freq_get(void)
26 {
27         int     f;
28 pthread_mutex_lock(&mutex_conf);
29         f = freq;
30 pthread_mutex_unlock(&mutex_conf);
31         return f;
32 }
33 //
34 /**** Gain ***************************************************
35 */
36 /*
37         0=1/8, 1=1/4, 2=1/2, 3=1, 4=2, 5=4
38         6=8, 7=16, 8=32, 9=64, 10=128
39 */
40 static int      gain;
41
42 void conf_gain_set(int f)
43 {
44 pthread_mutex_lock(&mutex_conf);
45         gain = f;
46 pthread_mutex_unlock(&mutex_conf);
47 }
48 int conf_gain_get(void)
49 {
50         int     f;
51 pthread_mutex_lock(&mutex_conf);
52         f = gain;
53 pthread_mutex_unlock(&mutex_conf);
54         return f;
55 }
56
57 //
58 /**** 設定ファイル *******************************************
59 */
60 /*
61         文字列の前から空白を取り除く
62 */
63 static void trims_space(char *name)
64 {
65         char    c;
66         char    *name0;
67         int     len;
68         
69         name0 = name;
70         len = strlen(name);
71         
72         // 最初の非空白文字までポインタ移動
73         while (*name) {
74                 c = *name;
75                 if (c != ' ') break;
76                 name++;
77                 len--;
78         }
79         if (len < 0) return;
80         // 移動
81         if (name0 != name) memmove(name0, name, len);
82         // 末尾に0入れる
83         name0[len] = 0;
84 }
85 /*
86         文字列の後ろから空白を取り除く
87 */
88 static void trime_space(char *name)
89 {
90         char    c;
91         int     len, i;
92
93         // 文字列末尾
94         len = strlen(name);
95         name += (len - 1);
96         // 最初の非空白文字までポインタ移動
97         for(i = len - 1; i >= 0; i--) {
98                 c = *name;
99                 if (c != ' ') break;
100                 name--;
101         }
102         // 末尾に0入れる
103         *(name + 1) = 0;
104 }
105 /*
106         文字列の前後から空白を取り除く
107 */
108 static void trim_space(char *name)
109 {
110         if (name == NULL) return;
111         trims_space(name);
112         trime_space(name);
113 }
114 /*
115         文字列の末尾からCR LFを除く
116 */
117 static void trim_crlf(char *name)
118 {
119         int     i;
120         int     len;
121         char    *src, *dst;
122         
123         if (name == NULL) return;
124         len = strlen(name);
125         src = name;
126         dst = name;
127         for(i = 0; i < len; i++) {
128                 if (*src != 0x0d && *src != 0x0a) {
129                         *dst = *src;
130                         dst++;
131                 }
132                 src++;
133         }
134         *dst = 0;
135 }
136
137 /*
138         読み込み
139
140 return
141         -1=ERR
142         0=OK
143 */
144 int conf_read(void)
145 {
146         FILE    *fp;
147         char    buf[256];
148 //      char    buf2[256];
149         int     f;
150         
151         fp = fopen(CONF_FILE, "rt");
152         if (fp == NULL) {
153                 syslog(LOG_ERR, "conf_read(): conf file not found. %s", CONF_FILE);
154                 return -1;
155         }
156
157         syslog(LOG_INFO, "conf_read():");
158         while(fgets(buf, sizeof(buf), fp)) {
159                 // CR LF除く
160                 trim_crlf(buf);
161                 trim_space(buf);
162                 PDEBUG(buf);
163                 PDEBUG("\n");
164                 // 記録周波数
165                 if (sscanf(buf, "freq = %d", &f) == 1) {
166                         conf_freq_set(f);
167                         syslog(LOG_INFO, "freq=%d", conf_freq_get());
168                 } 
169                 // Gain
170                 if (sscanf(buf, "gain = %d", &f) == 1) {
171                         conf_gain_set(f);
172                         syslog(LOG_INFO, "gain=%d", conf_gain_get());
173                 } 
174         }
175         fclose(fp);
176         return 0;
177 }
178
179