# Proper implementation of USB detection on Legato

**URL:** <https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018>\
**Category:** Legato Application Framework\
**Created:** [January 14, 2019, 3:15pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018 "2019-01-14T15:15:09Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazzox](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@lazzox](https://forum.legato.io/u/lazzox)\
**Post date:** [January 14, 2019, 3:15pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/1 "2019-01-14T15:15:09Z")

</div>

Hello,

My idea is to detect when a USB flash memory is connected and to copy logs (from logread) to it.  
For now I have two issues:

- Detection of USB flash memory

- Copying log to USB flash memory

I tried making an app that would poll if /dev/sda exists, but that would require access to /dev/sda file trough sandbox which isn’t possible to put into adef because it only exists when USB is mouted.

Questions:

1. What would be the right way to do this?
2. Would it be possible without editing existing linux kernel?
3. Does Legato provide a way to add udev rules like on Ubuntu?

Thanks in advance.

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [January 17, 2019, 1:45pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/2 "2019-01-17T13:45:37Z")

</div>

How about writing a script to check if the /dev/sda exists?  
After that you can use logread in the script to copy file to usb drive.

---

<div class="post-metadata">

**Author:** ![lazzox](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@lazzox](https://forum.legato.io/u/lazzox)\
**Post date:** [January 17, 2019, 1:59pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/3 "2019-01-17T13:59:13Z")

</div>

In order to be able to check if /dev/sda exists I need to have permission to read /dev/sda file.  
In order to do so I have to define the in .adef file of my application. When the application is started if /dev/sda isn’t present application won’t be able to start.

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [January 17, 2019, 2:11pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/4 "2019-01-17T14:11:43Z")

</div>

Yoy can use a linux a script to check or you can disable the sandbox feature.

---

<div class="post-metadata">

**Author:** ![lazzox](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@lazzox](https://forum.legato.io/u/lazzox)\
**Post date:** [January 17, 2019, 2:21pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/5 "2019-01-17T14:21:45Z")

</div>

As I understood disabling sandbox is a security risk?  
And if i would do it trough a linux script how would I make it run at startup without modifying kernel?

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [January 17, 2019, 2:46pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/6 "2019-01-17T14:46:54Z")

</div>

You can modify those init script in/etc/init.d to run your script.

---

<div class="post-metadata">

**Author:** ![lazzox](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@lazzox](https://forum.legato.io/u/lazzox)\
**Post date:** [January 17, 2019, 2:56pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/7 "2019-01-17T14:56:36Z")

</div>

Thank you for the hints, I would know how to do this with regular linux distribution and udev/init.d combination. However, we want to use SierraWireless provided SPK file without modifications of kernel and rootfs and have only access to legato framework. Can you help?

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [January 18, 2019, 1:37am UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/8 "2019-01-18T01:37:45Z")

</div>

I have no idea on udev. But I saw the link below saying we can use mdev instead. You can have a try.

> **[Using udev to control ports enumeration](https://forum.mangoh.io/t/using-udev-to-control-ports-enumeration/374)**
>
> Hi, I’m using an iot card with an ftdi chip to do some serial sending/ receiving. There is a problem with the ftdi chip where it resets and re-enumerates. when this happens, it re-enumerates to a different device name - my application code opens...

> **[USB driver failing to find or power USBs](https://forum.mangoh.io/t/usb-driver-failing-to-find-or-power-usbs/1033/19)**
>
> so… just to update everyone on this subject… after adding the magic udev rule to our build it seems that running i2cgpioctl enter “100” load setup defaults, enter “0” to exit does bring up the usb iot cards without having to reslot them, then simply...

> **[Where to add an ttyUSBx unbind rule](https://forum.mangoh.io/t/where-to-add-an-ttyusbx-unbind-rule/1445)**
>
> Hi, I connected an FT4232H mini module to the USB host port of my mangOH Green board, and the kernel automatically created 4 extra tty devices (ttyUSB1, ttyUSB2, ttyUSB3 and ttyUSB4). I want to unbind my ttyUSBx devices to unload the VCP driver,...

BTW, if you only want to use legato application, you need to first write a shell script to periodically checking if the new USB storage has been inserted (/dev/sda).  
Also this script will mount the USB drive and write the logread to USB drive in case /dev/sda is found.  
e.g.

* * *

/bin/mount -t auto -o sync /dev/sda1 "/tmp/usbflash”  
logread \> /tmp/usbflash  
umount /tmp/usbflash

* * *

After that you need to write a unsandboxed application which using system() API to call this script during when legato system starts.

---

<div class="post-metadata">

**Author:** ![lazzox](https://avatars.discourse-cdn.com/v4/letter/l/b9bd4f/32.png) [@lazzox](https://forum.legato.io/u/lazzox)\
**Post date:** [January 21, 2019, 9:55am UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/9 "2019-01-21T09:55:08Z")

</div>

_Here is what I did at the end:_

_I have modified /mnt/flash/ufs/etc/mdev.conf by adding line:_

_sd[a-z][0-9]? 0:0 660 @(echo $MDEV \> /tmp/usb\_det; /legato/systems/current/bin/app start Log2USB)_

_This line instructs mdev mechanism to save detected disk device name to temp file (e.g. sda1) and to start unsandboxed legato application Log2USB when USB disk is inserted. Log2USB application executes shell script which mounts, copies logs and unmounts disk._

_Only drawback for this solution is that /etc/mdev.conf file must be edited which is sometimes not handy (e.g. spk update in the field)._

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [January 21, 2019, 1:42pm UTC](https://forum.legato.io/t/proper-implementation-of-usb-detection-on-legato/4018/10 "2019-01-21T13:42:13Z")

</div>

Can you write an unsandboxed application to modify the mdev.conf filr?
