//
// logremotehid.m
// logremotehid
//
// Created by yg on 5/24/18.
//
#import <Foundation/Foundation.h>
#import "HIDRemoteSimpleProtocol.h"
#include <os/log_private.h>
NSString * RemoteHIDCommandDataToString (uint8_t * data, size_t length);
NSString * RemoteHIDCommandTypeToString (uint32_t cmdType);
NSString * RemoteHIDCommandToString (HIDDeviceHeader * command);
NSString * RemoteHIDPacketToString (NSData * data);
NSString * RemoteHIDCommandDataToString (uint8_t * data, size_t length)
{
NSMutableString * dataStr = [[NSMutableString alloc] initWithCapacity:2048];
[dataStr appendString:@"["];
for (int i = 0; i < length; i++) {
[dataStr appendFormat:@"0x }
[dataStr appendString:@"]"];
return dataStr;
}
NSString * RemoteHIDCommandTypeToString (uint32_t cmdType)
{
NSString * cmdTypeStr = nil;
switch (cmdType) {
case HIDPacketTypeDeviceConnect:
cmdTypeStr= @"HIDPacketTypeDeviceConnect";
break;
case HIDPacketTypeDeviceDisconnect:
cmdTypeStr= @"HIDPacketTypeDeviceDisconnect";
break;
case HIDPacketTypeHandleReport:
cmdTypeStr= @"HIDPacketTypeHandleReport";
break;
case HIDPacketTypeSetReport:
cmdTypeStr= @"HIDPacketTypeSetReport";
break;
case HIDPacketTypeGetReport:
cmdTypeStr= @"HIDPacketTypeGetReport";
break;
default:
cmdTypeStr = [NSString stringWithFormat:@"Unknown ( break;
}
return cmdTypeStr;
}
NSString * RemoteHIDCommandToString (HIDDeviceHeader * command)
{
NSMutableString * cmdStr = [[NSMutableString alloc] initWithString:@""];
[cmdStr appendFormat:@"device: switch (command->packetType) {
case HIDPacketTypeDeviceConnect:
[cmdStr appendFormat:@"data: break;
case HIDPacketTypeHandleReport:
case HIDPacketTypeSetReport:
case HIDPacketTypeGetReport:
[cmdStr appendFormat:@"type: break;
default:
break;
}
return cmdStr;
}
NSString * RemoteHIDPacketToString (NSData * data)
{
NSMutableString * pktStr = [[NSMutableString alloc] initWithCapacity:4096];
require_action_quiet(data.length >= (sizeof(HIDTransportHeader) + sizeof(HIDDeviceHeader)), exit, pktStr = nil);
[pktStr appendFormat:@"{id:
HIDDeviceHeader * packet = (HIDDeviceHeader *) (data.bytes + sizeof(HIDTransportHeader));
do {
require_action_quiet((((uint8_t *)packet + sizeof(HIDDeviceHeader)) <= ((uint8_t *)data.bytes + data.length)) &&
(packet->length >= sizeof (HIDDeviceHeader)) &&
(((uint8_t *)packet + packet->length) <= ((uint8_t *)data.bytes + data.length)) , exit, pktStr = nil);
[pktStr appendFormat:@",{
packet = (HIDDeviceHeader *)((uint8_t *)packet + packet->length);
} while ((uint8_t *)packet < ((uint8_t *)data.bytes + data.length));
[pktStr appendString:@"}"];
exit:
return pktStr;
}
NSAttributedString *
OSLogCopyFormattedString(const char *type, id value, __unused os_log_type_info_t info) {
/*
* Decoder called when a custom format is specified
*/
NSAttributedString *decoded = nil;
NSData *data = (NSData *) value;
if (!strcmp(type, "packet")) {
decoded = [[NSMutableAttributedString alloc] initWithString:RemoteHIDPacketToString(data)];
} else if (!strcmp(type, "command")) {
decoded = [[NSMutableAttributedString alloc] initWithString:RemoteHIDCommandToString((HIDDeviceHeader*)data.bytes)];
} else {
decoded = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@" }
return decoded;
}