mirror of
https://github.com/wpilibsuite/allwpilib
synced 2026-06-22 01:11:42 +00:00
Add methods to get the hostname and network interfaces.
This commit is contained in:
@@ -2,7 +2,13 @@
|
||||
|
||||
#include <stdio.h>
|
||||
|
||||
#include "llvm/raw_ostream.h"
|
||||
|
||||
int main() {
|
||||
llvm::outs() << "hostname: " << cs::GetHostname() << '\n';
|
||||
llvm::outs() << "IPv4 network addresses:\n";
|
||||
for (const auto& addr : cs::GetNetworkInterfaces())
|
||||
llvm::outs() << " " << addr << '\n';
|
||||
cs::USBCamera camera{"usbcam", 1};
|
||||
camera.SetVideoMode(cs::VideoMode::kMJPEG, 320, 240, 30);
|
||||
cs::MJPEGServer mjpegServer{"httpserver", 8081};
|
||||
|
||||
@@ -297,6 +297,11 @@ void CS_FreeEnumPropertyChoices(char** choices, int count);
|
||||
void CS_FreeEnumeratedProperties(CS_Property* properties, int count);
|
||||
void CS_FreeEnumeratedVideoModes(CS_VideoMode* modes, int count);
|
||||
|
||||
char* CS_GetHostname();
|
||||
|
||||
char** CS_GetNetworkInterfaces(int* count);
|
||||
void CS_FreeNetworkInterfaces(char** interfaces, int count);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -275,6 +275,10 @@ llvm::ArrayRef<CS_Source> EnumerateSourceHandles(
|
||||
llvm::ArrayRef<CS_Sink> EnumerateSinkHandles(
|
||||
llvm::SmallVectorImpl<CS_Sink>& vec, CS_Status* status);
|
||||
|
||||
std::string GetHostname();
|
||||
|
||||
std::vector<std::string> GetNetworkInterfaces();
|
||||
|
||||
} // namespace cs
|
||||
|
||||
#endif // CSCORE_CPP_H_
|
||||
|
||||
@@ -1089,4 +1089,26 @@ JNIEXPORT jintArray JNICALL Java_edu_wpi_cscore_CameraServerJNI_enumerateSinks
|
||||
return MakeJIntArray(env, arr);
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cscore_CameraServerJNI
|
||||
* Method: getHostname
|
||||
* Signature: ()Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jstring JNICALL Java_edu_wpi_cscore_CameraServerJNI_getHostname
|
||||
(JNIEnv *env, jclass)
|
||||
{
|
||||
return MakeJString(env, cs::GetHostname());
|
||||
}
|
||||
|
||||
/*
|
||||
* Class: edu_wpi_cscore_CameraServerJNI
|
||||
* Method: getNetworkInterfaces
|
||||
* Signature: ()[Ljava/lang/String;
|
||||
*/
|
||||
JNIEXPORT jobjectArray JNICALL Java_edu_wpi_cscore_CameraServerJNI_getNetworkInterfaces
|
||||
(JNIEnv *env, jclass)
|
||||
{
|
||||
return MakeJStringArray(env, cs::GetNetworkInterfaces());
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -181,4 +181,8 @@ public class CameraServerJNI {
|
||||
public static native int[] enumerateSources();
|
||||
|
||||
public static native int[] enumerateSinks();
|
||||
|
||||
public static native String getHostname();
|
||||
|
||||
public static native String[] getNetworkInterfaces();
|
||||
}
|
||||
|
||||
@@ -321,4 +321,24 @@ void CS_FreeEnumeratedVideoModes(CS_VideoMode* modes, int count) {
|
||||
std::free(modes);
|
||||
}
|
||||
|
||||
char* CS_GetHostname() {
|
||||
return cs::ConvertToC(cs::GetHostname());
|
||||
}
|
||||
|
||||
char** CS_GetNetworkInterfaces(int* count) {
|
||||
auto interfaces = cs::GetNetworkInterfaces();
|
||||
char** out =
|
||||
static_cast<char**>(std::malloc(interfaces.size() * sizeof(char*)));
|
||||
*count = interfaces.size();
|
||||
for (std::size_t i = 0; i < interfaces.size(); ++i)
|
||||
out[i] = cs::ConvertToC(interfaces[i]);
|
||||
return out;
|
||||
}
|
||||
|
||||
void CS_FreeNetworkInterfaces(char** interfaces, int count) {
|
||||
if (!interfaces) return;
|
||||
for (int i = 0; i < count; ++i) std::free(interfaces[i]);
|
||||
std::free(interfaces);
|
||||
}
|
||||
|
||||
} // extern "C"
|
||||
|
||||
@@ -7,6 +7,13 @@
|
||||
|
||||
#include "cscore_cpp.h"
|
||||
|
||||
#if defined(__linux__)
|
||||
#include <arpa/inet.h>
|
||||
#include <ifaddrs.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#endif
|
||||
|
||||
#include "llvm/SmallString.h"
|
||||
|
||||
#include "Notifier.h"
|
||||
@@ -511,4 +518,39 @@ llvm::ArrayRef<CS_Sink> EnumerateSinkHandles(
|
||||
return Sinks::GetInstance().GetAll(vec);
|
||||
}
|
||||
|
||||
std::string GetHostname() {
|
||||
#ifdef __linux__
|
||||
char name[256];
|
||||
if (::gethostname(name, sizeof(name)) != 0) return "";
|
||||
name[255] = '\0'; // Per POSIX, may not be null terminated if too long
|
||||
return name;
|
||||
#else
|
||||
return ""; // TODO
|
||||
#endif
|
||||
}
|
||||
|
||||
std::vector<std::string> GetNetworkInterfaces() {
|
||||
#ifdef __linux__
|
||||
struct ifaddrs* ifa;
|
||||
if (::getifaddrs(&ifa) != 0) return std::vector<std::string>{};
|
||||
|
||||
std::vector<std::string> rv;
|
||||
char buf[256];
|
||||
for (struct ifaddrs* i = ifa; i; i = i->ifa_next) {
|
||||
if (!i->ifa_addr) continue; // no address
|
||||
if (i->ifa_addr->sa_family != AF_INET) continue; // only return IPv4
|
||||
struct sockaddr_in* addr_in = reinterpret_cast<sockaddr_in*>(i->ifa_addr);
|
||||
const char* addr =
|
||||
::inet_ntop(addr_in->sin_family, &addr_in->sin_addr, buf, sizeof(buf));
|
||||
if (!addr) continue; // error converting address
|
||||
rv.emplace_back(addr);
|
||||
}
|
||||
|
||||
::freeifaddrs(ifa);
|
||||
return rv;
|
||||
#else
|
||||
return std::vector<std::string>{}; // TODO
|
||||
#endif
|
||||
}
|
||||
|
||||
} // namespace cs
|
||||
|
||||
Reference in New Issue
Block a user