Skip to content

Instantly share code, notes, and snippets.

@hiszpanski
Last active September 15, 2020 04:49
Show Gist options
  • Select an option

  • Save hiszpanski/3111678a6e44e2ca976f to your computer and use it in GitHub Desktop.

Select an option

Save hiszpanski/3111678a6e44e2ca976f to your computer and use it in GitHub Desktop.
Expect script, run by udev, to get camera serial number
#!/usr/bin/expect -f
##############################################################################
#
# cameo.tcl
# Expect script. Queries camera serial number via serial port and returns it.
#
# When a camera is connected, it enumerates as a serial device on the machine,
# for example /dev/ttyACM0. This is dependent on the order in which cameras
# connect, and as there is no identifying information in the USB descriptors,
# it is not possible to enumerate to a particular device based on these
# descriptors alone.
#
# This script connects to the camera, reads the serial number, and creates
# a symbolic-link device using this serial number, for example:
#
# /dev/OOKN001021500046 -> /dev/ttyACM0
#
# Thus the camera has a constant handle, regardless of the order in which it
# is connected.
#
# Place this file in /etc/udev. Also add a udev rule with:
#
# SUBSYSTEM=="tty",
# ACTION=="add",
# KERNEL=="ttyACM[0-9]",
# PROGRAM="cameo.tcl /dev/%k",
# SYMLINK+="%c"
#
# Chris Hiszpanski <chris@getkuna.com>
# Copyright 2015 Kuna Systems Corporation. All rights reserved.
#
##############################################################################
log_user 0
set timeout 30
set device [lindex $argv 0]
# Open serial connection
spawn picocom $device
# Login
expect {
"Terminal ready" { send "\r"; exp_continue }
"kuna login:" { send "root\r"; exp_continue }
"Ambarella login:" { send "root\r"; exp_continue }
"# " { }
}
# Get serial number
send "nandwrite --show_info | grep '^sn' | tr -d ' \\t'\r"
sleep 1
expect -re "sn=(.*)\r" {
set trimmed [ string map { "\r" "" } $expect_out(0,string) ]
set serial [ lindex [ split $trimmed '='] 1]
}
# Exit session
send "exit\r"
# Report serial number
send_user "$serial\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment