Skip to content

Commit

Permalink
Call PlatformMgr::InitChipStack from the device controller (project-c…
Browse files Browse the repository at this point in the history
  • Loading branch information
vivien-apple authored Jul 21, 2020
1 parent 2aef7c9 commit 0320920
Show file tree
Hide file tree
Showing 14 changed files with 79 additions and 179 deletions.
2 changes: 2 additions & 0 deletions config/standalone/standalone-chip.mk
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ STD_LIBS += \
-lDeviceLayer \
-lInetLayer \
-lnlfaultinjection \
-lSupportLayer \
-lSystemLayer

STD_LIBS += $(shell pkg-config --libs openssl)
Expand All @@ -158,6 +159,7 @@ STD_LINK_PREREQUISITES += \
$(CHIP_OUTPUT_DIR)/lib/libDeviceLayer.a \
$(CHIP_OUTPUT_DIR)/lib/libInetLayer.a \
$(CHIP_OUTPUT_DIR)/lib/libnlfaultinjection.a \
$(CHIP_OUTPUT_DIR)/lib/libSupportLayer.a \
$(CHIP_OUTPUT_DIR)/lib/libSystemLayer.a


Expand Down
1 change: 1 addition & 0 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -2476,6 +2476,7 @@ AC_MSG_NOTICE([
PThreads link libraries : ${PTHREAD_LIBS:--}
IniPP compile flags : ${INIPP_CPPFLAGS:--}
GIO compile flags : ${GIO_CFLAGS:--}
GIO link flags : ${GIO_LIBS:--}
C Preprocessor : ${CPP}
C Compiler : ${CC}
C++ Preprocessor : ${CXXCPP}
Expand Down
7 changes: 4 additions & 3 deletions examples/chip-tool/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,8 +257,6 @@ void DoEcho(DeviceController::ChipDeviceController * controller, const IPAddress
controller->SendMessage(NULL, buffer);
printf("Msg sent to server at %s:%d\n", host_ip_str, port);

controller->ServiceEvents();

sleep(SEND_DELAY);
}
}
Expand Down Expand Up @@ -301,7 +299,6 @@ void DoOnOff(DeviceController::ChipDeviceController * controller, Command comman
#endif

controller->SendMessage(NULL, buffer);
controller->ServiceEvents();
}

// ================================================================================
Expand All @@ -325,6 +322,9 @@ int main(int argc, char * argv[])
err = controller->Init(kLocalDeviceId);
VerifyOrExit(err == CHIP_NO_ERROR, fprintf(stderr, "Failed to initialize the device controller"));

err = controller->ServiceEvents();
SuccessOrExit(err);

err = controller->ConnectDevice(kRemoteDeviceId, host_addr, NULL, EchoKeyExchange, EchoResponse, ReceiveError, port);
VerifyOrExit(err == CHIP_NO_ERROR, fprintf(stderr, "Failed to connect to the device"));

Expand All @@ -335,6 +335,7 @@ int main(int argc, char * argv[])
else
{
DoOnOff(controller, command, commandArgs.endpointId);
controller->ServiceEventSignal();
}

exit:
Expand Down
4 changes: 1 addition & 3 deletions src/ble/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@

import("//build_overrides/chip.gni")

import("${chip_root}/src/platform/device.gni")
import("ble.gni")

declare_args() {
# Extra header to include in BleConfig.h for project.
ble_project_config_include = ""

# Extra header to include in BleConfig.h for platform.
ble_platform_config_include = ""
}

config("ble_config") {
Expand Down
136 changes: 32 additions & 104 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@

#include <core/CHIPCore.h>
#include <core/CHIPEncoding.h>
#include <platform/PlatformManager.h>
#include <platform/internal/CHIPDeviceLayerInternal.h>
#include <support/Base64.h>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
Expand Down Expand Up @@ -70,23 +72,7 @@ CHIP_ERROR ChipDeviceController::Init(NodeId localNodeId)

VerifyOrExit(mState == kState_NotInitialized, err = CHIP_ERROR_INCORRECT_STATE);

mSystemLayer = new System::Layer();
mInetLayer = new Inet::InetLayer();

// Initialize the CHIP System Layer.
err = mSystemLayer->Init(NULL);
if (err != CHIP_SYSTEM_NO_ERROR)
{
ChipLogError(Controller, "SystemLayer initialization failed: %s", ErrorStr(err));
}
SuccessOrExit(err);

// Initialize the CHIP Inet layer.
err = mInetLayer->Init(*mSystemLayer, NULL);
if (err != INET_NO_ERROR)
{
ChipLogError(Controller, "InetLayer initialization failed: %s", ErrorStr(err));
}
err = DeviceLayer::PlatformMgr().InitChipStack();
SuccessOrExit(err);

mState = kState_Initialized;
Expand All @@ -98,25 +84,20 @@ CHIP_ERROR ChipDeviceController::Init(NodeId localNodeId)

CHIP_ERROR ChipDeviceController::Shutdown()
{
if (mState != kState_Initialized)
{
return CHIP_ERROR_INCORRECT_STATE;
}

CHIP_ERROR err = CHIP_NO_ERROR;
mState = kState_NotInitialized;

VerifyOrExit(mState == kState_Initialized, err = CHIP_ERROR_INCORRECT_STATE);

mState = kState_NotInitialized;

err = DeviceLayer::PlatformMgr().Shutdown();
SuccessOrExit(err);

if (mSessionManager != NULL)
{
delete mSessionManager;
mSessionManager = NULL;
}
mSystemLayer->Shutdown();
mInetLayer->Shutdown();
delete mSystemLayer;
delete mInetLayer;
mSystemLayer = NULL;
mInetLayer = NULL;

mConState = kConnectionState_NotConnected;
memset(&mOnComplete, 0, sizeof(mOnComplete));
Expand All @@ -125,6 +106,7 @@ CHIP_ERROR ChipDeviceController::Shutdown()
mMessageNumber = 0;
mRemoteDeviceId.ClearValue();

exit:
return err;
}

Expand All @@ -147,8 +129,8 @@ CHIP_ERROR ChipDeviceController::ConnectDevice(NodeId remoteDeviceId, IPAddress

mSessionManager = new SecureSessionMgr<Transport::UDP>();

err = mSessionManager->Init(mLocalDeviceId, mSystemLayer,
Transport::UdpListenParameters(mInetLayer).SetAddressType(deviceAddr.Type()));
err = mSessionManager->Init(mLocalDeviceId, &DeviceLayer::SystemLayer,
Transport::UdpListenParameters(&DeviceLayer::InetLayer).SetAddressType(deviceAddr.Type()));
SuccessOrExit(err);

mSessionManager->SetDelegate(this);
Expand Down Expand Up @@ -252,87 +234,33 @@ CHIP_ERROR ChipDeviceController::SendMessage(void * appReqState, PacketBuffer *
return err;
}

CHIP_ERROR ChipDeviceController::GetLayers(Layer ** systemLayer, InetLayer ** inetLayer)
CHIP_ERROR ChipDeviceController::ServiceEvents()
{
if (mState != kState_Initialized)
{
return CHIP_ERROR_INCORRECT_STATE;
}
if (systemLayer != NULL)
{
*systemLayer = mSystemLayer;
}
if (inetLayer != NULL)
{
*inetLayer = mInetLayer;
}

return CHIP_NO_ERROR;
}

void ChipDeviceController::ServiceEvents()
{
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
if (mState != kState_Initialized)
{
return;
}

// Set the select timeout to 100ms
struct timeval aSleepTime;
aSleepTime.tv_sec = 0;
aSleepTime.tv_usec = 100 * 1000;

#if !__ZEPHYR__
static bool printed = false;

if (!printed)
{
{
ChipLogProgress(Controller, "CHIP node ready to service events; PID: %d; PPID: %d\n", getpid(), getppid());
printed = true;
}
}
#endif // !__ZEPHYR__
fd_set readFDs, writeFDs, exceptFDs;
int numFDs = 0;
CHIP_ERROR err = CHIP_NO_ERROR;

FD_ZERO(&readFDs);
FD_ZERO(&writeFDs);
FD_ZERO(&exceptFDs);
VerifyOrExit(mState == kState_Initialized, err = CHIP_ERROR_INCORRECT_STATE);

if (mSystemLayer->State() == System::kLayerState_Initialized)
{
mSystemLayer->PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
}

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (mInetLayer->State == Inet::InetLayer::kState_Initialized)
{
mInetLayer->PrepareSelect(numFDs, &readFDs, &writeFDs, &exceptFDs, aSleepTime);
}
#endif
err = DeviceLayer::PlatformMgr().StartEventLoopTask();
SuccessOrExit(err);

int selectRes = select(numFDs, &readFDs, &writeFDs, &exceptFDs, &aSleepTime);
if (selectRes < 0)
{
ChipLogError(Controller, "select failed: %s\n", ErrorStr(System::MapErrorPOSIX(errno)));
return;
}
exit:
return err;
}

if (mSystemLayer->State() == System::kLayerState_Initialized)
{
mSystemLayer->HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
}
CHIP_ERROR ChipDeviceController::ServiceEventSignal()
{
CHIP_ERROR err = CHIP_NO_ERROR;

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS
if (mInetLayer->State == Inet::InetLayer::kState_Initialized)
{
mInetLayer->HandleSelectResult(selectRes, &readFDs, &writeFDs, &exceptFDs);
}
#endif
VerifyOrExit(mState == kState_Initialized, err = CHIP_ERROR_INCORRECT_STATE);

#if CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK
DeviceLayer::SystemLayer.WakeSelect();
#else
err = CHIP_ERROR_UNSUPPORTED_CHIP_FEATURE;
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS || CHIP_SYSTEM_CONFIG_USE_NETWORK_FRAMEWORK

exit:
return err;
}

void ChipDeviceController::ClearRequestState()
Expand Down
20 changes: 7 additions & 13 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,18 @@ class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback
// ----- IO -----
/**
* @brief
* Allow the CHIP Stack to process any pending events
* This can be called in an event handler loop to tigger callbacks within the CHIP stack
* Note - Some platforms might need to implement their own event handler
* Start the event loop task within the CHIP stack
* @return CHIP_ERROR The return status
*/
void ServiceEvents();
CHIP_ERROR ServiceEvents();

/**
* @brief
* Get pointers to the Layers ownerd by the controller
*
* @param[out] systemLayer A pointer to the SystemLayer object
* @param[out] inetLayer A pointer to the InetLayer object
* @return CHIP_ERROR Indicates whether the layers were populated correctly
* Allow the CHIP Stack to process any pending events
* This can be called in an event handler loop to tigger callbacks within the CHIP stack
* @return CHIP_ERROR The return status
*/
CHIP_ERROR GetLayers(Layer ** systemLayer, InetLayer ** inetLayer);
CHIP_ERROR ServiceEventSignal();

virtual void OnMessageReceived(const MessageHeader & header, Transport::PeerConnectionState * state,
System::PacketBuffer * msgBuf, SecureSessionMgrBase * mgr);
Expand All @@ -177,9 +174,6 @@ class DLL_EXPORT ChipDeviceController : public SecureSessionMgrCallback
kConnectionState_SecureConnected = 2,
};

System::Layer * mSystemLayer;
Inet::InetLayer * mInetLayer;

SecureSessionMgr<Transport::UDP> * mSessionManager;

ConnectionState mConState;
Expand Down
30 changes: 3 additions & 27 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -204,8 +204,9 @@ - (BOOL)connect:(NSString *)ipAddress
}

// Start the IO pump
[self _serviceEvents];

dispatch_async(_chipSelectQueue, ^() {
self.cppController->ServiceEvents();
});
return YES;
}

Expand Down Expand Up @@ -331,31 +332,6 @@ - (BOOL)isConnected
return isConnected ? YES : NO;
}

