QtFit  0.1
Internal library development documentation
All Classes Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
abstractdatamessage.cpp
Go to the documentation of this file.
1 /*
2  Copyright 2021 Paul Colby
3 
4  This file is part of QtFit.
5 
6  QtFit is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published by
8  the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  QtFit is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with QtFit. If not, see <http://www.gnu.org/licenses/>.
18 */
19 
20 /*!
21  * \file
22  * Defines the AbstractDataMessage, and AbstractDataMessagePrivate classes.
23  */
24 
25 #include "abstractdatamessage.h"
26 #include "abstractdatamessage_p.h"
27 
28 #include <QDebug>
29 
31 
32 /*!
33  * \class AbstractDataMessage
34  *
35  * The AbstractDataMessage class is the polymorphic base class for all FIT Data Message classes.
36  */
37 
38 /*!
39  * \internal
40  *
41  * Constructs a AbstractDataMessage with private implementation \a d.
42  *
43  * \param d Pointer to private implementation.
44  */
45 AbstractDataMessage::AbstractDataMessage(AbstractDataMessagePrivate * const d) : d_ptr(d)
46 {
47 
48 }
49 
50 /*!
51  * Destroys the AbstractDataMessage object.
52  */
54 {
55  delete d_ptr;
56 }
57 
58 /*!
59  * Returns the data message's global message number.
60  *
61  * \return the global message number.
62  */
64 {
65  Q_D(const AbstractDataMessage);
66  return d->globalMessageNumber;
67 }
68 
69 /// \cond internal
70 
71 /*!
72  * \internal
73  *
74  * \class AbstractDataMessagePrivate
75  *
76  * The AbstractDataMessagePrivate class provides private implementation for AbstractDataMessage.
77  *
78  * \sa AbstractDataMessage
79  */
80 
81 /*!
82  * \internal
83  *
84  * Constructs a AbstractDataMessagePrivate object with public implementation \a q.
85  *
86  * \param q Pointer to public implementation.
87  */
88 AbstractDataMessagePrivate::AbstractDataMessagePrivate(AbstractDataMessage * const q)
89  : globalMessageNumber(static_cast<MesgNum>(0xFFFF)), q_ptr(q)
90 {
91 
92 }
93 
94 /*!
95  * \internal
96  *
97  * Destroys the AbstractDataMessagePrivate object.
98  */
100 {
101 
102 }
103 
104 /*!
105  * \internal
106  *
107  * Sets the current FIT data message's fields.
108  *
109  * The method iterates through the \a record, invoking the virtual setField method to assign the
110  * extraced values to the relevant data message fields. Derived classes are expected to override
111  * setField to implement that field number to class member mapping.
112  *
113  * \param defn Data field definition describing the \a record layout.
114  * \param record The FIT data record to read fields from.
115  *
116  * \return \c true if all fields were parsed safely.
117  *
118  * \sa setField
119  */
120 bool AbstractDataMessagePrivate::setFields(const DataDefinition * const defn, const QByteArray &record)
121 {
122  Q_ASSERT(defn->globalMessageNumber == this->globalMessageNumber);
123  int dataOffset=0; // Next field's offset within dataRecord.
124  for (const FieldDefinition &field: defn->fieldDefinitions) {
125  if (!setField(field.number, record.mid(dataOffset,field.size), field.baseType,
127  return false;
128  dataOffset += field.size;
129  }
130  return true;
131 }
132 
133 /*!
134  * \internal
135  * \fn AbstractDataMessagePrivate::setField
136  *
137  * Sets the value of the \a fieldId field.
138  *
139  * Derived classes must implement this method to extract the \a baseType value from \a data, and
140  * assign the extracted value the \a fieldId field.
141  *
142  * \param fieldId The field number within the given FIT data message.
143  * \param data The raw data to extract the field value from.
144  * \param baseType The FIT base type for the field.
145  * \param bigEndian Whether or not multibyte values in \a record are big-endian.
146  *
147  * \return \c true if the field was set, or safely ignored; \c false otherwise.
148  */
149 
150 /*!
151  * \internal
152  *
153  * Verifies that \a actual matches \a expected, and reports a warning if not.
154  *
155  * \param actual Actual base type.
156  * \param expected Expected base type.
157  * \param name Name of the field being verfied.
158  *
159  * \return \c true if \a actual and \a expected match, \c false otherwise.
160  */
161 inline bool verifyBaseType(const FitBaseType actual, const FitBaseType expected, const char *name)
162 {
163  if (actual == expected) return true;
164  qWarning() << name << "has base type" << actual << "but should be" << expected;
165  return false;
166 }
167 
168 /*!
169  * \internal
170  *
171  * Verifies that the size of \a data matches \a expectedSize, and reports a warning if not.
172  *
173  * \param data Data to verify the size of.
174  * \param expectedSize Exepcted size of \a data.
175  * \param name Name of the field being verified.
176  *
177  * \return \c true if \a data has \a expectedSize bytes, \c false otherwise.
178  */
179 inline bool verifyDataSize(const QByteArray &data, const int expectedSize, const char *name)
180 {
181  if (data.size() == expectedSize) return true;
182  qWarning() << name << "size is" << data.size() << "but should be" << expectedSize;
183  return false;
184 }
185 
186 /*!
187  * \internal
188  *
189  * Verifies the size and type of FIT Data Message fields.
190  *
191  * \param data Data to verify the size of.
192  * \param actualType Actual base type.
193  * \param expectedSize Expected size of \a data.
194  * \param expectedType Expected base type.
195  * \param messageFieldName Name of the field being verified.
196  *
197  * \return \c true if the size and type match, \c false otherwise.
198  */
199 bool AbstractDataMessagePrivate::verify(const QByteArray &data, const FitBaseType actualType,
200  const int expectedSize, const FitBaseType expectedType,
201  const char *messageFieldName)
202 {
203  return (verifyBaseType(actualType, expectedType, messageFieldName) &&
204  verifyDataSize(data, expectedSize, messageFieldName));
205 }
206 
207 /// \endcond
208 
#define QTFIT_END_NAMESPACE
Macro for ending the QtFit library's top-most namespace (if one is defined).
Definition: QtFit_global.h:78
#define QTFIT_BEGIN_NAMESPACE
Macro for starting the QtFit library's top-most namespace (if one is defined).
Definition: QtFit_global.h:77
bool verifyDataSize(const QByteArray &data, const int expectedSize, const char *name)
bool verifyBaseType(const FitBaseType actual, const FitBaseType expected, const char *name)
Declares the AbstractDataMessage class.
Declares the AbstractDataMessagePrivate class.
bool verify(const QByteArray &data, const FitBaseType actualType, const int expectedSize, const FitBaseType expectedType, const char *messageFieldName)
bool setFields(const DataDefinition *const defn, const QByteArray &record)
virtual bool setField(const int fieldId, const QByteArray &data, const FitBaseType baseType, const bool bigEndian)=0
The AbstractDataMessage class is the polymorphic base class for all FIT Data Message classes.
AbstractDataMessagePrivate *const d_ptr
Internal d-pointer.
~AbstractDataMessage()
Destroys the AbstractDataMessage object.
MesgNum globalMessageNumber() const
Returns the data message's global message number.
Data Message definition.
Definition: types_p.h:83
QList< FieldDefinition > fieldDefinitions
Definitons list of all fields, if any, present in the described Data Message.
Definition: types_p.h:88
Architecture architecture
Architecture type for any multi-byte fields.
Definition: types_p.h:84
MesgNum globalMessageNumber
FIT Global Message Number the Data Message represents.
Definition: types_p.h:85
Field Definition for FIT Data Messages.
Definition: types_p.h:69
FitBaseType baseType
Base type for interpreting unknown fields.
Definition: types_p.h:72
quint8 number
Unique ID for the FIT field within a given FIT data message.
Definition: types_p.h:70
quint8 size
Size (in bytes) of the field.
Definition: types_p.h:71
FitBaseType
Garmin FIT FitBaseType type.
Definition: types.h:3388
MesgNum
Garmin FIT MesgNum type.
Definition: types.h:91
@ BigEndian
Little-endian byte ordering.