AppDynamics IoT C++ SDK
AppDynamics IoT C++ library contains code that facilitates capturing availability, usage, network performance and errors of an IoT Application.
beacon.cpp File Reference
#include <string.h>
#include <stdlib.h>
#include "beacon.hpp"
#include "log.hpp"
#include "json_serializer.hpp"
#include "config.hpp"
#include "utils.hpp"

Macros

#define APPD_IOT_SDK_VERSION   "4.4.1.0"
 

Functions

static std::string appd_iot_serialize_beacon_to_json (beacon_t beacon)
 Serializes Beacon Data into JSON Format. More...
 
appd_iot_error_code_t appd_iot_init_device_config (appd_iot_device_config_t devcfg)
 Initializes Device Configuration
It is madatory to set Device ID and Device Type. More...
 
appd_iot_error_code_t appd_iot_add_custom_event_to_beacon (custom_event_t event)
 Adds Custom Event to Beacon. More...
 
appd_iot_error_code_t appd_iot_add_network_request_event_to_beacon (network_request_event_t event)
 Adds Network Request Event to Beacon. More...
 
appd_iot_error_code_t appd_iot_add_error_event_to_beacon (error_event_t event)
 Adds Error Event to Beacon. More...
 
appd_iot_error_code_t appd_iot_clear_all_beacons (void)
 Clears Beacons in memory. More...
 
appd_iot_error_code_t appd_iot_send_all_beacons (void)
 Sends Beacons in memory to collector.
Max Limit on the number of events in the beacon is defined by
APPD_IOT_MAX_CUSTOM_EVENTS, APPD_IOT_MAX_NETWORK_EVENTS and APPD_IOT_MAX_CUSTOM_EVENTS. More...
 
static void appd_iot_serialize_properties_data_to_json (json_t *json, data_t *data)
 Serializes Data into JSON Format. More...
 

Variables

static beacon_t global_beacon
 

Macro Definition Documentation

◆ APPD_IOT_SDK_VERSION

#define APPD_IOT_SDK_VERSION   "4.4.1.0"

Function Documentation

◆ appd_iot_serialize_beacon_to_json()

static std::string appd_iot_serialize_beacon_to_json ( beacon_t  beacon)
static

Serializes Beacon Data into JSON Format.

