Commit 296e2f7a authored by Carmelo Magnafico's avatar Carmelo Magnafico
Browse files

MDOR dump and checksum request script added. MDOR for upload, dump and check generated

parent 8d183b3c
Loading
Loading
Loading
Loading

MDOR_memorychecksum.py

0 → 100644
+244 −0
Original line number Diff line number Diff line
#/usr/bin/env python

#########################################################
#   Credits                                             #
#   Author: Carmelo Magnafico - BepiColombo ISA Team    #
#   Association: IASP/INAF                              #
#                                                       #
#   Ver: 0.1                                            #
#########################################################


#usage
#python MDORmemorychecksum.py templatefile.BC 0007 DM RAM 01 0000 FFFF
#
# This routine writes a memorydump MDOR 

from __future__ import division
import sys
import xml.etree.ElementTree as ET
import os
import binascii
import math
from datetime import datetime
from datetime import timedelta
import copy 
import memoryMap as memap #ISA memory mapping
import string
import numpy as np


#set some errors
class wrongdatapacklength(Exception):
    pass
class wrongAddress(Exception):
    pass
class wrongMemoryType(Exception):
    pass


#Settings
deltaSeconds = 10 #delta seconds between commands
datasplit = 224 #maximum length in byte for a single data sent in one command
dataword = 16 #bit of a word
output_folder = 'output/'
INT_BITS = 16
max_words = 16384



# Function to left
# rotate n by d bits
def leftRotate(n, d):
    # In n<<d, last d bits are 0.
    # To put first 3 bits of n at
    # last, do bitwise or of n<<d
    # with n >>(INT_BITS - d)
    return ( (n << d)|(n >> (INT_BITS - d))) & ((1 << INT_BITS) - 1)
 

def checksum(data):

    chsum = 0xF900
    for i in range(int(len(data)/4)):
        chsum = leftRotate(chsum , 1) ^ int(data[i*4:(i+1)*4],INT_BITS)

    return hex(chsum);


def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
 
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
 
    return False


thisdir = os.path.dirname(os.path.realpath(__file__))

templatefile = os.path.join(thisdir,"templates",sys.argv[1])
print("Using template:"+templatefile)


#memoryID
memoryID= sys.argv[4] #'RAM' or 'EEPROM'
#check
if not ( (memoryID == 'RAM') | (memoryID == 'EEPROM') ):
    raise wrongMemoryType('RAM or EEPROM allowed for memoryType')

#page code
page_code = sys.argv[5]
#check
if (len(page_code) < 1) & (len(page_code) > 2):
    raise wrongAddress('Wrong page length: '+page_code+'  | one or 2 decimal digit needed')

if not is_number(page_code):
    raise wrongAddress('Wrong page length: '+page_code+'  | a decimal digit needed')

page_code = format(int(page_code), '02X')

#start Address 
start_address = sys.argv[6]
end_address = sys.argv[7]


#memoryType
memoryType = sys.argv[3] #'PM' or 'DM'
if memoryType == 'PM':
    start_tot_address = '01'+page_code+start_address
    memory = memap.ISA_ProgamMemory
elif memoryType == 'DM':
    start_tot_address = '00'+page_code+start_address
    memory = memap.ISA_DataMemory
else:
    raise wrongMemoryType('DM or PM allowed for memoryType')



#check memoryID from address, raise an error if memoryID is not respected or reserved memory area is violated
saddr = start_tot_address[-6:]
eaddr = page_code+end_address

#check if the last address not exeed the page 
if int(eaddr[-4:],16) <= memap.page_size:

    memorypage = memory[ memap.memorydecode(saddr,page_code+format(int(eaddr[-4:],16),'04X'))]

    #check if the input are right
    if not memoryID == memory[ memap.memorydecode(saddr,saddr)]['type']:
        raise wrongMemoryType('You specified '+memoryID+' as input but you addressing to '+memory[memap.memorydecode(saddr,saddr)]['type'])

    #check if is all data could stay inside the same sector memoryID in the page
    
    sector = memory[ memap.memorydecode(saddr,saddr)]['sect']

    for MM in memorypage:
        if not MM['sect'] == sector:
            raise wrongdatapacklength('you are tring to read an ammount of data that exeed the sector')

