-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpanel.c
More file actions
4908 lines (4371 loc) · 173 KB
/
Copy pathpanel.c
File metadata and controls
4908 lines (4371 loc) · 173 KB
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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include "protocol.h"
#include <time.h>
#include <unistd.h>
#include <string.h>
#include <stddef.h>
#include <sys/sysinfo.h>
#include <sys/statvfs.h>
#include <mntent.h>
#include <dirent.h>
#include <utmp.h>
#include <sys/stat.h>
#include <sys/mount.h>
#include <sys/statvfs.h>
#include <sys/io.h>
#include <fcntl.h>
#include <errno.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/mman.h>
#include <stdint.h>
#include <regex.h>
//异步HID通信
#include <pthread.h>
#include <signal.h>
#include <ctype.h>
#include <stdarg.h>
//Discrete GPU
#include <nvml.h>
#include <glob.h>
//WLAN
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <ifaddrs.h>
#include <arpa/inet.h>
#include <linux/ethtool.h>
#include <linux/sockios.h>
#include <libusb-1.0/libusb.h>
#define VENDORID 0x5448
#define PRODUCTID 0x0002
#define TIMEOUT_MS 5000 // 传输超时时间(毫秒)
#define EP_IN 0x81 // 批量输入端点
#define EP_OUT 0x02 // 批量输出端点
#define INTERFACE 0x00 // 接口号
#define DebugToken true
#define SensorLog true
#define IfNoPanel false
#define hidwritedebug true
#define OTA true
#define FIRMWARE_PATH "/usr/bin/firmware"
#define MAXLEN 0x40
#define DURATION 1
#define MAX_PATH 256
#define MAX_LINE 512
#define MAX_OTA_DATA 56
// ITE
#define ITE_EC_DATA_PORT 0x62
#define ITE_EC_INDEX_PORT 0x66
#define ITE_EC_CMD_PORT 0x66
// EC RAM
#define EC_CMD_READ_RAM 0x80 //Test for read CPUTemp
#define EC_CMD_WRITE_RAM 0x81
#define EC_CMD_QUERY 0x84
// 固件升级器结构体
typedef struct {
unsigned char sequence;
// 可以添加其他状态信息
} FirmwareUpgrader;
// CPU使用率计算结构体
typedef struct {
unsigned long user;
unsigned long nice;
unsigned long system;
unsigned long idle;
unsigned long iowait;
unsigned long irq;
unsigned long softirq;
} CPUData;
typedef struct {
char name[128];
int temperature; // 温度 (°C)
int utilization_gpu; // GPU使用率 (%)
int utilization_memory; // 显存使用率 (%)
long memory_used; // 已用显存 (MB)
long memory_total; // 总显存 (MB)
double power_draw; // 功耗 (W)
int fan_speed; // 风扇转速 (%)
char driver_version[32]; // 驱动版本
} nvidia_gpu_info_t;
#define MAX_INTERFACES 32
#define MAX_IP_LENGTH 46 // IPv6地址最大长度
// 网络接口信息结构体
typedef struct {
char interface_name[32];
char ip_address[INET_ADDRSTRLEN];
char netmask[INET_ADDRSTRLEN];
char mac_address[18];
char status[16];
unsigned long long rx_bytes;
unsigned long long tx_bytes;
unsigned long long rx_bytes_prev;
unsigned long long tx_bytes_prev;
time_t last_update;
double rx_speed_kb;
double tx_speed_kb;
double rx_total_mb;
double tx_total_mb;
int initialized; // 标记是否已初始化
} network_interface_t;
// 全局接口管理器
typedef struct {
network_interface_t *interfaces;
int count;
int capacity;
} interface_manager_t;
static unsigned char COMMLEN = offsetof(Request, common_data.data) - offsetof(Request, length);
void TimeSleep1Sec();
int safe_usb_read(unsigned char* buffer, int length, int timeout_ms);
void firmware_upgrader_init(FirmwareUpgrader* upgrader);
int firmware_upgrade(FirmwareUpgrader* upgrader,
const char* firmware_path,
unsigned char new_major, unsigned char new_minor,
unsigned char new_patch, unsigned char new_build);
int init_hidreport(Request *request, unsigned char cmd, unsigned char aim, unsigned char id);
int first_init_hidreport(Request* request, unsigned char cmd, unsigned char aim,unsigned char total,unsigned char order);
void append_crc(Request *request);
void appendEmpty_crc(Request *request);
unsigned char cal_crc(unsigned char * data, int len);
int get_cpu_temperature();
int read_temperature_from_hwmon(void);
int read_temperature_for_truenas(void);
int read_temperature_via_truenas_api(void);
void read_cpu_data(CPUData *data);
float calculate_cpu_usage(const CPUData *prev, const CPUData *curr);
int get_igpu_temperature();
float get_igpu_usage();
unsigned int get_memory_usage();
int GetUserCount();
int file_exists(const char *filename);
int read_file(const char *filename, char *buffer, size_t buffer_size);
//Disk
// 硬盘池信息结构体
#define MAX_POOLS 20
#define MAX_DISKS_PER_POOL 20
#define MAX_PATH 256
#define MAX_OUTPUT 8192
#define MAX_COMMAND 512 // 增加命令缓冲区大小
// 结构体定义
typedef struct {
char name[MAX_PATH];
char partuuid[MAX_PATH];
char device_path[MAX_PATH];
char disk_name[MAX_PATH];
int temperature;
} DiskInfo;
typedef struct {
char name[MAX_PATH];
unsigned long long total_size;
unsigned long long used_size;
unsigned long long free_size;
DiskInfo disks[MAX_DISKS_PER_POOL];
int disk_count;
int highest_temp;
} PoolInfo;
//Linux disk
typedef struct {
char device[32]; // 设备名 (sda, nvme0n1等)
char model[128]; // 硬盘型号
char serial[64]; // 序列号
char mountpoint[MAX_PATH]; // 挂载点
unsigned long long total_size; // 总容量 (bytes)
unsigned long long free_size; // 可用容量 (bytes)
unsigned long long used_size; // 已用容量 (bytes)
double usage_percent; // 使用百分比
int temperature; // 温度 (°C)
char type[16]; // 硬盘类型 (SATA/NVMe)
} disk_info_t;
disk_info_t disks[MAX_DISKS_PER_POOL];
int disk_count = 0;
int execute_command(const char* command, char* output, size_t output_size);
void trim_string(char* str);
int file_exists(const char* path);
int read_file(const char* path, char* buffer, size_t buffer_size);
char* extract_disk_name(const char* device_path);
int is_valid_device_name(const char* name);
int is_uuid_format(const char* str);
unsigned long long parse_size(const char* size_str);
int get_all_pools(PoolInfo* pools, int max_pools);
int get_pool_info(PoolInfo* pool);
int get_pool_disks_and_partuuids(PoolInfo* pool);
char* find_device_by_partuuid(const char* partuuid);
int get_disk_temperature(const char* disk_name);
int update_pool_temperatures(PoolInfo* pool);
void display_pool_info(const PoolInfo* pool);
void check_and_update_pools();
void rescan_all_pools();
PoolInfo pools[MAX_POOLS];
int pool_count;
int disk_maxtemp = 0;
void get_disk_identity(const char *device, char *model, char *serial);
unsigned long long get_disk_size(const char *device);
void get_mountpoint(const char *device, char *mountpoint);
int get_mountpoint_usage(const char *mountpoint, unsigned long long *total,
unsigned long long *free, unsigned long long *used);
int scan_disk_devices(disk_info_t *disks, int max_disks);
int refresh_linux_disks(disk_info_t *disks, int count);
//EC6266
int acquire_io_permissions();
void release_io_permissions();
int ec_wait_ready();
void ec_write_index(unsigned char index);
void ec_write_data(unsigned char data);
unsigned char ec_read_data();
int ec_ram_read_byte(unsigned char address, unsigned char *value);
int ec_ram_write_byte(unsigned char address, unsigned char value);
int ec_ram_read_block(unsigned char start_addr, unsigned char *buffer, int length);
int ec_ram_write_block(unsigned char start_addr, unsigned char *data, int length);
int ec_query_version(char *version, int max_len);
void nvidia_print_info();
//异步HID
void signal_handler(int sig);
void* usb_read_thread(void *arg);
int safe_usb_read_timeout(unsigned char *data, int length, int *actual_length, unsigned int timeout_ms);
int usb_bulk_transfer_with_retry(libusb_device_handle *handle,
unsigned char endpoint,
unsigned char *data,
int length,
int *transferred,
unsigned int timeout,
int max_retries);
int start_usb_read_thread();
void stop_usb_read_thread();
int safe_usb_write(unsigned char *data, int length);
void* usb_send_thread(void* arg);
void systemoperation(unsigned char time,unsigned char cmd);
//WLAN
static interface_manager_t g_iface_manager = {0};
network_interface_t *ifaces;
int init_network_monitor();
void display_interface_info(const char *ifname);
int register_interface(const char *ifname);
void monitor_all_interfaces();
int register_all_physical_interfaces();
int get_interface_basic_info(const char *ifname,
char *status,
char *mac_addr,
char *ip_addr,
char *netmask);
int get_interface_traffic_info(const char *ifname,
double *rx_speed_kb,
double *tx_speed_kb,
double *rx_total_mb,
double *tx_total_mb);
int get_registered_interfaces(char interfaces[][32], int max_interfaces);
void cleanup_network_monitor();
bool IsNvidiaGPU;
char PageIndex = 0;
//1Hour Count
int HourTimeDiv = 0;
static volatile bool running = true;
pthread_t read_thread,send_thread;
// 互斥锁(用于线程安全)
pthread_mutex_t usb_mutex = PTHREAD_MUTEX_INITIALIZER;
pthread_mutex_t hour_time_mutex = PTHREAD_MUTEX_INITIALIZER;
struct utmp *ut;
//Discrete GPU
typedef struct {
unsigned int index;
char name[64];
nvmlTemperatureSensors_t temp_sensor;
unsigned int temperature;
unsigned int utilization;
unsigned int memory_used;
unsigned int memory_total;
unsigned int power_usage;
unsigned int fan_speed;
} gpu_info_t;
int nvidia_smi_available();
int nvidia_get_gpu_temperature();
int nvidia_get_gpu_utilization();
int nvidia_get_gpu_fan_speed();
typedef struct {
char devicename[64];
char cpuname[128];
char operatename[128];
char serial_number[64];
} system_info_t;
void get_system_info(system_info_t *info);
unsigned char DGPUtemp = 0;
int cpusuage,cpufan,memoryusage;
bool Isinitial = false;
unsigned char cputemp = 0;
unsigned char buffer[MAXLEN] = {0};
unsigned char ack[MAXLEN] = {0};
system_info_t sys_info;
Request request;
CPUData prev_data, curr_data;
unsigned char Ver[4] = {0,0,0,0};
int OTASendCnt = 0,OTAReceiveCnt = 0;
bool OTAContinue = true;
unsigned char OTAFile[]={0xFF,0xCC};
static libusb_device_handle *handle = NULL;
static libusb_context *usb_context = NULL;
bool OTAEnable = false;
int initialUSB()
{
int retry_count;
int res;
// 初始化libusb
res = libusb_init(&usb_context);
if (res < 0) {
fprintf(stderr, "Failed to initialize libusb: %s\n", libusb_error_name(res));
return -1;
}
for (retry_count = 0; retry_count < 3; retry_count++) {
#if DebugToken
printf("Attempting to open USB device %04x:%04x... (Attempt %d/%d)\n",
VENDORID, PRODUCTID, retry_count + 1, 3);
#endif
// 使用libusb_open_device_with_vid_pid打开设备
handle = libusb_open_device_with_vid_pid(usb_context, VENDORID, PRODUCTID);
if (handle != NULL) {
#if DebugToken
printf("USB device opened successfully\n");
// 获取设备描述符信息
struct libusb_device_descriptor desc;
libusb_device *dev = libusb_get_device(handle);
libusb_get_device_descriptor(dev, &desc);
printf("Device information:\n");
printf(" VID: 0x%04x\n", desc.idVendor);
printf(" PID: 0x%04x\n", desc.idProduct);
printf(" bcdDevice: 0x%04x\n", desc.bcdDevice);
#endif
// 如果设备是HID设备,你可能需要声明接口
// 这通常需要root权限或正确的udev规则
res = libusb_kernel_driver_active(handle, 0);
if (res == 1) {
printf("Kernel driver active, detaching...\n");
libusb_detach_kernel_driver(handle, 0);
}
// 声明接口
res = libusb_claim_interface(handle, 0);
if (res < 0) {
printf("Failed to claim interface: %s\n", libusb_error_name(res));
libusb_close(handle);
handle = NULL;
} else {
printf("Interface claimed successfully\n");
break;
}
}
if (handle == NULL) {
printf("ERROR: Failed to open device %04x:%04x\n", VENDORID, PRODUCTID);
printf("Available USB devices:\n");
printf("----------------------------\n");
// 枚举所有USB设备
libusb_device **list;
ssize_t cnt = libusb_get_device_list(usb_context, &list);
if (cnt < 0) {
printf("Failed to get device list\n");
} else {
for (ssize_t i = 0; i < cnt; i++) {
libusb_device *device = list[i];
struct libusb_device_descriptor desc;
res = libusb_get_device_descriptor(device, &desc);
if (res < 0) continue;
printf("Device Found:\n");
printf(" VID: 0x%04x\n", desc.idVendor);
printf(" PID: 0x%04x\n", desc.idProduct);
// 尝试打开设备获取更多信息
libusb_device_handle *temp_handle;
if (libusb_open(device, &temp_handle) == 0) {
unsigned char str[256];
if (desc.iManufacturer) {
res = libusb_get_string_descriptor_ascii(temp_handle,
desc.iManufacturer,
str, sizeof(str));
if (res > 0) printf(" Manufacturer: %s\n", str);
}
if (desc.iProduct) {
res = libusb_get_string_descriptor_ascii(temp_handle,
desc.iProduct,
str, sizeof(str));
if (res > 0) printf(" Product: %s\n", str);
}
libusb_close(temp_handle);
}
printf(" Bus: %03d, Address: %03d\n",
libusb_get_bus_number(device),
libusb_get_device_address(device));
printf("\n");
}
libusb_free_device_list(list, 1);
}
printf("----------------------------\n");
}
if (handle == NULL) {
printf("Device open failed, retrying in 3 seconds...\n");
sleep(3);
}
}
if (handle == NULL) {
printf("ERROR: Failed to open device %04x:%04x after %d attempts\n",
VENDORID, PRODUCTID, 3);
} else {
printf("Device successfully opened and ready for communication\n");
}
// 清除端点halt状态
libusb_clear_halt(handle, EP_OUT);
libusb_clear_halt(handle, EP_IN);
// if (start_usb_read_thread() != 0) {
// printf("Failed to start USB read thread\n");
// } else {
// printf("USB read thread started successfully\n");
// }
}
// 在主函数发送数据前,先测试读取
int main() {
//initial USB first
if(initialUSB() == -1)
return -1;
//OTA first
int otapage;
otapage = init_hidreport(&request, SET, TIME_AIM, 255);
append_crc(&request);
memcpy(buffer, &request, otapage);
if (safe_usb_write(buffer, otapage) < 0) {
printf("Failed to write Time data\n");
}
unsigned char response[64];
int read_len = safe_usb_read(response, 64, 1500);
if (read_len > 0) {
for (int i = 0; i < read_len; i++) {
printf("%02X ", response[i]); // 打印16进制数据
}
printf("\n");
}
memset(response,0x0,64);
// 休眠1秒,但分段休眠以便及时响应退出
for (int i = 0; i < 10; i++) {
usleep(100000); // 100ms
}
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
otapage = first_init_hidreport(&request, GET, GetVer_AIM, 255, 255);
append_crc(&request);
memcpy(buffer, &request, otapage);
if (safe_usb_write(buffer, otapage) == -1) {
printf("Failed to write otapage data\n");
}
read_len = safe_usb_read(response, 64, 1500);
if (read_len > 0) {
Ver[0] = response[5];
Ver[1] = response[6];
Ver[2] = response[7];
Ver[3] = response[8];
for (int i = 0; i < read_len; i++) {
printf("%02X ", response[i]); // 打印16进制数据
}
printf("\n");
}
memset(response,0x0,64);
// 休眠1秒,但分段休眠以便及时响应退出
for (int i = 0; i < 10; i++) {
usleep(100000); // 100ms
}
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
printf("\n");
otapage = first_init_hidreport(&request, GET, GetVer_AIM, 255, 255);
append_crc(&request);
memcpy(buffer, &request, otapage);
if (safe_usb_write(buffer, otapage) == -1) {
printf("Failed to write otapage data\n");
}
read_len = safe_usb_read(response, 64, 1500);
if (read_len > 0) {
Ver[0] = response[5];
Ver[1] = response[6];
Ver[2] = response[7];
Ver[3] = response[8];
for (int i = 0; i < read_len; i++) {
printf("%02X ", response[i]); // 打印16进制数据
}
printf("\n");
}
memset(response,0x0,64);
// 休眠5秒,但分段休眠以便及时响应退出
for (int i = 0; i < 10; i++) {
usleep(100000); // 100ms
}
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
FirmwareUpgrader upgrader;
firmware_upgrader_init(&upgrader);
int result = -1;
// 执行升级
result = firmware_upgrade(&upgrader, FIRMWARE_PATH, 0, 3, 1, 0);
if (result == 0) {
printf("No need Update!!\n");
}
else if(result == 1)
{
printf("Update Success!!\n");
sleep(1);
printf("Wait panel restart 15S!!\n");
sleep(15);
// 获取 I/O 权限
acquire_io_permissions();
ec_ram_write_byte(0x59,0);//Cut Panel Power
// 释放 I/O 权限
release_io_permissions();
printf("Start Cut Panel Power 3S!!\n");
sleep(1);
printf("Cut Panel Power 2S!!\n");
sleep(1);
printf("Cut Panel Power 1S!!\n");
sleep(1);
printf("Enable Panel Power!!\n");
// 获取 I/O 权限
acquire_io_permissions();
ec_ram_write_byte(0x59,1);//Enable Panel Power
// 释放 I/O 权限
release_io_permissions();
printf("Re connect Panel!!\n");
sleep(1);
sleep(1);
sleep(1);
sleep(1);
sleep(1);
sleep(1);
sleep(1);
sleep(1);
initialUSB();
}
else
{
printf("Update Fail!!\n");
}
OTAEnable = false;
if (start_usb_read_thread() != 0) {
printf("Failed to start USB read thread\n");
} else {
printf("USB read thread started successfully\n");
}
IsNvidiaGPU = nvidia_smi_available();
// 扫描所有物理网络接口
if (init_network_monitor() < 0) {
printf("No WLAN Port!\n");
return -1;
}
// 注册所有物理接口
int registered = register_all_physical_interfaces();
monitor_all_interfaces();
printf("\n=== Network Interface Information ===\n");
if (g_iface_manager.count > 0 && g_iface_manager.interfaces != NULL) {
for (int i = 0; i < g_iface_manager.count; i++) {
printf("\n[INFO] Displaying interface %d/%d\n", i+1, g_iface_manager.count);
// 检查接口名是否有效
if (g_iface_manager.interfaces[i].interface_name[0] != '\0') {
display_interface_info(g_iface_manager.interfaces[i].interface_name);
} else {
printf("[WARN] Interface %d has empty name\n", i+1);
}
}
} else {
printf("[INFO] No network interfaces registered or available\n");
}
// 扫描硬盘设备
pool_count = get_all_pools(pools, MAX_POOLS);
if (pool_count == 0) {
printf("No storage pools found in the system.\n");
printf("Please check if ZFS is properly configured.\n");
//如果不是ZFS就尝试直接读磁盘信息
disk_count = scan_disk_devices(disks, 10);
if (disk_count <= 0) {
printf("Can not find disks\n");
}
}
printf("Found %d storage pool(s):\n", pool_count);
for (int i = 0; i < pool_count; i++) {
printf(" %d. %s\n", i + 1, pools[i].name);
}
for (int i = 0; i < pool_count; i++) {
printf("Processing pool: %s\n", pools[i].name);
printf("%s\n", "--------------------------------------");
// 2.1 获取池的基本信息
if (get_pool_info(&pools[i]) != 0) {
printf("Failed to get basic information for pool %s\n", pools[i].name);
continue;
}
// 2.2 获取池中所有磁盘及其 PARTUUID
int disk_count = get_pool_disks_and_partuuids(&pools[i]);
if (disk_count == 0) {
printf("No valid disks found in pool %s\n", pools[i].name);
continue;
}
printf("Found %d disk(s) in pool %s\n", disk_count, pools[i].name);
// 2.3 更新每个磁盘的温度信息
if (update_pool_temperatures(&pools[i]) == 0) {
printf("Warning: Failed to get temperature information for some disks\n");
}
// 2.4 显示池的详细信息
display_pool_info(&pools[i]);
printf("\n");
}
for (int i = 0; i < pool_count; i++)
{
short Tttotal,Uuused;
Tttotal = pools[i].total_size;
Uuused = pools[i].used_size;
printf("Total size:%d,Used size:%d\n",Tttotal,Uuused);
}
//DiskPage
#if DebugToken
printf("-----------------------------------DiskPage initial start-----------------------------------\n");
#endif
int diskforcount = 0;
if(pool_count != 0)
{
if(pool_count % 2 == 0)
diskforcount = pool_count / 2;
else
diskforcount = pool_count / 2 + 1;
}
else
{
if(disk_count % 2 == 0)
diskforcount = disk_count / 2;
else
diskforcount = disk_count / 2 + 1;
}
int diskpage;
for (int i = 0; i < diskforcount; i++) {
diskpage = first_init_hidreport(&request, SET, DiskPage_AIM, diskforcount, (i + 1));
append_crc(&request);
memcpy(buffer, &request, diskpage);
#if DebugToken
printf("-----------------------------------DiskPage send %d times-----------------------------------\n",(i+1));
printf("Diskpage Head: %x\n",request.header);
printf("sequence %d\n",request.sequence);
printf("lenth %d\n",request.length);
printf("cmd %d\n",request.cmd);
printf("aim %d\n",request.aim);
printf("order %d\n",request.DiskPage_data.order);
printf("total: %d\n \n",request.DiskPage_data.total);
printf("diskcount %d\n",request.DiskPage_data.diskcount);
printf("count: %d\n",request.DiskPage_data.count);
printf("DiskLength: %d\n",request.DiskPage_data.diskStruct[0].disklength);
printf("Diskid: %d\n",request.DiskPage_data.diskStruct[0].disk_id);
printf("Diskunit: %d\n",request.DiskPage_data.diskStruct[0].unit);
printf("Disktotal: %d\n",request.DiskPage_data.diskStruct[0].total_size);
printf("Diskused: %d\n",request.DiskPage_data.diskStruct[0].used_size);
printf("Disktemp: %d\n",request.DiskPage_data.diskStruct[0].temp);
printf("Diskname: %s\n",request.DiskPage_data.diskStruct[0].name);
if(pool_count-(i*2)>1)
{
printf("DiskLength: %d\n",request.DiskPage_data.diskStruct[1].disklength);
printf("Diskid: %d\n",request.DiskPage_data.diskStruct[1].disk_id);
printf("Diskunit: %d\n",request.DiskPage_data.diskStruct[1].unit);
printf("Disktotal: %d\n",request.DiskPage_data.diskStruct[1].total_size);
printf("Diskused: %d\n",request.DiskPage_data.diskStruct[1].used_size);
printf("Disktemp: %d\n",request.DiskPage_data.diskStruct[1].temp);
printf("Diskname: %s\n",request.DiskPage_data.diskStruct[1].name);
}
printf("CRC:%d\n",request.DiskPage_data.crc);
printf("Send %d time\n",(i+1));
#endif
if (safe_usb_write(buffer, diskpage) < 0) {
printf("Failed to write DiskPage data\n");
break;
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
}
#if DebugToken
printf("-----------------------------------DiskPage initial end-----------------------------------\n");
#endif
printf("User Count :%d\n",GetUserCount());
// 显示所有存储池信息
// if (volume_count == 0) {
// printf("No non-system ZFS volumes found.\n");
// } else {
// printf("Found %d ZFS volumes:\n", volume_count);
// printf("===============================================================\n");
// /* 显示结果 */
// for (int i = 0; i < volume_count; i++) {
// printf("Volume %d:\n", i + 1);
// printf(" Name: %s\n", volumes[i].vol_name);
// printf(" Full Path: %s\n", volumes[i].full_name);
// printf(" Parent Pool: %s\n", volumes[i].parent_pool);
// printf(" Total Capacity: %.2f GB\n", volumes[i].total_gb);
// printf(" Used Capacity: %.2f GB\n", volumes[i].used_gb);
// printf(" Available: %.2f GB\n", volumes[i].avail_gb);
// printf(" Disk IDs: %s\n", volumes[i].disk_ids);
// printf(" Disk UUIDs: %s\n", volumes[i].disk_uuids);
// printf(" ------------------------------------------\n");
// }
// }
//HomePage
#if DebugToken
printf("-----------------------------------HomePage initial start-----------------------------------\n");
#endif
int homepage = init_hidreport(&request, SET, TIME_AIM, 255);
append_crc(&request);
memcpy(buffer, &request, homepage);
if (safe_usb_write(buffer, homepage) < 0) {
printf("Failed to write HomePage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
#if DebugToken
printf("-----------------------------------HomePage initial end-----------------------------------\n");
#endif
//SystemPage
#if DebugToken
printf("-----------------------------------SystemPage initial start-----------------------------------\n");
#endif
int systempage1 = first_init_hidreport(&request, SET, SystemPage_AIM, 2, 1);
append_crc(&request);
memcpy(buffer, &request, systempage1);
if (safe_usb_write(buffer, systempage1) == -1) {
printf("Failed to write SystemPage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
#if DebugToken
printf("-----------------------------------SystemPage initial second-----------------------------------\n");
#endif
int systempage2 = first_init_hidreport(&request, SET, SystemPage_AIM, 2, 2);
append_crc(&request);
memcpy(buffer, &request, systempage2);
if (safe_usb_write(buffer, systempage2) == -1) {
printf("Failed to write SystemPage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
#if DebugToken
printf("-----------------------------------SystemPage initial end-----------------------------------\n");
#endif
//ModePage
#if DebugToken
printf("-----------------------------------ModePage initial start-----------------------------------\n");
#endif
int modepage = first_init_hidreport(&request, SET, ModePage_AIM, 255, 255);
append_crc(&request);
memcpy(buffer, &request, modepage);
if (safe_usb_write(buffer, modepage) < 0) {
printf("Failed to write ModePage data\n");
}
#if DebugToken
printf("-----------------------------------ModePage initial end-----------------------------------\n");
#endif
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
//WLANPage
#if DebugToken
printf("-----------------------------------WLANPage initial start-----------------------------------\n");
#endif
int wlanpage;
for (int i = 0; i < g_iface_manager.count; i++)
{
wlanpage = first_init_hidreport(&request, SET, WlanPage_AIM, g_iface_manager.count, i + 1);
append_crc(&request);
memcpy(buffer, &request, wlanpage);
if (safe_usb_write(buffer, wlanpage) < 0) {
printf("Failed to write WlanPage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
/* code */
}
#if DebugToken
printf("-----------------------------------WLANPage initial end-----------------------------------\n");
#endif
get_system_info(&sys_info);
#if DebugToken
printf("-----------------------------------InfoPage initial start-----------------------------------\n");
#endif
int infopage;
infopage = first_init_hidreport(&request, SET, InfoPage_AIM, 4, 1);
append_crc(&request);
memcpy(buffer, &request, infopage);
if (safe_usb_write(buffer, infopage) < 0) {
printf("Failed to write InfoPage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
infopage = first_init_hidreport(&request, SET, InfoPage_AIM, 4, 2);
append_crc(&request);
memcpy(buffer, &request, infopage);
if (safe_usb_write(buffer, infopage) < 0) {
printf("Failed to write InfoPage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
infopage = first_init_hidreport(&request, SET, InfoPage_AIM, 4, 3);
append_crc(&request);
memcpy(buffer, &request, infopage);
if (safe_usb_write(buffer, infopage) < 0) {
printf("Failed to write InfoPage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
infopage = first_init_hidreport(&request, SET, InfoPage_AIM, 4, 4);
append_crc(&request);
memcpy(buffer, &request, infopage);
if (safe_usb_write(buffer, infopage) < 0) {
printf("Failed to write InfoPage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
#if DebugToken
printf("-----------------------------------InfoPage initial end-----------------------------------\n");
#endif
otapage = first_init_hidreport(&request, GET, GetVer_AIM, 255, 255);
append_crc(&request);
memcpy(buffer, &request, otapage);
if (safe_usb_write(buffer, otapage) == -1) {
printf("Failed to write otapage data\n");
}
sleep(1);
memset(buffer, 0x0, sizeof(unsigned char) * MAXLEN);
memset(&request, 0x0, sizeof(Request));
// 创建读取线程
#if !IfNoPanel
if (pthread_create(&send_thread, NULL, usb_send_thread, handle) != 0) {
printf("Failed to create send thread\n");
return -1;
}
#endif
while (running) {
usleep(100000); // 100ms
}
// 清理资源
if (read_thread) {
pthread_join(read_thread, NULL);
}
if (send_thread) {
stop_usb_read_thread();
}
libusb_release_interface(handle, 0);
libusb_close(handle);
libusb_exit(usb_context);
return handle == NULL ? 1 : 0;
}
// 关闭USB设备
void close_usb_device() {
if (handle != NULL) {
// 释放接口
libusb_release_interface(handle, INTERFACE);
// 重新附加内核驱动(如果之前分离了)
if (libusb_kernel_driver_active(handle, INTERFACE) == 0) {
libusb_attach_kernel_driver(handle, INTERFACE);
}
// 关闭设备
libusb_close(handle);
handle = NULL;
}
if (usb_context != NULL) {
libusb_exit(usb_context);
usb_context = NULL;
}
printf("USB device closed\n");
}
void TimeSleep1Sec()
{
pthread_mutex_lock(&hour_time_mutex);
if(HourTimeDiv == 3061)
HourTimeDiv = 0;
HourTimeDiv ++;
pthread_mutex_unlock(&hour_time_mutex);
printf("Time:%ld\n",(time(NULL) + 28800));
// 休眠1秒,但分段休眠以便及时响应退出
for (int i = 0; i < 10 && running; i++) {
usleep(100000); // 100ms
}
}
// 初始化固件升级器
void firmware_upgrader_init(FirmwareUpgrader* upgrader) {
upgrader->sequence = 0;
}
// 获取下一个序列号
unsigned char get_next_sequence(FirmwareUpgrader* upgrader) {
upgrader->sequence = (upgrader->sequence + 1) & 0xFF;
return upgrader->sequence;
}
// 封装读取函数,兼容之前的接口
int safe_usb_read(unsigned char* buffer, int length, int timeout_ms) {
int actual_length = 0;
int result = safe_usb_read_timeout(buffer, length, &actual_length, timeout_ms);
if (result == LIBUSB_SUCCESS) {
return actual_length; // 返回实际读取的字节数
} else {
return -1; // 返回错误
}
}
// 发送固件数据
int send_firmware_data(FirmwareUpgrader* upgrader,
const unsigned char* firmware,
uint32_t total_size,
void (*progress_callback)(int, int)) {
uint32_t sent_size = 0;
unsigned char chunk[57];
unsigned char buffer[64];
unsigned char response[64];
unsigned char response_data[56];
int data_len = 0;
int error_code = 0;