diff options
Diffstat (limited to 'src/irecovery.c')
| -rw-r--r-- | src/irecovery.c | 118 |
1 files changed, 59 insertions, 59 deletions
diff --git a/src/irecovery.c b/src/irecovery.c index 98b1e90..a623579 100644 --- a/src/irecovery.c +++ b/src/irecovery.c | |||
| @@ -43,18 +43,18 @@ void print_shell_usage() { | |||
| 43 | void parse_command(irecv_client_t client, unsigned char* command, unsigned int size) { | 43 | void parse_command(irecv_client_t client, unsigned char* command, unsigned int size) { |
| 44 | char* cmd = strtok(strdup(command), " "); | 44 | char* cmd = strtok(strdup(command), " "); |
| 45 | debug("Executing %s %s\n", cmd, command); | 45 | debug("Executing %s %s\n", cmd, command); |
| 46 | if(!strcmp(cmd, "/exit")) { | 46 | if (!strcmp(cmd, "/exit")) { |
| 47 | quit = 1; | 47 | quit = 1; |
| 48 | } else | 48 | } else |
| 49 | 49 | ||
| 50 | if(!strcmp(cmd, "/help")) { | 50 | if (!strcmp(cmd, "/help")) { |
| 51 | print_shell_usage(); | 51 | print_shell_usage(); |
| 52 | } else | 52 | } else |
| 53 | 53 | ||
| 54 | if(!strcmp(cmd, "/upload")) { | 54 | if (!strcmp(cmd, "/upload")) { |
| 55 | char* filename = strtok(NULL, " "); | 55 | char* filename = strtok(NULL, " "); |
| 56 | debug("Sending %s\n", filename); | 56 | debug("Sending %s\n", filename); |
| 57 | if(filename != NULL) { | 57 | if (filename != NULL) { |
| 58 | irecv_send_file(client, filename); | 58 | irecv_send_file(client, filename); |
| 59 | } | 59 | } |
| 60 | } | 60 | } |
| @@ -63,47 +63,14 @@ void parse_command(irecv_client_t client, unsigned char* command, unsigned int s | |||
| 63 | 63 | ||
| 64 | int recv_callback(irecv_client_t client, unsigned char* data, int size) { | 64 | int recv_callback(irecv_client_t client, unsigned char* data, int size) { |
| 65 | int i = 0; | 65 | int i = 0; |
| 66 | for(i = 0; i < size; i++) { | 66 | for (i = 0; i < size; i++) { |
| 67 | printf("%c", data[i]); | 67 | printf("%c", data[i]); |
| 68 | } | 68 | } |
| 69 | return size; | 69 | return size; |
| 70 | } | 70 | } |
| 71 | 71 | ||
| 72 | int send_callback(irecv_client_t client, unsigned char* command, int size) { | 72 | int send_callback(irecv_client_t client, unsigned char* command, int size) { |
| 73 | irecv_error_t error = 0; | ||
| 74 | if(command[0] == '/') { | ||
| 75 | parse_command(client, command, size); | ||
| 76 | return 0; | ||
| 77 | } | ||
| 78 | |||
| 79 | if(strstr(command, "getenv") != NULL) { | ||
| 80 | unsigned char* value = NULL; | ||
| 81 | error = irecv_send_command(client, command); | ||
| 82 | if(error != IRECV_E_SUCCESS) { | ||
| 83 | debug("%s\n", irecv_strerror(error)); | ||
| 84 | return error; | ||
| 85 | } | ||
| 86 | |||
| 87 | error = irecv_getenv(client, &value); | ||
| 88 | if(error != IRECV_E_SUCCESS) { | ||
| 89 | debug("%s\n", irecv_strerror(error)); | ||
| 90 | return error; | ||
| 91 | } | ||
| 92 | |||
| 93 | printf("%s\n", value); | ||
| 94 | free(value); | ||
| 95 | return 0; | ||
| 96 | } | ||
| 97 | 73 | ||
| 98 | if(!strcmp(command, "reboot")) { | ||
| 99 | error = irecv_send_command(client, command); | ||
| 100 | if(error != IRECV_E_SUCCESS) { | ||
| 101 | debug("%s\n", irecv_strerror(error)); | ||
| 102 | return error; | ||
| 103 | } | ||
| 104 | quit = 1; | ||
| 105 | return 0; | ||
| 106 | } | ||
| 107 | 74 | ||
| 108 | return size; | 75 | return size; |
| 109 | } | 76 | } |
| @@ -119,24 +86,25 @@ void append_command_to_history(char* cmd) { | |||
| 119 | 86 | ||
| 120 | void init_shell(irecv_client_t client) { | 87 | void init_shell(irecv_client_t client) { |
| 121 | irecv_error_t error = 0; | 88 | irecv_error_t error = 0; |
| 122 | //load_command_history(); | 89 | load_command_history(); |
| 123 | irecv_set_sender(client, &send_callback); | ||
| 124 | irecv_set_receiver(client, &recv_callback); | 90 | irecv_set_receiver(client, &recv_callback); |
| 125 | while(!quit) { | 91 | irecv_event_subscribe(client, IRECV_PRECOMMAND, &precommand_cb, NULL); |
| 92 | irecv_event_subscribe(client, IRECV_POSTCOMMAND, &postcommand_cb, NULL); | ||
| 93 | while (!quit) { | ||
| 126 | error = irecv_receive(client); | 94 | error = irecv_receive(client); |
| 127 | if(error != IRECV_E_SUCCESS) { | 95 | if (error != IRECV_E_SUCCESS) { |
| 128 | debug("%s\n", irecv_strerror(error)); | 96 | debug("%s\n", irecv_strerror(error)); |
| 129 | break; | 97 | break; |
| 130 | } | 98 | } |
| 131 | 99 | ||
| 132 | char* cmd = readline("> "); | 100 | char* cmd = readline("> "); |
| 133 | if(cmd && *cmd) { | 101 | if (cmd && *cmd) { |
| 134 | error = irecv_send(client, cmd); | 102 | error = irecv_send_command(client, cmd); |
| 135 | if(error != IRECV_E_SUCCESS) { | 103 | if (error != IRECV_E_SUCCESS) { |
| 136 | quit = 1; | 104 | quit = 1; |
| 137 | } | 105 | } |
| 138 | 106 | ||
| 139 | //append_command_to_history(cmd); | 107 | append_command_to_history(cmd); |
| 140 | free(cmd); | 108 | free(cmd); |
| 141 | } | 109 | } |
| 142 | } | 110 | } |
| @@ -155,13 +123,43 @@ void print_usage() { | |||
| 155 | exit(1); | 123 | exit(1); |
| 156 | } | 124 | } |
| 157 | 125 | ||
| 126 | int precommand_cb(irecv_client_t client, const irecv_event_t* event) { | ||
| 127 | irecv_error_t error = 0; | ||
| 128 | if (event->data[0] == '/') { | ||
| 129 | parse_command(client, event->data, strlen(event->data)); | ||
| 130 | return -1; | ||
| 131 | } | ||
| 132 | return 0; | ||
| 133 | } | ||
| 134 | |||
| 135 | int postcommand_cb(irecv_client_t client, const irecv_event_t* event) { | ||
| 136 | irecv_error_t error = 0; | ||
| 137 | if (strstr(event->data, "getenv") != NULL) { | ||
| 138 | unsigned char* value = NULL; | ||
| 139 | error = irecv_getenv(client, &value); | ||
| 140 | if (error != IRECV_E_SUCCESS) { | ||
| 141 | debug("%s\n", irecv_strerror(error)); | ||
| 142 | return error; | ||
| 143 | } | ||
| 144 | |||
| 145 | printf("%s\n", value); | ||
| 146 | free(value); | ||
| 147 | } | ||
| 148 | |||
| 149 | if (!strcmp(event->data, "reboot")) { | ||
| 150 | quit = 1; | ||
| 151 | } | ||
| 152 | |||
| 153 | return 0; | ||
| 154 | } | ||
| 155 | |||
| 158 | int main(int argc, char** argv) { | 156 | int main(int argc, char** argv) { |
| 159 | int i = 0; | 157 | int i = 0; |
| 160 | int opt = 0; | 158 | int opt = 0; |
| 161 | int action = 0; | 159 | int action = 0; |
| 162 | char* argument = NULL; | 160 | char* argument = NULL; |
| 163 | irecv_error_t error = 0; | 161 | irecv_error_t error = 0; |
| 164 | if(argc == 1) print_usage(); | 162 | if (argc == 1) print_usage(); |
| 165 | while ((opt = getopt(argc, argv, "vhrsc:f:k::")) > 0) { | 163 | while ((opt = getopt(argc, argv, "vhrsc:f:k::")) > 0) { |
| 166 | switch (opt) { | 164 | switch (opt) { |
| 167 | case 'v': | 165 | case 'v': |
| @@ -202,20 +200,22 @@ int main(int argc, char** argv) { | |||
| 202 | } | 200 | } |
| 203 | 201 | ||
| 204 | irecv_client_t client = NULL; | 202 | irecv_client_t client = NULL; |
| 205 | for(i = 0; i <= 5; i++) { | 203 | for (i = 0; i <= 5; i++) { |
| 206 | debug("Attempting to connect... \n"); | 204 | debug("Attempting to connect... \n"); |
| 207 | 205 | ||
| 208 | if(irecv_open(&client) != IRECV_E_SUCCESS) sleep(1); | 206 | if (irecv_open(&client) != IRECV_E_SUCCESS) |
| 209 | else break; | 207 | sleep(1); |
| 208 | else | ||
| 209 | break; | ||
| 210 | 210 | ||
| 211 | if(i == 5) { | 211 | if (i == 5) { |
| 212 | return -1; | 212 | return -1; |
| 213 | } | 213 | } |
| 214 | } | 214 | } |
| 215 | 215 | ||
| 216 | if(verbose) irecv_set_debug(client, verbose); | 216 | if (verbose) irecv_set_debug(client, verbose); |
| 217 | 217 | ||
| 218 | switch(action) { | 218 | switch (action) { |
| 219 | case kResetDevice: | 219 | case kResetDevice: |
| 220 | irecv_reset(client); | 220 | irecv_reset(client); |
| 221 | break; | 221 | break; |
| @@ -231,9 +231,9 @@ int main(int argc, char** argv) { | |||
| 231 | break; | 231 | break; |
| 232 | 232 | ||
| 233 | case kSendExploit: | 233 | case kSendExploit: |
| 234 | if(argument != NULL) { | 234 | if (argument != NULL) { |
| 235 | error = irecv_send_file(client, argument); | 235 | error = irecv_send_file(client, argument); |
| 236 | if(error != IRECV_E_SUCCESS) { | 236 | if (error != IRECV_E_SUCCESS) { |
| 237 | debug("%s\n", irecv_strerror(error)); | 237 | debug("%s\n", irecv_strerror(error)); |
| 238 | break; | 238 | break; |
| 239 | } | 239 | } |
