summaryrefslogtreecommitdiff
path: root/noncore/net/opietooth/manager/btconfhandler.cpp
Unidiff
Diffstat (limited to 'noncore/net/opietooth/manager/btconfhandler.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/net/opietooth/manager/btconfhandler.cpp391
1 files changed, 391 insertions, 0 deletions
diff --git a/noncore/net/opietooth/manager/btconfhandler.cpp b/noncore/net/opietooth/manager/btconfhandler.cpp
new file mode 100644
index 0000000..0048993
--- a/dev/null
+++ b/noncore/net/opietooth/manager/btconfhandler.cpp
@@ -0,0 +1,391 @@
1/* $Id$ */
2/* Bluetooth services configuration file handler */
3/***************************************************************************
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 ***************************************************************************/
11#include "btconfhandler.h"
12#include <string.h>
13#include <stdlib.h>
14#include <getopt.h>
15#include <qstring.h>
16#include <qstringlist.h>
17#include <qfile.h>
18#include <qtextstream.h>
19#include <opie2/odebug.h>
20
21//Keywords
22static QCString k_hcidEnable("HCID_ENABLE=");
23static QCString k_hiddEnable("HIDD_ENABLE=");
24static QCString k_sdpdEnable("SDPD_ENABLE=");
25static QCString k_rfcommEnable("RFCOMM_ENABLE=");
26static QCString k_dundEnable("DUND_ENABLE=");
27static QCString k_pandEnable("PAND_ENABLE=");
28static QCString k_pandOpts("PAND_OPTIONS=");
29static QCString k_dundOpts("DUND_OPTIONS=");
30
31const struct option pandCLOpts[] = //Pand command line options
32{
33 { "listen", 0, NULL, 's' },
34 { "encrypt", 0, NULL, 'E' },
35 { "secure", 0, NULL, 'S' },
36 { "master", 0, NULL, 'M' },
37 { "nosdp", 0, NULL, 'D' },
38 { "role", 1, NULL, 'r' },
39 { "persist", 0, NULL, 'p' },
40 { "cache", 0, NULL, 'C' },
41 { NULL, 0, NULL, 0 } //Marker of the end
42};
43
44const struct option dundCLOpts[] = //Pand command line options
45{
46 { "listen", 0, NULL, 's' },
47 { "nosdp", 0, NULL, 'D' },
48 { "persist", 0, NULL, 'p' },
49 { "cache", 0, NULL, 'C' },
50 { "msdun", 0, NULL, 'X' },
51 { "channel", 1, NULL, 'P' },
52 { "pppd", 1, NULL, 'd' },
53 { NULL, 0, NULL, 0 } //Marker of the end
54};
55
56using namespace OpieTooth;
57BTConfHandler::BTConfHandler(const QString& conf): confName(conf)
58{
59 hcidEnable = false;
60 hiddEnable = false;
61 sdpdEnable = false;
62 rfcommEnable = false;
63
64 //dund parameters
65 dundEnable = false;
66 dundDisableSDP = false;
67 dundPersist = false;
68 dundPersistPeriod = "";
69 dundCache = false;
70 dundCachePeriod = "";
71 dundMsdun = false;
72 dundMsdunTimeout = "";
73 dundChannel = "1";
74 dundPPPd = false;
75 dundPPPdPath = "";
76 dundPPPdOptions = "";
77
78 //pandparameters
79 pandEnable = false;
80 pandEncrypt = false;
81 pandSecure = false;
82 pandMaster = false;
83 pandDisableSDP = false;
84 pandRole = "";
85 pandPersist = false;
86 pandPersistPeriod = "";
87 pandCache = false;
88 pandCachePeriod = "";
89
90 readConfig(confName);
91}
92
93BTConfHandler::~BTConfHandler()
94{
95}
96
97/**
98 * getBoolArg
99 * Gets boolean argument from a string str with a keyword key
100 */
101bool BTConfHandler::getBoolArg(QString& str, QCString& keyword)
102{
103 QString arg = str.mid(keyword.length(), str.length() - keyword.length());
104 return (bool)(arg == "true");
105}
106
107/**
108 * getStringArg
109 * Gets string argument from a string str with a keyword key
110 */
111QString BTConfHandler::getStringArg(QString& str, QCString& keyword)
112{
113 QString arg = str.mid(keyword.length(), str.length() - keyword.length());
114 if (arg.left(1) == "\"")
115 arg.remove(0, 1);
116 if (arg.right(1) == "\"")
117 arg.remove(arg.length() - 1, 1);
118 return arg;
119}
120
121/**
122 * parsePanParameters
123 * Function gets PAND parameters from the command line conf
124 */
125void BTConfHandler::parsePanParameters(const QString& conf)
126{
127 int i; //just an index variable
128 int argc;
129 QStringList panList = QStringList::split(" ", conf);
130 argc = panList.count();
131 char* argv[argc + 1];
132 int shOpt;
133 i = 1;
134 optind = 0;
135 argv[0] = strdup("pand");
136 for (QStringList::Iterator arg = panList.begin();
137 arg != panList.end() && i < argc + 1; arg++, i++) {
138 argv[i] = strdup((*arg));
139 }
140 do {
141 shOpt = getopt_long(argc + 1, argv, "sESMDr:p:C:", pandCLOpts, NULL);
142 switch(shOpt) {
143 case 'E':
144 pandEncrypt = true;
145 break;
146 case 'S':
147 pandSecure = true;
148 break;
149 case 'M':
150 pandMaster = true;
151 break;
152 case 'D':
153 pandDisableSDP = true;
154 break;
155 case 'r':
156 if (optarg)
157 pandRole = optarg;
158 else
159 pandRole = "NG";
160 break;
161 case 'p':
162 pandPersist = true;
163 if (optarg)
164 pandPersistPeriod = optarg;
165 break;
166 case 'C':
167 pandCache = true;
168 if (optarg)
169 pandCachePeriod = optarg;
170 break;
171 case 's':
172 case '?':
173 default:
174 break;
175 }
176 } while (shOpt != -1);
177 for (i = 0; i < argc + 1; i++) {
178 free(argv[i]);
179 }
180}
181
182/**
183 * parseDunParameters
184 * Function gets PAND parameters from the command line conf
185 */
186void BTConfHandler::parseDunParameters(const QString& conf)
187{
188 int i; //just an index variable
189 int argc;
190 QStringList dunList = QStringList::split(" ", conf);
191 argc = dunList.count();
192 char* argv[argc + 1];
193 int shOpt;
194 i = 1;
195 optind = 0;
196 argv[0] = strdup("dund");
197 for (QStringList::Iterator arg = dunList.begin();
198 arg != dunList.end() && i < argc + 1; arg++, i++) {
199 argv[i] = strdup((*arg));
200 }
201 do {
202 shOpt = getopt_long(argc + 1, argv, "sDp:C:X:P:d:", dundCLOpts, NULL);
203 switch(shOpt) {
204 case 'D':
205 dundDisableSDP = true;
206 break;
207 case 'p':
208 dundPersist = true;
209 if (optarg)
210 dundPersistPeriod = optarg;
211 break;
212 case 'C':
213 dundCache = true;
214 if (optarg)
215 dundCachePeriod = optarg;
216 break;
217 case 'X':
218 dundMsdun = true;
219 if (optarg)
220 dundMsdunTimeout = optarg;
221 break;
222 case 'P':
223 if (optarg)
224 dundChannel = optarg;
225 else
226 dundChannel = "1";
227 break;
228 case 'd':
229 dundPPPd = true;
230 if (optarg)
231 dundPPPdPath = optarg;
232 break;
233 case 's':
234 case '?':
235 default:
236 break;
237 }
238 } while (shOpt != -1);
239 if (optind < argc + 1) {
240 for (i = optind; i < argc + 1; i++) {
241 if (!dundPPPdOptions.isEmpty())
242 dundPPPdOptions += " ";
243 dundPPPdOptions += argv[i];
244 }
245 }
246 for (i = 0; i < argc + 1; i++) {
247 free(argv[i]);
248 }
249}
250
251/**
252 * readConfig
253 * Reads configuration file with conf as a file name
254 * return true on success and false on failure
255 */
256bool BTConfHandler::readConfig(const QString& conf)
257{
258 QFile btConf(conf); //File we read
259 QString tmp; //temporary string
260 if (btConf.open(IO_ReadOnly)) {
261 QTextStream inStream(&btConf);
262 list = QStringList::split("\n", inStream.read(), true);
263 for (QStringList::Iterator line = list.begin();
264 line != list.end(); line++) {
265 QString tmpLine = (*line).simplifyWhiteSpace();
266 if (tmpLine.startsWith("#"))
267 continue;
268 else if (tmpLine.startsWith(k_hcidEnable))
269 hcidEnable = getBoolArg(tmpLine, k_hcidEnable);
270 else if (tmpLine.startsWith(k_hiddEnable))
271 hiddEnable = getBoolArg(tmpLine, k_hiddEnable);
272 else if (tmpLine.startsWith(k_sdpdEnable))
273 sdpdEnable = getBoolArg(tmpLine, k_sdpdEnable);
274 else if (tmpLine.startsWith(k_rfcommEnable))
275 rfcommEnable = getBoolArg(tmpLine, k_rfcommEnable);
276 else if (tmpLine.startsWith(k_dundEnable))
277 dundEnable = getBoolArg(tmpLine, k_dundEnable);
278 else if (tmpLine.startsWith(k_pandEnable))
279 pandEnable = getBoolArg(tmpLine, k_pandEnable);
280 else if (tmpLine.startsWith(k_pandOpts)) {
281 tmp = getStringArg(tmpLine, k_pandOpts);
282 parsePanParameters(tmp);
283 }
284 else if (tmpLine.startsWith(k_dundOpts)) {
285 tmp = getStringArg(tmpLine, k_dundOpts);
286 parseDunParameters(tmp);
287 }
288 }
289 return true;
290 }
291 return false;
292}
293
294/**
295 * writeConfig
296 * Writes configuration file with conf as a file name
297 * return true on success and false on failure
298 */
299bool BTConfHandler::saveConfig()
300{
301 QFile btConf(confName); //File we read
302 if (!btConf.open(IO_WriteOnly))
303 return false;
304 QTextStream stream(&btConf);
305 for (QStringList::Iterator line = list.begin();
306 line != list.end(); line++) {
307 QString tmpLine = (*line).simplifyWhiteSpace();
308 if (tmpLine.startsWith(k_hcidEnable))
309 (*line) = k_hcidEnable + ((hcidEnable)? "true": "false");
310 else if (tmpLine.startsWith(k_hiddEnable))
311 (*line) = k_hiddEnable + ((hiddEnable)? "true": "false");
312 else if (tmpLine.startsWith(k_sdpdEnable))
313 (*line) = k_sdpdEnable + ((sdpdEnable)? "true": "false");
314 else if (tmpLine.startsWith(k_rfcommEnable))
315 (*line) = k_rfcommEnable + ((rfcommEnable)? "true": "false");
316 else if (tmpLine.startsWith(k_dundEnable))
317 (*line) = k_dundEnable + ((dundEnable)? "true": "false");
318 else if (tmpLine.startsWith(k_pandEnable))
319 (*line) = k_pandEnable + ((pandEnable)? "true": "false");
320 else if (tmpLine.startsWith(k_pandOpts)) {
321 (*line) = k_pandOpts + "\"--listen";
322 (*line) += " --role " + pandRole;
323 if (pandEncrypt)
324 (*line) += " --encrypt";
325 if (pandSecure)
326 (*line) += " --secure";
327 if (pandMaster)
328 (*line) += " --master";
329 if (pandDisableSDP)
330 (*line) += " --nosdp";
331 if (pandPersist) {
332 if (pandPersistPeriod.isEmpty())
333 (*line) += " --persist";
334 else {
335 (*line) += " -p ";
336 (*line) += pandPersistPeriod;
337 }
338 }
339 if (pandCache) {
340 if (pandCachePeriod.isEmpty())
341 (*line) += " --cache";
342 else {
343 (*line) += " -C ";
344 (*line) += pandCachePeriod;
345 }
346 }
347 (*line) += "\"";
348 }
349 else if (tmpLine.startsWith(k_dundOpts)) {
350 (*line) = k_dundOpts + "\"--listen";
351 (*line) += " --channel " + dundChannel;
352 if (dundDisableSDP)
353 (*line) += " --nosdp";
354 if (dundPersist) {
355 if (dundPersistPeriod.isEmpty())
356 (*line) += " --persist";
357 else {
358 (*line) += " -p ";
359 (*line) += dundPersistPeriod;
360 }
361 }
362 if (dundCache) {
363 if (dundCachePeriod.isEmpty())
364 (*line) += " --cache";
365 else {
366 (*line) += " -C ";
367 (*line) += dundCachePeriod;
368 }
369 }
370 if (dundPPPd)
371 (*line) += " --pppd " + dundPPPdPath;
372 if (dundMsdun) {
373 if (dundMsdunTimeout.isEmpty())
374 (*line) += " --msdun";
375 else {
376 (*line) += " -X ";
377 (*line) += dundMsdunTimeout;
378 }
379 }
380 if (!dundPPPdOptions.isEmpty()) {
381 (*line) += " ";
382 (*line) += dundPPPdOptions;
383 }
384 (*line) += "\"";
385 }
386 stream << (*line) << endl;
387 }
388 return true;
389}
390
391//eof