Skip to content

Commit

Permalink
[NetworkCommissioning] Fix hardfault during commissioning
Browse files Browse the repository at this point in the history
- Move registration of wifi event handlers to ConnectivityManagerImpl::_Init()
- Set mpStatusChangeCallback at the start of AmebaWiFiDriver::Init()
- Clean up loggings
  • Loading branch information
pankore committed Apr 20, 2022
1 parent b154579 commit 15b9df7
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 20 deletions.
12 changes: 0 additions & 12 deletions src/ble/BLEEndPoint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -376,44 +376,35 @@ void BLEEndPoint::DoClose(uint8_t flags, CHIP_ERROR err)

void BLEEndPoint::FinalizeClose(uint8_t oldState, uint8_t flags, CHIP_ERROR err)
{
printf("\r\n\r\nFinalizeClose debug 0\r\n\r\n");
mState = kState_Closed;

// Ensure transmit queue is empty and set to NULL.
printf("\r\n\r\nFinalizeClose debug 1\r\n\r\n");
QueueTxLock();
mSendQueue = nullptr;
QueueTxUnlock();
printf("\r\n\r\nFinalizeClose debug 2\r\n\r\n");

// Fire application's close callback if we haven't already, and it's not suppressed.
if (oldState != kState_Closing && (flags & kBleCloseFlag_SuppressCallback) == 0)
{
printf("\r\n\r\nFinalizeClose debug 3\r\n\r\n");
DoCloseCallback(oldState, flags, err);
}

if ((flags & kBleCloseFlag_SuppressCallback) != 0)
{
printf("\r\n\r\nFinalizeClose debug 4\r\n\r\n");
printf("\r\n\r\nthis: %p\r\n\r\n", this);
mBleTransport->OnEndPointConnectionClosed(this, err);
}

// If underlying BLE connection has closed, connection object is invalid, so just free the end point and return.
if (err == BLE_ERROR_REMOTE_DEVICE_DISCONNECTED || err == BLE_ERROR_APP_CLOSED_CONNECTION)
{
printf("\r\n\r\nFinalizeClose debug 5\r\n\r\n");
mConnObj = BLE_CONNECTION_UNINITIALIZED; // Clear handle to BLE connection, so we don't double-close it.
Free();
}
else // Otherwise, try to signal close to remote device before end point releases BLE connection and frees itself.
{
printf("\r\n\r\nFinalizeClose debug 6\r\n\r\n");
if (mRole == kBleRole_Central && mConnStateFlags.Has(ConnectionStateFlag::kDidBeginSubscribe))
{
// Cancel send and receive-ack timers, if running.
printf("\r\n\r\nFinalizeClose debug 7\r\n\r\n");
StopAckReceivedTimer();
StopSendAckTimer();

Expand All @@ -422,15 +413,13 @@ void BLEEndPoint::FinalizeClose(uint8_t oldState, uint8_t flags, CHIP_ERROR err)
// we're really sure the unsubscribe request has been sent.
if (!mBle->mPlatformDelegate->UnsubscribeCharacteristic(mConnObj, &CHIP_BLE_SVC_ID, &mBle->CHIP_BLE_CHAR_2_ID))
{
printf("\r\n\r\nFinalizeClose debug 8\r\n\r\n");
ChipLogError(Ble, "BtpEngine unsub failed");

// If unsubscribe fails, release BLE connection and free end point immediately.
Free();
}
else if (mConnObj != BLE_CONNECTION_UNINITIALIZED)
{
printf("\r\n\r\nFinalizeClose debug 9\r\n\r\n");
// Unsubscribe request was sent successfully, and a confirmation wasn't spontaneously generated or
// received in the downcall to UnsubscribeCharacteristic, so set timer for the unsubscribe to complete.
err = StartUnsubscribeTimer();
Expand All @@ -446,7 +435,6 @@ void BLEEndPoint::FinalizeClose(uint8_t oldState, uint8_t flags, CHIP_ERROR err)
}
else // mRole == kBleRole_Peripheral, OR mTimerStateFlags.Has(ConnectionStateFlag::kDidBeginSubscribe) == false...
{
printf("\r\n\r\nFinalizeClose debug 10\r\n\r\n");
Free();
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/platform/Ameba/ConnectivityManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,10 @@ CHIP_ERROR ConnectivityManagerImpl::_Init()

// Set callback functions from chip_porting
chip_connmgr_set_callback_func((chip_connmgr_callback)(conn_callback_dispatcher), this);

// Register WiFi event handlers
wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL);
wifi_reg_event_handler(WIFI_EVENT_DISCONNECT, ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler, NULL);

// Ensure that station mode is enabled.
wifi_on(RTW_MODE_STA);
Expand Down Expand Up @@ -156,7 +160,6 @@ void ConnectivityManagerImpl::_OnPlatformEvent(const ChipDeviceEvent * event)
{
ChangeWiFiStationState(kWiFiStationState_Connecting_Failed);
}
DHCPProcess();
}
if (event->Type == DeviceEventType::kRtkWiFiScanCompletedEvent)
{
Expand Down Expand Up @@ -550,8 +553,6 @@ void ConnectivityManagerImpl::DriveStationState()
ChipLogProgress(DeviceLayer, "Attempting to connect WiFi station interface");
rtw_wifi_setting_t wifi_info;
CHIP_GetWiFiConfig(&wifi_info);
wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationConnectedHandler, NULL);
wifi_reg_event_handler(WIFI_EVENT_CONNECT, ConnectivityManagerImpl::RtkWiFiStationDisconnectedHandler, NULL);
wifi_connect((char *) wifi_info.ssid, RTW_SECURITY_WPA_WPA2_MIXED, (char *) wifi_info.password,
strlen((const char *) wifi_info.ssid), strlen((const char *) wifi_info.password), 0, NULL);
ChangeWiFiStationState(kWiFiStationState_Connecting);
Expand Down
10 changes: 5 additions & 5 deletions src/platform/Ameba/NetworkCommissioningWiFiDriver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,26 +40,26 @@ CHIP_ERROR AmebaWiFiDriver::Init(NetworkStatusChangeCallback * networkStatusChan
CHIP_ERROR err;
size_t ssidLen = 0;
size_t credentialsLen = 0;
mpScanCallback = nullptr;
mpConnectCallback = nullptr;
mpStatusChangeCallback = networkStatusChangeCallback;

err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiCredentialsKeyName, mSavedNetwork.credentials,
sizeof(mSavedNetwork.credentials), &credentialsLen);
if (err == CHIP_ERROR_NOT_FOUND)
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
return CHIP_NO_ERROR;
}

err = PersistedStorage::KeyValueStoreMgr().Get(kWiFiSSIDKeyName, mSavedNetwork.ssid, sizeof(mSavedNetwork.ssid), &ssidLen);
if (err == CHIP_ERROR_NOT_FOUND)
if (err == CHIP_ERROR_PERSISTED_STORAGE_VALUE_NOT_FOUND)
{
return CHIP_NO_ERROR;
}
mSavedNetwork.credentialsLen = credentialsLen;
mSavedNetwork.ssidLen = ssidLen;

mStagingNetwork = mSavedNetwork;
mpScanCallback = nullptr;
mpConnectCallback = nullptr;
mpStatusChangeCallback = networkStatusChangeCallback;
return err;
}

Expand Down

0 comments on commit 15b9df7

Please sign in to comment.