esp_local_ctrl/scripts: Update the script to use async methods

pull/9408/head
Aditya Patwardhan 2022-06-30 07:35:55 +05:30 zatwierdzone przez Laukik Hase
rodzic bd67a410f6
commit 1e90632639
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 11C571361F51A199
2 zmienionych plików z 29 dodań i 36 usunięć

Wyświetl plik

@ -1,23 +1,13 @@
#!/usr/bin/env python
#
# Copyright 2018 Espressif Systems (Shanghai) PTE LTD
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
#
from __future__ import print_function
import argparse
import asyncio
import json
import os
import ssl
@ -135,7 +125,7 @@ def get_security(secver, pop=None, verbose=False):
return None
def get_transport(sel_transport, service_name, check_hostname):
async def get_transport(sel_transport, service_name, check_hostname):
try:
tp = None
if (sel_transport == 'http'):
@ -151,15 +141,16 @@ def get_transport(sel_transport, service_name, check_hostname):
'esp_local_ctrl/session': '0002',
'esp_local_ctrl/control': '0003'}
)
await tp.connect(devname=service_name)
return tp
except RuntimeError as e:
on_except(e)
return None
def version_match(tp, protover, verbose=False):
async def version_match(tp, protover, verbose=False):
try:
response = tp.send_data('proto-ver', protover)
response = await tp.send_data('proto-ver', protover)
if verbose:
print('proto-ver response : ', response)
@ -186,11 +177,11 @@ def version_match(tp, protover, verbose=False):
return None
def has_capability(tp, capability='none', verbose=False):
async def has_capability(tp, capability='none', verbose=False):
# Note : default value of `capability` argument cannot be empty string
# because protocomm_httpd expects non zero content lengths
try:
response = tp.send_data('proto-ver', capability)
response = await tp.send_data('proto-ver', capability)
if verbose:
print('proto-ver response : ', response)
@ -220,14 +211,14 @@ def has_capability(tp, capability='none', verbose=False):
return False
def establish_session(tp, sec):
async def establish_session(tp, sec):
try:
response = None
while True:
request = sec.security_session(response)
if request is None:
break
response = tp.send_data('esp_local_ctrl/session', request)
response = await tp.send_data('esp_local_ctrl/session', request)
if (response is None):
return False
return True
@ -236,17 +227,17 @@ def establish_session(tp, sec):
return None
def get_all_property_values(tp, security_ctx):
async def get_all_property_values(tp, security_ctx):
try:
props = []
message = proto_lc.get_prop_count_request(security_ctx)
response = tp.send_data('esp_local_ctrl/control', message)
response = await tp.send_data('esp_local_ctrl/control', message)
count = proto_lc.get_prop_count_response(security_ctx, response)
if count == 0:
raise RuntimeError('No properties found!')
indices = [i for i in range(count)]
message = proto_lc.get_prop_vals_request(security_ctx, indices)
response = tp.send_data('esp_local_ctrl/control', message)
response = await tp.send_data('esp_local_ctrl/control', message)
props = proto_lc.get_prop_vals_response(security_ctx, response)
if len(props) != count:
raise RuntimeError('Incorrect count of properties!', len(props), count)
@ -258,14 +249,14 @@ def get_all_property_values(tp, security_ctx):
return []
def set_property_values(tp, security_ctx, props, indices, values, check_readonly=False):
async def set_property_values(tp, security_ctx, props, indices, values, check_readonly=False):
try:
if check_readonly:
for index in indices:
if prop_is_readonly(props[index]):
raise RuntimeError('Cannot set value of Read-Only property')
message = proto_lc.set_prop_vals_request(security_ctx, indices, values)
response = tp.send_data('esp_local_ctrl/control', message)
response = await tp.send_data('esp_local_ctrl/control', message)
return proto_lc.set_prop_vals_response(security_ctx, response)
except RuntimeError as e:
on_except(e)
@ -279,7 +270,7 @@ def desc_format(*args):
return desc
if __name__ == '__main__':
async def main():
parser = argparse.ArgumentParser(add_help=False)
parser = argparse.ArgumentParser(description='Control an ESP32 running esp_local_ctrl service')
@ -325,7 +316,7 @@ if __name__ == '__main__':
if args.transport == 'http':
args.service_name += '.local'
obj_transport = get_transport(args.transport, args.service_name, not args.dont_check_hostname)
obj_transport = await get_transport(args.transport, args.service_name, not args.dont_check_hostname)
if obj_transport is None:
print('---- Invalid transport ----')
exit(1)
@ -333,16 +324,16 @@ if __name__ == '__main__':
# If security version not specified check in capabilities
if args.secver is None:
# First check if capabilities are supported or not
if not has_capability(obj_transport):
if not await has_capability(obj_transport):
print('Security capabilities could not be determined. Please specify \'--sec_ver\' explicitly')
print('---- Invalid Security Version ----')
exit(2)
# When no_sec is present, use security 0, else security 1
args.secver = int(not has_capability(obj_transport, 'no_sec'))
args.secver = int(not await has_capability(obj_transport, 'no_sec'))
print('Security scheme determined to be :', args.secver)
if (args.secver != 0) and not has_capability(obj_transport, 'no_pop'):
if (args.secver != 0) and not await has_capability(obj_transport, 'no_pop'):
if len(args.pop) == 0:
print('---- Proof of Possession argument not provided ----')
exit(2)
@ -350,27 +341,27 @@ if __name__ == '__main__':
print('---- Proof of Possession will be ignored ----')
args.pop = ''
obj_security = get_security(args.secver, args.pop, False)
obj_security = get_security(args.secver, args.pop, args.verbose)
if obj_security is None:
print('---- Invalid Security Version ----')
exit(2)
if args.version != '':
print('\n==== Verifying protocol version ====')
if not version_match(obj_transport, args.version, args.verbose):
if not await version_match(obj_transport, args.version, args.verbose):
print('---- Error in protocol version matching ----')
exit(2)
print('==== Verified protocol version successfully ====')
print('\n==== Starting Session ====')
if not establish_session(obj_transport, obj_security):
if not await establish_session(obj_transport, obj_security):
print('Failed to establish session. Ensure that security scheme and proof of possession are correct')
print('---- Error in establishing session ----')
exit(3)
print('==== Session Established ====')
while True:
properties = get_all_property_values(obj_transport, obj_security)
properties = await get_all_property_values(obj_transport, obj_security)
if len(properties) == 0:
print('---- Error in reading property values ----')
exit(4)
@ -416,5 +407,8 @@ if __name__ == '__main__':
set_values += [value]
set_indices += [select - 1]
if not set_property_values(obj_transport, obj_security, properties, set_indices, set_values):
if not await set_property_values(obj_transport, obj_security, properties, set_indices, set_values):
print('Failed to set values!')
if __name__ == '__main__':
asyncio.run(main())

Wyświetl plik

@ -1876,7 +1876,6 @@ examples/protocols/esp_http_client/main/esp_http_client_example.c
examples/protocols/esp_local_ctrl/example_test.py
examples/protocols/esp_local_ctrl/main/app_main.c
examples/protocols/esp_local_ctrl/main/esp_local_ctrl_service.c
examples/protocols/esp_local_ctrl/scripts/esp_local_ctrl.py
examples/protocols/esp_local_ctrl/scripts/proto_lc.py
examples/protocols/http2_request/main/http2_request_example_main.c
examples/protocols/http_request/main/http_request_example_main.c