summaryrefslogtreecommitdiffstats
path: root/plugin/unicens-input/unicens-md-mic-cb.c
blob: c7bc836e07d73b2176e90876483414f041e79f81 (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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/*
 * Copyright (C) 2018, "IoT.bzh".
 * Author Jonathan Aillet
 *
 * 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.
 *
 */

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

#include <wrap-json.h>

#include <afb/afb-binding.h>

#include "unicens-md-mic.h"
#include "unicens-md-mic-cb.h"

/*******************************************************************************
 *	Unicens multi-directional microphone plugin verbs functions	       *
 ******************************************************************************/

void UnicensMdMicSetColorScheme(afb_req_t request)
{
	int32_t darkRed, darkBlue, darkGreen, lightRed, lightBlue, lightGreen, errorRed, errorBlue, errorGreen;

	uint8_t setColorSchemeData[UNICENS_MD_MIC_SET_COLOR_SCHEME_DATA_LENGTH];

	char *error, *info;

	json_object *receivedRequestJson, *unicensServiceRequestJson, *unicensServiceReturnedJson;

	afb_api_t apiHandle;

	if(! (receivedRequestJson = afb_req_json(request))) {
		afb_req_fail(request, "request_json", "Can't get request json");
		return;
	}

	if(! (apiHandle = (afb_api_t ) afb_req_get_api(request))) {
		afb_req_fail(request, "api_handle", "Can't get current hal controller api handle");
		return;
	}

	if(wrap_json_unpack(receivedRequestJson, "{s:{s:i, s:i, s:i}, s:{s:i, s:i, s:i}, s:{s:i, s:i, s:i}}",
						 "dark",
						 "red", &darkRed,
						 "green", &darkGreen,
						 "blue", &darkBlue,
						 "light",
						 "red", &lightRed,
						 "green", &lightGreen,
						 "blue", &lightBlue,
						 "error",
						 "red", &errorRed,
						 "green", &errorGreen,
						 "blue", &errorBlue)) {
		afb_req_fail_f(request, "requested_colors", "Can't get requested colors, might be a syntax error (json format should be : '%s')", UNICENS_MD_MIC_SET_COLOR_SCHEME_JSON);
		return;
	}

	if(darkRed < 0 || darkRed > 255 || darkGreen < 0 || darkGreen > 255 || darkBlue < 0 || darkBlue > 255 ||
	   lightRed < 0 || lightRed > 255 || lightGreen < 0 || lightGreen > 255 || lightBlue < 0 || lightBlue > 255 ||
	   errorRed < 0 || errorRed > 255 || errorGreen < 0 || errorGreen > 255 || errorBlue < 0 || errorBlue > 255) {
		afb_req_fail(request, "requested_colors", "Invalid requested colors, colors should be between 0 and 255");
		return;
	}

	setColorSchemeData[0] = (uint8_t) UNICENS_MD_MIC_SET_COLOR_SCHEME_CMD;
	setColorSchemeData[1] = (uint8_t) darkRed;
	setColorSchemeData[2] = (uint8_t) darkGreen;
	setColorSchemeData[3] = (uint8_t) darkBlue;
	setColorSchemeData[4] = (uint8_t) lightRed;
	setColorSchemeData[5] = (uint8_t) lightGreen;
	setColorSchemeData[6] = (uint8_t) lightBlue;
	setColorSchemeData[7] = (uint8_t) errorRed;
	setColorSchemeData[8] = (uint8_t) errorGreen;
	setColorSchemeData[9] = (uint8_t) errorBlue;

	wrap_json_pack(&unicensServiceRequestJson, "{s:i, s:i, s:y}",
						   "node", UNICENS_MD_MIC_NODE,
						   "msgid", UNICENS_MD_MIC_SET_COLOR_SCHEME_MSG_ID,
						   "data", setColorSchemeData, (size_t) UNICENS_MD_MIC_SET_COLOR_SCHEME_DATA_LENGTH);

	if(afb_api_call_sync(apiHandle,
			     UNICENS_API,
			     UNICENS_SEND_MSG_VERB,
			     unicensServiceRequestJson,
			     &unicensServiceReturnedJson,
			     &error,
			     &info)) {
		afb_req_fail_f(request,
			       "unicens_service_call",
			       "Error '%s' during call to '%s' verb of '%s' api (info: '%s')",
			       error, UNICENS_SEND_MSG_VERB, UNICENS_API, info);
		return;
	}

	afb_req_success_f(request, NULL, "Set color scheme correctly send on MOST network, multi-directional microphone colors should be set according to your request");
}

void UnicensMdMicSetMode(afb_req_t request)
{
	uint8_t setModeData[UNICENS_MD_MIC_SET_MODE_DATA_LENGTH];

	char *mode, *error, *info;

	json_object *receivedRequestJson, *unicensServiceRequestJson, *unicensServiceReturnedJson;

	afb_api_t apiHandle;

	if(! (receivedRequestJson = afb_req_json(request))) {
		afb_req_fail(request, "request_json", "Can't get request json");
		return;
	}

	if(! (apiHandle = (afb_api_t ) afb_req_get_api(request))) {
		afb_req_fail(request, "api_handle", "Can't get current hal controller api handle");
		return;
	}

	if(wrap_json_unpack(receivedRequestJson, "{s:s}", "mode", &mode)) {
		afb_req_fail(request, "requested_mode", "Can't get requested mode, might be a syntax error (mode should be a string)");
		return;
	}

	setModeData[0] = (uint8_t) UNICENS_MD_MIC_SET_COLOR_SCHEME_CMD;

	if(! strcasecmp(mode, "off")) {
		setModeData[1] = (uint8_t) UNICENS_MD_MIC_SET_MODE_OFF;
	}
	else if(! strcasecmp(mode, "idle")) {
		setModeData[1] = (uint8_t) UNICENS_MD_MIC_SET_MODE_IDLE;
	}
	else if(! strcasecmp(mode, "listening")) {
		setModeData[1] = (uint8_t) UNICENS_MD_MIC_SET_MODE_LISTENING;
	}
	else if(! strcasecmp(mode, "processing")) {
		setModeData[1] = (uint8_t) UNICENS_MD_MIC_SET_MODE_PROCESSING;
	}
	else if(! strcasecmp(mode, "error")) {
		setModeData[1] = (uint8_t) UNICENS_MD_MIC_SET_MODE_ERROR;
	}
	else if(! strcasecmp(mode, "doa")) {
		setModeData[1] = (uint8_t) UNICENS_MD_MIC_SET_MODE_DOA;
	}
	else {
		afb_req_fail(request, "requested_mode", "Invalid requested mode, should be : 'off', 'idle', 'listening', 'processing', 'error', or 'doa'");
		return;
	}

	wrap_json_pack(&unicensServiceRequestJson, "{s:i, s:i, s:y}",
						   "node", UNICENS_MD_MIC_NODE,
						   "msgid", UNICENS_MD_MIC_SET_MODE_MSG_ID,
						   "data", setModeData, (size_t) UNICENS_MD_MIC_SET_MODE_DATA_LENGTH);

	if(afb_api_call_sync(apiHandle,
			     UNICENS_API,
			     UNICENS_SEND_MSG_VERB,
			     unicensServiceRequestJson,
			     &unicensServiceReturnedJson,
			     &error,
			     &info)) {
		afb_req_fail_f(request,
			       "unicens_service_call",
			       "Error '%s' during call to '%s' verb of '%s' api (info: '%s')",
			       error, UNICENS_SEND_MSG_VERB, UNICENS_API, info);
		return;
	}

	afb_req_success_f(request, NULL, "Set mode correctly send on MOST network, multi-directional microphone mode should be set according to your request");
}

void UnicensMdMicSetDoa(afb_req_t request)
{
	uint8_t setDoaData[UNICENS_MD_MIC_SET_DOA_DATA_LENGTH];

	int32_t doa;

	char *error, *info;

	json_object *receivedRequestJson, *unicensServiceRequestJson, *unicensServiceReturnedJson;

	afb_api_t apiHandle;

	if(! (receivedRequestJson = afb_req_json(request))) {
		afb_req_fail(request, "request_json", "Can't get request json");
		return;
	}

	if(! (apiHandle = (afb_api_t ) afb_req_get_api(request))) {
		afb_req_fail(request, "api_handle", "Can't get current hal controller api handle");
		return;
	}

	if(wrap_json_unpack(receivedRequestJson, "{s:i}", "doa", &doa)) {
		afb_req_fail(request, "requested_doa", "Can't get requested doa (doa should be an integer)");
		return;
	}

	if(doa < 0 || doa > 359) {
		afb_req_fail(request, "requested_doa", "Invalid doa, should be expressed in degree between 0 and 359");
		return;
	}

	setDoaData[0] = (uint8_t) UNICENS_MD_MIC_SET_DOA_CMD;
	setDoaData[1] = (uint8_t) ((doa & 0xFF00) >> 8);
	setDoaData[2] = (uint8_t) (doa & 0xFF);

	wrap_json_pack(&unicensServiceRequestJson, "{s:i, s:i, s:y}",
						   "node", UNICENS_MD_MIC_NODE,
						   "msgid", UNICENS_MD_MIC_SET_DOA_MSG_ID,
						   "data", setDoaData, (size_t) UNICENS_MD_MIC_SET_DOA_DATA_LENGTH);

	if(afb_api_call_sync(apiHandle,
			     UNICENS_API,
			     UNICENS_SEND_MSG_VERB,
			     unicensServiceRequestJson,
			     &unicensServiceReturnedJson,
			     &error,
			     &info)) {
		afb_req_fail_f(request,
			       "unicens_service_call",
			       "Error '%s' during call to '%s' verb of '%s' api (info: '%s')",
			       error, UNICENS_SEND_MSG_VERB, UNICENS_API, info);
		return;
	}

	afb_req_success_f(request, NULL, "Set doa correctly send on MOST network, multi-directional microphone doa should be set according to your request");
}

void UnicensMdMicGetDoa(afb_req_t request)
{
	AFB_REQ_WARNING(request, "Not implemented because %s api does not include the doa with its response", UNICENS_API);

	afb_req_success_f(request, NULL, "Not implemented because %s api does not include the doa with its response", UNICENS_API);
}