Skip to content

Commit 8458f8b

Browse files
Damian-Nordicrlubos
authored andcommitted
openthread: rpc: avoid sending instance pointer over RPC
All functions except otInstanceXXX skipped sending a pointer to otInstance and simply assumed that only a single instance is supported. Rework otInstanceXXX APIs to make them consistent with the rest and more portable. Signed-off-by: Damian Krolik <[email protected]>
1 parent a423f4b commit 8458f8b

File tree

4 files changed

+74
-175
lines changed

4 files changed

+74
-175
lines changed

subsys/net/openthread/rpc/client/ot_rpc_instance.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,33 @@
1313

1414
#include <openthread/instance.h>
1515

16+
/*
17+
* The actual otInstance object resides on OT RPC server and is only accessed by OT RPC client using
18+
* remote OpenThread API calls. Nevertheless, otInstanceInitSingle() on OT RPC client shall return a
19+
* valid non-null address, so the variable below is defined to represent otInstance object.
20+
*/
21+
static char ot_instance;
22+
1623
otInstance *otInstanceInitSingle(void)
1724
{
1825
struct nrf_rpc_cbor_ctx ctx;
19-
uintptr_t instance_rep;
2026

2127
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
2228

23-
nrf_rpc_cbor_cmd_rsp_no_err(&ot_group, OT_RPC_CMD_INSTANCE_INIT_SINGLE, &ctx);
24-
nrf_rpc_rsp_decode_uint(&ot_group, &ctx, &instance_rep, sizeof(instance_rep));
25-
nrf_rpc_cbor_decoding_done(&ot_group, &ctx);
29+
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_INSTANCE_INIT_SINGLE, &ctx,
30+
nrf_rpc_rsp_decode_void, NULL);
2631

27-
return (otInstance *)instance_rep;
32+
return (otInstance *)&ot_instance;
2833
}
2934

3035
uint32_t otInstanceGetId(otInstance *aInstance)
3136
{
3237
struct nrf_rpc_cbor_ctx ctx;
3338
uint32_t id;
3439

35-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 1 + sizeof(uintptr_t));
36-
nrf_rpc_encode_uint(&ctx, (uintptr_t)aInstance);
40+
ARG_UNUSED(aInstance);
3741

42+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
3843
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_INSTANCE_GET_ID, &ctx, nrf_rpc_rsp_decode_u32,
3944
&id);
4045

@@ -46,9 +51,9 @@ bool otInstanceIsInitialized(otInstance *aInstance)
4651
struct nrf_rpc_cbor_ctx ctx;
4752
bool initialized;
4853

49-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 1 + sizeof(uintptr_t));
50-
nrf_rpc_encode_uint(&ctx, (uintptr_t)aInstance);
54+
ARG_UNUSED(aInstance);
5155

56+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
5257
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_INSTANCE_IS_INITIALIZED, &ctx,
5358
nrf_rpc_rsp_decode_bool, &initialized);
5459

@@ -59,9 +64,9 @@ void otInstanceFinalize(otInstance *aInstance)
5964
{
6065
struct nrf_rpc_cbor_ctx ctx;
6166

62-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 1 + sizeof(uintptr_t));
63-
nrf_rpc_encode_uint(&ctx, (uintptr_t)aInstance);
67+
ARG_UNUSED(aInstance);
6468

69+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
6570
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_INSTANCE_FINALIZE, &ctx,
6671
nrf_rpc_rsp_decode_void, NULL);
6772
}
@@ -71,9 +76,9 @@ otError otInstanceErasePersistentInfo(otInstance *aInstance)
7176
struct nrf_rpc_cbor_ctx ctx;
7277
otError error;
7378

74-
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 1 + sizeof(uintptr_t));
75-
nrf_rpc_encode_uint(&ctx, (uintptr_t)aInstance);
79+
ARG_UNUSED(aInstance);
7680

81+
NRF_RPC_CBOR_ALLOC(&ot_group, ctx, 0);
7782
nrf_rpc_cbor_cmd_no_err(&ot_group, OT_RPC_CMD_INSTANCE_ERASE_PERSISTENT_INFO, &ctx,
7883
ot_rpc_decode_error, &error);
7984

subsys/net/openthread/rpc/server/ot_rpc_instance.c

