Commit 31cff805 by Enrico Pozzobon

arduino uno working bench, better

parent 01418953
#pragma once
#ifdef __cplusplus
extern "C" {
#endif
void uartp_send(const void *src, uint16_t len);
uint16_t uartp_recv(void *dst, uint16_t buf_len);
#ifdef __cplusplus
}
#endif
#include "crypto_aead.h"
#include "api.h"
#include "uartp.h"
#define MAX_BYTES 100
#define DEBUG
//#define DEBUG
uint8_t npub[CRYPTO_NPUBBYTES];
uint8_t nsec[CRYPTO_NSECBYTES];
......@@ -15,6 +16,10 @@ unsigned long long int mlen;
uint8_t c[MAX_BYTES];
unsigned long long int clen;
#ifdef __cplusplus
extern "C" {
#endif
void uart_write(uint8_t x) {
Serial.write((uint8_t) (x));
}
......@@ -27,75 +32,9 @@ uint8_t uart_read() {
return (uint8_t) (0xff & r);
}
// Simple serial protocol with packets and checksum
const uint8_t AMUX_TAG = 0xf9;
const uint8_t AMUX_END = 0xf3;
const uint8_t AMUX_EXT = 0x80;
void uartp_send(const void *src, uint16_t len) {
uint8_t len_ind_0, len_ind_1, fcs, info;
const uint8_t *buf = (const uint8_t *) src;
uart_write(AMUX_TAG);
len_ind_0 = (uint8_t) (0xff & len);
len_ind_1 = (uint8_t) (0xff & (len >> 7));
if (len < 128) {
uart_write(len_ind_0);
} else {
uart_write(len_ind_0 | AMUX_EXT);
uart_write(len_ind_1);
}
fcs = 0;
for (uint16_t i = 0; i < len; i++) {
info = buf[i];
fcs += info;
uart_write(buf[i]);
}
fcs = 255 - fcs;
uart_write(fcs);
uart_write(AMUX_END);
}
uint16_t uartp_recv(void *dst, uint16_t buf_len) {
uint8_t *buf = (uint8_t *) dst;
uint8_t tag_old, tag, info, cs;
uint16_t len;
tag = AMUX_END;
while (1) {
do {
tag_old = tag;
tag = uart_read();
} while(tag != AMUX_TAG || tag_old != AMUX_END);
len = (uint16_t) uart_read();
if (len & AMUX_EXT) {
len &= (~AMUX_EXT);
len |= (uint16_t) (uart_read() << 7);
}
if (len > buf_len) {
return len;
}
uint16_t i = 0;
cs = 0;
for (i = 0; i < len; i++) {
info = uart_read();
buf[i] = info;
cs += info;
}
cs += uart_read();
tag = uart_read();
if (0xff == cs) {
if (AMUX_END == tag) {
return len;
}
}
}
#ifdef __cplusplus
}
#endif
#ifdef DEBUG
#define DEBUG_BUF_LEN 80
......@@ -118,7 +57,7 @@ int dbg_printf(const char *format, ...) {
return r;
}
#else
#define dbg_printf(...) (0)
#define dbg_printf(...) (0)
#endif
void assert(bool b) {
......@@ -134,7 +73,7 @@ void setup() {
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
delay(100);
Serial.println("Hello, World!");
Serial.print("Hello, World!");
}
......@@ -146,8 +85,8 @@ void loop() {
if (len == 0 || len > 255)
return;
dbg_printf("Received command %c\r\n", action);
uint16_t l = len - 1;
uint16_t rl = 0;
uint8_t *var = buf+1;
switch (action) {
case 'm': assert(l <= MAX_BYTES); memcpy(m, var, l); mlen = l; break;
......@@ -166,21 +105,17 @@ void loop() {
res = crypto_aead_decrypt(m, &mlen, nsec, c, clen, ad, adlen, npub, k);
digitalWrite(LED_BUILTIN, HIGH);
break;
}
switch (action) {
case'a': case'c': case'k': case'm': case'p': case's':
case'd': case'e':
l = 0;
break;
case'M': var = m; l = mlen; break;
case'C': var = c; l = clen; break;
case'A': var = ad; l = adlen; break;
case'K': var = k; l = CRYPTO_KEYBYTES; break;
case'P': var = npub; l = CRYPTO_NPUBBYTES; break;
case'S': var = nsec; l = CRYPTO_NSECBYTES; break;
case'M': var = m; rl = mlen; break;
case'C': var = c; rl = clen; break;
case'A': var = ad; rl = adlen; break;
case'K': var = k; rl = CRYPTO_KEYBYTES; break;
case'P': var = npub; rl = CRYPTO_NPUBBYTES; break;
case'S': var = nsec; rl = CRYPTO_NSECBYTES; break;
default:
dbg_printf("Unknown command\r\n");
assert(false);
}
buf[0] = action;
memcpy(buf+1, var, l);
uartp_send(buf, l+1);
memcpy(buf+1, var, rl);
uartp_send(buf, rl+1);
}
#include <stdint.h>
#include "uartp.h"
extern void uart_write(uint8_t x);
extern uint8_t uart_read();
// Simple serial protocol with packets and checksum
const uint8_t AMUX_TAG = 0xf9;
const uint8_t AMUX_END = 0xf3;
const uint8_t AMUX_EXT = 0x80;
void uartp_send(const void *src, uint16_t len) {
uint8_t len_ind_0, len_ind_1, fcs, info;
const uint8_t *buf = (const uint8_t *) src;
uart_write(AMUX_TAG);
len_ind_0 = (uint8_t) (0xff & len);
len_ind_1 = (uint8_t) (0xff & (len >> 7));
if (len < 128) {
uart_write(len_ind_0);
} else {
uart_write(len_ind_0 | AMUX_EXT);
uart_write(len_ind_1);
}
fcs = 0;
for (uint16_t i = 0; i < len; i++) {
info = buf[i];
fcs += info;
uart_write(buf[i]);
}
fcs = 255 - fcs;
uart_write(fcs);
uart_write(AMUX_END);
}
uint16_t uartp_recv(void *dst, uint16_t buf_len) {
uint8_t *buf = (uint8_t *) dst;
uint8_t tag_old, tag, info, cs;
uint16_t len;
tag = AMUX_END;
while (1) {
do {
tag_old = tag;
tag = uart_read();
} while(tag != AMUX_TAG || tag_old != AMUX_END);
len = (uint16_t) uart_read();
if (len & AMUX_EXT) {
len &= (~AMUX_EXT);
len |= (uint16_t) (uart_read() << 7);
}
if (len > buf_len) {
return len;
}
uint16_t i = 0;
cs = 0;
for (i = 0; i < len; i++) {
info = uart_read();
buf[i] = info;
cs += info;
}
cs += uart_read();
tag = uart_read();
if (0xff == cs) {
if (AMUX_END == tag) {
return len;
}
}
}
}
......@@ -6,6 +6,8 @@ import struct
import serial
import subprocess
def eprint(*args, **kargs):
print(*args, file=sys.stderr, **kargs)
......@@ -23,78 +25,91 @@ def get_serial():
devices.sort()
return devices[-1]
def main(argv):
eprint(argv[0])
script_dir = os.path.split(argv[0])[0]
if len(script_dir) > 0:
os.chdir(script_dir)
flash()
dev = get_serial()
ser = serial.Serial(dev, baudrate=115200, timeout=5)
class UARTP:
def __init__(self, ser):
UARTP.SYN = 0xf9
UARTP.FIN = 0xf3
self.ser = ser
def uart_read():
r = ser.read(1)
def uart_read(self):
r = self.ser.read(1)
if len(r) != 1:
raise Exception("Serial read error")
return r[0]
def uart_write(c):
def uart_write(self, c):
b = struct.pack("B", c)
r = ser.write(b)
r = self.ser.write(b)
if r != len(b):
raise Exception("Serial write error")
return r
def uart_recvframe():
def send(self, buf):
self.uart_write(UARTP.SYN)
len_ind_0 = 0xff & len(buf)
len_ind_1 = 0xff & (len(buf) >> 7)
if len(buf) < 128:
self.uart_write(len_ind_0)
else:
self.uart_write(len_ind_0 | 0x80)
self.uart_write(len_ind_1)
fcs = 0
for i in range(len(buf)):
info = buf[i]
fcs = (fcs + info) & 0xff
self.uart_write(buf[i])
fcs = (0xff - fcs) & 0xff
self.uart_write(fcs)
self.uart_write(UARTP.FIN)
eprint("sent frame '%s'" % buf.hex())
def recv(self):
tag_old = UARTP.FIN
while 1:
tag_old = 0
tag = 0xf3
while tag_old != 0xf3 or tag != 0xf9:
tag = tag_old
while 1:
if tag_old == UARTP.FIN:
if tag == UARTP.SYN:
break
tag_old = tag
tag = uart_read()
tag = self.uart_read()
tag_old = tag
l = uart_read()
l = self.uart_read()
if l & 0x80:
l &= 0x7f
l |= uart_read() << 7
l |= self.uart_read() << 7
fcs = 0
buf = []
for i in range(l):
info = uart_read()
info = self.uart_read()
buf.append(info)
fcs = (fcs + info) & 0xff
fcs = (fcs + uart_read()) & 0xff
fcs = (fcs + self.uart_read()) & 0xff
tag = uart_read()
tag = self.uart_read()
if fcs == 0xff:
if tag == 0xf3:
if tag == UARTP.FIN:
buf = bytes(buf)
eprint("rcvd frame '%s'" % buf.hex())
if len(buf) >= 1 and buf[0] == 0xde:
sys.stderr.buffer.write(buf[1:])
sys.stderr.flush()
else:
return buf
def uart_sendframe(buf):
uart_write(0xf9)
len_ind_0 = 0xff & len(buf)
len_ind_1 = 0xff & (len(buf) >> 7)
if len(buf) < 128:
uart_write(len_ind_0)
else:
uart_write(len_ind_0 | 0x80)
uart_write(len_ind_1)
fcs = 0
for i in range(len(buf)):
info = buf[i]
fcs = (fcs + info) & 0xff
uart_write(buf[i])
fcs = (0xff - fcs) & 0xff
uart_write(fcs)
uart_write(0xf3)
def main(argv):
eprint(argv[0])
script_dir = os.path.split(argv[0])[0]
if len(script_dir) > 0:
os.chdir(script_dir)
flash()
dev = get_serial()
ser = serial.Serial(dev, baudrate=115200, timeout=5)
uartp = UARTP(ser)
def stdin_read(n):
b = sys.stdin.buffer.read(n)
......@@ -123,16 +138,16 @@ def main(argv):
if action in b"ackmps":
v = stdin_readvar()
uart_sendframe(struct.pack("B", action) + v)
ack = uart_recvframe()
uartp.send(struct.pack("B", action) + v)
ack = uartp.recv()
if len(ack) != 1 or ack[0] != action:
raise Exception("Unacknowledged variable transfer")
eprint("Var %c successfully sent to board" % action)
elif action in b"ACKMPS":
c = struct.pack("B", action)
uart_sendframe(c)
v = uart_recvframe()
uartp.send(c)
v = uartp.recv()
if len(v) < 1 or v[0] != action:
raise Exception("Could not obtain variable from board")
v = v[1:]
......@@ -143,8 +158,8 @@ def main(argv):
elif action in b"ed":
c = struct.pack("B", action)
uart_sendframe(c)
ack = uart_recvframe()
uartp.send(c)
ack = uartp.recv()
if len(ack) != 1 or ack[0] != action:
raise Exception("Unacknowledged variable transfer")
eprint("Operation %c completed successfully" % action)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment