Skip to content

Commit

Permalink
Fix memory issue
Browse files Browse the repository at this point in the history
  • Loading branch information
erjiaqing committed Sep 30, 2020
1 parent 6e4b143 commit 1549798
Show file tree
Hide file tree
Showing 20 changed files with 217 additions and 46 deletions.
3 changes: 2 additions & 1 deletion examples/lighting-app/nrf5/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ nrf5_sdk("sdk") {
"MBEDTLS_X509_CREATE_C",
"MBEDTLS_X509_CSR_WRITE_C",
"OPENTHREAD_CONFIG_CLI_TRANSPORT=OT_CLI_TRANSPORT_CONSOLE",
"OPENTHREAD_CONFIG_ENABLE_BUILTIN_MBEDTLS_MANAGEMENT=1",
]
}

Expand Down Expand Up @@ -88,7 +89,7 @@ nrf5_executable("lighting_app") {
]

public_deps = [
"${chip_root}/examples/shell:chip_shell_collection",
"${chip_root}/examples/shell/shell_common:chip_shell_collection",
]

deps = [
Expand Down
22 changes: 21 additions & 1 deletion examples/lighting-app/nrf5/main/AppTask.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,16 @@
#include <setup_payload/QRCodeSetupPayloadGenerator.h>
#include <setup_payload/SetupPayload.h>

#include "attribute-storage.h"
#include "gen/cluster-id.h"

APP_TIMER_DEF(sFunctionTimer);

namespace {

constexpr int kFactoryResetTriggerTimeout = 3000;
constexpr int kFactoryResetCancelWindowTimeout = 3000;
constexpr size_t kAppTaskStackSize = 1536;
constexpr size_t kAppTaskStackSize = 4096;
constexpr int kAppTaskPriority = 2;
constexpr int kAppEventQueueSize = 10;
constexpr int kExampleVenderID = 0xabcd;
Expand Down Expand Up @@ -224,6 +227,8 @@ void AppTask::AppTaskMain(void * pvParameter)

SetDeviceName("LightingDemo._chip._udp.local.");

GetAppTask().UpdateClusterState();

while (true)
{
BaseType_t eventReceived = xQueueReceive(sAppEventQueue, &event, pdMS_TO_TICKS(10));
Expand Down Expand Up @@ -287,6 +292,7 @@ void AppTask::AppTaskMain(void * pvParameter)
sStatusLED.Animate();
sUnusedLED.Animate();
sUnusedLED_1.Animate();
GetAppTask().UpdateClusterState();

uint64_t nowUS = chip::System::Platform::Layer::GetClock_Monotonic();
uint64_t nextChangeTimeUS = mLastChangeTimeUS + 5 * 1000 * 1000UL;
Expand Down Expand Up @@ -527,6 +533,7 @@ void AppTask::PostEvent(const AppEvent * aEvent)

void AppTask::DispatchEvent(AppEvent * aEvent)
{

if (aEvent->Handler)
{
aEvent->Handler(aEvent);
Expand All @@ -536,3 +543,16 @@ void AppTask::DispatchEvent(AppEvent * aEvent)
NRF_LOG_INFO("Event received with no handler. Dropping event.");
}
}

void AppTask::UpdateClusterState(void)
{
uint8_t newValue = LightingMgr().IsTurnedOn();

// write the new on/off value
EmberAfStatus status = emberAfWriteAttribute(1, ZCL_ON_OFF_CLUSTER_ID, ZCL_ON_OFF_ATTRIBUTE_ID, CLUSTER_MASK_SERVER,
(uint8_t *) &newValue, ZCL_BOOLEAN_ATTRIBUTE_TYPE);
if (status != EMBER_ZCL_STATUS_SUCCESS)
{
NRF_LOG_INFO("ERR: updating on/off %x", status);
}
}
126 changes: 123 additions & 3 deletions examples/lighting-app/nrf5/main/LightingCLI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,135 @@
#include <lib/support/CHIPArgParser.hpp>
#include <lib/support/CodeUtils.h>
#include <lib/support/RandUtils.h>
#include <platform/CHIPDeviceLayer.h>
#include <support/logging/CHIPLogging.h>

#include <ChipShellCollection.h>

#include <AppEvent.h>
#include <AppTask.h>
#include <LightingManager.h>

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

namespace {

const size_t kShellTaskStackSize = 1536;
const int kShellTaskPriority = 2;
enum CLIEvents
{
kCLIEvent_LightOn = 0,
kCLIEvent_LightOff,
kCLIEvent_LightToggle,

kCLIEvent_FactoryReset,
};

void CLIAppEventHandler(AppEvent * event)
{
switch (event->CLIEvent.Event)
{
case kCLIEvent_LightOn:
LightingMgr().InitiateAction(LightingManager::ON_ACTION);
break;
case kCLIEvent_LightOff:
LightingMgr().InitiateAction(LightingManager::OFF_ACTION);
break;
case kCLIEvent_LightToggle: {
LightingManager::Action_t action;
if (LightingMgr().IsTurnedOn())
{
action = LightingManager::OFF_ACTION;
}
else
{
action = LightingManager::ON_ACTION;
}
LightingMgr().InitiateAction(action);
break;
}
case kCLIEvent_FactoryReset:
ConfigurationMgr().InitiateFactoryReset();
return;
default:
ChipLogError(Shell, "Unknown event received: %d", event->CLIEvent.Event);
return;
}
}

int cmd_light(int argc, char ** argv)
{
int end_point = 1;
AppEvent event;
event.Type = AppEvent::kEventType_CLI;
event.Handler = CLIAppEventHandler;

VerifyOrExit(argc > 0 && argc <= 2, streamer_printf(streamer_get(), "light on/off/toggle [endpoint=1]\n\r"));

if (strcmp(argv[0], "on") == 0)
{
event.CLIEvent.Event = kCLIEvent_LightOn;
}
else if (strcmp(argv[0], "off") == 0)
{
event.CLIEvent.Event = kCLIEvent_LightOff;
}
else if (strcmp(argv[0], "toggle") == 0)
{
event.CLIEvent.Event = kCLIEvent_LightToggle;
}

if (argc == 2)
{
sscanf(argv[1], "%d", &end_point);
}

GetAppTask().PostEvent(&event);
exit:
streamer_printf(streamer_get(), "\n\r");
return 0;
}

int cmd_app(int argc, char ** argv)
{
int end_point = 1;
AppEvent event;
event.Type = AppEvent::kEventType_CLI;
event.Handler = CLIAppEventHandler;

VerifyOrExit(argc > 0, streamer_printf(streamer_get(), "app subcommand|help\n\r"));

if (strcmp(argv[0], "factoryrst") == 0)
{
event.CLIEvent.Event = kCLIEvent_FactoryReset;
}
else
{
streamer_printf(streamer_get(), "app factoryrst : Do factory reset\n\r");
}

GetAppTask().PostEvent(&event);
exit:
streamer_printf(streamer_get(), "\n\r");
return 0;
}

shell_command_t cmd_lightingcli[] = {
{ &cmd_light, "light", "Lighting control" },
{ &cmd_app, "app", "App utilities" },
};

void cmd_app_init(void)
{
shell_register(cmd_lightingcli, ArraySize(cmd_lightingcli));
}

} // namespace

namespace {

const size_t kShellTaskStackSize = 2048;
const int kShellTaskPriority = 1;
TaskHandle_t sShellTaskHandle;

void LightingCLIMain(void * pvParameter)
Expand All @@ -57,10 +175,12 @@ void LightingCLIMain(void * pvParameter)
ChipLogDetail(Shell, "Initializing CHIP shell", rc);

cmd_misc_init();
cmd_base64_init();
cmd_app_init();
cmd_btp_init();
cmd_otcli_init();

ChipLogDetail(Shell, "Run CHIP shell Task", rc);

shell_task(NULL);
}

Expand Down
5 changes: 5 additions & 0 deletions examples/lighting-app/nrf5/main/include/AppEvent.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ struct AppEvent
kEventType_Timer,
kEventType_Lighting,
kEventType_Install,
kEventType_CLI,
};

uint16_t Type;
Expand All @@ -49,6 +50,10 @@ struct AppEvent
{
uint8_t Action;
} LightingEvent;
struct
{
uint8_t Event;
} CLIEvent;
};

EventHandler Handler;
Expand Down
1 change: 1 addition & 0 deletions examples/lighting-app/nrf5/main/include/AppTask.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class AppTask
void CancelTimer(void);

void DispatchEvent(AppEvent * event);
void UpdateClusterState(void);

static void FunctionTimerEventHandler(AppEvent * aEvent);
static void FunctionHandler(AppEvent * aEvent);
Expand Down
9 changes: 4 additions & 5 deletions examples/platform/nrf528xx/app/chipinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,13 +87,12 @@ ret_code_t ChipInit()
#if CHIP_ENABLE_OPENTHREAD
NRF_LOG_INFO("Initializing OpenThread stack");

mbedtls_platform_set_calloc_free(ot_calloc, ot_free);
nrf_cc310_platform_abort_init();
nrf_cc310_platform_mutex_init();
mbedtls_platform_setup(NULL);
otHeapSetCAllocFree(ot_calloc, ot_free);
// mbedtls_platform_set_calloc_free(ot_calloc, ot_free);

// otHeapSetCAllocFree(ot_calloc, ot_free);

otSysInit(0, NULL);
NRF_LOG_INFO("Initializing OpenThread stack");

// Configure multiprotocol to work with BLE.
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@
// Use smaller maximum interval to speed up reattaching.
#define OPENTHREAD_CONFIG_MLE_ATTACH_BACKOFF_MAXIMUM_INTERVAL (60 * 10 * 1000) // default 1200000 ms

#define OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE 1
#define OPENTHREAD_CONFIG_HEAP_EXTERNAL_ENABLE 0
#define OPENTHREAD_CONFIG_JOINER_ENABLE 1
#define OPENTHREAD_CONFIG_NCP_UART_ENABLE 1
#define OPENTHREAD_CONFIG_IP6_SLAAC_ENABLE 1
Expand Down
35 changes: 1 addition & 34 deletions examples/shell/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,6 @@ import("//build_overrides/chip.gni")
import("${chip_root}/src/platform/device.gni")
import("//build_overrides/openthread.gni")

config("chip_shell_collection_config") {
include_dirs = [
".",
"./include",
"${chip_root}/src/lib/shell",
]

defines = [
"ENABLE_CHIP_SHELL",
"OPENTHREAD_CONFIG_CLI_TRANSPORT=OT_CLI_TRANSPORT_CONSOLE",
]
}

static_library("chip_shell_collection") {
sources = [
"cmd_base64.cpp",
"cmd_btp.cpp",
"cmd_device.cpp",
"cmd_misc.cpp",
"cmd_otcli.cpp",
]

public_deps = [
"${chip_root}/src/lib",
"${chip_root}/src/lib/shell",
"${chip_root}/src/setup_payload",
"${openthread_root}:libopenthread-cli-ftd",
"${openthread_root}:libopenthread-ftd",
]

public_configs = [ ":chip_shell_collection_config" ]
}

if (chip_device_platform == "nrf5") {

} else {
Expand All @@ -62,7 +29,7 @@ if (chip_device_platform == "nrf5") {
]

public_deps = [
":chip_shell_collection",
"shell_common:chip_shell_collection",
"${chip_root}/src/lib/shell",
"${chip_root}/src/platform",
]
Expand Down
50 changes: 50 additions & 0 deletions examples/shell/shell_common/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# 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.

import("//build_overrides/chip.gni")
import("${chip_root}/src/platform/device.gni")
import("//build_overrides/openthread.gni")

config("chip_shell_collection_config") {
include_dirs = [
".",
"./include",
"${chip_root}/src/lib/shell",
]

defines = [
"ENABLE_CHIP_SHELL",
"OPENTHREAD_CONFIG_CLI_TRANSPORT=OT_CLI_TRANSPORT_CONSOLE",
]
}

static_library("chip_shell_collection") {
sources = [
"cmd_base64.cpp",
"cmd_btp.cpp",
"cmd_device.cpp",
"cmd_misc.cpp",
"cmd_otcli.cpp",
]

public_deps = [
"${chip_root}/src/lib",
"${chip_root}/src/lib/shell",
"${chip_root}/src/setup_payload",
"${openthread_root}:libopenthread-cli-ftd",
"${openthread_root}:libopenthread-ftd",
]

public_configs = [ ":chip_shell_collection_config" ]
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
Loading

0 comments on commit 1549798

Please sign in to comment.