Skip to content

Commit b39d418

Browse files
committed
test: threading test rpc call with thread join
1 parent 703566f commit b39d418

File tree

1 file changed

+43
-40
lines changed

1 file changed

+43
-40
lines changed

examples/test_rpc_thread/test_rpc_thread.ino

Lines changed: 43 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -5,42 +5,37 @@
55
#define THREAD_STACK_SIZE 500
66
#define THREAD_PRIORITY 5
77

8-
// 2nd thread definition
9-
struct k_thread rpc_thread;
10-
k_thread_stack_t *rpc_stack_area = k_thread_stack_alloc(THREAD_STACK_SIZE, 0);
11-
12-
// RPC call and its mutex
13-
struct k_mutex mtx;
14-
158

169
void rpc_thread_entry(void *p1, void *p2, void *p3) {
1710
RpcCall<MsgPack::str_t> *call = reinterpret_cast<RpcCall<MsgPack::str_t>*>(p1);
11+
struct k_mutex *mtx = reinterpret_cast<struct k_mutex*>(p2);
1812

13+
// Give setup() time to complete first result()
1914
k_sleep(K_MSEC(400));
2015

21-
Serial.println("\n*** Second Thread ***");
22-
Serial.println("*** Calling result() again...");
16+
Serial.println("\n--- Second Thread ---");
17+
Serial.println("Calling result() again...");
2318

24-
k_mutex_lock(&mtx, K_FOREVER);
19+
k_mutex_lock(mtx, K_FOREVER);
2520

2621
MsgPack::str_t msg;
2722
bool ok = call->result(msg);
2823

2924
if (ok) {
30-
Serial.println("*** Second call succeeded (unexpected!)");
31-
Serial.print("**** Message: ");
25+
Serial.println("ERR - Second call succeeded (unexpected!)");
26+
Serial.print("Message: ");
3227
Serial.println(msg.c_str());
3328
} else {
34-
Serial.println("*** Second call FAILED as expected (already executed)");
35-
Serial.print("*** Error Code: 0x");
29+
Serial.println("OK - Second call FAILED as expected (already executed)");
30+
Serial.print("Error Code: 0x");
3631
Serial.println(call->getErrorCode(), HEX);
37-
Serial.print("*** Error Message: ");
32+
Serial.print("Error Message: ");
3833
Serial.println(call->getErrorMessage().c_str());
3934
}
4035

41-
k_mutex_unlock(&mtx);
36+
k_mutex_unlock(mtx);
4237

43-
Serial.println("*** Second Thread End ***\n");
38+
Serial.println("--- Second Thread End ---\n");
4439
}
4540

4641

@@ -50,60 +45,68 @@ void setup() {
5045

5146
Serial.println("\n=== Threaded RPC Test ===\n");
5247

48+
Serial.println("*** Main Thread (setup) ***");
49+
5350
Bridge.begin();
5451
Monitor.begin();
5552

56-
k_mutex_init(&mtx);
53+
static struct k_mutex loop_mtx;
54+
k_mutex_init(&loop_mtx);
5755

58-
// ---- First result() call in main thread ----
59-
Serial.println("--- First thread waits for the other side ---");
60-
k_sleep(K_MSEC(5000));
61-
Serial.println("--- First result() call (main thread) ---");
6256
RpcCall loopback_call = Bridge.call("loopback", "TEST");
6357

6458
if (loopback_call.isError()) {
65-
Serial.println("--- Bridge call before execution");
66-
Serial.print("--- Error Code: 0x");
59+
Serial.println("OK - RPC call in Error mode before execution");
60+
Serial.print("Error Code: 0x");
6761
Serial.println(loopback_call.getErrorCode(), HEX);
68-
Serial.print("--- Error Message: ");
62+
Serial.print("Error Message: ");
6963
Serial.println(loopback_call.getErrorMessage().c_str());
64+
} else {
65+
Serial.println("ERR - RPC call not in Error mode before execution (unexpected)");
7066
}
7167

68+
Serial.println("Waiting for the other side...\n");
69+
delay(2000);
70+
71+
Serial.println("calling .result() on RPC call (main thread)");
72+
7273
MsgPack::str_t msg;
73-
k_mutex_lock(&mtx, K_FOREVER);
74+
k_mutex_lock(&loop_mtx, K_FOREVER);
7475
bool ok = loopback_call.result(msg);
75-
k_mutex_unlock(&mtx);
76+
k_mutex_unlock(&loop_mtx);
7677

7778
if (ok) {
78-
Serial.println("--- First call succeeded.");
79-
Serial.print("--- Message: ");
79+
Serial.println("OK - First call succeeded.");
80+
Serial.print("Message: ");
8081
Serial.println(msg.c_str());
8182
} else {
82-
Serial.println("--- First call FAILED (unexpected).");
83+
Serial.println("ERR - First call FAILED (unexpected).");
8384
}
8485

8586
// ---- Launch second thread ----
86-
Serial.println("\n--- Starting second thread...");
87+
Serial.println("\nStarting second thread...");
88+
89+
struct k_thread rpc_thread;
90+
91+
k_thread_stack_t *rpc_stack_area = k_thread_stack_alloc(THREAD_STACK_SIZE, 0);
8792

8893
k_tid_t rpc_tid = k_thread_create(
8994
&rpc_thread,
9095
rpc_stack_area,
9196
THREAD_STACK_SIZE,
9297
rpc_thread_entry,
93-
&loopback_call,
94-
NULL,
98+
&loopback_call, // p1 → RpcCall*
99+
&loop_mtx, // p2 → mutex
95100
NULL,
96101
THREAD_PRIORITY,
97102
0,
98-
K_NO_WAIT
103+
K_FOREVER
99104
);
100105

101-
k_thread_name_set(rpc_tid, "test");
102-
103-
Serial.println("--- Second thread launched.\n");
104-
105-
//k_thread_join(rpc_tid, K_NO_WAIT); // Works in ISRs only!
106-
k_sleep(K_MSEC(5000));
106+
k_thread_start(rpc_tid);
107+
Serial.println("Second thread launched... joining");
108+
k_thread_join(&rpc_thread, K_FOREVER);
109+
Serial.println("*** Main thread end ending setup ***");
107110

108111
}
109112

0 commit comments

Comments
 (0)