-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathAPRS_Connector.cpp
278 lines (251 loc) · 7.53 KB
/
APRS_Connector.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
/*
* File: APRS_Connector.cpp
* Author: paulalexander66
*
* Created on 10. Mai 2017, 19:33
*/
#include <valarray>
#include "APRS_Connector.h"
using namespace std;
APRS_Connector::APRS_Connector() {
m_Path = "";
slen=sizeof(si_other);
m_Port=0;
m_isACK=0;
}
APRS_Connector::APRS_Connector(const APRS_Connector& orig) {
}
APRS_Connector::~APRS_Connector() {
}
//Setting the Port and Server of the Python App
void APRS_Connector::setPort(int Port){
m_Port=Port;
}
int APRS_Connector::sendudp(char *msg, int length) {
//send the update
inet_aton(Python_Server , &si_other.sin_addr);
if (sendto(s, (char *)msg, length, 0 , (struct sockaddr *) &si_other, slen)==-1)
{
return -1;
}
return 0;
}
/* Return
0 success
-1 failure
1 No Error but did not receive paket
*/
int APRS_Connector::receiveUDP(char* Receiv,int Len){
//printf("in receiveUDP\n");
int iRec=-1;
//int iRec=recv (int __fd, void *__buf, size_t __n, int __flags)
try{
iRec=recv(s, Receiv, Len, MSG_DONTWAIT);
}catch(...) {
//printf("Error recceiving from UDP \n");
iRec=-1;
}
//printf("Received Return Code %d \n",iRec);
if(iRec>0){
//printf("Received message = %s \n", Receiv);
iRec=0;
}else{
//printf("Received message = 0 !! But no Error");
iRec=1;
}
return iRec;
}
int APRS_Connector::proofACK(char* buffer, int size){
int iReturn=0;
char Temp[size];
if (buffer[0]=='1'){
int i;
for (i=1;i<size;i++){
Temp[i-1]=buffer[i];
}
Temp[i-1]='\0';
//printf("new string %s\n",Temp);
strcpy(buffer,Temp);
iReturn=1;
}
return iReturn;
}
/*Pings the Aprs server
* Return
0 success
-1 failure
1 connection is there but some packets were lost
*/
int APRS_Connector::APRS_ping(string S_Server) {
int iReturn = -1;
//ping -c 4 aprs.fi
string s_ping = "ping -c 4 ";
s_ping.append(S_Server);
try {
const string ping_Result = exec(s_ping.c_str());
//printf("ping Result = %s",ping_Result.c_str());
int Beg_iFound = ping_Result.find("received,") + 9;
int End_iFound = ping_Result.find(" packet loss,");
int iError = ping_Result.find(" +4 errors, ");
if (iError > 0) {
//printf("%s is NOT availibel\n", S_Server.c_str());
iReturn = -1;
} else {
string sValue = ping_Result.substr(Beg_iFound, End_iFound - Beg_iFound);
//printf("ping Strip Result = %s\n",sValue.c_str());
if (sValue.compare(" 0%") == 0) {
//printf("%s is availibel \n", S_Server.c_str());
iReturn = 0;
} else if (sValue.compare(" 100%") == 0) {
//printf("%s is NOT availibel\n", S_Server.c_str());
iReturn = -1;
} else {
printf("%s availibel with %s packet loss\n", S_Server.c_str(), sValue.c_str());
iReturn = 1;
}
}
} catch (...) {
printf("Error ping Server %s \n", S_Server.c_str());
}
return iReturn;
}
/*Starting Aprs Connector
* Return
0 success
-1 failure
*/
int APRS_Connector::startAPRS_Connector(string PythonApp) {
int iReturn = -1;
//string s_pwd = "pwd";
//const string ping_Result = exec(s_pwd.c_str());
//int iLen = ping_Result.length();
//printf("pwd Result = %s", ping_Result.c_str());
m_Path.append("python ");
//m_Path.append(ping_Result.substr(0, iLen - 1));
//m_Path.append("/");
//m_Path.append(m_PythonApp);
m_Path.append(PythonApp);
m_Path.append(" &");
//printf("Path to python app %s \n", m_Path.c_str());
if(m_Port==0){
//printf("Port of APRS Connector not set \n");
return -1;
}
memset((char *) &si_other, 0, sizeof(si_other));
si_other.sin_family = AF_INET;
si_other.sin_port = htons(m_Port);
try {
int a = system(m_Path.c_str());
if(a < 0){
iReturn=-1;
}else{
//printf("APRS Connector started\n");
iReturn = 0;
}
} catch (...) {
//printf("Error starting APRS Connector \n");
iReturn = -1;
}
if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1)
{
iReturn=-1;
}
return iReturn;
}
/*is Aprs Connector python app running
* Return
0 success
-1 failure
*/
int APRS_Connector::process_activ(){
int iReturn=-1;
string s_process = "ps aux | grep python";
const string process_Result = exec(s_process.c_str());
//int iLen = process_Result.length();
//printf("process Result = %s", process_Result.c_str());
//printf("Path search process = %s\n",m_Path.substr(0,m_Path.length()-2).c_str());
int iFound =process_Result.find(m_Path.substr(0,m_Path.length()-2));
if (iFound > 0 ){
iReturn = 0;
}
return iReturn;
}
/*is Aprs Connector python app running
* Return
0 success
-1 failure
*/
int APRS_Connector::socket_activ(){
int iReturn=-1;
string s_socket = "netstat -tn | grep 14580 | awk '{ print $6 }'";
const string socket_Result = exec(s_socket.c_str());
// int iLen = socket_Result.length();
// printf("socket Result = %s %d\n", socket_Result.c_str(), iLen);
// printf("Path search process = %s\n",m_Path.substr(0,m_Path.length()-2).c_str());
int iFound =socket_Result.find("ESTABLISHED");
// printf("Found %d\n",iFound);
if (!iFound){
iReturn = 0;
}
return iReturn;
}
std::string APRS_Connector::exec(const char* cmd) {
char buffer[128];
std::string result = "";
FILE* pipe = popen(cmd, "r");
if (!pipe) throw std::runtime_error("popen() failed!");
try {
while (!feof(pipe)) {
if (fgets(buffer, 128, pipe) != NULL)
result += buffer;
}
} catch (...) {
pclose(pipe);
throw;
}
pclose(pipe);
return result;
}
string APRS_Connector::getIP_eth0(){
string eth0="";
string s_process = "ifconfig eth0";
const string process_Result = exec(s_process.c_str());
//printf("%s \n",process_Result.c_str());
try{
int iTest=process_Result.find(" inet addr:");
//printf("Result Test %d \n",iTest);
if(iTest<0){return eth0;} //No Ip Address found
int Beg_iFound = process_Result.find(" inet addr:") + 11;
int End_iFound = process_Result.find(" Bcast:");
string sValue = process_Result.substr(Beg_iFound, End_iFound - Beg_iFound);
//printf("Result %s \n",sValue.c_str());
return sValue;
}catch (...) {
printf("Error reading eth0 \n");
}
return eth0;
}
string APRS_Connector::getIP_wlan0(){
string eth0="";
string s_process = "ifconfig wlan0";
const string process_Result = exec(s_process.c_str());
//printf("%s \n",process_Result.c_str());
try{
int iTest=process_Result.find(" inet ");
//printf("Result Test %d \n",iTest);
if(iTest<0){return eth0;} //No Ip Address found
int Beg_iFound = process_Result.find(" inet ") + 6;
int End_iFound = process_Result.find(" netmask");
string sValue = process_Result.substr(Beg_iFound, End_iFound - Beg_iFound);
printf("Wlan0 Result %s \n",sValue.c_str());
return sValue;
}catch (...) {
printf("Error reading wlan0 \n");
}
return eth0;
}