-
Notifications
You must be signed in to change notification settings - Fork 2
/
lan_interface.cpp
189 lines (152 loc) · 3.93 KB
/
lan_interface.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#ifdef __linux__
#include <sys/socket.h>
#else
#include <winsock2.h>
#endif
#include <libusb-1.0/libusb.h>
#include <arpa/inet.h>
#include <QGraphicsScene>
#include "usb_interface.h"
#include "lan_interface.h"
#include "owoncontrol.h"
#include "consetting.h"
#include "main.h"
#include <unistd.h>
/********************/
/* Global Variables */
/********************/
int socket_desc;
int con_status;
struct sockaddr_in server;
/********************/
/* Code Starts Here */
/********************/
int connect_lan(void)
{
QByteArray byteArray;
const char * text;
QString success_str;
//Create socket
socket_desc = socket(AF_INET , SOCK_STREAM , 0); //IPv4 / TCP / IP Protocol
if (socket_desc == -1)
{
window_ptr->append_log("Could not create socket");
return(1);
}
/* convert QString to const char */
byteArray = cest_frm_ptr->get_ip_address().toUtf8();
text = byteArray.constData();
server.sin_family = AF_INET;
server.sin_addr.s_addr = inet_addr(text);
server.sin_port = htons( cest_frm_ptr->get_port() );
//server.sin_addr.s_addr = inet_addr("192.168.1.72");
//server.sin_port = htons( 3000 );
//Connect to remote server
con_status = connect(socket_desc , (struct sockaddr *)&server , sizeof(server));
if ( con_status == -1 )
{
window_ptr->append_log("LAN Connection Error");
return (1);
}
success_str = "LAN Connected to " + cest_frm_ptr->get_ip_address();
window_ptr->append_log(success_str);
return(0);
}
int disconnect_lan(void)
{
close(socket_desc);
con_status = -1;
window_ptr->append_log ("Disconnect LAN successful");
return(0);
}
int send_lan_data(char* data, int length)
{
int tx_count = 0U;
if(con_status != -1)
{
//Send some data
tx_count = send(socket_desc ,data , length , 0);
if( tx_count < 0)
{
window_ptr->append_log("Send failed");
return (1);
}
else
{
if(tx_count != length)
{
window_ptr->append_log("Byte Count Mismatch");
return (1);
}
}
}
else
{
window_ptr->append_log ("Failed to send command, Is the LAN connetion active?");
return(1);
}
return(0);
}
int get_lan_data(char* data, int length, int * transferred)
{
if(con_status != -1)
{
*transferred = recv(socket_desc, data, length, 0);
if( *transferred < 0)
{
window_ptr->append_log ("Read Failed");
return (1);
}
}
else
{
window_ptr->append_log ("Failed to read command, Is the LAN connetion active?");
return (1);
}
return (0);
}
int get_lan_bmp(void)
{
int allocated = 0;
int transferred = 0;
int downloaded = 0;
union
{
char data[12];
owon_start_response_st response;
}tempdata_u;
/* Start the USB transfer */
send_lan_data(start_bmp, sizeof(start_bmp));
/* Get the response */
if( get_lan_data(&tempdata_u.data[0], sizeof(tempdata_u), &transferred) )
{
return(1);
}
/* Clear previous buffer if exists */
if(NULL != bmp_buffer)
{
free(bmp_buffer);
bmp_buffer = nullptr;
}
/* Allocate memory to receive bmp */
bmp_buffer = (char*) malloc(tempdata_u.response.length);
allocated = tempdata_u.response.length;
//printf("Allocated: %d\n",allocated);
if(NULL == bmp_buffer)
{
window_ptr->append_log ("Failed to allocate Buffer!");
return -1;
}
/* Get the data from the oscilloscope */
do
{
if( get_lan_data(bmp_buffer + downloaded, allocated, &transferred) )
{
return (1);
}
downloaded += transferred;
//printf("Running download: %d\n",downloaded);
} while (allocated > downloaded);
//printf("Downloaded: %d\n",downloaded);
return 0;
}