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