text:
from keras.models import Sequential
from keras.layers import Dense, Activation, Reshape, Merge
from keras.layers.core import Flatten, Reshape, Dropout, Lambda
from keras.layers.convolutional import Convolution2D
from keras.layers.pooling import MaxPooling2D, AveragePooling1D
from keras.optimizers import Adam, SGD
from keras.utils.np_utils import to_categorical
from keras.callbacks import Callback as KerasCallback
import numpy as np
import cv2
import random as rand
from numpy import genfromtxt
import ntpath

import driving_data

#defining needed functions
"""
Suffle multiple arrays with respect of its indexing
Args:
    arrs: list of arrays of same length
Returns:
    list of shuffled arrays
"""
def shuffle_arrs (arrs):
    p = np.random.permutation (len(arrs[0]))
    return [arr[p] for arr in arrs]

"""
Removes specified @proportion of records which satisfies condition from given arrays (@arrs)
with respect of its indexing. Removing is based on specified array (@arr_idx) values.
Values of array are testing with @condition_lambda and dividing into two parts.
First part that satisfies @condition_lambda and second which is not.
Next first part is suffled and sparsed by @proportion, concatenated with second and returned.
Other array values will be sparsed correspondingly

You can use this function to remove portion of records with some specific values from
dataset represented by list of arrays

Args:
    proportion: floating point value from 0 to 1
    condition_lambda: lambda which will be used fot testing values of specified array
    arr_idx: index in array list which points to array that will be used for selecting values
    arrs: list of arrays

Returns:
    list of arrays
"""
def sparse_arrays_by_first_array_value (proportion, condition_lambda, arr_idx, arrs):

    target_arr = arrs [arr_idx]
    idx_less = np.where(condition_lambda(target_arr))
    idx_greater = np.where(np.logical_not(condition_lambda(target_arr)))

    less_arrs = [arr[idx_less] for arr in arrs]
    greater_arrs = [arr[idx_greater] for arr in arrs]

    less_arrs = shuffle_arrs (less_arrs)

    proportion_count = int(proportion * len (less_arrs[arr_idx]))
    less_arrs = [less_arr[0:proportion_count] for less_arr in less_arrs]

    retarrs = [np.concatenate ((less_arr, greater_arr)) for less_arr, greater_arr in zip(less_arrs, greater_arrs)]

    return retarrs

"""
Splits specified list of arrays in specified proportion
Args:
    arrs: list of arrays
    test_size: floating point value from 0 to 1 representing test dataset proportion
Returns:
    tuple of list of arrays
    first list is train dataset
    second is test dataset
"""
def train_test_split (arrs, test_size):
    arrs = shuffle_arrs (arrs)
    test_size_count = int ((1.0-test_size) * len(arrs[0]))
    train = [arr[0:test_size_count] for arr in arrs]
    test = [arr[test_size_count:] for arr in arrs]
    return train, test

"""
Loads datasets from CSV file, doesn't load images
Args:
    datasets: list of
        strings - dataset dir path or
        tuples - (dataset dir path, floating point proportion of dataset needed to be loaded)

    for example:
        ['dir/path', ('dir/path', 0.5)]
Returns:
    tuple of two arrays:
        steering wheel data array,
        camera image path array
"""
def load_datasets (datasets):
    all_steering = np.zeros ((0,))
    all_center = np.zeros ((0,))
    for dataset in datasets:
        dataset_file = ''
        dataset_size = 1
        if (isinstance(dataset, tuple)):
            dataset_file = dataset [0]
            dataset_size = dataset [1]
        else:
            dataset_file = dataset

        driving_log = genfromtxt (
            dataset_file + '/driving_log.csv',
            delimiter=',',
            skip_header=1,
            dtype=None)

        #center, left, right, steering, throttle, break, speed
        indices = range (len(driving_log))
        center, left, right, steering, throttle, breakvals, speed = zip (*driving_log)
        center = np.asarray (center)
        steering = np.asarray (steering)

        if (dataset_size < 1):
            arrs, not_needed = train_test_split ([steering, center], test_size=1-dataset_size)
            steering, center = arrs

        all_steering = np.concatenate([all_steering, steering])
        all_center = np.concatenate([
                all_center,
                np.core.defchararray.add(
                    dataset_file + '/IMG/',
                    np.asarray (
                            [ntpath.basename (path) for path in np.core.defchararray.decode(center)]
                        )
                )
            ])

    return all_steering, all_center

#loading needed datasets

#datasets with straight steering
#datasets = ['record-backward', 'record-forward', 'record-backward-2', 'record-forward-2']

if True:
  datasets = ['record-backward' ]

  steering, center_camera = load_datasets (datasets)

  '''
  #datasets with recovering from left divergence
  steering_lr, center_camera_lr = load_datasets ([
        #strong divergence, using only 1/4 of the dataset
        ('record-left-recover', 0.5),
        #medium divergence
        ('record-left-recover/2', 1),
        #light divergence
        ('record-left-recover/3', 1)
    ])

  #datasets with recovering from right divergence
  steering_rr, center_camera_rr = load_datasets ([
        ('record-right-recover', 0.5),
        ('record-right-recover/2', 1),
        ('record-right-recover/3', 1),
    ])

  #remove steering that less than 0 for recover from left dataset
  #so removing divergence steering and staying only recovering steering
  arrs = sparse_arrays_by_first_array_value (0, lambda v: v < 0, 0, [steering_lr, center_camera_lr])
  steering_lr, center_camera_lr = arrs

  #remove steering that greater than 0 for recover dataset
  arrs = sparse_arrays_by_first_array_value (0, lambda v: v > 0, 0, [steering_rr, center_camera_rr])
  steering_rr, center_camera_rr = arrs
  '''
  #joining datasets
  #steering = np.concatenate ([steering, steering_lr, steering_rr])
  #center_camera = np.concatenate ([center_camera, center_camera_lr, center_camera_rr])

  #We discussed proj 3 in our study group and I decided to filter steering wheel values around zero,
  #as it dominates in the dataset
  arrs = sparse_arrays_by_first_array_value (0.1, lambda v: abs(v) < 1e-5, 0, [steering, center_camera])
  steering, center_camera = arrs

#loading dataset images
"""
Loades dataset images
Args:
    paths: arrays of image paths
Returns:
    numpy arrays of grayscaled, normalized images,
    pixel values are in range (-0.5, 0.5)
"""
def load_images (paths):
    dataset_images = []
    for path in paths:
        im = cv2.imread (path, cv2.IMREAD_GRAYSCALE)
        im = np.subtract(np.divide(np.array(im).astype(np.float32), 255.0), 0.5)
        dataset_images.append (im)
    return np.array (dataset_images)

#ajs
dataset_images = load_images (center_camera)

#defining augmentation of the dataset
"""
Image augmentation function.
It creates new image with
    - randomized brightness (-0.3, 0.3)
    - randomly partially occluded with 30
        black 25x25 px squares
    - very slightely randomly:
        - rotated (1 degree)
        - shifted (2px)
        - scaled (0.02)

Args:
    image: base image for generation new one
Returns:
    newly generatied image
"""
def augment_image (image):

    image = np.copy (image)

    (h, w) = image.shape[:2]

    #randomize brightness
    brightness = rand.uniform (-0.3, 0.3)
    image = np.add(image, brightness)

    #random squares

    rect_w = 25
    rect_h = 25
    rect_count = 30
    for i in range (rect_count):
        pt1 = (rand.randint (0, w), rand.randint (0, h))
        pt2 = (pt1[0] + rect_w, pt1[1] + rect_h)
        cv2.rectangle(image, pt1, pt2, (-0.5, -0.5, -0.5), -1)

    #rotation and scaling
    rot = 1
    scale = 0.02
    Mrot = cv2.getRotationMatrix2D((h/2,w/2),rand.uniform(-rot, rot), rand.uniform(1.0 - scale, 1.0 + scale))

    #affine transform and shifts
    pts1 = np.float32([[0,0],[w,0],[w,h]])
    a = 0
    shift = 2
    shiftx = rand.randint (-shift, shift);
    shifty = rand.randint (-shift, shift);
    pts2 = np.float32([[
                0 + rand.randint (-a, a) + shiftx,
                0 + rand.randint (-a, a) + shifty
            ],[
                w + rand.randint (-a, a) + shiftx,
                0 + rand.randint (-a, a) + shifty
            ],[
                w + rand.randint (-a, a) + shiftx,
                h + rand.randint (-a, a) + shifty
            ]])
    M = cv2.getAffineTransform(pts1,pts2)

    augmented = cv2.warpAffine(
            cv2.warpAffine (
                image
                , Mrot, (w, h)
            )
            , M, (w,h)
        )

    return augmented

"""
Augments image basing on input image with augment_image.
Also randomly flips image and steering wheel correspondingly.
Also adds normal noise (0, 0.005) to steering angle
Args:
    im: image
    steering: steering wheel angle
Returns:
    tuple:
        image,
        steering wheel
"""
def augment_record (im, steering):
    im = augment_image(im)
    if (rand.uniform (0, 1) > 0.5):
        im = cv2.flip (im, 1)
        steering = - steering
    steering = steering + np.random.normal (0, 0.005)
    return im, steering

"""
Grenerator for training samples
Args:
    images_arr: train dataset images
    steering_arr: train dataset steering
    batch_size: batch size
Returns:
    Only generated batch of samples, not includes original samples
"""
def augment_generator (images_arr, steering_arr, batch_size):
    print("AJS HERE")
    last_index = len (images_arr) - 1
    while 1:
        batch_img = []
        batch_steering = []
        for i in range (batch_size):

            idx_img = rand.randint (0, last_index)
            im, steering = augment_record (images_arr [idx_img], steering_arr[idx_img])
            #im, steering = images_arr [idx_img], steering_arr[idx_img]

            batch_img.append (im)
            batch_steering.append (steering)

        batch_img = np.asarray (batch_img)
        batch_steering = np.asarray (batch_steering)
        yield (batch_img, batch_steering)

model = Sequential ([
        Reshape ((160, 320, 1), input_shape=(160, 320)),

        Convolution2D (24, 8, 8, border_mode='valid'),
        MaxPooling2D (pool_size=(2, 2)),
        Dropout (0.5),
        Activation ('relu'),

        #77x157
        Convolution2D (36, 5, 5, border_mode='valid'),
        MaxPooling2D (pool_size=(2, 2)),
        Dropout (0.5),
        Activation ('relu'),

        #37x77
        Convolution2D (48, 5, 5, border_mode='valid'),
        MaxPooling2D (pool_size=(2, 2)),
        Dropout (0.5),
        Activation ('relu'),

        #17x37
        Convolution2D (64, 3, 3, border_mode='valid'),
        MaxPooling2D (pool_size=(2, 2)),
        Dropout (0.5),
        Activation ('relu'),

        #8x18
        Convolution2D (64, 2, 2, border_mode='valid'),
        MaxPooling2D (pool_size=(2, 2)),
        Dropout (0.5),
        Activation ('relu'),

        #4x9
        Flatten (),

        Dense (1024),
        Dropout (0.5),
        Activation ('relu'),

        Dense (512),
        Dropout (0.5),
        Activation ('relu'),

        Dense (256),
        Activation ('relu'),

        Dense (128),
        Activation ('relu'),

        Dense (32),
        Activation ('tanh'),

        Dense (1)
])

optimizer = Adam (lr=1e-4)

model.compile (
    optimizer=optimizer,
    loss='mse',
    metrics=[]
)

#training

#splitting into train and validation dataset
#dataset_images = driving_data.xs
#steering = driving_data.ys
train, valid = train_test_split ([dataset_images, steering], test_size=0.33)
train_dataset_images, train_steering = train
valid_dataset_images, valid_steering = valid

