Instantly share code, notes, and snippets.
Last active
August 18, 2022 18:52
-
Star
(1)
1
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save bigunclemax/479850bd4aed70f0e998bf5ebf156081 to your computer and use it in GitHub Desktop.
ipc_navi_msg.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdint.h> | |
#include <string.h> | |
#include <stdlib.h> | |
#include <stdio.h> | |
#include <errno.h> | |
#include <unistd.h> | |
#include <sys/stat.h> | |
#include <time.h> | |
#define MAX_IN_ASCII 1024 | |
#define MAX_MSG_LEN 19 | |
#define IN_FILE "/fs/rwdata/customSettings/ipc/ascii.msg" | |
#define OUT_FILE "/fs/rwdata/customSettings/ipc/hex.msg" | |
#pragma pack(push, 1) | |
struct msg { | |
const uint8_t block_1[140]; | |
uint8_t super_wierd_dec_in_hex_len; | |
const uint8_t block_2[77]; | |
const uint8_t byte_1; | |
const uint8_t byte_2; | |
uint8_t msg_len; | |
const uint8_t byte_3; | |
const uint8_t byte_4; | |
const uint8_t byte_5; | |
const uint8_t byte_6; | |
}; | |
#pragma pack(pop) | |
struct msg m = { | |
.block_1 = { | |
0x00, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x49, 0x50, 0x43, 0x5f, | |
0x4d, 0x49, 0x44, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x2f, 0x49, 0x50, 0x43, 0x5f, 0x4d, 0x49, 0x44, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x23, 0x24, 0x00, 0x00 | |
}, | |
.super_wierd_dec_in_hex_len = 0x27, | |
.block_2 = { | |
0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xa6, 0x00, | |
0x01, 0xa1 | |
}, | |
.byte_1 = 0x08, | |
.byte_2 = 0xc0, | |
.msg_len = 0, | |
.byte_3 = 0x00, | |
.byte_4 = 0x20, | |
.byte_5 = 0x00, | |
.byte_6 = 0x01, | |
}; | |
int _min(int num1, int num2) | |
{ | |
return (num1 > num2 ) ? num2 : num1; | |
} | |
int main(int argc, char **argv) { | |
unsigned int delay = 1000000; | |
if (argc > 1) { | |
long ret = strtol(argv[1], NULL, 0); | |
if (ret) { | |
delay = ret; | |
} | |
} | |
time_t prev_mt = 0; | |
int curr_idx = 0; | |
int i_fsize = 0; | |
char i_string[MAX_IN_ASCII]; | |
while (1) { | |
usleep(delay); | |
struct stat sb; | |
if (stat(IN_FILE, &sb) == -1) { | |
printf("Get stat error: %d\n", errno); | |
continue; | |
} | |
if (prev_mt != sb.st_mtime) { | |
// printf("Last status change: %s", ctime(&sb.st_ctime)); | |
// printf("Last file access: %s", ctime(&sb.st_atime)); | |
printf("Last file modification: %s", ctime(&sb.st_mtime)); | |
prev_mt = sb.st_mtime; | |
FILE *i_f = fopen(IN_FILE, "r"); | |
if (i_f == NULL){ | |
printf("Can't open input file Errno: %d\n", errno); | |
continue; | |
} | |
fseek(i_f, 0, SEEK_END); | |
i_fsize = (int)ftell(i_f); | |
fseek(i_f, 0, SEEK_SET); /* same as rewind(f); */ | |
if (i_fsize > MAX_IN_ASCII) { | |
i_fsize = MAX_IN_ASCII; | |
} | |
size_t ret_code = fread(i_string, i_fsize, 1, i_f); | |
if (ret_code != 1) { | |
printf("Read error: %d\n", errno); | |
continue; | |
} | |
fclose(i_f); | |
curr_idx = 0; | |
} | |
char buffer[MAX_MSG_LEN]; | |
memset(buffer, 0, MAX_MSG_LEN); | |
int msg_length = _min(i_fsize, MAX_MSG_LEN); | |
if (i_fsize > MAX_MSG_LEN) | |
{ | |
//copy and rotate | |
int l1 = _min(MAX_MSG_LEN, i_fsize - curr_idx); | |
memcpy(buffer,i_string + curr_idx, l1); | |
memcpy(buffer + l1, i_string, MAX_MSG_LEN - l1); | |
curr_idx++; | |
curr_idx %= i_fsize; | |
} else { | |
//copy as is | |
memcpy(buffer, i_string, i_fsize); | |
} | |
uint8_t str_len_w_0 = msg_length + 1; | |
int tot_len = (int)sizeof(struct msg) + str_len_w_0; | |
uint8_t data[tot_len]; | |
memset(data, 0, tot_len); | |
m.msg_len = str_len_w_0 + 4; | |
memcpy(data, &m, sizeof(struct msg)); | |
memcpy(data + sizeof(struct msg), buffer, msg_length); | |
printf("DATA: %s\n", data+sizeof(struct msg)); | |
FILE *f = fopen(OUT_FILE, "wb"); | |
if (f == NULL){ | |
printf("Cant create file Errno: %d\n", errno); | |
return -1; | |
} | |
fwrite(data, tot_len, 1, f); | |
fclose(f); | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment