host_protocol_common.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (C) 2017 The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "chre/platform/shared/host_protocol_common.h"
  17. #include <string.h>
  18. #include "chre/platform/shared/host_messages_generated.h"
  19. using flatbuffers::FlatBufferBuilder;
  20. using flatbuffers::Offset;
  21. using flatbuffers::Vector;
  22. namespace chre {
  23. void HostProtocolCommon::encodeNanoappMessage(
  24. FlatBufferBuilder& builder, uint64_t appId, uint32_t messageType,
  25. uint16_t hostEndpoint, const void *messageData, size_t messageDataLen) {
  26. auto messageDataOffset = builder.CreateVector(
  27. static_cast<const uint8_t *>(messageData), messageDataLen);
  28. auto nanoappMessage = fbs::CreateNanoappMessage(
  29. builder, appId, messageType, hostEndpoint, messageDataOffset);
  30. finalize(builder, fbs::ChreMessage::NanoappMessage, nanoappMessage.Union());
  31. }
  32. Offset<Vector<int8_t>> HostProtocolCommon::addStringAsByteVector(
  33. FlatBufferBuilder& builder, const char *str) {
  34. return builder.CreateVector(reinterpret_cast<const int8_t *>(str),
  35. strlen(str) + 1);
  36. }
  37. void HostProtocolCommon::finalize(
  38. FlatBufferBuilder& builder, fbs::ChreMessage messageType,
  39. flatbuffers::Offset<void> message, uint16_t hostClientId) {
  40. fbs::HostAddress hostAddr(hostClientId);
  41. auto container = fbs::CreateMessageContainer(
  42. builder, messageType, message, &hostAddr);
  43. builder.Finish(container);
  44. }
  45. bool HostProtocolCommon::verifyMessage(const void *message, size_t messageLen) {
  46. bool valid = false;
  47. if (message != nullptr) {
  48. flatbuffers::Verifier verifier(static_cast<const uint8_t *>(message),
  49. messageLen);
  50. valid = fbs::VerifyMessageContainerBuffer(verifier);
  51. }
  52. return valid;
  53. }
  54. } // namespace chre