-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmbus-protocol.c
3085 lines (2557 loc) · 99 KB
/
mbus-protocol.c
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//------------------------------------------------------------------------------
// Copyright (C) 2010-2011, Robert Johansson, Raditex AB
// All rights reserved.
//
// FreeSCADA
// http://www.FreeSCADA.com
//
//------------------------------------------------------------------------------
#include <assert.h>
#include <stdio.h>
#include <string.h>
#include <mbus/mbus-protocol.h>
static int parse_debug = 1, debug = 1;
static char error_str[512];
#define NITEMS(x) (sizeof(x)/sizeof(x[0]))
//------------------------------------------------------------------------------
// internal data
//------------------------------------------------------------------------------
static mbus_slave_data slave_data[MBUS_MAX_PRIMARY_SLAVES];
//------------------------------------------------------------------------------
/// Return a string that contains an the latest error message.
//------------------------------------------------------------------------------
char *
mbus_error_str()
{
return error_str;
}
void
mbus_error_str_set(char *message)
{
if (message)
{
snprintf(error_str, sizeof(error_str), "%s", message);
}
}
void
mbus_error_reset()
{
snprintf(error_str, sizeof(error_str), "no errors");
}
//------------------------------------------------------------------------------
/// Return a pointer to the slave_data register. This register can be used for
/// storing current slave status.
//------------------------------------------------------------------------------
mbus_slave_data *
mbus_slave_data_get(size_t i)
{
if (i < MBUS_MAX_PRIMARY_SLAVES)
{
return &slave_data[i];
}
return NULL;
}
//------------------------------------------------------------------------------
//
// M-Bus FRAME RELATED FUNCTIONS
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
/// Allocate an M-bus frame data structure and initialize it according to which
/// frame type is requested.
//------------------------------------------------------------------------------
mbus_frame *
mbus_frame_new(int frame_type)
{
mbus_frame *frame = NULL;
if ((frame = malloc(sizeof(mbus_frame))) != NULL)
{
memset((void *)frame, 0, sizeof(mbus_frame));
frame->type = frame_type;
switch (frame->type)
{
case MBUS_FRAME_TYPE_ACK:
frame->start1 = MBUS_FRAME_ACK_START;
break;
case MBUS_FRAME_TYPE_SHORT:
frame->start1 = MBUS_FRAME_SHORT_START;
frame->stop = MBUS_FRAME_STOP;
break;
case MBUS_FRAME_TYPE_CONTROL:
frame->start1 = MBUS_FRAME_CONTROL_START;
frame->start2 = MBUS_FRAME_CONTROL_START;
frame->length1 = 3;
frame->length2 = 3;
frame->stop = MBUS_FRAME_STOP;
break;
case MBUS_FRAME_TYPE_LONG:
frame->start1 = MBUS_FRAME_LONG_START;
frame->start2 = MBUS_FRAME_LONG_START;
frame->stop = MBUS_FRAME_STOP;
break;
}
}
return frame;
}
//------------------------------------------------------------------------------
/// Free the memory resources allocated for the M-Bus frame data structure.
//------------------------------------------------------------------------------
int
mbus_frame_free(mbus_frame *frame)
{
if (frame)
{
free(frame);
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
/// Caclulate the checksum of the M-Bus frame. Internal.
//------------------------------------------------------------------------------
u_char
calc_checksum(mbus_frame *frame)
{
size_t i;
u_char cksum;
assert(frame != NULL);
switch(frame->type)
{
case MBUS_FRAME_TYPE_SHORT:
cksum = frame->control;
cksum += frame->address;
break;
case MBUS_FRAME_TYPE_CONTROL:
cksum = frame->control;
cksum += frame->address;
cksum += frame->control_information;
break;
case MBUS_FRAME_TYPE_LONG:
cksum = frame->control;
cksum += frame->address;
cksum += frame->control_information;
for (i = 0; i < frame->data_size; i++)
{
cksum += frame->data[i];
}
break;
case MBUS_FRAME_TYPE_ACK:
default:
cksum = 0;
}
return cksum;
}
//------------------------------------------------------------------------------
/// Caclulate the checksum of the M-Bus frame. The checksum algorithm is the
/// arithmetic sum of the frame content, without using carry. Which content
/// that is included in the checksum calculation depends on the frame type.
//------------------------------------------------------------------------------
int
mbus_frame_calc_checksum(mbus_frame *frame)
{
if (frame)
{
switch (frame->type)
{
case MBUS_FRAME_TYPE_ACK:
case MBUS_FRAME_TYPE_SHORT:
case MBUS_FRAME_TYPE_CONTROL:
case MBUS_FRAME_TYPE_LONG:
frame->checksum = calc_checksum(frame);
break;
default:
return -1;
}
}
return 0;
}
///
/// Calculate the values of the lengths fields in the M-Bus frame. Internal.
///
u_char
calc_length(const mbus_frame *frame)
{
assert(frame != NULL);
switch(frame->type)
{
case MBUS_FRAME_TYPE_CONTROL:
return 3;
case MBUS_FRAME_TYPE_LONG:
return frame->data_size + 3;
default:
return 0;
}
}
//------------------------------------------------------------------------------
/// Calculate the values of the lengths fields in the M-Bus frame.
//------------------------------------------------------------------------------
int
mbus_frame_calc_length(mbus_frame *frame)
{
if (frame)
{
frame->length1 = frame->length2 = calc_length(frame);
}
return 0;
}
//------------------------------------------------------------------------------
/// Return the M-Bus frame type
//------------------------------------------------------------------------------
int
mbus_frame_type(mbus_frame *frame)
{
if (frame)
{
return frame->type;
}
return -1;
}
//------------------------------------------------------------------------------
/// Verify that parsed frame is a valid M-bus frame.
//
// Possible checks:
//
// 1) Start/stop bytes
// 2) length field and actual data size
// 3) checksum
//
//------------------------------------------------------------------------------
int
mbus_frame_verify(mbus_frame *frame)
{
if (frame)
{
switch (frame->type)
{
case MBUS_FRAME_TYPE_ACK:
return frame->start1 == MBUS_FRAME_ACK_START;
case MBUS_FRAME_TYPE_SHORT:
if(frame->start1 != MBUS_FRAME_SHORT_START)
return -1;
break;
case MBUS_FRAME_TYPE_CONTROL:
case MBUS_FRAME_TYPE_LONG:
if(frame->start1 != MBUS_FRAME_CONTROL_START ||
frame->start2 != MBUS_FRAME_CONTROL_START ||
frame->length1 != frame->length2 ||
frame->length1 != calc_length(frame))
return -1;
break;
default:
return -1;
}
if(frame->stop != MBUS_FRAME_STOP ||
frame->checksum != calc_checksum(frame))
return -1;
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
//
// DATA ENCODING, DECODING, AND CONVERSION FUNCTIONS
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
///
/// Encode BCD data
///
//------------------------------------------------------------------------------
int
mbus_data_bcd_encode(u_char *bcd_data, size_t bcd_data_size, int value)
{
int val = 0, v0, v1, v2, x1, x2;
size_t i;
v2 = value;
if (bcd_data)
{
for (i = 0; i < bcd_data_size; i++)
{
v0 = v2;
v1 = (int)(v0 / 10.0);
v2 = (int)(v1 / 10.0);
x1 = v0 - v1 * 10;
x2 = v1 - v2 * 10;
bcd_data[bcd_data_size-1-i] = (x2 << 4) | x1;
}
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
///
/// Decode BCD data
///
//------------------------------------------------------------------------------
long
mbus_data_bcd_decode(u_char *bcd_data, size_t bcd_data_size)
{
long val = 0;
size_t i;
if (bcd_data)
{
for (i = bcd_data_size; i > 0; i--)
{
val = (val * 10) + ((bcd_data[i-1]>>4) & 0xF);
val = (val * 10) + ( bcd_data[i-1] & 0xF);
}
return val;
}
return -1;
}
//------------------------------------------------------------------------------
///
/// Decode INTEGER data
///
//------------------------------------------------------------------------------
int
mbus_data_int_decode(u_char *int_data, size_t int_data_size)
{
int val = 0;
size_t i;
if (int_data)
{
for (i = int_data_size; i > 0; i--)
{
val = (val << 8) + int_data[i-1];
}
return val;
}
return -1;
}
float
mbus_data_float_decode(u_char *float_data, size_t float_data_size)
{
float val = 0.0;
int tmp_val = 0;
size_t i;
if (float_data)
{
for (i = float_data_size; i > 0; i--)
{
tmp_val = (tmp_val << 8) + float_data[i-1];
}
val = *(float *)(&tmp_val);
return val;
}
return -1;
}
long
mbus_data_long_decode(u_char *int_data, size_t int_data_size)
{
long val = 0;
size_t i;
if (int_data)
{
for (i = int_data_size; i > 0; i--)
{
val = (val << 8) + int_data[i-1];
}
return val;
}
return -1;
}
//------------------------------------------------------------------------------
///
/// Encode INTEGER data (into 'int_data_size' bytes)
///
//------------------------------------------------------------------------------
int
mbus_data_int_encode(u_char *int_data, size_t int_data_size, int value)
{
int val = 0, i;
if (int_data)
{
for (i = 0; i < int_data_size; i++)
{
int_data[i] = (value>>(i*8)) & 0xFF;
}
return 0;
}
return -1;
}
//------------------------------------------------------------------------------
///
/// Decode string data.
///
//------------------------------------------------------------------------------
void
mbus_data_str_decode(u_char *dst, const u_char *src, size_t len)
{
size_t i;
i = 0;
dst[len] = '\0';
while(len > 0) {
dst[i++] = src[--len];
}
}
//------------------------------------------------------------------------------
///
/// Decode time data (usable for type f = 4 bytes or type g = 2 bytes)
///
//------------------------------------------------------------------------------
void
mbus_data_tm_decode(struct tm *t, u_char *t_data, size_t t_data_size)
{
t->tm_sec = 0;
t->tm_min = 0;
t->tm_hour = 0;
t->tm_mday = 0;
t->tm_mon = 0;
t->tm_year = 0;
t->tm_isdst = 0;
if (t && t_data)
{
if (t_data_size == 4) // Type F = Compound CP32: Date and Time
{
if ((t_data[0] & 0x80) == 0) // Time valid ?
{
t->tm_min = t_data[0] & 0x3F;
t->tm_hour = t_data[1] & 0x1F;
t->tm_mday = t_data[2] & 0x1F;
t->tm_mon = (t_data[3] & 0x0F) - 1;
t->tm_year = ((t_data[2] & 0xE0) >> 5) |
((t_data[3] & 0xF0) >> 1);
t->tm_isdst = (t_data[1] & 0x80) ? 1 : 0; // day saving time
}
}
else if (t_data_size == 2) // Type G: Compound CP16: Date
{
t->tm_mday = t_data[0] & 0x1F;
t->tm_mon = (t_data[1] & 0x0F) - 1;
t->tm_year = ((t_data[0] & 0xE0) >> 5) |
((t_data[1] & 0xF0) >> 1);
}
}
}
//------------------------------------------------------------------------------
///
/// Generate manufacturer code from 2-byte encoded data
///
//------------------------------------------------------------------------------
int
mbus_data_manufacturer_encode(u_char *m_data, u_char *m_code)
{
int m_val;
if (m_data == NULL || m_code == NULL)
return -1;
m_val = ((((int)m_code[0] - 64) & 0x001F) << 10) +
((((int)m_code[1] - 64) & 0x001F) << 5) +
((((int)m_code[2] - 64) & 0x001F));
mbus_data_int_encode(m_data, 2, m_val);
return 0;
}
//------------------------------------------------------------------------------
///
/// Generate manufacturer code from 2-byte encoded data
///
//------------------------------------------------------------------------------
const char *
mbus_decode_manufacturer(u_char byte1, u_char byte2)
{
static char m_str[4];
int m_id;
m_str[0] = byte1;
m_str[1] = byte2;
m_id = mbus_data_int_decode(m_str, 2);
m_str[0] = (char)(((m_id>>10) & 0x001F) + 64);
m_str[1] = (char)(((m_id>>5) & 0x001F) + 64);
m_str[2] = (char)(((m_id) & 0x001F) + 64);
m_str[3] = 0;
return m_str;
}
//------------------------------------------------------------------------------
//
// FIXED-LENGTH DATA RECORD FUNCTIONS
//
//------------------------------------------------------------------------------
//
// Value Field Medium/Unit Medium
// hexadecimal Bit 16 Bit 15 Bit 8 Bit 7
// 0 0 0 0 0 Other
// 1 0 0 0 1 Oil
// 2 0 0 1 0 Electricity
// 3 0 0 1 1 Gas
// 4 0 1 0 0 Heat
// 5 0 1 0 1 Steam
// 6 0 1 1 0 Hot Water
// 7 0 1 1 1 Water
// 8 1 0 0 0 H.C.A.
// 9 1 0 0 1 Reserved
// A 1 0 1 0 Gas Mode 2
// B 1 0 1 1 Heat Mode 2
// C 1 1 0 0 Hot Water Mode 2
// D 1 1 0 1 Water Mode 2
// E 1 1 1 0 H.C.A. Mode 2
// F 1 1 1 1 Reserved
//
///
/// For fixed-length frames, get a string describing the medium.
///
const char *
mbus_data_fixed_medium(mbus_data_fixed *data)
{
static char buff[256];
if (data)
{
switch ( (data->cnt1_type&0xC0)>>6 | (data->cnt2_type&0xC0)>>4 )
{
case 0x00:
snprintf(buff, sizeof(buff), "Other");
break;
case 0x01:
snprintf(buff, sizeof(buff), "Oil");
break;
case 0x02:
snprintf(buff, sizeof(buff), "Electricity");
break;
case 0x03:
snprintf(buff, sizeof(buff), "Gas");
break;
case 0x04:
snprintf(buff, sizeof(buff), "Heat");
break;
case 0x05:
snprintf(buff, sizeof(buff), "Steam");
break;
case 0x06:
snprintf(buff, sizeof(buff), "Hot Water");
break;
case 0x07:
snprintf(buff, sizeof(buff), "Water");
break;
case 0x08:
snprintf(buff, sizeof(buff), "H.C.A.");
break;
case 0x09:
snprintf(buff, sizeof(buff), "Reserved");
break;
case 0x0A:
snprintf(buff, sizeof(buff), "Gas Mode 2");
break;
case 0x0B:
snprintf(buff, sizeof(buff), "Heat Mode 2");
break;
case 0x0C:
snprintf(buff, sizeof(buff), "Hot Water Mode 2");
break;
case 0x0D:
snprintf(buff, sizeof(buff), "Water Mode 2");
break;
case 0x0E:
snprintf(buff, sizeof(buff), "H.C.A. Mode 2");
break;
case 0x0F:
snprintf(buff, sizeof(buff), "Reserved");
break;
default:
snprintf(buff, sizeof(buff), "unknown");
break;
}
return buff;
}
return NULL;
}
//------------------------------------------------------------------------------
// Hex code Hex code
//Unit share Unit share
// MSB..LSB MSB..LSB
// Byte 7/8 Byte 7/8
// h,m,s 000000 00 MJ/h 100000 20
// D,M,Y 000001 01 MJ/h * 10 100001 21
// Wh 000010 02 MJ/h * 100 100010 22
// Wh * 10 000011 03 GJ/h 100011 23
// Wh * 100 000100 04 GJ/h * 10 100100 24
// kWh 000101 05 GJ/h * 100 100101 25
// kWh * 10 000110 06 ml 100110 26
// kWh * 100 000111 07 ml * 10 100111 27
// MWh 001000 08 ml * 100 101000 28
// MWh * 10 001001 09 l 101001 29
// MWh * 100 001010 0A l * 10 101010 2A
// kJ 001011 0B l * 100 101011 2B
// kJ * 10 001100 0C m3 101100 2C
// kJ * 100 001101 0D m3 * 10 101101 2D
// MJ 001110 0E m3 * 100 101110 2E
// MJ * 10 001111 0F ml/h 101111 2F
// MJ * 100 010000 10 ml/h * 10 110000 30
// GJ 010001 11 ml/h * 100 110001 31
// GJ * 10 010010 12 l/h 110010 32
// GJ * 100 010011 13 l/h * 10 110011 33
// W 010100 14 l/h * 100 110100 34
// W * 10 010101 15 m3/h 110101 35
// W * 100 010110 16 m3/h * 10 110110 36
// kW 010111 17 m3/h * 100 110111 37
// kW * 10 011000 18 °C* 10-3 111000 38
// kW * 100 011001 19 units for HCA 111001 39
// MW 011010 1A reserved 111010 3A
// MW * 10 011011 1B reserved 111011 3B
// MW * 100 011100 1C reserved 111100 3C
// kJ/h 011101 1D reserved 111101 3D
// kJ/h * 10 011110 1E same but historic 111110 3E
// kJ/h * 100 011111 1F without units 111111 3F
//
//------------------------------------------------------------------------------
///
/// For fixed-length frames, get a string describing the unit of the data.
///
const char *
mbus_data_fixed_unit(int medium_unit_byte)
{
static char buff[256];
switch (medium_unit_byte & 0x3F)
{
case 0x00:
snprintf(buff, sizeof(buff), "h,m,s");
break;
case 0x01:
snprintf(buff, sizeof(buff), "D,M,Y");
break;
case 0x02:
snprintf(buff, sizeof(buff), "Wh");
break;
case 0x03:
snprintf(buff, sizeof(buff), "10 Wh");
break;
case 0x04:
snprintf(buff, sizeof(buff), "100 Wh");
break;
case 0x05:
snprintf(buff, sizeof(buff), "kWh");
break;
case 0x06:
snprintf(buff, sizeof(buff), "10 kWh");
break;
case 0x07:
snprintf(buff, sizeof(buff), "100 kWh");
break;
case 0x08:
snprintf(buff, sizeof(buff), "MWh");
break;
case 0x09:
snprintf(buff, sizeof(buff), "10 MWh");
break;
case 0x0A:
snprintf(buff, sizeof(buff), "100 MWh");
break;
case 0x0B:
snprintf(buff, sizeof(buff), "kJ");
break;
case 0x0C:
snprintf(buff, sizeof(buff), "10 kJ");
break;
case 0x0E:
snprintf(buff, sizeof(buff), "100 kJ");
break;
case 0x0D:
snprintf(buff, sizeof(buff), "MJ");
break;
case 0x0F:
snprintf(buff, sizeof(buff), "10 MJ");
break;
case 0x10:
snprintf(buff, sizeof(buff), "100 MJ");
break;
case 0x11:
snprintf(buff, sizeof(buff), "GJ");
break;
case 0x12:
snprintf(buff, sizeof(buff), "10 GJ");
break;
case 0x13:
snprintf(buff, sizeof(buff), "100 GJ");
break;
case 0x14:
snprintf(buff, sizeof(buff), "W");
break;
case 0x15:
snprintf(buff, sizeof(buff), "10 W");
break;
case 0x16:
snprintf(buff, sizeof(buff), "100 W");
break;
case 0x17:
snprintf(buff, sizeof(buff), "kW");
break;
case 0x18:
snprintf(buff, sizeof(buff), "10 kW");
break;
case 0x19:
snprintf(buff, sizeof(buff), "100 kW");
break;
case 0x1A:
snprintf(buff, sizeof(buff), "MW");
break;
case 0x1B:
snprintf(buff, sizeof(buff), "10 MW");
break;
case 0x1C:
snprintf(buff, sizeof(buff), "100 MW");
break;
case 0x1D:
snprintf(buff, sizeof(buff), "kJ/h");
break;
case 0x1E:
snprintf(buff, sizeof(buff), "10 kJ/h");
break;
case 0x1F:
snprintf(buff, sizeof(buff), "100 kJ/h");
break;
case 0x20:
snprintf(buff, sizeof(buff), "MJ/h");
break;
case 0x21:
snprintf(buff, sizeof(buff), "10 MJ/h");
break;
case 0x22:
snprintf(buff, sizeof(buff), "100 MJ/h");
break;
case 0x23:
snprintf(buff, sizeof(buff), "GJ/h");
break;
case 0x24:
snprintf(buff, sizeof(buff), "10 GJ/h");
break;
case 0x25:
snprintf(buff, sizeof(buff), "100 GJ/h");
break;
case 0x26:
snprintf(buff, sizeof(buff), "ml");
break;
case 0x27:
snprintf(buff, sizeof(buff), "10 ml");
break;
case 0x28:
snprintf(buff, sizeof(buff), "100 ml");
break;
case 0x29:
snprintf(buff, sizeof(buff), "l");
break;
case 0x2A:
snprintf(buff, sizeof(buff), "10 l");
break;
case 0x2B:
snprintf(buff, sizeof(buff), "100 l");
break;
case 0x2C:
snprintf(buff, sizeof(buff), "m^3");
break;
case 0x2D:
snprintf(buff, sizeof(buff), "10 m^3");
break;
case 0x2E:
snprintf(buff, sizeof(buff), "m^3");
break;
case 0x2F:
snprintf(buff, sizeof(buff), "ml/h");
break;
case 0x30:
snprintf(buff, sizeof(buff), "10 ml/h");
break;
case 0x31:
snprintf(buff, sizeof(buff), "100 ml/h");
break;
case 0x32:
snprintf(buff, sizeof(buff), "l/h");
break;
case 0x33:
snprintf(buff, sizeof(buff), "10 l/h");
break;
case 0x34:
snprintf(buff, sizeof(buff), "100 l/h");
break;
case 0x35:
snprintf(buff, sizeof(buff), "m^3/h");
break;
case 0x36:
snprintf(buff, sizeof(buff), "10 m^3/h");
break;
case 0x37:
snprintf(buff, sizeof(buff), "100 m^3/h");
break;
case 0x38:
snprintf(buff, sizeof(buff), "1e-3 °C");
break;
case 0x39:
snprintf(buff, sizeof(buff), "units for HCA");
break;
case 0x3A:
case 0x3B:
case 0x3C:
case 0x3D:
snprintf(buff, sizeof(buff), "reserved");
break;
case 0x3E:
snprintf(buff, sizeof(buff), "reserved but historic");
break;
case 0x3F:
snprintf(buff, sizeof(buff), "without units");
break;
default:
snprintf(buff, sizeof(buff), "unknown");
break;
}
return buff;
}
//------------------------------------------------------------------------------
//
// VARIABLE-LENGTH DATA RECORD FUNCTIONS
//
//------------------------------------------------------------------------------
//------------------------------------------------------------------------------
//
// Medium Code bin Code hex
// Other 0000 0000 00
// Oil 0000 0001 01
// Electricity 0000 0010 02
// Gas 0000 0011 03
// Heat (Volume measured at return temperature: outlet) 0000 0100 04
// Steam 0000 0101 05
// Hot Water 0000 0110 06
// Water 0000 0111 07
// Heat Cost Allocator. 0000 1000 08
// Compressed Air 0000 1001 09
// Cooling load meter (Volume measured at return temperature: outlet) 0000 1010 0A
// Cooling load meter (Volume measured at flow temperature: inlet) ♣ 0000 1011 0B
// Heat (Volume measured at flow temperature: inlet) 0000 1100 0C
// Heat / Cooling load meter ♣ 0000 1101 OD
// Bus / System 0000 1110 0E
// Unknown Medium 0000 1111 0F
// Reserved .......... 10 to 15
// Cold Water 0001 0110 16
// Dual Water 0001 0111 17
// Pressure 0001 1000 18
// A/D Converter 0001 1001 19
// Reserved .......... 20 to FF
//------------------------------------------------------------------------------
///
/// For variable-length frames, returns a string describing the medium.
///
const char *
mbus_data_variable_medium_lookup(u_char medium)
{
static char buff[256];
switch (medium)
{
case MBUS_VARIABLE_DATA_MEDIUM_OTHER:
snprintf(buff, sizeof(buff), "Other");
break;
case MBUS_VARIABLE_DATA_MEDIUM_OIL:
snprintf(buff, sizeof(buff), "Oil");
break;
case MBUS_VARIABLE_DATA_MEDIUM_ELECTRICITY:
snprintf(buff, sizeof(buff), "Electricity");
break;
case MBUS_VARIABLE_DATA_MEDIUM_GAS:
snprintf(buff, sizeof(buff), "Gas");
break;
case MBUS_VARIABLE_DATA_MEDIUM_HEAT:
snprintf(buff, sizeof(buff), "Heat");
break;
case MBUS_VARIABLE_DATA_MEDIUM_STEAM:
snprintf(buff, sizeof(buff), "Steam");
break;
case MBUS_VARIABLE_DATA_MEDIUM_HOT_WATER:
snprintf(buff, sizeof(buff), "Hot water");
break;
case MBUS_VARIABLE_DATA_MEDIUM_WATER:
snprintf(buff, sizeof(buff), "Water");
break;
case MBUS_VARIABLE_DATA_MEDIUM_HEAT_COST:
snprintf(buff, sizeof(buff), "Heat Cost Allocator");
break;
case MBUS_VARIABLE_DATA_MEDIUM_COMPR_AIR:
snprintf(buff, sizeof(buff), "Compressed Air");
break;
case MBUS_VARIABLE_DATA_MEDIUM_COOL_OUT:
snprintf(buff, sizeof(buff), "Cooling load meter: Outlet");