diff --git a/roles/4-server-options/tasks/main.yml b/roles/4-server-options/tasks/main.yml index eca44a6fb..2a908b772 100644 --- a/roles/4-server-options/tasks/main.yml +++ b/roles/4-server-options/tasks/main.yml @@ -28,6 +28,12 @@ when: squid_install | bool tags: base, squid, network, domain +- name: Install Bluetooth - only on Raspberry Pi + include_role: + name: bluetooth + when: is_rpi | bool and bluetooth_install | bool + tags: bluetooth + # NETWORK moved to the very end, after Stage 9 (9-LOCAL-ADDONS) # It can also be run manually using: cd /opt/iiab/iiab; ./iiab-network # diff --git a/roles/bluetooth/README.rst b/roles/bluetooth/README.rst new file mode 100644 index 000000000..5f1104ff1 --- /dev/null +++ b/roles/bluetooth/README.rst @@ -0,0 +1,11 @@ +================ +Bluetooth README +================ + +This role provides two services, pan or Personal Area Network, and term, terminal emulation. + +They may be turned on or off with iiab-bt-pan-on(off) and iiab-bt-term-on(off), respectively. + +The terminal emulates a vt100 and operates at 115200 bps, but does not always connect reliably. + +This role is only available for the Raspberry Pi. diff --git a/roles/bluetooth/defaults/main.yml b/roles/bluetooth/defaults/main.yml new file mode 100644 index 000000000..28fa4359c --- /dev/null +++ b/roles/bluetooth/defaults/main.yml @@ -0,0 +1,3 @@ +bluetooth_install: False +bluetooth_enabled: False +bluetooth_term_enabled: False diff --git a/roles/bluetooth/tasks/main.yml b/roles/bluetooth/tasks/main.yml new file mode 100644 index 000000000..421b9c63e --- /dev/null +++ b/roles/bluetooth/tasks/main.yml @@ -0,0 +1,4 @@ +# This is rpi only + +- include_tasks: rpi_install.yml + when: is_rpi and bluetooth_install diff --git a/roles/bluetooth/tasks/rpi_install.yml b/roles/bluetooth/tasks/rpi_install.yml new file mode 100644 index 000000000..3cf82c49b --- /dev/null +++ b/roles/bluetooth/tasks/rpi_install.yml @@ -0,0 +1,132 @@ +# This is rpi only + +- name: "Install rpi bluetooth packages" + package: + name: + - bluetooth + - bluez + - bluez-tools + state: present + +- name: Create bluetooth services + template: + backup: no + src: "{{ item.src }}" + dest: "{{ item.dest }}" + owner: root + group: root + mode: 0644 + with_items: + - { src: 'bt-agent.service.j2', dest: '/etc/systemd/system/bt-agent.service' } + - { src: 'bt-pan.service.j2', dest: '/etc/systemd/system/bt-pan.service' } + - { src: 'bt-term.service.j2', dest: '/etc/systemd/system/bt-term.service' } + - { src: 'network.conf.j2', dest: '/etc/bluetooth/network.conf' } + +- name: Create bluetooth utility scripts + template: + backup: no + src: "{{ item.src }}" + dest: "{{ item.dest }}" + owner: root + group: root + mode: 0755 + with_items: + - { src: 'iiab-bt-pan-on.j2', dest: '/usr/bin/iiab-bt-pan-on' } + - { src: 'iiab-bt-pan-off.j2', dest: '/usr/bin/iiab-bt-pan-off' } + - { src: 'iiab-bt-pan-discoverable-on.j2', dest: 'iiab-bt-pan-discoverable-on' } + - { src: 'iiab-bt-term-on.j2', dest: '/usr/bin/iiab-bt-term-on' } + - { src: 'iiab-bt-term-off.j2', dest: '/usr/bin/iiab-bt-term-off' } + +# Bluetooth service needs /usr/lib/bluetooth/bluetoothd -C --noplugin=sap +# Copy and patch it + +- name: Copy the bluetooth service + template: + dest: /etc/systemd/system/bluetooth.service + src: /lib/systemd/system/bluetooth.service + +- name: Add -C --noplugin=sap to execStart of bluetooth service + lineinfile: + path: /etc/systemd/system/bluetooth.service + regexp: '^ExecStart=/usr/lib/bluetooth/bluetoothd' + line: 'ExecStart=/usr/lib/bluetooth/bluetoothd -C --noplugin=sap' + +- name: Set discoverable not to timeout + lineinfile: + path: /etc/bluetooth/main.conf + regexp: '^#DiscoverableTimeout' + line: 'DiscoverableTimeout = 0' + +- name: Enable & Restart 'bt-agent' service + systemd: + daemon_reload: yes + name: bluetooth + enabled: yes + state: restarted + +# enable or disable bt-agent +- name: Enable & Restart 'bt-agent' service + systemd: + daemon_reload: yes + name: bt-agent + enabled: yes + state: restarted + when: bluetooth_enabled or bluetooth_term_enabled + +- name: Disable 'bt-agent' service + systemd: + daemon_reload: yes + name: bt-agent + enabled: no + state: stopped + when: not bluetooth_enabled and not bluetooth_term_enabled + +# enable or disable bt-pan +- name: Enable & Restart 'bt-pan' service + systemd: + daemon_reload: yes + name: bt-pan + enabled: yes + state: restarted + when: bluetooth_enabled | bool + +- name: Disable 'bt-pan' service + systemd: + daemon_reload: yes + name: bt-pan + enabled: no + state: stopped + when: not bluetooth_enabled | bool + +# enable or disable bt-term +- name: Enable & Restart 'bt-term' service + systemd: + daemon_reload: yes + name: bt-term + enabled: yes + state: restarted + when: bluetooth_term_enabled | bool + +- name: Disable 'bt-term' service + systemd: + daemon_reload: yes + name: bt-term + enabled: no + state: stopped + when: not bluetooth_term_enabled | bool + +- name: Add 'bluetooth' variable values to {{ iiab_ini_file }} + ini_file: + path: "{{ iiab_ini_file }}" + section: bluetooth + option: "{{ item.option }}" + value: "{{ item.value }}" + with_items: + - option: name + value: Bluetooth + - option: description + value: '"Bluetooth services for pan and terminal."' + - option: bluetooth_enabled + value: "{{ bluetooth_enabled }}" + - option: bluetooth_term_enabled + value: "{{ bluetooth_term_enabled }}" diff --git a/roles/bluetooth/templates/bt-agent.service.j2 b/roles/bluetooth/templates/bt-agent.service.j2 new file mode 100644 index 000000000..ba3537e0b --- /dev/null +++ b/roles/bluetooth/templates/bt-agent.service.j2 @@ -0,0 +1,10 @@ +[Unit] +Description=Bluetooth Auth Agent + +[Service] +ExecStart=/usr/bin/bt-agent -c NoInputNoOutput +Type=simple + +[Install] +WantedBy=multi-user.target + diff --git a/roles/bluetooth/templates/bt-pan.service.j2 b/roles/bluetooth/templates/bt-pan.service.j2 new file mode 100644 index 000000000..eb913841d --- /dev/null +++ b/roles/bluetooth/templates/bt-pan.service.j2 @@ -0,0 +1,15 @@ +[Unit] +Description=Bluetooth NEP PAN +#After=br0.network +Before=network.target +After=network-pre.target +Requires=network-pre.target + +[Service] +ExecStart=/usr/bin/bt-network -s nap br0 +ExecStartPost=/usr/bin/iiab-bt-pan-discoverable-on +Type=simple + +[Install] +WantedBy=multi-user.target + diff --git a/roles/bluetooth/templates/bt-term.service.j2 b/roles/bluetooth/templates/bt-term.service.j2 new file mode 100644 index 000000000..a0efc270d --- /dev/null +++ b/roles/bluetooth/templates/bt-term.service.j2 @@ -0,0 +1,12 @@ +[Unit] +Description=RFCOMM service +After=bluetooth.service +Requires=bluetooth.service + +[Service] +ExecStartPre=/usr/bin/sdptool add SP +ExecStartPre=/bin/hciconfig hci0 piscan +ExecStart=/usr/bin/rfcomm watch hci0 1 getty rfcomm0 115200 vt100 + +[Install] +WantedBy=multi-user.target diff --git a/roles/bluetooth/templates/iiab-bt-pan-discoverable-on.j2 b/roles/bluetooth/templates/iiab-bt-pan-discoverable-on.j2 new file mode 100644 index 000000000..fc6030e7c --- /dev/null +++ b/roles/bluetooth/templates/iiab-bt-pan-discoverable-on.j2 @@ -0,0 +1,5 @@ +#!/bin/bash + +bluetoothctl <