This repository was archived by the owner on Sep 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
203 lines (170 loc) · 5.65 KB
/
main.cpp
File metadata and controls
203 lines (170 loc) · 5.65 KB
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
// --------------------------------------------------------------------------------------------------------------
// main.cpp
//
// Copyright (C) 2016 par GeniALE.
// Marc-Andre Guimond <guimond.marcandre@gmail.com>.
// Tous droits reserves.
//
// Ce fichier est encode en UTF-8.
// --------------------------------------------------------------------------------------------------------------
// Standard includes.
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <cstdlib>
#include <errno.h>
// App includes.
#include "BBConfig.h"
#include "BBNetworkDiscovery.h"
// Lib includes.
#include "UDPClient.h"
// Common includes.
#include "Host.h"
#include "version.h"
#include "utils.h"
using namespace std;
// --------------------------------------------------------------------------------------------------------------
// Private constants.
// Location ID offset
#define kLocationIDOffset 17
// Subnet.
#define kSubnet "192.168.2."
// Max hosts to discover.
#define kNetworkDevicesMaxHosts 5
// BeagleBrew UDP traffic port.
#define kUDPPortBB 34923
// Config file parsing tables.
static NameID_t gLocationTable[] = kConfigDeviceMapping_LocationIDTable;
// --------------------------------------------------------------------------------------------------------------
// Private data types.
typedef struct
{
unsigned int locationID;
const char* name;
} LocationIDTable_t;
// --------------------------------------------------------------------------------------------------------------
// Private variables.
int gDiscoveryList[kNetworkDevicesMaxHosts];
// --------------------------------------------------------------------------------------------------------------
static void PrintHelp(char* inArg0)
{
printf("Usage:\n"
"\t%s [option]\n"
"Options:\n"
"\t-ver\t\tGet version info.\n"
"\t-vcs\t\tGet VCS info.\n"
"\t-t\t\tNumber of discovery scan iterations.\n", inArg0);
}
// --------------------------------------------------------------------------------------------------------------
static bool BBNetCheckDiscoveryList(int inLocationID)
{
bool isNotInList = true;
for (uint8_t deviceIdx = 0; deviceIdx < kNetworkDevicesMaxHosts; deviceIdx ++)
{
if (gDiscoveryList[deviceIdx] == inLocationID)
{
// Device already in list.
isNotInList = false;
break;
}
}
return isNotInList;
}
// --------------------------------------------------------------------------------------------------------------
static bool BBNetAddDeviceToList(int inDeviceID)
{
static int deviceListIdx = 0;
bool isSuccess = false;
if (BBNetCheckDiscoveryList(inDeviceID))
{
gDiscoveryList[deviceListIdx ++] = inDeviceID;
isSuccess = true;
}
else
{
isSuccess = false;
}
return isSuccess;
}
// ----------------------------------------------------------------------------
static const char* DeviceLocationGetName(unsigned int inID)
{
NameID_t* inNameIDTable = gLocationTable;
for (; inNameIDTable->ID != kDeviceLocationID_Unknown; inNameIDTable ++)
{
if (inNameIDTable->ID == inID)
{
break;
}
}
return inNameIDTable->name;
}
// --------------------------------------------------------------------------------------------------------------
int main(int argc, char** argv)
{
if (argc < 2)
{
PrintHelp(argv[0]);
}
else
{
if (strncmp("-ver", argv[1], strlen("-ver")) == 0)
{
printf("%s\n", versionID);
exit(1);
}
else if (strncmp("-vcs", argv[1], strlen("-vcs")) == 0)
{
printf("%s\n", versionVCS);
exit(1);
}
else if (strncmp("-t", argv[1], strlen("-t")) == 0)
{
if (argc < 3)
{
PrintHelp(argv[0]);
exit(-1);
}
int socketFileDescriptor = UDPBindSocket(kUDPPortBB);
if (socketFileDescriptor < 0)
{
PrintMessage(__FILENAME__, __FUNCTION__, "Error", "Connect failed (%s)", strerror(errno));
exit(-1);
}
int status = kSuccess;
int discoveryMaxIterations = atoi(argv[2]);
for (int discoveryIterations = 0; discoveryIterations < discoveryMaxIterations; discoveryIterations ++)
{
uint8_t* udpBuffer = (uint8_t*)calloc(1, kUDPBufferSize);
int serverPort;
char networkAddress[kHostAddressSize] = { 0 };
if ((status = UDPGetServerInfo(socketFileDescriptor, &serverPort, networkAddress, udpBuffer)) < 0)
{
PrintMessage(__FILENAME__, __FUNCTION__, "Error", "Failed to read UDP packet (%s)", strerror(errno));
exit(-1);
}
else
{
char* hostAddress = networkAddress + strlen(kSubnet);
if (BBNetAddDeviceToList(atoi(hostAddress)))
{
printf("Found %s on port %d ---> %s\n", networkAddress, serverPort, DeviceLocationGetName(udpBuffer[kLocationIDOffset]));
}
}
free(udpBuffer);
}
printf("Discovery terminated.\n");
status = UDPCloseSocket(socketFileDescriptor);
if (status < 0)
{
PrintMessage(__FILENAME__, __FUNCTION__, "Error", "Disconnect failed (%s)", strerror(errno));
exit(-1);
}
}
else
{
PrintHelp(argv[0]);
}
}
exit(1);
}