else:
    #Cut the data in pages
    raise wrongdatapacklength('you are tring to read an ammount of data that exeed the page')







actionTime = datetime(1900,1,1) + timedelta(seconds=deltaSeconds)

#Updating fields

#generating variables
#parsing xml template
tree = ET.parse(templatefile)
templateXML = tree.getroot()
now = datetime.now()
genTime = datetime.strftime(now,'%Y-%jT%H:%M:%S.%f')[:-3]+"Z"
sequenceNum = sys.argv[2]       #here to check in a folder or DB the first, empty, sequence number 
commandNum = int('060900000')
destination = 'R'



#calculating intervals
data_len = (int(eaddr[-4:],16) - int(saddr[-4:],16) + 1)/int(dataword/8) #[words] converting hex in bytes and then in words to calculate block numbers

block_num = math.ceil(data_len/max_words ) #number of commands to be sent


#print info
print("---------------------------------------------------")
print("             MDOR checksum request generation ")
print("---------------------------------------------------")
print("generating MDOR checksum request for %s %s, page %s from 0x%s to 0x%s"% (memoryType, memoryID, page_code, start_address, end_address) )
print("You requested a checksum of %i Bytes" % data_len)
print("Request split in %i commands" % block_num)
print("writing MDOR .....\n\n\n")

templateXML.find('.//genTime').text = genTime #find recursivelly ".//word"
occurrenceList = templateXML.find('.//occurrenceList')
occurrenceList.set('creationTime', genTime)
occurrenceList.set('count', "%i"%block_num)
command = occurrenceList.find('.//command')
#append the right number of commands
for i in range(0,int(block_num-1)):
    
    #append a new command to occurrenceList
    occurrenceList.append(copy.deepcopy(command))


index = 0



for c in templateXML.iter('command'):

    #generating variables
    commandNum += 1
    uniqueID_value = 'BPSA'+sequenceNum+'ISA'+("%09.i" % commandNum)

    if data_len >= max_words:
        length = max_words #[words]
        data_len = data_len - max_words #[words]
    else:
        length = (data_len % max_words ) #[words]
        
    #modify this command:mpo
    c.find('uniqueID').text = uniqueID_value
    c.find('destination').text = destination
    c.find('./releaseTime/actionTime').text = datetime.strftime(actionTime,'%H:%M:%S')
    
    
    for parameter in c.findall('./parameterList/parameter'):
        if parameter.get('name')=='PSA06060':
            parameter.find('value').text = memoryID
        if parameter.get('name')=='PSA06065':
            parameter.find('value').text = "%s"%start_tot_address #in bit
        if parameter.get('name')=='PSA06068':
            parameter.find('value').text = "%s"%int(length)
    
    #prepare for the next block
    start_tot_address = format(int(start_tot_address,16) + int(length*int(dataword/8)),'08X')

    index += 1
    

#<type>_<source>_<plancycle>_<optional text>_<counter>.<EXT>
outputfile = output_folder+'MDOR_'+'BPSA_'+'T000_'+'memorychecksum_'+sys.argv[2]+'.bc'
tree.write(outputfile)
print(outputfile+' correclty wrote')



MDOR_memorydump.py

0 → 100644
+244 −0
Original line number Diff line number Diff line
#/usr/bin/env python

#########################################################
#   Credits                                             #
#   Author: Carmelo Magnafico - BepiColombo ISA Team    #
#   Association: IASP/INAF                              #
#                                                       #
#   Ver: 0.1                                            #
#########################################################


#usage
#python MDORmemorydump.py templatefile.BC 0007 DM RAM 01 0000 FFFF
#
# This routine writes a memorydump MDOR 

from __future__ import division
import sys
import xml.etree.ElementTree as ET
import os
import binascii
import math
from datetime import datetime
from datetime import timedelta
import copy 
import memoryMap as memap #ISA memory mapping
import string
import numpy as np


#set some errors
class wrongdatapacklength(Exception):
    pass
class wrongAddress(Exception):
    pass
class wrongMemoryType(Exception):
    pass


#Settings
deltaSeconds = 10 #delta seconds between commands
datasplit = 224 #maximum length in byte for a single data sent in one command
dataword = 16 #bit of a word
output_folder = 'output/'
INT_BITS = 16
max_words = 2040



