summaryrefslogtreecommitdiffstats
path: root/common/recipes-core/sensor-util/files/sensor-util.c
blob: c37fc3821ea3a2765dd29d1c7038963449ede284 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
/*
 * yosemite-sensors
 *
 * Copyright 2015-present Facebook. All Rights Reserved.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <syslog.h>
#include <stdint.h>
#include <string.h>
#include <errno.h>
#include <stdbool.h>
#include <openbmc/pal.h>
#include <openbmc/sdr.h>

static int
print_usage() {
  printf("Usage: sensor-util [ %s ] <--threshold> <sensor num>\n",
      pal_fru_list);
}

static void
print_sensor_reading(float fvalue, thresh_sensor_t *thresh, bool threshold) {

  if (threshold) {
  	printf("%-23s: Curr: %.2f %-8s\t"
        "   UCR: %.2f "
        "   UNC: %.2f "
        "   UNR: %.2f "
        "   LCR: %.2f "
        "   LNC: %.2f "
        "   LNR: %.2f \n",
        thresh->name, fvalue, thresh->units,
        thresh->ucr_thresh,
        thresh->unc_thresh,
        thresh->unr_thresh,
        thresh->lcr_thresh,
        thresh->lnc_thresh,
        thresh->lnr_thresh);
  } else {
    printf("%-23s: %.2f %s\n", thresh->name, fvalue, thresh->units);
  }

}

static void
calculate_sensor_reading(uint8_t fru, uint8_t *sensor_list, int sensor_cnt, int num, bool threshold) {

  int i;
  uint8_t snr_num;
  float fvalue;
  char path[64];
  thresh_sensor_t thresh;

  for (i = 0; i < sensor_cnt; i++) {

    snr_num = sensor_list[i];

    /* If calculation is for a single sensor, ignore all others. */
    if (num && snr_num != num) {
      continue;
    }

    if (threshold) {
      if (sdr_get_snr_thresh(fru, snr_num, 0, &thresh))
        syslog(LOG_ERR, "sdr_init_snr_thresh failed for FRU %d num: 0x%X", fru, snr_num);
    } else {
      sdr_get_sensor_name(fru, sensor_list[i], thresh.name);
      sdr_get_sensor_units(fru, sensor_list[i], thresh.units);
    }

    usleep(50);

    if (pal_sensor_read(fru, snr_num, &fvalue) < 0) {
      printf("pal_sensor_read failed: FRU: %d num: 0x%X name: %-23s\n",
          fru, sensor_list[i], thresh.name, thresh.units);
      continue;
    }   else {
      print_sensor_reading(fvalue, &thresh, threshold);
    }
  }
}

int
main(int argc, char **argv) {

  int i;
  int ret;
  int sensor_cnt;
  uint8_t *sensor_list;
  uint8_t fru;
  uint8_t num = 0;
  bool threshold = false;

  if (argc < 2 || argc > 4) {
    print_usage();
    exit(-1);
  }

  i = 3; /* Starting at argument 3*/
  while (argc > 2 && i <= argc) {
    if (!(strcmp(argv[i-1], "--threshold"))) {
      threshold = true;
    } else {
      errno = 0;
      num = (uint8_t) strtol(argv[i-1], NULL, 0);
      if (errno) {
        print_usage();
        exit(-1);
      }
    }
    i++;
  }


  ret = pal_get_fru_id(argv[1], &fru);
  if (ret < 0) {
    print_usage();
    return ret;
  }

  if (fru == 0) {
    fru = 1;
    while (fru <= MAX_NUM_FRUS) {

      ret = pal_get_fru_sensor_list(fru, &sensor_list, &sensor_cnt);
      if (ret < 0) {
        return ret;
      }

      calculate_sensor_reading(fru, sensor_list, sensor_cnt, num, threshold);

      fru++;
      printf("\n");
    }
  } else {

    ret = pal_get_fru_sensor_list(fru, &sensor_list, &sensor_cnt);
    if (ret < 0) {
      return ret;
    }

    calculate_sensor_reading(fru, sensor_list, sensor_cnt, num, threshold);
  }

  return 0;
}
OpenPOWER on IntegriCloud