Add 'modem set-logging' command.

The 'modem set-logging' command replaces 'mm_debug' for setting logging
level in modem managers.

BUG=chromium-os:34360
TEST=Tested 'modem set-logging' with cromo and ModemManager.

Change-Id: I11c68eb36d602f5ff1fd5e06418e824b56d33fb7
Reviewed-on: https://gerrit.chromium.org/gerrit/33114
Commit-Ready: Ben Chan <benchan@chromium.org>
Tested-by: Ben Chan <benchan@chromium.org>
Reviewed-by: Thieu Le <thieule@chromium.org>
diff --git a/modem b/modem
index f714924..4b8bc64 100755
--- a/modem
+++ b/modem
@@ -273,6 +273,27 @@
     "string:${carrier}"
 }
 
+set_logging() {
+  local level="$1"
+  local manager
+
+  if [ -z "${level}" ]; then
+    echo "Valid logging levels are: debug, info, warn, error"
+    return
+  fi
+
+  case "${level}" in
+    debug|info|warn|error)
+      for manager in $(modem_managers); do
+        set_modem_manager_logging "${manager}" "${level}"
+      done
+      ;;
+    *)
+      error_exit "'${level}' is not a valid logging level."
+      ;;
+  esac
+}
+
 MADISON_CONFIG_GROUP_LOCATION=/var/lib/cromo/madison-config-group
 
 set_madison_config() {
@@ -349,6 +370,7 @@
   echo "  reset [-modem <modem>]                       Reset the modem"
   echo "  set-carrier [-modem <modem>] <carrier-name> " \
     "Set modem carrier firmware"
+  echo "  set-logging (debug|info|warn|error)          Set logging level"
   echo "  set-madison-config <config-group-name>       Set Madison config group"
   echo "  start-oma                                   " \
     "Enable and launch OMA session"
@@ -389,6 +411,9 @@
   set-carrier)
     set_carrier "$@"
     ;;
+  set-logging)
+    set_logging "$@"
+    ;;
   set-madison-config)
     set_madison_config "$@"
     ;;
diff --git a/modem-common.sh b/modem-common.sh
index 1e153b9..c852205 100644
--- a/modem-common.sh
+++ b/modem-common.sh
@@ -191,3 +191,32 @@
   [ -z "${modem}" ] && modem=$(mm1_modems | head -1)
   echo "${modem}"
 }
+
+# Returns all modem managers that are currently running.
+modem_managers() {
+  dbus_call org.freedesktop.DBus /org/freedesktop/DBus \
+      org.freedesktop.DBus.ListNames | awk '/ModemManager/ { print $2 }'
+}
+
+# Sets the log level of the specified modem manager.
+set_modem_manager_logging() {
+  local manager="$1"
+  local level="$2"
+
+  case "$manager" in
+    org.chromium.ModemManager)
+      dbus_call "${manager}" "${MM_OBJECT}" "${MM_IMANAGER}.SetLogging" \
+        "string:${level}"
+      ;;
+    org.freedesktop.ModemManager1)
+      if [ "${level}" = "error" ]; then
+        level=err
+      fi
+      dbus_call "${manager}" "${MM1_OBJECT}" "${MM1_IMANAGER}.SetLogging" \
+        "string:${level}"
+      ;;
+    *)
+      return 1
+      ;;
+  esac
+}