AgIsoStack++
A control-function-focused implementation of the major ISOBUS and J1939 protocols
Loading...
Searching...
No Matches
socketcand_windows_network_client.cpp
Go to the documentation of this file.
1//================================================================================================
8//================================================================================================
10
12
13namespace isobus
14{
16 name(deviceName)
17 {
18 initialize();
19 }
20
21 SocketCANdNetworkInterface::SocketCANdNetworkInterface(std::string deviceName, const std::string ipAddress, std::uint16_t port) :
22 name(deviceName),
23 targetIP(ipAddress),
24 targetPort(port)
25 {
26 initialize();
27 }
28
30 {
31 int result = WSAStartup(MAKEWORD(2, 2), &wsa_data);
32 if (result != 0)
33 {
34 CANStackLogger::critical("[HW]: Cannot initialize WinSock.");
35 WSACleanup();
36 }
37 }
38
40 {
41 return valid;
42 }
43
45 {
46 return name;
47 }
48
50 {
51 return targetIP;
52 }
53
57
59 {
60 if (!openCalled)
61 {
62 openCalled = true;
63 mainSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
64 beaconSocket = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
65
66 if (INVALID_SOCKET == mainSocket)
67 {
68 CANStackLogger::critical("[HW]: Error: cannot create socket, error code: %d", WSAGetLastError());
69 WSACleanup();
70 }
71 else
72 {
73 memset(&serverDefinition, 0, sizeof(serverDefinition));
74 serverDefinition.sin_addr.s_addr = inet_addr(targetIP.c_str());
75 serverDefinition.sin_family = AF_INET;
76 serverDefinition.sin_port = htons(targetPort);
77 }
78 }
79 else
80 {
81 CANStackLogger::critical("[HW]: Can't open %s because it is already open or there was a socket error.", name);
82 }
83 }
84
85 bool SocketCANdNetworkInterface::read_frame(isobus::HardwareInterfaceCANFrame &canFrame)
86 {
87 int result = recv(sock, buf, count, 0);
88
89 return result > 0;
90 }
91
92 bool SocketCANdNetworkInterface::write_frame(const isobus::HardwareInterfaceCANFrame &canFrame)
93 {
94 bool retVal = false;
95
96 return retVal;
97 }
98} // namespace isobus
A class that acts as a logging sink. The intent is that someone could make their own derived class of...
static void critical(const std::string &logText)
Logs a string to the log sink with Critical severity. Wraps sink_CAN_stack_log.
void close() override
Closes the connection.
bool write_frame(const isobus::HardwareInterfaceCANFrame &canFrame) override
Writes a frame to the bus (synchronous)
void open() override
Connects to the server.
const std::string get_connected_ip_address() const
Returns the IP that the plugin is connected to.
SocketCANdNetworkInterface(std::string deviceName)
Constructor for the socket CAN driver.
bool read_frame(isobus::HardwareInterfaceCANFrame &canFrame) override
Returns a frame from the hardware (synchronous), or false if no frame can be read.
bool get_is_valid() const override
Returns if the connection is valid.
void initialize()
Initializes some class variables.
std::string get_device_name() const
Returns the device name the driver is using.
This namespace encompasses all of the ISO11783 stack's functionality to reduce global namespace pollu...
A windows hardware plugin for connecting to and using a socketcand server.