Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to the echo server to process on/off cluster messages #892

Merged
merged 1 commit into from
May 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ third_party/mbedtls/

# Example specific rules
examples/**/sdkconfig
examples/**/sdkconfig.old

# Temporary Directories
.tmp/
Expand Down
79 changes: 79 additions & 0 deletions examples/wifi-echo/server/esp32/main/DataModelHandler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
*
* 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 implements the handler for data model messages.
*/

#include "esp_log.h"
#include <system/SystemPacketBuffer.h>

#include "DataModelHandler.h"
#include "LEDWidget.h"

extern "C" {
#include "chip-zcl/chip-zcl.h"
#include "gen/gen-cluster-id.h"
#include "gen/gen-types.h"
}

using namespace ::chip;

extern LEDWidget statusLED; // In wifi-echo.cpp

static const char * TAG = "data_model_server";

void InitDataModelHandler()
{
chipZclEndpointInit();
}

void HandleDataModelMessage(System::PacketBuffer * buffer)
{
ChipZclStatus_t zclStatus = chipZclProcessIncoming(buffer->Start(), buffer->DataLength());
if (zclStatus == CHIP_ZCL_STATUS_SUCCESS)
{
ESP_LOGI(TAG, "Data model processing success!");
}
else
{
ESP_LOGI(TAG, "Data model processing failure: %d", zclStatus);
}
System::PacketBuffer::Free(buffer);
}

extern "C" {
void chipZclPostAttributeChangeCallback(uint8_t endpoint, ChipZclClusterId clusterId, ChipZclAttributeId attributeId, uint8_t mask,
uint16_t manufacturerCode, uint8_t type, uint8_t size, uint8_t * value)
{
if (clusterId != CHIP_ZCL_CLUSTER_ON_OFF)
{
ESP_LOGI(TAG, "Unknown cluster ID: %d", clusterId);
return;
}

if (attributeId != CHIP_ZCL_CLUSTER_ON_OFF_SERVER_ATTRIBUTE_ON_OFF)
{
ESP_LOGI(TAG, "Unknown attribute ID: %d", attributeId);
return;
}

// At this point we can assume that value points to a boolean value.
statusLED.Set(*value);
}
} // extern "C"
9 changes: 9 additions & 0 deletions examples/wifi-echo/server/esp32/main/EchoServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
#include <support/ErrorStr.h>
#include <system/SystemPacketBuffer.h>

#include "DataModelHandler.h"

#define PORT CONFIG_ECHO_PORT

static const char * TAG = "echo_server";
Expand All @@ -63,6 +65,13 @@ static void echo(IPEndPointBasis * endpoint, System::PacketBuffer * buffer, cons
ESP_LOGI(TAG, "UDP packet received from %s:%u to %s:%u (%zu bytes)", src_addr, packet_info->SrcPort, dest_addr,
packet_info->DestPort, static_cast<size_t>(data_len));

if (data_len > 0 && buffer->Start()[0] < 0x20)
{
// Non-ACII; assume it's a data model message.
HandleDataModelMessage(buffer);
return;
}

// attempt to print the incoming message
char msg_buffer[data_len + 1];
msg_buffer[data_len] = 0; // Null-terminate whatever we received and treat like a string...
Expand Down
2 changes: 2 additions & 0 deletions examples/wifi-echo/server/esp32/main/component.mk
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,5 @@
# (Uses default behaviour of compiling all source files in directory, adding 'include' to include path.)

COMPONENT_DEPENDS := chip

COMPONENT_INCLUDES += $(PROJECT_PATH)/third_party/connectedhomeip/src/app
47 changes: 47 additions & 0 deletions examples/wifi-echo/server/esp32/main/include/DataModelHandler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*
*
* 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 defines the API for the handler for data model messages.
*/

#ifndef DATA_MODEL_HANDLER_H
#define DATA_MODEL_HANDLER_H

namespace chip {
namespace System {
class PacketBuffer;
} // namespace System
} // namespace chip

/**
* Initialize the data model handler. This must be called once, and before any
* HandleDataModelMessage calls happen.
*/
void InitDataModelHandler();

/**
* Handle a message that should be processed via our data model processing
* codepath.
*
* @param [in] buffer The buffer holding the message. This function guarantees
* that it will free the buffer before returning.
*/
void HandleDataModelMessage(chip::System::PacketBuffer * buffer);

#endif // DATA_MODEL_HANDLER_H
7 changes: 3 additions & 4 deletions examples/wifi-echo/server/esp32/main/wifi-echo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* limitations under the License.
*/

#include "DataModelHandler.h"
#include "LEDWidget.h"
#include "esp_event_loop.h"
#include "esp_heap_caps_init.h"
Expand Down Expand Up @@ -58,7 +59,7 @@ extern void startClient(void);

#endif // !CONFIG_DEVICE_TYPE_ESP32_DEVKITC

static LEDWidget statusLED;
LEDWidget statusLED;

const char * TAG = "wifi-echo-demo";

Expand Down Expand Up @@ -151,6 +152,7 @@ extern "C" void app_main()
statusLED.Init(STATUS_LED_GPIO_NUM);

// Start the Echo Server
InitDataModelHandler();
UDPEndPoint * sEndpoint = NULL;
startServer(sEndpoint);
#if CONFIG_USE_ECHO_CLIENT
Expand All @@ -160,9 +162,6 @@ extern "C" void app_main()
// Run the UI Loop
while (true)
{
statusLED.Blink(500);
statusLED.Animate();

vTaskDelay(50 / portTICK_PERIOD_MS);
}
}
Expand Down
4 changes: 1 addition & 3 deletions src/app/plugin/core-api/core-api.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,5 @@ ChipZclStatus_t chipZclProcessIncoming(uint8_t * rawBuffer, uint16_t rawBufferLe
ChipZclCommandContext_t context = { 0 };

chipZclDecodeZclHeader(&buffer, &context);
chipZclClusterCommandParse(&context);

return CHIP_ZCL_STATUS_SUCCESS;
return chipZclClusterCommandParse(&context);
}