use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.
the class MacGenerationService method calculateMac.
public byte[] calculateMac(final MessageMetadata messageMetadata, final String deviceIdentification, final FirmwareFile firmwareFile) throws ProtocolAdapterException {
final FirmwareFileHeader header = firmwareFile.getHeader();
this.validateHeader(header);
final byte[] iv = this.createIV(firmwareFile);
log.debug("Calculated IV: {}", Hex.toHexString(iv));
final byte[] decryptedFirmwareUpdateAuthenticationKey = this.secretManagementService.getKey(messageMetadata, deviceIdentification, SecurityKeyType.G_METER_FIRMWARE_UPDATE_AUTHENTICATION);
if (decryptedFirmwareUpdateAuthenticationKey == null || decryptedFirmwareUpdateAuthenticationKey.length == 0) {
throw new ProtocolAdapterException(String.format("No key of type %s found for device %s", SecurityKeyType.G_METER_FIRMWARE_UPDATE_AUTHENTICATION, deviceIdentification));
}
final CipherParameters cipherParameters = new KeyParameter(decryptedFirmwareUpdateAuthenticationKey);
final ParametersWithIV parameterWithIV = new ParametersWithIV(cipherParameters, iv);
final int macSizeBits = header.getSecurityLengthInt() * 8;
final GMac mac = new GMac(new GCMBlockCipher(new AESEngine()), macSizeBits);
mac.init(parameterWithIV);
final byte[] headerByteArray = firmwareFile.getHeaderByteArray();
final byte[] firmwareImageByteArray = firmwareFile.getFirmwareImageByteArray();
final byte[] input = ByteBuffer.allocate(headerByteArray.length + firmwareImageByteArray.length).put(headerByteArray).put(firmwareImageByteArray).array();
mac.update(input, 0, input.length);
final byte[] generatedMac = new byte[mac.getMacSize()];
mac.doFinal(generatedMac, 0);
if (header.getSecurityLengthInt() != generatedMac.length) {
throw new ProtocolAdapterException(String.format("Unable to generate correct MAC: Defined security length in firmware header (%d) differs from length of generated MAC (%d)", header.getSecurityLengthInt(), generatedMac.length));
}
return generatedMac;
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.
the class GetConfigurationObjectServiceSmr5 method getConfigurationObject.
@Override
ConfigurationObjectDto getConfigurationObject(final GetResult result) throws ProtocolAdapterException {
final DataObject resultData = result.getResultData();
if (resultData == null || !resultData.isBitString()) {
LOGGER.warn("Configuration object result data is not a BitString: {}", resultData);
throw new ProtocolAdapterException("Expected bit-string data as Configuration object result data, but got: " + (resultData == null ? "null" : resultData.getType()));
}
LOGGER.info("SMR5 Configuration object current BitString: {}", this.dlmsHelper.getDebugInfo(resultData));
final BitString bitString = resultData.getValue();
final byte[] flagByteArray = bitString.getBitString();
final List<ConfigurationFlagDto> configurationFlagDtos = this.toConfigurationFlags(flagByteArray);
final ConfigurationFlagsDto configurationFlagsDto = new ConfigurationFlagsDto(configurationFlagDtos);
return new ConfigurationObjectDto(configurationFlagsDto);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.
the class ClearAlarmRegisterCommandExecutor method execute.
@Override
public AccessResultCode execute(final DlmsConnectionManager conn, final DlmsDevice device, final ClearAlarmRegisterRequestDto clearAlarmRegisterRequestDto, final MessageMetadata messageMetadata) throws ProtocolAdapterException {
final AttributeAddress alarmRegister1AttributeAddress = this.dlmsObjectConfigService.getAttributeAddress(device, DlmsObjectType.ALARM_REGISTER_1, null);
final AccessResultCode resultCodeAlarmRegister1 = this.executeForAlarmRegister(conn, alarmRegister1AttributeAddress);
if (resultCodeAlarmRegister1 == null) {
throw new ProtocolAdapterException("Error occurred for clear alarm register 1.");
}
if (resultCodeAlarmRegister1 != AccessResultCode.SUCCESS) {
return resultCodeAlarmRegister1;
}
final Optional<AttributeAddress> optAlarmRegister2AttributeAddress = this.dlmsObjectConfigService.findAttributeAddress(device, DlmsObjectType.ALARM_REGISTER_2, null);
if (!optAlarmRegister2AttributeAddress.isPresent()) {
return resultCodeAlarmRegister1;
} else {
final AccessResultCode resultCodeAlarmRegister2 = this.executeForAlarmRegister(conn, optAlarmRegister2AttributeAddress.get());
if (resultCodeAlarmRegister2 != null) {
return resultCodeAlarmRegister2;
} else {
throw new ProtocolAdapterException("Error occurred for clear alarm register 2.");
}
}
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.
the class ClearMBusStatusOnAllChannelsCommandExecutor method readStatus.
private long readStatus(final DlmsConnectionManager conn, final Integer channel, final AttributeAddress attributeAddress) throws IOException, ProtocolAdapterException {
conn.getDlmsMessageListener().setDescription("ClearMBusStatusOnAllChannels-readStatus for channel" + channel + " - read status" + JdlmsObjectToStringUtil.describeAttributes(attributeAddress));
log.info("Reading status for M-Bus channel {} with attributeAddress: {}.", channel, attributeAddress);
final GetResult result = conn.getConnection().get(attributeAddress);
if (result == null) {
throw new ProtocolAdapterException("No GetResult received while reading status for M-Bus channel " + channel + ".");
}
return this.parseStatusFilter(result.getResultData(), channel);
}
use of org.opensmartgridplatform.adapter.protocol.dlms.exceptions.ProtocolAdapterException in project open-smart-grid-platform by OSGP.
the class SetAlarmNotificationsCommandExecutor method executeForAlarmFilter.
private AccessResultCode executeForAlarmFilter(final DlmsConnectionManager conn, final AttributeAddress alarmFilterAttributeAddress, final AlarmNotificationsDto alarmNotifications, final DlmsObjectType alarmRegisterDlmsObjectType) throws ProtocolAdapterException {
try {
final AlarmNotificationsDto alarmNotificationsOnDevice = this.retrieveCurrentAlarmNotifications(conn, alarmFilterAttributeAddress, alarmRegisterDlmsObjectType);
LOGGER.info("Alarm Filter on device before setting notifications: {}", alarmNotificationsOnDevice);
final Set<AlarmTypeDto> alarmTypesForRegister = this.alarmHelperService.alarmTypesForRegister(alarmRegisterDlmsObjectType);
final Set<AlarmNotificationDto> alarmNotificationsSet = alarmNotifications.getAlarmNotificationsSet().stream().filter(alarmNotificationDto -> alarmTypesForRegister.contains(alarmNotificationDto.getAlarmType())).collect(Collectors.toSet());
final long alarmFilterLongValueOnDevice = this.alarmFilterLongValue(alarmNotificationsOnDevice);
final long updatedAlarmFilterLongValue = this.calculateAlarmFilterLongValue(alarmNotificationsOnDevice, alarmNotificationsSet);
if (alarmFilterLongValueOnDevice == updatedAlarmFilterLongValue) {
return AccessResultCode.SUCCESS;
}
LOGGER.info("Modified Alarm Filter long value for device: {}", updatedAlarmFilterLongValue);
return this.writeUpdatedAlarmNotifications(conn, updatedAlarmFilterLongValue, alarmFilterAttributeAddress);
} catch (final IOException e) {
throw new ConnectionException(e);
}
}
Aggregations