* update EXI codec to latest given schema

git-svn-id: https://svn.code.sf.net/p/openv2g/code/trunk@44 d9f2db14-54d0-4bde-b00c-16405c910529
This commit is contained in:
daniel_peintner 2011-12-02 08:36:03 +00:00
parent 4e01b87700
commit 2b978cbcd2

View file

@ -1,259 +0,0 @@
/*
* Copyright (C) 2007-2011 Siemens AG
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*******************************************************************
*
* @author Daniel.Peintner.EXT@siemens.com
* @version 0.5
* @contact Joerg.Heuer@siemens.com
*
* <p>Sample program to illustrate how to read an EXI stream and
* directly write it again to an output</p>
*
* <p>e.g., data/test/sessionSetupReq.xml.exi out/test/sessionSetupReq.xml.exi</p>
********************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "EXIDecoder.h"
#include "StringTable.h"
#include "EXIEncoder.h"
#include "EXITypes.h"
#include "ByteStream.h"
#define BUFFER_SIZE 1000
#define ARRAY_SIZE_BYTES 100
#define ARRAY_SIZE_STRINGS 100
/* avoids warning: initializer element is not computable at load time */
uint8_t bufferIn[BUFFER_SIZE];
uint8_t bufferOut[BUFFER_SIZE];
uint8_t data[ARRAY_SIZE_BYTES];
uint32_t codepoints[ARRAY_SIZE_STRINGS];
int main_codec(int argc, char *argv[]) {
int errn = 0;
unsigned int i;
bitstream_t iStream, oStream;
uint16_t posDecode;
uint16_t posEncode;
/* EXI set-up */
exi_state_t stateDecode;
exi_state_t stateEncode;
exi_event_t event;
eqname_t eqn;
exi_value_t val;
/* BINARY memory setup */
bytes_t bytes = { ARRAY_SIZE_BYTES, data, 0 };
/* STRING memory setup */
string_ucs_t string = { ARRAY_SIZE_STRINGS, codepoints, 0 };
const char * localName;
const char * namespaceURI;
int noEndOfDocument = 1; /* true */
if (argc != 3) {
printf("Usage: %s exiInput exiOutput\n", argv[0]);
return -1;
}
/* input pos */
posDecode = 0;
/* parse EXI stream to internal byte structures */
readBytesFromFile(argv[1], bufferIn, BUFFER_SIZE, posDecode);
/* setup input stream */
iStream.size = BUFFER_SIZE;
iStream.data = bufferIn;
iStream.pos = &posDecode;
iStream.buffer = 0;
iStream.capacity = 0;
/* setup output stream */
posEncode = 0;
oStream.size = BUFFER_SIZE;
oStream.data = bufferOut;
oStream.pos = &posEncode;
oStream.buffer = 0;
oStream.capacity = 8;
val.binary = bytes;
val.string = string;
/* init decoder (read header, set initial state) */
exiInitDecoder(&iStream, &stateDecode);
/* init encoder (write header, set initial state) */
exiInitEncoder(&oStream, &stateEncode);
printf("[DECODE] >>> EXI >>> [ENCODE] \n");
do {
if (errn < 0) {
printf("[Encode-ERROR] %d \n", errn);
return errn;
}
errn = exiDecodeNextEvent(&iStream, &stateDecode, &event);
if (errn < 0) {
printf("[Decode-ERROR] %d \n", errn);
return errn;
}
switch (event) {
case START_DOCUMENT:
/* decode */
errn = exiDecodeStartDocument(&iStream, &stateDecode);
if (errn < 0) {
printf("[Decode-ERROR] %d \n", errn);
return errn;
}
printf(">> START_DOCUMENT \n");
/* encode */
errn = exiEncodeStartDocument(&oStream, &stateEncode);
break;
case END_DOCUMENT:
/* decode */
errn = exiDecodeEndDocument(&iStream, &stateDecode);
if (errn < 0) {
printf("[Decode-ERROR] %d \n", errn);
return errn;
}
printf(">> END_DOCUMENT \n");
/* encode */
errn = exiEncodeEndDocument(&oStream, &stateEncode);
/* signalize end of document */
noEndOfDocument = 0; /* false */
break;
case START_ELEMENT:
/* decode */
errn = exiDecodeStartElement(&iStream, &stateDecode, &eqn);
if (errn < 0) {
printf("[Decode-ERROR] %d \n", errn);
return errn;
}
/* EVSEStatus
* if(eqn.localPart=1 && eqn.namespaceURI=3) {
send CAN message...
/* ResponseCode
} else if(eqn.localPart=2 && eqn.namespaceURI=3) {
send CAN message...
} else {
} */
exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
exiGetUri(eqn.namespaceURI, &namespaceURI);
printf(">> SE {%s}%s \n", namespaceURI, localName);
/*fflush(stdout); */
/* encode */
errn = exiEncodeStartElement(&oStream, &stateEncode, &eqn);
break;
case START_ELEMENT_GENERIC:
/* unknown element */
/* read the element content as usual (element name, value, type, etc.) */
break;
case END_ELEMENT:
/* decode */
errn = exiDecodeEndElement(&iStream, &stateDecode, &eqn);
if (errn < 0) {
printf("[Decode-ERROR] %d \n", errn);
return errn;
}
exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
exiGetUri(eqn.namespaceURI, &namespaceURI);
printf("<< EE {%s}%s \n", namespaceURI, localName);
/* encode */
errn = exiEncodeEndElement(&oStream, &stateEncode, &eqn);
break;
case CHARACTERS:
/* decode */
errn = exiDecodeCharacters(&iStream, &stateDecode, &val);
if (errn < 0) {
printf("[Decode-ERROR] %d \n", errn);
return errn;
}
if (val.type == INTEGER_BIG) {
printf(" CH int64 : %ld \n", (long int)val.int64);
} else if (val.type == BINARY_BASE64 || val.type == BINARY_HEX) {
printf(" CH Binary (len == %d) : ", val.binary.len);
for(i=0; i<val.binary.len; i++) {
printf(" [%d]", val.binary.data[i]);
}
printf("\n");
} else if (val.type == BOOLEAN) {
printf(" CH Boolean : %d \n", val.boolean);
} else if (val.type == STRING) {
printf(" CH String (len==%d) : '", val.string.len);
for(i=0;i<val.string.len; i++) {
printf("%c", (char)val.string.codepoints[i]);
}
printf("'\n");
} else {
printf(" CH ?? \n");
}
/* encode */
errn = exiEncodeCharacters(&oStream, &stateEncode, &val);
break;
case ATTRIBUTE:
/* decode */
errn = exiDecodeAttribute(&iStream, &stateDecode, &eqn, &val);
if (errn < 0) {
printf("[Decode-ERROR] %d \n", errn);
return errn;
}
exiGetLocalName(eqn.namespaceURI, eqn.localPart, &localName);
exiGetUri(eqn.namespaceURI, &namespaceURI);
printf(" AT {%s}%s \n", namespaceURI, localName);
/* encode */
errn = exiEncodeAttribute(&oStream, &stateEncode, &eqn, &val);
break;
default:
/* ERROR */
return -1;
}
} while (noEndOfDocument);
/* write to file */
writeBytesToFile(oStream.data, posEncode, argv[2]);
return 0;
}