1 /*
2  * SPDX-FileCopyrightText: 2017-2021 Espressif Systems (Shanghai) CO LTD
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #ifndef _SERVER_COMMON_H_
8 #define _SERVER_COMMON_H_
9 
10 #include <string.h>
11 #include <stdint.h>
12 #include "mesh_access.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #define BLE_MESH_SERVER_RSP_MAX_LEN         384
19 
20 #define BLE_MESH_SERVER_TRANS_MIC_SIZE      4
21 
22 #define BLE_MESH_CHECK_SEND_STATUS(_func) do {                      \
23         int __status = (_func);                                     \
24         if (__status) {                                             \
25             BT_ERR("%s, Send failed, err %d", __func__, __status);  \
26         }                                                           \
27     } while(0);
28 
29 #define BLE_MESH_STATE_OFF                      0x00
30 #define BLE_MESH_STATE_ON                       0x01
31 #define BLE_MESH_STATE_RESTORE                  0x02
32 
33 /* Following 4 values are as per Mesh Model specification */
34 #define BLE_MESH_LIGHTNESS_MIN                  0x0001
35 #define BLE_MESH_LIGHTNESS_MAX                  0xFFFF
36 #define BLE_MESH_TEMPERATURE_MIN                0x0320
37 #define BLE_MESH_TEMPERATURE_MAX                0x4E20
38 #define BLE_MESH_TEMPERATURE_UNKNOWN            0xFFFF
39 
40 /* Refer 7.2 of Mesh Model Specification */
41 #define BLE_MESH_RANGE_UPDATE_SUCCESS           0x00
42 #define BLE_MESH_CANNOT_SET_RANGE_MIN           0x01
43 #define BLE_MESH_CANNOT_SET_RANGE_MAX           0x02
44 
45 #define BLE_MESH_UNKNOWN_REMAIN_TIME            0x3F
46 #define BLE_MESH_DEVICE_SPECIFIC_RESOLUTION     10
47 
48 enum {
49     BLE_MESH_TRANS_TIMER_START,  /* Proper transition timer has been started */
50     BLE_MESH_TRANS_FLAG_MAX,
51 };
52 
53 struct bt_mesh_state_transition {
54     bool just_started;
55 
56     uint8_t  trans_time;
57     uint8_t  remain_time;
58     uint8_t  delay;
59     uint32_t quo_tt;
60     uint32_t counter;
61     uint32_t total_duration;
62     int64_t  start_timestamp;
63 
64     BLE_MESH_ATOMIC_DEFINE(flag, BLE_MESH_TRANS_FLAG_MAX);
65     struct k_delayed_work timer;
66 };
67 
68 struct bt_mesh_last_msg_info {
69     uint8_t  tid;
70     uint16_t src;
71     uint16_t dst;
72     int64_t  timestamp;
73 };
74 
75 #define BLE_MESH_SERVER_RSP_BY_APP  0
76 #define BLE_MESH_SERVER_AUTO_RSP    1
77 
78 struct bt_mesh_server_rsp_ctrl {
79     /**
80      * @brief BLE Mesh Server Response Option
81      *        1. If get_auto_rsp is set to BLE_MESH_SERVER_RSP_BY_APP, then the response
82      *           of Client Get messages need to be replied by the application;
83      *        2. If get_auto_rsp is set to BLE_MESH_SERVER_AUTO_RSP, then the response
84      *           of Client Get messages will be replied by the server models;
85      *        3. If set_auto_rsp is set to BLE_MESH_SERVER_RSP_BY_APP, then the response
86      *           of Client Set messages need to be replied by the application;
87      *        4. If set_auto_rsp is set to BLE_MESH_SERVER_AUTO_RSP, then the response
88      *           of Client Set messages will be replied by the server models;
89      *        5. If status_auto_rsp is set to BLE_MESH_SERVER_RSP_BY_APP, then the response
90      *           of Server Status messages need to be replied by the application;
91      *        6. If status_auto_rsp is set to BLE_MESH_SERVER_AUTO_RSP, then the response
92      *           of Server status messages will be replied by the server models;
93      */
94     uint8_t get_auto_rsp : 1, /* Response for Client Get messages */
95             set_auto_rsp : 1, /* Response for Client Set messages */
96             status_auto_rsp : 1; /* Response for Server Status messages */
97 };
98 
99 uint8_t bt_mesh_get_default_trans_time(struct bt_mesh_model *model);
100 
101 int bt_mesh_get_light_lc_trans_time(struct bt_mesh_model *model, uint8_t *trans_time);
102 
103 int bt_mesh_server_get_optional(struct bt_mesh_model *model,
104                                 struct bt_mesh_msg_ctx *ctx,
105                                 struct net_buf_simple *buf,
106                                 uint8_t *trans_time, uint8_t *delay,
107                                 bool *optional);
108 
109 void bt_mesh_server_alloc_ctx(struct k_work *work);
110 void bt_mesh_server_free_ctx(struct k_work *work);
111 
112 bool bt_mesh_is_server_recv_last_msg(struct bt_mesh_last_msg_info *last,
113                                      uint8_t tid, uint16_t src, uint16_t dst, int64_t *now);
114 
115 void bt_mesh_server_update_last_msg(struct bt_mesh_last_msg_info *last,
116                                     uint8_t tid, uint16_t src, uint16_t dst, int64_t *now);
117 
118 struct net_buf_simple *bt_mesh_server_get_pub_msg(struct bt_mesh_model *model, uint16_t msg_len);
119 
120 #ifdef __cplusplus
121 }
122 #endif
123 
124 #endif /* _SERVER_COMMON_H_ */
125