aboutsummaryrefslogtreecommitdiffstats
path: root/plugins/alsa/alsa-api-ramp.c
blob: 53c3fe345146e3584a4c437b49215e410a3fa128 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/*
 * Copyright (C) 2018 "IoT.bzh"
 * Author Fulup Ar Foll <fulup@iot.bzh>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

#define _GNU_SOURCE  // needed for vasprintf

#include "alsa-softmixer.h"
#include <string.h>

// Set stream volume control in %
#define VOL_CONTROL_MAX  100
#define VOL_CONTROL_MIN  0
#define VOL_CONTROL_STEP 1

PUBLIC AlsaVolRampT *ApiRampGetByUid(SoftMixerT *mixer, const char *uid) {
    AlsaVolRampT *ramp = NULL;
	cds_list_for_each_entry(ramp, &mixer->ramps.list, list) {
		if (!strcasecmp(ramp->uid, uid))
			return ramp;
    }
    return NULL;
}

STATIC AlsaVolRampT *AttachOneRamp(SoftMixerT *mixer, const char *uid, json_object *rampJ) {
	const char * rampUid;
    AlsaVolRampT *ramp = calloc(1, sizeof (AlsaVolRampT));
	if (ramp == NULL) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail;
	}

	CDS_INIT_LIST_HEAD(&ramp->list);

    int error = wrap_json_unpack(rampJ, "{ss,si,si,si !}"
            , "uid", &rampUid
            , "delay", &ramp->delay
            , "up", &ramp->stepUp
            , "down", &ramp->stepDown
            );
    if (error) {
		AFB_API_ERROR(mixer->api,
				"%s mixer=%s hal=%s error=%s json=%s",
				__func__, mixer->uid, uid, wrap_json_get_error_string(error), json_object_get_string(rampJ));
		goto fail_ramp;
    }

    ramp->delay = ramp->delay * 1000; // move from ms to us
    ramp->uid = strdup(rampUid);
	if (ramp->uid == NULL) {
		SOFTMIXER_NOMEM(mixer->api);
		goto fail_ramp;
	}

    return ramp;

fail_ramp:
    free(ramp);
fail:
    return NULL;
}


static void rampDestroy(SoftMixerT * mixer, void * arg) {
	AlsaVolRampT * ramp = (AlsaVolRampT*) arg;
	AFB_API_DEBUG(mixer->api, "%s... %s not implemented", __func__, ramp->uid);
}

static AlsaVolRampT * rampCreate(SoftMixerT * mixer, const char * uid, json_object *argsJ) {
	AlsaVolRampT * newRamp = AttachOneRamp(mixer, uid, argsJ);
	if (!newRamp) {
		goto fail;
	}
	mixer->nbRamps++;
	cds_list_add_tail(&newRamp->list, &mixer->ramps.list);
	AlsaMixerTransactionObjectAdd(mixer->transaction, newRamp, rampDestroy);
fail:
	return newRamp;
}

PUBLIC int ApiRampAttach(SoftMixerT *mixer, afb_req_t request, const char * uid, json_object *argsJ) {

	if (mixer->nbRamps >= mixer->max.ramps) {
		afb_req_fail_f(request, "too-small", "mixer=%s hal=%s max ramp=%d", mixer->uid, uid, mixer->max.ramps);
		goto fail;
    }

	AlsaVolRampT * newRamp = NULL;

    switch (json_object_get_type(argsJ)) {
        long count;
        
        case json_type_object:
			newRamp = rampCreate(mixer, uid, argsJ);
			if (!newRamp) {
                afb_req_fail_f(request, "bad-ramp", "mixer=%s hal=%s invalid ramp= %s", mixer->uid, uid, json_object_get_string(argsJ));
				goto fail;
            }
            break;

        case json_type_array:
            count = json_object_array_length(argsJ);
			if (count > (mixer->max.ramps - mixer->nbRamps)) {
                afb_req_fail_f(request, "too-small", "mixer=%s hal=%s max ramp=%d", mixer->uid, uid, mixer->max.ramps);
				goto fail;
            }

            for (int idx = 0; idx < count; idx++) {
                json_object *streamAudioJ = json_object_array_get_idx(argsJ, idx);
				newRamp = rampCreate(mixer, uid, streamAudioJ);
				if (!newRamp) {
					afb_req_fail_f(request, "bad-ramp", "mixer=%s hal=%s invalid ramp= %s", mixer->uid, uid, json_object_get_string(argsJ));
					goto fail;
                }
            }
            break;
        default:
            afb_req_fail_f(request, "invalid-syntax", "mixer=%s hal=%s ramps invalid argsJ= %s", mixer->uid, uid, json_object_get_string(argsJ));
			goto fail;
    }
    return 0;

fail:
    return -1;
}