Nyx Node
Loading...
Searching...
No Matches
indi_number.c
1/* NyxNode
2 * Author: Jérôme ODIER <jerome.odier@lpsc.in2p3.fr>
3 * SPDX-License-Identifier: GPL-2.0-only (Mongoose backend) or GPL-3.0+
4 */
5
6/*--------------------------------------------------------------------------------------------------------------------*/
7
8#include <stdio.h>
9
10#include "../nyx_node_internal.h"
11
12/*--------------------------------------------------------------------------------------------------------------------*/
13/* PROP */
14/*--------------------------------------------------------------------------------------------------------------------*/
15
16static nyx_dict_t *_prop_new(STR_t name, STR_t label, STR_t format, nyx_variant_t min, nyx_variant_t max, nyx_variant_t step, nyx_variant_t value)
17{
18 if(label == NULL || label[0] == '\0')
19 {
20 label = name;
21 }
22
23 /*----------------------------------------------------------------------------------------------------------------*/
24
25 nyx_dict_t *result = nyx_dict_new();
26
27 /*----------------------------------------------------------------------------------------------------------------*/
28
29 nyx_dict_set_string_unref(result, "<>", "defNumber", false);
30
31 /*----------------------------------------------------------------------------------------------------------------*/
32
33 nyx_dict_set_string_unref(result, "@name", nyx_string_dup(name), true);
34 nyx_dict_set_string_unref(result, "@label", nyx_string_dup(label), true);
35 nyx_dict_set_string_unref(result, "@format", nyx_string_dup(format), true);
36
37 nyx_dict_set_string_unref(result, "@min", internal_variant_to_string(format, min), true);
38 nyx_dict_set_string_unref(result, "@max", internal_variant_to_string(format, max), true);
39 nyx_dict_set_string_unref(result, "@step", internal_variant_to_string(format, step), true);
40
41 /*----------------------------------------------------------------------------------------------------------------*/
42
43 nyx_dict_set_string_unref(result, "$", internal_variant_to_string(format, value), true);
44
45 /*----------------------------------------------------------------------------------------------------------------*/
46
47 return result;
48}
49
50/*--------------------------------------------------------------------------------------------------------------------*/
51
52nyx_dict_t *nyx_number_prop_new_int(STR_t name, STR_t label, STR_t format, int32_t min, int32_t max, int32_t step, int32_t value)
53{
54 return _prop_new(name, label, format, NYX_VARIANT_FROM_INT(min), NYX_VARIANT_FROM_INT(max), NYX_VARIANT_FROM_INT(step), NYX_VARIANT_FROM_INT(value));
55}
56
57nyx_dict_t *nyx_number_prop_new_uint(STR_t name, STR_t label, STR_t format, uint32_t min, uint32_t max, uint32_t step, uint32_t value)
58{
59 return _prop_new(name, label, format, NYX_VARIANT_FROM_UINT(min), NYX_VARIANT_FROM_UINT(max), NYX_VARIANT_FROM_UINT(step), NYX_VARIANT_FROM_UINT(value));
60}
61
62nyx_dict_t *nyx_number_prop_new_long(STR_t name, STR_t label, STR_t format, int64_t min, int64_t max, int64_t step, int64_t value)
63{
64 return _prop_new(name, label, format, NYX_VARIANT_FROM_LONG(min), NYX_VARIANT_FROM_LONG(max), NYX_VARIANT_FROM_LONG(step), NYX_VARIANT_FROM_LONG(value));
65}
66
67nyx_dict_t *nyx_number_prop_new_ulong(STR_t name, STR_t label, STR_t format, uint64_t min, uint64_t max, uint64_t step, uint64_t value)
68{
69 return _prop_new(name, label, format, NYX_VARIANT_FROM_ULONG(min), NYX_VARIANT_FROM_ULONG(max), NYX_VARIANT_FROM_ULONG(step), NYX_VARIANT_FROM_ULONG(value));
70}
71
72nyx_dict_t *nyx_number_prop_new_double(STR_t name, STR_t label, STR_t format, double min, double max, double step, double value)
73{
74 return _prop_new(name, label, format, NYX_VARIANT_FROM_DOUBLE(min), NYX_VARIANT_FROM_DOUBLE(max), NYX_VARIANT_FROM_DOUBLE(step), NYX_VARIANT_FROM_DOUBLE(value));
75}
76
77/*--------------------------------------------------------------------------------------------------------------------*/
78/* PROP SETTER & GETTER */
79/*--------------------------------------------------------------------------------------------------------------------*/
80
81static bool _prop_set(const nyx_dict_t *prop, nyx_variant_t value)
82{
83 STR_t format = nyx_dict_get_string(prop, "@format");
84
85 return nyx_dict_set_string(prop, "$", internal_variant_to_string(format, value), true);
86}
87
88/*--------------------------------------------------------------------------------------------------------------------*/
89
90bool nyx_number_prop_set_int(nyx_dict_t *prop, int32_t value)
91{
92 return _prop_set(prop, NYX_VARIANT_FROM_INT(value));
93}
94
95bool nyx_number_prop_set_uint(nyx_dict_t *prop, uint32_t value)
96{
97 return _prop_set(prop, NYX_VARIANT_FROM_UINT(value));
98}
99
100bool nyx_number_prop_set_long(nyx_dict_t *prop, int64_t value)
101{
102 return _prop_set(prop, NYX_VARIANT_FROM_LONG(value));
103}
104
105bool nyx_number_prop_set_ulong(nyx_dict_t *prop, uint64_t value)
106{
107 return _prop_set(prop, NYX_VARIANT_FROM_ULONG(value));
108}
109
110bool nyx_number_prop_set_double(nyx_dict_t *prop, double value)
111{
112 return _prop_set(prop, NYX_VARIANT_FROM_DOUBLE(value));
113}
114
115/*--------------------------------------------------------------------------------------------------------------------*/
116
117static nyx_variant_t _prop_get(const nyx_dict_t *prop)
118{
119 STR_t format = nyx_dict_get_string(prop, "@format");
120
121 return internal_string_to_variant(format, nyx_dict_get_string(prop, "$"));
122}
123
124/*--------------------------------------------------------------------------------------------------------------------*/
125
127{
128 return _prop_get(prop).value._int;
129}
130
132{
133 return _prop_get(prop).value._uint;
134}
135
137{
138 return _prop_get(prop).value._long;
139}
140
142{
143 return _prop_get(prop).value._ulong;
144}
145
147{
148 return _prop_get(prop).value._double;
149}
150
151/*--------------------------------------------------------------------------------------------------------------------*/
152/* VECTOR */
153/*--------------------------------------------------------------------------------------------------------------------*/
154
155nyx_dict_t *nyx_number_vector_new(
156 STR_t device,
157 STR_t name,
158 nyx_state_t state,
159 nyx_perm_t perm,
160 nyx_dict_t *props[],
161 const nyx_opts_t *opts
162) {
163 nyx_dict_t *result = nyx_dict_new();
164
165 nyx_list_t *children = nyx_list_new();
166
167 /*----------------------------------------------------------------------------------------------------------------*/
168
169 nyx_dict_set_string_unref(result, "<>", "defNumberVector", false);
170
171 /*----------------------------------------------------------------------------------------------------------------*/
172
173 nyx_dict_set_string_unref(result, "@client", "unknown", false);
174 nyx_dict_set_string_unref(result, "@device", nyx_string_dup(device), true);
175 nyx_dict_set_string_unref(result, "@name", nyx_string_dup(name), true);
176
177 nyx_dict_set_string_unref(result, "@state", nyx_state_to_str(state), false);
178 nyx_dict_set_string_unref(result, "@perm", nyx_perm_to_str(perm), false);
179
180 /*----------------------------------------------------------------------------------------------------------------*/
181
182 internal_set_opts(result, opts);
183
184 /*----------------------------------------------------------------------------------------------------------------*/
185
186 nyx_dict_set(result, "children", children);
187
188 if(props) for(; *props != NULL; props++)
189 {
190 nyx_list_push(children, *props);
191
192 nyx_object_unref(*props);
193 }
194
195 nyx_object_unref(children);
196
197 /*----------------------------------------------------------------------------------------------------------------*/
198
199 return result;
200}
201
202/*--------------------------------------------------------------------------------------------------------------------*/
203/* VECTOR */
204/*--------------------------------------------------------------------------------------------------------------------*/
205
206nyx_dict_t *nyx_number_set_vector_new(const nyx_dict_t *vector)
207{
208 return internal_prop_to_set_vector(vector, "setNumberVector", "oneNumber");
209}
210
211/*--------------------------------------------------------------------------------------------------------------------*/
Struct describing a JSON dict object.
Struct describing a JSON list object.
#define STR_t
Alias for const char *.
Definition nyx_node.h:71
__NYX_NULLABLE__ str_t nyx_string_dup(__NYX_NULLABLE__ STR_t s)
Similar to libc strdup.
int64_t nyx_number_prop_get_long(const nyx_dict_t *prop)
Gets the value of the provided property object.
int32_t nyx_number_prop_get_int(const nyx_dict_t *prop)
Gets the value of the provided property object.
bool nyx_number_prop_set_int(nyx_dict_t *prop, int32_t value)
Sets the new value of the provided property object.
Definition indi_number.c:90
double nyx_number_prop_get_double(const nyx_dict_t *prop)
Gets the value of the provided property object.
uint64_t nyx_number_prop_get_ulong(const nyx_dict_t *prop)
Gets the value of the provided property object.
bool nyx_number_prop_set_ulong(nyx_dict_t *prop, uint64_t value)
Sets the new value of the provided property object.
bool nyx_number_prop_set_long(nyx_dict_t *prop, int64_t value)
Sets the new value of the provided property object.
bool nyx_number_prop_set_uint(nyx_dict_t *prop, uint32_t value)
Sets the new value of the provided property object.
Definition indi_number.c:95
uint32_t nyx_number_prop_get_uint(const nyx_dict_t *prop)
Gets the value of the provided property object.
bool nyx_number_prop_set_double(nyx_dict_t *prop, double value)
Sets the new value of the provided property object.
nyx_perm_t
Vector permission hint.
Definition nyx_node.h:1948
nyx_state_t
Vector state hint.
Definition nyx_node.h:1913
Struct describing the options for INDI / Nyx vectors.
Definition nyx_node.h:2049