Skip to content

Commit

Permalink
Merge branch 'master' into 01_add_time_source
Browse files Browse the repository at this point in the history
  • Loading branch information
andreilitvin committed Jun 17, 2020
2 parents fca866b + af83f1d commit f522673
Show file tree
Hide file tree
Showing 21 changed files with 171 additions and 116 deletions.
2 changes: 1 addition & 1 deletion config/esp32/components/chip/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ menu "CHIP Device Layer"
config CHIP_TASK_STACK_SIZE
int "CHIP Task Stack Size"
range 0 65535
default 4608
default 8192
help
The size (in bytes) of the CHIP task stack.

Expand Down
18 changes: 13 additions & 5 deletions examples/wifi-echo/server/esp32/main/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@
#include <inet/InetError.h>
#include <inet/InetLayer.h>
#include <platform/CHIPDeviceLayer.h>
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
#include <system/SystemPacketBuffer.h>
#include <transport/SecureTransport.h>
#include <transport/SecureSessionMgr.h>
#include <transport/UDP.h>

#include "DataModelHandler.h"
Expand Down Expand Up @@ -85,7 +86,7 @@ bool ContentIsAPrintableString(System::PacketBuffer * buffer)
return isPrintable;
}

void newConnectionHandler(const MessageHeader & header, const IPPacketInfo & packet_info, SecureTransport * transport)
void newConnectionHandler(const MessageHeader & header, const IPPacketInfo & packet_info, SecureSessionMgr * transport)
{
CHIP_ERROR err;

Expand All @@ -106,7 +107,7 @@ void newConnectionHandler(const MessageHeader & header, const IPPacketInfo & pac

// Transport Callbacks
void echo(const MessageHeader & header, const IPPacketInfo & packet_info, System::PacketBuffer * buffer,
SecureTransport * transport)
SecureSessionMgr * transport)
{
CHIP_ERROR err;
const size_t data_len = buffer->DataLength();
Expand Down Expand Up @@ -159,10 +160,16 @@ void echo(const MessageHeader & header, const IPPacketInfo & packet_info, System
}
}

void error(CHIP_ERROR error, const IPPacketInfo & pi)
{
ESP_LOGE(TAG, "ERROR: %s\n Got UDP error", ErrorStr(error));
statusLED.BlinkOnError();
}

} // namespace

// The echo server assumes the platform's networking has been setup already
void setupTransport(IPAddressType type, SecureTransport * transport)
void setupTransport(IPAddressType type, SecureSessionMgr * transport)
{
CHIP_ERROR err = CHIP_NO_ERROR;
struct netif * netif = NULL;
Expand All @@ -176,6 +183,7 @@ void setupTransport(IPAddressType type, SecureTransport * transport)
SuccessOrExit(err);

transport->SetMessageReceiveHandler(echo, transport);
transport->SetReceiveErrorHandler(error);
transport->SetNewConnectionHandler(newConnectionHandler, transport);

exit:
Expand All @@ -190,7 +198,7 @@ void setupTransport(IPAddressType type, SecureTransport * transport)
}

