Example #1
0
 def processCmd(self, reserved, cmdId, payload):
     '''
     \brief deserialize and process command
     '''
     ApiConnector.logDump(payload,
                          "RawIO INP. Command ID: {0}".format(cmdId))
     if cmdId in self.notifIds:
         try:
             payloadList = struct.unpack('!' + str(len(payload)) + 'B',
                                         payload)
             (notifNames,
              params) = self.apiDef.deserialize(self.apiDef.NOTIFICATION,
                                                cmdId, payloadList)
             ApiConnector.log.debug("IO INP.    {0} : {1}".format(
                 notifNames, params))
             self.putNotification((notifNames, params))
         except ApiException.ConnectionError as ex:
             raise socket.error(0, ex.value)  # Initiate disconnection
         except Exception as ex:
             ApiConnector.log.error(
                 "Deserialization command {0}. Error {1}".format(cmdId, ex))
     else:
         self.ackCmdId = cmdId
         self.acknowledgeBuf = payload
         self.ackSignal()
 def processCmd(self, reserved, cmdId, payload):
     '''
     \brief deserialize and process command
     '''
     ApiConnector.logDump(payload, "RawIO INP. Command ID: {0}".format(cmdId))
     if cmdId in self.notifIds :
         try :
             payloadList = struct.unpack('!'+str(len(payload))+'B', payload)
             (notifNames, params) = self.apiDef.deserialize(self.apiDef.NOTIFICATION, cmdId, payloadList)
             ApiConnector.log.debug("IO INP.    {0} : {1}".format(notifNames, params))
             self.putNotification((notifNames, params))
         except ApiException.ConnectionError as ex:
             raise socket.error(0, ex.value)    # Initiate disconnection
         except Exception as ex :
             ApiConnector.log.error("Deserialization command {0}. Error {1}".format(cmdId, ex))
     else :
         self.ackCmdId = cmdId
         self.acknowledgeBuf = payload
         self.ackSignal()
Example #3
0
    def send(self, cmdNames, params):
        self.sendLock.acquire()
        try:
            if not self.isConnected:
                raise ApiException.ConnectionError("Disconnected")

            # Send data
            ApiConnector.log.debug("IO OUT.    {0} : {1}".format(
                cmdNames, params))
            (cmdId, paramsBinList) = self.apiDef.serialize(cmdNames, params)
            paramsBin = struct.pack('!' + str(len(paramsBinList)) + 'B',
                                    *paramsBinList)
            ApiConnector.logDump(paramsBin,
                                 "RawIO OUT. Command ID: {0}".format(cmdId))
            packet = self.muxMsg.build_message(cmdId, paramsBin)
            self.acknowledgeBuf = None
            self.ackCmdId = -1
            try:
                self.socket.sendall(packet)
            except socket.error, way:
                # Socket error. Disconnect from device. Stop command processing
                reason = "IO output error [{0}] {1}".format(
                    way.args[0], way.args[1])
                self.disconnect(reason)
                raise ApiException.ConnectionError(reason)

            # Waiting acknowledge
            self.sendSemaphor.acquire()
            if not self.isConnected:  # Disconnect happened during waiting ack.
                raise ApiException.ConnectionError(self.disconnectReason)

            # Process acknowledge
            cmdId = self.apiDef.nameToId(self.apiDef.COMMAND, (cmdNames[0], ))
            if self.ackCmdId != cmdId:
                reason = "Unexpected acknowledge {0} for command {1} ({2})".format(
                    self.ackCmdId, cmdId, cmdNames)
                self.disconnect(reason)
                raise ApiException.ConnectionError(reason)

            # Parse acknowledge
            ackList = struct.unpack('!' + str(len(self.acknowledgeBuf)) + 'B',
                                    self.acknowledgeBuf)
            (resCmdName,
             resParams) = self.apiDef.deserialize(self.apiDef.COMMAND,
                                                  self.ackCmdId, ackList)
            ApiConnector.log.debug("IO INP.    {0} : {1}".format(
                resCmdName, resParams))

            if self.apiDef.RC in resParams and resParams[
                    self.apiDef.RC] != self._RC_OK:
                if resParams[self.apiDef.RC] == self._RC_TIMEOUT:
                    raise ApiException.CommandTimeoutError(resCmdName)
                try:
                    desc = '({0})\n{1}'.format(
                        self.apiDef.responseFieldValueToDesc(
                            resCmdName,
                            self.apiDef.RC,
                            resParams[self.apiDef.RC],
                        ),
                        self.apiDef.rcToDescription(
                            resParams[self.apiDef.RC],
                            resCmdName,
                        ),
                    )
                except:
                    desc = None
                raise ApiException.APIError(cmd=resCmdName,
                                            rc=resParams[self.apiDef.RC],
                                            desc=desc)

            self.ackCmdId = -1
            self.acknowledgeBuf = None
 def send(self, cmdNames, params) :
     self.sendLock.acquire()
     try :
         if not self.isConnected :
             raise ApiException.ConnectionError("Disconnected")
 
         # Send data
         ApiConnector.log.debug("IO OUT.    {0} : {1}".format(cmdNames, params))
         (cmdId, paramsBinList) = self.apiDef.serialize(cmdNames, params)
         paramsBin = struct.pack('!'+str(len(paramsBinList))+'B', *paramsBinList) 
         ApiConnector.logDump(paramsBin, "RawIO OUT. Command ID: {0}".format(cmdId))
         packet = self.muxMsg.build_message(cmdId, paramsBin)
         self.acknowledgeBuf = None
         self.ackCmdId = -1
         try :
             self.socket.sendall(packet)
         except socket.error, way:
             # Socket error. Disconnect from device. Stop command processing
             reason = "IO output error [{0}] {1}".format(way.args[0], way.args[1])
             self.disconnect(reason)
             raise ApiException.ConnectionError(reason)
         
         # Waiting acknowledge
         self.sendSemaphor.acquire()
         if not self.isConnected :         # Disconnect happened during waiting ack.  
             raise ApiException.ConnectionError(self.disconnectReason)
                                                     
         # Process acknowledge
         cmdId = self.apiDef.nameToId(self.apiDef.COMMAND, (cmdNames[0],))
         if self.ackCmdId != cmdId :
             reason = "Unexpected acknowledge {0} for command {1} ({2})".format(self.ackCmdId, cmdId, cmdNames)
             self.disconnect(reason)
             raise ApiException.ConnectionError(reason)
 
         # Parse acknowledge
         ackList = struct.unpack('!'+str(len(self.acknowledgeBuf))+'B', self.acknowledgeBuf)
         (resCmdName, resParams) = self.apiDef.deserialize(self.apiDef.COMMAND, self.ackCmdId, ackList) 
         ApiConnector.log.debug("IO INP.    {0} : {1}".format(resCmdName, resParams))
         
         if self.apiDef.RC in resParams and resParams[self.apiDef.RC] != self._RC_OK : 
             if resParams[self.apiDef.RC] == self._RC_TIMEOUT :
                 raise ApiException.CommandTimeoutError(resCmdName)
             try:
                 desc = '({0})\n{1}'.format(
                     self.apiDef.responseFieldValueToDesc(
                         resCmdName,
                         self.apiDef.RC,
                         resParams[self.apiDef.RC],
                     ),
                     self.apiDef.rcToDescription(
                         resParams[self.apiDef.RC],
                         resCmdName,
                     ),
                 )
             except:
                 desc = None
             raise   ApiException.APIError(
                         cmd=resCmdName,
                         rc=resParams[self.apiDef.RC],
                         desc=desc
                     )
         
         self.ackCmdId = -1
         self.acknowledgeBuf = None