-
Notifications
You must be signed in to change notification settings - Fork 2
/
uhid-i2c-gamepad.c
1521 lines (1313 loc) · 79.4 KB
/
uhid-i2c-gamepad.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
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
/*
FreeplayTech UHID gamepad driver
This program sets up a gamepad device in the sytem using UHID-kernel interface.
Current version is mainly meant to be used with FreeplayTech gen2 device (embedded ATTINY controller for IO/ADC management).
Notes when using Pi Zero 2 W and willing to use WiringPi for interrupt:
You may need to clone and compile for unofficial github repository as official WiringPi ended development, please refer to: https://github.com/PinkFreud/WiringPi
Notes about ADCs (MCU or external), comments relative to current state of development, may change in the future:
- Driver/Diagnostic program are designed to only handle unsigned ADC values.
- Driver should be able to work upto 32bits(unsigned) ADC resolution.
- Diagnostic program is limited to 16bits(unsigned) because of terminal size limitations (mainly thinked to work on 640x480 screen, 80 cols by 30 lines).
Notes specific to driver part:
- At driver start, file shm_path(driver_main.h)/status will be created.
This file content will be set to "1" at driver start and "0" when driver closes.
If content set to "2" by external program, driver will enter lock state, a last report will set all inputs to "no pressed" (center value for analog).
To exit lock state, set back content to "1".
- Automatically reload configuation file if its modification time changes (doesn't affect I2C related settings).
*/
#include <unistd.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <dirent.h>
#include <ctype.h>
#include <errno.h>
#include <limits.h>
#include <signal.h>
#include <string.h>
#include <time.h>
#include <linux/uhid.h>
#include <linux/i2c-dev.h>
#include <i2c/smbus.h>
#include <termios.h>
#include "driver_i2c_registers.h"
#include "driver_debug_print.h"
#include "driver_config.h"
#include "nns_config.h"
#ifdef ALLOW_EXT_ADC //external ADC
#include "driver_adc_external.h"
#endif
#ifndef ALLOW_MCU_SEC_I2C
#undef USE_SHM_REGISTERS //can't use shm register "bridge" with MCU secondary feature
#endif
#ifdef DIAG_PROGRAM
#undef USE_WIRINGPI
#undef USE_GPIOD
#undef USE_SHM_REGISTERS
#else
#include "driver_hid_desc.h"
#endif
#if defined(USE_WIRINGPI) && defined(USE_GPIOD) //irq
#error "Only one kind of IRQ can be enabled at once, please refer to README.md for more informations."
#elif defined(USE_WIRINGPI)
#include <wiringPi.h>
#elif defined(USE_GPIOD)
#include <gpiod.h>
struct gpiod_chip *gpiod_chip;
struct gpiod_line *gpiod_input_line;
int gpiod_fd = -1;
char gpiod_consumer_name[128];
#ifdef ALLOW_MCU_SEC_I2C
struct gpiod_line *gpiod_lowbatt_line;
int gpiod_lowbatt_fd = -1;
char gpiod_consumer_lowbatt_name[128];
#endif
#endif
#include "driver_main.h"
//Debug related functions
/*
static void debug_print_binary_int(int val, int bits, char* var){ //print given var in binary format
if(!debug) return;
printf("DEBUG: BIN: %s : ", var); for(int i = bits-1; i > -1; i--){printf("%d", (val >> i) & 0b1);} printf("\n");
}
static void debug_print_binary_int_term (int line, int col, int val, int bits, char* var){ //print given var in binary format at given term position
printf("\e[%d;%dH\e[1;100m%s : ", line, col, var); for(int i = bits-1; i > -1; i--){printf("%d", (val >> i) & 0b1);} printf("\e[0m");
}
*/
//Time related functions
double get_time_double(void){ //get time in double (seconds), takes around 82 microseconds to run
struct timespec tp; int result = clock_gettime(CLOCK_MONOTONIC, &tp);
if (result == 0) {return tp.tv_sec + (double)tp.tv_nsec/1e9;}
return -1.; //failed
}
//UHID related functions
#ifndef DIAG_PROGRAM
static int uhid_create(int fd){ //create uhid device
if (!io_fd_valid(fd)){return -EIO;}
struct uhid_event ev;
memset(&ev, 0, sizeof(ev));
ev.type = UHID_CREATE2;
char buffer[strlen(uhid_device_name) + int_digit_count(uhid_device_id) + 2];
sprintf(buffer, "%s %d", uhid_device_name, uhid_device_id);
strcpy((char*)ev.u.create2.name, buffer);
memcpy(ev.u.create2.rd_data, hid_descriptor, sizeof(hid_descriptor));
ev.u.create2.rd_size = sizeof(hid_descriptor);
ev.u.create2.bus = BUS_USB;
ev.u.create2.vendor = hid_vendor;
ev.u.create2.product = hid_product;
ev.u.create2.version = 0;
ev.u.create2.country = 0;
return uhid_write(fd, &ev);
}
static void uhid_destroy(int fd){ //close uhid device
if (!io_fd_valid(fd)){return;} //already closed
struct uhid_event ev; memset(&ev, 0, sizeof(ev));
ev.type = UHID_DESTROY;
int ret = uhid_write(fd, &ev);
if (ret < 0){print_stderr("failed to destroy uhid device, errno:%d (%s)\n", -ret, strerror(-ret));}
print_stderr("uhid device destroyed\n");
}
static int uhid_write(int fd, const struct uhid_event* ev){ //write data to uhid device
//if (!io_fd_valid(fd)){return -EIO;} //already done in uhid_send_event()
ssize_t ret = write(fd, ev, sizeof(*ev));
if (ret < 0){print_stderr("write to uhid device failed with errno:%d (%m)\n", -ret);
} else if (ret != sizeof(*ev)){
print_stderr("wrong size wrote to uhid device: %zd != %zu\n", ret, sizeof(ev));
return -EFAULT;
}
return ret;
}
#endif
int uhid_send_event(int fd){ //send event to uhid device
if (uhid_disabled){return 0;}
if (!io_fd_valid(fd)){return -EIO;}
struct uhid_event ev;
memset(&ev, 0, sizeof(ev));
ev.type = UHID_INPUT2;
const int adc_size = sizeof(uint16_t);
int index = 0;
ev.u.input2.data[index++] = gamepad_report.buttons7to0; //digital msb
ev.u.input2.data[index++] = gamepad_report.buttons15to8; //digital lsb
ev.u.input2.data[index++] = gamepad_report.hat0; //dpad
memcpy(&ev.u.input2.data[index], &gamepad_report.left_x, adc_size); //x1
memcpy(&ev.u.input2.data[index+2], &gamepad_report.left_y, adc_size); //y1
memcpy(&ev.u.input2.data[index+4], &gamepad_report.right_x, adc_size); //x2
memcpy(&ev.u.input2.data[index+6], &gamepad_report.right_y, adc_size); //y2
index += adc_size*4;
#ifdef uhid_buttons_misc_enabled //defined in driver_config.h
ev.u.input2.data[index++] = gamepad_report.buttonsmisc; //digital misc
#endif
ev.u.input2.size = index;
#ifndef DIAG_PROGRAM
return uhid_write(fd, &ev);
#else
return write(fd, &ev, sizeof(ev));
#endif
}
//I2C related
int i2c_check_bus(int bus, int* bus_found){ //check I2C bus, return errno, set bus_found to NULL to disable bus searching
char fd_path[strlen(def_i2c_bus_path_format)+5];
bool quiet_mode_back = quiet_mode; quiet_mode = true; //disable output
if (bus < 0 || bus > 255){bus = 0;}
int bus_tmp = -1, fd = -1, tmp_addr_main = 0, tmp_addr_sec = 0;
//test provided bus num first
sprintf(fd_path, def_i2c_bus_path_format, bus);
fd = open(fd_path, O_RDWR);
if (io_fd_valid(fd) && mcu_search_i2c_addr(bus, &tmp_addr_main, &tmp_addr_sec) == 0){bus_tmp = bus;}
close(fd);
if (bus_tmp == -1 && bus_found != NULL){ //given num failed, search
double start = get_time_double();
for (int i=0; i<256; i++){
if (get_time_double() - start > 10.){quiet_mode = quiet_mode_back; print_stderr("stopped because timed out, bus:%d\n", i-1); break;} //timeout
sprintf(fd_path, def_i2c_bus_path_format, i);
fd = open(fd_path, O_RDWR);
if (io_fd_valid(fd) && mcu_search_i2c_addr(i, &tmp_addr_main, &tmp_addr_sec) == 0){close(fd); bus_tmp = i; break;}
close(fd);
}
}
if (bus_found != NULL){*bus_found = bus_tmp;}
quiet_mode = quiet_mode_back; //restore output
if (bus_tmp == -1){print_stderr("failed to found proper I2C bus\n"); errno = ENOENT; return -1;
} else {print_stderr("I2C bus '%d' found\n", bus_tmp);}
return 0;
}
int i2c_open_dev(int* fd, int bus, int addr){ //open I2C device, return 0 on success, -1:bus, -2:addr, -3:generic error
if (bus < 0){print_stderr("invalid I2C bus:%d\n", bus); errno = ENOENT; return -1;} //invalid bus
if (addr < 0 || addr > 127){print_stderr("invalid I2C address:0x%02X (%d)\n", addr, addr); errno = EREMOTEIO; return -2;} //invalid address
char fd_path[strlen(def_i2c_bus_path_format)+4]; sprintf(fd_path, def_i2c_bus_path_format, bus);
close(*fd); *fd = open(fd_path, O_RDWR);
if (!io_fd_valid(*fd)){print_stderr("failed to open '%s', errno:%d (%s)\n", fd_path, -*fd, strerror(-*fd)); *fd = -1; return -1;}
if (ioctl(*fd, i2c_ignore_busy ? I2C_SLAVE_FORCE : I2C_SLAVE, addr) < 0){ //invalid address
close(*fd); *fd = -1;
print_stderr("ioctl failed for address 0x%02X, errno:%d (%m)\n", addr, errno);
return -2;
}
if (i2c_smbus_read_byte_data(*fd, 0) < 0){ //invalid address
i2c_allerrors_count++; close(*fd); *fd = -2;
print_stderr("failed to read from address 0x%02X, errno:%d (%m)\n", addr, errno);
return -2;
}
print_stderr("I2C address:0x%02X opened\n", addr);
return 0;
}
void i2c_close_all(void){ //close all I2C files
int* fd_array[] = {&mcu_fd,
#ifdef ALLOW_MCU_SEC_I2C
&mcu_fd_sec,
#endif
#ifdef ALLOW_EXT_ADC
&adc_fd[0], &adc_fd[1], &adc_fd[2], &adc_fd[3],
#endif
};
int* addr_array[] = {&mcu_addr,
#ifdef ALLOW_MCU_SEC_I2C
&mcu_addr_sec,
#endif
#ifdef ALLOW_EXT_ADC
&adc_addr[0], &adc_addr[1], &adc_addr[2], &adc_addr[3],
#endif
};
for (int8_t i=0; i<(sizeof(fd_array)/sizeof(fd_array[0])); i++){
if(*fd_array[i] >= 0){ //"valid" fd
int ret = close(*fd_array[i]);
if (ret < 0){print_stderr("failed to close I2C handle for address 0x%02X, errno:%d (%m)\n", *addr_array[i], -ret);}
print_stderr("I2C handle for address:0x%02X closed\n", *addr_array[i]);
*fd_array[i] = -1;
}
}
}
void adc_data_compute(int adc_index){ //compute adc max value, flat in/out
if (!adc_params[adc_index].enabled){return;}
adc_params[adc_index].res_limit = 0xFFFFFFFF >> (32 - adc_params[adc_index].res); //compute adc limit
if(adc_params[adc_index].max > adc_params[adc_index].res_limit) {
print_stderr("WARNING: adc%d_max (%d) over ADC resolution (%dbits:%u), limited to said resolution\n", adc_index, adc_params[adc_index].max, adc_params[adc_index].res, adc_params[adc_index].res_limit);
adc_params[adc_index].max = adc_params[adc_index].res_limit;
} else {print_stderr("ADC%d resolution: %dbits (%u)\n", adc_index, adc_params[adc_index].res, adc_params[adc_index].res_limit);}
unsigned int adc_halfres = (adc_params[adc_index].res_limit / 2);
int_constrain(&adc_params[adc_index].flat_in, 0, 35); int_constrain(&adc_params[adc_index].flat_out, 0, 35); //limit flat to 0-35
adc_params[adc_index].flat_in_comp = adc_halfres * adc_params[adc_index].flat_in / 100; //compute inside flat
adc_params[adc_index].flat_out_comp = (adc_halfres * (100 + adc_params[adc_index].flat_out) / 100) - adc_halfres; //compute outside flat
if (adc_params[adc_index].flat_in_comp < 0){adc_params[adc_index].flat_in_comp = 0;}
if (adc_params[adc_index].flat_out_comp < 0){adc_params[adc_index].flat_out_comp = 0;}
print_stderr("ADC%d computed flats: inside:%d, outside:%d\n", adc_index, adc_params[adc_index].flat_in_comp, adc_params[adc_index].flat_out_comp);
if(adc_params[adc_index].autocenter){adc_params[adc_index].offset = adc_params[adc_index].raw - (adc_params[adc_index].res_limit / 2); //auto center
} else {adc_params[adc_index].offset = (((adc_params[adc_index].max - adc_params[adc_index].min) / 2) + adc_params[adc_index].min) - (adc_params[adc_index].res_limit / 2);}
print_stderr("ADC%d computed offset:%d\n", adc_index, adc_params[adc_index].offset);
logs_write("-ADC%d (%s): resolution:%d(%d), min:%d, max:%d, flat(inner):%d%%, flat(outer):%d%%, reversed:%d, autocenter:%d (offset:%d)\n", adc_index, (adc_fd_valid[adc_index])?"extern":"mcu", adc_params[adc_index].res, adc_params[adc_index].res_limit, adc_params[adc_index].min, adc_params[adc_index].max, adc_params[adc_index].flat_in, adc_params[adc_index].flat_out, adc_params[adc_index].reversed?1:0, adc_params[adc_index].autocenter?1:0, adc_params[adc_index].offset);
}
static int adc_defuzz(int value, int old_val, int fuzz){ //apply fuzz, based on input_defuzz_abs_event(): https://elixir.bootlin.com/linux/latest/source/drivers/input/input.c#L56
if (fuzz) {
if (value > old_val - fuzz / 2 && value < old_val + fuzz / 2){return old_val;}
if (value > old_val - fuzz && value < old_val + fuzz){return (old_val * 3 + value) / 4;}
if (value > old_val - fuzz * 2 && value < old_val + fuzz * 2){return (old_val + value) / 2;}
}
return value;
}
int adc_correct_offset_center(int adc_resolution, int adc_value, int adc_min, int adc_max, int adc_offset, int flat_in, int flat_out){ //apply offset center, expand adc range, inside/ouside flat, flat_in/out are values relative to adc resolution (not percent)
int max = adc_resolution / 2, offset = max + adc_offset;
adc_value -= offset; //center adc value to 0
int dir = 1, limit;
if (adc_value < 0){dir = -1; adc_value *= -1; limit = (adc_min - offset) * -1; //convert adc value to positive
} else {limit = adc_max - offset;}
if(limit==0){limit=1;} //avoid div by 0
adc_value = adc_value * (max + flat_out) / limit; //convert to 0->adc_min/adc_max to 0->adc_resolution+(outside flat)
if (flat_in > 0){adc_value = (adc_value - flat_in) * (max + flat_out) / ((max + flat_out) - flat_in);} //convert to (-inside flat)->adc_resolution+(outside flat) to 0->adc_resolution+(outside flat)
if(adc_value < 0){adc_value = 0;} adc_value *= dir; adc_value += max; //reapply proper direction and recenter value to adc_resolution/2
if(adc_value < 0){return 0;} else if (adc_value > adc_resolution){return adc_resolution;} //limit return from 0 to adc_resolution
return adc_value;
}
void i2c_poll_joystick(bool force_update){ //poll data from i2c device
poll_clock_start = get_time_double();
uint32_t inputs;
bool update_digital = true, update_adc = false;
if (!i2c_disabled){
uint8_t *mcu_registers_ptr = (uint8_t*)&i2c_joystick_registers; //pointer to i2c store
uint8_t read_limit = input_registers_count;
bool irq_triggered = true;
if (irq_enable && !(force_update || i2c_poll_rate_disable)){
#ifdef USE_WIRINGPI
if(digitalRead(irq_gpio) == HIGH){irq_triggered = false;} //no button pressed
#elif defined(USE_GPIOD)
struct gpiod_line_event gpiod_event;
if (gpiod_fd >= 0 && gpiod_line_event_read_fd(gpiod_fd, &gpiod_event) >= 0 && gpiod_event.event_type != GPIOD_LINE_EVENT_FALLING_EDGE){irq_triggered = false;}
#endif
}
if (!irq_triggered){
mcu_registers_ptr += read_limit; read_limit = 0; //shift register
update_digital = false;
}
if ((mcu_adc_read[0] || mcu_adc_read[1]) && (force_update || i2c_adc_poll_loop == i2c_adc_poll || i2c_poll_rate_disable)){update_adc = true; i2c_adc_poll_loop = 0;}
if (update_adc){
if (update_digital){
if (mcu_adc_read[1]){read_limit += 6;} else if (mcu_adc_read[0]){read_limit += 3;}
} else {
if (mcu_adc_read[0]){read_limit += 3;} else {mcu_registers_ptr += 3;}
if (mcu_adc_read[1]){read_limit += 3;}
}
}
if (read_limit > 0){
if (i2c_smbus_read_i2c_block_data(mcu_fd, mcu_registers_ptr - (uint8_t*)&i2c_joystick_registers, read_limit, mcu_registers_ptr) < 0){
i2c_errors_count++; i2c_allerrors_count++;
if (errno == 6){print_stderr("FATAL: i2c_smbus_read_i2c_block_data() failed, errno:%d (%m)\n", errno); kill_requested = true;}
if (i2c_errors_count >= i2c_errors_report){print_stderr("WARNING: I2C requests failed %d times in a row\n", i2c_errors_count); i2c_errors_count = 0;}
i2c_last_error = errno; return;
}
if (i2c_errors_count > 0){print_stderr("last I2C error: %d (%s)\n", -i2c_last_error, strerror(-i2c_last_error)); i2c_last_error = i2c_errors_count = 0;} //report last i2c error if high error count
if (update_digital){
inputs = (i2c_joystick_registers.input2 << 16) + (i2c_joystick_registers.input1 << 8) + i2c_joystick_registers.input0; //merge to ease work
if (inputs != mcu_input_digital_prev){mcu_input_digital_prev = inputs;} else {update_digital = false;} //only update if needed
}
}
} else { //uhid stress mode
uint8_t tmp; update_adc = true;
if (poll_stress_loop > 0){tmp=0xFF; poll_stress_loop=0;} else {tmp=0; poll_stress_loop++;} //toogle all data between 0 and 255
for (int i=0; i<=input_registers_count+6; i++){((uint8_t *)&i2c_joystick_registers)[i] = tmp;} //write data to registers struct
}
if (update_digital){
//dpad lookup
// U+L UP U+R
// 8 1 2
// LEFT 7 0 3 RIGHT
// 6 5 4
// D+L DOWN D+R
const uint8_t dpad_lookup[16] = {0/*0:none*/, 1/*1:up*/, 5/*2:down*/, 1/*3:up+down*/, 7/*4:left*/, 8/*5:up+left*/, 6/*6:down+left*/, 1/*7:up+down+left*/, 3/*8:right*/, 2/*9:up+right*/, 4/*10:down+right*/, 1/*11:up+down+right*/, 7/*12:left+right*/, 1/*13:up+left+right*/, 5/*14:down+left+right*/, 0/*15:up+down+left+right*/};
gamepad_report.hat0 = dpad_lookup[(uint8_t)(~(inputs >> mcu_input_dpad_start_index) & 0x0F)];
//digital: reorder raw input to ev mapping
uint16_t input_report_digital = 0xFFFF;
#ifdef uhid_buttons_misc_enabled //defined in driver_config.h
uint8_t input_report_digital_misc = 0xFF;
#endif
for (int i=0; i<mcu_input_map_size; i++){
int16_t curr_input = mcu_input_map[i];
if (curr_input != -127 && ~(inputs >> i) & 0b1){
if (curr_input >= BTN_GAMEPAD && curr_input < BTN_THUMBR + 1){input_report_digital &= ~(1U << (abs(curr_input - BTN_GAMEPAD)));} //gamepad
#ifdef uhid_buttons_misc_enabled
else if (curr_input >= BTN_MISC && curr_input < BTN_9 + 1){input_report_digital_misc &= ~(1U << (abs(curr_input - BTN_MISC)));} //misc
#endif
}
}
gamepad_report.buttons7to0 = ~(input_report_digital & 0xFF);
gamepad_report.buttons15to8 = ~(input_report_digital >> 8);
#ifdef uhid_buttons_misc_enabled
gamepad_report.buttonsmisc = ~input_report_digital_misc;
#endif
}
//analog
if (update_adc){
update_adc = false;
for (int i=0; i<4; i++){ //adc loop
if (adc_params[i].enabled){
bool adc_failed = false;
#ifdef ALLOW_EXT_ADC
if (!adc_fd_valid[i]){ //read mcu adc value
#endif
uint8_t *tmpPtr = (uint8_t*)&i2c_joystick_registers; //pointer to i2c store
uint8_t tmpPtrShift = i, tmpMask = 0xF0, tmpShift = 0; //pointer offset, lsb mask, lsb bitshift
if (i < 2){tmpPtr += input_registers_count/*mcu_i2c_register_adc0*/;} else {tmpPtr += input_registers_count + 3/*mcu_i2c_register_adc2*/; tmpPtrShift -= 2;} //update pointer
if (tmpPtrShift == 0){tmpMask = 0x0F; tmpShift = 4;} //adc0-2 lsb
adc_params[i].raw = ((*(tmpPtr + tmpPtrShift) << 8) | (*(tmpPtr + 2) & tmpMask) << tmpShift) >> (16 - adc_params[i].res); //char to word
#ifdef ALLOW_EXT_ADC
} else if (adc_type_funct_read[adc_type[i]](adc_fd[i], &adc_params[i]) < 0){adc_failed = true;} //external adc value, read failed
#endif
if (!adc_failed){
if (adc_firstrun){adc_data_compute(i); adc_params[i].raw_prev = adc_params[i].raw;} //compute adc max value, flat in/out, offset
if(adc_params[i].raw < adc_params[i].raw_min){adc_params[i].raw_min = adc_params[i].raw;} //update min value
if(adc_params[i].raw > adc_params[i].raw_max){adc_params[i].raw_max = adc_params[i].raw;} //update max value
adc_params[i].value = adc_defuzz(adc_params[i].raw, adc_params[i].raw_prev, adc_params[i].fuzz); adc_params[i].raw_prev = adc_params[i].raw; //defuzz
adc_params[i].value = adc_correct_offset_center(adc_params[i].res_limit, adc_params[i].value, adc_params[i].min, adc_params[i].max, adc_params[i].offset, adc_params[i].flat_in_comp, adc_params[i].flat_out_comp); //re-center adc value, apply flats and extend to adc range
if(adc_params[i].reversed){adc_params[i].value = abs(adc_params[i].res_limit - adc_params[i].value);} //reverse value
adc_params[i].value <<= 16 - adc_params[i].res; //convert to 16bits value for report
if (adc_params[i].value < 1){adc_params[i].value = 1;} else if (adc_params[i].value > 0xFFFF-1){adc_params[i].value = 0xFFFF-1;} //Reicast overflow fix
if(adc_map[i] > -1){ //adc mapped to -1 should be disabled during runtime if not in diagnostic mode
*js_values[adc_map[i]] = (uint16_t)adc_params[i].value; update_adc = true;
}
}
}
}
adc_firstrun = false;
}
i2c_adc_poll_loop++;
//report
i2c_poll_duration = get_time_double() - poll_clock_start;
#ifndef DIAG_PROGRAM
if (update_digital || update_adc){uhid_send_event(uhid_fd);} //uhid update
#endif
}
//MCU related functions
int mcu_search_i2c_addr(int bus, int* addr_main, int* addr_sec){ //search mcu on given i2c bus, return -1 on failure, 0 on success
*addr_main = *addr_sec = -1;
double start = get_time_double();
if (bus < 0){print_stderr("invalid I2C bus:%d\n", bus); return -1;} //invalid bus
char fd_path[strlen(def_i2c_bus_path_format)+4]; sprintf(fd_path, def_i2c_bus_path_format, bus);
int fd = open(fd_path, O_RDWR);
if (!io_fd_valid(fd)){print_stderr("failed to open '%s', errno:%d (%s)\n", fd_path, -fd, strerror(-fd)); return -1;}
int tmp_reg_manuf_main = offsetof(struct i2c_joystick_register_struct, manuf_ID) / sizeof(uint8_t); //main register manuf_ID
#ifdef ALLOW_MCU_SEC_I2C
int tmp_reg_manuf_sec = offsetof(struct i2c_secondary_address_register_struct, manuf_ID) / sizeof(uint8_t); //secondary register manuf_ID
#endif
for (int addr=0; addr <=127; addr++){
if (get_time_double() - start > 5.){print_stderr("stopped because timed out, bus:%d, addr:0x%02X\n", bus, addr-1); break;} //timeout
#ifdef ALLOW_MCU_SEC_I2C
if (ioctl(fd, I2C_SLAVE_FORCE, addr) < 0){print_stderr("ioctl failed for address 0x%02X, errno:%d (%m)\n", addr, errno); continue;} //invalid address
if (i2c_smbus_read_byte_data(fd, tmp_reg_manuf_sec) != mcu_manuf){continue;} //invalid manuf_id
int ret = i2c_smbus_read_byte_data(fd, mcu_sec_register_secondary_i2c_addr); if (ret != addr){continue;} //register content needs to match secondary address
*addr_sec = addr;
ret = i2c_smbus_read_byte_data(fd, mcu_sec_register_joystick_i2c_addr); if (ret < 0 || ret > 127){*addr_sec = -1; continue;} //return not in valid i2c range
*addr_main = ret;
#else
*addr_main = addr;
#endif
//check if addr_main valid
if (ioctl(fd, I2C_SLAVE_FORCE, *addr_main) < 0){print_stderr("ioctl failed for address 0x%02X (addr_main), errno:%d (%m)\n", *addr_main, errno);
} else if (i2c_smbus_read_byte_data(fd, tmp_reg_manuf_main) == mcu_manuf){break;}
*addr_sec = -1; *addr_main = -1;
}
close(fd);
bool mcu_sec_enabled = false;
#ifdef ALLOW_MCU_SEC_I2C
mcu_sec_enabled = true;
#endif
if (*addr_main == -1 || (mcu_sec_enabled && *addr_sec == -1)){print_stderr("failed to detect MCU address\n"); return -1;
} else if (!diag_mode_init && !quiet_mode){
print_stderr("detected possible MCU adress, main:0x%02X", *addr_main);
if (mcu_sec_enabled){fprintf(stderr, ", secondary:0x%02X", *addr_sec);}
fputc('\n', stderr);
}
return 0;
}
int mcu_check_manufacturer(){ //check device manufacturer, fill signature,id,version, return 0 on success, 1 if version missmatch , -1 on wrong manufacturer, -2 on i2c error
int tmp_reg = offsetof(struct i2c_joystick_register_struct, manuf_ID) / sizeof(uint8_t); //check signature
int ret = i2c_smbus_read_byte_data(mcu_fd, tmp_reg);
if (ret != mcu_manuf){
if (ret < 0){i2c_allerrors_count++; print_stderr("FATAL: reading I2C device manuf_ID (register:0x%02X) failed, errno:%d (%m)\n", tmp_reg, -ret);
} else {print_stderr("FATAL: invalid I2C device signature: 0x%02X\n", ret);}
return -1;
}
mcu_signature = (uint8_t)ret;
tmp_reg = offsetof(struct i2c_joystick_register_struct, device_ID) / sizeof(uint8_t); //check version
ret = i2c_smbus_read_word_data(mcu_fd, tmp_reg);
if (ret < 0){i2c_allerrors_count++; print_stderr("FATAL: reading I2C device device_ID (register:0x%02X) failed, errno:%d (%m)\n", tmp_reg, -ret); return -2;}
mcu_id = ret & 0xFF;
mcu_version = (ret >> 8) & 0xFF;
print_stderr("I2C device detected, signature:0x%02X, id:%d, version:%d\n", mcu_signature, mcu_id, mcu_version);
logs_write("I2C device: signature:0x%02X, id:%d, version:%d\n\n", mcu_signature, mcu_id, mcu_version);
if (mcu_version != mcu_version_even){
print_stderr("WARNING: program register version (%d) mismatch MCU version (%d)\n", mcu_version_even, mcu_version);
logs_write("WARNING: program register version (%d) mismatch MCU version (%d)\n", mcu_version_even, mcu_version);
return 1;
}
return 0;
}
int mcu_update_config0(){ //read/update config0 register, return 0 on success, -1 on error
int ret = i2c_smbus_read_byte_data(mcu_fd, mcu_i2c_register_config0);
if (ret < 0){
i2c_allerrors_count++;
print_stderr("FATAL: reading MCU config0 (register:0x%02X) failed, errno:%d (%m)\n", mcu_i2c_register_config0, -ret);
return -1;
}
mcu_config0.bits = (uint8_t)ret;
mcu_config0.vals.debounce_level = digital_debounce;
if (mcu_config0.bits == (uint8_t)ret){return 0;} //nothing changed
ret = i2c_smbus_write_byte_data(mcu_fd, mcu_i2c_register_config0, mcu_config0.bits); //update i2c config
if (ret < 0){
i2c_allerrors_count++;
print_stderr("FATAL: writing MCU config0 (register:0x%02X) failed, errno:%d (%m)\n", mcu_i2c_register_config0, -ret);
return -1;
}
return 0;
}
#if defined(ALLOW_MCU_SEC_I2C) && !defined(DIAG_PROGRAM)
int mcu_update_power_control(){ //read/update power_control register, return 0 on success, -1 on error
int power_control_ret = i2c_smbus_read_byte_data(mcu_fd_sec, mcu_sec_register_power_control);
if (power_control_ret < 0){
i2c_allerrors_count++;
print_stderr("FATAL: reading MCU sec power_control (register:0x%02X) failed, errno:%d (%m)\n", mcu_sec_register_power_control, -power_control_ret);
return -1;
}
#ifndef USE_SHM_REGISTERS
mcu_power_control_t mcu_power_control = {.bits=(uint8_t)power_control_ret}, mcu_power_control_back = {.bits=mcu_power_control.bits};
#endif
//low_batt gpio check
int gpio_state = -1;
if (battery_gpio_enable){
#ifdef USE_WIRINGPI
gpio_state = digitalRead(lowbattery_gpio);
if (lowbattery_gpio_invert){gpio_state = !gpio_state;}
#elif defined(USE_GPIOD)
if (gpiod_lowbatt_fd >= 0){
gpio_state = gpiod_line_get_value(gpiod_lowbatt_line);
if (gpio_state >= 0 && lowbattery_gpio_invert){gpio_state = !gpio_state;}
}
#endif
if (gpio_state > -1){
#ifndef USE_SHM_REGISTERS
mcu_power_control.vals.low_batt_mode = gpio_state;
#else
if (power_control_low_batt_mode != gpio_state){power_control_low_batt_mode = gpio_state;}
#endif
}
}
#ifndef USE_SHM_REGISTERS
if (mcu_power_control.bits == mcu_power_control_back.bits){return 0;} //nothing changed
if (i2c_smbus_write_byte_data(mcu_fd_sec, mcu_sec_register_write_protect, mcu_write_protect_disable) < 0){return -1;} //disable write protection
if (i2c_smbus_write_byte_data(mcu_fd_sec, mcu_sec_register_power_control, mcu_power_control.bits) < 0){return -1;} //update register
if (i2c_smbus_write_byte_data(mcu_fd_sec, mcu_sec_register_write_protect, mcu_write_protect_enable) < 0){return -1;} //enable write protection
#endif
return 0;
}
int mcu_update_battery_capacity(){ //read/update battery_capacity register, return 0 on success, -1 on error
struct stat battery_stat; int percent = 255; FILE *filehandle;
if (stat(battery_rsoc_file, &battery_stat) == 0){
filehandle = fopen(battery_rsoc_file, "r");
if (filehandle != NULL && !ferror(filehandle)){
char buffer[5]; fgets(buffer, 5, filehandle);
percent = atoi(buffer); if (percent < 0 || percent > 255){percent = 255;}
}
fclose(filehandle);
if (percent != 255){i2c_secondary_registers.battery_capacity = (uint8_t)percent;}
}
#ifndef USE_SHM_REGISTERS
static int percent_prev = -1;
if (percent == percent_prev){return 0;} //nothing changed
if (i2c_smbus_write_byte_data(mcu_fd_sec, mcu_sec_register_write_protect, mcu_write_protect_disable) < 0){return -1;} //disable write protection
if (i2c_smbus_write_byte_data(mcu_fd_sec, mcu_sec_register_battery_capacity, (uint8_t)percent) < 0){return -1;} //update register
if (i2c_smbus_write_byte_data(mcu_fd_sec, mcu_sec_register_write_protect, mcu_write_protect_enable) < 0){return -1;} //enable write protection
percent_prev = percent;
#endif
return 0;
}
#endif
int mcu_update_register(int* fd, uint8_t reg, uint8_t value, bool check){ //update and check register, return 0 on success, -1 on error
if(!io_fd_valid(*fd)){return -1;}
int ret = i2c_smbus_write_byte_data(*fd, reg, value);
if (ret < 0){
i2c_allerrors_count++;
print_stderr("writing to register 0x%02X failed, errno:%d (%m)\n", reg, -ret);
return -1;
}
if (check){
ret = i2c_smbus_read_byte_data(*fd, reg);
if (ret < 0){
i2c_allerrors_count++;
print_stderr("reading register 0x%02X failed, errno:%d (%m)\n", reg, -ret);
return -1;
}
if (ret != value){print_stderr("register 0x%02X update failed, expected:0x%02X but got 0x%02X\n", reg, value, ret); return -1;}
}
return 0;
}
int init_adc(){ //init adc data, return 0 on success, -1 on resolution read fail, -2 on adc conf
adc_firstrun = true; //force initial adc update
//mcu adc resolution
int tmp_adc_res = i2c_smbus_read_byte_data(mcu_fd, mcu_i2c_register_adc_res);
if (tmp_adc_res < 0){print_stderr("FATAL: reading MCU ADC resolution (register:0x%02X) failed, errno:%d (%m)\n", mcu_i2c_register_adc_res, -tmp_adc_res); i2c_allerrors_count++; return -1;
} else if (tmp_adc_res == 0){print_stderr("WARNING: MCU ADC is currently disabled, please refer to documentation for more informations\n");
} else {print_stderr("MCU ADC resolution: %dbits\n", tmp_adc_res);}
//mcu analog config
int ret = i2c_smbus_read_byte_data(mcu_fd, mcu_i2c_register_adc_conf);
if (ret < 0){print_stderr("FATAL: reading MCU ADC configuration (register:0x%02X) failed, errno:%d (%m)\n", mcu_i2c_register_adc_conf, -ret); i2c_allerrors_count++; return -2;}
uint8_t mcu_adc_config_old = (uint8_t)ret;
uint8_t mcu_adc_config_new = mcu_adc_config_old & 0xF0; //copy enable bits
bool mcu_adc_used[4] = {0};
for (int i=0; i<4; i++){ //mcu adc loop
int_constrain(&adc_map[i], -1, 3); //avoid overflow
mcu_adc_enabled[i] = (mcu_adc_config_old >> (i+4)) & 0b1; //mcu adc enabled
if (adc_map[i] > -1 || diag_mode){
#ifdef DIAG_PROGRAM
if (diag_first_run){adc_map[i] = -1; adc_params[i].enabled = true;} //set adcs in a "default" state for "first run" mode
#endif
if (!adc_fd_valid[i]){ //external adc not used
#ifdef ALLOW_EXT_ADC
adc_init_err[i] = -1;
#endif
adc_params[i].res = tmp_adc_res; //mcu adc resolution
if (!mcu_adc_enabled[i]){adc_params[i].enabled = false; //mcu adc fully disable
} else if (adc_params[i].enabled){mcu_adc_config_new |= 1U << i; mcu_adc_used[i] = true;} //mcu adc used
}
#ifdef ALLOW_EXT_ADC
else { //external
int_constrain(&adc_type[i], 0, adc_type_count-1); //avoid overflow
if (adc_type_funct_init[adc_type[i]](adc_fd[i], &adc_params[i]) < 0){adc_init_err[i] = -3; //init external adc failed
} else if (!adc_params[i].enabled){adc_init_err[i] = -1; //not enabled
} else {adc_init_err[i] = 0;} //ok
}
#endif
#ifdef DIAG_PROGRAM
if (diag_first_run){ //set adcs in a "default" state for "first run" mode
adc_params[i].res_limit = 0xFFFFFFFF >> (32 - adc_params[i].res); //compute adc limit
adc_params[i].min = 0;
adc_params[i].max = adc_params[i].res_limit;
}
#endif
} else {adc_params[i].enabled = false;} //disable adc because map is invalid
*js_values[i] = (uint16_t)0x7FFF; //reset uhid axis value, mainly used in diag part
//report
if(!diag_mode_init && !quiet_mode){
print_stderr("ADC%d: %s", i, adc_params[i].enabled?"enabled":"disabled");
fputs(", ", stderr);
if (mcu_adc_used[i]){fputs("MCU", stderr);
#ifdef ALLOW_EXT_ADC
} else if (adc_fd_valid[i]){fprintf(stderr, "External(0x%02X)", adc_addr[i]);
#endif
} else {fputs("None", stderr);}
fprintf(stderr, ", mapped to %s(%d)\n", js_axis_names[adc_map[i]+1], adc_map[i]);
}
}
mcu_adc_read[0] = mcu_adc_used[0] || mcu_adc_used[1]; //mcu adc0-1 used
mcu_adc_read[1] = mcu_adc_used[2] || mcu_adc_used[3]; //mcu adc2-3 used
if (mcu_adc_config_old != mcu_adc_config_new){ //mcu adc config needs update
ret = i2c_smbus_write_byte_data(mcu_fd, mcu_i2c_register_adc_conf, mcu_adc_config_new);
if (ret < 0){print_stderr("failed to set new MCU ADC configuration (0x%02X), errno:%d (%m)\n", mcu_i2c_register_adc_conf, -ret); i2c_allerrors_count++; return -2;
} else { //read back wrote value by safety
ret = i2c_smbus_read_byte_data(mcu_fd, mcu_i2c_register_adc_conf);
if (ret != mcu_adc_config_new){print_stderr("failed to update MCU ADC configuration, should be 0x%02X but is 0x%02X\n", mcu_adc_config_new, ret); return -2;
} else {print_stderr("MCU ADC configuration updated, is now 0x%02X, was 0x%02X\n", ret, mcu_adc_config_old);}
}
}
return 0;
}
//TTY related functions
static void tty_signal_handler(int sig){ //handle signal func
if (debug){print_stderr("DEBUG: signal received: %d\n", sig);}
if (term_backup.c_cflag){tcsetattr(STDIN_FILENO, TCSANOW, &term_backup);} //restore terminal to original state funct
kill_requested = true;
}
//SHM related functions
static int file_read(char* path, char* bufferptr, int buffersize){ //read file
int ret = 0;
FILE *filehandle = fopen(path, "r");
if (filehandle != NULL) {
fgets(bufferptr, buffersize, filehandle);
if (ferror(filehandle)){if (debug){print_stderr("failed to read '%s'\n", path);} ret = -EFAULT;}
fclose(filehandle);
} else {if (debug){print_stderr("failed to read '%s'\n", path);} return -EFAULT;}
return ret;
};
static int file_write(char* path, char* content){ //write file
FILE *filehandle = fopen(path, "w");
if (filehandle != NULL) {
fprintf(filehandle, "%s", content);
fclose(filehandle);
} else {if (debug){print_stderr("failed to write '%s'\n", path);} return -EFAULT;}
return 0;
}
static int folder_create(char* path, int rights, int uid, int gid) { //create folder(s), set rights/uid/gui if not -1. Return number of folder created, -errno on error
int ret, depth = 0; //security
struct stat file_stat = {0};
char curr_path[strlen(path)+1], sub_path[strlen(path)+2]; sub_path[0]='\0';
int tmp_rights = (rights==-1)?0755:rights;
strcpy(curr_path, path);
if(curr_path[strlen(curr_path)-1] == '/'){curr_path[strlen(curr_path)-1] = '\0';}
char *tmpPtr = strtok (curr_path, "/"); //split path
while (tmpPtr != NULL){
strcat(sub_path, "/"); strcat(sub_path, tmpPtr);
if (stat(sub_path, &file_stat) == -1){
ret = mkdir(sub_path, tmp_rights);
if (ret < 0){if (debug){print_stderr("failed to create directory '%s', errno:%d (%m)\n", sub_path, -ret);} return ret;}
print_stderr("directory '%s' created\n", sub_path);
if (chmod(sub_path, tmp_rights) < 0 && debug){print_stderr("failed to set directory '%s' rights, err: %m\n", sub_path);}
if (uid != -1 || gid != -1){if (chown(sub_path, uid, gid) < 0 && debug){print_stderr("failed to set directory '%s' owner, err: %m\n", sub_path);}}
} else if (debug){print_stderr("directory '%s' already exist\n", sub_path);}
depth++; if(depth > 10){if (debug){print_stderr("something gone very wrong, depth:10 hit, break\n");} break;}
tmpPtr = strtok (NULL, "/"); //next sub folder
}
return depth;
}
static int logs_write(const char* format, ...){ //write to log, return chars written or -1 on error, based on printf source code
int ret = 0;
if (logs_fh != NULL){
va_list args; va_start(args, format);
ret = vfprintf(logs_fh, format, args);
va_end(args);
} else {ret = -1;}
return ret;
}
static void shm_init(bool first){ //init shm related things, folders and files creation
char cwd_back[PATH_MAX]; getcwd(cwd_back, PATH_MAX); chdir(shm_fullpath); //backup cwd and switch to shm path
if (first){ //initial run, skip i2c part
if (folder_create(shm_fullpath, 0755, user_uid, user_gid) < 0){return;} //recursive folder create
//pid file
int pid = (int)getpid();
if (pid > 0){
sprintf(pid_path, "%s/pid.txt", shm_fullpath);
FILE *filehandle = fopen(pid_path, "w");
if (filehandle != NULL) {
fprintf(filehandle, "%d", pid); fclose(filehandle);
print_stderr("%s set to '%d'\n", pid_path, pid);
} else {pid_path[0] = '\0';} //failed to write pid file
}
//todo: log part needs rework
char shm_logs[strlen(shm_fullpath)+13]; //current path with additionnal 255 chars for filename
sprintf(shm_logs, "%s/driver.log", shm_fullpath); //log file
logs_fh = fopen(shm_logs, "w+");
if (logs_fh != NULL){
chmod(shm_logs, 0666); chown(shm_logs, (uid_t) user_uid, (gid_t) user_gid);
print_stderr("logs: %s\n", shm_logs);
} else {print_stderr("failed to open '%s' (%m)\n", shm_logs);}
shm_enable=true;
} else if (shm_enable){
#ifndef DIAG_PROGRAM
char buffer[5]; sprintf(buffer, "%d", shm_status);
if (file_write(shm_status_path_ptr, buffer) == 0){ //status file
print_stderr("'%s' content set to '%s'\n", shm_status_path_ptr, buffer);
chmod(shm_status_path_ptr, 0666); chown(shm_status_path_ptr, (uid_t) user_uid, (gid_t) user_gid);
}
#endif
#ifdef USE_SHM_REGISTERS //registers to files "bridge"
for (int i=0; i<shm_vars_arr_size; i++){
int ret = i2c_smbus_read_byte_data(*(shm_vars[i].fd), shm_vars[i].reg); //read register value
if (ret < 0){print_stderr("failed to read register 0x%02X, errno:%d (%m)\n", shm_vars[i].reg, -ret); ret = 0;}
for (int j=0; j<shm_vars[i].fields_count; j++){ //field loop
char* path = shm_vars[i].fields[j].filename;
if (path != NULL && strlen(path)){ //"valid" filename
uint8_t fields_val = (uint8_t)ret;
if (shm_vars[i].fields[j].bit_count == 0){shm_vars[i].fields[j].bit_count = 8;}
fields_val = bit_extract_uint8(fields_val, shm_vars[i].fields[j].bit_pos, shm_vars[i].fields[j].bit_count);
*(shm_vars[i].fields[j].var_ptr) = shm_vars[i].fields[j].var_back = fields_val;
sprintf(buffer, "%d", fields_val);
if (file_write(path, buffer) == 0){ //write file
chmod(path, 0666); chown(path, (uid_t) user_uid, (gid_t) user_gid); //right and owner
struct stat file_stat = {0}; stat(path, &file_stat); shm_vars[i].fields[j].mtime = file_stat.st_mtim; //backup file modification time
print_stderr("'%s' content set to '%s'\n", path, buffer);
}
}
}
}
#endif
}
chdir(cwd_back); //restore working folder
}
#ifndef DIAG_PROGRAM
static void shm_update(void){ //update registers/files linked to shm things
if (shm_enable){
char cwd_back[PATH_MAX]; getcwd(cwd_back, PATH_MAX); chdir(shm_fullpath); //backup cwd and switch to shm path
struct stat file_stat = {0};
//check shm_path/status update
if (stat(shm_status_path_ptr, &file_stat) == -1){
char buffer[3]; sprintf(buffer, "%d", shm_status);
if (file_write(shm_status_path_ptr, buffer) == 0 && debug){print_stderr("missing '%s' content set to '%s'\n", shm_status_path_ptr, buffer);} //recreate missing status file
} else {
char buffer[3]; file_read(shm_status_path_ptr, buffer, 2);
int shm_status_back = shm_status; if (buffer != NULL){shm_status = atoi(buffer);}
if (shm_status != shm_status_back){driver_lock = (shm_status>1)?true:false;}
}
#ifdef USE_SHM_REGISTERS
for (int i=0; i<shm_vars_arr_size; i++){
int register_value = i2c_smbus_read_byte_data(*(shm_vars[i].fd), shm_vars[i].reg); //read register value
if (register_value < 0){if (debug){print_stderr("failed to read register 0x%02X, errno:%d (%m)\n", shm_vars[i].reg, -register_value);} register_value = 0;}
bool register_update = false;
for (int j=0; j<shm_vars[i].fields_count; j++){
char* path = shm_vars[i].fields[j].filename, buffer[128];
struct timespec* mtime = &shm_vars[i].fields[j].mtime;
uint8_t bit_pos = shm_vars[i].fields[j].bit_pos, bit_count = shm_vars[i].fields[j].bit_count, *var_ptr = shm_vars[i].fields[j].var_ptr;
bool file_update = false, reg_update = false, var_updated = (shm_vars[i].fields[j].var_back != *var_ptr);
uint8_t tmp_reg = bit_extract_uint8((uint8_t)register_value, bit_pos, bit_count); //current register field value
if (var_updated || stat(path, &file_stat) != 0){file_update = true; reg_update = true; //var->reg,file
} else if (memcmp(mtime, &file_stat.st_mtim, sizeof(struct timespec)) != 0){ //file->var
if (shm_vars[i].reg_rw){ //allow update
if (file_read(path, buffer, 127) == 0){ //build updated value from file->reg,var
int tmp_value = atoi(buffer);
if (int_constrain(&tmp_value, 0, 0xFF >> (8 - bit_count)) != 0){file_update = true;} //file value outside of range, force update file with proper value
*var_ptr = tmp_value; *mtime = file_stat.st_mtim; reg_update = true;
if(debug){print_stderr("'%s' var set to '%d'\n", path, tmp_value);}
}
} else {file_update = true;} //restore file value
} else if (*var_ptr != tmp_reg){*var_ptr = tmp_reg; file_update = true;} //reg->var,file
if (file_update){ //var->file
sprintf(buffer, "%d", *var_ptr);
if (file_write(path, buffer) == 0){ //write file
chmod(path, 0666); chown(path, (uid_t) user_uid, (gid_t) user_gid); //right and owner
stat(path, &file_stat); *mtime = file_stat.st_mtim; //backup file modification time
if(debug){print_stderr("'%s' content set to '%s'\n", path, buffer);}
}
}
if (reg_update){register_update = true; register_value = bit_clear_uint8(register_value, bit_pos, bit_count); register_value |= *var_ptr << bit_pos;} //update register value
shm_vars[i].fields[j].var_back = *var_ptr;
}
if (register_update){ //var,file->reg
if (shm_vars[i].reg_wp != NULL){i2c_smbus_write_byte_data(*(shm_vars[i].fd), shm_vars[i].reg_wp->reg, shm_vars[i].reg_wp->disable);} //disable write protection
int ret = i2c_smbus_write_byte_data(*(shm_vars[i].fd), shm_vars[i].reg, (uint8_t)register_value);
if (shm_vars[i].reg_wp != NULL){i2c_smbus_write_byte_data(*(shm_vars[i].fd), shm_vars[i].reg_wp->reg, shm_vars[i].reg_wp->enable);} //enable write protection
if (debug && ret >= 0){
char binbuffer[9]={'\0'}; int_bin_chararray(register_value, 8, binbuffer);
print_stderr("register 0x%02X set to '%d'(%s)\n", shm_vars[i].reg, register_value, binbuffer);
}
}
}
#endif
chdir(cwd_back); //restore working folder
}
}
#endif
static void shm_close(void){ //close shm related things
#ifndef DIAG_PROGRAM
if (shm_enable){
char curr_path[strlen(shm_fullpath)+10]; //current path with additionnal 255 chars for filename
sprintf(curr_path, "%s/status", shm_fullpath); //current path
if (file_write (curr_path, "0") == 0){print_stderr("'%s' content set to 0\n", curr_path);}
}
#endif
if (logs_fh != NULL){fclose(logs_fh);}
}
//bit/binary specific
static uint8_t bit_extract_uint8(uint8_t value, uint8_t bit_pos, uint8_t bit_count){ //warning, no security
return (uint8_t)(value << (8 - bit_pos - bit_count)) >> (8 - bit_count);
}
static uint8_t bit_clear_uint8(uint8_t value, uint8_t bit_pos, uint8_t bit_count){ //warning, no security
for (uint8_t i = bit_pos; i < bit_pos+bit_count; i++){value &= ~(1<<i);} return value;
}
static char* int_bin_chararray(int val, int bits, char* var){ //int to binary format char array
char buffer[bits+1];
for(int i=0; i<bits; i++){buffer[i]=((val >> (bits-i-1)) & 0b1)+'0';} buffer[bits]='\0';
return strcpy(var, buffer);
}
//integer manipulation functs
void int_rollover(int* val, int min, int max){ //rollover int value between (incl) min and max, work both way
if (*val < min){*val = max;} else if (*val > max){*val = min;}
}
int int_constrain(int* val, int min, int max){ //limit int value to given (incl) min and max value, return 0 if val within min and max, -1 under min, 1 over max
int ret = 0;
if (*val < min){*val = min; ret = -1;} else if (*val > max){*val = max; ret = 1;}
return ret;
}
int int_digit_count(int num){ //number of digit of a integer, negative sign is consider as a digit
char buffer[12];
sprintf (buffer, "%d", num);
return strlen(buffer);
}
int in_array_int16(int16_t* arr, int16_t value, int arr_size){ //search in value in int16 array, return index or -1 on failure
for (int i=0; i < arr_size; i++) {if (arr[i] == value) {return i;}}
return -1;
}
//IO specific
bool io_fd_valid(int fd){ //check if a file descriptor is valid
int errno_back = errno; //backup previous errno
bool valid = fcntl(fd, F_GETFD) >= 0 || errno != EBADF;
errno = errno_back; //restore errno to avoid bad descriptor on all errors
return valid;
}
//Generic functions
static void program_close(void){ //regroup all close functs
if (already_killed){return;}
if (strlen(pid_path) > 0){remove(pid_path);} //delete pid file
if (term_backup.c_cflag){tcsetattr(STDIN_FILENO, TCSANOW, &term_backup);} //restore terminal to original state funct
#ifndef DIAG_PROGRAM
uhid_destroy(uhid_fd);
#endif
i2c_close_all();
shm_close();
//if (cfg_delete){remove(config_path);}
already_killed = true;
}
//main functions
#ifndef MULTI_INSTANCES
static int program_instances_count(char* path, char* program){ //scan /proc/ to check if program is already running, return number of instances found
DIR *proc_ptr; if ((proc_ptr = opendir("/proc/")) == NULL){print_stderr("failed to open folder /proc/\n"); return 0;} //failed to open /proc/
char fullpath[strlen(path) + strlen(program) + 2]; sprintf(fullpath, "%s/%s", path, program); //full path to current program
struct dirent *dir_entry; struct stat file_stat; int found = 0;
while ((dir_entry = readdir(proc_ptr)) != NULL){
char* proc_id = dir_entry->d_name; if (!isdigit(proc_id[0])){continue;} //skip any that can't be process id
char buffer_path[strlen(proc_id) + 13]; sprintf(buffer_path, "/proc/%s/exe", proc_id); //build full path to potencial exe symlink
if (lstat(buffer_path, &file_stat) == 0 && S_ISLNK(file_stat.st_mode)){ //exist and symlink
char resolved_path[PATH_MAX] = {'\0'};
realpath(buffer_path, resolved_path);
if (strlen(resolved_path) > 0){
if (strcmp(fullpath, resolved_path) == 0){found++; //matching
} else if (strlen(resolved_path) > 11){ //fix for dev, real path ends by " (deleted)" if link "broken" by new compilation