Blame view

RIOT/boards/msba2-common/tools/src/settings.c 5.09 KB
a752c7ab   elopes   add first test an...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
  /*
   * LPC 2000 Loader, http://www.pjrc.com/arm/lpc2k_pgm
   * Copyright (c) 2004, PJRC.COM, LLC, <paul@pjrc.com>
   *
   * This program is free software; you can redistribute it and/or modify it
   * under the terms of the GNU General Public License as published by the
   * Free Software Foundation; version 2 of the License.
   *
   * This program is distributed in the hope that it will be useful, but
   * WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
   * Public License for more details.
   *
   * You should have received a copy of the GNU General Public License along
   * with this program; if not, write to the Free Software Foundation, Inc.,
   * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
   */
  
  /* If this code fails to build, please provide at least the following
   * information when requesting (free) technical support.
   *
   * 1: Complete copy of all messages during the build.
   * 2: Output of "gtk-config --version"
   * 3: Output of "gtk-config --libs"
   * 4: Output of "gtk-config --cflags"
   * 5: Output of "uname -a"
   * 6: Version of GTK installed... eg, type: ls -l /lib/libgtk*
   * 7: Other info... which linux distribution, version, other software
   */
  
  
  #include <stdio.h>
  #include <stdlib.h>
  #include <string.h>
  #include <ctype.h>
  
  #include "settings.h"
  
  #define DEFAULT_FILE ""
  #define DEFAULT_PORT "/dev/ttyS0"
  #define DEFAULT_BAUD "115200"
  #define DEFAULT_CRYSTAL "16"
  
  char *baud_list[] = {"115200", "57600", "38400",
                       "19200", "9600", "4800", "2400", "1200", "300", NULL
                      };
  
  static char file[128] = {DEFAULT_FILE};
  static char port[64] = {DEFAULT_PORT};
  static char baud[64] = {DEFAULT_BAUD};
  static char crystal[64] = {DEFAULT_CRYSTAL};
  
  static char settings_file[256] = {'\0'};
  
  
  void init_settings(void)
  {
      const char *home_dir;
      char buf[1024];
  
      home_dir = getenv("HOME");
  
      if (home_dir && *home_dir) {
          snprintf(settings_file, sizeof(settings_file),
                   "%s/.lpc2k_pgm", home_dir);
          FILE *fp = fopen(settings_file, "r");
  
          if (fp == NULL) {
              return;
          }
  
          while (!feof(fp)) {
              char *p, *q;
              buf[0] = '\0';
              fgets(buf, sizeof(buf), fp);
  
              if (strncmp(buf, "file:", 5) == 0) {
                  for (p = buf + 5; isspace(*p); p++);
                  q = rindex(p, '\n');
                  if (q) {
                      *q = '\0';
                  }
                  q = rindex(p, '\r');
                  if (q) {
                      *q = '\0';
                  }
                  snprintf(file, sizeof(file), "%s", p);
              }
  
              if (strncmp(buf, "port:", 5) == 0) {
                  for (p = buf + 5; isspace(*p); p++);
                  q = rindex(p, '\n');
                  if (q) {
                      *q = '\0';
                  }
                  q = rindex(p, '\r');
                  if (q) {
                      *q = '\0';
                  }
                  snprintf(port, sizeof(port), "%s", p);
              }
  
              if (strncmp(buf, "baud:", 5) == 0) {
                  for (p = buf + 5; isspace(*p); p++);
                  q = rindex(p, '\n');
                  if (q) {
                      *q = '\0';
                  }
                  q = rindex(p, '\r');
                  if (q) {
                      *q = '\0';
                  }
                  snprintf(baud, sizeof(baud), "%s", p);
              }
  
              if (strncmp(buf, "xtal:", 5) == 0) {
                  for (p = buf + 5; isspace(*p); p++) ;
                  q = rindex(p, '\n');
                  if (q) {
                      *q = '\0';
                  }
                  q = rindex(p, '\r');
                  if (q) {
                      *q = '\0';
                  }
                  snprintf(crystal, sizeof(crystal), "%s", p);
              }
          }
  
          fclose(fp);
      }
  }
  
  void write_settings_file(void)
  {
      FILE *fp;
  
      if (settings_file[0] == '\0') {
          return;
      }
  
      fp = fopen(settings_file, "w");
  
      if (fp == NULL) {
          return;
      }
  
      fprintf(fp, "file: %s\n", file);
      fprintf(fp, "port: %s\n", port);
      fprintf(fp, "baud: %s\n", baud);
      fprintf(fp, "xtal: %s\n", crystal);
      fflush(fp);
      fclose(fp);
  }
  
  const char *file_setting(void)
  {
      return file;
  }
  
  const char *port_setting(void)
  {
      return port;
  }
  
  const char *baud_setting(void)
  {
      return baud;
  }
  
  const char *crystal_setting(void)
  {
      return crystal;
  }
  
  void new_file_setting(const char *new_file)
  {
      if (strcmp(file, new_file)) {
          snprintf(file, sizeof(file), "%s", new_file);
          write_settings_file();
      }
  }
  
  void new_port_setting(const char *new_port)
  {
      if (strcmp(port, new_port)) {
          snprintf(port, sizeof(port), "%s", new_port);
          write_settings_file();
      }
  }
  
  void new_baud_setting(const char *new_baud)
  {
      if (strcmp(baud, new_baud)) {
          snprintf(baud, sizeof(baud), "%s", new_baud);
          write_settings_file();
      }
  }
  
  void new_crystal_setting(const char *new_xtal)
  {
      if (strcmp(crystal, new_xtal)) {
          snprintf(crystal, sizeof(crystal), "%s", new_xtal);
          write_settings_file();
      }
  }