FIRSESNanoPBHelpers.h 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. //
  2. // Copyright 2022 Google LLC
  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. #ifndef FIRSESNanoPBHelpers_h
  16. #define FIRSESNanoPBHelpers_h
  17. #import <Foundation/Foundation.h>
  18. #import <TargetConditionals.h>
  19. #if __has_include("CoreTelephony/CTTelephonyNetworkInfo.h") && !TARGET_OS_MACCATALYST && \
  20. !TARGET_OS_OSX && !TARGET_OS_TV
  21. #define TARGET_HAS_MOBILE_CONNECTIVITY
  22. #import <CoreTelephony/CTCarrier.h>
  23. #import <CoreTelephony/CTTelephonyNetworkInfo.h>
  24. #endif
  25. #import <nanopb/pb.h>
  26. #import <nanopb/pb_decode.h>
  27. #import <nanopb/pb_encode.h>
  28. NS_ASSUME_NONNULL_BEGIN
  29. /// Deinitializes a nanopb struct. Rewritten here to expose to Swift, since `pb_free` is a macro.
  30. void nanopb_free(void* _Nullable);
  31. /// Returns an error associated with the istream. Written in Objective-C because Swift does not
  32. /// support C language macros
  33. NSString* FIRSESPBGetError(pb_istream_t istream);
  34. // It seems impossible to specify the nullability of the `fields` parameter below,
  35. // yet the compiler complains that it's missing a nullability specifier. Google
  36. // yields no results at this time.
  37. #pragma clang diagnostic push
  38. #pragma clang diagnostic ignored "-Wnullability-completeness"
  39. NSData* _Nullable FIRSESEncodeProto(const pb_field_t fields[],
  40. const void* _Nonnull proto,
  41. NSError** error);
  42. #pragma clang diagnostic pop
  43. /// Callocs a pb_bytes_array and copies the given NSData bytes into the bytes array.
  44. /// @note Memory needs to be freed manually, through pb_free or pb_release.
  45. /// @param data The data to copy into the new bytes array.
  46. pb_bytes_array_t* _Nullable FIRSESEncodeData(NSData* _Nullable data);
  47. /// Callocs a pb_bytes_array and copies the given NSString's bytes into the bytes array.
  48. /// @note Memory needs to be freed manually, through pb_free or pb_release.
  49. /// @param string The string to encode as pb_bytes.
  50. pb_bytes_array_t* _Nullable FIRSESEncodeString(NSString* _Nullable string);
  51. /// Decodes an array of nanopb bytes into an NSData object
  52. /// @param pbData nanopb data
  53. NSData* FIRSESDecodeData(pb_bytes_array_t* pbData);
  54. /// Decodes an array of nanopb bytes into an NSString object
  55. /// @param pbData nanopb data
  56. NSString* FIRSESDecodeString(pb_bytes_array_t* pbData);
  57. /// Checks if 2 nanopb arrays are equal
  58. /// @param array array to check
  59. /// @param expected expected value of the array
  60. BOOL FIRSESIsPBArrayEqual(pb_bytes_array_t* _Nullable array, pb_bytes_array_t* _Nullable expected);
  61. /// Checks if a nanopb string is equal to an NSString
  62. /// @param pbString nanopb string to check
  63. /// @param str NSString that's expected
  64. BOOL FIRSESIsPBStringEqual(pb_bytes_array_t* _Nullable pbString, NSString* _Nullable str);
  65. /// Checks if a nanopb array is equal to NSData
  66. /// @param pbArray nanopb array to check
  67. /// @param data NSData that's expected
  68. BOOL FIRSESIsPBDataEqual(pb_bytes_array_t* _Nullable pbArray, NSData* _Nullable data);
  69. /// Returns the protobuf tag number. Use this to specify which oneof message type we are
  70. /// using for the platform\_info field. This function is required to be in Objective-C because
  71. /// Swift does not support c-style macros.
  72. pb_size_t FIRSESGetAppleApplicationInfoTag(void);
  73. /// Returns sysctl entry, useful for obtaining OS build version from the kernel. Copied from a
  74. /// private method in GULAppEnvironmentUtil.
  75. NSString* _Nullable FIRSESGetSysctlEntry(const char* sysctlKey);
  76. /// C function to bridge from Swift to do nanopb bytes transfer.
  77. NSData* FIRSESTransportBytes(const void* _Nonnull proto);
  78. NS_ASSUME_NONNULL_END
  79. #endif /* FIRSESNanoPBHelpers_h */