1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747
|
/** Implementation of DKMethod class for encapsulating D-Bus methods.
Copyright (C) 2010 Free Software Foundation, Inc.
Written by: Niels Grewe <niels.grewe@halbordnung.de>
Created: June 2010
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02111 USA.
<title>DKMethod class reference</title>
*/
#import <Foundation/NSArray.h>
#import <Foundation/NSDebug.h>
#import <Foundation/NSDictionary.h>
#import <Foundation/NSException.h>
#import <Foundation/NSInvocation.h>
#import <Foundation/NSMethodSignature.h>
#import <Foundation/NSNull.h>
#import <Foundation/NSString.h>
#import "DKArgument.h"
#import "DKMethod.h"
#import "DKProxy+Private.h"
#include <dbus/dbus.h>
#include <stdint.h>
#include <string.h>
@implementation DKMethod
- (id) initWithName: (NSString*)aName
parent: (id)aParent
{
if (nil == (self = [super initWithName: aName
parent: aParent]))
{
return nil;
}
if (0 == [name length])
{
[self release];
return nil;
}
inArgs = [NSMutableArray new];
outArgs = [NSMutableArray new];
return self;
}
- (const char*) returnTypeBoxed: (BOOL)doBox
{
NSUInteger count = [outArgs count];
if (count == 0)
{
// No return value, void method.
return @encode(void);
}
else if ((count == 1) && (NO == doBox))
{
// One argument, and we don't want boxing
return [(DKArgument*)[outArgs objectAtIndex: 0] unboxedObjCTypeChar];
}
else
{
// Multiple return value, or we want boxing anyhow.
return @encode(id);
}
}
- (const char*) argumentTypeAtIndex: (NSUInteger)index
boxed: (BOOL)doBox
{
if (YES == doBox)
{
return @encode(id);
}
else if (index < [inArgs count])
{
return [[inArgs objectAtIndex: index] unboxedObjCTypeChar];
}
return NULL;
}
- (BOOL) isEqualToMethodSignature: (NSMethodSignature*)methodSignature
boxed: (BOOL)isBoxed
{
return [methodSignature isEqual: [self methodSignatureBoxed: isBoxed]];
}
- (NSInteger)boxingStateForArgumentAtIndex: (NSUInteger)argIndex
fromMethodSignature: (NSMethodSignature*)aSignature
atIndex: (NSUInteger)sigIndex
{
NSUInteger argCount = [inArgs count];
if (argIndex < argCount)
{
const char* typeFromSig = [aSignature getArgumentTypeAtIndex: sigIndex];
const char* boxedType = @encode(id);
const char *unboxedTypeFromDBus = [[inArgs objectAtIndex: argIndex] unboxedObjCTypeChar];
BOOL boxedMatch = NO;
BOOL unboxedMatch = NO;
if ((typeFromSig == NULL) || (unboxedTypeFromDBus == NULL))
{
return DK_ARGUMENT_INVALID;
}
boxedMatch = (0 == strcmp(typeFromSig, boxedType));
if (NO == boxedMatch)
{
unboxedMatch = (0 == strcmp(typeFromSig, unboxedTypeFromDBus));
if (unboxedMatch)
{
return DK_ARGUMENT_UNBOXED;
}
}
else
{
return DK_ARGUMENT_BOXED;
}
}
return DK_ARGUMENT_INVALID;
}
- (NSInteger)boxingStateForArgumentAtIndex: (NSUInteger)argIndex
fromMethodSignature: (NSMethodSignature*)aSignature
{
// Add an offest to accomodate self and _cmd
return [self boxingStateForArgumentAtIndex: argIndex
fromMethodSignature: aSignature
atIndex: (argIndex + 2)];
}
- (NSInteger)boxingStateForReturnValueFromMethodSignature: (NSMethodSignature*)aSignature
{
const char* sigReturn = [aSignature methodReturnType];
BOOL boxedReturnMatch = (0 == strcmp(sigReturn, [self returnTypeBoxed: YES]));
BOOL unboxedReturnMatch = NO;
if (boxedReturnMatch)
{
return DK_ARGUMENT_BOXED;
}
unboxedReturnMatch = (0 == strcmp(sigReturn, [self returnTypeBoxed: NO]));
if (unboxedReturnMatch)
{
return DK_ARGUMENT_UNBOXED;
}
return DK_ARGUMENT_INVALID;
}
/**
* Checks whether it is valid to use the receiver to handle an invocation with
* the specified method signature, no matter whether the boxed or non-boxed
* version of an argument is used.
*/
- (BOOL) isValidForMethodSignature: (NSMethodSignature*)aSignature
{
NSUInteger argIndex = 0;
NSUInteger argCount = [inArgs count];
// Subtract 2 to account for self and _cmd in the NSMethodSignature.
if (argCount != ([aSignature numberOfArguments] - 2))
{
return NO;
}
if (DK_ARGUMENT_INVALID == [self boxingStateForReturnValueFromMethodSignature: aSignature])
{
return NO;
}
while (argIndex < argCount)
{
NSInteger boxingState = [self boxingStateForArgumentAtIndex: argIndex
fromMethodSignature: aSignature];
if (DK_ARGUMENT_INVALID == boxingState)
{
return NO;
}
argIndex++;
}
// We passed all checks and can use the method for the given signature:
return YES;
}
- (const char*)objCTypesBoxed: (BOOL)doBox
{
/* Type-encodings are as follows:
* <return-type><arg-frame length><type/offset pairs>
* Nothing uses the frame length/offset information, though. So we can have a
* less paranoid stance on the offsets and sizes and spare ourselves the work
* of generating them.
*/
// Initial type string containing self and _cmd.
NSMutableString *typeString = [[NSMutableString alloc] initWithFormat: @"@0:%d", sizeof(id)];
NSUInteger offset = sizeof(id) + sizeof(SEL);
NSString *returnValue = nil;
NSEnumerator *en = [inArgs objectEnumerator];
DKArgument *arg = nil;
while (nil != (arg = [en nextObject]))
{
const char *typeChar;
if (doBox)
{
typeChar = @encode(id);
}
else
{
typeChar = [arg unboxedObjCTypeChar];
}
[typeString appendFormat: @"%s%d", typeChar, offset];
if (doBox)
{
offset = offset + sizeof(id);
}
else
{
offset = offset + [arg unboxedObjCTypeSize];
}
}
returnValue = [NSString stringWithFormat: @"%s%d%@", [self returnTypeBoxed: doBox],
offset,
typeString];
[typeString release];
NSDebugMLog(@"Generated Obj-C type string: %@", returnValue);
return [returnValue UTF8String];
}
- (NSMethodSignature*) methodSignatureBoxed: (BOOL)doBox
{
return [NSMethodSignature signatureWithObjCTypes: [self objCTypesBoxed: doBox]];
}
- (NSMethodSignature*) methodSignature
{
return [self methodSignatureBoxed: YES];
}
- (DKArgument*)DKArgumentAtIndex: (NSInteger)index
{
NSArray *args = nil;
if (index < 0)
{
args = outArgs;
// Convert to positive integer:
index *= -1;
// Decrement to start with 0:
index--;
}
else
{
args = inArgs;
}
if (index < [args count])
{
return [args objectAtIndex: index];
}
return nil;
}
- (void)addArgument: (DKArgument*)argument
direction: (NSString*)direction
{
if (nil == argument)
{
NSDebugMLog(@"Ignoring nil argument");
return;
}
if ((direction == nil) || [direction isEqualToString: kDKArgumentDirectionIn])
{
[inArgs addObject: argument];
}
else if ([direction isEqualToString: kDKArgumentDirectionOut])
{
[outArgs addObject: argument];
}
else
{
NSDebugMLog(@"Ignoring argument with unknown direction '%@'.", direction);
}
}
- (NSString*) interface
{
if ([parent respondsToSelector: @selector(name)])
{
return [parent name];
}
return nil;
}
- (BOOL) isDeprecated
{
return [[annotations valueForKey: @"org.freedesktop.DBus.Deprecated"] isEqualToString: @"true"];
}
- (BOOL) isOneway
{
return [[annotations valueForKey: @"org.freedesktop.DBus.Method.NoReply"] isEqualToString: @"true"];
}
- (void) unmarshallReturnValueFromIterator: (DBusMessageIter*)iter
intoInvocation: (NSInvocation*)inv
{
NSUInteger numArgs = [outArgs count];
NSMethodSignature *sig = [inv methodSignature];
BOOL doBox = YES;
NSInteger boxingState = [self boxingStateForReturnValueFromMethodSignature: sig];
// Make sure the return value is boxable
NSAssert1((DK_ARGUMENT_INVALID != boxingState),
@"The return value cannot be boxed into invocation with signature %@.",
sig);
// If it is not DK_ARGUMENT_INVALID, it leaves 0 and 1 as possible states:
doBox = (BOOL)boxingState;
if (0 == numArgs)
{
// Void return type, we retrun.
return;
}
else if (1 == numArgs)
{
// Pass the iterator and the invocation to the argument, index -1 indicates
// the return value.
[[outArgs objectAtIndex: 0] unmarshallFromIterator: iter
intoInvocation: inv
atIndex: -1
boxing: doBox];
}
else
{
NSMutableArray *returnValues = [NSMutableArray array];
NSUInteger index = 0;
NSNull *theNull = [NSNull null];
while (index < numArgs)
{
// We can only support objects here, so we always get the boxed value
id object = [[outArgs objectAtIndex: index] unmarshalledObjectFromIterator: iter];
// Do not try to add nil objects
if (nil == object)
{
object = theNull;
}
[returnValues addObject: object];
/*
* Proceed to the next value in the message, but raise an exception if
* we are missing some.
*/
if (NO == (BOOL)dbus_message_iter_next(iter))
{
[NSException raise: @"DKMethodUnmarshallingException"
format: @"D-Bus message too short when unmarshalling return value for '%@'.",
name];
}
index++;
}
[inv setReturnValue: &returnValues];
}
}
- (void) marshallReturnValueFromInvocation: (NSInvocation*)inv
intoIterator: (DBusMessageIter*)iter
{
NSUInteger numArgs = [outArgs count];
NSMethodSignature *sig = [inv methodSignature];
BOOL doBox = YES;
NSInteger boxingState = [self boxingStateForReturnValueFromMethodSignature: sig];
// Make sure the return value is boxable
NSAssert1(DK_ARGUMENT_INVALID != boxingState,
@"The return value cannot be boxed into invocation with signature %@.",
sig);
// If it is not DK_ARGUMENT_INVALID, it leaves 0 and 1 as possible states:
doBox = (BOOL)boxingState;
if (0 == numArgs)
{
return;
}
else if (1 == numArgs)
{
[[outArgs objectAtIndex: 0] marshallArgumentAtIndex: -1
fromInvocation: inv
intoIterator: iter
boxing: doBox];
}
else
{
/*
* For D-Bus methods with multiple out-direction arguments
* the caller will have stored the individual values as objects in an
* array.
*/
NSArray *retVal = nil;
NSUInteger retCount = 0;
NSInteger index = 0;
// Make sure the method did return an object:
NSAssert2((0 == strcmp(@encode(id), [sig methodReturnType])),
@"Invalid return value when constucting D-Bus reply for '%@' on %@",
NSStringFromSelector([inv selector]),
[inv target]);
[inv getReturnValue: &retVal];
// Make sure that it responds to the needed selectors:
NSAssert2(([retVal respondsToSelector: @selector(objectAtIndex:)]
&& [retVal respondsToSelector: @selector(count)]),
@"Expected array return value when constucting D-Bus reply for '%@' on %@",
NSStringFromSelector([inv selector]),
[inv target]);
retCount = [retVal count];
// Make sure that the number of argument matches:
NSAssert2((retCount == [outArgs count]),
@"Argument number mismatch when constucting D-Bus reply for '%@' on %@",
NSStringFromSelector([inv selector]),
[inv target]);
// Marshall them in order:
while (index < retCount)
{
[[outArgs objectAtIndex: index] marshallObject: [retVal objectAtIndex: index]
intoIterator: iter];
index++;
}
}
}
- (void)unmarshallArgumentsFromIterator: (DBusMessageIter*)iter
intoInvocation: (NSInvocation*)inv
{
NSUInteger numArgs = [inArgs count];
// Arguments start at index 2 (i.e. after self and _cmd)
NSUInteger index = 2;
NSMethodSignature *sig = [inv methodSignature];
while (index < (numArgs +2))
{
NSUInteger argIndex = index - 2;
BOOL doBox = YES;
NSInteger boxingState = [self boxingStateForArgumentAtIndex: argIndex
fromMethodSignature: sig];
NSAssert1((DK_ARGUMENT_INVALID != boxingState),
@"Argument cannot be boxed into invocation with signature %@.",
sig);
doBox = (BOOL)boxingState;
// Let the arguments umarshall themselves into the invocation
[[inArgs objectAtIndex: argIndex] unmarshallFromIterator: iter
intoInvocation: inv
atIndex: index
boxing: doBox];
/*
* Proceed to the next value in the message, but raise an exception if
* we are missing some arguments.
*/
if (NO == (BOOL)dbus_message_iter_next(iter))
{
[NSException raise: @"DKMethodUnmarshallingException"
format: @"D-Bus message too short when unmarshalling arguments for invocation of '%@' on '%@'.",
NSStringFromSelector([inv selector]),
[inv target]];
}
index++;
}
}
- (void) marshallArgumentsFromInvocation: (NSInvocation*)inv
intoIterator: (DBusMessageIter*)iter
{
// Start with index 2 to get the proper arguments
NSUInteger index = 2;
DKArgument *argument = nil;
NSEnumerator *argEnum = [inArgs objectEnumerator];
NSMethodSignature *sig = [inv methodSignature];
NSAssert1(([inArgs count] == ([[inv methodSignature] numberOfArguments] -2)),
@"Argument number mismatch when constructing D-Bus call for '%@'", name);
while (nil != (argument = [argEnum nextObject]))
{
BOOL doBox = YES;
NSInteger boxingState = [self boxingStateForArgumentAtIndex: (index -2 )
fromMethodSignature: sig];
NSAssert1((DK_ARGUMENT_INVALID != boxingState),
@"Argument cannot be boxed into invocation with signature %@.",
sig);
doBox = (BOOL)boxingState;
[argument marshallArgumentAtIndex: index
fromInvocation: inv
intoIterator: iter
boxing: doBox];
index++;
}
}
- (void) unmarshallFromIterator: (DBusMessageIter*)iter
intoInvocation: (NSInvocation*)inv
messageType: (int)type
{
if (DBUS_MESSAGE_TYPE_METHOD_RETURN == type)
{
// For method returns, we are interested in the return value.
[self unmarshallReturnValueFromIterator: iter
intoInvocation: inv];
}
else if (DBUS_MESSAGE_TYPE_METHOD_CALL == type)
{
// For method calls, we want to construct the invocation from the
// arguments.
[self unmarshallArgumentsFromIterator: iter
intoInvocation: inv];
}
}
- (void)marshallFromInvocation: (NSInvocation*)inv
intoIterator: (DBusMessageIter*)iter
messageType: (int)type
{
if (DBUS_MESSAGE_TYPE_METHOD_RETURN == type)
{
// If we are constructing a method return message, we want to obtain the
// return value.
[self marshallReturnValueFromInvocation: inv
intoIterator: iter];
}
else if (DBUS_MESSAGE_TYPE_METHOD_CALL == type)
{
// If we are constructing a method call, we want to marshall the arguments
[self marshallArgumentsFromInvocation: inv
intoIterator: iter];
}
}
- (NSUInteger)userVisibleArguments
{
return [inArgs count];
}
- (NSString*)methodDeclaration
{
NSMutableString *declaration = [NSMutableString stringWithString: @"- "];
NSArray *components = nil;
NSString *returnType = nil;
NSUInteger outCount = [outArgs count];
NSUInteger inCount = [self userVisibleArguments];
NSUInteger inIndex = 0;
NSEnumerator *argEnum = nil;
DKArgument *arg = nil;
if (0 == inCount)
{
components = [NSArray arrayWithObject: [self selectorString]];
}
else
{
components = [[self selectorString] componentsSeparatedByString: @":"];
}
NSAssert2(([components count] == (inCount + 1)), @"Invalid selector '%@' for method '%@'.",
[self selectorString],
name);
if (0 == outCount)
{
if ([self isOneway])
{
returnType = @"oneway void";
}
else
{
returnType = @"void";
}
}
else if (outCount > 1)
{
returnType = @"NSArray*";
}
else
{
Class retClass = [(DKArgument*)[outArgs objectAtIndex: 0] objCEquivalent];
if (Nil == retClass)
{
returnType = @"id";
}
else
{
returnType = [NSString stringWithFormat: @"%@*", NSStringFromClass(retClass)];
}
}
[declaration appendFormat: @"(%@)", returnType];
if (0 == inCount)
{
// If we have no arguments, we add the selector string here, because we will
// not run the loop in this case.
[declaration appendFormat: @"%@ ", [components objectAtIndex: 0]];
}
else
{
argEnum = [inArgs objectEnumerator];
while (nil != (arg = [argEnum nextObject]))
{
NSString *argType = @"id";
NSString *argName = [arg name];
Class theClass = [arg objCEquivalent];
if (theClass != Nil)
{
argType = [NSStringFromClass(theClass) stringByAppendingString: @"*"];
}
if (nil == argName)
{
argName = [NSString stringWithFormat: @"argument%ld", inIndex];
}
[declaration appendFormat:@"%@: (%@)%@ ",
[components objectAtIndex: inIndex],
argType,
argName];
inIndex++;
}
}
if ([self isDeprecated])
{
[declaration appendString: @"__attribute__((deprecated));"];
}
else
{
[declaration replaceCharactersInRange: NSMakeRange(([declaration length] - 1), 1)
withString: @";"];
}
return declaration;
}
- (NSString*)annotationValueForKey: (NSString*)key
{
NSString *value = [super annotationValueForKey: key];
// Perform validation whether we can use this selector string for the method,
// viz. determine whether it has the right number of colons (':').
if ([@"org.gnustep.objc.selector" isEqualToString: key])
{
const char* selectorString = [value UTF8String];
NSUInteger len = [value length];
NSUInteger i = 0;
NSUInteger expectedCount = [inArgs count];
NSUInteger actualCount = 0;
for (i = 0; i < len; i++)
{
if (':' == selectorString[i])
{
actualCount++;
}
if (actualCount > expectedCount)
{
return nil;
}
}
if (actualCount != expectedCount)
{
return nil;
}
}
return value;
}
- (NSString*)selectorString
{
NSString *selectorString = [self annotationValueForKey: @"org.gnustep.objc.selector"];
if (nil == selectorString)
{
// We generate a selector string from the method name by appending the
// correct number of colons
NSUInteger newLength = [name length] + [inArgs count];
selectorString = [name stringByPaddingToLength: newLength
withString: @":"
startingAtIndex: 0];
}
return selectorString;
}
- (void)setOutArgs: (NSMutableArray*)newOut
{
ASSIGN(outArgs, newOut);
[outArgs makeObjectsPerformSelector: @selector(setParent:) withObject: self];
}
- (void)setInArgs: (NSMutableArray*)newIn
{
ASSIGN(inArgs, newIn);
[inArgs makeObjectsPerformSelector: @selector(setParent:) withObject: self];
}
- (id)copyWithZone: (NSZone*)zone
{
DKMethod *newNode = [super copyWithZone: zone];
NSMutableArray *newIn = nil;
NSMutableArray *newOut = nil;
newIn = [[NSMutableArray allocWithZone: zone] initWithArray: inArgs
copyItems: YES];
newOut = [[NSMutableArray allocWithZone: zone] initWithArray: outArgs
copyItems: YES];
[newNode setOutArgs: newOut];
[newNode setInArgs: newIn];
[newOut release];
[newIn release];
return newNode;
}
- (void)dealloc
{
[inArgs release];
[outArgs release];
[super dealloc];
}
@end
|