Subversion Repositories Projects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
805 - 1
// ///////////////////////////////////////////////////////////////////////////////
2
// Copyright (C) 2010, Frank Blumenberg
3
//
4
// See License.txt for complete licensing and attribution information.
5
// Permission is hereby granted, free of charge, to any person obtaining a copy
6
// of this software and associated documentation files (the "Software"), to deal
7
// in the Software without restriction, including without limitation the rights
8
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
// copies of the Software, and to permit persons to whom the Software is
10
// furnished to do so, subject to the following conditions:
11
//
12
// The above copyright notice and this permission notice shall be included in all
13
// copies or substantial portions of the Software.
14
//
15
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
// THE SOFTWARE.
22
//
23
// ///////////////////////////////////////////////////////////////////////////////
24
 
25
#import "NSData+MKPayloadDecode.h"
26
#import "MKDataConstants.h"
27
 
28
static const NSString * HardwareType[] = { @"Default", @"FlightCtrl", @"NaviCtrl", @"MK3Mag" };
29
 
30
@implementation NSData (MKPayloadDecode)
31
 
32
//-------------------------------------------------------------------------------------------
33
- (NSString *) debugLabelWithIndex:(int *)theIndex {
34
  const char * bytes = [self bytes];
35
 
36
  *theIndex = (int)bytes[0];
37
 
38
  int dataLength = [self length] < 16 ? [self length] : 16;
39
 
40
  NSData * strData = [NSData dataWithBytesNoCopy:(void*)(++bytes) length:dataLength freeWhenDone:NO];
41
  NSString * label = [[[NSString alloc] initWithData:strData encoding:NSASCIIStringEncoding] autorelease];
42
 
43
  return [label stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
44
}
45
 
46
- (NSDictionary *) decodeAnalogLabelResponseForAddress:(MKAddress)address;
47
{
48
  int index;
49
  NSString * label = [self debugLabelWithIndex:&index];
50
 
51
  return [NSDictionary dictionaryWithObjectsAndKeys:label, kMKDataKeyLabel, [NSNumber numberWithInt:index], kMKDataKeyIndex, nil];
52
}
53
 
54
//-------------------------------------------------------------------------------------------
55
- (NSDictionary *) decodeDebugDataResponseForAddress:(MKAddress)address;
56
{
57
  DebugOut * debugData = (DebugOut *)[self bytes];
58
 
59
  NSNumber* theAddress=[NSNumber numberWithInt:address];
60
 
61
  NSMutableArray * targetArray = [[NSMutableArray alloc] initWithCapacity:32];
62
  NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
63
 
64
  for (int i = 0; i < 32; i++)
65
  {
66
    NSNumber *number = [NSNumber numberWithShort:debugData->Analog[i]];
67
    [targetArray addObject:number];
68
  }
69
 
70
  [pool drain];
71
 
72
  NSDictionary* responseDictionary=[NSDictionary dictionaryWithObjectsAndKeys:targetArray, kMKDataKeyDebugData,
73
                                    theAddress, kMKDataKeyAddress, nil];
74
 
75
  [targetArray release];
76
 
77
  return responseDictionary;
78
}
79
 
80
//-------------------------------------------------------------------------------------------
81
- (NSDictionary *) decodeLcdMenuResponseForAddress:(MKAddress)address;
82
{
83
  const char * bytes = [self bytes];
84
 
85
  NSNumber* theAddress=[NSNumber numberWithInt:address];
86
  NSNumber * menuItem = [NSNumber numberWithChar:bytes[0]];
87
  NSNumber * maxMenuItem = [NSNumber numberWithChar:bytes[1]];
88
 
89
  NSData * strData = [NSData dataWithBytesNoCopy:(char *)(bytes + 2) length:[self length] - 2 freeWhenDone:NO];
90
  NSString * label = [[[NSString alloc] initWithData:strData encoding:NSASCIIStringEncoding] autorelease];
91
 
92
  NSMutableArray * menuRows = [[NSMutableArray alloc] init];
93
 
94
  [menuRows addObject:[label substringWithRange:NSMakeRange(0, 20)]];
95
  [menuRows addObject:[label substringWithRange:NSMakeRange(20, 20)]];
96
  [menuRows addObject:[label substringWithRange:NSMakeRange(40, 20)]];
97
  [menuRows addObject:[label substringWithRange:NSMakeRange(60, 20)]];
98
 
99
  NSDictionary* d =[NSDictionary dictionaryWithObjectsAndKeys:menuItem, kMKDataKeyMenuItem,
100
                    maxMenuItem, kMKDataKeyMaxMenuItem,
101
                    menuRows, kMKDataKeyMenuRows, theAddress, kMKDataKeyAddress, nil];
102
 
103
  [menuRows autorelease];
104
 
105
  return d;
106
}
107
 
108
//-------------------------------------------------------------------------------------------
109
- (NSDictionary *) decodeLcdResponseForAddress:(MKAddress)address;
110
{
111
  NSString * label = [[NSString alloc] initWithData:self encoding:NSASCIIStringEncoding];
112
  NSNumber* theAddress=[NSNumber numberWithInt:address];
113
 
114
  NSMutableArray * menuRows = [[NSMutableArray alloc] init];
115
 
116
  [menuRows addObject:[label substringWithRange:NSMakeRange(0, 20)]];
117
  [menuRows addObject:[label substringWithRange:NSMakeRange(20, 20)]];
118
  [menuRows addObject:[label substringWithRange:NSMakeRange(40, 20)]];
119
  [menuRows addObject:[label substringWithRange:NSMakeRange(60, 20)]];
120
 
121
  NSDictionary* d=[NSDictionary dictionaryWithObjectsAndKeys:menuRows, kMKDataKeyMenuRows,
122
                                                             theAddress, kMKDataKeyAddress, nil];
123
 
124
  [label release];
125
  [menuRows release];
126
 
127
  return d;
128
}
129
 
130
//-------------------------------------------------------------------------------------------
131
- (NSDictionary *) decodeVersionResponseForAddress:(MKAddress)address;
132
{
133
  const VersionInfo * version = [self bytes];
134
  NSNumber* theAddress=[NSNumber numberWithInt:address];
135
 
136
  NSString * versionStr = [NSString stringWithFormat:@"%@ %d.%d %c", 
137
                           HardwareType[address],
138
                           version->SWMajor,
139
                           version->SWMinor,
140
                           (version->SWPatch + 'a')];
141
 
142
  NSString * versionStrShort = [NSString stringWithFormat:@"%d.%d%c", 
143
                                version->SWMajor,
144
                                version->SWMinor,
145
                                (version->SWPatch + 'a')];
146
 
147
  return [NSDictionary dictionaryWithObjectsAndKeys:versionStr, kMKDataKeyVersion,
148
          versionStrShort, kMKDataKeyVersionShort, theAddress, kMKDataKeyAddress, nil];
149
}
150
 
151
- (NSDictionary *) decodeChannelsDataResponse {
152
 
153
  return [NSDictionary dictionaryWithObjectsAndKeys:self, kMKDataKeyChannels, nil];
154
 
155
}
156
 
157
 
158
- (NSDictionary *) decodeOsdResponse {
159
//  NSValue* value = [NSValue valueWithBytes:[self bytes]
160
//                                  objCType:@encode(NaviData_t)];
161
 
162
  return [NSDictionary dictionaryWithObjectsAndKeys:self, kMKDataKeyRawValue, nil];
163
}
164
 
165
@end