SignalToNoise1.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or
  8. * without modification, are permitted (subject to the limitations
  9. * in the disclaimer below) provided that the following conditions
  10. * are met:
  11. *
  12. * * Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * * Redistributions in binary form must reproduce the above
  16. * copyright notice, this list of conditions and the following
  17. * disclaimer in the documentation and/or other materials
  18. * provided with the distribution.
  19. *
  20. * * Neither the name of Qualcomm Atheros nor the names of
  21. * its contributors may be used to endorse or promote products
  22. * derived from this software without specific prior written
  23. * permission.
  24. *
  25. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE
  26. * GRANTED BY THIS LICENSE. THIS SOFTWARE IS PROVIDED BY THE
  27. * COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
  28. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  29. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  30. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
  31. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  32. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  33. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  34. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  35. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
  37. * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  38. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. *--------------------------------------------------------------------*/
  41. /*====================================================================*
  42. *
  43. * signed SignalToNoise1 (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. *
  48. * Contributor(s):
  49. * Charles Maier
  50. *
  51. *--------------------------------------------------------------------*/
  52. #ifndef SIGNALTONOISE1_SOURCE
  53. #define SIGNALTONOISE1_SOURCE
  54. #include <stdio.h>
  55. #include <stdint.h>
  56. #include "../tools/flags.h"
  57. #include "../tools/error.h"
  58. #include "../tools/endian.h"
  59. #include "../plc/plc.h"
  60. #ifndef TONEMAPS1_SOURCE
  61. static uint8_t const mod2bits [AMP_BITS] =
  62. {
  63. 0,
  64. 1,
  65. 2,
  66. 3,
  67. 4,
  68. 6,
  69. 8,
  70. 10,
  71. 12
  72. };
  73. static uint8_t const mod2db [AMP_BITS] =
  74. {
  75. 0,
  76. 2,
  77. 4,
  78. 7,
  79. 10,
  80. 16,
  81. 22,
  82. 28,
  83. 36
  84. };
  85. #endif
  86. signed SignalToNoise1 (struct plc * plc)
  87. {
  88. struct channel * channel = (struct channel *)(plc->channel);
  89. struct message * message = (struct message *)(plc->message);
  90. byte tonemap [PLC_SLOTS + 1][AMP_CARRIERS >> 1];
  91. byte GIL [PLC_SLOTS];
  92. byte AGC [PLC_SLOTS];
  93. double SNR [PLC_SLOTS] =
  94. {
  95. 0.0
  96. };
  97. double BPC [PLC_SLOTS] =
  98. {
  99. 0.0
  100. };
  101. double AvgSNR = 0;
  102. double AvgBPC = 0;
  103. unsigned carriers = 0;
  104. unsigned carrier = 0;
  105. unsigned slots = 0;
  106. unsigned slot = 0;
  107. #ifndef __GNUC__
  108. #pragma pack (push,1)
  109. #endif
  110. struct __packed vs_tone_slot_char_request
  111. {
  112. struct ethernet_hdr ethernet;
  113. struct qualcomm_hdr qualcomm;
  114. uint8_t MACADDRESS [ETHER_ADDR_LEN];
  115. uint8_t TMSLOT;
  116. }
  117. * request = (struct vs_tone_slot_char_request *) (message);
  118. struct __packed vs_tone_slot_char_confirm
  119. {
  120. struct ethernet_hdr ethernet;
  121. struct qualcomm_hdr qualcomm;
  122. uint8_t MSTATUS;
  123. uint8_t TMSLOT;
  124. uint8_t NUMTMS;
  125. uint16_t TMNUMACTCARRIERS;
  126. uint8_t MOD_CARRIER [(INT_CARRIERS + 1) >> 1];
  127. uint8_t GIL;
  128. uint8_t AGC;
  129. }
  130. * confirm = (struct vs_tone_slot_char_confirm *) (message);
  131. #ifndef __GNUC__
  132. #pragma pack (pop)
  133. #endif
  134. memset (message, 0, sizeof (* message));
  135. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  136. QualcommHeader (&request->qualcomm, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_REQ));
  137. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  138. memcpy (request->MACADDRESS, plc->RDA, sizeof (request->MACADDRESS));
  139. request->TMSLOT = 0;
  140. if (SendMME (plc) <= 0)
  141. {
  142. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  143. return (-1);
  144. }
  145. if (ReadMME (plc, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  146. {
  147. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  148. return (-1);
  149. }
  150. carriers = LE16TOH (confirm->TMNUMACTCARRIERS);
  151. slots = confirm->NUMTMS;
  152. if (!slots)
  153. {
  154. error (PLC_EXIT (plc), ECANCELED, "No Tone Maps Available");
  155. return (-1);
  156. }
  157. memset (tonemap, 0, sizeof (tonemap));
  158. for (slot = 0; slot < slots; slot++)
  159. {
  160. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  161. QualcommHeader (&request->qualcomm, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_REQ));
  162. memcpy (request->MACADDRESS, plc->RDA, sizeof (request->MACADDRESS));
  163. request->TMSLOT = slot;
  164. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  165. if (SendMME (plc) <= 0)
  166. {
  167. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  168. return (-1);
  169. }
  170. if (ReadMME (plc, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  171. {
  172. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  173. return (-1);
  174. }
  175. if (confirm->MSTATUS)
  176. {
  177. Failure (plc, PLC_WONTDOIT);
  178. return (-1);
  179. }
  180. memcpy (&tonemap [slot][0], confirm->MOD_CARRIER, sizeof (confirm->MOD_CARRIER));
  181. GIL [slot] = confirm->GIL;
  182. AGC [slot] = confirm->AGC;
  183. }
  184. while (carrier < carriers)
  185. {
  186. unsigned value = 0;
  187. unsigned index = carrier >> 1;
  188. for (slot = 0; slot < PLC_SLOTS; slot++)
  189. {
  190. value = tonemap [slot][index];
  191. if ((carrier & 1))
  192. {
  193. value >>= 4;
  194. }
  195. value &= 0x0F;
  196. BPC [slot] += mod2bits [value];
  197. SNR [slot] += mod2db [value];
  198. AvgBPC += mod2bits [value];
  199. AvgSNR += mod2db [value];
  200. }
  201. carrier++;
  202. }
  203. AvgBPC /= carriers;
  204. AvgBPC /= slots;
  205. AvgSNR /= carriers;
  206. AvgSNR /= slots;
  207. printf (" SNR");
  208. for (slot = 0; slot < slots; slot++)
  209. {
  210. printf (" %8.3f", SNR [slot] / carriers);
  211. }
  212. printf (" %8.3f", AvgSNR);
  213. printf (" \n");
  214. printf (" ATN");
  215. for (slot = 0; slot < slots; slot++)
  216. {
  217. printf (" %8.3f", SNR [slot] / carriers - 60);
  218. }
  219. printf (" %8.3f", AvgSNR - 60);
  220. printf (" \n");
  221. printf (" BPC");
  222. for (slot = 0; slot < slots; slot++)
  223. {
  224. printf (" %8.3f", BPC [slot] / carriers);
  225. }
  226. printf (" %8.3f", AvgBPC);
  227. printf (" \n");
  228. printf (" AGC");
  229. for (slot = 0; slot < slots; slot++)
  230. {
  231. printf (" %02d", AGC [slot]);
  232. }
  233. printf (" \n");
  234. printf (" GIL");
  235. for (slot = 0; slot < slots; slot++)
  236. {
  237. printf (" %02d", GIL [slot]);
  238. }
  239. printf (" \n");
  240. return (0);
  241. }
  242. #endif