|
1 /************************************************************************** |
|
2 * |
|
3 * Copyright (C) 2006 Steve Karg <skarg@users.sourceforge.net> |
|
4 * Copyright (C) 2011 Krzysztof Malorny <malornykrzysztof@gmail.com> |
|
5 * Copyright (C) 2017 Mario de Sousa <msousa@fe.up.pt> |
|
6 * |
|
7 * Permission is hereby granted, free of charge, to any person obtaining |
|
8 * a copy of this software and associated documentation files (the |
|
9 * "Software"), to deal in the Software without restriction, including |
|
10 * without limitation the rights to use, copy, modify, merge, publish, |
|
11 * distribute, sublicense, and/or sell copies of the Software, and to |
|
12 * permit persons to whom the Software is furnished to do so, subject to |
|
13 * the following conditions: |
|
14 * |
|
15 * The above copyright notice and this permission notice shall be included |
|
16 * in all copies or substantial portions of the Software. |
|
17 * |
|
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
|
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
|
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
|
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY |
|
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, |
|
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE |
|
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
|
25 * |
|
26 *********************************************************************/ |
|
27 |
|
28 /* Analog Input Objects - customize for your use */ |
|
29 |
|
30 #include <stdbool.h> |
|
31 #include <stdint.h> |
|
32 #include <stdio.h> |
|
33 #include <string.h> |
|
34 #include <math.h> /* NAN maro */ |
|
35 |
|
36 #include "config_bacnet_for_beremiz_%(locstr)s.h" /* the custom configuration for beremiz plugin */ |
|
37 #include "bacdef.h" |
|
38 #include "bacdcode.h" |
|
39 #include "bacenum.h" |
|
40 #include "bacapp.h" |
|
41 #include "bactext.h" |
|
42 #include "device_%(locstr)s.h" |
|
43 #include "handlers.h" |
|
44 #include "ai_%(locstr)s.h" |
|
45 |
|
46 |
|
47 |
|
48 /* initial value for present_value property of each object */ |
|
49 #define AI_VALUE_INIT (0) |
|
50 |
|
51 /* The IEC 61131-3 located variables mapped onto the Analog Input objects of BACnet protocol */ |
|
52 %(AI_lvars)s |
|
53 |
|
54 |
|
55 /* The array where we keep all the state related to the Analog Input Objects */ |
|
56 #define MAX_ANALOG_INPUTS %(AI_count)s |
|
57 static ANALOG_INPUT_DESCR AI_Descr[MAX_ANALOG_INPUTS] = { |
|
58 %(AI_param)s |
|
59 }; |
|
60 |
|
61 |
|
62 |
|
63 /* These three arrays are used by the ReadPropertyMultiple handler, |
|
64 * as well as to initialize the XXX_Property_List used by the |
|
65 * Property List (PROP_PROPERTY_LIST) property. |
|
66 */ |
|
67 static const int Analog_Input_Properties_Required[] = { |
|
68 /* (1) Currently Supported */ |
|
69 /* (2) Required by standard ASHRAE 135-2016 */ |
|
70 /*(1)(2) */ |
|
71 PROP_OBJECT_IDENTIFIER, /* R R ( 75) */ |
|
72 PROP_OBJECT_NAME, /* R R ( 77) */ |
|
73 PROP_OBJECT_TYPE, /* R R ( 79) */ |
|
74 PROP_PRESENT_VALUE, /* W R ( 85) */ |
|
75 PROP_STATUS_FLAGS, /* R R (111) */ |
|
76 PROP_EVENT_STATE, /* R R ( 36) */ |
|
77 PROP_OUT_OF_SERVICE, /* W R ( 81) */ |
|
78 PROP_UNITS, /* W R (117) */ |
|
79 // PROP_PROPERTY_LIST, /* R R (371) */ |
|
80 -1 |
|
81 }; |
|
82 |
|
83 static const int Analog_Input_Properties_Optional[] = { |
|
84 /* (1) Currently Supported */ |
|
85 /* (2) Required by standard ASHRAE 135-2016 */ |
|
86 /*(1)(2) */ |
|
87 PROP_DESCRIPTION, /* R O ( 28) */ |
|
88 -1 |
|
89 }; |
|
90 |
|
91 static const int Analog_Input_Properties_Proprietary[] = { |
|
92 -1 |
|
93 }; |
|
94 |
|
95 /* This array stores the PROPERTY_LIST which may be read by clients. |
|
96 * End of list is marked by following the last element with the value '-1' |
|
97 * |
|
98 * It is initialized by Analog_Inputs_Init() based off the values |
|
99 * stored in Analog_Input_Properties_Required |
|
100 * Analog_Input_Properties_Optional |
|
101 * Analog_Input_Properties_Proprietary |
|
102 */ |
|
103 /* TODO: Allocate memory for this array with malloc() at startup */ |
|
104 static int Analog_Input_Properties_List[64]; |
|
105 |
|
106 |
|
107 |
|
108 /********************************************************/ |
|
109 /** Callback functions. **/ |
|
110 /** Functions required by BACnet devie implementation. **/ |
|
111 /********************************************************/ |
|
112 |
|
113 |
|
114 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
115 void Analog_Input_Property_Lists( |
|
116 const int **pRequired, |
|
117 const int **pOptional, |
|
118 const int **pProprietary) |
|
119 { |
|
120 if (pRequired) |
|
121 *pRequired = Analog_Input_Properties_Required; |
|
122 if (pOptional) |
|
123 *pOptional = Analog_Input_Properties_Optional; |
|
124 if (pProprietary) |
|
125 *pProprietary = Analog_Input_Properties_Proprietary; |
|
126 |
|
127 return; |
|
128 } |
|
129 |
|
130 |
|
131 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
132 void Analog_Input_Init( |
|
133 void) |
|
134 { |
|
135 unsigned i, j; |
|
136 static bool initialized = false; |
|
137 |
|
138 if (!initialized) { |
|
139 initialized = true; |
|
140 |
|
141 /* initialize the Analog_Input_Properties_List array */ |
|
142 int len = 0; |
|
143 len += BACnet_Init_Properties_List(Analog_Input_Properties_List + len, |
|
144 Analog_Input_Properties_Required); |
|
145 len += BACnet_Init_Properties_List(Analog_Input_Properties_List + len, |
|
146 Analog_Input_Properties_Optional); |
|
147 len += BACnet_Init_Properties_List(Analog_Input_Properties_List + len, |
|
148 Analog_Input_Properties_Proprietary); |
|
149 |
|
150 for (i = 0; i < MAX_ANALOG_INPUTS; i++) { |
|
151 // MJS: the following line in th original demo code was commented out so we do not |
|
152 // overwrite the initial values configured by the user in beremiz IDE |
|
153 // memset(&AI_Descr[i], 0x00, sizeof(ANALOG_INPUT_DESCR)); |
|
154 AI_Descr[i].Present_Value = AI_VALUE_INIT; |
|
155 AI_Descr[i].Out_Of_Service = 0; |
|
156 AI_Descr[i].Event_State = 0; |
|
157 // AI_Descr[i].Units = UNITS_NO_UNITS; |
|
158 } |
|
159 } |
|
160 } |
|
161 |
|
162 |
|
163 |
|
164 |
|
165 /* validate that the given instance exists */ |
|
166 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
167 bool Analog_Input_Valid_Instance( |
|
168 uint32_t object_instance) |
|
169 { |
|
170 return (Analog_Input_Instance_To_Index(object_instance) < MAX_ANALOG_INPUTS); |
|
171 } |
|
172 |
|
173 /* the number of Analog Input Objects */ |
|
174 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
175 unsigned Analog_Input_Count(void) {return MAX_ANALOG_INPUTS;} |
|
176 |
|
177 |
|
178 /* returns the instance (i.e. Object Identifier) that correlates to the correct index */ |
|
179 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
180 uint32_t Analog_Input_Index_To_Instance(unsigned index) {return AI_Descr[index].Object_Identifier;} |
|
181 |
|
182 |
|
183 |
|
184 /* returns the index that correlates to the correct instance number (Object Identifier) */ |
|
185 unsigned Analog_Input_Instance_To_Index( |
|
186 uint32_t object_instance) |
|
187 { |
|
188 unsigned index = 0; |
|
189 |
|
190 for (index = 0; index < MAX_ANALOG_INPUTS; index++) |
|
191 if (object_instance == AI_Descr[index].Object_Identifier) |
|
192 return index; |
|
193 |
|
194 /* error, this object ID is not in our list! */ |
|
195 return MAX_ANALOG_INPUTS; |
|
196 } |
|
197 |
|
198 |
|
199 |
|
200 |
|
201 float Analog_Input_Present_Value( |
|
202 uint32_t object_instance) |
|
203 { |
|
204 float value = AI_VALUE_INIT; |
|
205 unsigned index = 0; |
|
206 unsigned i = 0; |
|
207 |
|
208 index = Analog_Input_Instance_To_Index(object_instance); |
|
209 if (index < MAX_ANALOG_INPUTS) |
|
210 value = AI_Descr[index].Present_Value; |
|
211 |
|
212 return value; |
|
213 } |
|
214 |
|
215 |
|
216 |
|
217 |
|
218 /* note: the object name must be unique within this device */ |
|
219 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
220 bool Analog_Input_Object_Name( |
|
221 uint32_t object_instance, |
|
222 BACNET_CHARACTER_STRING * object_name) |
|
223 { |
|
224 bool status = false; |
|
225 unsigned index = Analog_Input_Instance_To_Index(object_instance); |
|
226 |
|
227 if (index < MAX_ANALOG_INPUTS) |
|
228 status = characterstring_init_ansi(object_name, AI_Descr[index].Object_Name); |
|
229 |
|
230 return status; |
|
231 } |
|
232 |
|
233 |
|
234 |
|
235 bool Analog_Input_Object_Description( |
|
236 uint32_t object_instance, |
|
237 BACNET_CHARACTER_STRING * object_name) |
|
238 { |
|
239 bool status = false; |
|
240 unsigned index = Analog_Input_Instance_To_Index(object_instance); |
|
241 |
|
242 if (index < MAX_ANALOG_INPUTS) |
|
243 status = characterstring_init_ansi(object_name, AI_Descr[index].Description); |
|
244 |
|
245 return status; |
|
246 } |
|
247 |
|
248 |
|
249 |
|
250 /* return apdu len, or BACNET_STATUS_ERROR on error */ |
|
251 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
252 int Analog_Input_Read_Property( |
|
253 BACNET_READ_PROPERTY_DATA * rpdata) |
|
254 { |
|
255 int apdu_len = 0; /* return value */ |
|
256 BACNET_BIT_STRING bit_string; |
|
257 BACNET_CHARACTER_STRING char_string; |
|
258 float real_value = (float) 1.414; |
|
259 unsigned object_index = 0; |
|
260 bool state = false; |
|
261 uint8_t *apdu = NULL; |
|
262 |
|
263 if ((rpdata == NULL) || (rpdata->application_data == NULL) || |
|
264 (rpdata->application_data_len == 0)) { |
|
265 return 0; |
|
266 } |
|
267 |
|
268 apdu = rpdata->application_data; |
|
269 |
|
270 object_index = Analog_Input_Instance_To_Index(rpdata->object_instance); |
|
271 if (object_index >= MAX_ANALOG_INPUTS) { |
|
272 rpdata->error_class = ERROR_CLASS_OBJECT; |
|
273 rpdata->error_code = ERROR_CODE_UNKNOWN_OBJECT; |
|
274 return BACNET_STATUS_ERROR; |
|
275 } |
|
276 |
|
277 switch (rpdata->object_property) { |
|
278 case PROP_OBJECT_IDENTIFIER: |
|
279 apdu_len = |
|
280 encode_application_object_id(&apdu[0], OBJECT_ANALOG_INPUT, |
|
281 rpdata->object_instance); |
|
282 break; |
|
283 |
|
284 case PROP_OBJECT_NAME: |
|
285 Analog_Input_Object_Name(rpdata->object_instance, &char_string); |
|
286 apdu_len = |
|
287 encode_application_character_string(&apdu[0], &char_string); |
|
288 break; |
|
289 |
|
290 case PROP_DESCRIPTION: |
|
291 Analog_Input_Object_Description(rpdata->object_instance, &char_string); |
|
292 apdu_len = |
|
293 encode_application_character_string(&apdu[0], &char_string); |
|
294 break; |
|
295 |
|
296 case PROP_OBJECT_TYPE: |
|
297 apdu_len = |
|
298 encode_application_enumerated(&apdu[0], OBJECT_ANALOG_INPUT); |
|
299 break; |
|
300 |
|
301 case PROP_PRESENT_VALUE: |
|
302 real_value = Analog_Input_Present_Value(rpdata->object_instance); |
|
303 apdu_len = encode_application_real(&apdu[0], real_value); |
|
304 break; |
|
305 |
|
306 case PROP_STATUS_FLAGS: |
|
307 bitstring_init(&bit_string); |
|
308 bitstring_set_bit(&bit_string, STATUS_FLAG_IN_ALARM, false); |
|
309 bitstring_set_bit(&bit_string, STATUS_FLAG_FAULT, false); |
|
310 bitstring_set_bit(&bit_string, STATUS_FLAG_OVERRIDDEN, false); |
|
311 bitstring_set_bit(&bit_string, STATUS_FLAG_OUT_OF_SERVICE, |
|
312 AI_Descr[object_index].Out_Of_Service); |
|
313 |
|
314 apdu_len = encode_application_bitstring(&apdu[0], &bit_string); |
|
315 break; |
|
316 |
|
317 case PROP_EVENT_STATE: |
|
318 apdu_len = |
|
319 encode_application_enumerated(&apdu[0], EVENT_STATE_NORMAL); |
|
320 break; |
|
321 |
|
322 case PROP_OUT_OF_SERVICE: |
|
323 state = AI_Descr[object_index].Out_Of_Service; |
|
324 apdu_len = encode_application_boolean(&apdu[0], state); |
|
325 break; |
|
326 |
|
327 case PROP_UNITS: |
|
328 apdu_len = |
|
329 encode_application_enumerated(&apdu[0], AI_Descr[object_index].Units); |
|
330 break; |
|
331 |
|
332 // case PROP_PROPERTY_LIST: |
|
333 // BACnet_encode_array(Analog_Input_Properties_List, |
|
334 // property_list_count(Analog_Input_Properties_List), |
|
335 // retfalse, encode_application_enumerated); |
|
336 // break; |
|
337 default: |
|
338 rpdata->error_class = ERROR_CLASS_PROPERTY; |
|
339 rpdata->error_code = ERROR_CODE_UNKNOWN_PROPERTY; |
|
340 apdu_len = BACNET_STATUS_ERROR; |
|
341 break; |
|
342 } |
|
343 /* only array properties can have array options */ |
|
344 if ((apdu_len >= 0) && |
|
345 (rpdata->object_property != PROP_EVENT_TIME_STAMPS) && |
|
346 // (rpdata->object_property != PROP_PROPERTY_LIST) && |
|
347 (rpdata->array_index != BACNET_ARRAY_ALL)) { |
|
348 rpdata->error_class = ERROR_CLASS_PROPERTY; |
|
349 rpdata->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; |
|
350 apdu_len = BACNET_STATUS_ERROR; |
|
351 } |
|
352 |
|
353 return apdu_len; |
|
354 } |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 /* returns true if successful */ |
|
360 /* This is a callback function. Callback set in My_Object_Table[] array declared in device.c, */ |
|
361 bool Analog_Input_Write_Property( |
|
362 BACNET_WRITE_PROPERTY_DATA * wp_data) |
|
363 { |
|
364 bool status = false; /* return value */ |
|
365 unsigned int object_index = 0; |
|
366 int len = 0; |
|
367 BACNET_APPLICATION_DATA_VALUE value; |
|
368 ANALOG_INPUT_DESCR *CurrentAI; |
|
369 |
|
370 /* decode the some of the request */ |
|
371 len = |
|
372 bacapp_decode_application_data(wp_data->application_data, |
|
373 wp_data->application_data_len, &value); |
|
374 /* FIXME: len < application_data_len: more data? */ |
|
375 if (len < 0) { |
|
376 /* error while decoding - a value larger than we can handle */ |
|
377 wp_data->error_class = ERROR_CLASS_PROPERTY; |
|
378 wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; |
|
379 return false; |
|
380 } |
|
381 if ((wp_data->object_property != PROP_EVENT_TIME_STAMPS) && |
|
382 (wp_data->array_index != BACNET_ARRAY_ALL)) { |
|
383 /* only array properties can have array options */ |
|
384 wp_data->error_class = ERROR_CLASS_PROPERTY; |
|
385 wp_data->error_code = ERROR_CODE_PROPERTY_IS_NOT_AN_ARRAY; |
|
386 return false; |
|
387 } |
|
388 object_index = Analog_Input_Instance_To_Index(wp_data->object_instance); |
|
389 if (object_index < MAX_ANALOG_INPUTS) |
|
390 CurrentAI = &AI_Descr[object_index]; |
|
391 else |
|
392 return false; |
|
393 |
|
394 switch (wp_data->object_property) { |
|
395 case PROP_PRESENT_VALUE: |
|
396 status = |
|
397 WPValidateArgType(&value, BACNET_APPLICATION_TAG_REAL, |
|
398 &wp_data->error_class, &wp_data->error_code); |
|
399 if (!status) { |
|
400 wp_data->error_class = ERROR_CLASS_PROPERTY; |
|
401 wp_data->error_code = ERROR_CODE_VALUE_OUT_OF_RANGE; |
|
402 status = false; // not really necessary here. |
|
403 } else { |
|
404 if (!AI_Descr[object_index].Out_Of_Service) { |
|
405 /* input objects can only be written to when Out_Of_Service is true! */ |
|
406 wp_data->error_class = ERROR_CLASS_PROPERTY; |
|
407 wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; |
|
408 status = false; |
|
409 } else { |
|
410 AI_Descr[object_index].Present_Value = value.type.Real; |
|
411 status = true; |
|
412 } |
|
413 } |
|
414 break; |
|
415 |
|
416 case PROP_OUT_OF_SERVICE: |
|
417 { |
|
418 bool Previous_Out_Of_Service = CurrentAI->Out_Of_Service; |
|
419 status = |
|
420 WPValidateArgType(&value, BACNET_APPLICATION_TAG_BOOLEAN, |
|
421 &wp_data->error_class, &wp_data->error_code); |
|
422 if (status) { |
|
423 CurrentAI->Out_Of_Service = value.type.Boolean; |
|
424 if (Previous_Out_Of_Service && !CurrentAI->Out_Of_Service) |
|
425 /* We have just changed from Out_of_Service -> In Service */ |
|
426 /* We need to update the Present_Value to the value |
|
427 * currently in the PLC... |
|
428 */ |
|
429 CurrentAI->Present_Value = *(CurrentAI->Located_Var_ptr); |
|
430 } |
|
431 break; |
|
432 } |
|
433 |
|
434 case PROP_UNITS: |
|
435 status = |
|
436 WPValidateArgType(&value, BACNET_APPLICATION_TAG_ENUMERATED, |
|
437 &wp_data->error_class, &wp_data->error_code); |
|
438 if (status) { |
|
439 CurrentAI->Units = value.type.Enumerated; |
|
440 } |
|
441 break; |
|
442 |
|
443 case PROP_OBJECT_IDENTIFIER: |
|
444 case PROP_OBJECT_NAME: |
|
445 case PROP_OBJECT_TYPE: |
|
446 case PROP_STATUS_FLAGS: |
|
447 case PROP_EVENT_STATE: |
|
448 case PROP_DESCRIPTION: |
|
449 // case PROP_PROPERTY_LIST: |
|
450 wp_data->error_class = ERROR_CLASS_PROPERTY; |
|
451 wp_data->error_code = ERROR_CODE_WRITE_ACCESS_DENIED; |
|
452 break; |
|
453 default: |
|
454 wp_data->error_class = ERROR_CLASS_PROPERTY; |
|
455 wp_data->error_code = ERROR_CODE_UNKNOWN_PROPERTY; |
|
456 break; |
|
457 } |
|
458 |
|
459 return status; |
|
460 } |
|
461 |
|
462 |
|
463 |
|
464 |
|
465 |
|
466 |
|
467 |
|
468 /********************************************/ |
|
469 /** Functions required for Beremiz plugin **/ |
|
470 /********************************************/ |
|
471 |
|
472 void Analog_Input_Copy_Present_Value_to_Located_Var(void) { |
|
473 unsigned i; |
|
474 for (i = 0; i < MAX_ANALOG_INPUTS; i++) { |
|
475 // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true |
|
476 if (AI_Descr[i].Out_Of_Service) |
|
477 continue; |
|
478 |
|
479 // copy the value |
|
480 *(AI_Descr[i].Located_Var_ptr) = Analog_Input_Present_Value(AI_Descr[i].Object_Identifier); |
|
481 } |
|
482 } |
|
483 |
|
484 |
|
485 |
|
486 void Analog_Input_Copy_Located_Var_to_Present_Value(void) { |
|
487 unsigned i; |
|
488 for (i = 0; i < MAX_ANALOG_INPUTS; i++) { |
|
489 // decouple PLC's Located Variable from Bacnet Object's Present Value if Out_Of_Service is true |
|
490 if (AI_Descr[i].Out_Of_Service) |
|
491 continue; |
|
492 |
|
493 // copy the value |
|
494 AI_Descr[i].Present_Value = *(AI_Descr[i].Located_Var_ptr); |
|
495 } |
|
496 } |
|
497 |