+ return(tracepos + sizeof(uint32_t) + sizeof(uint16_t) + sizeof(uint16_t) >= traceLen);
+}
+
+
+bool next_record_is_response(uint16_t tracepos, uint8_t *trace)
+{
+ uint16_t next_records_datalen = *((uint16_t *)(trace + tracepos + sizeof(uint32_t) + sizeof(uint16_t)));
+
+ return(next_records_datalen & 0x8000);
+}
+
+
+bool merge_topaz_reader_frames(uint32_t timestamp, uint32_t *duration, uint16_t *tracepos, uint16_t traceLen, uint8_t *trace, uint8_t *frame, uint8_t *topaz_reader_command, uint16_t *data_len)
+{
+
+#define MAX_TOPAZ_READER_CMD_LEN 16
+
+ uint32_t last_timestamp = timestamp + *duration;
+
+ if ((*data_len != 1) || (frame[0] == TOPAZ_WUPA) || (frame[0] == TOPAZ_REQA)) return false;
+
+ memcpy(topaz_reader_command, frame, *data_len);
+
+ while (!is_last_record(*tracepos, trace, traceLen) && !next_record_is_response(*tracepos, trace)) {
+ uint32_t next_timestamp = *((uint32_t *)(trace + *tracepos));
+ *tracepos += sizeof(uint32_t);
+ uint16_t next_duration = *((uint16_t *)(trace + *tracepos));
+ *tracepos += sizeof(uint16_t);
+ uint16_t next_data_len = *((uint16_t *)(trace + *tracepos)) & 0x7FFF;
+ *tracepos += sizeof(uint16_t);
+ uint8_t *next_frame = (trace + *tracepos);
+ *tracepos += next_data_len;
+ if ((next_data_len == 1) && (*data_len + next_data_len <= MAX_TOPAZ_READER_CMD_LEN)) {
+ memcpy(topaz_reader_command + *data_len, next_frame, next_data_len);
+ *data_len += next_data_len;
+ last_timestamp = next_timestamp + next_duration;
+ } else {
+ // rewind and exit
+ *tracepos = *tracepos - next_data_len - sizeof(uint16_t) - sizeof(uint16_t) - sizeof(uint32_t);
+ break;
+ }
+ uint16_t next_parity_len = (next_data_len-1)/8 + 1;
+ *tracepos += next_parity_len;
+ }
+
+ *duration = last_timestamp - timestamp;
+
+ return true;
+}
+