/**
  ******************************************************************************
  * @file   lv_obj_datasubs.h
  * @author Sifli software development team
  ******************************************************************************
*/
/**
 * @attention
 * Copyright (c) 2019 - 2022,  Sifli Technology
 *
 * 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 Sifli 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 Sifli 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
 *    Sifli 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 SIFLI TECHNOLOGY "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 SIFLI TECHNOLOGY 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 _LV_OBJ_DATASUBS_H_
#define _LV_OBJ_DATASUBS_H_
#include "lvgl.h"

/**********************
 *      TYPEDEFS
 **********************/
/**
 * The following interfaces can be used to refresh ui page data by way of subscription,
 * note except the notify interface that can be used across threads,
 * other interfaces cannot be used across threads.
 */

#define MAX_DATASUBS_NAME_LEN        64                     /**< maxium name length of data subscription                                           */

typedef void (*lv_obj_datasubs_cb_t)(struct _lv_obj_t *obj, uint32_t type, const void *data, uint16_t len, void *user_data);

/**
 * Data subscription manager structure
 */

typedef struct
{
    char                    id[MAX_DATASUBS_NAME_LEN];      /**< subscription name, user-defined, must be unique                                    */
    uint32_t                type;                           /**< subscribed type, user-defined, must be unique within the same subscription id      */
    lv_obj_t                *obj;                           /**< subscribed index, user-defined, must remain unique                                 */
    rt_list_t               list;                           /**< list link                                                                          */
    void                    *user_data;                     /**< user data                                                                          */
    lv_obj_datasubs_cb_t    cb;                             /**< The callback of the subscription will be performed in the notification message     */

} lv_obj_datasubs_t;

/**
 * Send message to all subscribed index
 * @param index uint16_t to subscribed index
 * @param data pointer to unvarnished transmission message
 * @param len uint16_t to message lenght
 * @return error number
 */
int lv_obj_datasubs_notify(const char *id, uint32_t type, const void *data, uint16_t len, void *user_data);

/**
 * Subscribe index for a object
 * @param index uint16_t to subscribed index
 * @param obj pointer to a subscribe object
 * @param cb structure to message callback
 * @return error number
 */
int lv_obj_data_subscribe(lv_obj_t *obj, const char *id, uint32_t type, lv_obj_datasubs_cb_t cb, void *user_data);

/**
 * Unsubscribe for a subscription name
 * @param index uint16_t to subscribed index
 * @param obj pointer to a subscribe object
 * @param type sub type to every name
 * @return error number
 */
int lv_obj_data_unsubscribe(lv_obj_t *obj, const char *id, uint32_t type);

/**
 * Unsubscribe all for target id
 * @param id string to subscribed name
 * @return error number
 */
int lv_obj_data_unsubscribe_all(const char *id);


/**
 * Unsubscribe all for target obj
 * @param obj to subscribed
 * @return error number
 */
int lv_obj_data_unbind(lv_obj_t *obj);


#endif /*_LV_OBJ_DATASUBS_H_*/
