|
| 1 | +/****************************************************************************** |
| 2 | + * # License |
| 3 | + * <b>Copyright 2021 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 | + |
| 14 | +#include "zwave_command_class_time_parameters.h" |
| 15 | +#include "ZW_classcmd.h" |
| 16 | +#include "zwave_command_handler.h" |
| 17 | +#include "zwave_command_class_indices.h" |
| 18 | +#include "zwave_command_classes_utils.h" |
| 19 | +#include "assert.h" |
| 20 | + |
| 21 | +#ifdef __cplusplus |
| 22 | +extern "C" { |
| 23 | +#endif |
| 24 | + |
| 25 | +#include <time.h> |
| 26 | +#include "platform_date_time.h" |
| 27 | + |
| 28 | +#include "zwave_controller_connection_info.h" |
| 29 | +#include "zwave_tx.h" |
| 30 | + |
| 31 | +/////////////////////////////////////////////////////////////////////////////// |
| 32 | +// Private functions, used to handle individual incoming commands. |
| 33 | +/////////////////////////////////////////////////////////////////////////////// |
| 34 | + |
| 35 | +/** |
| 36 | + * @brief Command handler for the Time parameters Get command. |
| 37 | + * |
| 38 | + * The Gateway will send return a Time Report frame containing |
| 39 | + * the current time (year, month, day, hours, minutes and seconds). |
| 40 | + * |
| 41 | + * @param connection_info Info about the connection properties of this frame. |
| 42 | + * @returns sl_status_t indicating the outcome of returning the time. |
| 43 | + */ |
| 44 | +static sl_status_t zwave_command_class_time_parameters_get( |
| 45 | + const zwave_controller_connection_info_t *connection_info) |
| 46 | +{ |
| 47 | + if (connection_info && connection_info->local.is_multicast) { |
| 48 | + return SL_STATUS_OK; |
| 49 | + } |
| 50 | + |
| 51 | + date_time_t time = platform_get_date_time(); |
| 52 | + ZW_TIME_PARAMETERS_REPORT_FRAME report |
| 53 | + = {.cmdClass = COMMAND_CLASS_TIME_PARAMETERS, |
| 54 | + .cmd = TIME_PARAMETERS_REPORT, |
| 55 | + .year1 = static_cast<uint8_t>((time.year + 1900) >> 8), //MSB |
| 56 | + .year2 = static_cast<uint8_t>((time.year + 1900) & 0xFF), //LSB |
| 57 | + .month = static_cast<uint8_t>(time.mon + 1), |
| 58 | + .day = static_cast<uint8_t>(time.day), |
| 59 | + .hourUtc = static_cast<uint8_t>(time.hour & 0xF), |
| 60 | + .minuteUtc = static_cast<uint8_t>(time.min), |
| 61 | + .secondUtc = static_cast<uint8_t>(time.sec)}; |
| 62 | + |
| 63 | + return zwave_command_class_send_report(connection_info, |
| 64 | + sizeof(report), |
| 65 | + (uint8_t *)&report); |
| 66 | +} |
| 67 | + |
| 68 | +sl_status_t |
| 69 | + zwave_command_class_time_parameters_set(const uint8_t *frame_data, |
| 70 | + uint16_t frame_length) |
| 71 | +{ |
| 72 | + if (frame_length < sizeof(ZW_TIME_PARAMETERS_SET_FRAME)) { |
| 73 | + return SL_STATUS_FAIL; |
| 74 | + } |
| 75 | + |
| 76 | + // Extract and parse the time from the frame data |
| 77 | + uint16_t year = (frame_data[2] << 8) | frame_data[3]; |
| 78 | + uint8_t month = frame_data[4]; |
| 79 | + uint8_t day = frame_data[5]; |
| 80 | + uint8_t hour = frame_data[6]; |
| 81 | + uint8_t minute = frame_data[7]; |
| 82 | + uint8_t second = frame_data[8]; |
| 83 | + |
| 84 | + // Create a date_time_t structure and populate it |
| 85 | + date_time_t new_time; |
| 86 | + new_time.year = year - 1900; |
| 87 | + new_time.mon = month - 1; |
| 88 | + new_time.day = day; |
| 89 | + new_time.hour = hour; |
| 90 | + new_time.min = minute; |
| 91 | + new_time.sec = second; |
| 92 | + |
| 93 | + // Update the system time |
| 94 | + if (platform_set_date_time(&new_time) != SL_STATUS_OK) { |
| 95 | + return SL_STATUS_FAIL; |
| 96 | + } |
| 97 | + |
| 98 | + return SL_STATUS_OK; |
| 99 | +} |
| 100 | + |
| 101 | +/////////////////////////////////////////////////////////////////////////////// |
| 102 | +// Public interface functions |
| 103 | +/////////////////////////////////////////////////////////////////////////////// |
| 104 | +sl_status_t zwave_command_class_time_parameters_support_handler( |
| 105 | + const zwave_controller_connection_info_t *connection_info, |
| 106 | + const uint8_t *frame_data, |
| 107 | + uint16_t frame_length) |
| 108 | +{ |
| 109 | + if (frame_length <= COMMAND_INDEX) { |
| 110 | + return SL_STATUS_NOT_SUPPORTED; |
| 111 | + } |
| 112 | + |
| 113 | + assert(frame_data[COMMAND_CLASS_INDEX] == COMMAND_CLASS_TIME_PARAMETERS); |
| 114 | + |
| 115 | + switch (frame_data[COMMAND_INDEX]) { |
| 116 | + case TIME_PARAMETERS_GET: |
| 117 | + return zwave_command_class_time_parameters_get(connection_info); |
| 118 | + case TIME_PARAMETERS_SET: |
| 119 | + return zwave_command_class_time_parameters_set(frame_data, frame_length); |
| 120 | + |
| 121 | + default: |
| 122 | + return SL_STATUS_NOT_SUPPORTED; |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +sl_status_t zwave_command_class_time_parameters_init() |
| 127 | +{ |
| 128 | + zwave_command_handler_t handler = {0}; |
| 129 | + handler.support_handler |
| 130 | + = &zwave_command_class_time_parameters_support_handler; |
| 131 | + handler.control_handler = NULL; |
| 132 | + handler.minimal_scheme = ZWAVE_CONTROLLER_ENCAPSULATION_NONE; |
| 133 | + handler.manual_security_validation = false; |
| 134 | + handler.command_class = COMMAND_CLASS_TIME_PARAMETERS; |
| 135 | + handler.version = TIME_PARAMETERS_VERSION; |
| 136 | + handler.command_class_name = "Time Parameters"; |
| 137 | + |
| 138 | + return zwave_command_handler_register_handler(handler); |
| 139 | +} |
| 140 | + |
| 141 | +#ifdef __cplusplus |
| 142 | +} |
| 143 | +#endif |
0 commit comments