Merge "Merge branch 'master' into cloud-interface" into cloud-interface
[platform/upstream/iotivity.git] / resource / csdk / stack / include / payload_logging.h
1 //******************************************************************
2 //
3 // Copyright 2015 Intel Mobile Communications GmbH All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #ifndef PAYLOAD_LOGGING_H_
22 #define PAYLOAD_LOGGING_H_
23
24 #include "logger.h"
25 #ifdef __TIZEN__
26 #include <dlog.h>
27 #endif
28
29 #ifndef __STDC_FORMAT_MACROS
30 #define __STDC_FORMAT_MACROS
31 #endif
32 #ifndef __STDC_LIMIT_MACROS
33 #define __STDC_LIMIT_MACROS
34 #endif
35 #include <inttypes.h>
36 #include "rdpayload.h"
37
38 #ifdef __cplusplus
39 extern "C"
40 {
41 #endif
42
43 // PL_TAG is made as generic predefined tag because of build problems in arduino for using logging
44 #define PL_TAG "PayloadLog"
45
46 #ifdef TB_LOG
47     #define OIC_LOG_PAYLOAD(level, payload) OCPayloadLog((level),(payload))
48     #define UUID_SIZE (16)
49 const char *convertTriggerEnumToString(OCPresenceTrigger trigger);
50 OCPresenceTrigger convertTriggerStringToEnum(const char * triggerStr);
51
52 static inline void OCPayloadLogRep(LogLevel level, OCRepPayload* payload)
53 {
54     OIC_LOG(level, (PL_TAG), "Payload Type: Representation");
55     OCRepPayload* rep = payload;
56     int i = 1;
57     while(rep)
58     {
59         OIC_LOG_V(level, PL_TAG, "\tResource #%d", i);
60         OIC_LOG_V(level, PL_TAG, "\tURI:%s", rep->uri);
61         OIC_LOG(level, PL_TAG, "\tResource Types:");
62         OCStringLL* strll =  rep->types;
63         while(strll)
64         {
65             OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
66             strll = strll->next;
67         }
68         OIC_LOG(level, PL_TAG, "\tInterfaces:");
69         strll =  rep->interfaces;
70         while(strll)
71         {
72             OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
73             strll = strll->next;
74         }
75
76         // TODO Finish Logging: Values
77         OCRepPayloadValue* val = rep->values;
78
79         OIC_LOG(level, PL_TAG, "\tValues:");
80
81         while(val)
82         {
83             switch(val->type)
84             {
85                 case OCREP_PROP_NULL:
86                     OIC_LOG_V(level, PL_TAG, "\t\t%s: NULL", val->name);
87                     break;
88                 case OCREP_PROP_INT:
89                     OIC_LOG_V(level, PL_TAG, "\t\t%s(int):%zd", val->name, val->i);
90                     break;
91                 case OCREP_PROP_DOUBLE:
92                     OIC_LOG_V(level, PL_TAG, "\t\t%s(double):%f", val->name, val->d);
93                     break;
94                 case OCREP_PROP_BOOL:
95                     OIC_LOG_V(level, PL_TAG, "\t\t%s(bool):%s", val->name, val->b ? "true" : "false");
96                     break;
97                 case OCREP_PROP_STRING:
98                     OIC_LOG_V(level, PL_TAG, "\t\t%s(string):%s", val->name, val->str);
99                     break;
100                 case OCREP_PROP_BYTE_STRING:
101                     OIC_LOG_V(level, PL_TAG, "\t\t%s(binary):", val->name);
102                     OIC_LOG_BUFFER(level, PL_TAG, val->ocByteStr.bytes, val->ocByteStr.len);
103                     break;
104                 case OCREP_PROP_OBJECT:
105                     // Note: Only prints the URI (if available), to print further, you'll
106                     // need to dig into the object better!
107                     OIC_LOG_V(level, PL_TAG, "\t\t%s(OCRep):%s", val->name, val->obj->uri);
108                     break;
109                 case OCREP_PROP_ARRAY:
110                     switch(val->arr.type)
111                     {
112                         case OCREP_PROP_INT:
113                             OIC_LOG_V(level, PL_TAG, "\t\t%s(int array):%zu x %zu x %zu",
114                                     val->name,
115                                     val->arr.dimensions[0], val->arr.dimensions[1],
116                                     val->arr.dimensions[2]);
117                             break;
118                         case OCREP_PROP_DOUBLE:
119                             OIC_LOG_V(level, PL_TAG, "\t\t%s(double array):%zu x %zu x %zu",
120                                     val->name,
121                                     val->arr.dimensions[0], val->arr.dimensions[1],
122                                     val->arr.dimensions[2]);
123                             break;
124                         case OCREP_PROP_BOOL:
125                             OIC_LOG_V(level, PL_TAG, "\t\t%s(bool array):%zu x %zu x %zu",
126                                     val->name,
127                                     val->arr.dimensions[0], val->arr.dimensions[1],
128                                     val->arr.dimensions[2]);
129                             break;
130                         case OCREP_PROP_STRING:
131                             OIC_LOG_V(level, PL_TAG, "\t\t%s(string array):%zu x %zu x %zu",
132                                     val->name,
133                                     val->arr.dimensions[0], val->arr.dimensions[1],
134                                     val->arr.dimensions[2]);
135                             break;
136                         case OCREP_PROP_BYTE_STRING:
137                             OIC_LOG_V(level, PL_TAG, "\t\t%s(byte array):%zu x %zu x %zu",
138                                     val->name,
139                                     val->arr.dimensions[0], val->arr.dimensions[1],
140                                     val->arr.dimensions[2]);
141                             break;
142                         case OCREP_PROP_OBJECT:
143                             OIC_LOG_V(level, PL_TAG, "\t\t%s(OCRep array):%zu x %zu x %zu",
144                                     val->name,
145                                     val->arr.dimensions[0], val->arr.dimensions[1],
146                                     val->arr.dimensions[2]);
147                             break;
148                         default:
149                             OIC_LOG_V(ERROR, PL_TAG, "\t\t%s <-- Unknown/unsupported array type!",
150                                     val->name);
151                             break;
152                     }
153                     break;
154                 default:
155                     OIC_LOG_V(ERROR, PL_TAG, "\t\t%s <-- Unknown type!", val->name);
156                     break;
157             }
158             val = val -> next;
159         }
160
161         ++i;
162         rep = rep->next;
163     }
164
165 }
166
167 static inline void OCPayloadLogDiscovery(LogLevel level, OCDiscoveryPayload* payload)
168 {
169     OIC_LOG(level, PL_TAG, "Payload Type: Discovery");
170     int i = 1;
171
172     if(!payload->resources)
173     {
174         OIC_LOG(level, PL_TAG, "\tNO Resources");
175         return;
176     }
177     OIC_LOG(level, PL_TAG, "\tSID:");
178     OIC_LOG_BUFFER(level, PL_TAG, payload->sid, UUID_SIZE);
179     OCResourcePayload* res = payload->resources;
180
181     while(res)
182     {
183         OIC_LOG_V(level, PL_TAG, "\tResource #%d", i);
184         OIC_LOG_V(level, PL_TAG, "\tURI:%s", res->uri);
185         OIC_LOG(level, PL_TAG, "\tResource Types:");
186         OCStringLL* strll =  res->types;
187         while(strll)
188         {
189             OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
190             strll = strll->next;
191         }
192         OIC_LOG(level, PL_TAG, "\tInterfaces:");
193         strll =  res->interfaces;
194         while(strll)
195         {
196             OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
197             strll = strll->next;
198         }
199
200         OIC_LOG_V(level, PL_TAG, "\tBitmap: %u", res->bitmap);
201         OIC_LOG_V(level, PL_TAG, "\tSecure?: %s", res->secure ? "true" : "false");
202         OIC_LOG_V(level, PL_TAG, "\tPort: %u", res->port);
203         OIC_LOG(level, PL_TAG, "");
204         res = res->next;
205         ++i;
206     }
207 }
208
209 static inline void OCPayloadLogDevice(LogLevel level, OCDevicePayload* payload)
210 {
211     OIC_LOG(level, PL_TAG, "Payload Type: Device");
212     OIC_LOG(level, PL_TAG, "\tSID:");
213     OIC_LOG_BUFFER(level, PL_TAG, payload->sid, UUID_SIZE);
214     OIC_LOG_V(level, PL_TAG, "\tDevice Name:%s", payload->deviceName);
215     OIC_LOG_V(level, PL_TAG, "\tSpec Version%s", payload->specVersion);
216     OIC_LOG_V(level, PL_TAG, "\tData Model Version:%s", payload->dataModelVersion);
217 }
218
219 static inline void OCPayloadLogPlatform(LogLevel level, OCPlatformPayload* payload)
220 {
221     OIC_LOG(level, PL_TAG, "Payload Type: Platform");
222     OIC_LOG_V(level, PL_TAG, "\tURI:%s", payload->uri);
223     OIC_LOG_V(level, PL_TAG, "\tPlatform ID:%s", payload->info.platformID);
224     OIC_LOG_V(level, PL_TAG, "\tMfg Name:%s", payload->info.manufacturerName);
225     OIC_LOG_V(level, PL_TAG, "\tMfg URL:%s", payload->info.manufacturerUrl);
226     OIC_LOG_V(level, PL_TAG, "\tModel Number:%s", payload->info.modelNumber);
227     OIC_LOG_V(level, PL_TAG, "\tDate of Mfg:%s", payload->info.dateOfManufacture);
228     OIC_LOG_V(level, PL_TAG, "\tPlatform Version:%s", payload->info.platformVersion);
229     OIC_LOG_V(level, PL_TAG, "\tOS Version:%s", payload->info.operatingSystemVersion);
230     OIC_LOG_V(level, PL_TAG, "\tHardware Version:%s", payload->info.hardwareVersion);
231     OIC_LOG_V(level, PL_TAG, "\tFirmware Version:%s", payload->info.firmwareVersion);
232     OIC_LOG_V(level, PL_TAG, "\tSupport URL:%s", payload->info.supportUrl);
233     OIC_LOG_V(level, PL_TAG, "\tSystem Time:%s", payload->info.systemTime);
234 }
235
236 static inline void OCPayloadLogPresence(LogLevel level, OCPresencePayload* payload)
237 {
238     OIC_LOG(level, PL_TAG, "Payload Type: Presence");
239     OIC_LOG_V(level, PL_TAG, "\tSequence Number:%u", payload->sequenceNumber);
240     OIC_LOG_V(level, PL_TAG, "\tMax Age:%d", payload->maxAge);
241     OIC_LOG_V(level, PL_TAG, "\tTrigger:%s", convertTriggerEnumToString(payload->trigger));
242     OIC_LOG_V(level, PL_TAG, "\tResource Type:%s", payload->resourceType);
243 }
244
245 static inline void OCPayloadLogSecurity(LogLevel level, OCSecurityPayload* payload)
246 {
247     OIC_LOG(level, PL_TAG, "Payload Type: Security");
248     OIC_LOG_V(level, PL_TAG, "\tSecurity Data: %s", payload->securityData);
249 }
250
251 static inline void OCRDPayloadLog(const LogLevel level, const OCRDPayload *payload)
252 {
253     if (!payload)
254     {
255         return;
256     }
257
258     if (payload->rdDiscovery)
259     {
260         OIC_LOG(level, PL_TAG, "RD Discovery");
261         OIC_LOG_V(level, PL_TAG, "  Device Name : %s", payload->rdDiscovery->n.deviceName);
262         OIC_LOG_V(level, PL_TAG, "  Device Identity : %s", payload->rdDiscovery->di.id);
263         OIC_LOG_V(level, PL_TAG, "  Bias: %d", payload->rdDiscovery->sel);
264     }
265     if (payload->rdPublish)
266     {
267         OIC_LOG(level, PL_TAG, "RD Publish");
268         OCResourceCollectionPayload *rdPublish = payload->rdPublish;
269         OCTagsLog(level, rdPublish->tags);
270         OCLinksLog(level, rdPublish->setLinks);
271     }
272 }
273
274 static inline void OCPayloadLog(LogLevel level, OCPayload* payload)
275 {
276     if(!payload)
277     {
278         OIC_LOG(level, PL_TAG, "NULL Payload");
279         return;
280     }
281     switch(payload->type)
282     {
283         case PAYLOAD_TYPE_REPRESENTATION:
284             OCPayloadLogRep(level, (OCRepPayload*)payload);
285             break;
286         case PAYLOAD_TYPE_DISCOVERY:
287             OCPayloadLogDiscovery(level, (OCDiscoveryPayload*)payload);
288             break;
289         case PAYLOAD_TYPE_DEVICE:
290             OCPayloadLogDevice(level, (OCDevicePayload*)payload);
291             break;
292         case PAYLOAD_TYPE_PLATFORM:
293             OCPayloadLogPlatform(level, (OCPlatformPayload*)payload);
294             break;
295         case PAYLOAD_TYPE_PRESENCE:
296             OCPayloadLogPresence(level, (OCPresencePayload*)payload);
297             break;
298         case PAYLOAD_TYPE_SECURITY:
299             OCPayloadLogSecurity(level, (OCSecurityPayload*)payload);
300             break;
301         case PAYLOAD_TYPE_RD:
302             OCRDPayloadLog(level, (OCRDPayload*)payload);
303             break;
304         default:
305             OIC_LOG_V(level, PL_TAG, "Unknown Payload Type: %d", payload->type);
306             break;
307     }
308 }
309 #else
310     #define OIC_LOG_PAYLOAD(level, payload)
311 #endif
312
313 #ifdef __cplusplus
314 }
315 #endif
316
317 #endif