forked from espressif/esp-idf
esp_local_ctrl/scripts: Removed python2 compatibility
This commit is contained in:
@@ -4,8 +4,6 @@
|
||||
# SPDX-License-Identifier: Apache-2.0
|
||||
#
|
||||
|
||||
from __future__ import print_function
|
||||
|
||||
import argparse
|
||||
import asyncio
|
||||
import json
|
||||
@@ -14,15 +12,12 @@ import ssl
|
||||
import struct
|
||||
import sys
|
||||
import textwrap
|
||||
from builtins import input
|
||||
|
||||
import proto_lc
|
||||
from future.utils import tobytes
|
||||
|
||||
try:
|
||||
import esp_prov
|
||||
import security
|
||||
|
||||
except ImportError:
|
||||
idf_path = os.environ['IDF_PATH']
|
||||
sys.path.insert(0, idf_path + '/components/protocomm/python')
|
||||
@@ -67,7 +62,7 @@ def encode_prop_value(prop, value):
|
||||
elif prop['type'] == PROP_TYPE_BOOLEAN:
|
||||
return struct.pack('?', value)
|
||||
elif prop['type'] == PROP_TYPE_STRING:
|
||||
return tobytes(value)
|
||||
return bytes(value, encoding='latin-1')
|
||||
return value
|
||||
except struct.error as e:
|
||||
print(e)
|
||||
@@ -306,10 +301,11 @@ async def main():
|
||||
help=argparse.SUPPRESS)
|
||||
|
||||
parser.add_argument('-v', '--verbose', dest='verbose', help='increase output verbosity', action='store_true')
|
||||
|
||||
args = parser.parse_args()
|
||||
|
||||
if args.version != '':
|
||||
print('==== Esp_Ctrl Version: ' + args.version + ' ====')
|
||||
print(f'==== Esp_Ctrl Version: {args.version} ====')
|
||||
|
||||
if args.service_name == '':
|
||||
args.service_name = 'my_esp_ctrl_device'
|
||||
@@ -318,20 +314,18 @@ async def main():
|
||||
|
||||
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)
|
||||
raise RuntimeError('Failed to establish connection')
|
||||
|
||||
# If security version not specified check in capabilities
|
||||
if args.secver is None:
|
||||
# First check if capabilities are supported or not
|
||||
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)
|
||||
print('Security capabilities could not be determined, please specify "--sec_ver" explicitly')
|
||||
raise ValueError('Invalid Security Version')
|
||||
|
||||
# When no_sec is present, use security 0, else security 1
|
||||
args.secver = int(not await has_capability(obj_transport, 'no_sec'))
|
||||
print('Security scheme determined to be :', args.secver)
|
||||
print(f'==== Security Scheme: {args.secver} ====')
|
||||
|
||||
if (args.secver != 0) and not await has_capability(obj_transport, 'no_pop'):
|
||||
if len(args.pop) == 0:
|
||||
@@ -343,28 +337,24 @@ async def main():
|
||||
|
||||
obj_security = get_security(args.secver, args.pop, args.verbose)
|
||||
if obj_security is None:
|
||||
print('---- Invalid Security Version ----')
|
||||
exit(2)
|
||||
raise ValueError('Invalid Security Version')
|
||||
|
||||
if args.version != '':
|
||||
print('\n==== Verifying protocol version ====')
|
||||
if not await version_match(obj_transport, args.version, args.verbose):
|
||||
print('---- Error in protocol version matching ----')
|
||||
exit(2)
|
||||
raise RuntimeError('Error in protocol version matching')
|
||||
print('==== Verified protocol version successfully ====')
|
||||
|
||||
print('\n==== Starting Session ====')
|
||||
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)
|
||||
raise RuntimeError('Error in establishing session')
|
||||
print('==== Session Established ====')
|
||||
|
||||
while True:
|
||||
properties = await get_all_property_values(obj_transport, obj_security)
|
||||
if len(properties) == 0:
|
||||
print('---- Error in reading property values ----')
|
||||
exit(4)
|
||||
raise RuntimeError('Error in reading property value')
|
||||
|
||||
print('\n==== Available Properties ====')
|
||||
print('{0: >4} {1: <16} {2: <10} {3: <16} {4: <16}'.format(
|
||||
@@ -381,7 +371,7 @@ async def main():
|
||||
inval = input('\nSelect properties to set (0 to re-read, \'q\' to quit) : ')
|
||||
if inval.lower() == 'q':
|
||||
print('Quitting...')
|
||||
exit(5)
|
||||
exit(0)
|
||||
invals = inval.split(',')
|
||||
selections = [int(val) for val in invals]
|
||||
if min(selections) < 0 or max(selections) > len(properties):
|
||||
|
@@ -1,34 +1,24 @@
|
||||
# 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 importlib.util
|
||||
import os
|
||||
|
||||
from future.utils import tobytes
|
||||
import sys
|
||||
from importlib.abc import Loader
|
||||
from typing import Any
|
||||
|
||||
|
||||
def _load_source(name, path):
|
||||
try:
|
||||
from importlib.machinery import SourceFileLoader
|
||||
return SourceFileLoader(name, path).load_module()
|
||||
except ImportError:
|
||||
# importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3)
|
||||
import imp
|
||||
return imp.load_source(name, path)
|
||||
def _load_source(name: str, path: str) -> Any:
|
||||
spec = importlib.util.spec_from_file_location(name, path)
|
||||
if not spec:
|
||||
return None
|
||||
|
||||
module = importlib.util.module_from_spec(spec)
|
||||
sys.modules[spec.name] = module
|
||||
assert isinstance(spec.loader, Loader)
|
||||
spec.loader.exec_module(module)
|
||||
return module
|
||||
|
||||
|
||||
idf_path = os.environ['IDF_PATH']
|
||||
@@ -36,6 +26,10 @@ constants_pb2 = _load_source('constants_pb2', idf_path + '/components/protocomm/
|
||||
local_ctrl_pb2 = _load_source('esp_local_ctrl_pb2', idf_path + '/components/esp_local_ctrl/python/esp_local_ctrl_pb2.py')
|
||||
|
||||
|
||||
def to_bytes(s: str) -> bytes:
|
||||
return bytes(s, encoding='latin-1')
|
||||
|
||||
|
||||
def get_prop_count_request(security_ctx):
|
||||
req = local_ctrl_pb2.LocalCtrlMessage()
|
||||
req.msg = local_ctrl_pb2.TypeCmdGetPropertyCount
|
||||
@@ -46,7 +40,7 @@ def get_prop_count_request(security_ctx):
|
||||
|
||||
|
||||
def get_prop_count_response(security_ctx, response_data):
|
||||
decrypt = security_ctx.decrypt_data(tobytes(response_data))
|
||||
decrypt = security_ctx.decrypt_data(to_bytes(response_data))
|
||||
resp = local_ctrl_pb2.LocalCtrlMessage()
|
||||
resp.ParseFromString(decrypt)
|
||||
if (resp.resp_get_prop_count.status == 0):
|
||||
@@ -66,7 +60,7 @@ def get_prop_vals_request(security_ctx, indices):
|
||||
|
||||
|
||||
def get_prop_vals_response(security_ctx, response_data):
|
||||
decrypt = security_ctx.decrypt_data(tobytes(response_data))
|
||||
decrypt = security_ctx.decrypt_data(to_bytes(response_data))
|
||||
resp = local_ctrl_pb2.LocalCtrlMessage()
|
||||
resp.ParseFromString(decrypt)
|
||||
results = []
|
||||
@@ -76,7 +70,7 @@ def get_prop_vals_response(security_ctx, response_data):
|
||||
'name': prop.name,
|
||||
'type': prop.type,
|
||||
'flags': prop.flags,
|
||||
'value': tobytes(prop.value)
|
||||
'value': prop.value
|
||||
}]
|
||||
return results
|
||||
|
||||
@@ -95,7 +89,7 @@ def set_prop_vals_request(security_ctx, indices, values):
|
||||
|
||||
|
||||
def set_prop_vals_response(security_ctx, response_data):
|
||||
decrypt = security_ctx.decrypt_data(tobytes(response_data))
|
||||
decrypt = security_ctx.decrypt_data(to_bytes(response_data))
|
||||
resp = local_ctrl_pb2.LocalCtrlMessage()
|
||||
resp.ParseFromString(decrypt)
|
||||
return (resp.resp_set_prop_vals.status == 0)
|
||||
|
@@ -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/proto_lc.py
|
||||
examples/protocols/http2_request/main/http2_request_example_main.c
|
||||
examples/protocols/http_request/main/http_request_example_main.c
|
||||
examples/protocols/http_server/advanced_tests/http_server_advanced_test.py
|
||||
|
Reference in New Issue
Block a user