From 9ef59bff9670540da90ca3662b244d0d1a27a289 Mon Sep 17 00:00:00 2001 From: Iker Perez de Albeniz Date: Thu, 29 Dec 2016 13:40:44 +0100 Subject: [PATCH] Base project taken from Allseen git repo, and a simple example --- .gitignore | 47 +- .../net_java_dev_jna_jna_platform_4_2_2.xml | 11 + .idea/misc.xml | 6 + .idea/modules.xml | 8 + .idea/vcs.xml | 6 + LICENSE | 30 +- README.md | 65 +- jwlanapi.iml | 12 + src/net/java/dev/wlanapi/Dot11BssidList.java | 66 ++ src/net/java/dev/wlanapi/Dot11PhyType.java | 32 + src/net/java/dev/wlanapi/Dot11Ssid.java | 62 ++ .../java/dev/wlanapi/NdisObjectHeader.java | 49 ++ src/net/java/dev/wlanapi/PDot11BssidList.java | 33 + src/net/java/dev/wlanapi/PDot11Ssid.java | 32 + .../wlanapi/PWlanConnectionAttributes.java | 39 + .../dev/wlanapi/PWlanInterfaceCapability.java | 38 + .../dev/wlanapi/PWlanNotificationData.java | 40 + .../dev/wlanapi/PWlanProfileInfoList.java | 37 + src/net/java/dev/wlanapi/ScanResult.java | 80 ++ src/net/java/dev/wlanapi/SystemErrorCode.java | 31 + src/net/java/dev/wlanapi/WifiManager.java | 550 ++++++++++++ src/net/java/dev/wlanapi/WlanApi.java | 811 ++++++++++++++++++ .../wlanapi/WlanAssociationAttributes.java | 54 ++ .../dev/wlanapi/WlanAvailableNetwork.java | 173 ++++ .../dev/wlanapi/WlanAvailableNetworkList.java | 108 +++ .../dev/wlanapi/WlanConnectionAttributes.java | 65 ++ .../java/dev/wlanapi/WlanConnectionMode.java | 26 + .../WlanConnectionNotificationData.java | 70 ++ .../dev/wlanapi/WlanConnectionParameters.java | 101 +++ .../dev/wlanapi/WlanInterfaceCapability.java | 76 ++ .../java/dev/wlanapi/WlanInterfaceInfo.java | 57 ++ .../dev/wlanapi/WlanInterfaceInfoList.java | 84 ++ .../java/dev/wlanapi/WlanInterfaceState.java | 28 + .../dev/wlanapi/WlanNotificationData.java | 65 ++ src/net/java/dev/wlanapi/WlanProfileInfo.java | 40 + .../java/dev/wlanapi/WlanProfileInfoList.java | 77 ++ .../dev/wlanapi/WlanSecurityAttributes.java | 39 + src/sample/SimpleTest.java | 42 + 38 files changed, 3161 insertions(+), 29 deletions(-) create mode 100644 .idea/libraries/net_java_dev_jna_jna_platform_4_2_2.xml create mode 100644 .idea/misc.xml create mode 100644 .idea/modules.xml create mode 100644 .idea/vcs.xml create mode 100644 jwlanapi.iml create mode 100644 src/net/java/dev/wlanapi/Dot11BssidList.java create mode 100644 src/net/java/dev/wlanapi/Dot11PhyType.java create mode 100644 src/net/java/dev/wlanapi/Dot11Ssid.java create mode 100644 src/net/java/dev/wlanapi/NdisObjectHeader.java create mode 100644 src/net/java/dev/wlanapi/PDot11BssidList.java create mode 100644 src/net/java/dev/wlanapi/PDot11Ssid.java create mode 100644 src/net/java/dev/wlanapi/PWlanConnectionAttributes.java create mode 100644 src/net/java/dev/wlanapi/PWlanInterfaceCapability.java create mode 100644 src/net/java/dev/wlanapi/PWlanNotificationData.java create mode 100644 src/net/java/dev/wlanapi/PWlanProfileInfoList.java create mode 100644 src/net/java/dev/wlanapi/ScanResult.java create mode 100644 src/net/java/dev/wlanapi/SystemErrorCode.java create mode 100644 src/net/java/dev/wlanapi/WifiManager.java create mode 100644 src/net/java/dev/wlanapi/WlanApi.java create mode 100644 src/net/java/dev/wlanapi/WlanAssociationAttributes.java create mode 100644 src/net/java/dev/wlanapi/WlanAvailableNetwork.java create mode 100644 src/net/java/dev/wlanapi/WlanAvailableNetworkList.java create mode 100644 src/net/java/dev/wlanapi/WlanConnectionAttributes.java create mode 100644 src/net/java/dev/wlanapi/WlanConnectionMode.java create mode 100644 src/net/java/dev/wlanapi/WlanConnectionNotificationData.java create mode 100644 src/net/java/dev/wlanapi/WlanConnectionParameters.java create mode 100644 src/net/java/dev/wlanapi/WlanInterfaceCapability.java create mode 100644 src/net/java/dev/wlanapi/WlanInterfaceInfo.java create mode 100644 src/net/java/dev/wlanapi/WlanInterfaceInfoList.java create mode 100644 src/net/java/dev/wlanapi/WlanInterfaceState.java create mode 100644 src/net/java/dev/wlanapi/WlanNotificationData.java create mode 100644 src/net/java/dev/wlanapi/WlanProfileInfo.java create mode 100644 src/net/java/dev/wlanapi/WlanProfileInfoList.java create mode 100644 src/net/java/dev/wlanapi/WlanSecurityAttributes.java create mode 100644 src/sample/SimpleTest.java diff --git a/.gitignore b/.gitignore index 32858aa..ba6a5d1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,12 +1,41 @@ -*.class +# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm +# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 -# Mobile Tools for Java (J2ME) -.mtj.tmp/ +# User-specific stuff: +.idea/workspace.xml +.idea/tasks.xml -# Package Files # -*.jar -*.war -*.ear +# Sensitive or high-churn files: +.idea/dataSources/ +.idea/dataSources.ids +.idea/dataSources.xml +.idea/dataSources.local.xml +.idea/sqlDataSources.xml +.idea/dynamic.xml +.idea/uiDesigner.xml -# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml -hs_err_pid* +# Gradle: +.idea/gradle.xml + +# Mongo Explorer plugin: +.idea/mongoSettings.xml + +## File-based project format: +*.iws + +## Plugin-specific files: + +# IntelliJ +/out/ + +# mpeltonen/sbt-idea plugin +.idea_modules/ + +# JIRA plugin +atlassian-ide-plugin.xml + +# Crashlytics plugin (for Android Studio and IntelliJ) +com_crashlytics_export_strings.xml +crashlytics.properties +crashlytics-build.properties +fabric.properties diff --git a/.idea/libraries/net_java_dev_jna_jna_platform_4_2_2.xml b/.idea/libraries/net_java_dev_jna_jna_platform_4_2_2.xml new file mode 100644 index 0000000..057415a --- /dev/null +++ b/.idea/libraries/net_java_dev_jna_jna_platform_4_2_2.xml @@ -0,0 +1,11 @@ + + + + + + + + + + + \ No newline at end of file diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..0548357 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..441aab2 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..35eb1dd --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/LICENSE b/LICENSE index 209c62c..68536f3 100644 --- a/LICENSE +++ b/LICENSE @@ -1,21 +1,15 @@ -MIT License +ISC License -Copyright (c) 2016 Iker Perez de Albeniz +Copyright (c) 2016 Iker Perez de Albeniz. All rights reserved. -Permission is hereby granted, free of charge, to any person obtaining a copy -of this software and associated documentation files (the "Software"), to deal -in the Software without restriction, including without limitation the rights -to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -copies of the Software, and to permit persons to whom the Software is -furnished to do so, subject to the following conditions: +Permission to use, copy, modify, and/or distribute this software for any +purpose with or without fee is hereby granted, provided that the above +copyright notice and this permission notice appear in all copies. -The above copyright notice and this permission notice shall be included in all -copies or substantial portions of the Software. - -THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE -SOFTWARE. +THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES +WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR +ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES +WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN +ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF +OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. diff --git a/README.md b/README.md index c9b887e..ccd3762 100644 --- a/README.md +++ b/README.md @@ -1,2 +1,63 @@ -# jwlanapi -A Java Wrapper for Windows wlanapi +# JWlanAPI +JWlanApi is a JNA wrapper for [Micosoft Windows Native Wifi API](https://msdn.microsoft.com/library/windows/desktop/ms706556(v=vs.85).aspx). The goal of this project is to provide an easy and reliable Wi-Fi connection manager for Windows based on Java (voiding using ***netsh*** or similar approaches). + +The base code is taken from [AllSeen/Alljoyn](https://allseenalliance.org/) proyect, and more specifically based on the [code commited by at4wireless](https://github.com/allseenalliance/compliance-tests/tree/master/java/components/validation-ctt/HEAD/ctt_testcases/src/com/at4wireless/alljoyn/wifiapi). + +## Current Status +Current code is a bit buggy, some method are not working and also more code is needed to handles some data and serialize them properly. + +TO BE DONE: + +* Make ***connect*** method work +* Make ***connectedSsid*** method work (current conected signal SSID) +* ScanResult signal level, frequency and capabilities values are not correctly handled. +* Serialize ScanResult capabilities (currently a string with int values, ned to use [DOT11_AUTH_ALGORITHM](https://msdn.microsoft.com/en-us/library/windows/desktop/ms705989(v=vs.85).aspx) and [DOT11_CIPHER_ALGORITHM](https://msdn.microsoft.com/en-us/library/windows/desktop/ms706003(v=vs.85).aspx) enums) +* Change all method to asynchronous result handling with custom listeners. + +## Example Code + +Using this library is quite easy, here you have a simple code snippet. + + ```java +import net.java.dev.wlanapi.ScanResult; +import net.java.dev.wlanapi.WifiManager; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Created by i.perezdealbeniz on 29/12/2016. + */ +public class SimpleTest { + + public static void main(String[] args) throws InterruptedException { + System.out.println("Java wlanapi.dll Wrapper Test:\n"); + System.out.println("******************************\n"); + WifiManager wlan_manager = new WifiManager(); + + System.out.printf("Wifi Enabled: %s\n\n",wlan_manager.isWifiEnabled()); + + System.out.println("Detected Wifi Networks:\n"); + List scan_results = wlan_manager.waitForScanResults(5, + TimeUnit.SECONDS); + for(int i=0; i < scan_results.size(); i++){ + System.out.println("\t SSID : "+scan_results.get(i).SSID); + System.out.println("\t\t BSSID : "+scan_results.get(i).BSSID); + System.out.println("\t\t Frequency (MHz): "+ + Integer.toString(scan_results.get(i).frequency)); + System.out.println("\t\t Signal Level (db) : "+ + Integer.toString(scan_results.get(i).level)); + System.out.println("\t\t Capabilities : "+ + scan_results.get(i).capabilities); + System.out.println(); + } + + System.out.println("Configured Wifi Networks:"); + List configured_networks = wlan_manager.getConfiguredNetworks(); + for(int i=0; i < scan_results.size(); i++){ + System.out.println("\t"+ Integer.toString(i)+"\t"+scan_results.get(i)); + } + } + +} + ``` \ No newline at end of file diff --git a/jwlanapi.iml b/jwlanapi.iml new file mode 100644 index 0000000..c7924a7 --- /dev/null +++ b/jwlanapi.iml @@ -0,0 +1,12 @@ + + + + + + + + + + + + \ No newline at end of file diff --git a/src/net/java/dev/wlanapi/Dot11BssidList.java b/src/net/java/dev/wlanapi/Dot11BssidList.java new file mode 100644 index 0000000..bba1108 --- /dev/null +++ b/src/net/java/dev/wlanapi/Dot11BssidList.java @@ -0,0 +1,66 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +/** + * Contains a list of basic service set (BSS) identifiers. + */ +public class Dot11BssidList extends Structure +{ + public static class ByReference extends Dot11BssidList implements Structure.ByReference + { + + } + + /** + * An NDIS_OBJECT_HEADER structure that contains the type, version, and size information of an NDIS structure. For most DOT11_BSSID_LIST structures, set + * the Type member to NDIS_OBJECT_TYPE_DEFAULT, set the Revision member to DOT11_BSSID_LIST_REVISION_1, and set the Size member to + * sizeof(DOT11_BSSID_LIST). + */ + public NdisObjectHeader Header; + + /** + * The number of entries in this structure. + */ + public long uNumOfEntries; + + /** + * The total number of entries supported. + */ + public long uTotalNumOfEntries; + + /** + * A list of BSS identifiers. A BSS identifier is stored as a DOT11_MAC_ADDRESS type. + */ + public char[][] BSSIDs; + + /*public Dot11BssidList() + { + BSSIDs = new char[1][6]; + }*/ + + @Override + protected List getFieldOrder() + { + return Arrays.asList("Header", "uNumOfEntries", "uTotalNumOfEntries", "BSSIDs"); + } + +} diff --git a/src/net/java/dev/wlanapi/Dot11PhyType.java b/src/net/java/dev/wlanapi/Dot11PhyType.java new file mode 100644 index 0000000..df4e1fb --- /dev/null +++ b/src/net/java/dev/wlanapi/Dot11PhyType.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +public enum Dot11PhyType +{ + UNKNOWN, + ANY, + FHSS, + DSSS, + IRBASEBAND, + OFDM, + HRDSSS, + ERP, + HT, + VHT, + IHV_start, //0x80000000 + IHV_end //0xffffffff +} diff --git a/src/net/java/dev/wlanapi/Dot11Ssid.java b/src/net/java/dev/wlanapi/Dot11Ssid.java new file mode 100644 index 0000000..1c2a0da --- /dev/null +++ b/src/net/java/dev/wlanapi/Dot11Ssid.java @@ -0,0 +1,62 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +/** + * Contains the SSID of an interface + */ +public class Dot11Ssid extends Structure +{ + public static class ByReference extends Dot11Ssid implements Structure.ByReference + { + + } + + public static int DOT11_SSID_MAX_LENGTH = 32; + + /** + * The length, in bytes, of the ucSSID array. + */ + public int uSSIDLength; + + /** + * The SSID. DOT11_SSID_MAX_LENGTH is set to 32. + */ + public byte[] ucSSID; + + public Dot11Ssid() + { + ucSSID = new byte[DOT11_SSID_MAX_LENGTH]; + } + + /*public Dot11Ssid(Pointer p) + { + uSSIDLength = p.getInt(0); + ucSSID = p.getByteArray(4, uSSIDLength); + }*/ + + @Override + protected List getFieldOrder() + { + return Arrays.asList("uSSIDLength", "ucSSID"); + } +} + diff --git a/src/net/java/dev/wlanapi/NdisObjectHeader.java b/src/net/java/dev/wlanapi/NdisObjectHeader.java new file mode 100644 index 0000000..9a23618 --- /dev/null +++ b/src/net/java/dev/wlanapi/NdisObjectHeader.java @@ -0,0 +1,49 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +/** + * Packages the object type, version, and size information that is required in many NDIS 6.0 structures. + */ +public class NdisObjectHeader extends Structure +{ + /** + * Specifies the type of NDIS Object that a structure describes. + */ + public char Type; + + /** + * Specifies the revision number of this structure. + */ + public char Revision; + + /** + * Specifies the total size, in bytes, of the NDIS structure that contains the NDIS_OBJECT_HEADER. This size includes the size of the NDIS_OBJECT_HEADER + * member and all other members of the structure. + */ + public short Size; + + @Override + protected List getFieldOrder() + { + return Arrays.asList("Type", "Revision", "Size"); + } +} diff --git a/src/net/java/dev/wlanapi/PDot11BssidList.java b/src/net/java/dev/wlanapi/PDot11BssidList.java new file mode 100644 index 0000000..273f6ca --- /dev/null +++ b/src/net/java/dev/wlanapi/PDot11BssidList.java @@ -0,0 +1,33 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class PDot11BssidList extends Structure +{ + public Dot11BssidList.ByReference dot11BssidList; + + @Override + protected List getFieldOrder() + { + return Arrays.asList("dot11BssidList"); + } + +} diff --git a/src/net/java/dev/wlanapi/PDot11Ssid.java b/src/net/java/dev/wlanapi/PDot11Ssid.java new file mode 100644 index 0000000..bb14579 --- /dev/null +++ b/src/net/java/dev/wlanapi/PDot11Ssid.java @@ -0,0 +1,32 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class PDot11Ssid extends Structure +{ + public Dot11Ssid.ByReference dot11Ssid; + + @Override + protected List getFieldOrder() + { + return Arrays.asList("dot11Ssid"); + } +} diff --git a/src/net/java/dev/wlanapi/PWlanConnectionAttributes.java b/src/net/java/dev/wlanapi/PWlanConnectionAttributes.java new file mode 100644 index 0000000..2273b60 --- /dev/null +++ b/src/net/java/dev/wlanapi/PWlanConnectionAttributes.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class PWlanConnectionAttributes extends Structure +{ + public static class ByReference extends PWlanConnectionAttributes implements Structure.ByReference + { + + } + + public WlanConnectionAttributes.ByReference WlanConnectionAttributes; + //public WlanConnectionAttributes WlanConnectionAttributes; + + @Override + protected List getFieldOrder() { + return Arrays.asList("WlanConnectionAttributes"); + } + +}; + diff --git a/src/net/java/dev/wlanapi/PWlanInterfaceCapability.java b/src/net/java/dev/wlanapi/PWlanInterfaceCapability.java new file mode 100644 index 0000000..fd13ad7 --- /dev/null +++ b/src/net/java/dev/wlanapi/PWlanInterfaceCapability.java @@ -0,0 +1,38 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class PWlanInterfaceCapability extends Structure +{ + public static class ByReference extends PWlanInterfaceCapability implements Structure.ByReference + { + + } + + public WlanInterfaceCapability.ByReference WlanInterfaceCapability; + //public WlanInterfaceCapability WlanInterfaceCapability; + + @Override + protected List getFieldOrder() { + return Arrays.asList("WlanInterfaceCapability"); + } + +}; diff --git a/src/net/java/dev/wlanapi/PWlanNotificationData.java b/src/net/java/dev/wlanapi/PWlanNotificationData.java new file mode 100644 index 0000000..53ecb7f --- /dev/null +++ b/src/net/java/dev/wlanapi/PWlanNotificationData.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class PWlanNotificationData extends Structure +{ + /*public static class ByReference extends PWlanNotificationData implements Structure.ByReference + { + + }*/ + /** + * TODO + */ + public WlanNotificationData.ByReference WlanNotificationData; + //public WlanInterfaceInfoList WlanNotificationData; + + @Override + protected List getFieldOrder() { + return Arrays.asList("WlanNotificationData"); + } + +}; diff --git a/src/net/java/dev/wlanapi/PWlanProfileInfoList.java b/src/net/java/dev/wlanapi/PWlanProfileInfoList.java new file mode 100644 index 0000000..3d405a2 --- /dev/null +++ b/src/net/java/dev/wlanapi/PWlanProfileInfoList.java @@ -0,0 +1,37 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class PWlanProfileInfoList extends Structure +{ + public static class ByReference extends PWlanProfileInfoList implements Structure.ByReference + { + + } + + public WlanProfileInfoList.ByReference WlanProfileInfoList; + + @Override + protected List getFieldOrder() + { + return Arrays.asList("WlanProfileInfoList"); + } +} diff --git a/src/net/java/dev/wlanapi/ScanResult.java b/src/net/java/dev/wlanapi/ScanResult.java new file mode 100644 index 0000000..c873d92 --- /dev/null +++ b/src/net/java/dev/wlanapi/ScanResult.java @@ -0,0 +1,80 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package net.java.dev.wlanapi; + +/** + * Describes information about a detected access point. In addition + * to the attributes described here, the supplicant keeps track of + * {@code quality}, {@code noise}, and {@code maxbitrate} attributes, + * but does not currently report them to external clients. + */ +public class ScanResult +{ + /** The network name. */ + public String SSID; + /** The address of the access point. */ + public String BSSID; + /** + * Describes the authentication, key management, and encryption schemes + * supported by the access point. + */ + public String capabilities; + /** + * The detected signal level in dBm. At least those are the units used by + * the TI driver. + */ + public int level; + /** + * The frequency in MHz of the channel over which the client is communicating + * with the access point. + */ + public int frequency; + + /** + * We'd like to obtain the following attributes, + * but they are not reported via the socket + * interface, even though they are known + * internally by wpa_supplicant. + * {@hide} + */ + public ScanResult(String SSID, String BSSID, String caps, int level, int frequency) { + this.SSID = SSID; + this.BSSID = BSSID; + this.capabilities = caps; + this.level = level; + this.frequency = frequency; + //networkConfig = null; + } + + @Override + public String toString() { + StringBuffer sb = new StringBuffer(); + String none = ""; + + sb.append("SSID: "). + append(SSID == null ? none : SSID). + append(", BSSID: "). + append(BSSID == null ? none : BSSID). + append(", capabilities: "). + append(capabilities == null ? none : capabilities). + append(", level: "). + append(level). + append(", frequency: "). + append(frequency); + + return sb.toString(); + } +} diff --git a/src/net/java/dev/wlanapi/SystemErrorCode.java b/src/net/java/dev/wlanapi/SystemErrorCode.java new file mode 100644 index 0000000..9b98c78 --- /dev/null +++ b/src/net/java/dev/wlanapi/SystemErrorCode.java @@ -0,0 +1,31 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +public enum SystemErrorCode +{ + ERROR_SUCCESS(0), + ERROR_NOT_ENOUGH_MEMORY(8), + ERROR_INVALID_PARAMETER(87), + ERROR_REMOTE_SESSION_LIMIT_EXCEEDED(1220); + + private int value; + + private SystemErrorCode(int value) + { + this.value = value; + } +} diff --git a/src/net/java/dev/wlanapi/WifiManager.java b/src/net/java/dev/wlanapi/WifiManager.java new file mode 100644 index 0000000..094d7bf --- /dev/null +++ b/src/net/java/dev/wlanapi/WifiManager.java @@ -0,0 +1,550 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.logging.Logger; + +import com.sun.jna.Pointer; +import com.sun.jna.WString; +import com.sun.jna.platform.win32.Guid; +import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.PointerByReference; + +public class WifiManager +{ + private PointerByReference hClient; + private Guid.GUID interfaceGuid; + + private static final Logger logger = Logger.getLogger( WifiManager.class.getCanonicalName()); + + private WlanApi.WlanNotificationCallback notificationCallback = new WlanApi.WlanNotificationCallback() + { + private boolean connected = false; + private boolean disconnected = false; + + public boolean isConnected() + { + return connected; + } + + public boolean isDisconnected() + { + return disconnected; + } + + @Override + public void callback(WlanNotificationData.ByReference pNotifData, Pointer pContext) + { + WlanConnectionNotificationData.ByReference pConnNotifData = null; + + if (pNotifData != null) + { + switch (pNotifData.NotificationSource) + { + case 8: //wlan_notification_source_acm + //print some notifications as examples + switch (pNotifData.NotificationCode) + { + case 10: //wlan_notification_acm_connection_complete + if (pNotifData.dwDataSize < pNotifData.size()) + { + return; + } + pConnNotifData = new WlanConnectionNotificationData.ByReference(pNotifData.pData.getValue()); + if (pConnNotifData.wlanReasonCode == 0) + { + logger.info("The connection succeeded."); + connected = true; + disconnected = false; + + if (pConnNotifData.wlanConnectionMode == 3 || + pConnNotifData.wlanConnectionMode == 2) + { + //the temporary profile generated for discovery + logger.info("The profile used for this connection is as follows"); + logger.info(pConnNotifData.strProfileXml); + } + } + else + { + logger.info("The connection failed."); + logger.info("Reason code: "+pConnNotifData.wlanReasonCode); + } + break; + case 9: //wlan_notification_acm_connection_start + if (pNotifData.dwDataSize != pNotifData.size()) + { + return; + } + pConnNotifData = new WlanConnectionNotificationData.ByReference(pNotifData.pData.getValue()); + //print out some connection information + logger.info("Currently connecting to "+pConnNotifData.dot11Ssid.ucSSID + +" using profile "+pConnNotifData.strProfileName.toString() + +", connection mode is "+pConnNotifData.wlanConnectionMode + +", BSS type is "+pConnNotifData.dot11BssType); + break; + case 21: //wlan_notification_acm_disconnected + if (pNotifData.dwDataSize < pNotifData.size()) + { + return; + } + pConnNotifData = new WlanConnectionNotificationData.ByReference(pNotifData.pData.getValue()); + if (pConnNotifData.wlanReasonCode == 0) + { + logger.info("The disconnection succeeded."); + disconnected = true; + connected = false; + } + else + { + logger.info("The disconnection failed."); + logger.info("Reason code: "+pConnNotifData.wlanReasonCode); + } + return; + + } + + return; + case 16: //wlan_notification_source_msm + logger.info("Got notification "+pNotifData.NotificationCode+" from MSM."); + return; + } + } + } + }; + + public WifiManager() + { + hClient = openHandle(); + interfaceGuid = enumInterfaces().InterfaceInfo[0].InterfaceGuid; + } + + private PointerByReference openHandle() + { + int dwError; + PointerByReference pdwNegotiatedVersion = new PointerByReference(); + PointerByReference phClientHandle = new PointerByReference(); + + if ((dwError = WlanApi.INSTANCE.WlanOpenHandle( + 2, + null, + pdwNegotiatedVersion, + //hClient.getPointer() + phClientHandle.getPointer() + )) != 0) + { + return null; + } + + return phClientHandle; + } + + private WlanInterfaceInfoList.ByReference enumInterfaces() + { + int dwError; + PointerByReference p = new PointerByReference(); + + //enumerate wireless interfaces + if ((dwError = WlanApi.INSTANCE.WlanEnumInterfaces( + hClient.getValue(), + null, + p + )) != 0) + { + return null; + } + + WlanInterfaceInfoList.ByReference pInterfaceInfoList = new WlanInterfaceInfoList.ByReference(p.getValue()); + + return pInterfaceInfoList; + } + + private void scan() + { + int dwError; + + dwError = WlanApi.INSTANCE.WlanScan( + hClient.getValue(), + interfaceGuid.getPointer(), + null, + null, + null + ); + } + + //get the list of visible wireless networks + private List getVisibleNetworkList() + { + int dwError; + WlanAvailableNetworkList.ByReference pAvailableNetworkList; + PointerByReference p = new PointerByReference(); + + if ((dwError = WlanApi.INSTANCE.WlanGetAvailableNetworkList( + hClient.getValue(), + interfaceGuid.getPointer(), + 1, + null, + p)) != 0) + { + return null; + } + + pAvailableNetworkList = new WlanAvailableNetworkList.ByReference(p.getValue()); + + List scanResults = new ArrayList(); + ScanResult network; + + for (int i = 0; i< pAvailableNetworkList.dwNumberOfItems; i++) + { + int ssidArrayLength = pAvailableNetworkList.Network[i].dot11Ssid.uSSIDLength; + if (ssidArrayLength > Dot11Ssid.DOT11_SSID_MAX_LENGTH) + { + ssidArrayLength = Dot11Ssid.DOT11_SSID_MAX_LENGTH; + } + + String ssid = new String(Arrays.copyOfRange(pAvailableNetworkList.Network[i].dot11Ssid.ucSSID, 0, ssidArrayLength)); + String bssid = Integer.toString(pAvailableNetworkList.Network[i].uNumberOfBssids); + String capabilities = Integer.toString(pAvailableNetworkList.Network[i].dot11DefaultAuthAlgorithm) + +", "+Integer.toString(pAvailableNetworkList.Network[i].dot11DefaultCipherAlgorithm); + int level = -100 + (pAvailableNetworkList.Network[i].wlanSignalQuality/2); + int frequency = 2400; + + network = new ScanResult(ssid, bssid, capabilities, level, frequency); + scanResults.add(network); + } + + return scanResults; + } + + public void connect(String targetNetworkType, String targetNetworkProfile) + { + WlanConnectionParameters.ByReference wlanConnPara = new WlanConnectionParameters.ByReference(); + PDot11Ssid pDot11Ssid = new PDot11Ssid(); + String type = targetNetworkType; + int dwError; + + //set the connection mode (connecting using a profile) + wlanConnPara.wlanConnectionMode = 0; + //set the profile name + wlanConnPara.strProfile = new WString(targetNetworkProfile); + //set the SSID + pDot11Ssid.dot11Ssid = new Dot11Ssid.ByReference(); + pDot11Ssid.dot11Ssid.ucSSID = targetNetworkType.getBytes(); + pDot11Ssid.dot11Ssid.uSSIDLength = targetNetworkType.length(); + + wlanConnPara.pDot11Ssid = pDot11Ssid; + + //get BSS type + if (type.equalsIgnoreCase("adhoc") || type.equalsIgnoreCase("a")) + { + wlanConnPara.dot11BssType = 2; + } + else if (type.equalsIgnoreCase("infrastructure") || type.equalsIgnoreCase("i")) + { + wlanConnPara.dot11BssType = 1; + } + else + { + dwError = 1; + return; + } + + //the desired BSSID list is empty + //pWlanConnPara.WlanConnectionParameters.pDesiredBssidList = null; + //no connection flags + wlanConnPara.dwFlags = 0; + + dwError = WlanApi.INSTANCE.WlanConnect( + hClient.getValue(), + interfaceGuid.getPointer(), + wlanConnPara, + null + ); + } + + private void disconnect() + { + int dwError; + + dwError = WlanApi.INSTANCE.WlanDisconnect( + hClient.getValue(), + interfaceGuid.getPointer(), + null + ); + } + + private void registerNotification() + { + int dwError; + int WLAN_NOTIFICATION_SOURCE_ACM = 8; + int WLAN_NOTIFICATION_SOURCE_MCM = 16; + + if ((dwError = WlanApi.INSTANCE.WlanRegisterNotification( + hClient.getValue(), + WLAN_NOTIFICATION_SOURCE_ACM, + false, + notificationCallback, + null, + null, + new IntByReference(0))) != 0) + { + return; + } + } + + private void unregisterNotification() + { + int dwError; + + //unregister notifications + if ((dwError = WlanApi.INSTANCE.WlanRegisterNotification( + hClient.getValue(), + 0, + false, + null, + null, + null, + new IntByReference(0) + )) == 0) + { + return; + //logger.debug("ACM notifications are successfully unregistered."); + } + /*else + { + logger.debug("Error "+dwError+" occurs when unregister ACM notifications."); + }*/ + } + + + + private List getProfileList() + { + int dwError; + //PWlanProfileInfoList.ByReference ppProfileList = new PWlanProfileInfoList.ByReference(); + PointerByReference p = new PointerByReference(); + + if ((dwError = WlanApi.INSTANCE.WlanGetProfileList( + hClient.getValue(), + interfaceGuid.getPointer(), + null, + p)) != 0) + { + return null; + } + + WlanProfileInfoList.ByReference pProfileList = new WlanProfileInfoList.ByReference(p.getValue()); + + List profileList = new ArrayList(); + for (int i = 0; i < pProfileList.dwNumberOfItems; i++) + { + String profileName = new String(pProfileList.ProfileInfo[i].strProfileName); + profileList.add(profileName.trim()); + } + + return profileList; + } + + private boolean setProfile(String profile) + { + int dwError; + int dwFlags = 0; + IntByReference dwReason = new IntByReference(); + + if ((dwError = WlanApi.INSTANCE.WlanSetProfile( + hClient.getValue(), + interfaceGuid.getPointer(), + dwFlags, + new WString(profile), + null, + true, + null, + dwReason)) != 0) + { + return false; + } + else + { + return true; + } + } + + private boolean deleteProfile(String profileName) + { + int dwError; + + if ((dwError = WlanApi.INSTANCE.WlanDeleteProfile( + hClient.getValue(), + interfaceGuid.getPointer(), + new WString(profileName), + null)) != 0) + { + return false; + } + else + { + return true; + } + } + + public boolean isWifiEnabled() + { + return (enumInterfaces().dwNumberOfItems != 0); + } + + public String connectedSsid() + { + int dwError; + IntByReference dwDataSize = new IntByReference(); + IntByReference pWlanOpcodeValueType = new IntByReference(); + WlanConnectionAttributes.ByReference pWlanConnectionAttributes = new WlanConnectionAttributes.ByReference(); + PointerByReference p = new PointerByReference(); + + WlanApi.INSTANCE.WlanFreeMemory(p.getValue()); + p.setValue(null); + + if((dwError = WlanApi.INSTANCE.WlanQueryInterface( + hClient.getValue(), + interfaceGuid.getPointer(), + 7, + null, + dwDataSize, + p, + pWlanOpcodeValueType + )) != 0) + { + return null; + } + + pWlanConnectionAttributes = new WlanConnectionAttributes.ByReference(p.getValue()); + + int ssidArrayLength = pWlanConnectionAttributes.wlanAssociationAttributes.dot11Ssid.uSSIDLength; + if (ssidArrayLength > Dot11Ssid.DOT11_SSID_MAX_LENGTH) + { + ssidArrayLength = Dot11Ssid.DOT11_SSID_MAX_LENGTH; + } + + return new String(Arrays.copyOfRange(pWlanConnectionAttributes.wlanAssociationAttributes.dot11Ssid.ucSSID, 0, ssidArrayLength)); + } + + public List waitForScanResults(long timeout, TimeUnit unit) throws InterruptedException + { + List scanResults = null; + long timeToWaitInMs = TimeUnit.MILLISECONDS.convert(timeout, unit); + long startTime = System.currentTimeMillis(); + scan(); + while ((scanResults == null) && (System.currentTimeMillis() < startTime + timeToWaitInMs)) + { + scanResults = getVisibleNetworkList(); + Thread.sleep(10); + } + + return scanResults; + } + + public String waitForDisconnect(long timeout, TimeUnit unit) throws InterruptedException + { + String ssid = connectedSsid(); + registerNotification(); + disconnect(); + + long timeToWaitInMs = TimeUnit.MILLISECONDS.convert(timeout, unit); + long startTime = System.currentTimeMillis(); + + while ((!notificationCallback.isDisconnected()) && (System.currentTimeMillis() < startTime + timeToWaitInMs)) + { + Thread.sleep(100); + } + + unregisterNotification(); + + if (notificationCallback.isDisconnected()) + { + return ssid; + } + else + { + return null; + } + } + + public String waitForConnect(String ssid, long timeout, TimeUnit unit) throws InterruptedException + { + String currentSsid = connectedSsid(); + if (ssid.equals(currentSsid)) + { + return currentSsid; + } + scan(); + registerNotification(); + + connect("adhoc", ssid); + + long timeToWaitInMs = TimeUnit.MILLISECONDS.convert(timeout, unit); + long startTime = System.currentTimeMillis(); + + while ((!notificationCallback.isConnected()) && (System.currentTimeMillis() < startTime + timeToWaitInMs)) + { + Thread.sleep(100); + } + + unregisterNotification(); + + if (notificationCallback.isConnected()) + { + return ssid; + } + else + { + return null; + } + } + + public List getConfiguredNetworks() + { + return getProfileList(); + } + + public boolean addNetwork(String profile) + { + return setProfile(profile); + } + + public boolean removeNetwork(String profileName) + { + return deleteProfile(profileName); + } + + public void release() + { + if (hClient != null) + { + WlanApi.INSTANCE.WlanCloseHandle( + hClient.getPointer(), + null + ); + } + if (interfaceGuid != null) + { + interfaceGuid = null; + //WlanApi.INSTANCE.WlanFreeMemory(interfaceGuid.getPointer()); + } + } +} diff --git a/src/net/java/dev/wlanapi/WlanApi.java b/src/net/java/dev/wlanapi/WlanApi.java new file mode 100644 index 0000000..8b7c922 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanApi.java @@ -0,0 +1,811 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import com.sun.jna.Callback; +import com.sun.jna.Library; +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import com.sun.jna.WString; +import com.sun.jna.ptr.IntByReference; +import com.sun.jna.ptr.PointerByReference; + +public interface WlanApi extends Library +{ + WlanApi INSTANCE = (WlanApi) Native.loadLibrary("wlanapi", WlanApi.class); + + /** + * Defines the type of notification callback function + */ + interface WlanNotificationCallback extends Callback + { + /** + * @param pWlanNotificationData + * A pointer to a WLAN_NOTIFICATION_DATA structure that contains the notification information. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Only the + * wlan_notification_acm_connection_complete and wlan_notification_acm_disconnected notifications are + * available. + * @param context + * A pointer to the context information provided by the client when it registered for the notification. + */ + void callback(WlanNotificationData.ByReference pWlanNotificationData, Pointer context); + + boolean isConnected(); + + boolean isDisconnected(); + } + + /** + * Opens a connection to the server + * + * @param dwClientVersion + * The highest version of the WLAN API that the client supports. + * 1 : client version for Windows XP with SP3 and Wireless LAN API for Windows XP with SP2. + * 2 : client version for Windows Vista and Windows Server 2008 + * @param pReserved + * Reserved for future use. Must be set to NULL + * @param pdwNegotiatedVersion + * The version of the WLAN API that will be used in this session. This value is usually the + * highest version supported by both the client and the server + * @param phClientHandle + * A handle for the client to use in this session. This handle is used by other functions throughout + * the session. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_INVALID_PARAMETER pdwNegotiatedVersion is NULL, phClientHandle is NULL or + * pReserved is not NULL. + * ERROR_NOT_ENOUGH_MEMORY Failed to allocate memory to create the client context. + * RPC_STATUS Various error codes. + * ERROR_REMOTE_SESSION_LIMIT_EXCEEDED Too many handles have been issued by the server. + */ + public int WlanOpenHandle( + int dwClientVersion, + PointerByReference pReserved, + PointerByReference pdwNegotiatedVersion, + Pointer phClientHandle); + + /** + * Closes a connection to the server. + * + * @param hClientHandle + * The client's session handle, which identifies the connection to be closed. This handle was obtained by a previous call to the + * WlanOpenHandle function. + * @param pReserved + * Reserved for future use. Set this parameter to NULL. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_INVALID_PARAMETER hClientHandle is NULL or invalid, or pReserved is not NULL. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * RPC_STATUS Various error codes. + */ + public int WlanCloseHandle( + Pointer hClientHandle, + PointerByReference pReserved); + + /** + * Enumerates all of the wireless LAN interfaces currently enabled on the local computer + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pReserved + * Reserved for future use. This parameter must be set to NULL. + * @param ppInterfaceList + * A pointer to storage for a pointer to receive the returned list of wireless LAN interfaces in a + * WLAN_INTERFACE_INFO_LIST structure. + * + * The buffer for the WLAN_INTERFACE_INFO_LIST returned is allocated by the WlanEnumInterfaces + * function if the call succeeds. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_INVALID_PARAMETER A parameter is incorrect. This error is returned if the hClientHandle or + * ppInterfaceList parameter is NULL. This error is returned if the + * pReserved is not NULL. This error is also returned if the hClientHandle + * parameter is not valid. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * RPC_STATUS Various error codes. + * ERROR_NOT_ENOUGH_MEMORY Not enough memory is available to process this request and allocate + * memory for the query results. + */ + public int WlanEnumInterfaces( + Pointer hClientHandle, + Pointer pReserved, + PointerByReference ppInterfaceList); + + /** + * Retrieves the capabilities of an interface + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of this interface. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * @param ppCapability + * A WLAN_INTERFACE_CAPABILITY structure that contains information about the capabilities of the specified interface. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_INVALID_PARAMETER hClientHandle is NULL or invalid, pInterfaceGuid is NULL, pReserved is not NULL or + * ppCapability is NULL. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * ERROR_NOT_SUPPORTED This function was called from an unsupported platform. This value will be returned + * if this function was called from a Windows XP with SP3 or Wireless LAN API for + * Windows XP with SP2 client. + * RPC_STATUS Various error codes. + */ + public int WlanGetInterfaceCapability( + Pointer hClientHandle, + Pointer pInterfaceGuid, + PointerByReference pReserved, + PWlanInterfaceCapability.ByReference ppCapability); + + /** + * Queries various parameters of a specified interface. + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the interface to be queried. + * @param OpCode + * A WLAN_INTF_OPCODE value that specifies the parameter to be queried. The following table lists the valid constants with the data type of the + * parameter in ppData: + * wlan_intf_opcode_autoconf_enabled BOOL + * wlan_intf_opcode_background_scan_enabled BOOL + * wlan_intf_opcode_radio_state WLAN_RADIO_STATE + * wlan_intf_opcode_bss_type DOT11_BSS_TYPE + * wlan_intf_opcode_interface_state WLAN_INTERFACE_STATE + * wlan_intf_opcode_current_connection WLAN_CONNECTION_ATTRIBUTES + * wlan_intf_opcode_channel_number ULONG + * wlan_intf_opcode_supported_infrastructure_auth_cipher_pairs WLAN_AUTH_CIPHER_PAIR_LIST + * wlan_intf_opcode_supported_adhoc_auth_cipher_pairs WLAN_AUTH_CIPHER_PAIR_LIST + * wlan_intf_opcode_supported_country_or_region_string_list WLAN_COUNTRY_OR_REGION_STRING_LIST + * wlan_intf_opcode_media_streaming_mode BOOL + * wlan_intf_opcode_statistics WLAN_STATISTICS + * wlan_intf_opcode_rssi LONG + * wlan_intf_opcode_current_operation_mode ULONG + * wlan_intf_opcode_supported_safe_mode BOOL + * wlan_intf_opcode_certified_safe_mode BOOL + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Only the wlan_intf_opcode_autoconf_enabled, wlan_intf_opcode_bss_type, + * wlan_intf_opcode_interface_state, and wlan_intf_opcode_current_connection constants are valid. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * @param pdwDataSize + * The size of the ppData parameter, in bytes. + * @param ppData + * Pointer to the memory location that contains the queried value of the parameter specified by the OpCode parameter. + * + * Note: If OpCode is set to wlan_intf_opcode_autoconf_enabled, wlan_intf_opcode_background_scan_enabled, or + * wlan_intf_opcode_media_streaming_mode, then the pointer referenced by ppData may point to an integer value. If the pointer referenced by ppData + * points to 0, then the integer value should be converted to the boolean value FALSE. If the pointer referenced by ppData points to a nonzero integer, then + * the integer value should be converted to the boolean value TRUE. + * @param pWlanOpcodeValueType + * If passed a non-NULL value, points to a WLAN_OPCODE_VALUE_TYPE value that specifies the type of opcode returned. This parameter may be NULL. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_ACCESS_DENIED The caller does not have sufficient permissions to perform the requested operation. + * + * Before WlanQueryInterface performs an operation, it retrieves the discretionary access control list (DACL) + * stored with the securable object associated with the specified OpCode. If the DACL does not contain an access + * control entry (ACE) that grants WLAN_READ_ACCESS permission to the access token of the calling thread, then + * WlanQueryInterface returns ERROR_ACCESS_DENIED. + * + * The following table shows the securable objects associated with each OpCode: + * wlan_intf_opcode_autoconf_enabled wlan_secure_ac_enabled + * wlan_intf_opcode_background_scan_enabled wlan_secure_bc_scan_enabled + * wlan_intf_opcode_bss_type wlan_secure_bss_type + * wlan_intf_opcode_current_operation_mode wlan_secure_current_operation_mode + * wlan_intf_opcode_media_streaming_mode wlan_secure_media_streaming_mode_enabled + * wlan_intf_opcode_radio_state None, if running as console user; wlan_secure_interface_properties + * if not running as console user. + * All other values wlan_secure_interface_properties + * + * By default, any user can query the operation mode of the interface: These default permissions can be changed + * by calling the WlanSetSecuritySettings function with SecurableObject set to wlan_secure_current_operation_mode. + * ERROR_INVALID_PARAMETER hClientHandle is NULL or invalid, pInterfaceGuid is NULL, pReserved is not NULL, ppData is NULL, or + * pdwDataSize is NULL. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * ERROR_INVALID_STATE OpCode is set to wlan_intf_opcode_current_connection and the client is not currently connected to a network. + * ERROR_NOT_ENOUGH_MEMORY Failed to allocate memory for the query results. + * RPC_STATUS Various error codes. + */ + public int WlanQueryInterface( + Pointer hClientHandle, + Pointer pInterfaceGuid, + long OpCode, + PointerByReference pReserved, + IntByReference pdwDataSize, + PointerByReference ppData, + IntByReference pWlanOpcodeValueType); + + /** + * Sets user-configurable parameters for a specified interface. + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the interface to be configured. + * @param OpCode + * A WLAN_INTF_OPCODE value that specifies the parameter to be set. The following table lists the valid constants along with the data type of + * the parameter in pData. + * wlan_intf_opcode_autoconf_enabled BOOL Enables or disables auto config for the indicated interface. + * wlan_intf_opcode_background_scan_enabled BOOL Enables or disables background scan for the indicated interface. + * wlan_intf_opcode_radio_state WLAN_PHY_RADIO_STATE Sets the software radio state of a specific physical layer (PHY) for + * the interface. + * wlan_intf_opcode_bss_type DOT11_BSS_TYPE Sets the BSS type. + * wlan_intf_opcode_media_streaming_mode BOOL Sets media streaming mode for the driver. + * wlan_intf_opcode_current_operation_mode ULONG Sets the current operation mode for the interface. For more + * information see Remarks. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Only the wlan_intf_opcode_autoconf_enabled and + * wlan_intf_opcode_bss_type constants are valid. + * @param dwDataSize + * The size of the pData parameter, in bytes. If dwDataSize is larger than the actual amount of memory allocated to pData, then an access + * violation will occur in the calling program. + * @param pData + * The value to be set as specified by the OpCode parameter. The type of data pointed to by pData must be appropriate for the specified OpCode. + * Use the table above to determine the type of data to use. + * + * Note: If OpCode is set to wlan_intf_opcode_autoconf_enabled, wlan_intf_opcode_background_scan_enabled, or + * wlan_intf_opcode_media_streaming_mode, then pData may point to an integer value. If pData points to 0, then the value is converted to + * FALSE. If pData points to a nonzero integer, then the value is converted to TRUE. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_ACCESS_DENIED The caller does not have sufficient permissions to perform the requested operation. + * + * Before WlanSetInterface performs an operation, it retrieves the discretionary access control list (DACL) + * stored with the securable object associated with the specified OpCode. If the DACL does not contain an + * access control entry (ACE) that grants WLAN_WRITE_ACCESS permission to the thread token of the calling + * thread, then WlanSetInterface returns ERROR_ACCESS_DENIED. + * + * The following table shows the securable objects associated with each OpCode: + * wlan_intf_opcode_autoconf_enabled wlan_secure_ac_enabled + * wlan_intf_opcode_background_scan_enabled wlan_secure_bc_scan_enabled + * wlan_intf_opcode_bss_type wlan_secure_bss_type + * wlan_intf_opcode_current_operation_mode wlan_secure_current_operation_mode + * wlan_intf_opcode_media_streaming_mode wlan_secure_media_streaming_mode_enabled + * wlan_intf_opcode_radio_state None, if running as console user; wlan_secure_interface_properties + * if not running as console user. + * All other values wlan_secure_interface_properties + * + * By default, only a user who is logged on as a member of the Administrators group or the Network Configuration + * Operators group can set the operation mode of the interface. These default permissions can be changed by + * calling the WlanSetSecuritySettings function with SecurableObject set to wlan_secure_current_operation_mode. + * ERROR_GEN_FAILURE The parameter specified by OpCode is not supported by the driver or NIC. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * ERROR_INVALID_PARAMETER One of the following conditions occurred: + * hClientHandle is NULL or not valid. + * pInterfaceGuid is NULL. + * pData is NULL. + * pReserved is not NULL. + * OpCode is set to wlan_intf_opcode_current_operation_mode and pData points to a value other than + * DOT11_OPERATION_MODE_EXTENSIBLE_STATION or DOT11_OPERATION_MODE_NETWORK_MONITOR. + * RPC_STATUS Various return codes to indicate errors occurred when connecting. + */ + public int WlanSetInterface( + Pointer hClientHandle, + Pointer pInterfaceGuid, + int OpCode, + int dwDataSize, + PointerByReference pData, + PointerByReference pReserved); + + /** + * Requests a scan for available networks on the indicated interface + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the interface to be queried. + * + * The GUID of each wireless LAN interface enabled on a local computer can be determined using the + * WlanEnumInterfaces function. + * @param pDot11Ssid + * A pointer to a DOT11_SSID structure that specifies the SSID of the network to be scanned. This + * parameter is optional. When set to NULL the returned list contains all available networks. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This parameter must be + * NULL. + * @param pIeData + * A pointer to an information element to include in probe requests. This parameter points to a + * WLAN_RAW_DATA structure that may include client provisioning availability information and 802.1X + * authentication requirements. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This parameter must be + * NULL. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_INVALID_PARAMETER hClientHandle is NULL or invalid, pInterfaceGuid is NULL or pReserved + * is not NULL. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * RPC_STATUS: Various error codes. + * ERROR_NOT_ENOUGH_MEMORY Failed to allocate memory for the query results. + */ + public int WlanScan( + Pointer hClientHandle, + Pointer pInterfaceGuid, + PointerByReference pDot11Ssid, + PointerByReference pIeData, + PointerByReference pReserved + ); + + /** + * Retrieves the list of available networks on a wireless LAN interface. + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * A pointer to the GUID of the wireless LAN interface to be queried. + * + * The GUID of each wireless LAN interface enabled on a local computer can be determined using the + * WlanEnumInterfaces function. + * @param dwFlags + * A set of flags that control the type of networks returned in the list. This parameter can be a combination + * of these possible values: + * WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_ADHOC_PROFILES Include all ad hoc network profiles in + * 0x00000001 the available network list, including + * profiles that are not visible. + * Note If this flag is specified on Windows + * XP with SP3 and Wireless LAN API for + * Windows XP with SP2, it is considered + * an invalid parameter. + * WLAN_AVAILABLE_NETWORK_INCLUDE_ALL_MANUAL_HIDDEN_PROFILES Include all hidden network profiles in + * 0x00000002 the available network list, including + * profiles that are not visible. + * Note If this flag is specified on Windows + * XP with SP3 and Wireless LAN API for + * Windows XP with SP2, it is considered + * an invalid parameter. + * @param pReserved + * Reserved for future use. This parameter must be set to NULL. + * @param ppAvailableNetworkList + * A pointer to storage for a pointer to receive the returned list of visible networks in a + * WLAN_AVAILABLE_NETWORK_LIST structure. + * + * The buffer for the WLAN_AVAILABLE_NETWORK_LIST returned is allocated by the + * WlanGetAvailableNetWorkList function if the call succeeds. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * + * If the function fails, the return value may be one of the following return codes: + * ERROR_INVALID_PARAMETER A parameter is incorrect. This error is returned if the + * hClientHandle, pInterfaceGuid, or ppAvailableNetworkList + * parameter is NULL. This error is returned if the pReserved + * is not NULL. This error is also returned if the dwFlags + * parameter value is set to value that is not valid or the + * hClientHandle parameter is not valid. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle + * table. + * ERROR_NDIS_DOT11_POWER_STATE_INVALID The radio associated with the interface is turned off. + * There are no available networks when the radio is off. + * RPC_STATUS Various error codes. + * ERROR_NOT_ENOUGH_MEMORY Not enough memory is available to process this request + * and allocate memory for the query results. + */ + public int WlanGetAvailableNetworkList( + Pointer hClientHandle, + Pointer pInterfaceGuid, + int dwFlags, + Pointer pReserved, + PointerByReference ppAvailableNetworkList + ); + + /** + * Retrieves all information about a specified wireless profile. + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the wireless interface. + * A list of the GUIDs for wireless interfaces on the local computer can be retrieved using the WlanEnumInterfaces function. + * @param strProfileName + * The name of the profile. Profile names are case-sensitive. This string must be NULL-terminated. The maximum length of the profile name is + * 255 characters. This means that the maximum length of this string, including the NULL terminator, is 256 characters. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: The name of the profile is derived automatically from the SSID of the + * network. For infrastructure network profiles, the name of the profile is the SSID of the network. For ad hoc network profiles, the name of + * the profile is the SSID of the ad hoc network followed by -adhoc. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * @param pstrProfileXml + * A string that is the XML representation of the queried profile. There is no predefined maximum string length. + * @param pdwFlags + * On input, a pointer to the address location used to provide additional information about the request. If this parameter is NULL on input, + * then no information on profile flags will be returned. On output, a pointer to the address location used to receive profile flags. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Per-user profiles are not supported. Set this parameter to NULL. + * + * The pdwFlags parameter can point to an address location that contains the following values: + * WLAN_PROFILE_GET_PLAINTEXT_KEY On input, this flag indicates that the caller wants to retrieve the plain text key from a wireless + * profile. If the calling thread has the required permissions, the WlanGetProfile function returns + * the plain text key in the keyMaterial element of the profile returned in the buffer pointed to by + * the pstrProfileXml parameter. + * + * For the WlanGetProfile call to return the plain text key, the wlan_secure_get_plaintext_key + * permissions from the WLAN_SECURABLE_OBJECT enumerated type must be set on the calling thread. The + * DACL must also contain an ACE that grants WLAN_READ_ACCESS permission to the access token of the + * calling thread. By default, the permissions for retrieving the plain text key is allowed only to the + * members of the Administrators group on a local machine. + * + * If the calling thread lacks the required permissions, the WlanGetProfile function returns the + * encrypted key in the keyMaterial element of the profile returned in the buffer pointed to by the + * pstrProfileXml parameter. No error is returned if the calling thread lacks the required permissions. + * + * Windows 7: This flag passed on input is an extension to native wireless APIs added on Windows 7 and + * later. The pdwFlags parameter is an __inout_opt parameter on Windows 7 and later. + * WLAN_PROFILE_GROUP_POLICY On output when the WlanGetProfile call is successful, this flag indicates that this profile was + * created by group policy. A group policy profile is read-only. Neither the content nor the preference + * order of the profile can be changed. + * WLAN_PROFILE_USER On output when the WlanGetProfile call is successful, this flag indicates that the profile is a user + * profile for the specific user in whose context the calling thread resides. If not set, this profile + * is an all-user profile. + * @param pdwGrantedAccess + * The access mask of the all-user profile + * WLAN_READ_ACCESS The user can view the contents of the profile. + * WLAN_EXECUTE_ACCESS The user has read access, and the user can also connect to and disconnect from a network using the profile. If + * a user has WLAN_EXECUTE_ACCESS, then the user also has WLAN_READ_ACCESS. + * WLAN_WRITE_ACCESS The user has execute access and the user can also modify the content of the profile or delete the profile. If a + * user has WLAN_WRITE_ACCESS, then the user also has WLAN_EXECUTE_ACCESS and WLAN_READ_ACCESS. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes. + * ERROR_ACCESS_DENIED The caller does not have sufficient permissions. This error is returned if the pstrProfileXml parameter + * specifies an all-user profile, but the caller does not have read access on the profile. + * ERROR_INVALID_HANDLE A handle is invalid. This error is returned if the handle specified in the hClientHandle parameter was not + * found in the handle table. + * ERROR_INVALID_PARAMETER A parameter is incorrect. This error is returned if any of the following conditions occur: + * - hClientHandle is NULL. + * - pInterfaceGuid is NULL. + * - pstrProfileXml is NULL. + * - pReserved is not NULL. + * ERROR_NOT_ENOUGH_MEMORY Not enough storage is available to process this command. This error is returned if the system was unable to + * allocate memory for the profile. + * ERROR_NOT_FOUND The profile specified by strProfileName was not found. + * Other Various RPC and other error codes. Use FormatMessage to obtain the message string for the returned error. + */ + public int WlanGetProfile( + Pointer hClientHandle, + Pointer pInterfaceGuid, + String strProfileName, + PointerByReference pReserved, + PointerByReference pstrProfileXml, + PointerByReference pdwFlags, + PointerByReference pdwGrantedAccess); + + /** + * Retrieves the list of profiles in preference order + * + * @param hClientHandle + * The client's session handle obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the wireless interface. + * + * A list of the GUIDs for wireless interfaces on the local computer can be retrieved using the WlanEnumInterfaces function. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * @param ppProfileList + * A PWLAN_PROFILE_INFO_LIST structure that contains the list of profile information. + * + * @return If function succeeds, the return value is ERROR_SUCCESS. + * + * If the function fails, the return value may be one of the following return codes. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * ERROR_INVALID_PARAMETER A parameter is incorrect. This error is returned if any of the following conditions occur: + * - hClientHandle is NULL. + * - pInterfaceGuid is NULL. + * - ppProfileList is NULL. + * - pReserved is not NULL. + * ERROR_NOT_ENOUGH_MEMORY Not enough memory is available to process this request and allocate memory for the query results. + * RPC_STATUS Various error codes. + */ + public int WlanGetProfileList( + Pointer hClientHandle, + Pointer pInterfaceGuid, + Pointer pReserved, + PointerByReference ppProfileList); + + /** + * Sets the content of a specific profile + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenbHandle function. + * @param pInterfaceGuid + * The GUID of the interface. + * @param dwFlags + * The flags to set on the profile. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: dwFlags must be 0. Per-user profiles are not supported. + * + * 0 The profile is an all-user profile. + * WLAN_PROFILE_GROUP_POLICY The profile is a group policy profile. + * 0x00000001 + * WLAN_PROFILE_USER The profile is a per-user profile. + * 0x00000002 + * @param strProfileXml + * Contains the XML representation of the profile. The WLANProfile element is the root profile element. To view sample profiles, see Wireless + * Profile Samples. There is no predefined maximum string length. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: The supplied profile must meet the compatibility criteria described in + * Wireless Profile Compatibility. + * @param strAllUserProfileSecurity + * Sets the security descriptor string on the all-user profile. For more information about profile permissions, see the Remarks section. + * If dwFlags is set to WLAN_PROFILE_USER, this parameter is ignored. + * If this parameter is set to NULL for a new all-user profile, the security descriptor associated with the wlan_secure_add_new_all_user_profiles + * object is used. If the security descriptor has not been modified by a WlanSetSecuritySettings call, all users have default permissions on a + * new all-user profile. Call WlanGetSecuritySettings to get the default permissions associated with the wlan_secure_add_new_all_user_profiles + * object. + * If this parameter is set to NULL for an existing all-user profile, the permissions of the profile are not changed. + * If this parameter is not NULL for an all-user profile, the security descriptor string associated with the profile is created or modified + * after the security descriptor object is created and parsed as a string. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This parameter must be NULL. + * @param bOverwrite + * Specifies whether this profile is overwriting an existing profile. If this parameter is FALSE and the profile already exists, the existing + * profile will not be overwritten and an error will be returned. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * @param pdwReasonCode + * A WLAN_REASON_CODE value that indicates why the profile is not valid. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes: + * ERROR_ACCESS_DENIED The caller does not have sufficient permissions to set the profile. + * + * When called with dwFlags set to 0 - that is, when setting an all-user profile - WlanSetProfile retrieves the + * discretionary access control list (DACL) stored with the wlan_secure_add_new_all_user_profiles object. When + * called with dwFlags set to WLAN_PROFILE_USER - that is, when setting a per-user profile - WlanSetProfile + * retrieves the discretionary access control list (DACL) stored with the wlan_secure_add_new_per_user_profiles + * object. In either case, if the DACL does not contain an access control entry (ACE) that grants WLAN_WRITE_ACCESS + * permission to the access token of the calling thread, then WlanSetProfile returns ERROR_ACCESS_DENIED. + * ERROR_ALREADY_EXISTS strProfileXml specifies a network that already exists. Typically, this return value is used when bOverwrite is + * FALSE; however, if bOverwrite is TRUE and dwFlags specifies a different profile type than the one used by the + * existing profile, then the existing profile will not be overwritten and ERROR_ALREADY_EXISTS will be returned. + * ERROR_BAD_PROFILE The profile specified by strProfileXml is not valid. If this value is returned, pdwReasonCode specifies the + * reason the profile is invalid. + * ERROR_INVALID_PARAMETER One of the following conditions occurred: + * - hClientHandle is NULL or invalid. + * - pInterfaceGuid is NULL. + * - pReserved is not NULL. + * - strProfileXml is NULL. + * - strProfileXml contains a zero-length ConfigBlob. If the profile must have an empty ConfigBlob, use + * 00 in the profile. + * - pdwReasonCode is NULL. + * - dwFlags is not set to one of the specified values. + * - dwFlags is set to WLAN_PROFILE_GROUP_POLICY and bOverwrite is set to FALSE. + * ERROR_NO_MATCH The interface does not support one or more of the capabilities specified in the profile. For example, if a + * profile specifies the use of WPA2 when the NIC only supports WPA, then this error code is returned. Also, + * if a profile specifies the use of FIPS mode when the NIC does not support FIPS mode, then this error code + * is returned. + * RPC_STATUS Various error codes. + */ + public int WlanSetProfile( + Pointer hClientHandle, + Pointer pInterfaceGuid, + int dwFlags, + WString strProfileXml, + WString strAllUserProfileSecurity, + boolean bOverwrite, + Pointer pReserved, + IntByReference pdwReasonCode); + + /** + * Deletes a wireless profile for a wireless interface on the local computer. + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the interface from which to delete the profile. + * @param strProfileName + * The name of the profile to be deleted. Profile names are case-sensitive. This string must be NULL-terminated. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: The supplied name must match the profile name derived automatically from + * the SSID of the network. For an infrastructure network profile, the SSID must be supplied for the profile name. For an ad hoc network profile, + * the supplied name must be the SSID of the ad hoc network followed by -adhoc. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * + * @return If a function succeeds, the return value is ERROR_SUCCESS. + * If a function fails, the return value may be one of the following return codes: + * ERROR_INVALID_PARAMETER The hClientHandle parameter is NULL or not valid, the pInterfaceGuid parameter is NULL, the strProfileName + * parameter is NULL, or pReserved is not NULL. + * ERROR_INVALID_HANDLE The handle specified in the hClientHandle parameter was not found in the handle table. + * ERROR_NOT_FOUND The wireless profile specified by strProfileName was not found in the profile store. + * ERROR_ACCESS_DENIED The caller does not have sufficient permissions to delete the profile. + * RPC_STATUS Various error codes. + */ + public int WlanDeleteProfile( + Pointer hClientHandle, + Pointer pInterfaceGuid, + WString strProfileName, + Pointer pReserved); + + /** + * Attempts to connect to a specific network. + * + * @param hClientHandle + * The client's session handle, returned by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the interface to use for the connection. + * @param pConnectionParameters + * Pointer to a WLAN_CONNECTION_PARAMETERS structure that specifies the connection type, mode, network profile, SSID that identifies the network + * and other parameters. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: There are some constraints on the WLAN_CONNECTION_PARAMETERS + * members. This means that structures that are valid for Windows Server 2008 and Windows Vista may not be valid for Windows XP with SP3 or + * Wireless LAN API for Windows XP with SP2. For a list of constraints, see WLAN_CONNECTION_PARAMETERS. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes. + * ERROR_INVALID_PARAMETER One of the following conditions occurred: + * - hClientHandle is NULL or invalid. + * - pInterfaceGuid is NULL. + * - pConnectionParameters is NULL. + * - The dwFlags member of the structure pointed to by pConnectionParameters is not set to one of the values + * specified on the WLAN_CONNECTION_PARAMETERS page. + * - The wlanConnectionMode member of the structure pointed to by pConnectionParameters is set to + * wlan_connection_mode_discovery_secure or wlan_connection_mode_discovery_unsecure, and the pDot11Ssid + * member of the same structure is NULL. + * - The wlanConnectionMode member of the structure pointed to by pConnectionParameters is set to + * wlan_connection_mode_discovery_secure or wlan_connection_mode_discovery_unsecure, and the dot11BssType + * member of the same structure is set to dot11_BSS_type_any. + * - The wlanConnectionMode member of the structure pointed to by pConnectionParameters is set to + * wlan_connection_mode_profile, and the strProfile member of the same structure is NULL or the length of + * the profile exceeds WLAN_MAX_NAME_LENGTH. + * - The wlanConnectionMode member of the structure pointed to by pConnectionParameters is set to + * wlan_connection_mode_profile, and the strProfile member of the same structure is NULL or the length of + * the profile is zero. + * - The wlanConnectionMode member of the structure pointed to by pConnectionParameters is set to + * wlan_connection_mode_invalid or wlan_connection_mode_auto. + * - The dot11BssType member of the structure pointed to by pConnectionParameters is set to + * dot11_BSS_type_infrastructure, and the dwFlags member of the same structure is set to + * WLAN_CONNECTION_ADHOC_JOIN_ONLY. + * - The dot11BssType member of the structure pointed to by pConnectionParameters is set to + * dot11_BSS_type_independent, and the dwFlags member of the same structure is set to + * WLAN_CONNECTION_HIDDEN_NETWORK. + * - The dwFlags member of the structure pointed to by pConnectionParameters is set to + * WLAN_CONNECTION_IGNORE_PRIVACY_BIT, and either the wlanConnectionMode member of the same structure + * is not set to wlan_connection_mode_temporary_profile or the dot11BssType member of the same structure + * is set to dot11_BSS_type_independent. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * RPC_STATUS Various error codes. + * ERROR_ACCESS_DENIED The caller does not have sufficient permissions. + */ + public int WlanConnect( + Pointer hClientHandle, + Pointer pInterfaceGuid, + WlanConnectionParameters.ByReference pConnectionParameters, + Pointer pReserved); + + /** + * Is used to register and unregister notifications on all wireless interfaces. + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param dwNotifSource + * The notification sources to be registered. These flags may be combined. When this parameter is set to WLAN_NOTIFICATION_SOURCE_NONE, + * WlanRegisterNotification unregisters notifications on all wireless interfaces. + * + * The possible values for this parameter are defined in the Wlanapi.h and L2cmn.h header files. + * + * The following table shows possible values: + * WLAN_NOTIFICATION_SOURCE_NONE Unregisters notifications. + * WLAN_NOTIFICATION_SOURCE_ALL Registers for all notifications available on the version of the operating system, including those + * generated by the 802.1X module. + * + * For Windows XP with SP3 and Wireless LAN API for Windows XP with SP2, setting dwNotifSource to + * WLAN_NOTIFICATION_SOURCE_ALL is functionally equivalent to setting dwNotifSource to + * WLAN_NOTIFICATION_SOURCE_ACM. + * WLAN_NOTIFICATION_SOURCE_ACM Registers for notifications generated by the auto configuration module. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Only the + * wlan_notification_acm_connection_complete and wlan_notification_acm_disconnected notifications are + * available. + * WLAN_NOTIFICATION_SOURCE_HNWK Registers for notifications generated by the wireless Hosted Network. This notification source is + * available on Windows 7 and on Windows Server 2008 R2 with the Wireless LAN Service installed. + * WLAN_NOTIFICATION_SOURCE_ONEX Registers for notifications generated by 802.1X. + * WLAN_NOTIFICATION_SOURCE_MSM Registers for notifications generated by MSM. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This value is not supported. + * WLAN_NOTIFICATION_SOURCE_SECURITY Registers for notifications generated by the security module. + * No notifications are currently defined for WLAN_NOTIFICATION_SOURCE_SECURITY. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This value is not supported. + * WLAN_NOTIFICATION_SOURCE_IHV Registers for notifications generated by independent hardware vendors (IHV). + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This value is not supported. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This parameter must be set to WLAN_NOTIFICATION_SOURCE_NONE, + * WLAN_NOTIFICATION_SOURCE_ALL, or WLAN_NOTIFICATION_SOURCE_ACM. + * @param bIgnoreDuplicate + * Specifies whether duplicate notifications will be ignored. If set to TRUE, a notification will not be sent to the client if it is identical + * to the previous one. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This parameter is ignored. + * @param funcCallback + * A WLAN_NOTIFICATION_CALLBACK type that defines the type of notification callback function. + * + * This parameter can be NULL if the dwNotifSource parameter is set to WLAN_NOTIFICATION_SOURCE_NONE to unregister notifications on all wireless + * interfaces. + * @param pCallbackContext + * A pointer to the client context that will be passed to the callback function with the notification. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * @param pdwPrevNotifSource + * A pointer to the previously registered notification sources. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes. + * ERROR_INVALID_PARAMETER A parameter is incorrect. This error is returned if hClientHandle is NULL or not valid or if pReserved is + * not NULL. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * ERROR_NOT_ENOUGH_MEMORY Failed to allocate memory for the query results. + * RPC_STATUS Various error codes. + */ + public int WlanRegisterNotification( + Pointer hClientHandle, + int dwNotifSource, + boolean bIgnoreDuplicate, + WlanApi.WlanNotificationCallback funcCallback, + PointerByReference pCallbackContext, + PointerByReference pReserved, + IntByReference pdwPrevNotifSource); + + /** + * Disconnects an interface from its current network. + * + * @param hClientHandle + * The client's session handle, obtained by a previous call to the WlanOpenHandle function. + * @param pInterfaceGuid + * The GUID of the interface to be disconnected. + * @param pReserved + * Reserved for future use. Must be set to NULL. + * + * @return If the function succeeds, the return value is ERROR_SUCCESS. + * If the function fails, the return value may be one of the following return codes. + * ERROR_INVALID_PARAMETER hClientHandle is NULL, pInterfaceGuid is NULL, or pReserved is not NULL. + * ERROR_INVALID_HANDLE The handle hClientHandle was not found in the handle table. + * RPC_STATUS Various error codes. + * ERROR_NOT_ENOUGH_MEMORY Failed to allocate memory for the query results. + * ERROR_ACCESS_DENIED The caller does not have sufficient permissions. + */ + public int WlanDisconnect( + Pointer hClientHandle, + Pointer pInterfaceGuid, + PointerByReference pReserved); + + /** + * Frees memory. Any memory returned from Native WiFi functions must be + * freed. + * + * @param pMemory + * Pointer to the memory to be freed. + */ + public void WlanFreeMemory( + Pointer pMemory); +} \ No newline at end of file diff --git a/src/net/java/dev/wlanapi/WlanAssociationAttributes.java b/src/net/java/dev/wlanapi/WlanAssociationAttributes.java new file mode 100644 index 0000000..7cc1193 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanAssociationAttributes.java @@ -0,0 +1,54 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; +import com.sun.jna.platform.win32.WinDef.ULONG; + +public class WlanAssociationAttributes extends Structure +{ + public Dot11Ssid dot11Ssid; + + public int dot11BssType; + + public char[] dot11Bssid; + + public int dot11PhyType; + + public ULONG uDot11PhyIndex; + + public long wlanSignalQuality; + + public ULONG ulRxRate; + + public ULONG ulTxRate; + + public WlanAssociationAttributes() + { + dot11Bssid = new char[6]; + } + + @Override + protected List getFieldOrder() + { + return Arrays.asList("dot11Ssid", "dot11BssType", "dot11Bssid", "dot11PhyType", + "uDot11PhyIndex", "wlanSignalQuality", "ulRxRate", "ulTxRate"); + } + +} diff --git a/src/net/java/dev/wlanapi/WlanAvailableNetwork.java b/src/net/java/dev/wlanapi/WlanAvailableNetwork.java new file mode 100644 index 0000000..eba4232 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanAvailableNetwork.java @@ -0,0 +1,173 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; +import com.sun.jna.Pointer; + +/** + * Contains information about an available wireless network. + */ +public class WlanAvailableNetwork extends Structure +{ + /** + * Contains the profile name associated with the network. If the network does not have a profile, this + * member will be empty. If multiple profiles are associated with the network, there will be multiple entries + * with the same SSID in the visible network list. Profile names are case-sensitive. This string must be NULL- + * terminated. + */ + public char[] strProfileName; + + /** + * A DOT11_SSID structure that contains the SSID of the visible wireless network. + */ + public Dot11Ssid dot11Ssid; + + /** + * A DOT11_BSS_TYPE value that specifies whether the network is infrastructure or ad hoc. + */ + public int dot11BssType; + + /** + * Indicates the number of BSSIDs in the network. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: uNumberOfBssids is at + * most 1, regardless of the number of access points broadcasting the SSID. + */ + public int uNumberOfBssids; + + /** + * Indicates whether the network is connectable or not. If set to TRUE the network is connectable, + * otherwise the network cannot be connected to. + */ + public boolean bNetworkConnectable; + + /** + * A WLAN_REASON_CODE value that indicates why a network cannot be connected to. This member is + * only valid when bNetworkConnectable is FALSE. + */ + public int wlanNotConnectableReason; + + /** + * The number of PHY types supported on available networks. The maximum value of uNumberOfPhyTypes + * is WLAN_MAX_PHY_TYPE_NUMBER, which has a value of 8. If more than + * WLAN_MAX_PHY_TYPE_NUMBER PHY types are supported, bMorePhyTypes must be set to TRUE. + */ + public int uNumberOfPhyTypes; + + /** + * Contains an array of DOT11_PHY_TYPE values that represent the PHY types supported by the available + * networks. When uNumberOfPhyTypes is greater than WLAN_MAX_PHY_TYPE_NUMBER, this array + * contains only the first WLAN_MAX_PHY_TYPE_NUMBER PHY types. + * dot11_phy_type_unknown Specifies an unknown or uninitialized PHY type. + * dot11_phy_type_any Specifies any PHY type. + * dot11_phy_type_fhss Specifies a frequency-hoping spread-spectrum (FHSS) PHY. + * Bluetooth devices can use FHSS or an adaption of FHSS. + * dot11_phy_tyep_dsss Specifies a direct sequence spread spectrum (DSSS) PHY. + * dot11_phy_type_irbaseband Specifies an infrared (IR) baseband PHY. + * dot11_phy_type_ofdm Specifies an orthogonal frequency division multiplexing (OFDM) PHY. + * 802.11a devices can use OFDM. + * dot11_phy_type_hrdsss Specifies a high-rate DSSS (HRDSSS) PHY. + * dot11_phy_type_erp Specifies an extended rate PHY (ERP). 802.11g devices can use ERP. + * dot11_phy_type_ht Specifies an 802.11n PHY type. + * dot11_phy_type_vht Specifies the 802.11ac PHY type. This is the very high troughput PHY + * type specified in IEEE 802.11ac. + * + * This value is supported on Windows 8.1, Windows Server 2012 R2, + * and later. + * dot11_phy_type_IHV_start Specifies the start of the range that is used to define PHY types that + * are developed by an independent hardware vendor (IHV). + * dot11_phy_type_IHV_end Specifies the end of the range that is used to define PHY types that + * are developed by an independent hardware vendor (IHV). + */ + public int[] dot11PhyTypes; + + /** + * Specifies if there are more than WLAN_MAX_PHY_TYPE_NUMBER PHY types supported. + * + * When this member is set to TRUE an application must call WlanGetNetworkBssList to get the complete + * list of PHY types. The returned WLAN_BSS_LIST structure has an array of WLAN_BSS_ENTRY structures. + * The uPhyId member of the WLAN_BSS_ENTRY structure contains the PHY type for an entry. + */ + public boolean bMorePhyTypes; + + /** + * A percentage value that represents the signal quality of the network. WLAN_SIGNAL_QUALITY is of + * type ULONG. This member contains a value between 0 and 100. A value of 0 implies an actual RSSI + * signal strength of -100 dbm. A value of 100 implies an actual RSSI signal strength of -50 dbm. You can + * calculate the RSSI signal strength value for wlanSignalQuality values between 1 and 99 using linear + * interpolation. + */ + public int wlanSignalQuality; + + /** + * Indicates whether security is enabled on the network. A value of TRUE indicates that security is enabled, + * otherwise it is not. + */ + public boolean bSecurityEnabled; + + /** + * A DOT11_AUTH_ALGORITHM value that indicates the default authentication algorithm used to join this + * network for the first time. + */ + public int dot11DefaultAuthAlgorithm; + + /** + * A DOT11_CIPHER_ALGORITHM value that indicates the default cipher algorithm to be used when + * joining this network. + */ + public int dot11DefaultCipherAlgorithm; + + /** + * Contains various flags for the network: + * WLAN_AVAILABLE_NETWORK_CONNECTED Tis network is currently connected. + * WLAN_AVAILABLE_NETWORK_HAS_PROFILE There is a profile for this network. + */ + public int dwFlags; + + /** + * Reserved for future use. Must be set to NULL. + */ + public Pointer dwReserved; + + public WlanAvailableNetwork() + { + dot11PhyTypes = new int[8]; + strProfileName = new char[256]; + dwReserved = null; + } + + public WlanAvailableNetwork(Pointer p) + { + dot11PhyTypes = new int[8]; + strProfileName = new char[256]; + dwReserved = null; + } + + @Override + protected List getFieldOrder() + { + return Arrays.asList("strProfileName", "dot11Ssid", "dot11BssType", "uNumberOfBssids", + "bNetworkConnectable", "wlanNotConnectableReason", "uNumberOfPhyTypes", + "dot11PhyTypes", "bMorePhyTypes", "wlanSignalQuality", "bSecurityEnabled", + "dot11DefaultAuthAlgorithm", "dot11DefaultCipherAlgorithm", "dwFlags", + "dwReserved"); + } + +} diff --git a/src/net/java/dev/wlanapi/WlanAvailableNetworkList.java b/src/net/java/dev/wlanapi/WlanAvailableNetworkList.java new file mode 100644 index 0000000..ef2f56b --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanAvailableNetworkList.java @@ -0,0 +1,108 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Native; +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + +/** + * Contains an array of information about available networks. + */ +public class WlanAvailableNetworkList extends Structure +{ + public static class ByReference extends WlanAvailableNetworkList implements Structure.ByReference + { + public ByReference() + { + + } + + public ByReference(Pointer p) + { + super(p); + } + } + + /** + * Contains the number of items in the Network member. + */ + public int dwNumberOfItems; + + /** + * The index of the current item. The index of the first item is 0. dwIndex must be less than + * dwNumberOfItems. + * + * This member is not used by the wireless service. Applications can use this member when processing + * individual networks in the WLAN_AVAILABLE_NETWORK_LIST structure. When an application passes + * this structure from one function to another, it can set the value of dwIndex to the index of the item + * currently being processed. This can help an application maintain state. + * + * dwIndex should always be initialized before use. + */ + public int dwIndex; + + /** + * An array of WLAN_AVAILABLE_NETWORK structures containing interface information. + */ + // public Pointer Network; + public WlanAvailableNetwork[] Network; + + public WlanAvailableNetworkList() + { + Network = new WlanAvailableNetwork[10]; + } + + public WlanAvailableNetworkList(Pointer p) + { + super(p); + dwNumberOfItems = p.getInt(0); + dwIndex = p.getInt(4); + Network = new WlanAvailableNetwork[dwNumberOfItems]; + + for (int i = 0; i < dwNumberOfItems; i++) + { + Network[i] = new WlanAvailableNetwork(p.getPointer(8+628*i)); + Network[i].strProfileName = p.getCharArray(8+628*i, 256); + Network[i].dot11Ssid = new Dot11Ssid(); + Network[i].dot11Ssid.uSSIDLength = p.getInt(520+628*i); + Network[i].dot11Ssid.ucSSID = p.getByteArray(524+628*i, Network[i].dot11Ssid.uSSIDLength); + Network[i].dot11BssType = p.getInt(548+628*i); + Network[i].uNumberOfBssids = p.getInt(552+628*i); + Network[i].bNetworkConnectable = (p.getInt(556+628*i) != 0); + Network[i].wlanNotConnectableReason = p.getInt(560+628*i); + Network[i].uNumberOfPhyTypes = p.getInt(564+628*i); + Network[i].dot11PhyTypes = p.getIntArray(568+628*i, Network[i].uNumberOfPhyTypes); + Network[i].bMorePhyTypes = (p.getInt(600+628*i) != 0); + Network[i].wlanSignalQuality = p.getInt(604+628*i); + Network[i].bSecurityEnabled = (p.getInt(608+628*i) != 0); + Network[i].dot11DefaultAuthAlgorithm = p.getInt(612+628*i); + Network[i].dot11DefaultCipherAlgorithm = p.getInt(616+628*i); + Network[i].dwFlags = p.getInt(620+628*i); + Network[i].dwReserved = p.getPointer(624+628*i); + } + //read(); + } + + @Override + protected List getFieldOrder() { + return Arrays.asList("dwNumberOfItems", "dwIndex", "Network"); + } +} + diff --git a/src/net/java/dev/wlanapi/WlanConnectionAttributes.java b/src/net/java/dev/wlanapi/WlanConnectionAttributes.java new file mode 100644 index 0000000..9864fa8 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanConnectionAttributes.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + +public class WlanConnectionAttributes extends Structure +{ + public static class ByReference extends WlanConnectionAttributes implements Structure.ByReference + { + public ByReference() + { + + } + + public ByReference(Pointer p) + { + super(p); + } + } + + public WlanConnectionAttributes() + { + strProfileName = new char[256]; + } + + public WlanConnectionAttributes(Pointer p) + { + super(p); + strProfileName = new char[256]; + read(); + } + + public int isState; + + public int wlanConnectionMode; + + public char[] strProfileName; + + public WlanAssociationAttributes wlanAssociationAttributes; + + public WlanSecurityAttributes wlanSecurityAttributes; + + @Override + protected List getFieldOrder() { + return Arrays.asList("isState", "wlanConnectionMode", "strProfileName", "wlanAssociationAttributes", "wlanSecurityAttributes"); + } +} diff --git a/src/net/java/dev/wlanapi/WlanConnectionMode.java b/src/net/java/dev/wlanapi/WlanConnectionMode.java new file mode 100644 index 0000000..c9b661c --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanConnectionMode.java @@ -0,0 +1,26 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +public enum WlanConnectionMode +{ + PROFILE, + TEMPORARY_PROFILE, + DISCOVERY_SECURE, + DISCOVERY_UNSECURE, + AUTO, + INVALID; +} diff --git a/src/net/java/dev/wlanapi/WlanConnectionNotificationData.java b/src/net/java/dev/wlanapi/WlanConnectionNotificationData.java new file mode 100644 index 0000000..6eba1e8 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanConnectionNotificationData.java @@ -0,0 +1,70 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + +public class WlanConnectionNotificationData extends Structure +{ + public static class ByReference extends WlanConnectionNotificationData implements Structure.ByReference + { + public ByReference(Pointer p) + { + super(p); + } + } + + private static final int WLAN_MAX_NAME_LENGTH = 256; + + public int wlanConnectionMode; + + public char[] strProfileName; + + public Dot11Ssid dot11Ssid; + + public int dot11BssType; + + public boolean bSecurityEnabled; + + public int wlanReasonCode; + + public int dwFlags; + + public String strProfileXml; + + public WlanConnectionNotificationData() + { + strProfileName = new char[WLAN_MAX_NAME_LENGTH]; + } + + public WlanConnectionNotificationData(Pointer p) + { + super(p); + strProfileName = new char[WLAN_MAX_NAME_LENGTH]; + } + + @Override + protected List getFieldOrder() + { + return Arrays.asList("wlanConnectionMode", "strProfileName", "dot11Ssid", "dot11BssType", "bSecurityEnabled", + "wlanReasonCode", "dwFlags", "strProfileXml"); + } + +} diff --git a/src/net/java/dev/wlanapi/WlanConnectionParameters.java b/src/net/java/dev/wlanapi/WlanConnectionParameters.java new file mode 100644 index 0000000..75640fe --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanConnectionParameters.java @@ -0,0 +1,101 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; +import com.sun.jna.WString; + +/** + * Specifies the parameters used when using the WlanConnect function + */ +public class WlanConnectionParameters extends Structure +{ + public static class ByReference extends WlanConnectionParameters implements Structure.ByReference + { + + } + + /** + * A WLAN_CONNECTION_MODE alue that specifies the mode of connection. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Only the wlan_connection_mode_profile value is supported. + */ + public int wlanConnectionMode; + + /** + * Specifies the profile being used for the connection. + * + * If wlanConnectionMode is set to wlan_connection_mode_profile, then strProfile specifies the name of the profile used for the connection. If + * wlanConnectionMode is set to wlan_connection_mode_temporary_profile, then strProfile specifies the XML representation of the profile used for the + * connection. If wlanConnectionMode is set to wlan_connection_mode_discovery_secure or wlan_connection_mode_discovery_unsecure, then strProfile + * should be set to NULL. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: The profile must meet the compatibility criteria described in Wireless Profile + * Compatibility. + */ + public WString strProfile; + + /** + * Pointer to a DOT11_SSID structure that specifies the SSID of the network to connect to. This parameter is optional. When set to NULL, all SSIDs in the + * profile will be tried. This parameter must not be NULL if WLAN_CONNECTION_MODE is set to wlan_connection_mode_discovery_secure or + * wlan_connection_mode_discovery_unsecure. + */ + public PDot11Ssid pDot11Ssid; + + /** + * Pointer to a DOT11_BSSID_LIST structure that contains the list of basic service set (BSS) identifiers desired for the connection. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This member must be NULL. + */ + public PDot11BssidList pDesiredBssidList; + + /** + * A DOT11_BSS_TYPE value that indicates the BSS type of the network. If a profile is provided, this BSS must be the same as the one in the profile. + */ + public int dot11BssType; + + /** + * The following table shows flags used to specify the connection parameters: + * WLAN_CONNECTION_HIDDEN_NETWORK 0x00000001 Connect to the destination network even if the destination is a hidden network. A hidden + * network does not broadcast its SSID. Do not use this flag if the destination network is an + * ad-hoc network. + * If the profile specified by strProfile is not NULL, then this flag is ignored and the + * nonBroadcast profile element determines whether to connect to a hidden network. + * WLAN_CONNECTION_ADHOC_JOIN_ONLY 0x00000002 Do not form an ad-hoc network. Only join an ad-hoc network if the network already + * exists. Do not use this flag if the destination network is an infrastructure network. + * WLAN_CONNECTION_IGNORE_PRIVACY_BIT 0x00000004 Ignore the privacy bit when connecting to the network. Ignoring the privacy bit has the + * effect of ignoring whether packets are encrypted and ignoring the method of encryption + * used. Only use this flag when connecting to an infrastructure network using a temporary + * profile. + * WLAN_CONNECTION_EAPOL_PASSTHROUGH 0x00000008 Exempt EAPOL traffic from encryption and decryption. This flag is used when an + * application must send EAPOL traffic over an infrastructure network that uses Open + * authentication and WEP encryption. This flag must not be used to connect to networks + * that require 802.1X authentication. This flag is only valid when wlanConnectionMode is + * set to wlan_connection_mode_temporary_profile. Avoid using this flag whenever + * possible. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: This member must be set to 0. + */ + public int dwFlags; + + @Override + protected List getFieldOrder() { + return Arrays.asList("wlanConnectionMode", "strProfile", "pDot11Ssid", "pDesiredBssidList", "dot11BssType", "dwFlags"); + } +} diff --git a/src/net/java/dev/wlanapi/WlanInterfaceCapability.java b/src/net/java/dev/wlanapi/WlanInterfaceCapability.java new file mode 100644 index 0000000..8d048cc --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanInterfaceCapability.java @@ -0,0 +1,76 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +/** + * Contains information about the capabilities of an interface + */ +public class WlanInterfaceCapability extends Structure +{ + public static class ByReference extends WlanInterfaceCapability implements Structure.ByReference + { + + } + + private static int WLAN_MAX_PHY_INDEX = 64; + + /** + * A WLAN_INTERFACE_TYPE value that indicates the type of the interface. + */ + public int interfaceType; + + /** + * Indicates whether 802.11d is supported by the interface. If TRUE, 802.11d is supported. + */ + public boolean bDot11DSupported; + + /** + * The maximum size of the SSID list supported by this interface. + */ + public int dwMaxDesiredSsidListSize; + + /** + * The maximum size of the basic service set (BSS) identifier list supported by this interface. + */ + public int dwMaxDesiredBssidListSize; + + /** + * Contains the number of supported PHY types. + */ + public int dwNumberOfSupportedPhys; + + /** + * An array of DOT11_PHY_TYPE values that specify the supported PHY types. WLAN_MAX_PHY_INDEX is set to 64. + */ + public int[] dot11PhyTypes; + + public WlanInterfaceCapability() + { + dot11PhyTypes = new int[WLAN_MAX_PHY_INDEX]; + } + + @Override + protected List getFieldOrder() + { + return Arrays.asList("interfaceType", "bDot11DSupported", "dwMaxDesiredSsidListSize", + "dwMaxDesiredBssidListSize", "dwNumberOfSupportedPhys", "dot11PhyTypes"); + } +} diff --git a/src/net/java/dev/wlanapi/WlanInterfaceInfo.java b/src/net/java/dev/wlanapi/WlanInterfaceInfo.java new file mode 100644 index 0000000..48bb93a --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanInterfaceInfo.java @@ -0,0 +1,57 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; +import com.sun.jna.platform.win32.Guid; + +public class WlanInterfaceInfo extends Structure +{ + public static class ByReference extends WlanInterfaceInfoList implements Structure.ByReference + { + + } + + /** + * Contains the GUID of the interface. + */ + public Guid.GUID InterfaceGuid; + + /** + * Contains the description of the interface + */ + public char[] strInterfaceDescription = new char[256]; + + /** + * Contains a WLAN_INTERFACE_STATE value that indicates the current state of the interface. + * + * Windows XP with SP3 and Wireless LAN API for Windows XP with SP2: Only the + * wlan_interface_state_connected, wlan_interface_state_disconnected, and + * wlan_interface_state_authenticating values are supported. + */ + public int isState; + + @Override + protected List getFieldOrder() + { + return Arrays.asList("InterfaceGuid", "strInterfaceDescription", "isState"); + } + +} + diff --git a/src/net/java/dev/wlanapi/WlanInterfaceInfoList.java b/src/net/java/dev/wlanapi/WlanInterfaceInfoList.java new file mode 100644 index 0000000..7543eb6 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanInterfaceInfoList.java @@ -0,0 +1,84 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + +public class WlanInterfaceInfoList extends Structure +{ + public static class ByReference extends WlanInterfaceInfoList implements Structure.ByReference + { + public ByReference() + { + + } + + public ByReference(Pointer p) + { + super(p); + } + } + + /** + * Contains the number of items in the InterfaceInfo member + */ + public int dwNumberOfItems; + + /** + * The index of the current item. The index of the first item is 0. dwIndex must be less than + * dwNumberOfItems. + * + * This member is not used by the wireless service. Applications can use this member when processing + * individual interfaces in the WLAN_INTERFACE_INFO_LIST structure. When an application passes this + * structure from one function to another, it can set the value of dwIndex to the index of the item currently + * being processed. This can help an application maintain state. + * + * dwIndex should always be initialized before use. + */ + public int dwIndex; + + /** + * An array of WLAN_INTERFACE_INFO structures containing interface information. + */ + // public Pointer[] InterfaceInfo; + public WlanInterfaceInfo[] InterfaceInfo; + + public WlanInterfaceInfoList() + { + InterfaceInfo = new WlanInterfaceInfo[1]; + } + + public WlanInterfaceInfoList(Pointer p) + { + super(p); + //dwNumberOfItems = p.getInt(0); + //dwIndex = p.getInt(4); + InterfaceInfo = new WlanInterfaceInfo[p.getInt(0)]; + read(); + } + + @Override + protected List getFieldOrder() + { + return Arrays.asList("dwNumberOfItems", "dwIndex", "InterfaceInfo"); + } + +}; + diff --git a/src/net/java/dev/wlanapi/WlanInterfaceState.java b/src/net/java/dev/wlanapi/WlanInterfaceState.java new file mode 100644 index 0000000..06adaf9 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanInterfaceState.java @@ -0,0 +1,28 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +public enum WlanInterfaceState +{ + NOT_READY, + CONNECTED, + AD_HOC_NETWORK_FORMED, + DISCONNECTING, + DISCONNECTED, + ASSOCIATING, + DISCOVERING, + AUTHENTICATING; +} diff --git a/src/net/java/dev/wlanapi/WlanNotificationData.java b/src/net/java/dev/wlanapi/WlanNotificationData.java new file mode 100644 index 0000000..585dda2 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanNotificationData.java @@ -0,0 +1,65 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; +import com.sun.jna.platform.win32.Guid; +import com.sun.jna.ptr.PointerByReference; + +/** + * Contains information provided when receiving notifications + */ +public class WlanNotificationData extends Structure +{ + public static class ByReference extends WlanNotificationData implements Structure.ByReference + { + + } + + /** + * TODO + */ + public int NotificationSource; + + /** + * TODO + */ + public int NotificationCode; + + /** + * TODO + */ + public Guid.GUID InterfaceGuid; + + /** + * TODO + */ + public int dwDataSize; + + /** + * TODO + */ + public PointerByReference pData; + + @Override + protected List getFieldOrder() + { + return Arrays.asList("NotificationSource", "NotificationCode", "InterfaceGuid", "dwDataSize", "pData"); + } +} diff --git a/src/net/java/dev/wlanapi/WlanProfileInfo.java b/src/net/java/dev/wlanapi/WlanProfileInfo.java new file mode 100644 index 0000000..acfa296 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanProfileInfo.java @@ -0,0 +1,40 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class WlanProfileInfo extends Structure +{ + public char[] strProfileName; + + public int dwFlags; + + public WlanProfileInfo() + { + strProfileName = new char[256]; + } + + @Override + protected List getFieldOrder() + { + return Arrays.asList("strProfileName", "dwFlags"); + } + +} diff --git a/src/net/java/dev/wlanapi/WlanProfileInfoList.java b/src/net/java/dev/wlanapi/WlanProfileInfoList.java new file mode 100644 index 0000000..fc62851 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanProfileInfoList.java @@ -0,0 +1,77 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Pointer; +import com.sun.jna.Structure; + +public class WlanProfileInfoList extends Structure +{ + public static class ByReference extends WlanProfileInfoList implements Structure.ByReference + { + public ByReference() + { + + } + + public ByReference(Pointer p) + { + super(p); + } + } + + public int dwNumberOfItems; + + public int dwIndex; + + public WlanProfileInfo[] ProfileInfo; + + public WlanProfileInfoList() + { + ProfileInfo = new WlanProfileInfo[10]; + } + + public WlanProfileInfoList(Pointer p) + { + super(p); + /*dwNumberOfItems = p.getInt(0); + dwIndex = p.getInt(4);*/ + if(p.getInt(0) != 0) + { + ProfileInfo = new WlanProfileInfo[p.getInt(0)]; + } + else + { + ProfileInfo = new WlanProfileInfo[1]; + } + + /*for (int i = 0; i < dwNumberOfItems; i++) + { + ProfileInfo[i].strProfileName = p.getCharArray(8+516*i, 256); + ProfileInfo[i].dwFlags = p.getInt(520+516*i); + }*/ + read(); + } + + @Override + protected List getFieldOrder() + { + return Arrays.asList("dwNumberOfItems", "dwIndex", "ProfileInfo"); + } +} diff --git a/src/net/java/dev/wlanapi/WlanSecurityAttributes.java b/src/net/java/dev/wlanapi/WlanSecurityAttributes.java new file mode 100644 index 0000000..8cc7743 --- /dev/null +++ b/src/net/java/dev/wlanapi/WlanSecurityAttributes.java @@ -0,0 +1,39 @@ +/******************************************************************************* + * Copyright AllSeen Alliance. All rights reserved. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + *******************************************************************************/ +package net.java.dev.wlanapi; + +import java.util.Arrays; +import java.util.List; + +import com.sun.jna.Structure; + +public class WlanSecurityAttributes extends Structure +{ + public boolean bSecurityEnabled; + + public boolean bOneXEnabled; + + public int dot11AuthAlgorithm; + + public int dot11CipherAlgorithm; + + @Override + protected List getFieldOrder() + { + return Arrays.asList("bSecurityEnabled", "bOneXEnabled", "dot11AuthAlgorithm", "dot11CipherAlgorithm"); + } + +} diff --git a/src/sample/SimpleTest.java b/src/sample/SimpleTest.java new file mode 100644 index 0000000..433ea5c --- /dev/null +++ b/src/sample/SimpleTest.java @@ -0,0 +1,42 @@ +package sample; + +import net.java.dev.wlanapi.ScanResult; +import net.java.dev.wlanapi.WifiManager; + +import java.util.List; +import java.util.concurrent.TimeUnit; + +/** + * Created by i.perezdealbeniz on 29/12/2016. + */ +public class SimpleTest { + + public static void main(String[] args) throws InterruptedException { + System.out.println("\nJava wlanapi.dll Wrapper Test:\n******************************\n"); + WifiManager wlan_manager = new WifiManager(); + + System.out.printf("Wifi Enabled: %s\n\n",wlan_manager.isWifiEnabled()); + + System.out.println("Detected Wifi Networks:\n"); + List scan_results = wlan_manager.waitForScanResults(5, + TimeUnit.SECONDS); + for(int i=0; i < scan_results.size(); i++){ + System.out.println("\t SSID : "+scan_results.get(i).SSID); + System.out.println("\t\t BSSID : "+scan_results.get(i).BSSID); + System.out.println("\t\t Frequency (MHz): "+ + Integer.toString(scan_results.get(i).frequency)); + System.out.println("\t\t Signal Level (db) : "+ + Integer.toString(scan_results.get(i).level)); + System.out.println("\t\t Capabilities : "+ + scan_results.get(i).capabilities); + System.out.println(); + } + + System.out.println("Configured Wifi Networks:"); + List configured_networks = wlan_manager.getConfiguredNetworks(); + for(int i=0; i < scan_results.size(); i++){ + System.out.println("\t"+ Integer.toString(i)+"\t"+scan_results.get(i)); + } + } + +}