-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Separate Rendezvous message handling from BLE session (#2418)
* 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
1 parent
2ab68ff
commit f59dcbc
Showing
3 changed files
with
102 additions
and
27 deletions.
There are no files selected for viewing
61 changes: 61 additions & 0 deletions
61
examples/wifi-echo/server/esp32/main/RendezvousMessageHandler.cpp
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,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; | ||
} |
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
36 changes: 36 additions & 0 deletions
36
examples/wifi-echo/server/esp32/main/include/RendezvousMessageHandler.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
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 |