summaryrefslogtreecommitdiffstats
path: root/common/recipes-core/sensor-util/files/sensor-util.c
blob: eb38d65ffea59ba7b1b34257aaaa1cfaec06e7b8 (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
165
166
167
168
169
170
171
172
173
/*
 * 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 <openbmc/pal.h>

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

static void
print_single_sensor_reading(uint8_t fru, uint8_t *sensor_list, int sensor_cnt, uint8_t num) {

  int i;
  float fvalue;
  char name[24];
  char units[64];
  int ret = 0;

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

    if (sensor_list[i] == num) {
      ret = 1;
      pal_get_sensor_name(fru, sensor_list[i], name);
      pal_get_sensor_units(fru, sensor_list[i], units);

      if (pal_sensor_read(fru, sensor_list[i], &fvalue) < 0) {

        printf("pal_sensor_read failed: fru: %d num: 0x%X name: %-23s\n",
            fru, sensor_list[i], name);
      } else {
  	    printf("%-23s: %.2f %s\n", name, fvalue, units);
      }

      break;
    }
  }

  if (!ret) {
    printf("Wrong sensor number!\n");
    print_usage();
    exit(-1);
  }
}

static void
print_sensor_reading(uint8_t fru, uint8_t *sensor_list, int sensor_cnt) {

  int i;
  float fvalue;
  char name[24];
  char units[64];

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

    /* Clear the variable */
    sprintf(name, "");
    sprintf(units, "");

    pal_get_sensor_name(fru, sensor_list[i], name);
    pal_get_sensor_units(fru, sensor_list[i], units);

    if (pal_sensor_read(fru, sensor_list[i], &fvalue) < 0) {

      printf("pal_sensor_read failed: fru: %d num: 0x%X name: %-23s\n",
          fru, sensor_list[i], name);
    }   else {
  	  printf("%-23s: %.2f %s\n", name, fvalue, units);
    }
  }
}

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

  int ret;
  int sensor_cnt;
  uint8_t *sensor_list;
  uint8_t fru;
  uint8_t num;

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

  if (argc == 3) {
    errno = 0;
    num = (uint8_t) strtol(argv[2], NULL, 0);
    if (errno) {
      printf("Sensor number format incorrect.\n");
      print_usage();
      exit(-1);
    }
  }


  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) {

      if (fru == FRU_NIC) {
        printf("\nsensor-util does not support nic\n");
        exit(-1);
      }

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

      if (num) {
        print_single_sensor_reading(fru, sensor_list, sensor_cnt, num);
      } else {
        print_sensor_reading(fru, sensor_list, sensor_cnt);
      }

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

    if (fru == FRU_NIC) {
      printf("\nsensor-util does not support nic\n");
      //exit(-1);
    }

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

    if (num) {
      print_single_sensor_reading(fru, sensor_list, sensor_cnt, num);
    } else {
        print_sensor_reading(fru, sensor_list, sensor_cnt);
      }
  }

  return 0;
}
OpenPOWER on IntegriCloud