Skip to content

misc: Fix exceptions on missing attributes on MicroPython. #84

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

Merged
merged 5 commits into from
May 28, 2024
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
2 changes: 1 addition & 1 deletion .github/workflows/python-linter.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
run: |
# stop the build if there are Python syntax errors or undefined names
flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
flake8 . --count --max-complexity=15 --max-line-length=120 --statistics
flake8 . --count --ignore=C901 --max-complexity=15 --max-line-length=120 --statistics
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ DEVICE_ID = "" # Provided by Arduino cloud when creating a device.
SECRET_KEY = "" # Provided by Arduino cloud when creating a device.
```

Note that by default, the client runs in asynchronous mode. In this mode, the client takes runs an asyncio loop that updates tasks and records, polls networking events, etc. The client also supports a synchronous mode, which requires periodic client polling. To run the client in synchronous mode, pass `sync_mode=True` when creating a client object and call `client.update()` periodically after connecting. For example:
Note that by default, the client runs in asynchronous mode. In this mode, the client runs an asyncio loop that updates tasks and records, polls networking events, etc. The client also supports a synchronous mode, which requires periodic client polling. To run the client in synchronous mode, pass `sync_mode=True` when creating a client object and call `client.update()` periodically after connecting. For example:

```Python
# Run the client in synchronous mode.
Expand Down
4 changes: 2 additions & 2 deletions examples/example.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,8 @@ def user_task(client):
if __name__ == "__main__":
# Parse command line args.
parser = argparse.ArgumentParser(description="arduino_iot_cloud.py")
parser.add_argument("-d", "--debug", action="store_true", help="Enable debugging messages")
parser.add_argument("-s", "--sync", action="store_true", help="Run in synchronous mode")
parser.add_argument("-d", "--debug", action="store_true", help="Enable debugging messages")
parser.add_argument("-s", "--sync", action="store_true", help="Run in synchronous mode")
args = parser.parse_args()

# Assume the host has an active Internet connection.
Expand Down
21 changes: 7 additions & 14 deletions src/arduino_iot_cloud/ucloud.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

import time
import sys
import logging
import cbor2
from senml import SenmlPack
Expand All @@ -22,7 +21,7 @@ class InvalidStateError(Exception):
try:
from arduino_iot_cloud._version import __version__
except (ImportError, AttributeError):
__version__ = "1.3.0"
__version__ = "1.3.1"

# Server/port for basic auth.
_DEFAULT_SERVER = "iot.arduino.cc"
Expand All @@ -40,7 +39,7 @@ def timestamp():


def timestamp_ms():
return time.time_ns()//1000000
return time.time_ns() // 1000000


def log_level_enabled(level):
Expand Down Expand Up @@ -199,14 +198,6 @@ def __init__(
self.async_mode = not sync_mode
self.connected = False

if "pin" in ssl_params:
try:
# Use M2Crypto to load key and cert from HSM.
import M2Crypto # noqa
except (ImportError, AttributeError):
logging.error("The m2crypto module is required to use HSM.")
sys.exit(1)

# Convert args to bytes if they are passed as strings.
if isinstance(device_id, str):
device_id = bytes(device_id, "utf-8")
Expand Down Expand Up @@ -372,9 +363,11 @@ def poll_discovery(self, aiot=None):
self.mqtt.subscribe(self.create_topic("shadow", "i"), qos=1)
self.mqtt.publish(self.create_topic("shadow", "o"), self.senmlpack.to_cbor(), qos=1)

# Push library version and mode.
libv = "%s-%s" % (__version__, "async" if self.async_mode else "sync")
self.mqtt.publish(self.command_topic, cbor2.dumps(cbor2.CBORTag(67328, [libv])), qos=1)
if hasattr(cbor2, "dumps"):
# Push library version and mode.
libv = "%s-%s" % (__version__, "async" if self.async_mode else "sync")
# Note we have to add the tag manually because python-ecosys's cbor2 doesn't suppor CBORTags.
self.mqtt.publish(self.command_topic, b"\xda\x00\x01\x07\x00" + cbor2.dumps([libv]), qos=1)
logging.info("Device configured via discovery protocol.")
if self.async_mode:
raise DoneException()
Expand Down
10 changes: 8 additions & 2 deletions src/arduino_iot_cloud/ussl.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
# SSL module with m2crypto backend for HSM support.

import ssl
import sys
import logging

pkcs11 = None

Expand All @@ -29,7 +31,7 @@ def wrap_socket(sock, ssl_params={}):
ctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
if hasattr(ctx, "set_default_verify_paths"):
ctx.set_default_verify_paths()
if verify != ssl.CERT_REQUIRED:
if hasattr(ctx, "check_hostname") and verify != ssl.CERT_REQUIRED:
ctx.check_hostname = False
ctx.verify_mode = verify
if keyfile is not None and certfile is not None:
Expand All @@ -41,7 +43,11 @@ def wrap_socket(sock, ssl_params={}):
return ctx.wrap_socket(sock, server_hostname=hostname)
else:
# Use M2Crypto to load key and cert from HSM.
from M2Crypto import m2, SSL, Engine
try:
from M2Crypto import m2, SSL, Engine
except (ImportError, AttributeError):
logging.error("The m2crypto module is required to use HSM.")
sys.exit(1)

global pkcs11
if pkcs11 is None:
Expand Down
2 changes: 1 addition & 1 deletion tests/ci.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def wdt_task(client, ts=[None]):
"-f", "--file-auth", action="store_true", help="Use key/cert files"
)
parser.add_argument(
"-s", "--sync", action="store_true", help="Run in synchronous mode"
"-s", "--sync", action="store_true", help="Run in synchronous mode"
)
args = parser.parse_args()

Expand Down
Loading