Home /Modules/wifi/SLWifiManager.sleela

SLWifiManager.sleela

lib/wifi/SLWifiManager.sleela · 214 line(s) · sleela

<< lib/wifi/SLWifiManager.sleela
   SLeeLa object model/facade for the native wifi_manager_t C API.
   
   Bridge boundary: this class owns request/state/policy semantics; it does not
   simulate a live radio. A host SLVM binding must connect its scan/select/connect/
   disconnect methods to wifi_manager_scan/select/connect/disconnect. Until that
   binding is installed operations return BRIDGE_REQUIRED or BACKEND_UNAVAILABLE. >>
#sleela 2.5
material;
class SLWifiManager {
  < ---- Descriptive metadata (version 1.11 nested-friendly accessors) ---- >
  << Eleven catalogued attributes with getter/setter pairs, so a GUI, loader,
     or catalog can read and (where appropriate) override this record.
     Values are faithful to the subject's real origin where publicly known. >>
  String metaVersion = "1.0.0";
  int metaId = 11439;
  String metaAuthor = "Max Rupplin vs. Dr. Mrs. Elisabeth Harkins";
  String metaDate = "2026-10-20";
  String metaCopyright = "SLeeLa project";
  String metaTrademark = "none";
  String metaCorporation = "none";
  < metaOrigin: National | <nation> | International | Galactic >
  String metaOrigin = "International";
  String metaBirthDate = "2026-10-20";
  < metaScienceRating: maturity/robustness on a 1..10 scale >
  int metaScienceRating = 9;
  < metaNationalSchedule: a real, citable instrument or "none" >
  String metaNationalSchedule = "none";

  String getMetaVersion() { return metaVersion; }
  void setMetaVersion(String v) { metaVersion = v; }
  int getMetaId() { return metaId; }
  void setMetaId(int v) { metaId = v; }
  String getAuthor() { return metaAuthor; }
  void setAuthor(String v) { metaAuthor = v; }
  String getDate() { return metaDate; }
  void setDate(String v) { metaDate = v; }
  String getCopyright() { return metaCopyright; }
  void setCopyright(String v) { metaCopyright = v; }
  String getTrademark() { return metaTrademark; }
  void setTrademark(String v) { metaTrademark = v; }
  String getCorporation() { return metaCorporation; }
  void setCorporation(String v) { metaCorporation = v; }
  String getOrigin() { return metaOrigin; }
  void setOrigin(String v) { metaOrigin = v; }
  String getBirthDate() { return metaBirthDate; }
  void setBirthDate(String v) { metaBirthDate = v; }
  int getScienceRating() { return metaScienceRating; }
  void setScienceRating(int v) { metaScienceRating = v; }
  String getNationalSchedule() { return metaNationalSchedule; }
  void setNationalSchedule(String v) { metaNationalSchedule = v; }
  static final int STATE_NEW = 0;
  static final int STATE_BRIDGE_READY = 1;
  static final int STATE_SCANNING = 2;
  static final int STATE_SCANNED = 3;
  static final int STATE_CONNECTING = 4;
  static final int STATE_CONNECTED = 5;
  static final int STATE_DISCONNECTED = 6;
  static final int STATE_FAILED = 7;
  static final int STATE_DISCONNECTING = 8;

  int nativeHandle;
  int state;
  int lastStatus;
  int lastScanCount;
  SLWifiNetwork selectedNetwork;
  SLWifiSecurityPolicy policy;

  void configure() {
    nativeHandle = -1;
    state = STATE_NEW;
    lastStatus = SLWifiStatus.BRIDGE_REQUIRED;
    lastScanCount = 0;
    selectedNetwork = null;
    policy = new SLWifiSecurityPolicy();
    policy.configureDefaults();
  }

  boolean bindNativeHandle(int handle) {
    if (handle < 0) {
      nativeHandle = -1;
      state = STATE_NEW;
      lastStatus = SLWifiStatus.BACKEND_UNAVAILABLE;
      return false;
    }
    nativeHandle = handle;
    state = STATE_BRIDGE_READY;
    lastStatus = SLWifiStatus.OK;
    return true;
  }

