@@ -54,6 +54,8 @@ LOG_MODULE_REGISTER(LOG_MODULE_NAME, CONFIG_OPENTHREAD_L2_LOG_LEVEL);
5454#define OT_WORKER_PRIORITY K_PRIO_PREEMPT(CONFIG_OPENTHREAD_THREAD_PRIORITY)
5555#endif
5656
57+ #define CHANNEL_COUNT OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MAX - OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN + 1
58+
5759enum pending_events {
5860 PENDING_EVENT_FRAME_TO_SEND , /* There is a tx frame to send */
5961 PENDING_EVENT_FRAME_RECEIVED , /* Radio has received new frame */
@@ -88,6 +90,8 @@ static uint16_t energy_detection_time;
8890static uint8_t energy_detection_channel ;
8991static int16_t energy_detected_value ;
9092
93+ static int8_t max_tx_power_table [CHANNEL_COUNT ];
94+
9195ATOMIC_DEFINE (pending_events , PENDING_EVENT_COUNT );
9296K_KERNEL_STACK_DEFINE (ot_task_stack ,
9397 CONFIG_OPENTHREAD_RADIO_WORKQUEUE_STACK_SIZE );
@@ -98,6 +102,26 @@ static otError tx_result;
98102K_FIFO_DEFINE (rx_pkt_fifo );
99103K_FIFO_DEFINE (tx_pkt_fifo );
100104
105+ static int8_t get_transmit_power_for_channel (uint8_t aChannel )
106+ {
107+ int8_t channel_max_power = OT_RADIO_POWER_INVALID ;
108+ int8_t power = 0 ; /* 0 dbm as default value */
109+
110+ if (aChannel >= OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN &&
111+ aChannel <= OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MAX ) {
112+ channel_max_power =
113+ max_tx_power_table [aChannel - OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN ];
114+ }
115+
116+ if (tx_power != OT_RADIO_POWER_INVALID ) {
117+ power = (channel_max_power < tx_power ) ? channel_max_power : tx_power ;
118+ } else if (channel_max_power != OT_RADIO_POWER_INVALID ) {
119+ power = channel_max_power ;
120+ }
121+
122+ return power ;
123+ }
124+
101125static inline bool is_pending_event_set (enum pending_events event )
102126{
103127 return atomic_test_bit (pending_events , event );
@@ -220,6 +244,10 @@ static void dataInit(void)
220244 net_pkt_append_buffer (tx_pkt , tx_payload );
221245
222246 sTransmitFrame .mPsdu = tx_payload -> data ;
247+
248+ for (size_t i = 0 ; i < CHANNEL_COUNT ; i ++ ) {
249+ max_tx_power_table [i ] = OT_RADIO_POWER_INVALID ;
250+ }
223251}
224252
225253void platformRadioInit (void )
@@ -270,7 +298,7 @@ void transmit_message(struct k_work *tx_job)
270298 channel = sTransmitFrame .mChannel ;
271299
272300 radio_api -> set_channel (radio_dev , sTransmitFrame .mChannel );
273- radio_api -> set_txpower (radio_dev , tx_power );
301+ radio_api -> set_txpower (radio_dev , get_transmit_power_for_channel ( channel ) );
274302
275303 net_pkt_set_ieee802154_frame_secured (tx_pkt ,
276304 sTransmitFrame .mInfo .mTxInfo .mIsSecurityProcessed );
@@ -611,7 +639,7 @@ otError otPlatRadioReceive(otInstance *aInstance, uint8_t aChannel)
611639 channel = aChannel ;
612640
613641 radio_api -> set_channel (radio_dev , aChannel );
614- radio_api -> set_txpower (radio_dev , tx_power );
642+ radio_api -> set_txpower (radio_dev , get_transmit_power_for_channel ( channel ) );
615643 radio_api -> start (radio_dev );
616644 sState = OT_RADIO_STATE_RECEIVE ;
617645
@@ -1223,3 +1251,22 @@ otError otPlatRadioConfigureEnhAckProbing(otInstance *aInstance, otLinkMetrics a
12231251}
12241252
12251253#endif /* CONFIG_OPENTHREAD_LINK_METRICS_SUBJECT */
1254+
1255+ otError otPlatRadioSetChannelMaxTransmitPower (otInstance * aInstance , uint8_t aChannel ,
1256+ int8_t aMaxPower )
1257+ {
1258+ ARG_UNUSED (aInstance );
1259+
1260+ if (aChannel < OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN ||
1261+ aChannel > OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MAX ) {
1262+ return OT_ERROR_INVALID_ARGS ;
1263+ }
1264+
1265+ max_tx_power_table [aChannel - OT_RADIO_2P4GHZ_OQPSK_CHANNEL_MIN ] = aMaxPower ;
1266+
1267+ if (aChannel == channel ) {
1268+ radio_api -> set_txpower (radio_dev , get_transmit_power_for_channel (aChannel ));
1269+ }
1270+
1271+ return OT_ERROR_NONE ;
1272+ }
0 commit comments