I need to execute a user space application every time a USB device is connected - c

I am working with an embedded platform. Typical software in this devices are Linux 2.6 + Busybox, so resources are limited.
I need to run an user space application every time a USB device is connected. I need to pass as parameter to this user space app the DeviceID and ProductID.
I don't really know which strategy should I follow to achieve this:
Writing a linux kernel module.
Doing it from inside the kernel (usb drivers) i'm currently doing this, but i dont think its the best way to do it
A user space app that 'polls' for usb connected devices.?
Which one should be the best way?
Thanks for your answer!

If you want to remain in user space, then you can use libudev.
You have an example here. You can extract product id and device id from this.

Even though other options like #aisbaa mentioned, modifying kernel is interesting and challenging one. I suggest you to modify the USB driver. Reason is, you need to send the arguments to the user space application(Product ID, Device ID).
These Ids will be obtained in driver. so calling user space app with these Ids are my choice.
For calling user space app nice explanation available here.

To the best of my knowledge, there is a mechanism for USB hot plugging in the kernel.
When a hot plug event happens, the user can be notified. Unfortunately, I am not very familiar with the details.
Maybe linux-3.3.5/samples/kobject/kset-example.c will give you some ideas.

Related

Get system date/time via USB

Is there any way to query the system's date/time via USB without installing anything on the host computer (maybe just drivers)?
Background of the original problem
To avoid the XY problem, let me explain a bit what I'm trying to do.
To be able to calculate a TOTP token for 2FA (e.g. like Google Authenticator app does) you need a real-time clock to get the date and time.
There's this USB device called SC4-HSM that I would like to use to calculate the tokens, however it doesn't have a clock and according to the designer, adding one would be too expensive (needs a battery, etc).
Possible solution to the original problem
This device is going to be used with a computer which already has an RTC of course. Thus I had the idea of querying the system for a date/time which would solve the issue.
(Note: I know that a USB device can be connected to all sorts of hosts and not all hosts will have an RTC, but since this only needs to work with a computer, I thought this shouldn't be an issue)
My first thought was that there might be some USB device class that had date/time needs, so I could register the device as that type and then I would be able to query the values.
After going through the device class codes list (Internet Archive) nothing jumped at me as needing date/time. The closest ones I could think of were:
Content Security (PDF)
Personal Healthcare
Smart Card Class (PDF)
I skimmed the device class documents in the USB Implementers Forum but there's nothing in there even remotely related to date or time.
Current problem
Since the USB specs seemed like a dead-end I thought that maybe there was a way to write a very simple USB driver that can be auto-loaded when the device is plugged in to a computer and then we can use the driver to return the date/time when the device asks for it (unless I'm misunderstanding something).
I am now looking through USB development docs like Michael Opdenacker's Linux USB drivers course, I tried the Linux USB Project which seems dead. Skimmed through Driver Development for Windows NT just to get an idea, however I am still not able to figure out if this is possible or not, and how hard it would be.
I'm a complete beginner at this and maybe this is something out of my skill level, but I would like to figure out if will I need weird hacks and workarounds or is there a much more straightforward way to do this?
There seems to be little information about it or I'm just searching the wrong places.
Any ideas/or pointers on either solving the original problem or the current one?
system time is not necessarily the general time i.e. the 'atomic' time you get from a NTP server
the most obvious solution is to use autorun, this is also possible on linux but normally autorun is blocked so the user explicitely has to activate it
https://askubuntu.com/questions/642511/how-to-autorun-files-and-scripts-in-ubuntu-when-inserting-a-usb-stick-like-autor
the linux command to get the time is date or hwclock or if the computer is connected to the net it may be possible to contact a NTP server (if the firewall does not block this)
then your autorun program has to send the data to the SC4-HSM. i do not know what USB classes the SC4-HSM implements if it implements CDC ACM (virtual COM port) this is easy:
Unable to sync computer time to Arduino via USB
(something like echo "T$(($(date +%s)+60*60*$TZ_adjust))" >/dev/tty.usbmodemfa131)
maybe it is possible to access system time over the USB drivers, i do not know this right now

Register virtual sound device from within application

I want to be able to process audio output of applications (VLC, Rhythmbox, ...) within my own one. Moreover, one should be able to select my application as the sink for the sound (e.g., in VLC or pavucontrol my application should appear as an output device).
How is this possible? Can it be done with ALSA, Pulseaudio, ...? Currently I am seeking for the easiest solution while later performant ones may become preferable. It would be great if most of the configuration could be done via API calls.
Thank you for your support!
I ended up writing a PulseAudio module. There one can create own sinks and directly get access to the audio stream. Have a look at my implementation here.

How to determine the last time the audio device was playing a file?

I would like to use C in order to get the last time the soundboard was playing a file. Is there a way I could do that?
None of the components you are using (tools, libraries, sound servers, drivers, kernel) logs the time when a sound is played.
If you are using one specific tool to play sounds, you could modify it to log the time.
Otherwise, you have to actively monitor the current status of the sound device.
(With ALSA, you could poll /proc/asound/card*/pcm*/sub*/status.)
I think it's not possible because of ALSA(Advanced Linux Sound Architecture) is just kernel component that provide device drivers for sound card.But i don't know if some user-space API's and library's like (alsa-ustils) can do that!,I advice may is better to check Sound-Player applications(VLC etc..) log ?!

Programming techniques/libraries for locating a user with a mobile device within a building [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 1 year ago.
Improve this question
I am looking to develop an application usable on the devices of visitors so that I can tell where they are. The initial idea I had, already done in some museums, is to have a number on specific locations within the museum, they call a phone number, enter that number, and get enhanced content, perhaps audio narration about that space. This is nicely device agnostic, though some WiFi only devices may not have a mechanism to call a phone number, but perhaps having folks on WiFi load a mobile website and choose their location (assuming the number of spots is not too many) from a list, and then click a button.
I'm new to this, and have been brainstorming on this, but I really don't know what I don't know. So here are my questions:
Can I use GPS indoors and get accurate information or will that basically not work in most buildings?
If I use a service like Skyhook Wireless, and use multiple WiFi routers, will I be able to locate the WiFi connected users accurately?
Can I use Bluetooth somehow? Setup for Bluetooth seems like a hassle, but maybe there's a Bluetooth mode that is simpler to use for an application like this?
For devices with a camera, what can I do with an image here. Photo of a QR Code? Are QR code reading libraries built into devices I can use, or would I need to find a library?
Are there are other techniques I might be able to use, maybe counting footsteps with the accelerometer somehow? Or using magnets somehow for devices with a compass?
Suggestions welcome, assume I'm trying to target as many smartphones as practical (Android, iOS, Blackberry, webOS, Windows Phone 7) that are popular in North America. If there is a way to also include devices that are not considered smartphones, that would be great too.
UPDATE
Why indoor navigation is so hard: Your phone can get you to the museum, but it can't guide you to the T-Rex by Nick Farina
UPDATE 2
Brooklyn Museum experiment with QR Codes: too soon to
tell/negative
Very few people can successfully scan and use a QR code
UPDATE 3
Think GPS is cool? IPS will blow your mind
QR codes are your best bet. They're cheap and, for example, in a museum you could put one next to every painting. For devices without a camera, you could choose to print a location-code next to the QR that they could enter manually in an app. Here's another solution as well:
Set up a wifi repeater/booster in each location you want to determine (ex: in a museum, each room) and record the SSID for each router. As the user walks from each room to the next, their device will automatically switch to the repeater with the highest quality signal (the one in that room). Simply have your application test for which SSID the user is connected to.
GPS is not accurate enough and may not work inside buildings and Skyhook wireless is only for one address. Having users call a number and/or go to a website to manually select a location is too many steps for the user, and the user may not have reception to place a call. Bluetooth is a huge hassle.
Can I use GPS indoors and get accurate
information or will that basically not
work in most buildings?
no, GPS needs a clear view of the sky. regardless, the accuracy of GPS is around +/- 50 feet and can be worse.
If I use a service like Skyhook
Wireless, and use multiple WiFi
routers, will I be able to locate the
WiFi connected users accurately?
aGPS is less accurate than GPS.
Can I use Bluetooth somehow?
bluetooth is not location aware.
android 2.3 introduces something called near-field communications. this would theoretically allow the person to wave their phone over a receiver and transmit information. it's quite new and i think the nexus s is the only device with hardware support for this. at best you are looking at some early adopter pain for that one.
Photo of a QR Code? Are QR code
reading libraries built into devices I
can use, or would I need to find a
library?
that's a good idea. QR codes are popular because they are simple. libraries are not part of the SDK but they exist. it would be hard to build one yourself if it came down to that. by the same token, you could just as the user to enter a simple location code. essentially the same thing and might be simpler than aligning the camera for a QR read.
it does depend on your application. if you want this to automatically locate everyone without an user intervention, QR codes / codes won't work because the user has to take some action. even NFC, from what i've read, operates on very short distances so the person would need to wave the phone across something closely not just walk by.
If you're talking indoors, I think you have some great ideas. GPS may be spotty depending on the building, and Wi-Fi triangulation only works if your signal is proportional to the proximity of the user to the unit. Here is my suggestion:
QR Codes sounds like the best option here if you don't mind having them all over the place. If someone can scan a QR code, (which they are on most all platforms, it's just making sure your users have a decoder installed and can actually use it), then you have three things going for you:
You know they have to be in range to scan it...so that's far more accurate than you can get with other location devices.
you can use the QR code to embed a link to a download site or embed the actual information into the QR code if it's not too much.
Most smartphones today have QR code scanner apps built in, and there are libraries (java and .net) that you could use to build your own app.
The downside here of course is that you're assuming the cameras are good enough quality and that someone isn't going to deface your QR code so that subsequent visitors can't read it.
Keep in mind the bump application. They don't use bluetooth or any form of wireless technique to determine if two people are near each other. http://bu.mp/faq
Since you have 1 building that you care about. Why not simply have a root device or network of devices in your building that you care about. This root device would communicate with all the clients to do your bidding.
Maybe cellphones can´t solve your problem, have you considered cameras on each room and a QR code tag on each guest? you might get the images from the security camera since they are infrared. Another way of achieving this is by locating the camera in front of the door so that the guests are always seen by the QR code algorithm.
Cellphones are just too diverse to implement this, have you thought that the guests might not have a cellphone at all?

Ethernet MAC address as activation code for an appliance?

Let's suppose you deploy a network-attached appliances (small form factor PCs) in the field. You want to allow these to call home after being powered on, then be identified and activated by end users.
Our current plan involves the user entering the MAC address into an activation page on our web site. Later our software (running on the box) will read the address from the interface and transmit this in a "call home" packet. If it matches, the server response with customer information and the box is activated.
We like this approach because it's easy to access, and usually printed on external labels (FCC requirement?).
Any problems to watch out for? (The hardware in use is small form factor so all NICs, etc are embedded and would be very hard to change. Customers don't normally have direct acccess to the OS in any way).
I know Microsoft does some crazy fuzzy-hashing function for Windows activation using PCI device IDs, memory size, etc. But that seems overkill for our needs.
--
#Neall Basically, calling into our server, for purposes of this discussion you could call us the manufacturer.
Neall is correct, we're just using the address as a constant. We will read it and transmit it within another packet (let's say HTTP POST), not depending on getting it somehow from Ethernet frames.
I don't think that the well-known spoofability of MAC addresses is an issue in this case. I think tweakt is just wanting to use them for initial identification. The device can read its own MAC address, and the installer can (as long as it's printed on a label) read the same number and know, "OK - this is the box that I put at location A."
tweakt - would these boxes be calling into the manufacturer's server, or the server of the company/person using them (or are those the same thing in this case)?
I don't think there's anything magic about what you're doing here - couldn't what you're doing be described as:
"At production we burn a unique number into each of our devices which is both readable by the end user (it's on the label) and accessible to the internal processor. Our users have to enter this number into our website along with their credit-card details, and the box subsequently contacts to the website for permission to operate"
"Coincidentally we also use this number as the MAC address for network packets as we have to uniquely assign that during production anyway, so it saved us duplicating this bit of work"
I would say the two obvious hazards are:
People hack around with your device and change this address to one which someone else has already activated. Whether this is likely to happen depends on some relationship between how hard it is and how expensive whatever they get to steal is. You might want to think about how easily they can take a firmware upgrade file and get the code out of it.
Someone uses a combination of firewall/router rules and a bit of custom software to generate a server which replicates the operation of your 'auth server' and grants permission to the device to proceed. You could make this harder with some combination of hashing/PKE as part of the protocol.
As ever, some tedious, expensive one-off hack is largely irrelevant, what you don't want is a class-break which can be distributed over the internet to every thieving dweep.
The MAC address is as unique as a serial number printed on a manual/sticker.
Microsoft does hashing to prevent MAC address spoofing, and to allow a bit more privacy.
With the only MAC approach, you can easily match a device to a customer by only being in the same subnet. The hash prevents that, by being opaque to what criteria are used and no way to reverse engineer individual parts.
(see password hashing)
From a security perspective, I know that it is possible to spoof a MAC, though I am not entirely sure how difficult it is or what it entails.
Otherwise, if the customers don't have easy access to the hardware or the OS, you should be fairly safe doing this... probably best to put a warning sticker on saying that messing with anything will disrupt communication to the server.

Resources