2828static z_owned_session_t s_wrapper ;
2929/* Private function prototypes -----------------------------------------------*/
3030/* Private functions ---------------------------------------------------------*/
31- static void rmw_zenoh_gen_attachment_gid (rmw_attachment_t * attachment ){
31+ static void rmw_zenoh_gen_attachment_gid (rmw_attachment_t * attachment ) {
3232 attachment -> rmw_gid_size = RMW_GID_SIZE ;
33- for (int i = 0 ; i < RMW_GID_SIZE ; i ++ ){
33+ for (int i = 0 ; i < RMW_GID_SIZE ; i ++ ) {
3434 attachment -> rmw_gid [i ] = z_random_u8 ();
3535 }
3636}
3737
38- static int rmw_zenoh_node_liveliness_keyexpr (picoros_node_t * node , char * keyexpr ){
38+ static int rmw_zenoh_node_liveliness_keyexpr (picoros_node_t * node , char * keyexpr ) {
3939#if USE_NODE_GUID == 1
4040 uint8_t * guid = node -> guid ;
4141#endif
@@ -62,12 +62,14 @@ static int rmw_zenoh_node_liveliness_keyexpr(picoros_node_t* node, char *keyexpr
6262 );
6363}
6464
65- static int rmw_zenoh_topic_keyexpr (picoros_node_t * node , rmw_topic_t * topic , char * keyexpr ){
66- return snprintf (keyexpr , KEYEXPR_SIZE , "%" PRIu32 "/%s/%s_/RIHS01_%s" , node -> domain_id , topic -> name , topic -> type , topic -> rihs_hash );
65+ static int rmw_zenoh_topic_keyexpr (picoros_node_t * node , rmw_topic_t * topic , char * keyexpr ) {
66+ return snprintf (keyexpr , KEYEXPR_SIZE , "%" PRIu32 "/%s/%s_/RIHS01_%s" , node -> domain_id , topic -> name , topic -> type ,
67+ topic -> rihs_hash );
6768}
6869
69- static int rmw_zenoh_service_keyexpr (picoros_node_t * node , rmw_topic_t * topic , char * keyexpr ){
70- return snprintf (keyexpr , KEYEXPR_SIZE , "%" PRIu32 "/%s/%s/%s_/RIHS01_%s" , node -> domain_id , node -> name , topic -> name , topic -> type , topic -> rihs_hash );
70+ static int rmw_zenoh_service_keyexpr (picoros_node_t * node , rmw_topic_t * topic , char * keyexpr ) {
71+ return snprintf (keyexpr , KEYEXPR_SIZE , "%" PRIu32 "/%s/%s/%s_/RIHS01_%s" , node -> domain_id , node -> name , topic -> name ,
72+ topic -> type , topic -> rihs_hash );
7173}
7274
7375static int rmw_zenoh_topic_liveliness_keyexpr (picoros_node_t * node , rmw_topic_t * topic , char * keyexpr , const char * entity_str ) {
@@ -154,41 +156,38 @@ static void srv_data_handler(z_loaned_query_t *query, void *arg) {
154156 _z_bytes_to_buf (b , rx_data , rx_data_len );
155157
156158 // process
157- picoros_service_reply_t reply =
158- srv -> user_callback (rx_data , rx_data_len , srv -> user_data );
159+ picoros_service_reply_t reply = srv -> user_callback (rx_data , rx_data_len , srv -> user_data );
159160
160- if (reply .data ){
161+ if (reply .data ) {
161162 // move reply to zbytes
162163 z_owned_bytes_t reply_payload ;
163164 z_bytes_copy_from_buf (& reply_payload , reply .data , reply .length );
164165
165166 // rmw attachment
166- srv -> attachment .sequence_number = 1 ;
167+ srv -> attachment .sequence_number = 1 ;
167168 srv -> attachment .time = z_clock_now ().tv_nsec ;
168169 z_query_reply_options_t options ;
169170 z_query_reply_options_default (& options );
170171 z_owned_bytes_t tx_attachment ;
171- z_bytes_from_static_buf (& tx_attachment , (uint8_t * )& srv -> attachment , sizeof (rmw_attachment_t ));
172+ z_bytes_from_static_buf (& tx_attachment , (uint8_t * )& srv -> attachment , sizeof (rmw_attachment_t ));
172173 options .attachment = z_bytes_move (& tx_attachment );
173174
174175 // send reply
175176 z_result_t res = z_query_reply (query , z_query_keyexpr (query ), z_bytes_move (& reply_payload ), & options );
176- if (res != Z_OK ){
177+ if (res != Z_OK ) {
177178 _PR_LOG ("Error sending service reply. Error:%d\n" , res );
178179 }
179180
180181 // cleanup
181182 z_bytes_drop (z_bytes_move (& reply_payload ));
182- if (reply .free_callback != NULL ){
183+ if (reply .free_callback != NULL ) {
183184 reply .free_callback (reply .data );
184185 }
185186 }
186187 z_free (rx_data );
187188}
188189
189- static void srv_drop_handler (void * arg ) {
190- _PR_LOG ("Drop srv callback\n" );
191- }
190+ static void srv_drop_handler (void * arg ) { _PR_LOG ("Drop srv callback\n" ); }
192191
193192/* Public functions ----------------------------------------------------------*/
194193
@@ -216,18 +215,18 @@ picoros_res_t picoros_interface_init(picoros_interface_t* ifx) {
216215 _PR_LOG ("Zenoh setup finished!\r\n" );
217216
218217 // Start read and lease tasks for zenoh-pico
219- if ((res = zp_start_read_task (z_session_loan_mut (& s_wrapper ), NULL )) != Z_OK
218+ if ((res = zp_start_read_task (z_session_loan_mut (& s_wrapper ), NULL )) != Z_OK
220219 || (res = zp_start_lease_task (z_session_loan_mut (& s_wrapper ), NULL )) != Z_OK
221220 ){
222221 z_session_drop (z_session_move (& s_wrapper ));
223222 _PR_LOG ("Failed to start read/lease tasks! Error:%d\n" , res );
224223 return PICOROS_ERROR ;
225224 }
226225
227- return PICOROS_OK ;
226+ return PICOROS_OK ;
228227}
229228
230- picoros_res_t picoros_node_init (picoros_node_t * node ){
229+ picoros_res_t picoros_node_init (picoros_node_t * node ) {
231230 z_result_t res = Z_OK ;
232231 char keyexpr [KEYEXPR_SIZE ];
233232
@@ -239,149 +238,146 @@ picoros_res_t picoros_node_init(picoros_node_t* node){
239238 z_owned_liveliness_token_t token ;
240239
241240 if ((res = z_liveliness_declare_token (z_session_loan (& s_wrapper ), & token , z_view_keyexpr_loan (& ke ), NULL )) != Z_OK ) {
242- _PR_LOG ("Unable to declare node liveliness token! Error:%d\n" , res );
241+ _PR_LOG ("Unable to declare node liveliness token! Error:%d\n" , res );
243242 return PICOROS_ERROR ;
244243 }
245- return PICOROS_OK ;
244+ return PICOROS_OK ;
246245}
247246
248247void zenoh_shutdown () {
249248 z_session_drop (z_session_move (& s_wrapper ));
250249}
251250
252- picoros_res_t picoros_publisher_declare (picoros_node_t * node , picoros_publisher_t * pub ) {
251+ picoros_res_t picoros_publisher_declare (picoros_node_t * node , picoros_publisher_t * pub ) {
253252 z_view_keyexpr_t ke ;
254253 z_result_t res = Z_OK ;
255254 char keyexpr [KEYEXPR_SIZE ];
256- if (pub -> topic .type != NULL ){
255+ if (pub -> topic .type != NULL ) {
257256 rmw_zenoh_topic_keyexpr (node , & pub -> topic , keyexpr );
258257 z_view_keyexpr_from_str_unchecked (& ke , keyexpr );
259258 }
260- else {
259+ else {
261260 z_view_keyexpr_from_str_unchecked (& ke , pub -> topic .name );
262261 }
262+
263263 rmw_zenoh_gen_attachment_gid (& pub -> attachment );
264264
265- if ((res = z_declare_publisher (z_session_loan (& s_wrapper ), & pub -> zpub , z_view_keyexpr_loan (& ke ), NULL )) != Z_OK ){
265+ if ((res = z_declare_publisher (z_session_loan (& s_wrapper ), & pub -> zpub , z_view_keyexpr_loan (& ke ), NULL )) != Z_OK ) {
266266 _PR_LOG ("Unable to declare node liveliness token! Error:%d\n" , res );
267267 return PICOROS_ERROR ;
268268 }
269269
270- if (pub -> topic .type != NULL ){
270+ if (pub -> topic .type != NULL ) {
271271 z_view_keyexpr_t ke2 ;
272272 rmw_zenoh_topic_liveliness_keyexpr (node , & pub -> topic , keyexpr , "MP" );
273273 z_view_keyexpr_from_str (& ke2 , keyexpr );
274274
275275 z_owned_liveliness_token_t token ;
276276 if ((res = z_liveliness_declare_token (z_session_loan (& s_wrapper ), & token , z_view_keyexpr_loan (& ke2 ), NULL )) != Z_OK ) {
277- _PR_LOG ("Unable to declare publisher liveliness token! Error:%d\n" , res );
277+ _PR_LOG ("Unable to declare publisher liveliness token! Error:%d\n" , res );
278278 return PICOROS_ERROR ;
279279 }
280280 }
281281 return PICOROS_OK ;
282282}
283283
284-
285-
286284// Publish to a topic
287- picoros_res_t picoros_publish (picoros_publisher_t * pub , uint8_t * payload , size_t len ) {
285+ picoros_res_t picoros_publish (picoros_publisher_t * pub , uint8_t * payload , size_t len ) {
288286 z_result_t res = Z_OK ;
289287 z_publisher_put_options_t options ;
290288 z_publisher_put_options_default (& options );
291289
292- // int64_t time_ns = tx_time_get()*1000000000ULL/TX_TIMER_TICKS_PER_SECOND;
293290 pub -> attachment .sequence_number ++ ;
294291 pub -> attachment .time = z_clock_now ().tv_nsec ;
295292
296293 z_owned_bytes_t z_attachment ;
297- z_bytes_from_static_buf (& z_attachment , (uint8_t * )& pub -> attachment , sizeof (rmw_attachment_t ));
294+ z_bytes_from_static_buf (& z_attachment , (uint8_t * )& pub -> attachment , sizeof (rmw_attachment_t ));
298295
299296 options .attachment = z_bytes_move (& z_attachment );
300297
301298 z_owned_bytes_t zbytes ;
302299 z_bytes_from_static_buf (& zbytes , payload , len );
303300
304- if ((res = z_publisher_put (z_publisher_loan (& pub -> zpub ), z_bytes_move (& zbytes ), & options )) != Z_OK ){
301+ if ((res = z_publisher_put (z_publisher_loan (& pub -> zpub ), z_bytes_move (& zbytes ), & options )) != Z_OK ) {
305302 _PR_LOG ("Unable to publish payload! Error:%d\n" , res );
306303 return PICOROS_ERROR ;
307304 }
308305 return PICOROS_OK ;
309306}
310307
311308// Subscribe to a topic
312- picoros_res_t picoros_subscriber_declare (picoros_node_t * node , picoros_subscriber_t * sub ) {
313- char keyexpr [KEYEXPR_SIZE ];
314- z_view_keyexpr_t ke ;
309+ picoros_res_t picoros_subscriber_declare (picoros_node_t * node , picoros_subscriber_t * sub ) {
310+ char keyexpr [KEYEXPR_SIZE ];
311+ z_view_keyexpr_t ke ;
315312 z_result_t res = Z_OK ;
316- if (sub -> topic .type != NULL ){
313+ if (sub -> topic .type != NULL ) {
317314 rmw_zenoh_topic_keyexpr (node , & sub -> topic , keyexpr );
318315 z_view_keyexpr_from_str_unchecked (& ke , keyexpr );
319- }
320- else {
316+ }
317+ else {
321318 z_view_keyexpr_from_str_unchecked (& ke , sub -> topic .name );
322- }
319+ }
323320
324- z_owned_closure_sample_t callback ;
321+ z_owned_closure_sample_t callback ;
325322 z_closure_sample (& callback , sub_data_handler , NULL , NULL );
326- callback ._val .context = sub -> user_callback ;
323+ callback ._val .context = sub -> user_callback ;
327324
328- if ((res = z_declare_subscriber (z_session_loan (& s_wrapper ), & sub -> zsub , z_view_keyexpr_loan (& ke ), z_closure_sample_move (& callback ), NULL )) != Z_OK ) {
325+ if ((res = z_declare_subscriber (z_session_loan (& s_wrapper ), & sub -> zsub , z_view_keyexpr_loan (& ke ),
326+ z_closure_sample_move (& callback ), NULL )) != Z_OK ) {
329327 _PR_LOG ("Unable to declare subscriber! Error:%d\n" , res );
330328 return PICOROS_ERROR ;
331- }
329+ }
332330
333- if (sub -> topic .type != NULL ){
331+ if (sub -> topic .type != NULL ) {
334332 rmw_zenoh_topic_liveliness_keyexpr (node , & sub -> topic , keyexpr , "MS" );
335333 z_view_keyexpr_from_str (& ke , keyexpr );
336334 z_owned_liveliness_token_t token ;
337335 if ((res = z_liveliness_declare_token (z_session_loan (& s_wrapper ), & token , z_view_keyexpr_loan (& ke ), NULL )) != Z_OK ) {
338336 _PR_LOG ("Unable to declare subscriber liveliness token! Error:%d\n" , res );
339337 return PICOROS_ERROR ;
340338 }
341- }
339+ }
342340 return PICOROS_OK ;
343-
344341}
345342
346- picoros_res_t picoros_service_declare (picoros_node_t * node , picoros_service_t * srv ){
347- z_result_t res ;
348- char keyexpr [KEYEXPR_SIZE ];
343+ picoros_res_t picoros_service_declare (picoros_node_t * node , picoros_service_t * srv ) {
344+ z_result_t res ;
345+ char keyexpr [KEYEXPR_SIZE ];
349346
350- z_view_keyexpr_t ke ;
351- if (srv -> topic .type != NULL ){
352- rmw_zenoh_service_keyexpr (node , & srv -> topic , keyexpr );
353- z_view_keyexpr_from_str_unchecked (& ke , keyexpr );
354- }
355- else {
356- z_view_keyexpr_from_str_unchecked (& ke , srv -> topic .name );
357- }
347+ z_view_keyexpr_t ke ;
348+ if (srv -> topic .type != NULL ) {
349+ rmw_zenoh_service_keyexpr (node , & srv -> topic , keyexpr );
350+ z_view_keyexpr_from_str_unchecked (& ke , keyexpr );
351+ }
352+ else {
353+ z_view_keyexpr_from_str_unchecked (& ke , srv -> topic .name );
354+ }
358355
359- rmw_zenoh_gen_attachment_gid (& srv -> attachment );
356+ rmw_zenoh_gen_attachment_gid (& srv -> attachment );
360357
361- z_queryable_options_t options = {};
362- options .complete = true; // needed for rmw_zenoh
358+ z_queryable_options_t options = {};
359+ options .complete = true; // needed for rmw_zenoh
363360
364- z_owned_closure_query_t callback ;
365- z_closure_query (& callback , srv_data_handler , srv_drop_handler , srv );
366- if ((res = z_declare_queryable (z_session_loan (& s_wrapper ), & srv -> zqable , z_view_keyexpr_loan (& ke ), z_closure_query_move (& callback ), & options )) != Z_OK ){
361+ z_owned_closure_query_t callback ;
362+ z_closure_query (& callback , srv_data_handler , srv_drop_handler , srv );
363+ if ((res = z_declare_queryable (z_session_loan (& s_wrapper ), & srv -> zqable , z_view_keyexpr_loan (& ke ),
364+ z_closure_query_move (& callback ), & options )) != Z_OK ) {
367365 _PR_LOG ("Unable to declare service! Error:%d\n" , res );
368366 return PICOROS_ERROR ;
369- }
370- if (srv -> topic .type != NULL ){
371- z_view_keyexpr_t ke2 ;
372- z_owned_liveliness_token_t token ;
373- rmw_zenoh_topic_liveliness_keyexpr (node , & srv -> topic , keyexpr , "SS" );
374- z_view_keyexpr_from_str (& ke2 , keyexpr );
375- if ((res = z_liveliness_declare_token (z_session_loan (& s_wrapper ), & token , z_view_keyexpr_loan (& ke2 ), NULL )) != Z_OK ) {
376- _PR_LOG ("Unable to declare service liveliness token! Error:%d\n" , res );
377- return PICOROS_ERROR ;
378- }
379- }
380- return PICOROS_OK ;
367+ }
368+ if (srv -> topic .type != NULL ) {
369+ z_view_keyexpr_t ke2 ;
370+ z_owned_liveliness_token_t token ;
371+ rmw_zenoh_topic_liveliness_keyexpr (node , & srv -> topic , keyexpr , "SS" );
372+ z_view_keyexpr_from_str (& ke2 , keyexpr );
373+ if ((res = z_liveliness_declare_token (z_session_loan (& s_wrapper ), & token , z_view_keyexpr_loan (& ke2 ), NULL )) != Z_OK ) {
374+ _PR_LOG ("Unable to declare service liveliness token! Error:%d\n" , res );
375+ return PICOROS_ERROR ;
376+ }
377+ }
378+ return PICOROS_OK ;
381379}
382380
383-
384-
385- picoros_res_t picoros_unsubscribe (picoros_subscriber_t * sub ) {
386- return (z_undeclare_subscriber (z_subscriber_move (& sub -> zsub )) == Z_OK ) ? PICOROS_OK : PICOROS_ERROR ;
381+ picoros_res_t picoros_unsubscribe (picoros_subscriber_t * sub ) {
382+ return (z_undeclare_subscriber (z_subscriber_move (& sub -> zsub )) == Z_OK ) ? PICOROS_OK : PICOROS_ERROR ;
387383}
0 commit comments