SingingCat 0
application
hijack.c
1#include "main-header.h"
2#include "sys/types.h"
3
4#include "hijack.h"
5
6void (*usb_receive_hook)(uint8_t *received, int size);
7void (*esp32_receive_hook)(uint8_t received);
8void hijack_init() {
9 usb_receive_hook = NULL;
10 esp32_receive_hook = NULL;
11}
12// is usb hijacked?
13int is_hijacked_usb_acm() {
14 return usb_receive_hook != NULL;
15}
16// is usb hijacked?
17int is_hijacked_esp32() {
18 return esp32_receive_hook != NULL;
19}
20void unhijack_esp32() {
21 if (esp32_receive_hook != NULL) {
22 printf("ESP32 Usart control regained userapp\r\n");
23 }
24 esp32_receive_hook = NULL;
25}
26void unhijack_usb_acm() {
27 if (usb_receive_hook != NULL) {
28 printf("USB ACM control regained from userapp\r\n");
29 }
30 usb_receive_hook = NULL;
31}
32// stop hijacker
33void unhijack_all() {
34 unhijack_usb_acm();
35 unhijack_esp32();
36}
37int hijack_esp32_usart(void ( *receive_hook )(uint8_t received)) {
38 if (receive_hook == NULL) {
39 printf("ESP32 not hijacked, hook is null\r\n");
40 return 1;
41 }
42 printf("ESP32 control transferred to userapp\r\n");
43 esp32_receive_hook = receive_hook;
44 return 0;
45}
46// hijacker announced its intention to hijack
47int hijack_usb_acm(void ( *receive_hook )(uint8_t *received, int size)) {
48 printf("USB ACM control transferred to userapp\r\n");
49 usb_receive_hook = receive_hook;
50 return 0;
51}
52// hijacker wants to send stuff via usb
53int send_via_usb_acm(uint8_t *buf, int size) {
54 mculib_usb_send(MAIN_MCULIBHANDLE, size, buf);
55 return 0;
56}
57// new byte(s) received, possibly for hijacker. returns 1 if sent to hijacker, 0 if not
58int hijack_receive_on_usb_acm(uint8_t *buf, int size) {
59 void (*h)(uint8_t *received, int size) = usb_receive_hook;
60 if (h == NULL) {
61 return 0;
62 }
63 h(buf, size);
64 return 1;
65}
66int hijack_receive_esp32(uint8_t received) {
67 void (*h)(uint8_t received) = esp32_receive_hook;
68 if (h == NULL) {
69 return 0;
70 }
71 h(received);
72 return 1;
73}