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

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

Usage:

GetServerItem <ip_address>
// e.g.:
GetServerItem 10.0.0.102
Note
Another possibility is via the device API.
See also: Sample 01_BaosDeviceInformation.cpp
//
// 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
{
std::uint16_t decodeServerItem_uint16(const ServerItem& serverItem)
{
return serverItem.data.at(0) << 8 | serverItem.data.at(1);
}
bool decodeServerItem_bool(const ServerItem& serverItem)
{
bool value = (serverItem.data.at(0) & 0x01) ? true : false;
return value;
}
std::string decodeServerItem_string(const ServerItem& serverItem)
{
return std::string(serverItem.data.begin(), serverItem.data.end());
}
} // end anonymous namespace
/******************************************
** Main
*******************************************/
int main(int argc, char* argv[])
{
try
{
std::cout << "******************************************" << std::endl;
std::cout << "GetServerItem 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);
// Reads some server items (single item per request)
// DeviceFriendlyName
auto serverItem = connection.getServerItem(ServerItemId::DeviceFriendlyName);
if (serverItem.has_value())
{
std::cout << "Device friendly name: " << decodeServerItem_string(*serverItem) << std::endl;
}
else
{
std::cerr << "Unable to get serveritem ServerItemId::DeviceFriendlyName" << std::endl;
}
// ManufactureCodeApp
serverItem = connection.getServerItem(ServerItemId::ManufactureCodeApp);
if (serverItem.has_value())
{
std::cout << "Manufacturer code app.: " << decodeServerItem_uint16(*serverItem) << std::endl;
}
else
{
std::cerr << "Unable to get serveritem ServerItemId::ManufactureCodeApp" << std::endl;
}
// ProgrammingMode
serverItem = connection.getServerItem(ServerItemId::ProgrammingMode);
if (serverItem.has_value())
{
std::cout << "Programming mode: " << std::boolalpha << decodeServerItem_bool(*serverItem) << std::endl;
}
else
{
std::cerr << "Unable to get serveritem ServerItemId::ProgrammingMode" << std::endl;
}
std::cout << std::endl;
// Reads multiple server items (multiple items per request)
// start = FirmwareVersion; number of = 10
std::cout << "Reading serveritems 3 - 12:" << std::endl;
const ServerItems serverItems = connection.getServerItems(3, 10);
// print server item description and it's data buffer as hex string
for (const ServerItem& item : serverItems)
{
const std::string description = serverItemIdToString(item.id);
std::cout << description << " data: ";
std::cout.fill('0');
std::cout << std::hex << std::uppercase;
for (auto& value : item.data)
{
std::cout << std::setw(2) << static_cast<int>(value) << " ";
}
std::cout << std::nouppercase << std::dec << std::endl;
}
}
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
BAOSLIB_EXPORT std::string serverItemIdToString(ServerItemId sid)
Convert the enum value to a human readable string.
std::vector< ServerItem > ServerItems
Type for a list of ServerItems.
Definition: Defines.h:421
Global BAOS sdk namespace.
Definition: config.h:62
void setBaosLogLevel(wzcpp::LogLevel level)