Line data Source code
1 : /**
2 : *
3 : * Copyright (c) 2020 Project CHIP Authors
4 : *
5 : * Licensed under the Apache License, Version 2.0 (the "License");
6 : * you may not use this file except in compliance with the License.
7 : * You may obtain a copy of the License at
8 : *
9 : * http://www.apache.org/licenses/LICENSE-2.0
10 : *
11 : * Unless required by applicable law or agreed to in writing, software
12 : * distributed under the License is distributed on an "AS IS" BASIS,
13 : * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 : * See the License for the specific language governing permissions and
15 : * limitations under the License.
16 : */
17 :
18 : #pragma once
19 :
20 : #include <string.h> // For mem* functions.
21 :
22 : #include <optional>
23 :
24 : #include <app/util/basic-types.h>
25 : #include <lib/core/NodeId.h>
26 :
27 : static_assert(sizeof(chip::NodeId) == sizeof(uint64_t), "Unexpected node if size");
28 :
29 : /**
30 : * @brief Defines binding types.
31 : */
32 : #ifdef DOXYGEN_SHOULD_SKIP_THIS
33 : enum EmberBindingType
34 : #else
35 : typedef uint8_t EmberBindingType;
36 : enum
37 : #endif
38 : {
39 : /** A binding that is currently not in use. */
40 : MATTER_UNUSED_BINDING = 0,
41 : /** A unicast binding whose 64-bit identifier is the destination EUI64. */
42 : MATTER_UNICAST_BINDING = 1,
43 : /** A multicast binding whose 64-bit identifier is the group address. This
44 : * binding can be used to send messages to the group and to receive
45 : * messages sent to the group. */
46 : MATTER_MULTICAST_BINDING = 3,
47 : };
48 :
49 : /**
50 : * @brief Size of EUI64 (an IEEE address) in bytes (8).
51 : */
52 : #define EUI64_SIZE 8
53 :
54 : /**
55 : * @brief EUI 64-bit ID (an IEEE address).
56 : */
57 : typedef uint8_t EmberEUI64[EUI64_SIZE];
58 :
59 : /**
60 : * @brief 16-bit ZigBee network address.
61 : */
62 : typedef uint16_t EmberNodeId;
63 :
64 : /**
65 : * @brief 802.15.4 PAN ID.
66 : */
67 : typedef uint16_t EmberPanId;
68 :
69 : /** @brief Defines an entry in the binding table.
70 : *
71 : * A binding entry specifies a local endpoint, a remote endpoint, a
72 : * cluster ID and either the destination EUI64 (for unicast bindings) or the
73 : * 64-bit group address (for multicast bindings).
74 : */
75 : struct EmberBindingTableEntry
76 : {
77 86 : static EmberBindingTableEntry ForNode(chip::FabricIndex fabric, chip::NodeId node, chip::EndpointId localEndpoint,
78 : chip::EndpointId remoteEndpoint, std::optional<chip::ClusterId> cluster)
79 : {
80 86 : EmberBindingTableEntry entry = {
81 : .type = MATTER_UNICAST_BINDING,
82 : .fabricIndex = fabric,
83 : .local = localEndpoint,
84 : .clusterId = cluster,
85 : .remote = remoteEndpoint,
86 : .nodeId = node,
87 86 : };
88 86 : return entry;
89 : }
90 :
91 2 : static EmberBindingTableEntry ForGroup(chip::FabricIndex fabric, chip::GroupId group, chip::EndpointId localEndpoint,
92 : std::optional<chip::ClusterId> cluster)
93 : {
94 2 : EmberBindingTableEntry entry = {
95 : .type = MATTER_MULTICAST_BINDING,
96 : .fabricIndex = fabric,
97 : .local = localEndpoint,
98 : .clusterId = cluster,
99 : .remote = 0,
100 : .groupId = group,
101 2 : };
102 2 : return entry;
103 : }
104 :
105 : /** The type of binding. */
106 : EmberBindingType type = MATTER_UNUSED_BINDING;
107 :
108 : chip::FabricIndex fabricIndex;
109 : /** The endpoint on the local node. */
110 : chip::EndpointId local;
111 : /** A cluster ID that matches one from the local endpoint's simple descriptor.
112 : * This cluster ID is set by the provisioning application to indicate which
113 : * part an endpoint's functionality is bound to this particular remote node
114 : * and is used to distinguish between unicast and multicast bindings. Note
115 : * that a binding can be used to to send messages with any cluster ID, not
116 : * just that listed in the binding.
117 : */
118 : std::optional<chip::ClusterId> clusterId;
119 : /** The endpoint on the remote node (specified by \c identifier). */
120 : chip::EndpointId remote;
121 : /** A 64-bit destination identifier. This is either:
122 : * - The destination chip::NodeId, for unicasts.
123 : * - A multicast ChipGroupId, for multicasts.
124 : * Which one is being used depends on the type of this binding.
125 : */
126 : union
127 : {
128 : chip::NodeId nodeId;
129 : chip::GroupId groupId;
130 : };
131 :
132 34 : bool operator==(EmberBindingTableEntry const & other) const
133 : {
134 34 : if (type != other.type)
135 : {
136 0 : return false;
137 : }
138 :
139 34 : if (type == MATTER_MULTICAST_BINDING && groupId != other.groupId)
140 : {
141 0 : return false;
142 : }
143 :
144 34 : if (type == MATTER_UNICAST_BINDING && (nodeId != other.nodeId || remote != other.remote))
145 : {
146 0 : return false;
147 : }
148 :
149 34 : return fabricIndex == other.fabricIndex && local == other.local && clusterId == other.clusterId;
150 : }
151 : };
152 :
153 : /**
154 : * @brief Function pointer for timer callback
155 : */
156 : typedef void (*TimerCallback)(chip::EndpointId);
157 :
158 : /** @brief The control structure for events.
159 : *
160 : * It holds the callback and its parameters.
161 : */
162 : typedef struct
163 : {
164 : /* Callback information */
165 : TimerCallback callback;
166 : chip::EndpointId endpoint;
167 :
168 : } EmberEventControl;
169 :
170 : /**
171 : * @brief Returns the value of the bitmask \c bits within
172 : * the register or byte \c reg.
173 : */
174 : #define READBITS(reg, bits) ((reg) & (bits))
175 :
176 : #define MILLISECOND_TICKS_PER_SECOND 1000
177 : #define MILLISECOND_TICKS_PER_DECISECOND (MILLISECOND_TICKS_PER_SECOND / 10)
|