forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt System layer to Zephyr OS (project-chip#1594)
* Adapt System layer to Zephyr socket API The System layer was based on the assumption that when BSD socket API is available then other POSIX functions can be used as well. Unfortunately, this is not the case in Zephyr which only implements a subset of POSIX requirements. Replace usage of pipe() system call with an equivalent usage of socketpair(). Update KConfig of the door-lock example for nrfconnect platform to set up networking properly. This only makes sure that libSystemLayer.a builds correctly when NCS and the socket-based Inet layer is used. However, the Inet layer itself still has some build issues which need to be resolved. * Use eventfd for system event * Adapt to GN build system and add unit tests Co-authored-by: Rafał Kuźnia <rafal.kuznia@nordicsemi.no>
- Loading branch information
Showing
12 changed files
with
467 additions
and
49 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file declares the abstraction of one-directional, anonymous | ||
* data stream built on top of two file descriptors. | ||
*/ | ||
|
||
#include <system/SystemWakeEvent.h> | ||
|
||
#if CHIP_SYSTEM_CONFIG_USE_SOCKETS | ||
|
||
// Include additional CHIP headers | ||
#include <support/CodeUtils.h> | ||
|
||
// Include system and language headers | ||
#include <errno.h> | ||
#include <fcntl.h> | ||
#include <unistd.h> | ||
|
||
#if !CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE | ||
#include <sys/eventfd.h> | ||
#endif | ||
|
||
namespace chip { | ||
namespace System { | ||
|
||
#if CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE | ||
|
||
namespace { | ||
inline int SetNonBlockingMode(int fd) | ||
{ | ||
int flags = ::fcntl(fd, F_GETFL, 0); | ||
return ::fcntl(fd, F_SETFL, flags | O_NONBLOCK); | ||
} | ||
} // anonymous namespace | ||
|
||
Error SystemWakeEvent::Open() | ||
{ | ||
mFDs[FD_READ] = mFDs[FD_WRITE] = -1; | ||
|
||
if (::pipe(mFDs) < 0) | ||
return chip::System::MapErrorPOSIX(errno); | ||
|
||
if (SetNonBlockingMode(mFDs[FD_READ]) < 0) | ||
return chip::System::MapErrorPOSIX(errno); | ||
|
||
if (SetNonBlockingMode(mFDs[FD_WRITE]) < 0) | ||
return chip::System::MapErrorPOSIX(errno); | ||
|
||
return CHIP_SYSTEM_NO_ERROR; | ||
} | ||
|
||
void SystemWakeEvent::Close() | ||
{ | ||
::close(mFDs[FD_WRITE]); | ||
::close(mFDs[FD_READ]); | ||
mFDs[FD_READ] = mFDs[FD_WRITE] = -1; | ||
} | ||
|
||
void SystemWakeEvent::Confirm() | ||
{ | ||
uint8_t buffer[128]; | ||
|
||
while (::read(mFDs[FD_READ], buffer, sizeof(buffer)) == sizeof(buffer)) | ||
continue; | ||
} | ||
|
||
void SystemWakeEvent::Notify() | ||
{ | ||
char byte = 1; | ||
::write(mFDs[FD_WRITE], &byte, 1); | ||
} | ||
|
||
#else // CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE | ||
|
||
Error SystemWakeEvent::Open() | ||
{ | ||
mFD = eventfd(0, EFD_NONBLOCK); | ||
|
||
if (mFD == -1) | ||
{ | ||
return chip::System::MapErrorPOSIX(errno); | ||
} | ||
|
||
return CHIP_SYSTEM_NO_ERROR; | ||
} | ||
|
||
void SystemWakeEvent::Close() | ||
{ | ||
::close(mFD); | ||
mFD = -1; | ||
} | ||
|
||
void SystemWakeEvent::Confirm() | ||
{ | ||
uint64_t value; | ||
::read(mFD, &value, sizeof(value)); | ||
} | ||
|
||
void SystemWakeEvent::Notify() | ||
{ | ||
uint64_t value = 1; | ||
::write(mFD, &value, sizeof(value)); | ||
} | ||
|
||
#endif // CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE | ||
|
||
} // namespace System | ||
} // namespace chip | ||
|
||
#endif // CHIP_SYSTEM_CONFIG_USE_SOCKETS |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* | ||
* Copyright (c) 2020 Project CHIP Authors | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file declares the abstraction of system wake event used for | ||
* resuming task from select system call. | ||
*/ | ||
|
||
#ifndef SYSTEMWAKEEVENT_H | ||
#define SYSTEMWAKEEVENT_H | ||
|
||
// Include configuration headers | ||
#include <system/SystemConfig.h> | ||
|
||
#include <system/SystemError.h> | ||
|
||
namespace chip { | ||
namespace System { | ||
|
||
using ::chip::System::Error; | ||
|
||
class SystemWakeEvent | ||
{ | ||
public: | ||
Error Open(); /**< Initialize the pipeline */ | ||
void Close(); /**< Close both ends of the pipeline. */ | ||
|
||
#if CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE | ||
int GetNotifFD() const { return mFDs[FD_READ]; } | ||
#else | ||
int GetNotifFD() const { return mFD; } | ||
#endif | ||
|
||
void Notify(); /**< Set the event. */ | ||
void Confirm(); /**< Clear the event. */ | ||
|
||
private: | ||
#if CHIP_SYSTEM_CONFIG_USE_POSIX_PIPE | ||
enum | ||
{ | ||
FD_READ = 0, | ||
FD_WRITE = 1 | ||
}; | ||
|
||
int mFDs[2]; | ||
#else | ||
int mFD; | ||
#endif | ||
}; | ||
|
||
} // namespace System | ||
} // namespace chip | ||
|
||
#endif // SYSTEMWAKEEVENT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.