  boolean nativeReady() { return nativeHandle >= 0; }
  int handle() { return nativeHandle; }
  int managerState() { return state; }
  int status() { return lastStatus; }
  int scanCount() { return lastScanCount; }
  SLWifiSecurityPolicy securityPolicy() { return policy; }

  int beginScanRequest() {
    if (!nativeReady()) {
      lastStatus = SLWifiStatus.BRIDGE_REQUIRED;
      return lastStatus;
    }
    state = STATE_SCANNING;
    lastScanCount = 0;
    return SLWifiStatus.OK;
  }

  int acceptScanResult(int statusCode, int count) {
    if (state != STATE_SCANNING) {
      lastStatus = SLWifiStatus.INVALID_ARGUMENT;
      return lastStatus;
    }
    if (statusCode != SLWifiStatus.OK && statusCode != SLWifiStatus.BUFFER_TOO_SMALL) {
      state = STATE_FAILED;
      lastStatus = statusCode;
      return lastStatus;
    }
    if (count < 0) {
      state = STATE_FAILED;
      lastStatus = SLWifiStatus.INVALID_ARGUMENT;
      return lastStatus;
    }
    lastScanCount = count;
    state = STATE_SCANNED;
    lastStatus = statusCode;
    return lastStatus;
  }

  boolean selectNetwork(SLWifiNetwork network) {
    if (state != STATE_SCANNED || network == null || !network.hasIdentity()) {
      lastStatus = SLWifiStatus.INVALID_ARGUMENT;
      return false;
    }
    selectedNetwork = network;
    lastStatus = SLWifiStatus.OK;
    return true;
  }

  int authorizeConnection(SLWifiCredentials credentials) {
    if (!nativeReady()) {
      lastStatus = SLWifiStatus.BRIDGE_REQUIRED;
      return lastStatus;
    }
    if (selectedNetwork == null) {
      lastStatus = SLWifiStatus.NETWORK_NOT_FOUND;
      return lastStatus;
    }
    if (!policy.allowsNetwork(selectedNetwork)) {
      lastStatus = SLWifiStatus.POLICY_BLOCKED;
      return lastStatus;
    }
    if (credentials == null || !credentials.isValid()) {
      lastStatus = SLWifiStatus.AUTH_REQUIRED;
      return lastStatus;
    }
    state = STATE_CONNECTING;
    lastStatus = SLWifiStatus.OK;
    return lastStatus;
  }

  int beginConnectRequest(SLWifiCredentials credentials) {
    return authorizeConnection(credentials);
  }

  int acceptConnectionResult(int statusCode) {
    if (state != STATE_CONNECTING) {
      lastStatus = SLWifiStatus.INVALID_ARGUMENT;
      return lastStatus;
    }
    lastStatus = statusCode;
    state = statusCode == SLWifiStatus.OK ? STATE_CONNECTED : STATE_SCANNED;
    return lastStatus;
  }

  int beginDisconnectRequest() {
    if (!nativeReady()) {
      lastStatus = SLWifiStatus.BRIDGE_REQUIRED;
      return lastStatus;
    }
    if (state != STATE_CONNECTED) {
      lastStatus = SLWifiStatus.NOT_CONNECTED;
      return lastStatus;
    }
    state = STATE_DISCONNECTING;
    lastStatus = SLWifiStatus.OK;
    return lastStatus;
  }

  int acceptDisconnectResult(int statusCode) {
    lastStatus = statusCode;
    if (statusCode == SLWifiStatus.OK || statusCode == SLWifiStatus.NOT_CONNECTED) {
      state = STATE_DISCONNECTED;
      selectedNetwork = null;
    } else {
      state = STATE_FAILED;
    }
    return lastStatus;
  }

  void clearCredentials(SLWifiCredentials credentials) {
    if (credentials != null) { credentials.clear(); }
  }

  void reset() {
    nativeHandle = -1;
    state = STATE_NEW;
    lastStatus = SLWifiStatus.BRIDGE_REQUIRED;
    lastScanCount = 0;
    selectedNetwork = null;
    policy.configureDefaults();
  }
}