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