CU_TEST_END CU_TEST_START(misc_sd_power_system_off) { nrf_cunit_mock_call * p_sd_mock_call; nrf_cunit_mock_call * p_transport_read_mock_call; nrf_cunit_mock_call * p_transport_consume_mock_call; nrf_cunit_expect_call_return((uint8_t *) "sd_power_system_off", &p_sd_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_extract", &p_transport_read_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_consume", &p_transport_consume_mock_call); p_transport_consume_mock_call->compare_rule[0] = COMPARE_ANY; p_sd_mock_call->arg[0] = 0x56; uint8_t in_data[] = {BLE_RPC_PKT_CMD, // Packet type SD_POWER_SYSTEM_OFF}; // Opcode p_transport_read_mock_call->arg[0] = BLE_RPC_PKT_CMD; p_transport_read_mock_call->result[0] = (uint32_t)in_data; p_transport_read_mock_call->result[1] = sizeof(in_data); ble_rpc_cmd_handle(NULL, 0); nrf_cunit_verify_call_return(); }
CU_TEST_END /* This test case will make the SoftDevice return error to the command decoder module and will * test if this error is encoded properly in the command response. */ CU_TEST_START(misc_soft_device_retuns_error) { // Preparation of expected output nrf_cunit_mock_call * p_sd_call_mock_obj; nrf_cunit_mock_call * p_transport_read_mock_call; nrf_cunit_mock_call * p_transport_write_mock_call; nrf_cunit_mock_call * p_transport_alloc_mock_call; nrf_cunit_mock_call * p_transport_consume_mock_call; nrf_cunit_expect_call_return((uint8_t *)"sd_ble_gap_adv_data_set", &p_sd_call_mock_obj); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_extract", &p_transport_read_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_pkt_write", &p_transport_write_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_tx_alloc", &p_transport_alloc_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_consume", &p_transport_consume_mock_call); p_transport_consume_mock_call->compare_rule[0] = COMPARE_ANY; uint8_t buffer[40]; p_transport_alloc_mock_call->result[0] = (uint32_t)buffer; uint8_t in_data[] = {BLE_RPC_PKT_CMD, // Packet type SD_BLE_GAP_ADV_DATA_SET, // Opcode 31, // Adv data len 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, // Adv data 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0 // SR data len }; p_sd_call_mock_obj->error = NRF_ERROR_BUSY; // Expected Transport Layer API call expected_transport_api_setup(p_transport_write_mock_call, SD_BLE_GAP_ADV_DATA_SET, NRF_ERROR_BUSY, BLE_RPC_PKT_RESP); p_transport_read_mock_call->arg[0] = BLE_RPC_PKT_CMD; p_transport_read_mock_call->result[0] = (uint32_t)in_data; p_transport_read_mock_call->result[1] = sizeof(in_data); ble_rpc_cmd_handle(NULL, 0); nrf_cunit_verify_call_return(); }
CU_TEST_END CU_TEST_START(cmd_decoder_alloc_error_in_command_resp_send_should_wait_for_free_buffer) { // Preparation of expected output nrf_cunit_mock_call * p_mock_call; /*** Data that is received on from the transport layer. ***********/ uint8_t in_data[] = {BLE_RPC_PKT_CMD, // Packet type 0xFF, // INVALID opcode 0x23, // Some data. 0x04, 0x00, 0x05, 0x06, 0x07, 0x08}; /*** Data has been received, and a call to pkt_extract is expected, return received data. ***/ nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_extract", &p_mock_call); p_mock_call->arg[0] = BLE_RPC_PKT_CMD; p_mock_call->result[0] = (uint32_t)in_data; p_mock_call->result[1] = sizeof(in_data); /************* A response should be returned, allocate a tx buffer. *************/ /*************** No buffer is available, so a NO_MEM is returned. ***************/ nrf_cunit_expect_call_return((uint8_t *)"hci_transport_tx_alloc", &p_mock_call); p_mock_call->error = NRF_ERROR_NO_MEM; /************* A response should be returned, allocate a tx buffer. *************/ /***********Second call to alloc returns a buffer which is now free. ************/ uint8_t buffer[20]; nrf_cunit_expect_call_return((uint8_t *)"hci_transport_tx_alloc", &p_mock_call); p_mock_call->result[0] = (uint32_t)buffer; /********************* Setting up the expected response. *********************/ nrf_cunit_expect_call_return((uint8_t *)"hci_transport_pkt_write", &p_mock_call); expected_transport_api_setup(p_mock_call, 0xFF, NRF_ERROR_NOT_SUPPORTED, BLE_RPC_PKT_RESP); /************* When cmd response has been send, the rx buffer should be freed. *************/ nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_consume", &p_mock_call); p_mock_call->compare_rule[0] = COMPARE_ANY; ble_rpc_cmd_handle(NULL, 0); nrf_cunit_verify_call_return(); }
CU_TEST_END /* This test case will test if the unrecognised command is properly responded to with a command * response with error code set to NRF_ERROR_NOT_SUPPORTED. */ CU_TEST_START(misc_unsupported_command) { // Preparation of expected output nrf_cunit_mock_call * p_transport_read_mock_call; nrf_cunit_mock_call * p_transport_write_mock_call; nrf_cunit_mock_call * p_transport_alloc_mock_call; nrf_cunit_mock_call * p_transport_consume_mock_call; nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_extract", &p_transport_read_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_pkt_write", &p_transport_write_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_tx_alloc", &p_transport_alloc_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_consume", &p_transport_consume_mock_call); p_transport_consume_mock_call->compare_rule[0] = COMPARE_ANY; uint8_t buffer[20]; p_transport_alloc_mock_call->result[0] = (uint32_t)buffer; // Expected Transport Layer API call expected_transport_api_setup(p_transport_write_mock_call, 0xFF, // INVALID_OPCODE NRF_ERROR_NOT_SUPPORTED, BLE_RPC_PKT_RESP); // Stimuli uint8_t in_data[] = {BLE_RPC_PKT_CMD, // Packet type 0xFF, // INVALID Opcode 0x23, // Some data. 0x04, 0x00, 0x05, 0x06, 0x07, 0x08}; p_transport_read_mock_call->arg[0] = BLE_RPC_PKT_CMD; p_transport_read_mock_call->result[0] = (uint32_t)in_data; p_transport_read_mock_call->result[1] = sizeof(in_data); ble_rpc_cmd_handle(NULL, 0); nrf_cunit_verify_call_return(); }
CU_TEST_END // This test case is intended to test that if an invalid (non-existing/unsupport) gap event is // received, then sd_ble_evt_get(...) shall return NRF_ERROR_NOT_FOUND. CU_TEST_START(gatts_evt_unsupported) { // Place holder for the actual decoded event length, size is 2 less (Too small). ble_evt_t actual_ble_evt = {{0}, {{0}}}; uint16_t actual_ble_evt_len = sizeof(actual_ble_evt); /**************************** Creation of the encoded input data ****************************/ /** The connected event as it looks on the wire (UART) */ uint8_t encoded_gatts_unsupported_evt[] = { BLE_RPC_PKT_EVT, (BLE_GATTS_EVT_LAST - 1), 0, // Evt ID - Doesn't exist. 0xaa, 0xbb, // Some data. 0xcc, 0xdd // Some data. }; uint16_t encoded_packet_length = 7; /**************************** Creation of the expected event ****************************/ /** We only validate that length can is returned correctly, without destroying the event. */ // NONE Needed as we verify unaligned pointer behavior. /**************************** Execution of the test / Verify decoder behavior ****************************/ // Simulate an event from the transport layer. -- Should stimulate an event from hci_transmport (evt. dispatcher) with pointer to RX buffer. // Execute the call to the decoder with the encoded data. // This should trigger an interrupt, and the data can be retrieved with sd_ble_evt_get(...) (void) ble_rpc_event_pkt_received(encoded_gatts_unsupported_evt, encoded_packet_length); uint32_t err_code = sd_ble_evt_get((uint8_t *)(&actual_ble_evt), &actual_ble_evt_len); CU_ASSERT_INT_EQUAL(err_code, NRF_ERROR_NOT_FOUND); CU_ASSERT_INT_EQUAL(actual_ble_evt_len, 0); nrf_cunit_verify_call_return(); }
static void ble_teardown_func(void) { nrf_cunit_verify_call_return(); }
CU_TEST_END CU_TEST_START(cmd_decoder_alloc_error_in_command_resp_send_with_data_should_wait_for_free_buffer) { uint16_t uuid_gatt = 0x1801; uint8_t uuid_type = BLE_UUID_TYPE_BLE; nrf_cunit_mock_call * p_mock_call; nrf_cunit_mock_call * p_sd_call_mock_obj; nrf_cunit_mock_call * p_transport_read_mock_call; nrf_cunit_mock_call * p_transport_alloc_mock_call; nrf_cunit_mock_call * p_transport_consume_mock_call; nrf_cunit_expect_call_return((uint8_t *)"sd_ble_uuid_encode", &p_sd_call_mock_obj); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_extract", &p_transport_read_mock_call); nrf_cunit_expect_call_return((uint8_t *)"hci_transport_tx_alloc", &p_transport_alloc_mock_call); p_transport_alloc_mock_call->error = NRF_ERROR_NO_MEM; uint8_t buffer[20]; nrf_cunit_expect_call_return((uint8_t *)"hci_transport_tx_alloc", &p_mock_call); p_mock_call->result[0] = (uint32_t)buffer; nrf_cunit_expect_call_return((uint8_t *)"hci_transport_rx_pkt_consume", &p_transport_consume_mock_call); p_transport_consume_mock_call->compare_rule[0] = COMPARE_ANY; p_transport_alloc_mock_call->error = NRF_ERROR_NO_MEM; /********************* Setting up the expected response. *********************/ uint8_t expected_transport_op_code = SD_BLE_UUID_ENCODE; uint32_t expected_transport_err_code = NRF_SUCCESS; uint8_t expected_transport_data[] = {BLE_RPC_PKT_RESP, // Packet type expected_transport_op_code, // op code (expected_transport_err_code & 0x000000ffu) >> 0, // error code (expected_transport_err_code & 0x0000ff00u) >> 8, // error code (expected_transport_err_code & 0x00ff0000u) >> 16, // error code (expected_transport_err_code & 0xff000000u) >> 24, // error code 2, // length of encoded UUID 0x01, 0x18}; // encoded UUID nrf_cunit_expect_call_return((uint8_t *)"hci_transport_pkt_write", &p_mock_call); p_mock_call->arg[1] = (uint32_t)expected_transport_data; p_mock_call->arg[2] = sizeof(expected_transport_data); // Expected SoftDevice call ble_uuid_t uuid_data; uuid_data.uuid = 0x1801; uuid_data.type = uuid_type; p_sd_call_mock_obj->arg[0] = (uint32_t)&uuid_data; p_sd_call_mock_obj->result[0] = 2; uint8_t in_data[] = {BLE_RPC_PKT_CMD, // Packet type SD_BLE_UUID_ENCODE, // Opcode 1, // ble_uuid present (not null) uuid_gatt & 0xFF, uuid_gatt >> 8, // ble_uuid_t->uuid uuid_type, // ble_uuid_t->type 1, // length pointer present (not null) 1}; // result buffer present (not null) p_transport_read_mock_call->arg[0] = BLE_RPC_PKT_CMD; p_transport_read_mock_call->result[0] = (uint32_t)in_data; p_transport_read_mock_call->result[1] = sizeof(in_data); ble_rpc_cmd_handle(NULL, 0); nrf_cunit_verify_call_return(); (void) uuid_data; }
void teardown_func(void) { // ----- Verifying the mock calls. nrf_cunit_verify_call_return(); }
static void event_decoder_suite_teardown(void) { nrf_cunit_verify_call_return(); }
CU_TEST_END CU_TEST_START(gatts_evt_write) { // Place holder for the actual decoded event. ble_evt_t actual_ble_evt = {{0}, {{0}}}; uint16_t actual_len = sizeof(actual_ble_evt); /**************************** Creation of the encoded input data ****************************/ /** The connected event as it looks on the wire (UART) */ uint8_t encoded_gatts_write_evt[] = { BLE_RPC_PKT_EVT, BLE_GATTS_EVT_WRITE, 0, // Evt ID 0x90, 0x78, // Conn Handle 0x56, 0x34, // Attribute handle 0x12, // Operation 0x01, 0x10, // service uuid 0x20, // service uuid type 0x03, 0x30, // char uuid 0x40, // char uuid type 0x05, 0x50, // desc uuid 0x60, // desc uuid type 0x07, 0x70, // Service handle 0x08, 0x80, // Value handle 0x90, // Attribute type 0x45, 0x54, // Offset 0x05, 0x00, // Data length 0x99, 0x88, 0x77, 0x66, 0x55 // Data }; uint16_t encoded_packet_length = 31; /**************************** Creation of the expected event ****************************/ /** The BLE event as it should look when it has been decoded and fetch by the application. */ uint32_t event_buffer[80 / sizeof(uint32_t)]; ble_evt_t * expected_ble_evt = (ble_evt_t *)event_buffer; memset(event_buffer, 0, 80); expected_ble_evt->header.evt_id = BLE_GATTS_EVT_WRITE; expected_ble_evt->header.evt_len = 33; expected_ble_evt->evt.gatts_evt.conn_handle = 0x7890; expected_ble_evt->evt.gatts_evt.params.write.handle = 0x3456; expected_ble_evt->evt.gatts_evt.params.write.op = 0x12; // context expected_ble_evt->evt.gatts_evt.params.write.context.srvc_uuid.uuid = 0x1001; expected_ble_evt->evt.gatts_evt.params.write.context.srvc_uuid.type = 0x20; expected_ble_evt->evt.gatts_evt.params.write.context.char_uuid.uuid = 0x3003; expected_ble_evt->evt.gatts_evt.params.write.context.char_uuid.type = 0x40; expected_ble_evt->evt.gatts_evt.params.write.context.desc_uuid.uuid = 0x5005; expected_ble_evt->evt.gatts_evt.params.write.context.desc_uuid.type = 0x60; expected_ble_evt->evt.gatts_evt.params.write.context.srvc_handle = 0x7007; expected_ble_evt->evt.gatts_evt.params.write.context.value_handle = 0x8008; expected_ble_evt->evt.gatts_evt.params.write.context.type = 0x90; expected_ble_evt->evt.gatts_evt.params.write.offset = 0x5445; expected_ble_evt->evt.gatts_evt.params.write.len = 0x5; expected_ble_evt->evt.gatts_evt.params.write.data[0] = 0x99; expected_ble_evt->evt.gatts_evt.params.write.data[1] = 0x88; //lint !e415 !e416 expected_ble_evt->evt.gatts_evt.params.write.data[2] = 0x77; //lint !e415 !e416 expected_ble_evt->evt.gatts_evt.params.write.data[3] = 0x66; //lint !e415 !e416 expected_ble_evt->evt.gatts_evt.params.write.data[4] = 0x55; //lint !e415 !e416 uint16_t expected_len = (uint16_t)(offsetof(ble_evt_t, evt.gatts_evt.params.write.data)); expected_len += expected_ble_evt->evt.gatts_evt.params.write.len; // One byte is already counted in the struct itself. /**************************** Execution of the test / Verify decoder behavior ****************************/ // Simulate an event from the transport layer. -- Should stimulate an event from hci_transmport (evt. dispatcher) with pointer to RX buffer. // Execute the call to the decoder with the encoded data. // This should trigger an interrupt, and the data can be retrieved with sd_ble_evt_get(...) (void) ble_rpc_event_pkt_received(encoded_gatts_write_evt, encoded_packet_length); // Fetch the decoded event and compare it to the expected. uint32_t err_code = sd_ble_evt_get((uint8_t *)(&actual_ble_evt), &actual_len); CU_ASSERT_INT_EQUAL(err_code, NRF_SUCCESS); CU_ASSERT(compare_ble_events(expected_ble_evt, expected_len, &actual_ble_evt, actual_len) == true); nrf_cunit_verify_call_return(); // CU_ASSERT(pull_and_verify_ble_evt(expected_ble_evt, expected_len) == true); }