1 /*
2  * Copyright (c) 2017, Nordic Semiconductor ASA
3  * All rights reserved.
4  *
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * 1. Redistributions of source code must retain the above copyright notice, this
11  *    list of conditions and the following disclaimer.
12  *
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
18  *    contributors may be used to endorse or promote products derived from this
19  *    software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY, AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  *
33  */
34 
35 /**
36  * @file
37  *   This file implements an immediate acknowledgement (Imm-Ack) generator for 802.15.4 radio driver.
38  *
39  */
40 
41 #include "nrf_802154_imm_ack_generator.h"
42 
43 #include <string.h>
44 
45 #include "nrf_802154_ack_data.h"
46 #include "nrf_802154_const.h"
47 #if NRF_802154_IE_WRITER_ENABLED
48 #include "mac_features/nrf_802154_ie_writer.h"
49 #endif
50 
51 #define IMM_ACK_INITIALIZER {IMM_ACK_LENGTH, ACK_HEADER_WITH_PENDING, 0x00, 0x00, 0x00, 0x00}
52 
53 static uint8_t m_ack_data[IMM_ACK_LENGTH + PHR_SIZE];
54 
nrf_802154_imm_ack_generator_init(void)55 void nrf_802154_imm_ack_generator_init(void)
56 {
57     const uint8_t ack_data[] = IMM_ACK_INITIALIZER;
58 
59     memcpy(m_ack_data, ack_data, sizeof(ack_data));
60 }
61 
nrf_802154_imm_ack_generator_reset(void)62 void nrf_802154_imm_ack_generator_reset(void)
63 {
64     // Intentionally empty
65 }
66 
nrf_802154_imm_ack_generator_create(const nrf_802154_frame_parser_data_t * p_frame_data)67 uint8_t * nrf_802154_imm_ack_generator_create(
68     const nrf_802154_frame_parser_data_t * p_frame_data)
69 {
70     #if NRF_802154_IE_WRITER_ENABLED
71     // The IE writer module can be in the IE_WRITER_PREPARE state if
72     // the previous transmission failed at an early stage.
73     // Reset it, to avoid data corruption in when this ACK is transmitted.
74     // Otherwise, the IE writer would commit data in nrf_802154_ie_writer_tx_ack_started_hook
75     // regardless if writing of IE elements is needed or not.
76     nrf_802154_ie_writer_reset();
77     #endif
78 
79     if (nrf_802154_frame_parser_parse_level_get(p_frame_data) < PARSE_LEVEL_FULL)
80     {
81         // The entire frame being acknowledged is necessary to correctly generate Ack
82         return NULL;
83     }
84 
85     const uint8_t * frame_dsn = nrf_802154_frame_parser_dsn_get(p_frame_data);
86 
87     if (frame_dsn == NULL)
88     {
89         return NULL;
90     }
91 
92     // Set valid sequence number in ACK frame.
93     m_ack_data[DSN_OFFSET] = *frame_dsn;
94 
95     // Set pending bit in ACK frame.
96     if (nrf_802154_ack_data_pending_bit_should_be_set(p_frame_data))
97     {
98         m_ack_data[FRAME_PENDING_OFFSET] = ACK_HEADER_WITH_PENDING;
99     }
100     else
101     {
102         m_ack_data[FRAME_PENDING_OFFSET] = ACK_HEADER_WITHOUT_PENDING;
103     }
104 
105     return m_ack_data;
106 }
107