evse_server.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. /*
  2. * Copyright (C) 2007-2012 Siemens AG
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU Lesser General Public License as published
  6. * by the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /*******************************************************************
  18. *
  19. * @author Sebastian.Kaebisch.EXT@siemens.com
  20. * @@version 0.6
  21. * @contact Joerg.Heuer@siemens.com
  22. *
  23. ********************************************************************/
  24. /* includes for the application handshake protocol */
  25. #include "appHand_service.h"
  26. #include "appHand_dataTypes.h"
  27. #include "evse_server.h"
  28. #include "v2g_service.h"
  29. #include "v2g_serviceDispatcher.h"
  30. #include "v2gtp.h"
  31. #define MAX_BYTE_SIZE 64
  32. #define MAX_STRING_SIZE 64
  33. #define MAX_STREAM_SIZE 100
  34. static int appHandshakeHandler(struct EXIDatabinder* appHandService, uint8_t* inStream, uint16_t sizeInStream, uint8_t* outStream, uint16_t* outStreamLength);
  35. static void printASCIIString(uint32_t* string, uint32_t len);
  36. int testV2GService(uint8_t* inStream, uint16_t inStreamLength, uint8_t* outStream, uint16_t* outStreamLength)
  37. {
  38. static uint8_t byte_array[MAX_BYTE_SIZE]; /* define MAX_BYTE_SIZE before*/
  39. static uint32_t string_array[MAX_STRING_SIZE]; /* define MAX_STRING_SIZE before*/
  40. uint16_t exiMsgLength;
  41. struct EXIDatabinder appHandService;
  42. struct EXIService service;
  43. static uint8_t isHandshake = 1;
  44. /* BINARY memory setup */
  45. bytes_t bytes = { MAX_BYTE_SIZE, byte_array, 0 };
  46. /* STRING memory setup */
  47. string_ucs_t string = { MAX_STRING_SIZE, string_array, 0 };
  48. /**********************************************
  49. * Init V2G server and initialize array types *
  50. * for the EXI decoding as well as the offset *
  51. * for the transportation header *
  52. **********************************************/
  53. init_v2gservice(&service, bytes, string, V2GTP_HEADER_LENGTH);
  54. /* check, if the DoIP header is correct and determine payload */
  55. if(read_v2gtpHeader(inStream,inStreamLength, &exiMsgLength))
  56. {
  57. /* v2gtp header not correct */
  58. return -1;
  59. }
  60. /* Here, it is assumed the first message is always the application handshake protocol.
  61. * The successor messages are 15118 charging based messages and handled by the message
  62. * dispatcher. */
  63. if(isHandshake)
  64. {
  65. /* init the de- / serializer */
  66. init_appHandDeserializer(&appHandService,bytes,string,V2GTP_HEADER_LENGTH);
  67. init_appHandSerializer(&appHandService,bytes,string,MAX_STREAM_SIZE,V2GTP_HEADER_LENGTH);
  68. if(appHandshakeHandler(&appHandService, inStream,inStreamLength,outStream,outStreamLength))
  69. {
  70. return -1; /* an error occured */
  71. }
  72. isHandshake = 0; /* here: next time a charging message is expected */
  73. }
  74. else
  75. {
  76. /****************************************************************************
  77. * Pass the received EXI message stream (inStream + exiMsgLength) to the *
  78. * v2g message dispatcher. The outStream contains the response message *
  79. * stream. *
  80. ****************************************************************************/
  81. if(messageDispatcher(&service, inStream, exiMsgLength, outStream, MAX_STREAM_SIZE, outStreamLength))
  82. {
  83. /* an error occured */
  84. return -1;
  85. }
  86. }
  87. /* write v2gtp header */
  88. write_v2gtpHeader(outStream, outStreamLength, V2GTP_EXI_TYPE);
  89. return 0;
  90. }
  91. /** Example implementation of the app handshake protocol for the EVSE side */
  92. static int appHandshakeHandler(struct EXIDatabinder* appHandService, uint8_t* inStream, uint16_t sizeInStream, uint8_t* outStream, uint16_t* outStreamLength)
  93. {
  94. struct EXIDocumentType_appHand exiDoc;
  95. struct AnonType_supportedAppProtocolReq handshake;
  96. struct AnonType_supportedAppProtocolRes resultHandshake;
  97. size_t i;
  98. init_AnonType_supportedAppProtocolReq(&handshake);
  99. init_EXIDocumentType_appHand(&exiDoc);
  100. /* we expect a supportedAppProtocolReq */
  101. exiDoc.supportedAppProtocolReq = &handshake;
  102. if(deserialize_appHand(appHandService,inStream,sizeInStream,&exiDoc))
  103. {
  104. /* an error occured */
  105. return -1;
  106. }
  107. printf("EVSE side: List of application handshake protocols of the EV \n");
  108. for(i=0;i<handshake.arraylen.AppProtocol;i++)
  109. {
  110. printf("\tProtocol entry #=%d\n",(i+1));
  111. printf("\t\tProtocolNamespace=");
  112. printASCIIString(handshake.AppProtocol[i].ProtocolNamespace.data,handshake.AppProtocol[i].ProtocolNamespace.arraylen.data);
  113. printf("\t\tVersion=%d.%d\n", handshake.AppProtocol[i].VersionNumberMajor,handshake.AppProtocol[i].VersionNumberMinor);
  114. printf("\t\tSchemaID=%d\n", handshake.AppProtocol[i].SchemaID);
  115. printf("\t\tPriority=%d\n", handshake.AppProtocol[i].Priority);
  116. }
  117. /* prepare response handshake response:
  118. * it is assumed, we support the 15118 1.0 version :-) */
  119. resultHandshake.ResponseCode=OK_SuccessfulNegotiation_responseCodeType;
  120. resultHandshake.SchemaID=handshake.AppProtocol[0].SchemaID; /* signal the protocol by the provided schema id*/
  121. resultHandshake.isused.SchemaID=1;
  122. /* assign the response message to the exiDoc */
  123. init_EXIDocumentType_appHand(&exiDoc);
  124. exiDoc.supportedAppProtocolRes=&resultHandshake;
  125. exiDoc.isused.supportedAppProtocolRes=1;
  126. if(serialize_appHand(appHandService, outStream,outStreamLength, &exiDoc))
  127. {
  128. return -1;
  129. }
  130. return 0;
  131. }
  132. static void printASCIIString(uint32_t* string, uint32_t len) {
  133. unsigned int i;
  134. for(i=0; i<len; i++) {
  135. printf("%c",(char)string[i]);
  136. }
  137. printf("\n");
  138. }