/
client.ino
1014 lines (974 loc) · 28.6 KB
/
client.ino
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
//
// --------------------------------------------------------------------------
// Gurux Ltd
//
//
//
//
// Version: $Revision: 10346 $,
// $Date: 2018-10-29 16:08:18 +0200 (Mon, 29 Oct 2018) $
// $Author: gurux01 $
//
// Copyright (c) Gurux Ltd
//
//---------------------------------------------------------------------------
//
// DESCRIPTION
//
// This file is a part of Gurux Device Framework.
//
// Gurux Device Framework is Open Source 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; version 2 of the License.
// Gurux Device Framework 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.
//
// More information of Gurux DLMS/COSEM Director: http://www.gurux.org/GXDLMSDirector
//
// This code is licensed under the GNU General Public License v2.
// Full text may be retrieved at http://www.gnu.org/licenses/gpl-2.0.txt
//---------------------------------------------------------------------------
#include <EEPROM.h>
#include "GXDLMSClient.h"
//---------------------------------------------------------------------------
// Un-comment ignored objects from ArduinoIgnore.h to improve performance
// and memory usage.
//---------------------------------------------------------------------------
//Received data.
gxByteBuffer frameData;
//How long reply is waited from the meter.
const uint32_t WAIT_TIME = 2000;
//How many times message is try to resend to the meter.
const uint8_t RESEND_COUNT = 3;
uint16_t EEPROM_SIZE()
{
return EEPROM.length();
}
int EEPROM_READ(uint16_t index, unsigned char* value)
{
*value = EEPROM.read(index);
return 0;
}
int EEPROM_WRITE(uint16_t index, unsigned char value)
{
EEPROM.write(index, value);
return 0;
}
///////////////////////////////////////////////////////////////////////
// Write trace to the serial port.
//
// This can be used for debugging.
///////////////////////////////////////////////////////////////////////
void GXTRACE(const char* str, const char* data)
{
//Send trace to the serial port.
byte c;
Serial1.write("\t:");
while ((c = pgm_read_byte(str++)) != 0)
{
Serial1.write(c);
}
if (data != NULL)
{
Serial1.write(data);
}
Serial1.write("\0");
//Serial1.flush();
delay(10);
}
///////////////////////////////////////////////////////////////////////
// Write trace to the serial port.
//
// This can be used for debugging.
///////////////////////////////////////////////////////////////////////
void GXTRACE_INT(const char* str, int32_t value)
{
char data[10];
sprintf(data, " %ld", value);
GXTRACE(str, data);
}
uint32_t runTime = 0;
///////////////////////////////////////////////////////////////////////
//Returns the approximate processor time in ms.
///////////////////////////////////////////////////////////////////////
uint32_t time_elapsed(void)
{
return millis();
}
///////////////////////////////////////////////////////////////////////
// Returns current time.
// Get current time.
// Because there is no clock, clock object keeps base time and uptime is added to that.
///////////////////////////////////////////////////////////////////////
void time_now(gxtime* value)
{
time_addSeconds(value, time_elapsed() / 1000);
}
int com_readSerialPort(
unsigned char eop)
{
//Read reply data.
uint16_t pos;
uint16_t available;
unsigned char eopFound = 0;
uint16_t lastReadIndex = frameData.position;
uint32_t start = millis();
do
{
available = Serial.available();
if (available != 0)
{
if (frameData.size + available > frameData.capacity)
{
bb_capacity(&frameData, 20 + frameData.size + available);
}
Serial.readBytes((char*) (frameData.data + frameData.size), available);
frameData.size += available;
//Search eop.
if (frameData.size > 5)
{
//Some optical strobes can return extra bytes.
for (pos = frameData.size - 1; pos != lastReadIndex; --pos)
{
if (frameData.data[pos] == eop)
{
eopFound = 1;
break;
}
}
lastReadIndex = pos;
}
}
else
{
delay(50);
}
//If the meter doesn't reply in given time.
if (!(millis() - start < WAIT_TIME))
{
GXTRACE_INT(GET_STR_FROM_EEPROM("Received bytes: \n"), frameData.size);
return DLMS_ERROR_CODE_RECEIVE_FAILED;
}
} while (eopFound == 0);
return DLMS_ERROR_CODE_OK;
}
// Read DLMS Data frame from the device.
int readDLMSPacket(
gxByteBuffer* data,
gxReplyData* reply)
{
int resend = 0, ret = DLMS_ERROR_CODE_OK;
if (data->size == 0)
{
return DLMS_ERROR_CODE_OK;
}
reply->complete = 0;
frameData.size = 0;
frameData.position = 0;
//Send data.
if ((ret = Serial.write(data->data, data->size)) != data->size)
{
//If failed to write all bytes.
GXTRACE(GET_STR_FROM_EEPROM("Failed to write all data to the serial port.\n"), NULL);
}
//Loop until packet is complete.
do
{
if ((ret = com_readSerialPort(0x7E)) != 0)
{
if (ret == DLMS_ERROR_CODE_RECEIVE_FAILED && resend == RESEND_COUNT)
{
return DLMS_ERROR_CODE_SEND_FAILED;
}
++resend;
GXTRACE_INT(GET_STR_FROM_EEPROM("Data send failed. Try to resend."), resend);
if ((ret = Serial.write(data->data, data->size)) != data->size)
{
//If failed to write all bytes.
GXTRACE(GET_STR_FROM_EEPROM("Failed to write all data to the serial port.\n"), NULL);
}
}
ret = Client.GetData(&frameData, reply);
if (ret != 0 && ret != DLMS_ERROR_CODE_FALSE)
{
break;
}
} while (reply->complete == 0);
return ret;
}
int com_readDataBlock(
message* messages,
gxReplyData* reply)
{
gxByteBuffer rr;
int pos, ret = DLMS_ERROR_CODE_OK;
//If there is no data to send.
if (messages->size == 0)
{
return DLMS_ERROR_CODE_OK;
}
BYTE_BUFFER_INIT(&rr);
//Send data.
for (pos = 0; pos != messages->size; ++pos)
{
//Send data.
if ((ret = readDLMSPacket(messages->data[pos], reply)) != DLMS_ERROR_CODE_OK)
{
return ret;
}
//Check is there errors or more data from server
while (reply_isMoreData(reply))
{
if ((ret = Client.ReceiverReady(reply->moreData, &rr)) != DLMS_ERROR_CODE_OK)
{
bb_clear(&rr);
return ret;
}
if ((ret = readDLMSPacket(&rr, reply)) != DLMS_ERROR_CODE_OK)
{
bb_clear(&rr);
return ret;
}
bb_clear(&rr);
}
}
return ret;
}
//Close connection to the meter.
int com_close()
{
int ret = DLMS_ERROR_CODE_OK;
gxReplyData reply;
message msg;
reply_init(&reply);
mes_init(&msg);
if ((ret = Client.ReleaseRequest(true, &msg)) != 0 ||
(ret = com_readDataBlock(&msg, &reply)) != 0)
{
//Show error but continue close.
}
reply_clear(&reply);
mes_clear(&msg);
if ((ret = Client.DisconnectRequest(&msg)) != 0 ||
(ret = com_readDataBlock(&msg, &reply)) != 0)
{
//Show error but continue close.
}
reply_clear(&reply);
mes_clear(&msg);
return ret;
}
//Read Invocation counter (frame counter) from the meter and update it.
int com_updateInvocationCounter(const char* invocationCounter)
{
int ret = DLMS_ERROR_CODE_OK;
//Read frame counter if security is used.
if (invocationCounter != NULL && Client.GetSecurity() != DLMS_SECURITY_NONE)
{
uint32_t ic = 0;
message messages;
gxReplyData reply;
unsigned short add = Client.GetClientAddress();
DLMS_AUTHENTICATION auth = Client.GetAuthentication();
DLMS_SECURITY security = Client.GetSecurity();
Client.SetClientAddress(16);
Client.SetAuthentication(DLMS_AUTHENTICATION_NONE);
Client.SetSecurity(DLMS_SECURITY_NONE);
if ((ret = com_initializeConnection()) == DLMS_ERROR_CODE_OK)
{
gxData d;
cosem_init(BASE(d), DLMS_OBJECT_TYPE_DATA, invocationCounter);
if ((ret = com_read(BASE(d), 2)) == 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("Invocation Counter:"), var_toInteger(&d.value));
ic = 1 + var_toInteger(&d.value);
}
obj_clear(BASE(d));
}
//Close connection. It's OK if this fails.
com_close();
//Return original settings.
Client.SetClientAddress(add);
Client.SetAuthentication(auth);
Client.SetSecurity(security);
Client.SetInvocationCounter(ic);
}
return ret;
}
//Initialize connection to the meter.
int com_initializeConnection()
{
int ret = DLMS_ERROR_CODE_OK;
message messages;
gxReplyData reply;
mes_init(&messages);
reply_init(&reply);
#ifndef DLMS_IGNORE_HDLC
//Get meter's send and receive buffers size.
if ((ret = Client.SnrmRequest(&messages)) != 0 ||
(ret = com_readDataBlock(&messages, &reply)) != 0 ||
(ret = Client.ParseUAResponse(&reply.data)) != 0)
{
mes_clear(&messages);
reply_clear(&reply);
return ret;
}
mes_clear(&messages);
reply_clear(&reply);
#endif //DLMS_IGNORE_HDLC
if ((ret = Client.AarqRequest(&messages)) != 0 ||
(ret = com_readDataBlock(&messages, &reply)) != 0 ||
(ret = Client.ParseAAREResponse(&reply.data)) != 0)
{
mes_clear(&messages);
reply_clear(&reply);
if (ret == DLMS_ERROR_CODE_APPLICATION_CONTEXT_NAME_NOT_SUPPORTED)
{
return ret;
}
return ret;
}
mes_clear(&messages);
reply_clear(&reply);
// Get challenge Is HLS authentication is used.
if (Client.GetAuthentication() > DLMS_AUTHENTICATION_LOW)
{
if ((ret = Client.GetApplicationAssociationRequest(&messages)) != 0 ||
(ret = com_readDataBlock(&messages, &reply)) != 0 ||
(ret = Client.ParseApplicationAssociationResponse(&reply.data)) != 0)
{
mes_clear(&messages);
reply_clear(&reply);
return ret;
}
mes_clear(&messages);
reply_clear(&reply);
}
return DLMS_ERROR_CODE_OK;
}
//Get Association view.
int com_getAssociationView()
{
int ret;
message data;
gxReplyData reply;
mes_init(&data);
reply_init(&reply);
if ((ret = Client.GetObjectsRequest(&data)) != 0 ||
(ret = com_readDataBlock(&data, &reply)) != 0 ||
(ret = Client.ParseObjects(&reply.data)) != 0)
{
}
//Parse object one at the time. This can be used if there is a limited amount of the memory available.
/*
//Value is not parsed for decrease the memory usage.
reply.ignoreValue = 1;
if ((ret = Client.GetObjectsRequest(&data)) == 0 &&
(ret = com_readDataBlock(&data, &reply)) == 0)
{
uint16_t pos, count;
if ((ret = Client.ParseObjectCount(&reply.data, &count)) != 0)
{
GXTRACE_INT("cl_parseObjectCount failed", ret);
}
gxObject obj;
for (pos = 0; pos != count; ++pos)
{
memset(&obj, 0, sizeof(gxObject));
if ((ret = Client.ParseNextObject(&reply.data, &obj)) != 0)
{
break;
}
//Only data and register objects are added to the association view.
if (obj.objectType == DLMS_OBJECT_TYPE_DATA)
{
gxData* data = (gxData*) malloc(sizeof(gxData));
INIT_OBJECT((*data), (DLMS_OBJECT_TYPE) obj.objectType, obj.logicalName);
data->base.shortName = obj.shortName;
oa_push(Client.GetObjects(), BASE((*data)));
}
else if (obj.objectType == DLMS_OBJECT_TYPE_REGISTER)
{
gxRegister* r = (gxRegister*) malloc(sizeof(gxRegister));
INIT_OBJECT((*r), (DLMS_OBJECT_TYPE) obj.objectType, obj.logicalName);
r->base.shortName = obj.shortName;
oa_push(Client.GetObjects(), BASE((*r)));
}
}
}
*/
mes_clear(&data);
reply_clear(&reply);
return ret;
}
//Read object.
int com_read(
gxObject* object,
unsigned char attributeOrdinal)
{
int ret;
message data;
gxReplyData reply;
mes_init(&data);
reply_init(&reply);
if ((ret = Client.Read(object, attributeOrdinal, &data)) != 0 ||
(ret = com_readDataBlock(&data, &reply)) != 0 ||
(ret = Client.UpdateValue(object, attributeOrdinal, &reply.dataValue)) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("com_read failed."), ret);
}
mes_clear(&data);
reply_clear(&reply);
return ret;
}
int com_write(
gxObject* object,
unsigned char attributeOrdinal)
{
int ret;
message data;
gxReplyData reply;
mes_init(&data);
reply_init(&reply);
if ((ret = Client.Write(object, attributeOrdinal, &data)) != 0 ||
(ret = com_readDataBlock(&data, &reply)) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("com_write failed."), ret);
}
mes_clear(&data);
reply_clear(&reply);
return ret;
}
int com_method(
gxObject* object,
unsigned char attributeOrdinal,
dlmsVARIANT* params)
{
int ret;
message messages;
gxReplyData reply;
mes_init(&messages);
reply_init(&reply);
if ((ret = Client.Method(object, attributeOrdinal, params, &messages)) != 0 ||
(ret = com_readDataBlock(&messages, &reply)) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("com_method failed."), ret);
}
mes_clear(&messages);
reply_clear(&reply);
return ret;
}
//Read objects.
int com_readList(
gxArray* list)
{
int pos, ret = DLMS_ERROR_CODE_OK;
gxByteBuffer bb, rr;
message messages;
gxReplyData reply;
if (list->size != 0)
{
mes_init(&messages);
if ((ret = Client.ReadList(list, &messages)) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("com_readList failed."), ret);
}
else
{
reply_init(&reply);
BYTE_BUFFER_INIT(&rr);
BYTE_BUFFER_INIT(&bb);
//Send data.
for (pos = 0; pos != messages.size; ++pos)
{
//Send data.
reply_clear(&reply);
if ((ret = readDLMSPacket(messages.data[pos], &reply)) != DLMS_ERROR_CODE_OK)
{
break;
}
//Check is there errors or more data from server
while (reply_isMoreData(&reply))
{
if ((ret = Client.ReceiverReady(reply.moreData, &rr)) != DLMS_ERROR_CODE_OK ||
(ret = readDLMSPacket(&rr, &reply)) != DLMS_ERROR_CODE_OK)
{
break;
}
bb_clear(&rr);
}
bb_set2(&bb, &reply.data, reply.data.position, -1);
}
if (ret == 0)
{
ret = Client.UpdateValues(list, &bb);
}
bb_clear(&bb);
bb_clear(&rr);
reply_clear(&reply);
}
mes_clear(&messages);
}
return ret;
}
int com_readRowsByEntry(
gxProfileGeneric* object,
unsigned long index,
unsigned long count)
{
int ret;
message data;
gxReplyData reply;
mes_init(&data);
reply_init(&reply);
if ((ret = Client.ReadRowsByEntry(object, index, count, &data)) != 0 ||
(ret = com_readDataBlock(&data, &reply)) != 0 ||
(ret = Client.UpdateValue((gxObject*)object, 2, &reply.dataValue)) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("com_readRowsByEntry failed."), ret);
}
mes_clear(&data);
reply_clear(&reply);
return ret;
}
///////////////////////////////////////////////////////////////////////////////////
int com_readRowsByRange(
gxProfileGeneric* object,
gxtime* start,
gxtime* end)
{
int ret;
message data;
gxReplyData reply;
mes_init(&data);
reply_init(&reply);
if ((ret = Client.ReadRowsByRange(object, start, end, &data)) != 0 ||
(ret = com_readDataBlock(&data, &reply)) != 0 ||
(ret = Client.UpdateValue((gxObject*)object, 2, &reply.dataValue)) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("com_readRowsByRange failed."), ret);
}
mes_clear(&data);
reply_clear(&reply);
return ret;
}
///////////////////////////////////////////////////////////////////////////////////
//Read scalers and units. They are static so they are read only once.
int com_readScalerAndUnits()
{
gxObject* obj;
int ret, pos;
objectArray objects;
gxArray list;
gxObject* object;
DLMS_OBJECT_TYPE types[] = { DLMS_OBJECT_TYPE_EXTENDED_REGISTER, DLMS_OBJECT_TYPE_REGISTER, DLMS_OBJECT_TYPE_DEMAND_REGISTER };
oa_init(&objects);
//Find registers and demand registers and read them.
ret = oa_getObjects2(Client.GetObjects(), types, 3, &objects);
if (ret != DLMS_ERROR_CODE_OK)
{
return ret;
}
if ((Client.GetNegotiatedConformance() & DLMS_CONFORMANCE_MULTIPLE_REFERENCES) != 0)
{
arr_init(&list);
//Try to read with list first. All meters do not support it.
for (pos = 0; pos != Client.GetObjects()->size; ++pos)
{
ret = oa_getByIndex(Client.GetObjects(), pos, &obj);
if (ret != DLMS_ERROR_CODE_OK)
{
oa_empty(&objects);
arr_clear(&list);
return ret;
}
if (obj->objectType == DLMS_OBJECT_TYPE_REGISTER ||
obj->objectType == DLMS_OBJECT_TYPE_EXTENDED_REGISTER)
{
arr_push(&list, key_init(obj, (void*)3));
}
else if (obj->objectType == DLMS_OBJECT_TYPE_DEMAND_REGISTER)
{
arr_push(&list, key_init(obj, (void*)4));
}
}
ret = com_readList(&list);
arr_clear(&list);
}
//If read list failed read items one by one.
if (ret != 0)
{
for (pos = 0; pos != objects.size; ++pos)
{
ret = oa_getByIndex(&objects, pos, &object);
if (ret != DLMS_ERROR_CODE_OK)
{
oa_empty(&objects);
return ret;
}
ret = com_read(object, object->objectType == DLMS_OBJECT_TYPE_DEMAND_REGISTER ? 4 : 3);
if (ret != DLMS_ERROR_CODE_OK)
{
oa_empty(&objects);
return ret;
}
}
}
//Do not clear objects list because it will free also objects from association view list.
oa_empty(&objects);
return ret;
}
///////////////////////////////////////////////////////////////////////////////////
//Read profile generic columns. They are static so they are read only once.
int com_readProfileGenericColumns()
{
int ret, pos;
objectArray objects;
gxObject* object;
oa_init(&objects);
ret = oa_getObjects(Client.GetObjects(), DLMS_OBJECT_TYPE_PROFILE_GENERIC, &objects);
if (ret != DLMS_ERROR_CODE_OK)
{
oa_empty(&objects);
return ret;
}
for (pos = 0; pos != objects.size; ++pos)
{
ret = oa_getByIndex(&objects, pos, &object);
if (ret != DLMS_ERROR_CODE_OK)
{
break;
}
ret = com_read(object, 3);
if (ret != DLMS_ERROR_CODE_OK)
{
break;
}
}
//Do not clear objects list because it will free also objects from association view list.
oa_empty(&objects);
return ret;
}
///////////////////////////////////////////////////////////////////////////////////
//Read profile generics rows.
int com_readProfileGenerics()
{
gxtime startTime, endTime;
int ret, pos;
char str[50];
char ln[25];
char* data = NULL;
gxByteBuffer ba;
objectArray objects;
gxProfileGeneric* pg;
oa_init(&objects);
ret = oa_getObjects(Client.GetObjects(), DLMS_OBJECT_TYPE_PROFILE_GENERIC, &objects);
if (ret != DLMS_ERROR_CODE_OK)
{
//Do not clear objects list because it will free also objects from association view list.
oa_empty(&objects);
return ret;
}
BYTE_BUFFER_INIT(&ba);
for (pos = 0; pos != objects.size; ++pos)
{
ret = oa_getByIndex(&objects, pos, (gxObject**)&pg);
if (ret != DLMS_ERROR_CODE_OK)
{
//Do not clear objects list because it will free also objects from association view list.
oa_empty(&objects);
return ret;
}
//Read entries in use.
ret = com_read((gxObject*)pg, 7);
if (ret != DLMS_ERROR_CODE_OK)
{
printf("Failed to read object %s %s attribute index %d\r\n", str, ln, 7);
//Do not clear objects list because it will free also objects from association view list.
oa_empty(&objects);
return ret;
}
//Read entries.
ret = com_read((gxObject*)pg, 8);
if (ret != DLMS_ERROR_CODE_OK)
{
printf("Failed to read object %s %s attribute index %d\r\n", str, ln, 8);
//Do not clear objects list because it will free also objects from association view list.
oa_empty(&objects);
return ret;
}
printf("Entries: %ld/%ld\r\n", pg->entriesInUse, pg->profileEntries);
//If there are no columns or rows.
if (pg->entriesInUse == 0 || pg->captureObjects.size == 0)
{
continue;
}
//Read first row from Profile Generic.
ret = com_readRowsByEntry(pg, 1, 1);
//Read last day from Profile Generic.
time_now(&startTime);
endTime = startTime;
time_clearTime(&startTime);
ret = com_readRowsByRange(pg, &startTime, &endTime);
}
//Do not clear objects list because it will free also objects from association view list.
oa_empty(&objects);
return ret;
}
// This function reads ALL objects that meter have excluded profile generic objects.
// It will loop all object's attributes.
int com_readValues()
{
gxByteBuffer attributes;
unsigned char ch;
char* data = NULL;
gxObject* object;
unsigned long index;
int ret, pos;
BYTE_BUFFER_INIT(&attributes);
for (pos = 0; pos != Client.GetObjects()->size; ++pos)
{
ret = oa_getByIndex(Client.GetObjects(), pos, &object);
if (ret != DLMS_ERROR_CODE_OK)
{
bb_clear(&attributes);
return ret;
}
///////////////////////////////////////////////////////////////////////////////////
// Profile generics are read later because they are special cases.
// (There might be so lots of data and we so not want waste time to read all the data.)
if (object->objectType == DLMS_OBJECT_TYPE_PROFILE_GENERIC)
{
continue;
}
ret = obj_getAttributeIndexToRead(object, &attributes);
if (ret != DLMS_ERROR_CODE_OK)
{
bb_clear(&attributes);
return ret;
}
for (index = 0; index < attributes.size; ++index)
{
ret = bb_getUInt8ByIndex(&attributes, index, &ch);
if (ret != DLMS_ERROR_CODE_OK)
{
bb_clear(&attributes);
return ret;
}
ret = com_read(object, ch);
if (ret != DLMS_ERROR_CODE_OK)
{
//Return error if not DLMS error.
if (ret != DLMS_ERROR_CODE_READ_WRITE_DENIED)
{
bb_clear(&attributes);
return ret;
}
ret = 0;
}
}
bb_clear(&attributes);
}
bb_clear(&attributes);
return ret;
}
//This function reads ALL objects that meter have. It will loop all object's attributes.
int com_readAllObjects(const char* invocationCounter)
{
int ret;
if (invocationCounter != NULL)
{
if ((ret = com_updateInvocationCounter(invocationCounter)) != 0)
{
return ret;
}
}
//Initialize connection.
ret = com_initializeConnection();
if (ret != DLMS_ERROR_CODE_OK)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("com_initializeConnection failed"), ret);
return ret;
}
GXTRACE_INT(GET_STR_FROM_EEPROM("com_initializeConnection SUCCEEDED"), ret);
///////////////////////////////////////////////////////////////////////////////////
// Un-commnent this if association view is read and serialized.
// Note! Modify com_getAssociationView to returns only wanted objects.
///////////////////////////////////////////////////////////////////////////////////
/*
//Only current association view (attribute #2) is serialized.
gxObject* obj = NULL;
gxAssociationLogicalName ln;
gxAssociationShortName sn;
static unsigned char CURRENT_LN[] = { 0, 0, 40, 0, 0, 0xFF };
gxSerializerSettings serializerSettings;
ser_init(&serializerSettings);
gxSerializerIgnore NON_SERIALIZED_OBJECTS[] = { IGNORE_ATTRIBUTE_BY_TYPE(DLMS_OBJECT_TYPE_ASSOCIATION_LOGICAL_NAME, GET_ATTRIBUTE_EXCEPT(2)) };
serializerSettings.ignoredAttributes = NON_SERIALIZED_OBJECTS;
serializerSettings.count = sizeof(NON_SERIALIZED_OBJECTS) / sizeof(NON_SERIALIZED_OBJECTS[0]);
gxObject* CURRENT_ASSOCIATION[1];
if (Client.UseLogicalNameReferencing())
{
if ((ret = INIT_OBJECT(ln, DLMS_OBJECT_TYPE_ASSOCIATION_LOGICAL_NAME, CURRENT_LN)) != 0)
{
return ret;
}
CURRENT_ASSOCIATION[0] = BASE(ln);
}
else
{
if ((ret = INIT_OBJECT(sn, DLMS_OBJECT_TYPE_ASSOCIATION_SHORT_NAME, CURRENT_LN)) != 0)
{
return ret;
}
NON_SERIALIZED_OBJECTS[0].objectType = DLMS_OBJECT_TYPE_ASSOCIATION_SHORT_NAME;
CURRENT_ASSOCIATION[0] = BASE(sn);
}
if ((ret = Client.LoadObjects(&serializerSettings, CURRENT_ASSOCIATION, sizeof(CURRENT_ASSOCIATION) / sizeof(CURRENT_ASSOCIATION[0]))) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("LoadObjects FAILED:"), ret);
//Read the association view from the meter if reading from the EEPROM fails.
//Get objects from the meter and read them.
ret = com_getAssociationView();
if (ret != DLMS_ERROR_CODE_OK)
{
//If error code is 260 association view is too big and can't fit to memory.
GXTRACE_INT(GET_STR_FROM_EEPROM("Association View read FAILED:"), ret);
return ret;
}
ret = com_readScalerAndUnits();
if (ret != DLMS_ERROR_CODE_OK)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("Scaler and units read FAILED:"), ret);
return ret;
}
///////////////////////////////////////////////////////////////////////////////////
//Read Profile Generic columns.
ret = com_readProfileGenericColumns();
if (ret != DLMS_ERROR_CODE_OK)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("Profile Generic columns read FAILED:"), ret);
return ret;
}
///////////////////////////////////////////////////////////////////////////////////
//Only current association view is serialized.
if (Client.UseLogicalNameReferencing())
{
oa_copy(&ln.objectList, Client.GetObjects());
}
else
{
oa_copy(&sn.objectList, Client.GetObjects());
}
if ((ret = Client.SaveObjects(&serializerSettings, CURRENT_ASSOCIATION, sizeof(CURRENT_ASSOCIATION) / sizeof(CURRENT_ASSOCIATION[0]))) != 0)
{
GXTRACE_INT(GET_STR_FROM_EEPROM("SaveObjects FAILED:"), ret);
return ret;
}
if (Client.UseLogicalNameReferencing())
{
obj_clear(BASE(ln));
}
else
{
obj_clear(BASE(sn));
}
}
else
{
if (Client.UseLogicalNameReferencing())
{
oa_attach(Client.GetObjects(), ln.objectList.data, ln.objectList.size);
}
else
{
oa_attach(Client.GetObjects(), sn.objectList.data, sn.objectList.size);
}
GXTRACE_INT(GET_STR_FROM_EEPROM("LoadObjects Items loaded:"), Client.GetObjects()->size);
}
*/
//Read just wanted objects withour serializating the association view.
char* data = NULL;
//Read Logical Device Name
gxData ldn;
cosem_init(BASE(ldn), DLMS_OBJECT_TYPE_DATA, "0.0.42.0.0.255");
com_read(BASE(ldn), 2);
obj_toString(BASE(ldn), &data);
GXTRACE(GET_STR_FROM_EEPROM("Logical Device Name"), data);
obj_clear(BASE(ldn));
free(data);
//Read clock
gxClock clock1;
cosem_init(BASE(clock1), DLMS_OBJECT_TYPE_CLOCK, "0.0.1.0.0.255");
com_read(BASE(clock1), 3);
com_read(BASE(clock1), 2);
obj_toString(BASE(clock1), &data);
GXTRACE(GET_STR_FROM_EEPROM("Clock"), data);
obj_clear(BASE(clock1));
free(data);
/*
TODO: This is an example how to read profile generic.
//Read Profile generic.
gxProfileGeneric pg;
cosem_init(BASE(pg), DLMS_OBJECT_TYPE_PROFILE_GENERIC, "1.0.99.1.0.255");
com_read(BASE(pg), 3);
com_readRowsByEntry(&pg, 1, 2);
obj_toString(BASE(pg), &data);
GXTRACE(GET_STR_FROM_EEPROM("Load profile"), data);
obj_clear(BASE(pg));
free(data);
*/
//Release dynamically allocated objects.
Client.ReleaseObjects();
return ret;
}
void setup() {
GXTRACE(GET_STR_FROM_EEPROM("Start application"), NULL);
BYTE_BUFFER_INIT(&frameData);
//Set frame capacity.
bb_capacity(&frameData, 128);
Client.init(true, 16, 1, DLMS_AUTHENTICATION_NONE, NULL, DLMS_INTERFACE_TYPE_HDLC);
//Un-comment this if you want to set system title, block cipher key or authentication key.
/*
Client.SetSecurity(DLMS_SECURITY_AUTHENTICATION_ENCRYPTION);
//TODO: Change logical name of the frame counter if it's used to com_readAllObjects.
gxByteBuffer bb;
bb_init(&bb);
bb_addHexString(&bb, GET_STR_FROM_EEPROM("3132333435363738"));
Client.SetSystemTitle(&bb);
bb_clear(&bb);
bb_addHexString(&bb, GET_STR_FROM_EEPROM("D0 D1 D2 D3 D4 D5 D6 D7D8 D9 DA DB DC DD DE DF"));
Client.SetAuthenticationKey(&bb);
bb_clear(&bb);
bb_addHexString(&bb, GET_STR_FROM_EEPROM("00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"));
Client.SetBlockCipherKey(&bb);
bb_clear(&bb);
*/
//Serial 1 is used to send trace.
Serial1.begin(115200);
// start serial port at 9600 bps:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}
void loop() {
int ret;
if (millis() - runTime > 5000)
{
runTime = millis();