Skip to content

Commit c445590

Browse files
Juha Heiskanenjuhhei01
authored andcommitted
MAC IE element library
Added MAC IE library which support IE Header and Payload base write and discover spesific element from received data. Unit test for new module. Change-Id: I80c571598603349b434bb4bfc080dc524da61326
1 parent 6e94b08 commit c445590

File tree

11 files changed

+362
-1
lines changed

11 files changed

+362
-1
lines changed

source/6LoWPAN/MAC/mac_ie_lib.c

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
*/
4+
#include "nsconfig.h"
5+
#include "ns_types.h"
6+
#include "string.h"
7+
#include "common_functions.h"
8+
#include "mac_common_defines.h"
9+
10+
11+
#define MAC_IE_HEADER_LENGTH_MASK 0x007f
12+
#define MAC_IE_HEADER_ID_MASK 0x7f80
13+
#define MAC_IE_PAYLOAD_LENGTH_MASK 0x07ff
14+
#define MAC_IE_PAYLOAD_ID_MASK 0x7800
15+
16+
static void mac_ie_header_parse(mac_header_IE_t *header_element, uint8_t *ptr)
17+
{
18+
uint16_t ie_dummy = common_read_16_bit_inverse(ptr);
19+
header_element->length = (ie_dummy & MAC_IE_HEADER_LENGTH_MASK);
20+
header_element->id = ((ie_dummy & MAC_IE_HEADER_ID_MASK ) >> 7 );
21+
header_element->content_ptr = ptr + 2;
22+
}
23+
24+
static void mac_ie_payload_parse(mac_payload_IE_t *payload_element, uint8_t *ptr)
25+
{
26+
uint16_t ie_dummy = common_read_16_bit_inverse(ptr);
27+
payload_element->length = (ie_dummy & MAC_IE_PAYLOAD_LENGTH_MASK);
28+
payload_element->id = ((ie_dummy & MAC_IE_PAYLOAD_ID_MASK ) >> 11);
29+
payload_element->content_ptr = ptr + 2;
30+
}
31+
32+
33+
uint8_t *mac_ie_header_base_write(uint8_t *ptr, uint8_t type, uint16_t length)
34+
{
35+
uint16_t ie_dummy = 0; //Header Type
36+
ie_dummy |= (length & MAC_IE_HEADER_LENGTH_MASK);
37+
ie_dummy |= ((type << 7 ) & MAC_IE_HEADER_ID_MASK);
38+
return common_write_16_bit_inverse(ie_dummy, ptr);
39+
}
40+
41+
uint8_t *mac_ie_payload_base_write(uint8_t *ptr, uint8_t type, uint16_t length)
42+
{
43+
uint16_t ie_dummy = 0x8000; //Payload type
44+
ie_dummy |= (length & MAC_IE_PAYLOAD_LENGTH_MASK);
45+
ie_dummy |= ((type << 11 ) & MAC_IE_PAYLOAD_ID_MASK);
46+
return common_write_16_bit_inverse(ie_dummy, ptr);
47+
}
48+
49+
uint16_t mac_ie_payload_discover(uint8_t *payload_ptr, uint16_t length, mac_payload_IE_t * payload_ie)
50+
{
51+
mac_payload_IE_t ie_element;
52+
while (length >= 2) {
53+
mac_ie_payload_parse(&ie_element, payload_ptr);
54+
if (payload_ie->id == ie_element.id) {
55+
payload_ie->content_ptr = ie_element.content_ptr;
56+
payload_ie->length = ie_element.length;
57+
return ie_element.length;
58+
}
59+
60+
length -= ie_element.length +2;
61+
62+
payload_ptr += ie_element.length + 2;
63+
}
64+
return 0;
65+
}
66+
67+
uint8_t mac_ie_header_discover(uint8_t *header_ptr, uint16_t length, mac_header_IE_t * header_ie)
68+
{
69+
mac_header_IE_t ie_element;
70+
while (length >= 2) {
71+
mac_ie_header_parse(&ie_element, header_ptr);
72+
if (header_ie->id == ie_element.id) {
73+
header_ie->content_ptr = ie_element.content_ptr;
74+
header_ie->length = ie_element.length;
75+
return ie_element.length;
76+
}
77+
78+
length -= ie_element.length +2;
79+
80+
header_ptr += ie_element.length + 2;
81+
}
82+
return 0;
83+
}

