Skip to content

Commit d7c22a6

Browse files
author
Hasnain Virk
committed
A few Cosmetics & methods for mask manipulation
LoRaMacChannelPlan class provides APIs which are not usable for PHY layer implementations who do not support custom channel plans. So we had some code in APIs which was explicitely using magic numbers for the channel mask. Although it turned out to be not a bug as a layer down we were checking for custom channel support. However, we now check for custom channel support before going deep into PHY layer that will make the code run faster and we have done some cosmetics to the code for readability. Channel mask is manipulated with inline methods
1 parent 90c02f2 commit d7c22a6

26 files changed

+412
-372
lines changed

features/lorawan/lorastack/mac/LoRaMacChannelPlan.cpp

Lines changed: 45 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,14 @@ lorawan_status_t LoRaMacChannelPlan::set_plan(const lorawan_channelplan_t& plan)
4848
phy_param_t phy_param;
4949
uint8_t max_num_channels;
5050

51+
// Check if the PHY layer supports custom channel plans or not.
52+
get_phy.attribute = PHY_CUSTOM_CHANNEL_PLAN_SUPPORT;
53+
phy_param = _lora_phy->get_phy_params(&get_phy);
54+
55+
if (!phy_param.value) {
56+
return LORAWAN_STATUS_SERVICE_UNKNOWN;
57+
}
58+
5159
// Check first how many channels the selected PHY layer supports
5260
get_phy.attribute = PHY_MAX_NB_CHANNELS;
5361
phy_param = _lora_phy->get_phy_params(&get_phy);
@@ -91,18 +99,26 @@ lorawan_status_t LoRaMacChannelPlan::get_plan(lorawan_channelplan_t& plan,
9199
get_phy_params_t get_phy;
92100
phy_param_t phy_param;
93101
uint8_t max_num_channels;
94-
uint16_t *channel_masks;
102+
uint16_t *channel_mask;
95103
uint8_t count = 0;
96104

105+
// Check if the PHY layer supports custom channel plans or not.
106+
get_phy.attribute = PHY_CUSTOM_CHANNEL_PLAN_SUPPORT;
107+
phy_param = _lora_phy->get_phy_params(&get_phy);
108+
109+
if (!phy_param.value) {
110+
return LORAWAN_STATUS_SERVICE_UNKNOWN;
111+
}
112+
97113
// Check first how many channels the selected PHY layer supports
98114
get_phy.attribute = PHY_MAX_NB_CHANNELS;
99115
phy_param = _lora_phy->get_phy_params(&get_phy);
100116
max_num_channels = (uint8_t) phy_param.value;
101117

102118
// Now check the Default channel mask
103-
get_phy.attribute = PHY_CHANNELS_MASK;
119+
get_phy.attribute = PHY_CHANNEL_MASK;
104120
phy_param = _lora_phy->get_phy_params(&get_phy);
105-
channel_masks = phy_param.channel_mask;
121+
channel_mask = phy_param.channel_mask;
106122

107123
// Request Mib to get channels
108124
memset(&mib_confirm, 0, sizeof(mib_confirm));
@@ -116,7 +132,7 @@ lorawan_status_t LoRaMacChannelPlan::get_plan(lorawan_channelplan_t& plan,
116132

117133
for (uint8_t i = 0; i < max_num_channels; i++) {
118134
// skip the channels which are not enabled
119-
if ((channel_masks[0] & (1U << i)) == 0) {
135+
if (_lora_phy->mask_bit_test(channel_mask, i) == 0) {
120136
continue;
121137
}
122138

@@ -143,32 +159,40 @@ lorawan_status_t LoRaMacChannelPlan::remove_plan()
143159
get_phy_params_t get_phy;
144160
phy_param_t phy_param;
145161
uint8_t max_num_channels;
146-
uint16_t *channel_masks;
147-
uint16_t *default_channel_masks;
162+
uint16_t *channel_mask;
163+
uint16_t *default_channel_mask;
164+
165+
// Check if the PHY layer supports custom channel plans or not.
166+
get_phy.attribute = PHY_CUSTOM_CHANNEL_PLAN_SUPPORT;
167+
phy_param = _lora_phy->get_phy_params(&get_phy);
168+
169+
if (!phy_param.value) {
170+
return LORAWAN_STATUS_SERVICE_UNKNOWN;
171+
}
148172

149173
// Check first how many channels the selected PHY layer supports
150174
get_phy.attribute = PHY_MAX_NB_CHANNELS;
151175
phy_param = _lora_phy->get_phy_params(&get_phy);
152176
max_num_channels = (uint8_t) phy_param.value;
153177

154178
// Now check the channel mask for enabled channels
155-
get_phy.attribute = PHY_CHANNELS_MASK;
179+
get_phy.attribute = PHY_CHANNEL_MASK;
156180
phy_param = _lora_phy->get_phy_params(&get_phy);
157-
channel_masks = phy_param.channel_mask;
181+
channel_mask = phy_param.channel_mask;
158182

159183
// Now check the channel mask for default channels
160-
get_phy.attribute = PHY_CHANNELS_DEFAULT_MASK;
184+
get_phy.attribute = PHY_DEFAULT_CHANNEL_MASK;
161185
phy_param = _lora_phy->get_phy_params(&get_phy);
162-
default_channel_masks = phy_param.channel_mask;
186+
default_channel_mask = phy_param.channel_mask;
163187

164188
for (uint8_t i = 0; i < max_num_channels; i++) {
165189
// skip any default channels
166-
if ((default_channel_masks[0] & (1U<<i)) != 0) {
190+
if (_lora_phy->mask_bit_test(default_channel_mask, i) != 0) {
167191
continue;
168192
}
169193

170194
// skip any channels which are not currently enabled
171-
if ((channel_masks[0] & (1U<<i)) == 0) {
195+
if (_lora_phy->mask_bit_test(channel_mask, i) == 0) {
172196
continue;
173197
}
174198

@@ -187,7 +211,14 @@ lorawan_status_t LoRaMacChannelPlan::remove_single_channel(uint8_t channel_id)
187211
get_phy_params_t get_phy;
188212
phy_param_t phy_param;
189213
uint8_t max_num_channels;
190-
uint16_t *channel_masks;
214+
215+
// Check if the PHY layer supports custom channel plans or not.
216+
get_phy.attribute = PHY_CUSTOM_CHANNEL_PLAN_SUPPORT;
217+
phy_param = _lora_phy->get_phy_params(&get_phy);
218+
219+
if (!phy_param.value) {
220+
return LORAWAN_STATUS_SERVICE_UNKNOWN;
221+
}
191222

192223
// Check first how many channels the selected PHY layer supports
193224
get_phy.attribute = PHY_MAX_NB_CHANNELS;
@@ -201,21 +232,7 @@ lorawan_status_t LoRaMacChannelPlan::remove_single_channel(uint8_t channel_id)
201232
return LORAWAN_STATUS_PARAMETER_INVALID;
202233
}
203234

204-
// Now check the Default channel mask
205-
get_phy.attribute = PHY_CHANNELS_DEFAULT_MASK;
206-
phy_param = _lora_phy->get_phy_params(&get_phy);
207-
channel_masks = phy_param.channel_mask;
208-
209-
// check if the channel ID give belongs to a default channel
210-
// Mostly the default channels are in the first mask if the region
211-
// have multiple channel masks for various sub-bands. So we check the first
212-
// mask only and return an error code if user sent a default channel id
213-
if ((channel_masks[0] & (1U << channel_id)) != 0) {
214-
return LORAWAN_STATUS_PARAMETER_INVALID;
215-
}
216-
217-
if(_lora_phy->remove_channel(channel_id) == false)
218-
{
235+
if (_lora_phy->remove_channel(channel_id) == false) {
219236
return LORAWAN_STATUS_PARAMETER_INVALID;
220237
}
221238

features/lorawan/lorastack/mac/LoRaMacMib.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -355,15 +355,15 @@ lorawan_status_t LoRaMacMib::get_request(loramac_mib_req_confirm_t *mibGet,
355355
}
356356
case MIB_CHANNELS_DEFAULT_MASK:
357357
{
358-
get_phy.attribute = PHY_CHANNELS_DEFAULT_MASK;
358+
get_phy.attribute = PHY_DEFAULT_CHANNEL_MASK;
359359
phy_param = _lora_phy->get_phy_params( &get_phy );
360360

361361
mibGet->param.default_channel_mask = phy_param.channel_mask;
362362
break;
363363
}
364364
case MIB_CHANNELS_MASK:
365365
{
366-
get_phy.attribute = PHY_CHANNELS_MASK;
366+
get_phy.attribute = PHY_CHANNEL_MASK;
367367
phy_param = _lora_phy->get_phy_params( &get_phy );
368368

369369
mibGet->param.channel_mask = phy_param.channel_mask;

0 commit comments

Comments
 (0)