proto.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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():
  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. return req.SerializeToString()
  35. def get_prop_count_response(response_data):
  36. resp = local_ctrl_pb2.LocalCtrlMessage()
  37. resp.ParseFromString(tobytes(response_data))
  38. if (resp.resp_get_prop_count.status == 0):
  39. return resp.resp_get_prop_count.count
  40. else:
  41. return 0
  42. def get_prop_vals_request(indices):
  43. req = local_ctrl_pb2.LocalCtrlMessage()
  44. req.msg = local_ctrl_pb2.TypeCmdGetPropertyValues
  45. payload = local_ctrl_pb2.CmdGetPropertyValues()
  46. payload.indices.extend(indices)
  47. req.cmd_get_prop_vals.MergeFrom(payload)
  48. return req.SerializeToString()
  49. def get_prop_vals_response(response_data):
  50. resp = local_ctrl_pb2.LocalCtrlMessage()
  51. resp.ParseFromString(tobytes(response_data))
  52. results = []
  53. if (resp.resp_get_prop_vals.status == 0):
  54. for prop in resp.resp_get_prop_vals.props:
  55. results += [{
  56. 'name': prop.name,
  57. 'type': prop.type,
  58. 'flags': prop.flags,
  59. 'value': tobytes(prop.value)
  60. }]
  61. return results
  62. def set_prop_vals_request(indices, values):
  63. req = local_ctrl_pb2.LocalCtrlMessage()
  64. req.msg = local_ctrl_pb2.TypeCmdSetPropertyValues
  65. payload = local_ctrl_pb2.CmdSetPropertyValues()
  66. for i, v in zip(indices, values):
  67. prop = payload.props.add()
  68. prop.index = i
  69. prop.value = v
  70. req.cmd_set_prop_vals.MergeFrom(payload)
  71. return req.SerializeToString()
  72. def set_prop_vals_response(response_data):
  73. resp = local_ctrl_pb2.LocalCtrlMessage()
  74. resp.ParseFromString(tobytes(response_data))
  75. return (resp.resp_set_prop_vals.status == 0)