Skip to content

Commit fbfada9

Browse files
author
Jarkko Paso
authored
Adaptation IF: Allocate fragmentation buffer only if needed (ARMmbed#2407)
* Adaptation IF: Allocate fragmentation buffer only if needed * unit tests: fixed adaptation interface tests
1 parent 66f1bff commit fbfada9

File tree

3 files changed

+22
-20
lines changed

3 files changed

+22
-20
lines changed

source/6LoWPAN/adaptation_interface.c

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -351,17 +351,14 @@ int8_t lowpan_adaptation_interface_init(int8_t interface_id, uint16_t mac_mtu_si
351351

352352
//Allocate new
353353
fragmenter_interface_t *interface_ptr = ns_dyn_mem_alloc(sizeof(fragmenter_interface_t));
354-
uint8_t *tx_buffer = ns_dyn_mem_alloc(mac_mtu_size);
355-
if (!interface_ptr || !tx_buffer) {
356-
ns_dyn_mem_free(interface_ptr);
357-
ns_dyn_mem_free(tx_buffer);
354+
if (!interface_ptr) {
358355
return -1;
359356
}
360357

361358
memset(interface_ptr, 0, sizeof(fragmenter_interface_t));
362359
interface_ptr->interface_id = interface_id;
363-
interface_ptr->fragment_indirect_tx_buffer = tx_buffer;
364-
interface_ptr->mtu_size = mac_mtu_size;
360+
interface_ptr->fragment_indirect_tx_buffer = NULL;
361+
interface_ptr->mtu_size = 0;
365362
interface_ptr->msduHandle = randLIB_get_8bit();
366363
interface_ptr->local_frag_tag = randLIB_get_16bit();
367364

@@ -997,6 +994,18 @@ int8_t lowpan_adaptation_interface_tx(protocol_interface_info_entry_t *cur, buff
997994

998995
//Check packet size
999996
bool fragmented_needed = lowpan_adaptation_request_longer_than_mtu(cur, buf, interface_ptr);
997+
if (fragmented_needed) {
998+
// If fragmentation TX buffer not allocated, do it now.
999+
if (!interface_ptr->fragment_indirect_tx_buffer && !interface_ptr->mtu_size) {
1000+
interface_ptr->fragment_indirect_tx_buffer = ns_dyn_mem_alloc(cur->mac_api->phyMTU);
1001+
if (interface_ptr->fragment_indirect_tx_buffer) {
1002+
interface_ptr->mtu_size = cur->mac_api->phyMTU;
1003+
} else {
1004+
tr_error("Failed to allocate fragmentation buffer");
1005+
goto tx_error_handler;
1006+
}
1007+
}
1008+
}
10001009
bool is_unicast = buf->link_specific.ieee802_15_4.requestAck;
10011010
bool indirect = buf->link_specific.ieee802_15_4.indirectTxProcess;
10021011

source/MAC/IEEE802_15_4/sw_mac.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -104,9 +104,9 @@ mac_api_t *ns_sw_mac_create(int8_t rf_driver_id, mac_description_storage_size_t
104104

105105
// Set default MTU size to 127 unless it is too much for PHY driver
106106
if (driver->phy_driver->phy_MTU > MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE) {
107-
this->phyMTU = driver->phy_driver->phy_MTU;
108-
} else {
109107
this->phyMTU = MAC_IEEE_802_15_4_MAX_PHY_PACKET_SIZE;
108+
} else {
109+
this->phyMTU = driver->phy_driver->phy_MTU;
110110
}
111111

112112
mac_store.setup = mac_mlme_data_base_allocate(mac_store.dev_driver->phy_driver->PHY_MAC, mac_store.dev_driver, storage_sizes, this->phyMTU);

test/nanostack/unittest/6LoWPAN/adaptation_interface/test_adaptation_interface.c

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,17 @@ bool test_lowpan_adaptation_interface_init()
6565
return false;
6666
}
6767

68-
if (-1 != lowpan_adaptation_interface_init(0, 127)) {
69-
return false;
70-
}
71-
7268
nsdynmemlib_stub.returnCounter = 0;
73-
74-
if (-1 != lowpan_adaptation_interface_init(0, 127)) {
75-
return false;
76-
}
77-
nsdynmemlib_stub.returnCounter = 1;
7869
if (-1 != lowpan_adaptation_interface_init(0, 127)) {
7970
return false;
8071
}
8172

82-
nsdynmemlib_stub.returnCounter = 2;
73+
nsdynmemlib_stub.returnCounter = 1;
8374
if (0 != lowpan_adaptation_interface_init(0, 127)) {
8475
return false;
8576
}
8677

87-
nsdynmemlib_stub.returnCounter = 2;
78+
nsdynmemlib_stub.returnCounter = 1;
8879
if (0 != lowpan_adaptation_interface_init(0, 127)) {
8980
return false;
9081
}
@@ -387,6 +378,7 @@ bool test_lowpan_adaptation_interface_tx()
387378
entry.id = 0;
388379
api.mcps_data_req = &mcps_data_req_cb;
389380
api.mcps_purge_req = &tester_mcps_purge_request;
381+
api.phyMTU = 127;
390382
mac_neighbor_table_stub.test_neigh = NULL;
391383

392384
buffer_t *test_buf = malloc(sizeof(buffer_t) + 2100);
@@ -428,7 +420,7 @@ bool test_lowpan_adaptation_interface_tx()
428420
}
429421

430422
//Test Indirect data allocation fail
431-
nsdynmemlib_stub.returnCounter = 2;
423+
nsdynmemlib_stub.returnCounter = 1;
432424
lowpan_adaptation_interface_init(0, 127);
433425

434426
if (0 != lowpan_adaptation_indirect_queue_params_set(&entry, 106, 1, 2)) {
@@ -774,6 +766,7 @@ bool test_lowpan_adaptation_interface_tx_confirm()
774766
entry.mac_api = &api;
775767
entry.id = 0;
776768
api.mcps_data_req = &mcps_data_req_cb;
769+
api.phyMTU = 127;
777770
mac_neighbor_table_stub.test_neigh = NULL;
778771

779772
mac_helper_stub.uint16_value = 110;

0 commit comments

Comments
 (0)