aboutsummaryrefslogtreecommitdiffstats
path: root/src/ConfigParser.cpp
blob: 1b6093e1ebd7e6e22264af550ca30e2acb302f89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
/*
   Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
   2011, 2012 Her Majesty the Queen in Right of Canada (Communications
   Research Center Canada)

   Copyright (C) 2016
   Matthias P. Braendli, matthias.braendli@mpb.li

    http://www.opendigitalradio.org

    The Configuration parser sets up the ensemble according
     to the configuration given in a boost property tree, which
     is directly derived from a config file.

     The format of the configuration is given in doc/example.mux
   */
/*
   This file is part of ODR-DabMux.

   ODR-DabMux is free software: you can redistribute it and/or modify
   it under the terms of the GNU General Public License as
   published by the Free Software Foundation, either version 3 of the
   License, or (at your option) any later version.

   ODR-DabMux is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with ODR-DabMux.  If not, see <http://www.gnu.org/licenses/>.
*/
#include "ConfigParser.h"

#ifdef HAVE_CONFIG_H
#   include "config.h"
#endif

#include <boost/property_tree/ptree.hpp>
#include <boost/algorithm/string/classification.hpp>
#include <boost/algorithm/string/split.hpp>
#include <memory>
#include <exception>
#include <iostream>
#include <vector>
#include <stdint.h>
#include <string>
#include <map>
#include <cstring>
#include "dabOutput/dabOutput.h"
#include "input/inputs.h"
#include "utils.h"
#include "DabMux.h"
#include "ManagementServer.h"

#include "input/Prbs.h"
#include "input/Zmq.h"
#include "input/File.h"
#include "input/Udp.h"


#ifdef _WIN32
#   pragma warning ( disable : 4103 )
#   include "Eti.h"
#   pragma warning ( default : 4103 )
#else
#   include "Eti.h"
#endif


#ifdef _WIN32
#   include <time.h>
#   include <process.h>
#   include <io.h>
#   include <conio.h>
#   include <winsock2.h> // For types...
typedef u_char uint8_t;
typedef WORD uint16_t;
typedef DWORD32 uint32_t;

#   ifndef __MINGW32__
#       include "xgetopt.h"
#   endif
#   define read _read
#   define snprintf _snprintf 
#   define sleep(a) Sleep((a) * 1000)
#else
#   include <unistd.h>
#   include <sys/time.h>
#   include <sys/wait.h>
#   include <sys/ioctl.h>
#   include <sys/times.h>
#endif

using namespace std;

static void setup_subchannel_from_ptree(DabSubchannel* subchan,
        const boost::property_tree::ptree &pt,
        std::shared_ptr<dabEnsemble> ensemble,
        const string& subchanuid);

static uint16_t get_announcement_flag_from_ptree(
        boost::property_tree::ptree& pt
        )
{
    uint16_t flags = 0;
    for (size_t flag = 0; flag < 16; flag++) {
        std::string announcement_name(annoucement_flags_names[flag]);
        bool flag_set = pt.get<bool>(announcement_name, false);

        if (flag_set) {
            flags |= (1 << flag);
        }
    }

    return flags;
}

