+static int parse_hmuartlgw(enum hmuartlgw_dst dst, uint8_t *buf, int buf_len, void *data)
+{
+ struct recv_data *rdata = data;
+
+ if (dst == HMUARTLGW_OS) {
+ switch (rdata->uartlgw_state) {
+ case HMUARTLGW_STATE_GET_FIRMWARE:
+ if (buf[0] == HMUARTLGW_OS_ACK) {
+ rdata->uartlgw_version[0] = buf[5];
+ rdata->uartlgw_version[1] = buf[6];
+ rdata->uartlgw_version[2] = buf[7];
+ rdata->uartlgw_state = HMUARTLGW_STATE_DONE;
+ }
+ break;
+ case HMUARTLGW_STATE_GET_CREDITS:
+ if (buf[0] == HMUARTLGW_OS_ACK) {
+ rdata->credits = buf[2] / 2;
+ rdata->uartlgw_state = HMUARTLGW_STATE_DONE;
+ }
+ break;
+ default:
+ break;
+ }
+ return 0;
+ }
+
+ switch(buf[0]) {
+ case HMUARTLGW_APP_ACK:
+ if (rdata->uartlgw_state == HMUARTLGW_STATE_GET_HMID) {
+ my_hmid = (buf[4] << 16) | (buf[5] << 8) | buf[6];
+ }
+
+ rdata->status = buf[1];
+ rdata->message_type = MESSAGE_TYPE_R;
+ rdata->uartlgw_state = HMUARTLGW_STATE_ACK_APP;
+#if 0
+ hexdump(buf, buf_len, "ACK Status: ");
+#endif
+
+ break;
+ case HMUARTLGW_APP_RECV:
+ if ((!hmid) ||
+ ((buf[7] == ((hmid >> 16) & 0xff)) &&
+ (buf[8] == ((hmid >> 8) & 0xff)) &&
+ (buf[9] == (hmid & 0xff)))) {
+ memset(rdata->message, 0, sizeof(rdata->message));
+ memcpy(rdata->message + 1, buf + 4, buf_len - 4);
+ rdata->message[LEN] = buf_len - 4;
+ rdata->message_type = MESSAGE_TYPE_E;
+ }
+ break;
+ default:
+ break;
+ }
+
+ return 1;
+}
+
+int send_wait_hmuartlgw(struct hm_dev *dev, struct recv_data *rdata, uint8_t *data, int data_len,
+ enum hmuartlgw_dst dst, enum hmuartlgw_state srcstate,
+ enum hmuartlgw_state dststate)
+{
+ int cnt = 5;
+
+ do {
+ rdata->uartlgw_state = srcstate;
+ hmuartlgw_send(dev->hmuartlgw, data, data_len, dst);
+ do { hmuartlgw_poll(dev->hmuartlgw, 500); } while (rdata->uartlgw_state != dststate);
+ if (rdata->status != HMUARTLGW_ACK_EINPROGRESS)
+ break;
+ usleep(200*1000);
+ } while (cnt--);
+ if (rdata->status == HMUARTLGW_ACK_EINPROGRESS) {
+ fprintf(stderr, "IO thinks it is busy, you might have to reset it!\n");
+ return 0;
+ }
+
+ return 1;
+}
+
+int send_hm_message(struct hm_dev *dev, struct recv_data *rdata, uint8_t *msg)