2023-12-15 00:36:37 -05:00
|
|
|
# SPDX-FileCopyrightText: 2018-2023 Espressif Systems (Shanghai) CO LTD
|
2022-07-13 08:18:15 -04:00
|
|
|
# SPDX-License-Identifier: Apache-2.0
|
2019-06-25 15:33:55 -04:00
|
|
|
#
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2022-07-13 08:18:15 -04:00
|
|
|
import importlib.util
|
2019-06-25 15:33:55 -04:00
|
|
|
import os
|
2022-07-13 08:18:15 -04:00
|
|
|
import sys
|
|
|
|
from importlib.abc import Loader
|
|
|
|
from typing import Any
|
2019-06-25 15:33:55 -04:00
|
|
|
|
2021-01-25 21:49:01 -05:00
|
|
|
|
2022-07-13 08:18:15 -04:00
|
|
|
def _load_source(name: str, path: str) -> Any:
|
|
|
|
spec = importlib.util.spec_from_file_location(name, path)
|
|
|
|
if not spec:
|
|
|
|
return None
|
2019-06-25 15:33:55 -04:00
|
|
|
|
2022-07-13 08:18:15 -04:00
|
|
|
module = importlib.util.module_from_spec(spec)
|
|
|
|
sys.modules[spec.name] = module
|
|
|
|
assert isinstance(spec.loader, Loader)
|
|
|
|
spec.loader.exec_module(module)
|
|
|
|
return module
|
2019-06-25 15:33:55 -04:00
|
|
|
|
|
|
|
|
|
|
|
idf_path = os.environ['IDF_PATH']
|
2021-01-25 21:49:01 -05:00
|
|
|
constants_pb2 = _load_source('constants_pb2', idf_path + '/components/protocomm/python/constants_pb2.py')
|
|
|
|
local_ctrl_pb2 = _load_source('esp_local_ctrl_pb2', idf_path + '/components/esp_local_ctrl/python/esp_local_ctrl_pb2.py')
|
2019-06-25 15:33:55 -04:00
|
|
|
|
|
|
|
|
2022-07-13 08:18:15 -04:00
|
|
|
def to_bytes(s: str) -> bytes:
|
|
|
|
return bytes(s, encoding='latin-1')
|
|
|
|
|
|
|
|
|
2021-05-31 01:35:21 -04:00
|
|
|
def get_prop_count_request(security_ctx):
|
2019-06-25 15:33:55 -04:00
|
|
|
req = local_ctrl_pb2.LocalCtrlMessage()
|
|
|
|
req.msg = local_ctrl_pb2.TypeCmdGetPropertyCount
|
|
|
|
payload = local_ctrl_pb2.CmdGetPropertyCount()
|
|
|
|
req.cmd_get_prop_count.MergeFrom(payload)
|
2021-05-31 01:35:21 -04:00
|
|
|
enc_cmd = security_ctx.encrypt_data(req.SerializeToString())
|
2023-12-15 00:36:37 -05:00
|
|
|
return enc_cmd.decode('latin-1')
|
2019-06-25 15:33:55 -04:00
|
|
|
|
|
|
|
|
2021-05-31 01:35:21 -04:00
|
|
|
def get_prop_count_response(security_ctx, response_data):
|
2022-07-13 08:18:15 -04:00
|
|
|
decrypt = security_ctx.decrypt_data(to_bytes(response_data))
|
2019-06-25 15:33:55 -04:00
|
|
|
resp = local_ctrl_pb2.LocalCtrlMessage()
|
2021-05-31 01:35:21 -04:00
|
|
|
resp.ParseFromString(decrypt)
|
2019-06-25 15:33:55 -04:00
|
|
|
if (resp.resp_get_prop_count.status == 0):
|
|
|
|
return resp.resp_get_prop_count.count
|
|
|
|
else:
|
|
|
|
return 0
|
|
|
|
|
|
|
|
|
2021-05-31 01:35:21 -04:00
|
|
|
def get_prop_vals_request(security_ctx, indices):
|
2019-06-25 15:33:55 -04:00
|
|
|
req = local_ctrl_pb2.LocalCtrlMessage()
|
|
|
|
req.msg = local_ctrl_pb2.TypeCmdGetPropertyValues
|
|
|
|
payload = local_ctrl_pb2.CmdGetPropertyValues()
|
|
|
|
payload.indices.extend(indices)
|
|
|
|
req.cmd_get_prop_vals.MergeFrom(payload)
|
2021-05-31 01:35:21 -04:00
|
|
|
enc_cmd = security_ctx.encrypt_data(req.SerializeToString())
|
2023-12-15 00:36:37 -05:00
|
|
|
return enc_cmd.decode('latin-1')
|
2019-06-25 15:33:55 -04:00
|
|
|
|
|
|
|
|
2021-05-31 01:35:21 -04:00
|
|
|
def get_prop_vals_response(security_ctx, response_data):
|
2022-07-13 08:18:15 -04:00
|
|
|
decrypt = security_ctx.decrypt_data(to_bytes(response_data))
|
2019-06-25 15:33:55 -04:00
|
|
|
resp = local_ctrl_pb2.LocalCtrlMessage()
|
2021-05-31 01:35:21 -04:00
|
|
|
resp.ParseFromString(decrypt)
|
2019-06-25 15:33:55 -04:00
|
|
|
results = []
|
|
|
|
if (resp.resp_get_prop_vals.status == 0):
|
|
|
|
for prop in resp.resp_get_prop_vals.props:
|
|
|
|
results += [{
|
2021-01-25 21:49:01 -05:00
|
|
|
'name': prop.name,
|
|
|
|
'type': prop.type,
|
|
|
|
'flags': prop.flags,
|
2022-07-13 08:18:15 -04:00
|
|
|
'value': prop.value
|
2019-06-25 15:33:55 -04:00
|
|
|
}]
|
|
|
|
return results
|
|
|
|
|
|
|
|
|
2021-05-31 01:35:21 -04:00
|
|
|
def set_prop_vals_request(security_ctx, indices, values):
|
2019-06-25 15:33:55 -04:00
|
|
|
req = local_ctrl_pb2.LocalCtrlMessage()
|
|
|
|
req.msg = local_ctrl_pb2.TypeCmdSetPropertyValues
|
|
|
|
payload = local_ctrl_pb2.CmdSetPropertyValues()
|
|
|
|
for i, v in zip(indices, values):
|
|
|
|
prop = payload.props.add()
|
|
|
|
prop.index = i
|
|
|
|
prop.value = v
|
|
|
|
req.cmd_set_prop_vals.MergeFrom(payload)
|
2021-05-31 01:35:21 -04:00
|
|
|
enc_cmd = security_ctx.encrypt_data(req.SerializeToString())
|
2023-12-15 00:36:37 -05:00
|
|
|
return enc_cmd.decode('latin-1')
|
2019-06-25 15:33:55 -04:00
|
|
|
|
|
|
|
|
2021-05-31 01:35:21 -04:00
|
|
|
def set_prop_vals_response(security_ctx, response_data):
|
2022-07-13 08:18:15 -04:00
|
|
|
decrypt = security_ctx.decrypt_data(to_bytes(response_data))
|
2019-06-25 15:33:55 -04:00
|
|
|
resp = local_ctrl_pb2.LocalCtrlMessage()
|
2021-05-31 01:35:21 -04:00
|
|
|
resp.ParseFromString(decrypt)
|
2019-06-25 15:33:55 -04:00
|
|
|
return (resp.resp_set_prop_vals.status == 0)
|