meta-ampere: Support UID button and LED

The UID button is used to provide visual identification of the system
when pushed. Pressing the UID button toggles the UID LED. The same
action can be done via Redfish (by patching IndicatorLED from
redfish/v1/Systems/system) or the IPMI chassis identify command.

Tested:
1. Press the UID button to toggles the UID on/off
2. Turn on the UID LED via ipmi command
      $ ipmitool chassis identify
3. Turn Lit/Blinking/Off UID LED via Redfish then check the IndicatorLED
   values
$ curl -X PATCH --user root:0penBmc -H "Content-Type: application/json" \
     -H "If-Match: *" --insecure https://BMC_IP/redfish/v1/Systems/system \
     -d '{"IndicatorLED": "Lit/Blinking/Off"}'
$curl -X GET --user root:0penBmc -H "Content-Type: application/json" \
      --insecure https://BMC_IP/redfish/v1/Systems/system
      The IndicatorLED is "Lit/Blinking/Off"

Signed-off-by: Hieu Huynh <hieu.huynh@amperecomputing.com>
Signed-off-by: Chanh Nguyen <chanh@os.amperecomputing.com>
Change-Id: I58eeae7ac22a9d4ddd4acdfbd167095bdc74072c
diff --git a/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button.bb b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button.bb
new file mode 100644
index 0000000..c19cdee
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button.bb
@@ -0,0 +1,29 @@
+SUMMARY = "MTJade ID Button pressed application"
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${COREBASE}/meta/files/common-licenses/Apache-2.0;md5=89aea4e17d99a7cacdbeed46a0096b10"
+
+inherit obmc-phosphor-systemd
+
+DEPENDS += "virtual/obmc-gpio-monitor"
+RDEPENDS_${PN} += "virtual/obmc-gpio-monitor"
+
+SRC_URI += "file://toggle_identify_led.sh"
+
+do_install() {
+        install -d ${D}${bindir}
+        install -m 0755 ${WORKDIR}/toggle_identify_led.sh \
+            ${D}${bindir}/toggle_identify_led.sh
+}
+
+SYSTEMD_ENVIRONMENT_FILE_${PN} +="obmc/gpio/id_button"
+
+ID_BUTTON_SERVICE = "id_button"
+
+TMPL = "phosphor-gpio-monitor@.service"
+INSTFMT = "phosphor-gpio-monitor@{0}.service"
+TGT = "multi-user.target"
+FMT = "../${TMPL}:${TGT}.requires/${INSTFMT}"
+
+SYSTEMD_SERVICE_${PN} += "id-button-pressed.service"
+SYSTEMD_LINK_${PN} += "${@compose_list(d, 'FMT', 'ID_BUTTON_SERVICE')}"
diff --git a/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/id-button-pressed.service b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/id-button-pressed.service
new file mode 100644
index 0000000..0a97ed7
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/id-button-pressed.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=ID Button Pressed Service
+Wants=mapper-wait@-xyz-openbmc_project-led-groups.service
+After=mapper-wait@-xyz-openbmc_project-led-groups.service
+StartLimitIntervalSec=0
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/toggle_identify_led.sh
+SyslogIdentifier=id-button
diff --git a/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/obmc/gpio/id_button b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/obmc/gpio/id_button
new file mode 100644
index 0000000..70e2b58
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/obmc/gpio/id_button
@@ -0,0 +1,5 @@
+DEVPATH=/dev/input/by-path/platform-gpio-keys-event
+KEY=133
+POLARITY=1
+TARGET=id-button-pressed.service
+EXTRA_ARGS=--continue
diff --git a/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/toggle_identify_led.sh b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/toggle_identify_led.sh
new file mode 100755
index 0000000..d62c84a
--- /dev/null
+++ b/meta-ampere/meta-jade/recipes-phosphor/gpio/id-button/toggle_identify_led.sh
@@ -0,0 +1,45 @@
+#!/bin/sh
+#
+# Copyright (c) 2021 Ampere Computing LLC
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#	http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Toggle the state of identify LED Group
+
+SERVICE="xyz.openbmc_project.LED.GroupManager"
+INTERFACE="xyz.openbmc_project.Led.Group"
+PROPERTY="Asserted"
+
+# Get enclosure_identify state
+identify_object=$(busctl tree $SERVICE --list | grep -m 1 identify)
+identify_state=$(busctl get-property $SERVICE $identify_object $INTERFACE $PROPERTY \
+	  |  awk '{print $NF;}')
+
+# Get enclosure_identify_blink state
+identify_blink_object=$(busctl tree $SERVICE --list | grep identify_blink)
+identify_blink_state=$(busctl get-property $SERVICE $identify_blink_object $INTERFACE $PROPERTY \
+	  |  awk '{print $NF;}')
+
+# Set state
+if [[ "$identify_state" == "false" && "$identify_blink_state" == "false" ]]; then
+    # Turn on the UID LED
+    busctl set-property $SERVICE $identify_object $INTERFACE $PROPERTY b true
+elif [[ "$identify_state" == "false" && "$identify_blink_state" == "true" ]]; then
+    # Turn off the UID LED when LED is blinking state
+    busctl set-property $SERVICE $identify_blink_object $INTERFACE $PROPERTY b false
+elif [[ "$identify_state" == "true" && "$identify_blink_state" == "false" ]]; then
+    # Turn off the UID LED
+    busctl set-property $SERVICE $identify_object $INTERFACE $PROPERTY b false
+else
+    echo "Invalid case! When identify_blink_state is true, the identify_state will set to false"
+fi
diff --git a/meta-ampere/meta-jade/recipes-phosphor/leds/mtjade-led-manager-config/led.yaml b/meta-ampere/meta-jade/recipes-phosphor/leds/mtjade-led-manager-config/led.yaml
index ba21c58..3e321ed 100644
--- a/meta-ampere/meta-jade/recipes-phosphor/leds/mtjade-led-manager-config/led.yaml
+++ b/meta-ampere/meta-jade/recipes-phosphor/leds/mtjade-led-manager-config/led.yaml
@@ -18,3 +18,15 @@
         DutyOn: 50
         Period: 0
         Priority: 'Blink'
+
+enclosure_identify:
+    identify:
+        Action: 'On'
+        DutyOn: 50
+        Period: 1000
+
+enclosure_identify_blink:
+    identify:
+        Action: 'Blink'
+        DutyOn: 50
+        Period: 1000
diff --git a/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend b/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend
index c9538de..980d926 100644
--- a/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend
+++ b/meta-ampere/meta-jade/recipes-phosphor/packagegroups/packagegroup-obmc-apps.bbappend
@@ -1,2 +1,2 @@
 RDEPENDS_${PN}-extras_append_mtjade = " phosphor-webui phosphor-image-signing"
-RDEPENDS_${PN}-inventory_append_mtjade = " fault-monitor"
+RDEPENDS_${PN}-inventory_append_mtjade = " fault-monitor id-button"