How the function works cdev_add()? - linux

Do I understand correctly that when the structure is initialized
struct dev_t dev;
dev = MKDEV(major,minor_first);
I create only the device file, it's right to say - to the node. Next, should I indicate how I will work with this device? To do this, you need the function
cdev_add(&my_ch_dev, dev, minor_count);
after
cdev_init(&my_ch_dev ,&dev_fops);
So, I mean that my driver will work with the created node as a character device? Thanks in advance!

Here is the details how it works
dev = MKDEV(major,minor_first);
Still kernel doesn't know whether we selected major/minor number or not, so for this you need to register he device by calling register_chrdev_region()
register_chrdev_region(dev,minor_count,"AYRAT_DEVICE"); so till now number(major/minor) has reserved the name(dev) so that other driver will not get the same name. Next you need to register your driver with kernel.
register with cdev by calling cdev_init(&my_ch_dev ,&dev_fops); Next you need to inform to kernel that we filled all member of struct cdev, so for this use cdev_add().
cdev_add(&my_ch_dev, dev, minor_count);

Related

zbus connection not displayed in busctl list

I'm using the zbus crate to make a server able to get events from the dbus.
It works well my code (basically the same as the example from the documentation) is able to receive events so it's fine.
I use busctl to send an event like in the example:
busctl --user call org.zbus.MyGreeter /org/zbus/MyGreeter org.zbus.MyGreeter1 SayHello s "Maria"
And my code is able to receive the event with the parameter just fine.
The thing is I'm having some issues with udev and while I was trying to fix it I found some weird things:
If I send an event with another user it fails with Call failed: the name org.zbus.MyGreeter was not provided by any .service files while my program is running
When I do busctl list --acquired I don't see org.zbus.MyGreeter in the result
My question is: is it normal my program does not appear in the busctl list? Am I doing something wrong or do I use the wrong library to do what I want to do?
Ok it seems there are 2 busses and I was not using the system bus.
I had to replace the method session to system to indicates I want my program to run on the system bus.
Like this:
let _ = ConnectionBuilder::system()?
.name("org.zbus.MyGreeter")?
.serve_at("/org/zbus/MyGreeter", greeter)?
.build()
.await?;
Doing this is not enough because my program does not have to permission to create a service on the bus system. So I had to create a file in /usr/share/dbus-1/system.d where I did write the configuration needed.

libudev udev_device_get_sysattr_value() return NULL for idVendor/idProduct when device is removed

I'm trying to implement USB monitor service/daemon to detect specfic PID/VID device only. I can easily detect device 'add' action attrib by filtering PID/VID but when I remove ANY USB device from port then as part of 'remove' action attrib, I try to filter PID/VID same like in 'add' action ... by using
const char* vendor = udev_device_get_sysattr_value(dev, "idVendor");
const char* product = udev_device_get_sysattr_value(dev, "idProduct");
but in above two char ptr I always get NULL.
Due to this my remove action will execute for all USB device which will remove from port
Is their any other way in libudev API to detect specific device remove only ?
Once the USB device is at the "remove" action, udev_device_get_sysattr_value(dev) will return NULL in my experience. Even the prior "unbind" action is too late.
One way to detect the removal of a specific device is to store its dev node when it is inserted.
First, find the udev_device* you want to monitor by enumerating the devices (struct udev_enumerate*)(device already plugged in) and monitoring the "add" or "bind" action.
In those states, the udev_device_get_sysattr_value will return the correct strings as you have already seen. If the values match the ones for the device you want to monitor, retrieve its current dev node by calling udev_device_get_devnode(dev). Store that dev node value (strdup to make a copy of it).
In the "unbind" or "remove" action, udev_device_get_devnode(dev) will still return the proper value as opposed to udev_device_get_sysattr_value(dev, prop_name). Match the dev_node with the previously stored dev node to identify that the action applies to the device you are monitoring.
Note that the dev node will likely change every time the device is inserted.

Get /dev path of USB device from VID and PID in node.js

