/**
 * Copyright (c) 2018 - 2020, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */
#ifndef ZB_CUSTOM1_THERMOSTAT_H__
#define ZB_CUSTOM1_THERMOSTAT_H__

/**Custom ha file for clusters***
** Edit : rohit.rajapure@shalaka.com******/

#include "zboss_api.h"
#include "zboss_api_addons.h"
#include "zb_zcl_thermostat.h"
#include "zb_zcl_power_config.h"
#include "zb_zcl_time.h"

#define ZB_HA_CUSTOM1_THERMOSTAT_DEVICE_ID    0x0301
#define ZB_ZCL_MANF_CODE        0x1037                 //manufacturer specific code id


/* Basic cluster attributes initial values. */
#define THERMOSTAT_INIT_BASIC_APP_VERSION            01                                    /**< Version of the application software (1 byte). */
#define THERMOSTAT_INIT_BASIC_STACK_VERSION          10                                    /**< Version of the implementation of the ZigBee stack (1 byte). */
#define THERMOSTAT_INIT_BASIC_HW_VERSION             0x22                                    /**< Version of the hardware of the device (1 byte). */
#define THERMOSTAT_INIT_BASIC_MANUF_NAME             "Eurotronic"                              /**< Manufacturer name (32 bytes). */
#define THERMOSTAT_INIT_BASIC_MODEL_ID               "SPZB0001"                  /**< Model number assigned by manufacturer (32-bytes long string). */
#define THERMOSTAT_INIT_BASIC_DATE_CODE              "20210602"  
#define THERMOSTAT_INIT_BASIC_POWER_SOURCE           ZB_ZCL_BASIC_POWER_SOURCE_BATTERY   /**< Type of power sources available for the device. For possible values see section 3.2.2.2.8 of ZCL specification. */
#define THERMOSTAT_INIT_BASIC_LOCATION_DESC          "Shalaka"                         /**< Describes the physical location of the device (16 bytes). May be modified during commisioning process. */
#define THERMOSTAT_INIT_BASIC_PH_ENV                 ZB_ZCL_BASIC_ENV_UNSPECIFIED          /**< Describes the type of physical environment. For possible values see section 3.2.2.2.10 of ZCL specification. */

/* Basic cluster attributes. */
typedef struct
{
    zb_uint8_t zcl_version;
    zb_uint8_t app_version;
    zb_uint8_t stack_version;
    zb_uint8_t hw_version;
    zb_char_t  mf_name[32];
    zb_char_t  model_id[32];
    zb_char_t  date_code[16];
    zb_uint8_t power_source;
    zb_char_t  location_id[17];
    zb_uint8_t ph_env;
    zb_char_t  sw_ver[17];
} zb_zcl_device_basic_attr_t;

/* Thermostat cluster attributes*/
typedef struct
{
    zb_int16_t  local_temperature;
    zb_int8_t   pi_heating_demand;   
    zb_int8_t   local_temperature_calibration;   
    zb_int16_t  occupied_heating_setpoint;
    zb_int16_t  unoccupied_heating_setpoint;
    zb_int16_t  min_heating_setpoint_limit;
    zb_int16_t  max_heating_setpoint_limit;   
    zb_uint8_t  remote_sensing;
    zb_uint8_t  control_seq_of_operation;
    zb_uint8_t  system_mode;   
    zb_uint8_t  trv_mode;
    zb_uint8_t  set_valve_position;
    zb_uint8_t  error;
    zb_int16_t  current_setpoint;
    zb_uint32_t host_flags;
} zb_zcl_thermostat_attrs_t;


/* Power config cluster attributes*/
typedef struct
{
    zb_uint8_t battery_voltage;
    zb_uint8_t battery_size;
    zb_uint8_t battery_quantity;
    zb_uint8_t battery_rated_voltage;
    zb_uint8_t battery_alarm_mask;
    zb_uint8_t battery_voltage_min_threshold;
    zb_uint8_t battery_percentage_remaining;
    zb_uint8_t battery_voltage_threshold1;
    zb_uint8_t battery_voltage_threshold2;
    zb_uint8_t battery_voltage_threshold3;
    zb_uint8_t battery_percentage_min_threshold;
    zb_uint8_t battery_percentage_threshold1;
    zb_uint8_t battery_percentage_threshold2;
    zb_uint8_t battery_percentage_threshold3;
    zb_uint32_t battery_alarm_state;
}zb_zcl_power_attrs_t;

