OSDN Git Service

Initial commit
[scilog/decode_sci.git] / decode_sci.c
1 /*
2         decode_sci.c
3         sciLogger high sample data decoder
4
5         入力ファイル Little Endian
6         このプログラムが動作するマシンもLittle Endianである必要あり。
7
8   Copyright Naoya Takamura@NT systemd design, 2011
9  
10   This program is free software; you can redistribute it and/or modify
11   it under the terms of the GNU General Public License as published by
12   the Free Software Foundation; either version 2 of the License, or
13   (at your option) any later version.
14  
15   This program is distributed in the hope that it will be useful,
16   but WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18   GNU General Public License for more details.
19  
20   You should have received a copy of the GNU General Public License
21   along with this program; if not, write to the Free Software
22   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23 */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdint.h>
28
29 #define VER     "1.0"
30 #define AD_CH   6
31 #define AD_SAMPLE       50
32 #define AD_BYTES        3
33
34 struct _recdata_head_t {
35     uint16_t    year;   // UTC
36     uint8_t             month;
37     uint8_t             day;
38     uint8_t             hour;
39     uint8_t             min;
40     uint8_t             sec;
41     uint32_t    tacc;   // ns
42     uint8_t             valid;
43     uint16_t    freq;   // 記録周波数 Hz
44 } __attribute__((__packed__));
45
46
47 typedef struct _recdata_head_t  recdata_head;
48
49 #define REC_DATALEN_HEAD        sizeof(recdata_head)
50
51 /*
52         Little Endian
53         3byte -> signed long(4byte)
54 */
55 static long b3_to_long32(unsigned char *ptr)
56 {
57         char    buf[4];
58         
59         buf[0] = *ptr++;
60         buf[1] = *ptr++;
61         buf[2] = *ptr;
62         if (*ptr & 0x80) {
63                 buf[3] = 0xFF;
64         } else {
65                 buf[3] = 0;
66         }
67         return *((long*)buf);
68 }
69
70 /*
71         data decode
72 */
73 static void read_high(FILE *fp)
74 {
75         recdata_head    d;
76         int     ch, j;
77         uint8_t data[AD_CH * AD_SAMPLE * AD_BYTES];
78
79         while(1) {
80                 // Header
81                 if (fread(&d, 1, REC_DATALEN_HEAD, fp) < 1) break;
82 //fprintf(stderr, "%02d:%02d:%02d.%03d %d %d", d.hour, d.min, d.sec, (1000*j/AD_SAMPLE), d.tacc, d.valid);
83                 if (d.freq > AD_SAMPLE || d.freq < 1) {
84                         fprintf(stderr, "read_high() Error freq=%d\n", d.freq);
85                         break;
86                 }
87                 int data_size = AD_CH * d.freq * AD_BYTES;
88                 // AD data
89                 if (fread(data, 1, data_size, fp) < 1) break;
90                 for(j = 0; j < d.freq; j++) {
91                         fprintf(stdout, "%02d:%02d:%02d.%03d", d.hour, d.min, d.sec, (1000*j/d.freq));
92                         for(ch = 0; ch < AD_CH; ch++) {
93                                 fprintf(stdout, ",%+07ld", b3_to_long32((char*)(data + ch*d.freq*AD_BYTES + j*AD_BYTES)));
94                         }
95                         fprintf(stdout, "\n");
96                 }
97         }
98 }
99
100 int main(int argc, char *argv[])
101 {
102         FILE    *fpin;
103         FILE    *fpout;
104         char    fname_out[128];
105         char    sz[32];
106         unsigned char   buf[256];
107
108 //printf("len=%d\n", REC_DATALEN_HEAD);
109 // exit(0);
110
111         if (argc == 1) {
112                 printf("sciLogger high sample data converter Ver"VER"\n");
113                 printf("Please set filename.\n");
114                 exit(0);
115         }
116         
117         fpin = fopen(argv[1], "rb");
118
119         if (fpin == NULL) {
120                 fprintf(stderr, "Input file open error.\n");
121                 goto errout2;
122         }
123         // 出力ファイル名
124         strcpy(fname_out, argv[1]);
125         strcat(fname_out, ".txt");
126
127         fpout = freopen(fname_out, "w", stdout);
128         if (fpout == NULL) {
129                 fprintf(stderr, "Output file open ERROR. %s\n", fname_out);
130                 goto errout1;
131         }
132         read_high(fpin);
133
134         fclose(fpout);
135         fclose(fpin);
136         return 0;
137 errout1:
138         fclose(fpin);
139 errout2:
140         return -1;
141 }