| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | #!/usr/bin/env python | 
					
						
							|  |  |  | # | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | # SPDX-FileCopyrightText: 2018-2022 Espressif Systems (Shanghai) CO LTD | 
					
						
							|  |  |  | # SPDX-License-Identifier: Apache-2.0 | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | # | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import argparse | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | import asyncio | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | import json | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | import os | 
					
						
							| 
									
										
										
										
											2020-05-07 12:47:46 +02:00
										 |  |  | import ssl | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  | import struct | 
					
						
							|  |  |  | import sys | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | import textwrap | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  | from getpass import getpass | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | import proto_lc | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | try: | 
					
						
							|  |  |  |     import esp_prov | 
					
						
							|  |  |  |     import security | 
					
						
							|  |  |  | except ImportError: | 
					
						
							|  |  |  |     idf_path = os.environ['IDF_PATH'] | 
					
						
							|  |  |  |     sys.path.insert(0, idf_path + '/components/protocomm/python') | 
					
						
							|  |  |  |     sys.path.insert(1, idf_path + '/tools/esp_prov') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     import esp_prov | 
					
						
							|  |  |  |     import security | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | # Set this to true to allow exceptions to be thrown | 
					
						
							|  |  |  | config_throw_except = False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Property types enum | 
					
						
							|  |  |  | PROP_TYPE_TIMESTAMP = 0 | 
					
						
							|  |  |  | PROP_TYPE_INT32     = 1 | 
					
						
							|  |  |  | PROP_TYPE_BOOLEAN   = 2 | 
					
						
							|  |  |  | PROP_TYPE_STRING    = 3 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | # Property flags enum | 
					
						
							|  |  |  | PROP_FLAG_READONLY = (1 << 0) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def prop_typestr(prop): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     if prop['type'] == PROP_TYPE_TIMESTAMP: | 
					
						
							|  |  |  |         return 'TIME(us)' | 
					
						
							|  |  |  |     elif prop['type'] == PROP_TYPE_INT32: | 
					
						
							|  |  |  |         return 'INT32' | 
					
						
							|  |  |  |     elif prop['type'] == PROP_TYPE_BOOLEAN: | 
					
						
							|  |  |  |         return 'BOOLEAN' | 
					
						
							|  |  |  |     elif prop['type'] == PROP_TYPE_STRING: | 
					
						
							|  |  |  |         return 'STRING' | 
					
						
							|  |  |  |     return 'UNKNOWN' | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def encode_prop_value(prop, value): | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         if prop['type'] == PROP_TYPE_TIMESTAMP: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return struct.pack('q', value) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_INT32: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return struct.pack('i', value) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_BOOLEAN: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return struct.pack('?', value) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_STRING: | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |             return bytes(value, encoding='latin-1') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         return value | 
					
						
							|  |  |  |     except struct.error as e: | 
					
						
							|  |  |  |         print(e) | 
					
						
							|  |  |  |     return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def decode_prop_value(prop, value): | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         if prop['type'] == PROP_TYPE_TIMESTAMP: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return struct.unpack('q', value)[0] | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_INT32: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return struct.unpack('i', value)[0] | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_BOOLEAN: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return struct.unpack('?', value)[0] | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_STRING: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return value.decode('latin-1') | 
					
						
							|  |  |  |         return value | 
					
						
							|  |  |  |     except struct.error as e: | 
					
						
							|  |  |  |         print(e) | 
					
						
							|  |  |  |     return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def str_to_prop_value(prop, strval): | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         if prop['type'] == PROP_TYPE_TIMESTAMP: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return int(strval) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_INT32: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return int(strval) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_BOOLEAN: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return bool(strval) | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         elif prop['type'] == PROP_TYPE_STRING: | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             return strval | 
					
						
							|  |  |  |         return strval | 
					
						
							|  |  |  |     except ValueError as e: | 
					
						
							|  |  |  |         print(e) | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def prop_is_readonly(prop): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     return (prop['flags'] & PROP_FLAG_READONLY) != 0 | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | def on_except(err): | 
					
						
							|  |  |  |     if config_throw_except: | 
					
						
							|  |  |  |         raise RuntimeError(err) | 
					
						
							|  |  |  |     else: | 
					
						
							|  |  |  |         print(err) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  | def get_security(secver, username, password, pop='', verbose=False): | 
					
						
							|  |  |  |     if secver == 2: | 
					
						
							|  |  |  |         return security.Security2(username, password, verbose) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     if secver == 1: | 
					
						
							|  |  |  |         return security.Security1(pop, verbose) | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |     if secver == 0: | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         return security.Security0(verbose) | 
					
						
							|  |  |  |     return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | async def get_transport(sel_transport, service_name, check_hostname): | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     try: | 
					
						
							|  |  |  |         tp = None | 
					
						
							|  |  |  |         if (sel_transport == 'http'): | 
					
						
							| 
									
										
										
										
											2022-12-16 16:04:22 +05:30
										 |  |  |             tp = esp_prov.transport.Transport_HTTP(service_name, None) | 
					
						
							|  |  |  |         elif (sel_transport == 'https'): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             example_path = os.environ['IDF_PATH'] + '/examples/protocols/esp_local_ctrl' | 
					
						
							|  |  |  |             cert_path = example_path + '/main/certs/rootCA.pem' | 
					
						
							| 
									
										
										
										
											2020-05-07 12:47:46 +02:00
										 |  |  |             ssl_ctx = ssl.create_default_context(cafile=cert_path) | 
					
						
							|  |  |  |             ssl_ctx.check_hostname = check_hostname | 
					
						
							|  |  |  |             tp = esp_prov.transport.Transport_HTTP(service_name, ssl_ctx) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         elif (sel_transport == 'ble'): | 
					
						
							|  |  |  |             tp = esp_prov.transport.Transport_BLE( | 
					
						
							|  |  |  |                 devname=service_name, service_uuid='0000ffff-0000-1000-8000-00805f9b34fb', | 
					
						
							|  |  |  |                 nu_lookup={'esp_local_ctrl/version': '0001', | 
					
						
							|  |  |  |                            'esp_local_ctrl/session': '0002', | 
					
						
							|  |  |  |                            'esp_local_ctrl/control': '0003'} | 
					
						
							|  |  |  |             ) | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |             await tp.connect(devname=service_name) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         return tp | 
					
						
							|  |  |  |     except RuntimeError as e: | 
					
						
							|  |  |  |         on_except(e) | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | async def version_match(tp, protover, verbose=False): | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     try: | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |         response = await tp.send_data('esp_local_ctrl/version', protover) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         if verbose: | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |             print('esp_local_ctrl/version response : ', response) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         # First assume this to be a simple version string | 
					
						
							|  |  |  |         if response.lower() == protover.lower(): | 
					
						
							|  |  |  |             return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             # Else interpret this as JSON structure containing | 
					
						
							|  |  |  |             # information with versions and capabilities of both | 
					
						
							|  |  |  |             # provisioning service and application | 
					
						
							|  |  |  |             info = json.loads(response) | 
					
						
							|  |  |  |             if info['prov']['ver'].lower() == protover.lower(): | 
					
						
							|  |  |  |                 return True | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         except ValueError: | 
					
						
							|  |  |  |             # If decoding as JSON fails, it means that capabilities | 
					
						
							|  |  |  |             # are not supported | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     except Exception as e: | 
					
						
							|  |  |  |         on_except(e) | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | async def has_capability(tp, capability='none', verbose=False): | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     # Note : default value of `capability` argument cannot be empty string | 
					
						
							|  |  |  |     # because protocomm_httpd expects non zero content lengths | 
					
						
							|  |  |  |     try: | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |         response = await tp.send_data('esp_local_ctrl/version', capability) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         if verbose: | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |             print('esp_local_ctrl/version response : ', response) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         try: | 
					
						
							|  |  |  |             # Interpret this as JSON structure containing | 
					
						
							|  |  |  |             # information with versions and capabilities of both | 
					
						
							|  |  |  |             # provisioning service and application | 
					
						
							|  |  |  |             info = json.loads(response) | 
					
						
							|  |  |  |             supported_capabilities = info['prov']['cap'] | 
					
						
							|  |  |  |             if capability.lower() == 'none': | 
					
						
							|  |  |  |                 # No specific capability to check, but capabilities | 
					
						
							|  |  |  |                 # feature is present so return True | 
					
						
							|  |  |  |                 return True | 
					
						
							|  |  |  |             elif capability in supported_capabilities: | 
					
						
							|  |  |  |                 return True | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         except ValueError: | 
					
						
							|  |  |  |             # If decoding as JSON fails, it means that capabilities | 
					
						
							|  |  |  |             # are not supported | 
					
						
							|  |  |  |             return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     except RuntimeError as e: | 
					
						
							|  |  |  |         on_except(e) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | async def establish_session(tp, sec): | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     try: | 
					
						
							|  |  |  |         response = None | 
					
						
							|  |  |  |         while True: | 
					
						
							|  |  |  |             request = sec.security_session(response) | 
					
						
							|  |  |  |             if request is None: | 
					
						
							|  |  |  |                 break | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |             response = await tp.send_data('esp_local_ctrl/session', request) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |             if (response is None): | 
					
						
							|  |  |  |                 return False | 
					
						
							|  |  |  |         return True | 
					
						
							|  |  |  |     except RuntimeError as e: | 
					
						
							|  |  |  |         on_except(e) | 
					
						
							|  |  |  |         return None | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | async def get_all_property_values(tp, security_ctx): | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     try: | 
					
						
							|  |  |  |         props = [] | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         message = proto_lc.get_prop_count_request(security_ctx) | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         response = await tp.send_data('esp_local_ctrl/control', message) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         count = proto_lc.get_prop_count_response(security_ctx, response) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         if count == 0: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             raise RuntimeError('No properties found!') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         indices = [i for i in range(count)] | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         message = proto_lc.get_prop_vals_request(security_ctx, indices) | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         response = await tp.send_data('esp_local_ctrl/control', message) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         props = proto_lc.get_prop_vals_response(security_ctx, response) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         if len(props) != count: | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |             raise RuntimeError('Incorrect count of properties!', len(props), count) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         for p in props: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             p['value'] = decode_prop_value(p, p['value']) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         return props | 
					
						
							|  |  |  |     except RuntimeError as e: | 
					
						
							|  |  |  |         on_except(e) | 
					
						
							|  |  |  |         return [] | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | async def set_property_values(tp, security_ctx, props, indices, values, check_readonly=False): | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     try: | 
					
						
							|  |  |  |         if check_readonly: | 
					
						
							|  |  |  |             for index in indices: | 
					
						
							|  |  |  |                 if prop_is_readonly(props[index]): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                     raise RuntimeError('Cannot set value of Read-Only property') | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         message = proto_lc.set_prop_vals_request(security_ctx, indices, values) | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         response = await tp.send_data('esp_local_ctrl/control', message) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         return proto_lc.set_prop_vals_response(security_ctx, response) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     except RuntimeError as e: | 
					
						
							|  |  |  |         on_except(e) | 
					
						
							|  |  |  |         return False | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | def desc_format(*args): | 
					
						
							|  |  |  |     desc = '' | 
					
						
							|  |  |  |     for arg in args: | 
					
						
							|  |  |  |         desc += textwrap.fill(replace_whitespace=False, text=arg) + '\n' | 
					
						
							|  |  |  |     return desc | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | async def main(): | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     parser = argparse.ArgumentParser(add_help=False) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser = argparse.ArgumentParser(description='Control an ESP32 running esp_local_ctrl service') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--version', dest='version', type=str, | 
					
						
							|  |  |  |                         help='Protocol version', default='') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--transport', dest='transport', type=str, | 
					
						
							| 
									
										
										
										
											2022-12-16 16:04:22 +05:30
										 |  |  |                         help='transport i.e http/https/ble', default='https') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--name', dest='service_name', type=str, | 
					
						
							|  |  |  |                         help='BLE Device Name / HTTP Server hostname or IP', default='') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     parser.add_argument('--sec_ver', dest='secver', type=int, default=None, | 
					
						
							|  |  |  |                         help=desc_format( | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |                             'Protocomm security scheme used for secure ' | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |                             'session establishment. Accepted values are :', | 
					
						
							|  |  |  |                             '\t- 0 : No security', | 
					
						
							|  |  |  |                             '\t- 1 : X25519 key exchange + AES-CTR encryption', | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |                             '\t- 2 : SRP6a + AES-GCM encryption', | 
					
						
							|  |  |  |                             '\t      + Authentication using Proof of Possession (PoP)')) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     parser.add_argument('--pop', dest='pop', type=str, default='', | 
					
						
							|  |  |  |                         help=desc_format( | 
					
						
							|  |  |  |                             'This specifies the Proof of possession (PoP) when security scheme 1 ' | 
					
						
							|  |  |  |                             'is used')) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |     parser.add_argument('--sec2_username', dest='sec2_usr', type=str, default='', | 
					
						
							|  |  |  |                         help=desc_format( | 
					
						
							|  |  |  |                             'Username for security scheme 2 (SRP6a)')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     parser.add_argument('--sec2_pwd', dest='sec2_pwd', type=str, default='', | 
					
						
							|  |  |  |                         help=desc_format( | 
					
						
							|  |  |  |                             'Password for security scheme 2 (SRP6a)')) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     parser.add_argument('--sec2_gen_cred', help='Generate salt and verifier for security scheme 2 (SRP6a)', action='store_true') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     parser.add_argument('--sec2_salt_len', dest='sec2_salt_len', type=int, default=16, | 
					
						
							|  |  |  |                         help=desc_format( | 
					
						
							|  |  |  |                             'Salt length for security scheme 2 (SRP6a)')) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('--dont-check-hostname', action='store_true', | 
					
						
							| 
									
										
										
										
											2020-05-07 12:47:46 +02:00
										 |  |  |                         # If enabled, the certificate won't be rejected for hostname mismatch. | 
					
						
							|  |  |  |                         # This option is hidden because it should be used only for testing purposes. | 
					
						
							|  |  |  |                         help=argparse.SUPPRESS) | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |     parser.add_argument('-v', '--verbose', dest='verbose', help='increase output verbosity', action='store_true') | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     args = parser.parse_args() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |     if args.secver == 2 and args.sec2_gen_cred: | 
					
						
							|  |  |  |         if not args.sec2_usr or not args.sec2_pwd: | 
					
						
							|  |  |  |             raise ValueError('Username/password cannot be empty for security scheme 2 (SRP6a)') | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         print('==== Salt-verifier for security scheme 2 (SRP6a) ====') | 
					
						
							|  |  |  |         security.sec2_gen_salt_verifier(args.sec2_usr, args.sec2_pwd, args.sec2_salt_len) | 
					
						
							|  |  |  |         sys.exit() | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     if args.version != '': | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |         print(f'==== Esp_Ctrl Version: {args.version} ====') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |     if args.service_name == '': | 
					
						
							|  |  |  |         args.service_name = 'my_esp_ctrl_device' | 
					
						
							| 
									
										
										
										
											2022-12-16 16:04:22 +05:30
										 |  |  |         if args.transport == 'http' or args.transport == 'https': | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |             args.service_name += '.local' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |     obj_transport = await get_transport(args.transport, args.service_name, not args.dont_check_hostname) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     if obj_transport is None: | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |         raise RuntimeError('Failed to establish connection') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     # If security version not specified check in capabilities | 
					
						
							|  |  |  |     if args.secver is None: | 
					
						
							|  |  |  |         # First check if capabilities are supported or not | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         if not await has_capability(obj_transport): | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |             print('Security capabilities could not be determined, please specify "--sec_ver" explicitly') | 
					
						
							|  |  |  |             raise ValueError('Invalid Security Version') | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         # When no_sec is present, use security 0, else security 1 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         args.secver = int(not await has_capability(obj_transport, 'no_sec')) | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |         print(f'==== Security Scheme: {args.secver} ====') | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |     if (args.secver == 1): | 
					
						
							|  |  |  |         if not await has_capability(obj_transport, 'no_pop'): | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |             if len(args.pop) == 0: | 
					
						
							|  |  |  |                 print('---- Proof of Possession argument not provided ----') | 
					
						
							|  |  |  |                 exit(2) | 
					
						
							|  |  |  |         elif len(args.pop) != 0: | 
					
						
							|  |  |  |             print('---- Proof of Possession will be ignored ----') | 
					
						
							|  |  |  |             args.pop = '' | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-10-11 23:21:40 +05:30
										 |  |  |     if (args.secver == 2): | 
					
						
							|  |  |  |         if len(args.sec2_usr) == 0: | 
					
						
							|  |  |  |             args.sec2_usr = input('Security Scheme 2 - SRP6a Username required: ') | 
					
						
							|  |  |  |         if len(args.sec2_pwd) == 0: | 
					
						
							|  |  |  |             prompt_str = 'Security Scheme 2 - SRP6a Password required: ' | 
					
						
							|  |  |  |             args.sec2_pwd = getpass(prompt_str) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     obj_security = get_security(args.secver, args.sec2_usr, args.sec2_pwd, args.pop, args.verbose) | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     if obj_security is None: | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |         raise ValueError('Invalid Security Version') | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     if args.version != '': | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         print('\n==== Verifying protocol version ====') | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         if not await version_match(obj_transport, args.version, args.verbose): | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |             raise RuntimeError('Error in protocol version matching') | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         print('==== Verified protocol version successfully ====') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     print('\n==== Starting Session ====') | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |     if not await establish_session(obj_transport, obj_security): | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |         print('Failed to establish session. Ensure that security scheme and proof of possession are correct') | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |         raise RuntimeError('Error in establishing session') | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |     print('==== Session Established ====') | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |     while True: | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         properties = await get_all_property_values(obj_transport, obj_security) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         if len(properties) == 0: | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |             raise RuntimeError('Error in reading property value') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |         print('\n==== Available Properties ====') | 
					
						
							|  |  |  |         print('{0: >4} {1: <16} {2: <10} {3: <16} {4: <16}'.format( | 
					
						
							|  |  |  |             'S.N.', 'Name', 'Type', 'Flags', 'Value')) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |         for i in range(len(properties)): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             print('[{0: >2}] {1: <16} {2: <10} {3: <16} {4: <16}'.format( | 
					
						
							|  |  |  |                 i + 1, properties[i]['name'], prop_typestr(properties[i]), | 
					
						
							|  |  |  |                 ['','Read-Only'][prop_is_readonly(properties[i])], | 
					
						
							|  |  |  |                 str(properties[i]['value']))) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         select = 0 | 
					
						
							|  |  |  |         while True: | 
					
						
							|  |  |  |             try: | 
					
						
							| 
									
										
										
										
											2021-05-31 11:05:21 +05:30
										 |  |  |                 inval = input('\nSelect properties to set (0 to re-read, \'q\' to quit) : ') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |                 if inval.lower() == 'q': | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                     print('Quitting...') | 
					
						
							| 
									
										
										
										
											2022-07-13 17:48:15 +05:30
										 |  |  |                     exit(0) | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |                 invals = inval.split(',') | 
					
						
							|  |  |  |                 selections = [int(val) for val in invals] | 
					
						
							|  |  |  |                 if min(selections) < 0 or max(selections) > len(properties): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                     raise ValueError('Invalid input') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |                 break | 
					
						
							|  |  |  |             except ValueError as e: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 print(str(e) + '! Retry...') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  |         if len(selections) == 1 and selections[0] == 0: | 
					
						
							|  |  |  |             continue | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |         set_values = [] | 
					
						
							|  |  |  |         set_indices = [] | 
					
						
							|  |  |  |         for select in selections: | 
					
						
							|  |  |  |             while True: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                 inval = input('Enter value to set for property (' + properties[select - 1]['name'] + ') : ') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |                 value = encode_prop_value(properties[select - 1], | 
					
						
							|  |  |  |                                           str_to_prop_value(properties[select - 1], inval)) | 
					
						
							|  |  |  |                 if value is None: | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |                     print('Invalid input! Retry...') | 
					
						
							| 
									
										
										
										
											2019-06-26 01:03:55 +05:30
										 |  |  |                     continue | 
					
						
							|  |  |  |                 break | 
					
						
							|  |  |  |             set_values += [value] | 
					
						
							|  |  |  |             set_indices += [select - 1] | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  |         if not await set_property_values(obj_transport, obj_security, properties, set_indices, set_values): | 
					
						
							| 
									
										
										
										
											2021-01-26 10:49:01 +08:00
										 |  |  |             print('Failed to set values!') | 
					
						
							| 
									
										
										
										
											2022-06-30 07:35:55 +05:30
										 |  |  | 
 | 
					
						
							|  |  |  | if __name__ == '__main__': | 
					
						
							|  |  |  |     asyncio.run(main()) |