Skip to content

Commit

Permalink
Type safety for GLibMatterContextInvokeSynchronous
Browse files Browse the repository at this point in the history
  • Loading branch information
arkq committed Mar 26, 2023
1 parent 49d6474 commit 6ff2e19
Show file tree
Hide file tree
Showing 9 changed files with 32 additions and 28 deletions.
7 changes: 3 additions & 4 deletions src/platform/Tizen/BLEManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,9 +445,8 @@ void BLEManagerImpl::HandleConnectionTimeout(System::Layer * layer, void * data)
sInstance.NotifyHandleConnectFailed(CHIP_ERROR_TIMEOUT);
}

CHIP_ERROR BLEManagerImpl::ConnectChipThing(void * userData)
CHIP_ERROR BLEManagerImpl::ConnectChipThing(const char * address)
{
auto address = reinterpret_cast<const char *>(userData);
CHIP_ERROR err = CHIP_NO_ERROR;
int ret;

Expand Down Expand Up @@ -504,7 +503,7 @@ void BLEManagerImpl::OnChipDeviceScanned(void * device, const Ble::ChipBLEDevice
mDeviceScanner->StopChipScan();

/* Initiate Connect */
PlatformMgrImpl().GLibMatterContextInvokeSynchronous(ConnectChipThing, deviceInfo->remote_address);
PlatformMgrImpl().GLibMatterContextInvokeSynchronous(ConnectChipThing, const_cast<const char *>(deviceInfo->remote_address));
}

void BLEManagerImpl::OnScanComplete()
Expand Down Expand Up @@ -956,7 +955,7 @@ CHIP_ERROR BLEManagerImpl::_Init()
mServiceMode = ConnectivityManager::kCHIPoBLEServiceMode_Enabled;

ChipLogProgress(DeviceLayer, "Initialize BLE");
err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_BleInitialize, nullptr);
err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_BleInitialize, static_cast<void *>(nullptr));
SuccessOrExit(err);

PlatformMgr().ScheduleWork(DriveBLEState, 0);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Tizen/BLEManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ class BLEManagerImpl final : public BLEManager,

// ==== Connection.
// void ConnectHandler(const char * address);
static CHIP_ERROR ConnectChipThing(gpointer userData);
static CHIP_ERROR ConnectChipThing(const char * userData);
void NotifyBLEConnectionEstablished(BLE_CONNECTION_OBJECT conId, CHIP_ERROR error);
void NotifyBLEDisconnection(BLE_CONNECTION_OBJECT conId, CHIP_ERROR error);
void NotifyHandleNewConnection(BLE_CONNECTION_OBJECT conId);
Expand Down
7 changes: 3 additions & 4 deletions src/platform/Tizen/ChipDeviceScanner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -143,22 +143,21 @@ gboolean ChipDeviceScanner::TimerExpiredCb(gpointer userData)
return G_SOURCE_REMOVE;
}

