Skip to content

Commit

Permalink
bug fix! oops on the ipv4 address conversion OC
Browse files Browse the repository at this point in the history
  • Loading branch information
elcritch committed Oct 10, 2020
1 parent 69637c5 commit 80ac050
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 10 deletions.
3 changes: 3 additions & 0 deletions esp-idf-examples/simplewifi/main/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ idf_component_register(SRC_DIRS "./nimcache"
# set(C_COMPILE_OPTIONS "${C_COMPILE_OPTIONS} -Wno-error=unused-label")
# list(APPEND C_COMPILE_OPTIONS "-Wno-error=unused-label ")

idf_build_set_property(C_COMPILE_OPTIONS -Wno-unused-label APPEND)
idf_build_set_property(C_COMPILE_OPTIONS -Wno-discarded-qualifiers APPEND)

idf_build_set_property(C_COMPILE_OPTIONS -Wno-error=unused-label APPEND)
idf_build_set_property(C_COMPILE_OPTIONS -Wno-error=parentheses APPEND)
idf_build_set_property(C_COMPILE_OPTIONS -Wno-error=implicit-function-declaration APPEND)
Expand Down
17 changes: 9 additions & 8 deletions esp-idf-examples/simplewifi/main/wifi_example_main.nim
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ const WIFI_PASSWORD {.strdefine.}: string = ""
# const CONFIG_EXAMPLE_WIFI_PASSWORD = getEnv("WIFI_PASSWORD")

const
GOT_IPV4_BIT* = EventBits_t(BIT(0))
GOT_IPV4_BIT* = EventBits_t(BIT(1))
CONNECTED_BITS* = (GOT_IPV4_BIT)

const TAG*: cstring = "example"
const TAG*: cstring = "simplewifi"
var sConnectEventGroup*: EventGroupHandle_t
var sIpAddr*: IpAddress
var sConnectionName*: cstring
Expand All @@ -30,9 +30,11 @@ proc ipReceivedHandler*(arg: pointer; event_base: esp_event_base_t; event_id: in
event_data: pointer) {.cdecl.} =
var event: ptr ip_event_got_ip_t = cast[ptr ip_event_got_ip_t](event_data)

# echo "event.ip_info.ip: " & repr(event.ip_info.ip)
logi TAG, "event.ip_info.ip: %s", $(event.ip_info.ip)

sIpAddr = toIpAddress(event.ip_info.ip)
# memcpy(addr(sIpAddr), addr(event.ip_info.ip), sizeof((sIpAddr)))
logw TAG, "got event ip: %s", $sIpAddr
discard xEventGroupSetBits(sConnectEventGroup, GOT_IPV4_BIT)

proc onWifiDisconnect*(arg: pointer;
Expand Down Expand Up @@ -96,7 +98,6 @@ proc exampleDisconnect*(): esp_err_t =

vEventGroupDelete(sConnectEventGroup)
sConnectEventGroup = nil

wifiStop()
logi(TAG, "Disconnected from %s", sConnectionName)
sConnectionName = nil
Expand All @@ -108,14 +109,14 @@ proc nim_app_main*() {.exportc.} =
tcpip_adapter_init()

check: esp_event_loop_create_default()

logi(TAG, "wifi setup!\n")
check: exampleConnect()

## Register event handlers to stop the server when Wi-Fi or Ethernet is disconnected,
## and re-start it upon connection.
##
IP_EVENT_STA_GOT_IP.eventRegister(ipReceivedHandler, nil)
WIFI_EVENT_STA_DISCONNECTED.eventRegister(onWifiDisconnect,nil)
# IP_EVENT_STA_GOT_IP.eventRegister(ipReceivedHandler, nil)

echo("wifi setup!\n")
echo("Wait for wifi\n")
echo("Wait done\n")
# vTaskDelay(10000 div portTICK_PERIOD_MS)
1 change: 1 addition & 0 deletions src/nesper/general.nim
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ var portTICK_PERIOD_MS* {.importc: "portTICK_PERIOD_MS", header: "<freertos/Free
proc NimMain() {.importc.}

when defined(NimAppMain):

proc nim_app_main*() {.importc.}

proc app_main*() {.exportc.} =
Expand Down
5 changes: 3 additions & 2 deletions src/nesper/net_utils.nim
Original file line number Diff line number Diff line change
Expand Up @@ -53,14 +53,15 @@ proc toIpAddress*(ip: ip4_addr_t): IpAddress =
result = IpAddress(family: IpAddressFamily.IPv4)
# copyMem(addr result.address_v4[0], unsafeAddr cast[uint32](ip.address), 4)

let address = ip.address
for i in 0..3:
result.address_v6[i] = uint8(ip.address shr (i*8))
result.address_v4[i] = uint8(address shr (i*8))

proc toIpAddress*(ip: ip6_addr_t): IpAddress =
result = IpAddress(family: IpAddressFamily.IPv6)
for i in 0..3:
# result.address_v6[i] = address[i]
for i in 0..3:
result.address_v4[i] = uint8(ip.address[i] shr (i*8))
result.address_v6[i] = uint8(ip.address[i] shr (i*8))
# for i in 0..15:
# result.address_v6[i] = uint8(address shr (i*8))

0 comments on commit 80ac050

Please sign in to comment.