-
Notifications
You must be signed in to change notification settings - Fork 0
/
cmd_handling.h
195 lines (166 loc) · 4.82 KB
/
cmd_handling.h
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
/*
* Copyright 2022 Barabas Raffai
*
* This file is part of ShrapnelDSP.
*
* ShrapnelDSP is free software: you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option)
* any later version.
*
* ShrapnelDSP is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License along with
* ShrapnelDSP. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "queue.h"
#include "audio_param.h"
#include "etl/list.h"
#include "event_send.h"
#include "cJSON.h"
#include "esp_log.h"
#include <climits>
#include "esp_err.h"
#include <memory>
#include <string.h>
#include "task.h"
#include "queue.h"
#include <iterator>
#define TAG "cmd_handling"
namespace shrapnel {
template<typename AudioParametersT>
class CommandHandling final
{
public:
struct Message
{
char json[128];
int fd;
};
/** \brief
*
* \param[in] queue Used to receive JSON messages containing parameter updates.
* \param[in] param Data received through \ref queue are
* translated to binary and sent to this object.
*/
CommandHandling(
QueueBase<Message> *queue,
AudioParametersT *param,
EventSendBase &event) :
queue(queue),
param(param),
event(event),
json(nullptr),
message({}) {}
void work(void)
{
/* TODO should not leave these uninitialised */
char *message_type;
cJSON *type;
int ret = queue->receive(&message, portMAX_DELAY);
if(ret != pdTRUE)
{
ESP_LOGE(TAG, "Queue failed to receive");
return;
}
#if !defined(TESTING)
ESP_LOGI(TAG, "%s stack %d", __FUNCTION__, uxTaskGetStackHighWaterMark(NULL));
#endif
size_t message_size = sizeof(message.json);
assert(message_size <= INT_MAX);
(void)message_size;
ESP_LOGI(TAG, "received websocket message: %.*s",
static_cast<int>(sizeof(message.json)),
message.json);
json = cJSON_ParseWithLength(message.json, sizeof(message.json));
if(json == NULL)
{
ESP_LOGE(TAG, "json parsing failed");
goto done;
}
type = cJSON_GetObjectItemCaseSensitive(json, "messageType");
if(cJSON_IsString(type) && (type->valuestring != NULL))
{
message_type = type->valuestring;
}
else
{
ESP_LOGE(TAG, "error parsing messageType from json");
goto done;
}
if(0 == strcmp(message_type, "initialiseParameters"))
{
initialise_parameters();
}
else if(0 == strcmp(message_type, "parameterUpdate"))
{
parameter_update();
}
else
{
ESP_LOGE(TAG, "unknown message type (%s)", message_type);
}
done:
cJSON_Delete(json);
}
private:
void parameter_update(void)
{
assert(json);
char *parsed_id;
cJSON *id = cJSON_GetObjectItemCaseSensitive(json, "id");
if(cJSON_IsString(id) && (id->valuestring != NULL))
{
parsed_id = id->valuestring;
}
else
{
ESP_LOGE(TAG, "error parsing id from json");
return;
}
float parsed_value;
cJSON *value = cJSON_GetObjectItemCaseSensitive(json, "value");
if(cJSON_IsNumber(value))
{
parsed_value = value->valuedouble;
}
else
{
ESP_LOGE(TAG, "error parsing value from json");
return;
}
int rc = param->update(parsed_id, parsed_value);
if(rc != 0)
{
ESP_LOGE(TAG, "Failed to update parameter (%s) with value %f", parsed_id, parsed_value);
}
event.send(message.json, message.fd);
}
void initialise_parameters(void)
{
assert(json);
Message output;
for(const auto& [key, value] : *param)
{
float tmp_f = value->get();
snprintf(output.json, sizeof(output.json),
"{\"id\":\"%s\",\"value\":%g}",
key.c_str(),
tmp_f);
event.send(output.json, -1);
}
}
QueueBase<Message> *queue;
AudioParametersT *param;
EventSendBase &event;
cJSON *json;
Message message;
};
// TODO is there a cleaner solution for this? TAG should be private, but since
// this is a class template, the implementation must be in here
#undef TAG
}