private async Task <ErrorCode> SetBusAddressAsyncInternal(uint uuid, int busAddress, bool sync) { var request = new BusRequest(); request.Write((byte)0x00); request.Write((byte)0x00); request.Write(uuid); request.Write((byte)0x00); request.Write((byte)busAddress); var result = sync ? TransceiveBroadcast(request, 1) : await TransceiveBroadcastAsync(request, 1); if (result.Success) { if (result.Response.ReadByte() == 1) { return(ErrorCode.Success); } else { return(ErrorCode.DeviceRejectedBusAddress); } } else { return(result.TransportError); } }
private async Task <(ErrorCode, string)> GetCommandNameAsyncInternal(byte key, bool sync) { (ErrorCode commandNameLengthError, byte length) = sync ? GetCommandNameLength(key) : await GetCommandNameLengthAsync(key); if (commandNameLengthError != ErrorCode.Success) { Log.Logger?.Error("unable to retrieve command name length", this); return(commandNameLengthError, null); } BusRequest request = new BusRequest(); request.Write(key); request.Write((byte)CommandKey.CommandInfoGetName); request.Write((byte)CommandKey.CommandInfoGetName); request.Write((byte)CommandKey.CommandInfoGetName); BusTransceiveResult result = sync ? Transceive(request, length) : await TransceiveAsync(request, length); if (result.Success) { return(ErrorCode.Success, Encoding.UTF8.GetString(result.Response.ReadBytes(length))); } else { return(result.TransportError, null); } }
private async Task <bool> SetDigitalOutputAsyncInternal(uint key, bool value, bool sync) { BusRequest request = new BusRequest(); request.Write((byte)(key + 33)); request.Write((byte)(value ? 1 : 0)); BusTransceiveResult result = sync ? Transceive(request, 0) : await TransceiveAsync(request, 0); if (!result.Success) { return(false); } else { if (value) { digitalOutputValue |= (UInt16)(1 << (int)key); } else { digitalOutputValue &= (UInt16)(~(1 << (int)key)); } return(true); } }
private async Task <ErrorCode> SetFloatValueAsyncInternal(byte key, float value, bool sync) { if (CommandSet == null) { Log.Logger?.Error("commandSet not populated", this); return(ErrorCode.DeviceNotInitialized); } if (key > CommandSet.Length || key == 0) { Log.Logger?.Error("key not within commandSetLength", this); return(ErrorCode.StellantriebInvalidKey); } CommandInternal command = commandSet[key - 1]; if (command.CommandLength == Command.Length.None || command.CommandLength == Command.Length.NoneAsText) { Log.Logger?.Error("%s: key not supported", this); return(ErrorCode.StellantriebInvalidKey); } if (command.AccessMode != Command.Access.WriteAccess) { Log.Logger?.Error("key not writable", this); return(ErrorCode.StellantriebValueReadOnly); } if (command.IsControlValue) { Log.Logger?.Error("value with key %u is not floating point, which was requested", this); return(ErrorCode.StellantriebInvalidKey); } command.bufferValid = false; BusRequest request = new BusRequest(); request.Write(key); switch (command.CommandLength) { case Command.Length.Byte: request.Write((byte)(value / command.Factor)); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); case Command.Length.Short: request.Write((Int16)(value / command.Factor)); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); case Command.Length.Long: request.Write((Int32)(value / command.Factor)); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); case Command.Length.Float: request.Write(value / command.Factor); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); default: return(ErrorCode.Unspecified); } }
/// <summary> /// Requests all available devices to enter a powerdown mode. /// </summary> /// <returns>Error code describing the result of the call.</returns> public ErrorCode GoToSleep() { var broadcast = new BusRequest(); broadcast.Write((byte)0x00); broadcast.Write((byte)0x06); return(SendBroadcast(broadcast)); }
/// <summary> /// Resets the bus address of all available devices. /// </summary> /// <returns>A task representing the asynchronous operation. /// Contains an error code describing the result of the call.</returns> public Task <ErrorCode> ResetAllBusAddressesAsync() { var broadcast = new BusRequest(); broadcast.Write((byte)0x00); broadcast.Write((byte)0x03); return(SendBroadcastAsync(broadcast)); }
/// <summary> /// Resets the bus address of all available devices. /// </summary> /// <returns>Error code describing the result of the call.</returns> public ErrorCode ResetAllBusAddresses() { var broadcast = new BusRequest(); broadcast.Write((byte)0x00); broadcast.Write((byte)0x03); return(SendBroadcast(broadcast)); }
/// <summary> /// Disable bus neighbours of available devices. /// </summary> /// <returns>A task representing the asynchronous operation. /// Contains an error code describing the result of the call.</returns> public Task <ErrorCode> DisableBusNeighboursAsync() { var broadcast = new BusRequest(); broadcast.Write((byte)0x00); broadcast.Write((byte)0x02); return(SendBroadcastAsync(broadcast)); }
/// <summary> /// Disable bus neighbours of available devices. /// </summary> /// <returns>Error code describing the result of the call.</returns> public ErrorCode DisableBusNeighbours() { var broadcast = new BusRequest(); broadcast.Write((byte)0x00); broadcast.Write((byte)0x02); return(SendBroadcast(broadcast)); }
/// <summary> /// Requests all available devices to enter a powerdown mode. /// </summary> /// <returns>A task representing the asynchronous operation. /// Contains an error code describing the result of the call.</returns> public Task <ErrorCode> GoToSleepAsync() { var broadcast = new BusRequest(); broadcast.Write((byte)0x00); broadcast.Write((byte)0x06); return(SendBroadcastAsync(broadcast)); }
/// <summary> /// Pings the device with the given UUID. /// </summary> /// <param name="uuid">UUID of the device to ping.</param> /// <returns>Error code describing the result of the call.</returns> public ErrorCode SendUuidPing(uint uuid) { var request = new BusRequest(); request.Write((byte)0x00); request.Write((byte)0x00); request.Write(uuid); return(TransceiveBroadcast(request, 0).TransportError); }
/// <summary> /// Pings the device with the given UUID. /// </summary> /// <param name="uuid">UUID of the device to ping.</param> /// <returns>A task representing the asynchronous operation. /// Contains an error code describing the result of the call.</returns> public async Task <ErrorCode> SendUuidPingAsync(uint uuid) { var request = new BusRequest(); request.Write((byte)0x00); request.Write((byte)0x00); request.Write(uuid); return((await TransceiveBroadcastAsync(request, 0)).TransportError); }
private async Task <ErrorCode> ResetBusAddressAsyncInternal(uint uuid, bool sync) { var request = new BusRequest(); request.Write((byte)0x00); request.Write((byte)0x00); request.Write(uuid); request.Write((byte)0x01); var result = sync ? TransceiveBroadcast(request, 0) : await TransceiveBroadcastAsync(request, 0); return(result.TransportError); }
/// <summary> /// Sends a broadcast to all devices which does nothing, but can be used to wake up devices which were put /// to sleep with GoToSleep. /// </summary> /// <returns>A task representing the asynchronous operation. /// Contains an error code describing the result of the call.</returns> public Task <ErrorCode> SendNopBroadcastAsync() { var request = new BusRequest(); request.Write((byte)0x00); return(SendBroadcastAsync(request)); }
/// <summary> /// Sends a broadcast to all devices which does nothing, but can be used to wake up devices which were put /// to sleep with GoToSleep. /// </summary> /// <returns>Error code describing the result of the call.</returns> public ErrorCode SendNopBroadcast() { var request = new BusRequest(); request.Write((byte)0x00); return(SendBroadcast(request)); }
private async Task <bool> SyncAsyncInternal(bool sync) { if (!initialized) { return(false); } if (syncSize > 2) { BusRequest request = new BusRequest(); request.Write((byte)0xFF); BusTransceiveResult result = sync ? Transceive(request, syncSize - 2) : await TransceiveAsync(request, syncSize - 2); if (!result.Success) { return(false); } else { if (numberOfDigitalInputs > 0) { digitalInputValue = result.Response.ReadUInt16(); } for (int i = 0; i < numberOfAnalogInputs; ++i) { analogInputs[i] = result.Response.ReadUInt16(); } } } return(true); }
private async Task <ErrorCode> PopulateCommandSetAsyncInternal(bool sync) { (ErrorCode commandSetLengthError, uint commandSetLength) = sync ? GetCommandsetLength() : await GetCommandsetLengthAsync(); if (commandSetLengthError != ErrorCode.Success) { Log.Logger?.Error("unable to retrieve command set length", this); return(commandSetLengthError); } if (commandSetLength == 0) { Log.Logger?.Error("command set length of 0 is unsupported", this); return(ErrorCode.StellantriebCommandLengthZero); } CommandInternal[] tempCommandSet = new CommandInternal[commandSetLength]; for (int i = 0; i < commandSetLength; ++i) { BusRequest request = new BusRequest(); request.Write((byte)(i + 1)); request.Write((byte)CommandKey.CommandInfoGet); request.Write((byte)CommandKey.CommandInfoGet); request.Write((byte)CommandKey.CommandInfoGet); BusTransceiveResult result = sync ? Transceive(request, 6) : await TransceiveAsync(request, 6); if (!result.Success) { return(result.TransportError); } tempCommandSet[i] = new CommandInternal( (Command.Access)result.Response.ReadByte(), (Command.Length)result.Response.ReadByte(), result.Response.ReadSingle()); } commandSet = tempCommandSet; return(ErrorCode.Success); }
private async Task <(ErrorCode, uint uuid)> SendBroadcastPingAsyncInternal(bool sync) { var request = new BusRequest(); request.Write((byte)0x00); request.Write((byte)0x00); var result = sync ? TransceiveBroadcast(request, 4) : await TransceiveBroadcastAsync(request, 4); if (result.Success) { return(result.TransportError, result.Response.ReadUInt32()); } else { return(result.TransportError, 0); } }
private async Task <(ErrorCode, int busAddress)> ReceiveBusAddressAsyncInternal(uint uuid, bool sync) { var request = new BusRequest(); request.Write((byte)0x00); request.Write((byte)0x00); request.Write(uuid); request.Write((byte)0x00); var result = sync ? TransceiveBroadcast(request, 1) : await TransceiveBroadcastAsync(request, 1); if (result.Success) { return(result.TransportError, result.Response.ReadByte()); } else { return(result.TransportError, 0); } }
private async Task <(ErrorCode, byte)> GetCommandNameLengthAsyncInternal(byte key, bool sync) { BusRequest request = new BusRequest(); request.Write(key); request.Write((byte)CommandKey.CommandInfoGetNameLength); request.Write((byte)CommandKey.CommandInfoGetNameLength); request.Write((byte)CommandKey.CommandInfoGetNameLength); BusTransceiveResult result = sync ? Transceive(request, 1) : await TransceiveAsync(request, 1); if (result.Success) { return(ErrorCode.Success, result.Response.ReadByte()); } else { return(result.TransportError, 0); } }
private async Task <ErrorCode> RequestBusAssertionAsyncInternal(int searchmaskLength, uint searchAddress, bool onlyDevicesWithoutAddress, bool sync) { if (searchmaskLength < 0 || searchmaskLength > 32) { return(ErrorCode.InvalidArgument); } var request = new BusRequest(); request.Write((byte)0x00); if (onlyDevicesWithoutAddress) { request.Write((byte)0x05); } else { request.Write((byte)0x04); } request.Write((byte)searchmaskLength); int searchAddressLength = 0; if (searchAddress >= (1 << 24)) { searchAddressLength = 4; } else if (searchAddress >= (1 << 16)) { searchAddressLength = 3; } else if (searchAddress >= (1 << 8)) { searchAddressLength = 2; } else if (searchAddress >= (1 << 0)) { searchAddressLength = 1; } for (int i = 0; i < searchAddressLength; ++i) { request.Write((byte)(searchAddress >> (i * 8))); } // Console.WriteLine("search mask length " + searchmaskLength + " search address " + BaseDevice.FormatUuid(searchAddress)); var result = sync ? TransceiveBroadcast(request, 0, attempts: 1) : await TransceiveBroadcastAsync(request, 0, attempts : 1); switch (result.TransportError) { case ErrorCode.Success: case ErrorCode.TransportReceptionMissingDataError: case ErrorCode.TransportChecksumError: return(ErrorCode.Success); case ErrorCode.TransportReceptionNoAnswerError: return(ErrorCode.NoAssertionDetected); } return(result.TransportError); }
private async Task <ErrorCode> SetIntValueAsyncInternal(byte key, int value, bool sync) { if (CommandSet == null) { Log.Logger?.Error("commandSet not populated", this); return(ErrorCode.DeviceNotInitialized); } if (key > CommandSet.Length || key == 0) { Log.Logger?.Error("key not within commandSetLength", this); return(ErrorCode.StellantriebInvalidKey); } CommandInternal command = commandSet[key - 1]; if (command.CommandLength == Command.Length.None || command.CommandLength == Command.Length.NoneAsText) { Log.Logger?.Error("%s: key not supported", this); return(ErrorCode.StellantriebInvalidKey); } if (command.AccessMode != Command.Access.WriteAccess) { Log.Logger?.Error("key not writable", this); return(ErrorCode.StellantriebValueReadOnly); } if (!command.IsControlValue) { Log.Logger?.Error("value with key %u is floating point, which was not requested", this); return(ErrorCode.StellantriebInvalidKey); } command.bufferValid = false; BusRequest request = new BusRequest(); request.Write(key); switch (command.CommandLength) { case Command.Length.Byte: request.Write((byte)(value)); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); case Command.Length.Short: request.Write((short)(value)); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); case Command.Length.Long: request.Write((Int32)(value)); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); case Command.Length.Float: // this actually does not make sense. But the problem ist that the configuration is // invalid in the first place. request.Write((float)value); return(sync ? Transceive(request, 0).TransportError : (await TransceiveAsync(request, 0)).TransportError); default: return(ErrorCode.Unspecified); } }
private async Task <(ErrorCode, int)> GetIntValueAsyncInternal(byte key, bool sync) { if (CommandSet == null) { Log.Logger?.Error("commandSet not populated", this); return(ErrorCode.DeviceNotInitialized, 0); } if (key > CommandSet.Length || key == 0) { Log.Logger?.Error("key not within commandSetLength", this); return(ErrorCode.StellantriebInvalidKey, 0); } CommandInternal command = commandSet[key - 1]; if (command.CommandLength == Command.Length.None || command.CommandLength == Command.Length.NoneAsText) { Log.Logger?.Error("%s: key not supported", this); return(ErrorCode.StellantriebInvalidKey, 0); } if (!command.IsControlValue) { Log.Logger?.Error("value with key %u is not a control value, which was requested", this); return(ErrorCode.StellantriebInvalidKey, 0); } if (!command.bufferValid) { BusRequest request = new BusRequest(); request.Write(key); switch (command.CommandLength) { case Command.Length.Byte: { BusTransceiveResult result = sync ? Transceive(request, 1) : await TransceiveAsync(request, 1); if (!result.Success) { return(result.TransportError, 0); } else { command.intBuffer = (int)result.Response.ReadByte(); } break; } case Command.Length.Short: { BusTransceiveResult result = sync ? Transceive(request, 2) : await TransceiveAsync(request, 2); if (!result.Success) { return(result.TransportError, 0); } else { command.intBuffer = (int)result.Response.ReadInt16(); } break; } case Command.Length.Long: { BusTransceiveResult result = sync ? Transceive(request, 4) : await TransceiveAsync(request, 4); if (!result.Success) { return(result.TransportError, 0); } else { command.intBuffer = (int)result.Response.ReadInt32(); } break; } case Command.Length.Float: { BusTransceiveResult result = sync ? Transceive(request, 4) : await TransceiveAsync(request, 4); if (!result.Success) { return(result.TransportError, 0); } else { // this actually does not make sense. But the problem ist that the configuration is // invalid in the first place. command.intBuffer = (int)result.Response.ReadSingle(); } break; } default: return(ErrorCode.Unspecified, 0); } // protocol definition: writable values are bufferable if (command.AccessMode == Command.Access.WriteAccess) { command.bufferValid = true; } } return(ErrorCode.Success, command.intBuffer); }