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 @@ | |||
| 23 | #include <readline/readline.h> | 23 | #include <readline/readline.h> |
| 24 | #include <readline/history.h> | 24 | #include <readline/history.h> |
| 25 | 25 | ||
| 26 | #define FILE_HISTORY_PATH "~/.irecovery/history" | 26 | #define FILE_HISTORY_PATH ".irecovery" |
| 27 | #define debug(...) if(verbose) fprintf(stderr, __VA_ARGS__) | ||
| 27 | 28 | ||
| 28 | enum { | 29 | enum { |
| 29 | kResetDevice, kStartShell, kSendCommand, kSendFile | 30 | kResetDevice, kStartShell, kSendCommand, kSendFile |
| @@ -40,21 +41,29 @@ void print_shell_usage() { | |||
| 40 | } | 41 | } |
| 41 | 42 | ||
| 42 | void parse_command(irecv_device_t* device, unsigned char* command, unsigned int size) { | 43 | void parse_command(irecv_device_t* device, unsigned char* command, unsigned int size) { |
| 43 | char* cmd = strtok(command, " "); | 44 | char* cmd = strtok(strdup(command), " "); |
| 44 | if(!strcmp(command, "/exit")) { | 45 | debug("Executing %s %s\n", cmd, command); |
| 46 | if(!strcmp(cmd, "/exit")) { | ||
| 45 | quit = 1; | 47 | quit = 1; |
| 46 | } else | 48 | } else |
| 47 | 49 | ||
| 48 | if(!strcmp(command, "/help")) { | 50 | if(!strcmp(cmd, "/help")) { |
| 49 | print_shell_usage(); | 51 | print_shell_usage(); |
| 50 | } else | 52 | } else |
| 53 | |||
| 54 | if(!strcmp(cmd, "/reconnect")) { | ||
| 55 | irecv_close(device); | ||
| 56 | irecv_open(device); | ||
| 57 | } else | ||
| 51 | 58 | ||
| 52 | if(!strcmp(command, "/upload")) { | 59 | if(!strcmp(cmd, "/upload")) { |
| 53 | char* filename = strtok(NULL, " "); | 60 | char* filename = strtok(NULL, " "); |
| 61 | debug("Sending %s\n", filename); | ||
| 54 | if(filename != NULL) { | 62 | if(filename != NULL) { |
| 55 | irecv_send_file(device, filename); | 63 | irecv_send_file(device, filename); |
| 56 | } | 64 | } |
| 57 | } | 65 | } |
| 66 | free(cmd); | ||
| 58 | } | 67 | } |
| 59 | 68 | ||
| 60 | int recv_callback(irecv_device_t* device, unsigned char* data, int size) { | 69 | 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) { | |||
| 66 | } | 75 | } |
| 67 | 76 | ||
| 68 | int send_callback(irecv_device_t* device, unsigned char* command, int size) { | 77 | int send_callback(irecv_device_t* device, unsigned char* command, int size) { |
| 78 | irecv_error_t error = 0; | ||
| 69 | if(command[0] == '/') { | 79 | if(command[0] == '/') { |
| 70 | parse_command(device, command, size); | 80 | parse_command(device, command, size); |
| 71 | return 0; | 81 | return 0; |
| 72 | } | 82 | } |
| 83 | |||
| 84 | if(strstr(command, "getenv") != NULL) { | ||
| 85 | unsigned char* value = NULL; | ||
| 86 | error = irecv_send_command(device, command); | ||
| 87 | if(error != IRECV_SUCCESS) { | ||
| 88 | debug("%s\n", irecv_strerror(error)); | ||
| 89 | return error; | ||
| 90 | } | ||
| 91 | |||
| 92 | error = irecv_getenv(device, &value); | ||
| 93 | if(error != IRECV_SUCCESS) { | ||
| 94 | debug("%s\n", irecv_strerror(error)); | ||
| 95 | return error; | ||
| 96 | } | ||
| 97 | |||
| 98 | printf("%s\n", value); | ||
| 99 | free(value); | ||
| 100 | return 0; | ||
| 101 | } | ||
| 102 | |||
| 103 | if(!strcmp(command, "reboot")) { | ||
| 104 | error = irecv_send_command(device, command); | ||
| 105 | if(error != IRECV_SUCCESS) { | ||
| 106 | debug("%s\n", irecv_strerror(error)); | ||
| 107 | return error; | ||
| 108 | } | ||
| 109 | quit = 1; | ||
| 110 | return 0; | ||
| 111 | } | ||
| 112 | |||
| 73 | return size; | 113 | return size; |
| 74 | } | 114 | } |
| 75 | 115 | ||
| @@ -83,17 +123,21 @@ void append_command_to_history(char* cmd) { | |||
| 83 | } | 123 | } |
| 84 | 124 | ||
| 85 | void init_shell(irecv_device_t* device) { | 125 | void init_shell(irecv_device_t* device) { |
| 126 | irecv_error_t error = 0; | ||
| 86 | load_command_history(); | 127 | load_command_history(); |
| 87 | irecv_set_sender(device, &send_callback); | 128 | irecv_set_sender(device, &send_callback); |
| 88 | irecv_set_receiver(device, &recv_callback); | 129 | irecv_set_receiver(device, &recv_callback); |
| 89 | while(!quit) { | 130 | while(!quit) { |
| 90 | if(irecv_update(device) != IRECV_SUCCESS) { | 131 | error = irecv_receive(device); |
| 132 | if(error != IRECV_SUCCESS) { | ||
| 133 | debug("%s\n", irecv_strerror(error)); | ||
| 91 | break; | 134 | break; |
| 92 | } | 135 | } |
| 93 | 136 | ||
| 94 | char* cmd = readline("> "); | 137 | char* cmd = readline("> "); |
| 95 | if(cmd && *cmd) { | 138 | if(cmd && *cmd) { |
| 96 | if(irecv_send_command(device, cmd) != IRECV_SUCCESS) { | 139 | error = irecv_send(device, cmd); |
| 140 | if(error != IRECV_SUCCESS) { | ||
| 97 | quit = 1; | 141 | quit = 1; |
| 98 | } | 142 | } |
| 99 | 143 | ||
| @@ -119,6 +163,7 @@ int main(int argc, char** argv) { | |||
| 119 | int opt = 0; | 163 | int opt = 0; |
| 120 | int action = 0; | 164 | int action = 0; |
| 121 | char* argument = NULL; | 165 | char* argument = NULL; |
| 166 | irecv_error_t error = 0; | ||
| 122 | if(argc == 1) print_usage(); | 167 | if(argc == 1) print_usage(); |
| 123 | while ((opt = getopt(argc, argv, "vhrsc:f:")) > 0) { | 168 | while ((opt = getopt(argc, argv, "vhrsc:f:")) > 0) { |
| 124 | switch (opt) { | 169 | switch (opt) { |
| @@ -161,9 +206,17 @@ int main(int argc, char** argv) { | |||
| 161 | } | 206 | } |
| 162 | if(verbose) irecv_set_debug(device, verbose); | 207 | if(verbose) irecv_set_debug(device, verbose); |
| 163 | 208 | ||
| 164 | if(irecv_open(device) < 0) { | 209 | int i = 0; |
| 165 | fprintf(stderr, "Unable to open device\n"); | 210 | for(i = 0; i <= 5; i++) { |
| 166 | return -1; | 211 | debug("Attempting to connect... "); |
| 212 | if(i == 5) { | ||
| 213 | irecv_exit(device); | ||
| 214 | return -1; | ||
| 215 | } | ||
| 216 | |||
| 217 | if(irecv_open(device) < 0) sleep(1); | ||
| 218 | else break; | ||
| 219 | debug("failed\n"); | ||
| 167 | } | 220 | } |
| 168 | 221 | ||
| 169 | switch(action) { | 222 | switch(action) { |
| @@ -172,11 +225,13 @@ int main(int argc, char** argv) { | |||
| 172 | break; | 225 | break; |
| 173 | 226 | ||
| 174 | case kSendFile: | 227 | case kSendFile: |
| 175 | irecv_send_file(device, argument); | 228 | error = irecv_send_file(device, argument); |
| 229 | debug("%s\n", irecv_strerror(error)); | ||
| 176 | break; | 230 | break; |
| 177 | 231 | ||
| 178 | case kSendCommand: | 232 | case kSendCommand: |
| 179 | irecv_send_command(device, argument); | 233 | error = irecv_send_command(device, argument); |
| 234 | debug("%s\n", irecv_strerror(error)); | ||
| 180 | break; | 235 | break; |
| 181 | 236 | ||
| 182 | case kStartShell: | 237 | case kStartShell: |
