socketcan alternative / add can0 to /dev/ - linux

I am currently working with CAN-BUS Systems.
I make the CAN Interface available using
sudo modprobe can
sudo modprobe can-raw
sudo modprobe mttcan
sudo ip link set can0 type can bitrate 250000
sudo ip link set up can0
It now shows up in ifconfig and works fine in my python code.
Since I am working with aws greengrass I need the device to show up in /dev/ in order to access it. Is there any way to use something different than socketcan? If so, how do I use it?
I have no idea how linux handles can interfaces.

Related

PWM using bcm2835 library as non-root in Raspberry Pi

I need to control some DC motors using PWM. I have been trying to use the bcm2835 library, but I need root access to run my program. I need to avoid this, as this program is part of a ROS package, which gives problems if run as root. I have tried to follow the instructions that appear in the section "Running as root", but I haven't been successful. I have done the following.
First, I have installed libcap2 and libcap-dev and added my user to the kmem group.
sudo apt-get install libcap2 libcap-dev
sudo adduser ubuntu kmem
Then, I have installed the version latest version of bcm2835 library.
wget http://www.airspayce.com/mikem/bcm2835/bcm2835-1.67.tar.gz
tar zxvf bcm2835-1.67.tar.gz
cd bcm2835-1.67
./configure
make
sudo make check
sudo make install
After that, I uncommented the #define BCM2835_HAVE_LIBCAP line in bcm2835.h, which I found in /usr/local/include/.
I allowed write access to /dev/mem by members of kmem group.
echo 'SUBSYSTEM=="mem", KERNEL=="mem", GROUP="kmem", MODE="0660"' | sudo tee /etc/udev/rules.d/98-mem.rules
sudo reboot
And finally, I compiled my program (called l298n) and run the following command.
sudo setcap cap_sys_rawio+ep l298n
After doing all this, my program is still not able to provide PWM control unless I run it as root.
Have I done anything wrong? Is there any other way around this? As far as I know, other libraries would run into the same problem, as it depends on the access level of /dev/mem.
I am running Ubuntu 20.04 LTS in a Raspberry Pi 3B+.
What you did wrong:
You edited the wrong bcm2835.h, you need to edit bcm2835.h where ever you unpacked bcm2835-1.67.tar.gz, not /usr/local/include.
You didn't recompile the library to actually have the added feature in it.
Also, if you're just accessing the GPIO pins and don't need non-root access to /dev/mem then there's no need to do anything more than adding access rights to /dev/gpiomem to your non-root user.
Have you tried version 68 already ?
There where made some changings concerning 'BCM2835_HAVE_LIBCAP'.

How to set up the baud rate for i2c-bus in Linux?

I have an ARM-computer which is based on the riko-3288 processor.
I also have the Ubuntu image for the device with a custom kernel (without the module system).
I am currently trying to set a baud rate of up to 100KHz for the i2c-bus.
I'm not an expert in Linux and I don't know what I have to do for it.
The i2c-bus works with higher baud rate, I think it's more than 1MHz.
I've read that I must write a command like this "dtparam=i2c_arm=on,i2c_arm=on_baudrate=100000" in the boot-loading file.
My boot-loading file bellow:
{boot-partition}/extlinux/extlinux.conf
label kernel-4.4
kernel /zImage
fdt /rk3288-miniarm.dtb
append earlyprintk console=tty1 console=ttyS2,115200n8 rw root=/dev/mmcblk1p7 rootfstype=ext4 init=/sbin/init
I tried past the command to the append string and I tried some different commands for it, but nothing works.
Could you tell me how I can change the baud rate for the i2c-bus?
P.S. And does someone know how to get the baud rate in the operation system?
I found the easy solution here.
http://www.chip-community.org/index.php/Troubleshooting#I2C_.2F_TWI_problems
For my cause:
sudo apt update
sudo apt install device-tree-compiler
sudo mount /dev/mmcblk1p6 /mnt # {boot-partition}
sudo cp /mnt/rk3288-miniarm.dtb /boot/rk3288-miniarm.dtb.bak
sudo fdtput --type u /mnt/rk3288-miniarm.dtb i2c4 clock-frequency 50000
sudo systemctl reboot

Raspberry Pi 2, how to disable device_tree to enable SPI?

I have a Raspberry Pi 2, Model B, and I'd like to use the SPI to talk to another Pi (for later using it for more SPI-devices). I'd also like to use WiringPi for C-coding my program.
I thought I would ensure I had everything I needed, so i ran a full upgrade:
sudo apt-get update && sudo apt-get upgrade -y
I installedWiringPi, and enabling SPI to be loaded from raspi-config, but I still can't do a gpio load SPI without the following error:
gpio: Unable to load/unload modules as this Pi has the device tree enabled.
You need to run the raspi-config program (as root) and select the
modules (SPI or I2C) that you wish to load/unload there and reboot.
There is more information here:
https://www.raspberrypi.org/forums/viewtopic.php?f=28&t=97314
I then tried what the link suggested:
... add "device_tree=" to your config.txt and reboot.
$ nano sudo /boot/config.txt
# Go to last line
# Add 'device_tree='
# Save, and exit
$ sudo reboot
Then I tried gpio load spi, but I still get the same error.
SPI is enabled in /boot/config.txt:
$ cat /boot/config.txt | grep spi
dtparam=spi=on
Have any of you suggestions to, what I can try to enable my SPI?
According to this thread,gpio load spi is deprecated.

