1
0
Fork 0

Initial support for sending the list command

This commit is contained in:
Shawn Nock 2019-05-01 16:25:19 +00:00
parent c0b555755b
commit 1ee27ccced
5 changed files with 119 additions and 9 deletions

View File

@ -2,9 +2,56 @@
// Created by nock on 26/03/19. // Created by nock on 26/03/19.
// //
#include <errno.h>
#include <stdio.h> #include <stdio.h>
#include "image.h" #include <string.h>
#include <unistd.h>
void image_main(int argc, char **argv){ #include "image.h"
printf("Doin' that image thang, boss\n"); #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();
}
} }

View File

@ -5,6 +5,10 @@
#ifndef UMCUMGR_IMAGE_H #ifndef UMCUMGR_IMAGE_H
#define 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 #endif //UMCUMGR_IMAGE_H

View File

@ -6,12 +6,13 @@
#include "cbor.h" #include "cbor.h"
#include "image.h" #include "image.h"
#include "termios_util.h"
#define DEFAULT_SERIAL_PORT "/dev/ttyUSB0" #define DEFAULT_SERIAL_PORT "/dev/ttyUSB0"
int main(int argc, char **argv) { int main(int argc, char **argv) {
int c; int c;
char *speed = "115200"; char *speed = NULL;
char *serial_port = DEFAULT_SERIAL_PORT; char *serial_port = DEFAULT_SERIAL_PORT;
while ((c = getopt(argc, argv, "c:s:")) != -1) { while ((c = getopt(argc, argv, "c:s:")) != -1) {
switch (c) { switch (c) {
@ -27,18 +28,30 @@ int main(int argc, char **argv) {
int serial_fd = open(serial_port, O_RDWR | O_NOCTTY | O_NDELAY); int serial_fd = open(serial_port, O_RDWR | O_NOCTTY | O_NDELAY);
if (serial_fd < 0) { 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; return 1;
} }
printf("Opened %s\n", serial_port);
struct termios t_options; 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); uint8_t rem = (uint8_t)(argc - optind);
if (rem) { if (rem) {
if (strcmp(argv[optind], "image") == 0) { if (strcmp(argv[optind], "image") == 0) {
image_main(rem, &argv[optind]); image_main(serial_fd, rem, &argv[optind]);
} }
} }
} }

45
src/nmgr.h Normal file
View File

@ -0,0 +1,45 @@
//
// Created by nock on 2019-05-01.
//
#ifndef UMCUMGR_NMGR_H
#define UMCUMGR_NMGR_H
#include <stdint.h>
#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

View File

@ -3,6 +3,7 @@
// //
#include <errno.h> #include <errno.h>
#include <stdlib.h>
#include <termios.h> #include <termios.h>
#include "termios_util.h" #include "termios_util.h"