1#!/bin/bash
2
3string=''
4pmbus_read() {
5    data=$(i2cget -f -y "$1" "$2" "$3" i "$4")
6
7    if [[ -z "$data" ]]; then
8        echo "i2c$1 device $2 command $3 error" >&2
9        exit 1
10    fi
11
12	arry=$(echo "${data}" | sed -e "s/$4\: //" | sed -e "s/\0x00//g" | sed -e "s/\0xff//g" | sed -e "s/\0x7f//g" | sed -e "s/\0x0f//g" | sed -e "s/\0x14//g")
13
14    string=''
15    for d in ${arry}
16    do
17        hex=${d/0x}
18        string+=$(echo -e "\x${hex}");
19    done
20}
21
22update_inventory() {
23      INVENTORY_SERVICE='xyz.openbmc_project.Inventory.Manager'
24      INVENTORY_OBJECT='/xyz/openbmc_project/inventory'
25      INVENTORY_PATH='xyz.openbmc_project.Inventory.Manager'
26      OBJECT_PATH="/system/chassis/motherboard/powersupply$1"
27      busctl call \
28          ${INVENTORY_SERVICE} \
29          ${INVENTORY_OBJECT} \
30          ${INVENTORY_PATH} \
31          Notify "a{oa{sa{sv}}}" 1 \
32          "${OBJECT_PATH}" 1 "$2" "$3" \
33          "$4" "$5" "$6"
34}
35
36if [ $# -eq 0 ]; then
37    echo 'No PSU device is given' >&2
38    exit 1
39fi
40
41IFS=" " read -ra arr <<< "${1//- /}"
42
43pmbus_read "${arr[1]}" "${arr[2]}" 0x99 11
44update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Decorator.Asset" 1 "Manufacturer" "s" "$string"
45
46pmbus_read "${arr[1]}" "${arr[2]}" 0x9a 11
47update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Decorator.Asset" 1 "Model" "s" "$string"
48
49pmbus_read "${arr[1]}" "${arr[2]}" 0xad 21
50update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Decorator.Asset" 1 "PartNumber" "s" "$string"
51
52pmbus_read "${arr[1]}" "${arr[2]}" 0x9e 18
53update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Decorator.Asset" 1 "SerialNumber" "s" "$string"
54
55update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Decorator.Cacheable" 1 "Cached" "b" "true"
56update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Decorator.Replaceable" 1 "FieldReplaceable" "b" "true"
57update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Item" 1 "Present" "b" "true"
58update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Item" 1 "PrettyName" "s" "powersupply${arr[0]}"
59update_inventory "${arr[0]}" "xyz.openbmc_project.Inventory.Item.PowerSupply" 0
60