CHIP_ERROR ChipDeviceScanner::TriggerScan(gpointer userData)
CHIP_ERROR ChipDeviceScanner::TriggerScan(ChipDeviceScanner * self)
{
auto self = reinterpret_cast<ChipDeviceScanner *>(userData);
CHIP_ERROR err = CHIP_NO_ERROR;
GSource * idleSource;
int ret;

// Trigger LE Scan
ret = bt_adapter_le_start_scan(LeScanResultCb, userData);
ret = bt_adapter_le_start_scan(LeScanResultCb, self);
VerifyOrExit(ret == BT_ERROR_NONE, ChipLogError(DeviceLayer, "bt_adapter_le_start_scan() failed: %s", get_error_message(ret));
err = CHIP_ERROR_INTERNAL);
self->mIsScanning = true;

// Setup timer for scan timeout
idleSource = g_timeout_source_new(self->mScanTimeoutMs);
g_source_set_callback(idleSource, TimerExpiredCb, userData, nullptr);
g_source_set_callback(idleSource, TimerExpiredCb, self, nullptr);
g_source_set_priority(idleSource, G_PRIORITY_HIGH_IDLE);
g_source_attach(idleSource, g_main_context_get_thread_default());
g_source_unref(idleSource);
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Tizen/ChipDeviceScanner.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class ChipDeviceScanner
private:
static void LeScanResultCb(int result, bt_adapter_le_device_scan_result_info_s * info, void * userData);
static gboolean TimerExpiredCb(gpointer user_data);
static CHIP_ERROR TriggerScan(gpointer userData);
static CHIP_ERROR TriggerScan(ChipDeviceScanner * userData);
void CheckScanFilter(ScanFilterType filterType, ScanFilterData & filterData);
int RegisterScanFilter(ScanFilterType filterType, ScanFilterData & filterData);
void UnRegisterScanFilter(void);
Expand Down
11 changes: 3 additions & 8 deletions src/platform/Tizen/DnssdImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,12 +108,10 @@ void OnRegister(dnssd_error_e result, dnssd_service_h service, void * data)
rCtx->mCallback(rCtx->mCbContext, rCtx->mType, rCtx->mName, CHIP_NO_ERROR);
}

CHIP_ERROR RegisterAsync(gpointer userData)
CHIP_ERROR RegisterAsync(chip::Dnssd::RegisterContext * rCtx)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);

auto rCtx = reinterpret_cast<chip::Dnssd::RegisterContext *>(userData);

int ret = dnssd_register_local_service(rCtx->mServiceHandle, OnRegister, rCtx);
VerifyOrReturnValue(ret == DNSSD_ERROR_NONE, GetChipError(ret),
ChipLogError(DeviceLayer, "dnssd_register_local_service() failed. ret: %d", ret));
Expand Down Expand Up @@ -231,11 +229,10 @@ void OnBrowse(dnssd_service_state_e state, dnssd_service_h service, void * data)
g_free(ifaceName);
}

CHIP_ERROR BrowseAsync(gpointer userData)
CHIP_ERROR BrowseAsync(chip::Dnssd::BrowseContext * bCtx)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);

auto * bCtx = reinterpret_cast<chip::Dnssd::BrowseContext *>(userData);
auto interfaceId = bCtx->mInterfaceId;
int ret;

Expand Down Expand Up @@ -394,12 +391,10 @@ void OnResolve(dnssd_error_e result, dnssd_service_h service, void * userData)
rCtx->mInstance->RemoveContext(rCtx);
}

CHIP_ERROR ResolveAsync(gpointer userData)
CHIP_ERROR ResolveAsync(chip::Dnssd::ResolveContext * rCtx)
{
ChipLogDetail(DeviceLayer, "DNSsd %s", __func__);

auto * rCtx = reinterpret_cast<chip::Dnssd::ResolveContext *>(userData);

int ret = dnssd_resolve_service(rCtx->mServiceHandle, OnResolve, rCtx);
VerifyOrReturnValue(ret == DNSSD_ERROR_NONE, GetChipError(ret),
ChipLogError(DeviceLayer, "dnssd_resolve_service() failed. ret: %d", ret));
Expand Down
2 changes: 1 addition & 1 deletion src/platform/Tizen/PlatformManagerImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void PlatformManagerImpl::_Shutdown()
g_thread_join(mGLibMainLoopThread);
}

