OSDN Git Service

1sec and high smpling file record add.
[scilog/scilog.git] / ad_file.c
1 /*
2         1secファイル
3         高速サンプルファイル
4
5 #define DEBUG_FILE_MIN
6         定義するとファイルが1日単位ではなく指定された分単位になる
7         ただし、ファイルのサイズは1日と同じサイズのまま
8 #deifne DEBUG_FILE_MIN_PERIOD
9         ファイルの単位
10         単位 分
11
12 */
13 #include <stdio.h>
14 #include <string.h>
15 #include <sys/types.h>
16 #include <sys/stat.h>
17 #include <stdlib.h>
18 #include <syslog.h>
19 #include <errno.h>
20 #include <unistd.h>
21
22 #include "ad_ring.h"
23 #include "ad_file.h"
24 #include "debug_print.h"
25 #include "conf.h"
26
27 // mkdir_p()で使用する
28 #define MKDIR_MODE      "755"
29
30 /**** util
31 */
32 /*
33         指定さえれたディレクトリを作成
34         親がないときは親まで作る
35         char *mode
36                 755とか
37         return
38                 0=OK
39                 -1=ERROR
40 */
41 static int mkdir_p(const char *path, char *mode)
42 {
43         struct stat     st;
44         char    cmd[128];
45
46         // コピー先ディレクトリ存在しなければ作成する
47         if (stat(path, &st) == -1) {
48                 PDEBUG("mkdir_p(): make dir %s\n", path);
49
50                 sprintf(cmd, "mkdir -p -m %s %s > /dev/null 2> /dev/null &", mode, path);
51                 switch(system(cmd)) {
52                         case 127:
53                                 // shell exec error
54                                 PDEBUG("mkdir_p(): shell exec error\n");
55                                 syslog(LOG_ERR, "mkdir_p(): system() shell exec error");
56                                 return -1;
57                         case -1:
58                                 // error
59                                 PDEBUG("mkdir_p(): error\n");
60                                 syslog(LOG_ERR, "mkdir_p(): system() error");
61                                 return -1;
62                         default:
63                                 // 作成されるまで待つ
64                                 sleep(3);
65                                 return 0;
66                 }
67         }
68         return 0;
69 }
70
71
72 /**** 1秒ファイル関係 ************************************************************
73 */
74 /*
75         1秒データファイル オープン
76
77         Return:
78                 1 = Error
79                 0 = OK
80  */
81 int sec_file_open(FILE **fp, AdData *D)
82 {
83         char    szDir[64];
84         struct stat     st;
85         char    fname[64];
86         char    path[128];
87         
88         // サブディレクトリ作成
89         sprintf(szDir, "%s/%s/%04d/%02d", DIR_DATA, sid_getp(), D->t.tm_year+1900, D->t.tm_mon+1);
90         // ディレクトリ存在しなければ作成する
91         if (stat(szDir, &st) == -1) mkdir_p(szDir, MKDIR_MODE);
92
93         // ファイル名作成
94 #ifdef  DEBUG_FILE_MIN
95         sprintf(fname, "%04d%02d%02d_%02d%02d.sec",
96                 D->t.tm_year+1900, D->t.tm_mon+1, D->t.tm_mday, D->t.tm_hour, D->t.tm_min / DEBUG_FILE_MIN_PERIOD * DEBUG_FILE_MIN_PERIOD);
97 #else
98         sprintf(fname, "%04d%02d%02d.sec", D->t.tm_year+1900, D->t.tm_mon+1, D->t.tm_mday);
99 #endif
100         // フルパス作成
101         strcpy(path, szDir);
102         strcat(path, "/");
103         strcat(path, fname);
104         PDEBUGF("path=%s\n", path);
105
106         *fp = fopen(path, "a");
107         if (*fp == NULL) {
108                 syslog(LOG_ERR, "%s(): fopen() ERROR. %s", __FUNCTION__, strerror(errno));
109                 return 1;
110         }
111         return 0;
112 }
113 /*
114         Return:
115                 1 = Error
116                 0 = OK
117  */
118 int sec_file_out(FILE *fp, char *out, int len)
119 {
120         int     ret;
121
122         if (fp == NULL) return 1;
123         if ((ret = fwrite(out, 1, len, fp)) < len) {
124                 syslog(LOG_ERR, "%s(): fwrite() return %d < %d. %s", __FUNCTION__, ret, len, strerror(errno));
125                 return 1;
126         }
127         return 0;
128 }
129 /*
130         1秒データファイル クローズ
131
132         Return:
133                 1 = Error
134                 0 = OK
135  */
136 int sec_file_close(FILE *fp)
137 {
138         if (fp == NULL) return 1;
139         fclose(fp);
140         return 0;
141 }
142
143 /*
144         1secファイル記録用データ作る
145 */
146 int sec_make_rec_data(AdData *ad, char *buf)
147 {
148         char    buf2[32];
149         int     ch;
150         
151         sprintf(buf, "%04d/%02d/%02d %02d:%02d:%02d,%3lu,%02X",
152                 ad->gps.year, ad->gps.month, ad->gps.day, ad->gps.hour, ad->gps.min, ad->gps.sec,
153                 ad->gps.tacc, ad->gps.valid);
154         for(ch = 0; ch < AD_CHNUM; ch++) {
155                 sprintf(buf2, ",%+7ld", ad->data1sec[ch]);
156                 strcat(buf, buf2);
157         }
158         strcat(buf, "\n");
159         return 0;
160 }
161 /**** 高速サンプルファイル関係 ************************************************************
162 */
163 /*
164         Return:
165                 1 = Error
166                 0 = OK
167  */
168 int high_file_open(FILE **fp, AdData *D)
169 {
170         char    szDir[64];
171         struct stat     st;
172         char    fname[64];
173         char    path[128];
174         
175         // サブディレクトリ作成
176         sprintf(szDir, "%s/%s/%04d/%02d", DIR_DATA, sid_getp(), D->t.tm_year+1900, D->t.tm_mon+1);
177         // ディレクトリ存在しなければ作成する
178         if (stat(szDir, &st) == -1) mkdir_p(szDir, MKDIR_MODE);
179
180         // ファイル名作成
181 #ifdef  DEBUG_FILE_MIN
182         sprintf(fname, "%04d%02d%02d_%02d%02d.sec",
183                 D->t.tm_year+1900, D->t.tm_mon+1, D->t.tm_mday, D->t.tm_hour, D->t.tm_min / DEBUG_FILE_MIN_PERIOD * DEBUG_FILE_MIN_PERIOD);
184 #else
185         sprintf(fname, "%04d%02d%02d%02d.high", D->t.tm_year+1900, D->t.tm_mon+1, D->t.tm_mday, D->t.tm_hour);
186 #endif
187         // フルパス作成
188         strcpy(path, szDir);
189         strcat(path, "/");
190         strcat(path, fname);
191         PDEBUGF("path=%s\n", path);
192
193         *fp = fopen(path, "a");
194         if (*fp == NULL) {
195                 syslog(LOG_ERR, "%s(): fopen() ERROR. %s", __FUNCTION__, strerror(errno));
196                 return 1;
197         }
198         return 0;
199 }
200 /*
201         Return:
202                 1 = Error
203                 0 = OK
204  */
205 int high_file_out(FILE *fp, char *out, int len)
206 {
207         int     ret;
208
209         if (fp == NULL) return 1;
210         if ((ret = fwrite(out, 1, len, fp)) < len) {
211                 syslog(LOG_ERR, "%s(): fwrite() return %d < %d. %s", __FUNCTION__, ret, len, strerror(errno));
212                 return 1;
213         }
214         return 0;
215 }
216 /*
217         Return:
218                 1 = Error
219                 0 = OK
220  */
221 int high_file_close(FILE *fp)
222 {
223         if (fp == NULL) return 1;
224         fclose(fp);
225         return 0;
226 }
227
228 /*
229         記録用データ作る
230 */
231 int high_make_rec_data(AdData *ad, char *ptr)
232 {
233         unsigned char   *src;
234         int     ch, i, j;
235         
236         *ptr++ = ad->gps.year & 0xFF;
237         *ptr++ = (ad->gps.year >> 8) & 0xFF;
238         *ptr++ = ad->gps.month;
239         *ptr++ = ad->gps.day;
240         *ptr++ = ad->gps.hour;
241         *ptr++ = ad->gps.min;
242         *ptr++ = ad->gps.sec;
243         *ptr++ = ad->gps.nano;
244         *ptr++ = ad->gps.valid;
245         // 
246         src = ad->data;
247         for(ch = 0; ch < AD_CHNUM; ch++) {
248                 for(i = 0; i < AD_SAMPLE; i++) {
249                         for(j = 0; j < AD_BYTES; j++) {
250                                 *ptr++ = *src++;
251                         }
252                         src++;
253                 }
254         }
255         return 0;
256 }
257
258
259
260