Skip to content

Commit 2bf27f5

Browse files
pawpawhecarlescufi
authored andcommitted
samples: prod_consumer: use app_shared.h instead of main.h
Normally main.c file doesn't have a header file, because it doesn't need to declare any interfaces to other modules. In this sample, it's better to put the shared variables in one place, and provide the interfaces to other modules, such as app_a, app_b, and main. Signed-off-by: Paul He <[email protected]>
1 parent befac2a commit 2bf27f5

File tree

5 files changed

+38
-31
lines changed

5 files changed

+38
-31
lines changed

samples/userspace/prod_consumer/src/app_a.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#include <logging/log.h>
1111

1212
#include "sample_driver.h"
13-
#include "main.h"
13+
#include "app_shared.h"
1414
#include "app_a.h"
1515
#include "app_syscall.h"
1616

samples/userspace/prod_consumer/src/app_b.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <sys/libc-hooks.h>
99
#include <logging/log.h>
1010

11-
#include "main.h"
11+
#include "app_shared.h"
1212
#include "app_b.h"
1313

1414
LOG_MODULE_REGISTER(app_b);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
* Copyright (c) 2019 Intel Corporation.
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#include "app_shared.h"
8+
9+
/* Define the shared partition, which will contain a memory region that
10+
* will be accessible by both applications A and B.
11+
*/
12+
K_APPMEM_PARTITION_DEFINE(shared_partition);
13+
14+
/* Define a memory pool to place in the shared area.
15+
*/
16+
K_APP_DMEM(shared_partition) struct sys_heap shared_pool;
17+
K_APP_DMEM(shared_partition) uint8_t shared_pool_mem[HEAP_BYTES];
18+
19+
/* queues for exchanging data between App A and App B */
20+
K_QUEUE_DEFINE(shared_queue_incoming);
21+
K_QUEUE_DEFINE(shared_queue_outgoing);

samples/userspace/prod_consumer/src/main.h renamed to samples/userspace/prod_consumer/src/app_shared.h

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,23 @@
44
* SPDX-License-Identifier: Apache-2.0
55
*/
66

7-
#ifndef MAIN_H
8-
#define MAIN_H
7+
#ifndef APP_SHARED_H
8+
#define APP_SHARED_H
99

1010
#include <kernel.h>
1111
#include <app_memory/app_memdomain.h>
1212
#include <sys/sys_heap.h>
1313

14+
#include "sample_driver.h"
15+
16+
#define BLK_SIZE (SAMPLE_DRIVER_MSG_SIZE + sizeof(void *))
17+
18+
#define HEAP_BYTES (BLK_SIZE * 16)
19+
1420
extern struct k_mem_partition shared_partition;
15-
#define SHARED_DATA K_APP_DMEM(shared_partition)
16-
#define SHARED_BSS K_APP_BMEM(shared_partition)
1721

1822
extern struct sys_heap shared_pool;
23+
extern uint8_t shared_pool_mem[HEAP_BYTES];
1924
extern struct k_queue shared_queue_incoming;
2025
extern struct k_queue shared_queue_outgoing;
2126

samples/userspace/prod_consumer/src/main.c

Lines changed: 6 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,11 @@
55
*/
66

77
#include <kernel.h>
8-
#include <device.h>
98
#include <sys/printk.h>
10-
#include <app_memory/app_memdomain.h>
119
#include <sys/libc-hooks.h>
1210
#include <logging/log.h>
1311

14-
#include "main.h"
15-
#include "sample_driver.h"
12+
#include "app_shared.h"
1613
#include "app_a.h"
1714
#include "app_b.h"
1815

@@ -21,31 +18,13 @@
2118

2219
LOG_MODULE_REGISTER(app_main);
2320

24-
/* Define the shared partition, which will contain a memory region that
25-
* will be accessible by both applications A and B.
26-
*/
27-
K_APPMEM_PARTITION_DEFINE(shared_partition);
28-
29-
/* Define a memory pool to place in the shared area.
30-
*/
31-
#define BLK_SIZE (SAMPLE_DRIVER_MSG_SIZE + sizeof(void *))
32-
33-
#define HEAP_BYTES (BLK_SIZE * 16)
34-
35-
K_APP_DMEM(shared_partition) struct sys_heap shared_pool;
36-
K_APP_DMEM(shared_partition) uint8_t shared_pool_mem[HEAP_BYTES];
37-
38-
/* queues for exchanging data between App A and App B */
39-
K_QUEUE_DEFINE(shared_queue_incoming);
40-
K_QUEUE_DEFINE(shared_queue_outgoing);
41-
4221
/* Define a thread for the root of application A.
43-
* We don't do this for Application B, instead we re-use the main thread
44-
* for it.
4522
*/
4623
struct k_thread app_a_thread;
4724
K_THREAD_STACK_DEFINE(app_a_stack, APP_A_STACKSIZE);
4825

26+
/* Define a thread for the root of application B.
27+
*/
4928
struct k_thread app_b_thread;
5029
K_THREAD_STACK_DEFINE(app_b_stack, APP_B_STACKSIZE);
5130

@@ -55,6 +34,8 @@ void main(void)
5534

5635
LOG_INF("APP A partition: %p %zu", (void *)app_a_partition.start,
5736
(size_t)app_a_partition.size);
37+
LOG_INF("APP B partition: %p %zu", (void *)app_b_partition.start,
38+
(size_t)app_b_partition.size);
5839
LOG_INF("Shared partition: %p %zu", (void *)shared_partition.start,
5940
(size_t)shared_partition.size);
6041
#ifdef Z_LIBC_PARTITION_EXISTS
@@ -69,7 +50,7 @@ void main(void)
6950
-1, K_INHERIT_PERMS, K_NO_WAIT);
7051

7152
/* Spawn supervisor entry for application B */
72-
thread_b = k_thread_create(&app_b_thread, app_b_stack, APP_A_STACKSIZE,
53+
thread_b = k_thread_create(&app_b_thread, app_b_stack, APP_B_STACKSIZE,
7354
app_b_entry, NULL, NULL, NULL,
7455
-1, K_INHERIT_PERMS, K_NO_WAIT);
7556

0 commit comments

Comments
 (0)