Parameters
beaconcontains beacon data to be serialized
Returns
string which contains json formatted data
436 {
437  /* Initialize JSON */
438  json_t* json = appd_iot_json_init();
439 
440  appd_iot_json_start_array(json, NULL);
441  appd_iot_json_start_object(json, NULL);
442 
443  /* Set SDK Version */
445 
446  /* Start Device Config Processing */
447  if (!(beacon.devcfg.device_id.empty() &&
448  beacon.devcfg.device_name.empty() &&
449  beacon.devcfg.device_type.empty()))
450  {
451  appd_iot_json_start_object(json, "deviceInfo");
452 
453  /* Start Device Config Processing */
454  if (!beacon.devcfg.device_id.empty())
455  {
456  appd_iot_json_add_string_key_value(json, "deviceId", beacon.devcfg.device_id.c_str());
457  }
458 
459  if (!beacon.devcfg.device_name.empty())
460  {
461  appd_iot_json_add_string_key_value(json, "deviceName", beacon.devcfg.device_name.c_str());
462  }
463 
464  if (!beacon.devcfg.device_type.empty())
465  {
466  appd_iot_json_add_string_key_value(json, "deviceType", beacon.devcfg.device_type.c_str());
467  }
468 
470  }
471 
472  if (!(beacon.devcfg.hw_version.empty() &&
473  beacon.devcfg.fw_version.empty() &&
474  beacon.devcfg.sw_version.empty() &&
475  beacon.devcfg.os_version.empty()))
476  {
477  appd_iot_json_start_object(json, "versionInfo");
478 
479  if (!beacon.devcfg.hw_version.empty())
480  {
481  appd_iot_json_add_string_key_value(json, "hardwareVersion", beacon.devcfg.hw_version.c_str());
482  }
483 
484  if (!beacon.devcfg.fw_version.empty())
485  {
486  appd_iot_json_add_string_key_value(json, "firmwareVersion", beacon.devcfg.fw_version.c_str());
487  }
488 
489  if (!beacon.devcfg.sw_version.empty())
490  {
491  appd_iot_json_add_string_key_value(json, "softwareVersion", beacon.devcfg.sw_version.c_str());
492  }
493 
494  if (!beacon.devcfg.os_version.empty())
495  {
496  appd_iot_json_add_string_key_value(json, "operatingSystemVersion", beacon.devcfg.os_version.c_str());
497  }
498 
500  } /* End Device Config Processing */
501 
502  /* Start Custom Event Processing */
503  if (beacon.custom_event_list.size() != 0)
504  {
505  std::list<custom_event_t>::iterator it = beacon.custom_event_list.begin();
506 
507  appd_iot_json_start_array(json, "customEvents");
508 
509  for (; it != beacon.custom_event_list.end(); ++it)
510  {
511  custom_event_t event = (custom_event_t)(*it);
512 
513  appd_iot_json_start_object(json, NULL);
514 
515  if (!event.type.empty())
516  {
517  appd_iot_json_add_string_key_value(json, "eventType", event.type.c_str());
518  }
519 
520  if (!event.summary.empty())
521  {
522  appd_iot_json_add_string_key_value(json, "eventSummary", event.summary.c_str());
523  }
524 
525  if (event.timestamp_ms != 0)
526  {
527  appd_iot_json_add_integer_key_value(json, "timestamp", event.timestamp_ms);
528  }
529 
530  if (event.duration_ms > 0)
531  {
532  appd_iot_json_add_integer_key_value(json, "duration", event.duration_ms);
533  }
534 
536 
538  }
539 
541  } /* End Custom Event Processing */
542 
543  /* Start Network Event Processing */
544  if (beacon.network_request_event_list.size() != 0)
545  {
546  std::list<network_request_event_t>::iterator it = beacon.network_request_event_list.begin();
547 
548  appd_iot_json_start_array(json, "networkRequestEvents");
549 
550  for (; it != beacon.network_request_event_list.end(); ++it)
551  {
553 
554  appd_iot_json_start_object(json, NULL);
555 
556  appd_iot_json_add_string_key_value(json, "url", event.url.c_str());
557 
558  if (event.resp_code != 0)
559  {
560  appd_iot_json_add_integer_key_value(json, "statusCode", event.resp_code);
561  }
562 
563  if (!event.error.empty())
564  {
565  appd_iot_json_add_string_key_value(json, "networkError", event.error.c_str());
566  }
567 
568  if (event.req_content_length > 0)
569  {
570  appd_iot_json_add_integer_key_value(json, "requestContentLength", event.req_content_length);
571  }
572 
573  if (event.resp_content_length > 0)
574  {
575  appd_iot_json_add_integer_key_value(json, "responseContentLength", event.resp_content_length);
576  }
577 
578  if (event.timestamp_ms != 0)
579  {
580  appd_iot_json_add_integer_key_value(json, "timestamp", event.timestamp_ms);
581  }
582 
583  if (event.duration_ms > 0)
584  {
585  appd_iot_json_add_integer_key_value(json, "duration", event.duration_ms);
586  }
587 
588  //Response Headers are expected to have {key, value} pairs as strings
589  if (!(event.resp_headers.stringmap.empty()))
590  {
591  appd_iot_json_start_object(json, "responseHeaders");
592 
593  for (std::map<std::string, std::string>::iterator resp_header_it = event.resp_headers.stringmap.begin();
594  resp_header_it != event.resp_headers.stringmap.end(); ++resp_header_it)
595  {
596  appd_iot_json_start_array(json, (resp_header_it->first).c_str());
597  appd_iot_json_add_string_value(json, (resp_header_it->second).c_str());
599  }
600 
602  }
603 
605 
607  }
608 
610  } /* End Network Event Processing */
611 
612 
613  /* Start Error Event Processing */
614  if (beacon.error_event_list.size() != 0)
615  {
616  std::list<error_event_t>::iterator it = beacon.error_event_list.begin();
617 
618  appd_iot_json_start_array(json, "errorEvents");
619 
620  for (; it != beacon.error_event_list.end(); ++it)
621  {
622  error_event_t event = (error_event_t)(*it);
623 
624  appd_iot_json_start_object(json, NULL);
625 
626  if (!event.name.empty())
627  {
628  appd_iot_json_add_string_key_value(json, "name", event.name.c_str());
629  }
630 
631  if (!event.message.empty())
632  {
633  appd_iot_json_add_string_key_value(json, "message", event.message.c_str());
634  }
635 
636  if (!event.severity.empty())
637  {
638  appd_iot_json_add_string_key_value(json, "severity", event.severity.c_str());
639  }
640 
641  if (event.timestamp_ms != 0)
642  {
643  appd_iot_json_add_integer_key_value(json, "timestamp", event.timestamp_ms);
644  }
645 
646  if (event.duration_ms > 0)
647  {
648  appd_iot_json_add_integer_key_value(json, "duration", event.duration_ms);
649  }
650 
651  if (!event.stack_trace_list.empty())
652  {
653  std::list<stack_trace_t>::iterator stack_trace_it = event.stack_trace_list.begin();
654 
655  appd_iot_json_add_integer_key_value(json, "errorStackTraceIndex", event.error_stack_trace_index);
656 
657  appd_iot_json_start_array(json, "stackTraces");
658 
659  //loop over stack traces
660  for (; stack_trace_it != event.stack_trace_list.end(); ++stack_trace_it)
661  {
662  stack_trace_t stack_trace = (stack_trace_t)(*stack_trace_it);
663 
664  appd_iot_json_start_object(json, NULL);
665 
666  appd_iot_json_add_string_key_value(json, "thread", stack_trace.thread.c_str());
667  appd_iot_json_add_string_key_value(json, "runtime", stack_trace.runtime.c_str());
668 
669  if (!(stack_trace.stack_frame_list.empty()))
670  {
671  std::list<stack_frame_t>::iterator stack_frame_it = stack_trace.stack_frame_list.begin();
672 
673  appd_iot_json_start_array(json, "stackFrames");
674 
675  //loop over stack frames within a single stack trace
676  for (; stack_frame_it != stack_trace.stack_frame_list.end(); ++stack_frame_it)
677  {
678  stack_frame_t stack_frame = (stack_frame_t)(*stack_frame_it);
679  appd_iot_json_start_object(json, NULL);
680 
681  if (!stack_frame.symbol_name.empty())
682  {
683  appd_iot_json_add_string_key_value(json, "symbolName", stack_frame.symbol_name.c_str());
684  appd_iot_json_add_integer_key_value(json, "symbolOffset", stack_frame.symbol_offset);
685  }
686 
687  if (!stack_frame.package_name.empty())
688  {
689  appd_iot_json_add_string_key_value(json, "packageName", stack_frame.package_name.c_str());
690  }
691 
692  if (!stack_frame.file_name.empty())
693  {
694  appd_iot_json_add_string_key_value(json, "filePath", stack_frame.file_name.c_str());
695  }
696 
697  if (stack_frame.lineno > 0)
698  {
699  appd_iot_json_add_integer_key_value(json, "lineNumber", stack_frame.lineno);
700  }
701 
702  appd_iot_json_add_integer_key_value(json, "absoluteAddress", stack_frame.absolute_addr);
703  appd_iot_json_add_integer_key_value(json, "imageOffset", stack_frame.image_offset);
704 
706  }
707 
709  }
710 
712  }
713 
715  }
716 
718 
720  }
721 
723  } /* End Error Event Processing */
724 
726 
728 
730 
731  const char* json_str = appd_iot_json_get_string(json);
732 
733  std::string ret_str;
734 
735  if (json_str != NULL)
736  {
737  //Assign operator for std::string will make a copy of the string.
738  ret_str = json_str;
739  }
740 
741  /* clearing root json object frees memory for all child json objects */
742  appd_iot_json_free(json);
743 
744  return ret_str;
745 }
static void appd_iot_serialize_properties_data_to_json(json_t *json, data_t *data)
Serializes Data into JSON Format.
Definition: beacon.cpp:339
int image_offset
Definition: beacon.hpp:67
std::string os_version
Definition: beacon.hpp:98
int symbol_offset
Definition: beacon.hpp:68
std::string package_name
Definition: beacon.hpp:63
appd_iot_error_code_t appd_iot_json_add_string_value(json_t *json, const char *value)
adds value to json object. This function is typically used to add string values to JSON arrays ...
Definition: json_serializer.cpp:580
uint64_t absolute_addr
Definition: beacon.hpp:66
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
json_t * appd_iot_json_init()
Creates, Initializes and returns a new json struct.
Definition: json_serializer.cpp:49
Definition: appd_iot_def.h:107
appd_iot_error_code_t appd_iot_json_add_integer_key_value(json_t *json, const char *key, int64_t intval)
adds key:value pair to json object with value as 64 bit integer
Definition: json_serializer.cpp:451
appd_iot_error_code_t appd_iot_json_add_string_key_value(json_t *json, const char *key, const char *strval)
adds key:value pair to json object with value as string
Definition: json_serializer.cpp:438
appd_iot_error_code_t appd_iot_json_end_array(json_t *json)
ends json object by adding char &#39;]&#39; to json buf.
Definition: json_serializer.cpp:220
std::string fw_version
Definition: beacon.hpp:96
Definition: beacon.hpp:60
std::list< network_request_event_t > network_request_event_list
Definition: beacon.hpp:105
Definition: beacon.hpp:71
Definition: beacon.hpp:38
device_cfg_t devcfg
Definition: beacon.hpp:103
const char * appd_iot_json_pretty_print(json_t *json)
format and returns json string with line breaks, indentiation at start/end of objects/arrays ...
Definition: json_serializer.cpp:634
std::string symbol_name
Definition: beacon.hpp:62
std::string device_type
Definition: beacon.hpp:93
const char * appd_iot_json_get_string(json_t *json)
returns the json string constructed so far.
Definition: json_serializer.cpp:624
#define APPD_IOT_SDK_VERSION
Definition: beacon.cpp:25
void appd_iot_json_free(json_t *json)
frees json structure
Definition: json_serializer.cpp:731
std::string device_name
Definition: beacon.hpp:92
int lineno
Definition: beacon.hpp:65
std::string device_id
Definition: beacon.hpp:94
Definition: beacon.hpp:78
std::list< stack_frame_t > stack_frame_list
Definition: beacon.hpp:75
std::string thread
Definition: beacon.hpp:73
Definition: beacon.hpp:47
appd_iot_error_code_t appd_iot_json_end_object(json_t *json)
ends json object by adding char &#39;}&#39; to json buf.
Definition: json_serializer.cpp:210
std::string file_name
Definition: beacon.hpp:64
std::list< custom_event_t > custom_event_list
Definition: beacon.hpp:104
std::list< error_event_t > error_event_list
Definition: beacon.hpp:106
appd_iot_error_code_t appd_iot_json_start_array(json_t *json, const char *array_name)
starts json array by adding char &#39;[&#39; to json buf. If a name is given it will be "name":[ ...
Definition: json_serializer.cpp:147
appd_iot_error_code_t appd_iot_json_start_object(json_t *json, const char *object_name)
starts json object by adding char &#39;{&#39; to json buf. If a name is given it will be "name":{ ...
Definition: json_serializer.cpp:159
std::string hw_version
Definition: beacon.hpp:95
JSON struct which holds json string in a buffer.
Definition: json_serializer.hpp:39
std::string runtime
Definition: beacon.hpp:74
std::string sw_version
Definition: beacon.hpp:97

