1#!/bin/bash
2# Copyright (c) 2022 Eriptic Technologies. See the COPYRIGHT
3# file at the top-level directory of this distribution.
4
5# Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6# http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7# <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8# option. This file may not be copied, modified, or distributed
9# except according to those terms.
10
11# Run this script on a linux host (tested on Debian 11) in order to connect a BLE board running IPv6 over BLE.
12
13# This script is based on the configuration example https://docs.zephyrproject.org/latest/samples/bluetooth/ipsp/README.html
14
15
16ROOT_UID=0		# Only users with $UID 0 have root privileges
17E_NOTROOT=87		# Non-root exit error
18
19
20# Run this script as root
21if [ "$EUID" -ne 0 ]
22then
23	echo "Must be root to run this script."
24	exit $E_NOTROOT
25fi
26
27
28
29# Create Help menu message
30version="BLE connect helper script 0.1.0 (2022)"
31argument[0]="\t-d <device name>\tSpecify the name of the device to which the router will connect"
32argument[1]="\t-h\t\t\tPrint Help (this message) and exit"
33
34
35CON=false
36
37
38function connect(){
39
40	while true
41	do
42		# Load 6LoWPAN module.
43		modprobe bluetooth_6lowpan
44
45		# Enable the bluetooth 6lowpan module.
46		echo 1 > /sys/kernel/debug/bluetooth/6lowpan_enable
47
48		# Look for available HCI devices. Use for debug only
49		# hciconfig
50
51		#scan for a device
52		device=''
53		while [ -z "$device" ]
54		do
55			echo "[$(date +"%T")] scanning for devices"
56			# Reset HCI device
57			invoke-rc.d bluetooth restart
58			#systemctl disable bluetooth.service
59			#systemctl enable bluetooth.service
60
61			hciconfig hci0 reset
62
63			# scan
64			device=$(timeout 1s stdbuf -oL hcitool lescan | grep "$1")
65		done
66
67		# Set space as the delimiter
68		IFS=' '
69		#Read the split words into an array based on space delimiter
70		read -a mac <<< "$device"
71		echo "connect to $device"
72		echo "connect $mac 2 " > /sys/kernel/debug/bluetooth/6lowpan_control
73		sleep 1
74
75		#make sure we select short connection events
76		#min connection interval 6*1.25ms = 7.5ms
77		echo "6" > /sys/kernel/debug/bluetooth/hci0/conn_min_interval
78		#max connection interval 7*1.25ms = 8.25ms
79		echo "7" > /sys/kernel/debug/bluetooth/hci0/conn_max_interval
80
81		#assign a static address to the bt0 interface
82		ip address add 2001:db8::2/64 dev bt0
83
84
85		#check if the device is disconnected
86		connected_device=$(hcitool con | grep $mac)
87		read -a mac1 <<< "$connected_device"
88		while [ ${mac1[2]} ]
89		do
90			sleep 3
91			connected_device=$(hcitool con | grep $mac)
92			read -a mac1 <<< "$connected_device"
93			echo "[$(date +"%T")] connected with $device"
94		done
95
96		echo "device disconnected"
97		CON=false
98
99	done
100}
101
102
103function help(){
104	# Print Help menu
105	echo -e "$version\nArguments:\n";
106	i=1
107	for i in "${argument[@]}" ; do
108		echo -e "$argument$i"
109	done
110}
111
112
113
114
115while getopts hd: option
116do
117	case "${option}"
118	in
119		h) help ; exit ;;
120		d) connect ${OPTARG};;
121	esac
122done