You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

102 lines
2.1KB

  1. #include "channel.h"
  2. #include "ieee80211.h"
  3. void awdl_chanseq_init(struct awdl_chan *seq) {
  4. for (int i = 0; i < AWDL_CHANSEQ_LENGTH; i++, seq++) {
  5. if (i < 8)
  6. *seq = CHAN_OPCLASS_149;
  7. else
  8. *seq = CHAN_OPCLASS_6;
  9. }
  10. }
  11. void awdl_chanseq_init_idle(struct awdl_chan *seq) {
  12. for (int i = 0; i < AWDL_CHANSEQ_LENGTH; i++, seq++) {
  13. switch (i) {
  14. case 8:
  15. *seq = CHAN_OPCLASS_6;
  16. break;
  17. case 0:
  18. case 9:
  19. case 10:
  20. *seq = CHAN_OPCLASS_149;
  21. break;
  22. default:
  23. *seq = CHAN_NULL;
  24. break;
  25. }
  26. }
  27. }
  28. void awdl_chanseq_init_static(struct awdl_chan *seq, const struct awdl_chan *chan) {
  29. for (int i = 0; i < AWDL_CHANSEQ_LENGTH; i++, seq++) {
  30. *seq = *chan;
  31. }
  32. }
  33. uint8_t awdl_chan_num(struct awdl_chan chan, enum awdl_chan_encoding enc) {
  34. switch (enc) {
  35. case AWDL_CHAN_ENC_SIMPLE:
  36. return chan.simple.chan_num;
  37. case AWDL_CHAN_ENC_LEGACY:
  38. return chan.legacy.chan_num;
  39. case AWDL_CHAN_ENC_OPCLASS:
  40. return chan.opclass.chan_num;
  41. default:
  42. return 0; /* unknown encoding */
  43. }
  44. }
  45. int awdl_chan_encoding_size(enum awdl_chan_encoding enc) {
  46. switch (enc) {
  47. case AWDL_CHAN_ENC_SIMPLE:
  48. return 1;
  49. case AWDL_CHAN_ENC_LEGACY:
  50. case AWDL_CHAN_ENC_OPCLASS:
  51. return 2;
  52. default:
  53. return -1; /* unknown encoding */
  54. }
  55. }
  56. /* adapted from iw/util.c */
  57. int ieee80211_channel_to_frequency(int chan) {
  58. /* see 802.11 17.3.8.3.2 and Annex J
  59. * there are overlapping channel numbers in 5GHz and 2GHz bands */
  60. if (chan <= 0)
  61. return 0; /* not supported */
  62. /* 2 GHz band */
  63. if (chan == 14)
  64. return 2484;
  65. else if (chan < 14)
  66. return 2407 + chan * 5;
  67. /* 5 GHz band */
  68. if (chan < 32)
  69. return 0; /* not supported */
  70. if (chan >= 182 && chan <= 196)
  71. return 4000 + chan * 5;
  72. else
  73. return 5000 + chan * 5;
  74. }
  75. /* from iw/util.c */
  76. int ieee80211_frequency_to_channel(int freq) {
  77. /* see 802.11-2007 17.3.8.3.2 and Annex J */
  78. if (freq == 2484)
  79. return 14;
  80. else if (freq < 2484)
  81. return (freq - 2407) / 5;
  82. else if (freq >= 4910 && freq <= 4980)
  83. return (freq - 4000) / 5;
  84. else if (freq <= 45000) /* DMG band lower limit */
  85. return (freq - 5000) / 5;
  86. else if (freq >= 58320 && freq <= 64800)
  87. return (freq - 56160) / 2160;
  88. else
  89. return 0;
  90. }