From: Nils Faerber Date: Sun, 13 Nov 2011 12:49:10 +0000 (+0100) Subject: Improve cmd parameter parsing and handling X-Git-Url: https://git.kernelconcepts.de/?p=metawatch.git;a=commitdiff_plain;h=79e351fe38d787ba602b20a3b13a250a0dfe4c00;hp=c0807b3785b36e8b31b42964cd8fe02d9c43a435 Improve cmd parameter parsing and handling --- diff --git a/metawatch.c b/metawatch.c index e487e35..e093168 100644 --- a/metawatch.c +++ b/metawatch.c @@ -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)) tlen -= ret; + fsync(mwdevice->mw_fd); if (MW_FRAME_DELAY) usleep(MW_FRAME_DELAY); diff --git a/mw_main.c b/mw_main.c index b9a547c..a799dd8 100644 --- a/mw_main.c +++ b/mw_main.c @@ -471,65 +471,130 @@ void do_weather(mwdata_t *mwdata) 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) { unsigned char mdata[32]; 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); + + for (i=0; i 0) + pcnt = separate_params(params, pbuf); + else + pcnt = 0; - if (strncmp(cmdline, "quit", 4) == 0) { + if (strncmp(cmd, "quit", 4) == 0) { //close(mw_fd); //exit(0); /* just in case we quit and show another screen */ mw_update_display(&mwdata->mwdevice, MW_SCREEN_MODE_IDLE, 0); + params_free(params); g_main_loop_quit (mwdata->mloop); } - if (strncmp(cmdline, "help", 4) == 0) { + if (strncmp(cmd, "help", 4) == 0) { print_help(); } - if (strncmp(cmdline, "ahand", 5) == 0) { - intopt = atoi(cmdline+5); - g_print("Advance analog hands by %d minutes\n", intopt); - mdata[0] = intopt / 60; - mdata[1] = intopt % 60; - mdata[2] = 0; - mw_send_frame(&mwdata->mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3); - } - if (strncmp(cmdline, "srtc", 4) == 0) { + if (strncmp(cmd, "ahand", 5) == 0) { + if (pcnt > 0) { + intopt = atoi(params[0]); /*atoi(cmdline+5);*/ + g_print("Advance analog hands by %d minutes\n", intopt); + mdata[0] = intopt / 60; + mdata[1] = intopt % 60; + 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(cmd, "srtc", 4) == 0) { time_t mtime; struct tm mtm; mtime = time(NULL); localtime_r(&mtime, &mtm); - fprintf(stderr, "Setting RTC from system time..."); + g_print("Setting RTC from system time..."); 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); } - if (strncmp(cmdline, "gistr", 5) == 0) { - intopt = cmdline[6]-0x30; - if (intopt>=0 && intopt <=3) + if (strncmp(cmd, "gistr", 5) == 0) { + if (pcnt > 0) + intopt = atoi(params[0]); + if (intopt >= 0 && intopt <= 3) mdata[0] = intopt; else mdata[0] = 0; 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); } - 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); } - 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); } - 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_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); } #endif - if (strncmp(cmdline, "svib", 4) == 0) { + if (strncmp(cmd, "svib", 4) == 0) { 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); } - if (strncmp(cmdline, "t2bmp", 5) == 0) { + if (strncmp(cmd, "t2bmp", 5) == 0) { bitmap_test2(&mwdata->mwdevice); } - if (strncmp(cmdline, "text", 4) == 0) { + if (strncmp(cmd, "text", 4) == 0) { text_test(&mwdata->mwdevice); } - if (strncmp(cmdline, "tline", 5) == 0) { + if (strncmp(cmd, "tline", 5) == 0) { line_test(&mwdata->mwdevice); } - if (strncmp(cmdline, "rbmp", 4) == 0) { + if (strncmp(cmd, "rbmp", 4) == 0) { bitmap_read(&mwdata->mwdevice); } - if (strncmp(cmdline, "tnote", 5) == 0) { + if (strncmp(cmd, "tnote", 5) == 0) { // test_notification(&mwdata->mwdevice); 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); } - if (strncmp(cmdline, "eoled", 5) == 0) { - intopt = cmdline[6]-0x30; - if (intopt == MW_OLED_UPPER || intopt == MW_OLED_LOWER) - mw_send_frame(&mwdata->mwdevice, MW_ENABLE_OLED_DISPLAY_MSG, intopt, NULL, 0); + if (strncmp(cmd, "eoled", 5) == 0) { + if (pcnt > 0) { + intopt = atoi(params[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) { - intopt = cmdline[6]-0x30; + if (strncmp(cmd, "toled", 5) == 0) { + intopt = atoi(params[0]); test_oled(&mwdata->mwdevice, intopt); //mw_write_oled_buffer(mwdevice, 0, 80, 0, mdata, 10); //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); } - if (strncmp(cmdline, "wet", 3) == 0) { + if (strncmp(cmd, "wet", 3) == 0) { do_weather(mwdata); } - if (strncmp(cmdline, "c24", 3) == 0) { + if (strncmp(cmd, "c24", 3) == 0) { mdata[0] = MW_RTC_CLOCK_24HR; 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; 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; 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; - 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 rcvd;