Skip to content

Commit 8de473b

Browse files
author
Jarkko Paso
authored
Implemented 802.15.4 mode setting (#2328)
* Implemented 802.15.4 mode setting * Fixed unit tests
1 parent 975b9e6 commit 8de473b

File tree

15 files changed

+140
-65
lines changed

15 files changed

+140
-65
lines changed

nanostack/mac_common_defines.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
#define MAC_IEEE_802_15_4_MIN_MPDU_OVERHEAD 9 /**< Minimum overhead added by MAC to MPDU */
4444
#define MAC_IEEE_802_15_4_MAX_BEACON_OVERHEAD 75 /**< Maximum overhead which is added by the MAC for beacon */
4545
#define MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE 127 /**< Maximum number of octets PHY layer is able to receive */
46+
#define MAC_IEEE_802_15_4G_MAX_PHY_PACKET_SIZE 2047 /**< Maximum number of octets PHY layer is able to receive */
4647

4748
#define MAC_IEEE_802_15_4_MAX_BEACON_PAYLOAD_LENGTH \
4849
(MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE - MAC_IEEE_802_15_4_MAX_BEACON_OVERHEAD) /**< Maximum size of beacon payload */

nanostack/mlme.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -264,6 +264,7 @@ typedef enum {
264264
macAutoRequestKeyIndex = 0x7b, /*<The index of the key used for automatic data*/
265265
macDefaultKeySource = 0x7c, /*<Default key source*/
266266
//NON standard extension
267+
mac802_15_4Mode = 0xf6, /*<IEEE 802.15.4 mode*/
267268
macDeviceDescriptionPanIDUpdate = 0xf7, /*<Thread pending link update case this will update device descrioton list pan-id to new one*/
268269
macTXPower = 0xf8, /*<TX output power*/
269270
macCCAThreshold = 0xf9, /*<CCA threshold*/

nanostack/platform/arm_hal_phy.h

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ typedef enum {
7979
PHY_EXTENSION_SET_RF_CONFIGURATION, /**< Set RF configuration using phy_rf_channel_configuration_s structure */
8080
PHY_EXTENSION_FILTERING_SUPPORT, /**< Return filtering modes that can be supported by the PHY driver. See phy_link_filters_e */
8181
PHY_EXTENSION_SET_TX_POWER, /**< Set TX output power which is given as percentage of maximum. 0 is the lowest possible TX power and 100 is the highest possible TX power */
82-
PHY_EXTENSION_SET_CCA_THRESHOLD /**< Set CCA threshold which is given as percentage of maximum threshold. 0 is the lowest(strictest) possible threshold and 100 is the highest possible threshold */
82+
PHY_EXTENSION_SET_CCA_THRESHOLD, /**< Set CCA threshold which is given as percentage of maximum threshold. 0 is the lowest(strictest) possible threshold and 100 is the highest possible threshold */
83+
PHY_EXTENSION_SET_802_15_4_MODE /**< Set IEEE 802.15.4 mode as defined by phy_802_15_4_mode_t*/
8384
} phy_extension_type_e;
8485

8586
/** Address types */
@@ -172,6 +173,15 @@ typedef enum {
172173
MODULATION_INDEX_UNDEFINED ///< Modulation index undefined
173174
} phy_modulation_index_e;
174175

176+
/**
177+
* @brief enum phy_802_15_4_mode_t IEEE 802.15.4 mode
178+
*
179+
*/
180+
typedef enum {
181+
IEEE_802_15_4_2011, /**<IEEE 802.15.4-2011*/
182+
IEEE_802_15_4G_2012 /**<IEEE 802.15.4g-2012*/
183+
} phy_802_15_4_mode_t;
184+
175185
/** Channel configuration */
176186
typedef struct phy_rf_channel_configuration_s {
177187
uint32_t channel_0_center_frequency; ///< Center frequency

source/6LoWPAN/ws/ws_bootstrap.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1892,6 +1892,12 @@ int ws_bootstrap_restart_delayed(int8_t interface_id)
18921892
int ws_bootstrap_set_rf_config(protocol_interface_info_entry_t *cur, phy_rf_channel_configuration_s rf_configs)
18931893
{
18941894
mlme_set_t set_request;
1895+
// Set MAC mode
1896+
phy_802_15_4_mode_t mac_mode = IEEE_802_15_4G_2012;
1897+
set_request.attr = mac802_15_4Mode;
1898+
set_request.value_pointer = &mac_mode;
1899+
set_request.value_size = sizeof(phy_802_15_4_mode_t);
1900+
cur->mac_api->mlme_req(cur->mac_api, MLME_SET, &set_request);
18951901
// Set RF configuration
18961902
set_request.attr = macRfConfiguration;
18971903
set_request.value_pointer = &rf_configs;

source/MAC/IEEE802_15_4/mac_defines.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ typedef struct protocol_interface_rf_mac_setup {
225225
uint8_t aUnitBackoffPeriod;
226226
uint8_t number_of_csma_ca_periods; /**< Number of CSMA-CA periods */
227227
uint16_t multi_cca_interval; /**< Length of the additional CSMA-CA period(s) in microseconds */
228+
uint16_t phy_mtu_size;
229+
phy_802_15_4_mode_t current_mac_mode;
228230
/* Indirect queue parameters */
229231
struct mac_pre_build_frame *indirect_pd_data_request_queue;
230232
struct mac_pre_build_frame enhanced_ack_buffer;

source/MAC/IEEE802_15_4/mac_mcps_sap.c

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,8 @@ void mcps_sap_data_req_handler_ext(protocol_interface_rf_mac_setup_s *rf_mac_set
228228
goto verify_status;
229229
}
230230

231-
if ((data_req->msduLength + ie_header_length + ie_payload_length) > rf_mac_setup->dev_driver->phy_driver->phy_MTU - MAC_DATA_PACKET_MIN_HEADER_LENGTH) {
232-
tr_debug("packet %u, %u", data_req->msduLength, rf_mac_setup->dev_driver->phy_driver->phy_MTU);
231+
if ((data_req->msduLength + ie_header_length + ie_payload_length) > rf_mac_setup->phy_mtu_size - MAC_DATA_PACKET_MIN_HEADER_LENGTH) {
232+
tr_debug("packet %u, %u", data_req->msduLength, rf_mac_setup->phy_mtu_size);
233233
status = MLME_FRAME_TOO_LONG;
234234
goto verify_status;
235235
}
@@ -358,7 +358,7 @@ void mcps_sap_data_req_handler_ext(protocol_interface_rf_mac_setup_s *rf_mac_set
358358
static int8_t mac_virtual_data_req_handler(protocol_interface_rf_mac_setup_s *rf_mac_setup, const uint8_t *data_ptr, uint16_t data_length)
359359
{
360360

361-
if (!rf_mac_setup->macUpState || data_length > rf_mac_setup->dev_driver->phy_driver->phy_MTU) {
361+
if (!rf_mac_setup->macUpState || data_length > rf_mac_setup->phy_mtu_size) {
362362
return -1;
363363
}
364364

@@ -1576,7 +1576,7 @@ static int8_t mcps_generic_packet_build(protocol_interface_rf_mac_setup_s *rf_pt
15761576
uint16_t mac_payload_length = frame_length;
15771577

15781578
if (mac_payload_length > MAC_IEEE_802_15_4_MAX_MAC_SAFE_PAYLOAD_SIZE &&
1579-
dev_driver->phy_MTU == MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE) {
1579+
rf_ptr->phy_mtu_size == MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE) {
15801580
/* IEEE 802.15.4-2003 only allowed unsecured payloads up to 102 bytes
15811581
* (always leaving room for maximum MAC overhead).
15821582
* IEEE 802.15.4-2006 allows bigger if MAC header is small enough, but
@@ -1596,8 +1596,8 @@ static int8_t mcps_generic_packet_build(protocol_interface_rf_mac_setup_s *rf_pt
15961596

15971597
//Add MHR length to total length
15981598
frame_length += buffer->mac_header_length_with_security + buffer->security_mic_len;
1599-
if ((frame_length) > dev_driver->phy_MTU - 2) {
1600-
tr_debug("Too Long %u, %u pa %u header %u mic %u", frame_length, mac_payload_length, buffer->mac_header_length_with_security, buffer->security_mic_len, dev_driver->phy_MTU);
1599+
if ((frame_length) > rf_ptr->phy_mtu_size - 2) {
1600+
tr_debug("Too Long %u, %u pa %u header %u mic %u", frame_length, mac_payload_length, buffer->mac_header_length_with_security, buffer->security_mic_len, rf_ptr->phy_mtu_size);
16011601
buffer->status = MLME_FRAME_TOO_LONG;
16021602
//decrement security counter
16031603
if (key_desc) {
@@ -1745,8 +1745,8 @@ int8_t mcps_generic_ack_build(protocol_interface_rf_mac_setup_s *rf_ptr, bool in
17451745
//Add MHR length to total length
17461746
frame_length += buffer->mac_header_length_with_security + buffer->security_mic_len;
17471747
uint16_t ack_mtu_size;
1748-
if (ENHANCED_ACK_MAX_LENGTH > dev_driver->phy_MTU) {
1749-
ack_mtu_size = dev_driver->phy_MTU;
1748+
if (ENHANCED_ACK_MAX_LENGTH > rf_ptr->phy_mtu_size) {
1749+
ack_mtu_size = rf_ptr->phy_mtu_size;
17501750
} else {
17511751
ack_mtu_size = ENHANCED_ACK_MAX_LENGTH;
17521752
}

source/MAC/IEEE802_15_4/mac_mlme.c

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ static void mac_mlme_timer_cb(int8_t timer_id, uint16_t slots);
6969
static void mac_mlme_start_confirm_handler(protocol_interface_rf_mac_setup_s *rf_ptr, const mlme_start_conf_t *conf);
7070
static void mac_mlme_scan_confirm_handler(protocol_interface_rf_mac_setup_s *rf_ptr, const mlme_scan_conf_t *conf);
7171
static int mac_mlme_set_symbol_rate(protocol_interface_rf_mac_setup_s *rf_mac_setup);
72+
static int mac_mlme_allocate_tx_buffers(protocol_interface_rf_mac_setup_s *rf_mac_setup, arm_device_driver_list_s *dev_driver, uint16_t mtu_size);
7273

7374
static void mac_mlme_energy_scan_start(protocol_interface_rf_mac_setup_s *rf_mac_setup, uint8_t channel)
7475
{
@@ -767,6 +768,29 @@ int8_t mac_mlme_set_req(protocol_interface_rf_mac_setup_s *rf_mac_setup, const m
767768
memcpy(rf_mac_setup->coord_long_address, set_req->value_pointer, 8);
768769
}
769770
return 0;
771+
case mac802_15_4Mode:
772+
pu8 = (uint8_t *) set_req->value_pointer;
773+
if (rf_mac_setup->current_mac_mode == *pu8) {
774+
return -1;
775+
}
776+
rf_mac_setup->current_mac_mode = *pu8;
777+
rf_mac_setup->dev_driver->phy_driver->extension(PHY_EXTENSION_SET_802_15_4_MODE, pu8);
778+
uint16_t new_mtu_size = MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE;
779+
if (*pu8 == IEEE_802_15_4G_2012) {
780+
new_mtu_size = MAC_IEEE_802_15_4G_MAX_PHY_PACKET_SIZE;
781+
}
782+
mac_api_t *mac_api = get_sw_mac_api(rf_mac_setup);
783+
if (rf_mac_setup->dev_driver->phy_driver->phy_MTU > new_mtu_size) {
784+
mac_api->phyMTU = rf_mac_setup->phy_mtu_size = new_mtu_size;
785+
} else {
786+
mac_api->phyMTU = rf_mac_setup->phy_mtu_size = rf_mac_setup->dev_driver->phy_driver->phy_MTU;
787+
}
788+
if (mac_mlme_allocate_tx_buffers(rf_mac_setup, rf_mac_setup->dev_driver, rf_mac_setup->phy_mtu_size)) {
789+
tr_err("Failed to reallocate TX buffers");
790+
return -1;
791+
}
792+
tr_debug("Set MAC mode to %s, MTU size: %u", *pu8 == IEEE_802_15_4G_2012 ? "IEEE 802.15.4G-2012" : "IEEE 802.15.4-2011", rf_mac_setup->phy_mtu_size);
793+
return 0;
770794
case macTXPower:
771795
pu8 = (uint8_t *) set_req->value_pointer;
772796
rf_mac_setup->dev_driver->phy_driver->extension(PHY_EXTENSION_SET_TX_POWER, pu8);
@@ -1067,9 +1091,29 @@ static int mac_mlme_set_symbol_rate(protocol_interface_rf_mac_setup_s *rf_mac_se
10671091
return -1;
10681092
}
10691093

1070-
protocol_interface_rf_mac_setup_s *mac_mlme_data_base_allocate(uint8_t *mac64, arm_device_driver_list_s *dev_driver, mac_description_storage_size_t *storage_sizes)
1094+
static int mac_mlme_allocate_tx_buffers(protocol_interface_rf_mac_setup_s *rf_mac_setup, arm_device_driver_list_s *dev_driver, uint16_t mtu_size)
10711095
{
1096+
ns_dyn_mem_free(rf_mac_setup->dev_driver_tx_buffer.buf);
1097+
ns_dyn_mem_free(rf_mac_setup->mac_beacon_payload);
10721098
uint16_t total_length = 0;
1099+
//Allocate tx buffer by given MTU + header + tail
1100+
total_length = mtu_size;
1101+
total_length += (dev_driver->phy_driver->phy_header_length + dev_driver->phy_driver->phy_tail_length);
1102+
rf_mac_setup->dev_driver_tx_buffer.buf = ns_dyn_mem_alloc(total_length);
1103+
if (!rf_mac_setup->dev_driver_tx_buffer.buf) {
1104+
return -1;
1105+
}
1106+
//allocate Beacon Payload buffer
1107+
rf_mac_setup->max_beacon_payload_length = mtu_size - MAC_IEEE_802_15_4_MAX_BEACON_OVERHEAD;
1108+
rf_mac_setup->mac_beacon_payload = ns_dyn_mem_alloc(rf_mac_setup->max_beacon_payload_length);
1109+
if (!rf_mac_setup->mac_beacon_payload) {
1110+
return -1;
1111+
}
1112+
return 0;
1113+
}
1114+
1115+
protocol_interface_rf_mac_setup_s *mac_mlme_data_base_allocate(uint8_t *mac64, arm_device_driver_list_s *dev_driver, mac_description_storage_size_t *storage_sizes, uint16_t mtu_size)
1116+
{
10731117
//allocate security
10741118
if (!dev_driver || !mac64 || !dev_driver->phy_driver || !storage_sizes) {
10751119
return NULL;
@@ -1102,20 +1146,9 @@ protocol_interface_rf_mac_setup_s *mac_mlme_data_base_allocate(uint8_t *mac64, a
11021146
mac_mlme_data_base_deallocate(entry);
11031147
return NULL;
11041148
}
1149+
entry->phy_mtu_size = mtu_size;
11051150

1106-
//Allocate tx buffer by given MTU + header + tail
1107-
total_length = dev_driver->phy_driver->phy_MTU;
1108-
total_length += (dev_driver->phy_driver->phy_header_length + dev_driver->phy_driver->phy_tail_length);
1109-
entry->dev_driver_tx_buffer.buf = ns_dyn_mem_alloc(total_length);
1110-
if (!entry->dev_driver_tx_buffer.buf) {
1111-
mac_mlme_data_base_deallocate(entry);
1112-
return NULL;
1113-
}
1114-
1115-
//allocate Beacon Payload buffer
1116-
entry->max_beacon_payload_length = dev_driver->phy_driver->phy_MTU - MAC_IEEE_802_15_4_MAX_BEACON_OVERHEAD;
1117-
entry->mac_beacon_payload = ns_dyn_mem_alloc(entry->max_beacon_payload_length);
1118-
if (!entry->mac_beacon_payload) {
1151+
if (mac_mlme_allocate_tx_buffers(entry, dev_driver, mtu_size)) {
11191152
mac_mlme_data_base_deallocate(entry);
11201153
return NULL;
11211154
}

source/MAC/IEEE802_15_4/mac_mlme.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ void mac_mlme_event_cb(void *mac_ptr);
106106

107107
void mac_mlme_set_active_state(struct protocol_interface_rf_mac_setup *entry, bool new_state);
108108

109-
struct protocol_interface_rf_mac_setup *mac_mlme_data_base_allocate(uint8_t *mac64, struct arm_device_driver_list *dev_driver, struct mac_description_storage_size_s *storage_sizes);
109+
struct protocol_interface_rf_mac_setup *mac_mlme_data_base_allocate(uint8_t *mac64, struct arm_device_driver_list *dev_driver, struct mac_description_storage_size_s *storage_sizes, uint16_t mtu_size);
110110
void mac_mlme_data_base_deallocate(struct protocol_interface_rf_mac_setup *rf_mac);
111111

112112
uint8_t mac_mlme_set_new_sqn(struct protocol_interface_rf_mac_setup *rf_setup);

source/MAC/IEEE802_15_4/mac_pd_sap.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ uint32_t mac_csma_backoff_get(protocol_interface_rf_mac_setup_s *rf_mac_setup)
111111
backoff_in_us += ((rf_mac_setup->multi_cca_interval * (rf_mac_setup->number_of_csma_ca_periods - 1)) - backoff_in_us);
112112
}
113113
if (rf_mac_setup->mac_tx_retry) {
114-
backoff_in_us += rf_mac_setup->fhss_api->get_retry_period(rf_mac_setup->fhss_api, rf_mac_setup->active_pd_data_request->DstAddr, rf_mac_setup->dev_driver->phy_driver->phy_MTU);
114+
backoff_in_us += rf_mac_setup->fhss_api->get_retry_period(rf_mac_setup->fhss_api, rf_mac_setup->active_pd_data_request->DstAddr, rf_mac_setup->phy_mtu_size);
115115
}
116116
}
117117
return backoff_in_us;

source/MAC/IEEE802_15_4/sw_mac.c

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,9 @@
3131
#include "mac_fhss_callbacks.h"
3232
#include "eventOS_callback_timer.h"
3333
#include "common_functions.h"
34+
#include "ns_trace.h"
35+
36+
#define TRACE_GROUP "swm"
3437

3538
//TODO: create linked list of created MACs
3639

@@ -96,13 +99,28 @@ mac_api_t *ns_sw_mac_create(int8_t rf_driver_id, mac_description_storage_size_t
9699
memset(this, 0, sizeof(mac_api_t));
97100
this->parent_id = -1;
98101
mac_store.dev_driver = driver;
99-
mac_store.setup = mac_mlme_data_base_allocate(mac_store.dev_driver->phy_driver->PHY_MAC, mac_store.dev_driver, storage_sizes);
102+
103+
// Set default MTU size to 127 unless it is too much for PHY driver
104+
if (driver->phy_driver->phy_MTU > MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE) {
105+
this->phyMTU = MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE;
106+
} else {
107+
this->phyMTU = driver->phy_driver->phy_MTU;
108+
}
109+
110+
mac_store.setup = mac_mlme_data_base_allocate(mac_store.dev_driver->phy_driver->PHY_MAC, mac_store.dev_driver, storage_sizes, this->phyMTU);
100111

101112
if (!mac_store.setup) {
102113
ns_dyn_mem_free(this);
103114
return NULL;
104115
}
105116

117+
// Set MAC mode to PHY driver
118+
mac_store.setup->current_mac_mode = IEEE_802_15_4_2011;
119+
if (mac_store.setup->dev_driver->phy_driver->extension) {
120+
mac_store.setup->dev_driver->phy_driver->extension(PHY_EXTENSION_SET_802_15_4_MODE, (uint8_t *) &mac_store.setup->current_mac_mode);
121+
}
122+
tr_debug("Set MAC mode to %s, MTU size: %u", "IEEE 802.15.4-2011", mac_store.setup->phy_mtu_size);
123+
106124
arm_net_phy_init(driver->phy_driver, &sw_mac_net_phy_rx, &sw_mac_net_phy_tx_done);
107125
arm_net_virtual_config_rx_cb_set(driver->phy_driver, &sw_mac_net_phy_config_parser);
108126
arm_net_virtual_confirmation_rx_cb_set(driver->phy_driver, &mac_mlme_virtual_confirmation_handle);
@@ -116,10 +134,8 @@ mac_api_t *ns_sw_mac_create(int8_t rf_driver_id, mac_description_storage_size_t
116134
this->mac64_get = &macext_mac64_address_get;
117135
this->mac64_set = &macext_mac64_address_set;
118136
this->mac_storage_sizes_get = &sw_mac_storage_decription_sizes_get;
119-
this->phyMTU = driver->phy_driver->phy_MTU;
120137

121138
mac_store.mac_api = this;
122-
123139
mac_store.virtual_driver = NULL;
124140
return this;
125141
}
@@ -279,8 +295,8 @@ static int8_t ns_sw_mac_api_enable_mcps_ext(mac_api_t *api, mcps_data_indication
279295
ns_dyn_mem_free(mac_store.setup->dev_driver_tx_buffer.enhanced_ack_buf);
280296

281297
uint16_t total_length;
282-
if (ENHANCED_ACK_MAX_LENGTH > dev_driver->phy_driver->phy_MTU) {
283-
total_length = dev_driver->phy_driver->phy_MTU;
298+
if (ENHANCED_ACK_MAX_LENGTH > mac_store.setup->phy_mtu_size) {
299+
total_length = mac_store.setup->phy_mtu_size;
284300
} else {
285301
total_length = ENHANCED_ACK_MAX_LENGTH;
286302
}

test/nanostack/unittest/mac/mac_mcps_sap/test_mac_mcps_sap.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,7 @@ static protocol_interface_rf_mac_setup_s *test_mac_rf_mac_class_allocate(void)
245245
memset(rf_mac_setup, 0, sizeof(protocol_interface_rf_mac_setup_s));
246246
rf_mac_setup->dev_driver_tx_buffer.buf = malloc(128);
247247
rf_mac_setup->dev_driver_tx_buffer.len = 127;
248+
rf_mac_setup->phy_mtu_size = 127;
248249

249250

250251
arm_device_driver_list_s *driver = malloc(sizeof(arm_device_driver_list_s));
@@ -825,6 +826,7 @@ bool test_mac_mcps_data_confirmation()
825826

826827
// Test FHSS channel retry
827828
rf_mac_setup->fhss_api = ns_fhss_create(0, 0, 0);
829+
fhss_config_stub.fhss_api_ptr.check_tx_conditions = &fhss_check_tx_conditions_callback;
828830
nsdynmemlib_stub.returnCounter = 3;
829831
data_req.TxAckReq = true;
830832
fhss_tx_condition = true;

0 commit comments

Comments
 (0)