1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _CLIENT_COMMON_H_
8 #define _CLIENT_COMMON_H_
9 
10 #include "mesh_access.h"
11 
12 #ifdef __cplusplus
13 extern "C" {
14 #endif
15 
16 /** Client model opcode pair table */
17 typedef struct {
18     uint32_t cli_op;    /* Client message opcode */
19     uint32_t status_op; /* Corresponding status message opcode */
20 } bt_mesh_client_op_pair_t;
21 
22 /** Client model user data context */
23 typedef struct {
24     /** Pointer to the client model */
25     struct bt_mesh_model *model;
26 
27     /** Size of the opcode pair table */
28     int op_pair_size;
29 
30     /** Pointer to the opcode pair table */
31     const bt_mesh_client_op_pair_t *op_pair;
32 
33     /**
34      * @brief This function is a callback function used to push the received unsolicited
35      *        messages to the application layer.
36      *
37      * @param[in]  opcode: Opcode of received status message
38      * @param[in]  model:  Model associated with the status message
39      * @param[in]  ctx:    Context information of the status message
40      * @param[in]  buf:    Buffer contains the status message value
41      *
42      * @return None
43      */
44     void (*publish_status)(uint32_t opcode, struct bt_mesh_model *model, struct bt_mesh_msg_ctx *ctx, struct net_buf_simple *buf);
45 
46     /** Pointer to the internal data of client model */
47     void *internal_data;
48 
49     /** Role of the device to which the client model belongs */
50     uint8_t msg_role;
51 } bt_mesh_client_user_data_t;
52 
53 /** Client model internal data context */
54 typedef struct  {
55     sys_slist_t queue;
56 } bt_mesh_client_internal_data_t;
57 
58 /** Client model sending message related context */
59 typedef struct {
60     sys_snode_t client_node;
61     struct bt_mesh_msg_ctx ctx;     /* Message context */
62     uint32_t opcode;                /* Message opcode */
63     uint32_t op_pending;            /* Expected status message opcode */
64     int32_t  timeout;               /* Calculated message timeout value */
65     struct k_delayed_work timer;    /* Time used to get response. Only for internal use. */
66 } bt_mesh_client_node_t;
67 
68 /** Client model sending message parameters */
69 typedef struct {
70     uint32_t opcode;                    /* Message opcode */
71     struct bt_mesh_model *model;        /* Pointer to the client model */
72     struct bt_mesh_msg_ctx ctx;         /* Message context */
73     int32_t msg_timeout;                /* Time to get corresponding response */
74     uint8_t msg_role;                   /* Role (Node/Provisioner) of the device */
75     const struct bt_mesh_send_cb *cb;   /* User defined callback function */
76     void *cb_data;                      /* User defined callback value */
77 } bt_mesh_client_common_param_t;
78 
79 void bt_mesh_client_model_lock(void);
80 
81 void bt_mesh_client_model_unlock(void);
82 
83 int bt_mesh_client_init(struct bt_mesh_model *model);
84 
85 int bt_mesh_client_deinit(struct bt_mesh_model *model);
86 
87 /**
88  * @brief Check if the msg received by client model is a publish msg or not
89  *
90  * @param model     Mesh (client) Model that the message belongs to.
91  * @param ctx       Message context, includes keys, TTL, etc.
92  * @param buf       The message buffer
93  * @param need_pub  Indicate if the msg sent to app layer as a publish msg
94  * @return 0 on success, or (negative) error code on failure.
95  */
96 bt_mesh_client_node_t *bt_mesh_is_client_recv_publish_msg(struct bt_mesh_model *model,
97                                                           struct bt_mesh_msg_ctx *ctx,
98                                                           struct net_buf_simple *buf, bool need_pub);
99 
100 int bt_mesh_client_send_msg(bt_mesh_client_common_param_t *param,
101                             struct net_buf_simple *msg, bool need_ack,
102                             k_work_handler_t timer_handler);
103 
104 int bt_mesh_client_free_node(bt_mesh_client_node_t *node);
105 
106 int bt_mesh_client_clear_list(void *data);
107 
108 /**
109  * @brief Set role of the client model for internal use.
110  *
111  * @param[in] model: Pointer to the client model
112  * @param[in] role:  Role of the device
113  *
114  * @return Zero - success, otherwise - fail
115  */
116 int bt_mesh_set_client_model_role(struct bt_mesh_model *model, uint8_t role);
117 
118 #ifdef __cplusplus
119 }
120 #endif
121 
122 #endif /* _CLIENT_COMMON_H_ */
123