Universal Serial Bus (USB) Usage in the app

I. A sample app for PAAS type requiring python. (for USB storage)

package.yaml

descriptor-schema-version: 2.5

info:
  name: PyNetTest
  description: "Python app to access a usb storage device"
  version: "1.5"
  author-link: "http://www.cisco.com"
  author-name: "Cisco Systems"

app:
  # Indicate app type (vm, paas, lxc etc.,)
  type: paas

  resources:
    profile: c1.small

    network:
      -
        interface-name: eth0
        ports:
            tcp: [9000]
            udp: [10000]

    devices:
      -
        type: usbdev
        label: HOST_DEV1
        usage: Needs usb storage device to persist data
        function: storage
        mandatory: True
        mount-point: /extra/storage/
        device-directory-to-mount: /usbdata
        vendorID: "0451"
        productID: "8027"        

# Specify runtime and startup
  startup:
    runtime: python
    runtime-version: 2.7
    target: main.py

activation_storage.json

{
    "resources": {
        "profile": "custom",
        "cpu": "25",
        "memory": "50",
        "disk": "100",
        "network": [{"interface-name": "eth0", "network-name": "iox-bridge0"}],
        "devices": [{"type": "usbdev", "label": "HOST_DEV1", "device-id": "/dev/bus/usb/001/030", "vid":"0451", "pid":"8027"}]
    }
}

Send the activation payload to CAF through one of the below procedure.

  • By selecting the USB device listed on LM in the app
  • Sending the payload activation_storage.json through ioxclient (modifying the device-id for the same type as specified from the output from the command ioxclient platform device lists )
ioxclient platform device lists
 output

Currently active profile :  829now
Command Name: plt-device-list
Saving current configuration
-------------Device Info----------------
{
 "ds_dev_ids": {
...........
 },
 "serial": [
.......
 ],
 "usbdev": [
  {
   "available": false,
   "bus": "1",
   "dev": "30",
   "device_class": "00\n",
   "device_id": "/dev/bus/usb/001/030",
   "device_name": null,
   "device_path": "/sys/devices/pci0000:00/0000:00:09.0/usb1/1-1",
   "generic_params": null,
   "is_generic": false,
   "is_storage": true,
   "pid": "1234",
   "port": null,
   "storage_params": {
    "device_name": "/dev/sdc1",
    "fstype": "vfat",
    "model": "UDisk",
    "mount_point": None
    "vendor": "General"
   },
   "type": "usbdev",
   "used_by": None,
   "vid": "abcd"
  }
 ],
 "usbport": [
.....
]
}

.....

Specifying the same device-id in activation_storage.json and passing that as payload if using ioxclient.

ioxclient application activate <appname> --payload activation_storage.json

Main.py

import os

def write_to_file(filepath, data):
    #Writing to file under filepath, with data
    with open(filepath, 'w') as fp:
        fp.write(data)


def write_to_usb():
    #Assuming the usb mount point is stored at the location HOST_DEV1
    try:
        usb_path = os.getenv("HOST_DEV1")
        if usb_path is None:
            print "No usb path specified in HOST_DEV1"
            return 0

        print "Writing to HOST_DEV1:",os.getenv("HOST_DEV1")
        new_dir = os.path.join(usb_path, "ioxusbdata")
        os.mkdir(new_dir)
        filename = "paas_app.txt"
        filepath = os.path.join(new_dir,filename)

        i = 0
        for i in range(5):
            str1 =  (time.strftime("Date :%d/%m/%Y, Time : %H:%M:%S"))
            write_to_file(filepath, str1+"\n")
            time.sleep(1)

def main():
  try:

    while True:
        # print "Main thread sleeping for 2 seconds.."
        time.sleep(2)
        write_to_usb()
  except KeyboardInterrupt:
    for t in threads:
        t.stop()

if "__main__" == __name__:
    main()




**II. A sample app for PAAS type requiring python. (for USB Serial)**


    devices:
      -
        type: usbdev
        label: HOST_DEV1
        usage: Needs usb storage device to persist data
        function: serial
        mandatory: True
        vendorID: "6001"
        productID: "0403"     

activation_serial.json

{
    "resources": {
        "profile": "custom",
        "cpu": "25",
        "memory": "50",
        "disk": "100",
        "network": [{"interface-name": "eth0", "network-name": "iox-bridge0"}],
        "devices": [{"type": "usbdev", "label": "HOST_DEV1", "device-id": "/dev/bus/usb/001/030", "vid":"6001", "pid":"0403"}]
    }
}

Send the activation payload to CAF through one of the below procedure.

  • By selecting the USB device listed on LM in the app
  • Sending the payload activation_serial.json through ioxclient (modifying the device-id for the same type as specified from the output from the command ioxclient platform device lists )
ioxclient platform device lists
 output

Currently active profile :  829now
Command Name: plt-device-list
Saving current configuration
-------------Device Info----------------
{
 "ds_dev_ids": {
...........
 },
 "serial": [
.......
 ],
 "usbdev": [
 {
  "available": true,
  "bus": "2",
  "dev": "2",
  "device_class": "00\n",
  "device_id": "/dev/bus/usb/002/002",
  "device_name": "/dev/ttyUSB0",
  "device_path": "/sys/devices/pci0000:00/0000:00:0a.0/usb2/2-1",
  "generic_params": {
   "device_name": "/dev/ttyUSB0",
   "device_path": "/devices/pci0000:00/0000:00:0a.0/usb2/2-1/2-1:1.0/ttyUSB0/tty/ttyUSB0",
   "model": "USB_Serial_Converter",
   "vendor": "FTDI"
  },
  "is_generic": true,
  "is_storage": false,
  "pid": "6001",
  "port": null,
  "storage_params": null,
  "support_fstype": [
   "vfat"
  ],
  "type": "usbdev",
  "used_by": null,
  "vid": "0403"
 }

 ],
 "usbport": [
.....
]
}

.....

Specifying the same device-id in activation_serial.json and passing that as payload if using ioxclient.

ioxclient application activate <appname> --payload activation_serial.json