libvisiontransfer  6.4.0
parametertransfer.cpp
1 /*******************************************************************************
2  * Copyright (c) 2019 Nerian Vision GmbH
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *******************************************************************************/
14 
15 #include "visiontransfer/parametertransfer.h"
16 #include "visiontransfer/exceptions.h"
17 #include "visiontransfer/internalinformation.h"
18 #include <cstring>
19 #include <string>
20 #include <iostream>
21 
22 using namespace std;
23 using namespace visiontransfer;
24 using namespace visiontransfer::internal;
25 
26 constexpr int ParameterTransfer::SOCKET_TIMEOUT_MS;
27 
28 ParameterTransfer::ParameterTransfer(const char* address, const char* service)
29  : socket(INVALID_SOCKET) {
30 
31  Networking::initNetworking();
32  addrinfo* addressInfo = Networking::resolveAddress(address, service);
33 
34  socket = Networking::connectTcpSocket(addressInfo);
35  Networking::setSocketTimeout(socket, SOCKET_TIMEOUT_MS);
36  checkProtocolVersion();
37 
38  freeaddrinfo(addressInfo);
39 }
40 
41 ParameterTransfer::~ParameterTransfer() {
42  if(socket != INVALID_SOCKET) {
43  Networking::closeSocket(socket);
44  }
45 }
46 
47 void ParameterTransfer::recvData(unsigned char* dest, int length) {
48  int bytesReceived = recv(socket, reinterpret_cast<char*>(dest), length, 0);
49  if(bytesReceived < 0) {
50  TransferException ex("Error receiving network packet: " + string(strerror(errno)));
51  throw ex;
52  } else if(bytesReceived < length) {
53  throw TransferException("Received too short network packet!");
54  }
55 }
56 
57 void ParameterTransfer::checkProtocolVersion() {
58  unsigned int version = 0;
59  recvData(reinterpret_cast<unsigned char*>(&version), sizeof(version));
60 
61  if(ntohl(version) != static_cast<unsigned int>(InternalInformation::CURRENT_PROTOCOL_VERSION)) {
62  throw ParameterException("Protocol version mismatch! Expected "
63  + std::to_string(InternalInformation::CURRENT_PROTOCOL_VERSION)
64  + " but received " + std::to_string(ntohl(version)));
65  }
66 }
67 
68 void ParameterTransfer::readParameter(unsigned char messageType, int32_t id, unsigned char* dest, int length) {
69  if(length > 8) {
70  throw ParameterException("Parameter type size mismatch!");
71  }
72 
73  unsigned int networkId = htonl(id);
74  unsigned char messageBuf[13];
75  memset(messageBuf, 0, sizeof(messageBuf));
76 
77  messageBuf[0] = messageType;
78  memcpy(&messageBuf[1], &networkId, 4);
79 
80  int written = send(socket, reinterpret_cast<char*>(messageBuf), sizeof(messageBuf), 0);
81  if(written != sizeof(messageBuf)) {
82  TransferException ex("Error sending parameter read request: " + string(strerror(errno)));
83  throw ex;
84  }
85 
86  unsigned char replyBuf[8];
87  recvData(replyBuf, sizeof(replyBuf));
88  memcpy(dest, replyBuf, length);
89 }
90 
91 template<typename T>
92 void ParameterTransfer::writeParameter(unsigned char messageType, int32_t id, T value) {
93  static_assert(sizeof(T) <= 8, "Parameter type musst be smaller or equal to 8 bytes");
94 
95  unsigned int networkId = htonl(id);
96  unsigned char messageBuf[13];
97 
98  memset(messageBuf, 0, sizeof(messageBuf));
99  messageBuf[0] = messageType;
100  memcpy(&messageBuf[1], &networkId, 4);
101  memcpy(&messageBuf[5], &value, sizeof(value));
102 
103  int written = send(socket, reinterpret_cast<char*>(messageBuf), sizeof(messageBuf), 0);
104  if(written != sizeof(messageBuf)) {
105  TransferException ex("Error sending parameter write request: " + string(strerror(errno)));
106  throw ex;
107  }
108 
109  unsigned char replyBuf[8];
110  recvData(replyBuf, sizeof(replyBuf));
111 
112  if(replyBuf[0] == 0 && replyBuf[1] == 0 && replyBuf[2] == 0 && replyBuf[3] == 0) {
113  throw ParameterException("Unable to write parameter");
114  }
115 }
116 
118  unsigned int data;
119  readParameter(MESSAGE_READ_INT, id, reinterpret_cast<unsigned char*>(&data), sizeof(data));
120  return static_cast<int>(ntohl(data));
121 }
122 
124  double data;
125  readParameter(MESSAGE_READ_DOUBLE, id, reinterpret_cast<unsigned char*>(&data), sizeof(data));
126  return data;
127 }
128 
130  unsigned int data;
131  readParameter(MESSAGE_READ_BOOL, id, reinterpret_cast<unsigned char*>(&data), sizeof(data));
132  return (data != 0);
133 }
134 
135 void ParameterTransfer::writeIntParameter(int32_t id, int32_t value) {
136  writeParameter(MESSAGE_WRITE_INT, id, htonl(static_cast<uint32_t>(value)));
137 }
138 
139 void ParameterTransfer::writeDoubleParameter(int32_t id, double value) {
140  writeParameter(MESSAGE_WRITE_DOUBLE, id, value);
141 }
142 
143 void ParameterTransfer::writeBoolParameter(int32_t id, int32_t value) {
144  writeParameter(MESSAGE_WRITE_BOOL, id, htonl(static_cast<uint32_t>(value)));
145 }
146 
147 
148 
void writeDoubleParameter(int32_t id, double value)
Writes a double precision floating point value to a parameter of the parameter server.
bool readBoolParameter(int32_t id)
Reads a boolean value from the parameter server.
void writeBoolParameter(int32_t id, int32_t value)
Writes a boolean value to a parameter of the parameter server.
Exception class that is used for all parameter-related exceptions.
Definition: exceptions.h:41
double readDoubleParameter(int32_t id)
Reads a double precision floating point value from the parameter server.
int readIntParameter(int32_t id)
Reads an integer value from the parameter server.
void writeIntParameter(int32_t id, int32_t value)
Writes an integer value to a parameter of the parameter server.
Exception class that is used for all transfer exceptions.
Definition: exceptions.h:33
Nerian Vision Technologies