@@ -24,33 +24,32 @@ OT_RPC_RESOURCE_TABLE_REGISTER(msg, otMessage, CONFIG_OPENTHREAD_RPC_MESSAGE_POO
24
24
static void ot_rpc_msg_free (const struct nrf_rpc_group * group , struct nrf_rpc_cbor_ctx * ctx ,
25
25
void * handler_data )
26
26
{
27
- ot_rpc_res_tab_key key = 0 ;
27
+ ot_rpc_res_tab_key key ;
28
28
otMessage * message ;
29
29
30
30
key = nrf_rpc_decode_uint (ctx );
31
+
31
32
if (!nrf_rpc_decoding_done_and_check (group , ctx )) {
32
33
ot_rpc_report_cmd_decoding_error (OT_RPC_CMD_MESSAGE_FREE );
33
34
return ;
34
35
}
35
36
37
+ ot_rpc_mutex_lock ();
36
38
message = ot_res_tab_msg_get (key );
37
39
38
40
if (message != NULL ) {
39
- ot_rpc_mutex_lock ();
40
41
otMessageFree (message );
41
- ot_rpc_mutex_unlock ( );
42
+ ot_res_tab_msg_free ( key );
42
43
}
43
44
44
- ot_res_tab_msg_free (key );
45
-
45
+ ot_rpc_mutex_unlock ();
46
46
nrf_rpc_rsp_send_void (group );
47
47
}
48
48
49
49
static void ot_rpc_msg_append (const struct nrf_rpc_group * group , struct nrf_rpc_cbor_ctx * ctx ,
50
50
void * handler_data )
51
51
{
52
52
otError error = OT_ERROR_INVALID_ARGS ;
53
- struct nrf_rpc_cbor_ctx rsp_ctx ;
54
53
uint32_t key ;
55
54
const void * data ;
56
55
size_t size = 0 ;
@@ -59,31 +58,29 @@ static void ot_rpc_msg_append(const struct nrf_rpc_group *group, struct nrf_rpc_
59
58
key = nrf_rpc_decode_uint (ctx );
60
59
data = nrf_rpc_decode_buffer_ptr_and_size (ctx , & size );
61
60
62
- if (data && size && nrf_rpc_decode_valid (ctx )) {
61
+ if (data ) {
62
+ ot_rpc_mutex_lock ();
63
63
message = ot_res_tab_msg_get (key );
64
64
65
65
if (message != NULL ) {
66
- ot_rpc_mutex_lock ();
67
66
error = otMessageAppend (message , data , size );
68
- ot_rpc_mutex_unlock ();
69
67
}
68
+
69
+ ot_rpc_mutex_unlock ();
70
70
}
71
71
72
72
if (!nrf_rpc_decoding_done_and_check (group , ctx )) {
73
73
ot_rpc_report_cmd_decoding_error (OT_RPC_CMD_MESSAGE_APPEND );
74
74
return ;
75
75
}
76
76
77
- NRF_RPC_CBOR_ALLOC (group , rsp_ctx , sizeof (otError ) + 1 );
78
- nrf_rpc_encode_uint (& rsp_ctx , error );
79
- nrf_rpc_cbor_rsp_no_err (group , & rsp_ctx );
77
+ nrf_rpc_rsp_send_uint (group , error );
80
78
}
81
79
82
80
static void ot_rpc_msg_udp_new (const struct nrf_rpc_group * group , struct nrf_rpc_cbor_ctx * ctx ,
83
81
void * handler_data )
84
82
{
85
83
ot_rpc_res_tab_key key ;
86
- struct nrf_rpc_cbor_ctx rsp_ctx ;
87
84
otMessageSettings settings ;
88
85
otMessageSettings * p_settings = & settings ;
89
86
@@ -96,122 +93,114 @@ static void ot_rpc_msg_udp_new(const struct nrf_rpc_group *group, struct nrf_rpc
96
93
97
94
ot_rpc_mutex_lock ();
98
95
key = ot_res_tab_msg_alloc (otUdpNewMessage (openthread_get_default_instance (), p_settings ));
99
- ot_rpc_mutex_unlock ();
100
96
101
97
if (ot_res_tab_msg_get (key ) == NULL ) {
102
98
key = 0 ;
103
99
}
104
100
105
- NRF_RPC_CBOR_ALLOC (group , rsp_ctx , sizeof (key ) + 1 );
106
- nrf_rpc_encode_uint (& rsp_ctx , key );
107
- nrf_rpc_cbor_rsp_no_err (group , & rsp_ctx );
101
+ ot_rpc_mutex_unlock ();
102
+ nrf_rpc_rsp_send_uint (group , key );
108
103
}
109
104
110
105
static void ot_rpc_msg_length (const struct nrf_rpc_group * group , struct nrf_rpc_cbor_ctx * ctx ,
111
106
void * handler_data )
112
107
{
113
108
ot_rpc_res_tab_key key ;
114
- struct nrf_rpc_cbor_ctx rsp_ctx ;
115
109
uint16_t length = 0 ;
116
110
otMessage * message ;
117
111
118
112
key = nrf_rpc_decode_uint (ctx );
113
+
119
114
if (!nrf_rpc_decoding_done_and_check (group , ctx )) {
120
115
ot_rpc_report_cmd_decoding_error (OT_RPC_CMD_MESSAGE_GET_LENGTH );
121
116
return ;
122
117
}
123
118
119
+ ot_rpc_mutex_lock ();
124
120
message = ot_res_tab_msg_get (key );
125
121
126
122
if (message != NULL ) {
127
- ot_rpc_mutex_lock ();
128
123
length = otMessageGetLength (message );
129
- ot_rpc_mutex_unlock ();
130
124
}
131
125
132
- NRF_RPC_CBOR_ALLOC (group , rsp_ctx , sizeof (length ) + 1 );
133
- nrf_rpc_encode_uint (& rsp_ctx , length );
134
- nrf_rpc_cbor_rsp_no_err (group , & rsp_ctx );
126
+ ot_rpc_mutex_unlock ();
127
+ nrf_rpc_rsp_send_uint (group , length );
135
128
}
136
129
137
130
static void ot_rpc_get_offset (const struct nrf_rpc_group * group , struct nrf_rpc_cbor_ctx * ctx ,
138
131
void * handler_data )
139
132
{
140
133
ot_rpc_res_tab_key key ;
141
- struct nrf_rpc_cbor_ctx rsp_ctx ;
142
134
uint16_t offset = 0 ;
143
135
otMessage * message ;
144
136
145
137
key = nrf_rpc_decode_uint (ctx );
138
+
146
139
if (!nrf_rpc_decoding_done_and_check (group , ctx )) {
147
140
ot_rpc_report_cmd_decoding_error (OT_RPC_CMD_MESSAGE_GET_OFFSET );
148
141
return ;
149
142
}
150
143
144
+ ot_rpc_mutex_lock ();
151
145
message = ot_res_tab_msg_get (key );
152
146
153
147
if (message != NULL ) {
154
- ot_rpc_mutex_lock ();
155
148
offset = otMessageGetOffset (message );
156
- ot_rpc_mutex_unlock ();
157
149
}
158
150
159
- NRF_RPC_CBOR_ALLOC (group , rsp_ctx , sizeof (offset ) + 1 );
160
- nrf_rpc_encode_uint (& rsp_ctx , offset );
161
- nrf_rpc_cbor_rsp_no_err (group , & rsp_ctx );
151
+ ot_rpc_mutex_unlock ();
152
+ nrf_rpc_rsp_send_uint (group , offset );
162
153
}
163
154
164
155
static void ot_rpc_msg_read (const struct nrf_rpc_group * group , struct nrf_rpc_cbor_ctx * ctx ,
165
156
void * handler_data )
166
157
{
158
+ ot_rpc_res_tab_key key ;
167
159
uint16_t offset ;
168
160
uint16_t length ;
169
- ot_rpc_res_tab_key key ;
161
+ otMessage * message ;
170
162
struct nrf_rpc_cbor_ctx rsp_ctx ;
171
- const uint16_t chunk_size = 64 ;
172
- uint8_t buf [chunk_size ];
163
+ uint16_t message_length ;
173
164
uint16_t read = 0 ;
174
- otMessage * message ;
175
165
176
166
key = nrf_rpc_decode_uint (ctx );
177
167
offset = nrf_rpc_decode_uint (ctx );
178
168
length = nrf_rpc_decode_uint (ctx );
169
+
179
170
if (!nrf_rpc_decoding_done_and_check (group , ctx )) {
180
171
ot_rpc_report_cmd_decoding_error (OT_RPC_CMD_MESSAGE_READ );
181
172
return ;
182
173
}
183
174
184
- NRF_RPC_CBOR_ALLOC (group , rsp_ctx , length + 2 );
185
-
175
+ ot_rpc_mutex_lock ();
186
176
message = ot_res_tab_msg_get (key );
187
177
188
178
if (message == NULL ) {
179
+ NRF_RPC_CBOR_ALLOC (group , rsp_ctx , 1 );
189
180
nrf_rpc_encode_null (& rsp_ctx );
190
181
goto exit ;
191
182
}
192
183
184
+ /* Get the actual message size before reading to allocate as small buffer as necessary. */
185
+ message_length = otMessageGetLength (message );
186
+ offset = MIN (offset , message_length );
187
+ length = MIN (length , message_length - offset );
188
+
189
+ NRF_RPC_CBOR_ALLOC (group , rsp_ctx , length + 3 );
190
+
193
191
if (!zcbor_bstr_start_encode (rsp_ctx .zs )) {
194
192
goto exit ;
195
193
}
196
194
197
- ot_rpc_mutex_lock ();
198
-
199
- do {
200
- read = otMessageRead (message , offset , buf ,
201
- (chunk_size < length ) ? chunk_size : length );
202
- memcpy (rsp_ctx .zs [0 ].payload_mut , buf , read );
203
- rsp_ctx .zs -> payload_mut += read ;
204
- length -= read ;
205
- offset += read ;
206
- } while (read > 0 && length > 0 );
207
-
208
- ot_rpc_mutex_unlock ();
195
+ read = otMessageRead (message , offset , rsp_ctx .zs -> payload_mut , length );
196
+ rsp_ctx .zs -> payload_mut += read ;
209
197
210
198
if (!zcbor_bstr_end_encode (rsp_ctx .zs , NULL )) {
211
199
goto exit ;
212
200
}
213
201
214
202
exit :
203
+ ot_rpc_mutex_unlock ();
215
204
nrf_rpc_cbor_rsp_no_err (group , & rsp_ctx );
216
205
}
217
206
@@ -232,14 +221,15 @@ static void ot_rpc_msg_get_thread_link_info(const struct nrf_rpc_group *group,
232
221
return ;
233
222
}
234
223
224
+ ot_rpc_mutex_lock ();
235
225
message = ot_res_tab_msg_get (key );
236
226
237
227
if (!message ) {
228
+ ot_rpc_mutex_unlock ();
238
229
ot_rpc_report_cmd_decoding_error (OT_RPC_CMD_MESSAGE_GET_THREAD_LINK_INFO );
239
230
return ;
240
231
}
241
232
242
- ot_rpc_mutex_lock ();
243
233
error = otMessageGetThreadLinkInfo (message , & link_info );
244
234
ot_rpc_mutex_unlock ();
245
235
0 commit comments