SignalToNoise2.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403
  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 SignalToNoise2 (struct plc * plc);
  44. *
  45. * amp.h
  46. *
  47. *
  48. * Contributor(s):
  49. * Charles Maier
  50. *
  51. *--------------------------------------------------------------------*/
  52. #ifndef SIGNALTONOISE2_SOURCE
  53. #define SIGNALTONOISE2_SOURCE
  54. #include <stdio.h>
  55. #include <stdint.h>
  56. #include "../tools/types.h"
  57. #include "../tools/flags.h"
  58. #include "../tools/error.h"
  59. #include "../tools/endian.h"
  60. #include "../plc/plc.h"
  61. #include "../mme/mme.h"
  62. #ifndef TONEMAPS2_SOURCE
  63. static uint8_t const mod2bits [AMP_BITS] =
  64. {
  65. 0,
  66. 1,
  67. 2,
  68. 3,
  69. 4,
  70. 6,
  71. 8,
  72. 10,
  73. 12
  74. };
  75. #endif
  76. #ifndef TONEMAPS2_SOURCE
  77. static uint8_t const mod2db [AMP_BITS] =
  78. {
  79. 0,
  80. 2,
  81. 4,
  82. 7,
  83. 10,
  84. 16,
  85. 22,
  86. 28,
  87. 36
  88. };
  89. #endif
  90. signed SignalToNoise2 (struct plc * plc)
  91. {
  92. struct channel * channel = (struct channel *)(plc->channel);
  93. struct message * message = (struct message *)(plc->message);
  94. byte tonemap [PLC_SLOTS + 1][AMP_CARRIERS >> 1];
  95. uint16_t GIL [PLC_SLOTS];
  96. uint16_t AGC [PLC_SLOTS];
  97. double SNR [PLC_SLOTS];
  98. double BPC [PLC_SLOTS];
  99. double AvgSNR;
  100. double AvgBPC;
  101. uint16_t extent = 0;
  102. uint16_t active = 0;
  103. uint16_t carriers = AMP_CARRIERS;
  104. uint16_t carrier = 0;
  105. uint8_t slots = PLC_SLOTS;
  106. uint8_t slot = 0;
  107. #ifndef __GNUC__
  108. #pragma pack (push,1)
  109. #endif
  110. struct __packed vs_rx_tone_map_char_request
  111. {
  112. struct ethernet_hdr ethernet;
  113. struct qualcomm_fmi qualcomm;
  114. uint8_t MME_SUBVER;
  115. uint8_t Reserved1 [3];
  116. uint8_t MACADDRESS [ETHER_ADDR_LEN];
  117. uint8_t TMSLOT;
  118. uint8_t COUPLING;
  119. }
  120. * request = (struct vs_rx_tone_map_char_request *) (message);
  121. struct __packed vs_rx_tonemap_char_confirm
  122. {
  123. struct ethernet_hdr ethernet;
  124. struct qualcomm_fmi qualcomm;
  125. uint8_t MSTATUS;
  126. uint8_t Reserved1;
  127. uint16_t MME_LEN;
  128. uint8_t MME_SUBVER;
  129. uint8_t Reserved2;
  130. uint8_t MACADDR [6];
  131. uint8_t TMSLOT;
  132. uint8_t COUPLING;
  133. uint8_t NUMTMS;
  134. uint8_t Reserved4;
  135. uint16_t TMNUMACTCARRIERS;
  136. uint32_t Reserved6;
  137. uint8_t GIL;
  138. uint8_t Reserved7;
  139. uint8_t AGC;
  140. uint8_t Reserved8;
  141. uint8_t MOD_CARRIER [1];
  142. }
  143. * confirm = (struct vs_rx_tonemap_char_confirm *) (message);
  144. struct __packed vs_rx_tonemap_char_fragment
  145. {
  146. struct ethernet_hdr ethernet;
  147. struct homeplug_fmi qualcomm;
  148. uint8_t MOD_CARRIER [1];
  149. }
  150. * fragment = (struct vs_rx_tonemap_char_fragment *) (message);
  151. #ifndef __GNUC__
  152. #pragma pack (pop)
  153. #endif
  154. memset (tonemap, 0, sizeof (tonemap));
  155. for (carrier = slot = 0; slot < slots; carrier = 0, slot++)
  156. {
  157. memset (message, 0, sizeof (* message));
  158. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  159. QualcommHeader1 (&request->qualcomm, 1, (VS_RX_TONE_MAP_CHAR | MMTYPE_REQ));
  160. memcpy (request->MACADDRESS, plc->RDA, sizeof (request->MACADDRESS));
  161. request->TMSLOT = slot;
  162. request->COUPLING = plc->coupling;
  163. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  164. if (SendMME (plc) <= 0)
  165. {
  166. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  167. return (-1);
  168. }
  169. if (ReadMME (plc, 1, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  170. {
  171. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  172. return (-1);
  173. }
  174. if (confirm->MSTATUS)
  175. {
  176. error (1, 0, "Device refused request for slot %d: %s", slot, MMECode (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF, confirm->MSTATUS));
  177. }
  178. GIL [slot] = confirm->GIL;
  179. AGC [slot] = confirm->AGC;
  180. carriers = LE16TOH (confirm->TMNUMACTCARRIERS);
  181. slots = confirm->NUMTMS;
  182. extent = LE16TOH (confirm->MME_LEN) - 22;
  183. if (extent > (AMP_CARRIERS >> 1))
  184. {
  185. error (1, EOVERFLOW, "Too many carriers");
  186. }
  187. plc->packetsize -= sizeof (struct vs_rx_tonemap_char_confirm);
  188. plc->packetsize += sizeof (confirm->MOD_CARRIER);
  189. if (plc->packetsize > extent)
  190. {
  191. plc->packetsize = extent;
  192. }
  193. memcpy (&tonemap [slot] [carrier], &confirm->MOD_CARRIER, plc->packetsize);
  194. carrier += plc->packetsize;
  195. extent -= plc->packetsize;
  196. while (extent)
  197. {
  198. if (ReadMME (plc, 1, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  199. {
  200. error (1, errno, CHANNEL_CANTREAD);
  201. }
  202. plc->packetsize -= sizeof (struct vs_rx_tonemap_char_fragment);
  203. plc->packetsize += sizeof (fragment->MOD_CARRIER);
  204. if (plc->packetsize > extent)
  205. {
  206. plc->packetsize = extent;
  207. }
  208. memcpy (&tonemap [slot] [carrier], fragment->MOD_CARRIER, plc->packetsize);
  209. carrier += plc->packetsize;
  210. extent -= plc->packetsize;
  211. }
  212. }
  213. carrier = 0;
  214. /*
  215. * LOW BANDS;
  216. */
  217. memset (BPC, 0, sizeof (BPC));
  218. memset (SNR, 0, sizeof (SNR));
  219. AvgBPC = 0;
  220. AvgSNR = 0;
  221. while (carrier < INT_CARRIERS)
  222. {
  223. unsigned value = 0;
  224. unsigned scale = 0;
  225. unsigned index = carrier >> 1;
  226. printf ("%04d", carrier);
  227. for (slot = 0; slot < slots; slot++)
  228. {
  229. value = tonemap [slot][index];
  230. if ((carrier & 1))
  231. {
  232. value >>= 4;
  233. }
  234. value &= 0x0F;
  235. if (value > (AMP_BITS-1))
  236. {
  237. error (0, EINVAL, "Index %d Slot %d Value %d", carrier, slot, value);
  238. }
  239. printf (",%02d", mod2bits [value]);
  240. BPC [slot] += mod2bits [value];
  241. SNR [slot] += mod2db [value];
  242. AvgBPC += mod2bits [value];
  243. AvgSNR += mod2db [value];
  244. value *= value;
  245. scale += value;
  246. }
  247. if (_anyset (plc->flags, PLC_GRAPH))
  248. {
  249. printf (" %03d ", scale);
  250. if (scale)
  251. {
  252. scale /= slots;
  253. while (scale--)
  254. {
  255. printf ("#");
  256. }
  257. active++;
  258. }
  259. }
  260. printf ("\n");
  261. carrier++;
  262. }
  263. AvgBPC /= active;
  264. AvgBPC /= slots;
  265. AvgSNR /= active;
  266. AvgSNR /= slots;
  267. printf (" SNR");
  268. for (slot = 0; slot < slots; slot++)
  269. {
  270. printf (",%8.3f", (float)(SNR [slot]) / active);
  271. }
  272. printf (",%8.3f", AvgSNR);
  273. printf (" \n");
  274. printf (" ATN");
  275. for (slot = 0; slot < slots; slot++)
  276. {
  277. printf (",%8.3f", (float)(SNR [slot]) / active - 60);
  278. }
  279. printf (",%8.3f", AvgSNR - 60);
  280. printf (" \n");
  281. printf (" BPC");
  282. for (slot = 0; slot < slots; slot++)
  283. {
  284. printf (",%8.3f", (float)(BPC [slot]) / active);
  285. }
  286. printf (",%8.3f", AvgBPC);
  287. printf (" \n");
  288. /*
  289. * HIGH BANDS;
  290. */
  291. memset (BPC, 0, sizeof (BPC));
  292. memset (SNR, 0, sizeof (SNR));
  293. AvgBPC = 0;
  294. AvgSNR = 0;
  295. while (carrier < carriers)
  296. {
  297. unsigned value = 0;
  298. unsigned scale = 0;
  299. unsigned index = carrier >> 1;
  300. printf ("%04d", carrier);
  301. for (slot = 0; slot < slots; slot++)
  302. {
  303. value = tonemap [slot][index];
  304. if ((carrier & 1))
  305. {
  306. value >>= 4;
  307. }
  308. value &= 0x0F;
  309. if (value > (AMP_BITS-1))
  310. {
  311. error (0, EINVAL, "Index %d Slot %d Value %d", carrier, slot, value);
  312. }
  313. printf (",%02d", mod2bits [value]);
  314. BPC [slot] += mod2bits [value];
  315. SNR [slot] += mod2db [value];
  316. AvgBPC += mod2bits [value];
  317. AvgSNR += mod2db [value];
  318. value *= value;
  319. scale += value;
  320. }
  321. if (_anyset (plc->flags, PLC_GRAPH))
  322. {
  323. printf (" %03d ", scale);
  324. if (scale)
  325. {
  326. scale /= slots;
  327. while (scale--)
  328. {
  329. printf ("#");
  330. }
  331. }
  332. }
  333. printf ("\n");
  334. carrier++;
  335. active++;
  336. }
  337. AvgBPC /= active;
  338. AvgBPC /= slots;
  339. AvgSNR /= active;
  340. AvgSNR /= slots;
  341. printf (" SNR");
  342. for (slot = 0; slot < slots; slot++)
  343. {
  344. printf (",%8.3f", (float)(SNR [slot]) / active);
  345. }
  346. printf (",%8.3f", AvgSNR);
  347. printf (" \n");
  348. printf (" ATN");
  349. for (slot = 0; slot < slots; slot++)
  350. {
  351. printf (",%8.3f", (float)(SNR [slot]) / active - 60);
  352. }
  353. printf (",%8.3f", AvgSNR - 60);
  354. printf (" \n");
  355. printf (" BPC");
  356. for (slot = 0; slot < slots; slot++)
  357. {
  358. printf (",%8.3f", (float)(BPC [slot]) / active);
  359. }
  360. printf (",%8.3f", AvgBPC);
  361. printf (" \n");
  362. printf (" AGC");
  363. for (slot = 0; slot < slots; slot++)
  364. {
  365. printf (",%02d", AGC [slot]);
  366. }
  367. printf (" \n");
  368. printf (" GIL");
  369. for (slot = 0; slot < slots; slot++)
  370. {
  371. printf (",%02d", GIL [slot]);
  372. }
  373. printf (" \n");
  374. return (0);
  375. }
  376. #endif