USB comms: part 4 towards @micolous PR #463

* make uart_communication(), storeCommand() and getCommand() static in comms.c
* move receiver thread creation and respective mutexes to comms.c
* add mutex and signal for tx buffer
* use comms.c for flasher as well
* remove comm functions from client/proxmark3.h
* this completes isolating all USB communication related functions in comms.c
* don't assume a port to be defined by a name. Change parameter in OpenProxmark() to void*
* comms.c: set sp and serial_port_name to NULL when offline
This commit is contained in:
pwpiwi 2018-06-03 14:25:20 +02:00 committed by GitHub
commit ad939de501
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
50 changed files with 274 additions and 248 deletions

View file

@ -10,18 +10,14 @@
#include <stdlib.h>
#include <string.h>
#include <inttypes.h>
#include <pthread.h>
#include "proxmark3.h"
#include "util.h"
#include "util_posix.h"
#include "flash.h"
#include "uart.h"
#include "comms.h"
#include "usb_cmd.h"
#ifdef _WIN32
# define unlink(x)
#else
# include <unistd.h>
#endif
void cmd_debug(UsbCommand* UC) {
// Debug
@ -37,29 +33,6 @@ void cmd_debug(UsbCommand* UC) {
printf("...\n");
}
void SendCommand(UsbCommand* txcmd) {
// printf("send: ");
// cmd_debug(txcmd);
if (!uart_send(sp,(uint8_t*)txcmd,sizeof(UsbCommand))) {
printf("Sending bytes to proxmark failed\n");
exit(1);
}
}
void ReceiveCommand(UsbCommand* rxcmd) {
uint8_t* prxcmd = (uint8_t*)rxcmd;
uint8_t* prx = prxcmd;
size_t rxlen;
while (true) {
if (uart_receive(sp, prx, sizeof(UsbCommand) - (prx-prxcmd), &rxlen)) {
prx += rxlen;
if ((prx-prxcmd) >= sizeof(UsbCommand)) {
return;
}
}
}
}
static void usage(char *argv0)
{
fprintf(stderr, "Usage: %s <port> [-b] image.elf [image.elf...]\n\n", argv0);
@ -77,7 +50,7 @@ static void usage(char *argv0)
int main(int argc, char **argv)
{
int can_write_bl = 0;
int can_write_bl = false;
int num_files = 0;
int res;
flash_file_t files[MAX_FILES];
@ -92,7 +65,7 @@ int main(int argc, char **argv)
for (int i = 2; i < argc; i++) {
if (argv[i][0] == '-') {
if (!strcmp(argv[i], "-b")) {
can_write_bl = 1;
can_write_bl = true;
} else {
usage(argv[0]);
return -1;
@ -110,12 +83,12 @@ int main(int argc, char **argv)
char* serial_port_name = argv[1];
fprintf(stderr,"Waiting for Proxmark to appear on %s", serial_port_name);
do {
msleep(1000);
fprintf(stderr, ".");
} while (!OpenProxmark(0, serial_port_name));
fprintf(stderr," Found.\n");
if (!OpenProxmark(serial_port_name, true, 120, true)) { // wait for 2 minutes
fprintf(stderr, "Could not find Proxmark on %s.\n\n", serial_port_name);
return -1;
} else {
fprintf(stderr," Found.\n");
}
res = flash_start_flashing(can_write_bl, serial_port_name);
if (res < 0)
@ -137,7 +110,8 @@ int main(int argc, char **argv)
if (res < 0)
return -1;
CloseProxmark(serial_port_name);
// Stop the command thread.
CloseProxmark();
fprintf(stderr, "All done.\n\n");
fprintf(stderr, "Have a nice day!\n");