|
| 1 | +/****************************************************************************** |
| 2 | + * # License |
| 3 | + * <b>Copyright 2024 Silicon Laboratories Inc. www.silabs.com</b> |
| 4 | + ****************************************************************************** |
| 5 | + * The licensor of this software is Silicon Laboratories Inc. Your use of this |
| 6 | + * software is governed by the terms of Silicon Labs Master Software License |
| 7 | + * Agreement (MSLA) available at |
| 8 | + * www.silabs.com/about-us/legal/master-software-license-agreement. This |
| 9 | + * software is distributed to you in Source Code format and is governed by the |
| 10 | + * sections of the MSLA applicable to Source Code. |
| 11 | + * |
| 12 | + *****************************************************************************/ |
| 13 | +// Includes from this component |
| 14 | +#include "zwave_command_class_switch_all.h" |
| 15 | +#include "zwave_command_classes_utils.h" |
| 16 | + |
| 17 | +// Generic includes |
| 18 | +#include <stdlib.h> |
| 19 | +#include <assert.h> |
| 20 | + |
| 21 | +// Includes from other ZPC Components |
| 22 | +#include "zwave_command_class_indices.h" |
| 23 | +#include "zwave_command_handler.h" |
| 24 | +#include "zpc_attribute_store_network_helper.h" |
| 25 | +#include "attribute_store_defined_attribute_types.h" |
| 26 | +#include "ZW_classcmd.h" |
| 27 | +#include "zpc_attribute_resolver.h" |
| 28 | + |
| 29 | +// Includes from other Unify Components |
| 30 | +#include "dotdot_mqtt.h" |
| 31 | +#include "dotdot_mqtt_generated_commands.h" |
| 32 | +#include "attribute_store_helper.h" |
| 33 | +#include "attribute_resolver.h" |
| 34 | +#include "attribute_timeouts.h" |
| 35 | +#include "sl_log.h" |
| 36 | + |
| 37 | +// Cpp include |
| 38 | +#include "attribute.hpp" |
| 39 | +#include "zwave_frame_generator.hpp" |
| 40 | +#include "zwave_frame_parser.hpp" |
| 41 | + |
| 42 | +// Attribute macro, shortening those long defines for attribute types: |
| 43 | +#define ATTRIBUTE(type) ATTRIBUTE_COMMAND_CLASS_SWITCH_ALL_##type |
| 44 | + |
| 45 | +// Log tag |
| 46 | +constexpr char LOG_TAG[] = "zwave_command_class_switch_all"; |
| 47 | + |
| 48 | + |
| 49 | +namespace |
| 50 | +{ |
| 51 | +zwave_frame_generator frame_generator(COMMAND_CLASS_SWITCH_ALL); |
| 52 | +} |
| 53 | + |
| 54 | +/////////////////////////////////////////////////////////////////////////////// |
| 55 | +// Helper functions |
| 56 | +/////////////////////////////////////////////////////////////////////////////// |
| 57 | +zwave_cc_version_t get_current_switch_all_version(attribute_store_node_t node) |
| 58 | +{ |
| 59 | + zwave_cc_version_t version |
| 60 | + = zwave_command_class_get_version_from_node(node, COMMAND_CLASS_SWITCH_ALL); |
| 61 | + |
| 62 | + if (version == 0) { |
| 63 | + sl_log_error(LOG_TAG, "switch_all Command Class Version not found"); |
| 64 | + } |
| 65 | + |
| 66 | + return version; |
| 67 | +} |
| 68 | +/////////////////////////////////////////////////////////////////////////////// |
| 69 | +// Resolution functions |
| 70 | +/////////////////////////////////////////////////////////////////////////////// |
| 71 | +//static sl_status_t zwave_command_class_switch_all_get( |
| 72 | +// attribute_store_node_t node, uint8_t *frame, uint16_t *frame_length) |
| 73 | +//{ |
| 74 | +// return frame_generator.generate_no_args_frame(switch_all_GET, |
| 75 | +// frame, |
| 76 | +// frame_length); |
| 77 | +//} |
| 78 | + |
| 79 | +// static sl_status_t zwave_command_class_switch_all_set( |
| 80 | +// attribute_store_node_t node, uint8_t *frame, uint16_t *frame_length) |
| 81 | +// { |
| 82 | +// try { |
| 83 | +// attribute_store::attribute value_node(node); |
| 84 | +// auto current_version = get_current_switch_all_version(node); |
| 85 | +// |
| 86 | +// // Compute expected size for set frame |
| 87 | +// const uint8_t expected_frame_size = 12; |
| 88 | +// |
| 89 | +// // Creating the frame |
| 90 | +// frame_generator.initialize_frame(switch_all_SET, |
| 91 | +// frame, |
| 92 | +// expected_frame_size); |
| 93 | +// frame_generator.add_value(value_node, DESIRED_OR_REPORTED_ATTRIBUTE); |
| 94 | +// frame_generator.validate_frame(frame_length); |
| 95 | +// } catch (const std::exception &e) { |
| 96 | +// sl_log_error(LOG_TAG, |
| 97 | +// "Error while generating switch_all Set frame : %s", |
| 98 | +// e.what()); |
| 99 | +// return SL_STATUS_FAIL; |
| 100 | +// } |
| 101 | +// |
| 102 | +// return SL_STATUS_OK; |
| 103 | +//} |
| 104 | + |
| 105 | +/////////////////////////////////////////////////////////////////////////////// |
| 106 | +// Frame parsing functions |
| 107 | +/////////////////////////////////////////////////////////////////////////////// |
| 108 | +//static sl_status_t zwave_command_class_switch_all_handle_report( |
| 109 | +// const zwave_controller_connection_info_t *connection_info, |
| 110 | +// const uint8_t *frame_data, |
| 111 | +// uint16_t frame_length) |
| 112 | +//{ |
| 113 | +// // Setup |
| 114 | +// attribute_store::attribute endpoint_node( |
| 115 | +// zwave_command_class_get_endpoint_node(connection_info)); |
| 116 | +// auto current_version = get_current_switch_all_version(endpoint_node); |
| 117 | +// |
| 118 | +// sl_log_debug(LOG_TAG, "switch_all Report frame received"); |
| 119 | +// |
| 120 | +// // Compute expected size for report frame |
| 121 | +// const uint8_t expected_size = 12; |
| 122 | + |
| 123 | +// // Parse the frame |
| 124 | +// try { |
| 125 | +// zwave_frame_parser parser(frame_data, frame_length); |
| 126 | + |
| 127 | +// if (!parser.is_frame_size_valid(expected_size)) { |
| 128 | +// sl_log_error(LOG_TAG, |
| 129 | +// "Invalid frame size for switch_all Report frame"); |
| 130 | +// return SL_STATUS_FAIL; |
| 131 | +// } |
| 132 | + |
| 133 | +// } catch (const std::exception &e) { |
| 134 | +// sl_log_error(LOG_TAG, |
| 135 | +// "Error while parsing switch_all Report frame : %s", |
| 136 | +// e.what()); |
| 137 | +// return SL_STATUS_FAIL; |
| 138 | +// } |
| 139 | +// return SL_STATUS_OK; |
| 140 | +//} |
| 141 | + |
| 142 | +/////////////////////////////////////////////////////////////////////////////// |
| 143 | +// Incoming commands handler |
| 144 | +/////////////////////////////////////////////////////////////////////////////// |
| 145 | +sl_status_t zwave_command_class_switch_all_control_handler( |
| 146 | + const zwave_controller_connection_info_t *connection_info, |
| 147 | + const uint8_t *frame_data, |
| 148 | + uint16_t frame_length) |
| 149 | +{ |
| 150 | + // Frame too short, it should have not come here. |
| 151 | + if (frame_length <= COMMAND_INDEX) { |
| 152 | + return SL_STATUS_NOT_SUPPORTED; |
| 153 | + } |
| 154 | + |
| 155 | + switch (frame_data[COMMAND_INDEX]) { |
| 156 | + // case switch_all_REPORT: |
| 157 | + // return zwave_command_class_switch_all_handle_report(connection_info, |
| 158 | + // frame_data, |
| 159 | + // frame_length); |
| 160 | + default: |
| 161 | + return SL_STATUS_NOT_SUPPORTED; |
| 162 | + } |
| 163 | +} |
| 164 | + |
| 165 | +/////////////////////////////////////////////////////////////////////////////// |
| 166 | +// Attribute Store callback functions |
| 167 | +/////////////////////////////////////////////////////////////////////////////// |
| 168 | +static void zwave_command_class_switch_all_on_version_attribute_update( |
| 169 | + attribute_store_node_t updated_node, attribute_store_change_t change) |
| 170 | +{ |
| 171 | + if (change == ATTRIBUTE_DELETED) { |
| 172 | + return; |
| 173 | + } |
| 174 | + |
| 175 | + // Confirm that we have a version attribute update |
| 176 | + assert(ATTRIBUTE(VERSION) == attribute_store_get_node_type(updated_node)); |
| 177 | + |
| 178 | + attribute_store::attribute version_node(updated_node); |
| 179 | + // Do not create the attributes until we are sure of the version |
| 180 | + //zwave_cc_version_t supporting_node_version = 0; |
| 181 | + |
| 182 | + // Wait for the version |
| 183 | + if (!version_node.reported_exists()) { |
| 184 | + return; |
| 185 | + } |
| 186 | + //supporting_node_version = version_node.reported<uint8_t>(); |
| 187 | + |
| 188 | + // Now we know we have a switch_all supporting endpoint. |
| 189 | + attribute_store::attribute endpoint_node |
| 190 | + = version_node.first_parent(ATTRIBUTE_ENDPOINT_ID); |
| 191 | + |
| 192 | + // Create the switch_all attributes |
| 193 | + endpoint_node.emplace_node(ATTRIBUTE(MODE)); |
| 194 | + |
| 195 | + endpoint_node.first_parent(ATTRIBUTE_HOME_ID).emplace_node(ATTRIBUTE(ON_OFF)); |
| 196 | + |
| 197 | +} |
| 198 | + |
| 199 | +/////////////////////////////////////////////////////////////////////////////// |
| 200 | +// Public interface functions |
| 201 | +/////////////////////////////////////////////////////////////////////////////// |
| 202 | +sl_status_t zwave_command_class_switch_all_init() |
| 203 | +{ |
| 204 | + // Attribute store callbacks |
| 205 | + attribute_store_register_callback_by_type( |
| 206 | + zwave_command_class_switch_all_on_version_attribute_update, |
| 207 | + ATTRIBUTE(VERSION)); |
| 208 | + |
| 209 | + // Attribute resolver rules |
| 210 | + // attribute_resolver_register_rule(ATTRIBUTE(MODE), |
| 211 | + // zwave_command_class_switch_all_set, |
| 212 | + // zwave_command_class_switch_all_get); |
| 213 | + // attribute_resolver_register_rule(ATTRIBUTE(ON_OFF), |
| 214 | + // zwave_command_class_switch_all_set, |
| 215 | + // nullptr); |
| 216 | + |
| 217 | + // The support side of things: Register our handler to the Z-Wave CC framework: |
| 218 | + zwave_command_handler_t handler = {}; |
| 219 | + handler.support_handler = NULL; |
| 220 | + handler.control_handler = &zwave_command_class_switch_all_control_handler; |
| 221 | + // Not supported, so this does not really matter |
| 222 | + handler.minimal_scheme = ZWAVE_CONTROLLER_ENCAPSULATION_NETWORK_SCHEME; |
| 223 | + handler.manual_security_validation = false; |
| 224 | + handler.command_class = COMMAND_CLASS_SWITCH_ALL; |
| 225 | + handler.version = SWITCH_ALL_VERSION; |
| 226 | + handler.command_class_name = "switch_all"; |
| 227 | + handler.comments = ""; |
| 228 | + |
| 229 | + zwave_command_handler_register_handler(handler); |
| 230 | + |
| 231 | + return SL_STATUS_OK; |
| 232 | +} |
0 commit comments