+ } else if (uart) {
+ uint32_t new_hmid = my_hmid;
+
+ hmuartlgw_set_debug(debug);
+
+ dev.hmuartlgw = hmuart_init(uart, parse_hmuartlgw, &rdata, 1);
+ if (!dev.hmuartlgw) {
+ fprintf(stderr, "Can't initialize HM-MOD-UART\n");
+ exit(EXIT_FAILURE);
+ }
+ dev.type = DEVICE_TYPE_HMUARTLGW;
+
+ out[0] = HMUARTLGW_APP_GET_HMID;
+ send_wait_hmuartlgw(&dev, &rdata, out, 1, HMUARTLGW_APP, HMUARTLGW_STATE_GET_HMID, HMUARTLGW_STATE_ACK_APP);
+
+ out[0] = HMUARTLGW_OS_GET_FIRMWARE;
+ send_wait_hmuartlgw(&dev, &rdata, out, 1, HMUARTLGW_OS, HMUARTLGW_STATE_GET_FIRMWARE, HMUARTLGW_STATE_DONE);
+
+ out[0] = HMUARTLGW_OS_GET_CREDITS;
+ send_wait_hmuartlgw(&dev, &rdata, out, 1, HMUARTLGW_OS, HMUARTLGW_STATE_GET_CREDITS, HMUARTLGW_STATE_DONE);
+
+ printf("HM-MOD-UART firmware version: %u.%u.%u, used credits: %u%%\n",
+ rdata.uartlgw_version[0],
+ rdata.uartlgw_version[1],
+ rdata.uartlgw_version[2],
+ rdata.credits);
+
+ if (rdata.credits >= 40) {
+ printf("\nRebooting HM-MOD-UART to avoid running out of credits\n");
+
+ hmuartlgw_enter_bootloader(dev.hmuartlgw);
+ hmuartlgw_enter_app(dev.hmuartlgw);
+ }
+
+ printf("\nHM-MOD-UART opened\n\n");
+
+ if (new_hmid && (my_hmid != new_hmid)) {
+ printf("Changing hmid from %06x to %06x\n", my_hmid, new_hmid);
+
+ out[0] = HMUARTLGW_APP_SET_HMID;
+ out[1] = (new_hmid >> 16) & 0xff;
+ out[2] = (new_hmid >> 8) & 0xff;
+ out[3] = new_hmid & 0xff;
+ send_wait_hmuartlgw(&dev, &rdata, out, 4, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP);
+
+ my_hmid = new_hmid;
+ }
+
+ if (kNo > 0) {
+ printf("Setting AES-key\n");
+
+ memset(out, 0, sizeof(out));
+ out[0] = HMUARTLGW_APP_SET_CURRENT_KEY;
+ memcpy(&(out[1]), key, 16);
+ out[17] = kNo;
+ send_wait_hmuartlgw(&dev, &rdata, out, 18, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP);
+
+ memset(out, 0, sizeof(out));
+ out[0] = HMUARTLGW_APP_SET_OLD_KEY;
+ memcpy(&(out[1]), key, 16);
+ out[17] = kNo;
+ send_wait_hmuartlgw(&dev, &rdata, out, 18, HMUARTLGW_APP, HMUARTLGW_STATE_WAIT_APP, HMUARTLGW_STATE_ACK_APP);
+ }