altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / lib / cc-log.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <string.h>
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <unistd.h>
23 #include <gconf/gconf-client.h>
24 #include "cc.h"
25
26 static char *cc_file_dir;
27
28 #define ALTOS_DIR_PATH  "/apps/aoview/log_dir"
29 #define DEFAULT_DIR     "AltOS"
30
31 static void
32 cc_file_save_conf(void)
33 {
34         GConfClient     *gconf_client;
35
36         g_type_init();
37         gconf_client = gconf_client_get_default();
38         if (gconf_client)
39         {
40                 gconf_client_set_string(gconf_client,
41                                         ALTOS_DIR_PATH,
42                                         cc_file_dir,
43                                         NULL);
44                 g_object_unref(G_OBJECT(gconf_client));
45         }
46 }
47
48 static void
49 cc_file_load_conf(void)
50 {
51         char *file_dir;
52         GConfClient     *gconf_client;
53
54         g_type_init();
55         gconf_client = gconf_client_get_default();
56         if (gconf_client)
57         {
58                 file_dir = gconf_client_get_string(gconf_client,
59                                                    ALTOS_DIR_PATH,
60                                                    NULL);
61                 g_object_unref(G_OBJECT(gconf_client));
62                 if (file_dir)
63                         cc_file_dir = strdup(file_dir);
64         }
65 }
66
67 void
68 cc_set_log_dir(char *dir)
69 {
70         cc_file_dir = strdup(dir);
71         cc_file_save_conf();
72 }
73
74 char *
75 cc_get_log_dir(void)
76 {
77         cc_file_load_conf();
78         if (!cc_file_dir) {
79                 cc_file_dir = cc_fullname(getenv("HOME"), DEFAULT_DIR);
80                 cc_file_save_conf();
81         }
82         return cc_file_dir;
83 }
84
85 char *
86 cc_make_filename(int serial, int flight, char *ext)
87 {
88         char            base[50];
89         char            seq[20];
90         struct tm       tm;
91         time_t          now;
92         char            *full;
93         int             r;
94         int             sequence;
95
96         now = time(NULL);
97         (void) localtime_r(&now, &tm);
98         cc_mkdir(cc_get_log_dir());
99         sequence = 0;
100         for (;;) {
101                 if (sequence)
102                         snprintf(seq, sizeof(seq), "-seq-%03d", sequence);
103                 else
104                         seq[0] = '\0';
105
106                 snprintf(base, sizeof (base), "%04d-%02d-%02d-serial-%03d-flight-%03d%s.%s",
107                          tm.tm_year + 1900,
108                          tm.tm_mon + 1,
109                          tm.tm_mday,
110                          serial,
111                          flight,
112                          seq,
113                          ext);
114                 full = cc_fullname(cc_get_log_dir(), base);
115                 r = access(full, F_OK);
116                 if (r < 0)
117                         return full;
118                 free(full);
119                 sequence++;
120         }
121
122 }