Install service unit files, busconfig and udev rules from the package.

This change required as a part of privilege separation work:
  https://github.com/openbmc/openbmc/issues/3383

The dependant change in openbmc repo:
  https://gerrit.openbmc-project.xyz/c/openbmc/openbmc/+/40359

Signed-off-by: Anton D. Kachalov <gmouse@google.com>
Change-Id: I89aec71b446e0b7be40a10e1cd94ea16d891a277
diff --git a/70-hwmon.rules b/70-hwmon.rules
new file mode 100644
index 0000000..503b2d0
--- /dev/null
+++ b/70-hwmon.rules
@@ -0,0 +1,2 @@
+SUBSYSTEM=="hwmon", ACTION=="add", RUN="/usr/bin/start_hwmon.sh start %p %E{OF_FULLNAME}"
+SUBSYSTEM=="hwmon", ACTION=="remove", RUN="/usr/bin/start_hwmon.sh stop %p %E{OF_FULLNAME}"
diff --git a/70-iio.rules b/70-iio.rules
new file mode 100644
index 0000000..45fb4b0
--- /dev/null
+++ b/70-iio.rules
@@ -0,0 +1,2 @@
+SUBSYSTEM=="iio", ACTION=="add", RUN="/usr/bin/start_hwmon.sh start %p %E{OF_FULLNAME}"
+SUBSYSTEM=="iio", ACTION=="remove", RUN="/usr/bin/start_hwmon.sh stop %p %E{OF_FULLNAME}"
diff --git a/meson.build b/meson.build
index 77f7761..764cdd6 100644
--- a/meson.build
+++ b/meson.build
@@ -92,6 +92,34 @@
     ],
     install: true)
 
+systemd_system_unit_dir = dependency('systemd').get_pkgconfig_variable(
+    'systemdsystemunitdir',
+    define_variable: ['prefix', get_option('prefix')])
+udev_dir = dependency('udev').get_pkgconfig_variable(
+    'udev_dir',
+    define_variable: ['prefix', get_option('prefix')])
+
+install_data(
+    'xyz.openbmc_project.Hwmon@.service',
+    install_dir: systemd_system_unit_dir
+)
+
+install_data(
+    'phosphor-hwmon.conf',
+    install_dir: get_option('sysconfdir') / 'dbus-1/system.d'
+)
+
+install_data(
+    ['70-hwmon.rules', '70-iio.rules'],
+    install_dir: udev_dir / 'rules.d'
+)
+
+install_data(
+    'start_hwmon.sh',
+    install_dir: get_option('bindir'),
+    install_mode: 'rwxr-xr-x'
+)
+
 subdir('msl')
 if not get_option('tests').disabled()
   subdir('test')
diff --git a/msl/meson.build b/msl/meson.build
index f63f722..72633f7 100644
--- a/msl/meson.build
+++ b/msl/meson.build
@@ -4,6 +4,12 @@
     input: 'max31785-msl',
     install: true,
     install_dir: get_option('bindir'),
+    install_mode: 'rwxr-xr-x',
     output: 'max31785-msl',
 )
+
+install_data(
+    'phosphor-max31785-msl@.service',
+    install_dir: systemd_system_unit_dir
+)
 endif
diff --git a/msl/phosphor-max31785-msl@.service b/msl/phosphor-max31785-msl@.service
new file mode 100644
index 0000000..67788ae
--- /dev/null
+++ b/msl/phosphor-max31785-msl@.service
@@ -0,0 +1,12 @@
+[Unit]
+Description=Phosphor MAX31785 MSL
+ConditionFileNotEmpty=/etc/default/obmc/hwmon-max31785/%I.conf
+Wants=mapper-wait@-xyz-openbmc_project-inventory.service
+After=mapper-wait@-xyz-openbmc_project-inventory.service
+
+[Service]
+Type=oneshot
+ExecStart=/usr/bin/env max31785-msl -p "${PATHS}" -b ${BUS} -r ${REVISION}
+EnvironmentFile=/etc/default/obmc/hwmon-max31785/%I.conf
+SyslogIdentifier=max31785-msl
+RemainAfterExit=yes
diff --git a/phosphor-hwmon.conf b/phosphor-hwmon.conf
new file mode 100644
index 0000000..b92c412
--- /dev/null
+++ b/phosphor-hwmon.conf
@@ -0,0 +1,7 @@
+<!DOCTYPE busconfig PUBLIC "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN"
+ "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd">
+<busconfig>
+  <policy user="root">
+    <allow own_prefix="xyz.openbmc_project.Hwmon"/>
+  </policy>
+</busconfig>
diff --git a/start_hwmon.sh b/start_hwmon.sh
new file mode 100755
index 0000000..2e346d0
--- /dev/null
+++ b/start_hwmon.sh
@@ -0,0 +1,24 @@
+#!/bin/bash
+
+action=$1
+devpath=$2
+of_fullname=$3
+
+#Use of_fullname if it's there, otherwise use devpath.
+
+path=$of_fullname
+if [ -z "$path" ]
+then
+    path=$devpath
+
+    if [[ "$path" =~ (.*)/hwmon/hwmon[0-9]+$ ]];
+    then
+        path=${BASH_REMATCH[1]}
+    fi
+fi
+
+# Needed to re-do escaping used to avoid bitbake separator conflicts
+path="${path//:/--}"
+# Needed to escape prior to being used as a unit argument
+path="$(systemd-escape "$path")"
+systemctl --no-block "$action" "xyz.openbmc_project.Hwmon@$path.service"
diff --git a/xyz.openbmc_project.Hwmon@.service b/xyz.openbmc_project.Hwmon@.service
new file mode 100644
index 0000000..b930b57
--- /dev/null
+++ b/xyz.openbmc_project.Hwmon@.service
@@ -0,0 +1,10 @@
+[Unit]
+Description=Phosphor Hwmon Poller
+ConditionFileNotEmpty=/etc/default/obmc/hwmon/%I.conf
+After=xyz.openbmc_project.ObjectMapper.service
+
+[Service]
+Restart=on-failure
+ExecStart=/usr/bin/env phosphor-hwmon-readd -o %I
+SyslogIdentifier=phosphor-hwmon-readd
+EnvironmentFile=/etc/default/obmc/hwmon/%I.conf