-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathcontrols.c
More file actions
3404 lines (3105 loc) · 97.1 KB
/
Copy pathcontrols.c
File metadata and controls
3404 lines (3105 loc) · 97.1 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
/*
* ICSim++
*
* Instrument Control Simulator
*
* University of Adelaide
*
*/
#include <pthread.h>
#include <math.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <time.h>
#include <getopt.h>
#include <signal.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <net/if.h>
#include <linux/can.h>
#include <linux/can/raw.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include "lib.h"
#include <errno.h>
//////////////////////////////////
////GLOBAL CONSTAND VARIABLES/////
//////////////////////////////////
#ifndef DATA_DIR
#define DATA_DIR "./data/"
#endif
#define DEFAULT_CAN_TRAFFIC DATA_DIR "sample-can.log"
#define DEFAULT_CANFD_TRAFFIC DATA_DIR "sample-canfd.log"
#define DEFAULT_FUEL_CONSTANT_FACTOR 0.1;
#define DEFAULT_FUEL_CONSTANT_FACTOR_SPEED_DIVIDER 50;
#define DEFAULT_BATTERY_CONSTANT_FACTOR 3;
#define DEFAULT_DIFFICULTY 1
#define MAX_LINE_LENGTH 1024
#define Hold_On 1
#define Hold_Off 0
#define MAX_SEGMENTS 50
#define PORT 8080
#define BUFFER_SIZE 1024
#define MAX_CLIENTS 5
// 0 = No randomization added to the packets other than location and ID
// 1 = Add NULL padding
// 2 = Randomize unused bytes
//Powertrain IDs
#define DEFAULT_THROTTLE_ID 16 //0x010
#define DEFAULT_THROTTLE_POS 4
#define DEFAULT_RPM_ID 32 //0x020
#define DEFAULT_RPM_POS 1
#define DEFAULT_SPEED_ID 48 //0x030
#define DEFAULT_SPEED_POS 3
#define DEFAULT_ENGINE_LOAD_ID 64 //0x040
#define DEFAULT_ENGINE_LOAD_POS 2
#define DEFAULT_FUEL_TANK_ID 80 //0x050
#define DEFAULT_FUEL_TANK_POS 4
#define DEFAULT_AIR_FLOW_ID 96 //0x060
#define DEFAULT_AIR_FLOW_POS 1
#define DEFAULT_OXYGEN_SENSOR_ID 112 //0x070
#define DEFAULT_OXYGEN_SENSOR_POS 3
#define DEFAULT_BATTERY_ID 128 //0x080
#define DEFAULT_BATTERY_POS 0
#define DEFAULT_VVT_ID 144 //0x090
#define DEFAULT_VVT_POS 2
#define DEFAULT_OIL_TEMPERATURE_ID 160 //0x0A0
#define DEFAULT_OIL_TEMPERATURE_POS 4
#define DEFAULT_RADIATOR_TEMPERATURE_ID 176 //0x0B0
#define DEFAULT_RADIATOR_TEMPERATURE_POS 0
#define DEFAULT_GEAR_ID 192 //0x0C0
#define DEFAULT_GEAR_POS 3
//Chassis
#define DEFAULT_BRAKECONTROL_ID 512 //0x200
#define DEFAULT_BRAKECONTROL_POS 2
#define DEFAULT_HANDBRAKE_ID 528 //0x210
#define DEFAULT_HAND_BRAKE_POS 0
#define DEFAULT_STEERING_ID 544 //0x220
#define DEFAULT_STEERING_POS 5
#define DEFAULT_ABS_ID 560 //0x230
#define DEFAULT_ABS_POS 3
#define DEFAULT_AIRBAG_ID 576 //0x240
#define DEFAULT_AIRBAG_POS 1
#define DEFAULT_CRUISECONTROL_ID 592 //0x250
#define DEFAULT_CRUISECONTROL_POS 0
//Body
#define DEFAULT_DOOR_ID 1024 // 0x400
#define DEFAULT_DOOR_POS 5
#define DEFAULT_WINDOW_ID 1040 // 0x410
#define DEFAULT_WINDOW_POS 2
#define DEFAULT_LIGHT_ID 1056 //0x420
#define DEFAULT_LIGHT_POS 0
#define DEFAULT_SIGNAL_ID 1072 // 0x430
#define DEFAULT_SIGNAL_POS 3
#define DEFAULT_HAZARD_ID 1077 // 0x435
#define DEFAULT_HAZARD_POS 1
#define DEFAULT_WIPER_ID 1088 //0x440
#define DEFAULT_WIPER_POS 4
#define DEFAULT_SUNROOF_ID 1104 //0x450
#define DEFAULT_SUNROOF_POS 3
#define DEFAULT_SIDEMIRROR_ID 1120 //0x460
#define DEFAULT_SIDEMIRROR_POS 2
#define DEFAULT_TRUNK_ID 1136 //0x470
#define DEFAULT_TRUNK_POS 1
#define DEFAULT_HORN_ID 1152 //0x480
#define DEFAULT_HORN_POS 5
//Infotainment
#define DEFAULT_AUDIOSYSTEM_ID 1536 //0x600
#define DEFAULT_AUDIOSYSTEM_POS 3
#define DEFAULT_AIRCONDITION_ID 1552 //0x610
#define DEFAULT_AIRCONDITION_POS 1
#define DEFAULT_BLUETOOTH_ID 1568 //0x620
#define DEFAULT_BLUETOOTH_POS 5
#define DEFAULT_SATELITENAVIGATION_ID 1584 //0x630
#define DEFAULT_SATELITENAVIGATION_POS 0
#define DEFAULT_DEFROSTER_ID 1600 //0x640
#define DEFAULT_DEFROSTER_POS 4
#define CAN_DOOR1_LOCK 1
#define CAN_DOOR2_LOCK 2
#define CAN_DOOR3_LOCK 4
#define CAN_DOOR4_LOCK 8
#define CAN_WINDOW1_LOCK 1
#define CAN_WINDOW2_LOCK 2
#define CAN_WINDOW3_LOCK 4
#define CAN_WINDOW4_LOCK 8
#define CAN_LEFT_SIGNAL 1
#define CAN_RIGHT_SIGNAL 2
#define CAN_HAZARD_ON 0x05
#define CAN_HAZARD_OFF 0x06
#define CAN_LIGHT_ON 0x0B
#define CAN_LIGHT_OFF 0x0A
#define CAN_BLUETOOTH_ON 0x0B
#define CAN_BLUETOOTH_OFF 0x0A
#define CAN_AIRCONDITION_ON 0x08
#define CAN_AIRCONDITION_OFF 0x09
#define CAN_AIRCONDITION_Up 0x0A
#define CAN_AIRCONDITION_Down 0x0B
#define CAN_HANDBRAKE_ON 0x0F
#define CAN_HANDBRAKE_OFF 0x0E
#define CAN_BRAKECONTROL_ON 0x0D
#define CAN_BRAKECONTROL_OFF 0x0C
#define CAN_ABS_ON 0x03
#define CAN_ABS_OFF 0x04
#define CAN_WIPER_ON 0x09
#define CAN_WIPER_OFF 0x08
#define CAN_AIRBAG_ON 0x06
#define CAN_AIRBAG_OFF 0x07
#define CAN_TRUNK_ON 0x0C
#define CAN_TRUNK_OFF 0x0D
#define CAN_SIDEMIRROR_ON 0x0E
#define CAN_SIDEMIRROR_OFF 0x0F
#define CAN_SUNROOF_ON 0x01
#define CAN_SUNROOF_OFF 0x02
#define CAN_HORN_ON 0x09
#define CAN_HORN_OFF 0x08
#define CAN_CRUISECONTROL_ON 0x05
#define CAN_CRUISECONTROL_OFF 0x06
#define CAN_STEERING_ON 0x07
#define CAN_STEERING_OFF 0x08
#define ON 1
#define OFF 0
#define CAN_DEFROSTER_INIT 0
#define CAN_DEFROSTER_FRONT_ON 1
#define CAN_DEFROSTER_FRONT_OFF 2
#define CAN_DEFROSTER_REAR_ON 3
#define CAN_DEFROSTER_REAR_OFF 4
#define CAN_DEFROSTER_BOTH_ON 5
#define CAN_DEFROSTER_BOTH_OFF 6
#define CAN_RADIO_ON 1
#define CAN_RADIO_OFF 0
#define CAN_RADIO_NextStation 2
#define CAN_RADIO_PreviousStation 3
#define CAN_RADIO_VolumeUp 4
#define CAN_RADIO_VolumeDown 5
#define DOOR_LOCKED 0
#define DOOR_UNLOCKED 1
#define WINDOW_LOCKED 0
#define WINDOW_UNLOCKED 1
#define SCREEN_WIDTH 720
#define SCREEN_HEIGHT 486
#define JOY_UNKNOWN -1
#define BUTTON_LOCK 4
#define PS3_BUTTON_LOCK 10
#define BUTTON_UNLOCK 5
#define PS3_BUTTON_UNLOCK 11
#define BUTTON_A 0
#define PS3_BUTTON_A 14
#define BUTTON_B 1
#define PS3_BUTTON_B 13
#define BUTTON_X 2
#define PS3_BUTTON_X 15
#define BUTTON_Y 3
#define PS3_BUTTON_Y 12
#define BUTTON_START 7
#define BUTTON_L 8
#define BUTTON_H 9
#define PS3_BUTTON_L 16
#define PS3_BUTTON_H 17
#define PS3_BUTTON_START 3
#define AXIS_LEFT_V 0
#define PS3_AXIS_LEFT_V 0
#define AXIS_LEFT_H 1
#define PS3_AXIS_LEFT_H 1
#define AXIS_L2 2
#define PS3_AXIS_L2 12
#define AXIS_RIGHT_H 3
#define PS3_AXIS_RIGHT_H 3
#define AXIS_RIGHT_V 4
#define PS3_AXIS_RIGHT_V 2
#define AXIS_R2 5
#define PS3_AXIS_R2 13
#define PS3_X_ROT 4
#define PS3_Y_ROT 5
#define PS3_Z_ROT 6 // The rotations are just guessed
#define MAX_RPM 5000.0
#define MAX_OIL_TEMPERATURE 95
#define MAX_SPEED 90.0 // Limiter 260.0 is full guage speed
#define MAX_STEERING_Angle 90.0
#define ACCEL_RATE 8.0 // 0-MAX_SPEED in seconds
#define USB_CONTROLLER 0
#define PS3_CONTROLLER 1
//////////////////////////////////
////VARIABLES INITIALIZATION//////
//////////////////////////////////
pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
char vcanName[50];
int gButtonY = BUTTON_Y;
int gButtonX = BUTTON_X;
int gButtonA = BUTTON_A;
int gButtonB = BUTTON_B;
int gButtonL = BUTTON_L;
int gButtonH = BUTTON_H;
int gButtonStart = BUTTON_START;
int gButtonLock = BUTTON_LOCK;
int gButtonUnlock = BUTTON_UNLOCK;
int gAxisL2 = AXIS_L2;
int gAxisR2 = AXIS_R2;
int gAxisRightH = AXIS_RIGHT_H;
int gAxisRightV = AXIS_RIGHT_V;
int gAxisLeftH = AXIS_LEFT_H;
int gAxisLeftV = AXIS_LEFT_V;
// Acelleromoter axis info
int gJoyX = JOY_UNKNOWN;
int gJoyY = JOY_UNKNOWN;
int gJoyZ = JOY_UNKNOWN;
//CAN / CANFD
int CANType = CAN_MTU;
int maxdlen;
#define DEFAULT_LOG_ALL_FRAMES 0
#define Frame_Log_Level_All 1
#define Frame_Log_Level_Both_User_System 2
#define Frame_Log_Level_UserOnly 3
#define Frame_Log_Level_SegmentOnly 4
//Network Segment Structure
typedef struct {
char name[20];
char interface[10];
unsigned int start_id;
unsigned int end_id;
int socket;
} NetworkSegment;
NetworkSegment segments[MAX_SEGMENTS];
int num_segments = -1;
//Analog joystick dead zone
const int JOYSTICK_DEAD_ZONE = 8000;
int gLastAccelValue = 0; // Non analog R2
int s; // socket
struct canfd_frame cf;
char *traffic_log = DEFAULT_CAN_TRAFFIC;
struct ifreq ifr;
int door_pos = DEFAULT_DOOR_POS;
int window_pos = DEFAULT_WINDOW_POS;
int signal_pos = DEFAULT_SIGNAL_POS;
int speed_pos = DEFAULT_SPEED_POS;
int rpm_pos = DEFAULT_RPM_POS;
int oil_temp_pos = DEFAULT_OIL_TEMPERATURE_POS;
int radiator_temp_pos = DEFAULT_RADIATOR_TEMPERATURE_POS;
int engine_load_pos = DEFAULT_ENGINE_LOAD_POS;
int air_flow_pos = DEFAULT_AIR_FLOW_POS;
int VVT_pos = DEFAULT_VVT_POS;
int gear_pos = DEFAULT_GEAR_POS;
int oxygen_sensor_pos = DEFAULT_OXYGEN_SENSOR_POS;
int battery_pos = DEFAULT_BATTERY_POS;
int throttle_pos = DEFAULT_THROTTLE_POS;
int fuel_pos = DEFAULT_FUEL_TANK_POS;
int light_pos = DEFAULT_LIGHT_POS;
int light_len = DEFAULT_LIGHT_POS;
int door_len = DEFAULT_DOOR_POS;
int window_len = DEFAULT_WINDOW_POS;
int bluetooth_len = DEFAULT_BLUETOOTH_POS;
int bluetooth_pos = DEFAULT_BLUETOOTH_POS;
int audiosys_len = DEFAULT_AUDIOSYSTEM_POS;
int audiosys_pos = DEFAULT_AUDIOSYSTEM_POS;
int aircondition_len = DEFAULT_AIRCONDITION_POS;
int aircondition_pos = DEFAULT_AIRCONDITION_POS;
int satelitenavigation_len = DEFAULT_SATELITENAVIGATION_POS;
int satelitenavigation_pos = DEFAULT_SATELITENAVIGATION_POS;
int defroster_len = DEFAULT_DEFROSTER_POS;
int defroster_pos = DEFAULT_DEFROSTER_POS;
int hand_brake_pos = DEFAULT_HAND_BRAKE_POS;
int hand_brake_len = DEFAULT_HAND_BRAKE_POS;
int brakecontrol_len = DEFAULT_BRAKECONTROL_POS;
int brakecontrol_pos = DEFAULT_BRAKECONTROL_POS;
int abs_len = DEFAULT_ABS_POS;
int abs_pos = DEFAULT_ABS_POS;
int wiper_pos = DEFAULT_WIPER_POS;
int wiper_len = DEFAULT_WIPER_POS;
int airbag_pos = DEFAULT_AIRBAG_POS;
int airbag_len = DEFAULT_AIRBAG_POS;
int hazard_pos = DEFAULT_HAZARD_POS;
int hazard_len = DEFAULT_HAZARD_POS;
int sunroof_pos = DEFAULT_SUNROOF_POS;
int sunroof_len = DEFAULT_SUNROOF_POS;
int sidemirror_pos = DEFAULT_SIDEMIRROR_POS;
int sidemirror_len = DEFAULT_SIDEMIRROR_POS;
int trunk_pos = DEFAULT_TRUNK_POS;
int trunk_len = DEFAULT_TRUNK_POS;
int horn_pos = DEFAULT_HORN_POS;
int horn_len = DEFAULT_HORN_POS;
int steering_pos = DEFAULT_STEERING_POS;
int steering_len = DEFAULT_STEERING_POS;
int cruisecontrol_pos = DEFAULT_CRUISECONTROL_POS;
int cruisecontrol_len = DEFAULT_CRUISECONTROL_POS;
int signal_len = DEFAULT_SIGNAL_POS;
int speed_len = DEFAULT_SPEED_POS;
int rpm_len = DEFAULT_RPM_POS;
int oil_temp_len = DEFAULT_OIL_TEMPERATURE_POS;
int radiator_temp_len = DEFAULT_RADIATOR_TEMPERATURE_POS;
int engine_load_len = DEFAULT_ENGINE_LOAD_POS;
int gear_len = DEFAULT_GEAR_POS;
int oxygen_sensor_len = DEFAULT_OXYGEN_SENSOR_POS;
int air_flow_len = DEFAULT_AIR_FLOW_POS;
int VVT_len = DEFAULT_VVT_POS;
int fuel_len = DEFAULT_FUEL_TANK_POS;
int battery_len = DEFAULT_BATTERY_POS;
int throttle_len = DEFAULT_THROTTLE_POS;
int difficulty = DEFAULT_DIFFICULTY;
char *script_path = NULL;
int lock_enabled = 0;
int unlock_enabled = 0;
int left_shift_enabled = 0;
int right_shift_enabled = 0;
int left_ctrl_enabled = 0;
int right_ctrl_enabled = 0;
char door_state = 0xf;
char window_state = 0xf;
char signal_state = 0;
char last_signal_state = 0;
char light_status = CAN_LIGHT_OFF;
char lightButtonPressed = CAN_LIGHT_OFF;
char bluetooth_status = CAN_BLUETOOTH_OFF;
char audiosys_status = CAN_RADIO_OFF;
char aircondition_status = CAN_AIRCONDITION_OFF;
char satelitenavigation_status = OFF;
char defroster_status = CAN_DEFROSTER_INIT;
char hand_brake_status = CAN_HANDBRAKE_OFF;
char handbrakeOn = CAN_HANDBRAKE_OFF;
char brakecontrol_status = CAN_BRAKECONTROL_OFF;
char abs_status = CAN_ABS_OFF;
char wiper_status = CAN_WIPER_OFF;
char wiperOn = CAN_WIPER_OFF;
char airbag_status = CAN_AIRBAG_OFF;
char hazard_status = CAN_HAZARD_OFF;
char sunroof_status = CAN_SUNROOF_OFF;
char sidemirror_status = CAN_SIDEMIRROR_OFF;
char trunk_status = CAN_TRUNK_OFF;
char horn_status = CAN_HORN_OFF;
char cruisecontrol_status = CAN_CRUISECONTROL_OFF;
int current_throttle_status = OFF;
int throttle = 0;
float throttle_position = 0;
int current_gear = 1;
float current_gear_factor = 8.056;
float current_oxygen_sensor = 0.9;
float speed_brake_throttle = 1.0;
float current_speed = 10;
char current_steering_angle = 0;
int current_rpm = 900;
int current_aircondition_value = 20;
float current_oil_temp = 0;
float current_radiator_temp = 0;
float current_engine_load = 0;
float current_air_flow = 0;
float current_VVT = 0;
float current_battery = 1000;
int charging_status = 0;
float current_fuel = 100;
int turning = 0;
int door_id, signal_id, speed_id, light_id, handbrake_id, wiper_id, rpm_id, oil_temp_id, radiator_temp_id, battery_id, fuel_id, throttle_id, bluetooth_id, audiosys_id, aircondition_id, satelitenavigation_id, defroster_id, brakecontrol_id, abs_id, cruisecontrol_id, steering_id, window_id, horn_id, trunk_id, sidemirror_id, sunroof_id, hazard_id, airbag_id, engine_load_id, gear_id, air_flow_id, oxygen_sensor_id, VVT_id;
int currentTime;
int lastAccel = 0;
int lastTempCheck = 0;
int timeinmiliseconds = 0;
int lastTurnSignal = 0;
int scripted_delay_time = 0;
int seed = 0;
int debug = 0;
int scripted_input = 0;
int play_id;
int gateway_id;
int kk = 0;
char data_file[256];
SDL_GameController *gGameController = NULL;
SDL_Joystick *gJoystick = NULL;
SDL_Haptic *gHaptic = NULL;
SDL_Renderer *renderer = NULL;
SDL_Texture *base_texture = NULL;
int gControllerType = USB_CONTROLLER;
pthread_t inputThread;
int server_fd = -1;
pthread_mutex_t cleanup_lock = PTHREAD_MUTEX_INITIALIZER;
// Function prototype
void *process_line(char *line);
char *trim_whitespace(char *str);
void *script_processor();
void simulate_key_press(SDL_Keycode key, int isHold);
char *convert_to_lowercase(char *line);
void kk_check(int);
void cleanup_resources(int client_socket) {
if (client_socket != -1) {
close(client_socket);
printf("Client socket closed.\n");
}
if (server_fd != -1) {
close(server_fd);
printf("Server socket closed.\n");
}
pthread_mutex_destroy(&cleanup_lock);
}
void sigint_handler(int signo) {
(void)signo; // Suppress unused parameter warning
pthread_mutex_lock(&cleanup_lock);
cleanup_resources(-1);
pthread_mutex_unlock(&cleanup_lock);
exit(0);
}
void socket_server_cleanup() {
if (server_fd != -1) {
close(server_fd);
printf("Socket closed.\n");
}
}
void *socket_server(void *arg) {
(void)arg;// Suppress unused parameter warning
int new_socket = -1;
struct sockaddr_in address;
char buffer[BUFFER_SIZE];
int addrlen = sizeof(address);
// Set up server socket
if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1) {
perror("Socket creation failed");
pthread_exit(NULL);
}
address.sin_family = AF_INET;
address.sin_addr.s_addr = INADDR_ANY;
address.sin_port = htons(PORT);
if (bind(server_fd, (struct sockaddr *)&address, sizeof(address)) == -1) {
perror("Bind failed");
cleanup_resources(-1);
pthread_exit(NULL);
}
if (listen(server_fd, MAX_CLIENTS) == -1) {
perror("Listen failed");
cleanup_resources(-1);
pthread_exit(NULL);
}
printf("Server listening on port %d...\n", PORT);
while (1) {
// Accept a new client connection
if ((new_socket = accept(server_fd, (struct sockaddr *)&address, (socklen_t *)&addrlen)) == -1) {
perror("Accept failed");
continue;
}
printf("Client connected.\n");
// Handle client messages
while (1) {
memset(buffer, 0, BUFFER_SIZE);
ssize_t bytes_read = read(new_socket, buffer, BUFFER_SIZE - 1);
if (bytes_read <= 0) {
if (bytes_read == 0) {
printf("Client disconnected.\n");
} else {
perror("Read error");
}
break;
}
printf("Received: %s\n", buffer);
// Respond to the client
const char *response = "Message received.\n";
// Process the received command
process_line(buffer);
if (send(new_socket, response, strlen(response), 0) == -1) {
perror("Send error");
break;
}
}
close(new_socket);
new_socket = -1;
}
cleanup_resources(new_socket);
pthread_exit(NULL);
}
// Adds data dir to file name
// Uses a single pointer so not to have a memory leak
// returns point to data_files or NULL if append is too large
char *get_data(char *fname) {
if(strlen(DATA_DIR) + strlen(fname) > 255) return NULL;
strncpy(data_file, DATA_DIR, 255);
strncat(data_file, fname, 255-strlen(data_file));
return data_file;
}
//////////////////////////////////
////SEND PACKET///////////////////
//////////////////////////////////
void send_pkt(int mtu) {
int i;
pthread_mutex_lock(&lock);
for (i = 0; i < num_segments; i++) {
if (cf.can_id >= segments[i].start_id && cf.can_id <= segments[i].end_id) {
if (write(segments[i].socket, &cf, mtu) != mtu) {
perror("write");
}
pthread_mutex_unlock(&lock);
return;
}
}
pthread_mutex_unlock(&lock);
printf("CAN ID 0x%X is not in any valid segment range\n", cf.can_id);
}
void send_pkt_old(int mtu) {
if(write(s, &cf, mtu) != mtu) {
perror("write");
}
}
NetworkSegment get_segment_name(struct canfd_frame *cf)
{
for (int i = 0; i < num_segments; i++) {
if (cf->can_id >= segments[i].start_id && cf->can_id <= segments[i].end_id) {
return segments[i];
}
}
// Return a default NetworkSegment if no match is found
NetworkSegment default_segment = {0}; // Zero-initialize all fields
return default_segment;
}
//////////////////////////////////
////Randomizes bytes in CAN packet if difficulty is hard enough
//////////////////////////////////
void randomize_pkt(int start, int stop) {
if (difficulty < 2) return;
int i = start;
for(;i < stop;i++) {
if(rand() % 3 < 1) cf.data[i] = rand() % 255;
}
}
//////////////////////////////////
////SEND DOOR LOCK PACKET/////////
//////////////////////////////////
void send_lock(char door) {
door_state |= door;
memset(&cf, 0, sizeof(cf));
cf.can_id = door_id;
cf.len = door_len;
cf.data[door_pos] = door_state;
if (door_pos) randomize_pkt(0, door_pos);
if (door_len != door_pos + 1) randomize_pkt(door_pos + 1, door_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND DOOR UNLOCK PACKET///////
//////////////////////////////////
void send_unlock(char door) {
door_state &= ~door;
memset(&cf, 0, sizeof(cf));
cf.can_id = door_id;
cf.len = door_len;
cf.data[door_pos] = door_state;
if (door_pos) randomize_pkt(0, door_pos);
if (door_len != door_pos + 1) randomize_pkt(door_pos + 1, door_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND WINDOW LOCK PACKET/////////
//////////////////////////////////
void send_window_lock(char window) {
window_state |= window;
memset(&cf, 0, sizeof(cf));
cf.can_id = window_id;
cf.len = window_len;
cf.data[window_pos] = window_state;
if (window_pos) randomize_pkt(0, window_pos);
if (window_len != window_pos + 1) randomize_pkt(window_pos + 1, window_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND WINDOW UNLOCK PACKET///////
//////////////////////////////////
void send_window_unlock(char window) {
window_state &= ~window;
memset(&cf, 0, sizeof(cf));
cf.can_id = window_id;
cf.len = window_len;
cf.data[window_pos] = window_state;
if (window_pos) randomize_pkt(0, window_pos);
if (window_len != window_pos + 1) randomize_pkt(window_pos + 1, window_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND HEAD LIGHT PACKET////////
//////////////////////////////////
void send_light_signal(char light) {
light_status = light;
memset(&cf, 0, sizeof(cf));
cf.can_id = light_id;
cf.len = light_len;
cf.data[light_pos] = light_status;
if (light_pos) randomize_pkt(0, light_pos);
if (light_len != light_pos + 1) randomize_pkt(light_pos + 1, light_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND BLUETOOTH PACKET////////
//////////////////////////////////
void send_bluetooth_signal(char bluetooth) {
bluetooth_status = bluetooth;
memset(&cf, 0, sizeof(cf));
cf.can_id = bluetooth_id;
cf.len = bluetooth_len;
cf.data[bluetooth_pos] = bluetooth_status;
if (bluetooth_pos) randomize_pkt(0, bluetooth_pos);
if (bluetooth_len != bluetooth_pos + 1) randomize_pkt(bluetooth_pos + 1, bluetooth_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND AUDIOSYSTEM PACKET////////
//////////////////////////////////
void send_audiosys_signal(char audiosys) {
memset(&cf, 0, sizeof(cf));
cf.can_id = audiosys_id;
cf.len = audiosys_len;
cf.data[audiosys_pos] = audiosys;
if (audiosys_pos) randomize_pkt(0, audiosys_pos);
if (audiosys_len != audiosys_pos + 1) randomize_pkt(audiosys_pos + 1, audiosys_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND SATELITE NAVIGATION PACKET////////
//////////////////////////////////
void send_satelitenavigation_signal(char satelitenavigation) {
satelitenavigation_status = satelitenavigation;
memset(&cf, 0, sizeof(cf));
cf.can_id = satelitenavigation_id;
cf.len = satelitenavigation_len;
cf.data[satelitenavigation_pos] = satelitenavigation_status;
if (satelitenavigation_pos) randomize_pkt(0, satelitenavigation_pos);
if (satelitenavigation_len != satelitenavigation_pos + 1) randomize_pkt(satelitenavigation_pos + 1, satelitenavigation_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND DEFROSTER PACKET////////
//////////////////////////////////
void send_defroster_signal(char defroster) {
defroster_status = defroster;
memset(&cf, 0, sizeof(cf));
cf.can_id = defroster_id;
cf.len = defroster_len;
cf.data[defroster_pos] = defroster_status;
if (defroster_pos) randomize_pkt(0, defroster_pos);
if (defroster_len != defroster_pos + 1) randomize_pkt(defroster_pos + 1, defroster_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND HANDBRAKE PACKET/////////
//////////////////////////////////
void send_hand_brake_signal(char hand_brake) {
hand_brake_status = hand_brake;
memset(&cf, 0, sizeof(cf));
cf.can_id = handbrake_id;
cf.len = hand_brake_len;
cf.data[hand_brake_pos] = hand_brake_status;
if (hand_brake_pos) randomize_pkt(0, hand_brake_pos);
if (hand_brake_len != hand_brake_pos + 1) randomize_pkt(hand_brake_pos + 1, hand_brake_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND BRAKE CONTROL PACKET/////////
//////////////////////////////////
void send_brakecontrol_signal(char brakecontrol) {
brakecontrol_status = brakecontrol;
memset(&cf, 0, sizeof(cf));
cf.can_id = brakecontrol_id;
cf.len = brakecontrol_len;
cf.data[brakecontrol_pos] = brakecontrol_status;
if (brakecontrol_pos) randomize_pkt(0, brakecontrol_pos);
if (brakecontrol_len != brakecontrol_pos + 1) randomize_pkt(brakecontrol_pos + 1, brakecontrol_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND Anti-lock Braking System PACKET/////////
//////////////////////////////////
void send_abs_signal(char abs) {
abs_status = abs;
memset(&cf, 0, sizeof(cf));
cf.can_id = abs_id;
cf.len = abs_len;
cf.data[abs_pos] = abs_status;
if (abs_pos) randomize_pkt(0, abs_pos);
if (abs_len != abs_pos + 1) randomize_pkt(abs_pos + 1, abs_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND WIPER PACKET/////////////
//////////////////////////////////
void send_wiper_signal(char wiper) {
wiper_status = wiper;
memset(&cf, 0, sizeof(cf));
cf.can_id = wiper_id;
cf.len = wiper_len;
cf.data[wiper_pos] = wiper_status;
if (wiper_pos) randomize_pkt(0, wiper_pos);
if (wiper_len != wiper_pos + 1) randomize_pkt(wiper_pos + 1, wiper_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND airbag PACKET/////////////
//////////////////////////////////
void send_airbag_signal(char airbag) {
airbag_status = airbag;
memset(&cf, 0, sizeof(cf));
cf.can_id = airbag_id;
cf.len = airbag_len;
cf.data[airbag_pos] = airbag_status;
if (airbag_pos) randomize_pkt(0, airbag_pos);
if (airbag_len != airbag_pos + 1) randomize_pkt(airbag_pos + 1, airbag_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND HAZARD PACKET/////////////
//////////////////////////////////
void send_hazard_signal(char hazard) {
hazard_status = hazard;
memset(&cf, 0, sizeof(cf));
cf.can_id = hazard_id;
cf.len = hazard_len;
cf.data[hazard_pos] = hazard_status;
if (hazard_pos) randomize_pkt(0, hazard_pos);
if (hazard_len != hazard_pos + 1) randomize_pkt(hazard_pos + 1, hazard_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND sunroof PACKET/////////////
//////////////////////////////////
void send_sunroof_signal(char sunroof) {
sunroof_status = sunroof;
memset(&cf, 0, sizeof(cf));
cf.can_id = sunroof_id;
cf.len = sunroof_len;
cf.data[sunroof_pos] = sunroof_status;
if (sunroof_pos) randomize_pkt(0, sunroof_pos);
if (sunroof_len != sunroof_pos + 1) randomize_pkt(sunroof_pos + 1, sunroof_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND sidemirror PACKET/////////////
//////////////////////////////////
void send_sidemirror_signal(char sidemirror) {
sidemirror_status = sidemirror;
memset(&cf, 0, sizeof(cf));
cf.can_id = sidemirror_id;
cf.len = sidemirror_len;
cf.data[sidemirror_pos] = sidemirror_status;
if (sidemirror_pos) randomize_pkt(0, sidemirror_pos);
if (sidemirror_len != sidemirror_pos + 1) randomize_pkt(sidemirror_pos + 1, sidemirror_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////
////SEND TRUNK PACKET/////////////
//////////////////////////////////
void send_trunk_signal(char trunk) {
trunk_status = trunk;
memset(&cf, 0, sizeof(cf));
cf.can_id = trunk_id;
cf.len = trunk_len;
cf.data[trunk_pos] = trunk_status;
if (trunk_pos) randomize_pkt(0, trunk_pos);
if (trunk_len != trunk_pos + 1) randomize_pkt(trunk_pos + 1, trunk_len);
if(CANType == CAN_MTU)
{
send_pkt(CAN_MTU);
}
else{
send_pkt(CANFD_MTU);
}
}
//////////////////////////////////