1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361
| static const struct { void (*func)(const u8_t *data); u16_t len; } prov_handlers[] = { { prov_invite, 1 }, { prov_capabilities, 11 }, { prov_start, 5, }, { prov_pub_key, 64 }, { prov_input_complete, 0 }, { prov_confirm, 16 }, { prov_random, 16 }, { prov_data, 33 }, { prov_complete, 0 }, { prov_failed, 1 }, };
static void close_link(u8_t err, u8_t reason) { PROV_D(", 10--->0"); #if defined(CONFIG_BT_MESH_PB_GATT) if (link.conn) { bt_mesh_pb_gatt_close(link.conn); return; } #endif
#if defined(CONFIG_BT_MESH_PB_ADV) if (err) { prov_send_fail_msg(err); }
link.rx.seg = 0; bearer_ctl_send(LINK_CLOSE, &reason, sizeof(reason)); #endif
atomic_clear_bit(link.flags, LINK_ACTIVE);
if (link.conf_inputs[0]) { bt_mesh_attention(NULL, 0); } }
static void prov_retransmit(struct k_work *work) { int i;
BT_DBG("");
if (!atomic_test_bit(link.flags, LINK_ACTIVE)) { BT_WARN("Link not active"); return; }
if (k_uptime_get() - link.tx.start > TRANSACTION_TIMEOUT) { BT_WARN("Giving up transaction"); reset_link(); return; }
for (i = 0; i < ARRAY_SIZE(link.tx.buf); i++) { struct net_buf *buf = link.tx.buf[i];
if (!buf) { break; }
if (BT_MESH_ADV(buf)->busy) { continue; }
BT_DBG("%u bytes: %s", buf->len, bt_hex(buf->data, buf->len));
if (i + 1 < ARRAY_SIZE(link.tx.buf) && link.tx.buf[i + 1]) { bt_mesh_adv_send(buf, NULL, NULL); } else { bt_mesh_adv_send(buf, &buf_sent_cb, NULL); }
} }
static void link_open(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len);
if (buf->len < 16) { BT_ERR("Too short bearer open message (len %u)", buf->len); return; }
if (atomic_test_bit(link.flags, LINK_ACTIVE)) { BT_WARN("Ignoring bearer open: link already active"); return; }
if (memcmp(buf->data, prov->uuid, 16)) { BT_DBG("Bearer open message not for us"); return; }
PROV_D("link_id: 0x%08x, 0--->1", rx->link_id); if (prov->link_open) { prov->link_open(BT_MESH_PROV_ADV); }
link.id = rx->link_id; atomic_set_bit(link.flags, LINK_ACTIVE); net_buf_simple_init(link.rx.buf, 0);
bearer_ctl_send(LINK_ACK, NULL, 0);
genie_event(GENIE_EVT_SDK_MESH_PROV_START, &rx->link_id);
link.expect = PROV_INVITE; }
static void link_ack(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len); }
static void link_close(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len); reset_link(); }
static void gen_prov_ctl(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("op 0x%02x len %u", BEARER_CTL(rx->gpc), buf->len);
switch (BEARER_CTL(rx->gpc)) { case LINK_OPEN: link_open(rx, buf); break; case LINK_ACK: if (!atomic_test_bit(link.flags, LINK_ACTIVE)) { return; }
link_ack(rx, buf); break; case LINK_CLOSE: if (!atomic_test_bit(link.flags, LINK_ACTIVE)) { return; }
link_close(rx, buf); break; default: BT_ERR("Unknown bearer opcode: 0x%02x", BEARER_CTL(rx->gpc)); return; } }
static void prov_msg_recv(void) { u8_t type = link.rx.buf->data[0];
BT_DBG("type 0x%02x len %u", type, link.rx.buf->len); BT_DBG("data %s", bt_hex(link.rx.buf->data, link.rx.buf->len));
if (!bt_mesh_fcs_check(link.rx.buf, link.rx.fcs)) { BT_ERR("Incorrect FCS"); return; }
gen_prov_ack_send(link.rx.id); link.rx.prev_id = link.rx.id; link.rx.id = 0;
if (type != PROV_FAILED && type != link.expect) { BT_WARN("Unexpected msg 0x%02x != 0x%02x", type, link.expect); prov_send_fail_msg(PROV_ERR_UNEXP_PDU); link.expect = PROV_FAILED; return; }
if (type >= ARRAY_SIZE(prov_handlers)) { BT_ERR("Unknown provisioning PDU type 0x%02x", type); close_link(PROV_ERR_NVAL_PDU, CLOSE_REASON_FAILED); return; }
if (1 + prov_handlers[type].len != link.rx.buf->len) { BT_ERR("Invalid length %u for type 0x%02x", link.rx.buf->len, type); close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); return; }
prov_handlers[type].func(&link.rx.buf->data[1]); }
static void gen_prov_cont(struct prov_rx *rx, struct net_buf_simple *buf) { u8_t seg = CONT_SEG_INDEX(rx->gpc);
BT_DBG("len %u, seg_index %u", buf->len, seg);
if (!link.rx.seg && link.rx.prev_id == rx->xact_id) { BT_WARN("Resending ack"); gen_prov_ack_send(rx->xact_id); return; }
if (rx->xact_id != link.rx.id) { BT_WARN("Data for unknown transaction (%u != %u)", rx->xact_id, link.rx.id); return; }
if (seg > link.rx.last_seg) { BT_ERR("Invalid segment index %u", seg); close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); return; } else if (seg == link.rx.last_seg) { u8_t expect_len;
expect_len = (link.rx.buf->len - 20 - (23 * (link.rx.last_seg - 1))); if (expect_len != buf->len) { BT_ERR("Incorrect last seg len: %u != %u", expect_len, buf->len); close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); return; } }
if (!(link.rx.seg & MESH_BIT(seg))) { BT_WARN("Ignoring already received segment"); return; }
memcpy(XACT_SEG_DATA(seg), buf->data, buf->len); XACT_SEG_RECV(seg);
if (!link.rx.seg) { prov_msg_recv(); } }
static void gen_prov_ack(struct prov_rx *rx, struct net_buf_simple *buf) { BT_DBG("len %u", buf->len);
if (!link.tx.buf[0]) { return; }
if (rx->xact_id == link.tx.id) { prov_clear_tx(); } }
static void gen_prov_start(struct prov_rx *rx, struct net_buf_simple *buf) { if (link.rx.seg) { BT_WARN("Got Start while there are unreceived segments"); return; }
if (link.rx.prev_id == rx->xact_id) { BT_WARN("Resending ack"); gen_prov_ack_send(rx->xact_id); return; }
link.rx.buf->len = net_buf_simple_pull_be16(buf); link.rx.id = rx->xact_id; link.rx.fcs = net_buf_simple_pull_u8(buf);
BT_DBG("len %u last_seg %u total_len %u fcs 0x%02x", buf->len, START_LAST_SEG(rx->gpc), link.rx.buf->len, link.rx.fcs);
if (link.rx.buf->len < 1) { BT_ERR("Ignoring zero-length provisioning PDU"); close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); return; }
if (link.rx.buf->len > link.rx.buf->size) { BT_ERR("Too large provisioning PDU (%u bytes)", link.rx.buf->len); close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); return; }
if (START_LAST_SEG(rx->gpc) > 0 && link.rx.buf->len <= 20) { BT_ERR("Too small total length for multi-segment PDU"); close_link(PROV_ERR_NVAL_FMT, CLOSE_REASON_FAILED); return; }
link.rx.seg = (1 << (START_LAST_SEG(rx->gpc) + 1)) - 1; link.rx.last_seg = START_LAST_SEG(rx->gpc); memcpy(link.rx.buf->data, buf->data, buf->len); XACT_SEG_RECV(0);
if (!link.rx.seg) { prov_msg_recv(); } }
static const struct { void (*const func)(struct prov_rx *rx, struct net_buf_simple *buf); const u8_t require_link; const u8_t min_len; } gen_prov[] = { { gen_prov_start, true, 3 }, { gen_prov_ack, true, 0 }, { gen_prov_cont, true, 0 }, { gen_prov_ctl, false, 0 }, };
static void gen_prov_recv(struct prov_rx *rx, struct net_buf_simple *buf) { if (buf->len < gen_prov[GPCF(rx->gpc)].min_len) { BT_ERR("Too short GPC message type %u", GPCF(rx->gpc)); return; }
if (!atomic_test_bit(link.flags, LINK_ACTIVE) && gen_prov[GPCF(rx->gpc)].require_link) { BT_DBG("Ignoring message that requires active link"); return; }
gen_prov[GPCF(rx->gpc)].func(rx, buf); }
void bt_mesh_pb_adv_recv(struct net_buf_simple *buf) { struct prov_rx rx; if (!bt_prov_active() && bt_mesh_is_provisioned()) { BT_DBG("Ignoring provisioning PDU - already provisioned"); return; }
if (buf->len < 6) { BT_WARN("Too short provisioning packet (len %u)", buf->len); return; }
rx.link_id = net_buf_simple_pull_be32(buf); rx.xact_id = net_buf_simple_pull_u8(buf); rx.gpc = net_buf_simple_pull_u8(buf);
BT_DBG("link_id 0x%08x xact_id %u", rx.link_id, rx.xact_id);
if (atomic_test_bit(link.flags, LINK_ACTIVE) && link.id != rx.link_id) { BT_DBG("Ignoring mesh beacon for unknown link"); return; }
gen_prov_recv(&rx, buf); }
|