◆ appd_iot_init_device_config()

appd_iot_error_code_t appd_iot_init_device_config ( appd_iot_device_config_t  devcfg)

Initializes Device Configuration
It is madatory to set Device ID and Device Type.

Parameters
devcfgcontains device configuration
Returns
appd_iot_error_code_t indicating function execution status
38 {
39 
40  if (devcfg.device_type == NULL)
41  {
42  appd_iot_log(APPD_IOT_LOG_ERROR, "Device Type cannot be NULL");
44  }
45 
47 
48  if (devcfg.device_id != NULL)
49  {
51  }
52  else
53  {
54  appd_iot_log(APPD_IOT_LOG_WARN, "Device ID is NULL");
55  }
56 
57  if (devcfg.device_name != NULL)
58  {
60  }
61 
62  if (devcfg.fw_version != NULL)
63  {
65  }
66 
67  if (devcfg.hw_version != NULL)
68  {
70  }
71 
72  if (devcfg.os_version != NULL)
73  {
75  }
76 
77  if (devcfg.sw_version != NULL)
78  {
80  }
81 
82  return APPD_IOT_SUCCESS;
83 }
std::string os_version
Definition: beacon.hpp:98
const char * os_version
Definition: appd_iot_def.h:178
static beacon_t global_beacon
Definition: beacon.cpp:27
const char * device_type
Definition: appd_iot_def.h:168
const char * device_id
Definition: appd_iot_def.h:170
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
const char * device_name
Definition: appd_iot_def.h:166
Definition: appd_iot_def.h:101
std::string fw_version
Definition: beacon.hpp:96
device_cfg_t devcfg
Definition: beacon.hpp:103
const char * hw_version
Definition: appd_iot_def.h:172
std::string device_type
Definition: beacon.hpp:93
Definition: appd_iot_def.h:32
std::string device_name
Definition: beacon.hpp:92
std::string device_id
Definition: beacon.hpp:94
std::string appd_iot_remove_character(const char *input, char c)
Removes a given character from the input string.
Definition: utils.cpp:27
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
const char * sw_version
Definition: appd_iot_def.h:176
const char * fw_version
Definition: appd_iot_def.h:174
std::string hw_version
Definition: beacon.hpp:95
std::string sw_version
Definition: beacon.hpp:97

