Welcome, guest | Sign In | My Account | Store | Cart

Implement an EMM generator simulator for CAS DVB Simulcrypt EMM

Python, 533 lines
  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
# CASpy - Python implementation of DVB Simulcrypt CAS
# Currently, only implement EMM Generator (EMMG)
# Implementing DVB-Simulcrypt message :

import binascii
import socket
import ctypes
from time import sleep
import math

# Utility functions, to allow for serialization of the ctypes.Structure classes
def serialize(ctypesObj):
    """
    FAQ: How do I copy bytes to Python from a ctypes.Structure?
    """
    return buffer(ctypesObj)[:]

def deserialize(ctypesObj, inputBytes):
    """
    FAQ: How do I copy bytes to a ctypes.Structure from Python?
    """
    fit = min(len(inputBytes), ctypes.sizeof(ctypesObj))
    ctypes.memmove(ctypes.addressof(ctypesObj), inputBytes, fit)

INPUT_BUFFER_LENGTH     = 1024                                                  # Units : [Bytes]. Input buffer size of received TCP messages
SEND_EMM_PERIOD_TIME    = 50e-3                                                 # Units : [Number of Seconds] / [IP packet]
SEND_EMM_FREQUENCY      = 1.0 / SEND_EMM_PERIOD_TIME                            # Units : [Number of IP Packets] / [Sec]
BYTE_TO_BIT_SCALE       = 8
KBPS_TO_BPS_SCALE       = 1000.0
BPS_TO_KBPS_SCALE       = 0.001
#EMM_DUMMY_DATA = binascii.unhexlify('01B040FFFFC7000009160500E03213012014030078001403007C0014030FFF4009040602E0C109040D01E1F409110500E0AB130120140300E800140300E8107A38F9E2')


# ****************************************************
# DVB Simulcrypt messages semantic 
# ****************************************************
simulcrypt_protocol_version         = 0x02                                      

# Parameters type value according to Simulcrypt standard, 6.22 Table 5 - Parameters : 
client_id_type                      = 0x0001                                    
client_id_length                    = 4                                    

section_TSpkt_flag_type             = 0x0002                                    
section_TSpkt_flag_length           = 1

data_channel_id_type                = 0x0003                                    
data_channel_id_length              = 2

data_stream_id_type                 = 0x0004                                    
data_stream_id_length               = 2

datagram_type                       = 0x0005                                    # Parameter length : Variable

bandwidth_type                      = 0x0006                                    # Units: kbit/s 
bandwidth_length                    = 2 

data_type_type                      = 0x0007                                    
data_type_length                    = 1

data_id_type                        = 0x0008                                    
data_id_length                      = 2

error_status_type                   = 0x7000                                    # See clause 6.2.6
error_status_length                 = 2

# According to 6.4, 6.5 - Messages :                 
Channel_setup_message_Type          = 0x0011
Channel_test_message_Type           = 0x0012                
Channel_status_message_Type         = 0x0013
Channel_close_message_Type          = 0x0014
Channel_error_message_Type          = 0x0015
Stream_setup_message_Type           = 0x0111
Stream_test_message_Type            = 0x0112
Stream_status_message_Type          = 0x0113
Stream_close_request_message_Type   = 0x0114
Stream_close_response_message_Type  = 0x0115
Stream_error_message_Type           = 0x0116
Stream_BW_request_message_Type      = 0x0117
Stream_BW_allocation_message_Type   = 0x0118
Data_provision_messageType          = 0x0211


# ****************************************************
#        SIMULCRYPT TLV CLASS
# ****************************************************
class CMessageHeader (ctypes.BigEndianStructure):
    """
    message_header
    """
    _pack_      = 1
    
    _length_    = 5
    
    _fields_    = [("protocol_version",    ctypes.c_ubyte),
                   ("message_type",        ctypes.c_ushort),
                   ("message_length",      ctypes.c_ushort)]

class CTypeLength (ctypes.BigEndianStructure):
    """
    Type Length, of the TLV struct
    """
    _pack_      = 1
    
    _length_    = 4
    
    _fields_    = [("param_type",        ctypes.c_ushort),
                   ("param_length",      ctypes.c_ushort)]
    
class CByteParam (ctypes.BigEndianStructure):
    """
    One byte unsigned parameter
    """
    _pack_      = 1
    
    _length_    = 5
    
    _fields_    = [("param_type",        ctypes.c_ushort),
                   ("param_length",      ctypes.c_ushort),
                   ("param_value",       ctypes.c_ubyte)]

