Skip to content

Commit fd112e3

Browse files
committed
Gone bug-huntin'
1 parent 7f0b477 commit fd112e3

File tree

3 files changed

+30
-19
lines changed

3 files changed

+30
-19
lines changed

README.md

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ Disclaimer: use responsibly, and at your own risk. While in my testing, I've see
55

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

2727
Known bugs:
28-
- ***From debug logging output, seems precomputed signal is getting truncated somehow! This is priority \#1 to fix***
28+
- From debug logging output, seems precomputed signal is getting truncated somehow! This is priority \#1 to fix. *Edit: Tentative fix in place*
2929
- Custom text input scene with expanded characterset (Add Manually) has odd behavior when navigating the keys near the numpad
30-
- 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?
31-
- 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`?
30+
- 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`?
3231
- 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
3332

3433
## Skunkworks ideas

application.fam

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ App(
1111
"dialogs",
1212
],
1313
provides=[],
14-
stack_size=2 * 1024,
14+
stack_size=5 * 1024,
1515
order=64, # keep it at the bottom of the list while still WIP
1616
fap_icon="icons/mag_10px.png",
1717
fap_category="Tools",

helpers/mag_helpers.c

+26-14
Original file line numberDiff line numberDiff line change
@@ -157,49 +157,61 @@ void track_to_bits(uint8_t* bit_array, const char* track_data, uint8_t track_ind
157157

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

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

166170
for(uint8_t k = 0; k < bitlen[track_index] - 1; k++) {
167171
crc ^= tmp & 1;
168172
lrc ^= (tmp & 1) << k;
169173
bit_array[i] = tmp & 1;
174+
//FURI_LOG_D(
175+
// TAG, "i, j, k: %d %d %d char %s bit %d", i, j, k, &track_data[j], bit_array[i]);
170176
i++;
171177
tmp >>= 1;
172178
}
173179
bit_array[i] = crc;
180+
//FURI_LOG_D(TAG, "i, j: %d %d char %s bit %d", i, j, &track_data[j], bit_array[i]);
174181
i++;
175182
}
176183

184+
FURI_LOG_D(TAG, "LRC");
177185
// finish calculating final "byte" (LRC)
178186
tmp = lrc;
179187
crc = 1;
180188
for(uint8_t j = 0; j < bitlen[track_index] - 1; j++) {
181189
crc ^= tmp & 1;
182190
bit_array[i] = tmp & 1;
191+
//FURI_LOG_D(TAG, "i, j: %d %d bit %d", i, j, bit_array[i]);
183192
i++;
184193
tmp >>= 1;
185194
}
186195
bit_array[i] = crc;
196+
//FURI_LOG_D(TAG, "i: %d bit %d", i, bit_array[i]);
187197
i++;
188198

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

193204
// Log the output (messy but works)
194-
char output[100] = {0x0};
195-
FuriString* tmp_str;
205+
//char output[500] = {0x0};
206+
/*FuriString* tmp_str;
196207
tmp_str = furi_string_alloc();
197208
for(uint8_t j = 0; bit_array[j] != 2; j++) {
198-
furi_string_printf(tmp_str, "%d", (bit_array[j] & 1));
199-
strcat(output, furi_string_get_cstr(tmp_str));
209+
furi_string_cat_printf(tmp_str, "%d", (bit_array[j] & 1));
210+
//strcat(output, furi_string_get_cstr(tmp_str));
200211
}
201-
FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), output);
202-
furi_string_free(tmp_str);
212+
FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), track_data);
213+
FURI_LOG_D(TAG, "Track %d: %s", (track_index + 1), furi_string_get_cstr(tmp_str));*/
214+
//furi_string_free(tmp_str);
203215
}
204216

205217
void mag_spoof(Mag* mag) {
@@ -209,8 +221,8 @@ void mag_spoof(Mag* mag) {
209221
// likely will be reworked to antirez's bitmap method anyway...
210222
const char* data1 = furi_string_get_cstr(mag->mag_dev->dev_data.track[0].str);
211223
const char* data2 = furi_string_get_cstr(mag->mag_dev->dev_data.track[1].str);
212-
uint8_t bit_array1[(strlen(data1) * bitlen[0]) + 1];
213-
uint8_t bit_array2[(strlen(data2) * bitlen[1]) + 1];
224+
uint8_t bit_array1[2 * (strlen(data1) * bitlen[0]) + 1];
225+
uint8_t bit_array2[2 * (strlen(data2) * bitlen[1]) + 1];
214226
track_to_bits(bit_array1, data1, 0);
215227
track_to_bits(bit_array2, data2, 1);
216228

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

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

236248
// Zeros between tracks
237249
if(setting->track == MagTrackStateAll) {
238-
for(uint8_t i = 0; i < ZERO_BETWEEN; i++) {
250+
for(uint16_t i = 0; i < ZERO_BETWEEN; i++) {
239251
if(!play_bit(0, setting)) break;
240252
}
241253
}
242254

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

250262
// Suffix of zeros
251-
for(uint8_t i = 0; i < ZERO_SUFFIX; i++) {
263+
for(uint16_t i = 0; i < ZERO_SUFFIX; i++) {
252264
if(!play_bit(0, setting)) break;
253265
}
254266
FURI_CRITICAL_EXIT();

0 commit comments

Comments
 (0)