summaryrefslogtreecommitdiffstats
path: root/plugin/most_unicens.c
blob: b182c50ac1ab1cc64b5461b685228e5043cbc7d4 (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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
 * Copyright (C) 2018, "IoT.bzh", Microchip Technology Inc. and its subsidiaries.
 * Author Jonathan Aillet
 * Author Tobias Jahnke
 * Contrib Fulup Ar Foll
 *
 * 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

#include <stdio.h>
#include <string.h>
#include <stdbool.h>

#include <wrap-json.h>

#include <ctl-plugin.h>

#include "wrap_unicens.h"
#include "wrap_volume.h"

CTLP_CAPI_REGISTER("hal-unicens")

#define PCM_MAX_CHANNELS  6

AFB_ApiT unicensHalApiHandle;

static uint8_t initialized = 0;

// Call at initialisation time
CTLP_ONLOAD(plugin, callbacks)
{
	AFB_ApiNotice(plugin->api, "OWN REPO HAL !!! Hal-Unicens Plugin Register: uid='%s' 'info='%s'", plugin->uid, plugin->info);

	unicensHalApiHandle = plugin->api;

	return 0;
}

CTLP_CAPI(MasterVol, source, argsJ, queryJ)
{
	int master_volume;

	json_object *valueJ;
        
        AFB_ApiNotice(source->api, "OWN REPO HAL !!! Hal-Unicens: MasterVolume=%s", json_object_to_json_string(queryJ));

	if(! initialized) {
		AFB_ApiWarning(source->api, "%s: Link to unicens binder is not initialized, can't set master volume, value=%s", __func__, json_object_get_string(queryJ));
		return -1;
	}

	if(! json_object_is_type(queryJ, json_type_array) || json_object_array_length(queryJ) <= 0) {
		AFB_ApiError(source->api, "%s: invalid json (should be a non empty json array) value=%s", __func__, json_object_get_string(queryJ));
		return -2;
	}

	valueJ = json_object_array_get_idx(queryJ, 0);
	if(! json_object_is_type(valueJ, json_type_int)) {
		AFB_ApiError(source->api, "%s: invalid json (should be an array of int) value=%s", __func__, json_object_get_string(queryJ));
		return -3;
	}

	master_volume = json_object_get_int(valueJ);
	wrap_volume_master(source->api, master_volume);

	return 0;
}

CTLP_CAPI(MasterSwitch, source, argsJ, queryJ)
{
	json_bool master_switch;
	json_object *valueJ;
        
        AFB_ApiNotice(source->api, "OWN REPO HAL !!! Hal-Unicens: MasterSwitch=%s", json_object_to_json_string(queryJ));

	if(! initialized) {
		AFB_ApiWarning(source->api, "%s: Link to unicens binder is not initialized, can't set master switch, value=%s", __func__, json_object_get_string(queryJ));
		return -1;
	}

	if(! json_object_is_type(queryJ, json_type_array) || json_object_array_length(queryJ) <= 0) {
		AFB_ApiError(source->api, "%s: invalid json (should be a non empty json array) value=%s", __func__, json_object_get_string(queryJ));
		return -2;
	}

	// In case if alsa doesn't return a proper json boolean
	valueJ = json_object_array_get_idx(queryJ, 0);
	switch(json_object_get_type(valueJ)) {
		case json_type_boolean:
			master_switch = json_object_get_boolean(valueJ);
			break;

		case json_type_int:
			master_switch = json_object_get_int(valueJ);
			break;

		default:
			AFB_ApiError(source->api, "%s: invalid json (should be an array of boolean/int) value=%s", __func__, json_object_get_string(queryJ));
			return -3;
	}

	// TBD: implement pause action for Unicens
	AFB_ApiWarning(source->api, "%s: Try to set master switch to %i, but function is not implemented", __func__, (int) master_switch);

	return 0;
}

CTLP_CAPI(PCMVol, source, argsJ, queryJ)
{
	int idx;
	int pcm_volume[PCM_MAX_CHANNELS];

	json_object *valueJ;
        
        AFB_ApiNotice(source->api, "OWN REPO HAL !!! Hal-Unicens: PCMVolume=%s", json_object_to_json_string(queryJ));

	if(! initialized) {
		AFB_ApiWarning(source->api, "%s: Link to unicens binder is not initialized, can't set PCM volume, value=%s", __func__, json_object_get_string(queryJ));
		return -1;
	}

	if(! json_object_is_type(queryJ, json_type_array) || json_object_array_length(queryJ) <= 0) {
		AFB_ApiError(source->api, "%s: invalid json (should be a non empty json array) value=%s", __func__, json_object_get_string(queryJ));
		return -1;
	}

	for(idx = 0; idx < json_object_array_length(queryJ); idx++) {
		valueJ = json_object_array_get_idx(queryJ, idx);
		if(! json_object_is_type(valueJ, json_type_int)) {
			AFB_ApiError(source->api, "%s: invalid json (should be an array of int) value=%s", __func__, json_object_get_string(queryJ));
			return -2;
		}

		pcm_volume[idx] = json_object_get_int(valueJ);
	}

	// If control only has one value, then replicate the first value
	for(idx = idx; idx < 6; idx ++)
		pcm_volume[idx] = pcm_volume[0];

	wrap_volume_pcm(source->api, pcm_volume, PCM_MAX_CHANNELS);

	return 0;
}

/* initializes ALSA sound card, UNICENS API */
CTLP_CAPI(Init, source, argsJ, queryJ)
{
	int err = 0;
	//int pcm_volume[PCM_MAX_CHANNELS] = { 80, 80, 80, 80, 80, 80 };

	AFB_ApiNotice(source->api, "OWN REPO HAL !!! Initializing HAL-MOST-UNICENS-BINDING");
        
	if((err = wrap_volume_init())) {
		AFB_ApiError(source->api, "Failed to initialize wrapper for volume library");
		return err;
	}
        
	if((err = wrap_ucs_subscribe_sync(source->api))) {
		AFB_ApiError(source->api, "Failed to subscribe to UNICENS binding");
		return err;
	}

	// Set output volume to pre-defined level in order to
	// avoid muted volume to be persistent after boot.
	//wrap_volume_master(source->api, 80);
	//wrap_volume_pcm(source->api, pcm_volume, PCM_MAX_CHANNELS);

	AFB_ApiNotice(source->api, "OWN REPO HAL !!! Initializing HAL-MOST-UNICENS-BINDING done..");

	initialized = 1;

	return 0;
}

// This receive UNICENS events
CTLP_CAPI(Events, source, argsJ, queryJ)
{
	uint16_t node = 0U;
	bool available = false;
        int error = false;
        json_object *j_tmp = NULL;
        
        if (initialized == 0) {
            AFB_ApiError(source->api, "Hal-Unicens: Not initialized while receiving event query=%s", json_object_to_json_string(queryJ));
            return 0;
        }
        
        if (json_object_object_get_ex(queryJ, "node", &j_tmp)) {
            node = (uint16_t)json_object_get_int(j_tmp);
        }
        else {
            error = -1;
        }
        
        if (json_object_object_get_ex(queryJ, "available", &j_tmp)) {
            available = (bool)json_object_get_boolean(j_tmp);
        }
        else {
            error = -2;
        }
        
        if(!error) {
            AFB_ApiNotice(source->api, "OWN REPO HAL !!! Node-Availability: node=0x%03X, available=%d", node, available);
            wrap_volume_node_avail(source->api, node, available);
	}
        else {
            AFB_ApiError(source->api, "Hal-Unicens: Failed to parse events query=%s", json_object_to_json_string(queryJ));
        }

	return error;
}