Commit 495616a8 authored by spiiroin's avatar spiiroin

Merge branch 'jb41748_configfs_and_refactoring' into 'master'

Add configfs support and refactor existing functionality

See merge request !36
parents b1f55178 e072697a
This diff is collapsed.
*.o
config.h*
000*
src/.deps/*
RPMS*
installroot*
autom4te.cache
cov-int
docs/html
This diff is collapsed.
/* config.h. Generated from config.h.in by configure. */
/* config.h.in. Generated from configure.ac by autoheader. */
/* Name of package */
#define PACKAGE "usb_moded"
/* Define to the address where bug reports for this package should be sent. */
#define PACKAGE_BUGREPORT ""
/* Define to the full name of this package. */
#define PACKAGE_NAME "usb_moded"
/* Define to the full name and version of this package. */
#define PACKAGE_STRING "usb_moded 0.86.0+mer24"
/* Define to the one symbol short name of this package. */
#define PACKAGE_TARNAME "usb_moded"
/* Define to the home page for this package. */
#define PACKAGE_URL ""
/* Define to the version of this package. */
#define PACKAGE_VERSION "0.86.0+mer24"
/* Define to 1 if you have the ANSI C header files. */
#define STDC_HEADERS 1
/* Version number of package */
#define VERSION "0.86.0+mer24"
/* Define to empty if `const' does not conform to ANSI C. */
/* #undef const */
......@@ -5,12 +5,8 @@ appsync = 1
network = 0
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = adb,diag
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
android_extra_sysfs_path = /sys/class/android_usb/android0/f_diag/clients
android_extra_sysfs_value = diag
idProduct = 0A05
......@@ -6,13 +6,9 @@ network = 1
network_interface = rndis0
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = rndis,adb
android_extra_sysfs_path = /sys/class/android_usb/android0/f_ffs/aliases
android_extra_sysfs_value = adb
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
idProduct = 0A03
dhcp_server = 1
......@@ -3,11 +3,7 @@ name = acm_mode
module = none
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = acm
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
android_extra_sysfs_path = /sys/class/android_usb/android/f_acm/acm_transports
android_extra_sysfs_value = tty
......@@ -3,11 +3,7 @@ name = at_mode
module = none
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = serial
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
android_extra_sysfs_path = /sys/class/android_usb/android0/f_serial/transports
android_extra_sysfs_value = smd,tty
......@@ -3,11 +3,7 @@ name = connection_sharing
module = none
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = rndis
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
idProduct = 0A02
connman_tethering = /net/connman/technology/gadget
......@@ -6,11 +6,7 @@ network_interface = rndis0
appsync = 1
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = rndis
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
idProduct = 0A02
nat = 1
......@@ -6,11 +6,7 @@ network_interface = rndis0
appsync = 1
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = rndis
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
idProduct = 0A02
dhcp_server = 1
......@@ -6,12 +6,8 @@ network = 1
network_interface = rndis0
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = diag,serial,rmnet,qdss,adb,rndis
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
android_extra_sysfs_path = /sys/class/android_usb/android0/f_diag/clients
android_extra_sysfs_value = diag
android_extra_sysfs_path2 = /sys/class/android_usb/android0/f_serial/transports
......
......@@ -6,12 +6,8 @@ network = 1
network_interface = rndis0
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = rndis,adb,diag,serial
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
android_extra_sysfs_path = /sys/class/android_usb/android0/f_diag/clients
android_extra_sysfs_value = diag
android_extra_sysfs_path2 = /sys/class/android_usb/android0/f_serial/transports
......
......@@ -3,10 +3,6 @@ name = mtp_mode
module = none
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = ffs
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
idProduct = 0A07
......@@ -3,9 +3,5 @@ name = mtp_mode
module = none
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = mtp
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
......@@ -3,10 +3,6 @@ name = pc_suite
module = none
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = ffs
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnect_disconnect = 0
idProduct = 0A07
......@@ -4,10 +4,6 @@ module = none
appsync = 1
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = mass_storage
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnec_disconnect = 0
idProduct = 55AA
#!/usr/bin/env python
# -*- encoding: utf8 -*-
# ----------------------------------------------------------------------------
# Copyright (C) 2012-2018 Jolla Ltd.
# Contact: Simo Piiroinen <simo.piiroinen@jollamobile.com>
# License: GPLv2
# ----------------------------------------------------------------------------
import sys,os
def is_local(path):
return not path.startswith("/")
def print_rule(dest, srce):
print "%s\\\n" % "\\\n\t".join(["%s:" % dest] + srce)
def set_extension(path, ext):
return os.path.splitext(path)[0] + ext
def normalize_path(srce):
return os.path.normpath(srce)
def fix_directory(dest, srce):
srce = os.path.dirname(srce)
dest = os.path.basename(dest)
return os.path.join(srce, dest)
if __name__ == "__main__":
data = sys.stdin.readlines()
data = map(lambda x:x.rstrip(), data)
data.reverse()
deps = []
# Note: dependencies are sorted only to keep
# future diffs cleaner
while data:
line = data.pop()
while line.endswith('\\'):
line = line[:-1] + data.pop()
if not ':' in line:
continue
dest,srce = line.split(":",1)
srce = srce.split()
srce,temp = srce[0],srce[1:]
# take dest path primary srce
dest = fix_directory(dest, srce)
# remove secondary deps with absolute path
temp = filter(is_local, temp)
# sort secondary sources
temp.sort()
srce = [srce] + temp
srce = map(normalize_path, srce)
deps.append((dest,srce))
for dest,srce in sorted(deps):
# emit normal compilation dependencies
print_rule(dest, srce)
# and for -fPIC in case it is needed
dest = set_extension(dest, ".pic.o")
print_rule(dest, srce)
......@@ -237,8 +237,7 @@ Those files will be read on start and usb_moded will keep a list of apps to laun
for a certain mode. This also means that if you change the files or add/remove some
you need to restart usb_moded. Later when the mode is activated, usb_moded will start
each of them after the module has been loaded and keep track if they have been started.
It will warn you if that failed. This works together with an optional softconnect option
that will need kernel support.
It will warn you if that failed.
These services will start before the whole setup for the usb is done. In case the application
only works after everything has been set up, you can start the application at the end by adding
......@@ -264,9 +263,6 @@ network_interface = usb0
sysfs_path = /* in case you need to echo parameters somewhere in a sysfs path */
sysfs_value = /* the values */
sysfs_reset_value = /* in case a reset value needs to be written */
softconnect_path = /* a path in case the module needs activation with a softconnect option */
softconnect = /* value to be written to enable */
softconnect_disconnect = /* value to be written for disable */
Only the mode name and module are mandatory. In case you do not use modules, use none as the value
for module (as for the android gadget for example. See the android section for more info)
......@@ -327,12 +323,8 @@ network = 1
network_interface = rndis0
[options]
sysfs_path = /sys/class/android_usb/android0/functions
# sysfs_value = comma separated list of functions to enable in this mode
sysfs_value = rndis
sysfs_reset_value = none
softconnect_path = /sys/class/android_usb/android0/enable
softconnect = 1
softconnec_disconnect = 0
idProduct = 0002
......
......@@ -11,7 +11,7 @@ Source1: usb_moded.conf
BuildRequires: pkgconfig(dbus-1)
BuildRequires: pkgconfig(dbus-glib-1)
BuildRequires: pkgconfig(glib-2.0)
BuildRequires: pkgconfig(udev)
BuildRequires: pkgconfig(libudev)
BuildRequires: pkgconfig(libkmod)
BuildRequires: doxygen
BuildRequires: pkgconfig(libsystemd)
......@@ -24,6 +24,9 @@ Requires: busybox-symlinks-dhcp
Requires(post): systemd
Requires(postun): systemd
Conflicts: dsme < 0.79.0
Conflicts: buteo-mtp-qt5-sync-plugin
Conflicts: buteo-mtp-qt5 < 0.5.0
Recommends: buteo-mtp-qt5
%description
Usb_moded is a daemon to control the USB states. For this
......@@ -71,7 +74,7 @@ usb networking.
%package mtp-mode
Summary: USB mode controller - mtp mode config
Group: Config
Requires: buteo-mtp
Requires: buteo-mtp-qt5
%description mtp-mode
Usb_moded is a daemon to control the USB states. For this
......@@ -357,6 +360,7 @@ touch %{buildroot}/%{_sysconfdir}/udhcpd.conf
install -d $RPM_BUILD_ROOT/lib/systemd/system/basic.target.wants/
install -m 644 -D systemd/%{name}.service %{buildroot}/lib/systemd/system/%{name}.service
ln -s ../%{name}.service $RPM_BUILD_ROOT/lib/systemd/system/basic.target.wants/%{name}.service
install -d %{buildroot}/usr/lib/systemd/user
install -m 644 -D systemd/usb-moded-args.conf %{buildroot}/var/lib/environment/usb-moded/usb-moded-args.conf
install -m 755 -D systemd/turn-usb-rescue-mode-off %{buildroot}/%{_bindir}/turn-usb-rescue-mode-off
install -m 644 -D systemd/usb-rescue-mode-off.service %{buildroot}/lib/systemd/system/usb-rescue-mode-off.service
......
#!/bin/sh
PROGNAME="$(basename $0)"
# ============================================================================
# ENV
# ============================================================================
CONFIGFS_ROOT="/config"
GADGET_DIR="$CONFIGFS_ROOT/usb_gadget/g1"
CONFIG_DIR="$GADGET_DIR/configs/b.1"
FUNCTION_DIR="$GADGET_DIR/functions"
UDC_CONTROL="$GADGET_DIR/UDC"
UDC_ENABLE="$(ls /sys/class/udc)"
UDC_DISABLE=""
# "charging_only"
FUNCTION_MASS_STORAGE="mass_storage.usb0"
# "developer_mode"
FUNCTION_RNDIS="rndis_bam.rndis"
# "mtp_mode"
FUNCTION_MTP="ffs.mtp"
# ============================================================================
# LOG
# ============================================================================
LOG_LEVEL=6
log_critical() { test $LOG_LEVEL -ge 2 && echo >&2 "$PROGNAME: C: $*" ; }
log_error() { test $LOG_LEVEL -ge 3 && echo >&2 "$PROGNAME: E: $*" ; }
log_warning() { test $LOG_LEVEL -ge 4 && echo >&2 "$PROGNAME: W: $*" ; }
log_notice() { test $LOG_LEVEL -ge 5 && echo >&2 "$PROGNAME: N: $*" ; }
log_info() { test $LOG_LEVEL -ge 6 && echo >&2 "$PROGNAME: I: $*" ; }
log_debug() { test $LOG_LEVEL -ge 7 && echo >&2 "$PROGNAME: D: $*" ; }
# ============================================================================
# CONFIGFS
# ============================================================================
configfs_validate() {
log_debug "Validate ConfigFS mountpoint"
mountpoint -q $CONFIGFS_ROOT || exit 1
test -d "$GADGET_DIR" || exit 1
test -f "$UDC_CONTROL" || exit 1
test -d "$CONFIG_DIR" || exit 1
test -d "$FUNCTION_DIR" || exit 1
}
# ============================================================================
# UDC
# ============================================================================
udc_control() {
local prev="$(cat $UDC_CONTROL)"
local next="$1"
if [ "$prev" != "$next" ]; then
echo > "$UDC_CONTROL" "$next"
fi
}
udc_disable() {
log_debug "Disable UDC"
udc_control "$UDC_DISABLE"
}
udc_enable() {
log_debug "Enable UDC"
udc_control "$UDC_ENABLE"
}
# ============================================================================
# FUNCTION
# ============================================================================
function_register() {
log_debug "Register function: $1"
test -d "$FUNCTION_DIR/$1" || mkdir "$FUNCTION_DIR/$1"
}
function_activate() {
log_debug "Activate function: $1"
test -h "$CONFIG_DIR/$1" || ln -s "$FUNCTION_DIR/$1" "$CONFIG_DIR/$1"
}
function_deactivate() {
log_debug "Deactivate function: $1"
test -h "$CONFIG_DIR/$1" && rm "$CONFIG_DIR/$1"
}
function_deactivate_all() {
local err=0
for f in $CONFIG_DIR/*; do
# symlink == function
test -h $f || continue
if ! function_deactivate $(basename $f); then
err=1
fi
done
return $err
}
function_list_active() {
log_debug "List active functions"
for f in $CONFIG_DIR/*; do
# symlink == function
test -h $f || continue
log_debug "Active function: $(basename $f)"
done
return 0
}
function_set_attr() {
local f="$1"
local k="$2"
local v="$3"
log_debug "Function $f: set $k = $v"
echo > "$FUNCTION_DIR/$f/$k" "$v"
}
# ============================================================================
# RNDIS
# ============================================================================
rndis_down() {
log_debug "Disable rndis interface"
ifconfig rndis0 down 2> /dev/null || :
}
rndis_up() {
log_debug "Enable rndis interface"
ifconfig rndis0 192.168.2.15 255.255.255.0 2> /dev/null || :
}
udhcpd_configure() {
log_debug "Configure udhcpd"
cat > /etc/udhcpd.conf <<-EOF
start 192.168.2.1
end 192.168.2.15
interface rndis0
option subnet 255.255.255.0
option lease 3600
max_leases 15
EOF
}
udhcpd_start() {
log_debug "Start udhcpd"
systemctl start udhcpd.service
}
udhcpd_stop() {
log_debug "Stop udhcpd"
systemctl stop udhcpd.service
}
# ============================================================================
# MTP
# ============================================================================
MTPD_ENV="LANG=en_GB.utf8 MSYNCD_LOGGING_LEVEL=7"
MTPD_BIN="/usr/lib/mtp/mtp_service"
MTPD_LOG="/tmp/mtpd.log"
mtp_mount() {
log_debug "Mount mtp endpoint directory"
if ! mountpoint -q /dev/mtp ; then
/bin/mount -o uid=100000,gid=100000 -t functionfs mtp /dev/mtp
fi
}
mtp_unmount() {
log_debug "Unmount mtp endpoint directory"
if mountpoint -q /dev/mtp ; then
/bin/umount /dev/mtp
fi
}
mtpd_start() {
log_debug "Start mtpd"
(/bin/su - nemo -c "$MTPD_ENV $MTPD_BIN" 2>$MTPD_LOG) &
local seen=0
for i in $(seq 10); do
sleep 0.333
if killall -0 "$MTPD_BIN" ; then
seen=$((seen+1))
test "$seen" -ge 3 && return 0
else
log_debug "Wait for mtpd ..."
fi
done
log_warning "Could not start mtpd"
return 1
}
mtpd_stop() {
log_debug "Stop mtpd"
/usr/bin/killall "$MTPD_BIN" || :
}
# ============================================================================
# USB_MODE
# ============================================================================
# -- undefined --
usb_mode_enter_undefined() {
log_debug "ENTER UNKNOWN MODE"
# same as enter undefined...
udc_disable
function_deactivate_all
}
usb_mode_leave_undefined() {
log_debug "LEAVE UNKNOWN MODE"
udc_disable
function_deactivate_all
}
# -- charging_only --
usb_mode_enter_charging_only() {
log_debug "ENTER CHARGING_ONLY"
function_register $FUNCTION_MASS_STORAGE
function_activate $FUNCTION_MASS_STORAGE
udc_enable
}
usb_mode_leave_charging_only() {
log_debug "LEAVE CHARGING_ONLY"
udc_disable
function_deactivate_all
}
# -- developer_mode --
usb_mode_enter_developer_mode() {
log_debug "ENTER DEVELOPER_MODE"
function_register $FUNCTION_RNDIS
function_set_attr $FUNCTION_RNDIS wceis 1
function_activate $FUNCTION_RNDIS
udc_enable
sleep 0.5
rndis_up
sleep 0.5
udhcpd_configure
udhcpd_start
}
usb_mode_leave_developer_mode() {
log_debug "LEAVE DEVELOPER_MODE"
udhcpd_stop
rndis_down
udc_disable
function_deactivate_all
}
# -- mtp_mode --
usb_mode_enter_mtp_mode() {
log_debug "ENTER MTP_MODE"
function_register $FUNCTION_MTP
function_activate $FUNCTION_MTP
mtp_mount
mtpd_start
udc_enable
}
usb_mode_leave_mtp_mode() {
log_debug "LEAVE MTP_MODE"
udc_disable
mtpd_stop
mtp_unmount
function_deactivate_all
}
# -- mode switch --
usb_mode_enter() {
case $1 in
mtp_mode)
usb_mode_enter_mtp_mode
;;
developer_mode)
usb_mode_enter_developer_mode
;;
charging_only)
usb_mode_enter_charging_only
;;
undefined)
usb_mode_enter_undefined
;;
*)
log_warning "Unknown mode: $1"
usb_mode_enter_undefined
;;
esac
}
usb_mode_leave() {
case $1 in
mtp_mode)
usb_mode_leave_mtp_mode
;;
developer_mode)
usb_mode_leave_developer_mode
;;
charging_only)
usb_mode_leave_charging_only
;;
undefined)
usb_mode_leave_undefined
;;
*)
log_warning "Unknown mode: $1"
usb_mode_leave_undefined
;;
esac
}
usb_mode_switch() {
configfs_validate
local next="$1"
local curr="$(cat /tmp/usb-mode)"
test ! -z "$curr" || curr="undefined"
if [ "$curr" != "$next" ]; then
log_info "SWITCH FROM: $curr TO: $next"
usb_mode_leave "$curr"
echo > /tmp/usb-mode "$next"
usb_mode_enter "$next"
else
log_info "ALREADY IN: $next"
fi
}
# ============================================================================
# MAIN
# ============================================================================
# try to guess if we were executed or sourced
if [ "$PROGNAME" = "set_usb_mode.sh" ]; then
TARGET_MODE="$1"
case $TARGET_MODE in
cha*)
TARGET_MODE="charging_only"
;;
dev*)
TARGET_MODE="developer_mode"
;;
mtp*)
TARGET_MODE="mtp_mode"
;;
und*)
TARGET_MODE="undefined"
;;
-v|--verbose)
LOG_LEVEL=$((LOG_LEVEL+1))
;;
-q|--quiet)
LOG_LEVEL=$((LOG_LEVEL-1))
;;
-s|--silent)
LOG_LEVEL=0
;;
-h|--help|--usage)