Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
lwc
/
compare
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Pipelines
Members
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
01418953
authored
Jul 13, 2019
by
Enrico Pozzobon
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
arduino uno working bench
parent
864c7749
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
319 additions
and
28 deletions
+319
-28
templates/uno/configure
+2
-1
templates/uno/include/crypto_aead.h
+7
-0
templates/uno/src/main.ino
+171
-25
templates/uno/test
+138
-2
test.py
+1
-0
No files found.
templates/uno/configure
View file @
01418953
#!/bin/bash
#!/bin/bash
mv
*
.c
*
.h src/
mv
*
.c src/
mv
*
.h include/
templates/uno/
src
/crypto_aead.h
→
templates/uno/
include
/crypto_aead.h
View file @
01418953
#ifdef __cplusplus
extern
"C"
{
#endif
int
crypto_aead_encrypt
(
int
crypto_aead_encrypt
(
unsigned
char
*
c
,
unsigned
long
long
*
clen
,
unsigned
char
*
c
,
unsigned
long
long
*
clen
,
const
unsigned
char
*
m
,
unsigned
long
long
mlen
,
const
unsigned
char
*
m
,
unsigned
long
long
mlen
,
...
@@ -17,3 +21,6 @@ int crypto_aead_decrypt(
...
@@ -17,3 +21,6 @@ int crypto_aead_decrypt(
const
unsigned
char
*
k
const
unsigned
char
*
k
);
);
#ifdef __cplusplus
}
#endif
templates/uno/src/main.ino
View file @
01418953
#include "crypto_aead.h"
#include "crypto_aead.h"
#include "api.h"
#include "api.h"
/*
#define MAX_BYTES 100
Blink
Turns an LED on for one second, then off for one second, repeatedly.
#define DEBUG
Most Arduinos have an on-board LED you can control. On the UNO, MEGA and ZERO
uint8_t
npub
[
CRYPTO_NPUBBYTES
];
it is attached to digital pin 13, on MKR1000 on pin 6. LED_BUILTIN is set to
uint8_t
nsec
[
CRYPTO_NSECBYTES
];
the correct LED pin independent of which board is used.
uint8_t
k
[
CRYPTO_KEYBYTES
];
If you want to know what pin the on-board LED is connected to on your Arduino
uint8_t
ad
[
MAX_BYTES
];
model, check the Technical Specs of your board at:
unsigned
long
long
int
adlen
;
https://www.arduino.cc/en/Main/Products
uint8_t
m
[
MAX_BYTES
];
unsigned
long
long
int
mlen
;
uint8_t
c
[
MAX_BYTES
];
unsigned
long
long
int
clen
;
modified 8 May 2014
void
uart_write
(
uint8_t
x
)
{
by Scott Fitzgerald
Serial
.
write
((
uint8_t
)
(
x
));
modified 2 Sep 2016
}
by Arturo Guadalupi
modified 8 Sep 2016
uint8_t
uart_read
()
{
by Colby Newman
int
r
;
do
{
r
=
Serial
.
read
();
}
while
(
r
==
-
1
);
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
;
This example code is in the public domain.
tag
=
AMUX_END
;
while
(
1
)
{
http://www.arduino.cc/en/Tutorial/Blink
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 DEBUG
#define DEBUG_BUF_LEN 80
int
dbg_printf
(
const
char
*
format
,
...)
{
char
printbuf
[
DEBUG_BUF_LEN
+
2
];
va_list
vargs
;
va_start
(
vargs
,
format
);
int
r
=
vsnprintf
(
printbuf
+
1
,
DEBUG_BUF_LEN
,
format
,
vargs
);
va_end
(
vargs
);
if
(
r
<
0
)
{
memcpy
(
printbuf
+
1
,
"DEBUG ERROR
\r\n
"
,
13
);
r
=
13
;
}
r
=
r
>
DEBUG_BUF_LEN
?
DEBUG_BUF_LEN
:
r
;
printbuf
[
0
]
=
0xde
;
// Debug messages should start with "\xde"
uartp_send
(
printbuf
,
r
+
1
);
return
r
;
}
#else
#define dbg_printf(...) (0)
#endif
void
assert
(
bool
b
)
{
if
(
b
)
return
;
dbg_printf
(
"Assertion failed
\r\n
"
);
for
(;;)
yield
();
}
// the setup function runs once when you press reset or power the board
void
setup
()
{
void
setup
()
{
// initialize digital pin LED_BUILTIN as an output.
Serial
.
begin
(
115200
);
pinMode
(
LED_BUILTIN
,
OUTPUT
);
pinMode
(
LED_BUILTIN
,
OUTPUT
);
digitalWrite
(
LED_BUILTIN
,
HIGH
);
delay
(
100
);
Serial
.
println
(
"Hello, World!"
);
}
}
// the loop function runs over and over again forever
void
loop
()
{
void
loop
()
{
digitalWrite
(
LED_BUILTIN
,
HIGH
);
// turn the LED on (HIGH is the voltage level)
static
uint8_t
buf
[
256
];
delay
(
1000
);
// wait for a second
int
res
;
digitalWrite
(
LED_BUILTIN
,
LOW
);
// turn the LED off by making the voltage LOW
uint16_t
len
=
uartp_recv
(
buf
,
255
);
delay
(
1000
);
// wait for a second
uint8_t
action
=
buf
[
0
];
if
(
len
==
0
||
len
>
255
)
return
;
dbg_printf
(
"Received command %c
\r\n
"
,
action
);
uint16_t
l
=
len
-
1
;
uint8_t
*
var
=
buf
+
1
;
switch
(
action
)
{
case
'm'
:
assert
(
l
<=
MAX_BYTES
);
memcpy
(
m
,
var
,
l
);
mlen
=
l
;
break
;
case
'c'
:
assert
(
l
<=
MAX_BYTES
);
memcpy
(
c
,
var
,
l
);
clen
=
l
;
break
;
case
'a'
:
assert
(
l
<=
MAX_BYTES
);
memcpy
(
ad
,
var
,
l
);
adlen
=
l
;
break
;
case
'k'
:
assert
(
l
==
CRYPTO_KEYBYTES
);
memcpy
(
k
,
var
,
l
);
break
;
case
'p'
:
assert
(
l
==
CRYPTO_NPUBBYTES
);
memcpy
(
npub
,
var
,
l
);
break
;
case
's'
:
assert
(
l
==
CRYPTO_NSECBYTES
);
memcpy
(
nsec
,
var
,
l
);
break
;
case
'e'
:
digitalWrite
(
LED_BUILTIN
,
LOW
);
res
=
crypto_aead_encrypt
(
c
,
&
clen
,
m
,
mlen
,
ad
,
adlen
,
nsec
,
npub
,
k
);
digitalWrite
(
LED_BUILTIN
,
HIGH
);
break
;
case
'd'
:
digitalWrite
(
LED_BUILTIN
,
LOW
);
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
;
}
buf
[
0
]
=
action
;
memcpy
(
buf
+
1
,
var
,
l
);
uartp_send
(
buf
,
l
+
1
);
}
}
templates/uno/test
View file @
01418953
...
@@ -2,8 +2,13 @@
...
@@ -2,8 +2,13 @@
import
os
import
os
import
sys
import
sys
import
struct
import
serial
import
subprocess
import
subprocess
def
eprint
(
*
args
,
**
kargs
):
print
(
*
args
,
file
=
sys
.
stderr
,
**
kargs
)
def
flash
():
def
flash
():
pipe
=
subprocess
.
PIPE
pipe
=
subprocess
.
PIPE
...
@@ -11,13 +16,144 @@ def flash():
...
@@ -11,13 +16,144 @@ def flash():
stdout
=
sys
.
stderr
,
stdin
=
pipe
)
stdout
=
sys
.
stderr
,
stdin
=
pipe
)
stdout
,
stderr
=
p
.
communicate
(
""
)
stdout
,
stderr
=
p
.
communicate
(
""
)
def
get_serial
():
import
serial.tools.list_ports
ports
=
serial
.
tools
.
list_ports
.
comports
()
devices
=
[
p
.
device
for
p
in
ports
]
devices
.
sort
()
return
devices
[
-
1
]
def
main
(
argv
):
def
main
(
argv
):
print
(
argv
[
0
],
file
=
sys
.
stderr
)
eprint
(
argv
[
0
]
)
script_dir
=
os
.
path
.
split
(
argv
[
0
])[
0
]
script_dir
=
os
.
path
.
split
(
argv
[
0
])[
0
]
if
len
(
script_dir
)
>
0
:
if
len
(
script_dir
)
>
0
:
os
.
chdir
(
script_dir
)
os
.
chdir
(
script_dir
)
flash
()
flash
()
return
0
;
dev
=
get_serial
()
ser
=
serial
.
Serial
(
dev
,
baudrate
=
115200
,
timeout
=
5
)
def
uart_read
():
r
=
ser
.
read
(
1
)
if
len
(
r
)
!=
1
:
raise
Exception
(
"Serial read error"
)
return
r
[
0
]
def
uart_write
(
c
):
b
=
struct
.
pack
(
"B"
,
c
)
r
=
ser
.
write
(
b
)
if
r
!=
len
(
b
):
raise
Exception
(
"Serial write error"
)
return
r
def
uart_recvframe
():
while
1
:
tag_old
=
0
tag
=
0xf3
while
tag_old
!=
0xf3
or
tag
!=
0xf9
:
tag_old
=
tag
tag
=
uart_read
()
tag_old
=
tag
l
=
uart_read
()
if
l
&
0x80
:
l
&=
0x7f
l
|=
uart_read
()
<<
7
fcs
=
0
buf
=
[]
for
i
in
range
(
l
):
info
=
uart_read
()
buf
.
append
(
info
)
fcs
=
(
fcs
+
info
)
&
0xff
fcs
=
(
fcs
+
uart_read
())
&
0xff
tag
=
uart_read
()
if
fcs
==
0xff
:
if
tag
==
0xf3
:
buf
=
bytes
(
buf
)
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
stdin_read
(
n
):
b
=
sys
.
stdin
.
buffer
.
read
(
n
)
if
len
(
b
)
!=
n
:
sys
.
exit
(
1
)
return
b
def
stdin_readvar
():
l
=
stdin_read
(
4
)
(
l
,
)
=
struct
.
unpack
(
"<I"
,
l
)
v
=
stdin_read
(
l
)
return
v
exp_hello
=
b
"Hello, World!"
hello
=
ser
.
read
(
len
(
exp_hello
))
if
hello
!=
exp_hello
:
eprint
(
"Improper board initialization message: "
)
return
1
eprint
(
"Board initialized properly"
)
sys
.
stdout
.
write
(
"Hello, World!
\n
"
)
sys
.
stdout
.
flush
()
while
1
:
action
=
stdin_read
(
1
)[
0
]
eprint
(
"Command
%
c from stdin"
%
action
)
if
action
in
b
"ackmps"
:
v
=
stdin_readvar
()
uart_sendframe
(
struct
.
pack
(
"B"
,
action
)
+
v
)
ack
=
uart_recvframe
()
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
()
if
len
(
v
)
<
1
or
v
[
0
]
!=
action
:
raise
Exception
(
"Could not obtain variable from board"
)
v
=
v
[
1
:]
eprint
(
"Var
%
c received from board:
%
s"
%
(
action
,
v
.
hex
()))
l
=
struct
.
pack
(
"<I"
,
len
(
v
))
sys
.
stdout
.
buffer
.
write
(
l
+
v
)
sys
.
stdout
.
flush
()
elif
action
in
b
"ed"
:
c
=
struct
.
pack
(
"B"
,
action
)
uart_sendframe
(
c
)
ack
=
uart_recvframe
()
if
len
(
ack
)
!=
1
or
ack
[
0
]
!=
action
:
raise
Exception
(
"Unacknowledged variable transfer"
)
eprint
(
"Operation
%
c completed successfully"
%
action
)
else
:
raise
Exception
(
"Unknown action
%
c"
%
action
)
return
0
if
__name__
==
"__main__"
:
if
__name__
==
"__main__"
:
...
...
test.py
View file @
01418953
...
@@ -42,6 +42,7 @@ def main(argv):
...
@@ -42,6 +42,7 @@ def main(argv):
if
read
(
14
)
!=
b
"Hello, World!
\n
"
:
if
read
(
14
)
!=
b
"Hello, World!
\n
"
:
raise
Exception
(
"Unexpected output"
)
raise
Exception
(
"Unexpected output"
)
print
(
"Ready"
)
m
=
b
""
m
=
b
""
ad
=
b
""
ad
=
b
""
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment