#include <unistd.h> in UIElements.m too.
[MacTF.git] / TFUSBControllerCommunicationBlock.m
blob1386fe4c4655d30659c7eb0f1e4e47525e61f526
1 //
2 //  TFUSBControllerCommunicationBlock.h
3 //  MacTF
4 //
5 //  Created by Kalle Olavi Niemitalo on 2007-10-23, based on:
6 //    TFUSBController.m
7 //    MacTF
8 //    Created by Nathan Oates on Sun Aug 01 2004.
9 //    Copyright (c) 2004-7 Nathan Oates nathan@noates.com All rights reserved.
10 //  May also include parts of:
11 //    uproar 0.1
12 //    Copyright (c) 2001 Kasima Tharnpipitchai <me@kasima.org>
14 // This source code is free software; you can redistribute it and/or
15 // modify it under the terms of the GNU Public License as published
16 // by the Free Software Foundation; either version 2 of the License,
17 // or (at your option) any later version.
19 // This source code is distributed in the hope that it will be useful,
20 // but WITHOUT ANY WARRANTY; without even the implied warranty of
21 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 // Please refer to the GNU Public License for more details.
24 // You should have received a copy of the GNU Public License along with
25 // this source code; if not, write to:
26 // Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 #import "TFUSBControllerCommunicationBlock.h"
30 @implementation TFUSBController (CommunicationBlock)
32 // ---------------------------------------------------------------------------
33 // Framing of communication blocks
34 // ---------------------------------------------------------------------------
36 - (UInt16) findCRC:(const UInt8*)p length:(size_t)n
38         UInt16 m_crc16 = 0x0000;
39         static const UInt16 crc16Tbl[256] = {
40                 0x0000, 0xc0c1, 0xc181, 0x0140, 0xc301, 0x03c0, 0x0280, 0xc241,
41                 0xc601, 0x06c0, 0x0780, 0xc741, 0x0500, 0xc5c1, 0xc481, 0x0440,
42                 0xcc01, 0x0cc0, 0x0d80, 0xcd41, 0x0f00, 0xcfc1, 0xce81, 0x0e40,
43                 0x0a00, 0xcac1, 0xcb81, 0x0b40, 0xc901, 0x09c0, 0x0880, 0xc841,
44                 0xd801, 0x18c0, 0x1980, 0xd941, 0x1b00, 0xdbc1, 0xda81, 0x1a40,
45                 0x1e00, 0xdec1, 0xdf81, 0x1f40, 0xdd01, 0x1dc0, 0x1c80, 0xdc41,
46                 0x1400, 0xd4c1, 0xd581, 0x1540, 0xd701, 0x17c0, 0x1680, 0xd641,
47                 0xd201, 0x12c0, 0x1380, 0xd341, 0x1100, 0xd1c1, 0xd081, 0x1040,
48                 0xf001, 0x30c0, 0x3180, 0xf141, 0x3300, 0xf3c1, 0xf281, 0x3240,
49                 0x3600, 0xf6c1, 0xf781, 0x3740, 0xf501, 0x35c0, 0x3480, 0xf441,
50                 0x3c00, 0xfcc1, 0xfd81, 0x3d40, 0xff01, 0x3fc0, 0x3e80, 0xfe41,
51                 0xfa01, 0x3ac0, 0x3b80, 0xfb41, 0x3900, 0xf9c1, 0xf881, 0x3840,
52                 0x2800, 0xe8c1, 0xe981, 0x2940, 0xeb01, 0x2bc0, 0x2a80, 0xea41,
53                 0xee01, 0x2ec0, 0x2f80, 0xef41, 0x2d00, 0xedc1, 0xec81, 0x2c40,
54                 0xe401, 0x24c0, 0x2580, 0xe541, 0x2700, 0xe7c1, 0xe681, 0x2640,
55                 0x2200, 0xe2c1, 0xe381, 0x2340, 0xe101, 0x21c0, 0x2080, 0xe041,
56                 0xa001, 0x60c0, 0x6180, 0xa141, 0x6300, 0xa3c1, 0xa281, 0x6240,
57                 0x6600, 0xa6c1, 0xa781, 0x6740, 0xa501, 0x65c0, 0x6480, 0xa441,
58                 0x6c00, 0xacc1, 0xad81, 0x6d40, 0xaf01, 0x6fc0, 0x6e80, 0xae41,
59                 0xaa01, 0x6ac0, 0x6b80, 0xab41, 0x6900, 0xa9c1, 0xa881, 0x6840,
60                 0x7800, 0xb8c1, 0xb981, 0x7940, 0xbb01, 0x7bc0, 0x7a80, 0xba41,
61                 0xbe01, 0x7ec0, 0x7f80, 0xbf41, 0x7d00, 0xbdc1, 0xbc81, 0x7c40,
62                 0xb401, 0x74c0, 0x7580, 0xb541, 0x7700, 0xb7c1, 0xb681, 0x7640,
63                 0x7200, 0xb2c1, 0xb381, 0x7340, 0xb101, 0x71c0, 0x7080, 0xb041,
64                 0x5000, 0x90c1, 0x9181, 0x5140, 0x9301, 0x53c0, 0x5280, 0x9241,
65                 0x9601, 0x56c0, 0x5780, 0x9741, 0x5500, 0x95c1, 0x9481, 0x5440,
66                 0x9c01, 0x5cc0, 0x5d80, 0x9d41, 0x5f00, 0x9fc1, 0x9e81, 0x5e40,
67                 0x5a00, 0x9ac1, 0x9b81, 0x5b40, 0x9901, 0x59c0, 0x5880, 0x9841,
68                 0x8801, 0x48c0, 0x4980, 0x8941, 0x4b00, 0x8bc1, 0x8a81, 0x4a40,
69                 0x4e00, 0x8ec1, 0x8f81, 0x4f40, 0x8d01, 0x4dc0, 0x4c80, 0x8c41,
70                 0x4400, 0x84c1, 0x8581, 0x4540, 0x8701, 0x47c0, 0x4680, 0x8641,
71                 0x8201, 0x42c0, 0x4380, 0x8341, 0x4100, 0x81c1, 0x8081, 0x4040,
72         };
73         size_t i;       
74         for(i = 0; i < n; i++)
75                         m_crc16 = (m_crc16 >> 8) ^ crc16Tbl[*p++ ^ (m_crc16 & 0xFF)];
76         return m_crc16;
79 - (NSMutableData*) swap:(NSData*)inData {
80         size_t len = [inData length];
81         size_t uneven = 0;
82         if (len % 2 != 0) {
83                 uneven = 1; // prepare space for padding
84         }
85         NSMutableData* outData = [NSMutableData dataWithLength:len+uneven];
86         swab([inData bytes], [outData mutableBytes], len-uneven);
87         if (uneven) {
88                 // swab() requires an even length.  The last byte was not given to
89                 // swab() above, so copy it separately.  Also clear the byte just
90                 // before the last one, even though the dataWithLength: class method
91                 // should have already cleared it.
92                 unsigned char final[2] = { 0, 0 };
93                 [inData getBytes:&final[1] range:(NSRange){len-1,1}];
94                 [outData replaceBytesInRange:(NSRange){len-1,2} withBytes:final];
95         }
96         return outData;
99 - (NSData*) prepareCommand:(UInt32)cmd withData:(NSData*)inData {
100         NSMutableData* block = [NSMutableData dataWithLength:8];
101         if (inData != nil)
102                 [block appendData:inData];
103         size_t blockLength = [block length];
104         if (blockLength > 0xFFFF)
105                 return nil; // wouldn't fit in UInt16
107         UInt32 cmd_bigendian = EndianU32_NtoB(cmd);
108         [block replaceBytesInRange:(NSRange){4,4} withBytes:&cmd_bigendian];
110         UInt16 crc = [self findCRC:(const UInt8*)[block bytes]+4 length:blockLength-4];
111         UInt16 crc_bigendian = EndianU16_NtoB(crc);
112         [block replaceBytesInRange:(NSRange){2,2} withBytes:&crc_bigendian];
114         UInt16 blockLength_bigendian = EndianU16_NtoB(blockLength);
115         [block replaceBytesInRange:(NSRange){0,2} withBytes:&blockLength_bigendian];
117         return [self swap:block];
120 // Check the size and CRC of a received communication block.
121 // Return an ECODE to be sent back, or USB_OK.
122 - (UInt32) checkCommunicationBlock:(NSData*)block {
123         if (block == nil) return USB_Err_RunFail;
124         size_t length = [block length];
125         if (length < 8) return USB_Err_Size;
126         UInt16 crc16_bigendian;
127         [block getBytes:&crc16_bigendian range:(NSRange){2,2}];
128         UInt16 crc = EndianU16_BtoN(crc16_bigendian);
129         UInt16 calcCrc = [self findCRC:(const UInt8*)[block bytes]+4 length:length-4];
130         if (crc != calcCrc) return USB_Err_Crc;
131         return USB_OK;
134 - (UInt32) cmdFromCommunicationBlock:(NSData*)block {
135         UInt32 cmd_bigendian;
136         [block getBytes:&cmd_bigendian range:(NSRange){4,4}];
137         return EndianU32_BtoN(cmd_bigendian);
140 - (BOOL) appendToData:(NSMutableData*)data fname:(NSString*)fname {
141         NSData* fnameData = [fname dataUsingEncoding:NSISOLatin1StringEncoding];
142         if (fnameData == nil) // fname contains characters unsupported by encoding
143                 return NO;
144         const size_t terminatorSize = 1;
145         [data appendData:fnameData];
146         [data increaseLengthBy:terminatorSize]; // fills with 0x00
147         return YES;
150 - (BOOL) appendToData:(NSMutableData*)data sizeAndFname:(NSString*)fname {
151         NSData* fnameData = [fname dataUsingEncoding:NSISOLatin1StringEncoding];
152         if (fnameData == nil) // fname contains characters unsupported by encoding
153                 return NO;
154         const size_t terminatorSize = 1;
155         const size_t nameSize = [fnameData length] + terminatorSize;
156         if (nameSize > 0xFFFF) // size would not fit in UInt16
157                 return NO;
158         const UInt16 nameSize_bigendian = EndianU16_NtoB(nameSize);
159         [data appendBytes:&nameSize_bigendian length:2];
160         [data appendData:fnameData];
161         [data increaseLengthBy:terminatorSize]; // fills with 0x00
162         return YES;
165 // ---------------------------------------------------------------------------
166 // Preparing communication blocks for various commands
167 // ---------------------------------------------------------------------------
169 - (NSData*) prepareFailWithECode:(UInt32)eCode {
170         const UInt32 eCode_bigendian = EndianU32_NtoB(eCode);
171         NSData* data = [NSData dataWithBytes:&eCode_bigendian length:4];
172         if (data == nil) // out of memory presumably
173                 return nil;
174         return [self prepareCommand:USB_Fail withData:data];
177 - (NSData*) prepareSuccess {
178         return [self prepareCommand:USB_Success withData:nil];
181 - (NSData*) prepareCancel {
182         return [self prepareCommand:USB_Cancel withData:nil];
185 // Prepare a communication block that asks the Toppy to list the files
186 // in a directory.  Return the communication block, or nil on error.
187 - (NSData*) prepareCmdHddDirWithPath:(NSString*)path {
188         NSMutableData* cmdData = [NSMutableData data];
189         if (cmdData == nil) // out of memory presumably
190                 return nil;
191         if (![self appendToData:cmdData fname:path])
192                 return nil;
193         return [self prepareCommand:USB_CmdHddDir withData:cmdData];
196 - (NSData*) prepareCmdHddFileSendWithDirection:(UInt8)direction
197                 fname:(NSString*)fname offset:(UInt64)offset
199         NSMutableData *cmdData = [NSMutableData dataWithBytes:&direction length:1];
200         if (cmdData == nil) // out of memory presumably
201                 return nil;
202         if (![self appendToData:cmdData sizeAndFname:fname])
203                 return nil;
204         const UInt64 offset_bigendian = EndianU64_NtoB(offset);
205         [cmdData appendBytes:&offset_bigendian length:8];
206         return [self prepareCommand:USB_CmdHddFileSend withData:cmdData];
209 - (NSData*) prepareDataHddFileEnd {
210         return [self prepareCommand:USB_DataHddFileEnd withData:nil];
213 - (NSData*) prepareCmdTurboWithMode:(SInt32)mode {
214         const SInt32 mode_bigendian = EndianS32_NtoB(mode);
215         NSData* cmdData = [NSData dataWithBytes:&mode_bigendian length:4];
216         if (cmdData == nil) // out of memory presumably
217                 return nil;
218         return [self prepareCommand:USB_CmdTurbo withData:cmdData];
221 - (NSData*) prepareCmdHddDelWithFname:(NSString*)fname {
222         NSMutableData* cmdData = [NSMutableData data];
223         if (cmdData == nil) // out of memory presumably
224                 return nil;
225         if (![self appendToData:cmdData fname:fname])
226                 return nil;
227         return [self prepareCommand:USB_CmdHddDel withData:cmdData];
230 - (NSData*) prepareCmdHddRenameFromFname:(NSString*)oldFname toFname:(NSString*)newFname {
231         NSMutableData* cmdData = [NSMutableData data];
232         if (cmdData == nil) // out of memory presumably
233                 return nil;
234         if (![self appendToData:cmdData sizeAndFname:oldFname])
235                 return nil;
236         if (![self appendToData:cmdData sizeAndFname:newFname])
237                 return nil;
238         return [self prepareCommand:USB_CmdHddRename withData:cmdData];
241 - (NSData*) prepareCmdHddCreateDirWithFname:(NSString*)fname {
242         NSMutableData* cmdData = [NSMutableData data];
243         if (cmdData == nil) // out of memory presumably
244                 return nil;
245         if (![self appendToData:cmdData fname:fname])
246                 return nil;
247         return [self prepareCommand:USB_CmdHddCreateDir withData:cmdData];
250 - (NSString*) fnameForFile:(NSString*)file atPath:(NSString*)path {
251         return [NSString stringWithFormat:@"%@\\%@", path, file];
254 @end