main_codec.old 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. /*
  2. * Copyright (C) 2007-2011 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 Daniel.Peintner.EXT@siemens.com
  20. * @version 0.5
  21. * @contact Joerg.Heuer@siemens.com
  22. *
  23. * <p>Sample program to illustrate how to read an EXI stream and
  24. * directly write it again to an output</p>
  25. *
  26. * <p>e.g., data/test/sessionSetupReq.xml.exi out/test/sessionSetupReq.xml.exi</p>
  27. ********************************************************************/
  28. #include <stdio.h>
  29. #include <stdlib.h>
  30. #include "EXIDecoder.h"
  31. #include "StringTable.h"
  32. #include "EXIEncoder.h"
  33. #include "EXITypes.h"
  34. #include "ByteStream.h"
  35. #define BUFFER_SIZE 1000
  36. #define ARRAY_SIZE_BYTES 100
  37. #define ARRAY_SIZE_STRINGS 100
  38. /* avoids warning: initializer element is not computable at load time */
  39. uint8_t bufferIn[BUFFER_SIZE];
  40. uint8_t bufferOut[BUFFER_SIZE];
  41. uint8_t data[ARRAY_SIZE_BYTES];
  42. uint32_t codepoints[ARRAY_SIZE_STRINGS];
  43. int main_codec(int argc, char *argv[]) {
  44. int errn = 0;
  45. unsigned int i;
  46. bitstream_t iStream, oStream;
  47. uint16_t posDecode;
  48. uint16_t posEncode;
  49. /* EXI set-up */
  50. exi_state_t stateDecode;
  51. exi_state_t stateEncode;
  52. exi_event_t event;
  53. eqname_t eqn;
  54. exi_value_t val;
  55. /* BINARY memory setup */
  56. bytes_t bytes = { ARRAY_SIZE_BYTES, data, 0 };
  57. /* STRING memory setup */
  58. string_ucs_t string = { ARRAY_SIZE_STRINGS, codepoints, 0 };
  59. const char * localName;
  60. const char * namespaceURI;
  61. int noEndOfDocument = 1; /* true */
  62. if (argc != 3) {
  63. printf("Usage: %s exiInput exiOutput\n", argv[0]);
  64. return -1;
  65. }
  66. /* input pos */
  67. posDecode = 0;
  68. /* parse EXI stream to internal byte structures */
  69. readBytesFromFile(argv[1], bufferIn, BUFFER_SIZE, posDecode);
  70. /* setup input stream */
  71. iStream.size = BUFFER_SIZE;
  72. iStream.data = bufferIn;
  73. iStream.pos = &posDecode;
  74. iStream.buffer = 0;
  75. iStream.capacity = 0;
  76. /* setup output stream */
  77. posEncode = 0;
  78. oStream.size = BUFFER_SIZE;
  79. oStream.data = bufferOut;
  80. oStream.pos = &posEncode;
  81. oStream.buffer = 0;
  82. oStream.capacity = 8;
  83. val.binary = bytes;
  84. val.string = string;
  85. /* init decoder (read header, set initial state) */
  86. exiInitDecoder(&iStream, &stateDecode);
  87. /* init encoder (write header, set initial state) */
  88. exiInitEncoder(&oStream, &stateEncode);
  89. printf("[DECODE] >>> EXI >>> [ENCODE] \n");
  90. do {
  91. if (errn < 0) {
  92. printf("[Encode-ERROR] %d \n", errn);
  93. return errn;
  94. }
  95. errn = exiDecodeNextEvent(&iStream, &stateDecode, &event);
  96. if (errn < 0) {
  97. printf("[Decode-ERROR] %d \n", errn);
  98. return errn;
  99. }
  100. switch (event) {
  101. case START_DOCUMENT:
  102. /* decode */
  103. errn = exiDecodeStartDocument(&iStream, &stateDecode);
  104. if (errn < 0) {
  105. printf("[Decode-ERROR] %d \n", errn);
  106. return errn;
  107. }
  108. printf(">> START_DOCUMENT \n");
  109. /* encode */
  110. errn = exiEncodeStartDocument(&oStream, &stateEncode);
  111. break;
  112. case END_DOCUMENT:
  113. /* decode */
  114. errn = exiDecodeEndDocument(&iStream, &stateDecode);
  115. if (errn < 0) {
  116. printf("[Decode-ERROR] %d \n", errn);
  117. return errn;
  118. }
  119. printf(">> END_DOCUMENT \n");
  120. /* encode */
  121. errn = exiEncodeEndDocument(&oStream, &stateEncode);
  122. /* signalize end of document */
  123. noEndOfDocument = 0; /* false */
  124. break;
  125. case START_ELEMENT:
  126. /* decode */
  127. errn = exiDecodeStartElement(&iStream, &stateDecode, &eqn);
  128. if (errn < 0) {
  129. printf("[Decode-ERROR] %d \n", errn);
  130. return errn;
  131. }
  132. /* EVSEStatus
  133. * if(eqn.localPart=1 && eqn.namespaceURI=3) {
  134. send CAN message...
  135. /* ResponseCode
  136. } else if(eqn.localPart=2 && eqn.namespaceURI=3) {
  137. send CAN message...
  138. } else {
  139. } */
  140. exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
  141. exiGetUri(eqn.namespaceURI, &namespaceURI);
  142. printf(">> SE {%s}%s \n", namespaceURI, localName);
  143. /*fflush(stdout); */
  144. /* encode */
  145. errn = exiEncodeStartElement(&oStream, &stateEncode, &eqn);
  146. break;
  147. case START_ELEMENT_GENERIC:
  148. /* unknown element */
  149. /* read the element content as usual (element name, value, type, etc.) */
  150. break;
  151. case END_ELEMENT:
  152. /* decode */
  153. errn = exiDecodeEndElement(&iStream, &stateDecode, &eqn);
  154. if (errn < 0) {
  155. printf("[Decode-ERROR] %d \n", errn);
  156. return errn;
  157. }
  158. exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
  159. exiGetUri(eqn.namespaceURI, &namespaceURI);
  160. printf("<< EE {%s}%s \n", namespaceURI, localName);
  161. /* encode */
  162. errn = exiEncodeEndElement(&oStream, &stateEncode, &eqn);
  163. break;
  164. case CHARACTERS:
  165. /* decode */
  166. errn = exiDecodeCharacters(&iStream, &stateDecode, &val);
  167. if (errn < 0) {
  168. printf("[Decode-ERROR] %d \n", errn);
  169. return errn;
  170. }
  171. if (val.type == INTEGER_BIG) {
  172. printf(" CH int64 : %ld \n", (long int)val.int64);
  173. } else if (val.type == BINARY_BASE64 || val.type == BINARY_HEX) {
  174. printf(" CH Binary (len == %d) : ", val.binary.len);
  175. for(i=0; i<val.binary.len; i++) {
  176. printf(" [%d]", val.binary.data[i]);
  177. }
  178. printf("\n");
  179. } else if (val.type == BOOLEAN) {
  180. printf(" CH Boolean : %d \n", val.boolean);
  181. } else if (val.type == STRING) {
  182. printf(" CH String (len==%d) : '", val.string.len);
  183. for(i=0;i<val.string.len; i++) {
  184. printf("%c", (char)val.string.codepoints[i]);
  185. }
  186. printf("'\n");
  187. } else {
  188. printf(" CH ?? \n");
  189. }
  190. /* encode */
  191. errn = exiEncodeCharacters(&oStream, &stateEncode, &val);
  192. break;
  193. case ATTRIBUTE:
  194. /* decode */
  195. errn = exiDecodeAttribute(&iStream, &stateDecode, &eqn, &val);
  196. if (errn < 0) {
  197. printf("[Decode-ERROR] %d \n", errn);
  198. return errn;
  199. }
  200. exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
  201. exiGetUri(eqn.namespaceURI, &namespaceURI);
  202. printf(" AT {%s}%s \n", namespaceURI, localName);
  203. /* encode */
  204. errn = exiEncodeAttribute(&oStream, &stateEncode, &eqn, &val);
  205. break;
  206. default:
  207. /* ERROR */
  208. return -1;
  209. }
  210. } while (noEndOfDocument);
  211. /* write to file */
  212. writeBytesToFile(oStream.data, posEncode, argv[2]);
  213. return 0;
  214. }