# Function to left
# rotate n by d bits
def leftRotate(n, d):
    # In n<<d, last d bits are 0.
    # To put first 3 bits of n at
    # last, do bitwise or of n<<d
    # with n >>(INT_BITS - d)
    return ( (n << d)|(n >> (INT_BITS - d))) & ((1 << INT_BITS) - 1)
 

def checksum(data):

    chsum = 0xF900
    for i in range(int(len(data)/4)):
        chsum = leftRotate(chsum , 1) ^ int(data[i*4:(i+1)*4],INT_BITS)

    return hex(chsum);


def is_number(s):
    try:
        float(s)
        return True
    except ValueError:
        pass
 
    try:
        import unicodedata
        unicodedata.numeric(s)
        return True
    except (TypeError, ValueError):
        pass
 
    return False


thisdir = os.path.dirname(os.path.realpath(__file__))

templatefile = os.path.join(thisdir,"templates",sys.argv[1])
print("Using template:"+templatefile)


#memoryID
memoryID= sys.argv[4] #'RAM' or 'EEPROM'
#check
if not ( (memoryID == 'RAM') | (memoryID == 'EEPROM') ):
    raise wrongMemoryType('RAM or EEPROM allowed for memoryType')

#page code
page_code = sys.argv[5]
#check
if (len(page_code) < 1) & (len(page_code) > 2):
    raise wrongAddress('Wrong page length: '+page_code+'  | one or 2 decimal digit needed')

if not is_number(page_code):
    raise wrongAddress('Wrong page length: '+page_code+'  | a decimal digit needed')

page_code = format(int(page_code), '02X')

#start Address 
start_address = sys.argv[6]
end_address = sys.argv[7]


#memoryType
memoryType = sys.argv[3] #'PM' or 'DM'
if memoryType == 'PM':
    start_tot_address = '01'+page_code+start_address
    memory = memap.ISA_ProgamMemory
elif memoryType == 'DM':
    start_tot_address = '00'+page_code+start_address
    memory = memap.ISA_DataMemory
else:
    raise wrongMemoryType('DM or PM allowed for memoryType')



#check memoryID from address, raise an error if memoryID is not respected or reserved memory area is violated
saddr = start_tot_address[-6:]
eaddr = page_code+end_address

#check if the last address not exeed the page 
if int(eaddr[-4:],16) <= memap.page_size:

    memorypage = memory[ memap.memorydecode(saddr,page_code+format(int(eaddr[-4:],16),'04X'))]

    #check if the input are right
    if not memoryID == memory[ memap.memorydecode(saddr,saddr)]['type']:
        raise wrongMemoryType('You specified '+memoryID+' as input but you addressing to '+memory[memap.memorydecode(saddr,saddr)]['type'])

    #check if is all data could stay inside the same sector memoryID in the page
    
    sector = memory[ memap.memorydecode(saddr,saddr)]['sect']

    for MM in memorypage:
        if not MM['sect'] == sector:
            raise wrongdatapacklength('you are tring to read an ammount of data that exeed the sector')

else:
    #Cut the data in pages
    raise wrongdatapacklength('you are tring to read an ammount of data that exeed the page')







actionTime = datetime(1900,1,1) + timedelta(seconds=deltaSeconds)

#Updating fields

#generating variables
#parsing xml template
tree = ET.parse(templatefile)
templateXML = tree.getroot()
now = datetime.now()
genTime = datetime.strftime(now,'%Y-%jT%H:%M:%S.%f')[:-3]+"Z"
sequenceNum = sys.argv[2]       #here to check in a folder or DB the first, empty, sequence number 
commandNum = int('060600000')
destination = 'R'



#calculating intervals
data_len = (int(eaddr[-4:],16) - int(saddr[-4:],16) + 1)/int(dataword/8) #[words] converting hex in bytes and then in words to calculate block numbers

block_num = math.ceil(data_len/max_words ) #number of commands to be sent


#print info
print("------------------------------------------")
print("             MDOR dump generation ")
print("------------------------------------------")
print("generating MDOR Dump for %s %s, page %s from 0x%s to 0x%s"% (memoryType, memoryID, page_code, start_address, end_address) )
print("You requested a dump of %i Bytes" % data_len)
print("Commands needed: %i" % block_num)
print("writing MDOR .....\n\n\n")

