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

This commit is contained in:
Stefan Persson 2011-12-28 15:45:07 +01:00
parent b6fb5ce106
commit 5546013552
2 changed files with 51 additions and 28 deletions

View file

@ -115,6 +115,7 @@ extern "C" {
#define TELLSTICK_ERROR_COMMUNICATION -5
#define TELLSTICK_ERROR_CONNECTING_SERVICE -6
#define TELLSTICK_ERROR_UNKNOWN_RESPONSE -7
#define TELLSTICK_ERROR_SYNTAX -8
#define TELLSTICK_ERROR_UNKNOWN -99
//Device typedef

View file

@ -78,6 +78,7 @@ void print_version() {
}
void print_device( int index ) {
tdInit();
int intId = tdGetDeviceId(index);
char *name = tdGetName(intId);
printf("%i\t%s\t", intId, name);
@ -102,13 +103,14 @@ void print_device( int index ) {
printf("\n");
}
void list_devices() {
int list_devices() {
tdInit();
int intNum = tdGetNumberOfDevices();
if (intNum < 0) {
char *errorString = tdGetErrorString(intNum);
fprintf(stderr, "Error fetching devices: %s\n", errorString);
tdReleaseString(errorString);
return;
return intNum;
}
printf("Number of devices: %i\n", intNum);
int i = 0;
@ -154,11 +156,13 @@ void list_devices() {
char *errorString = tdGetErrorString(sensorStatus);
fprintf(stderr, "Error fetching sensors: %s\n", errorString);
tdReleaseString(errorString);
return;
return sensorStatus;
}
return TELLSTICK_SUCCESS;
}
int find_device( char *device ) {
tdInit();
int deviceId = atoi(device);
if (deviceId == 0) { //Try to find the id from the name
int intNum = tdGetNumberOfDevices();
@ -178,11 +182,12 @@ int find_device( char *device ) {
return deviceId;
}
void switch_device( bool turnOn, char *device ) {
int switch_device( bool turnOn, char *device ) {
tdInit();
int deviceId = find_device( device );
if (deviceId == 0) {
printf("Device '%s', not found!\n", device);
return;
return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
}
char *name = tdGetName( deviceId );
@ -199,17 +204,19 @@ void switch_device( bool turnOn, char *device ) {
printf(" - %s\n", errorString);
tdReleaseString(errorString);
return retval;
}
void dim_device( char *device, int level ) {
int dim_device( char *device, int level ) {
tdInit();
int deviceId = find_device( device );
if (deviceId == 0) {
printf("Device '%s', not found!\n", device);
return;
return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
}
if (level < 0 || level > 255) {
printf("Level %i out of range!\n", level);
return;
return TELLSTICK_ERROR_SYNTAX;
}
char *name = tdGetName( deviceId );
@ -218,13 +225,15 @@ void dim_device( char *device, int level ) {
printf("Dimming device: %i %s to %i - %s\n", deviceId, name, level, errorString);
tdReleaseString(name);
tdReleaseString(errorString);
return retval;
}
void bell_device( char *device ) {
int bell_device( char *device ) {
tdInit();
int deviceId = find_device( device );
if (deviceId == 0) {
printf("Device '%s', not found!\n", device);
return;
return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
}
char *name = tdGetName( deviceId );
@ -233,13 +242,15 @@ void bell_device( char *device ) {
printf("Sending bell to: %i %s - %s\n", deviceId, name, errorString);
tdReleaseString(name);
tdReleaseString(errorString);
return retval;
}
void learn_device( char *device ) {
int learn_device( char *device ) {
tdInit();
int deviceId = find_device( device );
if (deviceId == 0) {
printf("Device '%s', not found!\n", device);
return;
return TELLSTICK_ERROR_DEVICE_NOT_FOUND;
}
char *name = tdGetName( deviceId );
@ -248,9 +259,11 @@ void learn_device( char *device ) {
printf("Learning device: %i %s - %s\n", deviceId, name, errorString);
tdReleaseString(name);
tdReleaseString(errorString);
return retval;
}
void send_raw_command( char *command ) {
int send_raw_command( char *command ) {
tdInit();
const int MAX_LENGTH = 100;
char msg[MAX_LENGTH];
@ -262,7 +275,7 @@ void send_raw_command( char *command ) {
fd = fopen(command, "r");
if (fd == NULL) {
printf("Error opening file %s\n", command);
return;
return TELLSTICK_ERROR_UNKNOWN;
}
fgets(msg, MAX_LENGTH, fd);
}
@ -271,6 +284,7 @@ void send_raw_command( char *command ) {
char *errorString = tdGetErrorString(retval);
printf("Sending raw command: %s\n", errorString);
tdReleaseString(errorString);
return retval;
}
int main(int argc, char **argv)
@ -294,48 +308,56 @@ int main(int argc, char **argv)
if (argc < 2) {
print_usage( argv[0] );
return -1;
return -TELLSTICK_ERROR_SYNTAX;
}
while ( (optch = getopt_long(argc,argv,optstring,long_opts,&longindex)) != -1 )
int returnSuccess = 0;
while ( (optch = getopt_long(argc,argv,optstring,long_opts,&longindex)) != -1 ){
int success = 0;
switch (optch) {
case 'b' :
bell_device( &optarg[0] );
success = bell_device( &optarg[0] );
break;
case 'd' :
if (level >= 0) {
dim_device( &optarg[0], level );
success = dim_device( &optarg[0], level );
break;
}
printf("Dim level missing or incorrect value.\n");
success = TELLSTICK_ERROR_SYNTAX;
break;
case 'f' :
switch_device(false, &optarg[0]);
success = switch_device(false, &optarg[0]);
break;
case 'h' :
print_usage( argv[0] );
break;
success = TELLSTICK_SUCCESS;
case 'i' :
print_version( );
break;
success = TELLSTICK_SUCCESS;
case 'l' :
list_devices();
success = list_devices();
break;
case 'n' :
switch_device(true, &optarg[0]);
success = switch_device(true, &optarg[0]);
break;
case 'e' :
learn_device(&optarg[0]);
success = learn_device(&optarg[0]);
break;
case 'r' :
send_raw_command(&optarg[0]);
success = send_raw_command(&optarg[0]);
break;
case 'v' :
level = atoi( &optarg[0] );
break;
default :
print_usage( argv[0] );
return -1;
success = TELLSTICK_ERROR_SYNTAX;
}
if(success != TELLSTICK_SUCCESS){
returnSuccess = success; //return last error message
}
}
tdClose(); //Cleaning up
return 0;
return -returnSuccess;
}