ToneMaps1.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 ToneMaps1 (struct plc * plc);
  44. *
  45. * plc.h
  46. *
  47. *
  48. * Contributor(s):
  49. * Charles Maier
  50. *
  51. *--------------------------------------------------------------------*/
  52. #ifndef TONEMAPS1_SOURCE
  53. #define TONEMAPS1_SOURCE
  54. #include <stdio.h>
  55. #include <stdint.h>
  56. #include "../tools/flags.h"
  57. #include "../plc/plc.h"
  58. #include "../tools/error.h"
  59. #include "../tools/endian.h"
  60. #ifndef SIGNALTONOISE1_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. #endif
  74. signed ToneMaps1 (struct plc * plc)
  75. {
  76. extern uint8_t const mod2bits [AMP_BITS];
  77. struct channel * channel = (struct channel *)(plc->channel);
  78. struct message * message = (struct message *)(plc->message);
  79. byte tonemap [PLC_SLOTS] [INT_CARRIERS >> 1];
  80. uint16_t carriers = 0;
  81. uint16_t carrier = 0;
  82. uint8_t slots = 0;
  83. uint8_t slot = 0;
  84. #ifndef __GNUC__
  85. #pragma pack (push,1)
  86. #endif
  87. struct __packed vs_tone_slot_char_request
  88. {
  89. struct ethernet_hdr ethernet;
  90. struct qualcomm_hdr qualcomm;
  91. uint8_t MACADDRESS [ETHER_ADDR_LEN];
  92. uint8_t TMSLOT;
  93. }
  94. * request = (struct vs_tone_slot_char_request *) (message);
  95. struct __packed vs_tone_slot_char_confirm
  96. {
  97. struct ethernet_hdr ethernet;
  98. struct qualcomm_hdr qualcomm;
  99. uint8_t MSTATUS;
  100. uint8_t TMSLOT;
  101. uint8_t NUMTMS;
  102. uint16_t TMNUMACTCARRIERS;
  103. uint8_t MOD_CARRIER [INT_CARRIERS/2];
  104. uint8_t GIL;
  105. uint8_t AGC;
  106. }
  107. * confirm = (struct vs_tone_slot_char_confirm *) (message);
  108. #ifndef __GNUC__
  109. #pragma pack (pop)
  110. #endif
  111. memset (message, 0, sizeof (* message));
  112. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  113. QualcommHeader (&request->qualcomm, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_REQ));
  114. memcpy (request->MACADDRESS, plc->RDA, sizeof (request->MACADDRESS));
  115. request->TMSLOT = 0;
  116. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  117. if (SendMME (plc) <= 0)
  118. {
  119. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  120. return (-1);
  121. }
  122. if (ReadMME (plc, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  123. {
  124. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  125. return (-1);
  126. }
  127. carriers = LE16TOH (confirm->TMNUMACTCARRIERS);
  128. slots = confirm->NUMTMS;
  129. if (!slots)
  130. {
  131. error (PLC_EXIT (plc), ECANCELED, "No Tone Maps Available");
  132. return (-1);
  133. }
  134. memset (tonemap, 0, sizeof (tonemap));
  135. for (slot = 0; slot < slots; slot++)
  136. {
  137. EthernetHeader (&request->ethernet, channel->peer, channel->host, channel->type);
  138. QualcommHeader (&request->qualcomm, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_REQ));
  139. memcpy (request->MACADDRESS, plc->RDA, sizeof (request->MACADDRESS));
  140. request->TMSLOT = slot;
  141. plc->packetsize = (ETHER_MIN_LEN - ETHER_CRC_LEN);
  142. if (SendMME (plc) <= 0)
  143. {
  144. error (PLC_EXIT (plc), errno, CHANNEL_CANTSEND);
  145. return (-1);
  146. }
  147. if (ReadMME (plc, 0, (VS_RX_TONE_MAP_CHAR | MMTYPE_CNF)) <= 0)
  148. {
  149. error (PLC_EXIT (plc), errno, CHANNEL_CANTREAD);
  150. return (-1);
  151. }
  152. if (confirm->MSTATUS)
  153. {
  154. Failure (plc, PLC_WONTDOIT);
  155. return (-1);
  156. }
  157. memcpy (&tonemap [slot][0], confirm->MOD_CARRIER, sizeof (confirm->MOD_CARRIER));
  158. }
  159. while (carrier < carriers)
  160. {
  161. unsigned value = 0;
  162. unsigned scale = 0;
  163. unsigned index = carrier >> 1;
  164. printf ("% 04d", carrier);
  165. for (slot = 0; slot < PLC_SLOTS; slot++)
  166. {
  167. value = tonemap [slot][index];
  168. if ((carrier & 1))
  169. {
  170. value >>= 4;
  171. }
  172. value &= 0x0F;
  173. printf (" %02d", mod2bits [value]);
  174. value *= value;
  175. scale += value;
  176. }
  177. if (slots)
  178. {
  179. scale /= slots;
  180. }
  181. if (_anyset (plc->flags, PLC_GRAPH))
  182. {
  183. printf (" %03d ", scale);
  184. while (scale--)
  185. {
  186. printf ("#");
  187. }
  188. }
  189. printf ("\n");
  190. carrier++;
  191. }
  192. return (0);
  193. }
  194. #endif