Lines changed: 10 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,13 @@ static ot_rpc_callback_t *ot_rpc_callback_del(uint32_t callback, uint32_t contex
7272
static void ot_rpc_cmd_instance_init_single(const struct nrf_rpc_group *group,
7373
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
7474
{
75-
otInstance *instance;
76-
7775
nrf_rpc_cbor_decoding_done(group, ctx);
7876

7977
ot_rpc_mutex_lock();
80-
instance = otInstanceInitSingle();
78+
(void)otInstanceInitSingle();
8179
ot_rpc_mutex_unlock();
8280

83-
nrf_rpc_rsp_send_uint(group, (uintptr_t)instance);
81+
nrf_rpc_rsp_send_void(group);
8482
}
8583

8684
NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_instance_init_single, OT_RPC_CMD_INSTANCE_INIT_SINGLE,
@@ -89,24 +87,12 @@ NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_instance_init_single, OT_RPC_CMD_I
8987
static void ot_rpc_cmd_instance_get_id(const struct nrf_rpc_group *group,
9088
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
9189
{
92-
otInstance *instance;
9390
uint32_t instance_id;
9491

95-
instance = (otInstance *)nrf_rpc_decode_uint(ctx);
96-
97-
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
98-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
99-
return;
100-
}
101-
102-
if (instance != openthread_get_default_instance()) {
103-
/* The instance is unknown to the OT RPC server. */
104-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
105-
return;
106-
}
92+
nrf_rpc_cbor_decoding_done(group, ctx);
10793

10894
ot_rpc_mutex_lock();
109-
instance_id = otInstanceGetId(instance);
95+
instance_id = otInstanceGetId(openthread_get_default_instance());
11096
ot_rpc_mutex_unlock();
11197

11298
nrf_rpc_rsp_send_uint(group, instance_id);
@@ -118,24 +104,12 @@ NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_instance_get_id, OT_RPC_CMD_INSTAN
118104
static void ot_rpc_cmd_instance_is_initialized(const struct nrf_rpc_group *group,
119105
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
120106
{
121-
otInstance *instance;
122107
bool initialized;
123108

124-
instance = (otInstance *)nrf_rpc_decode_uint(ctx);
125-
126-
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
127-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
128-
return;
129-
}
130-
131-
if (instance != openthread_get_default_instance()) {
132-
/* The instance is unknown to the OT RPC server. */
133-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
134-
return;
135-
}
109+
nrf_rpc_cbor_decoding_done(group, ctx);
136110

137111
ot_rpc_mutex_lock();
138-
initialized = otInstanceIsInitialized(instance);
112+
initialized = otInstanceIsInitialized(openthread_get_default_instance());
139113
ot_rpc_mutex_unlock();
140114

141115
nrf_rpc_rsp_send_bool(group, initialized);
@@ -148,23 +122,10 @@ NRF_RPC_CBOR_CMD_DECODER(ot_group, ot_rpc_cmd_instance_is_initialized,
148122
static void ot_rpc_cmd_instance_finalize(const struct nrf_rpc_group *group,
149123
struct nrf_rpc_cbor_ctx *ctx, void *handler_data)
150124
{
151-
otInstance *instance;
152-
153-
instance = (otInstance *)nrf_rpc_decode_uint(ctx);
154-
155-
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
156-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
157-
return;
158-
}
159-
160-
if (instance != openthread_get_default_instance()) {
161-
/* The instance is unknown to the OT RPC server. */
162-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
163-
return;
164-
}
125+
nrf_rpc_cbor_decoding_done(group, ctx);
165126

166127
ot_rpc_mutex_lock();
167-
otInstanceFinalize(instance);
128+
otInstanceFinalize(openthread_get_default_instance());
168129
ot_rpc_mutex_unlock();
169130

170131
nrf_rpc_rsp_send_void(group);
@@ -177,24 +138,12 @@ static void ot_rpc_cmd_instance_erase_persistent_info(const struct nrf_rpc_group
177138
struct nrf_rpc_cbor_ctx *ctx,
178139
void *handler_data)
179140
{
180-
otInstance *instance;
181141
otError error;
182142

183-
instance = (otInstance *)nrf_rpc_decode_uint(ctx);
184-
185-
if (!nrf_rpc_decoding_done_and_check(group, ctx)) {
186-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
187-
return;
188-
}
189-
190-
if (instance != openthread_get_default_instance()) {
191-
/* The instance is unknown to the OT RPC server. */
192-
ot_rpc_report_cmd_decoding_error(OT_RPC_CMD_INSTANCE_GET_ID);
193-
return;
194-
}
143+
nrf_rpc_cbor_decoding_done(group, ctx);
195144

196145
ot_rpc_mutex_lock();
197-
error = otInstanceErasePersistentInfo(instance);
146+
error = otInstanceErasePersistentInfo(openthread_get_default_instance());
198147
ot_rpc_mutex_unlock();
199148

200149
nrf_rpc_rsp_send_uint(group, error);

tests/subsys/net/openthread/rpc/client/src/instance_suite.c

Lines changed: 26 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,6 @@
1313

1414
#include <openthread/instance.h>
1515

16-
/* Instance address used when testing serialization of a function that takes otInstance* */
17-
#define INSTANCE_ADDR UINT32_MAX
18-
1916
static void nrf_rpc_err_handler(const struct nrf_rpc_err_report *report)
2017
{
2118
zassert_ok(report->code);
@@ -28,40 +25,34 @@ static void tc_setup(void *f)
2825
mock_nrf_rpc_tr_expect_reset();
2926
}
3027

31-
/* Test serialization of otInstanceInitSingle() returning 0 */
28+
/* Test serialization of otInstanceInitSingle() */
3229
ZTEST(ot_rpc_instance, test_otInstanceInitSingle_0)
3330
{
3431
otInstance *instance;
32+
otInstance *instance2;
3533

36-
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_INIT_SINGLE), RPC_RSP(0));
34+
/* Verify a non-null instance is returned from the function. */
35+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_INIT_SINGLE), RPC_RSP());
3736
instance = otInstanceInitSingle();
3837
mock_nrf_rpc_tr_expect_done();
3938

40-
zassert_equal(instance, NULL);
41-
}
42-
43-
/* Test serialization of otInstanceInitSingle() returning max allowed 0xffffffff */
44-
ZTEST(ot_rpc_instance, test_otInstanceInitSingle_max)
45-
{
46-
otInstance *instance;
39+
zassert_not_null(instance, NULL);
4740

48-
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_INIT_SINGLE),
49-
RPC_RSP(CBOR_UINT32(UINT32_MAX)));
50-
instance = otInstanceInitSingle();
41+
/* Verify that the same instance is returned for subsequent calls. */
42+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_INIT_SINGLE), RPC_RSP());
43+
instance2 = otInstanceInitSingle();
5144
mock_nrf_rpc_tr_expect_done();
5245

