proto_lc.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. # Copyright 2018 Espressif Systems (Shanghai) PTE LTD
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. #
  15. from __future__ import print_function
  16. import os
  17. from future.utils import tobytes
  18. def _load_source(name, path):
  19. try:
  20. from importlib.machinery import SourceFileLoader
  21. return SourceFileLoader(name, path).load_module()
  22. except ImportError:
  23. # importlib.machinery doesn't exists in Python 2 so we will use imp (deprecated in Python 3)
  24. import imp
  25. return imp.load_source(name, path)
  26. idf_path = os.environ['IDF_PATH']
  27. constants_pb2 = _load_source('constants_pb2', idf_path + '/components/protocomm/python/constants_pb2.py')
  28. local_ctrl_pb2 = _load_source('esp_local_ctrl_pb2', idf_path + '/components/esp_local_ctrl/python/esp_local_ctrl_pb2.py')
  29. def get_prop_count_request(security_ctx):
  30. req = local_ctrl_pb2.LocalCtrlMessage()
  31. req.msg = local_ctrl_pb2.TypeCmdGetPropertyCount
  32. payload = local_ctrl_pb2.CmdGetPropertyCount()
  33. req.cmd_get_prop_count.MergeFrom(payload)
  34. enc_cmd = security_ctx.encrypt_data(req.SerializeToString())
  35. return enc_cmd
  36. def get_prop_count_response(security_ctx, response_data):
  37. decrypt = security_ctx.decrypt_data(tobytes(response_data))
  38. resp = local_ctrl_pb2.LocalCtrlMessage()
  39. resp.ParseFromString(decrypt)
  40. if (resp.resp_get_prop_count.status == 0):
  41. return resp.resp_get_prop_count.count
  42. else:
  43. return 0
  44. def get_prop_vals_request(security_ctx, indices):
  45. req = local_ctrl_pb2.LocalCtrlMessage()
  46. req.msg = local_ctrl_pb2.TypeCmdGetPropertyValues
  47. payload = local_ctrl_pb2.CmdGetPropertyValues()
  48. payload.indices.extend(indices)
  49. req.cmd_get_prop_vals.MergeFrom(payload)
  50. enc_cmd = security_ctx.encrypt_data(req.SerializeToString())
  51. return enc_cmd
  52. def get_prop_vals_response(security_ctx, response_data):
  53. decrypt = security_ctx.decrypt_data(tobytes(response_data))
  54. resp = local_ctrl_pb2.LocalCtrlMessage()
  55. resp.ParseFromString(decrypt)
  56. results = []
  57. if (resp.resp_get_prop_vals.status == 0):
  58. for prop in resp.resp_get_prop_vals.props:
  59. results += [{
  60. 'name': prop.name,
  61. 'type': prop.type,
  62. 'flags': prop.flags,
  63. 'value': tobytes(prop.value)
  64. }]
  65. return results
  66. def set_prop_vals_request(security_ctx, indices, values):
  67. req = local_ctrl_pb2.LocalCtrlMessage()
  68. req.msg = local_ctrl_pb2.TypeCmdSetPropertyValues
  69. payload = local_ctrl_pb2.CmdSetPropertyValues()
  70. for i, v in zip(indices, values):
  71. prop = payload.props.add()
  72. prop.index = i
  73. prop.value = v
  74. req.cmd_set_prop_vals.MergeFrom(payload)
  75. enc_cmd = security_ctx.encrypt_data(req.SerializeToString())
  76. return enc_cmd
  77. def set_prop_vals_response(security_ctx, response_data):
  78. decrypt = security_ctx.decrypt_data(tobytes(response_data))
  79. resp = local_ctrl_pb2.LocalCtrlMessage()
  80. resp.ParseFromString(decrypt)
  81. return (resp.resp_set_prop_vals.status == 0)