#train_dataset_images = driving_data.train_xs
#train_steering = driving_data.train_ys
#valid_dataset_images =  np.asarray(driving_data.val_xs)
#valid_steering = np.asarray(driving_data.val_ys)
X, y = driving_data.get_dataset(driving_data.open_image_gray)
train_dataset_images = X
train_steering = y
X, y = driving_data.get_validation_dataset(driving_data.open_image_gray)
valid_dataset_images = X
valid_steering = y

print (len(train_dataset_images), len (valid_dataset_images))

batch_size = 112
epochs = 30

class SaveModel(KerasCallback):
    def on_epoch_end(self, epoch, logs={}):
        epoch += 1
        if (epoch>4):
            with open ('model-' + str(epoch) + '.json', 'w') as file:
                file.write (model.to_json ())
                file.close ()

            model.save_weights ('model-' + str(epoch) + '.h5')

model.fit_generator (
    augment_generator(train_dataset_images, train_steering, batch_size),
    samples_per_epoch=400*112,
    nb_epoch=epochs,
    validation_data=(valid_dataset_images, valid_steering),
    callbacks = [SaveModel ()]
)

steering_test, center_camera_test = load_datasets (['record-test'])
images_test = load_images (center_camera_test)
loss = model.evaluate (images_test, steering_test)
print ('test loss: ', loss)


text:
using StackExchange.Opserver.Data.SQL;

namespace StackExchange.Opserver.Views.SQL
{
    public class DatabasesModel
    {
        public SQLInstance Instance { get; set; }
        public string Database { get; set; }
        public string ObjectName { get; set; }
        public Views View { get; set; }

        // TODO: Remove for extensibility
        public enum Views
        {
            Tables,
            Views,
            BlitzIndex,
            MissingIndexes,
            UnusedIndexes,
            Storage,
            Other
        }

        public static string GetDatabaseClass(SQLInstance.SQLDatabaseInfo db)
        {
            if (db.IsSystemDatabase) return "system";
            if (db.State == DatabaseStates.Restoring) return "restoring";

            return db.MonitorStatus.Class();
        }
    }
}

redpajama_set_name: RedPajamaGithub


text:
/**
 * @file ota_demo_core_mqtt.c
 * @brief OTA update example using coreMQTT.
 */

/* Standard includes. */
#include <assert.h>
#include <stdlib.h>
#include <stdbool.h>
#include <errno.h>

/* Include Demo Config as the first non-system header. */
#include "demo_config.h"

/* OpenSSL sockets transport implementation. */
#include "openssl_posix.h"

/* Clock for timer. */
#include "clock.h"

/* pthread include. */
#include <pthread.h>
#include <semaphore.h>

/* MQTT include. */
#include "core_mqtt.h"
#include "mqtt_subscription_manager.h"

/*Include backoff algorithm header for retry logic.*/
#include "backoff_algorithm.h"

/* OTA Library include. */
#include "ota.h"
#include "ota_config.h"

/* OTA Library Interface include. */
#include "ota_os_posix.h"
#include "ota_mqtt_interface.h"
#include "ota_pal_posix.h"

/* Include firmware version struct definition. */
#include "ota_appversion32.h"

/* AWS IoT Core TLS ALPN definitions for MQTT authentication */
#include "aws_iot_alpn_defs.h"

/**
 * These configuration settings are required to run the OTA demo which uses mutual authentication.
 * Throw compilation error if the below configs are not defined.
 */
#ifndef AWS_IOT_ENDPOINT
    #error "Please define AWS IoT MQTT broker endpoint(AWS_IOT_ENDPOINT) in demo_config.h."
#endif
#ifndef ROOT_CA_CERT_PATH
    #error "Please define path to Root CA certificate of the MQTT broker(ROOT_CA_CERT_PATH) in demo_config.h."
#endif
#ifndef CLIENT_IDENTIFIER
    #error "Please define a unique client identifier, CLIENT_IDENTIFIER, in demo_config.h."
#endif
#ifndef CLIENT_CERT_PATH
    #error "Please define path to client certificate(CLIENT_CERT_PATH) in demo_config.h."
#endif
#ifndef CLIENT_PRIVATE_KEY_PATH
    #error "Please define path to client private key(CLIENT_PRIVATE_KEY_PATH) in demo_config.h."
#endif

/**
 * @brief Length of MQTT server host name.
 */
#define AWS_IOT_ENDPOINT_LENGTH             ( ( uint16_t ) ( sizeof( AWS_IOT_ENDPOINT ) - 1 ) )

/**
 * @brief Length of client identifier.
 */
#define CLIENT_IDENTIFIER_LENGTH            ( ( uint16_t ) ( sizeof( CLIENT_IDENTIFIER ) - 1 ) )

/**
 * @brief Transport timeout in milliseconds for transport send and receive.
 */
#define TRANSPORT_SEND_RECV_TIMEOUT_MS      ( 1000U )

/**
 * @brief Timeout for receiving CONNACK packet in milli seconds.
 */
#define CONNACK_RECV_TIMEOUT_MS             ( 2000U )

/**
 * @brief The maximum time interval in seconds which is allowed to elapse
 * between two Control Packets.
 *
 * It is the responsibility of the Client to ensure that the interval between
 * Control Packets being sent does not exceed the this Keep Alive value. In the
 * absence of sending any other Control Packets, the Client MUST send a
 * PINGREQ Packet.
 */
#define MQTT_KEEP_ALIVE_INTERVAL_SECONDS    ( 60U )

/**
 * @brief Period for waiting on ack.
 */
#define MQTT_ACK_TIMEOUT_MS                 ( 5000U )

/**
 * @brief Period for demo loop sleep in milliseconds.
 */
#define OTA_EXAMPLE_LOOP_SLEEP_PERIOD_MS    ( 5U )

/**
 * @brief Size of the network buffer to receive the MQTT message.
 *
 * The largest message size is data size from the AWS IoT streaming service,
 * otaconfigFILE_BLOCK_SIZE + extra for headers.
 */

#define OTA_NETWORK_BUFFER_SIZE                  ( otaconfigFILE_BLOCK_SIZE + 128 )

/**
 * @brief The delay used in the main OTA Demo task loop to periodically output the OTA
 * statistics like number of packets received, dropped, processed and queued per connection.
 */
#define OTA_EXAMPLE_TASK_DELAY_MS                ( 1000U )

/**
 * @brief The timeout for waiting for the agent to get suspended after closing the
 * connection.
 */
#define OTA_SUSPEND_TIMEOUT_MS                   ( 5000U )

/**
 * @brief The timeout for waiting before exiting the OTA demo.
 */
#define OTA_DEMO_EXIT_TIMEOUT_MS                 ( 3000U )

/**
 * @brief The maximum size of the file paths used in the demo.
 */
#define OTA_MAX_FILE_PATH_SIZE                   ( 260U )

/**
 * @brief The maximum size of the stream name required for downloading update file
 * from streaming service.
 */
#define OTA_MAX_STREAM_NAME_SIZE                 ( 128U )

/**
 * @brief The maximum back-off delay (in milliseconds) for retrying connection to server.
 */
#define CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS    ( 5000U )

/**
 * @brief The base back-off delay (in milliseconds) to use for connection retry attempts.
 */
#define CONNECTION_RETRY_BACKOFF_BASE_MS         ( 500U )

/**
 * @brief Number of milliseconds in a second.
 */
#define NUM_MILLISECONDS_IN_SECOND               ( 1000U )

/**
 * @brief The maximum number of retries for connecting to server.
 */
#define CONNECTION_RETRY_MAX_ATTEMPTS            ( 5U )

/**
 * @brief The MQTT metrics string expected by AWS IoT.
 */
#define METRICS_STRING                           "?SDK=" OS_NAME "&Version=" OS_VERSION "&Platform=" HARDWARE_PLATFORM_NAME "&OTALib=" OTA_LIB

/**
 * @brief The length of the MQTT metrics string expected by AWS IoT.
 */
#define METRICS_STRING_LENGTH                    ( ( uint16_t ) ( sizeof( METRICS_STRING ) - 1 ) )

#ifdef CLIENT_USERNAME

/**
 * @brief Append the username with the metrics string if #CLIENT_USERNAME is defined.
 *
 * This is to support both metrics reporting and username/password based client
 * authentication by AWS IoT.
 */
    #define CLIENT_USERNAME_WITH_METRICS    CLIENT_USERNAME METRICS_STRING
#endif

/**
 * @brief The common prefix for all OTA topics.
 */
#define OTA_TOPIC_PREFIX               "$aws/things/+/"

/**
 * @brief The string used for jobs topics.
 */
#define OTA_TOPIC_JOBS                 "jobs"

/**
 * @brief The string used for streaming service topics.
 */
#define OTA_TOPIC_STREAM               "streams"

/**
 * @brief The length of the outgoing publish records array used by the coreMQTT
 * library to track QoS > 0 packet ACKS for outgoing publishes.
 */
#define OUTGOING_PUBLISH_RECORD_LEN    ( 10U )

/**
 * @brief The length of the incoming publish records array used by the coreMQTT
 * library to track QoS > 0 packet ACKS for incoming publishes.
 */
#define INCOMING_PUBLISH_RECORD_LEN    ( 10U )

/*-----------------------------------------------------------*/

/* Linkage for error reporting. */
extern int errno;

/**
 * @brief Struct for firmware version.
 */
const AppVersion32_t appFirmwareVersion =
{
    .u.x.major = APP_VERSION_MAJOR,
    .u.x.minor = APP_VERSION_MINOR,
    .u.x.build = APP_VERSION_BUILD,
};

/* Each compilation unit must define the NetworkContext struct. */
struct NetworkContext
{
    OpensslParams_t * pParams;
};

/**
 * @brief Network connection context used in this demo.
 */
static NetworkContext_t networkContext;

/**
 * @brief MQTT connection context used in this demo.
 */
static MQTTContext_t mqttContext;

/**
 * @brief Keep a flag for indicating if the MQTT connection is alive.
 */
static bool mqttSessionEstablished = false;

/**
 * @brief Structure for openssl parameters.
 */
static OpensslParams_t opensslParams;

/**
 * @brief Mutex for synchronizing coreMQTT API calls.
 */
static pthread_mutex_t mqttMutex;

/**
 * @brief Semaphore for synchronizing buffer operations.
 */
static sem_t bufferSemaphore;

/**
 * @brief Semaphore for synchronizing wait for ack.
 */
static sem_t ackSemaphore;

/**
 * @brief Enum for type of OTA job messages received.
 */
typedef enum jobMessageType
{
    jobMessageTypeNextGetAccepted = 0,
    jobMessageTypeNextNotify,
    jobMessageTypeMax
} jobMessageType_t;

/**
 * @brief The network buffer must remain valid when OTA library task is running.
 */
static uint8_t otaNetworkBuffer[ OTA_NETWORK_BUFFER_SIZE ];

/**
 * @brief Update File path buffer.
 */
uint8_t updateFilePath[ OTA_MAX_FILE_PATH_SIZE ];

/**
 * @brief Certificate File path buffer.
 */
uint8_t certFilePath[ OTA_MAX_FILE_PATH_SIZE ];

/**
 * @brief Stream name buffer.
 */
uint8_t streamName[ OTA_MAX_STREAM_NAME_SIZE ];

/**
 * @brief Decode memory.
 */
uint8_t decodeMem[ otaconfigFILE_BLOCK_SIZE ];

/**
 * @brief Bitmap memory.
 */
uint8_t bitmap[ OTA_MAX_BLOCK_BITMAP_SIZE ];

/**
 * @brief Event buffer.
 */
static OtaEventData_t eventBuffer[ otaconfigMAX_NUM_OTA_DATA_BUFFERS ];

/**
 * @brief The buffer passed to the OTA Agent from application while initializing.
 */
