Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement zephyr urandom and monotime #19142

Merged
merged 2 commits into from
Nov 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions lib/std/monotimes.nim
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ when defined(js):
elif defined(posix) and not defined(osx):
import posix

when defined(zephyr):
proc k_uptime_ticks(): int64 {.importc: "k_uptime_ticks", header: "<kernel.h>".}
proc k_ticks_to_ns_floor64(ticks: int64): int64 {.importc: "k_ticks_to_ns_floor64", header: "<kernel.h>".}

elif defined(windows):
proc QueryPerformanceCounter(res: var uint64) {.
importc: "QueryPerformanceCounter", stdcall, dynlib: "kernel32".}
Expand All @@ -98,6 +102,9 @@ proc getMonoTime*(): MonoTime {.tags: [TimeEffect].} =
mach_timebase_info(machAbsoluteTimeFreq)
result = MonoTime(ticks: ticks * machAbsoluteTimeFreq.numer div
machAbsoluteTimeFreq.denom)
elif defined(zephyr):
let ticks = k_ticks_to_ns_floor64(k_uptime_ticks())
result = MonoTime(ticks: ticks)
elif defined(posix):
var ts: Timespec
discard clock_gettime(CLOCK_MONOTONIC, ts)
Expand Down
12 changes: 11 additions & 1 deletion lib/std/sysrand.nim
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ when defined(posix):
import posix

const
batchImplOS = defined(freebsd) or defined(openbsd) or (defined(macosx) and not defined(ios))
batchImplOS = defined(freebsd) or defined(openbsd) or defined(zephyr) or (defined(macosx) and not defined(ios))
batchSize {.used.} = 256

when batchImplOS:
Expand Down Expand Up @@ -207,6 +207,16 @@ elif defined(openbsd):
proc getRandomImpl(p: pointer, size: int): int {.inline.} =
result = getentropy(p, cint(size)).int

elif defined(zephyr):
proc sys_csrand_get(dst: pointer, length: csize_t): cint {.importc: "sys_csrand_get", header: "<random/rand32.h>".}
# Fill the destination buffer with cryptographically secure
# random data values
#

proc getRandomImpl(p: pointer, size: int): int {.inline.} =
# 0 if success, -EIO if entropy reseed error
result = sys_csrand_get(p, csize_t(size)).int

elif defined(freebsd):
type cssize_t {.importc: "ssize_t", header: "<sys/types.h>".} = int

Expand Down