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

WiFi event monitor: Upgrade to SDK event monitor #870

Closed
dnc40085 opened this issue Dec 22, 2015 · 10 comments
Closed

WiFi event monitor: Upgrade to SDK event monitor #870

dnc40085 opened this issue Dec 22, 2015 · 10 comments

Comments

@dnc40085
Copy link
Contributor

I'm looking to get some input on the API (and corresponding code) I have written to expose the event monitor added with esp_iot_sdk_v1.0.1_15_04_24 before I submit a pull request.

Here's a link to the branch: dev_new_event_monitor

Currently, wifi.eventmon.start() needs to be executed once by the user to register the C callback with the SDK. I would like to have it so it automatically registers the CB on boot but I'm not exactly sure how to do that.

@TerryE pertaining to issue #846, I'm still learning about the lua internals(and programming in general), am I correct in the assumption that you are suggesting that gL=L should be replaced with gL=lua_getstate()?

wifi.eventmon sub-module

CONSTANTS

wifi.eventmon.STA_CONNECTED, wifi.eventmon.STA_DISCONNECTED, wifi.eventmon.STA_AUTHMODE_CHANGE, wifi.eventmon.STA_GOT_IP, wifi.eventmon.STA_DHCP_TIMEOUT, wifi.eventmon.AP_STACONNECTED, wifi.eventmon.AP_STADISCONNECTED, wifi.eventmon.AP_PROBEREQRECVED

DISCONNECT REASON CONSTANTS

wifi.eventmon.reason.UNSPECIFIED, wifi.eventmon.reason.AUTH_EXPIRE, wifi.eventmon.reason.AUTH_LEAVE, wifi.eventmon.reason.ASSOC_EXPIRE, wifi.eventmon.reason.ASSOC_TOOMANY, wifi.eventmon.reason.NOT_AUTHED, wifi.eventmon.reason.NOT_ASSOCED, wifi.eventmon.reason.ASSOC_LEAVE, wifi.eventmon.reason.ASSOC_NOT_AUTHED, wifi.eventmon.reason.DISASSOC_PWRCAP_BAD, wifi.eventmon.reason.DISASSOC_SUPCHAN_BAD, wifi.eventmon.reason.IE_INVALID, wifi.eventmon.reason.MIC_FAILURE, wifi.eventmon.reason.4WAY_HANDSHAKE_TIMEOUT, wifi.eventmon.reason.GROUP_KEY_UPDATE_TIMEOUT, wifi.eventmon.reason.IE_IN_4WAY_DIFFERS, wifi.eventmon.reason.GROUP_CIPHER_INVALID, wifi.eventmon.reason.PAIRWISE_CIPHER_INVALID, wifi.eventmon.reason.AKMP_INVALID, wifi.eventmon.reason.UNSUPP_RSN_IE_VERSION, wifi.eventmon.reason.INVALID_RSN_IE_CAP, wifi.eventmon.reason.802_1X_AUTH_FAILED, wifi.eventmon.reason.CIPHER_SUITE_REJECTED, wifi.eventmon.reason.BEACON_TIMEOUT, wifi.eventmon.reason.NO_AP_FOUND, wifi.eventmon.reason.AUTH_FAIL, wifi.eventmon.reason.ASSOC_FAIL, wifi.eventmon.reason.HANDSHAKE_TIMEOUT

wifi.eventmon.register()

Description

Register/unregister callbacks for WiFi event monitor

Syntax

wifi.eventmon.register(Event, function(T))
wifi.eventmon.register(Event, nil)

Parameters

Event: WiFi event you would like to set a callback for

  • Valid WiFi events:
    • wifi.eventmon.STA_CONNECTED
    • wifi.eventmon.STA_DISCONNECTED
    • wifi.eventmon.STA_AUTHMODE_CHANGE
    • wifi.eventmon.STA_GOT_IP
    • wifi.eventmon.STA_DHCP_TIMEOUT
    • wifi.eventmon.AP_STACONNECTED
    • wifi.eventmon.AP_STADISCONNECTED
    • wifi.eventmon.AP_PROBEREQRECVED