templateXML.find('.//genTime').text = genTime #find recursivelly ".//word"
occurrenceList = templateXML.find('.//occurrenceList')
occurrenceList.set('creationTime', genTime)
occurrenceList.set('count', "%i"%block_num)
command = occurrenceList.find('.//command')
#append the right number of commands
for i in range(0,int(block_num-1)):
    
    #append a new command to occurrenceList
    occurrenceList.append(copy.deepcopy(command))


index = 0



for c in templateXML.iter('command'):

    #generating variables
    commandNum += 1
    uniqueID_value = 'BPSA'+sequenceNum+'ISA'+("%09.i" % commandNum)

    if data_len >= max_words:
        length = max_words #[words]
        data_len = data_len - max_words #[words]
    else:
        length = (data_len % max_words ) #[words]
        
    #modify this command:mpo
    c.find('uniqueID').text = uniqueID_value
    c.find('destination').text = destination
    c.find('./releaseTime/actionTime').text = datetime.strftime(actionTime,'%H:%M:%S')
    
    
    for parameter in c.findall('./parameterList/parameter'):
        if parameter.get('name')=='PSA06060':
            parameter.find('value').text = memoryID
        if parameter.get('name')=='PSA06065':
            parameter.find('value').text = "%s"%start_tot_address #in bit
        if parameter.get('name')=='PSA06067':
            parameter.find('value').text = "%s"%int(length)
    
    #prepare for the next block
    start_tot_address = format(int(start_tot_address,16) + int(length*int(dataword/8)),'08X')

    index += 1
    

#<type>_<source>_<plancycle>_<optional text>_<counter>.<EXT>
outputfile = output_folder+'MDOR_'+'BPSA_'+'T000_'+'memorydump_'+sys.argv[2]+'.bc'
tree.write(outputfile)
print(outputfile+' correclty wrote')



+30 −0
Original line number Diff line number Diff line
<planningData>
    <commandRequests>
        <header formatVersion="1" type="MDOR">
            <genTime>2023-186T16:22:10.793Z</genTime>
        </header>
        <occurrenceList count="1" creationTime="2023-186T16:22:10.793Z" author="ISA team">
            <command name="ZSAY0609">
                <passID />
                <uniqueID>BPSA0001ISA060900001</uniqueID>
                <insertOrDeleteFlag>Insert</insertOrDeleteFlag>
                <source>BPSA</source>
                <destination>R</destination>
                <releaseTime>
                    <actionTime>00:00:10</actionTime>
                </releaseTime>
                <parameterList count="3">
                    <parameter name="PSA06060" position="1">
                        <value representation="Engineering">RAM</value>
                    </parameter>
                    <parameter name="PSA06065" position="2">
                        <value representation="Raw" radix="Hexadecimal">00080000</value>
                    </parameter>
                    <parameter name="PSA06068" position="3">
                        <value representation="Raw" radix="Decimal">16383</value>
                    </parameter>
                </parameterList>
            </command>
        </occurrenceList>
    </commandRequests>
</planningData>
 No newline at end of file
+30 −0
Original line number Diff line number Diff line
<planningData>
    <commandRequests>
        <header formatVersion="1" type="MDOR">
            <genTime>2023-186T16:22:17.720Z</genTime>
        </header>
        <occurrenceList count="1" creationTime="2023-186T16:22:17.720Z" author="ISA team">
            <command name="ZSAY0609">
                <passID />
                <uniqueID>BPSA0002ISA060900001</uniqueID>
                <insertOrDeleteFlag>Insert</insertOrDeleteFlag>
                <source>BPSA</source>
                <destination>R</destination>
                <releaseTime>
                    <actionTime>00:00:10</actionTime>
                </releaseTime>
                <parameterList count="3">
                    <parameter name="PSA06060" position="1">
                        <value representation="Engineering">RAM</value>
                    </parameter>
                    <parameter name="PSA06065" position="2">
                        <value representation="Raw" radix="Hexadecimal">00090000</value>
                    </parameter>
                    <parameter name="PSA06068" position="3">
                        <value representation="Raw" radix="Decimal">16383</value>
                    </parameter>
                </parameterList>
            </command>
        </occurrenceList>
    </commandRequests>
</planningData>
 No newline at end of file
+198 −177

File changed and moved.

Preview size limit exceeded, changes collapsed.

Loading