// Parse the linkage section
static void parse_linkage(boost::property_tree::ptree& pt,
        std::shared_ptr<dabEnsemble> ensemble)
{
    using boost::property_tree::ptree;
    using boost::property_tree::ptree_error;

    auto pt_linking = pt.get_child_optional("linking");
    if (pt_linking)
    {
        for (const auto& it : *pt_linking) {
            const string setuid = it.first;
            const ptree pt_set = it.second;

            uint16_t lsn = hexparse(pt_set.get("lsn", "0"));
            if (lsn == 0) {
                etiLog.level(error) << "LSN for linking set " <<
                    setuid << " invalid or missing";
                throw runtime_error("Invalid service linking definition");
            }

            bool active = pt_set.get("active", false);
            bool hard = pt_set.get("hard", true);
            bool international = pt_set.get("international", false);

            string service_uid = pt_set.get("keyservice", "");
            if (service_uid.empty()) {
                etiLog.level(error) << "Key Service for linking set " <<
                    setuid << " invalid or missing";
                throw runtime_error("Invalid service linking definition");
            }

            auto linkageset = make_shared<LinkageSet>(setuid, lsn, hard, international);
            linkageset->data.active = active;
            linkageset->data.keyservice = service_uid; // TODO check if it exists

            auto pt_list = pt_set.get_child_optional("list");
            if (not pt_list) {
                etiLog.level(error) << "list missing in linking set " <<
                    setuid;
                throw runtime_error("Invalid service linking definition");
            }

            for (const auto& it : *pt_list) {
                const string linkuid = it.first;
                const ptree pt_link = it.second;

                ServiceLink link;

                string link_type = pt_link.get("type", "");
                if (link_type == "dab") link.type = ServiceLinkType::DAB;
                else if (link_type == "fm") link.type = ServiceLinkType::FM;
                else if (link_type == "drm") link.type = ServiceLinkType::DRM;
                else if (link_type == "amss") link.type = ServiceLinkType::AMSS;
                else {
                    etiLog.level(error) << "Invalid type " << link_type <<
                        " for link " << linkuid;
                    throw runtime_error("Invalid service linking definition");
                }

                link.id = hexparse(pt_link.get("id", "0"));
                if (link.id == 0) {
                    etiLog.level(error) << "id for link " <<
                        linkuid << " invalid or missing";
                    throw runtime_error("Invalid service linking definition");
                }

                if (international) {
                    link.ecc = hexparse(pt_link.get("ecc", "0"));
                    if (link.ecc == 0) {
                        etiLog.level(error) << "ecc for link " <<
                            linkuid << " invalid or missing";
                        throw runtime_error("Invalid service linking definition");
                    }
                }
                else {
                    link.ecc = 0;
                }

                linkageset->data.id_list.push_back(link);
            }
            ensemble->linkagesets.push_back(linkageset);
        }
    }
}

