ie.go 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. package wifi
  2. /*
  3. 802.11 information element parsing
  4. Beacons and probe responses carry a list of information elements (IEs)
  5. describing what an access point can do. The OS hands them over as raw
  6. bytes (the Windows WLAN API does, for every BSS of a scan), so they are
  7. decoded here, in plain Go, on every platform. The input comes from the air
  8. and must be treated as untrusted: every read is bounds checked.
  9. */
  10. import (
  11. "encoding/binary"
  12. "fmt"
  13. "math"
  14. "strconv"
  15. "strings"
  16. )
  17. // Element IDs used below
  18. const (
  19. ieSSID = 0
  20. ieSupportedRates = 1
  21. ieDSParameterSet = 3
  22. ieCountry = 7
  23. ieBSSLoad = 11
  24. ieHTCapabilities = 45
  25. ieRSN = 48
  26. ieExtSupportedRate = 50
  27. ieMobilityDomain = 54
  28. ieHTOperation = 61
  29. ieRMEnabledCaps = 70
  30. ieExtCapabilities = 127
  31. ieVHTCapabilities = 191
  32. ieVHTOperation = 192
  33. ieVendorSpecific = 221
  34. ieExtension = 255
  35. ieExtHECapabilities = 35
  36. ieExtHEOperation = 36
  37. ieExtEHTOperation = 106
  38. ieExtEHTCapabilities = 108
  39. )
  40. // bssElements is what could be learnt from the IEs of one BSS
  41. type bssElements struct {
  42. SSID []byte
  43. HasSSID bool
  44. DSChannel int
  45. Country string
  46. HasRSN bool
  47. HasWPA bool
  48. rsnAKMs []uint32
  49. wpaAKMs []uint32
  50. ciphers []uint32
  51. PMF string
  52. HT bool
  53. VHT bool
  54. HE bool
  55. EHT bool
  56. htWidth int //20 or 40, 0 when no HT operation element
  57. vhtWidth int //80 or 160 when the VHT operation says so
  58. sixGWidth int //Width from the HE 6 GHz operation information
  59. ehtWidth int //Width from the EHT operation information
  60. vhtCenter int
  61. sixGCenter int
  62. ehtCenter int
  63. htCenter int
  64. htStreams int
  65. vhtStreams int
  66. heStreams int
  67. vhtMaxMCS int
  68. heMaxMCS int
  69. StationCount *int
  70. ChannelUtilization *int
  71. WPS bool
  72. Roaming []string
  73. }
  74. // forEachIE walks a list of information elements and calls fn for each
  75. // complete element. A truncated trailing element is ignored.
  76. func forEachIE(b []byte, fn func(id byte, data []byte)) {
  77. for len(b) >= 2 {
  78. id := b[0]
  79. l := int(b[1])
  80. if len(b)-2 < l {
  81. return
  82. }
  83. fn(id, b[2:2+l])
  84. b = b[2+l:]
  85. }
  86. }
  87. // parseIEs decodes the information elements of a beacon / probe response
  88. func parseIEs(b []byte) bssElements {
  89. e := bssElements{}
  90. roaming := map[string]bool{}
  91. forEachIE(b, func(id byte, d []byte) {
  92. switch id {
  93. case ieSSID:
  94. if !e.HasSSID {
  95. e.HasSSID = true
  96. e.SSID = append([]byte{}, d...)
  97. }
  98. case ieDSParameterSet:
  99. if len(d) >= 1 {
  100. e.DSChannel = int(d[0])
  101. }
  102. case ieCountry:
  103. if len(d) >= 2 && isUpperLetter(d[0]) && isUpperLetter(d[1]) {
  104. e.Country = string(d[:2])
  105. }
  106. case ieBSSLoad:
  107. if len(d) >= 3 {
  108. count := int(binary.LittleEndian.Uint16(d[0:2]))
  109. util := int(math.Round(float64(d[2]) * 100 / 255))
  110. e.StationCount = &count
  111. e.ChannelUtilization = &util
  112. }
  113. case ieHTCapabilities:
  114. e.HT = true
  115. //Supported MCS set starts at offset 3, one byte per spatial stream
  116. if len(d) >= 7 {
  117. e.htStreams = countNonZero(d[3:7])
  118. }
  119. case ieHTOperation:
  120. e.HT = true
  121. if len(d) >= 2 {
  122. primary := int(d[0])
  123. offset := d[1] & 0x03
  124. anyWidth := d[1]&0x04 != 0
  125. e.htWidth = 20
  126. if anyWidth && offset == 1 {
  127. e.htWidth = 40
  128. e.htCenter = primary + 2
  129. } else if anyWidth && offset == 3 {
  130. e.htWidth = 40
  131. e.htCenter = primary - 2
  132. }
  133. }
  134. case ieRSN:
  135. akms, ciphers, caps, ok := parseCipherSuites(d, true)
  136. if ok {
  137. e.HasRSN = true
  138. e.rsnAKMs = akms
  139. e.ciphers = ciphers
  140. if caps&0x0040 != 0 {
  141. e.PMF = "Required"
  142. } else if caps&0x0080 != 0 {
  143. e.PMF = "Capable"
  144. }
  145. }
  146. case ieMobilityDomain:
  147. roaming["802.11r"] = true
  148. case ieRMEnabledCaps:
  149. roaming["802.11k"] = true
  150. case ieExtCapabilities:
  151. //Bit 19: BSS transition management
  152. if len(d) >= 3 && d[2]&0x08 != 0 {
  153. roaming["802.11v"] = true
  154. }
  155. case ieVHTCapabilities:
  156. e.VHT = true
  157. if len(d) >= 6 {
  158. e.vhtStreams, e.vhtMaxMCS = decodeMCSMap(binary.LittleEndian.Uint16(d[4:6]), []int{7, 8, 9})
  159. }
  160. case ieVHTOperation:
  161. e.VHT = true
  162. if len(d) >= 3 {
  163. seg0 := int(d[1])
  164. seg1 := int(d[2])
  165. switch d[0] {
  166. case 1:
  167. e.vhtWidth = 80
  168. e.vhtCenter = seg0
  169. if seg1 != 0 {
  170. e.vhtWidth = 160
  171. if absInt(seg1-seg0) == 8 {
  172. e.vhtCenter = seg1
  173. }
  174. }
  175. case 2, 3:
  176. e.vhtWidth = 160
  177. e.vhtCenter = seg0
  178. }
  179. }
  180. case ieVendorSpecific:
  181. if len(d) >= 4 && d[0] == 0x00 && d[1] == 0x50 && d[2] == 0xF2 {
  182. switch d[3] {
  183. case 1:
  184. akms, ciphers, _, ok := parseCipherSuites(d[4:], false)
  185. if ok {
  186. e.HasWPA = true
  187. e.wpaAKMs = akms
  188. if !e.HasRSN {
  189. e.ciphers = ciphers
  190. }
  191. }
  192. case 4:
  193. e.WPS = true
  194. }
  195. }
  196. case ieExtension:
  197. if len(d) < 1 {
  198. return
  199. }
  200. ext := d[0]
  201. d = d[1:]
  202. switch ext {
  203. case ieExtHECapabilities:
  204. e.HE = true
  205. //MAC caps (6) + PHY caps (11), then the Rx HE-MCS map for <= 80 MHz
  206. if len(d) >= 19 {
  207. e.heStreams, e.heMaxMCS = decodeMCSMap(binary.LittleEndian.Uint16(d[17:19]), []int{7, 9, 11})
  208. }
  209. case ieExtHEOperation:
  210. e.HE = true
  211. e.parseHEOperation(d)
  212. case ieExtEHTCapabilities:
  213. e.EHT = true
  214. case ieExtEHTOperation:
  215. e.EHT = true
  216. //Parameters (1) + basic EHT-MCS set (4), then the optional operation info
  217. if len(d) >= 8 && d[0]&0x01 != 0 {
  218. e.ehtWidth = []int{20, 40, 80, 160, 320, 0, 0, 0}[d[5]&0x07]
  219. e.ehtCenter = int(d[6])
  220. if d[7] != 0 {
  221. e.ehtCenter = int(d[7])
  222. }
  223. }
  224. }
  225. }
  226. })
  227. for _, r := range []string{"802.11k", "802.11v", "802.11r"} {
  228. if roaming[r] {
  229. e.Roaming = append(e.Roaming, r)
  230. }
  231. }
  232. return e
  233. }
  234. // parseHEOperation reads the 6 GHz operation information of an HE operation element
  235. func (e *bssElements) parseHEOperation(d []byte) {
  236. if len(d) < 6 {
  237. return
  238. }
  239. params := uint32(d[0]) | uint32(d[1])<<8 | uint32(d[2])<<16
  240. p := 6 //Parameters (3) + BSS color (1) + basic HE-MCS set (2)
  241. if params&(1<<14) != 0 {
  242. p += 3 //VHT operation information
  243. }
  244. if params&(1<<15) != 0 {
  245. p++ //Co-hosted BSS
  246. }
  247. if params&(1<<17) == 0 || len(d) < p+4 {
  248. return
  249. }
  250. e.sixGWidth = []int{20, 40, 80, 160}[d[p+1]&0x03]
  251. e.sixGCenter = int(d[p+2])
  252. if e.sixGWidth == 160 && d[p+3] != 0 {
  253. e.sixGCenter = int(d[p+3])
  254. }
  255. }
  256. // parseCipherSuites decodes an RSN element, or the body of a WPA (v1) vendor
  257. // element after its OUI and type. It returns the AKM suite types, the pairwise
  258. // cipher types and (RSN only) the capability field.
  259. func parseCipherSuites(d []byte, rsn bool) (akms []uint32, ciphers []uint32, caps uint16, ok bool) {
  260. oui := []byte{0x00, 0x0F, 0xAC}
  261. if !rsn {
  262. oui = []byte{0x00, 0x50, 0xF2}
  263. }
  264. if len(d) < 2 {
  265. return nil, nil, 0, false
  266. }
  267. d = d[2:] //Version
  268. if len(d) < 4 {
  269. //Only the version: the defaults (CCMP / 802.1X) for RSN
  270. if rsn {
  271. return []uint32{1}, []uint32{4}, 0, true
  272. }
  273. return nil, nil, 0, true
  274. }
  275. d = d[4:] //Group data cipher
  276. readList := func() ([]uint32, bool) {
  277. if len(d) < 2 {
  278. return nil, false
  279. }
  280. n := int(binary.LittleEndian.Uint16(d[0:2]))
  281. d = d[2:]
  282. if n > len(d)/4 {
  283. return nil, false
  284. }
  285. list := []uint32{}
  286. for i := 0; i < n; i++ {
  287. s := d[i*4 : i*4+4]
  288. if s[0] == oui[0] && s[1] == oui[1] && s[2] == oui[2] {
  289. list = append(list, uint32(s[3]))
  290. }
  291. }
  292. d = d[n*4:]
  293. return list, true
  294. }
  295. ciphers, okList := readList()
  296. if !okList {
  297. return nil, nil, 0, true
  298. }
  299. akms, okList = readList()
  300. if !okList {
  301. return nil, ciphers, 0, true
  302. }
  303. if rsn && len(d) >= 2 {
  304. caps = binary.LittleEndian.Uint16(d[0:2])
  305. }
  306. return akms, ciphers, caps, true
  307. }
  308. // decodeMCSMap reads a VHT / HE style MCS map (2 bits per spatial stream,
  309. // 3 = not supported). It returns the stream count and the highest MCS of the
  310. // first stream, picked from maxByCode.
  311. func decodeMCSMap(m uint16, maxByCode []int) (streams int, maxMCS int) {
  312. for i := 0; i < 8; i++ {
  313. code := int(m>>(2*i)) & 0x03
  314. if code == 3 {
  315. continue
  316. }
  317. streams = i + 1
  318. if i == 0 {
  319. maxMCS = maxByCode[code]
  320. }
  321. }
  322. return streams, maxMCS
  323. }
  324. // securityName describes the authentication an AP offers
  325. func (e *bssElements) securityName(privacy bool) string {
  326. has := func(list []uint32, types ...uint32) bool {
  327. for _, a := range list {
  328. for _, t := range types {
  329. if a == t {
  330. return true
  331. }
  332. }
  333. }
  334. return false
  335. }
  336. if e.HasRSN {
  337. sae := has(e.rsnAKMs, 8, 9, 24, 25)
  338. psk := has(e.rsnAKMs, 2, 4, 6)
  339. suiteB := has(e.rsnAKMs, 12, 13)
  340. ent := has(e.rsnAKMs, 1, 3, 5)
  341. owe := has(e.rsnAKMs, 18)
  342. wpaPrefix := "WPA2"
  343. if e.HasWPA {
  344. wpaPrefix = "WPA/WPA2"
  345. }
  346. switch {
  347. case sae && psk:
  348. return "WPA2/WPA3-Personal"
  349. case sae:
  350. return "WPA3-Personal"
  351. case psk:
  352. return wpaPrefix + "-Personal"
  353. case suiteB:
  354. return "WPA3-Enterprise 192-bit"
  355. case ent && e.PMF == "Required" && has(e.rsnAKMs, 5) && !has(e.rsnAKMs, 1, 3):
  356. return "WPA3-Enterprise"
  357. case ent:
  358. return wpaPrefix + "-Enterprise"
  359. case owe:
  360. return "Enhanced Open (OWE)"
  361. }
  362. return "WPA2"
  363. }
  364. if e.HasWPA {
  365. if has(e.wpaAKMs, 1) {
  366. return "WPA-Enterprise"
  367. }
  368. return "WPA-Personal"
  369. }
  370. if privacy {
  371. return "WEP"
  372. }
  373. return "Open"
  374. }
  375. // cipherName joins the pairwise ciphers an AP offers
  376. func (e *bssElements) cipherName(privacy bool) string {
  377. names := []string{}
  378. for _, c := range e.ciphers {
  379. n := rsnCipherName(c)
  380. if n != "" && !containsString(names, n) {
  381. names = append(names, n)
  382. }
  383. }
  384. if len(names) == 0 && privacy && !e.HasRSN && !e.HasWPA {
  385. return "WEP"
  386. }
  387. return strings.Join(names, " + ")
  388. }
  389. func rsnCipherName(t uint32) string {
  390. switch t {
  391. case 1:
  392. return "WEP-40"
  393. case 2:
  394. return "TKIP"
  395. case 4:
  396. return "AES-CCMP"
  397. case 5:
  398. return "WEP-104"
  399. case 8:
  400. return "AES-GCMP"
  401. case 9:
  402. return "AES-GCMP-256"
  403. case 10:
  404. return "AES-CCMP-256"
  405. }
  406. return ""
  407. }
  408. // phyInfo works out the 802.11 amendment, Wi-Fi generation, channel width and
  409. // center channel of a BSS from its elements and band.
  410. func (e *bssElements) phyInfo(band string, legacyMaxRate float64) (phy string, generation string, width int, center int) {
  411. width = 20
  412. if e.htWidth > 0 {
  413. width = e.htWidth
  414. center = e.htCenter
  415. }
  416. if band != "2.4 GHz" && e.vhtWidth > width {
  417. width = e.vhtWidth
  418. center = e.vhtCenter
  419. }
  420. if band == "6 GHz" && e.sixGWidth > 0 {
  421. width = e.sixGWidth
  422. center = e.sixGCenter
  423. }
  424. if e.ehtWidth > 0 {
  425. width = e.ehtWidth
  426. center = e.ehtCenter
  427. }
  428. switch {
  429. case e.EHT:
  430. return "802.11be", "Wi-Fi 7", width, center
  431. case e.HE && band == "6 GHz":
  432. return "802.11ax", "Wi-Fi 6E", width, center
  433. case e.HE:
  434. return "802.11ax", "Wi-Fi 6", width, center
  435. case e.VHT && band != "2.4 GHz":
  436. return "802.11ac", "Wi-Fi 5", width, center
  437. case e.HT:
  438. return "802.11n", "Wi-Fi 4", width, center
  439. case band == "60 GHz":
  440. return "802.11ad", "", width, center
  441. case band == "5 GHz":
  442. return "802.11a", "", width, center
  443. case legacyMaxRate > 11:
  444. return "802.11g", "", width, center
  445. case legacyMaxRate > 0:
  446. return "802.11b", "", width, center
  447. }
  448. return "", "", width, center
  449. }
  450. // spatialStreams returns the stream count of the newest PHY the AP supports
  451. func (e *bssElements) spatialStreams() int {
  452. for _, s := range []int{e.heStreams, e.vhtStreams, e.htStreams} {
  453. if s > 0 {
  454. return s
  455. }
  456. }
  457. return 0
  458. }
  459. // estimateMaxRate estimates the highest PHY rate (Mbps) an AP can offer from
  460. // its newest PHY, channel width and spatial streams, using the short guard
  461. // interval rates of the highest MCS it advertises.
  462. func estimateMaxRate(phy string, width int, streams int, maxMCS int, legacyMax float64) float64 {
  463. if streams <= 0 {
  464. streams = 1
  465. }
  466. col := map[int]int{20: 0, 40: 1, 80: 2, 160: 3, 320: 4}
  467. c, ok := col[width]
  468. if !ok {
  469. c = 0
  470. }
  471. var perStream float64
  472. switch phy {
  473. case "802.11be":
  474. perStream = []float64{172.059, 344.118, 720.588, 1441.176, 2882.353}[c]
  475. case "802.11ax":
  476. table := map[int][]float64{
  477. 7: {86.029, 172.059, 360.294, 720.588, 720.588},
  478. 9: {114.706, 229.412, 480.392, 960.784, 960.784},
  479. 11: {143.382, 286.765, 600.490, 1200.980, 1200.980},
  480. }
  481. row, ok := table[maxMCS]
  482. if !ok {
  483. row = table[11]
  484. }
  485. perStream = row[c]
  486. case "802.11ac":
  487. table := map[int][]float64{
  488. 7: {72.222, 150, 325, 650, 650},
  489. 8: {86.667, 180, 390, 780, 780},
  490. 9: {86.667, 200, 433.333, 866.667, 866.667},
  491. }
  492. row, ok := table[maxMCS]
  493. if !ok {
  494. row = table[9]
  495. }
  496. perStream = row[c]
  497. case "802.11n":
  498. if streams > 4 {
  499. streams = 4
  500. }
  501. perStream = 72.222
  502. if width >= 40 {
  503. perStream = 150
  504. }
  505. default:
  506. return legacyMax
  507. }
  508. return math.Round(perStream*float64(streams)*10) / 10
  509. }
  510. // legacyRates splits a rate set (units of 500 kbps, high bit = basic rate)
  511. // into the basic and the full list, formatted in Mbps, plus the highest rate
  512. func legacyRates(rates []uint16) (basic []string, all []string, max float64) {
  513. basic = []string{}
  514. all = []string{}
  515. for _, r := range rates {
  516. mbps := float64(r&0x7FFF) / 2
  517. if mbps <= 0 {
  518. continue
  519. }
  520. s := strconv.FormatFloat(mbps, 'f', -1, 64)
  521. if !containsString(all, s) {
  522. all = append(all, s)
  523. }
  524. if r&0x8000 != 0 && !containsString(basic, s) {
  525. basic = append(basic, s)
  526. }
  527. if mbps > max {
  528. max = mbps
  529. }
  530. }
  531. return basic, all, max
  532. }
  533. // ratesFromIE reads the supported / extended supported rates elements, which
  534. // use the same encoding as legacyRates but with the basic flag in bit 7
  535. func ratesFromIE(b []byte) []uint16 {
  536. rates := []uint16{}
  537. forEachIE(b, func(id byte, d []byte) {
  538. if id != ieSupportedRates && id != ieExtSupportedRate {
  539. return
  540. }
  541. for _, r := range d {
  542. v := uint16(r & 0x7F)
  543. if r&0x80 != 0 {
  544. v |= 0x8000
  545. }
  546. rates = append(rates, v)
  547. }
  548. })
  549. return rates
  550. }
  551. /*
  552. Channel and band helpers
  553. */
  554. // bandOfFrequency returns the band a center frequency (MHz) belongs to
  555. func bandOfFrequency(mhz int) string {
  556. switch {
  557. case mhz >= 2400 && mhz < 2500:
  558. return "2.4 GHz"
  559. case mhz >= 4900 && mhz < 5925:
  560. return "5 GHz"
  561. case mhz >= 5925 && mhz <= 7125:
  562. return "6 GHz"
  563. case mhz >= 57000 && mhz <= 71000:
  564. return "60 GHz"
  565. }
  566. return ""
  567. }
  568. // channelOfFrequency returns the channel number of a center frequency (MHz)
  569. func channelOfFrequency(mhz int) int {
  570. switch {
  571. case mhz == 2484:
  572. return 14
  573. case mhz >= 2412 && mhz <= 2472:
  574. return (mhz - 2407) / 5
  575. case mhz == 5935:
  576. return 2
  577. case mhz > 5950 && mhz <= 7115:
  578. return (mhz - 5950) / 5
  579. case mhz >= 4910 && mhz <= 4980:
  580. return (mhz - 4000) / 5
  581. case mhz >= 5000 && mhz < 5925:
  582. return (mhz - 5000) / 5
  583. case mhz >= 58320 && mhz <= 70200:
  584. return (mhz - 56160) / 2160
  585. }
  586. return 0
  587. }
  588. // frequencyOfChannel guesses the center frequency (MHz) of a channel when
  589. // only the channel number is known. Channels that exist in both the 5 GHz
  590. // and the 6 GHz band resolve to 5 GHz unless sixGHz is set.
  591. func frequencyOfChannel(ch int, sixGHz bool) int {
  592. switch {
  593. case ch <= 0:
  594. return 0
  595. case sixGHz && ch == 2:
  596. return 5935
  597. case sixGHz && ch <= 233:
  598. return 5950 + ch*5
  599. case ch == 14:
  600. return 2484
  601. case ch < 14:
  602. return 2407 + ch*5
  603. case ch >= 182 && ch <= 196:
  604. return 4000 + ch*5
  605. case ch <= 177:
  606. return 5000 + ch*5
  607. }
  608. return 0
  609. }
  610. // formatFrequency renders MHz as "5.180 GHz"
  611. func formatFrequency(mhz int) string {
  612. if mhz <= 0 {
  613. return ""
  614. }
  615. return fmt.Sprintf("%.3f GHz", float64(mhz)/1000)
  616. }
  617. // generationOfPHY maps an 802.11 amendment to its Wi-Fi generation name
  618. func generationOfPHY(phy string, band string) string {
  619. switch phy {
  620. case "802.11be":
  621. return "Wi-Fi 7"
  622. case "802.11ax":
  623. if band == "6 GHz" {
  624. return "Wi-Fi 6E"
  625. }
  626. return "Wi-Fi 6"
  627. case "802.11ac":
  628. return "Wi-Fi 5"
  629. case "802.11n":
  630. return "Wi-Fi 4"
  631. }
  632. return ""
  633. }
  634. // qualityFromRSSI maps an RSSI (dBm) to a 0 - 100 quality the way Windows
  635. // does: -100 dBm and below is 0 %, -50 dBm and above is 100 %
  636. func qualityFromRSSI(dbm int) int {
  637. if dbm == 0 {
  638. return -1
  639. }
  640. q := 2 * (dbm + 100)
  641. if q < 0 {
  642. return 0
  643. }
  644. if q > 100 {
  645. return 100
  646. }
  647. return q
  648. }
  649. // rssiFromQuality is the inverse of qualityFromRSSI
  650. func rssiFromQuality(q int) int {
  651. if q < 0 {
  652. return 0
  653. }
  654. return q/2 - 100
  655. }
  656. /*
  657. Small helpers
  658. */
  659. // ssidString turns raw SSID bytes into a string, reporting hidden SSIDs
  660. // (empty or all zero) as hidden
  661. func ssidString(b []byte) (string, bool) {
  662. allZero := true
  663. for _, c := range b {
  664. if c != 0 {
  665. allZero = false
  666. break
  667. }
  668. }
  669. if len(b) == 0 || allZero {
  670. return "", true
  671. }
  672. return strings.ToValidUTF8(string(b), "\uFFFD"), false
  673. }
  674. func formatMAC(b []byte) string {
  675. parts := make([]string, len(b))
  676. for i, c := range b {
  677. parts[i] = fmt.Sprintf("%02x", c)
  678. }
  679. return strings.Join(parts, ":")
  680. }
  681. func isUpperLetter(c byte) bool {
  682. return c >= 'A' && c <= 'Z'
  683. }
  684. func countNonZero(b []byte) int {
  685. n := 0
  686. for _, c := range b {
  687. if c != 0 {
  688. n++
  689. }
  690. }
  691. return n
  692. }
  693. func absInt(v int) int {
  694. if v < 0 {
  695. return -v
  696. }
  697. return v
  698. }
  699. func containsString(list []string, s string) bool {
  700. for _, v := range list {
  701. if v == s {
  702. return true
  703. }
  704. }
  705. return false
  706. }