I'm writing a node.js JavaScript app to run on a Mac (macOS server) that will communicate with USB devices that are plugged in. The library I need to use to interact with the device takes a path in the form /dev/pathToDevice - e.g. lib.connect('/dev/pathToDevice'); - whereas the only way I have to identify the USB device from within my code is by the vendorId (VID) and productId (PID) obtained via the usb-detection library from NPM on insertion.
How can I, in JavaScript, identify or derive the path to the device from the VID and PID of the inserted USB device, so I can pass that to the library?
You could write a small C++ program that uses libusbp_generic_interface_get_os_filename function from libusbp. You would then call the C program from node.js. Let me know if you need help writing the C++ program. I would probably start with the port_name example form libusbp's source tree.
Or, you could use the lsusb example from libusbp as is. It returns a path like /sys/devices/pci0000:00/0000:00:06.0/usb1/1-1 for each device. That is a directory with a bunch of special files; one of them is busnum and one of them is devnum, which are the two numbers you need to construct a path like /dev/bus/usb/DEVNUM/BUSNUM.
If that works, you might prefer to write a binding for libusbp from node.js instead of using a C executable.
Try drivelist , It gives you the mountpaths and its cross platform and you can combine this with usb-detection easily.
var drivelist = require("drivelist")
drivelist.list((error, devices)=> { console.log(devices) })
//Output
[ {
//stuff...
device: '/dev/disk0',
raw: '/dev/rdisk0',
mountpoints: [
{"path":"/","label":"Macintosh HD"},
{"path":"/private/var/vm","label":"VM"}
],
isRemovable: false,
//stuff....
]

CDROM function - where does the function pointer go to?

I am trying to debug an issue where the standard linux CDROMEJECT returns an error even though the disk ejected.
Standard eject command used.
ioctl(FP,CDROMEJECT)
When I look at the cdrom.c I can see the eject function and the errors (e.g: http://lxr.free-electrons.com/source/drivers/cdrom/cdrom.c#L2303). I am fine with where error codes are returned but you also have lines like this:
cdi->ops->lock_door(cdi, 0)
Now lock_door is a function pointer (see header file here). My issue is I cannot for the life-of-me figure out which function the lock_door pointer points to! I would expect this to point to a function that then sends a SCSI command to the CD/DVD drive.
I suspect I am missing some fundamental but really not sure what! Any idea where I find where the function pointers in struct cdrom_device_ops point to?
If you do an LXR "Freetext Search" for ".lock_door"
you can see
it being set for the scsi driver here
and if you click on sr_lock_door
you find it defined in sr_ioctl.c:
int sr_lock_door(struct cdrom_device_info *cdi, int lock) {
Scsi_CD *cd = cdi->handle;
return scsi_set_medium_removal(cd->device, lock ?
SCSI_REMOVAL_PREVENT : SCSI_REMOVAL_ALLOW);
}

Arduino - How to read a string from the Serial Port

I just recently started working with Arduino. I just have a quick question, I tried searching for an answer but have failed for days. Basically what I wanna ask is if there is a way to read a whole line from the Serial Port. Like the line highlighted in the picture below.
What I'm trying to do is using a Bluesmirf Silver Rn-42 to search the area for a bluetooth device and trigger a signal if a matching address is found. I just cant figure out how to read messages that are already on the Serial port.
Use .readString()
Example code:
String myString;
void setup()
{
Serial.begin(9600);
}
void loop()
{
while (Serial.available())
{
myString = Serial.readString();
//do stuff with the string
}
}
If you want to read something that's already in the serial port from the Arduino end, then you need to rethink your code. Anything you produce within your code to print to the serial monitor will already be in your program ready to access if you make it available in the right way. The exemplar string you provided, is simply an array of characters that you can store in an element within an array, making it accessible whenever you need it.
Hints:
Never read back from the serial monitor, it's really slow -.-
Make all the resources you require accessible and available in memory at the time you need it to save hassel & processing power.
Never make the same mistake twice.
However, if you want to read from the COM port that the Arduino is connected to in Windows, then you'll need to work with Libusb libraries found here: http://www.libusb.org/ for C. Any other language will be library or import dependent.

Resources