Skip to content

Commit

Permalink
Csg/feature/get pase data (#30)
Browse files Browse the repository at this point in the history
* Remove message lowercasing on rpc

* Remove redundant code

* Move the csg additions to above the main method, cannot forward declare the methods

* Remove message lowercasing on rpc

* Add debug prints and extra getpase command

* Add get pase session

* Initial PASE data fetch skeleton

* Add separate csg folder to hold constants and utils

* Add macros

* GetPaseData should return a yaml str.

* Add #defines, refactor into csg utilities, deserialize yaml

* Fix linter, comments, add copyright

* added get_pase_data RPC

* refactor methods and variables

* Add PASE Response parameters

* Debuggin additions

* Remove debugging additions

* Fix RPC to allow none values

* Remove pase from BLE call

* Added response, pake 1,2,3 parameters

* Remove merge artifact

* Remove unnecessary edits

* Add debug messages

* Remove debug messages

* Add todo

Co-authored-by: Mikael H. Moeller <[email protected]>
  • Loading branch information
2 people authored and doublemis1 committed Jun 28, 2021
1 parent 6b50e56 commit 7123b1d
Show file tree
Hide file tree
Showing 14 changed files with 285 additions and 7 deletions.
8 changes: 7 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,13 @@
"ratio": "cpp",
"set": "cpp",
"stack": "cpp",
"regex": "cpp"
"regex": "cpp",
"compare": "cpp",
"concepts": "cpp",
"numeric": "cpp",
"random": "cpp",
"ranges": "cpp",
"variant": "cpp"
},
"files.eol": "\n",
"editor.formatOnSave": true,
Expand Down
7 changes: 7 additions & 0 deletions src/controller/CHIPDeviceController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -811,6 +811,13 @@ CHIP_ERROR DeviceCommissioner::Shutdown()
return CHIP_NO_ERROR;
}

#if CHIP_CSG_TEST_HARNESS //CSG_TRACE_BEGIN
PASESession *DeviceCommissioner::GetPASESession()
{
return &mPairingSession;
}
#endif //CSG_TRACE_END

CHIP_ERROR DeviceCommissioner::PairDevice(NodeId remoteDeviceId, RendezvousParameters & params)
{
CHIP_ERROR err = CHIP_NO_ERROR;
Expand Down
3 changes: 3 additions & 0 deletions src/controller/CHIPDeviceController.h
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,9 @@ class DLL_EXPORT DeviceCommissioner : public DeviceController, public SessionEst

CHIP_ERROR Shutdown() override;

#if CHIP_CSG_TEST_HARNESS //CSG_TRACE_BEGON
PASESession *GetPASESession();
#endif //CSH_TRACE_END
// ----- Connection Management -----
/**
* @brief
Expand Down
16 changes: 16 additions & 0 deletions src/controller/python/ChipDeviceController-ScriptBinding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,9 @@ CHIP_ERROR
pychip_DeviceController_GetAddressAndPort(chip::Controller::DeviceCommissioner * devCtrl, chip::NodeId nodeId, char * outAddress,
uint64_t maxAddressLen, uint16_t * outPort);

#if CHIP_CSG_TEST_HARNESS //CSG_TRACE_BEGIN
const char * pychip_DeviceController_GetPASEData(chip::Controller::DeviceCommissioner * devCtrl);
#endif //CSG_TRACE_END
// Rendezvous
CHIP_ERROR pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator,
uint32_t setupPINCode, chip::NodeId nodeid);
Expand Down Expand Up @@ -210,6 +213,19 @@ void pychip_DeviceController_SetLogFilter(uint8_t category)
#endif
}

#if CHIP_CSG_TEST_HARNESS //CSG_TRACE_BEGIN
const char * pychip_DeviceController_GetPASEData(chip::Controller::DeviceCommissioner * devCtrl)
{
PASESession *pase_session = devCtrl->GetPASESession();
std::map<std::string, std::map<std::string, std::string>> *paseTrace = pase_session->getPASETrace();
/*
TODO: Fix the string return memory leak
https://github.com/chip-csg/connectedhomeip/issues/32
*/
return yaml_string_for_map(paseTrace);
}
#endif //CSG_TRACE_END

