Skip to content

Commit f65d17d

Browse files
committed
Added message module.
1 parent b713041 commit f65d17d

File tree

14 files changed

+116
-58
lines changed

14 files changed

+116
-58
lines changed

include/common.h

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,3 @@
11
#ifndef GLOBAL_LOG_LEVEL
22
#define GLOBAL_LOG_LEVEL LOG_LEVEL_INF
3-
#endif
4-
5-
#define MAX_MESSAGE_SIZE 255
6-
7-
struct message
8-
{
9-
uint8_t src_mac[6];
10-
uint8_t original_src_mac[6];
11-
uint8_t dst_mac[6];
12-
uint8_t msg_number;
13-
uint8_t ttl;
14-
uint8_t payload_len;
15-
uint8_t *payload;
16-
};
3+
#endif

mesh_access/src/mesh.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,12 @@
2121
#include <ipc/rpmsg_service.h>
2222

2323
#include <common.h>
24+
2425
#include <mesh.h>
2526

27+
// TODO Remove this and fix packaging
28+
#define MAX_MESSAGE_SIZE 255
29+
2630
LOG_MODULE_REGISTER(mesh_access, GLOBAL_LOG_LEVEL);
2731

2832
static int endpoint_id;

message/CMakeLists.txt

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
zephyr_library()
2+
3+
zephyr_include_directories(
4+
include
5+
../include
6+
)
7+
zephyr_library_sources(src/message.c)

message/Kconfig

Whitespace-only changes.

message/include/message.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <kernel.h>
2+
3+
#define MAX_MESSAGE_SIZE 255
4+
5+
#define SRC_MAC_POS 0
6+
#define ORIGINAL_SRC_MAC_POS 6
7+
#define DST_MAC_POS 12
8+
#define MSG_NUMBER_POS 18
9+
#define TTL_POS 20
10+
#define PAYLOAD_LENGTH_POS 21
11+
#define DATA_POS 22
12+
13+
#define HEADER_LENGTH 22
14+
15+
#define MAX_PAYLOAD_SIZE (MAX_MESSAGE_SIZE - HEADER_LENGTH)
16+
17+
struct message
18+
{
19+
uint8_t src_mac[6];
20+
uint8_t original_src_mac[6];
21+
uint8_t dst_mac[6];
22+
uint8_t msg_number;
23+
uint8_t ttl;
24+
uint8_t payload_len;
25+
uint8_t *payload;
26+
};
27+
28+
struct message *message_from_buffer(struct k_heap *heap, uint8_t *data, size_t length);
29+
30+
size_t message_to_buffer(uint8_t *buffer, struct message *msg);

message/src/message.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
#include <stdint.h>
2+
#include <stdio.h>
3+
4+
#include <logging/log.h>
5+
#include <logging/log_ctrl.h>
6+
#include <zephyr.h>
7+
8+
#include <common.h>
9+
#include <message.h>
10+
11+
LOG_MODULE_REGISTER(message, GLOBAL_LOG_LEVEL);
12+
13+
struct message *message_from_buffer(struct k_heap *heap, uint8_t *data, size_t length)
14+
{
15+
if (length < HEADER_LENGTH ||
16+
length > MAX_MESSAGE_SIZE ||
17+
length != data[PAYLOAD_LENGTH_POS] + HEADER_LENGTH)
18+
{
19+
LOG_DBG("Invalid packet");
20+
return NULL;
21+
}
22+
struct message *msg = (struct message *)k_heap_alloc(heap, sizeof(struct message), K_NO_WAIT);
23+
if (msg == NULL)
24+
{
25+
LOG_ERR("Cannot allocate heap memory");
26+
return NULL;
27+
}
28+
memcpy(msg->src_mac, data + SRC_MAC_POS, sizeof(uint8_t) * 6);
29+
memcpy(msg->original_src_mac, data + ORIGINAL_SRC_MAC_POS, sizeof(uint8_t) * 6);
30+
memcpy(msg->dst_mac, data + DST_MAC_POS, sizeof(uint8_t) * 6);
31+
msg->msg_number = data[MSG_NUMBER_POS];
32+
msg->ttl = data[TTL_POS];
33+
msg->payload_len = data[PAYLOAD_LENGTH_POS];
34+
msg->payload = (uint8_t *)k_heap_alloc(heap, msg->payload_len, K_NO_WAIT);
35+
if (msg->payload == NULL)
36+
{
37+
LOG_ERR("Cannot allocate heap memory");
38+
k_heap_free(heap, msg);
39+
return NULL;
40+
}
41+
memcpy(msg->payload, data + DATA_POS, msg->payload_len);
42+
return msg;
43+
}
44+
45+
size_t message_to_buffer(uint8_t *buffer, struct message *msg)
46+
{
47+
memcpy(buffer + SRC_MAC_POS, msg->src_mac, sizeof(uint8_t) * 6);
48+
memcpy(buffer + ORIGINAL_SRC_MAC_POS, msg->original_src_mac, sizeof(uint8_t) * 6);
49+
memcpy(buffer + DST_MAC_POS, msg->dst_mac, sizeof(uint8_t) * 6);
50+
buffer[MSG_NUMBER_POS] = msg->msg_number;
51+
buffer[TTL_POS] = msg->ttl;
52+
buffer[PAYLOAD_LENGTH_POS] = msg->payload_len;
53+
memcpy(buffer + DATA_POS, msg->payload, msg->payload_len);
54+
return msg->payload_len + HEADER_LENGTH;
55+
}

message/zephyr/module.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
name: message
2+
build:
3+
cmake: .
4+
kconfig: ./Kconfig

net_core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ cmake_minimum_required(VERSION 3.8.2)
22

33
set(BOARD nrf5340dk_nrf5340_cpunet)
44

5+
list(APPEND ZEPHYR_EXTRA_MODULES ${CMAKE_SOURCE_DIR}/../message/)
6+
57
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
68
project(NONE)
79

net_core/include/node.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
#define HEADER_LENGTH 22
2-
3-
#define SRC_MAC_POS 0
4-
#define ORIGINAL_SRC_MAC_POS 6
5-
#define DST_MAC_POS 12
6-
#define MSG_NUMBER_POS 18
7-
#define TTL_POS 20
8-
#define PAYLOAD_LENGTH_POS 21
9-
#define DATA_POS 22
10-
111
#define MAX_HASH_COUNT_LIMIT 100
122

133
/**

net_core/src/hash.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
#include <stdio.h>
33

44
#include <common.h>
5+
#include <message.h>
6+
57
#include <msg.h>
68
#include <node.h>
79

0 commit comments

Comments
 (0)