1 /*
2 * Copyright 2018 Oticon A/S
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <stddef.h>
7 #include <unistd.h>
8 #include "bs_tracing.h"
9 #include "bs_pc_2G4_types.h"
10 #include "bs_pc_2G4_priv.h"
11 #include "bs_pc_base.h"
12 #include "bs_oswrap.h"
13
p2G4_dev_req_tx_i(pb_dev_state_t * pb_dev_state,p2G4_tx_t * s,uint8_t * buf)14 void p2G4_dev_req_tx_i(pb_dev_state_t *pb_dev_state, p2G4_tx_t *s,
15 uint8_t *buf)
16 {
17 pb_send_msg(pb_dev_state->ff_dtp, P2G4_MSG_TX, (void *)s, sizeof(p2G4_tx_t));
18 pb_send_payload(pb_dev_state->ff_dtp, buf, s->packet_size);
19 }
20
p2G4_dev_req_txv2_i(pb_dev_state_t * pb_dev_state,p2G4_txv2_t * s,uint8_t * buf)21 void p2G4_dev_req_txv2_i(pb_dev_state_t *pb_dev_state, p2G4_txv2_t *s,
22 uint8_t *buf)
23 {
24 pb_send_msg(pb_dev_state->ff_dtp, P2G4_MSG_TXV2, (void *)s, sizeof(p2G4_txv2_t));
25 pb_send_payload(pb_dev_state->ff_dtp, buf, s->packet_size);
26 }
27
p2G4_dev_handle_tx_resp_i(pb_dev_state_t * pb_dev_state,pc_header_t header,p2G4_tx_done_t * tx_done_s)28 int p2G4_dev_handle_tx_resp_i(pb_dev_state_t *pb_dev_state, pc_header_t header,
29 p2G4_tx_done_t *tx_done_s)
30 {
31 int ret;
32 if (header == P2G4_MSG_TX_END) {
33 ret = pb_dev_read(pb_dev_state, tx_done_s, sizeof(p2G4_tx_done_t));
34 if (ret == -1)
35 return -1;
36 else
37 return 0;
38 } else if (header == PB_MSG_DISCONNECT) {
39 pb_dev_clean_up(pb_dev_state);
40 return -1;
41 } else {
42 INVALID_RESP(header);
43 return -1;
44 }
45 }
46
p2G4_dev_req_cca_i(pb_dev_state_t * pb_dev_state,p2G4_cca_t * s)47 void p2G4_dev_req_cca_i(pb_dev_state_t *pb_dev_state, p2G4_cca_t *s)
48 {
49 pb_send_msg(pb_dev_state->ff_dtp, P2G4_MSG_CCA_MEAS, (void *)s, sizeof(p2G4_cca_t));
50 }
51
p2G4_dev_get_tx_resp_i(pb_dev_state_t * pb_dev_state,p2G4_tx_done_t * tx_done_s)52 int p2G4_dev_get_tx_resp_i(pb_dev_state_t *pb_dev_state,
53 p2G4_tx_done_t *tx_done_s)
54 {
55 pc_header_t header;
56 int ret;
57
58 ret = pb_dev_read(pb_dev_state, &header, sizeof(header));
59 if (ret == -1)
60 return -1;
61
62 ret = p2G4_dev_handle_tx_resp_i(pb_dev_state, header,
63 tx_done_s);
64 return ret;
65 }
66
p2G4_dev_handle_cca_resp_i(pb_dev_state_t * pb_dev_state,pc_header_t header,p2G4_cca_done_t * cca_done_s)67 int p2G4_dev_handle_cca_resp_i(pb_dev_state_t *pb_dev_state, pc_header_t header,
68 p2G4_cca_done_t *cca_done_s)
69 {
70 int ret;
71 if (header == P2G4_MSG_CCA_END) {
72 ret = pb_dev_read(pb_dev_state, cca_done_s, sizeof(p2G4_cca_done_t));
73 if (ret == -1)
74 return -1;
75 else
76 return 0;
77 } else if (header == PB_MSG_DISCONNECT) {
78 pb_dev_clean_up(pb_dev_state);
79 return -1;
80 } else {
81 INVALID_RESP(header);
82 return -1;
83 }
84 }
85
p2G4_dev_get_rssi_resp_i(pb_dev_state_t * pb_dev_state,p2G4_rssi_done_t * RSSI_done_s)86 int p2G4_dev_get_rssi_resp_i(pb_dev_state_t *pb_dev_state,
87 p2G4_rssi_done_t *RSSI_done_s)
88 {
89 pc_header_t header;
90 int ret;
91
92 ret = pb_dev_read(pb_dev_state, &header, sizeof(header));
93 if (ret == -1)
94 return -1;
95
96 if (header == PB_MSG_DISCONNECT) {
97 pb_dev_clean_up(pb_dev_state);
98 return -1;
99 } else if (header == P2G4_MSG_RSSI_END) {
100 ret = pb_dev_read(pb_dev_state, RSSI_done_s, sizeof(p2G4_rssi_done_t));
101 if (ret == -1)
102 return -1;
103 else
104 return 0;
105 } else {
106 INVALID_RESP(header);
107 return -1;
108 }
109 }
110
p2G4_rx_pick_packet(pb_dev_state_t * pb_dev_state,size_t rx_size,uint8_t ** rx_buf,size_t buf_size)111 int p2G4_rx_pick_packet(pb_dev_state_t *pb_dev_state, size_t rx_size,
112 uint8_t **rx_buf, size_t buf_size){
113 if (rx_size > 0) {
114 uint8_t buf_ok = 0;
115 if (rx_size <= buf_size) {
116 buf_ok = 1;
117 } else if (buf_size == 0) {
118 *rx_buf = bs_malloc(rx_size);
119 buf_ok = 1;
120 }
121 if (buf_ok == 0) {
122 bs_trace_warning_line("Too small buffer to pick incoming packet (%i < %i"
123 ") => Disconnecting\n", buf_size, rx_size);
124 pb_dev_disconnect(pb_dev_state);
125 return -1;
126 }
127 if (pb_dev_read(pb_dev_state, *rx_buf, rx_size) == -1) {
128 return -1;
129 }
130 }
131 return 0;
132 }
133