BAOS SDK v2  1.0.1
An SDK providing access to IP-BAOS devices through BAOS binary protocol version 2.x
SetServerItem.cpp

This sample shows how to write server items via the connection API

Usage:

SetServerItem <ip_address>
// e.g.:
SetServerItem 10.0.0.102
//
// Copyright (c) 2002-2023 WEINZIERL ENGINEERING GmbH
// All rights reserved.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY DAMAGES OR OTHER LIABILITY,
// WHETHER IN CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE
//
#include "Helper.h"
#include <baos/Sdk.h>
#include <wzcpp/features/utils.h>
#include <iostream>
#include <memory>
using namespace wz::baos;
using namespace wz::baos::ip;
using namespace wz::baos::protocol;
/******************************************
** Anonymous namespace
*******************************************/
namespace
{
Buffer encodeServerItem_bool(bool value)
{
Buffer data(1, value ? 0x01 : 0x00);
return data;
}
Buffer encodeServerItem_string(const std::string& value)
{
Buffer data(value.begin(), value.end());
data.resize(30, 0);
return data;
}
} // end anonymous namespace
/******************************************
** Main
*******************************************/
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "SetServerItem sample" << std::endl;
std::cout << "******************************************" << std::endl;
std::cout << std::endl;
// Reduce log messages
setBaosLogLevel(wzcpp::LogLevel::warn);
// The CommandLineOptions::parse function
// validates the arguments and handles the help command
CommandLineOptions options;
options.parse(argc, argv);
if (options.wasHelpdisplayed())
{
return 0;
}
// Get the ip address from the parsed command line options
const std::string ipAddress = options.getIpAddress();
std::cout << " Connection requested for " << ipAddress << std::endl;
// Create a TPC/IP connection to the remote BAOS device
BaosTcpConnection connection;
connection.open(ipAddress);
// Write DeviceFriendlyName
// back it up, first
const auto backup = connection.getServerItem(ServerItemId::DeviceFriendlyName);
if (backup.has_value())
{
// write new name
ServerItem deviceNameItem(ServerItemId::DeviceFriendlyName, 30, encodeServerItem_string("Hello BAOS"));
connection.setServerItem(deviceNameItem);
// restore name from backup
connection.setServerItem(*backup);
}
else
{
std::cerr << "Unable to read ServerItem ServerItemId::DeviceFriendlyName" << std::endl;
}
// Write ProgrammingMode
// switch prog mode on
ServerItem progModeItem(ServerItemId::ProgrammingMode, 1, encodeServerItem_bool(true));
connection.setServerItem(progModeItem);
// switch prog mode off
progModeItem.data = encodeServerItem_bool(false);
connection.setServerItem(progModeItem);
}
catch (const std::exception& e)
{
std::cerr << "Failed: " << e.what() << std::endl;
return -1;
}
return 0;
}
Specialization for a TCP IP v4 BAOSConnection.
Global BAOS protocol defines and types.
Global BAOS SDK function and options.
Definition: Helper.h:23
Groups BAOS binary protocol specific types , defines and classes for Indications, Responses etc.
Definition: Defines.h:32
Global BAOS sdk namespace.
Definition: config.h:62
std::vector< std::uint8_t > Buffer
An alias type for a byte buffer.
Definition: config.h:64
void setBaosLogLevel(wzcpp::LogLevel level)