Skip to content

Commit

Permalink
Merge pull request #18 from atsign-foundation/cpswan-refactor
Browse files Browse the repository at this point in the history
feat: Refactor to use a new atClient class
  • Loading branch information
cpswan authored Jun 20, 2023
2 parents 88954bf + 45fcd88 commit 8cc3bc2
Show file tree
Hide file tree
Showing 35 changed files with 847 additions and 33 deletions.
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
project.pico-go
__pycache__
__pycache__
built
settings.json
version.py
13 changes: 9 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,23 @@ Communicate with the atPlatform using a Raspberry Pi Pico W.
Developed with [MicroPython](https://micropython.org/).

## Usage

### Raspberry Pi Pico W
- Install the latest `firmware.uf2` onto your Pico W from [atsign-foundation/micropython Releases](https://github.com/atsign-foundation/micropython/releases), as this is patched to enable AES CTR, which is used by atSigns.
- Fill all the fields of the `settings.json` file (ssid/passw of your Wi-Fi network and atSign).
- Download [Thonny IDE](https://thonny.org/) and place all the files of this repository in the Pico W file system.
- Place your `.atKeys` file in the `~/keys/` directory (if the folder doesn't exist, create it manually)
- Run `python3 build.py` to generate .mpy files
- Download [Thonny IDE](https://thonny.org/) and place all the files of the built directory onto the Pico W file system.
- Place your `.atKeys` file in the `~/.atsign/keys/` directory (if the folder doesn't exist, create it manually)
- Run `main.py` and select option `3` in the REPL ("Get privateKey for @[yourAtSign]")
- Re-launch the REPL (run `main.py` again)
- Now you can select option `2` in the REPL to automatically get authenticated in your DESS
- Enjoy! :)

(If you get an error when attempting to find the secondary or when trying to connect to it, run again the REPL)
(If you get an error when attempting to find the atServer or when trying to connect to it, run the REPL again)

(You can uncomment a few commented lines in the `send_verb` method to see the llookup verb answer content decrypted. Warning: some stored keys are not encrypted, if you try to see the decrypted content of one of those stored keys, you will get an error. This exception will be handled in the future)
### Micropython on Linux
- Compile micropython with coverage enabled (to get AES CTR) and place the binary somewhere on the PATH (e.g. ~/.local/bin/)
- `micropython main.py`

## Libraries
- [iot-core-micropython](https://github.com/GoogleCloudPlatform/iot-core-micropython)
Expand Down
125 changes: 125 additions & 0 deletions build.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
import os
import shutil
import subprocess
import sys
import shutil
import time

def runCross(src, dst, fname) -> (bool, str):
print("mpy-cross : "+fname)
sp = subprocess.run(["mpy-cross", "-o", dst, "-s", fname, src, "-march=armv6m"], capture_output=True, text=True)
return (sp.returncode==0, sp.stdout)

def isNewer(f1, f2):
if os.path.exists(f2):
return (os.path.getmtime(f1) > os.path.getmtime(f2))
return True

def parseArgs(aa, minArgs:int) -> dict:
ret = {}
ai = 0
curarg = None
for a in aa:
if a.startswith('-'):
if curarg is not None:
ret[curarg] = True
curarg = a
else:
if curarg is not None:
ret[curarg] = a
curarg=None
else:
ai+=1
ret[str(ai)] = a
if curarg is not None:
ret[curarg] = True
if ai < minArgs:
raise Exception("Require at least "+minArgs+" arguments")
return ret

def getEnvOrDef(vn, d) -> str:
return os.environ.get(vn, d)

def make_build_version(srcd) -> None:
vd = "major = '{}'\nminor='{}'\nbuild='{}'\ndate='{}'\n".format(
getEnvOrDef('RELEASE_MAJOR', "0"),
getEnvOrDef('RELEASE_MINOR', "0"),
getEnvOrDef('CI_COMMIT_SHORT_SHA', getEnvOrDef('USER', getEnvOrDef('USERNAME', 'anon')+'@'+time.strftime("%y-%m-%d %H:%M", time.localtime()))),
getEnvOrDef('CI_JOB_STARTED_AT', time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()))
)
with open(srcd+"/version.py", "w") as fh:
fh.write(vd)
print("Wrote version info: {}".format(vd))

def dobuild(srcd, dstd, faillogf, clean) -> bool:
if clean:
if os.path.exists(dstd):
shutil.rmtree(dstd)

if os.path.exists(dstd) is False:
os.mkdir(dstd)

# put build dates etc into version.py before builing
make_build_version(srcd)

failedPy = []
for root, dirs, files in os.walk(srcd):
ddir = dstd+root[len(srcd):]
# ddir = os.path.join(dstd, root[len(srcd):])
for d in dirs:
pd = os.path.join(ddir, d)
if os.path.exists(pd) is False:
print("mkdir "+pd)
os.mkdir(pd)
for f in files:
inf = os.path.join(root,f)
# if a python file, cross-compile UNLESS its boot.py/main.py as these MUST be straight python files for pycom boot process
if f.endswith('.py') and f!="main.py" and f!="boot.py":
outf = os.path.join(ddir,f[:-3]+".mpy")
if isNewer(inf, outf):
print("mpy-cross -o "+outf+" "+inf)
res, out = runCross(inf, outf, f)
if not res:
print(inf+" failed python cross compilier.")
failedPy.append(inf)
failedPy.append(out)
else:
print(inf+" unchanged")
else:
outf = os.path.join(ddir,f)
if isNewer(inf, outf):
print("cp "+inf+" "+outf)
shutil.copyfile(inf, outf)
else:
print(inf+" unchanged")

if len(failedPy)==0:
print("Project built successfully")
return True
else:
print("Project has {} python cross-compile failures:".format(len(failedPy)))
if faillogf is not None:
with open(faillogf, "w") as fh:
for f in failedPy:
fh.write("{}\n".format(f))
print("Fix and rebuild...logs in {}".format(faillogf))
else:
for f in failedPy:
print(f)
print("Fix and rebuild...")
return False

if __name__ == '__main__':
# start script
argd = parseArgs(sys.argv, 0)
SRC=argd.get("-s", "src")
DST=argd.get("-d", "built")
OUTF = argd.get("-o", None)

# check if clean run required
CLEAN = argd.get("-clean", False)!=False

if dobuild(SRC, DST, OUTF, CLEAN):
sys.exit(0)
else:
sys.exit(1)
6 changes: 0 additions & 6 deletions settings.json

This file was deleted.

File renamed without changes.
Loading

0 comments on commit 8cc3bc2

Please sign in to comment.