michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "nsWifiMonitor.h" michael@0: #include "nsWifiAccessPoint.h" michael@0: michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsComponentManagerUtils.h" michael@0: #include "nsIMutableArray.h" michael@0: michael@0: #include "plstr.h" michael@0: #include michael@0: michael@0: #define DLADM_STRSIZE 256 michael@0: #define DLADM_SECTIONS 3 michael@0: michael@0: using namespace mozilla; michael@0: michael@0: struct val_strength_t { michael@0: const char *strength_name; michael@0: int signal_value; michael@0: }; michael@0: michael@0: static val_strength_t strength_vals[] = { michael@0: { "very weak", -112 }, michael@0: { "weak", -88 }, michael@0: { "good", -68 }, michael@0: { "very good", -40 }, michael@0: { "excellent", -16 } michael@0: }; michael@0: michael@0: static nsWifiAccessPoint * michael@0: do_parse_str(char *bssid_str, char *essid_str, char *strength) michael@0: { michael@0: unsigned char mac_as_int[6] = { 0 }; michael@0: sscanf(bssid_str, "%x:%x:%x:%x:%x:%x", &mac_as_int[0], &mac_as_int[1], michael@0: &mac_as_int[2], &mac_as_int[3], &mac_as_int[4], &mac_as_int[5]); michael@0: michael@0: int signal = 0; michael@0: uint32_t strength_vals_count = sizeof(strength_vals) / sizeof (val_strength_t); michael@0: for (uint32_t i = 0; i < strength_vals_count; i++) { michael@0: if (!strncasecmp(strength, strength_vals[i].strength_name, DLADM_STRSIZE)) { michael@0: signal = strength_vals[i].signal_value; michael@0: break; michael@0: } michael@0: } michael@0: michael@0: nsWifiAccessPoint *ap = new nsWifiAccessPoint(); michael@0: if (ap) { michael@0: ap->setMac(mac_as_int); michael@0: ap->setSignal(signal); michael@0: ap->setSSID(essid_str, PL_strnlen(essid_str, DLADM_STRSIZE)); michael@0: } michael@0: return ap; michael@0: } michael@0: michael@0: static void michael@0: do_dladm(nsCOMArray &accessPoints) michael@0: { michael@0: GError *err = nullptr; michael@0: char *sout = nullptr; michael@0: char *serr = nullptr; michael@0: int exit_status = 0; michael@0: char * dladm_args[] = { "/usr/bin/pfexec", "/usr/sbin/dladm", michael@0: "scan-wifi", "-p", "-o", "BSSID,ESSID,STRENGTH" }; michael@0: michael@0: gboolean rv = g_spawn_sync("/", dladm_args, nullptr, (GSpawnFlags)0, nullptr, michael@0: nullptr, &sout, &serr, &exit_status, &err); michael@0: if (rv && !exit_status) { michael@0: char wlan[DLADM_SECTIONS][DLADM_STRSIZE+1]; michael@0: uint32_t section = 0; michael@0: uint32_t sout_scan = 0; michael@0: uint32_t wlan_put = 0; michael@0: bool escape = false; michael@0: nsWifiAccessPoint* ap; michael@0: char sout_char; michael@0: do { michael@0: sout_char = sout[sout_scan++]; michael@0: if (escape) { michael@0: escape = false; michael@0: if (sout_char != '\0') { michael@0: wlan[section][wlan_put++] = sout_char; michael@0: continue; michael@0: } michael@0: } michael@0: michael@0: if (sout_char =='\\') { michael@0: escape = true; michael@0: continue; michael@0: } michael@0: michael@0: if (sout_char == ':') { michael@0: wlan[section][wlan_put] = '\0'; michael@0: section++; michael@0: wlan_put = 0; michael@0: continue; michael@0: } michael@0: michael@0: if ((sout_char == '\0') || (sout_char == '\n')) { michael@0: wlan[section][wlan_put] = '\0'; michael@0: if (section == DLADM_SECTIONS - 1) { michael@0: ap = do_parse_str(wlan[0], wlan[1], wlan[2]); michael@0: if (ap) { michael@0: accessPoints.AppendObject(ap); michael@0: } michael@0: } michael@0: section = 0; michael@0: wlan_put = 0; michael@0: continue; michael@0: } michael@0: michael@0: wlan[section][wlan_put++] = sout_char; michael@0: michael@0: } while ((wlan_put <= DLADM_STRSIZE) && (section < DLADM_SECTIONS) && michael@0: (sout_char != '\0')); michael@0: } michael@0: michael@0: g_free(sout); michael@0: g_free(serr); michael@0: } michael@0: michael@0: nsresult michael@0: nsWifiMonitor::DoScan() michael@0: { michael@0: // Regularly get the access point data. michael@0: michael@0: nsCOMArray lastAccessPoints; michael@0: nsCOMArray accessPoints; michael@0: michael@0: while (mKeepGoing) { michael@0: michael@0: accessPoints.Clear(); michael@0: do_dladm(accessPoints); michael@0: michael@0: bool accessPointsChanged = !AccessPointsEqual(accessPoints, lastAccessPoints); michael@0: ReplaceArray(lastAccessPoints, accessPoints); michael@0: michael@0: nsresult rv = CallWifiListeners(lastAccessPoints, accessPointsChanged); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: michael@0: LOG(("waiting on monitor\n")); michael@0: michael@0: ReentrantMonitorAutoEnter mon(mReentrantMonitor); michael@0: mon.Wait(PR_SecondsToInterval(60)); michael@0: } michael@0: michael@0: return NS_OK; michael@0: }