typedef struct
{
    zb_uint32_t   timeId;
    zb_uint8_t   time_status;
    zb_int32_t  time_zone;
    zb_uint32_t  dst_start;
    zb_uint32_t  dst_end;
    zb_int32_t  dst_shift;
    zb_uint32_t  standard_time;
    zb_uint32_t  local_time;
    zb_uint32_t  last_settime;
    zb_uint32_t  valid_untilTime;
}zb_zcl_device_time_attrs_t;

/* Main application customizable context. Stores all settings and static values. */
typedef struct
{
    zb_zcl_device_basic_attr_t          basic_attr;
    zb_zcl_identify_attrs_t             identify_attr;
    zb_zcl_power_attrs_t                power_attr;
    zb_zcl_device_time_attrs_t          time_attr;
    zb_zcl_thermostat_attrs_t           thermostat_attr;
} thermostat_device_ctx_t;


static thermostat_device_ctx_t m_dev_ctx;

#define THERMOSTAT_SENSOR_ENDPOINT               1                                  /**< Device endpoint. Used to receive light controlling commands. */

//#define ZB_THERMOSTAT_SENSOR_REPORT_ATTR_COUNT  5                                    /**< Number of attributes mandatory for reporting in the Temperature and Pressure Measurement cluster. */
#define ZB_DEVICE_VER_THERMOSTAT         1                                   /**< thermostat device version. */
#define ZB_THERMOSTAT_IN_CLUSTER_NUM     5                                    /**< Number of the input (server) clusters in the thermostat device. */
#define ZB_THERMOSTAT_OUT_CLUSTER_NUM    3                                   /**< Number of the output (client) clusters in the thermostat device. */

#define ZB_CUSTOM_HA_THERMOSTAT_CLUSTER_NUM (ZB_THERMOSTAT_IN_CLUSTER_NUM + \
  ZB_THERMOSTAT_OUT_CLUSTER_NUM)

/* Number of attribute for reporting on device */
#define ZB_THERMOSTAT_SENSOR_REPORT_ATTR_COUNT (ZB_ZCL_THERMOSTAT_REPORT_ATTR_COUNT    \
                                            + ZB_ZCL_POWER_CONFIG_REPORT_ATTR_COUNT \
                                            + ZB_ZCL_TIME_REPORT_ATTR_COUNT)

/** @brief Declares cluster list for the thermostat device.
 *
 *  @param cluster_list_name            Cluster list variable name.
 *  @param basic_attr_list              Attribute list for the Basic cluster.
 *  @param identify_attr_list           Attribute list for the Identify cluster.
 *  @param thermostat_attr_list         Attribute list for the Thermostat cluster.
 *  @param power_config_attr_list       Attribute list for the Power configuration cluster.
 *  @param time_attr_list               Attribute list for the time cluster.
 */
#define ZB_DECLARE_CUSTOM_THERMOSTAT_CLUSTER_LIST(                  \
      cluster_list_name,                                            \
      basic_attr_list,                                              \
      identify_attr_list,                                           \
      power_config_attr_list,                                       \
      thermostat_attr_list,                                         \
      time_attr_list)                                               \
      zb_zcl_cluster_desc_t cluster_list_name[] =                   \
      {                                                             \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_IDENTIFY,                               \
          ZB_ZCL_ARRAY_SIZE(identify_attr_list, zb_zcl_attr_t),     \
          (identify_attr_list),                                     \
          ZB_ZCL_CLUSTER_SERVER_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        ),                                                          \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_BASIC,                                  \
          ZB_ZCL_ARRAY_SIZE(basic_attr_list, zb_zcl_attr_t),        \
          (basic_attr_list),                                        \
          ZB_ZCL_CLUSTER_SERVER_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        ),                                                          \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_POWER_CONFIG,                           \
          ZB_ZCL_ARRAY_SIZE(power_config_attr_list, zb_zcl_attr_t), \
          (power_config_attr_list),                                 \
          ZB_ZCL_CLUSTER_SERVER_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        ),                                                          \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_THERMOSTAT,                             \
          ZB_ZCL_ARRAY_SIZE(thermostat_attr_list, zb_zcl_attr_t),   \
          (thermostat_attr_list),                                   \
          ZB_ZCL_CLUSTER_SERVER_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        ),                                                          \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_TIME,                                   \
          ZB_ZCL_ARRAY_SIZE(time_attr_list, zb_zcl_attr_t),         \
          (time_attr_list),                                         \
          ZB_ZCL_CLUSTER_SERVER_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        ),                                                          \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_IDENTIFY,                               \
          0,                                                        \
          NULL,                                                     \
          ZB_ZCL_CLUSTER_CLIENT_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        ),                                                          \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_TIME,                                   \
          0,                                                        \
          NULL,                                                     \
          ZB_ZCL_CLUSTER_CLIENT_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        ),                                                          \
        ZB_ZCL_CLUSTER_DESC(                                        \
          ZB_ZCL_CLUSTER_ID_THERMOSTAT,                             \
          0,                                                        \
          NULL,                                                     \
          ZB_ZCL_CLUSTER_CLIENT_ROLE,                               \
          ZB_ZCL_MANF_CODE                                          \
        )                                                           \
      }                                                             \