CHIP_ERROR pychip_DeviceController_ConnectBLE(chip::Controller::DeviceCommissioner * devCtrl, uint16_t discriminator,
uint32_t setupPINCode, chip::NodeId nodeid)
{
Expand Down
20 changes: 18 additions & 2 deletions src/controller/python/chip-device-ctrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -845,6 +845,17 @@ def qr_code_parse(qr_code):
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 get_pase_data() -> Dict[Any, Any]:
"""
This method will return valid data only after the ble_connect, ip_connect method has been called
"""
try:
__check_supported_os()
pase_data = device_manager.devCtrl.GetPASEData()
return __get_response_dict(status = StatusCodeEnum.SUCCESS, result = pase_data)
except Exception as e:
return __get_response_dict(status = StatusCodeEnum.FAILED, error = str(e))

Expand All @@ -860,6 +871,11 @@ def start_rpc_server():
server.register_function(zcl_add_network)
server.register_function(zcl_enable_network)
server.register_function(resolve)
<<<<<<< HEAD
=======
server.register_function(qr_code_parse)
server.register_function(get_pase_data)
>>>>>>> Csg/feature/get pase data (#30)
server.register_multicall_functions()
print('Serving XML-RPC on localhost port 5000')
try:
Expand All @@ -883,8 +899,8 @@ def __check_supported_os()-> bool:

def main():
start_rpc_server()
# Never reach here

# Never Executed: does not return here
optParser = OptionParser()
optParser.add_option(
"-r",
Expand Down
15 changes: 15 additions & 0 deletions src/controller/python/chip/ChipDeviceCtrl.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
from .exceptions import *
import enum

import yaml

__all__ = ["ChipDeviceController"]

Expand Down Expand Up @@ -164,6 +165,17 @@ def ConnectIP(self, ipaddr, setupPinCode, nodeid):
self.devCtrl, ipaddr, setupPinCode, nodeid)
)

def GetPASEData(self):
pase_yaml_str = self._ChipStack.Call(
lambda: self._dmLib.pychip_DeviceController_GetPASEData(self.devCtrl)
)
pase_dict = yaml.safe_load(pase_yaml_str)
if pase_dict is None:
print("ERROR: Failed to parse yaml data returned")
raise ValueError(f"Invalid PASE yaml string returned: {pase_yaml_str}")
return pase_dict


def ResolveNode(self, fabricid, nodeid):
return self._ChipStack.CallAsync(
lambda: self._dmLib.pychip_Resolver_ResolveNode(fabricid, nodeid)
Expand Down Expand Up @@ -280,6 +292,9 @@ def _InitLib(self):
c_void_p]
self._dmLib.pychip_DeviceController_DeleteDeviceController.restype = c_uint32

self._dmLib.pychip_DeviceController_GetPASEData.argtypes = [c_void_p]
self._dmLib.pychip_DeviceController_GetPASEData.restype = c_char_p

self._dmLib.pychip_DeviceController_ConnectBLE.argtypes = [
c_void_p, c_uint16, c_uint32, c_uint64]
self._dmLib.pychip_DeviceController_ConnectBLE.restype = c_uint32
Expand Down
15 changes: 11 additions & 4 deletions src/controller/python/test_client/rpc_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,26 @@ async def main():

# Create an RPC server
server_1 = xmlrpc.client.ServerProxy("http://localhost:5050/")

discriminator = 3840
pin_code = 20202021
node_id = 1234

# Invoke RPCs
try:
print("Calling RPCs")
print("echo_alive Response:" + server_1.echo_alive("Test"))
except:
destroy_container(container_1)
scan = server_1.ble_scan()
print(f"scan: {scan}")
connect = server_1.ble_connect(discriminator, pin_code, node_id)
print(f"connect: {connect}")
pase_data = server_1.get_pase_data()
print(f"pase_data: {pase_data}")
except Exception as e:
print(e)

# Cleanup
destroy_container(container_1)


if __name__ == "__main__":
loop = asyncio.get_event_loop()
loop.run_until_complete(main())
Expand Down
11 changes: 11 additions & 0 deletions src/csg_test_harness/BUILD.gn
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import("//build_overrides/chip.gni")

static_library("csg_test_harness") {

sources = [
"utilities.cpp",
"constants.h",
]

cflags = [ "-Wconversion" ]
}
51 changes: 51 additions & 0 deletions src/csg_test_harness/constants.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

#pragma once
#define CHIP_CSG_TEST_HARNESS 1

#define CHARS_PER_BYTE (uint16_t)2
#include <map>
#include <string>
#include <stdlib.h>

const char * yaml_string_for_map(std::map<std::string, std::map<std::string, std::string>> *trace_map);
std::string stringForDataBuffer(const uint8_t *start, uint16_t data_length);

// Message Keys
extern std::string messageFromInitiator_key;
extern std::string messageFromResponder_key;

// Request parameter keys
extern std::string PBKDFParamRequest_key;

// Response parameter keys
extern std::string PBKDFParamResponse_key;
extern std::string PBKDFParamResponse_salt_length_key;
extern std::string PBKDFParamResponse_iter_count_key;

// PAKE Contribution (PAKE 1)
extern std::string PAKE_1_key;
extern std::string PAKE_1_Pa_key;
extern std::string PAKE_1_key_id_key;

// PAKE Contrib. Verification (PAKE 2)
extern std::string PAKE_2_key;
extern std::string PAKE_2_encryption_id_key;

// PAKE Verification (PAKE 3)
extern std::string PAKE_3_key;
77 changes: 77 additions & 0 deletions src/csg_test_harness/utilities.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/*
*
* Copyright (c) 2021 Project CHIP Authors
*
* 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.
*/

#include "constants.h"
#include <string.h>
#include <iostream>

// Message Payload (Initiator/Responder) keys
std::string messageFromResponder_key ("MessageFromResponder");
std::string messageFromInitiator_key ("MessageFromInitiator");

// Request Parameter Keys
std::string PBKDFParamRequest_key ("PBKDFParamRequest");

// Response Parameter Keys
std::string PBKDFParamResponse_key ("PBKDFParamResponse");
std::string PBKDFParamResponse_salt_length_key ("SaltLength");
std::string PBKDFParamResponse_iter_count_key ("IterCount");

// PAKE Contribution (PAKE 1)
std::string PAKE_1_key ("PAKE1");
std::string PAKE_1_Pa_key ("PAKEPa");
std::string PAKE_1_key_id_key ("PAKEKeyID");

std::string PAKE_2_key ("PAKE2");
std::string PAKE_2_encryption_id_key ("EncryptionID");

std::string PAKE_3_key ("PAKE3");

const char * yaml_string_for_map(std::map<std::string, std::map<std::string, std::string>> *trace_map)
{
std::string result_yaml_str = "";
for (auto& x: *trace_map) {
// for each message
result_yaml_str += x.first;
result_yaml_str += ":\n";
for (auto& y: x.second) {
// for each message item
result_yaml_str += " "; // indent 2 spaces for nested hash
result_yaml_str += y.first;
result_yaml_str += ": ";
result_yaml_str += y.second;
result_yaml_str += "\n";
}
}

char * response = new char [result_yaml_str.length()+1];
strcpy(response, result_yaml_str.c_str());
return response;
}

std::string stringForDataBuffer(const uint8_t *start, uint16_t data_length)
{
uint16_t total_length = (uint16_t)((data_length * CHARS_PER_BYTE) + 1);
char * data_string_ptr = (char *)malloc(total_length * sizeof(char));
std::string data_string;
for (uint16_t i=0; i < data_length; i++) {
sprintf(data_string_ptr+i*2, "%02x", start[i]);
}
data_string_ptr[data_length*2] = '\0';
data_string = std::string(data_string_ptr);
return data_string;
}
1 change: 1 addition & 0 deletions src/lib/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ static_library("lib") {
"${chip_root}/src/setup_payload",
"${chip_root}/src/system",
"${chip_root}/src/transport",
"${chip_root}/src/csg_test_harness",
]

if (chip_build_libshell) {
Expand Down
1 change: 1 addition & 0 deletions src/lib/core/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ static_library("core") {
"${chip_root}/src/lib/support",
"${chip_root}/src/system",
"${nlio_root}:nlio",
"${chip_root}/src/csg_test_harness",
]

allow_circular_includes_from = [
Expand Down
Loading

0 comments on commit 7123b1d

Please sign in to comment.