From ec44bfff97c0d70cb9bdb022f45bb7e84f13f582 Mon Sep 17 00:00:00 2001 From: Nirav Shah Date: Wed, 12 May 2021 22:54:17 -0400 Subject: [PATCH 1/7] Implemented Resolve method. --- src/controller/python/chip-device-ctrl.py | 39 +++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index b8c965a7adb450..10903e6a9f582d 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -41,7 +41,8 @@ from chip.ChipBleUtility import FAKE_CONN_OBJ_VALUE from chip.setup_payload import SetupPayload from xmlrpc.server import SimpleXMLRPCServer - +from enum import Enum +from typing import Any, Dict,Optional # Extend sys.path with one or more directories, relative to the location of the # running script, in which the chip package might be found . This makes it # possible to run the device manager shell from a non-standard install location, @@ -70,7 +71,9 @@ from chip.ChipBluezMgr import BluezManager as BleManager # The exceptions for CHIP Device Controller CLI - +class StatusCodeEnum(Enum): + SUCCESS = 0 + FAILED = 1 class ChipDevCtrlException(exceptions.ChipStackException): pass @@ -622,6 +625,21 @@ def echo_alive(message): print(message) return message +def resolve(fabric_id: int, node_id: int) -> str: + try: + __check_supported_os() + err = device_manager.devCtrl.ResolveNode(fabric_id, node_id) + if err == 0: + address = device_manager.devCtrl.GetAddressAndPort(int(args[1])) + address = "{}:{}".format( + *address) if address else "unknown" + + return __get_response_dict(status = StatusCodeEnum.SUCCESS, result = {'address': address}) + except exceptions.ChipStackException as ex: + return __get_response_dict(status = StatusCodeEnum.FAILED, error = str(ex)) + except Exception as e: + return __get_response_dict(status = StatusCodeEnum.FAILED, error = str(e)) + def ble_scan(): device_manager.do_blescan("") #TODO: Return a list of available devices @@ -631,6 +649,7 @@ def start_rpc_server(): with SimpleXMLRPCServer(("0.0.0.0", 5000)) as server: server.register_function(echo_alive) server.register_function(ble_scan) + server.register_function(resolve) server.register_multicall_functions() print('Serving XML-RPC on localhost port 5000') try: @@ -639,6 +658,22 @@ def start_rpc_server(): print("\nKeyboard interrupt received, exiting.") sys.exit(0) +def __get_response_dict(status: StatusCodeEnum, result: Optional[Dict[Any, Any]] = None, error:Optional[str] = None) -> Dict [Any, Any]: + if error is not None: + return { "status" : status.value, "error" :f'Unable to connect due to exception {error}' } + else: + if result is not None: + return { "status" : status.value, "result": result} + else: + return { "status" : status.value} + +def __check_supported_os()-> bool: + if platform.system() == 'Darwin': + raise Exception(platform.system() + " not supported") + elif sys.platform.startswith('linux'): + return True + + raise Exception("OS Not Supported") ######--------------------------------------------------###### def main(): From 62d9a2b0a436fed1311d78ff05391cdb885d6b8c Mon Sep 17 00:00:00 2001 From: Nirav Shah Date: Thu, 13 May 2021 14:36:24 -0400 Subject: [PATCH 2/7] Code review comments resolved. --- src/controller/python/chip-device-ctrl.py | 25 ++++++++++++----------- 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index 10903e6a9f582d..8b79e8fcb7ed78 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -41,6 +41,7 @@ from chip.ChipBleUtility import FAKE_CONN_OBJ_VALUE from chip.setup_payload import SetupPayload from xmlrpc.server import SimpleXMLRPCServer + from enum import Enum from typing import Any, Dict,Optional # Extend sys.path with one or more directories, relative to the location of the @@ -70,11 +71,17 @@ elif sys.platform.startswith('linux'): from chip.ChipBluezMgr import BluezManager as BleManager -# The exceptions for CHIP Device Controller CLI class StatusCodeEnum(Enum): SUCCESS = 0 FAILED = 1 +class RPCResponseKeyEnum(Enum): + STATUS = "status" + RESULT = "result" + ERROR = "error" + +# The exceptions for CHIP Device Controller CLI + class ChipDevCtrlException(exceptions.ChipStackException): pass @@ -625,7 +632,7 @@ def echo_alive(message): print(message) return message -def resolve(fabric_id: int, node_id: int) -> str: +def resolve(fabric_id: int, node_id: int) -> Dict[str, Any]: try: __check_supported_os() err = device_manager.devCtrl.ResolveNode(fabric_id, node_id) @@ -658,19 +665,13 @@ def start_rpc_server(): print("\nKeyboard interrupt received, exiting.") sys.exit(0) -def __get_response_dict(status: StatusCodeEnum, result: Optional[Dict[Any, Any]] = None, error:Optional[str] = None) -> Dict [Any, Any]: - if error is not None: - return { "status" : status.value, "error" :f'Unable to connect due to exception {error}' } - else: - if result is not None: - return { "status" : status.value, "result": result} - else: - return { "status" : status.value} +def __get_response_dict(status: StatusCodeEnum, result: Optional[Dict[Any, Any]] = None, error:Optional[str] = None) -> Dict [str, Any]: + return { RPCResponseKeyEnum.STATUS.value : status.value, RPCResponseKeyEnum.RESULT.value : result, RPCResponseKeyEnum.ERROR.value : error } def __check_supported_os()-> bool: - if platform.system() == 'Darwin': + if platform.system().lower() == 'darwin': raise Exception(platform.system() + " not supported") - elif sys.platform.startswith('linux'): + elif sys.platform.lower().startswith('linux'): return True raise Exception("OS Not Supported") From b628eb86f97cc35b5d0fa491332e632d560380d2 Mon Sep 17 00:00:00 2001 From: Xiaolei Zhou Date: Thu, 13 May 2021 14:17:07 -0700 Subject: [PATCH 3/7] Open qr_code_parse to dict api for RPC server --- src/controller/python/chip-device-ctrl.py | 4 ++++ src/controller/python/chip/setup_payload/setup_payload.py | 7 +++++++ 2 files changed, 11 insertions(+) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index b8c965a7adb450..7f2b6c5c9efe6f 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -627,10 +627,14 @@ def ble_scan(): #TODO: Return a list of available devices return "Scan started" +def qr_code_parse(qr_code): + return SetupPayload().ParseQrCode(qr_code).Dictionary() + def start_rpc_server(): with SimpleXMLRPCServer(("0.0.0.0", 5000)) as server: server.register_function(echo_alive) server.register_function(ble_scan) + server.register_function(qr_code_parse) server.register_multicall_functions() print('Serving XML-RPC on localhost port 5000') try: diff --git a/src/controller/python/chip/setup_payload/setup_payload.py b/src/controller/python/chip/setup_payload/setup_payload.py index f0bba33ae47059..5cb102dc4cb50f 100644 --- a/src/controller/python/chip/setup_payload/setup_payload.py +++ b/src/controller/python/chip/setup_payload/setup_payload.py @@ -95,3 +95,10 @@ def __InitNativeFunctions(self, chipLib): setter.Set("pychip_SetupPayload_ParseManualPairingCode", c_int32, [c_char_p, SetupPayload.AttributeVisitor, SetupPayload.VendorAttributeVisitor]) + + def Dictionary(self): + payload_dict = {} + attributes_array = self.attributes + self.vendor_attributes + for name, value in attributes_array: + payload_dict[name] = value + return payload_dict \ No newline at end of file From e48c2cd03f787c8a37db6f964315327bc2936fde Mon Sep 17 00:00:00 2001 From: Xiaolei Zhou Date: Fri, 14 May 2021 11:05:05 -0700 Subject: [PATCH 4/7] demarcator added in setup_payload, new line at the end --- src/controller/python/chip/setup_payload/setup_payload.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/controller/python/chip/setup_payload/setup_payload.py b/src/controller/python/chip/setup_payload/setup_payload.py index 5cb102dc4cb50f..c15385b1252f1e 100644 --- a/src/controller/python/chip/setup_payload/setup_payload.py +++ b/src/controller/python/chip/setup_payload/setup_payload.py @@ -96,9 +96,11 @@ def __InitNativeFunctions(self, chipLib): c_int32, [c_char_p, SetupPayload.AttributeVisitor, SetupPayload.VendorAttributeVisitor]) +######----------------------------------------------------------------------------------------###### + def Dictionary(self): payload_dict = {} attributes_array = self.attributes + self.vendor_attributes for name, value in attributes_array: payload_dict[name] = value - return payload_dict \ No newline at end of file + return payload_dict From 4245448bfccf83aa5021062c7abbdfa4cbe8111f Mon Sep 17 00:00:00 2001 From: Xiaolei Zhou Date: Thu, 20 May 2021 14:53:26 -0700 Subject: [PATCH 5/7] Update chip-device-ctrl.py --- src/controller/python/chip-device-ctrl.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index 8b115c1aa40f74..d2b4dd025b5422 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -698,7 +698,11 @@ def ip_connect(ip_address: string, pin_code: int, node_id: int) -> Dict[str, any return __get_response_dict(status = StatusCodeEnum.FAILED, error = str(e)) def qr_code_parse(qr_code): - return SetupPayload().ParseQrCode(qr_code).Dictionary() + try: + result = SetupPayload().ParseQrCode(qr_code).Dictionary() + return __get_response_dict(status = StatusCodeEnum.SUCCESS, result = result) + except Exception as e: + return __get_response_dict(status = StatusCodeEnum.FAILED, error = str(e)) def start_rpc_server(): From 8521f5f4e8a977b5e10126d4bfe740dba4128d66 Mon Sep 17 00:00:00 2001 From: Xiaolei Zhou Date: Tue, 1 Jun 2021 12:02:07 -0700 Subject: [PATCH 6/7] Update chip-device-ctrl.py --- src/controller/python/chip-device-ctrl.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index 0cf87351592a59..dca43bc68130c6 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -73,6 +73,7 @@ elif sys.platform.startswith('linux'): from chip.ChipBluezMgr import BluezManager as BleManager + class StatusCodeEnum(Enum): SUCCESS = 0 FAILED = 1 @@ -82,7 +83,6 @@ class RPCResponseKeyEnum(Enum): RESULT = "result" ERROR = "error" - # The exceptions for CHIP Device Controller CLI class ChipDevCtrlException(exceptions.ChipStackException): @@ -744,7 +744,6 @@ def start_rpc_server(): with SimpleXMLRPCServer(("0.0.0.0", 5000), allow_none=True) as server: server.register_function(echo_alive) server.register_function(ble_scan) - server.register_function(resolve) server.register_function(ble_connect) server.register_function(ip_connect) server.register_function(zcl_add_network) From 159360083ccdcec10288d62d48d32bf30e098212 Mon Sep 17 00:00:00 2001 From: Xiaolei Zhou Date: Tue, 1 Jun 2021 12:02:50 -0700 Subject: [PATCH 7/7] Update chip-device-ctrl.py --- src/controller/python/chip-device-ctrl.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/controller/python/chip-device-ctrl.py b/src/controller/python/chip-device-ctrl.py index dca43bc68130c6..5709a01d51f71f 100755 --- a/src/controller/python/chip-device-ctrl.py +++ b/src/controller/python/chip-device-ctrl.py @@ -85,6 +85,7 @@ class RPCResponseKeyEnum(Enum): # The exceptions for CHIP Device Controller CLI + class ChipDevCtrlException(exceptions.ChipStackException): pass