Berry bytes get and set work for 3 bytes values (#19225)

* Berry bytes `get` and `set` work for 3 bytes values

* Fix error message
This commit is contained in:
s-hadinger 2023-07-31 13:57:07 +02:00 committed by GitHub
parent 148c1a2f74
commit 80617e96e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 45 additions and 2 deletions

View File

@ -8,6 +8,7 @@ All notable changes to this project will be documented in this file.
- Support for MAX17043 fuel-gauge systems Lipo batteries (#18788)
- Support for multiple PCA9685 with extended functionality (#18805)
- Zigbee decode Aqara 0000/FF01 attribute 03 as Temperature
- Berry bytes `get` and `set` work for 3 bytes values
### Breaking Changed

View File

@ -356,6 +356,40 @@ static uint16_t buf_get2_be(buf_impl* attr, size_t offset)
return 0;
}
static uint32_t buf_get3_le(buf_impl* attr, size_t offset)
{
if ((int32_t)offset + 2 < attr->len) {
return attr->bufptr[offset] | (attr->bufptr[offset+1] << 8) | (attr->bufptr[offset+2] << 16);
}
return 0;
}
static uint16_t buf_get3_be(buf_impl* attr, size_t offset)
{
if ((int32_t)offset + 2 < attr->len) {
return attr->bufptr[offset+2] | (attr->bufptr[offset+1] << 8) | (attr->bufptr[offset] << 16);
}
return 0;
}
static void buf_set3_le(buf_impl* attr, size_t offset, uint32_t data)
{
if ((int32_t)offset + 2 < attr->len) {
attr->bufptr[offset] = data & 0xFF;
attr->bufptr[offset+1] = (data >> 8) & 0xFF;
attr->bufptr[offset+2] = (data >> 16) & 0xFF;
}
}
static void buf_set3_be(buf_impl* attr, size_t offset, uint32_t data)
{
if ((int32_t)offset + 2 < attr->len) {
attr->bufptr[offset+2] = data & 0xFF;
attr->bufptr[offset+1] = (data >> 8) & 0xFF;
attr->bufptr[offset] = (data >> 16) & 0xFF;
}
}
static void buf_set4_le(buf_impl* attr, size_t offset, uint32_t data)
{
if ((int32_t)offset + 3 < attr->len) {
@ -832,12 +866,18 @@ static int m_get(bvm *vm, bbool sign)
case 2: ret = buf_get2_le(&attr, idx);
if (sign) { ret = (int16_t)(uint16_t) ret; }
break;
case 3: ret = buf_get3_le(&attr, idx);
if (sign & (ret & 0x800000)) { ret = ret | 0xFF000000; }
break;
case 4: ret = buf_get4_le(&attr, idx); break;
case -2: ret = buf_get2_be(&attr, idx);
if (sign) { ret = (int16_t)(uint16_t) ret; }
break;
case -3: ret = buf_get3_be(&attr, idx);
if (sign & (ret & 0x800000)) { ret = ret | 0xFF000000; }
break;
case -4: ret = buf_get4_be(&attr, idx); break;
default: be_raise(vm, "type_error", "size must be -4, -2, -1, 0, 1, 2 or 4.");
default: be_raise(vm, "type_error", "size must be -4, -3, -2, -1, 0, 1, 2, 3 or 4.");
}
be_pop(vm, argc - 1);
if (vsize != 0) {
@ -911,10 +951,12 @@ static int m_set(bvm *vm)
case -1: /* fallback below */
case 1: buf_set1(&attr, idx, value); break;
case 2: buf_set2_le(&attr, idx, value); break;
case 3: buf_set3_le(&attr, idx, value); break;
case 4: buf_set4_le(&attr, idx, value); break;
case -2: buf_set2_be(&attr, idx, value); break;
case -3: buf_set3_be(&attr, idx, value); break;
case -4: buf_set4_be(&attr, idx, value); break;
default: be_raise(vm, "type_error", "size must be -4, -2, -1, 0, 1, 2 or 4.");
default: be_raise(vm, "type_error", "size must be -4, -3, -2, -1, 0, 1, 2, 3 or 4.");
}
be_pop(vm, argc - 1);
m_write_attributes(vm, 1, &attr); /* update attributes */