Skip to content

Adding to the functionality of the ESP32 library in order to provide … #69

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

Closed
wants to merge 6 commits into from
Closed
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
10 changes: 10 additions & 0 deletions adafruit_esp32spi/adafruit_esp32spi.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@
_GET_HOST_BY_NAME_CMD = const(0x35)
_START_SCAN_NETWORKS = const(0x36)
_GET_FW_VERSION_CMD = const(0x37)
_GET_REMOTE_DATA_CMD = const(0x3A)
_GET_TIME = const(0x3B)
_PING_CMD = const(0x3E)

Expand Down Expand Up @@ -768,3 +769,12 @@ def get_time(self):
resp = self._send_command_get_response(_GET_TIME)
return struct.unpack('<i', resp[0])
raise RuntimeError("Must be connected to WiFi before obtaining NTP.")

def get_remote_data(self, socket_num):
"""
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Method to obtain remote IP
"""
self._socknum_ll[0][0] = socket_num
resp = self._send_command_get_response(_GET_REMOTE_DATA_CMD,
self._socknum_ll, reply_params=2)
return resp[0]
Copy link
Contributor

@mscosti mscosti Aug 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the get_remote_data command in NINA returns 2 reply params. The first is the remote IP, the second is the remote port

instead of just returning the IP, I would return both.

something like

return { 'ip_addr': resp[0], 'port': resp[1] }

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File "/lib/adafruit_esp32spi/adafruit_esp32spi.py", line 550, in pretty_ip
KeyError: 0

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So by changing the return type an dictionary object means that when you call this method you will need to do an additional step to get the ip whenever you call this method. Dictionary objects have a method on it called .get(keyname). You'll need to update usages of this method so you actually grab the IP address from the dictionary.

Example:

remote_data = esp.get_remote_data(socknum)
Ip = remote_data.get('ip_addr')

9 changes: 9 additions & 0 deletions adafruit_esp32spi/adafruit_esp32spi_wsgiserver.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ def update_poll(self):
result = self.application(environ, self._start_response)
self.finish_response(result)


def finish_response(self, result):
"""
Called after the application callbile returns result data to respond with.
Expand Down Expand Up @@ -216,3 +217,11 @@ def _get_environ(self, client):
env[key] = value

return env

def check_remote_ip(self):
Copy link
Contributor

@mscosti mscosti Aug 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would move this to the Socket class as a @property, like we did for socknum.(https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI/blob/master/adafruit_esp32spi/adafruit_esp32spi_socket.py#L183-L186)
We could name the property remote_ip. You could add in a second property for remote_port as well.

then every socket instance could get the remote_ip by doing sock_instance.remote_ip, and the port by doing sock_instance.remote_port

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agreed! socket would be the correct home for this method

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if we move the remote ip fetching to a property on Socket, then we're still going to want to expose it some way to a server application. Luckily, the WSGI defines an environment request variable named REMOTE_ADDR. You could add that to the get_environ function here: https://github.com/adafruit/Adafruit_CircuitPython_ESP32SPI/blob/master/adafruit_esp32spi/adafruit_esp32spi_wsgiserver.py#L174-L179

it would look something like

env['REMOTE_ADDR'] = self._client_sock.remote_ip

The environment object gets passed to the application on every single incoming request, so all your request handlers would just automatically get the remote IP and can use it for processing if they need it.

"""
Functionality to control what IP is connecting to the server.
"""
sock_num = self._client_sock.socknum
remote_ip = _the_interface.get_remote_data(sock_num)
return _the_interface.pretty_ip(remote_ip)