aboutsummaryrefslogtreecommitdiff
path: root/tmk_core/protocol/lufa/lufa.c
diff options
context:
space:
mode:
authorFred Sundvik <fsundvik@gmail.com>2016-12-29 12:13:30 +0200
committerFred Sundvik <fsundvik@gmail.com>2016-12-29 12:13:30 +0200
commitdd685eceb2045371d38f24d454f1ab08ca7416f4 (patch)
treeeef012131a9ce190512a899d4850e34741989a26 /tmk_core/protocol/lufa/lufa.c
parent273faa4d9cd5a84207548f83ba550c9efee90933 (diff)
downloadqmk_firmware-dd685eceb2045371d38f24d454f1ab08ca7416f4.tar.gz
qmk_firmware-dd685eceb2045371d38f24d454f1ab08ca7416f4.zip
API Sysex fixes
Fix memory leaks by using stack instead of malloc Reduce memory usage by having less temporary bufffers Remove warnings by adding includes Decrease code size by 608 bytes (mostly due to not linking malloc) More robust handling of buffer overflows
Diffstat (limited to 'tmk_core/protocol/lufa/lufa.c')
-rw-r--r--tmk_core/protocol/lufa/lufa.c32
1 files changed, 22 insertions, 10 deletions
diff --git a/tmk_core/protocol/lufa/lufa.c b/tmk_core/protocol/lufa/lufa.c
index 097189770..6dd5959dc 100644
--- a/tmk_core/protocol/lufa/lufa.c
+++ b/tmk_core/protocol/lufa/lufa.c
@@ -1252,28 +1252,40 @@ void cc_callback(MidiDevice * device,
1252 // midi_send_cc(device, (chan + 1) % 16, num, val); 1252 // midi_send_cc(device, (chan + 1) % 16, num, val);
1253} 1253}
1254 1254
1255#ifdef API_SYSEX_ENABLE
1255uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0}; 1256uint8_t midi_buffer[MIDI_SYSEX_BUFFER] = {0};
1257#endif
1256 1258
1257void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) { 1259void sysex_callback(MidiDevice * device, uint16_t start, uint8_t length, uint8_t * data) {
1258 #ifdef API_SYSEX_ENABLE 1260 #ifdef API_SYSEX_ENABLE
1259 // SEND_STRING("\n"); 1261 // SEND_STRING("\n");
1260 // send_word(start); 1262 // send_word(start);
1261 // SEND_STRING(": "); 1263 // SEND_STRING(": ");
1264 // Don't store the header
1265 int16_t pos = start - 4;
1262 for (uint8_t place = 0; place < length; place++) { 1266 for (uint8_t place = 0; place < length; place++) {
1263 // send_byte(*data); 1267 // send_byte(*data);
1264 midi_buffer[start + place] = *data; 1268 if (pos >= 0) {
1265 if (*data == 0xF7) { 1269 if (*data == 0xF7) {
1266 // SEND_STRING("\nRD: "); 1270 // SEND_STRING("\nRD: ");
1267 // for (uint8_t i = 0; i < start + place + 1; i++){ 1271 // for (uint8_t i = 0; i < start + place + 1; i++){
1268 // send_byte(midi_buffer[i]); 1272 // send_byte(midi_buffer[i]);
1269 // SEND_STRING(" "); 1273 // SEND_STRING(" ");
1270 // } 1274 // }
1271 uint8_t * decoded = malloc(sizeof(uint8_t) * (sysex_decoded_length(start + place - 4))); 1275 const unsigned decoded_length = sysex_decoded_length(pos);
1272 uint16_t decode_length = sysex_decode(decoded, midi_buffer + 4, start + place - 4); 1276 uint8_t decoded[API_SYSEX_MAX_SIZE];
1273 process_api(decode_length, decoded); 1277 sysex_decode(decoded, midi_buffer, pos);
1278 process_api(decoded_length, decoded);
1279 return;
1280 }
1281 else if (pos >= MIDI_SYSEX_BUFFER) {
1282 return;
1283 }
1284 midi_buffer[pos] = *data;
1274 } 1285 }
1275 // SEND_STRING(" "); 1286 // SEND_STRING(" ");
1276 data++; 1287 data++;
1288 pos++;
1277 } 1289 }
1278 #endif 1290 #endif
1279} 1291}