1 /*  Bluetooth Mesh */
2 
3 /*
4  * SPDX-FileCopyrightText: 2017 Intel Corporation
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 #ifndef _FOUNDATION_H_
9 #define _FOUNDATION_H_
10 
11 #include "mesh_byteorder.h"
12 #include "net.h"
13 
14 #ifdef __cplusplus
15 extern "C" {
16 #endif
17 
18 #define OP_APP_KEY_ADD                     BLE_MESH_MODEL_OP_1(0x00)
19 #define OP_APP_KEY_UPDATE                  BLE_MESH_MODEL_OP_1(0x01)
20 #define OP_DEV_COMP_DATA_STATUS            BLE_MESH_MODEL_OP_1(0x02)
21 #define OP_MOD_PUB_SET                     BLE_MESH_MODEL_OP_1(0x03)
22 #define OP_HEALTH_CURRENT_STATUS           BLE_MESH_MODEL_OP_1(0x04)
23 #define OP_HEALTH_FAULT_STATUS             BLE_MESH_MODEL_OP_1(0x05)
24 #define OP_HEARTBEAT_PUB_STATUS            BLE_MESH_MODEL_OP_1(0x06)
25 #define OP_APP_KEY_DEL                     BLE_MESH_MODEL_OP_2(0x80, 0x00)
26 #define OP_APP_KEY_GET                     BLE_MESH_MODEL_OP_2(0x80, 0x01)
27 #define OP_APP_KEY_LIST                    BLE_MESH_MODEL_OP_2(0x80, 0x02)
28 #define OP_APP_KEY_STATUS                  BLE_MESH_MODEL_OP_2(0x80, 0x03)
29 #define OP_ATTENTION_GET                   BLE_MESH_MODEL_OP_2(0x80, 0x04)
30 #define OP_ATTENTION_SET                   BLE_MESH_MODEL_OP_2(0x80, 0x05)
31 #define OP_ATTENTION_SET_UNREL             BLE_MESH_MODEL_OP_2(0x80, 0x06)
32 #define OP_ATTENTION_STATUS                BLE_MESH_MODEL_OP_2(0x80, 0x07)
33 #define OP_DEV_COMP_DATA_GET               BLE_MESH_MODEL_OP_2(0x80, 0x08)
34 #define OP_BEACON_GET                      BLE_MESH_MODEL_OP_2(0x80, 0x09)
35 #define OP_BEACON_SET                      BLE_MESH_MODEL_OP_2(0x80, 0x0a)
36 #define OP_BEACON_STATUS                   BLE_MESH_MODEL_OP_2(0x80, 0x0b)
37 #define OP_DEFAULT_TTL_GET                 BLE_MESH_MODEL_OP_2(0x80, 0x0c)
38 #define OP_DEFAULT_TTL_SET                 BLE_MESH_MODEL_OP_2(0x80, 0x0d)
39 #define OP_DEFAULT_TTL_STATUS              BLE_MESH_MODEL_OP_2(0x80, 0x0e)
40 #define OP_FRIEND_GET                      BLE_MESH_MODEL_OP_2(0x80, 0x0f)
41 #define OP_FRIEND_SET                      BLE_MESH_MODEL_OP_2(0x80, 0x10)
42 #define OP_FRIEND_STATUS                   BLE_MESH_MODEL_OP_2(0x80, 0x11)
43 #define OP_GATT_PROXY_GET                  BLE_MESH_MODEL_OP_2(0x80, 0x12)
44 #define OP_GATT_PROXY_SET                  BLE_MESH_MODEL_OP_2(0x80, 0x13)
45 #define OP_GATT_PROXY_STATUS               BLE_MESH_MODEL_OP_2(0x80, 0x14)
46 #define OP_KRP_GET                         BLE_MESH_MODEL_OP_2(0x80, 0x15)
47 #define OP_KRP_SET                         BLE_MESH_MODEL_OP_2(0x80, 0x16)
48 #define OP_KRP_STATUS                      BLE_MESH_MODEL_OP_2(0x80, 0x17)
49 #define OP_MOD_PUB_GET                     BLE_MESH_MODEL_OP_2(0x80, 0x18)
50 #define OP_MOD_PUB_STATUS                  BLE_MESH_MODEL_OP_2(0x80, 0x19)
51 #define OP_MOD_PUB_VA_SET                  BLE_MESH_MODEL_OP_2(0x80, 0x1a)
52 #define OP_MOD_SUB_ADD                     BLE_MESH_MODEL_OP_2(0x80, 0x1b)
53 #define OP_MOD_SUB_DEL                     BLE_MESH_MODEL_OP_2(0x80, 0x1c)
54 #define OP_MOD_SUB_DEL_ALL                 BLE_MESH_MODEL_OP_2(0x80, 0x1d)
55 #define OP_MOD_SUB_OVERWRITE               BLE_MESH_MODEL_OP_2(0x80, 0x1e)
56 #define OP_MOD_SUB_STATUS                  BLE_MESH_MODEL_OP_2(0x80, 0x1f)
57 #define OP_MOD_SUB_VA_ADD                  BLE_MESH_MODEL_OP_2(0x80, 0x20)
58 #define OP_MOD_SUB_VA_DEL                  BLE_MESH_MODEL_OP_2(0x80, 0x21)
59 #define OP_MOD_SUB_VA_OVERWRITE            BLE_MESH_MODEL_OP_2(0x80, 0x22)
60 #define OP_NET_TRANSMIT_GET                BLE_MESH_MODEL_OP_2(0x80, 0x23)
61 #define OP_NET_TRANSMIT_SET                BLE_MESH_MODEL_OP_2(0x80, 0x24)
62 #define OP_NET_TRANSMIT_STATUS             BLE_MESH_MODEL_OP_2(0x80, 0x25)
63 #define OP_RELAY_GET                       BLE_MESH_MODEL_OP_2(0x80, 0x26)
64 #define OP_RELAY_SET                       BLE_MESH_MODEL_OP_2(0x80, 0x27)
65 #define OP_RELAY_STATUS                    BLE_MESH_MODEL_OP_2(0x80, 0x28)
66 #define OP_MOD_SUB_GET                     BLE_MESH_MODEL_OP_2(0x80, 0x29)
67 #define OP_MOD_SUB_LIST                    BLE_MESH_MODEL_OP_2(0x80, 0x2a)
68 #define OP_MOD_SUB_GET_VND                 BLE_MESH_MODEL_OP_2(0x80, 0x2b)
69 #define OP_MOD_SUB_LIST_VND                BLE_MESH_MODEL_OP_2(0x80, 0x2c)
70 #define OP_LPN_TIMEOUT_GET                 BLE_MESH_MODEL_OP_2(0x80, 0x2d)
71 #define OP_LPN_TIMEOUT_STATUS              BLE_MESH_MODEL_OP_2(0x80, 0x2e)
72 #define OP_HEALTH_FAULT_CLEAR              BLE_MESH_MODEL_OP_2(0x80, 0x2f)
73 #define OP_HEALTH_FAULT_CLEAR_UNREL        BLE_MESH_MODEL_OP_2(0x80, 0x30)
74 #define OP_HEALTH_FAULT_GET                BLE_MESH_MODEL_OP_2(0x80, 0x31)
75 #define OP_HEALTH_FAULT_TEST               BLE_MESH_MODEL_OP_2(0x80, 0x32)
76 #define OP_HEALTH_FAULT_TEST_UNREL         BLE_MESH_MODEL_OP_2(0x80, 0x33)
77 #define OP_HEALTH_PERIOD_GET               BLE_MESH_MODEL_OP_2(0x80, 0x34)
78 #define OP_HEALTH_PERIOD_SET               BLE_MESH_MODEL_OP_2(0x80, 0x35)
79 #define OP_HEALTH_PERIOD_SET_UNREL         BLE_MESH_MODEL_OP_2(0x80, 0x36)
80 #define OP_HEALTH_PERIOD_STATUS            BLE_MESH_MODEL_OP_2(0x80, 0x37)
81 #define OP_HEARTBEAT_PUB_GET               BLE_MESH_MODEL_OP_2(0x80, 0x38)
82 #define OP_HEARTBEAT_PUB_SET               BLE_MESH_MODEL_OP_2(0x80, 0x39)
83 #define OP_HEARTBEAT_SUB_GET               BLE_MESH_MODEL_OP_2(0x80, 0x3a)
84 #define OP_HEARTBEAT_SUB_SET               BLE_MESH_MODEL_OP_2(0x80, 0x3b)
85 #define OP_HEARTBEAT_SUB_STATUS            BLE_MESH_MODEL_OP_2(0x80, 0x3c)
86 #define OP_MOD_APP_BIND                    BLE_MESH_MODEL_OP_2(0x80, 0x3d)
87 #define OP_MOD_APP_STATUS                  BLE_MESH_MODEL_OP_2(0x80, 0x3e)
88 #define OP_MOD_APP_UNBIND                  BLE_MESH_MODEL_OP_2(0x80, 0x3f)
89 #define OP_NET_KEY_ADD                     BLE_MESH_MODEL_OP_2(0x80, 0x40)
90 #define OP_NET_KEY_DEL                     BLE_MESH_MODEL_OP_2(0x80, 0x41)
91 #define OP_NET_KEY_GET                     BLE_MESH_MODEL_OP_2(0x80, 0x42)
92 #define OP_NET_KEY_LIST                    BLE_MESH_MODEL_OP_2(0x80, 0x43)
93 #define OP_NET_KEY_STATUS                  BLE_MESH_MODEL_OP_2(0x80, 0x44)
94 #define OP_NET_KEY_UPDATE                  BLE_MESH_MODEL_OP_2(0x80, 0x45)
95 #define OP_NODE_IDENTITY_GET               BLE_MESH_MODEL_OP_2(0x80, 0x46)
96 #define OP_NODE_IDENTITY_SET               BLE_MESH_MODEL_OP_2(0x80, 0x47)
97 #define OP_NODE_IDENTITY_STATUS            BLE_MESH_MODEL_OP_2(0x80, 0x48)
98 #define OP_NODE_RESET                      BLE_MESH_MODEL_OP_2(0x80, 0x49)
99 #define OP_NODE_RESET_STATUS               BLE_MESH_MODEL_OP_2(0x80, 0x4a)
100 #define OP_SIG_MOD_APP_GET                 BLE_MESH_MODEL_OP_2(0x80, 0x4b)
101 #define OP_SIG_MOD_APP_LIST                BLE_MESH_MODEL_OP_2(0x80, 0x4c)
102 #define OP_VND_MOD_APP_GET                 BLE_MESH_MODEL_OP_2(0x80, 0x4d)
103 #define OP_VND_MOD_APP_LIST                BLE_MESH_MODEL_OP_2(0x80, 0x4e)
104 
105 #define STATUS_SUCCESS                     0x00
106 #define STATUS_INVALID_ADDRESS             0x01
107 #define STATUS_INVALID_MODEL               0x02
108 #define STATUS_INVALID_APPKEY              0x03
109 #define STATUS_INVALID_NETKEY              0x04
110 #define STATUS_INSUFF_RESOURCES            0x05
111 #define STATUS_IDX_ALREADY_STORED          0x06
112 #define STATUS_NVAL_PUB_PARAM              0x07
113 #define STATUS_NOT_SUB_MOD                 0x08
114 #define STATUS_STORAGE_FAIL                0x09
115 #define STATUS_FEAT_NOT_SUPP               0x0a
116 #define STATUS_CANNOT_UPDATE               0x0b
117 #define STATUS_CANNOT_REMOVE               0x0c
118 #define STATUS_CANNOT_BIND                 0x0d
119 #define STATUS_TEMP_STATE_CHG_FAIL         0x0e
120 #define STATUS_CANNOT_SET                  0x0f
121 #define STATUS_UNSPECIFIED                 0x10
122 #define STATUS_INVALID_BINDING             0x11
123 
124 enum {
125     BLE_MESH_VA_CHANGED,    /* Label information changed */
126 };
127 
128 struct label {
129     uint16_t ref;
130     uint16_t addr;
131     uint8_t  uuid[16];
132     bt_mesh_atomic_t flags[1];
133 };
134 
135 void bt_mesh_mod_sub_reset(bool store);
136 
137 void bt_mesh_cfg_reset(bool store);
138 
139 void bt_mesh_heartbeat(uint16_t src, uint16_t dst, uint8_t hops, uint16_t feat);
140 
141 void bt_mesh_attention(struct bt_mesh_model *model, uint8_t time);
142 
143 struct label *get_label(uint16_t index);
144 
145 uint8_t *bt_mesh_label_uuid_get(uint16_t addr);
146 
147 struct bt_mesh_hb_pub *bt_mesh_hb_pub_get(void);
148 void bt_mesh_hb_pub_disable(void);
149 struct bt_mesh_cfg_srv *bt_mesh_cfg_get(void);
150 
151 uint8_t bt_mesh_net_transmit_get(void);
152 uint8_t bt_mesh_relay_get(void);
153 uint8_t bt_mesh_friend_get(void);
154 uint8_t bt_mesh_relay_retransmit_get(void);
155 uint8_t bt_mesh_beacon_get(void);
156 uint8_t bt_mesh_gatt_proxy_get(void);
157 uint8_t bt_mesh_default_ttl_get(void);
158 
159 void bt_mesh_subnet_del(struct bt_mesh_subnet *sub, bool store);
160 
161 struct bt_mesh_app_key *bt_mesh_app_key_alloc(uint16_t app_idx);
162 void bt_mesh_app_key_del(struct bt_mesh_app_key *key, bool store);
163 
key_idx_pack(struct net_buf_simple * buf,uint16_t idx1,uint16_t idx2)164 static inline void key_idx_pack(struct net_buf_simple *buf,
165                                 uint16_t idx1, uint16_t idx2)
166 {
167     net_buf_simple_add_le16(buf, idx1 | ((idx2 & 0x00f) << 12));
168     net_buf_simple_add_u8(buf, idx2 >> 4);
169 }
170 
key_idx_unpack(struct net_buf_simple * buf,uint16_t * idx1,uint16_t * idx2)171 static inline void key_idx_unpack(struct net_buf_simple *buf,
172                                   uint16_t *idx1, uint16_t *idx2)
173 {
174     *idx1 = sys_get_le16(&buf->data[0]) & 0xfff;
175     *idx2 = sys_get_le16(&buf->data[1]) >> 4;
176     net_buf_simple_pull(buf, 3);
177 }
178 
179 #ifdef __cplusplus
180 }
181 #endif
182 
183 #endif /* _FOUNDATION_H_ */
184