Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

hwdetector #7

Open
wants to merge 11 commits into
base: Kyryl.Krynychnyi
Choose a base branch
from
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,8 @@ GlobalLogic Kharkiv, 2020

This repository is aimed for sharing our exercises and your solutions in scope of the course.

Homeworks:
- hwdetect - detect attached hardware using user level script file


Please refer to wiki for details.
10 changes: 10 additions & 0 deletions hwdetect/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
# Home work hot plugged hardware detector

Create the script 'hwdetect.sh' which detects connected hardware.
For monitor hardware from user level please use /dev folder,
output from lsusb and i2detect (optional).
Most interesting devices are:
- USB to TTL convertors,
- flash drives,
- SD cards,
- i2c devices.
37 changes: 37 additions & 0 deletions hwdetect/example.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Hardware that is detected from start

USB to ttl:
ttyUSB0

USB flash drives:
/dev/sda

I2C devices:
i2c-3 unknown i915 gmbus dpd N/A
i2c-1 unknown i915 gmbus dpc N/A
i2c-4 unknown DPDDC-A N/A
i2c-2 unknown i915 gmbus dpb N/A
i2c-0 unknown SMBus I801 adapter at f040 N/A
i2c-5 unknown DPDDC-C N/A

SD cards:
mmcblk0

Active detector
<disconnected<
ttyUSB0

>connected>
ttyUSB0

<disconnected<
mmcblk0

>connected>
mmcblk0

>connected>
/dev/sdb

<disconnected<
/dev/sdb
99 changes: 99 additions & 0 deletions hwdetect/hwdetect.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#!/bin/bash

usb0=$(mktemp)
flash0=$(mktemp)
sd0=$(mktemp)
i2c0=$(mktemp)

usb1=$(mktemp)
flash1=$(mktemp)
sd1=$(mktemp)
i2c1=$(mktemp)

echo -e "Hardware that is detected from start\n"
echo "USB to ttl:"
ls /dev | grep ttyUSB* > $usb0
if [[ -n $(cat $usb0) ]]; then
cat $usb0
else
echo "none"
fi
echo

echo "USB flash drives:"
ls /dev/sd[a-z] > $flash0
if [[ -n $(cat $flash0) ]]; then
cat $flash0
else
echo "none"
fi
echo

echo "I2C devices: "
i2cdetect -l > $i2c0
if [[ -n $(cat $i2c0) ]]; then
cat $i2c0
else
echo "none"
fi
echo

echo "SD cards: "
ls /dev | grep -E "mmcblk[0-9]+$" > $sd0
if [[ -n $(cat $sd0) ]]; then
cat $sd0
else
echo "none"
fi
echo

trap 'rm -f $usb0 $usb1 $flash0 $flash1 $i2c0 $i2c1 $sd0 $sd1 && echo -e "\n***********Detection ended***********" && exit' 2 3 15
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This line is too long. Is it possible to fit it to 80 columns by adding some variable like ALL_TMP_FILES or/and MSG_TEXT?


echo "Active detector"
while true; do
ls /dev | grep ttyUSB* > $usb1
usb_to_ttl=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$'<disconnected< %l\n' --unchanged-group-format='' $usb0 $usb1 )

ls /dev/sd[a-z] > $flash1
flash_drives=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$'<disconnected< %l\n' --unchanged-group-format='' $flash0 $flash1)

i2cdetect -l > $i2c1
i2c_devices=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$'<disconnected< %l\n' --unchanged-group-format='' $i2c0 $i2c1)

ls /dev | grep -E "mmcblk[0-9]+$" > $sd1
sd_cards=$(diff --new-line-format=$'>connected> %l\n' --old-line-format=$'<disconnected< %l\n' --unchanged-group-format='' $sd0 $sd1)

if [[ -n "$usb_to_ttl" ]]; then
for i in $(echo "$usb_to_ttl"); do
echo "$i"
done
echo
cat $usb1 > $usb0
fi

if [[ -n "$flash_drives" ]]; then
for j in $(echo "$flash_drives"); do
echo "$j"
done
echo
cat $flash1 > $flash0 | 2>/dev/null
fi

if [[ -n "$i2c_devices" ]]; then
for k in $(echo "$i2c_devices"); do
echo "$k"
done
echo
cat $i2c1 > $i2c0
fi

if [[ -n "$sd_cards" ]]; then
for l in $(echo "$sd_cards"); do
echo "$l"
done
echo
cat $sd1 > $sd0
fi

sleep 1
done