diff options
Diffstat (limited to 'src/irecovery.c')
-rw-r--r-- | src/irecovery.c | 79 |
1 files changed, 67 insertions, 12 deletions
diff --git a/src/irecovery.c b/src/irecovery.c index 7133606..1c4957e 100644 --- a/src/irecovery.c +++ b/src/irecovery.c @@ -23,7 +23,8 @@ #include <readline/readline.h> #include <readline/history.h> -#define FILE_HISTORY_PATH "~/.irecovery/history" +#define FILE_HISTORY_PATH ".irecovery" +#define debug(...) if(verbose) fprintf(stderr, __VA_ARGS__) enum { kResetDevice, kStartShell, kSendCommand, kSendFile @@ -40,21 +41,29 @@ void print_shell_usage() { } void parse_command(irecv_device_t* device, unsigned char* command, unsigned int size) { - char* cmd = strtok(command, " "); - if(!strcmp(command, "/exit")) { + char* cmd = strtok(strdup(command), " "); + debug("Executing %s %s\n", cmd, command); + if(!strcmp(cmd, "/exit")) { quit = 1; } else - if(!strcmp(command, "/help")) { + if(!strcmp(cmd, "/help")) { print_shell_usage(); } else + + if(!strcmp(cmd, "/reconnect")) { + irecv_close(device); + irecv_open(device); + } else - if(!strcmp(command, "/upload")) { + if(!strcmp(cmd, "/upload")) { char* filename = strtok(NULL, " "); + debug("Sending %s\n", filename); if(filename != NULL) { irecv_send_file(device, filename); } } + free(cmd); } int recv_callback(irecv_device_t* device, unsigned char* data, int size) { @@ -66,10 +75,41 @@ int recv_callback(irecv_device_t* device, unsigned char* data, int size) { } int send_callback(irecv_device_t* device, unsigned char* command, int size) { + irecv_error_t error = 0; if(command[0] == '/') { parse_command(device, command, size); return 0; } + + if(strstr(command, "getenv") != NULL) { + unsigned char* value = NULL; + error = irecv_send_command(device, command); + if(error != IRECV_SUCCESS) { + debug("%s\n", irecv_strerror(error)); + return error; + } + + error = irecv_getenv(device, &value); + if(error != IRECV_SUCCESS) { + debug("%s\n", irecv_strerror(error)); + return error; + } + + printf("%s\n", value); + free(value); + return 0; + } + + if(!strcmp(command, "reboot")) { + error = irecv_send_command(device, command); + if(error != IRECV_SUCCESS) { + debug("%s\n", irecv_strerror(error)); + return error; + } + quit = 1; + return 0; + } + return size; } @@ -83,17 +123,21 @@ void append_command_to_history(char* cmd) { } void init_shell(irecv_device_t* device) { + irecv_error_t error = 0; load_command_history(); irecv_set_sender(device, &send_callback); irecv_set_receiver(device, &recv_callback); while(!quit) { - if(irecv_update(device) != IRECV_SUCCESS) { + error = irecv_receive(device); + if(error != IRECV_SUCCESS) { + debug("%s\n", irecv_strerror(error)); break; } char* cmd = readline("> "); if(cmd && *cmd) { - if(irecv_send_command(device, cmd) != IRECV_SUCCESS) { + error = irecv_send(device, cmd); + if(error != IRECV_SUCCESS) { quit = 1; } @@ -119,6 +163,7 @@ int main(int argc, char** argv) { int opt = 0; int action = 0; char* argument = NULL; + irecv_error_t error = 0; if(argc == 1) print_usage(); while ((opt = getopt(argc, argv, "vhrsc:f:")) > 0) { switch (opt) { @@ -161,9 +206,17 @@ int main(int argc, char** argv) { } if(verbose) irecv_set_debug(device, verbose); - if(irecv_open(device) < 0) { - fprintf(stderr, "Unable to open device\n"); - return -1; + int i = 0; + for(i = 0; i <= 5; i++) { + debug("Attempting to connect... "); + if(i == 5) { + irecv_exit(device); + return -1; + } + + if(irecv_open(device) < 0) sleep(1); + else break; + debug("failed\n"); } switch(action) { @@ -172,11 +225,13 @@ int main(int argc, char** argv) { break; case kSendFile: - irecv_send_file(device, argument); + error = irecv_send_file(device, argument); + debug("%s\n", irecv_strerror(error)); break; case kSendCommand: - irecv_send_command(device, argument); + error = irecv_send_command(device, argument); + debug("%s\n", irecv_strerror(error)); break; case kStartShell: |