class CShortParam (ctypes.BigEndianStructure):
    """
    Two byte unsigned parameter
    """
    _pack_      = 1
    
    _length_    = 6
    
    _fields_    = [("param_type",        ctypes.c_ushort),
                   ("param_length",      ctypes.c_ushort),
                   ("param_value",       ctypes.c_ushort)]

class CLongParam (ctypes.BigEndianStructure):
    """
    Four byte unsigned parameter
    """
    _pack_      = 1
    
    _length_    = 8
        
    _fields_ = [("param_type",        ctypes.c_ushort),
                ("param_length",      ctypes.c_ushort),
                ("param_value",       ctypes.c_ulong)]


# ****************************************************
#        EMMG CLASS
# ****************************************************
class CEMMG:
    """
    Template for EMMG Simulator
    """
    def __init__(   self, 
                    client_id,                      # #1 
                    section_TSpkt_flag,             # #2
                    data_channel_id,                # #3
                    data_stream_id,                 # #4
                    bandwidth,                      # #5                        # Unit : Kbps
                    data_id,                        # #6
                    data_type,                      # #7
                    inputFile):                     # #8
        """
        These 7 parameters (excluding the actual data) form a single EMM stream. 
        One channel, One stream, One data id . 
        """
        # #1 
        self.client_id          = CLongParam(client_id_type,
                                             client_id_length,
                                             client_id)
        # #2                                               
        self.section_TSpkt_flag = CByteParam(section_TSpkt_flag_type,
                                             section_TSpkt_flag_length,
                                             section_TSpkt_flag)
        # #3                
        self.data_channel_id    = CShortParam(data_channel_id_type,
                                              data_channel_id_length,
                                              data_channel_id)
        # #4        
        self.data_stream_id     = CShortParam(data_stream_id_type,
                                              data_stream_id_length,
                                              data_stream_id)

        # #6        
        self.data_id            = CShortParam(data_id_type,
                                              data_id_length,
                                              data_id)
        # #7        
        self.data_type          = CByteParam(data_type_type,
                                             data_type_length,
                                             data_type)
        
        
        #8             
        #Initialize members                           
        self.Section                = 0
        self.IpEmmSectionPerCycle   = 0        
        self.IpEmmSection           = 0
        self.IpEmmSectionLen        = 0                                                
        self.dataTypeLength         = 0
        self._buildIpEmm_(inputFile, bandwidth)
         
        #5
        #Initialize members
        self.minimumBPS     = 0
        self.ReqBWfromMux   = 0
        self.requestBW      = 0  
        self._buildReqBWfromMux_()
               
        # DEBUG PRINTS        
        print 'Section file \"%s\" length %d, will be sent at %d mSec per cycle ' % (inputFile, len(self.Section), SEND_EMM_PERIOD_TIME * KBPS_TO_BPS_SCALE)
        print 'Minimum bitrate for this section (one section per cycle) : %.3f Kbps ' % (self.minimumBPS * BPS_TO_KBPS_SCALE)
        print 'User requested bandwidth %d Kbps, which is %d sections per cycle, %.3f Kbps ' % (bandwidth, self.IpEmmSectionPerCycle, (self.minimumBPS * self.IpEmmSectionPerCycle * BPS_TO_KBPS_SCALE))
        print 'Actual Requested Bandwidth from Multiplexer will be %d Kbps (%d sections per cycle)' % (self.ReqBWfromMux, (self.IpEmmSectionPerCycle + 1))

    def _buildIpEmm_ (self, inputFile, bandwidth):
        """
        Build data segment sent to TCP each SEND_EMM_PERIOD_TIME seconds
        EMMG sends EMMs every fixed cycle time : SEND_EMM_PERIOD_TIME
        Hence, the actual bitrate is determined by the number of sections being sent each time.
        Calculation of the number of EMM sections each cycle is done in EmmSecPerCycle  
        """
        fd                          = open(inputFile, 'rb')   
        self.Section                = fd.read()
        fd.close()                
        self.IpEmmSectionPerCycle   = self._calcNumberSectionsPerCycle_(bandwidth, len(self.Section))        
        self.IpEmmSection           = self.Section * self.IpEmmSectionPerCycle
        self.IpEmmSectionLen        = len(self.IpEmmSection)                                                
        self.dataTypeLength         = CTypeLength (datagram_type,
                                                   self.IpEmmSectionLen)  

    def _buildReqBWfromMux_ (self):
        """
        Build requested BW from Multiplexer
        """
        self.minimumBPS     = SEND_EMM_FREQUENCY * len(self.Section) * BYTE_TO_BIT_SCALE
        self.ReqBWfromMux   = int( self.minimumBPS * (self.IpEmmSectionPerCycle + 1) * BPS_TO_KBPS_SCALE )                  
        self.requestBW      = CShortParam(bandwidth_type,
                                          bandwidth_length,
                                          self.ReqBWfromMux)
        
    def _calcNumberSectionsPerCycle_ (self, requiredBW, sectionLength):
        """
        Calculate the number of sections needed to be transmitted each cycle of SEND_EMM_PERIOD_TIME seconds,
        given requiredBW in Kbps
        """
        SectionPerCycle = ((requiredBW * KBPS_TO_BPS_SCALE) * SEND_EMM_PERIOD_TIME) / (sectionLength * BYTE_TO_BIT_SCALE)                                   
        SectionPerCycle = int (math.ceil(SectionPerCycle))                      # Round up and cast to int the number of EMM sections being transmitted per cycle
        
        return SectionPerCycle

    def updateIpEmm (self, inputFile, requiredBW):
        """
        Update IP Emm length according to new requiredBW and new file.
        This method DOES NOT negotiate with Mux new BW Threshold, if requiredBW exceeds present threshold
        """
        self._buildIpEmm_(inputFile, requiredBW)
        
    def prepare_channel_setup_Msg (self):
        """
        Prepares and packs the emm_channel_setup message 
        """

        msgBody     =   serialize(self.client_id)           +   \
                        serialize(self.data_channel_id)     +   \
                        serialize(self.section_TSpkt_flag)


        
        msgHeader   =   serialize(CMessageHeader (simulcrypt_protocol_version,  # DVB simulcrypt protocol : 0x02
                                                  Channel_setup_message_Type,                 
                                                  len(msgBody)))                # Calculate total message length
                                          
        totalMsg    = msgHeader + msgBody
        
        return totalMsg 

    def prepare_stream_setup_Msg (self):
        """
        Prepares and packs the emm_stream_setup message 
        """

        msgBody     =   serialize(self.client_id)           +   \
                        serialize(self.data_channel_id)     +   \
                        serialize(self.data_stream_id)      +   \
                        serialize(self.data_id)             +   \
                        serialize(self.data_type)                                
        
        msgHeader   =   serialize(CMessageHeader (simulcrypt_protocol_version,  # DVB simulcrypt protocol : 0x02
                                                  Stream_setup_message_Type,                 
                                                  len(msgBody)))                # Calculate total message length
                                          
        totalMsg    = msgHeader + msgBody
        
        return totalMsg 
    
    def prepare_stream_BW_request_Msg (self):
        """
        Prepares and packs the stream bandwidth request message 
        """

        msgBody     =   serialize(self.client_id)           +   \
                        serialize(self.data_channel_id)     +   \
                        serialize(self.data_stream_id)      +   \
                        serialize(self.requestBW)                               
        
        msgHeader   =   serialize(CMessageHeader (simulcrypt_protocol_version,  # DVB simulcrypt protocol : 0x02
                                                  Stream_BW_request_message_Type,                 
                                                  len(msgBody)))                # Calculate total message length
                                          
        totalMsg    = msgHeader + msgBody
        
        return totalMsg 

    def prepare_Provision_Data_Msg (self):
        """
        Prepares and packs the Provision Data message 
        """

        msgBody     =   serialize(self.client_id)           +   \
                        serialize(self.data_channel_id)     +   \
                        serialize(self.data_stream_id)      +   \
                        serialize(self.data_id)             +   \
                        serialize(self.dataTypeLength)      +   \
                        self.IpEmmSection                                               
        
        msgHeader   =   serialize(CMessageHeader (simulcrypt_protocol_version,  # DVB simulcrypt protocol : 0x02
                                                  Data_provision_messageType,                 
                                                  len(msgBody)))                # Calculate total message length
                                          
        totalMsg    =   msgHeader + msgBody
        
        return totalMsg 

    def receiveMessage(self, strMsg):
        """
        Parse buffer received from mux
        """
        header      = CMessageHeader(0,0,0)
        paramTL     = CTypeLength   (0,0)                                       # Parameter Type-Length values (from the trio TYPE-LENGTH-VALUE) 
        longParam   = CLongParam    (0,0,0)
        shortParam  = CShortParam   (0,0,0)
        byteParam   = CByteParam    (0,0,0)

        JUSTIFY_LEN = 25
                        
        # Parse header       
        
        deserialize (header, strMsg)
        
        if header.message_type      == Channel_setup_message_Type           :
            print "Message type Channel_setup_message "
            
        elif header.message_type    == Channel_test_message_Type            :
            print "Message type Channel_test_message "  

        elif header.message_type    == Channel_status_message_Type          :
            print "Message type Channel_status_message "  

        elif header.message_type    == Channel_close_message_Type           :
            print "Message type Channel_close_message "  

        elif header.message_type    == Channel_error_message_Type           :
            print "Message type Channel_error_message "  

        elif header.message_type    == Stream_setup_message_Type            :
            print "Message type Stream_setup_message "

        elif header.message_type    == Stream_test_message_Type            :
            print "Message type Stream_test_message "

        elif header.message_type    == Stream_status_message_Type           :
            print "Message type Stream_status_message "

        elif header.message_type    == Stream_close_request_message_Type    :
            print "Message type Stream_close_request_message "
        
        elif header.message_type    == Stream_close_response_message_Type   :
            print "Message type Stream_close_response_message "
                
        elif header.message_type    == Stream_error_message_Type            :
            print "Message type Stream_error_message "        
        
        elif header.message_type    == Stream_BW_request_message_Type       :
            print "Message type Stream_BW_request_message "

        elif header.message_type    == Stream_BW_allocation_message_Type    :
            print "Message type Stream_BW_allocation_message "
                    
        elif header.message_type    == Data_provision_messageType           :
            print "Message type Data_provision_message "
        
        print   "Message Length   : ".ljust(JUSTIFY_LEN), header.message_length                        
               
        
        # Parse message body. Iterate on TLV structures        
        strMsg = strMsg[CMessageHeader._length_ : ]
                
        while len(strMsg) > 0 :
            
            deserialize(paramTL, strMsg)
            
            if paramTL.param_type == client_id_type :
                deserialize (longParam, strMsg)
                print "Client id : ".ljust(JUSTIFY_LEN), "0x%X" % longParam.param_value                
                strMsg = strMsg [CLongParam._length_ : ]
                
            elif paramTL.param_type == section_TSpkt_flag_type :
                deserialize (byteParam, strMsg)
                print "section TSpkt flag : ".ljust(JUSTIFY_LEN), byteParam.param_value                
                strMsg = strMsg [CByteParam._length_ : ]                                                 
        
            elif paramTL.param_type == data_channel_id_type :
                deserialize (shortParam, strMsg)
                print "data channel id : ".ljust(JUSTIFY_LEN), shortParam.param_value                
                strMsg = strMsg [CShortParam._length_ : ]                                                 
                
            elif paramTL.param_type == data_stream_id_type :
                deserialize (shortParam, strMsg)
                print "data stream id : ".ljust(JUSTIFY_LEN), shortParam.param_value                
                strMsg = strMsg [CShortParam._length_ : ]                                                 
                
            elif paramTL.param_type == bandwidth_type :
                deserialize (shortParam, strMsg)
                print "bandwidth : ".ljust(JUSTIFY_LEN), shortParam.param_value                
                strMsg = strMsg [CShortParam._length_ : ]        

            elif paramTL.param_type == data_type_type :
                deserialize (byteParam, strMsg)
                print "data type : ".ljust(JUSTIFY_LEN), byteParam.param_value                
                strMsg = strMsg [CByteParam._length_ : ]        

            elif paramTL.param_type == data_id_type :
                deserialize (shortParam, strMsg)
                print "data id : ".ljust(JUSTIFY_LEN), shortParam.param_value                
                strMsg = strMsg [CShortParam._length_ : ]        

            elif paramTL.param_type == error_status_type :
                deserialize (shortParam, strMsg)
                print "error status : ".ljust(JUSTIFY_LEN), shortParam.param_value                
                strMsg = strMsg [CShortParam._length_ : ]        


        
        
