-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy path__init__.py
More file actions
1288 lines (1070 loc) · 49.3 KB
/
__init__.py
File metadata and controls
1288 lines (1070 loc) · 49.3 KB
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
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#===============================================================================
# Python Hybrid Radio SPI - API to support ETSI TS 102 818
#
# Copyright (C) 2010 Global Radio
# Copyright (C) 2015 Ben Poor
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#===============================================================================
from spi import *
from bitarray import bitarray, bits2bytes
import math
import datetime, dateutil.tz
import logging
import sys
from datetime import timedelta
logger = logging.getLogger("spi.binary")
logger.setLevel(10)
class Ensemble:
"""
Describes a DAB ensemble
:param ecc: Extended Country Code (ECC)
:type ecc: integer
:param eid: Ensemble ID (EId)
:type eid: integer
:param version: Description version
:type version: integer
"""
def __init__(self, ecc, eid, version=1):
self.ecc = ecc
self.eid = eid
self.names = []
self.descriptions = []
self.media = []
self.keywords = []
self.links = []
self.services = []
self.frequencies = []
self.version = version
def __str__(self):
return "%02x.%04x" % (self.ecc, self.eid)
def __repr__(self):
return '<Ensemble: %s>' % str(self)
class Element:
def __init__(self, tag, attributes=None, children=None, cdata=None):
self.tag = tag
self.attributes = (attributes if attributes is not None else [])
self.children = (children if children is not None else [])
self.cdata = cdata
logger.debug('created new element: %s', self)
def tobytes(self):
logger.debug('rendering element %s', repr(self))
data = bitarray()
# encode attributes
for attribute in self.attributes:
logger.debug('rendering attribute: %s', attribute)
try: data += attribute.tobytes()
except Exception, e:
raise ValueError, 'error rendering attribute %s of %s: %s' % (attribute, self, str(e)), sys.exc_info()[2]
# encode children
for child in self.children:
logger.debug('rendering child element: %s', child)
try: data += child.tobytes()
except:
logger.exception('error rendering child %s of %s', child, self)
raise
# encode CData
if self.cdata is not None:
# print 'rendering cdata: %s' % self.cdata
logger.debug('rendering cdata: %s', self.cdata)
data += self.cdata.tobytes()
# b0-b7: element tag
bits = encode_number(self.tag, 8)
# b8-15: element data length (0-253 bytes)
# b16-31: extended element length (256-65536 bytes)
# b16-39: extended element length (65537-16777216 bytes)
datalength = len(data)/8
if datalength == 0:
raise ValueError('element data length is zero')
if datalength <= 253:
bits += encode_number(datalength, 8)
elif datalength >= 254 and datalength <= 1<<16:
bits += encode_number(0xfe, 8)
bits += encode_number(datalength, 16)
elif datalength > 1<<16 and datalength <= 1<<24:
bits += encode_number(0xff, 8)
bits += encode_number(datalength, 24)
else: raise ValueError('element data length exceeds the maximum allowed by the extended element length (24bits): %s > %s' + datalength + " > " + (1<<24))
logger.debug('element %s is rendered with data length: %d bytes: %s', repr(self), datalength, bitarray_to_hex(bits))
bits += data
return bits
def __iter__(self):
return iter(self.children)
def has_child(self, tag):
return len(self.get_children(tag))
def get_children(self, tag=None):
if tag is not None:
return [x for x in self.children if x.tag == tag]
return self.children
def has_attribute(self, tag):
return len(self.get_attributes(tag))
def get_attributes(self, tag=None):
if tag is not None:
return [x for x in self.attributes if x.tag == tag]
return self.attributes
@staticmethod
def frombits(bits):
# b0-b7: element tag
tag = int(bits[0:8].to01(), 2)
if tag < 0x02 or tag > 0x36: raise ValueError('invalid value for tag: 0x%02x' % tag)
# b8-15: element data length (0-253 bytes)
# b16-31: extended element length (256-65536 bytes)
# b16-39: extended element length (65537-16777216 bytes)
datalength = int(bits[8:16].to01(), 2)
start = 16
if datalength == 0xfe:
datalength = int(bits[start:start+16].to01(), 2)
start += 16
elif datalength == 0xff:
datalength = int(bits[start:start+24].to01(), 2)
start += 24
data = bits[start : start + (datalength * 8)]
i = 0
e = Element(tag)
logger.debug('parsing data of length %d bytes for element with tag 0x%02x', datalength, tag)
while i < data.length():
child_tag = int(data[i:i+8].to01(), 2)
child_datalength = int(data[i+8:i+16].to01(), 2)
start = 16
if child_datalength == 0xfe:
child_datalength = int(data[i+16:i+32].to01(), 2)
start = 32
elif child_datalength == 0xff:
child_datalength = int(data[i+16:i+40].to01(), 2)
start = 40
logger.debug('child tag 0x%02x for parent 0x%02x at offset %d has data length of %d bytes', child_tag, tag, i/8, child_datalength)
end = start + (child_datalength * 8)
if i + end > data.length():
raise ValueError('end of data for tag 0x%02x at offset %d requested is beyond length: %d > %d: %s' % (child_tag, i/8, (i + end)/8, data.length() / 8, bitarray_to_hex(data[i:i+64])))
child_data = data[i + start : i + end]
if child_data.length() < 16*8: logger.debug('child tag 0x%02x for parent 0x%02x has data: %s', child_tag, tag, bitarray_to_hex(child_data))
# attributes
if child_tag >= 0x80 and child_tag <= 0x87:
attribute = Attribute.frombits(tag, data[i:i+end])
e.attributes.append(attribute)
# token table
elif child_tag == 0x04:
tokens = decode_tokentable(child_data)
e.tokens = tokens
logger.debug('parsed token table: %s', tokens)
# default content ID
elif child_tag == 0x05:
default_contentid = decode_contentid(child_data)
e.default_contentid = default_contentid
# default language
elif child_tag == 0x06:
pass
# children
elif child_tag >= 0x02 and child_tag <= 0x36:
child = Element.frombits(data[i:i+end])
child.parent = e
e.children.append(child)
# cdata
elif child_tag == 0x01:
cdata = CData.frombits(data[i:i+end])
e.cdata = cdata
else:
raise ValueError('unknown element 0x%02x under parent 0x%02x' % (child_tag, tag))
i += end
return e
def __str__(self):
return 'tag=0x%02X, attributes=%s, children=%s, cdata=%s' % (self.tag, self.attributes, self.children, self.cdata)
def __repr__(self):
return '<Element: 0x%02X>' % self.tag
class Attribute:
def __init__(self, tag, value, f=None, *args, **kwargs):
if not isinstance(tag, int): raise ValueError('tag must be an integer')
self.tag = tag
self.value = value
self.f = f
self.args = args
self.kwargs = kwargs
logger.debug('created new attribute: %s', repr(self))
def tobytes(self):
if not self.f: raise ValueError('cant encode this attribute without an encoding function')
# encode data
data = None
logger.debug('encoding attribute %s with function %s', self, self.f)
data = self.f(self.value, *self.args, **self.kwargs)
#if isinstance(self.value, int) or isinstance(self.value, long): # integer
# if self.bitlength is None: raise ValueError('attribute %s with int value has no bitlength specification' % self)
# logger.debug('encoding attribute %s as int with %d bits', self, self.bitlength)
# data = encode_number(self.value, self.bitlength)
#elif isinstance(self.value, datetime.timedelta): # duration
# data = encode_number(self.value.seconds, 16)
# logger.debug('encoding attribute %s as duration', self)
#elif isinstance(self.value, Crid): # CRID
# data = bitarray()
# data.fromstring(str(self.value))
# logger.debug('encoding attribute %s as CRID', self)
#elif isinstance(self.value, Genre): # genre
# data = encode_genre(self.value)
# logger.debug('encoding attribute %s as genre', self)
#elif isinstance(self.value, datetime.datetime): # time
# data = encode_timepoint(self.value)
# logger.debug('encoding attribute %s as timepoint', self)
#elif isinstance(self.value, str): # string
# data = bitarray()
# data.fromstring(self.value)
# logger.debug('encoding attribute %s as string', self)
#elif isinstance(self.value, Bearer):
# data = encode_bearer(self.value)
# logger.debug('encoding attribute %s as bearer', self)
#elif isinstance(self.value, Ensemble):
# data = encode_ensembleid(self.value.ecc, self.value.eid)
# logger.debug('encoding attribute %s as ensemble ID', self.value)
#else:
# raise ValueError('dont know how to encode this type: %s = %s' % (self.value.__class__.__name__, str(self.value)))
#data.fill()
# b0-b7: tag
bits = encode_number(self.tag, 8)
# b8-15: element data length (0-253 bytes)
# b16-31: extended element length (256-65536 bytes)
# b16-39: extended element length (65537-16777216 bytes)
datalength = bits2bytes(data.length())
if datalength <= 253:
bits += encode_number(datalength, 8)
elif datalength >= 254 and datalength <= 1<<16:
tmp = bitarray()
tmp.fromstring('\xfe')
bits += tmp
bits += encode_number(datalength, 16)
elif datalength > 1<<16 and datalength <= 1<<24:
tmp = bitarray()
tmp.fromstring('\xff')
bits += tmp
bits += encode_number(datalength, 24)
else: raise ValueError('element data length exceeds the maximum allowed by the extended element length (24bits): %s > %s' + datalength + " > " + (1<<24))
bits += data
return bits
@staticmethod
def frombits(parent, bits):
# b0-b7: attribute tag
tag = int(bits[0:8].to01(), 2)
# b8-15: attribute data length (0-253 bytes)
# b16-31: extended attribute length (256-65536 bytes)
# b16-39: extended attribute length (65537-16777216 bytes)
datalength = int(bits[8:16].to01(), 2)
start = 16
if datalength >= 254 and datalength <= 1<<16:
datalength = int(bits[16:32].to01(), 2)
start = 32
elif datalength > 1<<16 and datalength <= 1<<24:
datalength = int(bits[16:40].to01(), 2)
start = 40
elif datalength > 1<<24:
raise ValueError('attribute data length exceeds the maximum allowed by the extended attribute length (24bits): %s > %s' + datalength + " > " + (1<<24))
data = bits[start:start+(datalength * 8)]
# decode data
if isinstance(parent, Element): parent_tag = parent.tag
else: parent_tag = int(parent)
if (parent_tag, tag) in [ # integer
(0x02, 0x80), (0x21, 0x80), (0x23, 0x80), (0x23, 0x81), (0x23, 0x82), (0x23, 0x84), (0x25, 0x80),
(0x1c, 0x81), (0x1c, 0x82), (0x1c, 0x87), (0x17, 0x81), (0x17, 0x82), (0x03, 0x80), (0x26, 0x81),
(0x2b, 0x84), (0x2b, 0x85), (0x2e, 0x81)
]:
logger.debug('decoding tag/attribute 0x%02x/0x%02x as int', parent_tag, tag)
value = int(data.to01(), 2)
elif (parent_tag, tag) in [ # string
(0x14, 0x80), (0x17, 0x80), (0x18, 0x80), (0x18, 0x83), (0x1c, 0x80), (0x20, 0x80), (0x20, 0x82),
(0x21, 0x82), (0x03, 0x82), (0x03, 0x83), (0x2b, 0x80), (0x2b, 0x82), (0x2e, 0x80), (0x31, 0x80),
(0x31, 0x81), (0x29, 0x82), (0x18, 0x81), (0x18, 0x83)
]:
logger.debug('decoding tag/attribute 0x%02x/0x%02x as string', parent_tag, tag)
value = data.tostring()
elif (parent_tag, tag) in [(0x2c, 0x81), (0x2c, 0x83), (0x2f, 0x80), (0x2f, 0x81)]: # duration
logger.debug('decoding tag/attribute 0x%02x/0x%02x as duration', parent_tag, tag)
value = datetime.timedelta(seconds=int(data.to01(), 2))
elif (parent_tag, tag) in []: # genre
logger.debug('decoding tag/attribute 0x%02x/0x%02x as genre', parent_tag, tag)
value = decode_genre(data)
elif (parent_tag, tag) in [(0x20, 0x81), (0x21, 0x81), (0x24, 0x80), (0x24, 0x81), (0x2c, 0x80), (0x2c, 0x82),
(0x03, 0x81)]: # time
logger.debug('decoding tag/attribute 0x%02x/0x%02x as timepoint', parent_tag, tag)
value = decode_timepoint(data)
elif (parent_tag, tag) in [(0x25, 0x80), (0x26, 0x80), (0x29, 0x80), (0x2d, 0x80)]: # content ID
logger.debug('decoding tag/attribute 0x%02x/0x%02x as ContentId', parent_tag, tag)
return Attribute(tag, decode_contentid(data))
elif (parent_tag, tag) in [(0x1c, 0x83), (0x1c, 0x84), (0x03, 0x84), (0x2b, 0x83), (0x2e, 0x83), (0x2e, 0x84)]: # ENUM
try:
value = decode_enum(parent_tag, tag, data)
except:
logger.warning('error decoding enum for parent 0x%02x from tag: 0x%02x - IGNORING for now' % (parent_tag, tag))
value = data
else:
raise ValueError('dont know how to decode attribute value for parent 0x%02x from tag: 0x%02x' % (parent_tag, tag))
return Attribute(tag, value)
def __str__(self):
return str('0x%x' % self.tag)
def __repr__(self):
return '<Attribute: tag=0x%02X, value=%s>' % (self.tag, self.value)
genre_map = dict(
IntentionCS=1,
FormatCS=2,
ContentCS=3,
IntendedAudienceCS=4,
OriginationCS=5,
ContentAlertCS=6,
MediaTypeCS=7,
AtmosphereCS=8
)
def encode_genre(genre):
segments = genre.href.split(':')
if len(segments) < 6: raise ValueError('genre is incorrectly formatted: %s' % genre)
bits = bitarray(4)
bits.setall(False)
# b0-3: RFU(0)
bits += encode_number(0,4)
# b4-7: CS
cs = segments[4]
if cs in genre_map.keys(): cs_val = genre_map[cs]
else: raise ValueError('unknown CS in genre: %s' % cs)
bits += encode_number(cs_val, 4)
# optional schema levels
if len(segments) >= 6:
levels = segments[6].split('.')
for level in levels:
bits += encode_number(int(level), 8)
return bits
def decode_genre(bits):
# b4-7: CS
cs_val = int(bits[4:8].to01(), 2)
if cs_val in genre_map.values(): cs = [x[0] for x in genre_map.items() if x[1] == cs_val]
else: raise ValueError('unknown CS value for genre: %d' % cs_val)
level = '%d' % cs_val
# optional schema levels
if bits.length() > 8:
i = 8
while i < bits.length():
sublevel = int(bits[i:i+8].to01(), 2)
level += '.%d' % sublevel
i += 8
return Genre('urn:tva:metadata:cs:ContentCS:2002:%s' % level)
def encode_string(string):
data = bitarray()
data.fromstring(string)
return data
def encode_timepoint(timepoint):
bits = bitarray(1)
bits.setall(False)
offset = (timepoint.utcoffset().days * 86400 + timepoint.utcoffset().seconds) + (timepoint.dst().days * 86400 + timepoint.dst().days)
timepoint = timepoint - timedelta(seconds=offset)
# b0: RFA(0)
# b1-17: Date
a = (14 - timepoint.month) / 12
y = timepoint.year + 4800 - a
m = timepoint.month + (12 * a) - 3
jdn = timepoint.day + ((153 * m) + 2) / 5 + (365 * y) + (y / 4) - (y / 100) + (y / 400) - 32045
jd = jdn + timepoint.hour / 24 + timepoint.minute / 1440 + timepoint.second / 86400
mjd = (int)(jd - 2400000.5)
bits += encode_number(mjd, 17)
# b18: RFA(0)
bits += bitarray('0')
# b19: LTO Flag
if timepoint.tzinfo is None or (timepoint.utcoffset().days == 0 and timepoint.utcoffset().seconds == 0):
bits += bitarray('0')
else:
bits += bitarray('1')
# b20: UTC Flag
# b21: UTC - 11 or 27 bits depending on the form
if timepoint.second > 0:
bits += bitarray('1')
bits += encode_number(timepoint.hour, 5)
bits += encode_number(timepoint.minute, 6)
bits += encode_number(timepoint.second, 6)
bits += bitarray('0' * 10)
else:
bits += bitarray('0')
bits += encode_number(timepoint.hour, 5)
bits += encode_number(timepoint.minute, 6)
# b32/48: LTO
if bits[19]:
bits += bitarray('00') # b49-50: RFA(0)
bits += bitarray('0' if offset > 0 else '1') # b51: LTO sign
bits += encode_number(offset / (60 * 60) * 2, 5) # b52-56: Half hours
return bits
def decode_timepoint(bits):
if not bits.any(): return None # NOW
mjd = int(bits[1:18].to01(), 2)
date = datetime.datetime.fromtimestamp((mjd - 40587) * 86400)
timepoint = datetime.datetime.combine(date, datetime.time())
# parse timezone
if bits[19]:
sign = bits[-6]
half_hours = int(bits[-5:].to01(), 2)
timezone = dateutil.tz.tzoffset(None, half_hours * 30 * 60 * (-1 if sign else 1))
else:
timezone = dateutil.tz.tzutc()
# parse date with UTC short form or long form
if bits[20]:
timepoint = timepoint.replace(hour=int(bits[21:26].to01(), 2),
minute=int(bits[26:32].to01(), 2),
second=int(bits[32:38].to01(), 2),
microsecond=int(bits[38:48].to01(), 2) * 1000,
tzinfo=timezone)
else:
timepoint = timepoint.replace(hour=int(bits[21:26].to01(), 2),
minute=int(bits[26:32].to01(), 2),
tzinfo=timezone)
return timepoint
def encode_bearer(bearer):
if isinstance(bearer, DabBearer):
bits = bitarray(4)
bits.setall(False)
# b0: RFA(0)
# b1: Ensemble Flag. Indicates whether ECC and EId are contained with the
# Content ID.
# 0 = ECC and EId are not present. The service that is referenced within the
# contentID is transmitted on the same ensemble as this EPG service
# 1 = ECC and EId are present.
if bearer.ecc is not None and bearer.eid is not None: bits[1] = True
# b2: X-PAD flag. Indicates whether the addressed component is carried in an
# X-PAD channel.
# 0 = Is not carried in an X-PAD channel.
# 1 = Is carried in an X-PAD channel.
if bearer.xpad is not None: bits[2] = True
# b3: SId encoding flag
# 0 = Audio service (SId is 16bit)
# 1 = Data service (SId is 32bit)
# no audio support right now
# b4-7: SCIdS
bits += encode_number(bearer.scids, 4)
# optional next 8 bits: ECC
if bearer.ecc is not None:
bits += encode_number(bearer.ecc, 8)
# optional next 16 bits: EId
if bearer.eid is not None:
bits += encode_number(bearer.eid, 16)
# next 16/32 bits: SId
bits += encode_number(bearer.sid, 16)
# optional next 8 bits: X-PAD extension
if bearer.xpad is not None:
bits += encode_number(bearer.xpad, 8)
elif isinstance(bearer, IpBearer):
bits = bitarray()
bits.fromstring(bearer.uri)
else:
raise ValueError('bearer %s not currently supported', bearer)
return bits
def encode_ensembleid(params):
ecc, eid = params
bits = bitarray()
# b0: ECC
bits += encode_number(ecc, 8)
# b8: EId
bits += encode_number(eid, 16)
return bits
def decode_contentid(bits):
"""decodes a ContentId from a bitarray"""
# b0: RFA(0)
# b1: Ensemble Flag. Indicates whether ECC and EId are contained with the
# Content ID.
# 0 = ECC and EId are not present. The service that is referenced within the
# contentID is transmitted on the same ensemble as this EPG service
# 1 = ECC and EId are present.
ecc = None
eid = None
sid = None
scids = None
xpad = None
try:
if bits.length() == 24: # EnsembleId
# ECC, EId
ecc = int(bits[0:8].to01(), 2)
eid = int(bits[8:24].to01(), 2)
return (ecc, eid)
else:
ensemble_flag = bits[1]
xpad_flag = bits[2]
sid_flag = bits[3]
# SCIdS
scids = int(bits[4:8].to01(), 2)
# ECC, EId
i = 8
if ensemble_flag:
ecc = int(bits[8:16].to01(), 2)
eid = int(bits[16:32].to01(), 2)
i = 32
# SId
if not sid_flag:
sid = int(bits[i:i+16].to01(), 2)
i += 16
elif sid_flag:
sid = int(bits[i:i+32].to01(), 2)
i += 32
# XPAD
if xpad_flag:
xpad = int(bits[i+3:i+8].to01(), 2)
return (ecc, eid, sid, scids, xpad)
except:
raise ValueError('error parsing ContentId from data: %s', bitarray_to_hex(bits))
def decode_tokentable(bits):
tokens = {}
i = 0
while i < bits.length():
tag = int(bits[i:i+8].to01(), 2)
length = int(bits[i+8:i+16].to01(), 2)
data = bits[i+16:i+16+(length*8)].tostring()
tokens[tag] = data
i += 16 + (length * 8)
return tokens
"""Map of possible num values and their binary equivalents.
Note that not all the values are currently implemented, which will
cause the decoder to skip over their details"""
enum_values = {
(0x1c, 0x83, 0x01) : False,
(0x1c, 0x83, 0x02) : True,
(0x1c, 0x84, 0x01) : "on-air",
(0x1c, 0x84, 0x02) : "off-air",
(0x2b, 0x83, 0x02) : Multimedia.LOGO_UNRESTRICTED,
(0x2b, 0x83, 0x04) : Multimedia.LOGO_COLOUR_SQUARE,
(0x2b, 0x83, 0x06) : Multimedia.LOGO_COLOUR_RECTANGLE
}
def decode_enum(parent_tag, tag, bits):
if bits.length() != 8: raise ValueError('enum data for parent/attribute 0x%02x/0x%02x is of incorrect length: %d bytes' % (parent_tag, tag, bits.length()/8))
value = int(bits.to01(), 2)
key = (parent_tag, tag, value)
if key in enum_values.keys():
return enum_values.get(key)
else:
raise NotImplementedError('enum for parent/attribute 0x%02x/0x%02x not implemented' % (parent_tag, tag))
class CData:
def __init__(self, value):
self.value = value
def __str__(self):
return self.value
def __repr__(self):
return '<CData: %s>' % str(self)
def tobytes(self):
# b0-b7: element tag
bits = bitarray()
bits.frombytes('\x01')
# b8-15: element data length (0-253 bytes)
# b16-31: extended element length (256-65536 bytes)
# b16-39: extended element length (65537-16777216 bytes)
datalength = len(self.value.encode('utf-8'))
if datalength <= 253:
tmp = encode_number(datalength, 8)
bits += tmp
elif datalength >= 254 and datalength <= 1<<16:
tmp = bitarray()
tmp.frombytes('\xfe')
bits += tmp
tmp = encode_number(datalength, 16)
bits += tmp
elif datalength > 1<<16 and datalength <= 1<<24:
tmp = bitarray()
tmp.frombytes('\xff')
bits += tmp
tmp = encode_number(datalength, 24)
bits += tmp
else: raise ValueError('element data length exceeds the maximum allowed by the extended element length (24bits): %s > %s' + datalength + " > " + (1<<24))
tmp = bitarray()
tmp.frombytes(self.value.encode('utf-8'))
bits += tmp
return bits
@staticmethod
def frombits(bits):
# b0-b7: element tag
tag = int(bits[0:8].to01(), 2)
if tag != 0x01: raise ValueError('CData does not have the correct tag: 0x%02x != 0x01', tag)
# b8-15: element data length (0-253 bytes)
# b16-31: extended element length (256-65536 bytes)
# b16-39: extended element length (65537-16777216 bytes)
datalength = int(bits[8:16].to01(), 2)
start = 16
if datalength >= 254 and datalength <= 1<<16:
datalength = int(bits[16:32].to01(), 2)
start = 32
elif datalength > 1<<16 and datalength <= 1<<24:
datalength = int(bits[16:40].to01(), 2)
start = 40
elif datalength > 1<<24:
raise ValueError('element data length exceeds the maximum allowed by the extended element length (24bits): %s > %s' + datalength + " > " + (1<<24))
data = bits[start:start+(datalength * 8)]
return CData(data.tostring())
def marshall(obj, **kwargs):
"""Marshalls an :class:Epg or :class:ServiceInfo to its binary document"""
if isinstance(obj, ServiceInfo): return marshall_serviceinfo(obj, kwargs.get('ensemble', None))
elif isinstance(obj, ProgrammeInfo): return marshall_programmeinfo(obj)
def marshall_serviceinfo(info, ensemble):
# serviceInformation
info_element = Element(0x03)
if info.version > 1: info_element.attributes.append(Attribute(0x80, info.version, encode_number, 16))
if info.created: info_element.attributes.append(Attribute(0x81, info.created, encode_timepoint))
if info.originator: info_element.attributes.append(Attribute(0x82, info.originator, encode_string))
if info.provider: info_element.attributes.append(Attribute(0x83, info.provider, encode_string))
# ensemble
if ensemble is None: raise ValueError('must specify an ensemble')
ensemble_element = build_ensemble(ensemble, info.services)
info_element.children.append(ensemble_element)
return info_element.tobytes().tobytes()
def marshall_programmeinfo(info):
# epg (default type is DAB, so no need to encode)
epg_element = Element(0x02)
for schedule in info.schedules:
schedule_element = build_schedule(schedule)
epg_element.children.append(schedule_element)
return epg_element.tobytes().tobytes()
def build_schedule(schedule):
# schedule
schedule_element = Element(0x21)
if schedule.version is not None and schedule.version > 1:
schedule_element.attributes.append(Attribute(0x80, schedule.version, encode_number, 16))
schedule_element.attributes.append(Attribute(0x81, schedule.created, encode_timepoint))
if schedule.originator is not None:
schedule_element.attributes.append(Attribute(0x82, schedule.originator, encode_string))
# schedule scope TODO
#scope = schedule.get_scope()
#if scope is not None:
# schedule_element.children.append(build_scope(scope))
# programmes
for programme in schedule.programmes:
programme_element = Element(0x1c)
programme_element.attributes.append(Attribute(0x81, programme.shortcrid, encode_number, 24))
if programme.crid is not None:
programme_element.attributes.append(Attribute(0x80, programme.crid, encode_string))
if programme.version is not None:
programme_element.attributes.append(Attribute(0x82, programme.version, encode_number, 16))
if programme.recommendation:
programme_element.attributes.append(Attribute(0x83, 0x02, encode_number, 8)) # hardcoded to 'yes'
# names
for name in programme.names:
child = build_name(name)
programme_element.children.append(child)
# descriptions
for description in programme.descriptions:
child = build_description(description)
programme_element.children.append(child)
# locations
for location in programme.locations:
child = build_location(location)
programme_element.children.append(child)
# media
if programme.media: programme_element.children.append(build_mediagroup(programme.media))
# genre
for genre in programme.genres:
child = build_genre(genre)
programme_element.children.append(child)
# membership
for membership in programme.memberships:
child = build_membership(membership)
programme_element.children.append(child)
# link
for link in programme.links:
child = build_link(link)
programme_element.children.append(child)
# events
for event in programme.events:
child = build_programme_event(event)
programme_element.children.append(child)
schedule_element.children.append(programme_element)
return schedule_element
def build_scope(scope):
scope_element = Element(0x24)
scope_element.attributes.append(Attribute(0x80, scope.start, encode_timepoint))
scope_element.attributes.append(Attribute(0x81, scope.end, encode_timepoint))
for bearer in scope.bearers:
service_scope_element = Element(0x25)
service_scope_element.attributes.append(Attribute(0x80, bearer, encode_bearer))
scope_element.children.append(service_scope_element)
return scope_element
def build_name(name):
name_element = None
if isinstance(name, ShortName): name_element = Element(0x10)
elif isinstance(name, MediumName): name_element = Element(0x11)
elif isinstance(name, LongName): name_element = Element(0x12)
name_element.cdata = CData(name.text)
return name_element
def build_location(location):
location_element = Element(0x19)
for time in location.times:
location_element.children.append(build_time(time))
for bearer in location.bearers:
bearer_element = Element(0x2d)
bearer_element.attributes.append(Attribute(0x80, bearer, encode_bearer))
location_element.children.append(bearer_element)
return location_element
def build_time(time):
time_element = None
if isinstance(time, Time):
time_element = Element(0x2c)
time_element.attributes.append(Attribute(0x80, time.billed_time, encode_timepoint))
if time.actual_time is not None:
time_element.attributes.append(Attribute(0x82, time.actual_time, encode_timepoint))
if time.actual_duration is not None:
time_element.attributes.append(Attribute(0x83, time.actual_duration.seconds, encode_number, 16))
time_element.attributes.append(Attribute(0x81, time.billed_duration.seconds, encode_number, 16))
elif isinstance(time, RelativeTime):
time_element = Element(0x2f)
time_element.attributes.append(Attribute(0x80, time.billed_offset.seconds, encode_number, 16))
time_element.attributes.append(Attribute(0x81, time.billed_duration.seconds, encode_number, 16))
if time.actual_offset is not None:
time_element.attributes.append(Attribute(0x82, time.actual_offset.seconds, encode_number, 16))
if time.actual_duration is not None:
time_element.attributes.append(Attribute(0x83, time.actual_duration.seconds, encode_number, 16))
return time_element
def build_description(description):
mediagroup_element = Element(0x13)
if isinstance(description, ShortDescription):
description_element = Element(0x1a)
description_element.cdata = CData(description.text)
mediagroup_element.children.append(description_element)
elif isinstance(description, LongDescription):
description_element = Element(0x1b)
description_element.cdata = CData(description.text)
mediagroup_element.children.append(description_element)
return mediagroup_element
def build_mediagroup(all_media):
mediagroup_element = Element(0x13)
for media in all_media :
if not isinstance(media, Multimedia):
raise ValueError('object must be of type %s (is %s)' % (Multimedia.__name__, type(media)))
media_element = Element(0x2b)
if media.content is not None:
media_element.attributes.append(Attribute(0x80, media.content, encode_string))
if media.url is not None:
media_element.attributes.append(Attribute(0x82, media.url, encode_string))
if media.type == Multimedia.LOGO_UNRESTRICTED:
media_element.attributes.append(Attribute(0x83, 0x02, encode_number, 8))
if media.width: media_element.attributes.append(Attribute(0x84, media.width, encode_number, 16))
if media.height: media_element.attributes.append(Attribute(0x85, media.height, encode_number, 16))
if media.type == Multimedia.LOGO_COLOUR_SQUARE:
media_element.attributes.append(Attribute(0x83, 0x04, encode_number, 8))
if media.type == Multimedia.LOGO_COLOUR_RECTANGLE:
media_element.attributes.append(Attribute(0x83, 0x06, encode_number, 8))
mediagroup_element.children.append(media_element)
return mediagroup_element
def build_genre(genre):
genre_element = Element(0x14)
genre_element.attributes.append(Attribute(0x80, genre.href, encode_string))
return genre_element
def build_membership(membership):
membership_element = Element(0x17)
if membership.crid is not None:
membership_element.attributes.append(Attribute(0x80, membership.crid, encode_string))
membership_element.attributes.append(Attribute(0x81, membership.shortcrid, encode_number, 24))
if membership.index is not None:
membership_element.attributes.append(0x82, membership.index, encode_number, 16)
return membership_element
def build_link(link):
link_element = Element(0x18)
link_element.attributes.append(Attribute(0x80, link.uri, encode_string))
if link.description is not None:
link_element.attributes.append(Attribute(0x83, link.description, encode_string))
if link.content is not None:
link_element.attributes.append(Attribute(0x81, link.content, encode_string))
if link.expiry is not None:
link_element.attributes.append(Attribute(0x84, link.expiry, encode_timepoint))
return link_element
def build_programme_event(event):
event_element = Element(0x2e)
if event.crid is not None:
event_element.attributes.append(Attribute(0x80, event.crid, encode_string))
event_element.attributes.append(Attribute(0x81, event.shortcrid, encode_number, 24))
if event.version is not None and event.version > 1:
event_element.attributes.append(Attribute(0x82, event.version, encode_number, 16))
if event.recommendation is True:
event_element.attributes.append(Attribute(0x83, 0x02, encode_number, 8))
# names
for name in event.names:
event_element.children.append(build_name(name))
# descriptions
for description in event.descriptions:
event_element.children.append(build_description(description))
# locations
for location in event.locations:
event_element.children.append(build_location(location))
# media
if event.media: event_element.children.append(build_mediagroup(event.media))
# genre
for genre in event.genres:
event_element.children.append(build_genre(genre))
# membership
for membership in event.memberships:
event_element.children.append(build_membership(membership))
# link
for link in event.links:
event_element.children.append(build_link(link))
return event_element
def build_service(service):
if not isinstance(service, Service): raise ValueError("must be an object of type %s (is %s)" % (Service.__name__, type(service)))
logger.debug('building service: %s', service)
service_element = Element(0x28)
# version
if service.version > 1: service_element.attributes.append(Attribute(0x80, service.version, encode_number, 16))
# service IDs - the first in the list is primary, all others secondary
for bearer in service.bearers:
serviceid_element = Element(0x29)
serviceid_element.attributes.append(Attribute(0x80, bearer, encode_bearer))
service_element.children.append(serviceid_element)
# names
for name in service.names:
service_element.children.append(build_name(name))
# descriptions
for description in service.descriptions:
service_element.children.append(build_description(description))
# media
if service.media: service_element.children.append(build_mediagroup(service.media))
# genre
for genre in service.genres:
service_element.children.append(build_genre(genre))
# language TODO
# keywords
if len(service.keywords):
service_element.children.append(build_keywords(service.keywords))
# links TODO
# radiodns lookup
if service.lookup:
from urlparse import urlparse
url = urlparse(service.lookup)
lookup_element = Element(0x31)