Improve cmd parameter parsing and handling

This commit is contained in:
Nils Faerber 2011-11-13 13:49:10 +01:00
parent c0807b3785
commit 79e351fe38
2 changed files with 111 additions and 43 deletions

View file

@ -90,6 +90,7 @@ int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char op
while (((ret = write(mwdevice->mw_fd, frame, tlen)) >= 0) && (tlen > 0)) while (((ret = write(mwdevice->mw_fd, frame, tlen)) >= 0) && (tlen > 0))
tlen -= ret; tlen -= ret;
fsync(mwdevice->mw_fd);
if (MW_FRAME_DELAY) if (MW_FRAME_DELAY)
usleep(MW_FRAME_DELAY); usleep(MW_FRAME_DELAY);

153
mw_main.c
View file

@ -471,65 +471,130 @@ void do_weather(mwdata_t *mwdata)
void print_help(void) void print_help(void)
{ {
g_print("Not yet implemented..\n");
}
#define PARAMS_MAX 8
#define PARAMS_LEN 16
static int separate_params(char *params[], char *pbuf)
{
char *ret;
char tbuf[128];
int cnt = 0;
memset(tbuf, 0, 128);
strncpy(tbuf, pbuf, strlen(pbuf));
// strdump(tbuf, strlen(tbuf));
/* we know we do have at least one parameter */
ret = strtok(tbuf, " ");
while (ret != NULL) {
// dbg_printf("tok '%s'\n", ret);
strncpy(params[cnt++], ret, PARAMS_LEN-1);
if (cnt > PARAMS_MAX)
break;
ret = strtok(NULL, " ");
}
return cnt;
}
static void params_free(char **params)
{
int i;
for (i=0; i < PARAMS_MAX; i++)
free(params[i]);
} }
void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata) void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
{ {
unsigned char mdata[32]; unsigned char mdata[32];
unsigned int intopt; unsigned int intopt;
char cmd[128], pbuf[128], *params[PARAMS_MAX];
int i, pcnt;
char *inbuf = cmdline;
fprintf(stderr, "command: '%s'\n", cmdline); g_print("command: '%s'\n", cmdline);
if (strncmp(cmdline, "quit", 4) == 0) { for (i=0; i<PARAMS_MAX; i++) {
params[i] = malloc(PARAMS_LEN);
memset(params[i], 0, PARAMS_LEN);
}
memset(cmd, 0, 128);
memset(pbuf, 0, 128);
i = 0;
do {
char c;
c = *(inbuf++);
cmd[i++] = c;
} while (*inbuf != 0 && *inbuf != 0x20);
i = 0;
if (inbuf != NULL && *inbuf == 0x20) {
inbuf++;
do {
pbuf[i++] = *(inbuf++);
} while (*inbuf != 0);
};
if (strlen(pbuf) > 0)
pcnt = separate_params(params, pbuf);
else
pcnt = 0;
if (strncmp(cmd, "quit", 4) == 0) {
//close(mw_fd); //close(mw_fd);
//exit(0); //exit(0);
/* just in case we quit and show another screen */ /* just in case we quit and show another screen */
mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0); mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
params_free(params);
g_main_loop_quit (mwdata->mloop); g_main_loop_quit (mwdata->mloop);
} }
if (strncmp(cmdline, "help", 4) == 0) { if (strncmp(cmd, "help", 4) == 0) {
print_help(); print_help();
} }
if (strncmp(cmdline, "ahand", 5) == 0) { if (strncmp(cmd, "ahand", 5) == 0) {
intopt = atoi(cmdline+5); if (pcnt > 0) {
g_print("Advance analog hands by %d minutes\n", intopt); intopt = atoi(params[0]); /*atoi(cmdline+5);*/
mdata[0] = intopt / 60; g_print("Advance analog hands by %d minutes\n", intopt);
mdata[1] = intopt % 60; mdata[0] = intopt / 60;
mdata[2] = 0; mdata[1] = intopt % 60;
mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3); mdata[2] = 0;
mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3);
} else
g_print("missing argument\n ahand # - # = no. of minutes\n");
} }
if (strncmp(cmdline, "srtc", 4) == 0) { if (strncmp(cmd, "srtc", 4) == 0) {
time_t mtime; time_t mtime;
struct tm mtm; struct tm mtm;
mtime = time(NULL); mtime = time(NULL);
localtime_r(&mtime, &mtm); localtime_r(&mtime, &mtm);
fprintf(stderr, "Setting RTC from system time..."); g_print("Setting RTC from system time...");
mw_set_rtc(&mwdata->mwdevice, &mtm); mw_set_rtc(&mwdata->mwdevice, &mtm);
fprintf(stderr, "OK\n"); g_print("OK\n");
} }
if (strncmp(cmdline, "grtc", 4) == 0) { if (strncmp(cmd, "grtc", 4) == 0) {
mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0); mw_send_frame(&mwdata->mwdevice, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0);
} }
if (strncmp(cmdline, "gistr", 5) == 0) { if (strncmp(cmd, "gistr", 5) == 0) {
intopt = cmdline[6]-0x30; if (pcnt > 0)
if (intopt>=0 && intopt <=3) intopt = atoi(params[0]);
if (intopt >= 0 && intopt <= 3)
mdata[0] = intopt; mdata[0] = intopt;
else else
mdata[0] = 0; mdata[0] = 0;
mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1); mw_send_frame(&mwdata->mwdevice, MW_GET_INFORMATION_STRING, 0, mdata, 1);
} }
if (strncmp(cmdline, "gdtype", 6) == 0) { if (strncmp(cmd, "gdtype", 6) == 0) {
mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0); mw_send_frame(&mwdata->mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0);
} }
if (strncmp(cmdline, "rvbat", 5) == 0) { if (strncmp(cmd, "rvbat", 5) == 0) {
mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0); mw_send_frame(&mwdata->mwdevice, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0);
} }
if (strncmp(cmdline, "rlight", 6) == 0) { if (strncmp(cmd, "rlight", 6) == 0) {
mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0); mw_send_frame(&mwdata->mwdevice, MW_READ_LIGHT_SENSOR_MSG, 0, NULL, 0);
} }
if (strncmp(cmdline, "modecfg", 6) == 0) { if (strncmp(cmd, "modecfg", 6) == 0) {
mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1); mw_configure_watch_mode(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0, 4, 1);
mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0); mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0);
} }
@ -545,67 +610,69 @@ void process_cmd(char *cmdline, int clinep, mwdata_t *mwdata)
mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5); mw_send_frame(&mwdata->mwdevice, MW_READ_BUTTON_CONFIG, 0, mdata, 5);
} }
#endif #endif
if (strncmp(cmdline, "svib", 4) == 0) { if (strncmp(cmd, "svib", 4) == 0) {
mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5); mw_set_vibrate_mode(&mwdata->mwdevice, 1, 300, 300, 5);
} }
if (strncmp(cmdline, "tbmp", 4) == 0) { if (strncmp(cmd, "tbmp", 4) == 0) {
bitmap_test(&mwdata->mwdevice); bitmap_test(&mwdata->mwdevice);
} }
if (strncmp(cmdline, "t2bmp", 5) == 0) { if (strncmp(cmd, "t2bmp", 5) == 0) {
bitmap_test2(&mwdata->mwdevice); bitmap_test2(&mwdata->mwdevice);
} }
if (strncmp(cmdline, "text", 4) == 0) { if (strncmp(cmd, "text", 4) == 0) {
text_test(&mwdata->mwdevice); text_test(&mwdata->mwdevice);
} }
if (strncmp(cmdline, "tline", 5) == 0) { if (strncmp(cmd, "tline", 5) == 0) {
line_test(&mwdata->mwdevice); line_test(&mwdata->mwdevice);
} }
if (strncmp(cmdline, "rbmp", 4) == 0) { if (strncmp(cmd, "rbmp", 4) == 0) {
bitmap_read(&mwdata->mwdevice); bitmap_read(&mwdata->mwdevice);
} }
if (strncmp(cmdline, "tnote", 5) == 0) { if (strncmp(cmd, "tnote", 5) == 0) {
// test_notification(&mwdata->mwdevice); // test_notification(&mwdata->mwdevice);
mw_do_notification(&mwdata->mwdevice, "TestNotification", "This is a pretty long text that needs to be broken and torn", 1); mw_do_notification(&mwdata->mwdevice, "TestNotification", "This is a pretty long text that needs to be broken and torn", 1);
} }
if (strncmp(cmdline, "tapp", 4) == 0) { if (strncmp(cmd, "tapp", 4) == 0) {
test_application(&mwdata->mwdevice); test_application(&mwdata->mwdevice);
} }
if (strncmp(cmdline, "eoled", 5) == 0) { if (strncmp(cmd, "eoled", 5) == 0) {
intopt = cmdline[6]-0x30; if (pcnt > 0) {
if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER) intopt = atoi(params[0]);
mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0); if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER)
mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0);
}
} }
if (strncmp(cmdline, "toled", 5) == 0) { if (strncmp(cmd, "toled", 5) == 0) {
intopt = cmdline[6]-0x30; intopt = atoi(params[0]);
test_oled(&mwdata->mwdevice, intopt); test_oled(&mwdata->mwdevice, intopt);
//mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10); //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10);
//mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0); //mw_send_frame(mwdevice, MW_UPDATE_OLED_DISPLAY_MSG, 0, NULL, 0);
} }
if (strncmp(cmdline, "cal", 3) == 0) { if (strncmp(cmd, "cal", 3) == 0) {
draw_idle_calendar(&mwdata->mwdevice); draw_idle_calendar(&mwdata->mwdevice);
} }
if (strncmp(cmdline, "wet", 3) == 0) { if (strncmp(cmd, "wet", 3) == 0) {
do_weather(mwdata); do_weather(mwdata);
} }
if (strncmp(cmdline, "c24", 3) == 0) { if (strncmp(cmd, "c24", 3) == 0) {
mdata[0] = MW_RTC_CLOCK_24HR; mdata[0] = MW_RTC_CLOCK_24HR;
mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_WRITE, MW_NVAL_TIME_FORMAT, 1, mdata); mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_WRITE, MW_NVAL_TIME_FORMAT, 1, mdata);
} }
if (strncmp(cmdline, "g24", 3) == 0) { if (strncmp(cmd, "g24", 3) == 0) {
mdata[0] = 0; mdata[0] = 0;
mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_TIME_FORMAT, 1, mdata); mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_TIME_FORMAT, 1, mdata);
} }
if (strncmp(cmdline, "sdm", 3) == 0) { if (strncmp(cmd, "sdm", 3) == 0) {
mdata[0] = MW_RTC_DATE_DDMM; mdata[0] = MW_RTC_DATE_DDMM;
mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_WRITE, MW_NVAL_DATE_FORMAT, 1, mdata); mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_WRITE, MW_NVAL_DATE_FORMAT, 1, mdata);
} }
if (strncmp(cmdline, "gdm", 3) == 0) { if (strncmp(cmd, "gdm", 3) == 0) {
mdata[0] = 0; mdata[0] = 0;
mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_TIME_FORMAT, 1, mdata); mw_nval_operation(&mwdata->mwdevice, MW_NVAL_OPERATION_READ, MW_NVAL_DATE_FORMAT, 1, mdata);
} }
params_free(params);
} }
int feed_menu(mwdata_t *mdata) int feed_menu(mwdata_t *mdata)
{ {
int rcvd; int rcvd;