AppDynamics IoT C++ SDK
AppDynamics IoT C++ library contains code that facilitates capturing availability, usage, network performance and errors of an IoT Application.
beacon.hpp File Reference
#include <string>
#include <map>
#include <list>
#include "appd_iot_interface.h"

Go to the source code of this file.

Data Structures

struct  data_t
 
struct  custom_event_t
 
struct  network_request_event_t
 
struct  stack_frame_t
 
struct  stack_trace_t
 
struct  error_event_t
 
struct  device_cfg_t
 
struct  beacon_t
 

Macros

#define APPD_IOT_MAX_CUSTOM_EVENTS   200
 
#define APPD_IOT_MAX_NETWORK_EVENTS   200
 
#define APPD_IOT_MAX_ERROR_EVENTS   200
 

Functions

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_send_all_beacons (void)
 Sends Beacons in memory to collector. More...
 
appd_iot_error_code_t appd_iot_clear_all_beacons (void)
 Clears Beacons in memory. More...
 

Macro Definition Documentation

◆ APPD_IOT_MAX_CUSTOM_EVENTS

#define APPD_IOT_MAX_CUSTOM_EVENTS   200

◆ APPD_IOT_MAX_NETWORK_EVENTS

#define APPD_IOT_MAX_NETWORK_EVENTS   200

◆ APPD_IOT_MAX_ERROR_EVENTS

#define APPD_IOT_MAX_ERROR_EVENTS   200

Function Documentation

◆ 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_send_all_beacons()

appd_iot_error_code_t appd_iot_send_all_beacons ( void  )

Sends Beacons in memory to collector.

Returns
appd_iot_error_code_t indicating function execution status

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_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