53-
zassert_equal(instance, (void *)UINT32_MAX);
46+
zassert_equal(instance, instance2);
5447
}
5548

5649
/* Test serialization of otInstanceGetId() returning 0 */
5750
ZTEST(ot_rpc_instance, test_otInstanceGetId_0)
5851
{
59-
otInstance *instance = (otInstance *)INSTANCE_ADDR;
6052
uint32_t id;
6153

62-
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_GET_ID, CBOR_UINT32(INSTANCE_ADDR)),
63-
RPC_RSP(0));
64-
id = otInstanceGetId(instance);
54+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_GET_ID), RPC_RSP(0));
55+
id = otInstanceGetId(NULL);
6556
mock_nrf_rpc_tr_expect_done();
6657

6758
zassert_equal(id, 0);
@@ -70,12 +61,11 @@ ZTEST(ot_rpc_instance, test_otInstanceGetId_0)
7061
/* Test serialization of otInstanceGetId() returning max allowed UINT32_MAX */
7162
ZTEST(ot_rpc_instance, test_otInstanceGetId_max)
7263
{
73-
otInstance *instance = (otInstance *)INSTANCE_ADDR;
7464
uint32_t id;
7565

76-
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_GET_ID, CBOR_UINT32(INSTANCE_ADDR)),
66+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_GET_ID),
7767
RPC_RSP(CBOR_UINT32(UINT32_MAX)));
78-
id = otInstanceGetId(instance);
68+
id = otInstanceGetId(NULL);
7969
mock_nrf_rpc_tr_expect_done();
8070

8171
zassert_equal(id, UINT32_MAX);
@@ -84,13 +74,11 @@ ZTEST(ot_rpc_instance, test_otInstanceGetId_max)
8474
/* Test serialization of otInstanceIsInitialized() returning false */
8575
ZTEST(ot_rpc_instance, test_otInstanceIsInitialized_false)
8676
{
87-
otInstance *instance = (otInstance *)INSTANCE_ADDR;
8877
bool initialized;
8978

90-
mock_nrf_rpc_tr_expect_add(
91-
RPC_CMD(OT_RPC_CMD_INSTANCE_IS_INITIALIZED, CBOR_UINT32(INSTANCE_ADDR)),
92-
RPC_RSP(CBOR_FALSE));
93-
initialized = otInstanceIsInitialized(instance);
79+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_IS_INITIALIZED),
80+
RPC_RSP(CBOR_FALSE));
81+
initialized = otInstanceIsInitialized(NULL);
9482
mock_nrf_rpc_tr_expect_done();
9583

