1
+ #include < uxr/client/client.h>
2
+
3
+ #include < stdio.h>
4
+ #include < string.h>
5
+ #include < stdlib.h>
6
+ #include < inttypes.h>
7
+
8
+ #define STREAM_HISTORY 8
9
+ #define BUFFER_SIZE UXR_CONFIG_UDP_TRANSPORT_MTU* STREAM_HISTORY
10
+
11
+ static uxrStreamId reliable_out;
12
+ static uxrStreamId reliable_in;
13
+
14
+ static uxrObjectId participant_id;
15
+ static uxrObjectId replier_id;
16
+
17
+ bool isArmable = true ;
18
+
19
+ void on_request (
20
+ uxrSession* session,
21
+ uxrObjectId object_id,
22
+ uint16_t request_id,
23
+ SampleIdentity* sample_id,
24
+ ucdrBuffer* ub,
25
+ uint16_t length,
26
+ void * args)
27
+ {
28
+ (void ) object_id;
29
+ (void ) request_id;
30
+ (void ) length;
31
+ (void ) args;
32
+
33
+ bool arm;
34
+ ucdr_deserialize_bool (ub,&arm);
35
+
36
+ if (arm){
37
+ printf (" Request recieved for Arming \n " );
38
+ }else {
39
+ printf (" Request recieved for Disarming \n " );
40
+ }
41
+ uint8_t reply_buffer[8 ] = {
42
+ 0
43
+ };
44
+
45
+ ucdrBuffer reply_ub;
46
+
47
+ bool result = false ;
48
+ if (isArmable){
49
+ result = arm;
50
+ }else {
51
+ result = false ;
52
+ }
53
+
54
+ ucdr_init_buffer (&reply_ub, reply_buffer, sizeof (reply_buffer));
55
+ ucdr_serialize_bool (&reply_ub,result);
56
+
57
+ uxr_buffer_reply (session, reliable_out, replier_id, sample_id, reply_buffer, sizeof (reply_buffer));
58
+
59
+ if (result){
60
+ printf (" Reply : Armed \n " );
61
+ }else {
62
+ printf (" Reply : Disarmed \n " );
63
+ }
64
+
65
+ }
66
+
67
+ int main (
68
+ int args,
69
+ char ** argv)
70
+ {
71
+ if (3 > args || 0 == atoi (argv[2 ]))
72
+ {
73
+ printf (" usage: program [-h | --help] | ip port [armable]\n " );
74
+ printf (" usage: armable: 0 to set arming condition to false \n " );
75
+ printf (" usage: armable: 1 to set arming condition to true \n " );
76
+ return 0 ;
77
+ }
78
+
79
+ char * ip = argv[1 ];
80
+ char * port = argv[2 ];
81
+ isArmable = (args == 4 ) ? atoi (argv[3 ]) : 1 ;
82
+ uint32_t key = 0xCCCCDDDD ;
83
+
84
+ // Transport
85
+ uxrUDPTransport transport;
86
+ if (!uxr_init_udp_transport (&transport, UXR_IPv4, ip, port))
87
+ {
88
+ printf (" Error at init transport.\n " );
89
+ return 1 ;
90
+ }
91
+
92
+ // Session
93
+ uxrSession session;
94
+ uxr_init_session (&session, &transport.comm , key);
95
+ uxr_set_request_callback (&session, on_request, 0 );
96
+ if (!uxr_create_session (&session))
97
+ {
98
+ printf (" Error at init session.\n " );
99
+ return 1 ;
100
+ }
101
+
102
+ // Streams
103
+ uint8_t output_reliable_stream_buffer[BUFFER_SIZE];
104
+ reliable_out = uxr_create_output_reliable_stream (&session, output_reliable_stream_buffer, BUFFER_SIZE,
105
+ STREAM_HISTORY);
106
+
107
+ uint8_t input_reliable_stream_buffer[BUFFER_SIZE];
108
+ reliable_in = uxr_create_input_reliable_stream (&session, input_reliable_stream_buffer, BUFFER_SIZE, STREAM_HISTORY);
109
+
110
+ // Create entities
111
+ participant_id = uxr_object_id (0x01 , UXR_PARTICIPANT_ID);
112
+ uint16_t participant_req = uxr_buffer_create_participant_bin (&session,reliable_out,participant_id,0 ," ArmMotors_Server" ,UXR_REPLACE);
113
+ uxrQoS_t qos = {
114
+ .durability = UXR_DURABILITY_TRANSIENT_LOCAL,
115
+ .reliability = UXR_RELIABILITY_RELIABLE,
116
+ .history = UXR_HISTORY_KEEP_ALL,
117
+ .depth = 1
118
+ };
119
+
120
+ replier_id = uxr_object_id (0x01 , UXR_REPLIER_ID);
121
+ uint16_t replier_req = uxr_buffer_create_replier_bin (&session,reliable_out,replier_id,participant_id," ArmMotorsService" ," ArmMotors_Request" ," ArmMotors_Response" ," ArmMotorsService_Request" ," ArmMotorsService_Reply" ,qos,UXR_REPLACE);
122
+
123
+ // Send create entities message and wait its status
124
+ uint8_t status[2 ];
125
+ uint16_t requests[2 ] = {
126
+ participant_req, replier_req
127
+ };
128
+ if (!uxr_run_session_until_all_status (&session, 1000 , requests, status, 2 ))
129
+ {
130
+ printf (" Error at create entities: participant: %i requester: %i\n " , status[0 ], status[1 ]);
131
+ return 1 ;
132
+ }
133
+
134
+ printf (" Entities Created Successfully \n " );
135
+
136
+ // Request requests
137
+ uxrDeliveryControl delivery_control = {
138
+ 0
139
+ };
140
+ delivery_control.max_samples = UXR_MAX_SAMPLES_UNLIMITED;
141
+ uint16_t read_data_req =
142
+ uxr_buffer_request_data (&session, reliable_out, replier_id, reliable_in, &delivery_control);
143
+ (void ) read_data_req;
144
+
145
+ // Read request
146
+ bool connected = true ;
147
+ while (connected)
148
+ {
149
+ connected = uxr_run_session_time (&session, 1000 );
150
+ }
151
+
152
+ return 0 ;
153
+ }
0 commit comments