/** @brief Declares simple descriptor for the "Device_name" device.
 *  
 *  @param ep_name          Endpoint variable name.
 *  @param ep_id            Endpoint ID.
 *  @param in_clust_num     Number of the supported input clusters.
 *  @param out_clust_num    Number of the supported output clusters.
 */
#define ZB_ZCL_DECLARE_CUSTOM_THERMOSTAT_DESC(ep_name, ep_id, in_clust_num, out_clust_num) \
  ZB_DECLARE_SIMPLE_DESC(in_clust_num, out_clust_num);                                     \
  ZB_AF_SIMPLE_DESC_TYPE(in_clust_num, out_clust_num) simple_desc_##ep_name =              \
  {                                                                                        \
    ep_id,                                                                                 \
    ZB_AF_HA_PROFILE_ID,                                                                   \
    ZB_HA_CUSTOM1_THERMOSTAT_DEVICE_ID,                                                    \
    ZB_DEVICE_VER_THERMOSTAT,                                                              \
    0,                                                                                     \
    in_clust_num,                                                                          \
    out_clust_num,                                                                         \
    {                                                                                      \
      ZB_ZCL_CLUSTER_ID_BASIC,                                                             \
      ZB_ZCL_CLUSTER_ID_POWER_CONFIG,                                                      \
      ZB_ZCL_CLUSTER_ID_IDENTIFY,                                                          \
      ZB_ZCL_CLUSTER_ID_THERMOSTAT,                                                        \
      ZB_ZCL_CLUSTER_ID_TIME,                                                              \
      ZB_ZCL_CLUSTER_ID_IDENTIFY,                                                          \
      ZB_ZCL_CLUSTER_ID_THERMOSTAT,                                                        \
      ZB_ZCL_CLUSTER_ID_TIME,                                                              \
    }                                                                                      \
  }


/** @brief Declares endpoint for the THERMOSTAT device.
 *   
 *  @param ep_name          Endpoint variable name.
 *  @param ep_id            Endpoint ID.
 *  @param cluster_list     Endpoint cluster list.
 */
#define ZB_HA_DECLARE_CUSTOM_THERMOSTAT_EP(ep_name, ep_id, cluster_list)              \
  ZB_ZCL_DECLARE_CUSTOM_THERMOSTAT_DESC(ep_name,                                       \
      ep_id,                                                                      \
      ZB_THERMOSTAT_IN_CLUSTER_NUM,                                             \
      ZB_THERMOSTAT_OUT_CLUSTER_NUM);                                           \
  ZBOSS_DEVICE_DECLARE_REPORTING_CTX(reporting_info## device_ctx_name,            \
                                     ZB_THERMOSTAT_SENSOR_REPORT_ATTR_COUNT);          \
  ZB_AF_DECLARE_ENDPOINT_DESC(ep_name, ep_id,                                     \
      ZB_AF_HA_PROFILE_ID,                                                        \
      0,                                                                          \
      NULL,                                                                       \
      ZB_ZCL_ARRAY_SIZE(cluster_list, zb_zcl_cluster_desc_t),                     \
      cluster_list,                                                               \
      (zb_af_simple_desc_1_1_t*)&simple_desc_##ep_name,                           \
      ZB_THERMOSTAT_SENSOR_REPORT_ATTR_COUNT, reporting_info## device_ctx_name, 0, NULL)


/*!
  @brief Declare application's device context for Thermostat device
  @param device_ctx - device context variable
  @param ep_name - endpoint variable name
*/
#define ZB_HA_DECLARE_CUSTOM_THERMOSTAT_CTX(device_ctx, ep_name)                                        \
  ZBOSS_DECLARE_DEVICE_CTX_1_EP(device_ctx, ep_name)

#endif // ZB_CUSTOM1_THERMOSTAT_H__
