From 6c49767a6c656fb097df71c41e4f123380bc1ca9 Mon Sep 17 00:00:00 2001 From: Jaremy Creechley Date: Mon, 14 Sep 2020 00:57:49 -0600 Subject: [PATCH] Fixing issue #15302 -- lwip doesn't support signals (#15303) * Fixing issue #15302 -- lwip doesn't support signals * Adding test to catch issue #15302 -- lwip/freertos net library don't try to build / run on windows, it'll compile only but not run Fixing issue #15302 -- reworking test to compile on other platforms --- compiler/platform.nim | 2 +- lib/posix/posix_freertos_consts.nim | 36 +++++++++++++++++++---------- lib/posix/posix_other.nim | 4 ++++ lib/pure/net.nim | 4 ++-- tests/stdlib/tlwip.nim | 30 ++++++++++++++++++++++++ 5 files changed, 61 insertions(+), 15 deletions(-) create mode 100644 tests/stdlib/tlwip.nim diff --git a/compiler/platform.nim b/compiler/platform.nim index df79b2f8dc..78c523c1a6 100644 --- a/compiler/platform.nim +++ b/compiler/platform.nim @@ -179,7 +179,7 @@ const props: {ospNeedsPIC, ospPosix}), (name: "FreeRTOS", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", - scriptExt: ".sh", curDir: ".", exeExt: ".elf", extSep: ".", + scriptExt: ".sh", curDir: ".", exeExt: "", extSep: ".", props: {ospPosix}), (name: "Any", parDir: "..", dllFrmt: "lib$1.so", altDirSep: "/", objExt: ".o", newLine: "\x0A", pathSep: ":", dirSep: "/", diff --git a/lib/posix/posix_freertos_consts.nim b/lib/posix/posix_freertos_consts.nim index 5e83fa0f5e..efe87aacfc 100644 --- a/lib/posix/posix_freertos_consts.nim +++ b/lib/posix/posix_freertos_consts.nim @@ -220,11 +220,11 @@ var IPV6_UNICAST_HOPS* {.importc: "IPV6_UNICAST_HOPS", header: "". var IPV6_V6ONLY* {.importc: "IPV6_V6ONLY", header: "".}: cint # -var TCP_NODELAY* {.importc: "TCP_NODELAY", header: "".}: cint -var TCP_KEEPALIVE* {.importc: "TCP_KEEPALIVE", header: "".}: cint -var TCP_KEEPIDLE* {.importc: "TCP_KEEPIDLE", header: "".}: cint -var TCP_KEEPINTVL* {.importc: "TCP_KEEPINTVL", header: "".}: cint -var TCP_KEEPCNT* {.importc: "TCP_KEEPCNT", header: "".}: cint +const TCP_NODELAY* = 0x01 # don't delay send to coalesce packets +const TCP_KEEPALIVE* = 0x02 # send KEEPALIVE probes when idle for pcb->keep_idle milliseconds +const TCP_KEEPIDLE* = 0x03 # set pcb->keep_idle - Same as TCP_KEEPALIVE, but use seconds for get/setsockopt +const TCP_KEEPINTVL* = 0x04 # set pcb->keep_intvl - Use seconds for get/setsockopt +const TCP_KEEPCNT* = 0x05 # set pcb->keep_cnt - Use number of probes sent for get/setsockopt # # var NL_SETD* {.importc: "NL_SETD", header: "".}: cint @@ -367,10 +367,25 @@ var SEM_FAILED* {.importc: "SEM_FAILED", header: "".}: pointer var FD_SETSIZE* {.importc: "FD_SETSIZE", header: "".}: cint # -var MSG_CTRUNC* {.importc: "MSG_CTRUNC", header: "".}: cint -var MSG_DONTROUTE* {.importc: "MSG_DONTROUTE", header: "".}: cint -var MSG_EOR* {.importc: "MSG_EOR", header: "".}: cint -var MSG_OOB* {.importc: "MSG_OOB", header: "".}: cint +# struct msghdr->msg_flags bit field values +const MSG_TRUNC* = 0x04 +const MSG_CTRUNC* = 0x08 + +# Flags we can use with send and recv. +const MSG_PEEK* = 0x01 # Peeks at an incoming message +const MSG_WAITALL* = 0x02 # Unimplemented: Requests that the function block until the full amount of data requested can be returned +const MSG_OOB* = 0x04 # Unimplemented: Requests out-of-band data. The significance and semantics of out-of-band data are protocol-specific +const MSG_DONTWAIT* = 0x08 # Nonblocking i/o for this operation only +const MSG_MORE* = 0x10 # Sender will send more +# const MSG_NOSIGNAL* = 0x20 # Uninmplemented: Requests not to send the SIGPIPE signal if an attempt to send is made on a stream-oriented socket that is no longer connected. + +# Alternately, they can defined like this, but the above seems better when they're known/stable values: +# var MSG_TRUNC* {.importc: "MSG_TRUNC", header: "".}: cint +# var MSG_CTRUNC* {.importc: "MSG_CTRUNC", header: "".}: cint +# var MSG_DONTROUTE* {.importc: "MSG_DONTROUTE", header: "".}: cint # not defined in lwip +# var MSG_EOR* {.importc: "MSG_EOR", header: "".}: cint # not defined in lwip +# var MSG_OOB* {.importc: "MSG_OOB", header: "".}: cint + # var SCM_RIGHTS* {.importc: "SCM_RIGHTS", header: "".}: cint var SO_ACCEPTCONN* {.importc: "SO_ACCEPTCONN", header: "".}: cint @@ -401,9 +416,6 @@ var SOL_SOCKET* {.importc: "SOL_SOCKET", header: "".}: cint const SocketMaxConnections {.intdefine.}: int = 32 var SOMAXCONN*: cint = SocketMaxConnections.cint -var MSG_PEEK* {.importc: "MSG_PEEK", header: "".}: cint -var MSG_TRUNC* {.importc: "MSG_TRUNC", header: "".}: cint -var MSG_WAITALL* {.importc: "MSG_WAITALL", header: "".}: cint var AF_INET* {.importc: "AF_INET", header: "".}: cint var AF_INET6* {.importc: "AF_INET6", header: "".}: cint # var AF_UNIX* {.importc: "AF_UNIX", header: "".}: cint diff --git a/lib/posix/posix_other.nim b/lib/posix/posix_other.nim index 1ad05060d4..a5eb32d226 100644 --- a/lib/posix/posix_other.nim +++ b/lib/posix/posix_other.nim @@ -625,6 +625,10 @@ elif defined(solaris): # Solaris doesn't have MSG_NOSIGNAL const MSG_NOSIGNAL* = 0'i32 +elif defined(freertos) or defined(lwip): + # LwIP/FreeRTOS doesn't have MSG_NOSIGNAL + const + MSG_NOSIGNAL* = 0x20'i32 else: var MSG_NOSIGNAL* {.importc, header: "".}: cint diff --git a/lib/pure/net.nim b/lib/pure/net.nim index 5d2ec8e302..f30b893a76 100644 --- a/lib/pure/net.nim +++ b/lib/pure/net.nim @@ -1061,7 +1061,7 @@ proc accept*(server: Socket, client: var owned(Socket), var addrDummy = "" acceptAddr(server, client, addrDummy, flags) -when defined(posix): +when defined(posix) and not defined(lwip): from posix import Sigset, sigwait, sigismember, sigemptyset, sigaddset, sigprocmask, pthread_sigmask, SIGPIPE, SIG_BLOCK, SIG_UNBLOCK @@ -1079,7 +1079,7 @@ template blockSigpipe(body: untyped): untyped = ## ## For convenience, this template is also available for non-POSIX system, ## where `body` will be executed as-is. - when not defined(posix): + when not defined(posix) or defined(lwip): body else: template sigmask(how: cint, set, oset: var Sigset): untyped {.gensym.} = diff --git a/tests/stdlib/tlwip.nim b/tests/stdlib/tlwip.nim new file mode 100644 index 0000000000..7f78222368 --- /dev/null +++ b/tests/stdlib/tlwip.nim @@ -0,0 +1,30 @@ +discard """ + targets: "c" + cmd: "nim $target --os:freertos --gc:arc $options $file" + disabled: "bsd" + disabled: "windows" + action: compile +""" + +# Note: +# This file tests FreeRTOS/LwIP cross-compilation on UNIX platforms +# Windows should run when compiled with esp-idf, however I'm not +# sure how to test for only compilation on Windows without running +# a test exe +# +# Note: +# disabling *BSDs since they're not playing well with `gcc` + +import net +import asynchttpserver, asyncdispatch + +proc cb*(req: Request) {.async.} = + await req.respond(Http200, "Hello World") + +proc run_http_server*() {.exportc.} = + echo "starting http server" + var server = newAsyncHttpServer() + + waitFor server.serve(Port(8181), cb) + +echo("ok")