mirror of
git://projects.qi-hardware.com/ben-blinkenlights.git
synced 2024-11-27 15:41:53 +02:00
lpc111x-isp/lpc111x.c: add printf-style dialog() variants
This commit is contained in:
parent
c4d64fafa4
commit
0553fed424
@ -11,6 +11,8 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
|
|
||||||
|
#define _GNU_SOURCE /* for vasprintf */
|
||||||
|
#include <stdarg.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
@ -79,29 +81,29 @@ static void trace_in(const uint8_t *s, int len)
|
|||||||
/* ----- Dialog functions -------------------------------------------------- */
|
/* ----- Dialog functions -------------------------------------------------- */
|
||||||
|
|
||||||
|
|
||||||
static int dialog(const char *cmd, void *buf, int buf_len, int idle)
|
static int dialog_fixed(void *buf, int buf_len, int idle, const char *msg)
|
||||||
{
|
{
|
||||||
int cmd_len = strlen(cmd);
|
int msg_len = strlen(msg);
|
||||||
char *tx_buf = alloca(cmd_len+3);
|
char *tx_buf = alloca(msg_len+3);
|
||||||
uint8_t *rx_buf = alloca(cmd_len+2+buf_len);
|
uint8_t *rx_buf = alloca(msg_len+2+buf_len);
|
||||||
int got;
|
int got;
|
||||||
|
|
||||||
memcpy(tx_buf, cmd, cmd_len);
|
memcpy(tx_buf, msg, msg_len);
|
||||||
memcpy(tx_buf+cmd_len, "\r\n", 2);
|
memcpy(tx_buf+msg_len, "\r\n", 2);
|
||||||
|
|
||||||
if (TRACE_PROGRESS)
|
if (TRACE_PROGRESS)
|
||||||
trace_out(tx_buf, cmd_len+2);
|
trace_out(tx_buf, msg_len+2);
|
||||||
got = swuart_trx(tx_buf, cmd_len+2, rx_buf, cmd_len+2+buf_len,
|
got = swuart_trx(tx_buf, msg_len+2, rx_buf, msg_len+2+buf_len,
|
||||||
idle, idle);
|
idle, idle);
|
||||||
|
|
||||||
if (got < cmd_len+2) {
|
if (got < msg_len+2) {
|
||||||
if (TRACE_FATAL)
|
if (TRACE_FATAL)
|
||||||
trace_in(rx_buf, got);
|
trace_in(rx_buf, got);
|
||||||
fprintf(stderr, "response too short for echo\n");
|
fprintf(stderr, "response too short for echo\n");
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
if (memcmp(rx_buf, cmd, cmd_len) ||
|
if (memcmp(rx_buf, msg, msg_len) ||
|
||||||
rx_buf[cmd_len] != '\r' || rx_buf[cmd_len+1] != '\n') {
|
rx_buf[msg_len] != '\r' || rx_buf[msg_len+1] != '\n') {
|
||||||
if (TRACE_FATAL)
|
if (TRACE_FATAL)
|
||||||
trace_in(rx_buf, got);
|
trace_in(rx_buf, got);
|
||||||
fprintf(stderr, "echo mismatch\n");
|
fprintf(stderr, "echo mismatch\n");
|
||||||
@ -109,15 +111,37 @@ static int dialog(const char *cmd, void *buf, int buf_len, int idle)
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (TRACE_ECHO)
|
if (TRACE_ECHO)
|
||||||
trace_in(rx_buf, cmd_len+2);
|
trace_in(rx_buf, msg_len+2);
|
||||||
|
|
||||||
got -= cmd_len+2;
|
got -= msg_len+2;
|
||||||
memcpy(buf, rx_buf+cmd_len+2, got);
|
memcpy(buf, rx_buf+msg_len+2, got);
|
||||||
|
|
||||||
return got;
|
return got;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int vdialog(void *buf, int buf_len, int idle,
|
||||||
|
const char *cmd, va_list ap)
|
||||||
|
{
|
||||||
|
char *msg;
|
||||||
|
|
||||||
|
vasprintf(&msg, cmd, ap);
|
||||||
|
return dialog_fixed(buf, buf_len, idle, msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
static int dialog(void *buf, int buf_len, int idle, const char *cmd, ...)
|
||||||
|
{
|
||||||
|
va_list ap;
|
||||||
|
int res;
|
||||||
|
|
||||||
|
va_start(ap, cmd);
|
||||||
|
res = vdialog(buf, buf_len, idle, cmd, ap);
|
||||||
|
va_end(ap);
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
static int autobaud(void)
|
static int autobaud(void)
|
||||||
{
|
{
|
||||||
uint8_t reply[100];
|
uint8_t reply[100];
|
||||||
@ -140,7 +164,7 @@ static int autobaud(void)
|
|||||||
if (TRACE_PROGRESS)
|
if (TRACE_PROGRESS)
|
||||||
trace_in(reply, got);
|
trace_in(reply, got);
|
||||||
|
|
||||||
got = dialog(SYNC, reply, sizeof(reply), 100);
|
got = dialog(reply, sizeof(reply), 100, SYNC);
|
||||||
|
|
||||||
if (got == 4 && !memcmp(reply, "OK\r\n", 4)) {
|
if (got == 4 && !memcmp(reply, "OK\r\n", 4)) {
|
||||||
if (TRACE_PROGRESS)
|
if (TRACE_PROGRESS)
|
||||||
@ -191,7 +215,7 @@ static void start_isp(void)
|
|||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
got = dialog("12000", reply, sizeof(reply), 100);
|
got = dialog(reply, sizeof(reply), 100, "12000");
|
||||||
if (got != 4 || memcmp(reply, "OK\r\n", 4)) {
|
if (got != 4 || memcmp(reply, "OK\r\n", 4)) {
|
||||||
if (TRACE_FATAL)
|
if (TRACE_FATAL)
|
||||||
trace_in(reply, got);
|
trace_in(reply, got);
|
||||||
@ -239,7 +263,7 @@ int main(int argc, char **argv)
|
|||||||
|
|
||||||
start_isp();
|
start_isp();
|
||||||
|
|
||||||
got = dialog("J", reply, sizeof(reply), 100);
|
got = dialog(reply, sizeof(reply), 100, "J");
|
||||||
trace_in(reply, got);
|
trace_in(reply, got);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
|
Loading…
Reference in New Issue
Block a user