Skip to content

Commit

Permalink
Split generated files out
Browse files Browse the repository at this point in the history
Updates userinput backend to run commands on matter thread
  • Loading branch information
achaulk-goog committed Aug 31, 2022
1 parent fb41aa2 commit 104841d
Show file tree
Hide file tree
Showing 26 changed files with 760 additions and 587 deletions.
2 changes: 1 addition & 1 deletion examples/bridge-app/linux/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ action("chip-bridge-codegen") {

args = [
"--generator",
"cpp",
"bridge",
"--output-dir",
rebase_path(target_gen_dir, root_build_dir),
rebase_path(
Expand Down
6 changes: 5 additions & 1 deletion examples/bridge-app/linux/Device.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ Device::Device(chip::Span<chip::DataVersion> dataVersions, chip::Span<EmberAfClu
chip::EndpointId parentId) :
mParentEndpointId(parentId),
mDataVersions(dataVersions), mClusters(clusters), mClusterImpl(clusterImpl), mDeviceTypeList(deviceTypeList)
{}
{
mEndpointType.cluster = clusters.data();
mEndpointType.clusterCount = (uint8_t) clusters.size();
mEndpointType.endpointSize = 0;
}

void Device::SetEndpointId(chip::EndpointId id)
{
Expand Down
45 changes: 44 additions & 1 deletion examples/bridge-app/linux/UserInputBackend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@
#include "DynamicDevice.h"
#include "main.h"

#include <platform/PlatformManager.h>

#include <algorithm>
#include <charconv>
#include <future>
#include <iostream>
#include <iterator>
#include <sstream>
Expand Down Expand Up @@ -343,6 +346,28 @@ void ParseValue(std::vector<uint8_t> * data, uint16_t size, const std::string &
case ZCL_DOUBLE_ATTRIBUTE_TYPE:
wr.Put(chip::TLV::Tag(), atof(str.c_str()));
break;
case ZCL_INT8S_ATTRIBUTE_TYPE:
case ZCL_INT16S_ATTRIBUTE_TYPE:
case ZCL_INT24S_ATTRIBUTE_TYPE:
case ZCL_INT32S_ATTRIBUTE_TYPE:
case ZCL_INT40S_ATTRIBUTE_TYPE:
case ZCL_INT48S_ATTRIBUTE_TYPE:
case ZCL_INT56S_ATTRIBUTE_TYPE:
case ZCL_INT64S_ATTRIBUTE_TYPE:
wr.Put(chip::TLV::Tag(), (int64_t) strtoll(str.c_str(), nullptr, 10));
break;

case ZCL_INT8U_ATTRIBUTE_TYPE:
case ZCL_INT16U_ATTRIBUTE_TYPE:
case ZCL_INT24U_ATTRIBUTE_TYPE:
case ZCL_INT32U_ATTRIBUTE_TYPE:
case ZCL_INT40U_ATTRIBUTE_TYPE:
case ZCL_INT48U_ATTRIBUTE_TYPE:
case ZCL_INT56U_ATTRIBUTE_TYPE:
case ZCL_INT64U_ATTRIBUTE_TYPE:
wr.Put(chip::TLV::Tag(), (uint64_t) strtoll(str.c_str(), nullptr, 10));
break;

default:
// Assume integer
wr.Put(chip::TLV::Tag(), (int64_t) strtoll(str.c_str(), nullptr, 10));
Expand Down Expand Up @@ -401,6 +426,7 @@ void SetValue(const std::vector<std::string> & tokens)

chip::TLV::TLVReader rd;
rd.Init(data.data(), data.size());
rd.Next();

if (!cluster->Push(attr->attributeId, rd))
{
Expand Down Expand Up @@ -547,14 +573,31 @@ void Help(const std::vector<std::string> & tokens)
}
}

struct Op
{
std::vector<std::string> * tokens;
const Command * command;
std::promise<void> lock;
};
void ProcessLineOnMatterThread(intptr_t arg)
{
Op * op = reinterpret_cast<Op *>(arg);
op->command->fn(*op->tokens);
op->lock.set_value();
}

void ProcessLine(std::vector<std::string> & tokens)
{
for (auto & cmd : commands)
{
if (tokens[0] == cmd.name)
{
tokens.erase(tokens.begin());
cmd.fn(tokens);

Op op{ &tokens, &cmd };
chip::DeviceLayer::PlatformMgr().ScheduleWork(&ProcessLineOnMatterThread, reinterpret_cast<intptr_t>(&op));
// Wait for command completion
op.lock.get_future().wait();
return;
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/bridge-app/linux/include/Clusters.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ struct CommonAttributeAccessInterface : public chip::app::AttributeAccessInterfa
static CommonCluster * FindCluster(const chip::app::ConcreteClusterPath & path);
};

#include "cpp/BridgeClustersImpl.h"
#include "bridge/BridgeClustersImpl.h"

namespace clusters {
struct BridgedDeviceBasicCluster : public CommonCluster
Expand Down
1 change: 0 additions & 1 deletion examples/bridge-app/linux/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,6 @@ int AddDeviceEndpoint(Device * dev)
while (1)
{
// Todo: Update this to schedule the work rather than use this lock
DeviceLayer::StackLock lock;
dev->SetEndpointId(gCurrentEndpointId);
ret =
emberAfSetDynamicEndpoint(index, gCurrentEndpointId, dev->endpointType(), dev->versions(), dev->deviceTypes());
Expand Down
10 changes: 5 additions & 5 deletions scripts/codegen.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from idl.generators import FileSystemGeneratorStorage, GeneratorStorage
from idl.generators.java import JavaGenerator
from idl.generators.cpp import CppGenerator
from idl.generators.bridge import BridgeGenerator


class CodeGeneratorTypes(enum.Enum):
Expand All @@ -38,13 +38,13 @@ class CodeGeneratorTypes(enum.Enum):
into underlying generators.
"""
JAVA = enum.auto()
CPP = enum.auto()
BRIDGE = enum.auto()

def CreateGenerator(self, *args, **kargs):
if self == CodeGeneratorTypes.JAVA:
return JavaGenerator(*args, **kargs)
elif self == CodeGeneratorTypes.CPP:
return CppGenerator(*args, **kargs)
elif self == CodeGeneratorTypes.BRIDGE:
return BridgeGenerator(*args, **kargs)
else:
raise Error("Unknown code generator type")

Expand Down Expand Up @@ -72,7 +72,7 @@ def write_new_data(self, relative_path: str, content: str):

__GENERATORS__ = {
'java': CodeGeneratorTypes.JAVA,
'cpp': CodeGeneratorTypes.CPP,
'bridge': CodeGeneratorTypes.BRIDGE,
}


Expand Down
20 changes: 18 additions & 2 deletions scripts/idl/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@ pw_python_package("idl") {
"matter_grammar.lark",

# Templates used for generation
"generators/cpp/BridgeClustersCpp.jinja",
"generators/bridge/BridgeClustersCpp.jinja",
"generators/bridge/BridgeClustersCommon.jinja",
"generators/bridge/BridgeClustersGlobalStructs.jinja",
"generators/java/ChipClustersCpp.jinja",
"generators/java/ChipClustersRead.jinja",

Expand All @@ -36,26 +38,40 @@ pw_python_package("idl") {
"tests/inputs/optional_argument.matter",
"tests/inputs/several_clusters.matter",
"tests/inputs/simple_attribute.matter",
"tests/outputs/cluster_struct_attribute/bridge/BridgeClustersImpl.h",
"tests/outputs/cluster_struct_attribute/bridge/BridgeGlobalStructs.h",
"tests/outputs/cluster_struct_attribute/bridge/DemoCluster.h",
"tests/outputs/cluster_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp",
"tests/outputs/cluster_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp",
"tests/outputs/global_struct_attribute/bridge/BridgeClustersImpl.h",
"tests/outputs/global_struct_attribute/bridge/BridgeGlobalStructs.h",
"tests/outputs/global_struct_attribute/bridge/DemoCluster.h",
"tests/outputs/global_struct_attribute/jni/DemoClusterClient-ReadImpl.cpp",
"tests/outputs/global_struct_attribute/jni/DemoClusterClient-InvokeSubscribeImpl.cpp",
"tests/outputs/optional_argument/jni/MyClusterClient-ReadImpl.cpp",
"tests/outputs/optional_argument/jni/MyClusterClient-InvokeSubscribeImpl.cpp",
"tests/outputs/several_clusters/bridge/BridgeClustersImpl.h",
"tests/outputs/several_clusters/bridge/BridgeGlobalStructs.h",
"tests/outputs/several_clusters/bridge/First.h",
"tests/outputs/several_clusters/bridge/Second.h",
"tests/outputs/several_clusters/bridge/Third.h",
"tests/outputs/several_clusters/jni/FirstClient-ReadImpl.cpp",
"tests/outputs/several_clusters/jni/SecondClient-ReadImpl.cpp",
"tests/outputs/several_clusters/jni/ThirdClient-ReadImpl.cpp",
"tests/outputs/several_clusters/jni/FirstClient-InvokeSubscribeImpl.cpp",
"tests/outputs/several_clusters/jni/SecondClient-InvokeSubscribeImpl.cpp",
"tests/outputs/several_clusters/jni/ThirdClient-InvokeSubscribeImpl.cpp",
"tests/outputs/simple_attribute/bridge/BridgeClustersImpl.h",
"tests/outputs/simple_attribute/bridge/BridgeGlobalStructs.h",
"tests/outputs/simple_attribute/bridge/MyCluster.h",
"tests/outputs/simple_attribute/jni/MyClusterClient-ReadImpl.cpp",
"tests/outputs/simple_attribute/jni/MyClusterClient-InvokeSubscribeImpl.cpp",
]

sources = [
"__init__.py",
"generators/__init__.py",
"generators/cpp/__init__.py",
"generators/bridge/__init__.py",
"generators/filters.py",
"generators/java/__init__.py",
"generators/types.py",
Expand Down
61 changes: 61 additions & 0 deletions scripts/idl/generators/bridge/BridgeClustersCommon.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <new>

{%- for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}
#include "bridge/{{cluster.name}}.h"
{%- endif %}
{%- endfor %}

namespace clusters {

struct ClusterInfo
{
chip::ClusterId id;
const char *name;
uint16_t size;
CommonCluster* (*ctor)(void*);
} static const kKnownClusters[] = {
{% for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}
{
{{cluster.code}},
"{{cluster.name}}",
sizeof({{cluster.name}}Cluster),
[](void *mem) -> CommonCluster* {
return new(mem) {{cluster.name}}Cluster();
},
},
{%- endif %}
{%- endfor %}
};

inline void BridgeRegisterAllAttributeOverrides()
{
{% for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}
static {{cluster.name}}Access {{cluster.name}};
registerAttributeAccessOverride(&{{cluster.name}});
{%- endif %}
{%- endfor %}
}

struct AttrInfo
{
chip::ClusterId cluster;
chip::AttributeId attr;
const char *name;
} static const kKnownAttributes[] = {
{% for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}

{%- for attr in cluster.attributes %}
{ {{cluster.code}}, {{attr.definition.code}}, "{{attr.definition.name | capitalcase}}" },
{%- endfor %}

{%- endif %}
{%- endfor %}
};

}
Original file line number Diff line number Diff line change
@@ -1,41 +1,8 @@
#include <app-common/zap-generated/cluster-id.h>
#include <app-common/zap-generated/attribute-id.h>
#pragma once

#include <new>
#include "BridgeGlobalStructs.h"

namespace clusters {
{% for struct in structs %}
struct {{struct.name}}
{
CHIP_ERROR Decode(chip::TLV::TLVReader & reader)
{
chip::app::Clusters::detail::Structs::{{struct.name}}::DecodableType t;
CHIP_ERROR err = t.Decode(reader);
if(err == CHIP_NO_ERROR) {
{%- for field in struct.fields %}
{{field.name}} = t.{{field.name}};
{%- endfor %}
}
return err;
}

CHIP_ERROR Encode(chip::TLV::TLVWriter & writer, chip::TLV::Tag tag) const
{
chip::app::Clusters::detail::Structs::{{struct.name}}::Type t;
{%- for field in struct.fields %}
t.{{field.name}} = {{field.name}};
{%- endfor %}
return t.Encode(writer, tag);
}

{%- for field in struct.fields %}
{{field | getField(None, idl)}} {{field.name}};
{%- endfor %}
};
{%- endfor %}

{%- for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}
struct {{cluster.name}}Cluster : public CommonCluster
{
{%- for struct in cluster.structs %}
Expand Down Expand Up @@ -158,7 +125,7 @@ struct {{cluster.name}}Access : public CommonAttributeAccessInterface
{%- if attr.is_writable %}
{%- if attr.definition.is_list %}
case {{attr.definition.code}}:
m{{attr.definition.name | capitalcase}}.ListWriteBegin(aPath);
c->m{{attr.definition.name | capitalcase}}.ListWriteBegin(aPath);
return;
{%- endif %}
{%- endif %}
Expand All @@ -177,64 +144,13 @@ struct {{cluster.name}}Access : public CommonAttributeAccessInterface
{%- if attr.is_writable %}
{%- if attr.definition.is_list %}
case {{attr.definition.code}}:
m{{attr.definition.name | capitalcase}}.ListWriteEnd(aPath, aWriteWasSuccessful);
c->m{{attr.definition.name | capitalcase}}.ListWriteEnd(aPath, aWriteWasSuccessful);
return;
{%- endif %}
{%- endif %}
{%- endfor %}
}
}
};
{%- endif %}
{%- endfor %}

struct ClusterInfo
{
chip::ClusterId id;
const char *name;
uint16_t size;
CommonCluster* (*ctor)(void*);
} static const kKnownClusters[] = {
{% for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}
{
{{cluster.code}},
"{{cluster.name}}",
sizeof({{cluster.name}}Cluster),
[](void *mem) -> CommonCluster* {
return new(mem) {{cluster.name}}Cluster();
},
},
{%- endif %}
{%- endfor %}
};

inline void BridgeRegisterAllAttributeOverrides()
{
{% for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}
static {{cluster.name}}Access {{cluster.name}};
registerAttributeAccessOverride(&{{cluster.name}});
{%- endif %}
{%- endfor %}
}

struct AttrInfo
{
chip::ClusterId cluster;
chip::AttributeId attr;
const char *name;
} static const kKnownAttributes[] = {
{% for cluster in clusters %}
{%- if cluster | dynamicCluster(idl) %}

{%- for attr in cluster.attributes %}
{ {{cluster.code}}, {{attr.definition.code}}, "{{attr.definition.name | capitalcase}}" },
{%- endfor %}

{%- endif %}
{%- endfor %}

};

}
Loading

0 comments on commit 104841d

Please sign in to comment.