diff --git a/src/image.c b/src/image.c index 29c5b07..a50310e 100644 --- a/src/image.c +++ b/src/image.c @@ -2,9 +2,56 @@ // Created by nock on 26/03/19. // +#include #include -#include "image.h" +#include +#include -void image_main(int argc, char **argv){ - printf("Doin' that image thang, boss\n"); +#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(); + } } \ No newline at end of file diff --git a/src/image.h b/src/image.h index 80b66e0..b1e5b74 100644 --- a/src/image.h +++ b/src/image.h @@ -5,6 +5,10 @@ #ifndef UMCUMGR_IMAGE_H #define UMCUMGR_IMAGE_H -void image_main(int, char **); +#include "nmgr.h" + +#define IMAGE_LIST_CMD "AAsAAAABAAFCAKD1Mw==" + +void image_main(int, int, char **); #endif //UMCUMGR_IMAGE_H diff --git a/src/main.c b/src/main.c index 78af9a6..9af177d 100644 --- a/src/main.c +++ b/src/main.c @@ -6,12 +6,13 @@ #include "cbor.h" #include "image.h" +#include "termios_util.h" #define DEFAULT_SERIAL_PORT "/dev/ttyUSB0" int main(int argc, char **argv) { int c; - char *speed = "115200"; + char *speed = NULL; char *serial_port = DEFAULT_SERIAL_PORT; while ((c = getopt(argc, argv, "c:s:")) != -1) { switch (c) { @@ -27,18 +28,30 @@ int main(int argc, char **argv) { int serial_fd = open(serial_port, O_RDWR | O_NOCTTY | O_NDELAY); if (serial_fd < 0) { - printf("Unable to open %s: %s\n", serial_port, strerror(errno)); + fprintf(stderr, "Unable to open %s: %s\n", serial_port, strerror(errno)); return 1; } + printf("Opened %s\n", serial_port); struct termios t_options; - tcgetattr(serial_fd, &t_options); - cfsetspeed(&t_options, ); + + if (tcgetattr(serial_fd, &t_options) < 0) { + fprintf(stderr, "Failed to get termios attrs.\n"); + } + + if (cfsetspeed(&t_options, speed_from_string(speed)) < 0) { + fprintf(stderr, "Failed to set port speed.\n", speed); + return 2; + } + + if (tcsetattr(serial_fd, TCSANOW, &t_options) < 0){ + fprintf(stderr, "Failed to set termios attrs.\n"); + } uint8_t rem = (uint8_t)(argc - optind); if (rem) { if (strcmp(argv[optind], "image") == 0) { - image_main(rem, &argv[optind]); + image_main(serial_fd, rem, &argv[optind]); } } } \ No newline at end of file diff --git a/src/nmgr.h b/src/nmgr.h new file mode 100644 index 0000000..afc3812 --- /dev/null +++ b/src/nmgr.h @@ -0,0 +1,45 @@ +// +// Created by nock on 2019-05-01. +// + +#ifndef UMCUMGR_NMGR_H +#define UMCUMGR_NMGR_H + +#include + +#define SHELL_NLIP_PKT_START1 6 +#define SHELL_NLIP_PKT_START2 9 + +#define SHELL_NLIP_DATA_START1 4 +#define SHELL_NLIP_DATA_START2 20 + +struct nmgr_hdr { + uint8_t nh_op; /* NMGR_OP_XXX */ + uint8_t nh_flags; + uint16_t nh_len; /* length of the payload */ + uint16_t nh_group; /* NMGR_GROUP_XXX */ + uint8_t nh_seq; /* sequence number */ + uint8_t nh_id; /* message ID within group */ +}; + +enum nmgr_op { + NMGR_OP_READ = 0, + NMGR_OP_WRITE = 2 +}; + +enum mgmt_group_id { + MGMT_GROUP_ID_DEFAULT = 0, + MGMT_GROUP_ID_IMAGE = 1 +}; + +enum nmgr_id { + NMGR_ID_CONS_ECHO_CTRL = 1, + NMGR_ID_RESET = 5, +}; + +enum imgmgr_nmgr_id { + IMGMGR_NMGR_ID_STATE = 0, + IMGMGR_NMGR_ID_UPLOAD = 1 +}; + +#endif //UMCUMGR_NMGR_H diff --git a/src/termios_util.c b/src/termios_util.c index b5d452f..75d297e 100644 --- a/src/termios_util.c +++ b/src/termios_util.c @@ -3,6 +3,7 @@ // #include +#include #include #include "termios_util.h"