Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid adv payloads are not committed before checking #171

Merged
merged 6 commits into from
Jan 22, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
94 changes: 56 additions & 38 deletions ble/Gap.h
Original file line number Diff line number Diff line change
Expand Up @@ -848,7 +848,6 @@ class Gap {
return INIT_POLICY_IGNORE_WHITELIST;
}


protected:
/* Override the following in the underlying adaptation layer to provide the functionality of scanning. */

Expand Down Expand Up @@ -940,7 +939,6 @@ class Gap {
*/
ble_error_t startAdvertising(void) {
ble_error_t rc;
setAdvertisingData(); /* Update the underlying stack. */
if ((rc = startAdvertising(_advParams)) == BLE_ERROR_NONE) {
state.advertising = 1;
}
Expand All @@ -954,7 +952,7 @@ class Gap {
*/
void clearAdvertisingPayload(void) {
_advPayload.clear();
setAdvertisingData();
setAdvertisingData(_advPayload, _scanResponse);
}

/**
Expand All @@ -972,12 +970,18 @@ class Gap {
* advertising payload.
*/
ble_error_t accumulateAdvertisingPayload(uint8_t flags) {
GapAdvertisingData advPayloadCopy = _advPayload;
ble_error_t rc;
if ((rc = _advPayload.addFlags(flags)) != BLE_ERROR_NONE) {
if ((rc = advPayloadCopy.addFlags(flags)) != BLE_ERROR_NONE) {
return rc;
}

return setAdvertisingData();
rc = setAdvertisingData(advPayloadCopy, _scanResponse);
if (rc == BLE_ERROR_NONE) {
_advPayload = advPayloadCopy;
}

return rc;
}

/**
Expand All @@ -993,14 +997,18 @@ class Gap {
* advertising payload.
*/
ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::Appearance app) {
setAppearance(app);

GapAdvertisingData advPayloadCopy = _advPayload;
ble_error_t rc;
if ((rc = _advPayload.addAppearance(app)) != BLE_ERROR_NONE) {
if ((rc = advPayloadCopy.addAppearance(app)) != BLE_ERROR_NONE) {
return rc;
}

return setAdvertisingData();
rc = setAdvertisingData(advPayloadCopy, _scanResponse);
if (rc == BLE_ERROR_NONE) {
_advPayload = advPayloadCopy;
}

return rc;
}

/**
Expand All @@ -1016,12 +1024,18 @@ class Gap {
* advertising payload.
*/
ble_error_t accumulateAdvertisingPayloadTxPower(int8_t power) {
GapAdvertisingData advPayloadCopy = _advPayload;
ble_error_t rc;
if ((rc = _advPayload.addTxPower(power)) != BLE_ERROR_NONE) {
if ((rc = advPayloadCopy.addTxPower(power)) != BLE_ERROR_NONE) {
return rc;
}

return setAdvertisingData();
rc = setAdvertisingData(advPayloadCopy, _scanResponse);
if (rc == BLE_ERROR_NONE) {
_advPayload = advPayloadCopy;
}

return rc;
}

/**
Expand All @@ -1048,16 +1062,18 @@ class Gap {
* payload.
*/
ble_error_t accumulateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
setDeviceName(data);
}

GapAdvertisingData advPayloadCopy = _advPayload;
ble_error_t rc;
if ((rc = _advPayload.addData(type, data, len)) != BLE_ERROR_NONE) {
if ((rc = advPayloadCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
return rc;
}

return setAdvertisingData();
rc = setAdvertisingData(advPayloadCopy, _scanResponse);
if (rc == BLE_ERROR_NONE) {
_advPayload = advPayloadCopy;
}

return rc;
}

/**
Expand All @@ -1077,16 +1093,18 @@ class Gap {
* matching AD type; otherwise, an appropriate error.
*/
ble_error_t updateAdvertisingPayload(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
if (type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
setDeviceName(data);
}

GapAdvertisingData advPayloadCopy = _advPayload;
ble_error_t rc;
if ((rc = _advPayload.updateData(type, data, len)) != BLE_ERROR_NONE) {
if ((rc = advPayloadCopy.updateData(type, data, len)) != BLE_ERROR_NONE) {
return rc;
}

return setAdvertisingData();
rc = setAdvertisingData(advPayloadCopy, _scanResponse);
if (rc == BLE_ERROR_NONE) {
_advPayload = advPayloadCopy;
}

return rc;
}

/**
Expand All @@ -1103,8 +1121,12 @@ class Gap {
* set.
*/
ble_error_t setAdvertisingPayload(const GapAdvertisingData &payload) {
_advPayload = payload;
return setAdvertisingData();
ble_error_t rc = setAdvertisingData(payload, _scanResponse);
if (rc == BLE_ERROR_NONE) {
_advPayload = payload;
}

return rc;
}

/**
Expand Down Expand Up @@ -1133,12 +1155,18 @@ class Gap {
* response payload.
*/
ble_error_t accumulateScanResponse(GapAdvertisingData::DataType type, const uint8_t *data, uint8_t len) {
GapAdvertisingData scanResponseCopy = _scanResponse;
ble_error_t rc;
if ((rc = _scanResponse.addData(type, data, len)) != BLE_ERROR_NONE) {
if ((rc = scanResponseCopy.addData(type, data, len)) != BLE_ERROR_NONE) {
return rc;
}

return setAdvertisingData();
rc = setAdvertisingData(_advPayload, scanResponseCopy);
if (rc == BLE_ERROR_NONE) {
_scanResponse = scanResponseCopy;
}

return rc;
}

/**
Expand All @@ -1150,7 +1178,7 @@ class Gap {
*/
void clearScanResponse(void) {
_scanResponse.clear();
setAdvertisingData();
setAdvertisingData(_advPayload, _scanResponse);
}

/**
Expand Down Expand Up @@ -1379,16 +1407,6 @@ class Gap {
return BLE_ERROR_NOT_IMPLEMENTED; /* Requesting action from porter(s): override this API if this capability is supported. */
}

private:
/**
* Helper function used to set the advertising data in the underlying BLE stack.
*
* @return BLE_ERROR_NONE if the advertising data was successfully set.
*/
ble_error_t setAdvertisingData(void) {
return setAdvertisingData(_advPayload, _scanResponse);
}

private:
/**
* Functionality that is BLE stack-dependent and must be implemented by the
Expand Down