xref: /btstack/test/mesh/mesh_message_test.cpp (revision b1a7ae8f2c2291d82e73add044cb36ef5c451958)
184242f88SMatthias Ringwald #include <stdio.h>
284242f88SMatthias Ringwald 
384242f88SMatthias Ringwald #include "CppUTest/TestHarness.h"
484242f88SMatthias Ringwald #include "CppUTest/CommandLineTestRunner.h"
584242f88SMatthias Ringwald 
684242f88SMatthias Ringwald #include "bluetooth_data_types.h"
784242f88SMatthias Ringwald #include "bluetooth_gatt.h"
8aaa248aaSMatthias Ringwald #include "btstack_debug.h"
984242f88SMatthias Ringwald #include "btstack_memory.h"
1084242f88SMatthias Ringwald #include "btstack_util.h"
1184242f88SMatthias Ringwald #include "mesh/adv_bearer.h"
1284242f88SMatthias Ringwald #include "mesh/gatt_bearer.h"
1363197f45SMatthias Ringwald #include "mesh/mesh_access.h"
1484242f88SMatthias Ringwald #include "mesh/mesh_crypto.h"
1584242f88SMatthias Ringwald #include "mesh/mesh_foundation.h"
1684242f88SMatthias Ringwald #include "mesh/mesh_iv_index_seq_number.h"
1784242f88SMatthias Ringwald #include "mesh/mesh_lower_transport.h"
1884242f88SMatthias Ringwald #include "mesh/mesh_network.h"
1984242f88SMatthias Ringwald #include "mesh/mesh_upper_transport.h"
2084242f88SMatthias Ringwald #include "mesh/provisioning.h"
2184242f88SMatthias Ringwald #include "mesh/mesh_peer.h"
2284242f88SMatthias Ringwald 
2384242f88SMatthias Ringwald extern "C" int mock_process_hci_cmd(void);
2484242f88SMatthias Ringwald 
2584242f88SMatthias Ringwald static mesh_network_pdu_t * received_network_pdu;
2684242f88SMatthias Ringwald static mesh_network_pdu_t * received_proxy_pdu;
2784242f88SMatthias Ringwald 
2884242f88SMatthias Ringwald static uint8_t outgoing_gatt_network_pdu_data[29];
2984242f88SMatthias Ringwald static uint8_t outgoing_gatt_network_pdu_len;
3084242f88SMatthias Ringwald 
3184242f88SMatthias Ringwald static uint8_t outgoing_adv_network_pdu_data[29];
3284242f88SMatthias Ringwald static uint8_t outgoing_adv_network_pdu_len;
3384242f88SMatthias Ringwald 
3484242f88SMatthias Ringwald static uint8_t  recv_upper_transport_pdu_data[100];
3584242f88SMatthias Ringwald static uint16_t recv_upper_transport_pdu_len;
3684242f88SMatthias Ringwald 
3784242f88SMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER
3884242f88SMatthias Ringwald static btstack_packet_handler_t adv_packet_handler;
3984242f88SMatthias Ringwald void adv_bearer_register_for_network_pdu(btstack_packet_handler_t packet_handler){
4084242f88SMatthias Ringwald     adv_packet_handler = packet_handler;
4184242f88SMatthias Ringwald }
4284242f88SMatthias Ringwald void adv_bearer_request_can_send_now_for_network_pdu(void){
4384242f88SMatthias Ringwald     // simulate can send now
4484242f88SMatthias Ringwald     uint8_t event[3];
4584242f88SMatthias Ringwald     event[0] = HCI_EVENT_MESH_META;
4684242f88SMatthias Ringwald     event[1] = 1;
4784242f88SMatthias Ringwald     event[2] = MESH_SUBEVENT_CAN_SEND_NOW;
4884242f88SMatthias Ringwald     (*adv_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
4984242f88SMatthias Ringwald }
5084242f88SMatthias Ringwald void adv_bearer_send_network_pdu(const uint8_t * network_pdu, uint16_t size, uint8_t count, uint16_t interval){
5184242f88SMatthias Ringwald     (void) count;
5284242f88SMatthias Ringwald     (void) interval;
5384242f88SMatthias Ringwald     // printf("ADV Network PDU: ");
5484242f88SMatthias Ringwald     // printf_hexdump(network_pdu, size);
5584242f88SMatthias Ringwald     memcpy(outgoing_adv_network_pdu_data, network_pdu, size);
5684242f88SMatthias Ringwald     outgoing_adv_network_pdu_len = size;
5784242f88SMatthias Ringwald }
5884242f88SMatthias Ringwald static void adv_bearer_emit_sent(void){
5984242f88SMatthias Ringwald     uint8_t event[3];
6084242f88SMatthias Ringwald     event[0] = HCI_EVENT_MESH_META;
6184242f88SMatthias Ringwald     event[1] = 1;
6284242f88SMatthias Ringwald     event[2] = MESH_SUBEVENT_MESSAGE_SENT;
6384242f88SMatthias Ringwald     (*adv_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
6484242f88SMatthias Ringwald }
6584242f88SMatthias Ringwald #endif
6684242f88SMatthias Ringwald 
6784242f88SMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER
6884242f88SMatthias Ringwald static btstack_packet_handler_t gatt_packet_handler;
6984242f88SMatthias Ringwald void gatt_bearer_register_for_network_pdu(btstack_packet_handler_t packet_handler){
7084242f88SMatthias Ringwald     gatt_packet_handler = packet_handler;
7184242f88SMatthias Ringwald }
7284242f88SMatthias Ringwald void gatt_bearer_register_for_mesh_proxy_configuration(btstack_packet_handler_t packet_handler){
7384242f88SMatthias Ringwald }
7484242f88SMatthias Ringwald void gatt_bearer_request_can_send_now_for_network_pdu(void){
7584242f88SMatthias Ringwald     // simulate can send now
7684242f88SMatthias Ringwald     uint8_t event[3];
7784242f88SMatthias Ringwald     event[0] = HCI_EVENT_MESH_META;
7884242f88SMatthias Ringwald     event[1] = 1;
7984242f88SMatthias Ringwald     event[2] = MESH_SUBEVENT_CAN_SEND_NOW;
8084242f88SMatthias Ringwald     (*gatt_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
8184242f88SMatthias Ringwald }
8284242f88SMatthias Ringwald void gatt_bearer_send_network_pdu(const uint8_t * network_pdu, uint16_t size){
8384242f88SMatthias Ringwald     // printf("ADV Network PDU: ");
8484242f88SMatthias Ringwald     // printf_hexdump(network_pdu, size);
8584242f88SMatthias Ringwald     memcpy(outgoing_gatt_network_pdu_data, network_pdu, size);
8684242f88SMatthias Ringwald     outgoing_gatt_network_pdu_len = size;
8784242f88SMatthias Ringwald }
8884242f88SMatthias Ringwald static void gatt_bearer_emit_sent(void){
8984242f88SMatthias Ringwald     uint8_t event[3];
9084242f88SMatthias Ringwald     event[0] = HCI_EVENT_MESH_META;
9184242f88SMatthias Ringwald     event[1] = 1;
9284242f88SMatthias Ringwald     event[2] = MESH_SUBEVENT_MESSAGE_SENT;
9384242f88SMatthias Ringwald     (*gatt_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
9484242f88SMatthias Ringwald }
9584242f88SMatthias Ringwald static void gatt_bearer_emit_connected(void){
9684242f88SMatthias Ringwald     uint8_t event[5];
9784242f88SMatthias Ringwald     event[0] = HCI_EVENT_MESH_META;
9884242f88SMatthias Ringwald     event[1] = 1;
9984242f88SMatthias Ringwald     event[2] = MESH_SUBEVENT_PROXY_CONNECTED;
10084242f88SMatthias Ringwald     little_endian_store_16(event, 3, 0x1234);
10184242f88SMatthias Ringwald     (*gatt_packet_handler)(HCI_EVENT_PACKET, 0, &event[0], sizeof(event));
10284242f88SMatthias Ringwald }
10384242f88SMatthias Ringwald #endif
10484242f88SMatthias Ringwald 
10563197f45SMatthias Ringwald // copy from mesh_message.c for now
10663197f45SMatthias Ringwald uint16_t mesh_pdu_dst(mesh_pdu_t * pdu){
10763197f45SMatthias Ringwald     switch (pdu->pdu_type){
10863197f45SMatthias Ringwald         case MESH_PDU_TYPE_TRANSPORT:
10963197f45SMatthias Ringwald             return mesh_transport_dst((mesh_transport_pdu_t*) pdu);
11063197f45SMatthias Ringwald         case MESH_PDU_TYPE_NETWORK:
11163197f45SMatthias Ringwald             return mesh_network_dst((mesh_network_pdu_t *) pdu);
1127a0b7488SMatthias Ringwald         case MESH_PDU_TYPE_UNSEGMENTED:
1137a0b7488SMatthias Ringwald             return mesh_network_dst(((mesh_unsegmented_pdu_t *) pdu)->segment);
114*b1a7ae8fSMatthias Ringwald         case MESH_PDU_TYPE_ACCESS:
115*b1a7ae8fSMatthias Ringwald             return mesh_access_dst((mesh_access_pdu_t *) pdu);
11663197f45SMatthias Ringwald         default:
1177a0b7488SMatthias Ringwald             btstack_assert(false);
11863197f45SMatthias Ringwald             return MESH_ADDRESS_UNSASSIGNED;
11963197f45SMatthias Ringwald     }
12063197f45SMatthias Ringwald }
12163197f45SMatthias Ringwald uint16_t mesh_pdu_ctl(mesh_pdu_t * pdu){
12263197f45SMatthias Ringwald     switch (pdu->pdu_type){
12363197f45SMatthias Ringwald         case MESH_PDU_TYPE_TRANSPORT:
12463197f45SMatthias Ringwald             return mesh_transport_ctl((mesh_transport_pdu_t*) pdu);
12563197f45SMatthias Ringwald         case MESH_PDU_TYPE_NETWORK:
12663197f45SMatthias Ringwald             return mesh_network_control((mesh_network_pdu_t *) pdu);
127*b1a7ae8fSMatthias Ringwald         case MESH_PDU_TYPE_ACCESS:
128*b1a7ae8fSMatthias Ringwald             return mesh_access_ctl((mesh_access_pdu_t *) pdu);
12963197f45SMatthias Ringwald         default:
1307a0b7488SMatthias Ringwald             btstack_assert(false);
13163197f45SMatthias Ringwald             return 0;
13263197f45SMatthias Ringwald     }
13363197f45SMatthias Ringwald }
13463197f45SMatthias Ringwald 
13584242f88SMatthias Ringwald void CHECK_EQUAL_ARRAY(uint8_t * expected, uint8_t * actual, int size){
13684242f88SMatthias Ringwald     int i;
13784242f88SMatthias Ringwald     for (i=0; i<size; i++){
13884242f88SMatthias Ringwald         if (expected[i] != actual[i]) {
13984242f88SMatthias Ringwald             printf("offset %u wrong\n", i);
14084242f88SMatthias Ringwald             printf("expected: "); printf_hexdump(expected, size);
14184242f88SMatthias Ringwald             printf("actual:   "); printf_hexdump(actual, size);
14284242f88SMatthias Ringwald         }
14384242f88SMatthias Ringwald         BYTES_EQUAL(expected[i], actual[i]);
14484242f88SMatthias Ringwald     }
14584242f88SMatthias Ringwald }
14684242f88SMatthias Ringwald 
14784242f88SMatthias Ringwald static int scan_hex_byte(const char * byte_string){
14884242f88SMatthias Ringwald     int upper_nibble = nibble_for_char(*byte_string++);
14984242f88SMatthias Ringwald     if (upper_nibble < 0) return -1;
15084242f88SMatthias Ringwald     int lower_nibble = nibble_for_char(*byte_string);
15184242f88SMatthias Ringwald     if (lower_nibble < 0) return -1;
15284242f88SMatthias Ringwald     return (upper_nibble << 4) | lower_nibble;
15384242f88SMatthias Ringwald }
15484242f88SMatthias Ringwald 
15584242f88SMatthias Ringwald static int btstack_parse_hex(const char * string, uint16_t len, uint8_t * buffer){
15684242f88SMatthias Ringwald     int i;
15784242f88SMatthias Ringwald     for (i = 0; i < len; i++) {
15884242f88SMatthias Ringwald         int single_byte = scan_hex_byte(string);
15984242f88SMatthias Ringwald         if (single_byte < 0) return 0;
16084242f88SMatthias Ringwald         string += 2;
16184242f88SMatthias Ringwald         buffer[i] = (uint8_t)single_byte;
16284242f88SMatthias Ringwald         // don't check seperator after last byte
16384242f88SMatthias Ringwald         if (i == len - 1) {
16484242f88SMatthias Ringwald             return 1;
16584242f88SMatthias Ringwald         }
16684242f88SMatthias Ringwald         // optional seperator
16784242f88SMatthias Ringwald         char separator = *string;
16884242f88SMatthias Ringwald         if (separator == ':' && separator == '-' && separator == ' ') {
16984242f88SMatthias Ringwald             string++;
17084242f88SMatthias Ringwald         }
17184242f88SMatthias Ringwald     }
17284242f88SMatthias Ringwald     return 1;
17384242f88SMatthias Ringwald }
17484242f88SMatthias Ringwald 
17584242f88SMatthias Ringwald #if 0
17684242f88SMatthias Ringwald static void btstack_print_hex(const uint8_t * data, uint16_t len, char separator){
17784242f88SMatthias Ringwald     int i;
17884242f88SMatthias Ringwald     for (i=0;i<len;i++){
17984242f88SMatthias Ringwald         printf("%02x", data[i]);
18084242f88SMatthias Ringwald         if (separator){
18184242f88SMatthias Ringwald             printf("%c", separator);
18284242f88SMatthias Ringwald         }
18384242f88SMatthias Ringwald     }
18484242f88SMatthias Ringwald     printf("\n");
18584242f88SMatthias Ringwald }
18684242f88SMatthias Ringwald #endif
18784242f88SMatthias Ringwald 
18884242f88SMatthias Ringwald static mesh_transport_key_t   test_application_key;
18984242f88SMatthias Ringwald static void mesh_application_key_set(uint16_t netkey_index, uint16_t appkey_index, uint8_t aid, const uint8_t *application_key) {
19084242f88SMatthias Ringwald     test_application_key.netkey_index = netkey_index;
19184242f88SMatthias Ringwald     test_application_key.appkey_index = appkey_index;
19284242f88SMatthias Ringwald     test_application_key.aid   = aid;
19384242f88SMatthias Ringwald     test_application_key.akf   = 1;
19484242f88SMatthias Ringwald     memcpy(test_application_key.key, application_key, 16);
19584242f88SMatthias Ringwald     mesh_transport_key_add(&test_application_key);
19684242f88SMatthias Ringwald }
19784242f88SMatthias Ringwald 
19884242f88SMatthias Ringwald static void load_network_key_nid_68(void){
19984242f88SMatthias Ringwald     mesh_network_key_t * network_key = btstack_memory_mesh_network_key_get();
20084242f88SMatthias Ringwald     network_key->nid = 0x68;
20184242f88SMatthias Ringwald     btstack_parse_hex("0953fa93e7caac9638f58820220a398e", 16, network_key->encryption_key);
20284242f88SMatthias Ringwald     btstack_parse_hex("8b84eedec100067d670971dd2aa700cf", 16, network_key->privacy_key);
20384242f88SMatthias Ringwald     mesh_network_key_add(network_key);
20484242f88SMatthias Ringwald     mesh_subnet_setup_for_netkey_index(network_key->netkey_index);
20584242f88SMatthias Ringwald }
20684242f88SMatthias Ringwald 
20784242f88SMatthias Ringwald static void load_network_key_nid_5e(void){
20884242f88SMatthias Ringwald     mesh_network_key_t * network_key = btstack_memory_mesh_network_key_get();
20984242f88SMatthias Ringwald     network_key->nid = 0x5e;
21084242f88SMatthias Ringwald     btstack_parse_hex("be635105434859f484fc798e043ce40e", 16, network_key->encryption_key);
21184242f88SMatthias Ringwald     btstack_parse_hex("5d396d4b54d3cbafe943e051fe9a4eb8", 16, network_key->privacy_key);
21284242f88SMatthias Ringwald     mesh_network_key_add(network_key);
21384242f88SMatthias Ringwald     mesh_subnet_setup_for_netkey_index(network_key->netkey_index);
21484242f88SMatthias Ringwald }
21584242f88SMatthias Ringwald 
21684242f88SMatthias Ringwald static void load_network_key_nid_10(void){
21784242f88SMatthias Ringwald     mesh_network_key_t * network_key = btstack_memory_mesh_network_key_get();
21884242f88SMatthias Ringwald     network_key->nid = 0x10;
21984242f88SMatthias Ringwald     btstack_parse_hex("3a4fe84a6cc2c6a766ea93f1084d4039", 16, network_key->encryption_key);
22084242f88SMatthias Ringwald     btstack_parse_hex("f695fcce709ccface4d8b7a1e6e39d25", 16, network_key->privacy_key);
22184242f88SMatthias Ringwald     mesh_network_key_add(network_key);
22284242f88SMatthias Ringwald     mesh_subnet_setup_for_netkey_index(network_key->netkey_index);
22384242f88SMatthias Ringwald }
22484242f88SMatthias Ringwald 
22584242f88SMatthias Ringwald static void load_provisioning_data_test_message(void){
22684242f88SMatthias Ringwald     uint8_t application_key[16];
22784242f88SMatthias Ringwald     btstack_parse_hex("63964771734fbd76e3b40519d1d94a48", 16, application_key);
22884242f88SMatthias Ringwald     mesh_application_key_set( 0, 0, 0x26, application_key);
22984242f88SMatthias Ringwald 
23084242f88SMatthias Ringwald     uint8_t device_key[16];
23184242f88SMatthias Ringwald     btstack_parse_hex("9d6dd0e96eb25dc19a40ed9914f8f03f", 16, device_key);
23284242f88SMatthias Ringwald     mesh_transport_set_device_key(device_key);
23384242f88SMatthias Ringwald }
23484242f88SMatthias Ringwald 
23584242f88SMatthias Ringwald static void test_lower_transport_callback_handler(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu){
23684242f88SMatthias Ringwald     switch (callback_type){
23784242f88SMatthias Ringwald         case MESH_NETWORK_PDU_RECEIVED:
23884242f88SMatthias Ringwald             printf("test MESH_NETWORK_PDU_RECEIVED\n");
23984242f88SMatthias Ringwald             received_network_pdu = network_pdu;
24084242f88SMatthias Ringwald             break;
24184242f88SMatthias Ringwald         case MESH_NETWORK_PDU_SENT:
24284242f88SMatthias Ringwald             printf("test MESH_NETWORK_PDU_SENT\n");
24384242f88SMatthias Ringwald             mesh_lower_transport_received_message(MESH_NETWORK_PDU_SENT, network_pdu);
24484242f88SMatthias Ringwald             break;
24584242f88SMatthias Ringwald         default:
24684242f88SMatthias Ringwald             break;
24784242f88SMatthias Ringwald     }
24884242f88SMatthias Ringwald }
24984242f88SMatthias Ringwald 
25084242f88SMatthias Ringwald static void test_proxy_server_callback_handler(mesh_network_callback_type_t callback_type, mesh_network_pdu_t * network_pdu){
25184242f88SMatthias Ringwald     switch (callback_type){
25284242f88SMatthias Ringwald         case MESH_NETWORK_PDU_RECEIVED:
25384242f88SMatthias Ringwald             printf("test MESH_PROXY_PDU_RECEIVED\n");
25484242f88SMatthias Ringwald             received_proxy_pdu = network_pdu;
25584242f88SMatthias Ringwald             break;
25684242f88SMatthias Ringwald         case MESH_NETWORK_PDU_SENT:
25784242f88SMatthias Ringwald             // printf("test MESH_PROXY_PDU_SENT\n");
25884242f88SMatthias Ringwald             // mesh_lower_transport_received_mesage(MESH_NETWORK_PDU_SENT, network_pdu);
25984242f88SMatthias Ringwald             break;
2601aa2a11eSMatthias Ringwald         case MESH_NETWORK_PDU_ENCRYPTED:
2611aa2a11eSMatthias Ringwald             printf("test MESH_NETWORK_PDU_ENCRYPTED\n");
2621aa2a11eSMatthias Ringwald             received_proxy_pdu = network_pdu;
2631aa2a11eSMatthias Ringwald             break;
26484242f88SMatthias Ringwald         default:
26584242f88SMatthias Ringwald             break;
26684242f88SMatthias Ringwald     }
26784242f88SMatthias Ringwald }
26884242f88SMatthias Ringwald 
269404d2482SMatthias Ringwald static void test_upper_transport_access_message_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){
2707a0b7488SMatthias Ringwald 
2717a0b7488SMatthias Ringwald     // ignore pdu sent
2727a0b7488SMatthias Ringwald     if (callback_type == MESH_TRANSPORT_PDU_SENT) return;
2737a0b7488SMatthias Ringwald 
2747a0b7488SMatthias Ringwald     // process pdu received
275160d2809SMatthias Ringwald     mesh_access_pdu_t    * access_pdu;
27684242f88SMatthias Ringwald     mesh_network_pdu_t   * network_pdu;
277a4bbc09dSMatthias Ringwald     mesh_segmented_pdu_t   * message_pdu;
2787a0b7488SMatthias Ringwald 
27984242f88SMatthias Ringwald     switch(pdu->pdu_type){
280160d2809SMatthias Ringwald         case MESH_PDU_TYPE_ACCESS:
281160d2809SMatthias Ringwald             access_pdu = (mesh_access_pdu_t *) pdu;
282160d2809SMatthias Ringwald             printf("test access handler MESH_PDU_TYPE_ACCESS received\n");
283160d2809SMatthias Ringwald             recv_upper_transport_pdu_len = access_pdu->len;
284160d2809SMatthias Ringwald             memcpy(recv_upper_transport_pdu_data, access_pdu->data, recv_upper_transport_pdu_len);
28584242f88SMatthias Ringwald             mesh_upper_transport_message_processed_by_higher_layer(pdu);
28684242f88SMatthias Ringwald             break;
287a4bbc09dSMatthias Ringwald         case MESH_PDU_TYPE_SEGMENTED:
288a4bbc09dSMatthias Ringwald             message_pdu = (mesh_segmented_pdu_t *) pdu;
289a4bbc09dSMatthias Ringwald             printf("test access handler MESH_PDU_TYPE_SEGMENTED received\n");
290aaa248aaSMatthias Ringwald             network_pdu = (mesh_network_pdu_t *) btstack_linked_list_get_first_item(&message_pdu->segments);
29184242f88SMatthias Ringwald             recv_upper_transport_pdu_len = mesh_network_pdu_len(network_pdu)  - 1;
29284242f88SMatthias Ringwald             memcpy(recv_upper_transport_pdu_data, mesh_network_pdu_data(network_pdu) + 1, recv_upper_transport_pdu_len);
29384242f88SMatthias Ringwald             mesh_upper_transport_message_processed_by_higher_layer(pdu);
29484242f88SMatthias Ringwald             break;
29584242f88SMatthias Ringwald         default:
296160d2809SMatthias Ringwald             btstack_assert(0);
29784242f88SMatthias Ringwald             break;
29884242f88SMatthias Ringwald     }
29984242f88SMatthias Ringwald }
30084242f88SMatthias Ringwald 
301404d2482SMatthias Ringwald static void test_upper_transport_control_message_handler(mesh_transport_callback_type_t callback_type, mesh_transport_status_t status, mesh_pdu_t * pdu){
3027a0b7488SMatthias Ringwald     // ignore pdu sent
3037a0b7488SMatthias Ringwald     if (callback_type == MESH_TRANSPORT_PDU_SENT) return;
3047a0b7488SMatthias Ringwald 
3057a0b7488SMatthias Ringwald     // process pdu received
30684242f88SMatthias Ringwald     mesh_transport_pdu_t * transport_pdu;
30784242f88SMatthias Ringwald     mesh_network_pdu_t   * network_pdu;
3088facb3eaSMatthias Ringwald     mesh_unsegmented_pdu_t * unsegmented_incoming_pdu;
30984242f88SMatthias Ringwald     switch(pdu->pdu_type){
31084242f88SMatthias Ringwald         case MESH_PDU_TYPE_TRANSPORT:
31184242f88SMatthias Ringwald             transport_pdu = (mesh_transport_pdu_t *) pdu;
31284242f88SMatthias Ringwald             printf("test MESH_CONTROL_TRANSPORT_PDU_RECEIVED\n");
31384242f88SMatthias Ringwald             recv_upper_transport_pdu_len = transport_pdu->len;
31484242f88SMatthias Ringwald             memcpy(recv_upper_transport_pdu_data, transport_pdu->data, recv_upper_transport_pdu_len);
31584242f88SMatthias Ringwald             mesh_upper_transport_message_processed_by_higher_layer(pdu);
31684242f88SMatthias Ringwald             break;
3178facb3eaSMatthias Ringwald         case MESH_PDU_TYPE_UNSEGMENTED:
3188facb3eaSMatthias Ringwald             unsegmented_incoming_pdu = (mesh_unsegmented_pdu_t *) pdu;
3193d9c3b8eSMatthias Ringwald             network_pdu = unsegmented_incoming_pdu->segment;
3208facb3eaSMatthias Ringwald             printf("test MESH_PDU_TYPE_UNSEGMENTED\n");
32184242f88SMatthias Ringwald             recv_upper_transport_pdu_len = mesh_network_pdu_len(network_pdu);
32284242f88SMatthias Ringwald             memcpy(recv_upper_transport_pdu_data, mesh_network_pdu_data(network_pdu), recv_upper_transport_pdu_len);
32384242f88SMatthias Ringwald             mesh_upper_transport_message_processed_by_higher_layer(pdu);
32484242f88SMatthias Ringwald             break;
32584242f88SMatthias Ringwald         default:
326160d2809SMatthias Ringwald             btstack_assert(0);
32784242f88SMatthias Ringwald             break;
32884242f88SMatthias Ringwald     }
32984242f88SMatthias Ringwald }
33084242f88SMatthias Ringwald 
33184242f88SMatthias Ringwald TEST_GROUP(MessageTest){
33284242f88SMatthias Ringwald     void setup(void){
33384242f88SMatthias Ringwald         btstack_memory_init();
33484242f88SMatthias Ringwald         btstack_crypto_init();
33584242f88SMatthias Ringwald         load_provisioning_data_test_message();
33684242f88SMatthias Ringwald         mesh_network_init();
33784242f88SMatthias Ringwald         mesh_lower_transport_init();
33884242f88SMatthias Ringwald         mesh_upper_transport_init();
33984242f88SMatthias Ringwald         mesh_network_key_init();
34084242f88SMatthias Ringwald         // intercept messages between network and lower layer
34184242f88SMatthias Ringwald         mesh_network_set_higher_layer_handler(&test_lower_transport_callback_handler);
34284242f88SMatthias Ringwald         mesh_network_set_proxy_message_handler(&test_proxy_server_callback_handler);
34384242f88SMatthias Ringwald         // register to receive upper transport messages
34484242f88SMatthias Ringwald         mesh_upper_transport_register_access_message_handler(&test_upper_transport_access_message_handler);
34584242f88SMatthias Ringwald         mesh_upper_transport_register_control_message_handler(&test_upper_transport_control_message_handler);
34684242f88SMatthias Ringwald         mesh_seq_auth_reset();
34784242f88SMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER
34884242f88SMatthias Ringwald         mesh_foundation_gatt_proxy_set(1);
34984242f88SMatthias Ringwald         gatt_bearer_emit_connected();
35084242f88SMatthias Ringwald #endif
35184242f88SMatthias Ringwald         outgoing_gatt_network_pdu_len = 0;
35284242f88SMatthias Ringwald         outgoing_adv_network_pdu_len = 0;
35384242f88SMatthias Ringwald         received_network_pdu = NULL;
35484242f88SMatthias Ringwald         recv_upper_transport_pdu_len =0;
35584242f88SMatthias Ringwald     }
35684242f88SMatthias Ringwald     void teardown(void){
35784242f88SMatthias Ringwald         // printf("-- teardown start --\n\n");
35884242f88SMatthias Ringwald         btstack_crypto_reset();
35984242f88SMatthias Ringwald         mesh_network_reset();
36084242f88SMatthias Ringwald         mesh_lower_transport_reset();
36184242f88SMatthias Ringwald         mesh_upper_transport_dump();
36284242f88SMatthias Ringwald         mesh_upper_transport_reset();
36384242f88SMatthias Ringwald         // mesh_network_dump();
36484242f88SMatthias Ringwald         // mesh_transport_dump();
36584242f88SMatthias Ringwald         printf("-- teardown complete --\n\n");
36684242f88SMatthias Ringwald     }
36784242f88SMatthias Ringwald };
36884242f88SMatthias Ringwald 
36984242f88SMatthias Ringwald static uint8_t transport_pdu_data[64];
37084242f88SMatthias Ringwald static uint16_t transport_pdu_len;
37184242f88SMatthias Ringwald 
37284242f88SMatthias Ringwald static     uint8_t test_network_pdu_len;
37384242f88SMatthias Ringwald static uint8_t test_network_pdu_data[29];
37484242f88SMatthias Ringwald 
37584242f88SMatthias Ringwald void test_receive_network_pdus(int count, char ** network_pdus, char ** lower_transport_pdus, char * access_pdu){
37684242f88SMatthias Ringwald     int i;
37784242f88SMatthias Ringwald     for (i=0;i<count;i++){
37884242f88SMatthias Ringwald         test_network_pdu_len = strlen(network_pdus[i]) / 2;
37984242f88SMatthias Ringwald         btstack_parse_hex(network_pdus[i], test_network_pdu_len, test_network_pdu_data);
38084242f88SMatthias Ringwald 
38184242f88SMatthias Ringwald         mesh_network_received_message(test_network_pdu_data, test_network_pdu_len, 0);
38284242f88SMatthias Ringwald 
38384242f88SMatthias Ringwald         while (received_network_pdu == NULL) {
38484242f88SMatthias Ringwald             mock_process_hci_cmd();
38584242f88SMatthias Ringwald         }
38684242f88SMatthias Ringwald 
38784242f88SMatthias Ringwald         transport_pdu_len = strlen(lower_transport_pdus[i]) / 2;
38884242f88SMatthias Ringwald         btstack_parse_hex(lower_transport_pdus[i], transport_pdu_len, transport_pdu_data);
38984242f88SMatthias Ringwald 
39084242f88SMatthias Ringwald         uint8_t * lower_transport_pdu     = mesh_network_pdu_data(received_network_pdu);
39184242f88SMatthias Ringwald         uint8_t   lower_transport_pdu_len = mesh_network_pdu_len(received_network_pdu);
39284242f88SMatthias Ringwald 
39384242f88SMatthias Ringwald         // printf_hexdump(lower_transport_pdu, lower_transport_pdu_len);
39484242f88SMatthias Ringwald 
39584242f88SMatthias Ringwald         CHECK_EQUAL( transport_pdu_len, lower_transport_pdu_len);
39684242f88SMatthias Ringwald         CHECK_EQUAL_ARRAY(transport_pdu_data, lower_transport_pdu, transport_pdu_len);
39784242f88SMatthias Ringwald 
39884242f88SMatthias Ringwald         // forward to mesh_transport
39984242f88SMatthias Ringwald         mesh_lower_transport_received_message(MESH_NETWORK_PDU_RECEIVED, received_network_pdu);
40084242f88SMatthias Ringwald 
40184242f88SMatthias Ringwald         // done
40284242f88SMatthias Ringwald         received_network_pdu = NULL;
40384242f88SMatthias Ringwald     }
40484242f88SMatthias Ringwald 
40584242f88SMatthias Ringwald     // wait for tranport pdu
40684242f88SMatthias Ringwald     while (recv_upper_transport_pdu_len == 0) {
40784242f88SMatthias Ringwald         mock_process_hci_cmd();
40884242f88SMatthias Ringwald     }
40984242f88SMatthias Ringwald 
41084242f88SMatthias Ringwald     transport_pdu_len = strlen(access_pdu) / 2;
41184242f88SMatthias Ringwald     btstack_parse_hex(access_pdu, transport_pdu_len, transport_pdu_data);
41284242f88SMatthias Ringwald 
41384242f88SMatthias Ringwald     printf("UpperTransportPDU: ");
41484242f88SMatthias Ringwald     printf_hexdump(recv_upper_transport_pdu_data, recv_upper_transport_pdu_len);
41584242f88SMatthias Ringwald     CHECK_EQUAL( transport_pdu_len, recv_upper_transport_pdu_len);
41684242f88SMatthias Ringwald     CHECK_EQUAL_ARRAY(transport_pdu_data, recv_upper_transport_pdu_data, transport_pdu_len);
41784242f88SMatthias Ringwald }
41884242f88SMatthias Ringwald 
41984242f88SMatthias Ringwald static void expect_gatt_network_pdu(const uint8_t * data, uint16_t len){
42084242f88SMatthias Ringwald         while (outgoing_gatt_network_pdu_len == 0) {
42184242f88SMatthias Ringwald             mock_process_hci_cmd();
42284242f88SMatthias Ringwald         }
42384242f88SMatthias Ringwald 
42484242f88SMatthias Ringwald         if (outgoing_gatt_network_pdu_len != test_network_pdu_len){
42584242f88SMatthias Ringwald             printf("Test Network PDU (%u): ", outgoing_gatt_network_pdu_len); printf_hexdump(outgoing_gatt_network_pdu_data, outgoing_gatt_network_pdu_len);
42684242f88SMatthias Ringwald             printf("Expected     PDU (%u): ", test_network_pdu_len); printf_hexdump(test_network_pdu_data, test_network_pdu_len);
42784242f88SMatthias Ringwald         }
42884242f88SMatthias Ringwald         CHECK_EQUAL( outgoing_gatt_network_pdu_len, test_network_pdu_len);
42984242f88SMatthias Ringwald         CHECK_EQUAL_ARRAY(test_network_pdu_data, outgoing_gatt_network_pdu_data, test_network_pdu_len);
43084242f88SMatthias Ringwald 
43184242f88SMatthias Ringwald         outgoing_gatt_network_pdu_len = 0;
43284242f88SMatthias Ringwald         gatt_bearer_emit_sent();
43384242f88SMatthias Ringwald }
43484242f88SMatthias Ringwald 
43584242f88SMatthias Ringwald static void expect_adv_network_pdu(const uint8_t * data, uint16_t len){
43684242f88SMatthias Ringwald         while (outgoing_adv_network_pdu_len == 0) {
43784242f88SMatthias Ringwald             mock_process_hci_cmd();
43884242f88SMatthias Ringwald         }
43984242f88SMatthias Ringwald 
44084242f88SMatthias Ringwald         if (outgoing_adv_network_pdu_len != test_network_pdu_len){
44184242f88SMatthias Ringwald             printf("Test Network PDU (%u): ", outgoing_adv_network_pdu_len); printf_hexdump(outgoing_adv_network_pdu_data, outgoing_adv_network_pdu_len);
44284242f88SMatthias Ringwald             printf("Expected     PDU (%u): ", test_network_pdu_len); printf_hexdump(test_network_pdu_data, test_network_pdu_len);
44384242f88SMatthias Ringwald         }
44484242f88SMatthias Ringwald         CHECK_EQUAL( outgoing_adv_network_pdu_len, test_network_pdu_len);
44584242f88SMatthias Ringwald         CHECK_EQUAL_ARRAY(test_network_pdu_data, outgoing_adv_network_pdu_data, test_network_pdu_len);
44684242f88SMatthias Ringwald 
44784242f88SMatthias Ringwald         outgoing_adv_network_pdu_len = 0;
44884242f88SMatthias Ringwald         adv_bearer_emit_sent();
44984242f88SMatthias Ringwald }
45084242f88SMatthias Ringwald 
45184242f88SMatthias Ringwald void test_send_access_message(uint16_t netkey_index, uint16_t appkey_index,  uint8_t ttl, uint16_t src, uint16_t dest, uint8_t szmic, char * control_pdu, int count, char ** lower_transport_pdus, char ** network_pdus){
45284242f88SMatthias Ringwald 
45384242f88SMatthias Ringwald     transport_pdu_len = strlen(control_pdu) / 2;
45484242f88SMatthias Ringwald     btstack_parse_hex(control_pdu, transport_pdu_len, transport_pdu_data);
45584242f88SMatthias Ringwald 
45684242f88SMatthias Ringwald     mesh_pdu_t * pdu;
45784242f88SMatthias Ringwald     if (count == 1 ){
45884242f88SMatthias Ringwald         // send as unsegmented access pdu
459d38a7664SMatthias Ringwald         static mesh_unsegmented_pdu_t unsegmented_pdu;
460d38a7664SMatthias Ringwald         unsegmented_pdu.pdu_header.pdu_type = MESH_PDU_TYPE_UNSEGMENTED;
461e41a4a08SMatthias Ringwald         mesh_network_pdu_t * segment     = mesh_network_pdu_get();
462d38a7664SMatthias Ringwald         unsegmented_pdu.segment = segment;
4637a0b7488SMatthias Ringwald         unsegmented_pdu.flags = 0;
464d38a7664SMatthias Ringwald         pdu = (mesh_pdu_t*) &unsegmented_pdu;
46584242f88SMatthias Ringwald     } else {
46684242f88SMatthias Ringwald         // send as segmented access pdu
467*b1a7ae8fSMatthias Ringwald         static mesh_access_pdu_t segmented_pdu;
468*b1a7ae8fSMatthias Ringwald         segmented_pdu.pdu_header.pdu_type = MESH_PDU_TYPE_ACCESS;
469*b1a7ae8fSMatthias Ringwald         segmented_pdu.flags = 0;
470*b1a7ae8fSMatthias Ringwald         pdu = (mesh_pdu_t *) &segmented_pdu;
47184242f88SMatthias Ringwald     }
47284242f88SMatthias Ringwald     mesh_upper_transport_setup_access_pdu(pdu, netkey_index, appkey_index, ttl, src, dest, szmic, transport_pdu_data, transport_pdu_len);
47384242f88SMatthias Ringwald     mesh_upper_transport_send_access_pdu(pdu);
47484242f88SMatthias Ringwald 
47584242f88SMatthias Ringwald     // check for all network pdus
47684242f88SMatthias Ringwald     int i;
47784242f88SMatthias Ringwald     for (i=0;i<count;i++){
47884242f88SMatthias Ringwald         // parse expected network pdu
47984242f88SMatthias Ringwald         test_network_pdu_len = strlen(network_pdus[i]) / 2;
48084242f88SMatthias Ringwald         btstack_parse_hex(network_pdus[i], test_network_pdu_len, test_network_pdu_data);
48184242f88SMatthias Ringwald 
48284242f88SMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER
48384242f88SMatthias Ringwald         expect_gatt_network_pdu(test_network_pdu_data, test_network_pdu_len);
48484242f88SMatthias Ringwald #endif
48584242f88SMatthias Ringwald 
48684242f88SMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER
48784242f88SMatthias Ringwald         expect_adv_network_pdu(test_network_pdu_data, test_network_pdu_len);
48884242f88SMatthias Ringwald #endif
48984242f88SMatthias Ringwald     }
49084242f88SMatthias Ringwald }
49184242f88SMatthias Ringwald 
49284242f88SMatthias Ringwald void test_send_control_message(uint16_t netkey_index, uint8_t ttl, uint16_t src, uint16_t dest, char * control_pdu, int count, char ** lower_transport_pdus, char ** network_pdus){
49384242f88SMatthias Ringwald 
49484242f88SMatthias Ringwald     transport_pdu_len = strlen(control_pdu) / 2;
49584242f88SMatthias Ringwald     btstack_parse_hex(control_pdu, transport_pdu_len, transport_pdu_data);
49684242f88SMatthias Ringwald 
49784242f88SMatthias Ringwald     uint8_t opcode = transport_pdu_data[0];
49884242f88SMatthias Ringwald 
49984242f88SMatthias Ringwald     mesh_pdu_t * pdu;
50084242f88SMatthias Ringwald     if (transport_pdu_len < 12){
50184242f88SMatthias Ringwald         // send as unsegmented control pdu
50284242f88SMatthias Ringwald         pdu = (mesh_pdu_t *) mesh_network_pdu_get();
50384242f88SMatthias Ringwald     } else {
50484242f88SMatthias Ringwald         // send as segmented control pdu
50584242f88SMatthias Ringwald         pdu = (mesh_pdu_t *) mesh_transport_pdu_get();
50684242f88SMatthias Ringwald     }
50784242f88SMatthias Ringwald     mesh_upper_transport_setup_control_pdu(pdu, netkey_index, ttl, src, dest, opcode, transport_pdu_data+1, transport_pdu_len-1);
50884242f88SMatthias Ringwald     mesh_upper_transport_send_control_pdu(pdu);
50984242f88SMatthias Ringwald 
51084242f88SMatthias Ringwald     // check for all network pdus
51184242f88SMatthias Ringwald     int i;
51284242f88SMatthias Ringwald     for (i=0;i<count;i++){
51384242f88SMatthias Ringwald         // expected network pdu
51484242f88SMatthias Ringwald         test_network_pdu_len = strlen(network_pdus[i]) / 2;
51584242f88SMatthias Ringwald         btstack_parse_hex(network_pdus[i], test_network_pdu_len, test_network_pdu_data);
51684242f88SMatthias Ringwald 
51784242f88SMatthias Ringwald #ifdef ENABLE_MESH_GATT_BEARER
51884242f88SMatthias Ringwald         expect_gatt_network_pdu(test_network_pdu_data, test_network_pdu_len);
51984242f88SMatthias Ringwald #endif
52084242f88SMatthias Ringwald 
52184242f88SMatthias Ringwald #ifdef ENABLE_MESH_ADV_BEARER
52284242f88SMatthias Ringwald         expect_adv_network_pdu(test_network_pdu_data, test_network_pdu_len);
52384242f88SMatthias Ringwald #endif
52484242f88SMatthias Ringwald 
52584242f88SMatthias Ringwald     }
52684242f88SMatthias Ringwald }
52784242f88SMatthias Ringwald #if 1
52884242f88SMatthias Ringwald // Message 1
52984242f88SMatthias Ringwald char * message1_network_pdus[] = {
53084242f88SMatthias Ringwald     (char *) "68eca487516765b5e5bfdacbaf6cb7fb6bff871f035444ce83a670df"
53184242f88SMatthias Ringwald };
53284242f88SMatthias Ringwald char * message1_lower_transport_pdus[] = {
53384242f88SMatthias Ringwald     (char *) "034b50057e400000010000",
53484242f88SMatthias Ringwald };
53584242f88SMatthias Ringwald char * message1_upper_transport_pdu = (char *) "034b50057e400000010000";
53684242f88SMatthias Ringwald TEST(MessageTest, Message1Receive){
53784242f88SMatthias Ringwald     load_network_key_nid_68();
53884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
53984242f88SMatthias Ringwald     test_receive_network_pdus(1, message1_network_pdus, message1_lower_transport_pdus, message1_upper_transport_pdu);
54084242f88SMatthias Ringwald }
54184242f88SMatthias Ringwald TEST(MessageTest, Message1Send){
54284242f88SMatthias Ringwald     uint16_t netkey_index = 0;
54384242f88SMatthias Ringwald     uint8_t  ttl          = 0;
54484242f88SMatthias Ringwald     uint16_t src          = 0x1201;
54584242f88SMatthias Ringwald     uint16_t dest         = 0xfffd;
54684242f88SMatthias Ringwald     uint32_t seq          = 1;
54784242f88SMatthias Ringwald     load_network_key_nid_68();
54884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
54984242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
55084242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message1_upper_transport_pdu, 1, message1_lower_transport_pdus, message1_network_pdus);
55184242f88SMatthias Ringwald }
55284242f88SMatthias Ringwald 
55384242f88SMatthias Ringwald // Message 2
55484242f88SMatthias Ringwald char * message2_network_pdus[] = {
55584242f88SMatthias Ringwald     (char *) "68d4c826296d7979d7dbc0c9b4d43eebec129d20a620d01e"
55684242f88SMatthias Ringwald };
55784242f88SMatthias Ringwald char * message2_lower_transport_pdus[] = {
55884242f88SMatthias Ringwald     (char *) "04320308ba072f",
55984242f88SMatthias Ringwald };
56084242f88SMatthias Ringwald char * message2_upper_transport_pdu = (char *) "04320308ba072f";
56184242f88SMatthias Ringwald TEST(MessageTest, Message2Receive){
56284242f88SMatthias Ringwald     load_network_key_nid_68();
56384242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
56484242f88SMatthias Ringwald     test_receive_network_pdus(1, message2_network_pdus, message2_lower_transport_pdus, message2_upper_transport_pdu);
56584242f88SMatthias Ringwald }
56684242f88SMatthias Ringwald TEST(MessageTest, Message2Send){
56784242f88SMatthias Ringwald     uint16_t netkey_index = 0;
56884242f88SMatthias Ringwald     uint8_t  ttl          = 0;
56984242f88SMatthias Ringwald     uint16_t src          = 0x2345;
57084242f88SMatthias Ringwald     uint16_t dest         = 0x1201;
57184242f88SMatthias Ringwald     uint32_t seq          = 0x014820;
57284242f88SMatthias Ringwald     load_network_key_nid_68();
57384242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
57484242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
57584242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message2_upper_transport_pdu, 1, message2_lower_transport_pdus, message2_network_pdus);
57684242f88SMatthias Ringwald }
57784242f88SMatthias Ringwald 
57884242f88SMatthias Ringwald // Message 3
57984242f88SMatthias Ringwald char * message3_network_pdus[] = {
58084242f88SMatthias Ringwald     (char *) "68da062bc96df253273086b8c5ee00bdd9cfcc62a2ddf572"
58184242f88SMatthias Ringwald };
58284242f88SMatthias Ringwald char * message3_lower_transport_pdus[] = {
58384242f88SMatthias Ringwald     (char *) "04fa0205a6000a",
58484242f88SMatthias Ringwald };
58584242f88SMatthias Ringwald char * message3_upper_transport_pdu = (char *) "04fa0205a6000a";
58684242f88SMatthias Ringwald TEST(MessageTest, Message3Receive){
58784242f88SMatthias Ringwald     load_network_key_nid_68();
58884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
58984242f88SMatthias Ringwald     test_receive_network_pdus(1, message3_network_pdus, message3_lower_transport_pdus, message3_upper_transport_pdu);
59084242f88SMatthias Ringwald }
59184242f88SMatthias Ringwald TEST(MessageTest, Message3Send){
59284242f88SMatthias Ringwald     uint16_t netkey_index = 0;
59384242f88SMatthias Ringwald     uint8_t  ttl          = 0;
59484242f88SMatthias Ringwald     uint16_t src          = 0x2fe3;
59584242f88SMatthias Ringwald     uint16_t dest         = 0x1201;
59684242f88SMatthias Ringwald     uint32_t seq          = 0x2b3832;
59784242f88SMatthias Ringwald     load_network_key_nid_68();
59884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
59984242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
60084242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message3_upper_transport_pdu, 1, message3_lower_transport_pdus, message3_network_pdus);
60184242f88SMatthias Ringwald }
60284242f88SMatthias Ringwald 
60384242f88SMatthias Ringwald // Message 4
60484242f88SMatthias Ringwald char * message4_network_pdus[] = {
60584242f88SMatthias Ringwald     (char *) "5e84eba092380fb0e5d0ad970d579a4e88051c"
60684242f88SMatthias Ringwald };
60784242f88SMatthias Ringwald char * message4_lower_transport_pdus[] = {
60884242f88SMatthias Ringwald     (char *) "0100",
60984242f88SMatthias Ringwald };
61084242f88SMatthias Ringwald char * message4_upper_transport_pdu = (char *) "0100";
61184242f88SMatthias Ringwald TEST(MessageTest, Message4Receive){
61284242f88SMatthias Ringwald     load_network_key_nid_5e();
61384242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
61484242f88SMatthias Ringwald     test_receive_network_pdus(1, message4_network_pdus, message4_lower_transport_pdus, message4_upper_transport_pdu);
61584242f88SMatthias Ringwald }
61684242f88SMatthias Ringwald TEST(MessageTest, Message4Send){
61784242f88SMatthias Ringwald     uint16_t netkey_index = 0;
61884242f88SMatthias Ringwald     uint8_t  ttl          = 0;
61984242f88SMatthias Ringwald     uint16_t src          = 0x1201;
62084242f88SMatthias Ringwald     uint16_t dest         = 0x2345;
62184242f88SMatthias Ringwald     uint32_t seq          = 0x000002;
62284242f88SMatthias Ringwald     load_network_key_nid_5e();
62384242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
62484242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
62584242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message4_upper_transport_pdu, 1, message4_lower_transport_pdus, message4_network_pdus);
62684242f88SMatthias Ringwald }
62784242f88SMatthias Ringwald 
62884242f88SMatthias Ringwald // Message 5
62984242f88SMatthias Ringwald char * message5_network_pdus[] = {
63084242f88SMatthias Ringwald     (char *) "5eafd6f53c43db5c39da1792b1fee9ec74b786c56d3a9dee",
63184242f88SMatthias Ringwald };
63284242f88SMatthias Ringwald char * message5_lower_transport_pdus[] = {
63384242f88SMatthias Ringwald     (char *) "02001234567800",
63484242f88SMatthias Ringwald };
63584242f88SMatthias Ringwald char * message5_upper_transport_pdu = (char *) "02001234567800";
63684242f88SMatthias Ringwald TEST(MessageTest, Message5Receive){
63784242f88SMatthias Ringwald     load_network_key_nid_5e();
63884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
63984242f88SMatthias Ringwald     test_receive_network_pdus(1, message5_network_pdus, message5_lower_transport_pdus, message5_upper_transport_pdu);
64084242f88SMatthias Ringwald }
64184242f88SMatthias Ringwald TEST(MessageTest, Message5Send){
64284242f88SMatthias Ringwald     uint16_t netkey_index = 0;
64384242f88SMatthias Ringwald     uint8_t  ttl          = 0;
64484242f88SMatthias Ringwald     uint16_t src          = 0x2345;
64584242f88SMatthias Ringwald     uint16_t dest         = 0x1201;
64684242f88SMatthias Ringwald     uint32_t seq          = 0x014834;
64784242f88SMatthias Ringwald     load_network_key_nid_5e();
64884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
64984242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
65084242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message5_upper_transport_pdu, 1, message5_lower_transport_pdus, message5_network_pdus);
65184242f88SMatthias Ringwald }
65284242f88SMatthias Ringwald 
65384242f88SMatthias Ringwald // Message 6
65484242f88SMatthias Ringwald char * message6_network_pdus[] = {
65584242f88SMatthias Ringwald     (char *) "68cab5c5348a230afba8c63d4e686364979deaf4fd40961145939cda0e",
65684242f88SMatthias Ringwald     (char *) "681615b5dd4a846cae0c032bf0746f44f1b8cc8ce5edc57e55beed49c0",
65784242f88SMatthias Ringwald };
65884242f88SMatthias Ringwald char * message6_lower_transport_pdus[] = {
65984242f88SMatthias Ringwald     (char *) "8026ac01ee9dddfd2169326d23f3afdf",
66084242f88SMatthias Ringwald     (char *) "8026ac21cfdc18c52fdef772e0e17308",
66184242f88SMatthias Ringwald };
66284242f88SMatthias Ringwald char * message6_upper_transport_pdu = (char *) "0056341263964771734fbd76e3b40519d1d94a48";
66384242f88SMatthias Ringwald TEST(MessageTest, Message6Receive){
66484242f88SMatthias Ringwald     load_network_key_nid_68();
66584242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
66684242f88SMatthias Ringwald     test_receive_network_pdus(2, message6_network_pdus, message6_lower_transport_pdus, message6_upper_transport_pdu);
66784242f88SMatthias Ringwald }
66884242f88SMatthias Ringwald TEST(MessageTest, Message6Send){
66984242f88SMatthias Ringwald     uint16_t netkey_index = 0;
67084242f88SMatthias Ringwald     uint16_t appkey_index = MESH_DEVICE_KEY_INDEX;
67184242f88SMatthias Ringwald     uint8_t  ttl          = 4;
67284242f88SMatthias Ringwald     uint16_t src          = 0x0003;
67384242f88SMatthias Ringwald     uint16_t dest         = 0x1201;
67484242f88SMatthias Ringwald     uint32_t seq          = 0x3129ab;
67584242f88SMatthias Ringwald     uint8_t  szmic        = 0;
67684242f88SMatthias Ringwald 
67784242f88SMatthias Ringwald     load_network_key_nid_68();
67884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
67984242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
68084242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message6_upper_transport_pdu, 2, message6_lower_transport_pdus, message6_network_pdus);
68184242f88SMatthias Ringwald }
68284242f88SMatthias Ringwald 
68384242f88SMatthias Ringwald // Message 7 - ACK
68484242f88SMatthias Ringwald char * message7_network_pdus[] = {
68584242f88SMatthias Ringwald     (char *) "68e476b5579c980d0d730f94d7f3509df987bb417eb7c05f",
68684242f88SMatthias Ringwald };
68784242f88SMatthias Ringwald char * message7_lower_transport_pdus[] = {
68884242f88SMatthias Ringwald     (char *) "00a6ac00000002",
68984242f88SMatthias Ringwald };
69084242f88SMatthias Ringwald char * message7_upper_transport_pdu = (char *) "00a6ac00000002";
69184242f88SMatthias Ringwald TEST(MessageTest, Message7Send){
69284242f88SMatthias Ringwald     uint16_t netkey_index = 0;
69384242f88SMatthias Ringwald     uint8_t  ttl          = 0x0b;
69484242f88SMatthias Ringwald     uint16_t src          = 0x2345;
69584242f88SMatthias Ringwald     uint16_t dest         = 0x0003;
69684242f88SMatthias Ringwald     uint32_t seq          = 0x014835;
69784242f88SMatthias Ringwald 
69884242f88SMatthias Ringwald     load_network_key_nid_68();
69984242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
70084242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
70184242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message7_upper_transport_pdu, 1, message7_lower_transport_pdus, message7_network_pdus);
70284242f88SMatthias Ringwald }
70384242f88SMatthias Ringwald // ACK message, handled in mesh_transport - can be checked with test_control_receive_network_pdu
70484242f88SMatthias Ringwald // TEST(MessageTest, Message7Receive){
70584242f88SMatthias Ringwald //     mesh_set_iv_index(0x12345678);
70684242f88SMatthias Ringwald //     test_receive_network_pdus(1, message7_network_pdus, message7_lower_transport_pdus, message7_upper_transport_pdu);
70784242f88SMatthias Ringwald // }
70884242f88SMatthias Ringwald 
70984242f88SMatthias Ringwald // Message 8 - ACK
71084242f88SMatthias Ringwald char * message8_network_pdus[] = {
71184242f88SMatthias Ringwald     (char *) "684daa6267c2cf0e2f91add6f06e66006844cec97f973105ae2534f958",
71284242f88SMatthias Ringwald };
71384242f88SMatthias Ringwald char * message8_lower_transport_pdus[] = {
71484242f88SMatthias Ringwald     (char *) "8026ac01ee9dddfd2169326d23f3afdf",
71584242f88SMatthias Ringwald };
71684242f88SMatthias Ringwald char * message8_upper_transport_pdu = (char *) "8026ac01ee9dddfd2169326d23f3afdf";
71784242f88SMatthias Ringwald // ACK message, handled in mesh_transport - can be checked with test_control_receive_network_pdu
71884242f88SMatthias Ringwald // TEST(MessageTest, Message8Receive){
71984242f88SMatthias Ringwald //     mesh_set_iv_index(0x12345678);
72084242f88SMatthias Ringwald //     test_receive_network_pdus(1, message8_network_pdus, message8_lower_transport_pdus, message8_upper_transport_pdu);
72184242f88SMatthias Ringwald // }
72284242f88SMatthias Ringwald 
72384242f88SMatthias Ringwald // Message 9 - ACK
72484242f88SMatthias Ringwald 
72584242f88SMatthias Ringwald // Message 10
72684242f88SMatthias Ringwald char * message10_network_pdus[] = {
72784242f88SMatthias Ringwald     (char *) "5e7b786568759f7777ed355afaf66d899c1e3d",
72884242f88SMatthias Ringwald };
72984242f88SMatthias Ringwald char * message10_lower_transport_pdus[] = {
73084242f88SMatthias Ringwald     (char *) "0101",
73184242f88SMatthias Ringwald };
73284242f88SMatthias Ringwald char * message10_upper_transport_pdu = (char *) "0101";
73384242f88SMatthias Ringwald TEST(MessageTest, Message10Receive){
73484242f88SMatthias Ringwald     load_network_key_nid_5e();
73584242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
73684242f88SMatthias Ringwald     test_receive_network_pdus(1, message10_network_pdus, message10_lower_transport_pdus, message10_upper_transport_pdu);
73784242f88SMatthias Ringwald }
73884242f88SMatthias Ringwald TEST(MessageTest, Message10Send){
73984242f88SMatthias Ringwald     uint16_t netkey_index = 0;
74084242f88SMatthias Ringwald     uint8_t  ttl          = 0;
74184242f88SMatthias Ringwald     uint16_t src          = 0x1201;
74284242f88SMatthias Ringwald     uint16_t dest         = 0x2345;
74384242f88SMatthias Ringwald     uint32_t seq          = 0x000003;
74484242f88SMatthias Ringwald 
74584242f88SMatthias Ringwald     load_network_key_nid_5e();
74684242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
74784242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
74884242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message10_upper_transport_pdu, 1, message10_lower_transport_pdus, message10_network_pdus);
74984242f88SMatthias Ringwald }
75084242f88SMatthias Ringwald 
75184242f88SMatthias Ringwald // Message 11
75284242f88SMatthias Ringwald // The Friend node responds to this poll with the first segment of the stored message. It also indicates that it has more data.
75384242f88SMatthias Ringwald 
75484242f88SMatthias Ringwald // Message 12
75584242f88SMatthias Ringwald char * message12_network_pdus[] = {
75684242f88SMatthias Ringwald     (char *) "5e8a18fc6e4d05ae21466087599c2426ce9a35",
75784242f88SMatthias Ringwald };
75884242f88SMatthias Ringwald char * message12_lower_transport_pdus[] = {
75984242f88SMatthias Ringwald     (char *) "0101",
76084242f88SMatthias Ringwald };
76184242f88SMatthias Ringwald char * message12_upper_transport_pdu = (char *) "0101";
76284242f88SMatthias Ringwald TEST(MessageTest, Message12Receive){
76384242f88SMatthias Ringwald     load_network_key_nid_5e();
76484242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
76584242f88SMatthias Ringwald     test_receive_network_pdus(1, message12_network_pdus, message12_lower_transport_pdus, message12_upper_transport_pdu);
76684242f88SMatthias Ringwald }
76784242f88SMatthias Ringwald TEST(MessageTest, Message12Send){
76884242f88SMatthias Ringwald     uint16_t netkey_index = 0;
76984242f88SMatthias Ringwald     uint8_t  ttl          = 0;
77084242f88SMatthias Ringwald     uint16_t src          = 0x1201;
77184242f88SMatthias Ringwald     uint16_t dest         = 0x2345;
77284242f88SMatthias Ringwald     uint32_t seq          = 0x000004;
77384242f88SMatthias Ringwald 
77484242f88SMatthias Ringwald     load_network_key_nid_5e();
77584242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
77684242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
77784242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message12_upper_transport_pdu, 1, message12_lower_transport_pdus, message12_network_pdus);
77884242f88SMatthias Ringwald }
77984242f88SMatthias Ringwald 
78084242f88SMatthias Ringwald // Message 13
78184242f88SMatthias Ringwald // The Friend node responds with the same message as last time.
78284242f88SMatthias Ringwald // Message 14
78384242f88SMatthias Ringwald // The Low Power node received the retransmitted stored message. As that message has the MD bit set
78484242f88SMatthias Ringwald // it sends another Friend Poll to obtain the next message.
78584242f88SMatthias Ringwald char * message14_network_pdus[] = {
78684242f88SMatthias Ringwald     (char *) "5e0bbaf92b5c8f7d3ae62a3c75dff683dce24e",
78784242f88SMatthias Ringwald };
78884242f88SMatthias Ringwald char * message14_lower_transport_pdus[] = {
78984242f88SMatthias Ringwald     (char *) "0100",
79084242f88SMatthias Ringwald };
79184242f88SMatthias Ringwald char * message14_upper_transport_pdu = (char *) "0100";
79284242f88SMatthias Ringwald TEST(MessageTest, Message14Receive){
79384242f88SMatthias Ringwald     load_network_key_nid_5e();
79484242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
79584242f88SMatthias Ringwald     test_receive_network_pdus(1, message14_network_pdus, message14_lower_transport_pdus, message14_upper_transport_pdu);
79684242f88SMatthias Ringwald }
79784242f88SMatthias Ringwald TEST(MessageTest, Message14Send){
79884242f88SMatthias Ringwald     uint16_t netkey_index = 0;
79984242f88SMatthias Ringwald     uint8_t  ttl          = 0;
80084242f88SMatthias Ringwald     uint16_t src          = 0x1201;
80184242f88SMatthias Ringwald     uint16_t dest         = 0x2345;
80284242f88SMatthias Ringwald     uint32_t seq          = 0x000005;
80384242f88SMatthias Ringwald 
80484242f88SMatthias Ringwald     load_network_key_nid_5e();
80584242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
80684242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
80784242f88SMatthias Ringwald     test_send_control_message(netkey_index, ttl, src, dest, message14_upper_transport_pdu, 1, message14_lower_transport_pdus, message14_network_pdus);
80884242f88SMatthias Ringwald }
80984242f88SMatthias Ringwald 
81084242f88SMatthias Ringwald // Message 15
81184242f88SMatthias Ringwald // The Friend node responds, with the next message in the friend queue. The Friend node has no more data, so it sets the MD to 0.
81284242f88SMatthias Ringwald char * message15_network_pdus[] = {
81384242f88SMatthias Ringwald     (char *) "5ea8dab50e7ee7f1d29805664d235eacd707217dedfe78497fefec7391",
81484242f88SMatthias Ringwald };
81584242f88SMatthias Ringwald char * message15_lower_transport_pdus[] = {
81684242f88SMatthias Ringwald     (char *) "8026ac21cfdc18c52fdef772e0e17308",
81784242f88SMatthias Ringwald };
81884242f88SMatthias Ringwald char * message15_upper_transport_pdu = (char *) "0100";
81984242f88SMatthias Ringwald // ACK message, handled in mesh_transport - can be checked with test_control_receive_network_pdu
82084242f88SMatthias Ringwald // not sure - no upper access message
82184242f88SMatthias Ringwald // TEST(MessageTest, Message15Receive){
82284242f88SMatthias Ringwald //     load_network_key_nid_5e();
82384242f88SMatthias Ringwald //     mesh_set_iv_index(0x12345678);
82484242f88SMatthias Ringwald //     test_receive_network_pdus(1, message15_network_pdus, message15_lower_transport_pdus, message15_upper_transport_pdu);
82584242f88SMatthias Ringwald // }
82684242f88SMatthias Ringwald 
82784242f88SMatthias Ringwald // Message 16
82884242f88SMatthias Ringwald char * message16_network_pdus[] = {
82984242f88SMatthias Ringwald     (char *) "68e80e5da5af0e6b9be7f5a642f2f98680e61c3a8b47f228",
83084242f88SMatthias Ringwald };
83184242f88SMatthias Ringwald char * message16_lower_transport_pdus[] = {
83284242f88SMatthias Ringwald     (char *) "0089511bf1d1a81c11dcef",
83384242f88SMatthias Ringwald };
83484242f88SMatthias Ringwald char * message16_upper_transport_pdu = (char *) "800300563412";
83584242f88SMatthias Ringwald TEST(MessageTest, Message16Receive){
83684242f88SMatthias Ringwald     load_network_key_nid_68();
83784242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
83884242f88SMatthias Ringwald     test_receive_network_pdus(1, message16_network_pdus, message16_lower_transport_pdus, message16_upper_transport_pdu);
83984242f88SMatthias Ringwald }
84084242f88SMatthias Ringwald TEST(MessageTest, Message16Send){
84184242f88SMatthias Ringwald     uint16_t netkey_index = 0;
84284242f88SMatthias Ringwald     uint16_t appkey_index = MESH_DEVICE_KEY_INDEX;
84384242f88SMatthias Ringwald     uint8_t  ttl          = 0x0b;
84484242f88SMatthias Ringwald     uint16_t src          = 0x1201;
84584242f88SMatthias Ringwald     uint16_t dest         = 0x0003;
84684242f88SMatthias Ringwald     uint32_t seq          = 0x000006;
84784242f88SMatthias Ringwald     uint8_t  szmic        = 0;
84884242f88SMatthias Ringwald 
84984242f88SMatthias Ringwald     load_network_key_nid_68();
85084242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
85184242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
85284242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message16_upper_transport_pdu, 1, message16_lower_transport_pdus, message16_network_pdus);
85384242f88SMatthias Ringwald }
85484242f88SMatthias Ringwald 
85584242f88SMatthias Ringwald // Message 17
85684242f88SMatthias Ringwald // A Relay node receives the message from the Low Power node and relays it, decrementing the TTL value.
85784242f88SMatthias Ringwald // Message 18
85884242f88SMatthias Ringwald char * message18_network_pdus[] = {
85984242f88SMatthias Ringwald     (char *) "6848cba437860e5673728a627fb938535508e21a6baf57",
86084242f88SMatthias Ringwald };
86184242f88SMatthias Ringwald char * message18_lower_transport_pdus[] = {
86284242f88SMatthias Ringwald     (char *) "665a8bde6d9106ea078a",
86384242f88SMatthias Ringwald };
86484242f88SMatthias Ringwald char * message18_upper_transport_pdu = (char *) "0400000000";
86584242f88SMatthias Ringwald TEST(MessageTest, Message18Receive){
86684242f88SMatthias Ringwald     load_network_key_nid_68();
86784242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
86884242f88SMatthias Ringwald     test_receive_network_pdus(1, message18_network_pdus, message18_lower_transport_pdus, message18_upper_transport_pdu);
86984242f88SMatthias Ringwald }
87084242f88SMatthias Ringwald TEST(MessageTest, Message18Send){
87184242f88SMatthias Ringwald     uint16_t netkey_index = 0;
87284242f88SMatthias Ringwald     uint16_t appkey_index = 0;
87384242f88SMatthias Ringwald     uint8_t  ttl          = 3;
87484242f88SMatthias Ringwald     uint16_t src          = 0x1201;
87584242f88SMatthias Ringwald     uint16_t dest         = 0xffff;
87684242f88SMatthias Ringwald     uint32_t seq          = 0x00007;
87784242f88SMatthias Ringwald     uint8_t  szmic        = 0;
87884242f88SMatthias Ringwald 
87984242f88SMatthias Ringwald     load_network_key_nid_68();
88084242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
88184242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
88284242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message18_upper_transport_pdu, 1, message18_lower_transport_pdus, message18_network_pdus);
88384242f88SMatthias Ringwald }
88484242f88SMatthias Ringwald 
88584242f88SMatthias Ringwald 
88684242f88SMatthias Ringwald // Message 19
88784242f88SMatthias Ringwald // The Low Power node sends another Health Current Status message indicating that there are three faults:
88884242f88SMatthias Ringwald // Battery Low Warning, Power Supply Interrupted Warning, and Supply Voltage Too Low Warning.
88984242f88SMatthias Ringwald char * message19_network_pdus[] = {
89084242f88SMatthias Ringwald     (char *) "68110edeecd83c3010a05e1b23a926023da75d25ba91793736",
89184242f88SMatthias Ringwald };
89284242f88SMatthias Ringwald char * message19_lower_transport_pdus[] = {
89384242f88SMatthias Ringwald     (char *) "66ca6cd88e698d1265f43fc5",
89484242f88SMatthias Ringwald };
89584242f88SMatthias Ringwald char * message19_upper_transport_pdu = (char *) "04000000010703";
89684242f88SMatthias Ringwald TEST(MessageTest, Message19Receive){
89784242f88SMatthias Ringwald     load_network_key_nid_68();
89884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
89984242f88SMatthias Ringwald     test_receive_network_pdus(1, message19_network_pdus, message19_lower_transport_pdus, message19_upper_transport_pdu);
90084242f88SMatthias Ringwald }
90184242f88SMatthias Ringwald TEST(MessageTest, Message19Send){
90284242f88SMatthias Ringwald     uint16_t netkey_index = 0;
90384242f88SMatthias Ringwald     uint16_t appkey_index = 0;
90484242f88SMatthias Ringwald     uint8_t  ttl          = 3;
90584242f88SMatthias Ringwald     uint16_t src          = 0x1201;
90684242f88SMatthias Ringwald     uint16_t dest         = 0xffff;
90784242f88SMatthias Ringwald     uint32_t seq          = 0x00009;
90884242f88SMatthias Ringwald     uint8_t  szmic        = 0;
90984242f88SMatthias Ringwald 
91084242f88SMatthias Ringwald     load_network_key_nid_68();
91184242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
91284242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
91384242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message19_upper_transport_pdu, 1, message19_lower_transport_pdus, message19_network_pdus);
91484242f88SMatthias Ringwald }
91584242f88SMatthias Ringwald 
91684242f88SMatthias Ringwald // Message 20
91784242f88SMatthias Ringwald char * message20_network_pdus[] = {
91884242f88SMatthias Ringwald     (char *) "e85cca51e2e8998c3dc87344a16c787f6b08cc897c941a5368",
91984242f88SMatthias Ringwald };
92084242f88SMatthias Ringwald char * message20_lower_transport_pdus[] = {
92184242f88SMatthias Ringwald     (char *) "669c9803e110fea929e9542d",
92284242f88SMatthias Ringwald };
92384242f88SMatthias Ringwald char * message20_upper_transport_pdu = (char *) "04000000010703";
92484242f88SMatthias Ringwald TEST(MessageTest, Message20Receive){
92584242f88SMatthias Ringwald     load_network_key_nid_68();
92684242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
92784242f88SMatthias Ringwald     test_receive_network_pdus(1, message20_network_pdus, message20_lower_transport_pdus, message20_upper_transport_pdu);
92884242f88SMatthias Ringwald }
92984242f88SMatthias Ringwald TEST(MessageTest, Message20Send){
93084242f88SMatthias Ringwald     uint16_t netkey_index = 0;
93184242f88SMatthias Ringwald     uint16_t appkey_index = 0;
93284242f88SMatthias Ringwald     uint8_t  ttl          = 3;
93384242f88SMatthias Ringwald     uint16_t src          = 0x1234;
93484242f88SMatthias Ringwald     uint16_t dest         = 0xffff;
93584242f88SMatthias Ringwald     uint32_t seq          = 0x070809;
93684242f88SMatthias Ringwald     uint8_t  szmic        = 0;
93784242f88SMatthias Ringwald 
93884242f88SMatthias Ringwald     load_network_key_nid_68();
93984242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
94084242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
94184242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message20_upper_transport_pdu, 1, message20_lower_transport_pdus, message20_network_pdus);
94284242f88SMatthias Ringwald }
94384242f88SMatthias Ringwald 
94484242f88SMatthias Ringwald // Message 21
94584242f88SMatthias Ringwald // The Low Power node sends a vendor command to a group address.
94684242f88SMatthias Ringwald char * message21_network_pdus[] = {
94784242f88SMatthias Ringwald     (char *) "e84e8fbe003f58a4d61157bb76352ea6307eebfe0f30b83500e9",
94884242f88SMatthias Ringwald };
94984242f88SMatthias Ringwald char * message21_lower_transport_pdus[] = {
95084242f88SMatthias Ringwald     (char *) "664d92e9dfcf3ab85b6e8fcf03",
95184242f88SMatthias Ringwald };
95284242f88SMatthias Ringwald char * message21_upper_transport_pdu = (char *) "d50a0048656c6c6f";
95384242f88SMatthias Ringwald TEST(MessageTest, Message21Receive){
95484242f88SMatthias Ringwald     load_network_key_nid_68();
95584242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
95684242f88SMatthias Ringwald     test_receive_network_pdus(1, message21_network_pdus, message21_lower_transport_pdus, message21_upper_transport_pdu);
95784242f88SMatthias Ringwald }
95884242f88SMatthias Ringwald TEST(MessageTest, Message21Send){
95984242f88SMatthias Ringwald     uint16_t netkey_index = 0;
96084242f88SMatthias Ringwald     uint16_t appkey_index = 0;
96184242f88SMatthias Ringwald     uint8_t  ttl          = 3;
96284242f88SMatthias Ringwald     uint16_t src          = 0x1234;
96384242f88SMatthias Ringwald     uint16_t dest         = 0xc105;
96484242f88SMatthias Ringwald     uint32_t seq          = 0x07080a;
96584242f88SMatthias Ringwald     uint8_t  szmic        = 0;
96684242f88SMatthias Ringwald 
96784242f88SMatthias Ringwald     load_network_key_nid_68();
96884242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
96984242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
97084242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, dest, szmic, message21_upper_transport_pdu, 1, message21_lower_transport_pdus, message21_network_pdus);
97184242f88SMatthias Ringwald }
97284242f88SMatthias Ringwald 
97384242f88SMatthias Ringwald // Message 22
97484242f88SMatthias Ringwald char * message22_network_pdus[] = {
97584242f88SMatthias Ringwald     (char *) "e8d85caecef1e3ed31f3fdcf88a411135fea55df730b6b28e255",
97684242f88SMatthias Ringwald };
97784242f88SMatthias Ringwald char * message22_lower_transport_pdus[] = {
97884242f88SMatthias Ringwald     (char *) "663871b904d431526316ca48a0",
97984242f88SMatthias Ringwald };
98084242f88SMatthias Ringwald char * message22_upper_transport_pdu = (char *) "d50a0048656c6c6f";
98184242f88SMatthias Ringwald char * message22_label_string = (char *) "0073e7e4d8b9440faf8415df4c56c0e1";
98284242f88SMatthias Ringwald 
98384242f88SMatthias Ringwald TEST(MessageTest, Message22Receive){
98484242f88SMatthias Ringwald     load_network_key_nid_68();
98584242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
98684242f88SMatthias Ringwald     uint8_t label_uuid[16];
98784242f88SMatthias Ringwald     btstack_parse_hex(message22_label_string, 16, label_uuid);
98884242f88SMatthias Ringwald     mesh_virtual_address_register(label_uuid, 0xb529);
98984242f88SMatthias Ringwald     test_receive_network_pdus(1, message22_network_pdus, message22_lower_transport_pdus, message22_upper_transport_pdu);
99084242f88SMatthias Ringwald }
99184242f88SMatthias Ringwald 
99284242f88SMatthias Ringwald TEST(MessageTest, Message22Send){
99384242f88SMatthias Ringwald     uint16_t netkey_index = 0;
99484242f88SMatthias Ringwald     uint16_t appkey_index = 0;
99584242f88SMatthias Ringwald     uint8_t  ttl          = 3;
99684242f88SMatthias Ringwald     uint16_t src          = 0x1234;
99784242f88SMatthias Ringwald     uint32_t seq          = 0x07080b;
99884242f88SMatthias Ringwald     uint8_t  szmic        = 0;
99984242f88SMatthias Ringwald 
100084242f88SMatthias Ringwald     load_network_key_nid_68();
100184242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
100284242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
100384242f88SMatthias Ringwald     uint8_t label_uuid[16];
100484242f88SMatthias Ringwald     btstack_parse_hex(message22_label_string, 16, label_uuid);
100584242f88SMatthias Ringwald     mesh_virtual_address_t * virtual_address = mesh_virtual_address_register(label_uuid, 0xb529);
100684242f88SMatthias Ringwald     uint16_t pseudo_dst = virtual_address->pseudo_dst;
100784242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, pseudo_dst, szmic, message22_upper_transport_pdu, 1, message22_lower_transport_pdus, message22_network_pdus);
100884242f88SMatthias Ringwald }
100984242f88SMatthias Ringwald 
101084242f88SMatthias Ringwald // Message 23
101184242f88SMatthias Ringwald char * message23_network_pdus[] = {
101284242f88SMatthias Ringwald     (char *) "e877a48dd5fe2d7a9d696d3dd16a75489696f0b70c711b881385",
101384242f88SMatthias Ringwald };
101484242f88SMatthias Ringwald char * message23_lower_transport_pdus[] = {
101584242f88SMatthias Ringwald     (char *) "662456db5e3100eef65daa7a38",
101684242f88SMatthias Ringwald };
101784242f88SMatthias Ringwald char * message23_upper_transport_pdu = (char *) "d50a0048656c6c6f";
101884242f88SMatthias Ringwald char * message23_label_string = (char *) "f4a002c7fb1e4ca0a469a021de0db875";
101984242f88SMatthias Ringwald 
102084242f88SMatthias Ringwald TEST(MessageTest, Message23Receive){
102184242f88SMatthias Ringwald     load_network_key_nid_68();
102284242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
102384242f88SMatthias Ringwald     uint8_t label_uuid[16];
102484242f88SMatthias Ringwald     btstack_parse_hex(message23_label_string, 16, label_uuid);
102584242f88SMatthias Ringwald     mesh_virtual_address_register(label_uuid, 0x9736);
102684242f88SMatthias Ringwald     test_receive_network_pdus(1, message23_network_pdus, message23_lower_transport_pdus, message23_upper_transport_pdu);
102784242f88SMatthias Ringwald }
102884242f88SMatthias Ringwald TEST(MessageTest, Message23Send){
102984242f88SMatthias Ringwald     uint16_t netkey_index = 0;
103084242f88SMatthias Ringwald     uint16_t appkey_index = 0;
103184242f88SMatthias Ringwald     uint8_t  ttl          = 3;
103284242f88SMatthias Ringwald     uint16_t src          = 0x1234;
103384242f88SMatthias Ringwald     uint32_t seq          = 0x07080c;
103484242f88SMatthias Ringwald     uint8_t  szmic        = 0;
103584242f88SMatthias Ringwald 
103684242f88SMatthias Ringwald     load_network_key_nid_68();
103784242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
103884242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
103984242f88SMatthias Ringwald     uint8_t label_uuid[16];
104084242f88SMatthias Ringwald     btstack_parse_hex(message23_label_string, 16, label_uuid);
104184242f88SMatthias Ringwald     mesh_virtual_address_t * virtual_address = mesh_virtual_address_register(label_uuid, 0x9736);
104284242f88SMatthias Ringwald     uint16_t pseudo_dst = virtual_address->pseudo_dst;
104384242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, pseudo_dst, szmic, message23_upper_transport_pdu, 1, message23_lower_transport_pdus, message23_network_pdus);
104484242f88SMatthias Ringwald }
104584242f88SMatthias Ringwald #endif
104684242f88SMatthias Ringwald 
104784242f88SMatthias Ringwald // Message 24
104884242f88SMatthias Ringwald char * message24_network_pdus[] = {
104984242f88SMatthias Ringwald     (char *) "e8624e65bb8c1794e998b4081f47a35251fdd3896d99e4db489b918599",
105084242f88SMatthias Ringwald     (char *) "e8a7d0f0a2ea42dc2f4dd6fb4db33a6c088d023b47",
105184242f88SMatthias Ringwald };
105284242f88SMatthias Ringwald char * message24_lower_transport_pdus[] = {
105384242f88SMatthias Ringwald     (char *) "e6a03401c3c51d8e476b28e3aa5001f3",
105484242f88SMatthias Ringwald     (char *) "e6a034211c01cea6",
105584242f88SMatthias Ringwald };
105684242f88SMatthias Ringwald char * message24_upper_transport_pdu = (char *) "ea0a00576f726c64";
105784242f88SMatthias Ringwald char * message24_label_string = (char *) "f4a002c7fb1e4ca0a469a021de0db875";
105884242f88SMatthias Ringwald TEST(MessageTest, Message24Receive){
105984242f88SMatthias Ringwald     load_network_key_nid_68();
106084242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
106184242f88SMatthias Ringwald     uint8_t label_uuid[16];
106284242f88SMatthias Ringwald     btstack_parse_hex(message24_label_string, 16, label_uuid);
106384242f88SMatthias Ringwald     mesh_virtual_address_register(label_uuid, 0x9736);
106484242f88SMatthias Ringwald     test_receive_network_pdus(2, message24_network_pdus, message24_lower_transport_pdus, message24_upper_transport_pdu);
106584242f88SMatthias Ringwald }
106684242f88SMatthias Ringwald TEST(MessageTest, Message24Send){
106784242f88SMatthias Ringwald     uint16_t netkey_index = 0;
106884242f88SMatthias Ringwald     uint16_t appkey_index = 0;
106984242f88SMatthias Ringwald     uint8_t  ttl          = 3;
107084242f88SMatthias Ringwald     uint16_t src          = 0x1234;
107184242f88SMatthias Ringwald     uint32_t seq          = 0x07080d;
107284242f88SMatthias Ringwald     uint8_t  szmic        = 1;
107384242f88SMatthias Ringwald 
107484242f88SMatthias Ringwald     load_network_key_nid_68();
107584242f88SMatthias Ringwald     mesh_set_iv_index(0x12345677);
107684242f88SMatthias Ringwald     mesh_sequence_number_set(seq);
107784242f88SMatthias Ringwald     uint8_t label_uuid[16];
107884242f88SMatthias Ringwald     btstack_parse_hex(message24_label_string, 16, label_uuid);
107984242f88SMatthias Ringwald     mesh_virtual_address_t * virtual_address = mesh_virtual_address_register(label_uuid, 0x9736);
108084242f88SMatthias Ringwald     uint16_t pseudo_dst = virtual_address->pseudo_dst;
108184242f88SMatthias Ringwald     test_send_access_message(netkey_index, appkey_index, ttl, src, pseudo_dst, szmic, message24_upper_transport_pdu, 2, message24_lower_transport_pdus, message24_network_pdus);
108284242f88SMatthias Ringwald }
108384242f88SMatthias Ringwald 
108484242f88SMatthias Ringwald // Proxy Configuration Test
108584242f88SMatthias Ringwald char * proxy_config_pdus[] = {
108684242f88SMatthias Ringwald     (char *) "0210386bd60efbbb8b8c28512e792d3711f4b526",
108784242f88SMatthias Ringwald };
108884242f88SMatthias Ringwald char * proxy_config_lower_transport_pdus[] = {
108984242f88SMatthias Ringwald     (char *) "0000",
109084242f88SMatthias Ringwald };
109184242f88SMatthias Ringwald char * proxy_config_upper_transport_pdu = (char *) "ea0a00576f726c64";
109284242f88SMatthias Ringwald TEST(MessageTest, ProxyConfigReceive){
109384242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
109484242f88SMatthias Ringwald     load_network_key_nid_10();
109584242f88SMatthias Ringwald     int i = 0;
109684242f88SMatthias Ringwald     char ** network_pdus = proxy_config_pdus;
109784242f88SMatthias Ringwald     test_network_pdu_len = strlen(network_pdus[i]) / 2;
109884242f88SMatthias Ringwald     btstack_parse_hex(network_pdus[i], test_network_pdu_len, test_network_pdu_data);
109984242f88SMatthias Ringwald     mesh_network_process_proxy_configuration_message(&test_network_pdu_data[1], test_network_pdu_len-1);
110084242f88SMatthias Ringwald     while (received_proxy_pdu == NULL) {
110184242f88SMatthias Ringwald         mock_process_hci_cmd();
110284242f88SMatthias Ringwald     }
110384242f88SMatthias Ringwald     char ** lower_transport_pdus = proxy_config_lower_transport_pdus;
110484242f88SMatthias Ringwald     transport_pdu_len = strlen(lower_transport_pdus[i]) / 2;
110584242f88SMatthias Ringwald     btstack_parse_hex(lower_transport_pdus[i], transport_pdu_len, transport_pdu_data);
110684242f88SMatthias Ringwald 
110784242f88SMatthias Ringwald     uint8_t * lower_transport_pdu     = mesh_network_pdu_data(received_proxy_pdu);
110884242f88SMatthias Ringwald     uint8_t   lower_transport_pdu_len = mesh_network_pdu_len(received_proxy_pdu);
110984242f88SMatthias Ringwald 
111084242f88SMatthias Ringwald     // printf_hexdump(lower_transport_pdu, lower_transport_pdu_len);
111184242f88SMatthias Ringwald 
111284242f88SMatthias Ringwald     CHECK_EQUAL( transport_pdu_len, lower_transport_pdu_len);
111384242f88SMatthias Ringwald     CHECK_EQUAL_ARRAY(transport_pdu_data, lower_transport_pdu, transport_pdu_len);
111484242f88SMatthias Ringwald 
111584242f88SMatthias Ringwald     // done
111684242f88SMatthias Ringwald     mesh_network_message_processed_by_higher_layer(received_proxy_pdu);
111784242f88SMatthias Ringwald     received_proxy_pdu = NULL;
111884242f88SMatthias Ringwald }
111984242f88SMatthias Ringwald 
112084242f88SMatthias Ringwald 
112184242f88SMatthias Ringwald TEST(MessageTest, ProxyConfigSend){
112284242f88SMatthias Ringwald     uint16_t netkey_index = 0;
112384242f88SMatthias Ringwald     uint8_t  ctl          = 1;
112484242f88SMatthias Ringwald     uint8_t  ttl          = 0;
112584242f88SMatthias Ringwald     uint16_t src          = 1;
112684242f88SMatthias Ringwald     uint16_t dest         = 0;
112784242f88SMatthias Ringwald     uint32_t seq          = 1;
112884242f88SMatthias Ringwald     uint8_t  nid          = 0x10;
112984242f88SMatthias Ringwald     mesh_set_iv_index(0x12345678);
113084242f88SMatthias Ringwald     load_network_key_nid_10();
113184242f88SMatthias Ringwald     mesh_network_pdu_t * network_pdu = mesh_network_pdu_get();
113284242f88SMatthias Ringwald     uint8_t data[] = { 0 , 0 };
113384242f88SMatthias Ringwald     mesh_network_setup_pdu(network_pdu, netkey_index, nid, ctl, ttl, seq, src, dest, data, sizeof(data));
11341aa2a11eSMatthias Ringwald     mesh_network_encrypt_proxy_configuration_message(network_pdu);
113584242f88SMatthias Ringwald     while (received_proxy_pdu == NULL) {
113684242f88SMatthias Ringwald         mock_process_hci_cmd();
113784242f88SMatthias Ringwald     }
113884242f88SMatthias Ringwald     uint8_t * proxy_pdu_data  = received_proxy_pdu->data;
113984242f88SMatthias Ringwald     uint8_t   proxy_pdu_len   = received_proxy_pdu->len;
114084242f88SMatthias Ringwald 
114184242f88SMatthias Ringwald     int i = 0;
114284242f88SMatthias Ringwald     char ** network_pdus = proxy_config_pdus;
114384242f88SMatthias Ringwald     transport_pdu_len = strlen(network_pdus[i]) / 2;
114484242f88SMatthias Ringwald     btstack_parse_hex(network_pdus[i], transport_pdu_len, transport_pdu_data);
114584242f88SMatthias Ringwald 
114684242f88SMatthias Ringwald     CHECK_EQUAL( transport_pdu_len-1, proxy_pdu_len);
114784242f88SMatthias Ringwald     CHECK_EQUAL_ARRAY(transport_pdu_data+1, proxy_pdu_data, transport_pdu_len-1);
114884242f88SMatthias Ringwald 
114984242f88SMatthias Ringwald     received_proxy_pdu = NULL;
115084242f88SMatthias Ringwald 
115184242f88SMatthias Ringwald     mesh_network_pdu_free(network_pdu);
115284242f88SMatthias Ringwald }
115384242f88SMatthias Ringwald 
115484242f88SMatthias Ringwald static btstack_crypto_aes128_t crypto_request_aes128;
115584242f88SMatthias Ringwald static uint8_t plaintext[16];
115684242f88SMatthias Ringwald static uint8_t identity_key[16];
115784242f88SMatthias Ringwald static uint8_t hash[16];
115884242f88SMatthias Ringwald static uint8_t random_value[8];
115984242f88SMatthias Ringwald 
116084242f88SMatthias Ringwald static void mesh_proxy_handle_get_aes128(void * arg){
116184242f88SMatthias Ringwald     UNUSED(arg);
116284242f88SMatthias Ringwald     uint8_t expected_hash[8];
116384242f88SMatthias Ringwald     uint8_t expected_random_value[8];
116484242f88SMatthias Ringwald 
116584242f88SMatthias Ringwald     btstack_parse_hex("00861765aefcc57b", 8, expected_hash);
116684242f88SMatthias Ringwald     CHECK_EQUAL_ARRAY(&hash[8], expected_hash, 8);
116784242f88SMatthias Ringwald 
116884242f88SMatthias Ringwald     btstack_parse_hex("34ae608fbbc1f2c6", 8, expected_random_value);
116984242f88SMatthias Ringwald     CHECK_EQUAL_ARRAY(random_value, expected_random_value, 8);
117084242f88SMatthias Ringwald }
117184242f88SMatthias Ringwald 
117284242f88SMatthias Ringwald TEST(MessageTest, ServiceDataUsingNodeIdentityTest){
117384242f88SMatthias Ringwald     btstack_parse_hex("34ae608fbbc1f2c6", 8, random_value);
117484242f88SMatthias Ringwald     memset(plaintext, 0, sizeof(plaintext));
117584242f88SMatthias Ringwald     memcpy(&plaintext[6] , random_value, 8);
117684242f88SMatthias Ringwald     big_endian_store_16(plaintext, 14, 0x1201);
117784242f88SMatthias Ringwald     // 84396c435ac48560b5965385253e210c
117884242f88SMatthias Ringwald     btstack_parse_hex("84396c435ac48560b5965385253e210c", 16, identity_key);
117984242f88SMatthias Ringwald     btstack_crypto_aes128_encrypt(&crypto_request_aes128, identity_key, plaintext, hash, mesh_proxy_handle_get_aes128, NULL);
118084242f88SMatthias Ringwald }
118184242f88SMatthias Ringwald 
118284242f88SMatthias Ringwald // Mesh v1.0, 8.2.1
118384242f88SMatthias Ringwald static btstack_crypto_aes128_cmac_t aes_cmac_request;
118484242f88SMatthias Ringwald static uint8_t k4_result[1];
118584242f88SMatthias Ringwald static void handle_k4_result(void *arg){
118684242f88SMatthias Ringwald     printf("ApplicationkeyIDTest: %02x\n", k4_result[0]);
118784242f88SMatthias Ringwald     CHECK_EQUAL( 0x26, k4_result[0]);
118884242f88SMatthias Ringwald }
118984242f88SMatthias Ringwald TEST(MessageTest, ApplicationkeyIDTest){
119084242f88SMatthias Ringwald     static uint8_t application_key[16];
119184242f88SMatthias Ringwald     btstack_parse_hex("63964771734fbd76e3b40519d1d94a48", 16, application_key);
119284242f88SMatthias Ringwald     mesh_k4(&aes_cmac_request, application_key, &k4_result[0], &handle_k4_result, NULL);
119384242f88SMatthias Ringwald }
119484242f88SMatthias Ringwald 
119584242f88SMatthias Ringwald int main (int argc, const char * argv[]){
119684242f88SMatthias Ringwald     return CommandLineTestRunner::RunAllTests(argc, argv);
119784242f88SMatthias Ringwald }
1198