forked from audiohacked/OpenCorsairLink
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
209 lines (174 loc) · 6.22 KB
/
main.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
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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
* This file is part of OpenCorsairLink.
* Copyright (C) 2017 Sean Nelson <[email protected]>
* OpenCorsairLink 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
* any later version.
* OpenCorsairLink 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 OpenCorsairLink. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <libusb.h>
#include "options.h"
#include "device.h"
#include "driver.h"
#include "print.h"
#include "scan.h"
#include "lowlevel/asetek4.h"
#include "lowlevel/hid.h"
#include "lowlevel/rmi.h"
#include "protocol/hid/core.h"
#include "protocol/rmi/core.h"
int psu_settings(struct corsair_device_scan scanned_device, struct option_parse_return settings) {
int r;
int i;
char name[20];
name[sizeof(name) - 1] = 0;
uint32_t time = 0;
uint16_t supply_volts, supply_watts, temperature,
output_volts, output_amps, output_watts;
struct corsair_device_info *dev;
struct libusb_device_handle *handle;
dev = scanned_device.device;
handle = scanned_device.handle;
msg_debug("DEBUG: shortcuts set\n");
r = dev->driver->init(handle, dev->write_endpoint);
msg_debug("DEBUG: init done\n");
/* fetch device name, vendor name, product name */
r = dev->driver->vendor(dev, handle, name);
msg_info("Vendor: %s\n", name);
r = dev->driver->product(dev, handle, name);
msg_info("Product: %s\n", name);
r = dev->driver->fw_version(dev, handle, name);
msg_info("Firmware: %s\n", name);
msg_debug("DEBUG: string done\n");
/* fetch temperatures */
for (i=0; i<2; i++) {
r = dev->driver->temperature(dev, handle, i, &temperature);
msg_info("Temperature %d: %5.2f C\n", i, convert_bytes_double(temperature));
}
/* fetch device powered time and device uptime */
r = dev->driver->psu_time.powered(dev, handle, &time);
msg_info("Powered: %u (%dd. %dh)\n",
time, time/(24*60*60), time/(60*60)%24);
r = dev->driver->psu_time.uptime(dev, handle, &time);
msg_info("Uptime: %u (%dd. %dh)\n",
time, time/(24*60*60), time/(60*60)%24);
msg_debug("DEBUG: time done\n");
/* fetch Supply Voltage and Total Watts Consumming */
r = dev->driver->power.supply_voltage(dev, handle, &supply_volts);
msg_info("Supply Voltage: %5.2f\n", convert_bytes_double(supply_volts));
r = dev->driver->power.total_wattage(dev, handle, &supply_watts);
msg_info("Total Watts: %5.2f\n", convert_bytes_double(supply_watts));
msg_debug("DEBUG: supply done\n");
/* fetch PSU output */
for (i=0; i<3; i++) {
if (i==0)
msg_info("Output 12v:\n");
if (i==1)
msg_info("Output 5v:\n");
if (i==2)
msg_info("Output 3.3v:\n");
r = dev->driver->power.select(dev, handle, i);
r = dev->driver->power.voltage(dev, handle, &output_volts);
msg_info("\tVoltage %5.2f\n", convert_bytes_double(output_volts));
r = dev->driver->power.amperage(dev, handle, &output_amps);
msg_info("\tAmps %5.2f\n", convert_bytes_double(output_amps));
r = dev->driver->power.wattage(dev, handle, &output_watts);
msg_info("\tWatts %5.2f\n", convert_bytes_double(output_watts));
}
r = dev->driver->power.select(dev, handle, 0);
r = dev->driver->deinit(handle, dev->write_endpoint);
return 0;
}
int hydro_settings(struct corsair_device_scan scanned_device, struct option_parse_return settings) {
int r;
int i;
char name[20];
name[sizeof(name) - 1] = 0;
struct corsair_device_info *dev;
struct libusb_device_handle *handle;
uint16_t temperature;
double celsius = 0;
dev = scanned_device.device;
handle = scanned_device.handle;
msg_debug("DEBUG: shortcuts set\n");
r = dev->driver->init(handle, dev->write_endpoint);
msg_debug("DEBUG: init done\n");
/* fetch device name, vendor name, product name */
r = dev->driver->vendor(dev, handle, name);
msg_info("Vendor: %s\n", name);
r = dev->driver->product(dev, handle, name);
msg_info("Product: %s\n", name);
r = dev->driver->fw_version(dev, handle, name);
msg_info("Firmware: %s\n", name);
for (i=0; i<3; i++) {
r = dev->driver->temperature(dev, handle, i, &temperature);
if (dev->driver == &corsairlink_driver_asetek) {
uint8_t v1 = (temperature>>8);
uint8_t v2 = (temperature&0xFF);
msg_debug("DEBUG: %02X %02X\n", v1, v2);
celsius = (double)v1 + ((double)v2/10);
}
msg_info("Temperature %d: %5.2f\n", i, (double)celsius);
if (dev->driver == &corsairlink_driver_asetek) {
break;
}
}
r = dev->driver->led(dev, handle, &settings.led_color, &settings.warning_led, settings.warning_led_temp, (settings.warning_led_temp > -1));
if (dev->driver == &corsairlink_driver_asetek) {
if (settings.fan1.s6 != 0)
dev->driver->fan.custom(dev, handle, &settings.fan1);
if (settings.pump_mode != DEFAULT)
dev->driver->pump.profile(dev, handle, settings.pump_mode);
}
r = dev->driver->deinit(handle, dev->write_endpoint);
msg_debug("DEBUG: deinit done\n");
return 0;
}
int main(int argc, char *argv[])
{
int r; // result from libusb functions
int8_t device_number = -1;
struct option_flags flags;
struct option_parse_return settings;
options_parse(argc, argv, &flags, &device_number,
&settings);
libusb_context *context = NULL;
r = libusb_init(&context);
if (r < 0) {
msg_info("Init Error %d\n", r);
return 1;
}
libusb_set_debug(context, 3);
int device_count = corsairlink_device_scanner(context);
msg_debug("DEBUG: scan done, start routines\n");
msg_debug("DEBUG: device_number = %d\n", device_number);
if (device_number >= 0) {
if (scanlist[device_number].device->driver == &corsairlink_driver_rmi) {
psu_settings(scanlist[device_number], settings);
} else {
hydro_settings(scanlist[device_number], settings);
}
} else {
for (int i=0; i<device_count; i++) {
if (scanlist[i].device->driver == &corsairlink_driver_rmi) {
psu_settings(scanlist[i], settings);
} else {
hydro_settings(scanlist[i], settings);
}
}
}
exit:
corsairlink_close(context);
return 0;
}