linux-bluetooth.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script during install
@ 2018-11-26 16:49 Christian Zimmermann
  2018-11-26 16:49 ` [PATCH BlueZ 2/2] Removed obsolete conversion functions, since the conversion is now done once " Christian Zimmermann
  2018-11-27 11:50 ` [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script " Luiz Augusto von Dentz
  0 siblings, 2 replies; 3+ messages in thread
From: Christian Zimmermann @ 2018-11-26 16:49 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Christian Zimmermann

---
 Makefile.am                                |  8 +++
 tools/bluez4_conversion/convert_config.sh  | 89 ++++++++++++++++++++++++++++++
 tools/bluez4_conversion/convert_devices.sh |  3 +
 3 files changed, 100 insertions(+)
 create mode 100755 tools/bluez4_conversion/convert_config.sh
 create mode 100644 tools/bluez4_conversion/convert_devices.sh

diff --git a/Makefile.am b/Makefile.am
index 0b26ccc3e..6dc5711d9 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -477,6 +477,8 @@ endif
 
 EXTRA_DIST += $(manual_pages:.1=.txt)
 
+EXTRA_DIST += tools/bluez4_conversion/convert_config.sh tools/bluez4_conversion/convert_devices.sh
+
 DISTCHECK_CONFIGURE_FLAGS = --disable-datafiles --enable-library \
 						--enable-health \
 						--enable-midi \
@@ -535,3 +537,9 @@ clean-local:
 	-find $(top_builddir) -name "*.gcda" -delete
 	$(RM) -r lib/bluetooth
 endif
+
+BLUEZ4_CONFIG_CONVERSION = $(srcdir)/tools/bluez4_conversion/convert_config.sh
+BLUEZ4_DEVICE_CONVERSION = $(srcdir)/tools/bluez4_conversion/convert_devices.sh
+install-exec-hook:
+	$(BLUEZ4_CONFIG_CONVERSION)
+	$(BLUEZ4_DEVICE_CONVERION)
\ No newline at end of file
diff --git a/tools/bluez4_conversion/convert_config.sh b/tools/bluez4_conversion/convert_config.sh
new file mode 100755
index 000000000..477e18fae
--- /dev/null
+++ b/tools/bluez4_conversion/convert_config.sh
@@ -0,0 +1,89 @@
+!/bin/sh
+
+CONFIG_DIR="/var/lib/bluetooth"
+
+convert_on_mode()
+{
+    config_file=$1
+    #Read on_mode from config file
+    on_mode=$( cat $config_file | grep -Po 'mode \K(.*)')
+    if [ ! -z "$on_mode" ]
+    then
+        if [ $on_mode == "discoverable" ]
+        then
+            #Write on mode to new INI file
+            echo -e "Discoverable=true" >> settings
+        else
+            #Write on mode to new INI file
+            echo -e "Discoverable=false" >> settings
+        fi
+    else
+        echo "On mode not found"
+    fi
+}
+
+convert_discoverable_timeout()
+{
+    config_file=$1
+    #Read discoverable timeout from config file
+    discoverable_timeout=$( cat $config_file | grep -Po 'discovto \K([0-9]+)')
+    if [ ! -z "$discoverable_timeout" ]
+    then
+        #Write discoverable timeout to new INI file
+        echo -e "DiscoverableTimeout=$discoverable_timeout" >> settings
+    else
+        echo "Discoverable Timeout not found"
+    fi
+}
+
+convert_pairable_timeout()
+{
+    config_file=$1
+    #Read pairable timeout from config file
+    pairable_timeout=$( cat $config_file | grep -Po 'pairto \K([0-9]+)')
+    if [ ! -z "$pairable_timeout" ]
+    then
+        #Write discoverable timeout to new INI file
+        echo -e "PairableTimeout=$pairable_timeout" >> settings
+    else
+        echo "Pairable Timeout not found"
+    fi
+}
+
+convert_local_name()
+{
+    config_file=$1
+    #Read local name from config file
+    local_name=$( cat $config_file | grep -Po 'name \K(.*)')
+    if [ ! -z "$local_name" ]
+    then
+        #Write local_name to new INI file
+        echo -e "Alias=$local_name" >> settings
+    else
+        echo "Local name not found"
+    fi
+}
+
+###
+# Main script body
+###
+for entry in `ls $CONFIG_DIR`; do
+    cd $CONFIG_DIR
+    cd $entry
+    if [ -f "config" ]
+    then
+        if [ ! -f "settings" ]
+        then
+            #Create new settings INI file
+            touch settings
+            echo -e "[General]" >> settings
+            convert_pairable_timeout config
+            convert_discoverable_timeout config
+            convert_on_mode config
+            convert_local_name config
+            rm config
+        else
+            echo "Settings already available"
+        fi
+    fi
+done
diff --git a/tools/bluez4_conversion/convert_devices.sh b/tools/bluez4_conversion/convert_devices.sh
new file mode 100644
index 000000000..a8a013b06
--- /dev/null
+++ b/tools/bluez4_conversion/convert_devices.sh
@@ -0,0 +1,3 @@
+# TODO
+# This file is intended for replacing the conversion of Bluez4 device datas when upgrading to Bluez5 INI format
+# This is currently checked at every bluetoothd startup in src/adapter.c
\ No newline at end of file
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* [PATCH BlueZ 2/2] Removed obsolete conversion functions, since the conversion is now done once during install
  2018-11-26 16:49 [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script during install Christian Zimmermann
@ 2018-11-26 16:49 ` Christian Zimmermann
  2018-11-27 11:50 ` [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script " Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Christian Zimmermann @ 2018-11-26 16:49 UTC (permalink / raw)
  To: linux-bluetooth; +Cc: Christian Zimmermann

---
 src/adapter.c |  51 ---------------------------
 src/storage.c | 110 ----------------------------------------------------------
 src/storage.h |   4 ---
 3 files changed, 165 deletions(-)

diff --git a/src/adapter.c b/src/adapter.c
index c24432125..ff8f64300 100644
--- a/src/adapter.c
+++ b/src/adapter.c
@@ -423,18 +423,6 @@ void btd_adapter_set_class(struct btd_adapter *adapter, uint8_t major,
 	set_dev_class(adapter);
 }
 
-static uint8_t get_mode(const char *mode)
-{
-	if (strcasecmp("off", mode) == 0)
-		return MODE_OFF;
-	else if (strcasecmp("connectable", mode) == 0)
-		return MODE_CONNECTABLE;
-	else if (strcasecmp("discoverable", mode) == 0)
-		return MODE_DISCOVERABLE;
-	else
-		return MODE_UNKNOWN;
-}
-
 const char *btd_adapter_get_storage_dir(struct btd_adapter *adapter)
 {
 	static char dir[25];
@@ -5639,44 +5627,6 @@ static void convert_device_storage(struct btd_adapter *adapter)
 	textfile_foreach(filename, convert_proximity_entry, address);
 }
 
-static void convert_config(struct btd_adapter *adapter, const char *filename,
-							GKeyFile *key_file)
-{
-	char address[18];
-	char str[MAX_NAME_LENGTH + 1];
-	char config_path[PATH_MAX];
-	int timeout;
-	uint8_t mode;
-	char *data;
-	gsize length = 0;
-
-	ba2str(&adapter->bdaddr, address);
-	snprintf(config_path, PATH_MAX, STORAGEDIR "/%s/config", address);
-
-	if (read_pairable_timeout(address, &timeout) == 0)
-		g_key_file_set_integer(key_file, "General",
-						"PairableTimeout", timeout);
-
-	if (read_discoverable_timeout(address, &timeout) == 0)
-		g_key_file_set_integer(key_file, "General",
-						"DiscoverableTimeout", timeout);
-
-	if (read_on_mode(address, str, sizeof(str)) == 0) {
-		mode = get_mode(str);
-		g_key_file_set_boolean(key_file, "General", "Discoverable",
-					mode == MODE_DISCOVERABLE);
-	}
-
-	if (read_local_name(&adapter->bdaddr, str) == 0)
-		g_key_file_set_string(key_file, "General", "Alias", str);
-
-	create_file(filename, S_IRUSR | S_IWUSR);
-
-	data = g_key_file_to_data(key_file, &length, NULL);
-	g_file_set_contents(filename, data, length, NULL);
-	g_free(data);
-}
-
 static void fix_storage(struct btd_adapter *adapter)
 {
 	char filename[PATH_MAX];
@@ -5753,7 +5703,6 @@ static void load_config(struct btd_adapter *adapter)
 					btd_adapter_get_storage_dir(adapter));
 
 	if (stat(filename, &st) < 0) {
-		convert_config(adapter, filename, key_file);
 		convert_device_storage(adapter);
 	}
 
diff --git a/src/storage.c b/src/storage.c
index 734a9e08e..6317d7cf5 100644
--- a/src/storage.c
+++ b/src/storage.c
@@ -22,128 +22,18 @@
  *
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <stdio.h>
-#include <errno.h>
-#include <ctype.h>
-#include <fcntl.h>
-#include <unistd.h>
 #include <stdlib.h>
-#include <time.h>
-#include <sys/file.h>
-#include <sys/stat.h>
 
 #include <glib.h>
 
 #include "lib/bluetooth.h"
 #include "lib/sdp.h"
 #include "lib/sdp_lib.h"
-#include "lib/uuid.h"
 
 #include "textfile.h"
 #include "uuid-helper.h"
 #include "storage.h"
 
-/* When all services should trust a remote device */
-#define GLOBAL_TRUST "[all]"
-
-struct match {
-	GSList *keys;
-	char *pattern;
-};
-
-static inline int create_filename(char *buf, size_t size,
-				const bdaddr_t *bdaddr, const char *name)
-{
-	char addr[18];
-
-	ba2str(bdaddr, addr);
-
-	return create_name(buf, size, STORAGEDIR, addr, name);
-}
-
-int read_discoverable_timeout(const char *src, int *timeout)
-{
-	char filename[PATH_MAX], *str;
-
-	create_name(filename, PATH_MAX, STORAGEDIR, src, "config");
-
-	str = textfile_get(filename, "discovto");
-	if (!str)
-		return -ENOENT;
-
-	if (sscanf(str, "%d", timeout) != 1) {
-		free(str);
-		return -ENOENT;
-	}
-
-	free(str);
-
-	return 0;
-}
-
-int read_pairable_timeout(const char *src, int *timeout)
-{
-	char filename[PATH_MAX], *str;
-
-	create_name(filename, PATH_MAX, STORAGEDIR, src, "config");
-
-	str = textfile_get(filename, "pairto");
-	if (!str)
-		return -ENOENT;
-
-	if (sscanf(str, "%d", timeout) != 1) {
-		free(str);
-		return -ENOENT;
-	}
-
-	free(str);
-
-	return 0;
-}
-
-int read_on_mode(const char *src, char *mode, int length)
-{
-	char filename[PATH_MAX], *str;
-
-	create_name(filename, PATH_MAX, STORAGEDIR, src, "config");
-
-	str = textfile_get(filename, "onmode");
-	if (!str)
-		return -ENOENT;
-
-	strncpy(mode, str, length);
-	mode[length - 1] = '\0';
-
-	free(str);
-
-	return 0;
-}
-
-int read_local_name(const bdaddr_t *bdaddr, char *name)
-{
-	char filename[PATH_MAX], *str;
-	int len;
-
-	create_filename(filename, PATH_MAX, bdaddr, "config");
-
-	str = textfile_get(filename, "name");
-	if (!str)
-		return -ENOENT;
-
-	len = strlen(str);
-	if (len > HCI_MAX_NAME_LENGTH)
-		str[HCI_MAX_NAME_LENGTH] = '\0';
-	strcpy(name, str);
-
-	free(str);
-
-	return 0;
-}
-
 sdp_record_t *record_from_string(const char *str)
 {
 	sdp_record_t *rec;
diff --git a/src/storage.h b/src/storage.h
index 1c0ad57ec..8b7d9aa30 100644
--- a/src/storage.h
+++ b/src/storage.h
@@ -21,9 +21,5 @@
  *
  */
 
-int read_discoverable_timeout(const char *src, int *timeout);
-int read_pairable_timeout(const char *src, int *timeout);
-int read_on_mode(const char *src, char *mode, int length);
-int read_local_name(const bdaddr_t *bdaddr, char *name);
 sdp_record_t *record_from_string(const char *str);
 sdp_record_t *find_record_in_list(sdp_list_t *recs, const char *uuid);
-- 
2.11.0


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script during install
  2018-11-26 16:49 [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script during install Christian Zimmermann
  2018-11-26 16:49 ` [PATCH BlueZ 2/2] Removed obsolete conversion functions, since the conversion is now done once " Christian Zimmermann
@ 2018-11-27 11:50 ` Luiz Augusto von Dentz
  1 sibling, 0 replies; 3+ messages in thread
From: Luiz Augusto von Dentz @ 2018-11-27 11:50 UTC (permalink / raw)
  To: zimmermach; +Cc: linux-bluetooth

Hi Christian,
On Mon, Nov 26, 2018 at 6:53 PM Christian Zimmermann
<zimmermach@gmail.com> wrote:
>
> ---
>  Makefile.am                                |  8 +++
>  tools/bluez4_conversion/convert_config.sh  | 89 ++++++++++++++++++++++++++++++
>  tools/bluez4_conversion/convert_devices.sh |  3 +
>  3 files changed, 100 insertions(+)
>  create mode 100755 tools/bluez4_conversion/convert_config.sh
>  create mode 100644 tools/bluez4_conversion/convert_devices.sh

The permission of the convert_config.sh look incorrect, besides that
we normally avoid having extra directories so I would put them under
tools, or better yet scripts since these are not written in C. Id also
don't want to make them specific to a version so they might carry over
other all the legacy formats and always convert to the latest, for
that reason perhaps we should have unit test for the conversion tools.

> diff --git a/Makefile.am b/Makefile.am
> index 0b26ccc3e..6dc5711d9 100644
> --- a/Makefile.am
> +++ b/Makefile.am
> @@ -477,6 +477,8 @@ endif
>
>  EXTRA_DIST += $(manual_pages:.1=.txt)
>
> +EXTRA_DIST += tools/bluez4_conversion/convert_config.sh tools/bluez4_conversion/convert_devices.sh
> +
>  DISTCHECK_CONFIGURE_FLAGS = --disable-datafiles --enable-library \
>                                                 --enable-health \
>                                                 --enable-midi \
> @@ -535,3 +537,9 @@ clean-local:
>         -find $(top_builddir) -name "*.gcda" -delete
>         $(RM) -r lib/bluetooth
>  endif
> +
> +BLUEZ4_CONFIG_CONVERSION = $(srcdir)/tools/bluez4_conversion/convert_config.sh
> +BLUEZ4_DEVICE_CONVERSION = $(srcdir)/tools/bluez4_conversion/convert_devices.sh
> +install-exec-hook:
> +       $(BLUEZ4_CONFIG_CONVERSION)
> +       $(BLUEZ4_DEVICE_CONVERION)
> \ No newline at end of file
> diff --git a/tools/bluez4_conversion/convert_config.sh b/tools/bluez4_conversion/convert_config.sh
> new file mode 100755
> index 000000000..477e18fae
> --- /dev/null
> +++ b/tools/bluez4_conversion/convert_config.sh
> @@ -0,0 +1,89 @@
> +!/bin/sh
> +
> +CONFIG_DIR="/var/lib/bluetooth"
> +
> +convert_on_mode()
> +{
> +    config_file=$1
> +    #Read on_mode from config file
> +    on_mode=$( cat $config_file | grep -Po 'mode \K(.*)')
> +    if [ ! -z "$on_mode" ]
> +    then
> +        if [ $on_mode == "discoverable" ]
> +        then
> +            #Write on mode to new INI file
> +            echo -e "Discoverable=true" >> settings
> +        else
> +            #Write on mode to new INI file
> +            echo -e "Discoverable=false" >> settings
> +        fi
> +    else
> +        echo "On mode not found"
> +    fi
> +}
> +
> +convert_discoverable_timeout()
> +{
> +    config_file=$1
> +    #Read discoverable timeout from config file
> +    discoverable_timeout=$( cat $config_file | grep -Po 'discovto \K([0-9]+)')
> +    if [ ! -z "$discoverable_timeout" ]
> +    then
> +        #Write discoverable timeout to new INI file
> +        echo -e "DiscoverableTimeout=$discoverable_timeout" >> settings
> +    else
> +        echo "Discoverable Timeout not found"
> +    fi
> +}
> +
> +convert_pairable_timeout()
> +{
> +    config_file=$1
> +    #Read pairable timeout from config file
> +    pairable_timeout=$( cat $config_file | grep -Po 'pairto \K([0-9]+)')
> +    if [ ! -z "$pairable_timeout" ]
> +    then
> +        #Write discoverable timeout to new INI file
> +        echo -e "PairableTimeout=$pairable_timeout" >> settings
> +    else
> +        echo "Pairable Timeout not found"
> +    fi
> +}
> +
> +convert_local_name()
> +{
> +    config_file=$1
> +    #Read local name from config file
> +    local_name=$( cat $config_file | grep -Po 'name \K(.*)')
> +    if [ ! -z "$local_name" ]
> +    then
> +        #Write local_name to new INI file
> +        echo -e "Alias=$local_name" >> settings
> +    else
> +        echo "Local name not found"
> +    fi
> +}
> +
> +###
> +# Main script body
> +###
> +for entry in `ls $CONFIG_DIR`; do
> +    cd $CONFIG_DIR
> +    cd $entry
> +    if [ -f "config" ]
> +    then
> +        if [ ! -f "settings" ]
> +        then
> +            #Create new settings INI file
> +            touch settings
> +            echo -e "[General]" >> settings
> +            convert_pairable_timeout config
> +            convert_discoverable_timeout config
> +            convert_on_mode config
> +            convert_local_name config
> +            rm config
> +        else
> +            echo "Settings already available"
> +        fi
> +    fi
> +done
> diff --git a/tools/bluez4_conversion/convert_devices.sh b/tools/bluez4_conversion/convert_devices.sh
> new file mode 100644
> index 000000000..a8a013b06
> --- /dev/null
> +++ b/tools/bluez4_conversion/convert_devices.sh
> @@ -0,0 +1,3 @@
> +# TODO
> +# This file is intended for replacing the conversion of Bluez4 device datas when upgrading to Bluez5 INI format
> +# This is currently checked at every bluetoothd startup in src/adapter.c
> \ No newline at end of file
> --
> 2.11.0
>


-- 
Luiz Augusto von Dentz

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2018-11-27 11:50 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-11-26 16:49 [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script during install Christian Zimmermann
2018-11-26 16:49 ` [PATCH BlueZ 2/2] Removed obsolete conversion functions, since the conversion is now done once " Christian Zimmermann
2018-11-27 11:50 ` [PATCH BlueZ 1/2] Added config conversion for Bluez4 as shell script " Luiz Augusto von Dentz

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).