Skip to content

Error codes not being cascaded correctly from the low-level API to the Arduino API level #8

@gigapod

Description

@gigapod

HI Felix:

I found this and thought I'd post this here so it doesn't get lost in email...

An example - in XBeeArduino.cpp here

bool XBeeArduino::setApiOptions(const uint8_t options) {
    if (moduleType_ == XBEE_LORA) {
        return XBeeSetAPIOptions(xbee_, options) == API_SEND_SUCCESS;
    }
    return false;
}

The code is looking at the return value from XBeeSetAPIOptions() as an error code int, but XBeeSetAPIOptions() returns a bool.

From xbee.c here

bool XBeeSetAPIOptions(XBee* self, const uint8_t value) {
    uint8_t response[33];
    uint8_t responseLength;
    int status = apiSendAtCommandAndGetResponse(self, AT_AO, (const uint8_t[]){value}, 1, response, &responseLength, 5000);
    if(status != API_SEND_SUCCESS){
        XBEEDebugPrint("Failed to set API Options\n");
    }
    return status;
}

This returns status, which is an int. The return value is being passively cast to a bool. Since API_SEND_SUCCESS is defined as 0, a success value is being cast to false, not true, which is the. expected success value.

I believe the low-level routine should be:

bool XBeeSetAPIOptions(XBee* self, const uint8_t value) {
   uint8_t response[33];
   uint8_t responseLength;
   int status = apiSendAtCommandAndGetResponse(self, AT_AO, (const uint8_t[]){value}, 1, response, &responseLength, 5000);
   if(status != API_SEND_SUCCESS){
       XBEEDebugPrint("Failed to set API Options\n");
       return false;
   }
   return true;
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions