How to initialize Bluetooth in a startup script with Yocto Poky Linux - linux

I have a script that initializes my bluetooth setup on an Intel Edison. It allows pairing and connecting to this headless machine running Yocto Poky Linux. It was suggested that I put the startup script in /etc/init.d and run update-rc.d myscript.sh defaults. The script ran but it didn't work (generated boot errors saying bluetooth device not found) because Bluetooth had not started yet. I did some reasearch and after removing my links I did update-rc.d myscript.sh defaults 99 which was claimed to run the script last but it did't make any differrence -- it still ran in the same place in the boot sequence. I verified that the links had S99 on them so it seemed like they were set up correctly. There is another post on SO asking a similar question but that was a Ubuntu system where mine is Poky Linux. That solution suggested putting the startup script in a directory that does not exist on my system. There were other suggestions, putting it in rc.local, which I did and got the same result, it runs before Bluetooth is initialized.
Here is my script. My program is called nmea_thread and is run last. Everything else is initializing Bluetooth.
#!/bin/sh
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread

Often bluetooth is initialized asynchronously, so you can't be sure that your script will be run after hci0 is added. Good solution is to wait for BT initialization in background:
#!/bin/bash
if [ "$1" != "background" ]; then
$0 background &
else
#Wait until BT is initialized
for ((i = 0; i <= 100; i++)) do
hciconfig hci0 && break
usleep 100000
done
/usr/sbin/rfkill unblock bluetooth
/usr/bin/hciconfig hci0 up
/usr/bin/hciconfig hci0 piscan
/usr/bin/hciconfig hic0 sspmode 0
/home/root/simpleAgent/simple-agent &
/home/root/nmea_thread
fi

hciattach is the correct way.
syntax
hciattach /dev/ttyAMA0 bcm43xx 3000000

you need to flash the driver first before initializing it. Currently i don't remember how, but thats how i made it with raspberry pi and yocto.
Note if you use systemV, you can do call it from a script and it will work
Using SystemD, you need to make it in a service and wait. Falshing should be done in the two cases.

Related

run program when usb connected to raspberry pi

I'm trying to run a program when I plug USB to my Raspberry Pi 3 model B.
I referenced below two
https://hackaday.com/2009/09/18/how-to-write-udev-rules/
https://unix.stackexchange.com/questions/65891/how-to-execute-a-shellscript-when-i-plug-in-a-usb-device
write a *.rules file in /etc/udev/rules.d/ directory
sudo nano /etc/udev/rules.d/81-usb.rules
fill with
KERNEL=="sda1", RUN+="echo Hello World > /home/pi/hello.txt"
restart udev
sudo /etc/init.d/udev restart
I almost tried every variation, rebooted a lot, just in case I'm missing something. But nothing works.
Thanks for reading.
Thanks stark.
Anyway I'm answering my own Question.
I changed it from
KERNEL=="sda1", RUN+="echo Hello World > /home/pi/hello.txt"
to
KERNEL=="sda1", RUN+="/home/pi/test.sh"
also did
chmod +x /home/pi/test.sh

Detect if an ethernet port is unplugged

I want to trap the event of unplugging an ethernet port on a Linux Ubuntu 14.04.
I want to create a script that detect whenever an ethernet port is unplugged and write it to a log.
Which is the best way to trap such an event
Just put an executable script inside /etc/network/if-post-down.d.
#!/bin/bash
set -e
if [[ "$IFACE" == "wlan0" ]]; then
logger "The wlan0 interface is down!"
# Do whatever you want here.
fi
Make sure to chmod +x it, also.
Read more about these events/scripts here on the Ubuntu Wiki.

Write a bash script that executes a command when a USB device is plugged in/removed [duplicate]

