2#include "main-header.h"
3#include <platform-header.h>
8#define CNWDEBUGCOM(a, ...) noop()
12int add_uint8(
byte *buf,
int bufsize,
int *offset, uint32_t value) {
13 if (bufsize - *offset < 2) {
16 buf[*offset] = value & 0xFF;
17 *offset = *offset + 1;
21int add_uint16(
byte *buf,
int bufsize,
int *offset, uint32_t value) {
22 if (bufsize - *offset < 3) {
27 for (i = 0; i < 2; i++) {
28 buf[*offset] = value & 0xFF;
30 *offset = *offset + 1;
36int add_uint32(
byte *buf,
int bufsize,
int *offset, uint32_t value) {
37 if (bufsize - *offset < 5) {
42 for (i = 0; i < 4; i++) {
43 buf[*offset] = value & 0xFF;
45 *offset = *offset + 1;
51int add_uint64(
byte *buf,
int bufsize,
int *offset, uint64_t value) {
52 if (bufsize - *offset < 9) {
57 for (i = 0; i < 8; i++) {
58 buf[*offset] = value & 0xFF;
60 *offset = *offset + 1;
69int encode(
byte *buf,
int *bufsize,
struct command *com) {
72 CNWDEBUGCOM(
"-------- Encoding --------\n");
85 if ((err = add_uint64(buf, *bufsize, &offset, (uint64_t)com->
sender)) != 0) {
88 if ((err = add_uint64(buf, *bufsize, &offset, (uint64_t)com->
recipient)) != 0) {
91 if ((err = add_uint64(buf, *bufsize, &offset, (uint64_t)com->
target)) != 0) {
94 if ((err = add_uint32(buf, *bufsize, &offset, (uint32_t)com->
index)) != 0) {
97 if ((err = add_uint8(buf, *bufsize, &offset, com->
com)) != 0) {
100 if ((err = add_uint8(buf, *bufsize, &offset, com->
flags)) != 0) {
103 if ((err = add_uint8(buf, *bufsize, &offset, com->
argctr)) != 0) {
110 for (i = 0; i < com->
argctr; i++) {
117 if (arg[1] & (1 << 7)) {
121 if ((offset + argsize) >= *bufsize) {
125 for (j = 0; j < argsize + header; j++) {
126 buf[offset++] = arg[j];
133 add_uint16(buf, *bufsize, &temp, offset);
136 for (i = 0; i < offset; i++) {
137 chk = chk + (chk ^ ((uint16_t)buf[i]));
139 add_uint16(buf, *bufsize, &temp, chk);
145int typesize(uint8_t type) {
146 if ((type == 1) || (type == 5)) {
149 if ((type == 2) || (type = 6)) {
152 if ((type == 3) || (type = 7)) {
155 if ((type == 4) || (type = 8)) {
158 printf(
"WARNING - got a type %i\n", type);
163int fieldlen(uint8_t fieldtype, uint8_t array_size) {
164 uint8_t ts = typesize(fieldtype & ~(1 << 7));
166 if (fieldtype & (1 << 7)) {
167 return ts * array_size;
172int fieldlen_inmem(uint8_t fieldtype, uint8_t array_size) {
173 int t = fieldlen(fieldtype, array_size);
175 if (fieldtype & (1 << 7)) {
int get_arg_size_inmem(const struct command *com, int index)
given an argument by index [0..n], will returns the number in bytes this argument takes up in memory....
const byte * get_arg_new(const struct command *com, int index)
given an argument by index[0..n], will returns a pointer to it. This will include the fieldnumber and...