ec: fix button/powerbtn behavior

This changes does 2 things:
- it unifies volume up/down/up_down_hold and pwr_button_hold all under
one ec driver function and uses the parameters to indicate how to invoke
it. This is to reduce code and also have common error guarding (below)
- it guards against assertion errors in the EC by overwriting anything
less than 1 as the value (1ms) with 1ms.

BUG=b:123212205
TEST=manual testing
dut-control pwr_button_hold:10

> powerbtn  10
Simulating 10 ms power button press.
[936.095299 power button pressed]
[936.095475 buttons: 5]
[936.095666 SW 0x07]
[936.095932 event set 0x0000000000000004]
Simulating power button release.
> [936.104859 power button released]
[936.105017 buttons: 4]
[936.105208 SW 0x05]

dut-control volume_up_hold:10

> button vup 10
[948.650757 Button 'Volume Up' was pressed]
[948.650962 buttons: 6]

dut-control volume_down_hold:10

> [953.951354 Battery 48% (Display 0.0 %) / 2h:0 to full]
[958.651181 DEBUG MODE: Active!]
button vdown 10
[965.652122 Button 'Volume Down' was released]
[965.652333 buttons: 2]
> [968.651407 DEBUG MODE: Exit!]

dut-control volume_up_down_hold:100

> button vup vdown 100
[973.135337 Button 'Volume Up' was released]
[973.135545 buttons: 0]
[973.143246 Button 'Volume Down' was pressed]
[973.143455 buttons: 4]
[973.250463 Button 'Volume Up' was pressed]
[973.250668 buttons: 6]
[973.258372 Button 'Volume Down' was released]
[973.258583 buttons: 2]
[973.258753 DEBUG MODE: Exit!]
>

dut-control volume_up_down_hold:0
2019-01-22 18:49:43,270 - ec - ERROR - Trying to set ec button press to 0 ms.
Overwriting the value to be 1ms. // Servod logs.

// EC console

> button vup vdown 1
[1165.667043 Button 'Volume Up' was released]
[1165.667256 buttons: 0]
[1165.674961 Button 'Volume Down' was pressed]
[1165.675176 buttons: 4]

Change-Id: Icd9895e8da16c74a0e60156393d3b8cfd0fe43b3
Signed-off-by: Ruben Rodriguez Buchillon <coconutruben@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1426284
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Reviewed-by: Todd Broch <tbroch@chromium.org>
diff --git a/servo/data/detachable.xml b/servo/data/detachable.xml
index 800914b..4a48f26 100644
--- a/servo/data/detachable.xml
+++ b/servo/data/detachable.xml
@@ -2,23 +2,24 @@
   <!-- ******** Controls ******** -->
   <control>
     <name>volume_up_hold</name>
-    <doc>Emulated volume up on ec</doc>
+    <doc>Emulated volume up on ec. Unit in ms.</doc>
     <params cmd="get" interface="10" drv="na" />
-    <params cmd="set" subtype="volume_up_hold" interface="10"
-    drv="ec" input_type="int" />
+    <params cmd="set" subtype="button_hold" interface="10" drv="ec"
+    input_type="int" ec_cmd="button" ec_args="vup" />
   </control>
   <control>
     <name>volume_down_hold</name>
-    <doc>Emulated volume down on ec</doc>
+    <doc>Emulated volume down on ec. Unit in ms.</doc>
     <params cmd="get" interface="10" drv="na" />
-    <params cmd="set" subtype="volume_down_hold" interface="10"
-    drv="ec" input_type="int" />
+    <params cmd="set" subtype="button_hold" interface="10" drv="ec"
+    input_type="int" ec_cmd="button" ec_args="vdown" />
   </control>
   <control>
     <name>volume_up_down_hold</name>
-    <doc>Emulated both volume up and down buttons pressed on ec</doc>
+    <doc>Emulated both volume up and down buttons pressed on ec. Unit in
+    ms.</doc>
     <params cmd="get" interface="10" drv="na" />
-    <params cmd="set" subtype="volume_up_down_hold" interface="10"
-    drv="ec" input_type="int" />
+    <params cmd="set" subtype="button_hold" interface="10" drv="ec"
+    input_type="int" ec_cmd="button" ec_args="vup vdown" />
   </control>
 </root>
diff --git a/servo/data/ec_common.xml b/servo/data/ec_common.xml
index 9da64c0..93fac0b 100644
--- a/servo/data/ec_common.xml
+++ b/servo/data/ec_common.xml
@@ -106,8 +106,8 @@
     <name>pwr_button_hold</name>
     <doc>milliseconds to hold the power button</doc>
     <params cmd="get" interface="10" drv="na" />
-    <params cmd="set" subtype='pwr_button_hold' interface="10"
-    drv="ec" input_type="int" />
+    <params cmd="set" subtype='button_hold' interface="10"
+    drv="ec" input_type="int" ec_cmd="powerbtn"/>
   </control>
   <control>
     <name>power_state</name>
diff --git a/servo/drv/ec.py b/servo/drv/ec.py
index 9f581e0..44a7b63 100644
--- a/servo/drv/ec.py
+++ b/servo/drv/ec.py
@@ -289,37 +289,25 @@
     """
     self._issue_cmd('btnpress voldown %d' % int(value))
 
-  def _Set_volume_up_hold(self, value):
-    """Setter of Vup for tablets/ detachables
+  def _Set_button_hold(self, value):
+    """Setter for a button hold on the ec.
+
+    'pwrbtn' or button for tablets/detachables.
 
     Args:
-      value: number of ms to hold the volume button
+      value: number of ms to hold the volume button. Has to be at least 1.
     """
-    self._issue_cmd('button vup %d' % value)
-
-  def _Set_volume_down_hold(self, value):
-    """Setter of Vdown for tablets/ detachables
-
-    Args:
-      value: number of ms to hold the volume button
-    """
-    self._issue_cmd('button vdown %d' % value)
-
-  def _Set_volume_up_down_hold(self, value):
-    """Setter of Vup and vdown for tablets/ detachables
-
-    Args:
-      value: number of ms to hold the volume buttons
-    """
-    self._issue_cmd('button vup vdown %d' % value)
-
-  def _Set_pwr_button_hold(self, value):
-    """Setter of pwr_button_hold.
-
-    Args:
-      value: hold interval, unit: msec.
-    """
-    self._issue_cmd('powerbtn %d' % value)
+    if value < 1:
+      self._logger.error('Trying to set ec button press to %d ms. Overwriting '
+                         'the value to be 1ms.', value)
+      value = 1
+    # One of 'button' or 'powerbtn'
+    cmd = self._params.get('ec_cmd')
+    # 'button' cmd requires vup|vdown or both while 'powerbtn' requires
+    # no args.
+    argline = self._params.get('ec_args', '')
+    ec_cmd = '%s %s %d' % (cmd, argline, value)
+    self._issue_cmd(ec_cmd)
 
   def _Get_cpu_temp(self):
     """Getter of cpu_temp.