1
0
Fork 0
umcumgr/src/image.c

57 lines
1.5 KiB
C
Raw Normal View History

//
// Created by nock on 26/03/19.
//
#include <errno.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "image.h"
#include "nmgr.h"
void image_help(void){
printf("umcumgr image [list|upload|reset]\n");
}
static char *hex(char in) {
static char out[3] = {0};
static char lookup[] = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
};
out[0] = lookup[in >> 4];
out[1] = lookup[in & 0xf];
out[2] = 0;
return out;
}
void image_main(int fd, int argc, char **argv){
if (!strcmp(argv[1], "list")) {
int rc = write(fd, &(char []){SHELL_NLIP_PKT_START1, SHELL_NLIP_PKT_START2}, 2);
rc = write(fd, IMAGE_LIST_CMD, sizeof(IMAGE_LIST_CMD)-1);
if (rc < sizeof(IMAGE_LIST_CMD)-1) {
fprintf(stderr, "Failed to send command: %s", strerror(errno));
}
rc = write(fd, "\n", 1);
//return;
char tmp[256] = {0};
while (1) {
if (read(fd, &tmp, 255) < 0) {
if (errno == EAGAIN) {
continue;
}
fprintf(stderr, "Failed to read from port: %s\n", strerror(errno));
break;
}
printf("%s ", tmp);
break;
}
} else if (!strcmp(argv[1], "upload")) {
printf("upload\n");
} else if (!strcmp(argv[1], "reset")) {
printf("reset\n");
} else {
image_help();
}
}