static OtaAppBuffer_t otaBuffer =
{
    .pUpdateFilePath    = updateFilePath,
    .updateFilePathsize = OTA_MAX_FILE_PATH_SIZE,
    .pCertFilePath      = certFilePath,
    .certFilePathSize   = OTA_MAX_FILE_PATH_SIZE,
    .pStreamName        = streamName,
    .streamNameSize     = OTA_MAX_STREAM_NAME_SIZE,
    .pDecodeMemory      = decodeMem,
    .decodeMemorySize   = otaconfigFILE_BLOCK_SIZE,
    .pFileBitmap        = bitmap,
    .fileBitmapSize     = OTA_MAX_BLOCK_BITMAP_SIZE
};

/**
 * @brief Array to track the outgoing publish records for outgoing publishes
 * with QoS > 0.
 *
 * This is passed into #MQTT_InitStatefulQoS to allow for QoS > 0.
 *
 */
static MQTTPubAckInfo_t pOutgoingPublishRecords[ OUTGOING_PUBLISH_RECORD_LEN ];

/**
 * @brief Array to track the incoming publish records for incoming publishes
 * with QoS > 0.
 *
 * This is passed into #MQTT_InitStatefulQoS to allow for QoS > 0.
 *
 */
static MQTTPubAckInfo_t pIncomingPublishRecords[ INCOMING_PUBLISH_RECORD_LEN ];

/*-----------------------------------------------------------*/

/**
 * @brief Sends an MQTT CONNECT packet over the already connected TCP socket.
 *
 * @param[in] pMqttContext MQTT context pointer.
 * @param[in] createCleanSession Creates a new MQTT session if true.
 * If false, tries to establish the existing session if there was session
 * already present in broker.
 * @param[out] pSessionPresent Session was already present in the broker or not.
 * Session present response is obtained from the CONNACK from broker.
 *
 * @return EXIT_SUCCESS if an MQTT session is established;
 * EXIT_FAILURE otherwise.
 */
static int establishMqttSession( MQTTContext_t * pMqttContext );

/**
 * @brief Publish message to a topic.
 *
 * This function publishes a message to a given topic & QoS.
 *
 * @param[in] pacTopic Mqtt topic filter.
 *
 * @param[in] topicLen Length of the topic filter.
 *
 * @param[in] pMsg Message to publish.
 *
 * @param[in] msgSize Message size.
 *
 * @param[in] qos Quality of Service
 *
 * @return OtaMqttSuccess if success , other error code on failure.
 */
static OtaMqttStatus_t mqttPublish( const char * const pacTopic,
                                    uint16_t topicLen,
                                    const char * pMsg,
                                    uint32_t msgSize,
                                    uint8_t qos );

/**
 * @brief Subscribe to the MQTT topic filter, and registers the handler for the topic filter with the subscription manager.
 *
 * This function subscribes to the Mqtt topics with the Quality of service
 * received as parameter. This function also registers a callback for the
 * topicfilter.
 *
 * @param[in] pTopicFilter Mqtt topic filter.
 *
 * @param[in] topicFilterLength Length of the topic filter.
 *
 * @param[in] qos Quality of Service
 *
 * @return OtaMqttSuccess if success , other error code on failure.
 */
static OtaMqttStatus_t mqttSubscribe( const char * pTopicFilter,
                                      uint16_t topicFilterLength,
                                      uint8_t qos );

/**
 * @brief Unsubscribe to the Mqtt topics.
 *
 * This function unsubscribes to the Mqtt topics with the Quality of service
 * received as parameter.
 *
 * @param[in] pTopicFilter Mqtt topic filter.
 *
 * @param[in] topicFilterLength Length of the topic filter.
 *
 * @param[qos] qos Quality of Service
 *
 * @return  OtaMqttSuccess if success , other error code on failure.
 */
static OtaMqttStatus_t mqttUnsubscribe( const char * pTopicFilter,
                                        uint16_t topicFilterLength,
                                        uint8_t qos );

/**
 * @brief Thread to call the OTA agent task.
 *
 * @param[in] pParam Can be used to pass down functionality to the agent task
 * @return void* returning null.
 */
static void * otaThread( void * pParam );

/**
 * @brief Start OTA demo.
 *
 * The OTA task is created with initializing the OTA agent and
 * setting the required interfaces. The demo loop then starts,
 * establishing an MQTT connection with the broker and waiting
 * for an update. After a successful update the OTA agent requests
 * a manual reset to the downloaded executable.
 *
 * @return EXIT_SUCCESS or EXIT_FAILURE.
 */
static int startOTADemo( void );

/**
 * @brief Set OTA interfaces.
 *
 * @param[in]  pOtaInterfaces pointer to OTA interface structure.
 */
static void setOtaInterfaces( OtaInterfaces_t * pOtaInterfaces );

/**
 * @brief Disconnect from the MQTT broker and close connection.
 *
 */
static void disconnect( void );

/**
 * @brief Attempt to connect to the MQTT broker.
 *
 * @return int EXIT_SUCCESS if a connection is established.
 */
static int establishConnection( void );

/**
 * @brief Initialize MQTT by setting up transport interface and network.
 *
 * @param[in] pMqttContext Structure representing MQTT connection.
 * @param[in] pNetworkContext Network context to connect on.
 * @return int EXIT_SUCCESS if MQTT component is initialized
 */
static int initializeMqtt( MQTTContext_t * pMqttContext,
                           NetworkContext_t * pNetworkContext );

/**
 * @brief Retry logic to establish a connection to the server.
 *
 * If the connection fails, keep retrying with exponentially increasing
 * timeout value, until max retries, max timeout or successful connect.
 *
 * @param[in] pNetworkContext Network context to connect on.
 * @return int EXIT_FAILURE if connection failed after retries.
 */
static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext );

/**
 * @brief Random number to be used as a back-off value for retrying connection.
 *
 * @return uint32_t The generated random number.
 */
static uint32_t generateRandomNumber();

/* Callbacks used to handle different events. */

/**
 * @brief The OTA agent has completed the update job or it is in
 * self test mode. If it was accepted, we want to activate the new image.
 * This typically means we should reset the device to run the new firmware.
 * If now is not a good time to reset the device, it may be activated later
 * by your user code. If the update was rejected, just return without doing
 * anything and we'll wait for another job. If it reported that we should
 * start test mode, normally we would perform some kind of system checks to
 * make sure our new firmware does the basic things we think it should do
 * but we'll just go ahead and set the image as accepted for demo purposes.
 * The accept function varies depending on your platform. Refer to the OTA
 * PAL implementation for your platform in aws_ota_pal.c to see what it
 * does for you.
 *
 * @param[in] event Event from OTA lib of type OtaJobEvent_t.
 * @return None.
 */
static void otaAppCallback( OtaJobEvent_t event,
                            void * pData );

/**
 * @brief callback to use with the MQTT context to notify incoming packet events.
 *
 * @param[in] pMqttContext MQTT context which stores the connection.
 * @param[in] pPacketInfo Parameters of the incoming packet.
 * @param[in] pDeserializedInfo Deserialized packet information to be dispatched by
 * the subscription manager to event callbacks.
 */
static void mqttEventCallback( MQTTContext_t * pMqttContext,
                               MQTTPacketInfo_t * pPacketInfo,
                               MQTTDeserializedInfo_t * pDeserializedInfo );

/**
 * @brief Callback registered with the OTA library that notifies the OTA agent
 * of an incoming PUBLISH containing a job document.
 *
 * @param[in] pContext MQTT context which stores the connection.
 * @param[in] pPublishInfo MQTT packet information which stores details of the
 * job document.
 */
static void mqttJobCallback( MQTTContext_t * pContext,
                             MQTTPublishInfo_t * pPublishInfo );

/**
 * @brief Callback that notifies the OTA library when a data block is received.
 *
 * @param[in] pContext MQTT context which stores the connection.
 * @param[in] pPublishInfo MQTT packet that stores the information of the file block.
 */
static void mqttDataCallback( MQTTContext_t * pContext,
                              MQTTPublishInfo_t * pPublishInfo );

static SubscriptionManagerCallback_t otaMessageCallback[] = { mqttJobCallback, mqttDataCallback };

/*-----------------------------------------------------------*/

void otaEventBufferFree( OtaEventData_t * const pxBuffer )
{
    if( sem_wait( &bufferSemaphore ) == 0 )
    {
        pxBuffer->bufferUsed = false;
        ( void ) sem_post( &bufferSemaphore );
    }
    else
    {
        LogError( ( "Failed to get buffer semaphore: "
                    ",errno=%s",
                    strerror( errno ) ) );
    }
}

/*-----------------------------------------------------------*/

OtaEventData_t * otaEventBufferGet( void )
{
    uint32_t ulIndex = 0;
    OtaEventData_t * pFreeBuffer = NULL;

    if( sem_wait( &bufferSemaphore ) == 0 )
    {
        for( ulIndex = 0; ulIndex < otaconfigMAX_NUM_OTA_DATA_BUFFERS; ulIndex++ )
        {
            if( eventBuffer[ ulIndex ].bufferUsed == false )
            {
                eventBuffer[ ulIndex ].bufferUsed = true;
                pFreeBuffer = &eventBuffer[ ulIndex ];
                break;
            }
        }

        ( void ) sem_post( &bufferSemaphore );
    }
    else
    {
        LogError( ( "Failed to get buffer semaphore: "
                    ",errno=%s",
                    strerror( errno ) ) );
    }

    return pFreeBuffer;
}

/*-----------------------------------------------------------*/

static void otaAppCallback( OtaJobEvent_t event,
                            void * pData )
{
    OtaErr_t err = OtaErrUninitialized;

    switch( event )
    {
        case OtaJobEventActivate:
            LogInfo( ( "Received OtaJobEventActivate callback from OTA Agent." ) );

            /* Activate the new firmware image. */
            OTA_ActivateNewImage();

            /* Shutdown OTA Agent, if it is required that the unsubscribe operations are not
             * performed while shutting down please set the second parameter to 0 instead of 1. */
            OTA_Shutdown( 0, 1 );

            /* Requires manual activation of new image.*/
            LogError( ( "New image activation failed." ) );

            break;

        case OtaJobEventFail:
            LogInfo( ( "Received OtaJobEventFail callback from OTA Agent." ) );

            /* Nothing special to do. The OTA agent handles it. */
            break;

        case OtaJobEventStartTest:

            /* This demo just accepts the image since it was a good OTA update and networking
             * and services are all working (or we would not have made it this far). If this
             * were some custom device that wants to test other things before validating new
             * image, this would be the place to kick off those tests before calling
             * OTA_SetImageState() with the final result of either accepted or rejected. */

            LogInfo( ( "Received OtaJobEventStartTest callback from OTA Agent." ) );
            err = OTA_SetImageState( OtaImageStateAccepted );

            if( err != OtaErrNone )
            {
                LogError( ( " Failed to set image state as accepted." ) );
            }

            break;

        case OtaJobEventProcessed:
            LogDebug( ( "Received OtaJobEventProcessed callback from OTA Agent." ) );

            if( pData != NULL )
            {
                otaEventBufferFree( ( OtaEventData_t * ) pData );
            }

            break;

        case OtaJobEventSelfTestFailed:
            LogDebug( ( "Received OtaJobEventSelfTestFailed callback from OTA Agent." ) );

            /* Requires manual activation of previous image as self-test for
             * new image downloaded failed.*/
            LogError( ( "Self-test failed, shutting down OTA Agent." ) );

            /* Shutdown OTA Agent, if it is required that the unsubscribe operations are not
             * performed while shutting down please set the second parameter to 0 instead of 1. */
            OTA_Shutdown( 0, 1 );

            break;

        default:
            LogDebug( ( "Received invalid callback event from OTA Agent." ) );
    }
}