if __name__ == '__main__':
                   
    EMM_INPUT_FILE = r'D:\EmmgSimulator\section'  
                     
    #--- Prepare EMMG
    EMMG1 = CEMMG ( client_id           = 0x00099999, 
                    section_TSpkt_flag  = 0x0,
                    data_channel_id     = 0x1,
                    data_stream_id      = 0x32,
                    bandwidth           = 20,                                 # Units : Kbps. bandwidth request
                    data_id             = 0x1,
                    data_type           = 0x1,
                    inputFile           = EMM_INPUT_FILE)  

    #--- Connect to Mux EMM TCP SERVER
    muxEmmSocket    = socket.socket()   
    host            = '10.40.2.195'
    port            = 20000
    muxEmmSocket.connect ((host, port))
    
    print
    print " CHANNEL SETUP" 
    print " *************"
    #--- Send Channel Setup Message
    sendMsg = EMMG1.prepare_channel_setup_Msg()        
    muxEmmSocket.send(sendMsg);
    
    #--- Get Channel Status Message
    muxEmmMsg = muxEmmSocket.recv(INPUT_BUFFER_LENGTH)    
    EMMG1.receiveMessage(muxEmmMsg)

    print
    print " STREAM SETUP" 
    print " ************"
    #--- Send Stream Setup Message
    sendMsg = EMMG1.prepare_stream_setup_Msg()        
    muxEmmSocket.send(sendMsg);
    
    #--- Get Stream Status Message
    muxEmmMsg = muxEmmSocket.recv(INPUT_BUFFER_LENGTH)    
    EMMG1.receiveMessage(muxEmmMsg)    

    print
    print " Stream BW Allocation" 
    print " ********************"
    #--- Send stream BW request Message
    sendMsg = EMMG1.prepare_stream_BW_request_Msg()        
    muxEmmSocket.send(sendMsg);
    
    #--- Get Stream BW allocation Message
    muxEmmMsg = muxEmmSocket.recv(INPUT_BUFFER_LENGTH)    
    EMMG1.receiveMessage(muxEmmMsg)    

    print
    print " Send provision Data" 
    print " *******************"
    #--- Send stream BW request Message
    sendMsg = EMMG1.prepare_Provision_Data_Msg()        
    
    counter = 0
        
    while True :
        
        # Send TCP message with the required EMM
        muxEmmSocket.send(sendMsg);
        counter += 1
            
        # Change EMM bitrate 
        if counter == 400:                                                      
            EMMG1.updateIpEmm(EMM_INPUT_FILE, 60)
            sendMsg = EMMG1.prepare_Provision_Data_Msg()
        # Change EMM bitrate 
        elif counter == 800:                                                      
            EMMG1.updateIpEmm(EMM_INPUT_FILE, 150)
            sendMsg = EMMG1.prepare_Provision_Data_Msg()
        # Change EMM bitrate 
        elif counter == 1200:                                                      
            EMMG1.updateIpEmm(EMM_INPUT_FILE, 20)
            sendMsg = EMMG1.prepare_Provision_Data_Msg()        
        
        # Wait for SEND_EMM_PERIOD_TIME before sending again    
        sleep (SEND_EMM_PERIOD_TIME)                        
        
        # Print statistics
        if counter == 8000:
            break
        elif (counter % 80) == 0 :
            print "Sent " , counter, " packets."    
            print "Message length : %d" % len(sendMsg)
               

 
    
    

For my work, I had to test EMM CAS messages handling implementation, but had trouble with the EMM simulators currently at hand. So I decided to write my own EMM generator, using python, and the result is here.

2 comments

giri babu 7 years, 11 months ago  # | flag

Hi, First, I would like to thank you for the code you had shared. It was a great help for me as I faced trouble in implementing my own EMMG based on simulcrypt.

Hence i found your code here and want to use this code as my starting point. Here I could not understand one aspect. Can you please help me. what is "EMM_INPUT_FILE" used as input and what exactly it contains

Thank you and warm regards, Giri

Sharon (author) 7 years, 11 months ago  # | flag

Hello Giri. Glad my script helped you. First time for me to share a piece a code I've written. The "EMM_INPUT_FILE" is the EMM binary file that is sent to the set-top box. It's vendor specific, and for my testing I used just a small regular binary file (some 60 bytes is the nominal size of these things, as I understand). I used this script to test my system robustness to EMM bursts, and I didn't actually send it to a real set top box. Please let me know if you have any more questions. You can contact me on sfsharon@gmail.com .

Sharon

Created by Sharon on Fri, 23 Dec 2011 (MIT)
Python recipes (4591)
Sharon's recipes (2)

Required Modules

  • (none specified)

Other Information and Tasks