Ieee80211BitDomainTest.cc 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. //
  2. // Copyright (C) 2014 OpenSim Ltd.
  3. //
  4. // This program is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public License
  6. // as published by the Free Software Foundation; either version 2
  7. // of the License, or (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public License
  15. // along with this program; if not, see <http://www.gnu.org/licenses/>.
  16. //
  17. #include "Ieee80211BitDomainTest.h"
  18. #include "inet/common/BitVector.h"
  19. #include "inet/common/ShortBitVector.h"
  20. #include <fstream>
  21. namespace inet {
  22. Define_Module(Ieee80211BitDomainTest);
  23. void Ieee80211BitDomainTest::initialize(int stage)
  24. {
  25. if (stage == INITSTAGE_LOCAL)
  26. {
  27. convCoder = NULL;
  28. interleaver = NULL;
  29. scrambler = NULL;
  30. testType = par("testType");
  31. if (!strcmp(testType,"convCoder"))
  32. convCoder = getModuleFromPar<ConvolutionalCoderModule>(par("convolutionalCoderModule"), this);
  33. else if(!strcmp(testType, "interleaver"))
  34. interleaver = getModuleFromPar<Ieee80211OFDMInterleaverModule>(par("interleaverModule"), this);
  35. else if(!strcmp(testType, "scrambler"))
  36. scrambler = getModuleFromPar<AdditiveScramblerModule>(par("scramblerModule"), this);
  37. else if (!strcmp(testType, "all"))
  38. {
  39. convCoder = getModuleFromPar<ConvolutionalCoderModule>(par("convolutionalCoderModule"), this);
  40. interleaver = getModuleFromPar<Ieee80211OFDMInterleaverModule>(par("interleaverModule"), this);
  41. scrambler = getModuleFromPar<AdditiveScramblerModule>(par("scramblerModule"), this);
  42. }
  43. else
  44. throw cRuntimeError("Unknown (= %s) test type", testType);
  45. }
  46. else if (stage == INITSTAGE_LAST)
  47. {
  48. const char *testFile = par("testFile");
  49. fileStream = new std::ifstream(testFile);
  50. if (!fileStream->is_open())
  51. throw cRuntimeError("Cannot open the test file: %s", testFile);
  52. int numberOfRandomErrors = par("numberOfRandomErrors");
  53. if (!strcmp(testType,"convCoder"))
  54. testConvolutionalCoder(numberOfRandomErrors);
  55. else if(!strcmp(testType, "interleaver"))
  56. testInterleaver();
  57. else if(!strcmp(testType, "scrambler"))
  58. testScrambler();
  59. else if (!strcmp(testType, "all"))
  60. testIeee80211BitDomain();
  61. }
  62. }
  63. void Ieee80211BitDomainTest::testConvolutionalCoder(unsigned int numberOfRandomErrors) const
  64. {
  65. srand(time(NULL));
  66. std::string strInput;
  67. while (*fileStream >> strInput)
  68. {
  69. BitVector input = BitVector(strInput.c_str());
  70. BitVector encoded;
  71. encoded = convCoder->encode(input);
  72. int numOfErrors = numberOfRandomErrors;
  73. while (numOfErrors--)
  74. {
  75. int pos = rand() % encoded.getSize();
  76. encoded.toggleBit(pos);
  77. }
  78. BitVector decoded = convCoder->decode(encoded).first;
  79. if (input != decoded)
  80. EV_DETAIL << "Convolutional Coder test has failed" << endl;
  81. }
  82. }
  83. void Ieee80211BitDomainTest::testScrambler() const
  84. {
  85. std::string strInput;
  86. while (*fileStream >> strInput)
  87. {
  88. BitVector input = BitVector(strInput.c_str());
  89. BitVector scrambledInput = scrambler->scramble(input);
  90. if (input != scrambler->descramble(scrambledInput))
  91. EV_DETAIL << "Descrambling has failed" << endl;
  92. }
  93. }
  94. void Ieee80211BitDomainTest::testInterleaver() const
  95. {
  96. std::string strInput;
  97. while (*fileStream >> strInput)
  98. {
  99. BitVector input = BitVector(strInput.c_str());
  100. BitVector interleavedInput = interleaver->interleave(input);
  101. if (interleaver->deinterleave(interleavedInput) != input)
  102. EV_DETAIL << "Deinterleaving has failed" << endl;
  103. }
  104. }
  105. void Ieee80211BitDomainTest::testIeee80211BitDomain() const
  106. {
  107. std::string strInput;
  108. *fileStream >> strInput;
  109. BitVector input = BitVector(strInput.c_str());
  110. // EV_DETAIL << "The scrambling sequence is: " << scrambler->getScramblingSequcene() << endl;
  111. BitVector scrambledInput = scrambler->scramble(input);
  112. BitVector bccEncodedInput = convCoder->encode(scrambledInput);
  113. BitVector interleavedInput = interleaver->interleave(bccEncodedInput);
  114. BitVector deinterleavedInput = interleaver->deinterleave(interleavedInput);
  115. if (bccEncodedInput != deinterleavedInput)
  116. EV_DETAIL << "Deinterleaving has failed" << endl;
  117. BitVector bccDecodedInput = convCoder->decode(deinterleavedInput).first;
  118. if (bccDecodedInput != scrambledInput)
  119. EV_DETAIL << "BCC decoding has failed" << endl;
  120. BitVector descrambledInput = scrambler->descramble(bccDecodedInput); // Note: scrambling and descrambling are the same operations
  121. if (descrambledInput != input)
  122. EV_DETAIL << "Descrambling has failed" << endl;
  123. }
  124. } /* namespace inet */