// The echo server assumes the platform's networking has been setup already
void startServer(SecureTransport * transport_ipv4, SecureTransport * transport_ipv6)
void startServer(SecureSessionMgr * transport_ipv4, SecureSessionMgr * transport_ipv6)
{
setupTransport(kIPAddressType_IPv6, transport_ipv6);
setupTransport(kIPAddressType_IPv4, transport_ipv4);
Expand Down
6 changes: 3 additions & 3 deletions examples/wifi-echo/server/esp32/main/wifi-echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,12 +35,12 @@

#include <platform/CHIPDeviceLayer.h>
#include <support/ErrorStr.h>
#include <transport/SecureTransport.h>
#include <transport/SecureSessionMgr.h>

using namespace ::chip;
using namespace ::chip::DeviceLayer;

extern void startServer(SecureTransport * transportIPv4, SecureTransport * transportIPv6);
extern void startServer(SecureSessionMgr * transportIPv4, SecureSessionMgr * transportIPv6);
extern void startClient(void);

#if CONFIG_DEVICE_TYPE_M5STACK
Expand Down Expand Up @@ -170,7 +170,7 @@ extern "C" void app_main()

// Start the Echo Server
InitDataModelHandler();
SecureTransport sTransportIPv4, sTransportIPv6;
SecureSessionMgr sTransportIPv4, sTransportIPv6;
startServer(&sTransportIPv4, &sTransportIPv6);
#if CONFIG_USE_ECHO_CLIENT
startClient();
Expand Down
16 changes: 15 additions & 1 deletion src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ CHIP_ERROR ChipDeviceController::ConnectDevice(NodeId remoteDeviceId, IPAddress
mDeviceAddr = deviceAddr;
mDevicePort = devicePort;
mAppReqState = appReqState;
mDeviceCon = new SecureTransport();
mDeviceCon = new SecureSessionMgr();

err = mDeviceCon->Init(mLocalDeviceId, mInetLayer, Transport::UdpListenParameters().SetAddressType(deviceAddr.Type()));
SuccessOrExit(err);
Expand Down Expand Up @@ -187,6 +187,20 @@ CHIP_ERROR ChipDeviceController::ManualKeyExchange(const unsigned char * remote_
return err;
}

CHIP_ERROR ChipDeviceController::PopulatePeerAddress(Transport::PeerAddress & peerAddress)
{
CHIP_ERROR err = CHIP_NO_ERROR;

VerifyOrExit(IsSecurelyConnected(), err = CHIP_ERROR_INCORRECT_STATE);

peerAddress.SetIPAddress(mDeviceAddr);
peerAddress.SetPort(mDevicePort);
peerAddress.SetTransportType(Transport::Type::kUdp);

exit:
return err;
}

bool ChipDeviceController::IsConnected()
{
return kState_Initialized && (mConState == kConnectionState_Connected || mConState == kConnectionState_SecureConnected);
Expand Down
13 changes: 11 additions & 2 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#include <core/CHIPCore.h>
#include <core/CHIPTLV.h>
#include <support/DLLUtil.h>
#include <transport/SecureTransport.h>
#include <transport/SecureSessionMgr.h>
#include <transport/UDP.h>

namespace chip {
Expand Down Expand Up @@ -89,6 +89,15 @@ class DLL_EXPORT ChipDeviceController
CHIP_ERROR ManualKeyExchange(const unsigned char * remote_public_key, const size_t public_key_length,
const unsigned char * local_private_key, const size_t private_key_length);

/**
* @brief
* Get the PeerAddress of a connected peer
*
* @param[inout] peerAddress The PeerAddress object which will be populated with the details of the connected peer
* @return CHIP_ERROR An error if there's no active connection
*/
CHIP_ERROR PopulatePeerAddress(Transport::PeerAddress & peerAddress);

/**
* @brief
* Disconnect from a connected device
Expand Down Expand Up @@ -159,7 +168,7 @@ class DLL_EXPORT ChipDeviceController

System::Layer * mSystemLayer;
Inet::InetLayer * mInetLayer;
SecureTransport * mDeviceCon;
SecureSessionMgr * mDeviceCon;

ConnectionState mConState;
void * mAppReqState;
Expand Down
4 changes: 2 additions & 2 deletions src/darwin/CHIPTool/CHIPTool/CHIPViewControllerBase.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ NS_ASSUME_NONNULL_BEGIN

@interface CHIPViewControllerBase : UIViewController

@property (readwrite) BOOL useCorrectKey;
@property (readwrite) BOOL useCorrectKeyStateChanged;
@property (readwrite) BOOL useIncorrectKey;
@property (readwrite) BOOL useIncorrectKeyStateChanged;
@property (readwrite) CHIPDeviceController * chipController;
@property (weak, nonatomic) IBOutlet UILabel * resultLabel;
@property (weak, nonatomic) IBOutlet UISwitch * encryptionKeySwitch;
Expand Down
20 changes: 10 additions & 10 deletions src/darwin/CHIPTool/CHIPTool/CHIPViewControllerBase.m
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,8 @@ - (void)viewDidLoad
}
[self.serverIPTextField setHidden:shouldHide];
[self.IPLabel setHidden:shouldHide];
self.useCorrectKey = YES;
self.useCorrectKeyStateChanged = NO;
self.useIncorrectKey = NO;
self.useIncorrectKeyStateChanged = NO;
}

- (void)_appEnteredBackground:(NSNotification *)notification
Expand Down Expand Up @@ -122,7 +122,7 @@ - (void)_connect

NSData * peer_key = [NSData dataWithBytes:peer_key_bytes length:sizeof(peer_key_bytes)];
NSData * local_key = NULL;
if (self.useCorrectKey) {
if (!self.useIncorrectKey) {
NSLog(@"Using correct key");
local_key = [NSData dataWithBytes:local_key_bytes length:sizeof(local_key_bytes)];
} else {
Expand All @@ -143,13 +143,13 @@ - (void)_connect
- (IBAction)encryptionKey:(id)sender
{
if ([self.encryptionKeySwitch isOn]) {
self.useCorrectKey = YES;
[self postResult:@"App will use correct key"];
} else {
self.useCorrectKey = NO;
self.useIncorrectKey = YES;
[self postResult:@"App will use incorrect key"];
} else {
self.useIncorrectKey = NO;
[self postResult:@"App will use correct key"];
}
self.useCorrectKeyStateChanged = YES;
self.useIncorrectKeyStateChanged = YES;
}

- (void)dismissKeyboard
Expand Down Expand Up @@ -180,9 +180,9 @@ - (void)reconnectIfNeeded
}
}

if (!addrInfo || needsReconnect || self.useCorrectKeyStateChanged) {
if (!addrInfo || needsReconnect || self.useIncorrectKeyStateChanged) {
[self _connect];
self.useCorrectKeyStateChanged = NO;
self.useIncorrectKeyStateChanged = NO;
}
}

Expand Down
18 changes: 8 additions & 10 deletions src/darwin/CHIPTool/CHIPTool/UI/Base.lproj/Main.storyboard
Original file line number Diff line number Diff line change
Expand Up @@ -384,13 +384,13 @@
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Use Correct Encryption Keys" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="778-me-ArK">
<rect key="frame" x="16" y="273" width="219.66666666666666" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" text="Use Incorrect Encryption Keys" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="778-me-ArK">
<rect key="frame" x="16" y="273" width="231" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" selected="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="dN9-8p-BcA">
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" selected="YES" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="dN9-8p-BcA">
<rect key="frame" x="310" y="268" width="51" height="31"/>
<connections>
<action selector="encryptionKey:" destination="NR6-8N-IDd" eventType="valueChanged" id="Ggg-GX-tuM"/>
Expand Down Expand Up @@ -418,7 +418,6 @@
<constraint firstItem="6Z1-j3-AvT" firstAttribute="leading" secondItem="UPn-Qe-uvu" secondAttribute="leading" constant="16" id="fdf-Mv-mqP"/>
<constraint firstItem="6Z1-j3-AvT" firstAttribute="top" secondItem="lvN-Eg-W6F" secondAttribute="bottom" constant="40" id="keR-8B-8EU"/>
<constraint firstItem="Tzm-Xh-QOZ" firstAttribute="leading" secondItem="UPn-Qe-uvu" secondAttribute="leading" constant="16" id="oFI-15-MgQ"/>
<constraint firstItem="dN9-8p-BcA" firstAttribute="leading" secondItem="778-me-ArK" secondAttribute="trailing" constant="74.333333333333343" id="vAd-2E-26Q"/>
<constraint firstItem="778-me-ArK" firstAttribute="centerY" secondItem="dN9-8p-BcA" secondAttribute="centerY" id="vRO-8x-KDG"/>
<constraint firstItem="tC3-Jk-Opv" firstAttribute="top" secondItem="7aI-Vo-UyO" secondAttribute="bottom" constant="8" id="xkI-6e-Plp"/>
</constraints>
Expand Down Expand Up @@ -610,23 +609,21 @@
<action selector="offButtonTapped:" destination="UgW-Ob-0KX" eventType="touchUpInside" id="JJt-5D-6pb"/>
</connections>
</button>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" on="YES" translatesAutoresizingMaskIntoConstraints="NO" id="kmC-Kl-vj2">
<rect key="frame" x="312" y="255" width="49" height="31"/>
<switch opaque="NO" contentMode="scaleToFill" horizontalHuggingPriority="750" verticalHuggingPriority="750" contentHorizontalAlignment="center" contentVerticalAlignment="center" translatesAutoresizingMaskIntoConstraints="NO" id="kmC-Kl-vj2">
<rect key="frame" x="310" y="255" width="51" height="31"/>
<connections>
<action selector="encryptionKey:" destination="UgW-Ob-0KX" eventType="valueChanged" id="WaE-yH-elT"/>
</connections>
</switch>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="751" text="Use Correct Encryption Keys" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="HeR-1a-hjt">
<rect key="frame" x="16" y="260" width="220" height="21"/>
<label opaque="NO" userInteractionEnabled="NO" contentMode="left" horizontalHuggingPriority="251" verticalHuggingPriority="251" horizontalCompressionResistancePriority="751" text="Use Incorrect Encryption Keys" textAlignment="natural" lineBreakMode="tailTruncation" baselineAdjustment="alignBaselines" adjustsFontSizeToFit="NO" translatesAutoresizingMaskIntoConstraints="NO" id="HeR-1a-hjt">
<rect key="frame" x="16" y="260" width="231" height="21"/>
<fontDescription key="fontDescription" type="system" pointSize="17"/>
<color key="textColor" white="1" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<nil key="highlightedColor"/>
</label>
</subviews>
<color key="backgroundColor" white="0.0" alpha="1" colorSpace="custom" customColorSpace="genericGamma22GrayColorSpace"/>
<constraints>
<constraint firstItem="kG5-yT-GzM" firstAttribute="trailing" secondItem="kmC-Kl-vj2" secondAttribute="trailing" constant="16" id="0xy-jd-RXB"/>
<constraint firstItem="kmC-Kl-vj2" firstAttribute="leading" secondItem="HeR-1a-hjt" secondAttribute="trailing" constant="76" id="1IU-FQ-Yaf"/>
<constraint firstItem="cwX-vi-vgG" firstAttribute="width" secondItem="1XM-cR-Y6o" secondAttribute="width" id="582-Pj-QoZ"/>
<constraint firstItem="XIj-Oo-qmz" firstAttribute="leading" secondItem="kG5-yT-GzM" secondAttribute="leading" constant="16" id="6o4-kh-TOw"/>
<constraint firstItem="HeR-1a-hjt" firstAttribute="leading" secondItem="kG5-yT-GzM" secondAttribute="leading" constant="16" id="9XA-y5-9Om"/>
Expand All @@ -636,6 +633,7 @@
<constraint firstItem="XIj-Oo-qmz" firstAttribute="leading" secondItem="c45-FF-oLb" secondAttribute="leadingMargin" id="KLq-hK-kli"/>
<constraint firstItem="ERS-fv-Yhs" firstAttribute="leading" secondItem="kG5-yT-GzM" secondAttribute="leading" constant="16" id="LVO-tn-vpG"/>
<constraint firstItem="eg3-7c-7WS" firstAttribute="leading" secondItem="kG5-yT-GzM" secondAttribute="leading" constant="77" id="LyU-T1-mwz"/>
<constraint firstItem="kG5-yT-GzM" firstAttribute="trailing" secondItem="kmC-Kl-vj2" secondAttribute="trailing" constant="16" id="O1w-nm-0Or"/>
<constraint firstItem="1XM-cR-Y6o" firstAttribute="top" secondItem="kmC-Kl-vj2" secondAttribute="bottom" constant="23" id="OOf-aG-laS"/>
<constraint firstItem="1XM-cR-Y6o" firstAttribute="centerY" secondItem="cwX-vi-vgG" secondAttribute="centerY" id="PIV-cB-jxE"/>
<constraint firstItem="ERS-fv-Yhs" firstAttribute="top" secondItem="jhI-Pt-QaG" secondAttribute="bottom" constant="30" id="SfK-C8-ojg"/>
Expand Down
28 changes: 17 additions & 11 deletions src/darwin/Framework/CHIP/CHIPDeviceController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,12 @@
static const char * const CHIP_WORK_QUEUE = "com.zigbee.chip.work";
static const char * const CHIP_SELECT_QUEUE = "com.zigbee.chip.select";

// NOTE: Remote device ID is in sync with the echo server device id
// At some point, we may want to add an option to connect to a device without
// knowing its id, because the ID can be learned on the first response that is received.
constexpr chip::NodeId kLocalDeviceId = 112233;
constexpr chip::NodeId kRemoteDeviceId = 12344321;

@implementation AddressInfo
- (instancetype)initWithIP:(NSString *)ip
{
Expand Down Expand Up @@ -89,7 +95,7 @@ - (instancetype)init
return nil;
}

if (CHIP_NO_ERROR != _cppController->Init()) {
if (CHIP_NO_ERROR != _cppController->Init(kLocalDeviceId)) {
CHIP_LOG_ERROR("Error: couldn't initialize c++ controller");
delete _cppController;
_cppController = NULL;
Expand Down Expand Up @@ -161,7 +167,7 @@ - (BOOL)connect:(NSString *)ipAddress
peer_key:(NSData *)peer_key
error:(NSError * __autoreleasing *)error
{
__block CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;

// TODO maybe refactor
// the work queue is being used for atomic access to chip's cpp controller
Expand Down Expand Up @@ -204,13 +210,13 @@ - (BOOL)connect:(NSString *)ipAddress

- (AddressInfo *)getAddressInfo
{
__block CHIP_ERROR err = CHIP_NO_ERROR;
__block chip::IPAddress ipAddr;
__block uint16_t port;

CHIP_ERROR err = CHIP_NO_ERROR;
chip::Transport::PeerAddress peerAddr = chip::Transport::PeerAddress::Uninitialized();
[self.lock lock];
err = self.cppController->GetDeviceAddress(&ipAddr, &port);
err = self.cppController->PopulatePeerAddress(peerAddr);
[self.lock unlock];
chip::IPAddress ipAddr = peerAddr.GetIPAddress();
uint16_t port = peerAddr.GetPort();

if (err != CHIP_NO_ERROR) {
return nil;
Expand All @@ -227,7 +233,7 @@ - (AddressInfo *)getAddressInfo

- (BOOL)sendMessage:(NSData *)message error:(NSError * __autoreleasing *)error
{
__block CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;

[self.lock lock];
size_t messageLen = [message length];
Expand All @@ -252,7 +258,7 @@ - (BOOL)sendMessage:(NSData *)message error:(NSError * __autoreleasing *)error

- (BOOL)sendCHIPCommand:(ChipZclClusterId_t)cluster command:(ChipZclCommandId_t)command
{
__block CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;
[self.lock lock];
// FIXME: This needs a better buffersizing setup!
static const size_t bufferSize = 1024;
Expand Down Expand Up @@ -288,7 +294,7 @@ - (BOOL)sendCHIPCommand:(ChipZclClusterId_t)cluster command:(ChipZclCommandId_t)

- (BOOL)disconnect:(NSError * __autoreleasing *)error
{
__block CHIP_ERROR err = CHIP_NO_ERROR;
CHIP_ERROR err = CHIP_NO_ERROR;

[self.lock lock];

Expand All @@ -307,7 +313,7 @@ - (BOOL)disconnect:(NSError * __autoreleasing *)error

- (BOOL)isConnected
{
__block bool isConnected = false;
bool isConnected = false;

[self.lock lock];
isConnected = self.cppController->IsConnected();
Expand Down
Loading

0 comments on commit f522673

Please sign in to comment.