Skip to content

Commit

Permalink
[Telink]: Fix fail at the end of commissioning
Browse files Browse the repository at this point in the history
  • Loading branch information
rikorsev authored and restyled-io[bot] committed Mar 4, 2022
1 parent 8fef5fa commit 2278546
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
63 changes: 54 additions & 9 deletions src/platform/telink/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,10 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
err = HandleGAPDisconnect(event);
break;

case DeviceEventType::kPlatformTelinkBleDisconnectRequest:
err = HandleDisconnectRequest(event);
break;

case DeviceEventType::kPlatformTelinkBleRXWrite:
err = HandleRXCharWrite(event);
break;
Expand All @@ -789,6 +793,10 @@ void BLEManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
err = HandleBleConnectionClosed(event);
break;

case DeviceEventType::kOperationalNetworkEnabled:
err = HandleOperationalNetworkEnabled(event);
break;

default:
ChipLogDetail(DeviceLayer, "Event: Unknown (0x%04x)", event->Type);
}
Expand Down Expand Up @@ -954,25 +962,62 @@ CHIP_ERROR BLEManagerImpl::HandleGAPDisconnect(const ChipDeviceEvent * event)
return CHIP_NO_ERROR;
}

CHIP_ERROR BLEManagerImpl::HandleThreadStateChange(const ChipDeviceEvent * event)
CHIP_ERROR BLEManagerImpl::HandleDisconnectRequest(const ChipDeviceEvent * event)
{
ble_sts_t status = BLE_SUCCESS;
uint16_t handle = event->Platform.BleConnEvent.connHandle;
uint8_t reason = event->Platform.BleConnEvent.HciResult;

if(ConnectivityMgr().IsThreadProvisioned())
ChipLogDetail(DeviceLayer, "HandleDisconnectRequest");

/* Trigger disconnect. DisconnectCallback call occures on completion */
status = blc_ll_disconnect(handle, reason);
if(status != BLE_SUCCESS && status != LL_ERR_CONNECTION_NOT_ESTABLISH)
{
// If Thread is provisionet it is time to disconnect BLE
status = blc_ll_disconnect(BLS_CONN_HANDLE, CHIP_BLE_DISCONNECT_REASON);
if(status != BLE_SUCCESS && status != LL_ERR_CONNECTION_NOT_ESTABLISH)
{
ChipLogError(DeviceLayer, "Fail to disconnect. Error %d", status);
ChipLogError(DeviceLayer, "Fail to disconnect. Error %d", status);

return CHIP_ERROR_INCORRECT_STATE;
}
return CHIP_ERROR_INCORRECT_STATE;
}

return CHIP_NO_ERROR;
}

CHIP_ERROR BLEManagerImpl::HandleOperationalNetworkEnabled(const ChipDeviceEvent * event)
{
ChipDeviceEvent disconnectEvent;

ChipLogDetail(DeviceLayer, "HandleOperationalNetworkEnabled");

disconnectEvent.Type = DeviceEventType::kPlatformTelinkBleDisconnectRequest;
disconnectEvent.Platform.BleConnEvent.connHandle = BLS_CONN_HANDLE;
disconnectEvent.Platform.BleConnEvent.HciResult = CHIP_BLE_DISCONNECT_REASON;
ReturnErrorOnFailure(PlatformMgr().PostEvent(&disconnectEvent));

return CHIP_NO_ERROR;
}

CHIP_ERROR BLEManagerImpl::HandleThreadStateChange(const ChipDeviceEvent * event)
{
CHIP_ERROR error = CHIP_NO_ERROR;

ChipLogDetail(DeviceLayer, "HandleThreadStateChange");

if (event->Type == DeviceEventType::kThreadStateChange && event->ThreadStateChange.RoleChanged)
{
ChipDeviceEvent attachEvent;
attachEvent.Type = DeviceEventType::kThreadConnectivityChange;
attachEvent.ThreadConnectivityChange.Result = kConnectivity_Established;

error = PlatformMgr().PostEvent(&attachEvent);
VerifyOrExit(error == CHIP_NO_ERROR,
ChipLogError(DeviceLayer, "Failed to post Thread connectivity change: %" CHIP_ERROR_FORMAT, error.Format()));

}

exit:
return error;
}

CHIP_ERROR BLEManagerImpl::HandleBleConnectionClosed(const ChipDeviceEvent * event)
{
/* It is time to swich to IEEE802154 radio if it is provisioned */
Expand Down
10 changes: 10 additions & 0 deletions src/platform/telink/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,11 +126,21 @@ class BLEManagerImpl final : public BLEManager, private BleLayer, private BlePla

CHIP_ERROR HandleGAPConnect(const ChipDeviceEvent * event);
CHIP_ERROR HandleGAPDisconnect(const ChipDeviceEvent * event);
CHIP_ERROR HandleDisconnectRequest(const ChipDeviceEvent * event);
CHIP_ERROR HandleRXCharWrite(const ChipDeviceEvent *event);
CHIP_ERROR HandleTXCharCCCDWrite(const ChipDeviceEvent *event);
CHIP_ERROR HandleTXCharComplete(const ChipDeviceEvent * event);
CHIP_ERROR HandleBleConnectionClosed(const ChipDeviceEvent * event);

/*
@todo WORKAROUND: Due to abscense of non-cuncurrent mode in Matter
we are emulating connection to Thread with this events and manually
disconnect BLE ass soon as OperationalNetworkEnabled occures.
This functionality shall be removed as soon as non-cuncurrent mode
would be implemented
*/
CHIP_ERROR HandleThreadStateChange(const ChipDeviceEvent * event);
CHIP_ERROR HandleOperationalNetworkEnabled(const ChipDeviceEvent * event);

/* Callbacks from BLE stack*/
static void DriveBLEState(intptr_t arg);
Expand Down
1 change: 1 addition & 0 deletions src/platform/telink/CHIPDevicePlatformEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ enum InternalPlatformSpecificEventTypes
kPlatformTelinkEvent = kRange_InternalPlatformSpecific,
kPlatformTelinkBleConnected,
kPlatformTelinkBleDisconnected,
kPlatformTelinkBleDisconnectRequest,
kPlatformTelinkBleCCCWrite,
kPlatformTelinkBleRXWrite,
kPlatformTelinkBleTXComplete,
Expand Down

0 comments on commit 2278546

Please sign in to comment.