source/6LoWPAN/MAC/mac_ie_lib.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
*/
4+
5+
#ifndef MAC_IE_LIB_H_
6+
#define MAC_IE_LIB_H_
7+
8+
struct mac_payload_IE_s;
9+
struct mac_payload_IE_s;
10+
11+
uint8_t *mac_ie_header_base_write(uint8_t *ptr, uint8_t type, uint16_t length);
12+
uint8_t *mac_ie_payload_base_write(uint8_t *ptr, uint8_t type, uint16_t length);
13+
uint16_t mac_ie_payload_discover(uint8_t *payload_ptr, uint16_t length, struct mac_payload_IE_s * payload_ie);
14+
uint8_t mac_ie_header_discover(uint8_t *header_ptr, uint16_t length, struct mac_header_IE_s * header_ie);
15+
16+
#endif /* MAC_IE_LIB_H_ */

sources.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ SRCS += \
1111
source/6LoWPAN/IPHC_Decode/lowpan_context.c \
1212
source/6LoWPAN/MAC/beacon_handler.c \
1313
source/6LoWPAN/MAC/mac_helper.c \
14+
source/6LoWPAN/MAC/mac_ie_lib.c \
1415
source/6LoWPAN/MAC/mac_response_handler.c \
1516
source/6LoWPAN/MAC/mac_data_poll.c \
1617
source/6LoWPAN/MAC/mac_pairwise_key.c \
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
include ../../makefile_defines.txt
2+
3+
COMPONENT_NAME = mac_ie_lib_unit
4+
5+
#This must be changed manually
6+
SRC_FILES = \
7+
../../../../../source/6LoWPAN/MAC/mac_ie_lib.c
8+
9+
TEST_SRC_FILES = \
10+
main.cpp \
11+
mac_ie_libtest.cpp \
12+
test_mac_ie_lib.c \
13+
../../stub/mbed_trace_stub.c \
14+
../../stub/common_functions_stub.c \
15+
16+
include ../../MakefileWorker.mk
17+
18+
CPPUTESTFLAGS += -DFEA_TRACE_SUPPORT
19+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#include "CppUTest/TestHarness.h"
2+
#include "test_mac_ie_lib.h"
3+
4+
TEST_GROUP(mac_ie_lib)
5+
{
6+
void setup()
7+
{
8+
}
9+
10+
void teardown()
11+
{
12+
}
13+
};
14+
15+
TEST(mac_ie_lib, test_mac_ie_header_base_write)
16+
{
17+
CHECK(test_mac_ie_header_base_write());
18+
}
19+
20+
TEST(mac_ie_lib, test_mac_ie_payload_base_write)
21+
{
22+
CHECK(test_mac_ie_payload_base_write());
23+
}
24+
25+
TEST(mac_ie_lib, test_mac_ie_payload_discover)
26+
{
27+
CHECK(test_mac_ie_payload_discover());
28+
}
29+
30+
TEST(mac_ie_lib, test_mac_ie_header_discover)
31+
{
32+
CHECK(test_mac_ie_header_discover());
33+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*
2+
* Copyright (c) 2016, Arm Limited and affiliates.
3+
* SPDX-License-Identifier: Apache-2.0
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#include "CppUTest/CommandLineTestRunner.h"
19+
#include "CppUTest/TestPlugin.h"
20+
#include "CppUTest/TestRegistry.h"
21+
#include "CppUTestExt/MockSupportPlugin.h"
22+
int main(int ac, char** av)
23+
{
24+
return CommandLineTestRunner::RunAllTests(ac, av);
25+
}
26+
27+
IMPORT_TEST_GROUP(mac_ie_lib);
28+
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
#include "nsconfig.h"
2+
#include "ns_types.h"
3+
#include "string.h"
4+
#include "common_functions.h"
5+
#include "mac_common_defines.h"
6+
#include "test_mac_ie_lib.h"
7+
#include "mac_ie_lib.h"
8+
#include "common_functions_stub.h"
9+
10+
bool test_mac_ie_header_base_write()
11+
{
12+
uint8_t buffer[4];
13+
uint8_t *ptr = mac_ie_header_base_write(buffer, 0, 0);
14+
if (ptr != &buffer[2]) {
15+
return false;
16+
}
17+
return true;
18+
}
19+
20+
bool test_mac_ie_payload_base_write()
21+
{
22+
uint8_t buffer[4];
23+
uint8_t *ptr = mac_ie_payload_base_write(buffer, 0, 0);
24+
if (ptr != &buffer[2]) {
25+
return false;
26+
}
27+
return true;
28+
}
29+
30+
bool test_mac_ie_payload_discover()
31+
{
32+
uint8_t buffer[60];
33+
mac_payload_IE_t mac_payload_ie;
34+
35+
mac_payload_ie.id = MAC_PAYLOAD_MPX_IE_GROUP_ID;
36+
37+
if (mac_ie_payload_discover(buffer, 0, &mac_payload_ie) ) {
38+
return false;
39+
}
40+
common_functions_stub.readuint16_from_queue = 0;
41+
common_functions_stub.uint16_value = 0x8000 + 5;
42+
common_functions_stub.uint16_value |= ((MAC_PAYLOAD_MPX_IE_GROUP_ID << 11 ) & 0x7800);
43+
if (mac_ie_payload_discover(buffer, 16, &mac_payload_ie) != 5) {
44+
return false;
45+
}
46+
47+
if (mac_payload_ie.content_ptr != &buffer[2] || mac_payload_ie.length != 5) {
48+
return false;
49+
}
50+
51+
common_functions_stub.readuint16_from_queue = 2;
52+
53+
common_functions_stub.uint16_value_queue[1] = 0x8000 + 7;
54+
common_functions_stub.uint16_value_queue[1] |= ((MAC_PAYLOAD_IE_ESDU_GROUP_ID << 11 ) & 0x7800);
55+
common_functions_stub.uint16_value_queue[0] = 0x8000 + 40;
56+
common_functions_stub.uint16_value_queue[0] |= ((MAC_PAYLOAD_MPX_IE_GROUP_ID << 11 ) & 0x7800);
57+
58+
59+
if (mac_ie_payload_discover(buffer, 49, &mac_payload_ie) != 40) {
60+
return false;
61+
}
62+
63+
if (mac_payload_ie.content_ptr != &buffer[11] || mac_payload_ie.length != 40) {
64+
return false;
65+
}
66+
67+
68+
return true;
69+
}
70+
71+
bool test_mac_ie_header_discover()
72+
{
73+
uint8_t buffer[60];
74+
mac_header_IE_t header_ie;
75+
header_ie.id = MAC_HEADER_ASSIGNED_EXTERNAL_ORG_IE_ID;
76+
if (mac_ie_header_discover(buffer, 0, &header_ie) ) {
77+
return false;
78+
}
79+
common_functions_stub.readuint16_from_queue = 0;
80+
81+
common_functions_stub.uint16_value = 5;
82+
common_functions_stub.uint16_value |= ((MAC_HEADER_ASSIGNED_EXTERNAL_ORG_IE_ID << 7 ) & 0x7f80);
83+
if (mac_ie_header_discover(buffer, 16, &header_ie) != 5) {
84+
return false;
85+
}
86+
87+
if (header_ie.content_ptr != &buffer[2] || header_ie.length != 5) {
88+
return false;
89+
}
90+
91+
common_functions_stub.readuint16_from_queue = 2;
92+
93+
common_functions_stub.uint16_value_queue[1] = 7;
94+
common_functions_stub.uint16_value_queue[1] |= ((MAC_HEADER_VENDOR_SPESIFIC_IE_ID << 7 ) & 0x7f80);
95+
common_functions_stub.uint16_value_queue[0] = 40;
96+
common_functions_stub.uint16_value_queue[0] |= ((MAC_HEADER_ASSIGNED_EXTERNAL_ORG_IE_ID << 7 ) & 0x7f80);
97+
98+
99+
if (mac_ie_header_discover(buffer, 49, &header_ie) != 40) {
100+
return false;
101+
}
102+
103+
if (header_ie.content_ptr != &buffer[11] || header_ie.length != 40) {
104+
return false;
105+
}
106+
107+
108+
return true;
109+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#ifndef __TEST_MAC_IE_LIB_H__
2+
#define __TEST_MAC_IE_LIB_H__
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
bool test_mac_ie_header_base_write();
9+
bool test_mac_ie_payload_base_write();
10+
bool test_mac_ie_payload_discover();
11+
bool test_mac_ie_header_discover();
12+
13+
14+
#ifdef __cplusplus
15+
}
16+
#endif
17+
18+
#endif // __TEST_MAC_IE_LIB_H__

test/nanostack/unittest/stub/common_functions_stub.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ COMMON_FUNCTIONS_FN uint32_t common_read_32_bit(const uint8_t data_buf[__static
6565
return common_functions_stub.uint32_value;
6666
}
6767

68-
COMMON_FUNCTIONS_FN uint32_t common_read_16_bit_inverse(const uint8_t data_buf[__static 4])
68+
COMMON_FUNCTIONS_FN uint16_t common_read_16_bit_inverse(const uint8_t data_buf[__static 4])
6969
{
7070
memcpy(common_functions_stub.argument_table, data_buf, 2);
7171
if (common_functions_stub.readuint16_from_queue) {
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/*
2+
* Copyright (c) 2018 ARM Limited. All rights reserved.
3+
*/
4+
#include "ns_config.h"
5+
#include "ns_types.h"
6+
#include "string.h"
7+
#include "common_functions.h"
8+
#include "mac_common_defines.h"
9+
#include "mac_ie_lib_stub.h"
10+
11+
mac_ie_lib_stub_def_t mac_ie_lib_stub_def;
12+
13+
14+
uint8_t *mac_ie_header_base_write(uint8_t *ptr, uint8_t type, uint16_t length)
15+
{
16+
return ptr + 2;
17+
}
18+
19+
uint8_t *mac_ie_payload_base_write(uint8_t *ptr, uint8_t type, uint16_t length)
20+
{
21+
return ptr + 2;
22+
}
23+
24+
uint16_t mac_ie_payload_discover(uint8_t *payload_ptr, uint16_t length, mac_payload_IE_t * payload_ie)
25+
{
26+
payload_ie->content_ptr = payload_ptr;
27+
return mac_ie_lib_stub_def.value_uint16;
28+
}
29+
30+
uint8_t mac_ie_header_discover(uint8_t *header_ptr, uint16_t length, mac_header_IE_t * header_ie)
31+
{
32+
header_ie->content_ptr = payload_ptr;
33+
return mac_ie_lib_stub_def.value_uint8;
34+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#ifndef __MAC_IE_LIB_STUB_H__
2+
#define __MAC_IE_LIB_STUB_H__
3+
4+
#ifdef __cplusplus
5+
extern "C" {
6+
#endif
7+
8+
typedef struct {
9+
uint16_t value_uint16;
10+
uint16_t value_uint8;
11+
} mac_ie_lib_stub_def_t;
12+
13+
extern mac_ie_lib_stub_def_t mac_ie_lib_stub_def;
14+
15+
16+
#ifdef __cplusplus
17+
}
18+
#endif
19+
20+
#endif // __MAC_IE_LIB_STUB_H__

0 commit comments

Comments
 (0)