Is there a Bash script and/or daemon that I can write that will detect a specific USB drive and then sync that drive with a directory?
For future reference, here's how to run a bash script upon detection of a USB drive.
Connect your device and run lsusb to retrieve the device's info. You should see something similar to this:
$ lsusb
Bus 002 Device 039: ID 0bc2:2100 Seagate RSS LLC
In this case, the vendor ID of the device is 0bc2 and the product ID is 2100.
Now you can create your UDEV rule using a text editor of your choice.
$sudo vi /etc/udev/rules.d/85-my_usb_device_rule.rules
And add this:
ACTION=="add", SUBSYSTEM=="usb", SYSFS{idVendor}=="0bc2",
SYSFS{idProduct}=="2100", RUN+="/home/myhome/my_script"
/home/myhome/my_script is the path to your script which it would do whatever you want.
To make sure the detection script will execute right away, run this command to reload the UDEV rules:
$sudo udevadm control --reload-rules
This was tested on Fedora 14.
I didn't do it myself, but you can try udev rules like this:
# Hitachi SimpleDrive mini, model HTS545050B9A300 (500 GB USB HDD)
SUBSYSTEM=="block", ATTR{size}=="976768002", ATTRS{product}=="SimpleDrive mini", ATTRS{serial}=="2512009121920487", ACTION=="add", RUN+="/lib/udev/local.usb.hdd.sh add $devpath"
Place it in /etc/udev/rules.d/90-local.rules or similar place, certainly dependable on your OS.
Here is an example python deamon that you could use for the listening part, then copying the files to your directory shouldn't be a problem.
There are excellent answers here already but depending on your use case, it could be as simple as
[ -d /run/media/$USER/USB_STICK ] && do_stuff
USB_STICK here is the label of the usb stick.

How to disable a specific usb port permanently in linux?

Is it possible to permanently disable a usb port in linux?
I have already figured out how to disable it:
echo -n "0000:00:12.0" > /sys/bus/pci/drivers/ohci_hcd/unbind
BUT after restart it is enabled.
I have placed this script :
#!/bin/sh
case "$1" in
resume|thaw)
echo -n "0000:00:12.0" > /sys/bus/pci/drivers/ohci_hcd/unbind
;;
esac
in /etc/pm/sleep.d/0_disable_usb2
But again without success. I also thought that i could disable it through bios but as i could see i can disable the whole pci.
Is there any way of doing this?
.. My Operating system is Debian 7.7 64bit. The reason i want to do this is I am trying to configure my system for realtime capabilities and my usb soundcard sharing the same IRQ with this port.
For me usb mounting is handle by a service udisk2.service if you would like to stop usb mounting then stop below service
root#mahasan-Inspiron-5537:~# systemctl start udisks2.service
root#mahasan-Inspiron-5537:~# systemctl disable udisks2.service
First of all you should find your USB number of your device, Simply by using lsusb.
And in Linux everything is a file, So you can manage all of your hardware using the files.
As it describes Here if your kernel is > 2.6.38 you should use this keywords:
echo "0" > "/sys/bus/usb/devices/usb2/power/autosuspend_delay_ms"
Then:
echo "auto" > "/sys/bus/usb/devices/usb1/power/control"
echo "0" > /sys/bus/usb/devices/usb{x}/authorized

Simple replacement of init to just start console

On a very simple PC, I want to replace Ubuntu 12.04 /sbin/init by the most simple bash script in order to have the very minimum number of running processes. Obviously, no X, no USB, no detection of new hardware, no upgrade, no apt, "nothing", I just need a working console with a DHCP-based Wi-Fi IP address (ssid, passphrase are already stored in /etc/network/interfaces). That's all. Currently, I have tried this in replacement of /sbin/init:
#!/bin/sh
mount -o rw,remount /
mount -t proc none /proc
udevd --daemon
mkdir /run/network
ifup -a &
while [ 1 ]; do
/sbin/getty -8 115200 tty1 vt100
done
It's working as I'm getting an IP address and I can login but:
A) While running shutdown, I get "shutdown: Unable to shutdown system:"
B) control-c is not working in the console
C) After a login, I get: "bash: cannot set terminal process group (-1): Inappropriate ioctl for device"
D) After a login, I get: "bash: no job control in this shell"
Also, I have noticed that all the user-space processes have a "?" in the tty column when running ps avx. How can I fix those problems? I don't want to use upstart in order to really control what is started on the PC and have the very bare minimum.
I ended up using Busybox init. Great tiny init...
You could leverage runlevels and based on your question runlevel 3 is what you want to use.
If you have some services that you do not wish to start, you could turn them off too for that runlevel.
For booting into runlevel 3, you just append the boot argument to the kernel in your boot loader:
<EXISTING_BOOT_CMD> 3
If your distro uses systemd instead of sysvinit, they are instead called as targets. The equivalent of runlevel 3 in systemd is usually named as multi-user.target
The kernel boot argument you would need to pass in this case is systemd.unit=multi-user.target
<EXISTING_BOOT_CMD> systemd.unit=multi-user.target
An alternative, if you do not want to touch the boot loader:
systemctl enable multi-user.target

Resources