◆ appd_iot_add_custom_event_to_beacon()

appd_iot_error_code_t appd_iot_add_custom_event_to_beacon ( custom_event_t  event)

Adds Custom Event to Beacon.

Parameters
eventcontains custom event data to be sent to collector
Returns
appd_iot_error_code_t indicating function execution status
91 {
93  {
94  global_beacon.custom_event_list.push_back(event);
95 
96  appd_iot_log(APPD_IOT_LOG_INFO, "Custom Event Added, Size:%zu", global_beacon.custom_event_list.size());
97 
98  return APPD_IOT_SUCCESS;
99  }
100  else
101  {
103  "Max Custom Events (%d) in Buffer. Send Events in Buffer to Collector before adding new events",
105 
106  return APPD_IOT_ERR_MAX_LIMIT;
107  }
108 }
static beacon_t global_beacon
Definition: beacon.cpp:27
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
Definition: appd_iot_def.h:103
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
#define APPD_IOT_MAX_CUSTOM_EVENTS
Definition: beacon.hpp:25
std::list< custom_event_t > custom_event_list
Definition: beacon.hpp:104
Definition: appd_iot_def.h:34

◆ appd_iot_add_network_request_event_to_beacon()

appd_iot_error_code_t appd_iot_add_network_request_event_to_beacon ( network_request_event_t  event)

