master ohmu/fastfetch-openbsd / src / detection / wifi / wifi.h
ohmu  ·  2026-10-15
 1#pragma once
 2
 3#include "fastfetch.h"
 4#include "modules/wifi/option.h"
 5
 6struct FFWifiInterface {
 7    FFstrbuf description;
 8    FFstrbuf status;
 9};
10
11struct FFWifiConnection {
12    FFstrbuf status;
13    FFstrbuf ssid;
14    FFstrbuf bssid;
15    FFstrbuf protocol;
16    FFstrbuf security;
17    double signalQuality; // Percentage
18    double rxRate;
19    double txRate;
20    uint16_t channel;
21    uint16_t channelWidth; // MHz
22    uint16_t frequency;    // MHz
23};
24
25typedef struct FFWifiResult {
26    struct FFWifiInterface inf;
27    struct FFWifiConnection conn;
28} FFWifiResult;
29
30const char* ffDetectWifi(FFlist* result /*list of FFWifiItem*/);
31
32static inline uint16_t ffWifiFreqToChannel(uint16_t frequency) {
33    // Return 0 for unknown / non-standard frequencies.
34    if (frequency == 2484) {
35        return 14;
36    }
37
38    // 2.4 GHz channels 1-13
39    if (frequency >= 2412 && frequency <= 2472 && ((frequency - 2407) % 5) == 0) {
40        return (uint16_t) ((frequency - 2407) / 5);
41    }
42
43    // 4.9 GHz public safety band (e.g. channels 182-196)
44    if (frequency >= 4910 && frequency <= 4980 && ((frequency - 4000) % 5) == 0) {
45        return (uint16_t) ((frequency - 4000) / 5);
46    }
47
48    // 5 GHz channels
49    if (frequency >= 5000 && frequency <= 5895 && ((frequency - 5000) % 5) == 0) {
50        return (uint16_t) ((frequency - 5000) / 5);
51    }
52
53    // 6 GHz channels (Wi-Fi 6E/7)
54    // 5935 MHz is a special case mapped to channel 2.
55    if (frequency == 5935) {
56        return 2;
57    }
58    if (frequency >= 5955 && frequency <= 7115 && ((frequency - 5950) % 5) == 0) {
59        return (uint16_t) ((frequency - 5950) / 5);
60    }
61
62    return 0;
63}
64
65static inline uint16_t ffWifiChannelToFreq(uint16_t channel) {
66    // Inverse of ffWifiFreqToChannel for the common 2.4 GHz and 5 GHz bands.
67    // 4.9 GHz and 6 GHz channels overlap numerically, so they are not handled here.
68    if (channel == 14) {
69        return 2484;
70    }
71    if (channel >= 1 && channel <= 13) {
72        return (uint16_t) (2407 + 5 * channel);
73    }
74    if (channel >= 36 && channel <= 177) { // 5 GHz: 5000 + 5*channel covers 36..177
75        return (uint16_t) (5000 + 5 * channel);
76    }
77    return 0;
78}