1 /*
2  * Copyright (c) 2017 - 2023, 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 module implements an acknowledgement generator for 802.15.4 radio driver.
38  *
39  */
40 
41 #include "nrf_802154_ack_generator.h"
42 
43 #include <assert.h>
44 
45 #include "nrf_802154_const.h"
46 #include "nrf_802154_enh_ack_generator.h"
47 #include "nrf_802154_imm_ack_generator.h"
48 
49 typedef enum
50 {
51     FRAME_VERSION_BELOW_2015,
52     FRAME_VERSION_2015_OR_ABOVE,
53     FRAME_VERSION_INVALID
54 } frame_version_t;
55 
frame_version_is_2015_or_above(const nrf_802154_frame_parser_data_t * p_frame_data)56 static frame_version_t frame_version_is_2015_or_above(
57     const nrf_802154_frame_parser_data_t * p_frame_data)
58 {
59     switch (nrf_802154_frame_parser_frame_version_get(p_frame_data))
60     {
61         case FRAME_VERSION_0:
62         case FRAME_VERSION_1:
63             return FRAME_VERSION_BELOW_2015;
64 
65         case FRAME_VERSION_2:
66             return FRAME_VERSION_2015_OR_ABOVE;
67 
68         default:
69             return FRAME_VERSION_INVALID;
70     }
71 }
72 
nrf_802154_ack_generator_init(void)73 void nrf_802154_ack_generator_init(void)
74 {
75     // Both generators are initialized to enable sending both Imm-Acks and Enh-Acks.
76     nrf_802154_imm_ack_generator_init();
77     nrf_802154_enh_ack_generator_init();
78 }
79 
nrf_802154_ack_generator_reset(void)80 void nrf_802154_ack_generator_reset(void)
81 {
82     // Both generators are reset to enable sending both Imm-Ack and Enh-Ack.
83     nrf_802154_imm_ack_generator_reset();
84     nrf_802154_enh_ack_generator_reset();
85 }
86 
nrf_802154_ack_generator_create(const nrf_802154_frame_parser_data_t * p_frame_data)87 uint8_t * nrf_802154_ack_generator_create(const nrf_802154_frame_parser_data_t * p_frame_data)
88 {
89     // This function should not be called if ACK is not requested.
90     assert(nrf_802154_frame_parser_ar_bit_is_set(p_frame_data));
91 
92     switch (frame_version_is_2015_or_above(p_frame_data))
93     {
94         case FRAME_VERSION_BELOW_2015:
95             return nrf_802154_imm_ack_generator_create(p_frame_data);
96 
97         case FRAME_VERSION_2015_OR_ABOVE:
98             return nrf_802154_enh_ack_generator_create(p_frame_data);
99 
100         default:
101             return NULL;
102     }
103 }
104