jobMessageType_t getJobMessageType( const char * pTopicName,
                                    uint16_t topicNameLength )
{
    uint16_t index = 0U;
    MQTTStatus_t mqttStatus = MQTTSuccess;
    bool isMatch = false;
    jobMessageType_t jobMessageIndex = jobMessageTypeMax;

    /* For suppressing compiler-warning: unused variable. */
    ( void ) mqttStatus;

    /* Lookup table for OTA job message string. */
    static const char * const pJobTopicFilters[ jobMessageTypeMax ] =
    {
        OTA_TOPIC_PREFIX OTA_TOPIC_JOBS "/$next/get/accepted",
        OTA_TOPIC_PREFIX OTA_TOPIC_JOBS "/notify-next",
    };

    /* Match the input topic filter against the wild-card pattern of topics filters
    * relevant for the OTA Update service to determine the type of topic filter. */
    for( ; index < jobMessageTypeMax; index++ )
    {
        mqttStatus = MQTT_MatchTopic( pTopicName,
                                      topicNameLength,
                                      pJobTopicFilters[ index ],
                                      strlen( pJobTopicFilters[ index ] ),
                                      &isMatch );
        assert( mqttStatus == MQTTSuccess );

        if( isMatch )
        {
            jobMessageIndex = index;
            break;
        }
    }

    return jobMessageIndex;
}

/*-----------------------------------------------------------*/

static void mqttJobCallback( MQTTContext_t * pContext,
                             MQTTPublishInfo_t * pPublishInfo )
{
    OtaEventData_t * pData;
    OtaEventMsg_t eventMsg = { 0 };
    jobMessageType_t jobMessageType = 0;

    assert( pPublishInfo != NULL );
    assert( pContext != NULL );

    ( void ) pContext;

    jobMessageType = getJobMessageType( pPublishInfo->pTopicName, pPublishInfo->topicNameLength );

    switch( jobMessageType )
    {
        case jobMessageTypeNextGetAccepted:
        case jobMessageTypeNextNotify:

            pData = otaEventBufferGet();

            if( pData != NULL )
            {
                memcpy( pData->data, pPublishInfo->pPayload, pPublishInfo->payloadLength );
                pData->dataLength = pPublishInfo->payloadLength;
                eventMsg.eventId = OtaAgentEventReceivedJobDocument;
                eventMsg.pEventData = pData;

                /* Send job document received event. */
                OTA_SignalEvent( &eventMsg );
            }
            else
            {
                LogError( ( "No OTA data buffers available." ) );
            }

            break;

        default:
            LogInfo( ( "Received job message %s size %ld.\n\n",
                       pPublishInfo->pTopicName,
                       pPublishInfo->payloadLength ) );
    }
}

/*-----------------------------------------------------------*/

static void mqttDataCallback( MQTTContext_t * pContext,
                              MQTTPublishInfo_t * pPublishInfo )
{
    OtaEventData_t * pData;
    OtaEventMsg_t eventMsg = { 0 };

    assert( pPublishInfo != NULL );
    assert( pContext != NULL );

    ( void ) pContext;

    LogInfo( ( "Received data message callback, size %zu.\n\n", pPublishInfo->payloadLength ) );

    pData = otaEventBufferGet();

    if( pData != NULL )
    {
        memcpy( pData->data, pPublishInfo->pPayload, pPublishInfo->payloadLength );
        pData->dataLength = pPublishInfo->payloadLength;
        eventMsg.eventId = OtaAgentEventReceivedFileBlock;
        eventMsg.pEventData = pData;

        /* Send job document received event. */
        OTA_SignalEvent( &eventMsg );
    }
    else
    {
        LogError( ( "No OTA data buffers available." ) );
    }
}

/*-----------------------------------------------------------*/

static void mqttEventCallback( MQTTContext_t * pMqttContext,
                               MQTTPacketInfo_t * pPacketInfo,
                               MQTTDeserializedInfo_t * pDeserializedInfo )
{
    assert( pMqttContext != NULL );
    assert( pPacketInfo != NULL );
    assert( pDeserializedInfo != NULL );

    /* Handle incoming publish. The lower 4 bits of the publish packet
     * type is used for the dup, QoS, and retain flags. Hence masking
     * out the lower bits to check if the packet is publish. */
    if( ( pPacketInfo->type & 0xF0U ) == MQTT_PACKET_TYPE_PUBLISH )
    {
        assert( pDeserializedInfo->pPublishInfo != NULL );
        /* Handle incoming publish. */
        SubscriptionManager_DispatchHandler( pMqttContext, pDeserializedInfo->pPublishInfo );
    }
    else
    {
        /* Handle other packets. */
        switch( pPacketInfo->type )
        {
            case MQTT_PACKET_TYPE_SUBACK:
                LogInfo( ( "Received SUBACK.\n\n" ) );
                break;

            case MQTT_PACKET_TYPE_UNSUBACK:
                LogInfo( ( "Received UNSUBACK.\n\n" ) );
                break;

            case MQTT_PACKET_TYPE_PINGRESP:

                /* Nothing to be done from application as library handles
                 * PINGRESP. */
                LogWarn( ( "PINGRESP should not be handled by the application "
                           "callback when using MQTT_ProcessLoop.\n\n" ) );
                break;

            case MQTT_PACKET_TYPE_PUBACK:
                LogInfo( ( "PUBACK received for packet id %u.\n\n",
                           pDeserializedInfo->packetIdentifier ) );
                sem_post( &ackSemaphore );
                break;

            /* Any other packet type is invalid. */
            default:
                LogError( ( "Unknown packet type received:(%02x).\n\n",
                            pPacketInfo->type ) );
        }
    }
}

/*-----------------------------------------------------------*/

static uint32_t generateRandomNumber()
{
    return( rand() );
}

/*-----------------------------------------------------------*/

static int initializeMqtt( MQTTContext_t * pMqttContext,
                           NetworkContext_t * pNetworkContext )
{
    int returnStatus = EXIT_SUCCESS;
    MQTTStatus_t mqttStatus = MQTTBadParameter;
    MQTTFixedBuffer_t networkBuffer;
    TransportInterface_t transport;

    assert( pMqttContext != NULL );
    assert( pNetworkContext != NULL );

    /* Fill in TransportInterface send and receive function pointers.
     * For this demo, TCP sockets are used to send and receive data
     * from network.  TLS over TCP channel is used as the transport
     * layer for the MQTT connection. Network context is SSL context
     * for OpenSSL.*/
    transport.pNetworkContext = pNetworkContext;
    transport.send = Openssl_Send;
    transport.recv = Openssl_Recv;
    transport.writev = NULL;

    /* Fill the values for network buffer. */
    networkBuffer.pBuffer = otaNetworkBuffer;
    networkBuffer.size = OTA_NETWORK_BUFFER_SIZE;

    /* Initialize MQTT library. */
    mqttStatus = MQTT_Init( pMqttContext,
                            &transport,
                            Clock_GetTimeMs,
                            mqttEventCallback,
                            &networkBuffer );

    if( mqttStatus != MQTTSuccess )
    {
        returnStatus = EXIT_FAILURE;
        LogError( ( "MQTT_Init failed: Status = %s.", MQTT_Status_strerror( mqttStatus ) ) );
    }
    else
    {
        mqttStatus = MQTT_InitStatefulQoS( pMqttContext,
                                           pOutgoingPublishRecords,
                                           OUTGOING_PUBLISH_RECORD_LEN,
                                           pIncomingPublishRecords,
                                           INCOMING_PUBLISH_RECORD_LEN );

        if( mqttStatus != MQTTSuccess )
        {
            returnStatus = EXIT_FAILURE;
            LogError( ( "MQTT_InitStatefulQoS failed: Status = %s.", MQTT_Status_strerror( mqttStatus ) ) );
        }
    }

    return returnStatus;
}

/*-----------------------------------------------------------*/

static int connectToServerWithBackoffRetries( NetworkContext_t * pNetworkContext )
{
    int returnStatus = EXIT_SUCCESS;
    BackoffAlgorithmStatus_t backoffAlgStatus = BackoffAlgorithmSuccess;
    OpensslStatus_t opensslStatus = OPENSSL_SUCCESS;
    BackoffAlgorithmContext_t reconnectParams;
    ServerInfo_t serverInfo;
    OpensslCredentials_t opensslCredentials;
    uint16_t nextRetryBackOff;

    /* Initialize information to connect to the MQTT broker. */
    serverInfo.pHostName = AWS_IOT_ENDPOINT;
    serverInfo.hostNameLength = AWS_IOT_ENDPOINT_LENGTH;
    serverInfo.port = AWS_MQTT_PORT;

    /* Initialize credentials for establishing TLS session. */
    memset( &opensslCredentials, 0, sizeof( OpensslCredentials_t ) );
    opensslCredentials.pRootCaPath = ROOT_CA_CERT_PATH;

    /* If #CLIENT_USERNAME is defined, username/password is used for authenticating
     * the client. */
    #ifndef CLIENT_USERNAME
        opensslCredentials.pClientCertPath = CLIENT_CERT_PATH;
        opensslCredentials.pPrivateKeyPath = CLIENT_PRIVATE_KEY_PATH;
    #endif

    /* AWS IoT requires devices to send the Server Name Indication (SNI)
     * extension to the Transport Layer Security (TLS) protocol and provide
     * the complete endpoint address in the host_name field. Details about
     * SNI for AWS IoT can be found in the link below.
     * https://docs.aws.amazon.com/iot/latest/developerguide/transport-security.html */
    opensslCredentials.sniHostName = AWS_IOT_ENDPOINT;

    if( AWS_MQTT_PORT == 443 )
    {
        /* Pass the ALPN protocol name depending on the port being used.
         * Please see more details about the ALPN protocol for the AWS IoT MQTT
         * endpoint in the link below.
         * https://aws.amazon.com/blogs/iot/mqtt-with-tls-client-authentication-on-port-443-why-it-is-useful-and-how-it-works/
         *
         * For username and password based authentication in AWS IoT,
         * #AWS_IOT_PASSWORD_ALPN is used. More details can be found in the
         * link below.
         * https://docs.aws.amazon.com/iot/latest/developerguide/custom-authentication.html
         */
        #ifdef CLIENT_USERNAME
            opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_MQTT_CUSTOM_AUTH_OPENSSL;
            opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CUSTOM_AUTH_OPENSSL_LEN;
        #else
            opensslCredentials.pAlpnProtos = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL;
            opensslCredentials.alpnProtosLen = AWS_IOT_ALPN_MQTT_CA_AUTH_OPENSSL_LEN;
        #endif
    }

    /* Initialize reconnect attempts and interval */
    BackoffAlgorithm_InitializeParams( &reconnectParams,
                                       CONNECTION_RETRY_BACKOFF_BASE_MS,
                                       CONNECTION_RETRY_MAX_BACKOFF_DELAY_MS,
                                       CONNECTION_RETRY_MAX_ATTEMPTS );

    /* Attempt to connect to MQTT broker. If connection fails, retry after
     * a timeout. Timeout value will exponentially increase until maximum
     * attempts are reached.
     */
    do
    {
        /* Establish a TLS session with the MQTT broker. This example connects
         * to the MQTT broker as specified in AWS_IOT_ENDPOINT and AWS_MQTT_PORT
         * at the demo config header. */
        LogInfo( ( "Establishing a TLS session to %.*s:%d.",
                   AWS_IOT_ENDPOINT_LENGTH,
                   AWS_IOT_ENDPOINT,
                   AWS_MQTT_PORT ) );
        opensslStatus = Openssl_Connect( pNetworkContext,
                                         &serverInfo,
                                         &opensslCredentials,
                                         TRANSPORT_SEND_RECV_TIMEOUT_MS,
                                         TRANSPORT_SEND_RECV_TIMEOUT_MS );

        if( opensslStatus != OPENSSL_SUCCESS )
        {
            /* Generate a random number and get back-off value (in milliseconds) for the next connection retry. */
            backoffAlgStatus = BackoffAlgorithm_GetNextBackoff( &reconnectParams, generateRandomNumber(), &nextRetryBackOff );

            if( backoffAlgStatus == BackoffAlgorithmRetriesExhausted )
            {
                LogError( ( "Connection to the broker failed, all attempts exhausted." ) );
                returnStatus = EXIT_FAILURE;
            }
            else if( backoffAlgStatus == BackoffAlgorithmSuccess )
            {
                LogWarn( ( "Connection to the broker failed. Retrying connection "
                           "after %hu ms backoff.",
                           ( unsigned short ) nextRetryBackOff ) );
                Clock_SleepMs( nextRetryBackOff );
            }
        }
    } while( ( opensslStatus != OPENSSL_SUCCESS ) && ( backoffAlgStatus == BackoffAlgorithmSuccess ) );

    return returnStatus;
}

