Skip to content

Commit

Permalink
firmware: decoding BER...
Browse files Browse the repository at this point in the history
  • Loading branch information
twystd committed Aug 16, 2024
1 parent 4bfc44e commit f99d53e
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 18 deletions.
2 changes: 0 additions & 2 deletions Rev.0/emulator/encoding/bisync/bisync.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,10 @@ func (codec *Bisync) decode(msg []uint8, f callback) error {
codec.reset()

case STX:
println("STX")
codec.SOH = false
codec.STX = true

case ETX:
println("ETX")
codec.SOH = false
codec.STX = false

Expand Down
32 changes: 17 additions & 15 deletions Rev.0/emulator/ssmp/ssmp.go
Original file line number Diff line number Diff line change
Expand Up @@ -318,14 +318,16 @@ func listen(USB string, tx chan []byte, rx chan []byte, pipe chan []byte, errors
for {
select {
case msg := <-pipe:
// if len(msg) > 20 {
// debugf("read (%v bytes) [%v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v ...]",
// len(msg),
// msg[0], msg[1], msg[2], msg[3], msg[4], msg[5], msg[6], msg[7], msg[8], msg[9],
// msg[10], msg[11], msg[12], msg[13], msg[14], msg[15], msg[16], msg[17], msg[18], msg[19])
// } else {
// debugf("read (%v bytes) %v", len(msg), msg)
// }
if msg[0] != 22 {
debugf("read (%v bytes) %v", len(msg), string(msg))
} else if len(msg) > 20 {
debugf("read (%v bytes) [%v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v ...]",
len(msg),
msg[0], msg[1], msg[2], msg[3], msg[4], msg[5], msg[6], msg[7], msg[8], msg[9],
msg[10], msg[11], msg[12], msg[13], msg[14], msg[15], msg[16], msg[17], msg[18], msg[19])
} else {
debugf("read (%v bytes) %v", len(msg), msg)
}

select {
case rx <- msg:
Expand All @@ -339,13 +341,13 @@ func listen(USB string, tx chan []byte, rx chan []byte, pipe chan []byte, errors
warnf("write error (%v)", err)
} else if N != len(msg) {
warnf("write error (%v)", fmt.Errorf("sent %v bytes of %v)", N, len(msg)))
// } else if len(msg) > 20 {
// debugf("write (%v bytes) [%v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v ...]",
// N,
// msg[0], msg[1], msg[2], msg[3], msg[4], msg[5], msg[6], msg[7], msg[8], msg[9],
// msg[10], msg[11], msg[12], msg[13], msg[14], msg[15], msg[16], msg[17], msg[18], msg[19])
// } else {
// debugf("write (%v bytes) %v", N, msg)
} else if len(msg) > 20 {
debugf("write (%v bytes) [%v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v %v ...]",
N,
msg[0], msg[1], msg[2], msg[3], msg[4], msg[5], msg[6], msg[7], msg[8], msg[9],
msg[10], msg[11], msg[12], msg[13], msg[14], msg[15], msg[16], msg[17], msg[18], msg[19])
} else {
debugf("write (%v bytes) %v", N, msg)
}

case <-idle:
Expand Down
2 changes: 2 additions & 0 deletions Rev.0/firmware/lib/include/encoding/encoding.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ typedef struct packet {
PACKET tag;
union {
struct {
uint8_t version;
uint32_t request_id;
} get;

struct {
uint8_t version;
uint32_t request_id;
} get_response;
};
Expand Down
40 changes: 40 additions & 0 deletions Rev.0/firmware/lib/src/encoding/BER/BER.c
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <encoding/BER/BER.h>

void unpack_sequence(const uint8_t *message, int N, int *ix);

// clang-format off
const uint8_t RESPONSE[] = {
48, 41, 2, 1, 0, 4, 6, 112,
Expand All @@ -28,9 +31,46 @@ struct packet BER_decode(const uint8_t *message, int N) {
packet p = {
.tag = PACKET_GET,
.get = {
.version = 0,
.request_id = 1,
},
};

// ... unpack message
int ix = 0;

if (ix < N) {
uint8_t tag = message[ix++];

switch (tag) {
case 0x30:
unpack_sequence(message, N, &ix);
break;
}
}

fflush(stdout);

return p;
}

void unpack_sequence(const uint8_t *message, int N, int *ix) {
// ... length
uint64_t length = 0;
if (*ix < N) {
uint8_t b = message[*ix++];

if (b & 0x80 == 0x80) {
length = (uint64_t)(b & 0x7f);
} else {
int len = b & 0x7f;
for (int i = 0; i < len && *ix < N; i++) {
length <<= 8;
length += (uint64_t)(message[*ix++]);
}
}
}

printf("<<sequence %llu>> ", length);
fflush(stdout);
}
6 changes: 5 additions & 1 deletion TODO.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@
- [x] return SnmpGetResponse
- [x] encode::DLE
- [x] require STX-ETX framing
- [x] BER
- [ ] CRC
- [ ] BER
- https://www.oss.com/asn1/resources/asn1-made-simple/asn1-quick-reference/basic-encoding-rules.html
```
2024-08-16 15:33:33 DEBUG SSMP write (58 bytes) [22 22 2 48 37 16 2 16 1 0 4 16 6 112 117 98 108 105 99 160 ...]
```
- [x] SSMP idle - revert to MODE_UNKNOWN
- [ ] Enable FIFO
- [ ] pico: txrx/uart conflict
Expand Down

0 comments on commit f99d53e

Please sign in to comment.