-
Notifications
You must be signed in to change notification settings - Fork 0
/
i2cset.c
87 lines (75 loc) · 1.77 KB
/
i2cset.c
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
#include <errno.h>
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/io.h>
#include "ipmi_i2c_commands.h"
void usage(char* program_name)
{
printf("Usage:\n");
printf(" %s <I2C_BUS> <I2C_ADDR> <I2C_REG> <VAL>\n", program_name);
}
int str_to_uint8(const char* str, uint8_t* result)
{
errno = 0;
char* temp;
unsigned long int val = strtoul(str, &temp, 0);
if (temp == str ||
*temp != '\0' ||
(val == ULONG_MAX && errno == ERANGE)
) {
return -1;
}
if ((val & 0xFF) != val) {
return -2;
}
*result = (uint8_t)(val & 0xFF);
return 0;
}
int main(int argc, char* argv[])
{
if (iopl(3)) {
fprintf(stderr, "Error! Need sudo priviliges\n");
exit(1);
}
if (argc != 5) {
fprintf(stderr, "Error! Not enough arguments\n");
usage(argv[0]);
exit(2);
}
uint8_t bus, addr, reg, val;
if (str_to_uint8(argv[1], &bus)) {
fprintf(stderr, "Error! Wrong number for the <I2C_BUS> value\n");
usage(argv[0]);
exit(3);
}
#ifndef EXTENDED_BUS_ID
if (bus > 7) {
fprintf(stderr, "Error! IPMI specification permits bus ID only in range 0..7\n");
fprintf(stderr, "If you want to use bus ID above 7, please recompile with EXTENDED_BUS_ID define\n");
fprintf(stderr, "Please see README.md for details\n");
exit(3);
}
#endif
if (str_to_uint8(argv[2], &addr)) {
fprintf(stderr, "Error! Wrong number for the <I2C_ADDR> value\n");
usage(argv[0]);
exit(4);
}
if (str_to_uint8(argv[3], ®)) {
fprintf(stderr, "Error! Wrong number for the <I2C_REG> value\n");
usage(argv[0]);
exit(5);
}
if (str_to_uint8(argv[4], &val)) {
fprintf(stderr, "Error! Wrong number for the <VAL> value\n");
usage(argv[0]);
exit(6);
}
int ret = write_i2c(bus, addr, reg, val);
if (ret) {
fprintf(stderr, "Error\n");
}
return ret;
}