Returns

T: Table returned by event.

  • wifi.eventmon.STA_CONNECTED: Station is connected to access point.
    • SSID: SSID of access point.
    • BSSID: BSSID of access point.
    • channel: The channel the access point is on.
  • wifi.eventmon.STA_DISCONNECT: Station was disconnected from access point.
    • SSID: SSID of access point
    • BSSID: BSSID of access point.
    • REASON: See "DISCONNECT REASON CONSTANTS" above
  • wifi.eventmon.STA_AUTHMODE_CHANGE: Access point has changed authorization mode.
    • new_auth_mode
    • old_auth_mode
  • wifi.eventmon.STA_GOT_IP: Station got an IP address.
    • IP: The IP address assigned to the station.
    • netmask: Subnet mask.
    • gateway: The IP address of the access point the station is connected to.
  • wifi.eventmon.STA_DHCP_TIMEOUT: Station DHCP request has timed out.
    • T==nil
  • wifi.eventmon.AP_STACONNECTED: A new client has connected to the access point.
    • MAC: MAC address of client that has connected.
    • AID: SDK has no details concerning this return value.
  • wifi.eventmon.AP_STADISCONNECTED: A client has disconnected fro the access point.
    • MAC: MAC address of client that has disconnected.
    • AID: SDK has no details concerning this return value.
  • wifi.eventmon.AP_PROBEREQRECVED: A probe request was received.
    • MAC: MAC address of the client that is probing the access point.
    • RSSI: Received Signal Strength Indicator of client

