Debian Codegraph @

examples/dds/fgfdm_log.c

raw symbols SCIP
1 #include <stdio.h>
2 #include <string.h>
3 #include <stdlib.h>
4
5 #include <dds/dds.h>
6
7 #include <Network/DDS/dds_gui.h>
8 #include <Network/DDS/dds_fdm.h>
9 #include <Network/DDS/dds_ctrls.h>
10
11
12 /* An array of one message(aka sample in dds terms) will be used. */
13 #define MAX_SAMPLES 1
14
15 #ifndef _WIN32
16 # include <termios.h>
17
18 void set_mode(int want_key)
19 {
20 static struct termios old, new;
21 if (!want_key) {
22 tcsetattr(STDIN_FILENO, TCSANOW, &old);
23 return;
24 }
25
26 tcgetattr(STDIN_FILENO, &old);
27 new = old;
28 new.c_lflag &= ~(ICANON | ECHO);
29 tcsetattr(STDIN_FILENO, TCSANOW, &new);
30 }
31
32 int get_key()
33 {
34 int c = 0;
35 struct timeval tv;
36 fd_set fs;
37 tv.tv_usec = tv.tv_sec = 0;
38
39 FD_ZERO(&fs);
40 FD_SET(STDIN_FILENO, &fs);
41 select(STDIN_FILENO + 1, &fs, 0, 0, &tv);
42
43 if (FD_ISSET(STDIN_FILENO, &fs)) {
44 c = getchar();
45 }
46 return c;
47 }
48 #else
49 # include <conio.h>
50
51 int get_key()
52 {
53 if (kbhit()) {
54 return getch();
55 }
56 return 0;
57 }
58
59 void set_mode(int want_key)
60 {
61 }
62 #endif
63
64 int main()
65 {
66 dds_entity_t participant;
67 dds_entity_t gui_topic;
68 dds_entity_t fdm_topic;
69 dds_entity_t ctrls_topic;
70 dds_entity_t gui_reader;
71 dds_entity_t fdm_reader;
72 dds_entity_t ctrls_reader;
73 dds_entity_t rdcond;
74 void *samples[MAX_SAMPLES];
75 dds_sample_info_t infos[MAX_SAMPLES];
76 dds_return_t rc;
77 dds_qos_t *qos;
78 int status;
79
80 /* Initialize sample buffer, by pointing the void pointer within
81 * the buffer array to a valid sample memory location. */
82 samples[0] = FG_DDS_FDM__alloc ();
83
84 /* Create a Participant. */
85 participant = dds_create_participant(DDS_DOMAIN_DEFAULT, NULL, NULL);
86 if(participant < 0)
87 DDS_FATAL("dds_create_participant: %s\n", dds_strretcode(-participant));
88
89 /* Create a wait set */
90 dds_entity_t waitset = dds_create_waitset(participant);
91
92 qos = dds_create_qos();
93 dds_qset_reliability(qos, DDS_RELIABILITY_RELIABLE, DDS_SECS(10));
94
95 /* Create a GUI Topic. */
96 gui_topic = dds_create_topic(
97 participant, &FG_DDS_GUI_desc, "FG_DDS_GUI", NULL, NULL);
98 if(gui_topic < 0)
99 DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-gui_topic));
100
101 gui_reader = dds_create_reader(participant, gui_topic, qos, NULL);
102 if(gui_reader < 0)
103 DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-gui_reader));
104
105 rdcond = dds_create_readcondition(gui_reader, DDS_NOT_READ_SAMPLE_STATE);
106 status = dds_waitset_attach(waitset, rdcond, gui_reader);
107 if (status < 0)
108 DDS_FATAL("ds_waitset_attach: %s\n", dds_strretcode(-status));
109
110 /* Create an FDM Topic. */
111 fdm_topic = dds_create_topic(
112 participant, &FG_DDS_FDM_desc, "FG_DDS_FDM", NULL, NULL);
113 if(fdm_topic < 0)
114 DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-fdm_topic));
115
116 fdm_reader = dds_create_reader(participant, fdm_topic, qos, NULL);
117 if(fdm_reader < 0)
118 DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-fdm_reader));
119
120 rdcond = dds_create_readcondition(fdm_reader, DDS_NOT_READ_SAMPLE_STATE);
121 status = dds_waitset_attach(waitset, rdcond, fdm_reader);
122 if (status < 0)
123 DDS_FATAL("ds_waitset_attach: %s\n", dds_strretcode(-status));
124
125 /* Create a Ctrls Topic. */
126 ctrls_topic = dds_create_topic(
127 participant, &FG_DDS_Ctrls_desc, "FG_DDS_Ctrls", NULL, NULL);
128 if(ctrls_topic < 0)
129 DDS_FATAL("dds_create_topic: %s\n", dds_strretcode(-ctrls_topic));
130
131 ctrls_reader = dds_create_reader(participant, ctrls_topic, qos, NULL);
132 if(ctrls_reader < 0)
133 DDS_FATAL("dds_create_reader: %s\n", dds_strretcode(-ctrls_reader));
134
135 rdcond = dds_create_readcondition(ctrls_reader, DDS_NOT_READ_SAMPLE_STATE);
136 status = dds_waitset_attach(waitset, rdcond, ctrls_reader);
137 if (status < 0)
138 DDS_FATAL("ds_waitset_attach: %s\n", dds_strretcode(-status));
139
140 dds_delete_qos(qos);
141
142 printf("\n=== [fgfdm_log] Waiting for a sample ...\n");
143 fflush(stdout);
144
145 /* Wait for a new packet. */
146 set_mode(1);
147
148 size_t num = 1;
149 dds_attach_t results[num];
150 dds_duration_t timeout = DDS_MSECS(500);
151 while(dds_waitset_wait(waitset, results, num, timeout) >= 0)
152 {
153 /* Do the actual read.
154 * The return value contains the number of read samples. */
155 rc = dds_take(gui_reader, samples, infos, 1, 1);
156 if(rc < 0)
157 DDS_FATAL("dds_read: (gui) %s\n", dds_strretcode(-rc));
158
159 if((rc > 0) &&(infos[0].valid_data))
160 {
161 FG_DDS_GUI *gui = (FG_DDS_GUI*) samples[0];
162 printf("=== [fgfdm_log] Received : ");
163 printf("GUI Message:\n");
164 printf(" version: %i\n", gui->version);
165 printf(" tuned_freq: %lf\n", gui->tuned_freq);
166 printf(" nav_radial: %lf\n", gui->nav_radial);
167 printf(" dist_nm: %lf\n", gui->dist_nm);
168 }
169
170 rc = dds_take(fdm_reader, samples, infos, 1, 1);
171 if(rc < 0)
172 DDS_FATAL("dds_read: (fdm) %s\n", dds_strretcode(-rc));
173
174 /* Check if we read some data and it is valid. */
175 if((rc > 0) &&(infos[0].valid_data))
176 {
177 /* Print Message. */
178 FG_DDS_FDM *fdm = (FG_DDS_FDM*) samples[0];
179 printf("=== [fgfdm_log] Received : ");
180 printf("FDM Message:\n");
181 printf(" version: %i\n", fdm->version);
182 printf(" longitude: %lf\n", fdm->longitude);
183 printf(" latitude: %lf\n", fdm->latitude);
184 printf(" altitude: %lf\n", fdm->altitude);
185 }
186
187 rc = dds_take(ctrls_reader, samples, infos, 1, 1);
188 if(rc < 0)
189 DDS_FATAL("dds_read: (ctrls) %s\n", dds_strretcode(-rc));
190
191 /* Check if we read some data and it is valid. */
192 if((rc > 0) &&(infos[0].valid_data))
193 {
194 /* Print Message. */
195 FG_DDS_Ctrls *ctrls = (FG_DDS_Ctrls*) samples[0];
196 printf("=== [fgfdm_log] Received : ");
197 printf("Ctrls Message:\n");
198 printf(" version: %i\n", ctrls->version);
199 printf(" aileron: %lf\n", ctrls->aileron);
200 printf(" elevator: %lf\n", ctrls->elevator);
201 printf(" rudder: %lf\n", ctrls->rudder);
202 }
203
204 fflush(stdout);
205
206 if (get_key()) break;
207 }
208 set_mode(0);
209
210 /* Free the data location. */
211 FG_DDS_FDM_free (samples[0], DDS_FREE_ALL);
212
213 /* Deleting the participant will delete all its children recursively as well. */
214 rc = dds_delete(participant);
215 if(rc != DDS_RETCODE_OK)
216 DDS_FATAL("dds_delete: %s\n", dds_strretcode(-rc));
217
218 return EXIT_SUCCESS;
219 }