// TODO kill this with fire (NW might implicitly replace this?)
- (void)_serviceEvents
{
__weak typeof(self) weakSelf = self;
dispatch_async(self.chipSelectQueue, ^() {
typeof(self) strongSelf = weakSelf;
if (!strongSelf) {
return;
}

[self.lock lock];

if (!self.cppController->IsConnected()) {
[self.lock unlock];
// cancel the loop, it'll restart the next time a connection is established
return;
}

self.cppController->ServiceEvents();
[self.lock unlock];

[self _serviceEvents];
});
}

- (void)registerCallbacks:appCallbackQueue onMessage:(ControllerOnMessageBlock)onMessage onError:(ControllerOnErrorBlock)onError
{
self.appCallbackQueue = appCallbackQueue;
Expand Down
4 changes: 1 addition & 3 deletions src/inet/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,13 @@ import("//build_overrides/nlassert.gni")
import("//build_overrides/nlfaultinjection.gni")
import("//build_overrides/nlio.gni")

import("${chip_root}/src/platform/device.gni")
import("${chip_root}/gn/chip/tests.gni")
import("inet.gni")

declare_args() {
# Extra header to include in SystemConfig.h for project.
inet_project_config_include = ""

# Extra header to include in SystemConfig.h for platform.
inet_platform_config_include = ""
}

config("inet_config") {
Expand Down
6 changes: 6 additions & 0 deletions src/lib/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,12 @@ libCHIP_a_CPPFLAGS = \
$(SOCKETS_CPPFLAGS) \
$(NULL)

if CHIP_DEVICE_LAYER_TARGET_LINUX
libCHIP_a_CPPFLAGS += \
$(GIO_CFLAGS) \
$(NULL)
endif

libCHIP_a_SOURCES = $(CHIP_BUILD_SYSTEM_LAYER_SOURCE_FILES)
libCHIP_a_SOURCES += $(CHIP_BUILD_INET_LAYER_SOURCE_FILES)
libCHIP_a_SOURCES += $(CHIP_BUILD_CORE_LAYER_SOURCE_FILES)
Expand Down
4 changes: 1 addition & 3 deletions src/platform/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,6 @@ if (device_platform != "none") {
declare_args() {
# Extra header to include in CHIPDeviceConfig.h for project.
chip_device_project_config_include = ""

# Extra header to include in CHIPDeviceConfig.h for platform.
chip_device_platform_config_include = ""
}

config("platform_config") {
Expand Down Expand Up @@ -104,6 +101,7 @@ if (device_platform != "none") {

public_deps = [
"${chip_root}/src/ble",
"${chip_root}/src/inet",
"${chip_root}/src/lib/core:chip_config_header",
"${chip_root}/src/lib/support",
"${nlio_root}:nlio",
Expand Down
Loading

0 comments on commit 0320920

Please sign in to comment.