CHIP_ERROR PlatformManagerImpl::GLibMatterContextInvokeSynchronous(CHIP_ERROR (*func)(void *), void * userData)
CHIP_ERROR PlatformManagerImpl::_GLibMatterContextInvokeSynchronous(CHIP_ERROR (*func)(void *), void * userData)
{
GLibMatterContextInvokeData invokeData{ func, userData };

Expand Down
14 changes: 13 additions & 1 deletion src/platform/Tizen/PlatformManagerImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,11 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener
* @param[in] userData User data to pass to the function.
* @returns The result of the function.
*/
CHIP_ERROR GLibMatterContextInvokeSynchronous(CHIP_ERROR (*func)(void *), void * userData);
template <typename T>
CHIP_ERROR GLibMatterContextInvokeSynchronous(CHIP_ERROR (*func)(T *), T * userData)
{
return _GLibMatterContextInvokeSynchronous((CHIP_ERROR(*)(void *)) func, (void *) userData);
}

private:
// ===== Methods that implement the PlatformManager abstract interface.
Expand All @@ -77,6 +81,14 @@ class PlatformManagerImpl final : public PlatformManager, public Internal::Gener

static PlatformManagerImpl sInstance;

/**
* @brief Invoke a function on the Matter GLib context.
*
* @note This function does not provide type safety for the user data. Please,
* use the GLibMatterContextInvokeSynchronous() template function instead.
*/
CHIP_ERROR _GLibMatterContextInvokeSynchronous(CHIP_ERROR (*func)(void *), void * userData);

GMainLoop * mGLibMainLoop;
GThread * mGLibMainLoopThread;
};
Expand Down
13 changes: 6 additions & 7 deletions src/platform/Tizen/WiFiManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,10 +374,9 @@ CHIP_ERROR WiFiManager::_WiFiScan(gpointer userData)
return CHIP_NO_ERROR;
}

CHIP_ERROR WiFiManager::_WiFiConnect(gpointer userData)
CHIP_ERROR WiFiManager::_WiFiConnect(wifi_manager_ap_h ap)
{
wifi_manager_ap_h ap = static_cast<wifi_manager_ap_h>(userData);
CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;
int wifiErr;

wifiErr = wifi_manager_connect(sInstance.mWiFiManagerHandle, ap, _ConnectedCb, nullptr);
Expand Down Expand Up @@ -553,7 +552,7 @@ void WiFiManager::Init()
sInstance.mModuleState = WIFI_MANAGER_MODULE_STATE_DETACHED;
sInstance.mConnectionState = WIFI_MANAGER_CONNECTION_STATE_DISCONNECTED;

PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiInitialize, nullptr);
PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiInitialize, static_cast<void *>(nullptr));
}

void WiFiManager::Deinit()
Expand All @@ -577,7 +576,7 @@ CHIP_ERROR WiFiManager::Activate()
VerifyOrExit((err = IsActivated(&isWiFiActivated)) == CHIP_NO_ERROR, );
VerifyOrExit(isWiFiActivated == false, ChipLogProgress(DeviceLayer, "WiFi is already activated"));

err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiActivate, nullptr);
err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiActivate, static_cast<void *>(nullptr));
SuccessOrExit(err);

exit:
Expand All @@ -592,7 +591,7 @@ CHIP_ERROR WiFiManager::Deactivate()
VerifyOrExit((err = IsActivated(&isWiFiActivated)) == CHIP_NO_ERROR, );
VerifyOrExit(isWiFiActivated == true, ChipLogProgress(DeviceLayer, "WiFi is already deactivated"));

err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiDeactivate, nullptr);
err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiDeactivate, static_cast<void *>(nullptr));
SuccessOrExit(err);

exit:
Expand Down Expand Up @@ -625,7 +624,7 @@ CHIP_ERROR WiFiManager::Connect(const char * ssid, const char * key,
}
else
{
err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiScan, nullptr);
err = PlatformMgrImpl().GLibMatterContextInvokeSynchronous(_WiFiScan, static_cast<void *>(nullptr));
SuccessOrExit(err);
}

Expand Down
2 changes: 1 addition & 1 deletion src/platform/Tizen/WiFiManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class WiFiManager
static CHIP_ERROR _WiFiActivate(gpointer userData);
static CHIP_ERROR _WiFiDeactivate(gpointer userData);
static CHIP_ERROR _WiFiScan(gpointer userData);
static CHIP_ERROR _WiFiConnect(gpointer userData);
static CHIP_ERROR _WiFiConnect(wifi_manager_ap_h ap);

void _WiFiDeinitialize();
void _WiFiSetStates();
Expand Down

0 comments on commit 6ff2e19

Please sign in to comment.