+SR_PRIV int dlm_channel_state_set(const struct sr_dev_inst *sdi,
+ const int ch_index, gboolean ch_state)
+{
+ GSList *l;
+ struct sr_channel *ch;
+ struct dev_context *devc = NULL;
+ struct scope_state *state;
+ const struct scope_config *model = NULL;
+ struct sr_scpi_dev_inst *scpi;
+ gboolean chan_found;
+ gboolean *pod_enabled;
+ int i, result;
+
+ result = SR_OK;
+
+ scpi = sdi->conn;
+ devc = sdi->priv;
+ state = devc->model_state;
+ model = devc->model_config;
+ chan_found = FALSE;
+
+ pod_enabled = g_malloc0(sizeof(gboolean) * model->pods);
+
+ for (l = sdi->channels; l; l = l->next) {
+ ch = l->data;
+
+ switch (ch->type) {
+ case SR_CHANNEL_ANALOG:
+ if (ch->index == ch_index) {
+ if (dlm_analog_chan_state_set(scpi, ch->index + 1, ch_state) != SR_OK) {
+ result = SR_ERR;
+ break;
+ }
+
+ ch->enabled = ch_state;
+ state->analog_states[ch->index].state = ch_state;
+ chan_found = TRUE;
+ break;
+ }
+ break;
+ case SR_CHANNEL_LOGIC:
+ i = ch->index - DLM_DIG_CHAN_INDEX_OFFS;
+
+ if (ch->index == ch_index) {
+ if (dlm_digital_chan_state_set(scpi, i + 1, ch_state) != SR_OK) {
+ result = SR_ERR;
+ break;
+ }
+
+ ch->enabled = ch_state;
+ state->digital_states[i] = ch_state;
+ chan_found = TRUE;
+
+ /* The corresponding pod has to be enabled also. */
+ pod_enabled[i / 8] |= ch->enabled;
+ } else {
+ /* Also check all other channels. Maybe we can disable a pod. */
+ pod_enabled[i / 8] |= ch->enabled;
+ }
+ break;
+ default:
+ result = SR_ERR_NA;
+ }
+ }
+
+ for (i = 0; i < model->pods; i++) {
+ if (state->pod_states[i] == pod_enabled[i])
+ continue;
+
+ if (dlm_digital_pod_state_set(scpi, i + 1, pod_enabled[i]) != SR_OK) {
+ result = SR_ERR;
+ break;
+ }
+
+ state->pod_states[i] = pod_enabled[i];
+ }
+
+ g_free(pod_enabled);
+
+ if ((result == SR_OK) && !chan_found)
+ result = SR_ERR_BUG;
+
+ return result;
+}
+