bthci/bthci2/CommandsEvents/generator/event.py
changeset 0 29b1cd4cb562
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 # Copyright (c) 2006-2009 Nokia Corporation and/or its subsidiary(-ies).
       
     2 # All rights reserved.
       
     3 # This component and the accompanying materials are made available
       
     4 # under the terms of "Eclipse Public License v1.0"
       
     5 # which accompanies this distribution, and is available
       
     6 # at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     7 #
       
     8 # Initial Contributors:
       
     9 # Nokia Corporation - initial contribution.
       
    10 #
       
    11 # Contributors:
       
    12 #
       
    13 # Description:
       
    14 # Writes a 'event'.
       
    15 # Template substitution values are deduced from event name and event parameters.
       
    16 # Template substitutions are:
       
    17 # $CLASSNAME: TNameOfCommandEvent. Name of event class.
       
    18 # $FILENAME: nameofcommandevent. Lower case version for use in filenames.
       
    19 # $HEADERGUARD: NAMEOFCOMMANDEVENT. Upper case version for use in header guard defines.
       
    20 # $EVENTCODE: ENameOfEventEvent. Event code for event, must be in hciopcodes.h.
       
    21 # $PARAMETERS: If Status parameter: THCIErrorCode aErrorCode, const TDesC8& aEventData. Else: const TDesC8& aEventData
       
    22 # $FAKERPARAMETERS: fake event parameters TDesC8& aEventData
       
    23 # $FAKERWRITE_IMPLEMENTATION: the writing of the fake event parameters into the event data
       
    24 # $CONSTRUCTOR_PARAMETERS: If Status parameter: aErrorCode, aEventData. Else: EOK, aEventData
       
    25 # $ACCESSOR_DEFINITION: const ParameterType GetParameterName() const;
       
    26 # - for every parameter
       
    27 # $ACCESSOR_IMPLEMENTATION: const ParameterType TNameOfCommandCompleteEvent::GetParameterName() const { retunr iParameterName; }
       
    28 # - for every parameter
       
    29 # $GENERATE_TIME_STAMP: timestamp showing when the generator produced the file
       
    30 # 
       
    31 #
       
    32 
       
    33 from time import strftime
       
    34 from utils import doTimeStampCompareAndWrite
       
    35 
       
    36 # If event doesn't have Status parameter we need to provide a default value of EOK to constructor.
       
    37 def hasStatus(aParams):
       
    38     for p in aParams:
       
    39         if p.iName == 'Status':
       
    40             return True
       
    41 
       
    42     return False
       
    43 
       
    44 # Make accessor definition for all parameters except Status (provided by THCIEventBase)
       
    45 def makeAccessorDef(aParams):
       
    46     def_str = ''
       
    47     
       
    48     for p in aParams:
       
    49         if p.getName() != 'Status':
       
    50             def_str += p.accessorDef('Event') + '\n\t'
       
    51 
       
    52     return def_str
       
    53 
       
    54 # Make accessor implementation for all parameters in except Status (provided by THCIEventBase)
       
    55 def makeAccessorImpl(aEvent, aParams):
       
    56     impl_str = ''
       
    57 
       
    58     for p in aParams:
       
    59         if p.getName() != 'Status':
       
    60             impl_str += p.eventAccessorImpl('T' + aEvent)
       
    61 
       
    62     return impl_str
       
    63 
       
    64 # Make parameters for faking event
       
    65 # trying to make something like...
       
    66 #   THCIErrorCode aErrorCode, TBTDevAddr aBDADDR, TBTLinkKey aLinkKey, TUint8 aKeyType, TDesC8& aFakeEventData
       
    67 # or 
       
    68 #   THCIErrorCode aErrorCode, TUint8 NumResponses, BTDevAddr aBDADDR[]
       
    69 # for use in creating the parameter string to fake up an event
       
    70 def makeFakingParameters(aParams, aCompleteEvent):
       
    71 
       
    72     if hasStatus(aParams):
       
    73         fakingparams_str = 'THCIErrorCode aStatus, '
       
    74     else:
       
    75         fakingparams_str = ''
       
    76     
       
    77     if aCompleteEvent:
       
    78         fakingparams_str += 'TUint8 aNumHCICommandPackets, '	
       
    79 
       
    80     for p in aParams:
       
    81         if p.getName() != 'Status':
       
    82             fakingparams_str += p.toEventParameter() + ', '
       
    83 
       
    84     fakingparams_str += 'TDes8& aEventData'
       
    85 
       
    86     return fakingparams_str
       
    87 
       
    88 # this adds
       
    89 #	iErrorCode = aErrorCode; // set the base class error value	
       
    90 # dependant on whether the event has status 
       
    91 def makeEventSetErrorCode(aParams, aMemberInitialise, aPreHeaderBytes):
       
    92    if (hasStatus(aParams)):
       
    93         if aMemberInitialise:
       
    94             setErrorCode_str = 'iErrorCode = aStatus;'
       
    95         else:
       
    96             bytesBefore = 0;
       
    97             for p in aParams:
       
    98                 if p.getName() == 'Status':
       
    99                     break
       
   100                 bytesBefore += p.iType.iSize;
       
   101             offset = aPreHeaderBytes + bytesBefore;
       
   102             setErrorCode_str = 'iErrorCode = THCIErrorCode(iEventData[' + str(offset) + ']); // Status field is in octet ' + str(offset+1)
       
   103    else:
       
   104         setErrorCode_str = ''
       
   105    return setErrorCode_str
       
   106 
       
   107 # Make the string for the the Parameter_Total_Length field value
       
   108 # produces something like
       
   109 #       3 + 3
       
   110 def makeParameterTotalLengthValue(aParams, aCompleteEvent):
       
   111     totalParamSize = 0
       
   112     totalArrayParamSize = 0;
       
   113     toPacketEnd = False
       
   114     arrayMultipier = '';
       
   115     for p in aParams:
       
   116         if p.iType.iSize == 'n':
       
   117             toPacketEnd = True
       
   118             break
       
   119         if p.iArray:
       
   120             arrayMultipier = 'a' + p.iArray
       
   121             totalArrayParamSize += p.iType.iSize
       
   122             continue
       
   123         totalParamSize += int(p.iType.iSize)
       
   124     val = ''
       
   125     if toPacketEnd:
       
   126         val = 'aEventData.Length()'
       
   127     else:
       
   128         if arrayMultipier != '':
       
   129             val = arrayMultipier + ' * ' + str(totalArrayParamSize) + ' + '
       
   130         val += str(totalParamSize)
       
   131     if aCompleteEvent:
       
   132         val += ' + KCommandCompleteCommonFieldsLength'
       
   133     return val
       
   134 
       
   135 # Make the implementation for faking the event
       
   136 # produces something like
       
   137 #       for (i = 0;i<numiitems;i++)
       
   138 #          WriteDevAddr(aBDADDR[i],0,6,i);
       
   139 # or 
       
   140 #	WriteLinkKey(aLinkKey,6);
       
   141 #	WriteTUint8(aKeyType,22);
       
   142 def makeFakingWriteImplementation(aParams, aCompleteEvent):
       
   143     ## (the parameter length is now output by the base class)
       
   144     impl_str = ''
       
   145     
       
   146     ## Write the functions to set the data for the parameters
       
   147     ## If there are multiple array parameters then they are ordered like an
       
   148     ## array of structures, see BT Spec 2.0 Vol 2 Part E 5.2. The logic below allows
       
   149     ## for multiple such arrays of structures based on sets of consecutive parameters
       
   150     ## sharing the same array bound - however this is not really needed since no current
       
   151     ## event has more than one array.
       
   152     firstOne = True
       
   153     currentArray = None
       
   154     for p in aParams:
       
   155         if True: #p.getName() != 'Status': ## why don't we do the status?
       
   156            if not firstOne:
       
   157                impl_str += '\n\t'
       
   158            else:
       
   159                firstOne = False
       
   160            if p.iArray:
       
   161                if currentArray != p.iArray:
       
   162                    if currentArray != None:
       
   163                        # end the previous array loop (actually, this is never needed)
       
   164                        impl_str += '\t}\n\t'
       
   165                        currentArray = None
       
   166                    # start the new array loop
       
   167                    currentArray = p.iArray	# remember we're doing this array
       
   168                    impl_str += 'for(int i=0;i<a' + p.iArray + ';++i)\n\t\t{\n\t'
       
   169                    
       
   170                # continue generating the array
       
   171                impl_str += '\t'+ p.iType.iEventSetter + '(a' + p.getName() + '[i]' 
       
   172                if p.iType.iTypename == 'TUint32' or p.iType.iEventSetter == 'PutPaddedString':
       
   173                    impl_str += ', ' + str(p.iType.iSize)
       
   174                impl_str += ', aEventData);'
       
   175            else: 
       
   176                if currentArray != None:
       
   177                    # finish off the array we were doing (won't actually happen, arrays always come last really)
       
   178                    impl_str += '\t}\n\t'
       
   179                impl_str += p.iType.iEventSetter + '(a' + p.getName() 
       
   180                if p.iType.iTypename == 'TUint32' or p.iType.iEventSetter == 'PutPaddedString':
       
   181                   impl_str += ', ' + str(p.iType.iSize)
       
   182                impl_str += ', aEventData);'
       
   183 
       
   184     if currentArray != None:
       
   185         impl_str += '\n\t\t}'
       
   186         
       
   187     return impl_str
       
   188     
       
   189 def eventPreHeaderBytes():
       
   190    return 2
       
   191 
       
   192 def writeEvent(aEvent, aParams, aHeaderTemplate, aSourceTemplate, aHeaderPath, aSourcePath):
       
   193     c = {'EVENT' : aEvent,
       
   194          'CLASSNAME' : 'T' + aEvent + 'Event',
       
   195          'FILENAME' : aEvent.lower() + 'event',
       
   196          'HEADERGUARD' : aEvent.upper() + 'EVENT',
       
   197          'EVENTCODE' : 'E' + aEvent + 'Event',
       
   198          'ACCESSOR_DEFINITION' : makeAccessorDef(aParams),
       
   199          'ACCESSOR_IMPLEMENTATION' : makeAccessorImpl(aEvent + 'Event', aParams),
       
   200          'GENERATE_TIME_STAMP': strftime("%a, %d %b %Y %H:%M:%S") + ' (time stamp)'}
       
   201     if hasStatus(aParams):
       
   202         c['PARAMETERS'] = 'THCIErrorCode aErrorCode, const TDesC8& aEventData'
       
   203         c['CONSTRUCTOR_PARAMETERS'] = 'aErrorCode, aEventData'
       
   204     else:
       
   205         c['PARAMETERS'] = 'const TDesC8& aEventData'
       
   206         c['CONSTRUCTOR_PARAMETERS'] = 'EOK, aEventData'
       
   207     c['FAKERPARAMETERS'] = makeFakingParameters(aParams, False)
       
   208     c['FAKER_PARAMETER_TOTAL_LENGTH'] = makeParameterTotalLengthValue(aParams, False)
       
   209     c['FAKERSETERRORCODEBYPARAM'] = makeEventSetErrorCode(aParams, True, eventPreHeaderBytes())
       
   210     c['EVENTSETERRORCODEBYEXTRACT'] = makeEventSetErrorCode(aParams, False, eventPreHeaderBytes())
       
   211     c['FAKERWRITE_IMPLEMENTATION'] = makeFakingWriteImplementation(aParams, False)
       
   212 
       
   213     #file(aHeaderPath + '\\' + c['FILENAME'] + '.h', 'w+').write(aHeaderTemplate.substitute(c))
       
   214     #file(aSourcePath + '\\' + c['FILENAME'] + '.cpp', 'w+').write(aSourceTemplate.substitute(c))
       
   215 
       
   216     doTimeStampCompareAndWrite(aHeaderPath + '\\' + c['FILENAME'] + '.h', aHeaderTemplate.substitute(c))
       
   217     doTimeStampCompareAndWrite(aSourcePath + '\\' + c['FILENAME'] + '.cpp', aSourceTemplate.substitute(c))