Programmatically connecting to a wifi network in Linux

There are multiple questions regarding this functionality for Android or iOS, but I am attempting to do this on Linux (OpenWrt 15.05 to be specific). I want to scan for wireless networks, present the list to the user, have them select one and send the username/password to the network.
I wrote a quick C program using iw_scan from the wireless_tools package so I can see the wireless networks, but I am having issues trying to determine how to connect to a wireless network. iwconfig from the same package just appears to modify an already existing connection.
I have spent a few hours surfing the web thinking there has got to be a package that already does this since it is such a common use case. Anyone know of one?
Thanks.
wpa_supplicant
Take a look at wpa_supplicant package. It includes wpa_supplicant daemon that can be controlled via wpa_cli and wpa_gui utilities or D-Bus API.
Programs like wicd and Network Manager uses wpa_supplicant internally.
NetworkManager
You can also use D-Bus API or command line interface (nmcli) for NetworkManager. It provides a higher level abstraction than wpa_supplicant does.
I've tried number of solutions to this, from wicd, to network manager's cli interface nmcli, to wifi in python. I didn't want a UI, I didn't want network manager, and the python wifi project was too complicated and I faced some problems with it. So I wrote a short, simple bash script for connecting to wpa and open networks (WEP not supported), a wrapper on wpa_passphrase and wpa_supplicant:
sudo apt-get install -y wireless-tools wpasupplicant expect macchanger
sudo wget -O /usr/local/bin/wifi-connect https://raw.githubusercontent.com/erjoalgo/erjoalgo-gnu-scripts/master/wifi-connect
sudo chmod +x /usr/local/bin/wifi-connect
wifi-connect -e Internet -p my-password
wifi-connect -h
The wpa-passphrase files (including plaintext passphrases) are persisted in $HOME/.config/wifi-connect, and if no ESSID is provided, it will automatically connect to any known available network. It supports changing the iface mac address via macchanger.

How to install Wireshark on Linux and capture USB traffic?

I have encountered numerous problems in the installation of Wireshark, and the capture of USB traffic, especially due to user permissions.
How to install Wireshark on Linux and capture USB traffic?
Tested on Ubuntu 14.04, but probably works on other distributions since none of the steps are specific to Ubuntu.
The first time you follow the tutorial, do all the steps 1 -> 7.
When you restart your computer, you have to repeat steps 6 and 7 to see the USB interfaces in Wireshark.
Install Wireshark and libpcap:
sudo apt-get install wireshark libpcap0.8
For Debian, Ubuntu and other Debian derivatives, continue to step 3.
For other Linux based systems or other installation methods, see the Wireshark Wiki, then go to step 6.
Reconfigure wireshark to allow non-superusers to track packets:
sudo dpkg-reconfigure wireshark-common
Select <Yes> in the prompt
Add your username to the "wireshark" usergroup:
sudo usermod -a -G wireshark <your_username>
You can verify if it’s done correctly by displaying the groups your username is part of:
groups <your_username>
If not, you can add the group "wireshark" manually:
groupadd wireshark
And then add your username to the group (see above)
Important: Logout of your session, then log back in.
This step depends on the kernel version that is installed on your machine. To know the version of your kernel, type:
uname -r
For versions of the kernel prior to 2.6.21, if debugfs is not already mounted on /sys/kernel/debug, ensure that it is mounted there by issuing the following command:
sudo mount -t debugfs / /sys/kernel/debug
For kernel version 2.6.21 and later, load the loadable module usbmon in the Kernel:
`sudo modprobe usbmon`
See [Wireshark Wiki](https://wiki.wireshark.org/CaptureSetup/USB#Linux) for more information about this differentiation.
If the usbmon interfaces don't appear in Wireshark, look for interfaces using dumpcap (the command-line tool of Wireshark):
sudo dumpcap -D
You should see the usbmon* interfaces. Now display the permissions of the usbmon interfaces:
ls -l /dev/usbmon*
If the usbmon* files have 'crw-------', then it's normal that Wireshark cannot read them because it's not run as root. Do not execute wireshark in root mode, it may damage files. Instead, you can give it regular users privileges :
sudo setfacl -m u:$USER:r /dev/usbmon*
Now the usbmon interfaces should appear in Wireshark.
Sources:
https://wiki.wireshark.org/CaptureSetup/USB#Linux
https://wiki.wireshark.org/CaptureSetup/CapturePrivileges#Most_UNIXes
https://unix.stackexchange.com/questions/55722/wireshark-couldnt-run-usr-sbin-dumpcap-in-child-process
http://anonscm.debian.org/viewvc/collab-maint/ext-maint/wireshark/trunk/debian/README.Debian?view=markup

Resources