#include <switch.h>
#include <stdio.h>
#include <string.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <stdlib.h>
PadState g_pad;
HidsysUniquePadId g_unique_pad_ids[2] = {0};
s32 g_total_entries = 0;
bool g_led_state = false; // Track current LED state
void turn_led_on() {
HidsysNotificationLedPattern pattern;
memset(&pattern, 0, sizeof(pattern));
pattern.baseMiniCycleDuration = 0x1;
pattern.totalMiniCycles = 0x1;
pattern.totalFullCycles = 0x0;
pattern.startIntensity = 0xF;
pattern.miniCycles[0].ledIntensity = 0xF;
pattern.miniCycles[0].transitionSteps = 0x0;
pattern.miniCycles[0].finalStepDuration = 0x7;
// Always refresh pad IDs to ensure they're current
padUpdate(&g_pad);
g_total_entries = 0;
memset(g_unique_pad_ids, 0, sizeof(g_unique_pad_ids));
HidNpadIdType npad_id_type = padIsHandheld(&g_pad) ? HidNpadIdType_Handheld : HidNpadIdType_No1;
Result rc = hidsysGetUniquePadsFromNpad(npad_id_type, g_unique_pad_ids, 2, &g_total_entries);
if (R_SUCCEEDED(rc) && g_total_entries > 0) {
for(int i = 0; i < g_total_entries; i++) {
hidsysSetNotificationLedPattern(&pattern, g_unique_pad_ids[i]);
}
g_led_state = true;
}
}
void turn_led_off() {
HidsysNotificationLedPattern pattern;
memset(&pattern, 0, sizeof(pattern)); // Zero pattern turns LED off
// Always refresh pad IDs to ensure they're current
padUpdate(&g_pad);
g_total_entries = 0;
memset(g_unique_pad_ids, 0, sizeof(g_unique_pad_ids));
HidNpadIdType npad_id_type = padIsHandheld(&g_pad) ? HidNpadIdType_Handheld : HidNpadIdType_No1;
Result rc = hidsysGetUniquePadsFromNpad(npad_id_type, g_unique_pad_ids, 2, &g_total_entries);
if (R_SUCCEEDED(rc) && g_total_entries > 0) {
for(int i = 0; i < g_total_entries; i++) {
hidsysSetNotificationLedPattern(&pattern, g_unique_pad_ids[i]);
}
g_led_state = false;
}
}
void toggle_led() {
if (g_led_state) {
turn_led_off();
} else {
turn_led_on();
}
}
bool print_ip_local() {
int sock = socket(AF_INET, SOCK_DGRAM, 0);
if (sock < 0) {
printf("Cannot create socket\n");
return false;
}
// Try connecting to local broadcast address (works even without internet)
struct sockaddr_in remote = {
.sin_family = AF_INET,
.sin_port = htons(9), // Discard port
.sin_addr.s_addr = inet_addr("255.255.255.255") // Local broadcast
};
// Enable broadcast
int broadcast = 1;
if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0) {
printf("Setsockopt failed\n");
close(sock);
return false;
}
if (connect(sock, (struct sockaddr*)&remote, sizeof(remote)) < 0) {
printf("Local network not available\n");
close(sock);
return false;
}
// Get the local address
struct sockaddr_in local;
socklen_t len = sizeof(local);
if (getsockname(sock, (struct sockaddr*)&local, &len) == 0) {
printf("Your Local IP Address: %s\n", inet_ntoa(local.sin_addr));
close(sock);
return true;
} else {
printf("Could not get IP address\n");
}
close(sock);
return false;
}
// Function to run when we have network connectivity
void on_network_connected() {
turn_led_on();
}
// Function to run when no network is available
void on_network_disconnected() {
turn_led_off();
}
int main(int argc, char *argv[]) {
consoleInit(NULL);
padConfigureInput(1, HidNpadStyleSet_NpadStandard);
padInitializeDefault(&g_pad);
hidsysInitialize();
bool network_available = false;
if (R_FAILED(socketInitializeDefault())) {
printf("Network unavailable\n");
} else {
network_available = print_ip_local();
socketExit();
}
// Execute appropriate function based on network status
if (network_available) {
on_network_connected();
} else {
on_network_disconnected();
}
while (appletMainLoop()) {
padUpdate(&g_pad);
u64 kDown = padGetButtonsDown(&g_pad);
if (kDown & HidNpadButton_A) {
//toggle_led(); // Toggle notification led between on and off
}
if (kDown & HidNpadButton_Plus)
{
turn_led_off();
break;
}
consoleUpdate(NULL);
}
consoleExit(NULL);
hidsysExit();
return 0;
}