Skip to content

Commit

Permalink
Separate Rendezvous message handling from BLE session (#2418)
Browse files Browse the repository at this point in the history
* Separate Rendezvous message handling from BLE session

With this change, BLE handling remains in RendezvousSession and
message processing independent of BLE moves to RendervousMessageHandler.

* Restyled by clang-format

* Use fixed-length message buffer.

Co-authored-by: Restyled.io <[email protected]>
  • Loading branch information
kpschoedel and restyled-commits authored Sep 8, 2020
1 parent 2ab68ff commit f59dcbc
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 27 deletions.
61 changes: 61 additions & 0 deletions examples/wifi-echo/server/esp32/main/RendezvousMessageHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
*
* 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.
*/

#include "RendezvousMessageHandler.h"
#include <platform/ESP32/ESP32Utils.h>
#include <support/CodeUtils.h>
#include <support/logging/CHIPLogging.h>
#include <system/SystemPacketBuffer.h>

using namespace ::chip;

extern CHIP_ERROR SetWiFiStationProvisioning(char * ssid, char * key);

CHIP_ERROR RendezvousMessageHandler::HandleMessageReceived(System::PacketBuffer * buffer)
{
CHIP_ERROR err = CHIP_NO_ERROR;
bool isWiFiCredentials = false;
char * key = nullptr;
char * ssid = nullptr;

// Pending definition of an actual message format, WiFi credentials have the form
// ‘::SSID:password:’, where ‘:’ can be any single ASCII character.
constexpr size_t maxBufferLen =
4 + chip::DeviceLayer::Internal::kMaxWiFiSSIDLength + chip::DeviceLayer::Internal::kMaxWiFiKeyLength;
char msg[maxBufferLen];

const size_t bufferLen = buffer->DataLength();
VerifyOrExit(bufferLen < sizeof msg, err = CHIP_ERROR_INVALID_MESSAGE_TYPE);
memcpy(msg, buffer->Start(), bufferLen);
msg[bufferLen] = 0;
ChipLogProgress(NetworkProvisioning, "RendezvousMessageHandler: Receive message: %s", msg);

isWiFiCredentials = ((bufferLen > 3) && (msg[0] == msg[1]) && (msg[0] == msg[bufferLen - 1]));
VerifyOrExit(isWiFiCredentials, err = CHIP_ERROR_INVALID_MESSAGE_TYPE);

msg[1] = 0;
ssid = strtok(&msg[2], msg);
key = strtok(NULL, msg);
VerifyOrExit(ssid && key, err = CHIP_ERROR_INVALID_MESSAGE_TYPE);

ChipLogProgress(NetworkProvisioning, "RendezvousMessageHandler: SSID: %s, key: %s", ssid, key);
err = SetWiFiStationProvisioning(ssid, key);
SuccessOrExit(err);

exit:
return err;
}
32 changes: 5 additions & 27 deletions examples/wifi-echo/server/esp32/main/RendezvousSession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,14 @@
*/

#include "RendezvousSession.h"
#include "RendezvousMessageHandler.h"
#include <support/CodeUtils.h>
#include <support/ErrorStr.h>
#include <support/logging/CHIPLogging.h>
#include <system/SystemPacketBuffer.h>

using namespace ::chip;

extern CHIP_ERROR SetWiFiStationProvisioning(char * ssid, char * key);

BluetoothWidget * RendezvousSession::mVirtualLed;

Ble::BLEEndPoint * RendezvousSession::mEndPoint = nullptr;
Expand Down Expand Up @@ -131,32 +130,11 @@ void RendezvousSession::HandleMessageReceived(Ble::BLEEndPoint * endPoint, Packe
}
else
{
const size_t bufferLen = buffer->DataLength();
char msg[bufferLen];
msg[bufferLen] = 0;
memcpy(msg, buffer->Start(), bufferLen);

ChipLogProgress(Ble, "RendezvousSession: Receive message: %s", msg);

if ((bufferLen > 3) && (msg[0] == msg[1]) && (msg[0] == msg[bufferLen - 1]))
{
// WiFi credentials, of the form ‘::SSID:password:’, where ‘:’ can be any single ASCII character.
msg[1] = 0;
char * ssid = strtok(&msg[2], msg);
char * key = strtok(NULL, msg);
if (ssid && key)
{
ChipLogProgress(Ble, "RendezvousSession: SSID: %s, key: %s", ssid, key);
SetWiFiStationProvisioning(ssid, key);
}
else
{
ChipLogError(Ble, "RendezvousSession: SSID: %p, key: %p", ssid, key);
}
}
else
// When paired, offer the RendezvousMessageHandler a chance to process the message.
CHIP_ERROR err = RendezvousMessageHandler::HandleMessageReceived(buffer);
if (err == CHIP_ERROR_INVALID_MESSAGE_TYPE)
{
// Echo.
// If the handler did not recognize the message, treat it as an echo request.
mEndPoint->Send(buffer);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
*
* 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.
*/

#ifndef _RENDEZVOUS_MESSAGE_HANDLER_H
#define _RENDEZVOUS_MESSAGE_HANDLER_H

#include <core/CHIPError.h>
#include <platform/CHIPDeviceLayer.h>

using namespace ::chip;

class RendezvousMessageHandler
{
public:
// Handle a rendezvous message. Returns:
// - CHIP_NO_ERROR if the message was handled successfully.
// - CHIP_ERROR_INVALID_MESSAGE_TYPE if the message was not recognized.
// - Some other error encountered processing a specific message type.
static CHIP_ERROR HandleMessageReceived(System::PacketBuffer * buffer);
};

#endif // _RENDEZVOUS_MESSAGE_HANDLER_H

0 comments on commit f59dcbc

Please sign in to comment.