YARP
Yet Another Robot Platform
 
Loading...
Searching...
No Matches
yarpAudioPlayer_IDL.cpp
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2006-2021 Istituto Italiano di Tecnologia (IIT)
3 * SPDX-License-Identifier: BSD-3-Clause
4 */
5
6// Autogenerated by Thrift Compiler (0.14.1-yarped)
7//
8// This is an automatically generated file.
9// It could get re-generated if the ALLOW_IDL_GENERATION flag is on.
10
11#include <yarp/conf/version.h>
12#include <yarpAudioPlayer_IDL.h>
14#include <yarp/os/LogStream.h>
15
17
18#include <algorithm>
19
20namespace
21{
22 YARP_LOG_COMPONENT(SERVICE_LOG_COMPONENT, "yarpAudioPlayer_IDL")
23}
24
25//yarpAudioPlayer_IDL_getRemoteProtocolVersion_helper declaration
36
38{
39 yarp::os::idl::WireWriter writer(connection);
40 if (!writer.writeListHeader(1)) {
41 return false;
42 }
43 if (!writer.writeString("getRemoteProtocolVersion")) {
44 return false;
45 }
46 return true;
47}
48
49bool yarpAudioPlayer_IDL_getRemoteProtocolVersion_helper ::read(yarp::os::ConnectionReader & connection)
50 {
51 yarp::os::idl::WireReader reader(connection);
52 if (!reader.readListHeader()) {
53 reader.fail();
54 return false;
55 }
56
57 if (!helper_proto.read(connection)) {
58 reader.fail();
59 return false;
60 }
61 return true;
62}
63
64//ProtocolVersion, client side
66 {
67 if(!yarp().canWrite()) {
68 yError(" Missing server method yarpAudioPlayer_IDL::getRemoteProtocolVersion");
69 }
71 bool ok = yarp().write(helper, helper);
72 if (ok) {
73 return helper.helper_proto;}
74 else {
76 return failureproto;}
77}
78
79//ProtocolVersion, client side
81 {
82 auto locproto = this->getLocalProtocolVersion();
83 auto remproto = this->getRemoteProtocolVersion();
84 if (remproto.protocol_version != locproto.protocol_version)
85 {
86 yCError(SERVICE_LOG_COMPONENT) << "Invalid communication protocol.";
87 yCError(SERVICE_LOG_COMPONENT) << "Local Protocol Version: " << locproto.toString();
88 yCError(SERVICE_LOG_COMPONENT) << "Remote Protocol Version: " << remproto.toString();
89 return false;
90 }
91 return true;
92}
93
94//ProtocolVersion, server side
96{
98 //myproto.protocol_version using default value = 0
99 //to change this value add the following line to the .thrift file:
100 //const i16 protocol_version = <your_number_here>
101 myproto.protocol_version = 0;
105 return myproto;
106}
107
108// play helper class declaration
110 public yarp::os::Portable
111{
112public:
114 explicit yarpAudioPlayer_IDL_play_helper(const std::string& filename);
115 bool write(yarp::os::ConnectionWriter& connection) const override;
116 bool read(yarp::os::ConnectionReader& connection) override;
117
118 class Command :
120 {
121 public:
122 Command() = default;
123 explicit Command(const std::string& filename);
124
125 ~Command() override = default;
126
127 bool write(yarp::os::ConnectionWriter& connection) const override;
128 bool read(yarp::os::ConnectionReader& connection) override;
129
130 bool write(const yarp::os::idl::WireWriter& writer) const override;
131 bool writeTag(const yarp::os::idl::WireWriter& writer) const;
132 bool writeArgs(const yarp::os::idl::WireWriter& writer) const;
133
134 bool read(yarp::os::idl::WireReader& reader) override;
137
138 std::string filename{};
139 };
140
141 class Reply :
143 {
144 public:
145 Reply() = default;
146 ~Reply() override = default;
147
148 bool write(yarp::os::ConnectionWriter& connection) const override;
149 bool read(yarp::os::ConnectionReader& connection) override;
150
151 bool write(const yarp::os::idl::WireWriter& writer) const override;
152 bool read(yarp::os::idl::WireReader& reader) override;
153
154 bool return_helper{false};
155 };
156
157 using funcptr_t = bool (*)(const std::string&);
158 void call(yarpAudioPlayer_IDL* ptr);
159
162
163 static constexpr const char* s_tag{"play"};
164 static constexpr size_t s_tag_len{1};
165 static constexpr size_t s_cmd_len{2};
166 static constexpr size_t s_reply_len{1};
167 static constexpr const char* s_prototype{"bool yarpAudioPlayer_IDL::play(const std::string& filename)"};
168 static constexpr const char* s_help{
169 "Play the given audio file\n"
170 "@return true/false on success/failure"
171 };
172};
173
174// test helper class declaration
176 public yarp::os::Portable
177{
178public:
180 yarpAudioPlayer_IDL_test_helper(const std::string& type, const double duration, const double frequency, const std::int16_t amplitude, const std::int16_t sampling_rate, const std::int16_t channels);
181 bool write(yarp::os::ConnectionWriter& connection) const override;
182 bool read(yarp::os::ConnectionReader& connection) override;
183
184 class Command :
186 {
187 public:
188 Command() = default;
189 Command(const std::string& type, const double duration, const double frequency, const std::int16_t amplitude, const std::int16_t sampling_rate, const std::int16_t channels);
190
191 ~Command() override = default;
192
193 bool write(yarp::os::ConnectionWriter& connection) const override;
194 bool read(yarp::os::ConnectionReader& connection) override;
195
196 bool write(const yarp::os::idl::WireWriter& writer) const override;
197 bool writeTag(const yarp::os::idl::WireWriter& writer) const;
198 bool writeArgs(const yarp::os::idl::WireWriter& writer) const;
199
200 bool read(yarp::os::idl::WireReader& reader) override;
203
204 std::string type{"sine"};
205 double duration{0.000000};
206 double frequency{0.000000};
207 std::int16_t amplitude{30000};
208 std::int16_t sampling_rate{16000};
209 std::int16_t channels{1};
210 };
211
212 class Reply :
214 {
215 public:
216 Reply() = default;
217 ~Reply() override = default;
218
219 bool write(yarp::os::ConnectionWriter& connection) const override;
220 bool read(yarp::os::ConnectionReader& connection) override;
221
222 bool write(const yarp::os::idl::WireWriter& writer) const override;
223 bool read(yarp::os::idl::WireReader& reader) override;
224
225 bool return_helper{false};
226 };
227
228 using funcptr_t = bool (*)(const std::string&, const double, const double, const std::int16_t, const std::int16_t, const std::int16_t);
229 void call(yarpAudioPlayer_IDL* ptr);
230
233
234 static constexpr const char* s_tag{"test"};
235 static constexpr size_t s_tag_len{1};
236 static constexpr size_t s_cmd_len{7};
237 static constexpr size_t s_reply_len{1};
238 static constexpr const char* s_prototype{"bool yarpAudioPlayer_IDL::test(const std::string& type, const double duration, const double frequency, const std::int16_t amplitude, const std::int16_t sampling_rate, const std::int16_t channels)"};
239 static constexpr const char* s_help{
240 "Generate and send a test sound\n"
241 "@param type can be \"sine\" , \"sawtooth\" , \"squarewave\"\n"
242 "@param duration the duration of the sound in seconds\n"
243 "@param frequency the signal in hertz\n"
244 "@param amplitude the amplitude of the signal (16 bit)\n"
245 "@param channels number of channels\n"
246 "@param sampling_rate signal sampling rate (samples/s)\n"
247 "@return true/false on success/failure"
248 };
249};
250
251// play helper class implementation
253 cmd{filename}
254{
255}
256
258{
259 return cmd.write(connection);
260}
261
263{
264 return reply.read(connection);
265}
266
268 filename{filename}
269{
270}
271
273{
274 yarp::os::idl::WireWriter writer(connection);
275 if (!writer.writeListHeader(s_cmd_len)) {
276 return false;
277 }
278 return write(writer);
279}
280
282{
283 yarp::os::idl::WireReader reader(connection);
284 if (!reader.readListHeader()) {
285 reader.fail();
286 return false;
287 }
288 return read(reader);
289}
290
292{
293 if (!writeTag(writer)) {
294 return false;
295 }
296 if (!writeArgs(writer)) {
297 return false;
298 }
299 return true;
300}
301
303{
304 if (!writer.writeTag(s_tag, 1, s_tag_len)) {
305 return false;
306 }
307 return true;
308}
309
311{
312 if (!writer.writeString(filename)) {
313 return false;
314 }
315 return true;
316}
317
319{
320 if (!readTag(reader)) {
321 return false;
322 }
323 if (!readArgs(reader)) {
324 return false;
325 }
326 return true;
327}
328
330{
331 std::string tag = reader.readTag(s_tag_len);
332 if (reader.isError()) {
333 return false;
334 }
335 if (tag != s_tag) {
336 reader.fail();
337 return false;
338 }
339 return true;
340}
341
343{
344 if (reader.noMore()) {
345 reader.fail();
346 return false;
347 }
348 if (!reader.readString(filename)) {
349 reader.fail();
350 return false;
351 }
352 if (!reader.noMore()) {
353 reader.fail();
354 return false;
355 }
356 return true;
357}
358
360{
361 yarp::os::idl::WireWriter writer(connection);
362 return write(writer);
363}
364
366{
367 yarp::os::idl::WireReader reader(connection);
368 return read(reader);
369}
370
372{
373 if (!writer.isNull()) {
374 if (!writer.writeListHeader(s_reply_len)) {
375 return false;
376 }
377 if (!writer.writeBool(return_helper)) {
378 return false;
379 }
380 }
381 return true;
382}
383
385{
386 if (!reader.readListReturn()) {
387 return false;
388 }
389 if (reader.noMore()) {
390 reader.fail();
391 return false;
392 }
393 if (!reader.readBool(return_helper)) {
394 reader.fail();
395 return false;
396 }
397 return true;
398}
399
404
405// test helper class implementation
406yarpAudioPlayer_IDL_test_helper::yarpAudioPlayer_IDL_test_helper(const std::string& type, const double duration, const double frequency, const std::int16_t amplitude, const std::int16_t sampling_rate, const std::int16_t channels) :
407 cmd{type, duration, frequency, amplitude, sampling_rate, channels}
408{
409}
410
412{
413 return cmd.write(connection);
414}
415
417{
418 return reply.read(connection);
419}
420
421yarpAudioPlayer_IDL_test_helper::Command::Command(const std::string& type, const double duration, const double frequency, const std::int16_t amplitude, const std::int16_t sampling_rate, const std::int16_t channels) :
422 type{type},
423 duration{duration},
424 frequency{frequency},
425 amplitude{amplitude},
426 sampling_rate{sampling_rate},
427 channels{channels}
428{
429}
430
432{
433 yarp::os::idl::WireWriter writer(connection);
434 if (!writer.writeListHeader(s_cmd_len)) {
435 return false;
436 }
437 return write(writer);
438}
439
441{
442 yarp::os::idl::WireReader reader(connection);
443 if (!reader.readListHeader()) {
444 reader.fail();
445 return false;
446 }
447 return read(reader);
448}
449
451{
452 if (!writeTag(writer)) {
453 return false;
454 }
455 if (!writeArgs(writer)) {
456 return false;
457 }
458 return true;
459}
460
462{
463 if (!writer.writeTag(s_tag, 1, s_tag_len)) {
464 return false;
465 }
466 return true;
467}
468
470{
471 if (!writer.writeString(type)) {
472 return false;
473 }
474 if (!writer.writeFloat64(duration)) {
475 return false;
476 }
477 if (!writer.writeFloat64(frequency)) {
478 return false;
479 }
480 if (!writer.writeI16(amplitude)) {
481 return false;
482 }
483 if (!writer.writeI16(sampling_rate)) {
484 return false;
485 }
486 if (!writer.writeI16(channels)) {
487 return false;
488 }
489 return true;
490}
491
493{
494 if (!readTag(reader)) {
495 return false;
496 }
497 if (!readArgs(reader)) {
498 return false;
499 }
500 return true;
501}
502
504{
505 std::string tag = reader.readTag(s_tag_len);
506 if (reader.isError()) {
507 return false;
508 }
509 if (tag != s_tag) {
510 reader.fail();
511 return false;
512 }
513 return true;
514}
515
517{
518 if (!reader.readString(type)) {
519 type = "sine";
520 }
521 if (!reader.readFloat64(duration)) {
522 duration = 1;
523 }
524 if (!reader.readFloat64(frequency)) {
525 frequency = 440;
526 }
527 if (!reader.readI16(amplitude)) {
528 amplitude = 30000;
529 }
530 if (!reader.readI16(sampling_rate)) {
531 sampling_rate = 16000;
532 }
533 if (!reader.readI16(channels)) {
534 channels = 1;
535 }
536 if (!reader.noMore()) {
537 reader.fail();
538 return false;
539 }
540 return true;
541}
542
544{
545 yarp::os::idl::WireWriter writer(connection);
546 return write(writer);
547}
548
550{
551 yarp::os::idl::WireReader reader(connection);
552 return read(reader);
553}
554
556{
557 if (!writer.isNull()) {
558 if (!writer.writeListHeader(s_reply_len)) {
559 return false;
560 }
561 if (!writer.writeBool(return_helper)) {
562 return false;
563 }
564 }
565 return true;
566}
567
569{
570 if (!reader.readListReturn()) {
571 return false;
572 }
573 if (reader.noMore()) {
574 reader.fail();
575 return false;
576 }
577 if (!reader.readBool(return_helper)) {
578 reader.fail();
579 return false;
580 }
581 return true;
582}
583
588
589// Constructor
591{
592 yarp().setOwner(*this);
593}
594
595bool yarpAudioPlayer_IDL::play(const std::string& filename)
596{
597 if (!yarp().canWrite()) {
598 yError("Missing server method '%s'?", yarpAudioPlayer_IDL_play_helper::s_prototype);
599 }
600 yarpAudioPlayer_IDL_play_helper helper{filename};
601 bool ok = yarp().write(helper, helper);
602 return ok ? helper.reply.return_helper : bool{};
603}
604
605bool yarpAudioPlayer_IDL::test(const std::string& type, const double duration, const double frequency, const std::int16_t amplitude, const std::int16_t sampling_rate, const std::int16_t channels)
606{
607 if (!yarp().canWrite()) {
608 yError("Missing server method '%s'?", yarpAudioPlayer_IDL_test_helper::s_prototype);
609 }
610 yarpAudioPlayer_IDL_test_helper helper{type, duration, frequency, amplitude, sampling_rate, channels};
611 bool ok = yarp().write(helper, helper);
612 return ok ? helper.reply.return_helper : bool{};
613}
614
615// help method
616std::vector<std::string> yarpAudioPlayer_IDL::help(const std::string& functionName)
617{
618 bool showAll = (functionName == "--all");
619 std::vector<std::string> helpString;
620 if (showAll) {
621 helpString.emplace_back("*** Available commands:");
622 helpString.emplace_back(yarpAudioPlayer_IDL_play_helper::s_tag);
623 helpString.emplace_back(yarpAudioPlayer_IDL_test_helper::s_tag);
624 helpString.emplace_back("help");
625 } else {
626 if (functionName == yarpAudioPlayer_IDL_play_helper::s_tag) {
627 helpString.emplace_back(yarpAudioPlayer_IDL_play_helper::s_prototype);
628 helpString.emplace_back(yarpAudioPlayer_IDL_play_helper::s_help);
629 }
630 if (functionName == yarpAudioPlayer_IDL_test_helper::s_tag) {
631 helpString.emplace_back(yarpAudioPlayer_IDL_test_helper::s_prototype);
632 helpString.emplace_back(yarpAudioPlayer_IDL_test_helper::s_help);
633 }
634 if (functionName == "help") {
635 helpString.emplace_back("std::vector<std::string> help(const std::string& functionName = \"--all\")");
636 helpString.emplace_back("Return list of available commands, or help message for a specific function");
637 helpString.emplace_back("@param functionName name of command for which to get a detailed description. If none or '--all' is provided, print list of available commands");
638 helpString.emplace_back("@return list of strings (one string per line)");
639 }
640 }
641 if (helpString.empty()) {
642 helpString.emplace_back("Command not found");
643 }
644 return helpString;
645}
646
647// read from ConnectionReader
649{
650 constexpr size_t max_tag_len = 1;
651 size_t tag_len = 1;
652
653 yarp::os::idl::WireReader reader(connection);
654 reader.expectAccept();
655 if (!reader.readListHeader()) {
656 reader.fail();
657 return false;
658 }
659
660 std::string tag = reader.readTag(1);
661 bool direct = (tag == "__direct__");
662 if (direct) {
663 tag = reader.readTag(1);
664 }
665 while (tag_len <= max_tag_len && !reader.isError()) {
666 if(tag == "getRemoteProtocolVersion") {
667 if (!reader.noMore()) {
668 yError("Reader invalid protocol?! %s:%d - %s", __FILE__, __LINE__, __YFUNCTION__);
669 reader.fail();
670 return false;
671 }
672
673 auto proto = getLocalProtocolVersion();
674
675 yarp::os::idl::WireWriter writer(reader);
676 if (!writer.writeListHeader(1)) {
677 yWarning("Writer invalid protocol?! %s:%d - %s", __FILE__, __LINE__, __YFUNCTION__);
678 return false;}
679 if (!writer.write(proto)) {
680 yWarning("Writer invalid protocol?! %s:%d - %s", __FILE__, __LINE__, __YFUNCTION__);
681 return false;
682 }
683 reader.accept();
684 return true;
685 }
688 if (!helper.cmd.readArgs(reader)) {
689 return false;
690 }
691
692 helper.call(this);
693
694 yarp::os::idl::WireWriter writer(reader);
695 if (!helper.reply.write(writer)) {
696 return false;
697 }
698 reader.accept();
699 return true;
700 }
703 if (!helper.cmd.readArgs(reader)) {
704 return false;
705 }
706
707 helper.call(this);
708
709 yarp::os::idl::WireWriter writer(reader);
710 if (!helper.reply.write(writer)) {
711 return false;
712 }
713 reader.accept();
714 return true;
715 }
716 if (tag == "help") {
717 std::string functionName;
718 if (!reader.readString(functionName)) {
719 functionName = "--all";
720 }
721 auto help_strings = help(functionName);
722 yarp::os::idl::WireWriter writer(reader);
723 if (!writer.isNull()) {
724 if (!writer.writeListHeader(2)) {
725 return false;
726 }
727 if (!writer.writeTag("many", 1, 0)) {
728 return false;
729 }
730 if (!writer.writeListBegin(0, help_strings.size())) {
731 return false;
732 }
733 for (const auto& help_string : help_strings) {
734 if (!writer.writeString(help_string)) {
735 return false;
736 }
737 }
738 if (!writer.writeListEnd()) {
739 return false;
740 }
741 }
742 reader.accept();
743 return true;
744 }
745 if (reader.noMore()) {
746 reader.fail();
747 return false;
748 }
749 std::string next_tag = reader.readTag(1);
750 if (next_tag.empty()) {
751 break;
752 }
753 tag.append("_").append(next_tag);
754 tag_len = std::count(tag.begin(), tag.end(), '_') + 1;
755 }
756 return false;
757}
#define yError(...)
Definition Log.h:361
#define __YFUNCTION__
Definition Log.h:24
#define yWarning(...)
Definition Log.h:340
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
yarp::os::ApplicationNetworkProtocolVersion helper_proto
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
bool readArgs(yarp::os::idl::WireReader &reader)
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
bool writeTag(const yarp::os::idl::WireWriter &writer) const
bool writeArgs(const yarp::os::idl::WireWriter &writer) const
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool readTag(yarp::os::idl::WireReader &reader)
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
static constexpr const char * s_help
void call(yarpAudioPlayer_IDL *ptr)
static constexpr const char * s_prototype
static constexpr const char * s_tag
bool(*)(const std::string &) funcptr_t
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
bool writeArgs(const yarp::os::idl::WireWriter &writer) const
bool readTag(yarp::os::idl::WireReader &reader)
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool readArgs(yarp::os::idl::WireReader &reader)
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
bool writeTag(const yarp::os::idl::WireWriter &writer) const
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
static constexpr const char * s_help
bool(*)(const std::string &, const double, const double, const std::int16_t, const std::int16_t, const std::int16_t) funcptr_t
bool write(yarp::os::ConnectionWriter &connection) const override
Write this object to a network connection.
static constexpr const char * s_tag
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
static constexpr const char * s_prototype
void call(yarpAudioPlayer_IDL *ptr)
yarpAudioPlayer_IDL Interface.
virtual std::vector< std::string > help(const std::string &functionName="--all")
virtual yarp::os::ApplicationNetworkProtocolVersion getLocalProtocolVersion()
virtual bool test(const std::string &type="sine", const double duration=1, const double frequency=440, const std::int16_t amplitude=30000, const std::int16_t sampling_rate=16000, const std::int16_t channels=1)
Generate and send a test sound.
virtual bool checkProtocolVersion()
virtual bool play(const std::string &filename)
Play the given audio file.
virtual yarp::os::ApplicationNetworkProtocolVersion getRemoteProtocolVersion()
bool read(yarp::os::ConnectionReader &connection) override
Read this object from a network connection.
An interface for reading from a network connection.
An interface for writing to a network connection.
This is a base class for objects that can be both read from and be written to the YARP network.
Definition Portable.h:25
yarp::os::WireLink & yarp()
Get YARP state associated with this object.
Definition Wire.h:28
A "tamed" Portable, that promises to serialize itself in an IDL-friendly way.
IDL-friendly connection reader.
Definition WireReader.h:27
bool readString(std::string &str, bool *is_vocab=nullptr)
bool readI16(std::int16_t &x)
std::string readTag(size_t len=static_cast< size_t >(-1))
bool readFloat64(yarp::conf::float64_t &x)
IDL-friendly connection writer.
Definition WireWriter.h:28
bool writeBool(bool x, bool skip_tag=false) const
bool write(const WirePortable &obj) const
bool writeI16(std::int16_t x, bool skip_tag=false) const
bool writeListHeader(int len) const
bool writeTag(const char *tag, int split, int len) const
bool writeFloat64(yarp::conf::float64_t x, bool skip_tag=false) const
bool writeString(const std::string &str, bool skip_tag=false) const
bool writeListBegin(int tag, size_t len) const
#define yCError(component,...)
#define YARP_LOG_COMPONENT(name,...)
The main, catch-all namespace for YARP.
Definition dirs.h:16
#define YARP_VERSION_PATCH
Definition version.h:12
#define YARP_VERSION_MAJOR
Definition version.h:10
#define YARP_VERSION_MINOR
Definition version.h:11