/*-----------------------------------------------------------*/

static int establishMqttSession( MQTTContext_t * pMqttContext )
{
    int returnStatus = EXIT_SUCCESS;
    MQTTStatus_t mqttStatus = MQTTBadParameter;
    MQTTConnectInfo_t connectInfo = { 0 };

    bool sessionPresent = false;

    assert( pMqttContext != NULL );

    /* Establish MQTT session by sending a CONNECT packet. */

    /* If #createCleanSession is true, start with a clean session
     * i.e. direct the MQTT broker to discard any previous session data.
     * If #createCleanSession is false, directs the broker to attempt to
     * reestablish a session which was already present. */
    connectInfo.cleanSession = true;

    /* The client identifier is used to uniquely identify this MQTT client to
     * the MQTT broker. In a production device the identifier can be something
     * unique, such as a device serial number. */
    connectInfo.pClientIdentifier = CLIENT_IDENTIFIER;
    connectInfo.clientIdentifierLength = CLIENT_IDENTIFIER_LENGTH;

    /* The maximum time interval in seconds which is allowed to elapse
     * between two Control Packets.
     * It is the responsibility of the Client to ensure that the interval between
     * Control Packets being sent does not exceed the this Keep Alive value. In the
     * absence of sending any other Control Packets, the Client MUST send a
     * PINGREQ Packet. */
    connectInfo.keepAliveSeconds = MQTT_KEEP_ALIVE_INTERVAL_SECONDS;

    /* Use the username and password for authentication, if they are defined.
     * Refer to the AWS IoT documentation below for details regarding client
     * authentication with a username and password.
     * https://docs.aws.amazon.com/iot/latest/developerguide/custom-authentication.html
     * An authorizer setup needs to be done, as mentioned in the above link, to use
     * username/password based client authentication.
     *
     * The username field is populated with voluntary metrics to AWS IoT.
     * The metrics collected by AWS IoT are the operating system, the operating
     * system's version, the hardware platform, and the MQTT Client library
     * information. These metrics help AWS IoT improve security and provide
     * better technical support.
     *
     * If client authentication is based on username/password in AWS IoT,
     * the metrics string is appended to the username to support both client
     * authentication and metrics collection. */
    #ifdef CLIENT_USERNAME
        connectInfo.pUserName = CLIENT_USERNAME_WITH_METRICS;
        connectInfo.userNameLength = strlen( CLIENT_USERNAME_WITH_METRICS );
        connectInfo.pPassword = CLIENT_PASSWORD;
        connectInfo.passwordLength = strlen( CLIENT_PASSWORD );
    #else
        connectInfo.pUserName = METRICS_STRING;
        connectInfo.userNameLength = METRICS_STRING_LENGTH;
        /* Password for authentication is not used. */
        connectInfo.pPassword = NULL;
        connectInfo.passwordLength = 0U;
    #endif /* ifdef CLIENT_USERNAME */

    if( pthread_mutex_lock( &mqttMutex ) == 0 )
    {
        /* Send MQTT CONNECT packet to broker. */
        mqttStatus = MQTT_Connect( pMqttContext, &connectInfo, NULL, CONNACK_RECV_TIMEOUT_MS, &sessionPresent );

        pthread_mutex_unlock( &mqttMutex );
    }
    else
    {
        LogError( ( "Failed to acquire mutex for executing MQTT_Connect"
                    ",errno=%s",
                    strerror( errno ) ) );
    }

    if( mqttStatus != MQTTSuccess )
    {
        returnStatus = EXIT_FAILURE;
        LogError( ( "Connection with MQTT broker failed with status %s.",
                    MQTT_Status_strerror( mqttStatus ) ) );
    }
    else
    {
        LogInfo( ( "MQTT connection successfully established with broker.\n\n" ) );
    }

    return returnStatus;
}

/*-----------------------------------------------------------*/

static int establishConnection( void )
{
    int returnStatus = EXIT_FAILURE;

    /* Set the pParams member of the network context with desired transport. */
    networkContext.pParams = &opensslParams;

    /* Attempt to connect to the MQTT broker. If connection fails, retry after
     * a timeout. Timeout value will be exponentially increased till the maximum
     * attempts are reached or maximum timeout value is reached. The function
     * returns EXIT_FAILURE if the TCP connection cannot be established to
     * broker after configured number of attempts. */
    returnStatus = connectToServerWithBackoffRetries( &networkContext );

    if( returnStatus != EXIT_SUCCESS )
    {
        /* Log error to indicate connection failure. */
        LogError( ( "Failed to connect to MQTT broker %.*s.",
                    AWS_IOT_ENDPOINT_LENGTH,
                    AWS_IOT_ENDPOINT ) );
    }
    else
    {
        /* Establish MQTT session on top of TCP+TLS connection. */
        LogInfo( ( "Creating an MQTT connection to %.*s.",
                   AWS_IOT_ENDPOINT_LENGTH,
                   AWS_IOT_ENDPOINT ) );

        /* Sends an MQTT Connect packet using the established TLS session,
         * then waits for connection acknowledgment (CONNACK) packet. */
        returnStatus = establishMqttSession( &mqttContext );

        if( returnStatus != EXIT_SUCCESS )
        {
            LogError( ( "Failed creating an MQTT connection to %.*s.",
                        AWS_IOT_ENDPOINT_LENGTH,
                        AWS_IOT_ENDPOINT ) );
        }
        else
        {
            LogDebug( ( "Success creating MQTT connection to %.*s.",
                        AWS_IOT_ENDPOINT_LENGTH,
                        AWS_IOT_ENDPOINT ) );

            mqttSessionEstablished = true;
        }
    }

    return returnStatus;
}

/*-----------------------------------------------------------*/

static void disconnect( void )
{
    /* Disconnect from broker. */
    LogInfo( ( "Disconnecting the MQTT connection with %s.", AWS_IOT_ENDPOINT ) );

    if( mqttSessionEstablished == true )
    {
        if( pthread_mutex_lock( &mqttMutex ) == 0 )
        {
            /* Disconnect MQTT session. */
            MQTT_Disconnect( &mqttContext );

            /* Clear the mqtt session flag. */
            mqttSessionEstablished = false;

            pthread_mutex_unlock( &mqttMutex );
        }
        else
        {
            LogError( ( "Failed to acquire mutex to execute MQTT_Disconnect"
                        ",errno=%s",
                        strerror( errno ) ) );
        }
    }
    else
    {
        LogError( ( "MQTT already disconnected." ) );
    }

    /* End TLS session, then close TCP connection. */
    ( void ) Openssl_Disconnect( &networkContext );
}

/*-----------------------------------------------------------*/

static void registerSubscriptionManagerCallback( const char * pTopicFilter,
                                                 uint16_t topicFilterLength )
{
    bool isMatch = false;
    MQTTStatus_t mqttStatus = MQTTSuccess;
    SubscriptionManagerStatus_t subscriptionStatus = SUBSCRIPTION_MANAGER_SUCCESS;

    uint16_t index = 0U;

    /* For suppressing compiler-warning: unused variable. */
    ( void ) mqttStatus;

    /* Lookup table for OTA message string. */
    static const char * const pWildCardTopicFilters[] =
    {
        OTA_TOPIC_PREFIX OTA_TOPIC_JOBS "/#",
        OTA_TOPIC_PREFIX OTA_TOPIC_STREAM "/#"
    };

    /* Match the input topic filter against the wild-card pattern of topics filters
    * relevant for the OTA Update service to determine the type of topic filter. */
    for( ; index < 2; index++ )
    {
        mqttStatus = MQTT_MatchTopic( pTopicFilter,
                                      topicFilterLength,
                                      pWildCardTopicFilters[ index ],
                                      strlen( pWildCardTopicFilters[ index ] ),
                                      &isMatch );
        assert( mqttStatus == MQTTSuccess );

        if( isMatch )
        {
            /* Register callback to subscription manager. */
            subscriptionStatus = SubscriptionManager_RegisterCallback( pWildCardTopicFilters[ index ],
                                                                       strlen( pWildCardTopicFilters[ index ] ),
                                                                       otaMessageCallback[ index ] );

            if( subscriptionStatus != SUBSCRIPTION_MANAGER_SUCCESS )
            {
                LogWarn( ( "Failed to register a callback to subscription manager with error = %d.",
                           subscriptionStatus ) );
            }

            break;
        }
    }
}

/*-----------------------------------------------------------*/

static OtaMqttStatus_t mqttSubscribe( const char * pTopicFilter,
                                      uint16_t topicFilterLength,
                                      uint8_t qos )
{
    OtaMqttStatus_t otaRet = OtaMqttSuccess;

    MQTTStatus_t mqttStatus = MQTTBadParameter;
    MQTTContext_t * pMqttContext = &mqttContext;
    MQTTSubscribeInfo_t pSubscriptionList[ 1 ];

    assert( pMqttContext != NULL );
    assert( pTopicFilter != NULL );
    assert( topicFilterLength > 0 );

    ( void ) qos;

    /* Start with everything at 0. */
    ( void ) memset( ( void * ) pSubscriptionList, 0x00, sizeof( pSubscriptionList ) );

    /* Set the topic and topic length. */
    pSubscriptionList[ 0 ].pTopicFilter = pTopicFilter;
    pSubscriptionList[ 0 ].topicFilterLength = topicFilterLength;

    if( pthread_mutex_lock( &mqttMutex ) == 0 )
    {
        /* Send SUBSCRIBE packet. */
        mqttStatus = MQTT_Subscribe( pMqttContext,
                                     pSubscriptionList,
                                     sizeof( pSubscriptionList ) / sizeof( MQTTSubscribeInfo_t ),
                                     MQTT_GetPacketId( pMqttContext ) );

        pthread_mutex_unlock( &mqttMutex );
    }
    else
    {
        LogError( ( "Failed to acquire mqtt mutex for executing MQTT_Subscribe"
                    ",errno=%s",
                    strerror( errno ) ) );
    }

    if( mqttStatus != MQTTSuccess )
    {
        LogError( ( "Failed to send SUBSCRIBE packet to broker with error = %u.",
                    mqttStatus ) );

        otaRet = OtaMqttSubscribeFailed;
    }
    else
    {
        LogInfo( ( "SUBSCRIBE topic %.*s to broker.\n\n",
                   topicFilterLength,
                   pTopicFilter ) );

        registerSubscriptionManagerCallback( pTopicFilter, topicFilterLength );
    }

    return otaRet;
}

/*-----------------------------------------------------------*/

