Changeset 554601


Ignore:
Timestamp:
12/28/11 15:45:07 (17 months ago)
Author:
Stefan Persson <stefan.persson@…>
Branches:
('master', 'a231b56d57a69513303d0061f5fa3e3cd51c85c1')('controller-upgrade', '72b31cc86eeeef18f1371a3067b6e8a5ca21abfc')('windows_service_fixes', 'df6bd2788365991d36d5af2a75833b8de2a5860f')
Children:
44e5b81a909587475868949602bb06e09d9354f1
Parents:
b6fb5ce106ed038b516319e81b7117b7a36ec8ac
git-author:
Stefan Persson <stefan.persson@telldus.se>2011-12-28 15:45:07+01:00
git-committer:
Stefan Persson <stefan.persson@telldus.se>2011-12-28 15:45:07+01:00
Message:

Tdtool exiting with error codes on failures. Closes #146.

Location:
telldus-core
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • telldus-core/client/telldus-core.h

    r8eaaf6 r554601  
    116116#define TELLSTICK_ERROR_CONNECTING_SERVICE -6 
    117117#define TELLSTICK_ERROR_UNKNOWN_RESPONSE -7 
     118#define TELLSTICK_ERROR_SYNTAX -8 
    118119#define TELLSTICK_ERROR_UNKNOWN -99 
    119120 
  • telldus-core/tdtool/main.cpp

    rb6fb5ce r554601  
    7979 
    8080void print_device( int index ) { 
     81        tdInit(); 
    8182        int intId = tdGetDeviceId(index); 
    8283        char *name = tdGetName(intId); 
     
    103104} 
    104105 
    105 void list_devices() { 
     106int list_devices() { 
     107        tdInit(); 
    106108        int intNum = tdGetNumberOfDevices(); 
    107109        if (intNum < 0) { 
     
    109111                fprintf(stderr, "Error fetching devices: %s\n", errorString); 
    110112                tdReleaseString(errorString); 
    111                 return; 
     113                return intNum; 
    112114        } 
    113115        printf("Number of devices: %i\n", intNum); 
     
    155157                fprintf(stderr, "Error fetching sensors: %s\n", errorString); 
    156158                tdReleaseString(errorString); 
    157                 return; 
    158         } 
     159                return sensorStatus; 
     160        } 
     161        return TELLSTICK_SUCCESS; 
    159162} 
    160163 
    161164int find_device( char *device ) { 
     165        tdInit(); 
    162166        int deviceId = atoi(device); 
    163167        if (deviceId == 0) { //Try to find the id from the name 
     
    179183} 
    180184 
    181 void switch_device( bool turnOn, char *device ) { 
     185int switch_device( bool turnOn, char *device ) { 
     186        tdInit(); 
    182187        int deviceId = find_device( device ); 
    183188        if (deviceId == 0) { 
    184189                printf("Device '%s', not found!\n", device); 
    185                 return; 
     190                return TELLSTICK_ERROR_DEVICE_NOT_FOUND; 
    186191        } 
    187192 
     
    200205        printf(" - %s\n", errorString); 
    201206        tdReleaseString(errorString); 
    202 } 
    203  
    204 void dim_device( char *device, int level ) { 
     207        return retval; 
     208} 
     209 
     210int dim_device( char *device, int level ) { 
     211        tdInit(); 
    205212        int deviceId = find_device( device ); 
    206213        if (deviceId == 0) { 
    207214                printf("Device '%s', not found!\n", device); 
    208                 return; 
     215                return TELLSTICK_ERROR_DEVICE_NOT_FOUND; 
    209216        } 
    210217        if (level < 0 || level > 255) { 
    211218                printf("Level %i out of range!\n", level); 
    212                 return; 
     219                return TELLSTICK_ERROR_SYNTAX; 
    213220        } 
    214221 
     
    219226        tdReleaseString(name); 
    220227        tdReleaseString(errorString); 
    221 } 
    222  
    223 void bell_device( char *device ) { 
     228        return retval; 
     229} 
     230 
     231int bell_device( char *device ) { 
     232        tdInit(); 
    224233        int deviceId = find_device( device ); 
    225234        if (deviceId == 0) { 
    226235                printf("Device '%s', not found!\n", device); 
    227                 return; 
     236                return TELLSTICK_ERROR_DEVICE_NOT_FOUND; 
    228237        } 
    229238 
     
    234243        tdReleaseString(name); 
    235244        tdReleaseString(errorString); 
    236 } 
    237  
    238 void learn_device( char *device ) { 
     245        return retval; 
     246} 
     247 
     248int learn_device( char *device ) { 
     249        tdInit(); 
    239250        int deviceId = find_device( device ); 
    240251        if (deviceId == 0) { 
    241252                printf("Device '%s', not found!\n", device); 
    242                 return; 
     253                return TELLSTICK_ERROR_DEVICE_NOT_FOUND; 
    243254        } 
    244255 
     
    249260        tdReleaseString(name); 
    250261        tdReleaseString(errorString); 
    251 } 
    252  
    253 void send_raw_command( char *command ) { 
     262        return retval; 
     263} 
     264 
     265int send_raw_command( char *command ) { 
     266        tdInit(); 
    254267        const int MAX_LENGTH = 100; 
    255268        char msg[MAX_LENGTH]; 
     
    263276                if (fd == NULL) { 
    264277                        printf("Error opening file %s\n", command); 
    265                         return; 
     278                        return TELLSTICK_ERROR_UNKNOWN; 
    266279                } 
    267280                fgets(msg, MAX_LENGTH, fd); 
     
    272285        printf("Sending raw command: %s\n", errorString); 
    273286        tdReleaseString(errorString); 
     287        return retval; 
    274288} 
    275289 
     
    295309        if (argc < 2) { 
    296310                print_usage( argv[0] ); 
    297                 return -1; 
    298         } 
    299  
    300         while ( (optch = getopt_long(argc,argv,optstring,long_opts,&longindex)) != -1 ) 
     311                return -TELLSTICK_ERROR_SYNTAX; 
     312        } 
     313 
     314        int returnSuccess = 0; 
     315        while ( (optch = getopt_long(argc,argv,optstring,long_opts,&longindex)) != -1 ){ 
     316                int success = 0; 
    301317                switch (optch) { 
    302318                        case 'b' : 
    303                                 bell_device( &optarg[0] ); 
     319                                success = bell_device( &optarg[0] ); 
    304320                                break; 
    305321                        case 'd' : 
    306322                                if (level >= 0) { 
    307                                         dim_device( &optarg[0], level ); 
     323                                        success = dim_device( &optarg[0], level ); 
     324                                        break; 
    308325                                } 
     326                                printf("Dim level missing or incorrect value.\n"); 
     327                                success = TELLSTICK_ERROR_SYNTAX; 
    309328                                break; 
    310329                        case 'f' : 
    311                                 switch_device(false, &optarg[0]); 
     330                                success = switch_device(false, &optarg[0]); 
    312331                                break; 
    313332                        case 'h' : 
    314333                                print_usage( argv[0] ); 
    315                                 break; 
     334                                success = TELLSTICK_SUCCESS; 
    316335                        case 'i' : 
    317336                                print_version( ); 
    318                                 break; 
     337                                success = TELLSTICK_SUCCESS; 
    319338                        case 'l' : 
    320                                 list_devices(); 
     339                                success = list_devices(); 
    321340                                break; 
    322341                        case 'n' : 
    323                                 switch_device(true, &optarg[0]); 
     342                                success = switch_device(true, &optarg[0]); 
    324343                                break; 
    325344                        case 'e' : 
    326                                 learn_device(&optarg[0]); 
     345                                success = learn_device(&optarg[0]); 
    327346                                break; 
    328347                        case 'r' : 
    329                                 send_raw_command(&optarg[0]); 
     348                                success = send_raw_command(&optarg[0]); 
    330349                                break; 
    331350                        case 'v' : 
     
    334353                        default : 
    335354                                print_usage( argv[0] ); 
    336                                 return -1; 
    337                 } 
    338  
     355                                success = TELLSTICK_ERROR_SYNTAX; 
     356                } 
     357                if(success != TELLSTICK_SUCCESS){ 
     358                        returnSuccess = success;  //return last error message 
     359                } 
     360        } 
    339361        tdClose(); //Cleaning up 
    340         return 0; 
    341 } 
     362        return -returnSuccess; 
     363} 
Note: See TracChangeset for help on using the changeset viewer.