I am running into a problem which seems to have to do with the code execution pointer, and the behavior makes zero sense at all. There is a conditional statement which doesn't match, and since the code it controls is only one line, I omitted the surrounding { } braces for brevity. This is a pattern that I have reused many other times all throughout this fairly complex project without any observable bad consequences.
However, when I let the code run this way, execution apparently stops at that line. Here is the code block:
/**
* @brief iWRAP "NAME" event handler
* @param[in] mac MAC address of remote device
* @param[in] friendly_name Null-ternimated device name string
*/
void my_iwrap_evt_name(const iwrap_address_t *mac, const char *friendly_name) {
uint8_t name_len = 0;
if (friendly_name) name_len = strlen(friendly_name);
uint8_t payload[13 + name_len];
payload[0] = mac -> address[5];
payload[1] = mac -> address[4];
payload[2] = mac -> address[3];
payload[3] = mac -> address[2];
payload[4] = mac -> address[1];
payload[5] = mac -> address[0];
payload[6] = 0;
payload[7] = 0;
payload[8] = 0;
payload[9] = 0; // RSSI
payload[10] = 4; // inquiry status
payload[11] = find_pairing_from_mac(mac);
if (name_len) memcpy(payload + 13, friendly_name, name_len);
// send kg_evt_bluetooth_inquiry_response(...) event
skipPacket = 0;
if (kg_evt_bluetooth_inquiry_response) skipPacket = kg_evt_bluetooth_inquiry_response(payload + 0, payload + 6, payload[9], payload[10], payload[11], payload[12], name_len ? payload + 13 : 0);
if (!skipPacket) send_keyglove_packet(KG_PACKET_TYPE_EVENT, 13 + name_len, KG_PACKET_CLASS_BLUETOOTH, KG_PACKET_ID_EVT_BLUETOOTH_INQUIRY_RESPONSE, payload);
}
The offending line of code is this:
if (kg_evt_bluetooth_inquiry_response) skipPacket = kg_evt_bluetooth_inquiry_response(payload + 0, payload + 6, payload[9], payload[10], payload[11], payload[12], name_len ? payload + 13 : 0);
Note that "kg_evt_bluetooth_inquiry_response" function pointer which is currently NULL, so this condition doesn't match. Now, if I modify this one line of code to include braces:
if (kg_evt_bluetooth_inquiry_response) {
skipPacket = kg_evt_bluetooth_inquiry_response(payload + 0, payload + 6, payload[9], payload[10], payload[11], payload[12], name_len ? payload + 13 : 0);
}
Suddenly, everything works. There is NO reason I can think of why this should be occurring. Is there some weird Arduino IDE Java -> C++ conversion that is messing with the code blocks before compiling? Can I check this somehow?
I have also noticed that if I add a log output command (not shown here) earlier inside the function, in a way that is logically unrelated (and separated by many lines of code) from the condition shown here, the problem also goes away even if I don't add braces. I suspect some weird memory leak in my code or a buffer overflow or something, but I have no way of doing low-level debugging with breakpoints, watches, etc.
I am using Arduino v1.0.6 with a Teensy++ 2.0 (AT90USB1286) and Teensyduino v1.20 (latest as of this moment).