evse_cm_mnbc_sound.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. /*====================================================================*
  2. *
  3. * Copyright (c) 2013 Qualcomm Atheros, Inc.
  4. *
  5. * All rights reserved.
  6. *
  7. *====================================================================*/
  8. /*====================================================================*
  9. *
  10. * signed evse_cm_mnbc_sound (struct session * session, struct channel * channel, struct message * message);
  11. *
  12. * slac.h
  13. *
  14. * the GP specification recommends that the EVSE-HLE set an overall
  15. * timer once the cm_start_atten_char message is received and use it
  16. * to terminate the msound loop in case some msounds are lost;
  17. *
  18. * accept only CM_MNBC_SOUND.IND that match RunID from the earlier
  19. * CM_SLAC_PARAM.REQ and CM_START_ATTRN_CHAT.IND;
  20. *
  21. * each CM_MNBC_MSOUND.IND is accompanied by a CM_ATTEN_PROFILE.IND
  22. * but sometimes they arrive out of expected order;
  23. *
  24. * store the running total of CM_ATTEN_PROFILE.IND.AAG values in
  25. * the session variable and compute the average based on actual
  26. * number of sounds before returning;
  27. *
  28. *--------------------------------------------------------------------*/
  29. #ifndef EVSE_CM_MNBC_SOUND_SOURCE
  30. #define EVSE_CM_MNBC_SOUND_SOURCE
  31. #include <stdio.h>
  32. #include <memory.h>
  33. #include <sys/time.h>
  34. #include "../ether/channel.h"
  35. #include "../tools/memory.h"
  36. #include "../tools/error.h"
  37. #include "../tools/flags.h"
  38. #include "../tools/timer.h"
  39. #include "../slac/slac.h"
  40. signed evse_cm_mnbc_sound (struct session * session, struct channel * channel, struct message * message)
  41. {
  42. struct timeval ts;
  43. struct timeval tc;
  44. signed timer = 100 * session->TIME_OUT;
  45. unsigned AAG [SLAC_GROUPS];
  46. unsigned sounds = 0;
  47. ssize_t length;
  48. session->sounds = 0;
  49. memset (AAG, 0, sizeof (AAG));
  50. memset (session->AAG, 0, sizeof (session->AAG));
  51. if (gettimeofday (& ts, NULL) == - 1)
  52. {
  53. debug (1, __func__, CANT_START_TIMER);
  54. }
  55. while ((length = readpacket (channel, message, sizeof (* message))) >= 0)
  56. {
  57. struct homeplug1 * homeplug = (struct homeplug1 *) (message);
  58. if (! length)
  59. {
  60. }
  61. else if (ntohs (homeplug->ethernet.MTYPE) != ETH_P_HPAV)
  62. {
  63. debug (session->exit, __func__, "bad MTYPE");
  64. }
  65. else if (homeplug->homeplug.MMV != HOMEPLUG_MMV)
  66. {
  67. debug (session->exit, __func__, "bad MMV");
  68. }
  69. else if (LE16TOH (homeplug->homeplug.MMTYPE) == (CM_MNBC_SOUND | MMTYPE_IND))
  70. {
  71. struct cm_mnbc_sound_indicate * indicate = (struct cm_mnbc_sound_indicate *) (message);
  72. if (! memcmp (session->RunID, indicate->MSVarField.RunID, sizeof (session->RunID)))
  73. {
  74. debug (0, __func__, "<-- CM_MNBC_SOUND.IND (%d)", sounds);
  75. #if SLAC_DEBUG
  76. if (_anyset (session->flags, SLAC_VERBOSE))
  77. {
  78. char string [256];
  79. debug (0, __func__, "CM_MNBC_SOUND.IND.APPLICATION_TYPE %d", indicate->APPLICATION_TYPE);
  80. debug (0, __func__, "CM_MNBC_SOUND.IND.SECURITY_TYPE %d", indicate->SECURITY_TYPE);
  81. debug (0, __func__, "CM_MNBC_SOUND.IND.MSVarField.SenderID %s", HEXSTRING (string, indicate->MSVarField.SenderID));
  82. debug (0, __func__, "CM_MNBC_SOUND.IND.MSVarField.Count %d", indicate->MSVarField.CNT);
  83. debug (0, __func__, "CM_MNBC_SOUND.IND.MSVarField.RunID %s", HEXSTRING (string, indicate->MSVarField.RunID));
  84. debug (0, __func__, "CM_MNBC_SOUND.IND.MSVarField.RND %s", HEXSTRING (string, indicate->MSVarField.RND));
  85. }
  86. #endif
  87. if (memcmp (session->PEV_MAC, indicate->ethernet.OSA, sizeof (session->PEV_MAC)))
  88. {
  89. debug (session->exit, __func__, "Unexpected OSA");
  90. }
  91. sounds++;
  92. }
  93. }
  94. else if (LE16TOH (homeplug->homeplug.MMTYPE) == (CM_ATTEN_PROFILE | MMTYPE_IND))
  95. {
  96. struct cm_atten_profile_indicate * indicate = (struct cm_atten_profile_indicate *) (message);
  97. if (! memcmp (session->PEV_MAC, indicate->PEV_MAC, sizeof (session->PEV_MAC)))
  98. {
  99. debug (0, __func__, "<-- CM_ATTEN_PROFILE.IND (%d)", session->sounds);
  100. #if SLAC_DEBUG
  101. if (_anyset (session->flags, SLAC_VERBOSE))
  102. {
  103. char string [256];
  104. debug (0, __func__, "CM_ATTEN_PROFILE.PEV_MAC %s", HEXSTRING (string, indicate->PEV_MAC));
  105. debug (0, __func__, "CM_ATTEN_PROFILE.NumGroups %d", indicate->NumGroups);
  106. debug (0, __func__, "CM_ATTEN_PROFILE.AAG %s", hexstring (string, sizeof (string), indicate->AAG, indicate->NumGroups));
  107. }
  108. #endif
  109. for (session->NumGroups = 0; session->NumGroups < indicate->NumGroups; session->NumGroups++)
  110. {
  111. AAG [session->NumGroups] += indicate->AAG [session->NumGroups];
  112. }
  113. session->NumGroups = indicate->NumGroups;
  114. session->sounds++;
  115. }
  116. }
  117. if (gettimeofday (& tc, NULL) == - 1)
  118. {
  119. debug (1, __func__, CANT_RESET_TIMER);
  120. }
  121. if ((MILLISECONDS (ts, tc) < timer) && (session->sounds < session->NUM_SOUNDS))
  122. {
  123. continue;
  124. }
  125. if (session->sounds > 0)
  126. {
  127. for (session->NumGroups = 0; session->NumGroups < SLAC_GROUPS; ++ session->NumGroups)
  128. {
  129. session->AAG [session->NumGroups] = AAG [session->NumGroups] / session->sounds;
  130. }
  131. }
  132. return (0);
  133. }
  134. return (debug (session->exit, __func__, "Sound timeout"));
  135. }
  136. #endif