Merge branch '1.3-rel' (5fdb8a1)
[iotivity.git] / resource / csdk / stack / include / experimental / 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 #ifdef __TIZEN__
25 #include <dlog.h>
26 #endif
27
28 #ifndef __STDC_FORMAT_MACROS
29 #define __STDC_FORMAT_MACROS
30 #endif
31 #ifndef __STDC_LIMIT_MACROS
32 #define __STDC_LIMIT_MACROS
33 #endif
34
35 #include "experimental/logger.h"
36 #include "oic_malloc.h"
37 #include "ocpayload.h"
38 #include "ocstack.h"
39
40 #ifdef __cplusplus
41 extern "C"
42 {
43 #endif
44
45 // PL_TAG is made as generic predefined tag because of build problems in arduino for using logging
46 #define PL_TAG "PayloadLog"
47
48 #ifdef TB_LOG
49     #define OIC_LOG_PAYLOAD(level, payload) OCPayloadLog((level),(payload))
50     #define UUID_SIZE (16)
51
52 const char *OC_CALL convertTriggerEnumToString(OCPresenceTrigger trigger);
53 OCPresenceTrigger OC_CALL convertTriggerStringToEnum(const char * triggerStr);
54
55 INLINE_API void OCPayloadLogRep(LogLevel level, OCRepPayload* payload);
56
57 INLINE_API void OCPayloadLogRepValues(LogLevel level, OCRepPayloadValue* val)
58 {
59     while (val)
60     {
61         switch(val->type)
62         {
63             case OCREP_PROP_NULL:
64                 OIC_LOG_V(level, PL_TAG, "\t\t%s: NULL", val->name);
65                 break;
66             case OCREP_PROP_INT:
67                 OIC_LOG_V(level, PL_TAG, "\t\t%s(int):%" PRId64, val->name, val->i);
68                 break;
69             case OCREP_PROP_DOUBLE:
70                 OIC_LOG_V(level, PL_TAG, "\t\t%s(double):%f", val->name, val->d);
71                 break;
72             case OCREP_PROP_BOOL:
73                 OIC_LOG_V(level, PL_TAG, "\t\t%s(bool):%s", val->name, val->b ? "true" : "false");
74                 break;
75             case OCREP_PROP_STRING:
76                 OIC_LOG_V(level, PL_TAG, "\t\t%s(string):%s", val->name, val->str);
77                 break;
78             case OCREP_PROP_BYTE_STRING:
79                 OIC_LOG_V(level, PL_TAG, "\t\t%s(binary):", val->name);
80                 OIC_LOG_BUFFER(level, PL_TAG, val->ocByteStr.bytes, val->ocByteStr.len);
81                 break;
82             case OCREP_PROP_OBJECT:
83                 // Note: Only prints the URI (if available), to print further, you'll
84                 // need to dig into the object better!
85                 OIC_LOG_V(level, PL_TAG, "\t\t%s(object):", val->name);
86                 OCPayloadLogRep(level, val->obj);
87                 break;
88             case OCREP_PROP_ARRAY:
89                 switch(val->arr.type)
90                 {
91                     case OCREP_PROP_INT:
92                         OIC_LOG_V(level, PL_TAG, "\t\t%s(int array):%" PRIuPTR " x %" PRIuPTR " x %" PRIuPTR ": ",
93                                 val->name,
94                                 val->arr.dimensions[0], val->arr.dimensions[1],
95                                 val->arr.dimensions[2]);
96                         OIC_LOG(level, PL_TAG, "\t\t Values:");
97                         for (size_t i = 0; i < val->arr.dimensions[0]; i++)
98                         {
99                             OIC_LOG_V(level, PL_TAG, "\t\t\t %" PRId64, val->arr.iArray[i]);
100                         }
101                         break;
102                     case OCREP_PROP_DOUBLE:
103                         OIC_LOG_V(level, PL_TAG, "\t\t%s(double array):%" PRIuPTR " x %" PRIuPTR " x %" PRIuPTR ": ",
104                                 val->name,
105                                 val->arr.dimensions[0], val->arr.dimensions[1],
106                                 val->arr.dimensions[2]);
107                         OIC_LOG(level, PL_TAG, "\t\t Values:");
108                         for (size_t i = 0; i < val->arr.dimensions[0]; i++)
109                         {
110                             OIC_LOG_V(level, PL_TAG, "\t\t\t %lf", val->arr.dArray[i]);
111                         }
112                         break;
113                     case OCREP_PROP_BOOL:
114                         OIC_LOG_V(level, PL_TAG, "\t\t%s(bool array):%" PRIuPTR " x %" PRIuPTR " x %" PRIuPTR ": ",
115                                 val->name,
116                                 val->arr.dimensions[0], val->arr.dimensions[1],
117                                 val->arr.dimensions[2]);
118                         OIC_LOG(level, PL_TAG, "\t\t Values:");
119                         for (size_t i = 0; i < val->arr.dimensions[0]; i++)
120                         {
121                             OIC_LOG_V(level, PL_TAG, "\t\t\t %d", val->arr.bArray[i]);
122                         }
123                         break;
124                     case OCREP_PROP_STRING:
125                         OIC_LOG_V(level, PL_TAG, "\t\t%s(string array):%" PRIuPTR " x %" PRIuPTR " x %" PRIuPTR ": ",
126                                 val->name,
127                                 val->arr.dimensions[0], val->arr.dimensions[1],
128                                 val->arr.dimensions[2]);
129                         OIC_LOG(level, PL_TAG, "\t\t Values:");
130                         for (size_t i = 0; i < val->arr.dimensions[0]; i++)
131                         {
132                             OIC_LOG_V(level, PL_TAG, "\t\t\t %s", val->arr.strArray[i]);
133                         }
134                         break;
135                     case OCREP_PROP_BYTE_STRING:
136                         OIC_LOG_V(level, PL_TAG, "\t\t%s(byte array):%" PRIuPTR " x %" PRIuPTR " x %" PRIuPTR ": ",
137                                 val->name,
138                                 val->arr.dimensions[0], val->arr.dimensions[1],
139                                 val->arr.dimensions[2]);
140                         OIC_LOG(level, PL_TAG, "\t\t Values:");
141                         for (size_t i = 0; i < val->arr.dimensions[0]; i++)
142                         {
143                             OIC_LOG_BUFFER(level, PL_TAG, val->arr.ocByteStrArray[i].bytes, val->arr.ocByteStrArray[i].len);
144                         }
145                         break;
146                     case OCREP_PROP_OBJECT:
147                         OIC_LOG_V(level, PL_TAG, "\t\t%s(object array):%" PRIuPTR " x %" PRIuPTR " x %" PRIuPTR ": ",
148                                 val->name,
149                                 val->arr.dimensions[0], val->arr.dimensions[1],
150                                 val->arr.dimensions[2]);
151                         OIC_LOG(level, PL_TAG, "\t\t Values:");
152
153                         for (size_t i = 0; i < val->arr.dimensions[0]; i++)
154                         {
155                             OCPayloadLogRep(level, val->arr.objArray[i]);
156                         }
157                         break;
158                     case OCREP_PROP_ARRAY: //Seems as nested arrays doesn't not supported in API
159                     default:
160                         OIC_LOG_V(ERROR, PL_TAG, "%s <-- Unknown/unsupported array type!",
161                                 val->name);
162                         break;
163                 }
164                 break;
165             default:
166                 OIC_LOG_V(ERROR, PL_TAG, "%s <-- Unknown type!", val->name);
167                 break;
168         }
169         val = val -> next;
170     }
171 }
172
173 INLINE_API void OCPayloadLogRep(LogLevel level, OCRepPayload* payload)
174 {
175     OIC_LOG(level, (PL_TAG), "Payload Type: Representation");
176     uint32_t i = 1;
177     for (OCRepPayload* rep = payload; rep; rep = rep->next, ++i)
178     {
179         OIC_LOG_V(level, PL_TAG, "\tResource #%d", i);
180         if (rep->uri)
181         {
182             OIC_LOG_V(level, PL_TAG, "\tURI:%s", rep->uri);
183         }
184         if (rep->types)
185         {
186             OIC_LOG(level, PL_TAG, "\tResource Types:");
187             for (OCStringLL* strll = rep->types; strll; strll = strll->next)
188             {
189                 OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
190             }
191         }
192         if (rep->interfaces)
193         {
194             OIC_LOG(level, PL_TAG, "\tInterfaces:");
195             for (OCStringLL* strll = rep->interfaces; strll; strll = strll->next)
196             {
197                 OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
198             }
199         }
200         OIC_LOG(level, PL_TAG, "\tValues:");
201         OCPayloadLogRepValues(level, rep->values);
202     }
203 }
204
205 static void OCStringLLPrint(LogLevel level, OCStringLL *type)
206 {
207     for (OCStringLL *strll = type; strll; strll = strll->next)
208     {
209         OIC_LOG_V(level, PL_TAG, "\t\t %s", strll->value);
210     }
211 }
212
213 INLINE_API void OCPayloadLogDiscovery(LogLevel level, OCDiscoveryPayload* payload)
214 {
215     OIC_LOG(level, PL_TAG, "Payload Type: Discovery");
216
217     while(payload && payload->resources)
218     {
219         OIC_LOG_V(level, PL_TAG, "\tDI: %s", payload->sid);
220         if (payload->name)
221         {
222             OIC_LOG_V(level, PL_TAG, "\tNAME: %s", payload->name);
223         }
224
225         if (payload->type)
226         {
227             OIC_LOG(level, PL_TAG, "\tResource Type:");
228             OCStringLLPrint(level, payload->type);
229         }
230
231         if (payload->iface)
232         {
233             OIC_LOG(level, PL_TAG, "\tInterface:");
234             OCStringLLPrint(level, payload->iface);
235         }
236
237         OCResourcePayload* res = payload->resources;
238
239         uint32_t i = 1;
240         while(res)
241         {
242             OIC_LOG_V(level, PL_TAG, "\tLink#%d", i);
243             OIC_LOG_V(level, PL_TAG, "\tURI:%s", res->uri);
244             if (res->rel)
245             {
246                 OIC_LOG_V(level, PL_TAG, "\tRelation:%s", res->rel);
247             }
248             if (res->anchor)
249             {
250                 OIC_LOG_V(level, PL_TAG, "\tAnchor:%s", res->anchor);
251             }
252             OIC_LOG(level, PL_TAG, "\tResource Types:");
253             OCStringLL* strll =  res->types;
254             while(strll)
255             {
256                 OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
257                 strll = strll->next;
258             }
259             OIC_LOG(level, PL_TAG, "\tInterfaces:");
260             strll =  res->interfaces;
261             while(strll)
262             {
263                 OIC_LOG_V(level, PL_TAG, "\t\t%s", strll->value);
264                 strll = strll->next;
265             }
266
267             OIC_LOG_V(level, PL_TAG, "\tBitmap: %u", res->bitmap);
268             OIC_LOG_V(level, PL_TAG, "\tSecure?: %s", res->secure ? "true" : "false");
269             OIC_LOG_V(level, PL_TAG, "\tPort: %u", res->port);
270
271             uint32_t j = 1;
272             OCEndpointPayload* eps = res->eps;
273             while (eps)
274             {
275                 OIC_LOG_V(level, PL_TAG, "\tEndpoint #%d", j);
276                 OIC_LOG_V(level, PL_TAG, "\t\ttps: %s", eps->tps);
277                 OIC_LOG_V(level, PL_TAG, "\t\taddr: %s", eps->addr);
278                 OIC_LOG_V(level, PL_TAG, "\t\tport: %d", eps->port);
279                 OIC_LOG_V(level, PL_TAG, "\t\tpri: %d", eps->pri);
280                 eps = eps->next;
281                 ++j;
282             }
283
284             OIC_LOG(level, PL_TAG, "");
285             res = res->next;
286             ++i;
287         }
288         payload = payload->next;
289     }
290 }
291
292 INLINE_API void OCPayloadLogPresence(LogLevel level, OCPresencePayload* payload)
293 {
294     OIC_LOG(level, PL_TAG, "Payload Type: Presence");
295     OIC_LOG_V(level, PL_TAG, "\tSequence Number:%u", payload->sequenceNumber);
296     OIC_LOG_V(level, PL_TAG, "\tMax Age:%d", payload->maxAge);
297     OIC_LOG_V(level, PL_TAG, "\tTrigger:%s", convertTriggerEnumToString(payload->trigger));
298     OIC_LOG_V(level, PL_TAG, "\tResource Type:%s", payload->resourceType);
299 }
300
301 INLINE_API void OCPayloadLogSecurity(LogLevel level, OCSecurityPayload* payload)
302 {
303     size_t payloadSize = payload->payloadSize;
304     OIC_LOG(level, PL_TAG, "Payload Type: Security");
305
306     if (payloadSize > 0)
307     {
308         // Add a zero-character string terminator.
309         char *securityData = (char *)OICMalloc(payloadSize + 1);
310
311         if (securityData)
312         {
313             memcpy(securityData, payload->securityData, payloadSize);
314             // assert(securityData[payloadSize - 1] != '\0');
315             securityData[payloadSize] = '\0';
316             OIC_LOG_V(level, PL_TAG, "\tSecurity Data: %s", securityData);
317             OICFree(securityData);
318         }
319     }
320 }
321
322 INLINE_API void OCPayloadLog(LogLevel level, OCPayload* payload)
323 {
324     if(!payload)
325     {
326         OIC_LOG(level, PL_TAG, "NULL Payload");
327         return;
328     }
329     switch(payload->type)
330     {
331         case PAYLOAD_TYPE_REPRESENTATION:
332             OCPayloadLogRep(level, (OCRepPayload*)payload);
333             break;
334         case PAYLOAD_TYPE_DISCOVERY:
335             OCPayloadLogDiscovery(level, (OCDiscoveryPayload*)payload);
336             break;
337         case PAYLOAD_TYPE_PRESENCE:
338             OCPayloadLogPresence(level, (OCPresencePayload*)payload);
339             break;
340         case PAYLOAD_TYPE_SECURITY:
341             OCPayloadLogSecurity(level, (OCSecurityPayload*)payload);
342             break;
343         default:
344             OIC_LOG_V(level, PL_TAG, "Unknown Payload Type: %d", payload->type);
345             break;
346     }
347 }
348 #else
349     #define OIC_LOG_PAYLOAD(level, payload)
350 #endif
351
352 #ifdef __cplusplus
353 }
354 #endif
355
356 #endif