ToneMaps2.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 ToneMaps2 (struct plc * plc);
  44. *
  45. * amp.h
  46. *
  47. *
  48. * Contributor(s):
  49. * Charles Maier
  50. *
  51. *--------------------------------------------------------------------*/
  52. #ifndef TONEMAPS2_SOURCE
  53. #define TONEMAPS2_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. #include "../mme/mme.h"
  61. #ifndef SIGNALTONOISE2_SOURCE
  62. static uint8_t const mod2bits [AMP_BITS] =
  63. {
  64. 0,
  65. 1,
  66. 2,
  67. 3,
  68. 4,
  69. 6,
  70. 8,
  71. 10,
  72. 12
  73. };
  74. #endif
  75. signed ToneMaps2 (struct plc * plc)
  76. {
  77. uint8_t tonemap [PLC_SLOTS + 1][AMP_CARRIERS >> 1];
  78. struct channel * channel = (struct channel *)(plc->channel);
  79. struct message * message = (struct message *)(plc->message);
  80. uint16_t extent = 0;
  81. uint16_t carriers = AMP_CARRIERS;
  82. uint16_t carrier = 0;
  83. uint8_t slots = PLC_SLOTS;
  84. uint8_t slot = 0;
  85. #ifndef __GNUC__
  86. #pragma pack (push,1)
  87. #endif
  88. struct __packed vs_tonemap_char_request
  89. {
  90. struct ethernet_hdr ethernet;
  91. struct qualcomm_fmi qualcomm;
  92. uint8_t MME_SUBVER;
  93. uint8_t Reserved1 [3];
  94. uint8_t MACADDRESS [ETHER_ADDR_LEN];
  95. uint8_t TMSLOT;
  96. uint8_t COUPLING;
  97. }
  98. * request = (struct vs_tonemap_char_request *) (message);
  99. struct __packed vs_tonemap_char_confirm
  100. {
  101. struct ethernet_hdr ethernet;
  102. struct qualcomm_fmi qualcomm;
  103. uint8_t MSTATUS;
  104. uint8_t Reserved1;
  105. uint16_t MME_LEN;
  106. struct __packed vs_tonemap_char_header
  107. {
  108. uint8_t MME_SUBVER;
  109. uint8_t Reserved2;
  110. uint8_t MACADDRESS [6];
  111. uint8_t TMSLOT;
  112. uint8_t COUPLING;
  113. uint8_t NUMTMS;
  114. uint8_t Reserved4;
  115. uint16_t TMNUMACTCARRIERS;
  116. uint16_t Reserved5;
  117. uint16_t NUMCARRIERS;
  118. uint16_t Reserved6;
  119. uint32_t Reserved7;
  120. }
  121. header;
  122. uint8_t MOD_CARRIER [1];
  123. }
  124. * confirm = (struct vs_tonemap_char_confirm *) (message);
  125. struct __packed vs_tonemap_char_fragment
  126. {
  127. struct ethernet_hdr ethernet;
  128. struct homeplug_fmi qualcomm;
  129. uint8_t MOD_CARRIER [1];
  130. }
  131. * fragment = (struct vs_tonemap_char_fragment *) (message);
  132. #ifndef __GNUC__
  133. #pragma pack (pop)
  134. #endif
  135. memset (tonemap, 0, sizeof (tonemap));
  136. for (carrier = slot = 0; slot < slots; carrier = 0, slot++)
  137. {
  138. memset (message, 0, sizeof (* message));
  139. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  140. QualcommHeader1 (&request->qualcomm, 1, (VS_TONE_MAP_CHAR | MMTYPE_REQ));
  141. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  142. memcpy (request->MACADDRESS, plc->RDA, sizeof (request->MACADDRESS));
  143. request->TMSLOT = slot;
  144. request->COUPLING = plc->coupling;
  145. if (SendMME (plc) <= 0)
  146. {
  147. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  148. return (-1);
  149. }
  150. if (ReadMME (plc, 1, (VS_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  151. {
  152. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  153. return (-1);
  154. }
  155. if (confirm->MSTATUS)
  156. {
  157. error (1, 0, "Device refused request for slot %d: %s", slot, MMECode (VS_TONE_MAP_CHAR | MMTYPE_CNF, confirm->MSTATUS));
  158. }
  159. carriers = LE16TOH (confirm->header.TMNUMACTCARRIERS);
  160. slots = confirm->header.NUMTMS;
  161. extent = LE16TOH (confirm->MME_LEN) - sizeof (struct vs_tonemap_char_header);
  162. if (extent > (AMP_CARRIERS >> 1))
  163. {
  164. error (1, EOVERFLOW, "Too many carriers");
  165. }
  166. plc->packetsize -= sizeof (struct vs_tonemap_char_confirm);
  167. plc->packetsize += sizeof (confirm->MOD_CARRIER);
  168. if (plc->packetsize > extent)
  169. {
  170. plc->packetsize = extent;
  171. }
  172. memcpy (&tonemap [slot] [carrier], &confirm->MOD_CARRIER, plc->packetsize);
  173. carrier += plc->packetsize;
  174. extent -= plc->packetsize;
  175. while (extent)
  176. {
  177. if (ReadMME (plc, 1, (VS_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  178. {
  179. error (1, errno, CHANNEL_CANTREAD);
  180. }
  181. plc->packetsize -= sizeof (struct vs_tonemap_char_fragment);
  182. plc->packetsize += sizeof (fragment->MOD_CARRIER);
  183. if (plc->packetsize > extent)
  184. {
  185. plc->packetsize = extent;
  186. }
  187. memcpy (&tonemap [slot] [carrier], fragment->MOD_CARRIER, plc->packetsize);
  188. carrier += plc->packetsize;
  189. extent -= plc->packetsize;
  190. }
  191. }
  192. for (carrier = 0; carrier < carriers; carrier++)
  193. {
  194. uint16_t scale = 0;
  195. uint16_t value = 0;
  196. uint16_t index = carrier >> 1;
  197. printf ("%04d", carrier);
  198. for (slot = 0; slot < slots; slot++)
  199. {
  200. value = tonemap [slot][index];
  201. if ((carrier & 1))
  202. {
  203. value >>= 4;
  204. }
  205. value &= 0x0F;
  206. printf (",%02d", mod2bits [value]);
  207. value *= value;
  208. scale += value;
  209. }
  210. if (slots)
  211. {
  212. scale /= slots;
  213. }
  214. printf (" %03d ", scale);
  215. if (_anyset (plc->flags, PLC_GRAPH))
  216. {
  217. while (scale--)
  218. {
  219. printf ("#");
  220. }
  221. }
  222. printf ("\n");
  223. }
  224. return (0);
  225. }
  226. #endif