-
Notifications
You must be signed in to change notification settings - Fork 81
/
Copy pathcontrol_protocol_methods.cpp
629 lines (505 loc) · 37.3 KB
/
control_protocol_methods.cpp
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
#include "nmos/control_protocol_methods.h"
#include "cpprest/json_utils.h"
#include "nmos/control_protocol_resource.h"
#include "nmos/control_protocol_resources.h"
#include "nmos/control_protocol_state.h"
#include "nmos/control_protocol_utils.h"
#include "nmos/json_fields.h"
#include "nmos/slog.h"
namespace nmos
{
namespace details
{
// NcObject methods implementation
// Get property value
web::json::value get(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
const auto& property_id = nmos::fields::nc::id(arguments);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Get property: " << property_id.serialize();
// find the relevant nc_property_descriptor
const auto& property = find_property(parse_nc_property_id(property_id), parse_nc_class_id(nmos::fields::nc::class_id(resource->data)), get_control_protocol_class);
if (!property.is_null())
{
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok }, resource->data.at(nmos::fields::nc::name(property)));
}
// unknown property
utility::stringstream_t ss;
ss << U("unknown property: ") << property_id.serialize() << U(" to do Get");
return make_control_protocol_error_response(handle, { nc_method_status::property_not_implemented }, ss.str());
}
// Set property value
web::json::value set(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler get_control_protocol_datatype, control_protocol_property_changed_handler property_changed, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
const auto& property_id = nmos::fields::nc::id(arguments);
const auto& val = nmos::fields::nc::value(arguments);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Set property: " << property_id.serialize() << " value: " << val.serialize();
// find the relevant nc_property_descriptor
const auto property_id_ = parse_nc_property_id(property_id);
const auto& property = find_property(property_id_, parse_nc_class_id(nmos::fields::nc::class_id(resource->data)), get_control_protocol_class);
if (!property.is_null())
{
if (nmos::fields::nc::is_read_only(property))
{
return make_control_protocol_message_response(handle, { nc_method_status::read_only });
}
if ((val.is_null() && !nmos::fields::nc::is_nullable(property))
|| (!val.is_array() && nmos::fields::nc::is_sequence(property))
|| (val.is_array() && !nmos::fields::nc::is_sequence(property)))
{
return make_control_protocol_message_response(handle, { nc_method_status::parameter_error });
}
try
{
// do property constraints validation
nmos::details::constraints_validation(val, get_runtime_property_constraints(property_id_, resource->data.at(nmos::fields::nc::runtime_property_constraints)), nmos::fields::nc::constraints(property), { get_datatype_descriptor(property.at(nmos::fields::nc::type_name), get_control_protocol_datatype), get_control_protocol_datatype });
// update property
modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
resource.data[nmos::fields::nc::name(property)] = val;
// do notification that the specified property has changed
if (property_changed)
{
property_changed(resource, nmos::fields::nc::name(property), -1);
}
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { property_id_, nc_property_change_type::type::value_changed, val } }));
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok });
}
catch (const nmos::control_protocol_exception& e)
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "Set property: " << property_id.serialize() << " value: " << val.serialize() << " error: " << e.what();
return make_control_protocol_message_response(handle, { nc_method_status::parameter_error });
}
}
// unknown property
utility::stringstream_t ss;
ss << U("unknown property: ") << property_id.serialize() << " to do Set";
return make_control_protocol_error_response(handle, { nc_method_status::property_not_implemented }, ss.str());
}
// Get sequence item
web::json::value get_sequence_item(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
const auto& property_id = nmos::fields::nc::id(arguments);
const auto& index = nmos::fields::nc::index(arguments);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Get sequence item: " << property_id.serialize() << " index: " << index;
// find the relevant nc_property_descriptor
const auto& property = find_property(parse_nc_property_id(property_id), parse_nc_class_id(nmos::fields::nc::class_id(resource->data)), get_control_protocol_class);
if (!property.is_null())
{
const auto& data = resource->data.at(nmos::fields::nc::name(property));
if (!nmos::fields::nc::is_sequence(property) || data.is_null() || !data.is_array())
{
// property is not a sequence
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is not a sequence to do GetSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::invalid_request }, ss.str());
}
if (data.as_array().size() > (size_t)index)
{
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok }, data.at(index));
}
// out of bound
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is outside the available range to do GetSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::index_out_of_bounds }, ss.str());
}
// unknown property
utility::stringstream_t ss;
ss << U("unknown property: ") << property_id.serialize() << U(" to do GetSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::property_not_implemented }, ss.str());
}
// Set sequence item
web::json::value set_sequence_item(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler get_control_protocol_datatype, control_protocol_property_changed_handler property_changed, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
const auto& property_id = nmos::fields::nc::id(arguments);
const auto& index = nmos::fields::nc::index(arguments);
const auto& val = nmos::fields::nc::value(arguments);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Set sequence item: " << property_id.serialize() << " index: " << index << " value: " << val.serialize();
// find the relevant nc_property_descriptor
const auto property_id_ = parse_nc_property_id(property_id);
const auto& property = find_property(property_id_, parse_nc_class_id(nmos::fields::nc::class_id(resource->data)), get_control_protocol_class);
if (!property.is_null())
{
if (nmos::fields::nc::is_read_only(property))
{
return make_control_protocol_message_response(handle, { nc_method_status::read_only });
}
auto& data = resource->data.at(nmos::fields::nc::name(property));
if (!nmos::fields::nc::is_sequence(property) || data.is_null() || !data.is_array())
{
// property is not a sequence
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is not a sequence to do SetSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::invalid_request }, ss.str());
}
if (data.as_array().size() > (size_t)index)
{
try
{
// do property constraints validation
nmos::details::constraints_validation(val, get_runtime_property_constraints(property_id_, resource->data.at(nmos::fields::nc::runtime_property_constraints)), nmos::fields::nc::constraints(property), { get_datatype_descriptor(property.at(nmos::fields::nc::type_name), get_control_protocol_datatype), get_control_protocol_datatype });
// update property
modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
resource.data[nmos::fields::nc::name(property)][index] = val;
// do notification that the specified property has changed
if (property_changed)
{
property_changed(resource, nmos::fields::nc::name(property), index);
}
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { property_id_, nc_property_change_type::type::sequence_item_changed, val, nc_id(index) } }));
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok });
}
catch (const nmos::control_protocol_exception& e)
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "Set sequence item: " << property_id.serialize() << " index: " << index << " value: " << val.serialize() << " error: " << e.what();
return make_control_protocol_message_response(handle, { nc_method_status::parameter_error });
}
}
// out of bound
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is outside the available range to do SetSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::index_out_of_bounds }, ss.str());
}
// unknown property
utility::stringstream_t ss;
ss << U("unknown property: ") << property_id.serialize() << U(" to do SetSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::property_not_implemented }, ss.str());
}
// Add item to sequence
web::json::value add_sequence_item(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler get_control_protocol_datatype, control_protocol_property_changed_handler property_changed, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
using web::json::value;
const auto& property_id = nmos::fields::nc::id(arguments);
const auto& val = nmos::fields::nc::value(arguments);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Add sequence item: " << property_id.serialize() << " value: " << val.serialize();
// find the relevant nc_property_descriptor
const auto property_id_ = parse_nc_property_id(property_id);
const auto& property = find_property(property_id_, parse_nc_class_id(nmos::fields::nc::class_id(resource->data)), get_control_protocol_class);
if (!property.is_null())
{
if (nmos::fields::nc::is_read_only(property))
{
return make_control_protocol_message_response(handle, { nc_method_status::read_only });
}
if (!nmos::fields::nc::is_sequence(property))
{
// property is not a sequence
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is not a sequence to do AddSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::invalid_request }, ss.str());
}
auto& data = resource->data.at(nmos::fields::nc::name(property));
const nc_id sequence_item_index = data.is_null() ? 0 : nc_id(data.as_array().size());
try
{
// do property constraints validation
nmos::details::constraints_validation(val, get_runtime_property_constraints(property_id_, resource->data.at(nmos::fields::nc::runtime_property_constraints)), nmos::fields::nc::constraints(property), { get_datatype_descriptor(property.at(nmos::fields::nc::type_name), get_control_protocol_datatype), get_control_protocol_datatype });
// update property
modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
auto& sequence = resource.data[nmos::fields::nc::name(property)];
if (data.is_null()) { sequence = value::array(); }
web::json::push_back(sequence, val);
// do notification that the specified property has changed
if (property_changed)
{
property_changed(resource, nmos::fields::nc::name(property), sequence.as_array().size()-1);
}
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { property_id_, nc_property_change_type::type::sequence_item_added, val, sequence_item_index } }));
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok }, sequence_item_index);
}
catch (const nmos::control_protocol_exception& e)
{
slog::log<slog::severities::error>(gate, SLOG_FLF) << "Add sequence item: " << property_id.serialize() << " value: " << val.serialize() << " error: " << e.what();
return make_control_protocol_message_response(handle, { nc_method_status::parameter_error });
}
}
// unknown property
utility::stringstream_t ss;
ss << U("unknown property: ") << property_id.serialize() << U(" to do AddSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::property_not_implemented }, ss.str());
}
// Delete sequence item
web::json::value remove_sequence_item(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
const auto& property_id = nmos::fields::nc::id(arguments);
const auto& index = nmos::fields::nc::index(arguments);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Remove sequence item: " << property_id.serialize() << " index: " << index;
// find the relevant nc_property_descriptor
const auto& property = find_property(parse_nc_property_id(property_id), parse_nc_class_id(nmos::fields::nc::class_id(resource->data)), get_control_protocol_class);
if (!property.is_null())
{
const auto& data = resource->data.at(nmos::fields::nc::name(property));
if (!nmos::fields::nc::is_sequence(property) || data.is_null() || !data.is_array())
{
// property is not a sequence
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is not a sequence to do RemoveSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::invalid_request }, ss.str());
}
if (data.as_array().size() > (size_t)index)
{
modify_control_protocol_resource(resources, resource->id, [&](nmos::resource& resource)
{
auto& sequence = resource.data[nmos::fields::nc::name(property)].as_array();
sequence.erase(index);
}, make_propertry_changed_event(nmos::fields::nc::oid(resource->data), { { parse_nc_property_id(property_id), nc_property_change_type::type::sequence_item_removed, nc_id(index) } }));
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok });
}
// out of bound
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is outside the available range to do RemoveSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::index_out_of_bounds }, ss.str());
}
// unknown property
utility::stringstream_t ss;
ss << U("unknown property: ") << property_id.serialize() << U(" to do RemoveSequenceItem");
return make_control_protocol_error_response(handle, { nc_method_status::property_not_implemented }, ss.str());
}
// Get sequence length
web::json::value get_sequence_length(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
using web::json::value;
const auto& property_id = nmos::fields::nc::id(arguments);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Get sequence length: " << property_id.serialize();
// find the relevant nc_property_descriptor
const auto& property = find_property(parse_nc_property_id(property_id), parse_nc_class_id(nmos::fields::nc::class_id(resource->data)), get_control_protocol_class);
if (!property.is_null())
{
if (!nmos::fields::nc::is_sequence(property))
{
// property is not a sequence
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << U(" is not a sequence to do GetSequenceLength");
return make_control_protocol_error_response(handle, { nc_method_status::invalid_request }, ss.str());
}
const auto& data = resource->data.at(nmos::fields::nc::name(property));
if (nmos::fields::nc::is_nullable(property))
{
// can be null
if (data.is_null())
{
// null
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok }, value::null());
}
}
else
{
// cannot be null
if (data.is_null())
{
// null
utility::stringstream_t ss;
ss << U("property: ") << property_id.serialize() << " is a null sequence to do GetSequenceLength";
return make_control_protocol_error_response(handle, { nc_method_status::invalid_request }, ss.str());
}
}
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nmos::fields::nc::is_deprecated(property) ? nc_method_status::property_deprecated : nc_method_status::ok }, uint32_t(data.as_array().size()));
}
// unknown property
utility::stringstream_t ss;
ss << U("unknown property: ") << property_id.serialize() << " to do GetSequenceLength";
return make_control_protocol_error_response(handle, { nc_method_status::property_not_implemented }, ss.str());
}
// NcBlock methods implementation
// Gets descriptors of members of the block
web::json::value get_member_descriptors(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
using web::json::value;
const auto& recurse = nmos::fields::nc::recurse(arguments); // If recurse is set to true, nested members is to be retrieved
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Get descriptors of members of the block: " << "recurse: " << recurse;
auto descriptors = value::array();
nmos::get_member_descriptors(resources, resource, recurse, descriptors.as_array());
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nc_method_status::ok }, descriptors);
}
// Finds member(s) by path
web::json::value find_members_by_path(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
using web::json::value;
// Relative path to search for (MUST not include the role of the block targeted by oid)
const auto& path = arguments.at(nmos::fields::nc::path);
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Find member(s) by path: " << "path: " << path.serialize();
if (0 == path.size())
{
// empty path
return make_control_protocol_error_response(handle, { nc_method_status::parameter_error }, U("empty path to do FindMembersByPath"));
}
auto descriptors = value::array();
value descriptor;
for (const auto& role : path.as_array())
{
// look for the role in members
if (resource->data.has_field(nmos::fields::nc::members))
{
auto& members = nmos::fields::nc::members(resource->data);
auto member_found = std::find_if(members.begin(), members.end(), [&](const web::json::value& nc_block_member_descriptor)
{
return role.as_string() == nmos::fields::nc::role(nc_block_member_descriptor);
});
if (members.end() != member_found)
{
descriptor = *member_found;
// use oid to look for the next resource
resource = nmos::find_resource(resources, utility::s2us(std::to_string(nmos::fields::nc::oid(*member_found))));
}
else
{
// no role
utility::stringstream_t ss;
ss << U("role: ") << role.as_string() << U(" not found to do FindMembersByPath");
return make_control_protocol_error_response(handle, { nc_method_status::bad_oid }, ss.str());
}
}
else
{
// no members
return make_control_protocol_error_response(handle, { nc_method_status::bad_oid }, U("no members to do FindMembersByPath"));
}
}
web::json::push_back(descriptors, descriptor);
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nc_method_status::ok }, descriptors);
}
// Finds members with given role name or fragment
web::json::value find_members_by_role(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
using web::json::value;
const auto& role = nmos::fields::nc::role(arguments); // Role text to search for
const auto& case_sensitive = nmos::fields::nc::case_sensitive(arguments); // Signals if the comparison should be case sensitive
const auto& match_whole_string = nmos::fields::nc::match_whole_string(arguments); // TRUE to only return exact matches
const auto& recurse = nmos::fields::nc::recurse(arguments); // TRUE to search nested blocks
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Find members with given role name or fragment: " << "role: " << role;
if (role.empty())
{
// empty role
return make_control_protocol_error_response(handle, { nc_method_status::parameter_error }, U("empty role to do FindMembersByRole"));
}
auto descriptors = value::array();
nmos::find_members_by_role(resources, resource, role, match_whole_string, case_sensitive, recurse, descriptors.as_array());
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nc_method_status::ok }, descriptors);
}
// Finds members with given class id
web::json::value find_members_by_class_id(nmos::resources& resources, nmos::resources::iterator resource, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
using web::json::value;
const auto& class_id = parse_nc_class_id(nmos::fields::nc::class_id(arguments)); // Class id to search for
const auto& include_derived = nmos::fields::nc::include_derived(arguments); // If TRUE it will also include derived class descriptors
const auto& recurse = nmos::fields::nc::recurse(arguments); // TRUE to search nested blocks
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Find members with given class id: " << "class_id: " << nmos::details::make_nc_class_id(class_id).serialize();
if (class_id.empty())
{
// empty class_id
return make_control_protocol_error_response(handle, { nc_method_status::parameter_error }, U("empty classId to do FindMembersByClassId"));
}
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
auto descriptors = value::array();
nmos::find_members_by_class_id(resources, resource, class_id, include_derived, recurse, descriptors.as_array());
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nc_method_status::ok }, descriptors);
}
// NcClassManager methods implementation
// Get a single class descriptor
web::json::value get_control_class(nmos::resources&, nmos::resources::iterator, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler get_control_protocol_class, get_control_protocol_datatype_handler, control_protocol_property_changed_handler, slog::base_gate& gate)
{
using web::json::value;
const auto& class_id = parse_nc_class_id(nmos::fields::nc::class_id(arguments)); // Class id to search for
const auto& include_inherited = nmos::fields::nc::include_inherited(arguments); // If set the descriptor would contain all inherited elements
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Get a single class descriptor: " << "class_id: " << nmos::details::make_nc_class_id(class_id).serialize();
if (class_id.empty())
{
// empty class_id
return make_control_protocol_error_response(handle, { nc_method_status::parameter_error }, U("empty classId to do GetControlClass"));
}
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
const auto& control_class = get_control_protocol_class(class_id);
if (!control_class.class_id.empty())
{
auto& description = control_class.description;
auto& name = control_class.name;
auto& fixed_role = control_class.fixed_role;
auto properties = control_class.properties;
auto methods = value::array();
for (const auto& method : control_class.methods) { web::json::push_back(methods, method.first); }
auto events = control_class.events;
if (include_inherited)
{
auto inherited_class_id = class_id;
inherited_class_id.pop_back();
while (!inherited_class_id.empty())
{
const auto& inherited_control_class = get_control_protocol_class(inherited_class_id);
{
for (const auto& property : inherited_control_class.properties.as_array()) { web::json::push_back(properties, property); }
for (const auto& method : inherited_control_class.methods) { web::json::push_back(methods, method.first); }
for (const auto& event : inherited_control_class.events.as_array()) { web::json::push_back(events, event); }
}
inherited_class_id.pop_back();
}
}
const auto descriptor = fixed_role.is_null()
? details::make_nc_class_descriptor(description, class_id, name, properties, methods, events)
: details::make_nc_class_descriptor(description, class_id, name, fixed_role.as_string(), properties, methods, events);
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nc_method_status::ok }, descriptor);
}
return make_control_protocol_error_response(handle, { nc_method_status::parameter_error }, U("classId not found"));
}
// Get a single datatype descriptor
web::json::value get_datatype(nmos::resources&, nmos::resources::iterator, int32_t handle, const web::json::value& arguments, bool is_deprecated, get_control_protocol_class_handler, get_control_protocol_datatype_handler get_control_protocol_datatype, control_protocol_property_changed_handler, slog::base_gate& gate)
{
// note, model mutex is already locked by the outter function, so access to control_protocol_resources is OK...
const auto& name = nmos::fields::nc::name(arguments); // name of datatype
const auto& include_inherited = nmos::fields::nc::include_inherited(arguments); // If set the descriptor would contain all inherited elements
slog::log<slog::severities::more_info>(gate, SLOG_FLF) << "Get a single datatype descriptor: " << "name: " << name;
if (name.empty())
{
// empty name
return make_control_protocol_error_response(handle, { nc_method_status::parameter_error }, U("empty name to do GetDatatype"));
}
const auto& datatype = get_control_protocol_datatype(name);
if (datatype.descriptor.size())
{
auto descriptor = datatype.descriptor;
if (include_inherited)
{
const auto& type = nmos::fields::nc::type(descriptor);
if (nc_datatype_type::Struct == type)
{
auto descriptor_ = descriptor;
for (;;)
{
const auto& parent_type = descriptor_.at(nmos::fields::nc::parent_type);
if (!parent_type.is_null())
{
const auto& parent_datatype = get_control_protocol_datatype(parent_type.as_string());
if (parent_datatype.descriptor.size())
{
descriptor_ = parent_datatype.descriptor;
const auto& fields = nmos::fields::nc::fields(descriptor_);
for (const auto& field : fields)
{
web::json::push_back(descriptor.at(nmos::fields::nc::fields), field);
}
}
}
else
{
break;
}
}
}
}
return make_control_protocol_message_response(handle, { is_deprecated ? nmos::nc_method_status::method_deprecated : nc_method_status::ok }, descriptor);
}
return make_control_protocol_error_response(handle, { nc_method_status::parameter_error }, U("name not found"));
}
}
}