static OtaMqttStatus_t mqttPublish( const char * const pacTopic,
                                    uint16_t topicLen,
                                    const char * pMsg,
                                    uint32_t msgSize,
                                    uint8_t qos )
{
    OtaMqttStatus_t otaRet = OtaMqttSuccess;

    MQTTStatus_t mqttStatus = MQTTBadParameter;
    MQTTPublishInfo_t publishInfo = { 0 };
    MQTTContext_t * pMqttContext = &mqttContext;
    struct timespec ts = { 0 };
    int ret;

    /* Set the required publish parameters. */
    publishInfo.pTopicName = pacTopic;
    publishInfo.topicNameLength = topicLen;
    publishInfo.qos = qos;
    publishInfo.pPayload = pMsg;
    publishInfo.payloadLength = msgSize;

    if( pthread_mutex_lock( &mqttMutex ) == 0 )
    {
        mqttStatus = MQTT_Publish( pMqttContext,
                                   &publishInfo,
                                   MQTT_GetPacketId( pMqttContext ) );

        if( mqttStatus != MQTTSuccess )
        {
            LogError( ( "Failed to send PUBLISH packet to broker with error = %u.", mqttStatus ) );

            otaRet = OtaMqttPublishFailed;
        }

        pthread_mutex_unlock( &mqttMutex );
    }
    else
    {
        LogError( ( "Failed to acquire mqtt mutex for executing MQTT_Publish"
                    ",errno=%s",
                    strerror( errno ) ) );

        otaRet = OtaMqttPublishFailed;
    }

    if( ( mqttStatus == MQTTSuccess ) && ( qos == 1 ) )
    {
        /* Calculate relative interval as current time plus number of seconds. */
        clock_gettime( CLOCK_REALTIME, &ts );
        ts.tv_sec += MQTT_ACK_TIMEOUT_MS;

        while( ( ret = sem_timedwait( &ackSemaphore, &ts ) ) == -1 && errno == EINTR )
        {
            continue;
        }

        if( ret == -1 )
        {
            LogError( ( "Failed to receive ack for publish."
                        ",errno=%s",
                        strerror( errno ) ) );

            otaRet = OtaMqttPublishFailed;
        }
    }

    return otaRet;
}

/*-----------------------------------------------------------*/

static OtaMqttStatus_t mqttUnsubscribe( const char * pTopicFilter,
                                        uint16_t topicFilterLength,
                                        uint8_t qos )
{
    OtaMqttStatus_t otaRet = OtaMqttSuccess;
    MQTTStatus_t mqttStatus = MQTTBadParameter;

    MQTTSubscribeInfo_t pSubscriptionList[ 1 ];
    MQTTContext_t * pMqttContext = &mqttContext;

    ( void ) qos;

    /* Start with everything at 0. */
    ( void ) memset( ( void * ) pSubscriptionList, 0x00, sizeof( pSubscriptionList ) );

    /* Set the topic and topic length. */
    pSubscriptionList[ 0 ].pTopicFilter = pTopicFilter;
    pSubscriptionList[ 0 ].topicFilterLength = topicFilterLength;

    if( pthread_mutex_lock( &mqttMutex ) == 0 )
    {
        /* Send UNSUBSCRIBE packet. */
        mqttStatus = MQTT_Unsubscribe( pMqttContext,
                                       pSubscriptionList,
                                       sizeof( pSubscriptionList ) / sizeof( MQTTSubscribeInfo_t ),
                                       MQTT_GetPacketId( pMqttContext ) );

        pthread_mutex_unlock( &mqttMutex );
    }
    else
    {
        LogError( ( "Failed to acquire mutex for executing MQTT_Unsubscribe"
                    ",errno=%s",
                    strerror( errno ) ) );
    }

    if( mqttStatus != MQTTSuccess )
    {
        LogError( ( "Failed to send UNSUBSCRIBE packet to broker with error = %u.",
                    mqttStatus ) );

        otaRet = OtaMqttUnsubscribeFailed;
    }
    else
    {
        LogInfo( ( "UNSUBSCRIBE topic %.*s to broker.\n\n",
                   topicFilterLength,
                   pTopicFilter ) );
    }

    return otaRet;
}

/*-----------------------------------------------------------*/

static void setOtaInterfaces( OtaInterfaces_t * pOtaInterfaces )
{
    /* Initialize OTA library OS Interface. */
    pOtaInterfaces->os.event.init = Posix_OtaInitEvent;
    pOtaInterfaces->os.event.send = Posix_OtaSendEvent;
    pOtaInterfaces->os.event.recv = Posix_OtaReceiveEvent;
    pOtaInterfaces->os.event.deinit = Posix_OtaDeinitEvent;
    pOtaInterfaces->os.timer.start = Posix_OtaStartTimer;
    pOtaInterfaces->os.timer.stop = Posix_OtaStopTimer;
    pOtaInterfaces->os.timer.delete = Posix_OtaDeleteTimer;
    pOtaInterfaces->os.mem.malloc = STDC_Malloc;
    pOtaInterfaces->os.mem.free = STDC_Free;

    /* Initialize the OTA library MQTT Interface.*/
    pOtaInterfaces->mqtt.subscribe = mqttSubscribe;
    pOtaInterfaces->mqtt.publish = mqttPublish;
    pOtaInterfaces->mqtt.unsubscribe = mqttUnsubscribe;

    /* Initialize the OTA library PAL Interface.*/
    pOtaInterfaces->pal.getPlatformImageState = otaPal_GetPlatformImageState;
    pOtaInterfaces->pal.setPlatformImageState = otaPal_SetPlatformImageState;
    pOtaInterfaces->pal.writeBlock = otaPal_WriteBlock;
    pOtaInterfaces->pal.activate = otaPal_ActivateNewImage;
    pOtaInterfaces->pal.closeFile = otaPal_CloseFile;
    pOtaInterfaces->pal.reset = otaPal_ResetDevice;
    pOtaInterfaces->pal.abort = otaPal_Abort;
    pOtaInterfaces->pal.createFile = otaPal_CreateFileForRx;
}

/*-----------------------------------------------------------*/

static void * otaThread( void * pParam )
{
    /* Calling OTA agent task. */
    OTA_EventProcessingTask( pParam );
    LogInfo( ( "OTA Agent stopped." ) );
    return NULL;
}
/*-----------------------------------------------------------*/
static int startOTADemo( void )
{
    /* Status indicating a successful demo or not. */
    int returnStatus = EXIT_SUCCESS;

    /* coreMQTT library return status. */
    MQTTStatus_t mqttStatus = MQTTBadParameter;

    /* OTA library return status. */
    OtaErr_t otaRet = OtaErrNone;

    /* OTA Agent state returned from calling OTA_GetAgentState.*/
    OtaState_t state = OtaAgentStateStopped;

    /* OTA event message used for sending event to OTA Agent.*/
    OtaEventMsg_t eventMsg = { 0 };

    /* OTA library packet statistics per job.*/
    OtaAgentStatistics_t otaStatistics = { 0 };

    /* OTA Agent thread handle.*/
    pthread_t threadHandle;

    /* Status return from call to pthread_join. */
    int returnJoin = 0;

    /* OTA interface context required for library interface functions.*/
    OtaInterfaces_t otaInterfaces;

    /* Maximum time to wait for the OTA agent to get suspended. */
    int16_t suspendTimeout;

    /* Set OTA Library interfaces.*/
    setOtaInterfaces( &otaInterfaces );

    LogInfo( ( "OTA over MQTT demo, Application version %u.%u.%u",
               appFirmwareVersion.u.x.major,
               appFirmwareVersion.u.x.minor,
               appFirmwareVersion.u.x.build ) );

    /****************************** Init OTA Library. ******************************/

    if( returnStatus == EXIT_SUCCESS )
    {
        if( ( otaRet = OTA_Init( &otaBuffer,
                                 &otaInterfaces,
                                 ( const uint8_t * ) ( CLIENT_IDENTIFIER ),
                                 otaAppCallback ) ) != OtaErrNone )
        {
            LogError( ( "Failed to initialize OTA Agent, exiting = %u.",
                        otaRet ) );

            returnStatus = EXIT_FAILURE;
        }
    }

    /****************************** Create OTA Task. ******************************/

    if( returnStatus == EXIT_SUCCESS )
    {
        if( pthread_create( &threadHandle, NULL, otaThread, NULL ) != 0 )
        {
            LogError( ( "Failed to create OTA thread: "
                        ",errno=%s",
                        strerror( errno ) ) );

            returnStatus = EXIT_FAILURE;
        }
    }

    /****************************** OTA Demo loop. ******************************/

    if( returnStatus == EXIT_SUCCESS )
    {
        /* Wait till OTA library is stopped, output statistics for currently running
         * OTA job */
        while( ( ( state = OTA_GetState() ) != OtaAgentStateStopped ) )
        {
            if( mqttSessionEstablished != true )
            {
                /* Connect to MQTT broker and create MQTT connection. */
                if( EXIT_SUCCESS == establishConnection() )
                {
                    /* Check if OTA process was suspended and resume if required. */
                    if( state == OtaAgentStateSuspended )
                    {
                        /* Resume OTA operations. */
                        OTA_Resume();
                    }
                    else
                    {
                        /* Send start event to OTA Agent.*/
                        eventMsg.eventId = OtaAgentEventStart;
                        OTA_SignalEvent( &eventMsg );
                    }
                }
            }

            if( mqttSessionEstablished == true )
            {
                /* Acquire the mqtt mutex lock. */
                if( pthread_mutex_lock( &mqttMutex ) == 0 )
                {
                    /* Loop to receive packet from transport interface. */
                    mqttStatus = MQTT_ProcessLoop( &mqttContext );

                    pthread_mutex_unlock( &mqttMutex );
                }
                else
                {
                    LogError( ( "Failed to acquire mutex to execute process loop"
                                ",errno=%s",
                                strerror( errno ) ) );
                }

                if( ( mqttStatus == MQTTSuccess ) || ( mqttStatus == MQTTNeedMoreBytes ) )
                {
                    /* Get OTA statistics for currently executing job. */
                    OTA_GetStatistics( &otaStatistics );

                    LogInfo( ( " Received: %u   Queued: %u   Processed: %u   Dropped: %u",
                               otaStatistics.otaPacketsReceived,
                               otaStatistics.otaPacketsQueued,
                               otaStatistics.otaPacketsProcessed,
                               otaStatistics.otaPacketsDropped ) );

                    /* Delay to allow data to buffer for MQTT_ProcessLoop. */
                    Clock_SleepMs( OTA_EXAMPLE_LOOP_SLEEP_PERIOD_MS );
                }
                else
                {
                    LogError( ( "MQTT_ProcessLoop returned with status = %s.",
                                MQTT_Status_strerror( mqttStatus ) ) );

                    /* Disconnect from broker and close connection. */
                    disconnect();

                    /* Suspend OTA operations. */
                    otaRet = OTA_Suspend();

                    if( otaRet == OtaErrNone )
                    {
                        suspendTimeout = OTA_SUSPEND_TIMEOUT_MS;

                        while( ( ( state = OTA_GetState() ) != OtaAgentStateSuspended ) && ( suspendTimeout > 0 ) )
                        {
                            /* Wait for OTA Library state to suspend */
                            Clock_SleepMs( OTA_EXAMPLE_TASK_DELAY_MS );
                            suspendTimeout -= OTA_EXAMPLE_TASK_DELAY_MS;
                        }
                    }
                    else
                    {
                        LogError( ( "OTA failed to suspend. "
                                    "StatusCode=%d.", otaRet ) );
                    }
                }
            }
        }
    }

    /****************************** Wait for OTA Thread. ******************************/

    if( returnStatus == EXIT_SUCCESS )
    {
        returnJoin = pthread_join( threadHandle, NULL );

        if( returnJoin != 0 )
        {
            LogError( ( "Failed to join thread"
                        ",error code = %d",
                        returnJoin ) );

            returnStatus = EXIT_FAILURE;
        }
    }

    return returnStatus;
}

/*-----------------------------------------------------------*/

/**
 * @brief Entry point of demo.
 *
 * This example initializes the OTA library to enable OTA updates via the
 * MQTT broker. It simply connects to the MQTT broker with the users
 * credentials and spins in an indefinite loop to allow MQTT messages to be
 * forwarded to the OTA agent for possible processing. The OTA agent does all
 * of the real work; checking to see if the message topic is one destined for
 * the OTA agent. If not, it is simply ignored.
 */
