Changes between Initial Version and Version 1 of Android/wireless

10/22/2017 05:28:45 AM (5 years ago)



  • Android/wireless

    v1 v1  
     2          <div id="wikipage" class="trac-content"><p>
     3</p><div class="wiki-toc">
     5  <li>
     6    <a href="#AndroidWi-Fi">Android Wi-Fi</a>
     7    <ol>
     8      <li>
     9        <a href="#Build-timeConfiguration">Build-time Configuration</a>
     10      </li>
     11      <li>
     12        <a href="#AndroidFrameworkAPI">Android Framework API</a>
     13      </li>
     14      <li>
     15        <a href="#Packages">Packages</a>
     16      </li>
     17      <li>
     18        <a href="#FirmwareandKernelModuleloading">Firmware and Kernel Module loading</a>
     19      </li>
     20      <li>
     21        <a href="#ClientMode">Client Mode</a>
     22        <ol>
     23          <li>
     24            <a href="#Configuration">Configuration</a>
     25          </li>
     26          <li>
     27            <a href="#Operation">Operation</a>
     28            <ol>
     29              <li>
     30                <a href="#AndroidUI">Android UI</a>
     31              </li>
     32              <li>
     33                <a href="#AndroidApplication">Android Application</a>
     34              </li>
     35              <li>
     36                <a href="#command-line">command-line</a>
     37              </li>
     38            </ol>
     39          </li>
     40        </ol>
     41      </li>
     42      <li>
     43        <a href="#AccessPointMode">Access Point Mode</a>
     44        <ol>
     45          <li>
     46            <a href="#Configuration1">Configuration</a>
     47          </li>
     48          <li>
     49            <a href="#Operation1">Operation</a>
     50            <ol>
     51              <li>
     52                <a href="#AndroidUI1">Android UI</a>
     53              </li>
     54              <li>
     55                <a href="#AndroidApplication1">Android Application</a>
     56              </li>
     57              <li>
     58                <a href="#command-line1">command-line</a>
     59              </li>
     60            </ol>
     61          </li>
     62        </ol>
     63      </li>
     64    </ol>
     65  </li>
     70See also:
     72<ul><li><a class="wiki" href="/wiki/Android#bluetooth">Android Bluetooth</a>
     73</li><li><a class="wiki" href="/wiki/Android#nfc">Android NFC</a>
     74</li></ul><h1 id="AndroidWi-Fi">Android Wi-Fi</h1>
     76Android uses the standard <a class="ext-link" href=""><span class="icon">​</span>hostapd</a> and <a class="ext-link" href=""><span class="icon">​</span>wpa_supplicant</a> users-space daemons for Access Point and authentication management. These are fairly close forks of the upstream projects with some changes specifically for Android. Source for both is currently in the external/wpa_supplicent_8 directory. Currently this relates most closely to the version 2.1-devel branch.
     79The Android network daemon <tt>netd</tt> (source in /system/netd) manages the overall Linux based network configuration listening for commands over a socket interface. There is a Network Daemon Command shell application, <tt>ndc</tt> that can be used to send/receive commands via command-line.
     82The <tt>wifi</tt> legacy module in hardware/hardware_liblegacy/wifi.c implements the API defined in <a class="ext-link" href=""><span class="icon">​</span>hardware/libhardware_legacy/include/hardware_legacy/wifi.h</a>
     85<span class="wikianchor" id="build-config"></span>
     87<h2 id="Build-timeConfiguration">Build-time Configuration</h2>
     89The file for your build target defines the details of the configuration of <tt>hostapd</tt> and <tt>wpa_supplicant</tt> used in the build process.
     92The Gateworks Android BSP defines the following:
     94<pre class="wiki"># wireless device (used by various modules to include per-device support)
     95BOARD_WLAN_DEVICE := wl12xx_mac80211
     97# Supplicant
     99BOARD_WPA_SUPPLICANT_PRIVATE_LIB := lib_driver_cmd_wl12xx
     100# skip populating rtl/conf files
     104# AP
     105HOSTAPD_VERSION                  := VER_0_8_x
     106BOARD_HOSTAPD_DRIVER             := NL80211
     107BOARD_HOSTAPD_PRIVATE_LIB        := lib_driver_cmd_wl12xx
     108</pre><ul><li>the wl12xx configuration and library are fairly mac80211 compliant and thus should provide support for all mac80211 drivers we include
     109</li><li>the 'PRIVATE_LIB's are used when communicating with the driver and allow for easy customization
     111Additional defines can be used for drivers that require specific kernel modules to be loaded or firmware to be loaded for various operation modes. The actual loading of the modules and firmware is done by <a class="ext-link" href=""><span class="icon">​</span>wifi.c</a>:
     113<pre class="wiki"># kernel modules
     114WIFI_DRIVER_MODULE_PATH - full path of kernel module(s)
     115WIFI_DRIVER_MODULE_NAME - kernel module name (w/o the ko)
     116WIFI_DRIVER_MODULE_ARG - kernel module params
     119# firmware
     120WIFI_FIRMWARE_LOADER - firmware loader
     121WIFI_DRIVER_FW_PATH_PARAM - params to pass to fw loader
     122WIFI_DRIVER_FW_PATH_AP - full path to AP firmware
     123WIFI_DRIVER_FW_PATH_STA - full path to STA firmware
     124WIFI_DRIVER_FW_PATH_P2P - full path to P2P firmware
     125</pre><ul><li>Note by default these are all blank or null meaning not used and the Gateworks BSP does not define these meaning it supports wireless devices that either need no module/firmware loading or that it is assumed that was done during
     127<span class="wikianchor" id="framework"></span>
     129<h2 id="AndroidFrameworkAPI">Android Framework API</h2>
     131The Android framework talks to {{{netd}} via the <a class="ext-link" href=""><span class="icon">​</span>NetworkManagementService class</a>
     134The <a class="ext-link" href=""><span class="icon">​</span>WifiManager</a> class provides the primary API for managing al aspects of Wi-Fi connectivity. To perform operations that pertain to network connectivity at an abstract level use <a class="ext-link" href=""><span class="icon">​</span>ConnectivityManager</a> which answers queries about the state of network connectivity and notifies applications when network connectivity changes.
     137The <a class="ext-link" href=""><span class="icon">​</span>WifiConfiguration</a> class reprsents a configured Wi-Fi network, including the security configuration. Android is not intended to provide a build that is suitable for all wireless devices and you may find that customization via the above is necessary.
     140<span class="wikianchor" id="packages"></span>
     142<h2 id="Packages">Packages</h2>
     144The Android modules/packages required for wireless are:
     146<ul><li>client mode: wpa_supplicant
     147</li><li>access point mode: hostapd hostapd_cli dnsmasq iptables
     149<span class="wikianchor" id="kernel-modules"></span>
     150<span class="wikianchor" id="firmware"></span>
     152<h2 id="FirmwareandKernelModuleloading">Firmware and Kernel Module loading</h2>
     154The <tt>libhardware_legacy</tt> module provides an API used by the Android framework that facilitates kernel module loading, firmware loading, wpa_supplicant service management, and dhcp service management. The module is heavily configurable for specific wireless devices at build-time through:
     156<ul><li> variables:
     157<pre class="wiki"># kernel Module
     162# Firmware
     168</pre></li><li>functions and their native JNI mappings via WifiNative class:
     169<ul><li>driver loading/unloading (used by android_net_wifi_WifiNative.cpp)
     170<ul><li>wifi_load_driver() / loadDriver
     171</li><li>wifi_unload_driver() / unloadDriver
     172</li><li>is_wifi_driver_loaded() / isDriverLoaded
     173</li></ul></li><li>supplicant (used by android_net_wifi_WifiNative.cpp)
     174<ul><li>wifi_start_supplicant() / startSupplicant
     175</li><li>wifi_stop_supplicant() / killSupplicant
     176</li><li>wifi_connect_to_supplicant() / connectToSupplicantNative
     177</li><li>wifi_close_supplicant_connection() / closeSupplicantConnectionNative
     178</li></ul></li><li>configuration (used by android_net_wifi_WifiNative.cpp)
     179<ul><li>wifi_wait_for_event() / waitForEventNative
     180</li><li>wifi_command() / doBoolenaCommandNative / doIntCommandNative / doStringCommandNative
     184</li></ul></li><li>firmware loading (used by SoftapController.cpp)
     187</li></ul></li></ul></li><li>paths for the user-space daemons are stored in wifi.c
     189<span class="wikianchor" id="sta"></span>
     191<h2 id="ClientMode">Client Mode</h2>
     193Android uses the standard <a class="ext-link" href=""><span class="icon">​</span>wpa_supplicant</a> users-space daemon for Access Point and authentication management on the client-side.
     195<h3 id="Configuration">Configuration</h3>
     197The <tt>wpa_supplicant.conf</tt> file is stored in /data/misc/wifi/wpa_supplicant.conf and used by the <a class="ext-link" href=""><span class="icon">​</span>WifiConfigStore class</a> and the <a class="ext-link" href=""><span class="icon">​</span>SettingsBackupAgent class</a>
     199<h3 id="Operation">Operation</h3>
     201The <tt>wpa_supplicant</tt> application runs as a backgaround daemon launched by the Android <tt>init</tt> process. It is communicated with via a socket.
     203<h4 id="AndroidUI">Android UI</h4>
     205You can use Wireless Networking Client support via the Settings App: Settings -&gt; Wi-Fi. You must enable Wi-Fi and configure one or more Wi-Fi networks at which point the <a class="missing wiki">NetworkManagementService?</a> will handle the associations.
     207<h4 id="AndroidApplication">Android Application</h4>
     209An Application can use Wireless Client mode via the  <a class="ext-link" href=""><span class="icon">​</span>WifiConfiguration class</a> (representing a configured Wi-Fi network including security) and the <a class="ext-link" href=""><span class="icon">​</span>WifiManager class</a> functions:
     211<ul><li><a class="ext-link" href=""><span class="icon">​</span>setWifiEnabled()</a> - Enable or disable Wi-Fi
     212</li><li><a class="ext-link" href=""><span class="icon">​</span>getWifiState()</a> - get wi-Fi enabled state
     213</li><li><a class="ext-link" href=""><span class="icon">​</span>isWifiEnabled()</a> - return whether Wi-Fi is enabled or disabled
     214</li><li><a class="ext-link" href=""><span class="icon">​</span>startScan()</a> - request a scan for access points
     215</li><li><a class="ext-link" href=""><span class="icon">​</span>getScanResults()</a> - returnteh results of the latest access point scan
     216</li></ul><h4 id="command-line">command-line</h4>
     218From a command-line perspective you can manually configure and launch <tt>wpa_supplicant</tt> if you wish:
     220<div class="code"><pre><span class="c"># restore default wpa_supplicant.conf and start wpa_supplicant service
     221</span>cp /system/etc/wifi/wpa_supplicant.conf /data/misc/wifi/wpa_supplicant
     222chmod 660 /data/misc/wifi/wpa_supplicant.conf
     223chwon system.wifi /data/misc/wifi/wpa_supplicant.conf
     224start wpa_supplicant
     226<span class="wikianchor" id="ap"></span>
     228<h2 id="AccessPointMode">Access Point Mode</h2>
     230Android uses the <a class="ext-link" href=""><span class="icon">​</span>hostapd</a> users-space daemon for Access Point and authentication management on the AP side. For a DHCP server it uses the typical dnsmasq server.
     233The <tt>hostapd</tt> application provides the Access Point management and is managed by the <tt>netd</tt> SoftapController class which accepts the following commands:
     235<ul><li>startap - starts hostapd
     236</li><li>stopap - stops hostapd
     237</li><li>fwreload &lt;STA|AP|P2P&gt; - reload firmware for specified operating mode
     238</li><li>status - returns if AP is running or not
     239</li><li>set &lt;wlan iface&gt; &lt;SSID &lt;hidden|broadcast&gt; &lt;channel&gt; &lt;wpa2?-psk|open&gt; &lt;passphrase&gt; - configure AP
     241The <tt>dnsmasq</tt> application provides a DHCP server and is managed by the <tt>netd</tt> TetherController class which also manages <tt>iptables</tt>.
     244The <tt>netd</tt> TetherController class accepts the following commands:
     248which accesps the following commands.
     251<ul><li>tether stop - stop tethering
     252</li><li>tether start - start tethering
     253</li><li>tether status - report tether status
     254</li><li>tether interface add &lt;iface&gt; - add an interface to the tether
     255</li><li>tether interface remove &lt;iface&gt; - remove an interface from the tether
     256</li><li>tether interface list - list tethered interfaces
     257</li><li>tether dns set &lt;dns1&gt; &lt;...&gt; - add 1 or more dns forwarders
     258</li><li>tether dns list - list dns forwarders
     260Network Address Translation (NAT) is provided by the <tt>netd</tt> NatController class which uses the <tt>iptables</tt> shell command and accepts the following commands:
     262<ul><li>nat enable &lt;internal interface&gt; &lt;externalinterface&gt; [&lt;route-rules...&gt;] - enable NAT
     263<ul><li>route-rules take the form of: &lt;action&gt; &lt;addr&gt; what will be used with the <tt>ip</tt> tool as: ip rule &lt;action&gt; from &lt;addr&gt; table
     264</li></ul></li><li>nat disable &lt;internal interface&gt; &lt;externalinterface&gt; - disable NAT
     265</li></ul><h3 id="Configuration1">Configuration</h3>
     267The <tt>hostapd.conf</tt> file is stored in /data/misc/wifi/hostapd.conf and used by the <a class="ext-link" href=""><span class="icon">​</span>SoftapController class</a>.
     270The <tt>dnsmasq</tt> application is launched by <tt>netd</tt> with all configuration available on the cmdline. It will store lease information in /data/misc/dhcp/dnsmasq.leases
     273When configuration through the Android UI, the Settings application (Settings -&gt; Wireless &amp; Networks -&gt; More... -&gt; Tethering &amp; portable hotspot -&gt; Set up Wi-Fi hotspot) will store the SSID and Passkey settings in the /data/misc/wifi/softap.conf file which are used to dynamically create /data/misc/wifi/hostapd.conf.
     275<h3 id="Operation1">Operation</h3>
     277Unlike other system services that are started from Android's init process, <tt>hostapd</tt> is started on-demand by the Network Daemon <tt>netd</tt> via it's <a class="ext-link" href=""><span class="icon">​</span>!SoftapController class</a> when it receives the <tt>softap startap</tt> command.
     280This command is sent to <tt>netd</tt> when the <a class="ext-link" href=""><span class="icon">​</span>NetworkManagementService</a> <tt>startAccessPoint</tt> function is called.
     282<h4 id="AndroidUI1">Android UI</h4>
     284From an Android UI perspective, you can enable an Access Point (aka Hotspot) and NAT routing (aka Tethering) via the Settings Application:
     286<ol><li>Goto Settings -&gt; Wireless &amp; Networks -&gt; More... -&gt; Tethering &amp; portable hotspot -&gt; Set up Wi-Fi hotspot and specify your SSID and security settings
     287</li></ol><ol start="2"><li>Goto Settings -&gt; Wireless &amp; Networks -&gt; More... -&gt; Tethering &amp; portable hotspot -&gt; Portable Wi-Fi hotspot
     289Note that enabling the Wi-Fi hotspot via the Settings GUI will not 'stick' and again be enabled on re-boot by design.
     291<h4 id="AndroidApplication1">Android Application</h4>
     293An application can use the <a class="ext-link" href=""><span class="icon">​</span>WifiManager class</a> to manage an Access Point:
     295<ul><li><a class="ext-link" href=""><span class="icon">​</span>setWifiApEnabled()</a> - start AP Mode
     296</li><li><a class="ext-link" href=""><span class="icon">​</span>getWifiApState()</a> - get Wi-Fi AP enabled state
     297</li><li><a class="ext-link" href=""><span class="icon">​</span>isWifiApEnabled()</a> - return whether Wi-Fi AP is enabled or disabled
     298</li><li><a class="ext-link" href=""><span class="icon">​</span>getWifiApConfiguration()</a> - get the Wi-Fi AP configuration
     299</li><li><a class="ext-link" href=""><span class="icon">​</span>setWifiApConfiguration()</a> - set the Wi-Fi AP configuration
     301If your wireless device requires module or firmware loading, <tt>netd</tt> will do this as well by calling <tt>fwreload &lt;STA|AP|P2P&gt;</tt> using the API defined in hardware/libhardware_legacy/include/wifi.h however note that the firmware load command and patches for each option are a build-time option using paths and arguments defined in By default these are all blank meaning no firmware or modules will be loaded.
     303<h4 id="command-line1">command-line</h4>
     305While you can manage <tt>hostapd</tt> and <tt>dnsmasq</tt> IP forwarding, and Network Address Translation manually it is likely much easier to use the API that the Network Daemon <tt>netd</tt> provides either through the Android java framework via the <a class="ext-link" href=""><span class="icon">​</span>NetworkManagementService class</a> or via command-line with its command-line interface app <tt>netd</tt>.
     308For example, you can use <tt>netd</tt> to manage an Access Point with Linux NAT Routine (aka a 'Tethered Hotspot') using <tt>ncd</tt>:
     310<ol><li>configure Access Point:
     311<div class="code"><pre>ndc softap <span class="nb">set </span>wlan0 testssid broadcast 11 wpa-psk testpass <span class="c"># config hostapd
     312</span></pre></div><ul><li>the <tt>softap set</tt> command has the usage: <tt>sotfap set &lt;iface&gt; &lt;SSID&gt; &lt;hidden|broadcast&gt; &lt;channel&gt; &lt;wpa-psk|wpa2-psk|open&gt; &lt;passphrase&gt;</tt>
     313<ul><li>/data/misc/wifi/hostapd.conf (which must be writable by netd) will be written to:
     314<pre class="wiki">interface=&lt;iface&gt;
     321</pre></li><li>for hidden an 'ignore_broadcast_ssid=1' will be added, for broadcast 'ignore_broadcast_ssid=0'
     322</li><li>for wpa-psk 'wpa=1, wpa_pairwise=TKIP CCMD, wpa_psk' entries will be added
     323</li><li>for wpa2-psk 'wpa=2, rsn_pairwise=CCMP, wpa_psk' entries will be added
     324</li></ul></li></ul></li></ol><ol start="2"><li>start the Access Point:
     325<div class="code"><pre>ndc softap startap <span class="c"># start hostapd
     326</span></pre></div><ul><li>Note that you can stop the Access Point at any time with <tt>ncd softap stopap</tt>
     327</li></ul></li></ol><ol start="3"><li>configure the wlan0 interface:
     328<div class="code"><pre>ndc interface setcfg wlan0 24 up
     329</pre></div></li></ol><ol start="4"><li>configure tethering
     330<div class="code"><pre>ndc tether interface add wlan0 <span class="c"># add wlan0 to tether
     331</span>ndc tether dns <span class="nb">set </span> <span class="c"># add DNS servers
     332</span>ndc ipfwd <span class="nb">enable</span> <span class="c"># enable IP forwarding
     333</span></pre></div></li></ol><ol start="5"><li>start tether of wlan0 with a specified DHCP pool (.10 to .99 in this case) (runs dnsmasq and configures NAT)
     334<div class="code"><pre>ndc tether start
     335</pre></div></li></ol><ol start="6"><li>start Network Address Translation (NAT)
     336<div class="code"><pre>ndc nat <span class="nb">enable </span>wlan0 eth0 0
     338Note that when using Access Point mode, you must not enable Wireless from the Settings Application as this puts the radio  in Client mode.