Skip to content

Commit 8f44ac6

Browse files
author
juanjo
committed
Adding a new example and including libraries
1 parent 2bb3f76 commit 8f44ac6

File tree

10 files changed

+165
-355
lines changed

10 files changed

+165
-355
lines changed

Cbor/CborDecoder.cpp

Lines changed: 48 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -443,35 +443,59 @@ void CborReader::Run() {
443443

444444

445445

446-
void CborExampleListener::OnInteger(int value) {
447-
printf("integer: %d\n", value);
448-
}
446+
// TEST HANDLERS
449447

450-
void CborExampleListener::OnBytes(unsigned char *data, int size) {
451-
printf("bytes with size: %d", size);
452-
}
448+
void CborDebugListener::OnInteger(int value) {
449+
Serial.print("integer:");
450+
Serial.println(value);
451+
}
452+
453+
void CborDebugListener::OnBytes(unsigned char *data, int size) {
454+
Serial.println("bytes with size" + size);
455+
}
453456

454-
void CborExampleListener::OnString(String &str) {
455-
printf("string: '%.*s'\n", (int)str.size(), str.c_str());
456-
}
457+
void CborDebugListener::OnString(String &str) {
458+
Serial.print("string:");
459+
int lastStringLength = str.length();
460+
//Serial.print(lastStringLength);
461+
Serial.println(str);
462+
}
457463

458-
void CborExampleListener::OnArray(int size) {
459-
printf("array: %d\n", size);
460-
}
464+
void CborDebugListener::OnArray(int size) {
465+
Serial.println("array:" + size);
466+
}
461467

462-
void CborExampleListener::OnMap(int size) {
463-
printf("map: %d\n", size);
464-
}
468+
void CborDebugListener::OnMap(int size) {
469+
Serial.println("map:" + size);
470+
}
465471

466-
void CborExampleListener::OnTag(unsigned int tag) {
467-
printf("tag: %d\n", tag);
468-
}
472+
void CborDebugListener::OnTag(unsigned int tag) {
473+
Serial.println("tag:" + tag);
474+
}
469475

470-
void CborExampleListener::OnSpecial(int code) {
471-
printf("special: %d\n", code);
472-
}
476+
void CborDebugListener::OnSpecial(unsigned int code) {
477+
Serial.println("special" + code);
478+
}
473479

474-
void CborExampleListener::OnError(const char *error) {
475-
printf("error: %s\n", error);
476-
}
480+
void CborDebugListener::OnError(const char *error) {
481+
Serial.print("error:");
477482

483+
Serial.println(error);
484+
}
485+
486+
void CborDebugListener::OnExtraInteger(unsigned long long value, int sign) {
487+
if(sign >= 0) {
488+
Serial.println("extra integer: %llu\n" + value);
489+
} else {
490+
Serial.println("extra integer: -%llu\n" + value);
491+
}
492+
}
493+
494+
void CborDebugListener::OnExtraTag(unsigned long long tag) {
495+
Serial.println("extra tag: %llu\n" + tag);
496+
}
497+
498+
void CborDebugListener::OnExtraSpecial(unsigned long long tag) {
499+
Serial.println("extra special: %llu\n" + tag);
500+
501+
}

Cbor/CborEncoder.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,54 @@ unsigned int CborStaticOutput::getSize() {
5252
}
5353

5454

55+
CborDynamicOutput::CborDynamicOutput() {
56+
init(256);
57+
}
58+
59+
CborDynamicOutput::CborDynamicOutput(unsigned int initalCapacity) {
60+
init(initalCapacity);
61+
}
62+
63+
CborDynamicOutput::~CborDynamicOutput() {
64+
delete buffer;
65+
}
66+
67+
void CborDynamicOutput::init(unsigned int initalCapacity) {
68+
this->capacity = initalCapacity;
69+
this->buffer = new unsigned char[initalCapacity];
70+
this->offset = 0;
71+
}
72+
73+
74+
unsigned char *CborDynamicOutput::getData() {
75+
return buffer;
76+
}
77+
78+
unsigned int CborDynamicOutput::getSize() {
79+
return offset;
80+
}
81+
82+
void CborDynamicOutput::putByte(unsigned char value) {
83+
if(offset < capacity) {
84+
buffer[offset++] = value;
85+
} else {
86+
capacity *= 2;
87+
buffer = (unsigned char *) realloc(buffer, capacity);
88+
buffer[offset++] = value;
89+
}
90+
}
91+
92+
void CborDynamicOutput::putBytes(const unsigned char *data, int size) {
93+
while(offset + size > capacity) {
94+
capacity *= 2;
95+
buffer = (unsigned char *) realloc(buffer, capacity);
96+
}
97+
98+
memcpy(buffer + offset, data, size);
99+
offset += size;
100+
}
101+
102+
55103

56104
void CborWriter::writeTypeAndValue(int majorType, unsigned int value) {
57105
majorType <<= 5;

Cbor/CborEncoder.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@ class CborStaticOutput : public CborOutput {
4545
};
4646

4747

48+
class CborDynamicOutput : public CborOutput {
49+
public:
50+
CborDynamicOutput();
51+
CborDynamicOutput(unsigned int initalCapacity);
52+
~CborDynamicOutput();
53+
54+
55+
virtual unsigned char *getData();
56+
virtual unsigned int getSize();
57+
virtual void putByte(unsigned char value);
58+
virtual void putBytes(const unsigned char *data, int size);
59+
private:
60+
void init(unsigned int initalCapacity);
61+
unsigned char *buffer;
62+
unsigned int capacity;
63+
unsigned int offset;
64+
};
65+
4866
class CborWriter {
4967
public:
5068
CborWriter(CborOutput &output);

Cbor/CborParser.cpp~

Lines changed: 0 additions & 48 deletions
This file was deleted.

Cbor/CborParser.h~

Lines changed: 0 additions & 91 deletions
This file was deleted.

0 commit comments

Comments
 (0)