int main( int argc,
          char ** argv )
{
    ( void ) argc;
    ( void ) argv;

    /* Return error status. */
    int returnStatus = EXIT_SUCCESS;

    /* Semaphore initialization flag. */
    bool bufferSemInitialized = false;
    bool ackSemInitialized = false;
    bool mqttMutexInitialized = false;

    /* Maximum time in milliseconds to wait before exiting demo . */
    int16_t waitTimeoutMs = OTA_DEMO_EXIT_TIMEOUT_MS;

    /* Initialize semaphore for buffer operations. */
    if( sem_init( &bufferSemaphore, 0, 1 ) != 0 )
    {
        LogError( ( "Failed to initialize buffer semaphore"
                    ",errno=%s",
                    strerror( errno ) ) );

        returnStatus = EXIT_FAILURE;
    }
    else
    {
        bufferSemInitialized = true;
    }

    /* Initialize semaphore for ack. */
    if( sem_init( &ackSemaphore, 0, 0 ) != 0 )
    {
        LogError( ( "Failed to initialize ack semaphore"
                    ",errno=%s",
                    strerror( errno ) ) );

        returnStatus = EXIT_FAILURE;
    }
    else
    {
        ackSemInitialized = true;
    }

    /* Initialize mutex for coreMQTT APIs. */
    if( pthread_mutex_init( &mqttMutex, NULL ) != 0 )
    {
        LogError( ( "Failed to initialize mutex for mqtt apis"
                    ",errno=%s",
                    strerror( errno ) ) );

        returnStatus = EXIT_FAILURE;
    }
    else
    {
        mqttMutexInitialized = true;
    }

    if( returnStatus == EXIT_SUCCESS )
    {
        /* Initialize MQTT library. Initialization of the MQTT library needs to be
         * done only once in this demo. */
        returnStatus = initializeMqtt( &mqttContext, &networkContext );
    }

    if( returnStatus == EXIT_SUCCESS )
    {
        /* Start OTA demo. */
        returnStatus = startOTADemo();
    }

    /* Disconnect from broker and close connection. */
    disconnect();

    if( bufferSemInitialized == true )
    {
        /* Cleanup semaphore created for buffer operations. */
        if( sem_destroy( &bufferSemaphore ) != 0 )
        {
            LogError( ( "Failed to destroy buffer semaphore"
                        ",errno=%s",
                        strerror( errno ) ) );

            returnStatus = EXIT_FAILURE;
        }
    }

    if( ackSemInitialized == true )
    {
        /* Cleanup semaphore created for ack. */
        if( sem_destroy( &ackSemaphore ) != 0 )
        {
            LogError( ( "Failed to destroy ack semaphore"
                        ",errno=%s",
                        strerror( errno ) ) );

            returnStatus = EXIT_FAILURE;
        }
    }

    if( mqttMutexInitialized == true )
    {
        /* Cleanup mutex created for MQTT operations. */
        if( pthread_mutex_destroy( &mqttMutex ) != 0 )
        {
            LogError( ( "Failed to destroy mutex for mqtt apis"
                        ",errno=%s",
                        strerror( errno ) ) );

            returnStatus = EXIT_FAILURE;
        }
    }

    /* Wait and log message before exiting demo. */
    while( waitTimeoutMs > 0 )
    {
        Clock_SleepMs( OTA_EXAMPLE_TASK_DELAY_MS );
        waitTimeoutMs -= OTA_EXAMPLE_TASK_DELAY_MS;

        LogError( ( "Exiting demo in %d sec", waitTimeoutMs / 1000 ) );
    }

    return returnStatus;
}


text:
namespace lldb_private {

class RegisterContext :
    public std::enable_shared_from_this<RegisterContext>,
    public ExecutionContextScope
{
public:
    //------------------------------------------------------------------
    // Constructors and Destructors
    //------------------------------------------------------------------
    RegisterContext (Thread &thread, uint32_t concrete_frame_idx);

    virtual
    ~RegisterContext ();

    void
    InvalidateIfNeeded (bool force);

    //------------------------------------------------------------------
    // Subclasses must override these functions
    //------------------------------------------------------------------
    virtual void
    InvalidateAllRegisters () = 0;

    virtual size_t
    GetRegisterCount () = 0;

    virtual const RegisterInfo *
    GetRegisterInfoAtIndex (size_t reg) = 0;

    virtual size_t
    GetRegisterSetCount () = 0;

    virtual const RegisterSet *
    GetRegisterSet (size_t reg_set) = 0;

    virtual bool
    ReadRegister (const RegisterInfo *reg_info, RegisterValue &reg_value) = 0;

    virtual bool
    WriteRegister (const RegisterInfo *reg_info, const RegisterValue &reg_value) = 0;

    virtual bool
    ReadAllRegisterValues (lldb::DataBufferSP &data_sp)
    {
        return false;
    }

    virtual bool
    WriteAllRegisterValues (const lldb::DataBufferSP &data_sp)
    {
        return false;
    }

    // These two functions are used to implement "push" and "pop" of register states.  They are used primarily
    // for expression evaluation, where we need to push a new state (storing the old one in data_sp) and then
    // restoring the original state by passing the data_sp we got from ReadAllRegisters to WriteAllRegisterValues.
    // ReadAllRegisters will do what is necessary to return a coherent set of register values for this thread, which
    // may mean e.g. interrupting a thread that is sitting in a kernel trap.  That is a somewhat disruptive operation,
    // so these API's should only be used when this behavior is needed.

    virtual bool
    ReadAllRegisterValues (lldb_private::RegisterCheckpoint &reg_checkpoint);

    virtual bool
    WriteAllRegisterValues (const lldb_private::RegisterCheckpoint &reg_checkpoint);

    bool
    CopyFromRegisterContext (lldb::RegisterContextSP context);

    virtual uint32_t
    ConvertRegisterKindToRegisterNumber (uint32_t kind, uint32_t num) = 0;

    //------------------------------------------------------------------
    // Subclasses can override these functions if desired
    //------------------------------------------------------------------
    virtual uint32_t
    NumSupportedHardwareBreakpoints ();

    virtual uint32_t
    SetHardwareBreakpoint (lldb::addr_t addr, size_t size);

    virtual bool
    ClearHardwareBreakpoint (uint32_t hw_idx);

    virtual uint32_t
    NumSupportedHardwareWatchpoints ();

    virtual uint32_t
    SetHardwareWatchpoint (lldb::addr_t addr, size_t size, bool read, bool write);

    virtual bool
    ClearHardwareWatchpoint (uint32_t hw_index);

    virtual bool
    HardwareSingleStep (bool enable);

    virtual Error
    ReadRegisterValueFromMemory (const lldb_private::RegisterInfo *reg_info, lldb::addr_t src_addr, uint32_t src_len, RegisterValue &reg_value);

    virtual Error
    WriteRegisterValueToMemory (const lldb_private::RegisterInfo *reg_info, lldb::addr_t dst_addr, uint32_t dst_len, const RegisterValue &reg_value);

    //------------------------------------------------------------------
    // Subclasses should not override these
    //------------------------------------------------------------------
    virtual lldb::tid_t
    GetThreadID() const;

    virtual Thread &
    GetThread ()
    {
        return m_thread;
    }

    const RegisterInfo *
    GetRegisterInfoByName (const char *reg_name, uint32_t start_idx = 0);

    const RegisterInfo *
    GetRegisterInfo (uint32_t reg_kind, uint32_t reg_num);

    uint64_t
    GetPC (uint64_t fail_value = LLDB_INVALID_ADDRESS);

    bool
    SetPC (uint64_t pc);

    bool SetPC (Address addr);

    uint64_t
    GetSP (uint64_t fail_value = LLDB_INVALID_ADDRESS);

    bool
    SetSP (uint64_t sp);

    uint64_t
    GetFP (uint64_t fail_value = LLDB_INVALID_ADDRESS);

    bool
    SetFP (uint64_t fp);

    const char *
    GetRegisterName (uint32_t reg);

    uint64_t
    GetReturnAddress (uint64_t fail_value = LLDB_INVALID_ADDRESS);

    uint64_t
    GetFlags (uint64_t fail_value = 0);

    uint64_t
    ReadRegisterAsUnsigned (uint32_t reg, uint64_t fail_value);

    uint64_t
    ReadRegisterAsUnsigned (const RegisterInfo *reg_info, uint64_t fail_value);

    bool
    WriteRegisterFromUnsigned (uint32_t reg, uint64_t uval);

    bool
    WriteRegisterFromUnsigned (const RegisterInfo *reg_info, uint64_t uval);
    bool
    ConvertBetweenRegisterKinds (int source_rk, uint32_t source_regnum, int target_rk, uint32_t& target_regnum);

    //------------------------------------------------------------------
    // lldb::ExecutionContextScope pure virtual functions
    //------------------------------------------------------------------
    virtual lldb::TargetSP
    CalculateTarget ();

    virtual lldb::ProcessSP
    CalculateProcess ();

    virtual lldb::ThreadSP
    CalculateThread ();

    virtual lldb::StackFrameSP
    CalculateStackFrame ();

    virtual void
    CalculateExecutionContext (ExecutionContext &exe_ctx);

    uint32_t
    GetStopID () const
    {
        return m_stop_id;
    }

    void
    SetStopID (uint32_t stop_id)
    {
        m_stop_id = stop_id;
    }

protected:
    //------------------------------------------------------------------
    // Classes that inherit from RegisterContext can see and modify these
    //------------------------------------------------------------------
    Thread &m_thread;               // The thread that this register context belongs to.
    uint32_t m_concrete_frame_idx;    // The concrete frame index for this register context
    uint32_t m_stop_id;             // The stop ID that any data in this context is valid for
private:
    //------------------------------------------------------------------
    // For RegisterContext only
    //------------------------------------------------------------------
    DISALLOW_COPY_AND_ASSIGN (RegisterContext);
};

} // namespace lldb_private

#endif  // liblldb_RegisterContext_h_


text:
import os
from distutils.dir_util import copy_tree
from glob import glob

copy_folders = [
    'APScheduler-*',
    'setuptools*',
    'six-*',
    'pytz*',
    'tzlocal*',

]

def copy_file():
    site_packages = os.path.join(os.environ.get('PYTHON'), 'Lib', 'site-packages')
    for folder in copy_folders:
        setupfolders = glob(os.path.join(site_packages, folder))
        for f in setupfolders:
            foldername = os.path.basename(f)
            copy_tree(f,
                      'dist/dlbot/{}'.format(foldername))
    copy_tree(r'dist/dlbot/PyQt5/Qt/plugins', r'dist/dlbot')
    # os.remove('dist/dlbot/{}/requires.txt'.format(apscheduler_foldername))

if __name__ == "__main__":
    copy_file()


text:
class VNL_EXPORT vnl_least_squares_cost_function : public vnl_cost_function
{
 public:
  vnl_least_squares_cost_function(vnl_least_squares_function* f);

  double f(const vnl_vector<double>& x) override;

  void gradf(const vnl_vector<double>& x, vnl_vector<double>& gradient) override;

 protected:
  vnl_vector<double> storage_;
  vnl_matrix<double> jacobian_;
  vnl_least_squares_function* f_;
};

#endif // vnl_least_squares_cost_function_h_


text:
$:.unshift File.join(File.dirname(__FILE__), 'lib')

require 'rubygems'
require 'IRC'
require 'json'
require 'logger'
require 'wikipedia_api'

Thread.abort_on_exception = true