Adds Network Request Event to Beacon.

Parameters
eventcontains network request event data to be sent to collector
Returns
appd_iot_error_code_t indicating function execution status
116 {
118  {
119  global_beacon.network_request_event_list.push_back(event);
120 
121  appd_iot_log(APPD_IOT_LOG_INFO, "Network Event Added, Size:%zu",
123 
124  return APPD_IOT_SUCCESS;
125  }
126  else
127  {
129  "Max Network Events (%d) in Buffer. Send Events in Buffer to Collector before adding new events",
131 
132  return APPD_IOT_ERR_MAX_LIMIT;
133  }
134 }
static beacon_t global_beacon
Definition: beacon.cpp:27
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
#define APPD_IOT_MAX_NETWORK_EVENTS
Definition: beacon.hpp:26
std::list< network_request_event_t > network_request_event_list
Definition: beacon.hpp:105
Definition: appd_iot_def.h:103
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
Definition: appd_iot_def.h:34

◆ appd_iot_add_error_event_to_beacon()

appd_iot_error_code_t appd_iot_add_error_event_to_beacon ( error_event_t  event)

Adds Error Event to Beacon.

Parameters
eventcontains error event data to be sent to collector
Returns
appd_iot_error_code_t indicating function execution status
142 {
144  {
145  global_beacon.error_event_list.push_back(event);
146 
147  appd_iot_log(APPD_IOT_LOG_INFO, "Error Event Added, Size:%zu", global_beacon.error_event_list.size());
148 
149  return APPD_IOT_SUCCESS;
150  }
151  else
152  {
154  "Max Error Events (%d) in Buffer. Send Events in Buffer to Collector before adding new events",
156 
157  return APPD_IOT_ERR_MAX_LIMIT;
158  }
159 
160 }
static beacon_t global_beacon
Definition: beacon.cpp:27
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
#define APPD_IOT_MAX_ERROR_EVENTS
Definition: beacon.hpp:27
Definition: appd_iot_def.h:103
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
Definition: appd_iot_def.h:34
std::list< error_event_t > error_event_list
Definition: beacon.hpp:106

◆ appd_iot_clear_all_beacons()

appd_iot_error_code_t appd_iot_clear_all_beacons ( void  )

Clears Beacons in memory.

Returns
appd_iot_error_code_t indicating function execution status
168 {
169  appd_iot_log(APPD_IOT_LOG_INFO, "Clearing All Beacons");
170  appd_iot_log(APPD_IOT_LOG_INFO, "Clearing %zu Custom Events", global_beacon.custom_event_list.size());
171  appd_iot_log(APPD_IOT_LOG_INFO, "Clearing %zu Network Events",
173  appd_iot_log(APPD_IOT_LOG_INFO, "Clearing %zu Error Events", global_beacon.error_event_list.size());
174 
178 
179  return APPD_IOT_SUCCESS;
180 }
static beacon_t global_beacon
Definition: beacon.cpp:27
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
std::list< network_request_event_t > network_request_event_list
Definition: beacon.hpp:105
Definition: appd_iot_def.h:103
Definition: appd_iot_def.h:30
std::list< custom_event_t > custom_event_list
Definition: beacon.hpp:104
std::list< error_event_t > error_event_list
Definition: beacon.hpp:106

◆ appd_iot_send_all_beacons()

appd_iot_error_code_t appd_iot_send_all_beacons ( void  )

Sends Beacons in memory to collector.
Max Limit on the number of events in the beacon is defined by
APPD_IOT_MAX_CUSTOM_EVENTS, APPD_IOT_MAX_NETWORK_EVENTS and APPD_IOT_MAX_CUSTOM_EVENTS.

Sends Beacons in memory to collector.

Returns
appd_iot_error_code_t indicating function execution status
190 {
191 
192  if (global_beacon.custom_event_list.size() == 0 &&
194  global_beacon.error_event_list.size() == 0)
195  {
196  appd_iot_log(APPD_IOT_LOG_INFO, "No Events Present");
197  return APPD_IOT_SUCCESS;
198  }
199 
200  appd_iot_log(APPD_IOT_LOG_INFO, "Sending All Beacons");
201  appd_iot_log(APPD_IOT_LOG_INFO, "Sending %zu Custom Events", global_beacon.custom_event_list.size());
202  appd_iot_log(APPD_IOT_LOG_INFO, "Sending %zu Network Events",
204  appd_iot_log(APPD_IOT_LOG_INFO, "Sending %zu Error Events", global_beacon.error_event_list.size());
205 
206  /* Init all the data structures - REQ and RESP */
207  appd_iot_http_req_t http_req;
208  appd_iot_http_resp_t* http_resp = NULL;
209  std::string jsondata;
213 
214  if (http_req_send_cb == NULL)
215  {
216  appd_iot_log(APPD_IOT_LOG_ERROR, "Network Interface Not Available");
218  }
219 
221 
222  if (jsondata.empty())
223  {
224  appd_iot_log(APPD_IOT_LOG_ERROR, "Failed to Serialize Data to JSON Format");
225  return APPD_IOT_ERR_NULL_PTR;
226  }
227 
228  char jsonlen_buf[10];
229  snprintf(jsonlen_buf, sizeof(jsonlen_buf), "%lu", jsondata.length());
230 
231  appd_iot_init_to_zero(&http_req, sizeof(http_req));
232 
233  http_req.data = jsondata.c_str();
234  http_req.type = "POST";
235  http_req.url = appd_iot_get_eum_collector_url();
236  http_req.headers_count = 3;
237  http_req.headers = (appd_iot_data_t*)calloc(http_req.headers_count, sizeof(appd_iot_data_t));
238 
239  if (http_req.headers == NULL)
240  {
241  appd_iot_log(APPD_IOT_LOG_ERROR, "Failed to Create HTTP Request Headers");
242  return APPD_IOT_ERR_NULL_PTR;
243  }
244 
245  appd_iot_data_set_string(&http_req.headers[0], "Accept", "application/json");
246  appd_iot_data_set_string(&http_req.headers[1], "Content-Type", "application/json");
247  appd_iot_data_set_string(&http_req.headers[2], "Content-Length", jsonlen_buf);
248 
249  appd_iot_log(APPD_IOT_LOG_INFO, "Content Len:%lu", jsondata.length());
250 
251  http_resp = http_req_send_cb(&http_req);
252 
253  free(http_req.headers);
254 
255  /* check if any error present in http response */
256  if (http_resp != NULL)
257  {
258  retcode = http_resp->error;
259  }
260  else
261  {
262  appd_iot_log(APPD_IOT_LOG_ERROR, "NULL HTTP Response Returned");
263  retcode = APPD_IOT_ERR_NULL_PTR;
264  }
265 
266  /* Return if there is an error executing http req */
267  if (retcode != APPD_IOT_SUCCESS)
268  {
269  appd_iot_log(APPD_IOT_LOG_ERROR, "Error Executing HTTP Request:%s",
270  appd_iot_error_code_to_str(retcode));
271 
272  if (http_resp_done_cb != NULL)
273  {
274  http_resp_done_cb(http_resp);
275  }
276 
277  return retcode;
278  }
279 
280  /* Read http response headers, content and response code */
281  for (int i = 0; i < http_resp->headers_count; i++)
282  {
283  if ((http_resp->headers + i) == NULL)
284  {
285  continue;
286  }
287 
288  if (http_resp->headers[i].key == NULL || http_resp->headers[i].strval == NULL ||
289  http_resp->headers[i].value_type != APPD_IOT_STRING)
290  {
291  continue;
292  }
293 
294  appd_iot_log(APPD_IOT_LOG_INFO, "Response Header%d (%s:%s)", i, http_resp->headers[i].key,
295  http_resp->headers[i].strval);
296  }
297 
298  if (http_resp->content_len > 0)
299  {
300  appd_iot_log(APPD_IOT_LOG_INFO, "Response Content Len:%lu", http_resp->content_len);
301  appd_iot_log(APPD_IOT_LOG_INFO, "Response Content:%s", http_resp->content);
302  }
303 
304 
305  if (http_resp->resp_code >= 200 && http_resp->resp_code < 300)
306  {
307  appd_iot_log(APPD_IOT_LOG_INFO, "RespCode:%d Beacon Sent Successfully", http_resp->resp_code);
309  retcode = APPD_IOT_SUCCESS;
310  }
311  else if ((http_resp->resp_code == 402) ||
312  (http_resp->resp_code == 403) ||
313  (http_resp->resp_code == 429))
314  {
316  appd_iot_disable_sdk(http_resp->resp_code);
317  retcode = APPD_IOT_ERR_NETWORK_REJECT;
318  }
319  else
320  {
321  appd_iot_log(APPD_IOT_LOG_ERROR, "Resp Code:%d Send Beacons Network Request Failed", http_resp->resp_code);
322  retcode = APPD_IOT_ERR_NETWORK_ERROR;
323  }
324 
325  if (http_resp_done_cb != NULL)
326  {
327  http_resp_done_cb(http_resp);
328  }
329 
330  return retcode;
331 }
AppDynamics Associative Array Data Structure Underlying implementation is a Sequential List of <key...
Definition: appd_iot_def.h:204
appd_iot_error_code_t appd_iot_clear_all_beacons(void)
Clears Beacons in memory.
Definition: beacon.cpp:167
Definition: appd_iot_def.h:44
static beacon_t global_beacon
Definition: beacon.cpp:27
Definition: appd_iot_def.h:42
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
Definition: appd_iot_def.h:36
Definition: appd_iot_def.h:192
Definition: appd_iot_def.h:38
std::list< network_request_event_t > network_request_event_list
Definition: beacon.hpp:105
appd_iot_http_req_send_cb_t appd_iot_get_http_req_send_cb(void)
Get http request send callback function pointer.
Definition: config.cpp:156
const char * appd_iot_error_code_to_str(appd_iot_error_code_t error_code) __APPD_IOT_API
Convert error code to string.
Definition: log.cpp:193
appd_iot_error_code_t
Error Code Enums for SDK log.
Definition: appd_iot_def.h:27
static std::string appd_iot_serialize_beacon_to_json(beacon_t beacon)
Serializes Beacon Data into JSON Format.
Definition: beacon.cpp:435
Definition: appd_iot_def.h:103
static void appd_iot_init_to_zero(void *ptr, size_t size)
Initializes memory block pointed by ptr to zero.
Definition: appd_iot_def.h:478
AppDynamics HTTP Response Structure Mandatory: One of the resp_code or error fields should be popul...
Definition: appd_iot_def.h:365
void(* appd_iot_http_resp_done_cb_t)(appd_iot_http_resp_t *http_resp)
Http Response Done Callback is triggered after http send callback returns and http response is proc...
Definition: appd_iot_def.h:407
appd_iot_http_resp_t *(* appd_iot_http_req_send_cb_t)(const appd_iot_http_req_t *http_req)
Http Request Send Callback implements the functionality to send HTTP Request The callback implement...
Definition: appd_iot_def.h:395
AppDynamics HTTP Request Structure Mandatory: All Fields Data is provided in raw format...
Definition: appd_iot_def.h:346
Definition: appd_iot_def.h:30
Definition: appd_iot_def.h:99
const char * appd_iot_get_eum_collector_url(void)
Get Configured EUM Collector URL.
Definition: config.cpp:193
std::list< custom_event_t > custom_event_list
Definition: beacon.hpp:104
void appd_iot_disable_sdk(int http_resp_code)
Set SDK state to disabled state based on the HTTP Response Code.
Definition: config.cpp:245
std::list< error_event_t > error_event_list
Definition: beacon.hpp:106
appd_iot_http_resp_done_cb_t appd_iot_get_http_resp_done_cb(void)
Get http response done callback function pointer.
Definition: config.cpp:165
static void appd_iot_data_set_string(appd_iot_data_t *data, const char *key, const char *value)
Sets data field attributes for string value.
Definition: appd_iot_def.h:468

◆ appd_iot_serialize_properties_data_to_json()

static void appd_iot_serialize_properties_data_to_json ( json_t json,
data_t data 
)
static

Serializes Data into JSON Format.

Parameters
jsonobject which contains buffer to which serialized data is written to
datacontains data_t object from which data properties are read
340 {
341  if (json == NULL)
342  {
343  appd_iot_log(APPD_IOT_LOG_ERROR, "Serializing Properties failed due to NULL json Object");
344  return;
345  }
346 
347  if (data == NULL)
348  {
349  appd_iot_log(APPD_IOT_LOG_ERROR, "Serializing Properties failed due to NULL data Object");
350  return;
351  }
352 
353  if (!data->stringmap.empty())
354  {
355  appd_iot_json_start_object(json, "stringProperties");
356 
357  for (std::map<std::string, std::string>::iterator it = data->stringmap.begin();
358  it != data->stringmap.end(); ++it)
359  {
360  std::string key = it->first;
361  std::string value = it->second;
362  appd_iot_json_add_string_key_value(json, key.c_str(), value.c_str());
363  }
364 
366  }
367 
368  if (!data->integermap.empty())
369  {
370  appd_iot_json_start_object(json, "longProperties");
371 
372  for (std::map<std::string, int64_t>::iterator it = data->integermap.begin();
373  it != data->integermap.end(); ++it)
374  {
375  std::string key = it->first;
376  int64_t value = it->second;
377  appd_iot_json_add_integer_key_value(json, key.c_str(), value);
378  }
379 
381  }
382 
383  if (!data->doublemap.empty())
384  {
385  appd_iot_json_start_object(json, "doubleProperties");
386 
387  for (std::map<std::string, double>::iterator it = data->doublemap.begin();
388  it != data->doublemap.end(); ++it)
389  {
390  std::string key = it->first;
391  double value = it->second;
392  appd_iot_json_add_double_key_value(json, key.c_str(), value);
393  }
394 
396  }
397 
398  if (!data->boolmap.empty())
399  {
400  appd_iot_json_start_object(json, "booleanProperties");
401 
402  for (std::map<std::string, bool>::iterator it = data->boolmap.begin();
403  it != data->boolmap.end(); ++it)
404  {
405  std::string key = it->first;
406  bool value = it->second;
407  appd_iot_json_add_boolean_key_value(json, key.c_str(), value);
408  }
409 
411  }
412 
413  if (!data->datetimemap.empty())
414  {
415  appd_iot_json_start_object(json, "datetimeProperties");
416 
417  for (std::map<std::string, int64_t>::iterator it = data->datetimemap.begin();
418  it != data->datetimemap.end(); ++it)
419  {
420  std::string key = it->first;
421  int64_t value = it->second;
422  appd_iot_json_add_integer_key_value(json, key.c_str(), value);
423  }
424 
426  }
427 }
appd_iot_error_code_t appd_iot_json_add_double_key_value(json_t *json, const char *key, double doubleval)
adds double value to json object
Definition: json_serializer.cpp:460
void appd_iot_log(appd_iot_log_level_t log_level, const char *format,...)
Reads log message, appends log header and triggers log write callback function.
Definition: log.cpp:89
appd_iot_error_code_t appd_iot_json_add_integer_key_value(json_t *json, const char *key, int64_t intval)
adds key:value pair to json object with value as 64 bit integer
Definition: json_serializer.cpp:451
appd_iot_error_code_t appd_iot_json_add_string_key_value(json_t *json, const char *key, const char *strval)
adds key:value pair to json object with value as string
Definition: json_serializer.cpp:438
std::map< std::string, bool > boolmap
Definition: beacon.hpp:34
std::map< std::string, std::string > stringmap
Definition: beacon.hpp:31
std::map< std::string, int64_t > integermap
Definition: beacon.hpp:32
std::map< std::string, int64_t > datetimemap
Definition: beacon.hpp:35
appd_iot_error_code_t appd_iot_json_add_boolean_key_value(json_t *json, const char *key, bool boolval)
adds key:value pair to json object with value as boolean
Definition: json_serializer.cpp:472
std::map< std::string, double > doublemap
Definition: beacon.hpp:33
Definition: appd_iot_def.h:99
appd_iot_error_code_t appd_iot_json_end_object(json_t *json)
ends json object by adding char &#39;}&#39; to json buf.
Definition: json_serializer.cpp:210
appd_iot_error_code_t appd_iot_json_start_object(json_t *json, const char *object_name)
starts json object by adding char &#39;{&#39; to json buf. If a name is given it will be "name":{ ...
Definition: json_serializer.cpp:159

Variable Documentation

◆ global_beacon

beacon_t global_beacon
static