evse_cm_mnbc_sound.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 evse_cm_mnbc_sound (struct session * session, struct channel * channel, struct message * message);
  44. *
  45. * slac.h
  46. *
  47. * the GP specification recommends that the EVSE-HLE set an overall
  48. * timer once the cm_start_atten_char message is received and use it
  49. * to terminate the msound loop in case some msounds are lost;
  50. *
  51. * accept only CM_MNBC_SOUND.IND that match RunID from the earlier
  52. * CM_SLAC_PARAM.REQ and CM_START_ATTRN_CHAT.IND;
  53. *
  54. * each CM_MNBC_MSOUND.IND is accompanied by a CM_ATTEN_PROFILE.IND
  55. * but sometimes they arrive out of expected order;
  56. *
  57. * store the running total of CM_ATTEN_PROFILE.IND.AAG values in
  58. * the session variable and compute the average based on actual
  59. * number of sounds before returning;
  60. *
  61. *--------------------------------------------------------------------*/
  62. #ifndef EVSE_CM_MNBC_SOUND_SOURCE
  63. #define EVSE_CM_MNBC_SOUND_SOURCE
  64. #include <stdio.h>
  65. #include <memory.h>
  66. #include <sys/time.h>
  67. #include "../ether/channel.h"
  68. #include "../tools/memory.h"
  69. #include "../tools/error.h"
  70. #include "../tools/flags.h"
  71. #include "../tools/timer.h"
  72. #include "../slac/slac.h"
  73. signed evse_cm_mnbc_sound (struct session * session, struct channel * channel, struct message * message)
  74. {
  75. struct timeval ts;
  76. struct timeval tc;
  77. signed timer = 100 * session->TIME_OUT;
  78. unsigned AAG [SLAC_GROUPS];
  79. unsigned sounds = 0;
  80. ssize_t length;
  81. session->sounds = 0;
  82. memset (AAG, 0, sizeof (AAG));
  83. memset (session->AAG, 0, sizeof (session->AAG));
  84. if (gettimeofday (& ts, NULL) == - 1)
  85. {
  86. slac_debug (session, 1, __func__, CANT_START_TIMER);
  87. }
  88. while ((length = readpacket (channel, message, sizeof (* message))) >= 0)
  89. {
  90. struct homeplug * homeplug = (struct homeplug *) (message);
  91. if (! length)
  92. {
  93. }
  94. else if (ntohs (homeplug->ethernet.MTYPE) != ETH_P_HPAV)
  95. {
  96. slac_debug (session, session->exit, __func__, "bad MTYPE");
  97. }
  98. else if (homeplug->homeplug.MMV != HOMEPLUG_MMV)
  99. {
  100. slac_debug (session, session->exit, __func__, "bad MMV");
  101. }
  102. else if (LE16TOH (homeplug->homeplug.MMTYPE) == (CM_MNBC_SOUND | MMTYPE_IND))
  103. {
  104. struct cm_mnbc_sound_indicate * indicate = (struct cm_mnbc_sound_indicate *) (message);
  105. if (! memcmp (session->RunID, indicate->MSVarField.RunID, sizeof (session->RunID)))
  106. {
  107. slac_debug (session, 0, __func__, "<-- CM_MNBC_SOUND.IND (%d)", sounds);
  108. #if SLAC_DEBUG
  109. if (_anyset (session->flags, SLAC_VERBOSE))
  110. {
  111. char string [256];
  112. slac_debug (session, 0, __func__, "CM_MNBC_SOUND.IND.APPLICATION_TYPE %d", indicate->APPLICATION_TYPE);
  113. slac_debug (session, 0, __func__, "CM_MNBC_SOUND.IND.SECURITY_TYPE %d", indicate->SECURITY_TYPE);
  114. slac_debug (session, 0, __func__, "CM_MNBC_SOUND.IND.MSVarField.SenderID %s", HEXSTRING (string, indicate->MSVarField.SenderID));
  115. slac_debug (session, 0, __func__, "CM_MNBC_SOUND.IND.MSVarField.Count %d", indicate->MSVarField.CNT);
  116. slac_debug (session, 0, __func__, "CM_MNBC_SOUND.IND.MSVarField.RunID %s", HEXSTRING (string, indicate->MSVarField.RunID));
  117. slac_debug (session, 0, __func__, "CM_MNBC_SOUND.IND.MSVarField.RND %s", HEXSTRING (string, indicate->MSVarField.RND));
  118. }
  119. #endif
  120. if (memcmp (session->PEV_MAC, indicate->ethernet.OSA, sizeof (session->PEV_MAC)))
  121. {
  122. slac_debug (session, session->exit, __func__, "Unexpected OSA");
  123. }
  124. sounds++;
  125. }
  126. }
  127. else if (LE16TOH (homeplug->homeplug.MMTYPE) == (CM_ATTEN_PROFILE | MMTYPE_IND))
  128. {
  129. struct cm_atten_profile_indicate * indicate = (struct cm_atten_profile_indicate *) (message);
  130. if (! memcmp (session->PEV_MAC, indicate->PEV_MAC, sizeof (session->PEV_MAC)))
  131. {
  132. slac_debug (session, 0, __func__, "<-- CM_ATTEN_PROFILE.IND (%d)", session->sounds);
  133. #if SLAC_DEBUG
  134. if (_anyset (session->flags, SLAC_VERBOSE))
  135. {
  136. char string [256];
  137. slac_debug (session, 0, __func__, "CM_ATTEN_PROFILE.PEV_MAC %s", HEXSTRING (string, indicate->PEV_MAC));
  138. slac_debug (session, 0, __func__, "CM_ATTEN_PROFILE.NumGroups %d", indicate->NumGroups);
  139. slac_debug (session, 0, __func__, "CM_ATTEN_PROFILE.AAG %s", hexstring (string, sizeof (string), indicate->AAG, indicate->NumGroups));
  140. }
  141. #endif
  142. for (session->NumGroups = 0; session->NumGroups < indicate->NumGroups; session->NumGroups++)
  143. {
  144. AAG [session->NumGroups] += indicate->AAG [session->NumGroups];
  145. }
  146. session->NumGroups = indicate->NumGroups;
  147. session->sounds++;
  148. }
  149. }
  150. if (gettimeofday (& tc, NULL) == - 1)
  151. {
  152. slac_debug (session, 1, __func__, CANT_RESET_TIMER);
  153. }
  154. if ((MILLISECONDS (ts, tc) < timer) && (session->sounds < session->NUM_SOUNDS))
  155. {
  156. continue;
  157. }
  158. if (session->sounds > 0)
  159. {
  160. for (session->NumGroups = 0; session->NumGroups < SLAC_GROUPS; ++ session->NumGroups)
  161. {
  162. session->AAG [session->NumGroups] = AAG [session->NumGroups] / session->sounds;
  163. }
  164. }
  165. return (0);
  166. }
  167. return (slac_debug (session, session->exit, __func__, "Sound timeout"));
  168. }
  169. #endif