X-Git-Url: https://git.kernelconcepts.de/?p=metawatch.git;a=blobdiff_plain;f=metawatch.c;h=e093168d833fff9bb3bdf44b6fa8cd10ffb9fb9c;hp=0b33d05196961e936f987ca002384c357fdadd25;hb=5668a2fcb129077040e1856c5a3d1595c7ef06e7;hpb=c8307dc90a7315f574760f6de5b588a419f1511a diff --git a/metawatch.c b/metawatch.c index 0b33d05..e093168 100644 --- a/metawatch.c +++ b/metawatch.c @@ -1,8 +1,22 @@ /* - * (c) 2011 Siegen, Germany by Nils Faerber + * (c) 2011 Siegen, Germany by Nils Faerber * - * license GPL + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. */ + #include #include #include @@ -12,11 +26,35 @@ #include #include #include +#include +#include "metawatch.h" #include "metawatch_protocol.h" #include "crc16ccitt.h" +#ifdef DEBUG +const char *mw_screen_mode_names[] = { + "idle screen", + "application screen", + "notification screen", + "scroll" +}; + +const char *mw_status_string[] = { + "Reserved", + "Mode Change", + "Display Timeout" +}; +#endif + + +#define MW_FRAME_DELAY 0x00 + +/* ---------------------------------------------------------------------- + * Debugging helpers + * ---------------------------------------------------------------------- */ + void dump_frame(unsigned char *frame, int len) { int i; @@ -27,7 +65,7 @@ void dump_frame(unsigned char *frame, int len) } -int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len) +int mw_send_frame(mwdevice_t *mwdevice, unsigned char msg_type, unsigned char options, unsigned char *data, unsigned char len) { unsigned short crc; unsigned char frame[64]; @@ -45,47 +83,64 @@ int mw_send_frame(int mw_fd, unsigned char msg_type, unsigned char options, unsi crc = crc16ccitt(frame, len+4); *(unsigned short *)(frame+len+4) = crc; +#ifdef DEBUG dump_frame(frame, tlen); +#endif - while (((ret = write(mw_fd, frame, tlen)) >= 0) && (tlen > 0)) + 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); + if (tlen == 0 && ret >= 0) return 0; else return ret; } -/* - * host to watch commands - */ -void mw_set_rtc(int mw_fd, unsigned char clk1224, unsigned char date_fmt) +/* ---------------------------------------------------------------------- + * Host to watch commands + * ---------------------------------------------------------------------- */ + + +int mw_set_rtc(mwdevice_t *mwdevice, struct tm *mw_tm) { - time_t mtime; - struct tm mtm; unsigned short year; unsigned char data[32]; - mtime = time(NULL); - localtime_r(&mtime, &mtm); - - year = mtm.tm_year + 1900; + year = mw_tm->tm_year + 1900; data[0] = (year & 0x0f00) >> 8; data[1] = (year & 0x00ff); - data[2] = mtm.tm_mon + 1; - data[3] = mtm.tm_mday; - data[4] = mtm.tm_wday; - data[5] = mtm.tm_hour; - data[6] = mtm.tm_min; - data[7] = mtm.tm_sec; - data[8] = clk1224; - data[9] = date_fmt; + data[2] = mw_tm->tm_mon + 1; + data[3] = mw_tm->tm_mday; + data[4] = mw_tm->tm_wday; + data[5] = mw_tm->tm_hour; + data[6] = mw_tm->tm_min; + data[7] = mw_tm->tm_sec; + + return mw_send_frame(mwdevice, MW_SET_REAL_TIME_CLOCK, 0, data, 8); +} + +int mw_nval_operation(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata) +{ + unsigned char data[32]; - mw_send_frame(mw_fd, MW_SET_REAL_TIME_CLOCK, 0, data, 10); + data[0] = (identifier & 0x00ff); + data[1] = (identifier & 0xff00) >> 8; + data[2] = size; + if (size > 0 && mdata != NULL) + memcpy((data+3), mdata, size); + + if (operation == MW_NVAL_OPERATION_READ || operation == MW_NVAL_OPERATION_WRITE) { + return mw_send_frame(mwdevice, MW_NVAL_OPERATION, operation, data, 3+size); + } else + return -1; } -void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles) +int mw_set_vibrate_mode(mwdevice_t *mwdevice, unsigned char enable, unsigned short on_time, unsigned short off_time, unsigned char cycles) { unsigned char mdata[7]; @@ -93,35 +148,233 @@ void mw_set_vibrate_mode(int mw_fd, unsigned char enable, unsigned short on_time *(unsigned short *)(mdata+1) = on_time; /* miliseconds */ *(unsigned short *)(mdata+3) = off_time; /* miliseconds */ mdata[5] = cycles; - mw_send_frame(mw_fd, MW_SET_VIBRATE_MODE, 0, mdata, 6); + + return mw_send_frame(mwdevice, MW_SET_VIBRATE_MODE, 0, mdata, 6); } -void mw_configure_watch_mode(int mw_fd, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert) +int mw_configure_watch_mode(mwdevice_t *mwdevice, unsigned char mode, unsigned char save, unsigned char timeout, unsigned char invert) { unsigned char mdata[3]; mdata[0] = timeout; /* seconds */ mdata[1] = invert; /* 0=normal, 1=invert */ - mw_send_frame(mw_fd, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 3), mdata, 2); + + return mw_send_frame(mwdevice, MW_CONFIGURE_MODE, (mode & 0x0f) | ((save & 0x01) << 4), mdata, 2); } -void mw_update_display(int mw_fd, unsigned char mode, unsigned char copy) +int mw_update_display(mwdevice_t *mwdevice, unsigned char mode, unsigned char copy) { - mw_send_frame(mw_fd, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 3), NULL, 0); + mwdevice->screen_mode = (mode & 0x0f); + return mw_send_frame(mwdevice, MW_UPDATE_DISPLAY, (mode & 0x0f) | ((copy & 0x01) << 4), NULL, 0); +} + +int mw_load_template(mwdevice_t *mwdevice, unsigned char mode, unsigned char template_select) +{ + return mw_send_frame(mwdevice, MW_LOAD_TEMPLATE, (mode & 0x0f), &template_select, 1); } /* - * watch responses, events or notifications + * send line for screen-mode mode from *buffer to watch, starting at display row row_offset + * this is only for digital LCD watch */ -void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len) +int mw_write_buffer(mwdevice_t *mwdevice, + unsigned char mode, + unsigned char numlines, /* number of lines, 0=two lines or 1=one line */ + unsigned char row_offset, /* start at row_offset in display, e.g. lower part in idle @31 */ + unsigned char *buffer, int buflen) +{ + unsigned char mdata[32]; + + if (mwdevice->devtype != MW_DEVICE_TYPE_DIGITAL && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_DIGI) + return -1; + + buflen = 12 * (buflen / 12); /* crop to 12 bytes */ + if ((numlines == 0 && buflen < 12) || (numlines == 1 && buflen < 24)) { + fprintf(stderr, "mw_write_buffer: bufferlen does not match number of lines\n"); + return -1; + }; + memset(mdata, 0, 32); + mdata[0] = row_offset; + memcpy((mdata+1), buffer, 12); + if (numlines == 0) { + mdata[13] = row_offset+1; + memcpy((mdata+14), (buffer+12), 12); + }; + + return mw_send_frame(mwdevice, MW_WRITE_BUFFER, (mode & 0x0f) | (((numlines & 0x01)<< 4) & 0x10), mdata, numlines ? 13 : 26); +} + +/* + Options: + B0 : row select, 0 first row, 1 second row + B1 : display select, 0 upper OLED, 1 lower OLED + B2 : if 1 send an event upon completion + B3..4: scroll type, + B5..7: unused + Scroll types: + B0: First buffer in a chain of scroll buffers + B1: Any buffer except first or last + B2: Last buffer of a chain + B3: reserved / unused + Payload: + 0: Start index col + 1..: data +*/ +int mw_write_oled_buffer(mwdevice_t *mwdevice, + unsigned char mode, /* idle or scroll */ + unsigned char oled, /* which OLED */ + unsigned char numcols, + unsigned char col_index, /* starting index */ + unsigned char *buffer, int buflen) +{ + unsigned char mdata[32]; + int i; + + if (mwdevice->devtype != MW_DEVICE_TYPE_ANA_DIGI && mwdevice->devtype != MW_DEVICE_TYPE_DEVB_ANA_DIGI) + return -1; + + fprintf(stderr, "write oled buf len = %d\n", buflen); + /* lower row first since display wil be updated after completion of upper row */ + if (buflen > 80) { + for (i=80; i 12) + return -1; + if (minutes > 60) + return -1; + if (seconds > 60) + return -1; + + mdata[0] = hours; + mdata[1] = minutes; + mdata[2] = seconds; + + return mw_send_frame(mwdevice, MW_ADVANCE_WATCH_HANDS, 0, mdata, 3); +} + +int mw_battery_configuration(mwdevice_t *mwdevice, unsigned char warn_lvl, unsigned char bt_off_lvl) +{ + unsigned char mdata[4]; + + if (warn_lvl < 28 || warn_lvl > 42) + return -1; + if (bt_off_lvl < 28 || bt_off_lvl > 42) + return -1; + + mdata[0] = warn_lvl; + mdata[1] = bt_off_lvl; + + return mw_send_frame(mwdevice, MW_BATTERY_CONFIG_MSG, 0, mdata, 2); +} + + +/* ---------------------------------------------------------------------- + * Watch responses, events or notifications + * ---------------------------------------------------------------------- */ + +int mw_get_device_type_response(mwdevice_t *mwdevice, unsigned char devtype) +{ +#ifdef DEBUG + fprintf(stderr, "Got device type "); + switch(devtype) { + case 0: + fprintf(stderr, "Reserved\n"); + break; + case 1: + fprintf(stderr, "Ana-Digi\n"); + break; + case 2: + fprintf(stderr, "Digital\n"); + break; + case 3: + fprintf(stderr, "Development Board Digital\n"); + break; + case 4: + fprintf(stderr, "Development Board Ana-Digi\n"); + break; + default: + fprintf(stderr, "unknown %d\n", devtype); + break; + }; +#endif + mwdevice->devtype = devtype; + if (mwdevice->mw_get_device_type_response_cb != NULL) + mwdevice->mw_get_device_type_response_cb(mwdevice, devtype, mwdevice->mw_gdtypersp_data); + return 0; +} + +void mw_set_get_device_type_response_cb(mwdevice_t *mwdevice, void (*mw_get_device_type_response_cb) (mwdevice_t *mwdevice, unsigned char devtype, void *user_data), void *user_data) +{ + if (mw_get_device_type_response_cb != NULL) + mwdevice->mw_get_device_type_response_cb = mw_get_device_type_response_cb; + if (user_data != NULL) + mwdevice->mw_gdtypersp_data = user_data; +} + +int mw_nval_operation_response(mwdevice_t *mwdevice, unsigned char operation, unsigned short identifier, unsigned char size, void *mdata) +{ + return -1; +} + +int mw_get_real_time_clock_response(mwdevice_t *mwdevice, unsigned char *rtcrsp, int len) { struct tm mtm; unsigned short year; - unsigned char clk1224, date_fmt; - if (len != 10) { - fprintf(stderr, "get real time clock response too short %d != 10\n", len); - return; + if (len != 8) { + fprintf(stderr, "get real time clock response length wrong %d != 10\n", len); + return -1; } year = *(unsigned short *)rtcrsp; @@ -132,204 +385,414 @@ void mw_get_real_time_clock_response(int mw_fd, unsigned char *rtcrsp, int len) mtm.tm_hour = rtcrsp[5]; mtm.tm_min = rtcrsp[6]; mtm.tm_sec = rtcrsp[7]; - clk1224 = rtcrsp[8]; - date_fmt = rtcrsp[9]; - fprintf(stderr, "watch RTC is %s, clock format is %s, date format is %s\n", asctime(&mtm), clk1224 ? "24h" : "AM/PM", date_fmt ? "DD/MM" : "MM/DD"); +#ifdef DEBUG + fprintf(stderr, "watch RTC is %s\n", asctime(&mtm)); +#endif + if (mwdevice->mw_get_real_time_clock_response_cb != NULL) + mwdevice->mw_get_real_time_clock_response_cb(mwdevice, &mtm, mwdevice->mw_grtcrsp_data); + + return 0; +} + +void mw_set_get_real_time_clock_response_cb(mwdevice_t *mwdevice, void (*mw_get_real_time_clock_response_cb) (mwdevice_t *mwdevice, struct tm *mw_tm, void *user_data), void *user_data) +{ + if (mw_get_real_time_clock_response_cb != NULL) + mwdevice->mw_get_real_time_clock_response_cb = mw_get_real_time_clock_response_cb; + if (user_data != NULL) + mwdevice->mw_grtcrsp_data = user_data; +} + + +int mw_get_battery_voltage_response(mwdevice_t *mwdevice, unsigned char *batrsp, int len) +{ + unsigned char power_good = batrsp[0]; + unsigned char bat_charging = batrsp[1]; + unsigned short voltage = *(unsigned short *)(batrsp+2); + unsigned short voltage_avg = *(unsigned short *)(batrsp+4); + +#ifdef DEBUG + fprintf(stderr, "battery is at %dmV (average is %dmV), %s and %s\n", voltage, voltage_avg, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging"); +#endif + + if (mwdevice->mw_get_battery_voltage_response_cb != NULL) + mwdevice->mw_get_battery_voltage_response_cb(mwdevice, &voltage, &voltage_avg, &power_good, &bat_charging, mwdevice->mw_gbatvrsp_data); + + return 0; +} + +void mw_set_get_battery_voltage_response_cb(mwdevice_t *mwdevice, void (*mw_get_battery_voltage_response_cb) (mwdevice_t *mwdevice, unsigned short *voltage, unsigned short *voltage_avg, unsigned char *pgood, unsigned char *charging, void *user_data), void *user_data) +{ + if (mw_get_battery_voltage_response_cb != NULL) + mwdevice->mw_get_battery_voltage_response_cb = mw_get_battery_voltage_response_cb; + if (user_data != NULL) + mwdevice->mw_gbatvrsp_data = user_data; +} + +#if 0 +int mw_read_button_config_response(mwdevice_t *mwdevice, unsigned char *btnrsp, int len) +{ +#ifdef DEBUG + fprintf(stderr, "read button config response\n"); + fprintf(stderr, "screen mode : 0x%02x\n", btnrsp[0]); + fprintf(stderr, "button index : 0x%02x\n", btnrsp[1]); + fprintf(stderr, "mask table : 0x%02x (", btnrsp[2]); + fprintf(stderr, "%s ", (btnrsp[2] & 0x01) ? "Absolute, " : ""); + fprintf(stderr, "%s ", (btnrsp[2] & 0x02) ? "Press&Release, " : ""); + fprintf(stderr, "%s ", (btnrsp[2] & 0x04) ? "Press&Hold, " : ""); + fprintf(stderr, "%s ", (btnrsp[2] & 0x08) ? "Press&LongHold, " : ""); + fprintf(stderr, "%s ", (btnrsp[2] & 0x10) ? "Immediate" : ""); + fprintf(stderr, ")\n"); + fprintf(stderr, "callback msg type: 0x%02x\n", btnrsp[3]); + fprintf(stderr, "callback msg opts: 0x%02d\n", btnrsp[4]); +#endif + + return 0; +} +#endif + +void mw_set_read_button_config_response_cb(mwdevice_t *mwdevice, void (*mw_read_button_config_response_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data) +{ + if (mw_read_button_config_response_cb != NULL) + mwdevice->mw_read_button_config_response_cb = mw_read_button_config_response_cb; + if (user_data != NULL) + mwdevice->mw_rbtncnfrsp_data = user_data; } -void mw_get_battery_voltage_response(int mw_fd, unsigned char *batrsp, int len) +int mw_button_event_message(mwdevice_t *mwdevice, unsigned char *btnevt, int len, unsigned char opts) { - unsigned short voltage = *(unsigned short *)batrsp; - unsigned char power_good = batrsp[2]; - unsigned char bat_charging = batrsp[3]; +#ifdef DEBUG + fprintf(stderr, "Button event message, button %d options %d\n", btnevt[0], opts); +#endif - fprintf(stderr, "battery is at %dV, %s and %s\n", voltage, power_good ? "power is good" : "power fault", bat_charging ? "charging" : "not charging"); + if (mwdevice->mw_button_event_message_cb != NULL) + mwdevice->mw_button_event_message_cb(mwdevice, btnevt[0], opts, mwdevice->mw_btnevmsg_data); + + return 0; } +void mw_set_button_event_message_cb(mwdevice_t *mwdevice, void (*mw_button_event_message_cb) (mwdevice_t *mwdevice, unsigned char buttons, unsigned char options, void *user_data), void *user_data) +{ + if (mw_button_event_message_cb != NULL) + mwdevice->mw_button_event_message_cb = mw_button_event_message_cb; + if (user_data != NULL) + mwdevice->mw_btnevmsg_data = user_data; +} + +int mw_read_light_sensor_response(mwdevice_t *mwdevice, unsigned char *lightrsp, int len) +{ + unsigned short light_level = *(unsigned short *)(lightrsp); + unsigned short light_level_avg = *(unsigned short *)(lightrsp+2); + +#ifdef DEBUG + fprintf(stderr, "light sensor is at %d, average at %d\n", light_level, light_level_avg); +#endif + + if (mwdevice->mw_read_light_sensor_response_cb != NULL) + mwdevice->mw_read_light_sensor_response_cb(mwdevice, &light_level, &light_level_avg, mwdevice->mw_rlsrsp_data); + + return 0; +} + +void mw_set_read_light_sensor_response_cb(mwdevice_t *mwdevice, void (*mw_read_light_sensor_response_cb) (mwdevice_t *mwdevice, unsigned short *light_level, unsigned short *light_level_avg, void *user_data), void *user_data) +{ + if (mw_read_light_sensor_response_cb != NULL) + mwdevice->mw_read_light_sensor_response_cb = mw_read_light_sensor_response_cb; + if (user_data != NULL) + mwdevice->mw_rlsrsp_data = user_data; +} + +int mw_status_change_event(mwdevice_t *mwdevice, unsigned char option, unsigned char *statrsp, int len) +{ + unsigned char mode = (option & 0x0f); + unsigned char status = statrsp[0]; +#ifdef DEBUG + fprintf(stderr, "Status change event for mode %s: %s\n", mw_screen_mode_names[mode], mw_status_string[status]); +#endif + + // mwdevice->screen_mode = mode; + if (mwdevice->mw_status_change_event_cb != NULL) + mwdevice->mw_status_change_event_cb(mwdevice, &mode, &status, mwdevice->mw_stchev_data); + + return 0; +} -int decode_frame(int mw_fd, unsigned char *buf, int len) +void mw_set_status_change_event_cb(mwdevice_t *mwdevice, void (*mw_status_change_event_cb) (mwdevice_t *mwdevice, unsigned char *scrmode, unsigned char *status, void *user_data), void *user_data) +{ + if (mw_status_change_event_cb != NULL) + mwdevice->mw_status_change_event_cb = mw_status_change_event_cb; + if (user_data != NULL) + mwdevice->mw_stchev_data = user_data; +} + +int mw_low_battery_warning_message(mwdevice_t *mwdevice) +{ +#ifdef DEBUG + fprintf(stderr, "Watch battery low, please connect charger\n"); +#endif + if (mwdevice->mw_low_battery_warning_message_cb != NULL) + mwdevice->mw_low_battery_warning_message_cb(mwdevice, mwdevice->mw_lbatwarnmsg_data); + + return 0; +} + +void mw_set_low_battery_warning_message_cb(mwdevice_t *mwdevice, void (*mw_low_battery_warning_message_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data) +{ + if (mw_low_battery_warning_message_cb != NULL) + mwdevice->mw_low_battery_warning_message_cb = mw_low_battery_warning_message_cb; + if (user_data != NULL) + mwdevice->mw_lbatwarnmsg_data = user_data; +} + +int mw_low_battery_bt_off_message(mwdevice_t *mwdevice) +{ +#ifdef DEBUG + fprintf(stderr, "Watch battery extremely low - radio will turn off\n"); +#endif + if (mwdevice->mw_low_battery_bt_off_message_cb != NULL) + mwdevice->mw_low_battery_bt_off_message_cb(mwdevice, mwdevice->mw_lbatbtoff_data); + + return 0; +} + +void mw_set_low_battery_bt_off_message_cb(mwdevice_t *mwdevice, void (*mw_low_battery_bt_off_message_cb) (mwdevice_t *mwdevice, void *user_data), void *user_data) +{ + if (mw_low_battery_bt_off_message_cb != NULL) + mwdevice->mw_low_battery_bt_off_message_cb = mw_low_battery_bt_off_message_cb; + if (user_data != NULL) + mwdevice->mw_lbatbtoff_data = user_data; +} + +/* ---------------------------------------------------------------------- + * Protocol handling + * ---------------------------------------------------------------------- */ + +int mw_decode_frame(mwdevice_t *mwdevice, unsigned char *buf, int len) { unsigned short crc; unsigned char msglen; unsigned char msgtype; unsigned char msgopt; unsigned char *msgdata; + unsigned char msgdatalen; /* check frame */ crc = *(unsigned short *)(buf+len-2); if (crc != crc16ccitt(buf, len-2)) { fprintf(stderr, "decode frame CRC error\n"); - return 1; - } else - fprintf(stderr, "decode frame CRC OK\n"); + return -1; + } if (buf[0] != MW_SOF) { fprintf(stderr, "decode frame SOF not found\n"); - return 1; - } else - fprintf(stderr, "decode frame found SOF\n"); + return -1; + } msglen = buf[1]; msgtype = buf[2]; msgopt = buf[3]; msgdata = (buf+4); + msgdatalen = msglen - 4 - 2; switch (msgtype) { - case MW_GET_REAL_TIME_CLOCK_RSP: - mw_get_real_time_clock_response(mw_fd, msgdata, len-4-2); + case MW_GET_DEVICE_TYPE_RSP: + mw_get_device_type_response(mwdevice, msgdata[0]); break; case MW_GET_INFORMATION_STRING_RSP: - msgdata[len-4-2] = 0; - fprintf(stderr, "Got info string '%s'\n", (msgdata+4)); + msgdata[len-2] = 0; + fprintf(stderr, "Got info string '%s'\n", msgdata); + break; + case MW_GET_REAL_TIME_CLOCK_RSP: + mw_get_real_time_clock_response(mwdevice, msgdata, msgdatalen); break; case MW_READ_BATTERY_VOLTAGE_RSP: - mw_get_battery_voltage_response(mw_fd, msgdata, len-4-2); + mw_get_battery_voltage_response(mwdevice, msgdata, msgdatalen); + break; + case MW_READ_LIGHT_SENSOR_RSP: + mw_read_light_sensor_response(mwdevice, msgdata, msgdatalen); + break; +#if 0 + case MW_READ_BUTTON_CONFIG_RSP: + mw_read_button_config_response(mwdevice, msgdata, msgdatalen); + break; +#endif + case MW_BUTTON_EVENT_MESSAGE: + mw_button_event_message(mwdevice, msgdata, msgdatalen, msgopt); break; case MW_LOW_BATTERY_WARNING_MSG: - fprintf(stderr, "Watch battery low, please connect charger\n"); + mw_low_battery_warning_message(mwdevice); break; case MW_LOW_BATTERY_BT_OFF_MSG: - fprintf(stderr, "Watch battery extremely low - radio will turn off\n"); + mw_low_battery_bt_off_message(mwdevice); + break; + case MW_NVAL_OPERATION_RSP: + fprintf(stderr, "NVAL operation response - "); + switch (msgopt) { + case 0x00: + fprintf(stderr, "success\n"); + break; + case 0x01: + fprintf(stderr, "failure!\n"); + break; + case 0x09: + fprintf(stderr, "Item Not initialized (Identifier Not found\n"); + break; + case 0x0a: + fprintf(stderr, "Operation failed\n"); + break; + case 0x0c: + fprintf(stderr, "Bad Item length\n"); + break; + default: + fprintf(stderr, "unknown status 0x%02x\n", msgopt); + break; + }; + break; + case MW_STATUS_CHANGE_EVENT: + mw_status_change_event(mwdevice, msgopt, msgdata, msgdatalen); break; default: +#ifdef DEBUG fprintf(stderr, "Unkown msgtype 0x%02x\n", msgtype); +#endif break; }; - return 0; + return msglen; } -void process_cmd(char *cmdline, int clinep, int mw_fd) +int mw_feed_msg_buffer(mwdevice_t *mwdevice, unsigned char *buf, int len) { - unsigned char mdata[32]; - - fprintf(stderr, "command: '%s'\n", cmdline); - - if (strncmp(cmdline, "quit", 4) == 0) { - close(mw_fd); - exit(0); - } - - if (strncmp(cmdline, "srtc", 4) == 0) { - fprintf(stderr, "Setting RTC from system time..."); - mw_set_rtc(mw_fd, MW_RTC_CLOCK_24HR, MW_RTC_DATE_DDMM); - fprintf(stderr, "OK\n"); - } - if (strncmp(cmdline, "grtc", 4) == 0) { - mw_send_frame(mw_fd, MW_GET_REAL_TIME_CLOCK, 0, NULL, 0); - } - if (strncmp(cmdline, "gistr", 5) == 0) { - mdata[0] = 0; - mw_send_frame(mw_fd, MW_GET_INFORMATION_STRING, 0, mdata, 1); - } - if (strncmp(cmdline, "gdtype", 6) == 0) { - mw_send_frame(mw_fd, MW_GET_DEVICE_TYPE, 0, NULL, 0); - } - if (strncmp(cmdline, "rvbat", 5) == 0) { - mw_send_frame(mw_fd, MW_READ_BATTERY_VOLTAGE_MSG, 0, NULL, 0); - } - if (strncmp(cmdline, "modecfg", 6) == 0) { - mw_configure_watch_mode(mw_fd, MW_SCREEN_MODE_IDLE, 0, 4, 1); - mw_update_display(mw_fd, MW_SCREEN_MODE_IDLE, 0); - } - if (strncmp(cmdline, "rbtcfg", 6) == 0) { - mdata[0] = 0; /* idle screen */ - mdata[1] = 1; /* button index */ - mdata[2] = 2; /* button press type */ - mdata[3] = 3; /* callback message type */ - mdata[4] = 4; /* callback message option */ - mw_send_frame(mw_fd, MW_READ_BUTTON_CONFIG, 0, NULL, 0); - } - if (strncmp(cmdline, "svib", 4) == 0) { - mw_set_vibrate_mode(mw_fd, 1, 300, 300, 5); + char msgbuf[64]; + int tlen; + + if (len <= 0) + return -1; + + memcpy((mwdevice->pbuf+mwdevice->pbuf_len), buf, len); + mwdevice->pbuf_len += len; + + while (mwdevice->pbuf_len > 0) { + /* scan for MW_SOF */ + while (mwdevice->pbuf[0] != MW_SOF) { + memmove(mwdevice->pbuf, (mwdevice->pbuf+1), --mwdevice->pbuf_len); + } + tlen = mwdevice->pbuf[1]; + /* OK, there is an SOF but the message is too short */ + if (tlen > mwdevice->pbuf_len) { + /* we have to wait for more data to come in */ + break; + } + /* here we have a complete msg */ + memcpy(msgbuf, mwdevice->pbuf, tlen); + mw_decode_frame(mwdevice, (unsigned char *)msgbuf, tlen); + memmove(mwdevice->pbuf, (mwdevice->pbuf+tlen), tlen); + mwdevice->pbuf_len -= tlen; } + return 0; } -int menu(int mw_fd) +/* ---------------------------------------------------------------------- + * General code usage + * ---------------------------------------------------------------------- */ + +int mw_init(mwdevice_t *mwdevice, int mw_fd) { - fd_set mfds; - struct termios tconfd; - char cmdline[128]; - unsigned char msg_buf[64]; - unsigned char clinep = 0; - int rcvd; + memset(mwdevice, 0, sizeof(mwdevice_t)); + mwdevice->mw_fd = mw_fd; + memset(mwdevice->pbuf, 0, MW_PBUF_LEN); + mwdevice->pbuf_len = 0; - tcgetattr(0, &tconfd); - cfmakeraw(&tconfd); - tconfd.c_oflag |= ONLCR | OPOST; - tconfd.c_lflag |= ISIG; - tcsetattr(0, TCSANOW, &tconfd); + /* figure out which device we run with */ + mw_send_frame(mwdevice, MW_GET_DEVICE_TYPE, 0, NULL, 0); - FD_ZERO(&mfds); - FD_SET(0, &mfds); - FD_SET(mw_fd, &mfds); + return 0; +} - memset(cmdline, 0, 128); +/* ---------------------------------------------------------------------- + * Convenience functions not strictly part of the protocol + * ---------------------------------------------------------------------- */ - do { - rcvd = 0; - if (select(mw_fd+1, &mfds, NULL, NULL, NULL) > 0) { - if (FD_ISSET(mw_fd, &mfds)) { - rcvd = read(mw_fd, msg_buf, 64); - fprintf(stderr, "read %d bytes:\n", rcvd); - if (rcvd > 0) { - dump_frame(msg_buf, rcvd); - decode_frame(mw_fd, msg_buf, rcvd); - } - }; - if (FD_ISSET(0, &mfds)) { - rcvd = read(0, (cmdline+clinep), 1); - if (rcvd > 0) { - if (cmdline[clinep] == '\r') { - printf("\n"); - cmdline[clinep--] = '\0'; - process_cmd(cmdline, clinep, mw_fd); - clinep = 0; - memset(cmdline, 0, 128); - } else { - clinep++; - if (clinep > 75) - clinep = 75; - printf("\r> %s", cmdline); - fflush(stdout); - } - } - }; - } else +int mw_get_resolution(mwdevice_t *mwdevice, unsigned int *width, unsigned int *height) +{ + if (width == NULL || height == NULL) + return -1; + + switch (mwdevice->devtype) { + case MW_DEVICE_TYPE_RESERVED: + return -1; + case MW_DEVICE_TYPE_ANA_DIGI: + case MW_DEVICE_TYPE_DEVB_ANA_DIGI: + *width = 80; + *height = 16; + break; + case MW_DEVICE_TYPE_DIGITAL: + case MW_DEVICE_TYPE_DEVB_DIGI: + *width = 96; + *height = 96; break; - FD_ZERO(&mfds); - FD_SET(0, &mfds); - FD_SET(mw_fd, &mfds); - } while (rcvd > 0); + default: + break; + }; return 0; } -int main(int argc, char **argv) -{ - int mw_fd; - - if (argc != 2) { - fprintf(stderr, "Usage:\n\t%s \n", argv[0]); - return 1; - }; - - crc16ccitt_init(); - mw_fd = open(argv[1], O_RDWR); - if (mw_fd < 0) { - perror("open"); - return 1; - }; - - menu(mw_fd); +/* if flip=1 bits in each byte are inverted 7->1, 6->2, 5->3,... + if invert=1 each byte is inverted + */ +void bmap_buffer_flipinvert(unsigned char flip, unsigned char invert, unsigned char *buf, int len) +{ + int i; + unsigned char tmp; + + while (len--) { + tmp = 0; + if (flip) { + for (i=0; i<8; i++) + tmp |= ((*buf & (1<> i) << (7-i); + // fprintf(stderr, "0x%02x -> 0x%02x\n", *buf, tmp); + } else + tmp = *buf; + *buf = invert ? ~tmp : tmp; + buf++; + } +} - fsync(mw_fd); - close(mw_fd); +void mw_send_bitmap(mwdevice_t *mwdevice, unsigned char mode, int width, int height, int offset, unsigned char *bmapbuf, int buflen) +{ +#ifdef DEBUG + unsigned int i, x; +#endif + unsigned int y, rowlength; + unsigned char mw_buf[24]; + + rowlength = (((width-1) / 8) + 1); + if ((height + offset) > 96) + height = 96 - offset; + +#ifdef DEBUG + fprintf(stderr, "row length = %d bytes\n", rowlength); + fprintf(stderr, "bitmap resolution is %d x %d\n", width, height); + fprintf(stderr, "\n"); + for (y=0; y 12) ? 12 : rowlength); + memcpy((mw_buf+12), (bmapbuf+((y+1)*rowlength)), (rowlength > 12) ? 12 : rowlength); + mw_write_buffer(mwdevice, mode, 0, offset+y, mw_buf, 24); + } +}