Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
Gone bug-huntin'
Browse files Browse the repository at this point in the history
  • Loading branch information
zacharyweiss committed Jan 26, 2023
1 parent 7f0b477 commit fd112e3
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 19 deletions.
7 changes: 3 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Disclaimer: use responsibly, and at your own risk. While in my testing, I've see

## TODO
Emulation:
- ***Fix signal truncation issue!***
- Fix signal truncation issue! *Edit: Tentative fix in place*
- General code cleanup
- Reverse track precompute & replay
- Prefix/between/suffix addition to config menu
Expand All @@ -25,10 +25,9 @@ File management:
- Update Add Manually flow to reflect new file format (currently only sets Track 2)

Known bugs:
- ***From debug logging output, seems precomputed signal is getting truncated somehow! This is priority \#1 to fix***
- From debug logging output, seems precomputed signal is getting truncated somehow! This is priority \#1 to fix. *Edit: Tentative fix in place*
- Custom text input scene with expanded characterset (Add Manually) has odd behavior when navigating the keys near the numpad
- Track 1 data typically starts with a `%` sign. Unless escaped, it won't be displayed when printed, as C considers it a special character. To confirm: how does this impact the emulation when iterating through the chars? Does it get played correctly?
- Possible file format issues when Track 2 data exists but Track 1 is left empty; doesn't seem to be setting the Track 2 field with anything (doesn't overwrite existing data). However, `flipper_format_read_string()` doesn't seem to return `false`. Is the bug in my code, or with `flipper_format`?
- File format issues when Track 2 data exists but Track 1 is left empty; doesn't seem to be setting the Track 2 field with anything (doesn't overwrite existing data). However, `flipper_format_read_string()` doesn't seem to return `false`. Is the bug in my code, or with `flipper_format`?
- Attempting to play a track that doesn't have data results in a crash (as one might expect). Need to lock out users from selecting empty tracks in the config menu or do better error handling

## Skunkworks ideas
Expand Down
2 changes: 1 addition & 1 deletion application.fam
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ App(
"dialogs",
],
provides=[],
stack_size=2 * 1024,
stack_size=5 * 1024,
order=64, # keep it at the bottom of the list while still WIP
fap_icon="icons/mag_10px.png",
fap_category="Tools",
Expand Down
40 changes: 26 additions & 14 deletions helpers/mag_helpers.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,49 +157,61 @@ void track_to_bits(uint8_t* bit_array, const char* track_data, uint8_t track_ind

int tmp, crc, lrc = 0;
int i = 0;
// Please forgive the mess. This was a bug battlezone. Will clean up over the weekend
// So many stupid things done here, many learnings lol
//FURI_LOG_D(TAG, "%d", strlen(track_data));
//FURI_LOG_D(TAG, "%d", strlen(track_data) * bitlen[track_index]);

// convert track data to bits
for(uint8_t j = 0; track_data[i] != '\0'; j++) {
for(uint8_t j = 0; track_data[j] != '\0'; j++) {
crc = 1;
tmp = track_data[j] - sublen[track_index];

for(uint8_t k = 0; k < bitlen[track_index] - 1; k++) {
crc ^= tmp & 1;
lrc ^= (tmp & 1) << k;
bit_array[i] = tmp & 1;
//FURI_LOG_D(
// TAG, "i, j, k: %d %d %d char %s bit %d", i, j, k, &track_data[j], bit_array[i]);
i++;
tmp >>= 1;
}
bit_array[i] = crc;
//FURI_LOG_D(TAG, "i, j: %d %d char %s bit %d", i, j, &track_data[j], bit_array[i]);
i++;
}

FURI_LOG_D(TAG, "LRC");
// finish calculating final "byte" (LRC)
tmp = lrc;
crc = 1;
for(uint8_t j = 0; j < bitlen[track_index] - 1; j++) {
crc ^= tmp & 1;
bit_array[i] = tmp & 1;
//FURI_LOG_D(TAG, "i, j: %d %d bit %d", i, j, bit_array[i]);
i++;
tmp >>= 1;
}
bit_array[i] = crc;
//FURI_LOG_D(TAG, "i: %d bit %d", i, bit_array[i]);
i++;

// My makeshift end sentinel. All other values 0/1
bit_array[i] = 2;
//FURI_LOG_D(TAG, "i: %d bit %d", i, bit_array[i]);
i++;

// Log the output (messy but works)
char output[100] = {0x0};
FuriString* tmp_str;
//char output[500] = {0x0};
/*FuriString* tmp_str;
tmp_str = furi_string_alloc();
for(uint8_t j = 0; bit_array[j] != 2; j++) {
furi_string_printf(tmp_str, "%d", (bit_array[j] & 1));
strcat(output, furi_string_get_cstr(tmp_str));
furi_string_cat_printf(tmp_str, "%d", (bit_array[j] & 1));
//strcat(output, furi_string_get_cstr(tmp_str));
}
FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), output);
furi_string_free(tmp_str);
FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), track_data);
FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), furi_string_get_cstr(tmp_str));*/
//furi_string_free(tmp_str);
}

void mag_spoof(Mag* mag) {
Expand All @@ -209,8 +221,8 @@ void mag_spoof(Mag* mag) {
// likely will be reworked to antirez's bitmap method anyway...
const char* data1 = furi_string_get_cstr(mag->mag_dev->dev_data.track[0].str);
const char* data2 = furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str);
uint8_t bit_array1[(strlen(data1) * bitlen[0]) + 1];
uint8_t bit_array2[(strlen(data2) * bitlen[1]) + 1];
uint8_t bit_array1[2 * (strlen(data1) * bitlen[0]) + 1];
uint8_t bit_array2[2 * (strlen(data2) * bitlen[1]) + 1];
track_to_bits(bit_array1, data1, 0);
track_to_bits(bit_array2, data2, 1);

Expand All @@ -222,33 +234,33 @@ void mag_spoof(Mag* mag) {
// Critical timing section (need to eliminate ifs? does this impact timing?)
FURI_CRITICAL_ENTER();
// Prefix of zeros
for(uint8_t i = 0; i < ZERO_PREFIX; i++) {
for(uint16_t i = 0; i < ZERO_PREFIX; i++) {
if(!play_bit(0, setting)) break;
}

// Track 1
if((setting->track == MagTrackStateAll) || (setting->track == MagTrackStateOne)) {
for(uint8_t i = 0; bit_array1[i] != 2; i++) {
for(uint16_t i = 0; bit_array1[i] != 2; i++) {
if(!play_bit((bit_array1[i] & 1), setting)) break;
}
}

// Zeros between tracks
if(setting->track == MagTrackStateAll) {
for(uint8_t i = 0; i < ZERO_BETWEEN; i++) {
for(uint16_t i = 0; i < ZERO_BETWEEN; i++) {
if(!play_bit(0, setting)) break;
}
}

// Track 2 (TODO: Reverse track)
if((setting->track == MagTrackStateAll) || (setting->track == MagTrackStateTwo)) {
for(uint8_t i = 0; bit_array2[i] != 2; i++) {
for(uint16_t i = 0; bit_array2[i] != 2; i++) {
if(!play_bit((bit_array2[i] & 1), setting)) break;
}
}

// Suffix of zeros
for(uint8_t i = 0; i < ZERO_SUFFIX; i++) {
for(uint16_t i = 0; i < ZERO_SUFFIX; i++) {
if(!play_bit(0, setting)) break;
}
FURI_CRITICAL_EXIT();
Expand Down

0 comments on commit fd112e3

Please sign in to comment.