Bert Vermeulen | ac136b5 | 2014-05-28 15:53:27 +0200 | [diff] [blame] | 1 | /* |
| 2 | * This file is part of the libsigrok project. |
| 3 | * |
| 4 | * Copyright (C) 2014 Bert Vermeulen <bert@biot.com> |
| 5 | * |
| 6 | * This program is free software: you can redistribute it and/or modify |
| 7 | * it under the terms of the GNU General Public License as published by |
| 8 | * the Free Software Foundation, either version 3 of the License, or |
| 9 | * (at your option) any later version. |
| 10 | * |
| 11 | * This program is distributed in the hope that it will be useful, |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 | * GNU General Public License for more details. |
| 15 | * |
| 16 | * You should have received a copy of the GNU General Public License |
| 17 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 | */ |
| 19 | |
| 20 | #include <string.h> |
| 21 | #include "libsigrok.h" |
| 22 | #include "libsigrok-internal.h" |
| 23 | |
| 24 | /* @cond PRIVATE */ |
| 25 | #define LOG_PREFIX "soft-trigger" |
| 26 | /* @endcond */ |
| 27 | |
| 28 | SR_PRIV struct soft_trigger_logic *soft_trigger_logic_new( |
| 29 | const struct sr_dev_inst *sdi, struct sr_trigger *trigger) |
| 30 | { |
| 31 | struct soft_trigger_logic *stl; |
| 32 | |
| 33 | stl = g_malloc0(sizeof(struct soft_trigger_logic)); |
| 34 | stl->sdi = sdi; |
| 35 | stl->trigger = trigger; |
| 36 | stl->unitsize = (g_slist_length(sdi->channels) + 7) / 8; |
| 37 | stl->prev_sample = g_malloc0(stl->unitsize); |
| 38 | |
| 39 | return stl; |
| 40 | } |
| 41 | |
| 42 | SR_PRIV void soft_trigger_logic_free(struct soft_trigger_logic *stl) |
| 43 | { |
| 44 | g_free(stl->prev_sample); |
| 45 | g_free(stl); |
| 46 | } |
| 47 | |
| 48 | static gboolean logic_check_match(struct soft_trigger_logic *stl, |
| 49 | uint8_t *sample, struct sr_trigger_match *match) |
| 50 | { |
| 51 | int bit, prev_bit; |
| 52 | gboolean result; |
| 53 | |
| 54 | stl->count++; |
| 55 | result = FALSE; |
| 56 | bit = *(sample + match->channel->index / 8) |
| 57 | & (1 << (match->channel->index % 8)); |
| 58 | if (match->match == SR_TRIGGER_ZERO) |
| 59 | result = bit == 0; |
| 60 | else if (match->match == SR_TRIGGER_ONE) |
| 61 | result = bit != 0; |
| 62 | else { |
| 63 | /* Edge matches. */ |
| 64 | if (stl->count == 1) |
| 65 | /* First sample, don't have enough for an edge match yet. */ |
| 66 | return FALSE; |
| 67 | prev_bit = *(stl->prev_sample + match->channel->index / 8) |
| 68 | & (1 << (match->channel->index % 8)); |
| 69 | if (match->match == SR_TRIGGER_RISING) |
| 70 | result = prev_bit == 0 && bit != 0; |
| 71 | else if (match->match == SR_TRIGGER_FALLING) |
| 72 | result = prev_bit != 0 && bit == 0; |
| 73 | else if (match->match == SR_TRIGGER_EDGE) |
| 74 | result = prev_bit != bit; |
| 75 | } |
| 76 | |
| 77 | return result; |
| 78 | } |
| 79 | |
| 80 | /* Returns the offset (in samples) within buf of where the trigger |
| 81 | * occurred, or -1 if not triggered. */ |
| 82 | SR_PRIV int soft_trigger_logic_check(struct soft_trigger_logic *stl, |
| 83 | uint8_t *buf, int len) |
| 84 | { |
| 85 | struct sr_datafeed_packet packet; |
| 86 | struct sr_trigger_stage *stage; |
| 87 | struct sr_trigger_match *match; |
| 88 | GSList *l, *l_stage; |
| 89 | int offset; |
| 90 | int i; |
| 91 | gboolean match_found; |
| 92 | |
| 93 | offset = -1; |
| 94 | for (i = 0; i < len; i += stl->unitsize) { |
| 95 | l_stage = g_slist_nth(stl->trigger->stages, stl->cur_stage); |
| 96 | stage = l_stage->data; |
| 97 | if (!stage->matches) |
| 98 | /* No matches supplied, client error. */ |
| 99 | return SR_ERR_ARG; |
| 100 | |
| 101 | match_found = TRUE; |
| 102 | for (l = stage->matches; l; l = l->next) { |
| 103 | match = l->data; |
| 104 | if (!match->channel->enabled) |
| 105 | /* Ignore disabled channels with a trigger. */ |
| 106 | continue; |
| 107 | if (!logic_check_match(stl, buf + i, match)) { |
| 108 | match_found = FALSE; |
| 109 | break; |
| 110 | } |
| 111 | } |
| 112 | memcpy(stl->prev_sample, buf + i, stl->unitsize); |
| 113 | if (match_found) { |
| 114 | /* Matched on the current stage. */ |
| 115 | if (l_stage->next) { |
| 116 | /* Advance to next stage. */ |
| 117 | stl->cur_stage++; |
| 118 | } else { |
| 119 | /* Matched on last stage, fire trigger. */ |
| 120 | offset = i / stl->unitsize; |
| 121 | |
| 122 | packet.type = SR_DF_TRIGGER; |
| 123 | packet.payload = NULL; |
| 124 | sr_session_send(stl->sdi, &packet); |
| 125 | break; |
| 126 | } |
| 127 | } else if (stl->cur_stage > 0) { |
| 128 | /* |
| 129 | * We had a match at an earlier stage, but failed on the |
| 130 | * current stage. However, we may have a match on this |
| 131 | * stage in the next bit -- trigger on 0001 will fail on |
| 132 | * seeing 00001, so we need to go back to stage 0 -- but |
| 133 | * at the next sample from the one that matched originally, |
| 134 | * which the counter increment at the end of the loop |
| 135 | * takes care of. |
| 136 | */ |
| 137 | i -= stl->cur_stage * stl->unitsize; |
| 138 | if (i < -1) |
| 139 | i = -1; /* Oops, went back past this buffer. */ |
| 140 | /* Reset trigger stage. */ |
| 141 | stl->cur_stage = 0; |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | return offset; |
| 146 | } |
| 147 | |