// // SDLControlFramePayloadEndService.m // SmartDeviceLink-iOS // // Created by Joel Fischer on 7/24/17. // Copyright © 2017 smartdevicelink. All rights reserved. // #import "SDLControlFramePayloadEndService.h" #import "bson_object.h" #import "SDLControlFramePayloadConstants.h" NS_ASSUME_NONNULL_BEGIN @interface SDLControlFramePayloadEndService () @property (assign, nonatomic, readwrite) int32_t hashId; @end @implementation SDLControlFramePayloadEndService - (instancetype)initWithHashId:(int32_t)hashId { self = [super init]; if (!self) return nil; _hashId = hashId; return self; } - (instancetype)initWithData:(nullable NSData *)data { self = [super init]; if (!self) return nil; _hashId = SDLControlFrameInt32NotFound; if (data != nil) { [self sdl_parse:data]; } return self; } - (nullable NSData *)data { if (self.hashId == SDLControlFrameInt32NotFound) { return nil; } BsonObject payloadObject; bson_object_initialize_default(&payloadObject); if (self.hashId != SDLControlFrameInt32NotFound) { bson_object_put_int32(&payloadObject, SDLControlFrameHashIdKey, self.hashId); } BytePtr bsonData = bson_object_to_bytes(&payloadObject); NSUInteger length = bson_object_size(&payloadObject); bson_object_deinitialize(&payloadObject); return [[NSData alloc] initWithBytes:bsonData length:length]; } - (void)sdl_parse:(NSData *)data { BsonObject payloadObject = bson_object_from_bytes((BytePtr)data.bytes); self.hashId = bson_object_get_int32(&payloadObject, SDLControlFrameHashIdKey); bson_object_deinitialize(&payloadObject); } - (NSString *)description { return [NSString stringWithFormat:@"<%@>: hash id: %d", NSStringFromClass(self.class), self.hashId]; } @end NS_ASSUME_NONNULL_END