void parse_ptree(
        boost::property_tree::ptree& pt,
        std::shared_ptr<dabEnsemble> ensemble)
{
    using boost::property_tree::ptree;
    using boost::property_tree::ptree_error;
    /******************** READ GENERAL OPTIONS *****************/
    ptree pt_general = pt.get_child("general");

    /* Dab mode logic */
    ensemble->mode = pt_general.get("dabmode", 2);
    if ((ensemble->mode < 1) || (ensemble->mode > 4)) {
        throw runtime_error("Mode must be between 1-4");
    }
    if (ensemble->mode == 4) {
        ensemble->mode = 0;
    }

    /* Enable Logging to syslog conditionally */
    if (pt_general.get<bool>("syslog", false)) {
        etiLog.register_backend(new LogToSyslog()); // TODO don't leak the LogToSyslog backend
    }


    /******************** READ ENSEMBLE PARAMETERS *************/
    ptree pt_ensemble = pt.get_child("ensemble");

    /* Ensemble ID */
    ensemble->id = hexparse(pt_ensemble.get("id", "0"));

    /* Extended Country Code */
    ensemble->ecc = hexparse(pt_ensemble.get("ecc", "0"));

    ensemble->international_table = pt_ensemble.get("international-table", 0);

    string lto_auto = pt_ensemble.get("local-time-offset", "");
    if (lto_auto == "auto") {
        ensemble->lto_auto = true;
        ensemble->lto = 0;
    }
    else {
        double lto_hours = pt_ensemble.get("local-time-offset", 0.0);
        if (round(lto_hours * 2) != lto_hours * 2) {
            etiLog.level(error) << "Ensemble local time offset " <<
                lto_hours << "h cannot be expressed in half-hour blocks.";
            throw runtime_error("ensemble local-time-offset definition error");
        }
        if (lto_hours > 12 || lto_hours < -12) {
            etiLog.level(error) << "Ensemble local time offset " <<
                lto_hours << "h out of bounds [-12, +12].";
            throw runtime_error("ensemble local-time-offset definition error");
        }
        ensemble->lto_auto = false;
        ensemble->lto = abs(rint(lto_hours * 2));
    }

    int success = -5;
    string ensemble_label = pt_ensemble.get<string>("label");
    string ensemble_short_label(ensemble_label);
    try {
        ensemble_short_label = pt_ensemble.get<string>("shortlabel");
        success = ensemble->label.setLabel(ensemble_label, ensemble_short_label);
    }
    catch (ptree_error &e) {
        etiLog.level(warn) << "Ensemble short label undefined, "
            "truncating label " << ensemble_label;

        success = ensemble->label.setLabel(ensemble_label);
    }
    switch (success)
    {
        case 0:
            break;
        case -1:
            etiLog.level(error) << "Ensemble short label " <<
                ensemble_short_label << " is not subset of label '" <<
                ensemble_label << "'";
            throw runtime_error("ensemble label definition error");
        case -2:
            etiLog.level(error) << "Ensemble short label " <<
                ensemble_short_label << " is too long (max 8 characters)";
            throw runtime_error("ensemble label definition error");
        case -3:
            etiLog.level(error) << "Ensemble label " <<
                ensemble_label << " is too long (max 16 characters)";
            throw runtime_error("ensemble label definition error");
        default:
            etiLog.level(error) <<
                "Ensemble short label definition: program error !";
            abort();
    }

    try {
        ptree pt_announcements = pt_ensemble.get_child("announcements");
        for (auto announcement : pt_announcements) {
            string name = announcement.first;
            ptree pt_announcement = announcement.second;

            auto cl = make_shared<AnnouncementCluster>(name);
            cl->cluster_id = hexparse(pt_announcement.get<string>("cluster"));
            cl->flags = get_announcement_flag_from_ptree(
                    pt_announcement.get_child("flags"));
            cl->subchanneluid = pt_announcement.get<string>("subchannel");

            rcs.enrol(cl.get());
            ensemble->clusters.push_back(cl);
        }
    }
    catch (ptree_error& e) {
        etiLog.level(info) << "No announcements defined in ensemble";
        etiLog.level(debug) << "because " << e.what();
    }

    /******************** READ SERVICES PARAMETERS *************/

    map<string, shared_ptr<DabService> > allservices;

    /* For each service, we keep a separate SCIdS counter */
    map<shared_ptr<DabService>, int> SCIdS_per_service;

    ptree pt_services = pt.get_child("services");
    for (ptree::iterator it = pt_services.begin();
            it != pt_services.end(); ++it) {
        string serviceuid = it->first;
        ptree pt_service = it->second;

        shared_ptr<DabService> service;

        bool service_already_existing = false;

        for (auto srv : ensemble->services) {
            if (srv->uid == serviceuid) {
                service = srv;
                service_already_existing = true;
                break;
            }
        }

        if (not service_already_existing) {
            auto new_srv = make_shared<DabService>(serviceuid);
            ensemble->services.push_back(new_srv);
            service = new_srv;
        }

        try {
            /* Parse announcements */
            service->ASu = get_announcement_flag_from_ptree(
                    pt_service.get_child("announcements"));

            auto clusterlist = pt_service.get<std::string>("announcements.clusters", "");
            vector<string> clusters_s;
            boost::split(clusters_s,
                    clusterlist,
                    boost::is_any_of(","));

            for (const auto& cluster_s : clusters_s) {
                if (cluster_s == "") {
                    continue;
                }
                try {
                    service->clusters.push_back(hexparse(cluster_s));
                }
                catch (std::logic_error& e) {
                    etiLog.level(warn) << "Cannot parse '" << clusterlist <<
                        "' announcement clusters for service " << serviceuid <<
                        ": " << e.what();
                }
            }

            if (service->ASu != 0 and service->clusters.empty()) {
                etiLog.level(warn) << "Cluster list for service " << serviceuid <<
                    "is empty, but announcements are defined";
            }
        }
        catch (ptree_error& e) {
            service->ASu = 0;
            service->clusters.clear();

            etiLog.level(info) << "No announcements defined in service " <<
                serviceuid;
        }

        int success = -5;

        string servicelabel = pt_service.get<string>("label");
        string serviceshortlabel(servicelabel);
        try {
            serviceshortlabel = pt_service.get<string>("shortlabel");
            success = service->label.setLabel(servicelabel, serviceshortlabel);
        }
        catch (ptree_error &e) {
            etiLog.level(warn) << "Service short label undefined, "
                "truncating label " << servicelabel;

            success = service->label.setLabel(servicelabel);
        }
        switch (success)
        {
            case 0:
                break;
            case -1:
                etiLog.level(error) << "Service short label " <<
                    serviceshortlabel << " is not subset of label '" <<
                    servicelabel << "'";
                throw runtime_error("service label definition error");
            case -2:
                etiLog.level(error) << "Service short label " <<
                    serviceshortlabel << " is too long (max 8 characters)";
                throw runtime_error("service label definition error");
            case -3:
                etiLog.level(error) << "Service label " <<
                    servicelabel << " is too long (max 16 characters)";
                throw runtime_error("service label definition error");
            default:
                etiLog.level(error) <<
                    "Service short label definition: program error !";
                abort();
        }

        stringstream def_serviceid;
        def_serviceid << DEFAULT_SERVICE_ID + ensemble->services.size();
        service->id = hexparse(pt_service.get("id", def_serviceid.str()));
        service->pty = hexparse(pt_service.get("pty", "0"));
        service->language = hexparse(pt_service.get("language", "0"));

        // keep service in map, and check for uniqueness of the UID
        if (allservices.count(serviceuid) == 0) {
            allservices[serviceuid] = service;

            // Set the service's SCIds to zero
            SCIdS_per_service[service] = 0;
        }
        else {
            stringstream ss;
            ss << "Service with uid " << serviceuid << " not unique!";
            throw runtime_error(ss.str());
        }
    }


    /******************** READ SUBCHAN PARAMETERS **************/
    map<string, DabSubchannel*> allsubchans;

    ptree pt_subchans = pt.get_child("subchannels");
    for (ptree::iterator it = pt_subchans.begin(); it != pt_subchans.end(); ++it) {
        string subchanuid = it->first;
        auto subchan = new DabSubchannel(subchanuid);

        ensemble->subchannels.push_back(subchan);

        try {
            setup_subchannel_from_ptree(subchan, it->second, ensemble,
                    subchanuid);
        }
        catch (runtime_error &e) {
            etiLog.log(error,
                    "%s\n", e.what());
            throw;
        }


        // keep subchannels in map, and check for uniqueness of the UID
        if (allsubchans.count(subchanuid) == 0) {
            allsubchans[subchanuid] = subchan;
        }
        else {
            stringstream ss;
            ss << "Subchannel with uid " << subchanuid << " not unique!";
            throw runtime_error(ss.str());
        }
    }

    /******************** READ COMPONENT PARAMETERS ************/
    map<string, DabComponent*> allcomponents;
    ptree pt_components = pt.get_child("components");
    for (ptree::iterator it = pt_components.begin(); it != pt_components.end(); ++it) {
        string componentuid = it->first;
        ptree pt_comp = it->second;

        shared_ptr<DabService> service;
        try {
            // Those two uids serve as foreign keys to select the service+subchannel
            string service_uid = pt_comp.get<string>("service");
            if (allservices.count(service_uid) != 1) {
                stringstream ss;
                ss << "Component with uid " << componentuid << " is refers to unknown service "
                    << service_uid << " !";
                throw runtime_error(ss.str());
            }
            service = allservices[service_uid];
        }
        catch (ptree_error &e) {
            stringstream ss;
            ss << "Component with uid " << componentuid << " is missing service definition!";
            throw runtime_error(ss.str());
        }

        DabSubchannel* subchannel;
        try {
            string subchan_uid = pt_comp.get<string>("subchannel");
            if (allsubchans.count(subchan_uid) != 1) {
                stringstream ss;
                ss << "Component with uid " << componentuid << " is refers to unknown subchannel "
                    << subchan_uid << " !";
                throw runtime_error(ss.str());
            }
            subchannel = allsubchans[subchan_uid];
        }
        catch (ptree_error &e) {
            stringstream ss;
            ss << "Component with uid " << componentuid << " is missing subchannel definition!";
            throw runtime_error(ss.str());
        }

        int figType = hexparse(pt_comp.get("figtype", "-1"));
        int packet_address = hexparse(pt_comp.get("address", "-1"));
        int packet_datagroup = pt_comp.get("datagroup", false);
        uint8_t component_type = hexparse(pt_comp.get("type", "0"));

        auto component = new DabComponent(componentuid);

        component->serviceId = service->id;
        component->subchId = subchannel->id;
        component->SCIdS = SCIdS_per_service[service]++;
        component->type = component_type;

        int success = -5;
        string componentlabel = pt_comp.get("label", "");
        string componentshortlabel(componentlabel);
        try {
            componentshortlabel = pt_comp.get<string>("shortlabel");
            success = component->label.setLabel(componentlabel, componentshortlabel);
        }
        catch (ptree_error &e) {
            if (not componentlabel.empty()) {
                etiLog.level(warn) << "Component short label undefined, "
                    "truncating label " << componentlabel;
            }

            success = component->label.setLabel(componentlabel);
        }
        switch (success)
        {
            case 0:
                break;
            case -1:
                etiLog.level(error) << "Component short label " <<
                    componentshortlabel << " is not subset of label '" <<
                    componentlabel << "'";
                throw runtime_error("component label definition error");
            case -2:
                etiLog.level(error) << "Component short label " <<
                    componentshortlabel << " is too long (max 8 characters)";
                throw runtime_error("component label definition error");
            case -3:
                etiLog.level(error) << "Component label " <<
                    componentlabel << " is too long (max 16 characters)";
                throw runtime_error("component label definition error");
            default:
                etiLog.level(error) <<
                    "Component short label definition: program error !";
                abort();
        }

        if (component->SCIdS == 0 and not component->label.long_label().empty()) {
            etiLog.level(warn) << "Primary component " << component->uid <<
                " has label set. Since V2.1.1 of the specification, only secondary"
                " components are allowed to have labels.";
        }

        if (figType != -1) {
            if (figType >= (1<<12)) {
                stringstream ss;
                ss << "Component with uid " << componentuid <<
                    ": figtype '" << figType << "' is too large !";
                throw runtime_error(ss.str());
            }

            if (component->isPacketComponent(ensemble->subchannels)) {
                component->packet.appType = figType;

            }
            else {
                component->audio.uaType = figType;
            }

            if (packet_address != -1) {
                if (! component->isPacketComponent(ensemble->subchannels)) {
                    stringstream ss;
                    ss << "Component with uid " << componentuid <<
                        " is not packet, cannot have address defined !";
                    throw runtime_error(ss.str());
                }

                component->packet.address = packet_address;
            }
            if (packet_datagroup) {
                if (! component->isPacketComponent(ensemble->subchannels)) {
                    stringstream ss;
                    ss << "Component with uid " << componentuid <<
                        " is not packet, cannot have datagroup enabled !";
                    throw runtime_error(ss.str());
                }

                component->packet.datagroup = packet_datagroup;
            }

        }

        ensemble->components.push_back(component);

    }

    parse_linkage(pt, ensemble);
}