class WikipediaUpdater
  attr_accessor :logger
  USER_AGENT = 'Dbpedialite/1'
  HTTP_TIMEOUT = 5

  def initialize
    @logger = Logger.new(STDOUT)
  end

  def ping_sindice(data)
    uri = URI.parse("http://api.sindice.com/v2/ping")
    begin
      req = Net::HTTP::Post.new(uri.request_uri)
      req['User-Agent'] = USER_AGENT
      req['Content-Type'] = 'text/plain'
      req['Content-Length'] = data.length
      req['Accept'] = 'text/plain'
      req.body = data

      res = Net::HTTP.start(uri.host, uri.port) do |http|
        http.read_timeout = HTTP_TIMEOUT
        http.open_timeout = HTTP_TIMEOUT
        http.request(req)
      end
      raise res.body unless res.code == '200'
      puts "Sindice Response: #{res.body}"
    rescue Exception => e
      puts "Failed to ping sindice: #{e}"
    end
  end

  def run
    mutex = Mutex.new
    last_message_time = Time.now

    @irc_queue = Queue.new
    @uri_queue = Queue.new

    @irc_thread = Thread.new(@irc_queue) do |iq|
      bot = IRC.new('dbpedialite', "irc.wikimedia.org", 6667, "dbpedia lite")
      IRCEvent.add_callback('endofmotd') { |event| bot.add_channel('#en.wikipedia') }
      IRCEvent.add_callback('nicknameinuse') { |event| STDERR.puts event.message; exit(1) }
      IRCEvent.add_callback('privmsg') do |event|
        # add the message to the queue as soon as it is received
        iq.enq event.message
        # update the last message time so it can be monitored
        mutex.synchronize { last_message_time = Time.now }
      end
      bot.connect
    end

    # check that we are getting regular updates
    @monitor_thread = Thread.new do
      begin
        sleep 30
        age = nil
        mutex.synchronize { age = (Time.now - last_message_time) }
      end while (age < 30)
      STDERR.puts "Exiting: it's over 30 seconds since we've received a message at #{last_message_time}"
      exit(1)
    end

    @resolver_thread = Thread.new(@irc_queue, @uri_queue) do |iq, uq|
      loop do
        # retrive a message from the irc queue
        message = iq.deq

        # remove mirc colours
        message.gsub!(/\cc\d{1,2}|\cc/, '')

        # extract title from irc message
        title = $1 if message=~/\[\[(.*?)\]\]/
        next unless title

        # Ignore special and talk pages
        next if title =~ /^(\w+|\w+ talk):/

        # Lookup the page id
        data = WikipediaApi.page_info(:titles => title)
        next if data.nil?

        # Display status
        printf("[%s] %2d %9d: %s\n", iq.length, data['ns'], data['pageid'], data['title'])

        # Enqueue the URI for submission
        if data['ns'] == 0
          uq.enq "http://www.dbpedialite.org/things/#{data['pageid']}.rdf"
        else
          puts "Unknown namespace for #{title}"
        end
      end
    end

    @submission_thread = Thread.new(@uri_queue) do |uq|
      loop do
        sleep 60

        # De-queue all the URIs
        uris = []
        while uq.length > 0
          uris << uq.pop
        end
        uris.uniq!

        unless uris.empty?
          ping_sindice uris.join("\n")
        end
      end
    end

    [@irc_thread, @monitor_thread, @resolver_thread, @submission_thread].each { |t| t.join }
  end
end

updater = WikipediaUpdater.new
updater.run


text:
namespace Khala.EventSourcing
{
    using System;
    using System.Threading;
    using Microsoft.Owin.BuilderProperties;
    using Owin;

    public static class AppBuilderExtensions
    {
        [Obsolete("Use EnqueuePendingEvents(IAppBuilder, IEventPublisher) instead. This method will be removed in version 1.0.0.")]
        public static void EnqueuePendingEvents<T>(
            this IAppBuilder app,
            IEventSourcedRepository<T> repository)
            where T : class, IEventSourced
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (repository == null)
            {
                throw new ArgumentNullException(nameof(repository));
            }

            app.EnqueuePendingEvents(repository.EventPublisher);
        }

        public static void EnqueuePendingEvents(
            this IAppBuilder app,
            IEventPublisher eventPublisher)
        {
            if (app == null)
            {
                throw new ArgumentNullException(nameof(app));
            }

            if (eventPublisher == null)
            {
                throw new ArgumentNullException(nameof(eventPublisher));
            }

            var appProperties = new AppProperties(app.Properties);
            CancellationToken cancellationToken = appProperties.OnAppDisposing;
            eventPublisher.EnqueueAll(cancellationToken);
        }
    }
}


text:
package baylandtag.ag_dummy_person_table;

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Tab;
import javafx.scene.control.TabPane;
import javafx.stage.Stage;

public class TabPaneExample extends Application {
	public static void main(String[] args) {
		launch(args);
	}

	@Override
	public void start(Stage primaryStage) throws Exception {

		TabPane tabPane = new TabPane();
		Tab tab1 = new Tab();
		tab1.setText("Tab 1");
		tab1.setContent(new Label("TAB 1"));

		Tab tab2 = new Tab();
		tab2.setText("Tab 2");
		tab2.setContent(new Label("TAB 2"));
		tab2.setClosable(false);

		Tab tab3 = new Tab();
		tab3.setText("Tab 3");
		tab3.setContent(new Label("TAB 3"));
		tab3.setDisable(true);

		tabPane.getTabs().addAll(tab1, tab2, tab3);

		// -- step 2 select specific tab
		tabPane.getSelectionModel().select(tab2);

		Scene mainScene = new Scene(tabPane);
		primaryStage.setScene(mainScene);
		primaryStage.show();

	}

}


text:
<?xml version="1.0" encoding="ISO-8859-1"?>

<process-definition xmlns="urn:jbpm.org:jpdl-3.2" name="(COJUD) Fluxo básico geral da COJUD no SG">
    <description><![CDATA[Código do fluxo: COJUD_FBG_SG

Descrição: Processos deslocados à COJUD.

Raia        Localização              Papel
Secretaria  Secretaria Segundo Grau  Chefe de Secretaria
Secretaria  Secretaria Segundo Grau  Servidor de Secretaria]]></description>
    <!-- SWIMLANES -->
    <swimlane name="Contadoria">
        <assignment pooled-actors="#{localizacaoAssignment.getPooledActors('602:1338,602:5906')}" actor-id="#{actor.id}"/>
    </swimlane>
    <swimlane name="Nó de Desvio - (COJUD) Fluxo básico geral da COJUD no SG">
        <assignment pooled-actors="#{localizacaoAssignment.getPooledActors('602:1338,1342:1469')}"/>
    </swimlane>
    <!-- START-STATE -->
    <start-state name="Início">
        <task name="Tarefa inicial" swimlane="Contadoria"/>
        <transition to="(COJUD) Processos pendentes de juntada de cálculo judicial - ANALISAR" name="(COJUD) Processos pendentes de juntada de cálculo judicial - ANALISAR"/>
    </start-state>
    <!-- NODES -->
    <task-node end-tasks="true" name="(COJUD) Processos pendentes de juntada de cálculo judicial - ANALISAR">
        <task name="(COJUD) Processos pendentes de juntada de cálculo judicial - ANALISAR" swimlane="Contadoria">
            <controller>
                <variable name="aviso" mapped-name="textAlert:aviso" access="read,write"/>
                <variable name="movimentacaoLote" mapped-name="movimentarLote:movimentacaoLote" access="read,write"/>
            </controller>
        </task>
        <description><![CDATA[*Variáveis*

1. aviso
   Label: Para juntar o cálculo, clique no número do processo e o anexe através do menu INCLUIR PETIÇÕES E DOCUMENTOS.
   Escrita: Sim.
   Obrig.: Não.
   Tipo: Aviso.

2. Variável: movimentarLote
   Label:
   Escrita: Sim
   Obrig.: Não
   Tipo: Habilitar Movimentação em Lote]]></description>
        <transition to="Nó de Desvio - (COJUD) Fluxo básico geral da COJUD no SG" name="Nó de Desvio - (COJUD) Fluxo básico geral da COJUD no SG">
            <condition expression="#{true}"/>
        </transition>
        <transition to="(COJUD) Devolver ao órgão de origem" name="Finalizar tarefa e devolver processo"/>
        <event type="task-end">
            <action expression="#{tramitacaoProcessualService.apagaVariavelTarefa('mostrarBotaoGravarNoFluxo')}"/>
        </event>
        <event type="task-create">
            <action expression="#{tramitacaoProcessualService.gravaVariavelTarefa('mostrarBotaoGravarNoFluxo', false)}"/>
        </event>
    </task-node>
    <node name="(COJUD) Devolver ao órgão de origem">
        <transition to="Término" name="Término"/>
        <event type="node-enter">
            <action expression="#{processoJudicialService.sinalizarFluxo(tramitacaoProcessualService.recuperaProcesso(), 'pje:aguarda_cojud', true, false, true)}"/>
        </event>
    </node>
    <end-state name="Término"/>
    <task-node end-tasks="true" name="Nó de Desvio - (COJUD) Fluxo básico geral da COJUD no SG">
        <task name="Nó de Desvio - (COJUD) Fluxo básico geral da COJUD no SG" swimlane="Nó de Desvio - (COJUD) Fluxo básico geral da COJUD no SG"/>
        <transition to="Término" name="Término"/>
        <transition to="(COJUD) Processos pendentes de juntada de cálculo judicial - ANALISAR" name="(COJUD) Processos pendentes de juntada de cálculo judicial - ANALISAR"/>
    </task-node>
    <!-- PROCESS-EVENTS -->
    <event type="superstate-enter">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="process-start">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="before-signal">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="task-end">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="task-create">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="subprocess-created">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="task-assign">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="transition">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="after-signal">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="timer">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="task-start">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="subprocess-end">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="process-end">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="node-leave">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="superstate-leave">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
    <event type="node-enter">
        <script>br.com.infox.ibpm.util.JbpmEvents.raiseEvent(executionContext)</script>
    </event>
</process-definition>


text: pyinstaller --onefile main.py


text:
import React from 'react';
import Text from 'rsg-components/Text';
import Code from 'rsg-components/Code';
import { showSpaces, unquote, PropDescriptor } from './util';

const defaultValueBlacklist = ['null', 'undefined'];

export default function renderDefault(prop: PropDescriptor): React.ReactNode {
	// Workaround for issue https://github.com/reactjs/react-docgen/issues/221
	// If prop has defaultValue it can not be required
	if (prop.defaultValue) {
		const defaultValueString = showSpaces(unquote(String(prop.defaultValue.value)));
		if (prop.type || prop.flowType || prop.tsType) {
			const propName = prop.type
				? prop.type.name
				: prop.flowType
				? prop.flowType.type
				: prop.tsType && prop.tsType.type;

			if (defaultValueBlacklist.indexOf(prop.defaultValue.value) > -1) {
				return {defaultValueString};
			} else if (propName === 'func' || propName === 'function') {
				return (
					<Text size="small" color="light" underlined title={defaultValueString}>
						Function
					</Text>
				);
			} else if (propName === 'shape' || propName === 'object') {
				try {
					// We eval source code to be able to format the defaultProp here. This
					// can be considered safe, as it is the source code that is evaled,
					// which is from a known source and safe by default
					// eslint-disable-next-line no-eval
					const object = eval(`(${prop.defaultValue.value})`);
					return (
						<Text size="small" color="light" underlined title={JSON.stringify(object, null, 2)}>
							Shape
						</Text>
					);
				} catch (e) {
					// eval will throw if it contains a reference to a property not in the
					// local scope. To avoid any breakage we fall back to rendering the
					// prop without any formatting
					return (
						<Text size="small" color="light" underlined title={prop.defaultValue.value}>
							Shape
						</Text>
					);
				}
			}
		}

		return {defaultValueString};
	} else if (prop.required) {
		return (
			<Text size="small" color="light">
				Required
			</Text>
		);
	}
	return '';
}