Example

 wifi.eventmon.register(wifi.eventmon.STA_CONNECTED, function(T) 
 print("\n\tSTA - CONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
 T.BSSID.."\n\tChannel: "..T.channel)
 end)

 wifi.eventmon.register(wifi.eventmon.STA_DISCONNECTED, function(T) 
 print("\n\tSTA - DISCONNECTED".."\n\tSSID: "..T.SSID.."\n\tBSSID: "..
 T.BSSID.."\n\treason: "..T.reason)
 end)

 wifi.eventmon.register(wifi.eventmon.STA_AUTHMODE_CHANGE, Function(T) 
 print("\n\tSTA - AUTHMODE CHANGE".."\n\told_auth_mode: "..
 T.old_auth_mode.."\n\tnew_auth_mode: "..T.new_auth_mode) 
 end)

 wifi.eventmon.register(wifi.eventmon.stasgottipa_got_ip, function(T) 
 print("\n\tSTA - GOT IP".."\n\tStation IP: "..T.IP.."\n\tSubnet mask: "..
 T.netmask.."\n\tGateway IP: "..T.gateway)
 end)

 wifi.eventmon.register(wifi.eventmon.STA_DHCP_TIMEOUT, function() 
 print("\n\tSTA - DHCP TIMEOUT")
 end)

 wifi.eventmon.register(wifi.eventmon.AP_STACONNECTED, function(T) 
 print("\n\tAP - STATION CONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID)
 end)

wifi.eventmon.register(wifi.eventmon.AP_STADISCONNECTED, function(T) 
 print("\n\tAP - STATION DISCONNECTED".."\n\tMAC: "..T.MAC.."\n\tAID: "..T.AID)
 end)

wifi.eventmon.register(wifi.eventmon.AP_PROBEREQRECVED, function(T) 
 print("\n\tAP - STATION DISCONNECTED".."\n\tMAC: ".. T.MAC.."\n\tRSSI: "..T.RSSI)
 end)
@TerryE
Copy link
Collaborator

TerryE commented Dec 22, 2015

Firstly use lua_getstate().

Next the convention is that constants are uppercase. and I think that the 'mode' is superfluous, so why not
wifi.eventmon.STA_GOT_IP etc.? Then this will be consistent with other modules.

We also need to document T. Sorry but I am a bit snowed under at the moment so I can't do a decent code review, but if we do spin off a temporary dev150 then why don't we consider merging in this PR to this branch to make it easier to kick the tyres".

Third, I feel that ẁifi.c is getting a little bloated. Maybe it's spin this off into a wifi folder. and even make it an optional module.

@jmattsson
Copy link
Member

Over all I'm in favour here! Terry already made some excellent suggestions, so I'll just add another couple here:

  • I'd like to see either an unregister function, or just have the register function take a nil for the function argument to deregister. Passing a string is very non-standard.
  • You should be able to register the event callback inside the Lua module init function (look for luaopen_xyz in the existing modules for examples).

And finally a question: Will this replace the earlier eventMon{Reg,Start,Stop} functions?

@nickandrew
Copy link
Contributor

+1 Looks good.

@dnc40085
Copy link
Contributor Author

Thank you for your input!
@TerryE

  • 1.Thanks for the confirmation, I added gL=lua_getstate() to luaopen_wifi and removed all instances of gL=L in the wifi module so they wouldn't interfere.
  • 2a. Done. Should I un-abbreviate the constants or should I leave them be?
  • 2b. Done.
    1. I've been thinking that for a while.
      I tried to do this but I can't figure it out, I put the code in a separate source file and tried using NODEMCU_MODULE(WIFI_EVENTMON, "wifi.eventmon", wifi_eventmon_map, luaopen_wifi_eventmon); and it told me wifi.eventmon didn't exist. so then I tried putting the define for wifi_eventmon_map in a header file and inlcuded it in both wifi.c and wifi_eventmon.c and ended up getting a linker error saying wifi_eventmon_map not defined.

Also, I couldn't figure out how to make files compile when I put them in the new folder app/modules/wifi. I figure that the new folder needs a makefile, but I'm not skilled in the art of makefile-fu so I don't know.

@jmattsson

    1. Done. I can't figure out why I did that in the first place.
      Would it be preferred to have a wifi.eventmon.unregister(event) function instead of wifi.eventmon.register(event, nil)?
    1. Thank you for the info, I always wondered what luaopen_xyz was for.
      Just out of curiosity, is luaopen_xyz executed on boot or when the module is first accessed?
    1. It was my intention to replace wifi.sta.eventmon with the new one... But if it would be preferred to keep it and have them both coexist, I don't see a problem with that.
      I just didn't like that the old one polled wifi_station_get_connect_status() constantly, then triggered the user callback if it changed, it unnecessarily loads the cpu down limiting how fast the user's application can run.

Also, anybody think the name eventmon is good or should it be changed?

@TerryE
Copy link
Collaborator

TerryE commented Dec 25, 2015

Merry Christmas!! I'll reply tomorrow 😄

@dnc40085
Copy link
Contributor Author

Thank you, Merry Christmas to you as well.

@TerryE
Copy link
Collaborator

TerryE commented Dec 26, 2015

A quick comment on using the lua_openXYZ() hook: you need to be a little careful here if your module depends on others having done their initialisation, because there is no guarantee as to the order in which these are called, so some modules may need to defer some initialisation processing to a timer or task callback, but this isn't the case here, I think.

@dnc40085
Copy link
Contributor Author

I figured out how to split the event monitor off into a separate file, not sure if it is the right way, but it works!

Now to figure out how to get the files to compile in a subdirectory of modules...

@TerryE
Copy link
Collaborator

TerryE commented Dec 28, 2015

This is standard stuff -- recursion magic in make. I can do this for you.

This was referenced Feb 1, 2016
@dnc40085
Copy link
Contributor Author

dnc40085 commented May 2, 2016

I forgot to close this.

@dnc40085 dnc40085 closed this as completed May 2, 2016
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants