diff --git a/Cbor/CborDecoder.cpp b/Cbor/CborDecoder.cpp index 76a06dd..f786946 100644 --- a/Cbor/CborDecoder.cpp +++ b/Cbor/CborDecoder.cpp @@ -1,500 +1,826 @@ +/** + * CBOR Decoder + */ + #include "CborDecoder.h" #include "Arduino.h" +CborInput::CborInput(void *data, int size) +{ + this->data = (unsigned char *)data; + this->size = size; + this->offset = 0; +} +CborInput::~CborInput() +{ + // Intentionally empty. +} -CborInput::CborInput(void *data, int size) { - this->data = (unsigned char *)data; - this->size = size; - this->offset = 0; +bool CborInput::hasBytes(int count) +{ + return size - offset >= count; } -CborInput::~CborInput() { +unsigned char CborInput::getByte() +{ + return data[offset++]; +} +unsigned short CborInput::getShort() +{ + unsigned short value = ((unsigned short)data[offset] << 8) | + ((unsigned short)data[offset + 1]); + offset += 2; + + return value; } -bool CborInput::hasBytes(int count) { - return size - offset >= count; +unsigned int CborInput::getInt() +{ + unsigned int value = ((unsigned int)data[offset] << 24) | + ((unsigned int)data[offset + 1] << 16) | + ((unsigned int)data[offset + 2] << 8) | + ((unsigned int)data[offset + 3]); + offset += 4; + + return value; } -unsigned char CborInput::getByte() { - return data[offset++]; +unsigned long long CborInput::getLong() +{ + unsigned long long value = ((unsigned long long)data[offset] << 56) | + ((unsigned long long)data[offset + 1] << 48) | + ((unsigned long long)data[offset + 2] << 40) | + ((unsigned long long)data[offset + 3] << 32) | + ((unsigned long long)data[offset + 4] << 24) | + ((unsigned long long)data[offset + 5] << 16) | + ((unsigned long long)data[offset + 6] << 8) | + ((unsigned long long)data[offset + 7]); + offset += 8; + + return value; } -unsigned short CborInput::getShort() { - unsigned short value = ((unsigned short)data[offset] << 8) | ((unsigned short)data[offset + 1]); - offset += 2; - return value; +void CborInput::getBytes(void *to, int count) +{ + memcpy(to, data + offset, count); + offset += count; } -unsigned int CborInput::getInt() { - unsigned int value = ((unsigned int)data[offset] << 24) | ((unsigned int)data[offset + 1] << 16) | ((unsigned int)data[offset + 2] << 8) | ((unsigned int)data[offset + 3]); - offset += 4; - return value; -} - -unsigned long long CborInput::getLong() { - unsigned long long value = ((unsigned long long)data[offset] << 56) | ((unsigned long long)data[offset+1] << 48) | ((unsigned long long)data[offset+2] << 40) | ((unsigned long long)data[offset+3] << 32) | ((unsigned long long)data[offset+4] << 24) | ((unsigned long long)data[offset+5] << 16) | ((unsigned long long)data[offset+6] << 8) | ((unsigned long long)data[offset+7]); - offset += 8; - return value; -} - -void CborInput::getBytes(void *to, int count) { - memcpy(to, data + offset, count); - offset += count; -} - - -CborReader::CborReader(CborInput &input) { - this->input = &input; - this->state = STATE_TYPE; -} - -CborReader::CborReader(CborInput &input, CborListener &listener) { - this->input = &input; - this->listener = &listener; - this->state = STATE_TYPE; -} - -CborReader::~CborReader() { - -} - -void CborReader::SetListener(CborListener &listener) { - this->listener = &listener; -} - -void CborReader::Run() { - unsigned int temp; - while(1) { - if(state == STATE_TYPE) { - if(input->hasBytes(1)) { - unsigned char type = input->getByte(); - unsigned char majorType = type >> 5; - unsigned char minorType = type & 31; - - switch(majorType) { - case 0: // positive integer - if(minorType < 24) { - listener->OnInteger(minorType); - } else if(minorType == 24) { // 1 byte - currentLength = 1; - state = STATE_PINT; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_PINT; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_PINT; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_PINT; - } else { - state = STATE_ERROR; - listener->OnError("invalid integer type"); - } - break; - case 1: // negative integer - if(minorType < 24) { - listener->OnInteger(-minorType); - } else if(minorType == 24) { // 1 byte - currentLength = 1; - state = STATE_NINT; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_NINT; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_NINT; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_NINT; - } else { - state = STATE_ERROR; - listener->OnError("invalid integer type"); - } - break; - case 2: // bytes - if(minorType < 24) { - state = STATE_BYTES_DATA; - currentLength = minorType; - } else if(minorType == 24) { - state = STATE_BYTES_SIZE; - currentLength = 1; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_BYTES_SIZE; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_BYTES_SIZE; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_BYTES_SIZE; - } else { - state = STATE_ERROR; - listener->OnError("invalid bytes type"); - } - break; - case 3: // string - if(minorType < 24) { - state = STATE_STRING_DATA; - currentLength = minorType; - } else if(minorType == 24) { - state = STATE_STRING_SIZE; - currentLength = 1; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_STRING_SIZE; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_STRING_SIZE; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_STRING_SIZE; - } else { - state = STATE_ERROR; - listener->OnError("invalid string type"); - } - break; - case 4: // array - if(minorType < 24) { - listener->OnArray(minorType); - } else if(minorType == 24) { - state = STATE_ARRAY; - currentLength = 1; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_ARRAY; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_ARRAY; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_ARRAY; - } else { - state = STATE_ERROR; - listener->OnError("invalid array type"); - } - break; - case 5: // map - if(minorType < 24) { - listener->OnMap(minorType); - } else if(minorType == 24) { - state = STATE_MAP; - currentLength = 1; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_MAP; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_MAP; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_MAP; - } else { - state = STATE_ERROR; - listener->OnError("invalid array type"); - } - break; - case 6: // tag - if(minorType < 24) { - listener->OnTag(minorType); - } else if(minorType == 24) { - state = STATE_TAG; - currentLength = 1; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_TAG; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_TAG; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_TAG; - } else { - state = STATE_ERROR; - listener->OnError("invalid tag type"); - } - break; - case 7: // special - if(minorType < 24) { - listener->OnSpecial(minorType); - } else if(minorType == 24) { - state = STATE_SPECIAL; - currentLength = 1; - } else if(minorType == 25) { // 2 byte - currentLength = 2; - state = STATE_SPECIAL; - } else if(minorType == 26) { // 4 byte - currentLength = 4; - state = STATE_SPECIAL; - } else if(minorType == 27) { // 8 byte - currentLength = 8; - state = STATE_SPECIAL; - } else { - state = STATE_ERROR; - listener->OnError("invalid special type"); - } - break; - } - } else break; - } else if(state == STATE_PINT) { - if(input->hasBytes(currentLength)) { - switch(currentLength) { - case 1: - listener->OnInteger(input->getByte()); - state = STATE_TYPE; - break; - case 2: - listener->OnInteger(input->getShort()); - state = STATE_TYPE; - break; - case 4: - temp = input->getInt(); - if(temp <= INT_MAX) { - listener->OnInteger(temp); - } else { - listener->OnExtraInteger(temp, 1); - } - state = STATE_TYPE; - break; - case 8: - listener->OnExtraInteger(input->getLong(), 1); - state = STATE_TYPE; - break; - } - } else break; - } else if(state == STATE_NINT) { - if(input->hasBytes(currentLength)) { - switch(currentLength) { - case 1: - listener->OnInteger(-(int)input->getByte()); - state = STATE_TYPE; - break; - case 2: - listener->OnInteger(-(int)input->getShort()); - state = STATE_TYPE; - break; - case 4: - temp = input->getInt(); - if(temp <= INT_MAX) { - listener->OnInteger(-(int) temp); - } else if(temp == 2147483648u) { - listener->OnInteger(INT_MIN); - } else { - listener->OnExtraInteger(temp, -1); - } - state = STATE_TYPE; - break; - case 8: - listener->OnExtraInteger(input->getLong(), -1); - break; - } - } else break; - } else if(state == STATE_BYTES_SIZE) { - if(input->hasBytes(currentLength)) { - switch(currentLength) { - case 1: - currentLength = input->getByte(); - state = STATE_BYTES_DATA; - break; - case 2: - currentLength = input->getShort(); - state = STATE_BYTES_DATA; - break; - case 4: - currentLength = input->getInt(); - state = STATE_BYTES_DATA; - break; - case 8: - state = STATE_ERROR; - listener->OnError("extra long bytes"); - break; - } - } else break; - } else if(state == STATE_BYTES_DATA) { - if(input->hasBytes(currentLength)) { - unsigned char *data = new unsigned char[currentLength]; - input->getBytes(data, currentLength); - state = STATE_TYPE; - listener->OnBytes(data, currentLength); - } else break; - } else if(state == STATE_STRING_SIZE) { - if(input->hasBytes(currentLength)) { - switch(currentLength) { - case 1: - currentLength = input->getByte(); - state = STATE_STRING_DATA; - break; - case 2: - currentLength = input->getShort(); - state = STATE_STRING_DATA; - break; - case 4: - currentLength = input->getInt(); - state = STATE_STRING_DATA; - break; - case 8: - state = STATE_ERROR; - listener->OnError("extra long array"); - break; - } - } else break; - } else if(state == STATE_STRING_DATA) { - if(input->hasBytes(currentLength)) { - unsigned char data[currentLength]; - input->getBytes(data, currentLength); - state = STATE_TYPE; - String str = (const char *) data; - listener->OnString(str); - } else break; - } else if(state == STATE_ARRAY) { - if(input->hasBytes(currentLength)) { - switch(currentLength) { - case 1: - listener->OnArray(input->getByte()); - state = STATE_TYPE; - break; - case 2: - listener->OnArray(currentLength = input->getShort()); - state = STATE_TYPE; - break; - case 4: - listener->OnArray(input->getInt()); - state = STATE_TYPE; - break; - case 8: - state = STATE_ERROR; - listener->OnError("extra long array"); - break; - } - } else break; - } else if(state == STATE_MAP) { - if(input->hasBytes(currentLength)) { - switch(currentLength) { - case 1: - listener->OnMap(input->getByte()); - state = STATE_TYPE; - break; - case 2: - listener->OnMap(currentLength = input->getShort()); - state = STATE_TYPE; - break; - case 4: - listener->OnMap(input->getInt()); - state = STATE_TYPE; - break; - case 8: - state = STATE_ERROR; - listener->OnError("extra long map"); - break; - } - } else break; - } else if(state == STATE_TAG) { - if(input->hasBytes(currentLength)) { - switch(currentLength) { - case 1: - listener->OnTag(input->getByte()); - state = STATE_TYPE; - break; - case 2: - listener->OnTag(input->getShort()); - state = STATE_TYPE; - break; - case 4: - listener->OnTag(input->getInt()); - state = STATE_TYPE; - break; - case 8: - listener->OnExtraTag(input->getLong()); - state = STATE_TYPE; - break; - } - } else break; - } else if(state == STATE_SPECIAL) { - if (input->hasBytes(currentLength)) { - switch (currentLength) { - case 1: - listener->OnSpecial(input->getByte()); - state = STATE_TYPE; - break; - case 2: - listener->OnSpecial(input->getShort()); - state = STATE_TYPE; - break; - case 4: - listener->OnSpecial(input->getInt()); - state = STATE_TYPE; - break; - case 8: - listener->OnExtraSpecial(input->getLong()); - state = STATE_TYPE; - break; - } - } else break; - } else if(state == STATE_ERROR) { - break; - } else { - Serial.print("UNKNOWN STATE"); - } - } +CborReader::CborReader(CborInput &input) +{ + this->input = &input; + this->state = STATE_TYPE; } +CborReader::CborReader(CborInput &input, CborListener &listener) +{ + this->input = &input; + this->listener = &listener; + this->state = STATE_TYPE; +} + +CborReader::~CborReader() +{ + // Intentionally empty. +} + +void CborReader::SetListener(CborListener &listener) +{ + this->listener = &listener; +} + +void CborReader::Run() +{ + unsigned int temp = 0; + + while (1) + { + switch (state) + { + case STATE_TYPE: + StateTypeHelper(); + break; + case STATE_PINT: + StatePintHelper(temp); + break; + + case STATE_NINT: + StateNintHelper(temp); + break; + + case STATE_BYTES_SIZE: + StateByteSizeHelper(); + break; + + case STATE_BYTES_DATA: + StateBytesDataHelper(); + break; + + case STATE_STRING_SIZE: + StateStringSizeHelper(); + break; + + case STATE_STRING_DATA: + StateStringDataHelper(); + break; + + case STATE_ARRAY: + StateArrayHelper(); + break; + + case STATE_MAP: + StateMapHelper(); + break; + + case STATE_TAG: + StateTagHelper(); + break; + + case STATE_SPECIAL: + StateSpecialHelper(); + break; + + case STATE_ERROR: + // Intentional fallthrough to default. + + default: + Serial.print("UNKNOWN STATE"); + break; + } + } +} + +void CborReader::StateTypeHelper() +{ + if (!input->hasBytes(1)) + { + return; + } + + unsigned char type = input->getByte(); + unsigned char majorType = type >> 5; + unsigned char minorType = type & 31; + + switch (majorType) + { + case 0: // positive integer + if (minorType < 24) + { + listener->OnInteger(minorType); + } + else if (minorType == 24) + { // 1 byte + currentLength = 1; + state = STATE_PINT; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_PINT; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_PINT; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_PINT; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid integer type"); + } + break; + + case 1: // negative integer + if (minorType < 24) + { + listener->OnInteger(-minorType); + } + else if (minorType == 24) + { // 1 byte + currentLength = 1; + state = STATE_NINT; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_NINT; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_NINT; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_NINT; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid integer type"); + } + break; + + case 2: // bytes + if (minorType < 24) + { + state = STATE_BYTES_DATA; + currentLength = minorType; + } + else if (minorType == 24) + { + state = STATE_BYTES_SIZE; + currentLength = 1; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_BYTES_SIZE; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_BYTES_SIZE; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_BYTES_SIZE; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid bytes type"); + } + break; + + case 3: // string + if (minorType < 24) + { + state = STATE_STRING_DATA; + currentLength = minorType; + } + else if (minorType == 24) + { + state = STATE_STRING_SIZE; + currentLength = 1; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_STRING_SIZE; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_STRING_SIZE; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_STRING_SIZE; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid string type"); + } + break; + + case 4: // array + if (minorType < 24) + { + listener->OnArray(minorType); + } + else if (minorType == 24) + { + state = STATE_ARRAY; + currentLength = 1; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_ARRAY; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_ARRAY; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_ARRAY; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid array type"); + } + break; + + case 5: // map + if (minorType < 24) + { + listener->OnMap(minorType); + } + else if (minorType == 24) + { + state = STATE_MAP; + currentLength = 1; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_MAP; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_MAP; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_MAP; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid array type"); + } + break; + + case 6: // tag + if (minorType < 24) + { + listener->OnTag(minorType); + } + else if (minorType == 24) + { + state = STATE_TAG; + currentLength = 1; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_TAG; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_TAG; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_TAG; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid tag type"); + } + break; + + case 7: // special + if (minorType < 24) + { + listener->OnSpecial(minorType); + } + else if (minorType == 24) + { + state = STATE_SPECIAL; + currentLength = 1; + } + else if (minorType == 25) + { // 2 byte + currentLength = 2; + state = STATE_SPECIAL; + } + else if (minorType == 26) + { // 4 byte + currentLength = 4; + state = STATE_SPECIAL; + } + else if (minorType == 27) + { // 8 byte + currentLength = 8; + state = STATE_SPECIAL; + } + else + { + state = STATE_ERROR; + listener->OnError("invalid special type"); + } + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StatePintHelper(unsigned int &temp) +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + listener->OnInteger(input->getByte()); + state = STATE_TYPE; + break; + + case 2: + listener->OnInteger(input->getShort()); + state = STATE_TYPE; + break; + + case 4: + temp = input->getInt(); + if (temp <= INT_MAX) + { + listener->OnInteger(temp); + } + else + { + listener->OnExtraInteger(temp, 1); + } + state = STATE_TYPE; + break; + + case 8: + listener->OnExtraInteger(input->getLong(), 1); + state = STATE_TYPE; + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StateNintHelper(unsigned int &temp) +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + listener->OnInteger(-(int)input->getByte()); + state = STATE_TYPE; + break; + + case 2: + listener->OnInteger(-(int)input->getShort()); + state = STATE_TYPE; + break; + + case 4: + temp = input->getInt(); + if (temp <= INT_MAX) + { + listener->OnInteger(-(int)temp); + } + else if (temp == 2147483648u) + { + listener->OnInteger(INT_MIN); + } + else + { + listener->OnExtraInteger(temp, -1); + } + state = STATE_TYPE; + break; + + case 8: + listener->OnExtraInteger(input->getLong(), -1); + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StateByteSizeHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + currentLength = input->getByte(); + state = STATE_BYTES_DATA; + break; + + case 2: + currentLength = input->getShort(); + state = STATE_BYTES_DATA; + break; + + case 4: + currentLength = input->getInt(); + state = STATE_BYTES_DATA; + break; + + case 8: + state = STATE_ERROR; + listener->OnError("extra long bytes"); + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StateBytesDataHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + unsigned char *data = new unsigned char[currentLength]; + input->getBytes(data, currentLength); + state = STATE_TYPE; + listener->OnBytes(data, currentLength); +} + +void CborReader::StateStringSizeHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + currentLength = input->getByte(); + state = STATE_STRING_DATA; + break; + + case 2: + currentLength = input->getShort(); + state = STATE_STRING_DATA; + break; + + case 4: + currentLength = input->getInt(); + state = STATE_STRING_DATA; + break; + + case 8: + state = STATE_ERROR; + listener->OnError("extra long array"); + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StateStringDataHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + unsigned char data[currentLength]; + input->getBytes(data, currentLength); + state = STATE_TYPE; + String str = (const char *)data; + listener->OnString(str); +} + +void CborReader::StateArrayHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + listener->OnArray(input->getByte()); + state = STATE_TYPE; + break; + + case 2: + listener->OnArray(currentLength = input->getShort()); + state = STATE_TYPE; + break; + + case 4: + listener->OnArray(input->getInt()); + state = STATE_TYPE; + break; + + case 8: + state = STATE_ERROR; + listener->OnError("extra long array"); + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StateMapHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + listener->OnMap(input->getByte()); + state = STATE_TYPE; + break; + + case 2: + listener->OnMap(currentLength = input->getShort()); + state = STATE_TYPE; + break; + + case 4: + listener->OnMap(input->getInt()); + state = STATE_TYPE; + break; + + case 8: + state = STATE_ERROR; + listener->OnError("extra long map"); + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StateTagHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + listener->OnTag(input->getByte()); + state = STATE_TYPE; + break; + + case 2: + listener->OnTag(input->getShort()); + state = STATE_TYPE; + break; + + case 4: + listener->OnTag(input->getInt()); + state = STATE_TYPE; + break; + + case 8: + listener->OnExtraTag(input->getLong()); + state = STATE_TYPE; + break; + + default: + // Shouldn't be here. Log? + break; + } +} + +void CborReader::StateSpecialHelper() +{ + if (!input->hasBytes(currentLength)) + { + return; + } + + switch (currentLength) + { + case 1: + listener->OnSpecial(input->getByte()); + state = STATE_TYPE; + break; + + case 2: + listener->OnSpecial(input->getShort()); + state = STATE_TYPE; + break; + + case 4: + listener->OnSpecial(input->getInt()); + state = STATE_TYPE; + break; + + case 8: + listener->OnExtraSpecial(input->getLong()); + state = STATE_TYPE; + break; + + default: + // Shouldn't be here. Log? + break; + } +} // TEST HANDLERS -void CborDebugListener::OnInteger(int value) { - Serial.print("integer:"); - Serial.println(value); +void CborDebugListener::OnInteger(int value) +{ + Serial.print("integer:"); + Serial.println(value); } -void CborDebugListener::OnBytes(unsigned char *data, int size) { - Serial.println("bytes with size" + size); +void CborDebugListener::OnBytes(unsigned char *data, int size) +{ + Serial.println("bytes with size" + size); } -void CborDebugListener::OnString(String &str) { - Serial.print("string:"); - int lastStringLength = str.length(); - //Serial.print(lastStringLength); - Serial.println(str); +void CborDebugListener::OnString(String &str) +{ + Serial.print("string:"); + int lastStringLength = str.length(); + Serial.println(str); } -void CborDebugListener::OnArray(int size) { - Serial.println("array:" + size); +void CborDebugListener::OnArray(int size) +{ + Serial.println("array:" + size); } -void CborDebugListener::OnMap(int size) { - Serial.println("map:" + size); +void CborDebugListener::OnMap(int size) +{ + Serial.println("map:" + size); } -void CborDebugListener::OnTag(unsigned int tag) { - Serial.println("tag:" + tag); +void CborDebugListener::OnTag(unsigned int tag) +{ + Serial.println("tag:" + tag); } -void CborDebugListener::OnSpecial(unsigned int code) { - Serial.println("special" + code); +void CborDebugListener::OnSpecial(unsigned int code) +{ + Serial.println("special" + code); } -void CborDebugListener::OnError(const char *error) { - Serial.print("error:"); +void CborDebugListener::OnError(const char *error) +{ + Serial.print("error:"); - Serial.println(error); + Serial.println(error); } -void CborDebugListener::OnExtraInteger(unsigned long long value, int sign) { - if(sign >= 0) { - Serial.println("extra integer: %llu\n" + value); - } else { - Serial.println("extra integer: -%llu\n" + value); - } +void CborDebugListener::OnExtraInteger(unsigned long long value, int sign) +{ + Serial.print("extra integer: "); + + if (sign < 0) + { + Serial.println("-"); + } + + Serial.println("%llu\n" + value); } -void CborDebugListener::OnExtraTag(unsigned long long tag) { - Serial.println("extra tag: %llu\n" + tag); +void CborDebugListener::OnExtraTag(unsigned long long tag) +{ + Serial.println("extra tag: %llu\n" + tag); } -void CborDebugListener::OnExtraSpecial(unsigned long long tag) { - Serial.println("extra special: %llu\n" + tag); - +void CborDebugListener::OnExtraSpecial(unsigned long long tag) +{ + Serial.println("extra special: %llu\n" + tag); } diff --git a/Cbor/CborDecoder.h b/Cbor/CborDecoder.h index 0b0535d..e7a209b 100644 --- a/Cbor/CborDecoder.h +++ b/Cbor/CborDecoder.h @@ -1,105 +1,133 @@ -#ifndef CBORDE_H -#define CBORDE_H +/** + * Copyright 2014-2015 Stanislav Ovsyannikov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CBOR_DECODER_H +#define CBOR_DECODER_H #include "Arduino.h" #define INT_MAX 4 #define INT_MIN 4 - -typedef enum { - STATE_TYPE, - STATE_PINT, - STATE_NINT, - STATE_BYTES_SIZE, - STATE_BYTES_DATA, - STATE_STRING_SIZE, - STATE_STRING_DATA, - STATE_ARRAY, - STATE_MAP, - STATE_TAG, - STATE_SPECIAL, - STATE_ERROR +typedef enum +{ + STATE_TYPE, + STATE_PINT, + STATE_NINT, + STATE_BYTES_SIZE, + STATE_BYTES_DATA, + STATE_STRING_SIZE, + STATE_STRING_DATA, + STATE_ARRAY, + STATE_MAP, + STATE_TAG, + STATE_SPECIAL, + STATE_ERROR } CborReaderState; -class CborInput { +class CborInput +{ public: - CborInput(void *data, int size); - ~CborInput(); + CborInput(void *data, int size); + ~CborInput(); + + bool hasBytes(int count); + unsigned char getByte(); + unsigned short getShort(); + unsigned int getInt(); + unsigned long long getLong(); + void getBytes(void *to, int count); - bool hasBytes(int count); - unsigned char getByte(); - unsigned short getShort(); - unsigned int getInt(); - unsigned long long getLong(); - void getBytes(void *to, int count); private: - unsigned char *data; - int size; - int offset; + unsigned char *data; + int size; + int offset; }; - - - -class CborListener { +class CborListener +{ public: - virtual void OnInteger(int value) = 0; - virtual void OnBytes(unsigned char *data, int size) = 0; - virtual void OnString(String &str) = 0; - virtual void OnArray(int size) = 0; - virtual void OnMap(int size) = 0; - virtual void OnTag(unsigned int tag) = 0; - virtual void OnSpecial(unsigned int code) = 0; - virtual void OnError(const char *error) = 0; - virtual void OnExtraInteger(unsigned long long value, int sign) {} - virtual void OnExtraTag(unsigned long long tag) {} - virtual void OnExtraSpecial(unsigned long long tag) {} + virtual void OnInteger(int value) = 0; + virtual void OnBytes(unsigned char *data, int size) = 0; + virtual void OnString(String &str) = 0; + virtual void OnArray(int size) = 0; + virtual void OnMap(int size) = 0; + virtual void OnTag(unsigned int tag) = 0; + virtual void OnSpecial(unsigned int code) = 0; + virtual void OnError(const char *error) = 0; + virtual void OnExtraInteger(unsigned long long value, int sign) {} + virtual void OnExtraTag(unsigned long long tag) {} + virtual void OnExtraSpecial(unsigned long long tag) {} }; -class CborDebugListener : public CborListener { +class CborDebugListener : public CborListener +{ public: - virtual void OnInteger(int value); - virtual void OnBytes(unsigned char *data, int size); - virtual void OnString(String &str); - virtual void OnArray(int size); - virtual void OnMap(int size); - virtual void OnTag(unsigned int tag); - virtual void OnSpecial(unsigned int code); - virtual void OnError(const char *error); - - virtual void OnExtraInteger(unsigned long long value, int sign); - virtual void OnExtraTag(unsigned long long tag); - virtual void OnExtraSpecial(unsigned long long tag); + virtual void OnInteger(int value); + virtual void OnBytes(unsigned char *data, int size); + virtual void OnString(String &str); + virtual void OnArray(int size); + virtual void OnMap(int size); + virtual void OnTag(unsigned int tag); + virtual void OnSpecial(unsigned int code); + virtual void OnError(const char *error); + + virtual void OnExtraInteger(unsigned long long value, int sign); + virtual void OnExtraTag(unsigned long long tag); + virtual void OnExtraSpecial(unsigned long long tag); }; -class CborReader { +class CborReader +{ public: - CborReader(CborInput &input); - CborReader(CborInput &input, CborListener &listener); - ~CborReader(); - void Run(); - void SetListener(CborListener &listener); + CborReader(CborInput &input); + CborReader(CborInput &input, CborListener &listener); + ~CborReader(); + void Run(); + void SetListener(CborListener &listener); + private: - CborListener *listener; - CborInput *input; - CborReaderState state; - int currentLength; + CborListener *listener; + CborInput *input; + CborReaderState state; + int currentLength; + void StateTypeHelper(); + void StatePintHelper(unsigned int &temp); + void StateNintHelper(unsigned int &temp); + void StateByteSizeHelper(); + void StateBytesDataHelper(); + void StateStringSizeHelper(); + void StateStringDataHelper(); + void StateArrayHelper(); + void StateMapHelper(); + void StateTagHelper(); + void StateSpecialHelper(); }; +class CborExampleListener : public CborListener +{ +public: + void OnInteger(int value); + void OnBytes(unsigned char *data, int size); + void OnString(String &str); + void OnArray(int size); + void OnMap(int size); + void OnTag(unsigned int tag); + void OnSpecial(int code); + void OnError(const char *error); +}; -class CborExampleListener : public CborListener { - public: - void OnInteger(int value); - void OnBytes(unsigned char *data, int size); - void OnString(String &str); - void OnArray(int size); - void OnMap(int size); - void OnTag(unsigned int tag); - void OnSpecial(int code); - void OnError(const char *error); - }; - - - -#endif +#endif // CBOR_DECODER_H diff --git a/Cbor/CborEncoder.cpp b/Cbor/CborEncoder.cpp index fe6dc53..41f0974 100644 --- a/Cbor/CborEncoder.cpp +++ b/Cbor/CborEncoder.cpp @@ -1,208 +1,264 @@ +/** + * CBOR Encoder + */ + #include "CborEncoder.h" #include "Arduino.h" #include - - - - -CborStaticOutput::CborStaticOutput(unsigned int capacity) { - this->capacity = capacity; - this->buffer = new unsigned char[capacity]; - this->offset = 0; +CborStaticOutput::CborStaticOutput(unsigned int capacity) +{ + this->capacity = capacity; + this->buffer = new unsigned char[capacity]; + this->offset = 0; } -CborStaticOutput::~CborStaticOutput() { - delete buffer; +CborStaticOutput::~CborStaticOutput() +{ + if (buffer) + { + delete buffer; + } } -void CborStaticOutput::putByte(unsigned char value) { - if(offset < capacity) { - buffer[offset++] = value; - } else { - Serial.print("buffer overflow error"); - } -} +void CborStaticOutput::putByte(unsigned char value) +{ + if (offset < capacity) + { + buffer[offset++] = value; + return; + } -void CborStaticOutput::putBytes(const unsigned char *data, int size) { - if(offset + size - 1 < capacity) { - memcpy(buffer + offset, data, size); - offset += size; - } else { - Serial.print("buffer overflow error"); - } + Serial.print("buffer overflow error"); } +void CborStaticOutput::putBytes(const unsigned char *data, int size) +{ + if (offset + size - 1 < capacity) + { + memcpy(buffer + offset, data, size); + offset += size; + return; + } - -CborWriter::CborWriter(CborOutput &output) { - this->output = &output; + Serial.print("buffer overflow error"); } -CborWriter::~CborWriter() { - +CborWriter::CborWriter(CborOutput &output) +{ + this->output = &output; } -unsigned char *CborStaticOutput::getData() { - return buffer; +CborWriter::~CborWriter() +{ + // Intentionally empty. } -unsigned int CborStaticOutput::getSize() { - return offset; +unsigned char *CborStaticOutput::getData() +{ + return buffer; } +unsigned int CborStaticOutput::getSize() +{ + return offset; +} -CborDynamicOutput::CborDynamicOutput() { - init(256); +CborDynamicOutput::CborDynamicOutput() +{ + init(256); } -CborDynamicOutput::CborDynamicOutput(unsigned int initalCapacity) { - init(initalCapacity); +CborDynamicOutput::CborDynamicOutput(unsigned int initalCapacity) +{ + init(initalCapacity); } -CborDynamicOutput::~CborDynamicOutput() { +CborDynamicOutput::~CborDynamicOutput() +{ + if (buffer) + { delete buffer; + } } -void CborDynamicOutput::init(unsigned int initalCapacity) { - this->capacity = initalCapacity; - this->buffer = new unsigned char[initalCapacity]; - this->offset = 0; +void CborDynamicOutput::init(unsigned int initalCapacity) +{ + this->capacity = initalCapacity; + this->buffer = new unsigned char[initalCapacity]; + this->offset = 0; } - -unsigned char *CborDynamicOutput::getData() { - return buffer; +unsigned char *CborDynamicOutput::getData() +{ + return buffer; } -unsigned int CborDynamicOutput::getSize() { - return offset; +unsigned int CborDynamicOutput::getSize() +{ + return offset; } -void CborDynamicOutput::putByte(unsigned char value) { - if(offset < capacity) { - buffer[offset++] = value; - } else { - capacity *= 2; - buffer = (unsigned char *) realloc(buffer, capacity); - buffer[offset++] = value; - } +void CborDynamicOutput::putByte(unsigned char value) +{ + if (offset < capacity) + { + buffer[offset++] = value; + return; + } + + capacity *= 2; + buffer = (unsigned char *)realloc(buffer, capacity); + buffer[offset++] = value; } -void CborDynamicOutput::putBytes(const unsigned char *data, int size) { - while(offset + size > capacity) { - capacity *= 2; - buffer = (unsigned char *) realloc(buffer, capacity); - } +void CborDynamicOutput::putBytes(const unsigned char *data, int size) +{ + while (offset + size > capacity) + { + capacity *= 2; + buffer = (unsigned char *)realloc(buffer, capacity); + } - memcpy(buffer + offset, data, size); - offset += size; + memcpy(buffer + offset, data, size); + offset += size; } - - -void CborWriter::writeTypeAndValue(int majorType, unsigned int value) { - majorType <<= 5; - if(value < 24) { - output->putByte(majorType | value); - } else if(value < 256) { - output->putByte(majorType | 24); - output->putByte(value); - } else if(value < 65536) { - output->putByte(majorType | 25); - output->putByte(value >> 8); - output->putByte(value); - } else { - output->putByte(majorType | 26); - output->putByte(value >> 24); - output->putByte(value >> 16); - output->putByte(value >> 8); - output->putByte(value); - } +void CborWriter::writeTypeAndValue(int majorType, unsigned int value) +{ + majorType <<= 5; + + if (value < 24) + { + output->putByte(majorType | value); + } + else if (value < 256) + { + output->putByte(majorType | 24); + output->putByte(value); + } + else if (value < 65536) + { + output->putByte(majorType | 25); + output->putByte(value >> 8); + output->putByte(value); + } + else + { + output->putByte(majorType | 26); + output->putByte(value >> 24); + output->putByte(value >> 16); + output->putByte(value >> 8); + output->putByte(value); + } } -void CborWriter::writeTypeAndValue(int majorType, unsigned long long value) { - majorType <<= 5; - if(value < 24ULL) { - output->putByte(majorType | value); - } else if(value < 256ULL) { - output->putByte(majorType | 24); - output->putByte(value); - } else if(value < 65536ULL) { - output->putByte(majorType | 25); - output->putByte(value >> 8); - } else if(value < 4294967296ULL) { - output->putByte(majorType | 26); - output->putByte(value >> 24); - output->putByte(value >> 16); - output->putByte(value >> 8); - output->putByte(value); - } else { - output->putByte(majorType | 27); - output->putByte(value >> 56); - output->putByte(value >> 48); - output->putByte(value >> 40); - output->putByte(value >> 32); - output->putByte(value >> 24); - output->putByte(value >> 16); - output->putByte(value >> 8); - output->putByte(value); - } +void CborWriter::writeTypeAndValue(int majorType, unsigned long long value) +{ + majorType <<= 5; + + if (value < 24ULL) + { + output->putByte(majorType | value); + } + else if (value < 256ULL) + { + output->putByte(majorType | 24); + output->putByte(value); + } + else if (value < 65536ULL) + { + output->putByte(majorType | 25); + output->putByte(value >> 8); + } + else if (value < 4294967296ULL) + { + output->putByte(majorType | 26); + output->putByte(value >> 24); + output->putByte(value >> 16); + output->putByte(value >> 8); + output->putByte(value); + } + else + { + output->putByte(majorType | 27); + output->putByte(value >> 56); + output->putByte(value >> 48); + output->putByte(value >> 40); + output->putByte(value >> 32); + output->putByte(value >> 24); + output->putByte(value >> 16); + output->putByte(value >> 8); + output->putByte(value); + } } -void CborWriter::writeInt(unsigned int value) { - writeTypeAndValue(0, value); +void CborWriter::writeInt(unsigned int value) +{ + writeTypeAndValue(0, value); } -void CborWriter::writeInt(unsigned long long value) { - writeTypeAndValue(0, value); +void CborWriter::writeInt(unsigned long long value) +{ + writeTypeAndValue(0, value); } -void CborWriter::writeInt(long long value) { - if(value < 0) { - writeTypeAndValue(1, (unsigned long long) -value); - } else { - writeTypeAndValue(0, (unsigned long long) value); - } -} +void CborWriter::writeInt(long long value) +{ + if (value < 0) + { + writeTypeAndValue(1, (unsigned long long)-value); + return; + } -void CborWriter::writeInt(int value) { - if(value < 0) { - writeTypeAndValue(1, (unsigned int) -value); - } else { - writeTypeAndValue(0, (unsigned int) value); - } + writeTypeAndValue(0, (unsigned long long)value); } -void CborWriter::writeBytes(const unsigned char *data, unsigned int size) { - writeTypeAndValue(2, size); - output->putBytes(data, size); -} +void CborWriter::writeInt(int value) +{ + if (value < 0) + { + writeTypeAndValue(1, (unsigned int)-value); + return; + } -void CborWriter::writeString(const char *data, unsigned int size) { - writeTypeAndValue(3, size); - output->putBytes((const unsigned char *)data, size); + writeTypeAndValue(0, (unsigned int)value); } -void CborWriter::writeString(const String str) { - writeTypeAndValue(3, (unsigned int)str.length()); - output->putBytes((const unsigned char *)str.c_str(), str.length()); +void CborWriter::writeBytes(const unsigned char *data, unsigned int size) +{ + writeTypeAndValue(2, size); + output->putBytes(data, size); } +void CborWriter::writeString(const char *data, unsigned int size) +{ + writeTypeAndValue(3, size); + output->putBytes((const unsigned char *)data, size); +} -void CborWriter::writeArray(int size) { - writeTypeAndValue(4, (unsigned int)size); +void CborWriter::writeString(const String str) +{ + writeTypeAndValue(3, (unsigned int)str.length()); + output->putBytes((const unsigned char *)str.c_str(), str.length()); } -void CborWriter::writeMap(int size) { - writeTypeAndValue(5, (unsigned int)size); +void CborWriter::writeArray(int size) +{ + writeTypeAndValue(4, (unsigned int)size); } -void CborWriter::writeTag(const unsigned int tag) { - writeTypeAndValue(6, tag); +void CborWriter::writeMap(int size) +{ + writeTypeAndValue(5, (unsigned int)size); } -void CborWriter::writeSpecial(int special) { - writeTypeAndValue(7, (unsigned int)special); +void CborWriter::writeTag(const unsigned int tag) +{ + writeTypeAndValue(6, tag); } +void CborWriter::writeSpecial(int special) +{ + writeTypeAndValue(7, (unsigned int)special); +} diff --git a/Cbor/CborEncoder.h b/Cbor/CborEncoder.h index 45b3876..a8f6eb1 100644 --- a/Cbor/CborEncoder.h +++ b/Cbor/CborEncoder.h @@ -1,92 +1,96 @@ -/* - Copyright 2014-2015 Stanislav Ovsyannikov - - Licensed under the Apache License, Version 2.0 (the "License"); - you may not use this file except in compliance with the License. - You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. -*/ - - - - -#ifndef CBOREN_H -#define CBOREN_H +/** + * Copyright 2014-2015 Stanislav Ovsyannikov + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef CBOR_ENCODER_H +#define CBOR_ENCODER_H #include "Arduino.h" -class CborOutput { +class CborOutput +{ public: - virtual unsigned char *getData() = 0; - virtual unsigned int getSize() = 0; - virtual void putByte(unsigned char value) = 0; - virtual void putBytes(const unsigned char *data, int size) = 0; + virtual unsigned char *getData() = 0; + virtual unsigned int getSize() = 0; + virtual void putByte(unsigned char value) = 0; + virtual void putBytes(const unsigned char *data, int size) = 0; }; -class CborStaticOutput : public CborOutput { +class CborStaticOutput : public CborOutput +{ public: - CborStaticOutput(unsigned int capacity); - ~CborStaticOutput(); - virtual unsigned char *getData(); - virtual unsigned int getSize(); - virtual void putByte(unsigned char value); - virtual void putBytes(const unsigned char *data, int size); + CborStaticOutput(unsigned int capacity); + ~CborStaticOutput(); + virtual unsigned char *getData(); + virtual unsigned int getSize(); + virtual void putByte(unsigned char value); + virtual void putBytes(const unsigned char *data, int size); + private: - unsigned char *buffer; - unsigned int capacity; - unsigned int offset; + unsigned char *buffer; + unsigned int capacity; + unsigned int offset; }; - -class CborDynamicOutput : public CborOutput { +class CborDynamicOutput : public CborOutput +{ public: - CborDynamicOutput(); - CborDynamicOutput(unsigned int initalCapacity); - ~CborDynamicOutput(); + CborDynamicOutput(); + CborDynamicOutput(unsigned int initalCapacity); + ~CborDynamicOutput(); + virtual unsigned char *getData(); + virtual unsigned int getSize(); + virtual void putByte(unsigned char value); + virtual void putBytes(const unsigned char *data, int size); - virtual unsigned char *getData(); - virtual unsigned int getSize(); - virtual void putByte(unsigned char value); - virtual void putBytes(const unsigned char *data, int size); private: - void init(unsigned int initalCapacity); - unsigned char *buffer; - unsigned int capacity; - unsigned int offset; + void init(unsigned int initalCapacity); + unsigned char *buffer; + unsigned int capacity; + unsigned int offset; }; -class CborWriter { +class CborWriter +{ public: - CborWriter(CborOutput &output); - ~CborWriter(); + CborWriter(CborOutput &output); + ~CborWriter(); + + void writeInt(int value); + void writeInt(long long value); + void writeInt(unsigned int value); + void writeInt(unsigned long long value); + void writeBytes(const unsigned char *data, unsigned int size); + void writeString(const char *data, unsigned int size); + void writeString(const String str); + void writeArray(int size); + void writeMap(int size); + void writeTag(const unsigned int tag); + void writeSpecial(int special); - void writeInt(int value); - void writeInt(long long value); - void writeInt(unsigned int value); - void writeInt(unsigned long long value); - void writeBytes(const unsigned char *data, unsigned int size); - void writeString(const char *data, unsigned int size); - void writeString(const String str); - void writeArray(int size); - void writeMap(int size); - void writeTag(const unsigned int tag); - void writeSpecial(int special); private: - void writeTypeAndValue(int majorType, unsigned int value); - void writeTypeAndValue(int majorType, unsigned long long value); - CborOutput *output; + void writeTypeAndValue(int majorType, unsigned int value); + void writeTypeAndValue(int majorType, unsigned long long value); + CborOutput *output; }; -class CborSerializable { +class CborSerializable +{ public: - virtual void Serialize(CborWriter &writer) = 0; + virtual void Serialize(CborWriter &writer) = 0; }; -#endif + +#endif // CBOR_ENCODER_H diff --git a/Cbor/Examples/Cbor_Serialport_sender/Cbor_Serialport_sender.ino b/Cbor/Examples/Cbor_Serialport_sender/Cbor_Serialport_sender.ino index 5d838db..610e86f 100644 --- a/Cbor/Examples/Cbor_Serialport_sender/Cbor_Serialport_sender.ino +++ b/Cbor/Examples/Cbor_Serialport_sender/Cbor_Serialport_sender.ino @@ -1,44 +1,42 @@ /* + Arduino Sketch to show how Send Encode Cbor package via Serial port -Arduino Sketch to show how Send Encode Cbor package via Serial port + Author: Juanjo Tara + Email: j.tara@arduino.cc + Date: 24/04/2015 -Author: Juanjo Tara -email: j.tara@arduino.cc -date: 24/04/2015 + Updated: Rich Morgan + Email: rich.l.morgan@gmail.com + Date: 15/12/2023 */ - #include "CborEncoder.h" - - -void setup() { - Serial.begin(9600); - +void setup() +{ + Serial.begin(9600); } -void loop() { - +void loop() +{ testSerialPort(); delay(10000); - } -void testSerialPort() { - CborStaticOutput output(32); - CborWriter writer(output); - //Write a Cbor Package with a number and String - writer.writeInt(124); - writer.writeString("I"); - - //get length and data of cbor package - unsigned int datalength = output.getSize(); - unsigned char *datapkg = output.getData(); - - //print in Serial port the Data length and Cbor in binary - //Serial.print("datalength:"); - Serial.print(datalength); - Serial.write(datapkg,datalength); -} +void testSerialPort() +{ + CborStaticOutput output(32); + CborWriter writer(output); + // Write a Cbor Package with a number and String + writer.writeInt(124); + writer.writeString("I"); + // get length and data of cbor package + unsigned int datalength = output.getSize(); + unsigned char *datapkg = output.getData(); + + // print in Serial port the Data length and Cbor in binary + Serial.print(datalength); + Serial.write(datapkg, datalength); +} diff --git a/Cbor/Examples/Cbor_Serialport_sender/Cbor_Serialport_sender.ino~ b/Cbor/Examples/Cbor_Serialport_sender/Cbor_Serialport_sender.ino~ deleted file mode 100644 index 2c40df5..0000000 --- a/Cbor/Examples/Cbor_Serialport_sender/Cbor_Serialport_sender.ino~ +++ /dev/null @@ -1,44 +0,0 @@ -/* - -Arduino Sketch to show how Send Encode Cbor package via Serial port - -Author: Juanjo Tara -email: j.tara@arduino.cc -date: 24/04/2015 -*/ - - -#include "CborEncoder.h" - - - -void setup() { - Serial.begin(9600); - -} - -void loop() { - - testSerialPort(); - delay(10000); - -} - -void testSerialPort() { - CborStaticOutput output(32); - CborWriter writer(output); - //Write a Cbor Package with a number and String - writer.writeInt(124); - writer.writeString("I"); - - //get length and data of cbor package - unsigned int datalength = output.getSize(); - unsigned char *datapkg = output.getData(); - - //print in Serial port the Data length and Cbor in binary - //Serial.print("datalength:"); - Serial.print(datalength); - Serial.write(*datapkg,datalength); -} - - diff --git a/Cbor/Examples/Cbor_master_reader/Cbor_master_reader.ino b/Cbor/Examples/Cbor_master_reader/Cbor_master_reader.ino index a4e72bb..13bf583 100644 --- a/Cbor/Examples/Cbor_master_reader/Cbor_master_reader.ino +++ b/Cbor/Examples/Cbor_master_reader/Cbor_master_reader.ino @@ -1,39 +1,41 @@ /* + Arduino Sketch to show how to decode a CBOR package received from I2c. + Load this sketch in the first (master) Arduino. -Arduino Sketch to show how decode a Cbor package received from I2c -This sketch must to load in the master Arduino + Author: Juanjo Tara + Email: j.tara@arduino.cc + Date: 24/04/2015 -Author: Juanjo Tara -email: j.tara@arduino.cc -date: 24/04/2015 + Updated: Rich Morgan + Email: rich.l.morgan@gmail.com + Date: 15/12/2023 */ - #include #include - void setup() { - Wire.begin(); // join i2c bus (address optional for master) - Serial.begin(9600); // start serial for output - + Wire.begin(); // join i2c bus (address optional for master) + Serial.begin(9600); // start serial for output } void loop() { - Wire.requestFrom(2, 4); // request 6 bytes from slave device #2 + Wire.requestFrom(2, 4); // request 6 bytes from slave device #2 - while (Wire.available()) // slave may send less than requested + while (Wire.available()) // slave may send less than requested { - char c = Wire.read(); // receive a byte as character - Serial.print(c); // print the character + char c = Wire.read(); // receive a byte as character + Serial.print(c); // print the character } CborInput input(data, size); CborReader reader(input); CborExampleListener listener; + reader.SetListener(listener); reader.Run(); + delay(500); } diff --git a/Cbor/Examples/Cbor_slave_sender/Cbor_slave_sender.ino b/Cbor/Examples/Cbor_slave_sender/Cbor_slave_sender.ino index d2dccb2..3811659 100644 --- a/Cbor/Examples/Cbor_slave_sender/Cbor_slave_sender.ino +++ b/Cbor/Examples/Cbor_slave_sender/Cbor_slave_sender.ino @@ -1,44 +1,43 @@ /* + Arduino Sketch to show how to send encoded CBOR package via I2c + as slave requester. Load this sketch must in the second (slave) + Arduino. -Arduino Sketch to show how Send Encode Cbor package and sending via I2c as Slave requester -This sketch must to load in the Slave Arduino + Author: Juanjo Tara + Email: j.tara@arduino.cc + Date: 24/04/2015 -Author: Juanjo Tara -email: j.tara@arduino.cc -date: 24/04/2015 + Updated: Rich Morgan + Email: rich.l.morgan@gmail.com + Date: 15/12/2023 */ - - - #include "CborEncoder.h" #include - -void setup() { +void setup() +{ Wire.begin(2); // join i2c bus with address #2 Wire.onRequest(requestEvent); // register event + Serial.begin(9600); - // writting(); } -void loop() { - +void loop() +{ + // Intentionally empty. } - -// function that executes whenever data is requested by master -// this function is registered as an event, see setup() +// Function that executes whenever data is requested by master. +// This function is registered as an event, see setup() void requestEvent() { - - //get length and data of cbor package + // get length and data of cbor package unsigned char *datapkg = output.getData(); int datalength = output.getSize(); - + Serial.print("datalength:"); Serial.print(datalength); - - Wire.write(*datapkg); // respond with message + Wire.write(*datapkg); // respond with message } diff --git a/Cbor/Examples/cbortest/cbortest.ino b/Cbor/Examples/cbortest/cbortest.ino index 30fc385..987eaae 100644 --- a/Cbor/Examples/cbortest/cbortest.ino +++ b/Cbor/Examples/cbortest/cbortest.ino @@ -1,50 +1,50 @@ /* + Arduino Sketch to show how encode and decode CBOR package. -Arduino Sketch to show how encode and Decode Cbor package + Author: Juanjo Tara + Email: j.tara@arduino.cc + Date: 24/04/2015 -Author: Juanjo Tara -email: j.tara@arduino.cc -date: 24/04/2015 + Updated: Rich Morgan + Email: rich.l.morgan@gmail.com + Date: 15/12/2023 */ - - - #include "CborEncoder.h" #include "CborDecoder.h" -void setup() { - +void setup() +{ Serial.begin(9600); - //Serial.print("hola"); + test1(); } -void loop() { +void loop() +{ + // Intentionally empty. +} +void test1() +{ + CborStaticOutput output(32); + CborWriter writer(output); -} + // Write a Cbor Package with a number and String + writer.writeInt(124); + writer.writeString("I"); + unsigned int sizeee = output.getSize(); + Serial.print("datalength:"); + Serial.println(sizeee); -void test1() { - CborStaticOutput output(32); - CborWriter writer(output); + delay(1000); - //Write a Cbor Package with a number and String - writer.writeInt(124); - writer.writeString("I"); + CborInput input(output.getData(), output.getSize()); + CborDebugListener listener; + CborReader reader(input); - unsigned int sizeee = output.getSize(); - Serial.print("datalength:"); - Serial.println(sizeee); - delay(1000); - - - CborInput input(output.getData(), output.getSize()); - CborDebugListener listener; - CborReader reader(input); - reader.SetListener(listener); - reader.Run(); - + reader.SetListener(listener); + reader.Run(); }