mirror of
https://github.com/bluekitchen/btstack.git
synced 2025-02-04 15:39:59 +00:00
826 lines
28 KiB
C
826 lines
28 KiB
C
/*
|
|
* Copyright (C) 2019 BlueKitchen GmbH
|
|
*
|
|
* Redistribution and use in source and binary forms, with or without
|
|
* modification, are permitted provided that the following conditions
|
|
* are met:
|
|
*
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
* notice, this list of conditions and the following disclaimer.
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
* documentation and/or other materials provided with the distribution.
|
|
* 3. Neither the name of the copyright holders nor the names of
|
|
* contributors may be used to endorse or promote products derived
|
|
* from this software without specific prior written permission.
|
|
* 4. Any redistribution, use, or modification is done solely for
|
|
* personal benefit and not for any commercial purpose or for
|
|
* monetary gain.
|
|
*
|
|
* THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
|
|
* RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
|
|
* THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
* SUCH DAMAGE.
|
|
*
|
|
* Please inquire about commercial licensing options at
|
|
* contact@bluekitchen-gmbh.com
|
|
*
|
|
*/
|
|
|
|
#define __BTSTACK_FILE__ "mesh_access.c"
|
|
|
|
#include <string.h>
|
|
#include <stdio.h>
|
|
#include <stdarg.h>
|
|
#include "mesh_access.h"
|
|
#include "btstack_memory.h"
|
|
#include "btstack_debug.h"
|
|
#include "bluetooth_company_id.h"
|
|
#include "mesh_transport.h"
|
|
#include "mesh_foundation.h"
|
|
#include "btstack_tlv.h"
|
|
|
|
static void mesh_access_message_process_handler(mesh_pdu_t * pdu);
|
|
|
|
static mesh_element_t primary_element;
|
|
|
|
static btstack_linked_list_t mesh_elements;
|
|
|
|
static uint16_t mid_counter;
|
|
|
|
static const btstack_tlv_t * btstack_tlv_singleton_impl;
|
|
static void * btstack_tlv_singleton_context;
|
|
|
|
static void mesh_access_setup_tlv(void){
|
|
if (btstack_tlv_singleton_impl) return;
|
|
btstack_tlv_get_instance(&btstack_tlv_singleton_impl, &btstack_tlv_singleton_context);
|
|
}
|
|
|
|
void mesh_access_init(void){
|
|
// Access layer - add Primary Element to list of elements
|
|
mesh_element_add(&primary_element);
|
|
|
|
// register with upper transport
|
|
mesh_upper_transport_register_access_message_handler(&mesh_access_message_process_handler);
|
|
}
|
|
|
|
mesh_element_t * mesh_primary_element(void){
|
|
return &primary_element;
|
|
}
|
|
|
|
void mesh_access_set_primary_element_address(uint16_t unicast_address){
|
|
primary_element.unicast_address = unicast_address;
|
|
}
|
|
|
|
uint16_t mesh_access_get_primary_element_address(void){
|
|
return primary_element.unicast_address;
|
|
}
|
|
|
|
void mesh_access_set_primary_element_location(uint16_t location){
|
|
primary_element.loc = location;
|
|
}
|
|
|
|
void mesh_element_add(mesh_element_t * element){
|
|
btstack_linked_list_add_tail(&mesh_elements, (void*) element);
|
|
}
|
|
|
|
mesh_element_t * mesh_element_for_unicast_address(uint16_t unicast_address){
|
|
btstack_linked_list_iterator_t it;
|
|
btstack_linked_list_iterator_init(&it, &mesh_elements);
|
|
while (btstack_linked_list_iterator_has_next(&it)){
|
|
mesh_element_t * element = (mesh_element_t *) btstack_linked_list_iterator_next(&it);
|
|
if (element->unicast_address != unicast_address) continue;
|
|
return element;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
// Model Identifier utilities
|
|
|
|
uint32_t mesh_model_get_model_identifier(uint16_t vendor_id, uint16_t model_id){
|
|
return (vendor_id << 16) | model_id;
|
|
}
|
|
|
|
uint32_t mesh_model_get_model_identifier_bluetooth_sig(uint16_t model_id){
|
|
return (BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC << 16) | model_id;
|
|
}
|
|
|
|
uint16_t mesh_model_get_model_id(uint32_t model_identifier){
|
|
return model_identifier & 0xFFFFu;
|
|
}
|
|
|
|
uint16_t mesh_model_get_vendor_id(uint32_t model_identifier){
|
|
return model_identifier >> 16;
|
|
}
|
|
|
|
int mesh_model_is_bluetooth_sig(uint32_t model_identifier){
|
|
return mesh_model_get_vendor_id(model_identifier) == BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC;
|
|
}
|
|
|
|
mesh_model_t * mesh_model_get_configuration_server(void){
|
|
return mesh_model_get_by_identifier(&primary_element, mesh_model_get_model_identifier_bluetooth_sig(MESH_SIG_MODEL_ID_CONFIGURATION_SERVER));
|
|
}
|
|
|
|
void mesh_element_add_model(mesh_element_t * element, mesh_model_t * mesh_model){
|
|
if (mesh_model_is_bluetooth_sig(mesh_model->model_identifier)){
|
|
element->models_count_sig++;
|
|
} else {
|
|
element->models_count_vendor++;
|
|
}
|
|
mesh_model->mid = mid_counter++;
|
|
mesh_model->element = element;
|
|
btstack_linked_list_add_tail(&element->models, (btstack_linked_item_t *) mesh_model);
|
|
}
|
|
|
|
void mesh_model_iterator_init(mesh_model_iterator_t * iterator, mesh_element_t * element){
|
|
btstack_linked_list_iterator_init(&iterator->it, &element->models);
|
|
}
|
|
|
|
int mesh_model_iterator_has_next(mesh_model_iterator_t * iterator){
|
|
return btstack_linked_list_iterator_has_next(&iterator->it);
|
|
}
|
|
|
|
mesh_model_t * mesh_model_iterator_next(mesh_model_iterator_t * iterator){
|
|
return (mesh_model_t *) btstack_linked_list_iterator_next(&iterator->it);
|
|
}
|
|
|
|
void mesh_element_iterator_init(mesh_element_iterator_t * iterator){
|
|
btstack_linked_list_iterator_init(&iterator->it, &mesh_elements);
|
|
}
|
|
|
|
int mesh_element_iterator_has_next(mesh_element_iterator_t * iterator){
|
|
return btstack_linked_list_iterator_has_next(&iterator->it);
|
|
}
|
|
|
|
mesh_element_t * mesh_element_iterator_next(mesh_element_iterator_t * iterator){
|
|
return (mesh_element_t *) btstack_linked_list_iterator_next(&iterator->it);
|
|
}
|
|
|
|
mesh_model_t * mesh_model_get_by_identifier(mesh_element_t * element, uint32_t model_identifier){
|
|
mesh_model_iterator_t it;
|
|
mesh_model_iterator_init(&it, element);
|
|
while (mesh_model_iterator_has_next(&it)){
|
|
mesh_model_t * model = mesh_model_iterator_next(&it);
|
|
if (model->model_identifier != model_identifier) continue;
|
|
return model;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
mesh_model_t * mesh_access_model_for_address_and_model_identifier(uint16_t element_address, uint32_t model_identifier, uint8_t * status){
|
|
mesh_element_t * element = mesh_element_for_unicast_address(element_address);
|
|
if (element == NULL){
|
|
*status = MESH_FOUNDATION_STATUS_INVALID_ADDRESS;
|
|
return NULL;
|
|
}
|
|
mesh_model_t * model = mesh_model_get_by_identifier(element, model_identifier);
|
|
if (model == NULL) {
|
|
*status = MESH_FOUNDATION_STATUS_INVALID_MODEL;
|
|
} else {
|
|
*status = MESH_FOUNDATION_STATUS_SUCCESS;
|
|
}
|
|
return model;
|
|
}
|
|
|
|
uint16_t mesh_pdu_src(mesh_pdu_t * pdu){
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
return mesh_transport_src((mesh_transport_pdu_t*) pdu);
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
return mesh_network_src((mesh_network_pdu_t *) pdu);
|
|
default:
|
|
return MESH_ADDRESS_UNSASSIGNED;
|
|
}
|
|
}
|
|
|
|
uint16_t mesh_pdu_dst(mesh_pdu_t * pdu){
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
return mesh_transport_dst((mesh_transport_pdu_t*) pdu);
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
return mesh_network_dst((mesh_network_pdu_t *) pdu);
|
|
default:
|
|
return MESH_ADDRESS_UNSASSIGNED;
|
|
}
|
|
}
|
|
|
|
uint16_t mesh_pdu_netkey_index(mesh_pdu_t * pdu){
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
return ((mesh_transport_pdu_t*) pdu)->netkey_index;
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
return ((mesh_network_pdu_t *) pdu)->netkey_index;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
uint16_t mesh_pdu_appkey_index(mesh_pdu_t * pdu){
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
return ((mesh_transport_pdu_t*) pdu)->appkey_index;
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
return ((mesh_network_pdu_t *) pdu)->appkey_index;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
uint16_t mesh_pdu_len(mesh_pdu_t * pdu){
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
return ((mesh_transport_pdu_t*) pdu)->len;
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
return ((mesh_network_pdu_t *) pdu)->len - 10;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
uint8_t * mesh_pdu_data(mesh_pdu_t * pdu){
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
return ((mesh_transport_pdu_t*) pdu)->data;
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
return &((mesh_network_pdu_t *) pdu)->data[10];
|
|
default:
|
|
return NULL;
|
|
}
|
|
}
|
|
|
|
// message parser
|
|
|
|
static int mesh_access_get_opcode(uint8_t * buffer, uint16_t buffer_size, uint32_t * opcode, uint16_t * opcode_size){
|
|
switch (buffer[0] >> 6){
|
|
case 0:
|
|
case 1:
|
|
if (buffer[0] == 0x7f) return 0;
|
|
*opcode = buffer[0];
|
|
*opcode_size = 1;
|
|
return 1;
|
|
case 2:
|
|
if (buffer_size < 2) return 0;
|
|
*opcode = big_endian_read_16(buffer, 0);
|
|
*opcode_size = 2;
|
|
return 1;
|
|
case 3:
|
|
if (buffer_size < 3) return 0;
|
|
*opcode = (buffer[0] << 16) | little_endian_read_16(buffer, 1);
|
|
*opcode_size = 3;
|
|
return 1;
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
static int mesh_access_transport_get_opcode(mesh_transport_pdu_t * transport_pdu, uint32_t * opcode, uint16_t * opcode_size){
|
|
return mesh_access_get_opcode(transport_pdu->data, transport_pdu->len, opcode, opcode_size);
|
|
}
|
|
|
|
static int mesh_access_network_get_opcode(mesh_network_pdu_t * network_pdu, uint32_t * opcode, uint16_t * opcode_size){
|
|
// TransMIC already removed by mesh_upper_transport_validate_unsegmented_message_ccm
|
|
return mesh_access_get_opcode(&network_pdu->data[10], network_pdu->len - 10, opcode, opcode_size);
|
|
}
|
|
|
|
int mesh_access_pdu_get_opcode(mesh_pdu_t * pdu, uint32_t * opcode, uint16_t * opcode_size){
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
return mesh_access_transport_get_opcode((mesh_transport_pdu_t*) pdu, opcode, opcode_size);
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
return mesh_access_network_get_opcode((mesh_network_pdu_t *) pdu, opcode, opcode_size);
|
|
default:
|
|
return 0;
|
|
}
|
|
}
|
|
|
|
void mesh_access_parser_skip(mesh_access_parser_state_t * state, uint16_t bytes_to_skip){
|
|
state->data += bytes_to_skip;
|
|
state->len -= bytes_to_skip;
|
|
}
|
|
|
|
int mesh_access_parser_init(mesh_access_parser_state_t * state, mesh_pdu_t * pdu){
|
|
state->data = mesh_pdu_data(pdu);
|
|
state->len = mesh_pdu_len(pdu);
|
|
|
|
uint16_t opcode_size = 0;
|
|
int ok = mesh_access_get_opcode(state->data, state->len, &state->opcode, &opcode_size);
|
|
if (ok){
|
|
mesh_access_parser_skip(state, opcode_size);
|
|
}
|
|
return ok;
|
|
}
|
|
|
|
uint16_t mesh_access_parser_available(mesh_access_parser_state_t * state){
|
|
return state->len;
|
|
}
|
|
|
|
uint8_t mesh_access_parser_get_u8(mesh_access_parser_state_t * state){
|
|
uint8_t value = *state->data;
|
|
mesh_access_parser_skip(state, 1);
|
|
return value;
|
|
}
|
|
|
|
uint16_t mesh_access_parser_get_u16(mesh_access_parser_state_t * state){
|
|
uint16_t value = little_endian_read_16(state->data, 0);
|
|
mesh_access_parser_skip(state, 2);
|
|
return value;
|
|
}
|
|
|
|
uint32_t mesh_access_parser_get_u24(mesh_access_parser_state_t * state){
|
|
uint32_t value = little_endian_read_24(state->data, 0);
|
|
mesh_access_parser_skip(state, 3);
|
|
return value;
|
|
}
|
|
|
|
uint32_t mesh_access_parser_get_u32(mesh_access_parser_state_t * state){
|
|
uint32_t value = little_endian_read_24(state->data, 0);
|
|
mesh_access_parser_skip(state, 4);
|
|
return value;
|
|
}
|
|
|
|
void mesh_access_parser_get_u128(mesh_access_parser_state_t * state, uint8_t * dest){
|
|
reverse_128( state->data, dest);
|
|
mesh_access_parser_skip(state, 16);
|
|
}
|
|
|
|
void mesh_access_parser_get_label_uuid(mesh_access_parser_state_t * state, uint8_t * dest){
|
|
memcpy( dest, state->data, 16);
|
|
mesh_access_parser_skip(state, 16);
|
|
}
|
|
|
|
void mesh_access_parser_get_key(mesh_access_parser_state_t * state, uint8_t * dest){
|
|
memcpy( dest, state->data, 16);
|
|
mesh_access_parser_skip(state, 16);
|
|
}
|
|
|
|
uint32_t mesh_access_parser_get_model_identifier(mesh_access_parser_state_t * parser){
|
|
if (mesh_access_parser_available(parser) == 4){
|
|
return mesh_access_parser_get_u32(parser);
|
|
} else {
|
|
return (BLUETOOTH_COMPANY_ID_BLUETOOTH_SIG_INC << 16) | mesh_access_parser_get_u16(parser);
|
|
}
|
|
}
|
|
|
|
// Mesh Access Message Builder
|
|
|
|
// message builder
|
|
|
|
static int mesh_access_setup_opcode(uint8_t * buffer, uint32_t opcode){
|
|
if (opcode < 0x100){
|
|
buffer[0] = opcode;
|
|
return 1;
|
|
}
|
|
if (opcode < 0x10000){
|
|
big_endian_store_16(buffer, 0, opcode);
|
|
return 2;
|
|
}
|
|
buffer[0] = opcode >> 16;
|
|
little_endian_store_16(buffer, 1, opcode & 0xffff);
|
|
return 3;
|
|
}
|
|
|
|
mesh_transport_pdu_t * mesh_access_transport_init(uint32_t opcode){
|
|
mesh_transport_pdu_t * pdu = mesh_transport_pdu_get();
|
|
if (!pdu) return NULL;
|
|
|
|
pdu->len = mesh_access_setup_opcode(pdu->data, opcode);
|
|
return pdu;
|
|
}
|
|
|
|
void mesh_access_transport_add_uint8(mesh_transport_pdu_t * pdu, uint8_t value){
|
|
pdu->data[pdu->len++] = value;
|
|
}
|
|
|
|
void mesh_access_transport_add_uint16(mesh_transport_pdu_t * pdu, uint16_t value){
|
|
little_endian_store_16(pdu->data, pdu->len, value);
|
|
pdu->len += 2;
|
|
}
|
|
|
|
void mesh_access_transport_add_uint24(mesh_transport_pdu_t * pdu, uint32_t value){
|
|
little_endian_store_24(pdu->data, pdu->len, value);
|
|
pdu->len += 3;
|
|
}
|
|
|
|
void mesh_access_transport_add_uint32(mesh_transport_pdu_t * pdu, uint32_t value){
|
|
little_endian_store_32(pdu->data, pdu->len, value);
|
|
pdu->len += 4;
|
|
}
|
|
void mesh_access_transport_add_model_identifier(mesh_transport_pdu_t * pdu, uint32_t model_identifier){
|
|
if (mesh_model_is_bluetooth_sig(model_identifier)){
|
|
mesh_access_transport_add_uint16( pdu, mesh_model_get_model_id(model_identifier) );
|
|
} else {
|
|
mesh_access_transport_add_uint32( pdu, model_identifier );
|
|
}
|
|
}
|
|
|
|
mesh_network_pdu_t * mesh_access_network_init(uint32_t opcode){
|
|
mesh_network_pdu_t * pdu = mesh_network_pdu_get();
|
|
if (!pdu) return NULL;
|
|
|
|
pdu->len = mesh_access_setup_opcode(&pdu->data[10], opcode) + 10;
|
|
return pdu;
|
|
}
|
|
|
|
void mesh_access_network_add_uint8(mesh_network_pdu_t * pdu, uint8_t value){
|
|
pdu->data[pdu->len++] = value;
|
|
}
|
|
|
|
void mesh_access_network_add_uint16(mesh_network_pdu_t * pdu, uint16_t value){
|
|
little_endian_store_16(pdu->data, pdu->len, value);
|
|
pdu->len += 2;
|
|
}
|
|
|
|
void mesh_access_network_add_uint24(mesh_network_pdu_t * pdu, uint16_t value){
|
|
little_endian_store_24(pdu->data, pdu->len, value);
|
|
pdu->len += 3;
|
|
}
|
|
|
|
void mesh_access_network_add_uint32(mesh_network_pdu_t * pdu, uint16_t value){
|
|
little_endian_store_32(pdu->data, pdu->len, value);
|
|
pdu->len += 4;
|
|
}
|
|
|
|
void mesh_access_network_add_model_identifier(mesh_network_pdu_t * pdu, uint32_t model_identifier){
|
|
if (mesh_model_is_bluetooth_sig(model_identifier)){
|
|
mesh_access_network_add_uint16( pdu, mesh_model_get_model_id(model_identifier) );
|
|
} else {
|
|
mesh_access_network_add_uint32( pdu, model_identifier );
|
|
}
|
|
}
|
|
|
|
// access message template
|
|
|
|
mesh_network_pdu_t * mesh_access_setup_unsegmented_message(const mesh_access_message_t *template, ...){
|
|
mesh_network_pdu_t * network_pdu = mesh_access_network_init(template->opcode);
|
|
if (!network_pdu) return NULL;
|
|
|
|
va_list argptr;
|
|
va_start(argptr, template);
|
|
|
|
// add params
|
|
const char * format = template->format;
|
|
uint16_t word;
|
|
uint32_t longword;
|
|
while (*format){
|
|
switch (*format){
|
|
case '1':
|
|
word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
|
|
mesh_access_network_add_uint8( network_pdu, word);
|
|
break;
|
|
case '2':
|
|
word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
|
|
mesh_access_network_add_uint16( network_pdu, word);
|
|
break;
|
|
case '3':
|
|
longword = va_arg(argptr, uint32_t);
|
|
mesh_access_network_add_uint24( network_pdu, longword);
|
|
break;
|
|
case '4':
|
|
longword = va_arg(argptr, uint32_t);
|
|
mesh_access_network_add_uint32( network_pdu, longword);
|
|
break;
|
|
case 'm':
|
|
longword = va_arg(argptr, uint32_t);
|
|
mesh_access_network_add_model_identifier( network_pdu, longword);
|
|
break;
|
|
default:
|
|
log_error("Unsupported mesh message format specifier '%c", *format);
|
|
break;
|
|
}
|
|
format++;
|
|
}
|
|
|
|
va_end(argptr);
|
|
|
|
return network_pdu;
|
|
}
|
|
|
|
mesh_transport_pdu_t * mesh_access_setup_segmented_message(const mesh_access_message_t *template, ...){
|
|
mesh_transport_pdu_t * transport_pdu = mesh_access_transport_init(template->opcode);
|
|
if (!transport_pdu) return NULL;
|
|
|
|
va_list argptr;
|
|
va_start(argptr, template);
|
|
|
|
// add params
|
|
const char * format = template->format;
|
|
uint16_t word;
|
|
uint32_t longword;
|
|
while (*format){
|
|
switch (*format++){
|
|
case '1':
|
|
word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
|
|
mesh_access_transport_add_uint8( transport_pdu, word);
|
|
break;
|
|
case '2':
|
|
word = va_arg(argptr, int); // minimal va_arg is int: 2 bytes on 8+16 bit CPUs
|
|
mesh_access_transport_add_uint16( transport_pdu, word);
|
|
break;
|
|
case '3':
|
|
longword = va_arg(argptr, uint32_t);
|
|
mesh_access_transport_add_uint24( transport_pdu, longword);
|
|
break;
|
|
case '4':
|
|
longword = va_arg(argptr, uint32_t);
|
|
mesh_access_transport_add_uint32( transport_pdu, longword);
|
|
break;
|
|
case 'm':
|
|
longword = va_arg(argptr, uint32_t);
|
|
mesh_access_transport_add_model_identifier( transport_pdu, longword);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
}
|
|
|
|
va_end(argptr);
|
|
|
|
return transport_pdu;
|
|
}
|
|
|
|
|
|
static const mesh_operation_t * mesh_model_lookup_operation(mesh_model_t * model, mesh_pdu_t * pdu){
|
|
|
|
uint32_t opcode = 0;
|
|
uint16_t opcode_size = 0;
|
|
int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size);
|
|
if (!ok) return NULL;
|
|
|
|
uint16_t len = mesh_pdu_len(pdu);
|
|
|
|
// find opcode in table
|
|
const mesh_operation_t * operation = model->operations;
|
|
if (operation == NULL) return NULL;
|
|
for ( ; operation->handler != NULL ; operation++){
|
|
if (operation->opcode != opcode) continue;
|
|
if ((opcode_size + operation->minimum_length) > len) continue;
|
|
return operation;
|
|
}
|
|
return NULL;
|
|
}
|
|
|
|
static int mesh_access_validate_appkey_index(mesh_model_t * model, uint16_t appkey_index){
|
|
// DeviceKey is valid for all models
|
|
if (appkey_index == MESH_DEVICE_KEY_INDEX) return 1;
|
|
// check if AppKey that is bound to this particular model
|
|
return mesh_model_contains_appkey(model, appkey_index);
|
|
}
|
|
|
|
static void mesh_access_message_process_handler(mesh_pdu_t * pdu){
|
|
// get opcode and size
|
|
uint32_t opcode = 0;
|
|
uint16_t opcode_size = 0;
|
|
|
|
int ok = mesh_access_pdu_get_opcode( pdu, &opcode, &opcode_size);
|
|
if (!ok) {
|
|
mesh_access_message_processed(pdu);
|
|
return;
|
|
}
|
|
|
|
uint16_t len = mesh_pdu_len(pdu);
|
|
printf("MESH Access Message, Opcode = %x: ", opcode);
|
|
switch (pdu->pdu_type){
|
|
case MESH_PDU_TYPE_NETWORK:
|
|
printf_hexdump(&((mesh_network_pdu_t *) pdu)->data[10], len);
|
|
break;
|
|
case MESH_PDU_TYPE_TRANSPORT:
|
|
printf_hexdump(((mesh_transport_pdu_t *) pdu)->data, len);
|
|
break;
|
|
default:
|
|
break;
|
|
}
|
|
|
|
uint16_t dst = mesh_pdu_dst(pdu);
|
|
uint16_t appkey_index = mesh_pdu_appkey_index(pdu);
|
|
if (mesh_network_address_unicast(dst)){
|
|
// loookup element by unicast address
|
|
mesh_element_t * element = mesh_element_for_unicast_address(dst);
|
|
if (element != NULL){
|
|
// iterate over models, look for operation
|
|
mesh_model_iterator_t model_it;
|
|
mesh_model_iterator_init(&model_it, element);
|
|
while (mesh_model_iterator_has_next(&model_it)){
|
|
mesh_model_t * model = mesh_model_iterator_next(&model_it);
|
|
// find opcode in table
|
|
const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu);
|
|
if (operation == NULL) break;
|
|
if (mesh_access_validate_appkey_index(model, appkey_index) == 0) break;
|
|
operation->handler(model, pdu);
|
|
return;
|
|
}
|
|
}
|
|
} else {
|
|
// iterate over all elements / models, check subscription list
|
|
mesh_element_iterator_t it;
|
|
mesh_element_iterator_init(&it);
|
|
while (mesh_element_iterator_has_next(&it)){
|
|
mesh_element_t * element = (mesh_element_t *) mesh_element_iterator_next(&it);
|
|
mesh_model_iterator_t model_it;
|
|
mesh_model_iterator_init(&model_it, element);
|
|
while (mesh_model_iterator_has_next(&model_it)){
|
|
mesh_model_t * model = mesh_model_iterator_next(&model_it);
|
|
if (mesh_model_contains_subscription(model, dst)){
|
|
// find opcode in table
|
|
const mesh_operation_t * operation = mesh_model_lookup_operation(model, pdu);
|
|
if (operation == NULL) break;
|
|
if (mesh_access_validate_appkey_index(model, appkey_index) == 0) break;
|
|
operation->handler(model, pdu);
|
|
return;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// operation not found -> done
|
|
printf("Message not handled\n");
|
|
mesh_access_message_processed(pdu);
|
|
}
|
|
|
|
void mesh_access_message_processed(mesh_pdu_t * pdu){
|
|
mesh_upper_transport_message_processed_by_higher_layer(pdu);
|
|
}
|
|
|
|
int mesh_model_contains_subscription(mesh_model_t * mesh_model, uint16_t address){
|
|
int i;
|
|
for (i=0;i<MAX_NR_MESH_SUBSCRIPTION_PER_MODEL;i++){
|
|
if (mesh_model->subscriptions[i] == address) return 1;
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
static uint32_t mesh_transport_key_tag_for_internal_index(uint16_t internal_index){
|
|
return ((uint32_t) 'M' << 24) | ((uint32_t) 'A' << 16) | ((uint32_t) internal_index);
|
|
}
|
|
|
|
// Mesh App Keys
|
|
|
|
typedef struct {
|
|
uint16_t netkey_index;
|
|
uint16_t appkey_index;
|
|
uint8_t aid;
|
|
uint8_t key[16];
|
|
} mesh_persistent_app_key_t;
|
|
|
|
void mesh_store_app_key(uint16_t internal_index, uint16_t netkey_index, uint16_t appkey_index, uint8_t aid, const uint8_t * application_key){
|
|
mesh_access_setup_tlv();
|
|
|
|
mesh_persistent_app_key_t data;
|
|
printf("Store AppKey: internal index 0x%x, AppKey Index 0x%06x, AID %02x: ", internal_index, appkey_index, aid);
|
|
printf_hexdump(application_key, 16);
|
|
uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index);
|
|
data.netkey_index = netkey_index;
|
|
data.appkey_index = appkey_index;
|
|
data.aid = aid;
|
|
memcpy(data.key, application_key, 16);
|
|
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
|
|
}
|
|
|
|
void mesh_delete_app_key(uint16_t internal_index){
|
|
mesh_access_setup_tlv();
|
|
|
|
uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index);
|
|
btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag);
|
|
}
|
|
|
|
void mesh_load_app_keys(void){
|
|
mesh_access_setup_tlv();
|
|
printf("Load App Keys\n");
|
|
uint16_t internal_index;
|
|
for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){
|
|
mesh_persistent_app_key_t data;
|
|
uint32_t tag = mesh_transport_key_tag_for_internal_index(internal_index);
|
|
int app_key_len = btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &data, sizeof(data));
|
|
if (app_key_len == 0) return;
|
|
|
|
mesh_transport_key_t * key = btstack_memory_mesh_transport_key_get();
|
|
if (key == NULL) return;
|
|
|
|
key->internal_index = internal_index;
|
|
key->appkey_index = data.appkey_index;
|
|
key->netkey_index = data.netkey_index;
|
|
key->aid = data.aid;
|
|
key->akf = 1;
|
|
memcpy(key->key, data.key, 16);
|
|
mesh_transport_key_add(key);
|
|
printf("Load AppKey: internal index 0x%x, AppKey Index 0x%06x, , AID %02x: ", key->internal_index, key->appkey_index, key->aid);
|
|
printf_hexdump(key->key, 16);
|
|
}
|
|
}
|
|
|
|
void mesh_delete_app_keys(void){
|
|
printf("Delete App Keys\n");
|
|
|
|
uint16_t internal_index;
|
|
for (internal_index = 0; internal_index < MAX_NR_MESH_TRANSPORT_KEYS; internal_index++){
|
|
mesh_delete_app_key(internal_index);
|
|
}
|
|
}
|
|
|
|
|
|
// Model to Appkey List
|
|
|
|
static uint32_t mesh_model_tag_for_index(uint16_t internal_model_id){
|
|
return ((uint32_t) 'M' << 24) | ((uint32_t) 'B' << 16) | ((uint32_t) internal_model_id);
|
|
}
|
|
|
|
static void mesh_load_appkey_list(mesh_model_t * model){
|
|
mesh_access_setup_tlv();
|
|
uint32_t tag = mesh_model_tag_for_index(model->mid);
|
|
btstack_tlv_singleton_impl->get_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->appkey_indices, sizeof(model->appkey_indices));
|
|
}
|
|
|
|
static void mesh_store_appkey_list(mesh_model_t * model){
|
|
mesh_access_setup_tlv();
|
|
uint32_t tag = mesh_model_tag_for_index(model->mid);
|
|
btstack_tlv_singleton_impl->store_tag(btstack_tlv_singleton_context, tag, (uint8_t *) &model->appkey_indices, sizeof(model->appkey_indices));
|
|
}
|
|
|
|
static void mesh_delete_appkey_list(mesh_model_t * model){
|
|
mesh_access_setup_tlv();
|
|
uint32_t tag = mesh_model_tag_for_index(model->mid);
|
|
btstack_tlv_singleton_impl->delete_tag(btstack_tlv_singleton_context, tag);
|
|
}
|
|
|
|
void mesh_load_appkey_lists(void){
|
|
printf("Load Appkey Lists\n");
|
|
// iterate over elements and models
|
|
mesh_element_iterator_t element_it;
|
|
mesh_element_iterator_init(&element_it);
|
|
while (mesh_element_iterator_has_next(&element_it)){
|
|
mesh_element_t * element = mesh_element_iterator_next(&element_it);
|
|
mesh_model_iterator_t model_it;
|
|
mesh_model_iterator_init(&model_it, element);
|
|
while (mesh_model_iterator_has_next(&model_it)){
|
|
mesh_model_t * model = mesh_model_iterator_next(&model_it);
|
|
mesh_load_appkey_list(model);
|
|
}
|
|
}
|
|
}
|
|
|
|
void mesh_delete_appkey_lists(void){
|
|
printf("Delete Appkey Lists\n");
|
|
mesh_access_setup_tlv();
|
|
// iterate over elements and models
|
|
mesh_element_iterator_t element_it;
|
|
mesh_element_iterator_init(&element_it);
|
|
while (mesh_element_iterator_has_next(&element_it)){
|
|
mesh_element_t * element = mesh_element_iterator_next(&element_it);
|
|
mesh_model_iterator_t model_it;
|
|
mesh_model_iterator_init(&model_it, element);
|
|
while (mesh_model_iterator_has_next(&model_it)){
|
|
mesh_model_t * model = mesh_model_iterator_next(&model_it);
|
|
mesh_delete_appkey_list(model);
|
|
}
|
|
}
|
|
}
|
|
|
|
void mesh_model_reset_appkeys(mesh_model_t * mesh_model){
|
|
int i;
|
|
for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){
|
|
mesh_model->appkey_indices[i] = MESH_APPKEY_INVALID;
|
|
}
|
|
}
|
|
|
|
uint8_t mesh_model_bind_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){
|
|
int i;
|
|
for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){
|
|
if (mesh_model->appkey_indices[i] == appkey_index) return MESH_FOUNDATION_STATUS_SUCCESS;
|
|
}
|
|
for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){
|
|
if (mesh_model->appkey_indices[i] == MESH_APPKEY_INVALID) {
|
|
mesh_model->appkey_indices[i] = appkey_index;
|
|
mesh_store_appkey_list(mesh_model);
|
|
return MESH_FOUNDATION_STATUS_SUCCESS;
|
|
}
|
|
}
|
|
return MESH_FOUNDATION_STATUS_INSUFFICIENT_RESOURCES;
|
|
}
|
|
|
|
void mesh_model_unbind_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){
|
|
int i;
|
|
for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){
|
|
if (mesh_model->appkey_indices[i] == appkey_index) {
|
|
mesh_model->appkey_indices[i] = MESH_APPKEY_INVALID;
|
|
mesh_store_appkey_list(mesh_model);
|
|
}
|
|
}
|
|
}
|
|
|
|
int mesh_model_contains_appkey(mesh_model_t * mesh_model, uint16_t appkey_index){
|
|
uint16_t i;
|
|
for (i=0;i<MAX_NR_MESH_APPKEYS_PER_MODEL;i++){
|
|
if (mesh_model->appkey_indices[i] == appkey_index) return 1;
|
|
}
|
|
return 0;
|
|
|
|
}
|
|
|