1 /*
2  * Copyright 2018 Oticon A/S
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 #include <stdlib.h>
7 #include <string.h>
8 #include "bs_pc_2G4_types.h"
9 #include "bs_oswrap.h"
10 #include "bs_utils.h"
11 #include "p2G4_pending_tx_rx_list.h"
12 
13 tx_l_c_t tx_l_c;
14 static tx_el_t *tx_list = NULL;
15 
16 static uint nbr_devs;
17 static int max_tx_nbr; //highest device transmitting at this point
18 
txl_create(uint n_devs)19 void txl_create(uint n_devs){
20   tx_l_c.tx_list = bs_calloc(n_devs, sizeof(tx_el_t));
21   tx_l_c.used = bs_calloc(n_devs, sizeof(uint));
22   tx_l_c.ctr = 0;
23   tx_list = tx_l_c.tx_list;
24   nbr_devs = n_devs;
25   max_tx_nbr = -1;
26 }
27 
txl_free(void)28 void txl_free(void){
29   if ( tx_l_c.tx_list != NULL ) {
30     for (int d = 0 ; d < nbr_devs; d++){
31       if ( tx_list[d].packet != NULL ) {
32         free(tx_list[d].packet);
33       }
34     }
35     free(tx_l_c.tx_list);
36     free(tx_l_c.used);
37   }
38 }
39 
40 /**
41  * Register a tx which has just been initiated by a device
42  * Note that the tx itself does not start yet (when that happens txl_activate() should be called)
43  */
txl_register(uint d,p2G4_txv2_t * tx_s,uint8_t * packet)44 void txl_register(uint d, p2G4_txv2_t *tx_s, uint8_t* packet){
45   tx_l_c.used[d] = TXS_OFF;
46   memcpy(&(tx_list[d].tx_s), tx_s, sizeof(p2G4_txv2_t) );
47   tx_list[d].packet = packet;
48 }
49 
50 /**
51  * Activate a given tx in the tx_list_c (we have reached the begining of the Tx)
52  */
txl_start_tx(uint d)53 void txl_start_tx(uint d){
54   tx_l_c.used[d] = TXS_NOISE;
55   tx_l_c.ctr++;
56   max_tx_nbr = BS_MAX(max_tx_nbr, ((int)d));
57 }
58 
59 /**
60  * Activate a given tx in the tx_list_c (we have reached the begining of the Tx)
61  */
txl_start_packet(uint d)62 void txl_start_packet(uint d){
63   tx_l_c.used[d] |= TXS_PACKET_ONGOING | TXS_PACKET_STARTED;
64   /*Note: No need to update the counter, as the interference level is the same with or without packet*/
65 }
66 
67 /**
68  * Mark that the packet has ended (noise may continue)
69  */
txl_end_packet(uint d)70 void txl_end_packet(uint d){
71   tx_l_c.used[d] |= TXS_PACKET_ENDED;
72   tx_l_c.used[d] &= ~TXS_PACKET_ONGOING;
73   /*Note: No need to update the counter, as the interference level is the same with or without packet*/
74 }
75 
76 /**
77  * A given tx has just ended
78  */
txl_clear(uint d)79 void txl_clear(uint d){
80   tx_l_c.used[d] = TXS_OFF;
81   if (tx_list[d].packet != NULL) {
82     free(tx_list[d].packet);
83     tx_list[d].packet = NULL;
84   }
85   tx_l_c.ctr++;
86 
87   for (int i = max_tx_nbr; i >= 0 ; i--){
88     if (tx_l_c.used[i]){
89       max_tx_nbr = i;
90       return;
91     }
92   }
93   max_tx_nbr = -1;//if we didnt find any
94 }
95 
txl_get_max_tx_nbr(void)96 int txl_get_max_tx_nbr(void){
97   return max_tx_nbr;
98 }
99