static Inputs::dab_input_zmq_config_t setup_zmq_input(
        const boost::property_tree::ptree &pt,
        const std::string& subchanuid)
{
    using boost::property_tree::ptree_error;

    Inputs::dab_input_zmq_config_t zmqconfig;

    try {
        zmqconfig.buffer_size = pt.get<int>("zmq-buffer");
    }
    catch (ptree_error &e) {
        stringstream ss;
        ss << "Subchannel " << subchanuid << ": " << "no zmq-buffer defined!";
        throw runtime_error(ss.str());
    }
    try {
        zmqconfig.prebuffering = pt.get<int>("zmq-prebuffering");
    }
    catch (ptree_error &e) {
        stringstream ss;
        ss << "Subchannel " << subchanuid << ": " << "no zmq-prebuffer defined!";
        throw runtime_error(ss.str());
    }

    zmqconfig.curve_encoder_keyfile = pt.get<string>("encoder-key","");
    zmqconfig.curve_secret_keyfile = pt.get<string>("secret-key","");
    zmqconfig.curve_public_keyfile = pt.get<string>("public-key","");

    zmqconfig.enable_encryption = pt.get<int>("encryption", 0);

    return zmqconfig;
}

static void setup_subchannel_from_ptree(DabSubchannel* subchan,
        const boost::property_tree::ptree &pt,
        std::shared_ptr<dabEnsemble> ensemble,
        const string& subchanuid)
{
    using boost::property_tree::ptree;
    using boost::property_tree::ptree_error;

    string type;
    /* Read type first */
    try {
        type = pt.get<string>("type");
    }
    catch (ptree_error &e) {
        stringstream ss;
        ss << "Subchannel with uid " << subchanuid << " has no type defined!";
        throw runtime_error(ss.str());
    }

    /* Both inputfile and inputuri are supported, and are equivalent.
     * inputuri has precedence
     */
    string inputUri = pt.get<string>("inputuri", "");

    if (inputUri == "") {
        try {
            inputUri = pt.get<string>("inputfile");
        }
        catch (ptree_error &e) {
            stringstream ss;
            ss << "Subchannel with uid " << subchanuid << " has no inputUri defined!";
            throw runtime_error(ss.str());
        }
    }

    string proto;
    size_t protopos = inputUri.find("://");
    if (protopos == string::npos) {
        proto = "file";
    }
    else {
        proto = inputUri.substr(0, protopos);
    }

    subchan->inputUri = inputUri;

    dabProtection* protection = &subchan->protection;

    const bool nonblock = pt.get("nonblock", false);
    if (nonblock) {
        etiLog.level(warn) << "The nonblock option is not supported";
    }

    if (type == "dabplus" or type == "audio") {
        subchan->type = subchannel_type_t::Audio;
        subchan->bitrate = 0;

        if (proto == "file") {
            if (type == "audio") {
                subchan->input = make_shared<Inputs::MPEGFile>();
            }
            else if (type == "dabplus") {
                subchan->input = make_shared<Inputs::RawFile>();
            }
            else {
                throw logic_error("Incomplete handling of file input");
            }
        }
        else if (proto == "tcp"  ||
                 proto == "epgm" ||
                 proto == "ipc") {

            auto zmqconfig = setup_zmq_input(pt, subchanuid);

            if (type == "audio") {
                auto inzmq = make_shared<Inputs::ZmqMPEG>(subchanuid, zmqconfig);
                rcs.enrol(inzmq.get());
                subchan->input = inzmq;
            }
            else if (type == "dabplus") {
                auto inzmq = make_shared<Inputs::ZmqAAC>(subchanuid, zmqconfig);
                rcs.enrol(inzmq.get());
                subchan->input = inzmq;
            }

            if (proto == "epgm") {
                etiLog.level(warn) << "Using untested epgm:// zeromq input";
            }
            else if (proto == "ipc") {
                etiLog.level(warn) << "Using untested ipc:// zeromq input";
            }
        }
        else {
            stringstream ss;
            ss << "Subchannel with uid " << subchanuid <<
                ": Invalid protocol for " << type << " input (" <<
                proto << ")" << endl;
            throw runtime_error(ss.str());
        }
    }
    else if (type == "data" and proto == "prbs") {
        subchan->input = make_shared<Inputs::Prbs>();
        subchan->type = subchannel_type_t::DataDmb;
        subchan->bitrate = DEFAULT_DATA_BITRATE;
    }
    else if (type == "data" or type == "dmb") {
        if (proto == "udp") {
            subchan->input = make_shared<Inputs::Udp>();
        } else if (proto == "file" or proto == "fifo") {
            subchan->input = make_shared<Inputs::RawFile>();
        } else {
            stringstream ss;
            ss << "Subchannel with uid " << subchanuid <<
                ": Invalid protocol for data input (" <<
                proto << ")" << endl;
            throw runtime_error(ss.str());
        }

        subchan->type = subchannel_type_t::DataDmb;
        subchan->bitrate = DEFAULT_DATA_BITRATE;

        if (type == "dmb") {
            /* The old dmb input took care of interleaving and Reed-Solomon encoding. This
             * code is unported.
             * See dabInputDmbFile.cpp
             */
            etiLog.level(warn) << "uid " << subchanuid << " of type Dmb uses RAW input";
        }
    }
    else if (type == "packet" or type == "enhancedpacket") {
        subchan->type = subchannel_type_t::Packet;
        subchan->bitrate = DEFAULT_PACKET_BITRATE;

        bool enhanced = (type == "enhancedpacket");
        subchan->input = make_shared<Inputs::PacketFile>(enhanced);
    }
    else {
        stringstream ss;
        ss << "Subchannel with uid " << subchanuid << " has unknown type!";
        throw runtime_error(ss.str());
    }
    subchan->startAddress = 0;

    if (type == "audio") {
        protection->form = UEP;
        protection->level = 2;
        protection->uep.tableIndex = 0;
    }
    else {
        protection->level = 2;
        protection->form = EEP;
        protection->eep.profile = EEP_A;
    }

    /* Get bitrate */
    try {
        subchan->bitrate = pt.get<int>("bitrate");
        if ((subchan->bitrate & 0x7) != 0) {
            stringstream ss;
            ss << "Subchannel with uid " << subchanuid <<
                ": Bitrate (" << subchan->bitrate << " not a multiple of 8!";
            throw runtime_error(ss.str());
        }
    }
    catch (ptree_error &e) {
        stringstream ss;
        ss << "Error, no bitrate defined for subchannel " << subchanuid;
        throw runtime_error(ss.str());
    }

    /* Get id */
    try {
        subchan->id = hexparse(pt.get<std::string>("id"));
    }
    catch (ptree_error &e) {
        for (int i = 0; i < 64; ++i) { // Find first free subchannel
            vector<DabSubchannel*>::iterator subchannel = getSubchannel(ensemble->subchannels, i);
            if (subchannel == ensemble->subchannels.end()) {
                subchannel = ensemble->subchannels.end() - 1;
                subchan->id = i;
                break;
            }
        }
    }

    /* Get optional protection profile */
    string profile = pt.get("protection-profile", "");

    if (profile == "EEP_A") {
        protection->form = EEP;
        protection->eep.profile = EEP_A;
    }
    else if (profile == "EEP_B") {
        protection->form = EEP;
        protection->eep.profile = EEP_B;
    }
    else if (profile == "UEP") {
        protection->form = UEP;
    }

    /* Get protection level */
    try {
        int level = pt.get<int>("protection");

        if (protection->form == UEP) {
            if ((level < 1) || (level > 5)) {
                stringstream ss;
                ss << "Subchannel with uid " << subchanuid <<
                    ": protection level must be between "
                    "1 to 5 inclusively (current = " << level << " )";
                throw runtime_error(ss.str());
            }
        }
        else if (protection->form == EEP) {
            if ((level < 1) || (level > 4)) {
                stringstream ss;
                ss << "Subchannel with uid " << subchanuid <<
                    ": protection level must be between "
                    "1 to 4 inclusively (current = " << level << " )";
                throw runtime_error(ss.str());
            }
        }
        protection->level = level - 1;
    }
    catch (ptree_error &e) {
        stringstream ss;
        ss << "Subchannel with uid " << subchanuid <<
            ": protection level undefined!";
        throw runtime_error(ss.str());
    }
}