9684
zassert_false(initialized);
@@ -99,13 +87,10 @@ ZTEST(ot_rpc_instance, test_otInstanceIsInitialized_false)
9987
/* Test serialization of otInstanceIsInitialized() returning true */
10088
ZTEST(ot_rpc_instance, test_otInstanceIsInitialized_true)
10189
{
102-
otInstance *instance = (otInstance *)INSTANCE_ADDR;
10390
bool initialized;
10491

105-
mock_nrf_rpc_tr_expect_add(
106-
RPC_CMD(OT_RPC_CMD_INSTANCE_IS_INITIALIZED, CBOR_UINT32(INSTANCE_ADDR)),
107-
RPC_RSP(CBOR_TRUE));
108-
initialized = otInstanceIsInitialized(instance);
92+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_IS_INITIALIZED), RPC_RSP(CBOR_TRUE));
93+
initialized = otInstanceIsInitialized(NULL);
10994
mock_nrf_rpc_tr_expect_done();
11095

11196
zassert_true(initialized);
@@ -114,24 +99,19 @@ ZTEST(ot_rpc_instance, test_otInstanceIsInitialized_true)
11499
/* Test serialization of otInstanceFinalize() */
115100
ZTEST(ot_rpc_instance, test_otInstanceFinalize)
116101
{
117-
otInstance *instance = (otInstance *)INSTANCE_ADDR;
118-
119-
mock_nrf_rpc_tr_expect_add(
120-
RPC_CMD(OT_RPC_CMD_INSTANCE_FINALIZE, CBOR_UINT32(INSTANCE_ADDR)), RPC_RSP());
121-
otInstanceFinalize(instance);
102+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_FINALIZE), RPC_RSP());
103+
otInstanceFinalize(NULL);
122104
mock_nrf_rpc_tr_expect_done();
123105
}
124106

125107
/* Test serialization of otInstanceErasePersistentInfo() returning success */
126108
ZTEST(ot_rpc_instance, test_otInstanceErasePersistentInfo_ok)
127109
{
128-
otInstance *instance = (otInstance *)INSTANCE_ADDR;
129110
otError error;
130111

131-
mock_nrf_rpc_tr_expect_add(
132-
RPC_CMD(OT_RPC_CMD_INSTANCE_ERASE_PERSISTENT_INFO, CBOR_UINT32(INSTANCE_ADDR)),
133-
RPC_RSP(OT_ERROR_NONE));
134-
error = otInstanceErasePersistentInfo(instance);
112+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_ERASE_PERSISTENT_INFO),
113+
RPC_RSP(OT_ERROR_NONE));
114+
error = otInstanceErasePersistentInfo(NULL);
135115
mock_nrf_rpc_tr_expect_done();
136116

137117
zassert_equal(error, OT_ERROR_NONE);
@@ -140,13 +120,11 @@ ZTEST(ot_rpc_instance, test_otInstanceErasePersistentInfo_ok)
140120
/* Test serialization of otInstanceErasePersistentInfo() returning error */
141121
ZTEST(ot_rpc_instance, test_otInstanceErasePersistentInfo_error)
142122
{
143-
otInstance *instance = (otInstance *)INSTANCE_ADDR;
144123
otError error;
145124

146-
mock_nrf_rpc_tr_expect_add(
147-
RPC_CMD(OT_RPC_CMD_INSTANCE_ERASE_PERSISTENT_INFO, CBOR_UINT32(INSTANCE_ADDR)),
148-
RPC_RSP(OT_ERROR_INVALID_STATE));
149-
error = otInstanceErasePersistentInfo(instance);
125+
mock_nrf_rpc_tr_expect_add(RPC_CMD(OT_RPC_CMD_INSTANCE_ERASE_PERSISTENT_INFO),
126+
RPC_RSP(OT_ERROR_INVALID_STATE));
127+
error = otInstanceErasePersistentInfo(NULL);
150128
mock_nrf_rpc_tr_expect_done();
151129

152130
zassert_equal(error, OT_ERROR_INVALID_STATE);

0 commit comments

Comments
 (0)