|
From: Peter K. <pk...@us...> - 2001-04-27 18:33:23
|
The following file was modified in apps/bluetooth/experimental:
Name Old version New version Comment
---- ----------- ----------- -------
btconfig.c 1.1 1.2=20=20=20=20=20=20=20=20=20=20=20=20=20
The accompanying log:
Always read the configuration file (can be disabled with -f /dev/null).
Command line options override the configuration file.
The diff of the modified file(s):
--- btconfig.c 2001/04/27 14:41:23 1.1
+++ btconfig.c 2001/04/27 18:33:23 1.2
@@ -1,7 +1,7 @@
/*
* btconfig.c -- Configures BT driver from options or config file
*
- * Copyright (C) 2000, 2001 Axis Communications AB
+ * Copyright (C) 2001 Axis Communications AB
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -37,6 +37,8 @@
*
*/
=20
+/****************** INCLUDE FILES SECTION ********************************=
***/
+
#include <stdio.h>
#include <syslog.h>
#include <string.h>
@@ -60,13 +62,10 @@
#include "bt_conf.h"
#include "bt_misc.h"
=20=20=20=20=20
-/*=20
- * Syntax : btconfig [--force_msswitch <0/1> --wrscan_enable <mode>=20
- * --name <name> --file <filename>]
- */
-
/****************** CONSTANT AND MACRO SECTION ***************************=
***/
=20
+#define D(x) //x
+
#ifndef FALSE
#define FALSE (0)
#endif
@@ -79,122 +78,148 @@
#define DOMAIN_NAME_LENGTH 100
#define BUF_MAXSIZE 300
=20
-#define PRE " ("
-#define POST ")"
+#define CONF_FILE "/etc/bt.conf"
=20
-#define D(x) //x
+/****************** TYPE DEFINITION SECTION ******************************=
***/
=20
/****************** LOCAL FUNCTION DECLARATION SECTION *******************=
***/
=20
+static void show_usage(void);
+
/* File parsing functions */
static int parse_file(char *filename);
static int parse_line(char* line, char** field, char** value);
static int configure_field(const char* field, const char* value);
=20
-/*************************************************************************=
**/
+/****************** GLOBAL VARIABLE DECLARATION SECTION ******************=
***/
=20
+/****************** LOCAL VARIABLE DECLARATION SECTION *******************=
***/
+
static int option_index =3D 0;
-static char bt_name[248];
+static char bt_name[MAX_BTNAME_LEN+1];
static char ip_addr[32] =3D "0.0.0.0";
static char ip_name[HOST_NAME_LENGTH + DOMAIN_NAME_LENGTH] =3D "";
static char ip_addrname[36 + HOST_NAME_LENGTH + DOMAIN_NAME_LENGTH] =3D "";
=20
-static int var_add_ip =3D TRUE;
-static int var_add_host_name =3D TRUE;
+static int var_add_ip =3D FALSE;
+static int var_add_host_name =3D FALSE;
+static int var_write_scan_enable =3D -1; /* not yet set */
static int var_force_ms_switch =3D -1; /* not yet set */
static int var_set_local_name =3D 0;
=20
/* long option list */
static struct option long_options[] =3D
{
- {"file", 1, NULL, 'f'}, /* read options from file */
- {"force_msswitch", 1, NULL, 'm'}, /* force m/s switch as server */
+ { "file", 1, NULL, 'f' }, /* config file name */
+ { "help", 1, NULL, 'h' }, /* show help */
+ { "force-msswitch", 1, NULL, 'm' }, /* force m/s switch as server */
{"name", 1, NULL, 'n'}, /* set BT friendly name */
- {"wrscan_enable", 1, NULL, 'w'}, /* sets write scan enable */
+ { "wrscan-enable", 1, NULL, 'w' }, /* sets write scan enable */
{0, 0, 0, 0}
};
=20
-/*************************************************************************=
*/
+/****************** FUNCTION DEFINITION SECTION **************************=
***/
=20
void
-show_menu()
+show_usage(void)
{
- printf("\nWrong syntax or missing parameters\n");
- printf("Syntax : ./btconfig [--force_msswitch <0/1> --wrscan_enable <mod=
e> ");
- printf("--name <name> --file <filename>]\n");
+ printf("Syntax: btconfig [--force-msswitch (0|1)] [--wrscan-enable <mode=
>] [--name <name>] [--file <config file>]\n");
}
=20
int
main(int argc, char **argv)
{
int bt_cfd, opt;
-=20=20
- /* Print header if called via http */
- if (getenv("REQUEST_METHOD") !=3D NULL)
- {
- printf("Content-type: text/plain\r\n\r\n");
- }
-
- /* Open BT control tty */
- bt_cfd =3D bt_openctrl();
-
- /* Wait until stack is initiated */
- while (!bt_isinitiated())
- sleep(1);
-
- if (argc < 2)
- show_menu();
-=20=20
- /* Now parse options, if using both options and file the last argument=
=20
- overrides any earlier options */
+ int opt_write_scan_enable =3D -1;
+ int opt_force_ms_switch =3D -1;
+ char *opt_name =3D NULL;
+ char *opt_config_file =3D CONF_FILE;
=20
-#define OPTIONS_STRING "f:m:n:w:"=20=20
+ /* Parse command line options */
=20=20=20
- while ((opt =3D getopt_long(argc, argv, OPTIONS_STRING,=20
+ while ((opt =3D getopt_long(argc, argv, "f:hm:n:w:",
long_options, &option_index)) !=3D -1)
{
switch (opt)
{
+ case 'f':
+ opt_config_file =3D optarg;
+ break;
+
+ case 'h':
+ show_usage();
+ exit(0);
+
case 'm':
- /* Force a master slave switch as server */
- var_force_ms_switch =3D atoi(optarg);
+ /* Force a master/slave switch as server */
+ opt_force_ms_switch =3D atoi(optarg);
break;
=20
case 'n':
/* Set BT friendly name */
- var_set_local_name =3D 1;
- strncpy(bt_name, optarg, strlen(optarg));
+ opt_name =3D optarg;
break;
=20=20=20=20=20=20=20
case 'w':
/* Set write scan enable */
- bt_write_scan_enable(bt_cfd, atoi(optarg));
- break;
-
- case 'f':
- /* Read options from file */
- parse_file(optarg);=20=20
+ opt_write_scan_enable =3D atoi(optarg);
break;
=20
default:
-=20=20=20=20=20=20
break;
}
}
=20=20=20
+ parse_file(opt_config_file);
+
+ /* Command line arguments override the configuration file */
+
+ if (opt_name)
+ {
+ strncpy(bt_name, opt_name, MAX_BTNAME_LEN);
+ bt_name[MAX_BTNAME_LEN] =3D 0;
+ var_set_local_name =3D 1;
+ }
+=20=20
+ if (opt_write_scan_enable >=3D 0)
+ var_write_scan_enable =3D opt_write_scan_enable;
+
+ if (opt_force_ms_switch >=3D 0)
+ var_force_ms_switch =3D opt_force_ms_switch;
+
+ /* Configure the stack according to the options specified */
+=20=20
+ if (var_set_local_name ||
+ var_write_scan_enable >=3D 0 ||
+ var_force_ms_switch >=3D 0)
+ {
+ /* Wait until stack is initiated */
+ while (!bt_isinitiated())
+ sleep(1);
+
+ /* Open BT control tty */
+ if ((bt_cfd =3D bt_openctrl()) >=3D 0)
+ {
/* Configure BT local name */
if (var_set_local_name)
bt_set_local_name(bt_cfd, bt_name);
=20=20=20
+ /* Configure Write Scan Enable */
+ if (var_write_scan_enable >=3D 0)
+ bt_write_scan_enable(bt_cfd, var_write_scan_enable);
+
/* Configure Force M/S switch */
if (var_force_ms_switch >=3D 0)
bt_force_msswitch_as_server(bt_cfd, var_force_ms_switch);
=20
close(bt_cfd);
+ }
+ }
+
exit(0);
}
=20
-/*******************************************************************/
+/*************************************************************************=
*/
/*=20
* File parsing functions=20
*/=20
@@ -206,8 +231,7 @@
=20=20=20
if (!(f =3D fopen(filename, "r")))
{
- perror("fopen");=20=20=20=20
- exit(1);
+ return TRUE;
}
=20=20=20
syslog(LOG_INFO, __FUNCTION__": Opened %s file ok", filename);
@@ -227,23 +251,25 @@
}
}
=20
+ fclose(f);
+
/* Name handling */
- strcpy(ip_addrname, PRE);
+ if (*ip_addr || *ip_name)
+ {
+ strcpy(ip_addrname, " (");
strcat(ip_addrname, ip_addr);
- if (strlen(ip_name))
+ if (*ip_name)
{
- if (strlen(ip_addrname) > strlen(PRE)) {
+ if (*ip_addr) {
strcat(ip_addrname, ", ");
}
strcat(ip_addrname, ip_name);
}
- strcat(ip_addrname, POST);
+ strcat(ip_addrname, ")");
=20
- if (strlen(ip_addrname) > (strlen(PRE) + strlen(POST)))
- {
if ((strlen(bt_name) + strlen(ip_addrname)) > MAX_BTNAME_LEN)
{
- D(syslog(LOG_INFO, __FUNCTION__": Name to long, trunkating"));
+ D(syslog(LOG_INFO, __FUNCTION__": Name to long, truncating"));
strcpy(bt_name + MAX_BTNAME_LEN - strlen(ip_addrname), ip_addrname);
}
else
@@ -255,10 +281,6 @@
D(syslog(LOG_INFO, __FUNCTION__": Final name is (%d): %s\n",
strlen(bt_name), bt_name));
=20
- fclose(f);
-
- var_set_local_name =3D 1;
-=20=20
return TRUE;
} /* parse_file */
=20
@@ -333,7 +355,6 @@
int
configure_field(const char* field, const char* value)
{
-=20=20
if (!strcasecmp(field, "DeviceName"))
{
if (strlen(value) <=3D MAX_BTNAME_LEN)
@@ -349,6 +370,8 @@
syslog(LOG_INFO, ": Name to long, truncating to (%d): %s",
(int)strlen(bt_name), bt_name);
}
+
+ var_set_local_name =3D 1;
}
else if (!strcasecmp(field, "AddIP"))
{
@@ -402,10 +425,11 @@
else if (!strcasecmp(field, "ForceMSSwitch"))=20=20
{
var_force_ms_switch =3D !strcasecmp(value, "yes");
- D(syslog(LOG_INFO, __FUNCTION__ ": Force M/S switch : %s", value));
+ D(syslog(LOG_INFO, __FUNCTION__ ": Force M/S switch: %s",
+ (var_force_ms_switch ? "yes" : "no")));
}
=20=20=20
return TRUE;
} /* configure_field */
=20
-/***************************************************************/
+/****************** END OF FILE btconfig.c *******************************=
***/
|