custom_prov.py 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. # APIs for interpreting and creating protobuf packets for `custom-config` protocomm endpoint
  16. from __future__ import print_function
  17. import proto
  18. import utils
  19. from future.utils import tobytes
  20. def print_verbose(security_ctx, data):
  21. if (security_ctx.verbose):
  22. print('++++ ' + data + ' ++++')
  23. def custom_config_request(security_ctx, info, version):
  24. # Form protobuf request packet from custom-config data
  25. cmd = proto.custom_config_pb2.CustomConfigRequest()
  26. cmd.info = tobytes(info)
  27. cmd.version = version
  28. enc_cmd = security_ctx.encrypt_data(cmd.SerializeToString()).decode('latin-1')
  29. print_verbose(security_ctx, 'Client -> Device (CustomConfig cmd) ' + utils.str_to_hexstr(enc_cmd))
  30. return enc_cmd
  31. def custom_config_response(security_ctx, response_data):
  32. # Interpret protobuf response packet
  33. decrypt = security_ctx.decrypt_data(tobytes(response_data))
  34. cmd_resp = proto.custom_config_pb2.CustomConfigResponse()
  35. cmd_resp.ParseFromString(decrypt)
  36. print_verbose(security_ctx, 'CustomConfig status ' + str(cmd_resp.status))
  37. return cmd_resp.status
  38. def custom_data_request(security_ctx, data):
  39. # Encrypt the custom data
  40. enc_cmd = security_ctx.encrypt_data(tobytes(data))
  41. print_verbose(security_ctx, 'Client -> Device (CustomData cmd) ' + utils.str_to_hexstr(enc_cmd))
  42. return enc_cmd
  43. def custom_data_response(security_ctx, response_data):
  44. # Decrypt response packet
  45. decrypt = security_ctx.decrypt_data(tobytes(response_data))
  46. print('CustomData response: ' + str(decrypt))
  47. return 0