C_runtime/PLCObject.cpp
author Edouard Tisserant <edouard.tisserant@gmail.com>
Wed, 05 Jun 2024 15:05:54 +0200
changeset 3954 5744391252ec
parent 3949 f64dce4e2f62
child 3956 9b4ffbb95338
permissions -rw-r--r--
merge
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     1
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
     2
#include <string.h>
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
     3
#include <filesystem>
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
     4
#include <dlfcn.h>
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
     5
#include <fstream>
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
     6
#include <iostream>
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
     7
#include <vector>
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     8
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
     9
#include "Logging.hpp"
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    10
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    11
#include "PLCObject.hpp"
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    12
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    13
#include "beremiz.h"
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    14
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    15
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    16
// File name of the last transferred PLC md5 hex digest
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    17
// with typo in the name, for compatibility with Python runtime
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    18
#define LastTransferredPLC "lasttransferedPLC.md5"
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    19
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    20
// File name of the extra files list
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    21
#define ExtraFilesList "extra_files.txt"
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    22
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    23
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    24
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    25
PLCObject::PLCObject(void)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    26
{
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
    27
    m_status.PLCstatus = Empty;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
    28
    m_handle = NULL;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
    29
    m_debugToken = 0;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
    30
    m_argc = 0;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
    31
    m_argv = NULL;
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    32
    m_PSK_ID = "";
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    33
    m_PSK_secret = "";
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    34
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    35
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    36
PLCObject::~PLCObject(void)
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    37
{
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    38
}
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
    39
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    40
uint32_t PLCObject::AppendChunkToBlob(
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    41
    const binary_t *data, const binary_t *blobID, binary_t *newBlobID)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    42
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    43
    // Append data to blob with given blobID
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    44
    // Output new blob's md5 into newBlobID
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    45
    // Return 0 if success
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    46
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    47
    newBlobID->data = (uint8_t *)malloc(MD5::digestsize);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    48
    if (newBlobID->data == NULL)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    49
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    50
        return ENOMEM;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    51
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    52
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    53
    std::vector<uint8_t> k(blobID->data, blobID->data + blobID->dataLength);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    54
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    55
    auto nh = m_mapBlobIDToBlob.extract(k);
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    56
    if (nh.empty())
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    57
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    58
        return ENOENT;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    59
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    60
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    61
    Blob *blob = nh.mapped();
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    62
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    63
    uint32_t res = blob->appendChunk(data->data, data->dataLength);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    64
    if (res != 0)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    65
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    66
        return res;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    67
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    68
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    69
    MD5::digest_t digest = blob->digest();
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    70
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    71
    std::vector<uint8_t> nk((uint8_t*)digest.data, (uint8_t*)digest.data + MD5::digestsize);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    72
    nh.key() = nk;
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    73
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    74
    m_mapBlobIDToBlob.insert(std::move(nh));
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    75
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    76
    memcpy(newBlobID->data, digest.data, MD5::digestsize);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    77
    newBlobID->dataLength = MD5::digestsize;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
    78
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    79
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    80
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    81
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    82
#define LOG_READ_BUFFER_SIZE 1 << 10 // 1KB
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    83
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    84
uint32_t PLCObject::GetLogMessage(
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    85
    uint8_t level, uint32_t msgID, log_message *message)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    86
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    87
    char buf[LOG_READ_BUFFER_SIZE];
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    88
    uint32_t tick;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    89
    uint32_t tv_sec;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    90
    uint32_t tv_nsec;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    91
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    92
    uint32_t resultLen = m_PLCSyms.GetLogMessage(
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    93
        level, msgID, buf, LOG_READ_BUFFER_SIZE - 1,
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    94
        &tick, &tv_sec, &tv_nsec);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    95
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    96
    if (resultLen == 0)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    97
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    98
        return ENOENT;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
    99
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   100
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   101
    // Get log message with given msgID
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   102
    message->msg = (char *)malloc(resultLen);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   103
    if (message->msg == NULL)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   104
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   105
        return ENOMEM;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   106
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   107
    // Copy the log message into eRPC message
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   108
    memcpy(message->msg, buf, resultLen);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   109
    message->msg[resultLen + 1] = '\0';
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   110
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   111
    message->tick = tick;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   112
    message->sec = tv_sec;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   113
    message->nsec = tv_nsec;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   114
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   115
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   116
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   117
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   118
uint32_t PLCObject::GetPLCID(PSKID *plcID)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   119
{
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   120
    // Get PSK ID
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   121
    plcID->ID = (char *)malloc(m_PSK_ID.size() + 1);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   122
    if (plcID->ID == NULL)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   123
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   124
        return ENOMEM;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   125
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   126
    memcpy(plcID->ID, m_PSK_ID.c_str(), m_PSK_ID.size());
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   127
    plcID->ID[m_PSK_ID.size()] = '\0';
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   128
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   129
    // Get PSK secret
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   130
    plcID->PSK = (char *)malloc(m_PSK_secret.size() + 1);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   131
    if (plcID->PSK == NULL)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   132
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   133
        free(plcID->ID);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   134
        return ENOMEM;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   135
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   136
    memcpy(plcID->PSK, m_PSK_secret.c_str(), m_PSK_secret.size());
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   137
    plcID->PSK[m_PSK_secret.size()] = '\0';
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   138
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   139
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   140
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   141
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   142
uint32_t PLCObject::GetPLCstatus(PLCstatus *status)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   143
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   144
    // Get PLC status
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   145
    *status = m_status;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   146
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   147
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   148
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   149
uint32_t PLCObject::GetTraceVariables(
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   150
    uint32_t debugToken, TraceVariables *traces)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   151
{
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   152
    if(debugToken != m_debugToken)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   153
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   154
        return EINVAL;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   155
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   156
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   157
    // Check if there are any traces
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   158
    m_tracesMutex.lock();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   159
    size_t sz = m_traces.size();
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   160
    if(sz > 0)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   161
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   162
        // Allocate memory for traces
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   163
        traces->traces.elements = (trace_sample *)malloc(sz * sizeof(trace_sample));
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   164
        if(traces->traces.elements == NULL)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   165
        {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   166
            m_tracesMutex.unlock();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   167
            return ENOMEM;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   168
        }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   169
        // Copy traces from vector
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   170
        memcpy(traces->traces.elements, m_traces.data(), sz * sizeof(trace_sample));
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   171
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   172
        // Clear the vector
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   173
        // note that the data is not freed here, it is meant to be freed by eRPC server code
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   174
        m_traces.clear();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   175
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   176
    m_tracesMutex.unlock();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   177
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   178
    traces->traces.elementsCount = sz;
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   179
    traces->PLCstatus = m_status.PLCstatus;
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   180
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   181
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   182
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   183
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   184
uint32_t PLCObject::MatchMD5(const char *MD5, bool *match)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   185
{
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   186
    // an empty PLC is never considered to match
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   187
    if(m_status.PLCstatus == Empty)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   188
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   189
        *match = false;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   190
        return 0;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   191
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   192
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   193
    // Load the last transferred PLC md5 hex digest
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   194
    std::string md5sum;
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   195
    try {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   196
        std::ifstream(std::string(LastTransferredPLC), std::ios::binary) >> md5sum;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   197
    } catch (std::exception e) {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   198
        *match = false;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   199
        return 0;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   200
    }
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   201
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   202
    // Compare the given MD5 with the last transferred PLC md5
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   203
    *match = (md5sum == MD5);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   204
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   205
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   206
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   207
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   208
#if defined(_WIN32) || defined(_WIN64)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   209
// For Windows platform
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   210
#define SHARED_OBJECT_EXT ".dll"
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   211
#elif defined(__APPLE__) || defined(__MACH__)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   212
// For MacOS platform
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   213
#define SHARED_OBJECT_EXT ".dylib"
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   214
#else
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   215
// For Linux/Unix platform
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   216
#define SHARED_OBJECT_EXT ".so"
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   217
#endif
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   218
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   219
uint32_t PLCObject::BlobAsFile(
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   220
    const binary_t *BlobID, std::filesystem::path filename)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   221
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   222
    // Extract the blob from the map
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   223
    auto nh = m_mapBlobIDToBlob.extract(
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   224
        std::vector<uint8_t>(BlobID->data, BlobID->data + BlobID->dataLength));
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   225
    if (nh.empty())
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   226
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   227
        return ENOENT;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   228
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   229
    Blob *blob = nh.mapped();
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   230
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   231
    // Realize the blob into a file
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   232
    uint32_t res = blob->asFile(filename);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   233
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   234
    delete blob;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   235
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   236
    if (res != 0)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   237
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   238
        return res;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   239
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   240
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   241
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   242
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   243
uint32_t PLCObject::NewPLC(
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   244
    const char *md5sum, const binary_t *plcObjectBlobID,
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   245
    const list_extra_file_1_t *extrafiles, bool *success)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   246
{
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   247
    if(m_status.PLCstatus == Started)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   248
    {
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   249
        *success = false;
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   250
        return EBUSY;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   251
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   252
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   253
    if(m_status.PLCstatus == Broken)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   254
    {
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   255
        *success = false;
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   256
        return EINVAL;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   257
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   258
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   259
    // Unload the PLC object
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   260
    UnLoadPLC();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   261
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   262
    // Purge the PLC object
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   263
    PurgePLC();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   264
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   265
    // Concatenate md5sum and shared object extension to obtain filename
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   266
    std::filesystem::path filename =
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   267
        std::filesystem::path(md5sum) += SHARED_OBJECT_EXT;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   268
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   269
    // Create the PLC object shared object file
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   270
    BlobAsFile(plcObjectBlobID, filename);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   271
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   272
    // create "lasttransferedPLC.md5" file and Save md5sum in it
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   273
    std::ofstream(std::string(LastTransferredPLC), std::ios::binary) << md5sum;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   274
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   275
    // create "extra_files.txt" file
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   276
    std::ofstream extra_files_log(std::string(ExtraFilesList), std::ios::binary);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   277
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   278
    // Create extra files
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   279
    for (int i = 0; i < extrafiles->elementsCount; i++)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   280
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   281
        extra_file *extrafile = extrafiles->elements + i;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   282
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   283
        BlobAsFile(plcObjectBlobID, extrafile->fname);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   284
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   285
        // Save the extra file name in "extra_files.txt"
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   286
        extra_files_log << extrafile->fname << std::endl;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   287
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   288
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   289
    // Load the PLC object
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   290
    uint32_t res = LoadPLC();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   291
    if (res != 0)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   292
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   293
        *success = false;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   294
        return res;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   295
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   296
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   297
    m_status.PLCstatus = Stopped;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   298
    *success = true;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   299
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   300
    return 0;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   301
}
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   302
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   303
#define DLSYM(sym)                                                           \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   304
    do                                                                       \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   305
    {                                                                        \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   306
        m_PLCSyms.sym = (decltype(m_PLCSyms.sym))dlsym(m_handle, #sym);      \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   307
        if (m_PLCSyms.sym == NULL)                                           \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   308
        {                                                                    \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   309
            /* TODO: use log instead */                                      \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   310
            std::cout << "Error dlsym " #sym ": " << dlerror() << std::endl; \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   311
            return errno;                                                    \
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   312
        }                                                                    \
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   313
    } while (0);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   314
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   315
uint32_t PLCObject::LoadPLC(void)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   316
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   317
    // Load the last transferred PLC md5 hex digest
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   318
    std::string md5sum;
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   319
    try {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   320
        std::ifstream(std::string(LastTransferredPLC), std::ios::binary) >> md5sum;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   321
    } catch (std::exception e) {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   322
        return ENOENT;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   323
    }
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   324
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   325
    // Concatenate md5sum and shared object extension to obtain filename
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   326
    std::filesystem::path filename(md5sum + SHARED_OBJECT_EXT);
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   327
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   328
    // Load the shared object file
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   329
    m_handle = dlopen(std::filesystem::absolute(filename).c_str(), RTLD_NOW);
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   330
    if (m_handle == NULL)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   331
    {
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   332
        std::cout << "Error: " << dlerror() << std::endl;
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   333
        return errno;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   334
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   335
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   336
    // Resolve shared object symbols
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   337
    FOR_EACH_PLC_SYMBOLS_DO(DLSYM);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   338
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   339
    // Set content of PLC_ID to md5sum
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   340
    m_PLCSyms.PLC_ID = (uint8_t *)malloc(md5sum.size() + 1);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   341
    if (m_PLCSyms.PLC_ID == NULL)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   342
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   343
        return ENOMEM;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   344
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   345
    memcpy(m_PLCSyms.PLC_ID, md5sum.c_str(), md5sum.size());
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   346
    m_PLCSyms.PLC_ID[md5sum.size()] = '\0';
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   347
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   348
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   349
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   350
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   351
#define ULSYM(sym)            \
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   352
    do                        \
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   353
    {                         \
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   354
        m_PLCSyms.sym = NULL; \
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   355
    } while (0);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   356
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   357
uint32_t PLCObject::UnLoadPLC(void)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   358
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   359
    // Unload the shared object file
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   360
    FOR_EACH_PLC_SYMBOLS_DO(ULSYM);
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   361
    if(m_handle != NULL)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   362
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   363
        dlclose(m_handle);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   364
        m_handle = NULL;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   365
    }
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   366
    return 0;
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   367
}
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   368
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   369
uint32_t PLCObject::PurgeBlobs(void)
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   370
{
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   371
    // Purge all blobs
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   372
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   373
    for (auto &blob : m_mapBlobIDToBlob)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   374
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   375
        delete blob.second;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   376
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   377
    m_mapBlobIDToBlob.clear();
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   378
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   379
    return 0;
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   380
}
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   381
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   382
uint32_t PLCObject::PurgePLC(void)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   383
{
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   384
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   385
    // Open the extra files list
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   386
    std::ifstream extra_files_log(std::string(ExtraFilesList), std::ios::binary);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   387
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   388
    // Remove extra files
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   389
    std::string extra_file;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   390
    while (std::getline(extra_files_log, extra_file))
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   391
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   392
        std::filesystem::remove(extra_file);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   393
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   394
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   395
    // Load the last transferred PLC md5 hex digest
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   396
    std::string md5sum;
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   397
    try {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   398
        std::ifstream(std::string(LastTransferredPLC), std::ios::binary) >> md5sum;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   399
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   400
        // Remove the PLC object shared object file
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   401
        std::filesystem::remove(md5sum + SHARED_OBJECT_EXT);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   402
    } catch (std::exception e) {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   403
        // ignored
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   404
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   405
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   406
    try {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   407
        // Remove the last transferred PLC md5 hex digest
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   408
        std::filesystem::remove(std::string(LastTransferredPLC));
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   409
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   410
        // Remove the extra files list
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   411
        std::filesystem::remove(std::string(ExtraFilesList));
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   412
    } catch (std::exception e) {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   413
        // ignored
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   414
    }
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   415
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   416
    return 0;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   417
}
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   418
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   419
uint32_t PLCObject::RepairPLC(void)
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   420
{
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   421
    // Repair the PLC object
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   422
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   423
    if(m_status.PLCstatus == Broken)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   424
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   425
        // Unload the PLC object
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   426
        UnLoadPLC();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   427
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   428
        // Purge the PLC object
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   429
        PurgePLC();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   430
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   431
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   432
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   433
    LogMessage(LOG_WARNING, "RepairPLC not implemented");
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   434
    return 0;
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   435
}
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   436
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   437
uint32_t PLCObject::ResetLogCount(void)
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   438
{
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   439
    m_PLCSyms.ResetLogCount();
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   440
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   441
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   442
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   443
uint32_t PLCObject::SeedBlob(const binary_t *seed, binary_t *blobID)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   444
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   445
    // Create a blob with given seed
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   446
    // Output new blob's md5 into blobID
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   447
    // Return 0 if success
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   448
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   449
    Blob *blob = NULL;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   450
    try
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   451
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   452
        blob = new Blob(seed->data, seed->dataLength);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   453
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   454
    catch (int e)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   455
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   456
        return e;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   457
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   458
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   459
    MD5::digest_t digest = blob->digest();
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   460
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   461
    std::vector<uint8_t> k((uint8_t*)digest.data, (uint8_t*)digest.data + MD5::digestsize);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   462
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   463
    m_mapBlobIDToBlob[k] = blob;
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   464
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   465
    blobID->data = (uint8_t *)malloc(MD5::digestsize);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   466
    if (blobID->data == NULL)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   467
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   468
        return ENOMEM;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   469
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   470
    memcpy(blobID->data, digest.data, MD5::digestsize);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   471
    blobID->dataLength = MD5::digestsize;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   472
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   473
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   474
}
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   475
void PLCObject::PurgeTraceBuffer(void)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   476
{
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   477
    // Free trace buffer
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   478
    m_tracesMutex.lock();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   479
    for(trace_sample s : m_traces){
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   480
        free(s.TraceBuffer.data);
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   481
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   482
    m_traces.clear();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   483
    m_tracesMutex.unlock();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   484
}
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   485
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   486
uint32_t PLCObject::SetTraceVariablesList(
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   487
    const list_trace_order_1_t *orders, int32_t *debugtoken)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   488
{
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   489
    if(m_status.PLCstatus == Empty)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   490
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   491
        return EINVAL;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   492
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   493
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   494
    // increment debug token
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   495
    m_debugToken++;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   496
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   497
    if(orders->elementsCount == 0)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   498
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   499
        // actually disables debug
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   500
        m_PLCSyms.suspendDebug(1);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   501
        *debugtoken = -5; // DEBUG_SUSPENDED
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   502
        return 0;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   503
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   504
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   505
    // suspend debug before any operation
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   506
    int res = m_PLCSyms.suspendDebug(0);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   507
    if(res == 0)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   508
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   509
        // forget about all previous debug variables
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   510
        m_PLCSyms.ResetDebugVariables();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   511
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   512
        // call RegisterTraceVariables for each trace order
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   513
        for (int i = 0; i < orders->elementsCount; i++)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   514
        {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   515
            trace_order *order = orders->elements + i;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   516
            res = m_PLCSyms.RegisterDebugVariable(order->idx, order->force.data, order->force.dataLength);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   517
            if(res != 0)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   518
            {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   519
                // if any error, disable debug
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   520
                // since debug is already suspended, resume it first
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   521
                m_PLCSyms.resumeDebug();
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   522
                m_PLCSyms.suspendDebug(1);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   523
                *debugtoken = -res;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   524
                return EINVAL;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   525
            }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   526
        }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   527
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   528
        // old traces are not valid anymore
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   529
        PurgeTraceBuffer();
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   530
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   531
        // Start debug thread if not already started
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   532
        if(!m_traceThread.joinable())
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   533
        {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   534
            m_traceThread = std::thread(&PLCObject::TraceThreadProc, this);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   535
        }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   536
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   537
        m_PLCSyms.resumeDebug();
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   538
        *debugtoken = m_debugToken;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   539
        return 0;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   540
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   541
    return res;
3937
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   542
}
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   543
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   544
uint32_t PLCObject::StartPLC(void)
e13543d716b6 C++ runtime: add eRPC server, minimal CLI and Makefile. WIP.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents:
diff changeset
   545
{
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   546
    LogMessage(LOG_INFO, "Starting PLC");
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   547
    uint32_t res = m_PLCSyms.startPLC(m_argc, m_argv);
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   548
    if(res != 0)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   549
    {
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   550
        m_status.PLCstatus = Broken;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   551
        return res;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   552
    }
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   553
    m_status.PLCstatus = Started;
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   554
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   555
    return 0;
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   556
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   557
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   558
uint32_t PLCObject::StopPLC(bool *success)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   559
{
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   560
    LogMessage(LOG_INFO, "Stopping PLC");
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   561
    uint32_t res = m_PLCSyms.stopPLC();
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   562
    if(res == 0)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   563
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   564
        m_status.PLCstatus = Stopped;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   565
    } else {
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   566
        m_status.PLCstatus = Broken;
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   567
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   568
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   569
    // Stop debug thread
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   570
    if(m_traceThread.joinable())
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   571
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   572
        m_traceThread.join();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   573
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   574
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   575
    return res;
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   576
}
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   577
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   578
uint32_t PLCObject::LogMessage(uint8_t level, std::string message)
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   579
{
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   580
    // if PLC isn't loaded, log to stdout
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   581
    if(m_PLCSyms.LogMessage == NULL)
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   582
    {
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   583
        std::cout << level << message << std::endl;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   584
        return ENOSYS;
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   585
    }
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   586
3940
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   587
    // Log std::string message with given level
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   588
    return m_PLCSyms.LogMessage(level, (char *)message.c_str(), message.size());
934bd46a7500 C++ runtime: WIP: untested PLCObject implementation. Still missing tracing.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3937
diff changeset
   589
}
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   590
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   591
void PLCObject::TraceThreadProc(void)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   592
{
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   593
    uint32_t err = 0;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   594
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   595
    m_PLCSyms.resumeDebug();
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   596
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   597
    while(m_status.PLCstatus == Started)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   598
    {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   599
        unsigned int tick;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   600
        unsigned int size;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   601
        void * buff;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   602
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   603
        // Data allocated here is meant to be freed by eRPC server code
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   604
        uint8_t* ourData = NULL;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   605
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   606
        m_PLClibMutex.lock();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   607
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   608
        int res = m_PLCSyms.GetDebugData(&tick, &size, &buff);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   609
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   610
        if(res == 0)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   611
        {   
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   612
            ourData = (uint8_t *)malloc(size);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   613
            if(ourData != NULL)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   614
            {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   615
                memcpy(ourData, buff, size);
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   616
            }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   617
            m_PLCSyms.FreeDebugData();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   618
        }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   619
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   620
        m_PLClibMutex.unlock();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   621
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   622
        if(ourData == NULL)
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   623
        {
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   624
            err = res == 0 ? ENOMEM : res;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   625
            break;
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   626
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   627
        } else {   
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   628
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   629
            m_tracesMutex.lock();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   630
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   631
            m_traces.push_back(trace_sample{tick, binary_t{ourData, size}});
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   632
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   633
            m_tracesMutex.unlock();
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   634
        }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   635
    }
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   636
3949
f64dce4e2f62 C runtime: initial fixes. Now runs and traces first_steps example.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3945
diff changeset
   637
    PurgeTraceBuffer();
3945
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   638
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   639
    LogMessage(err ? LOG_CRITICAL : LOG_INFO,
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   640
        err == ENOMEM ? "Out of memory in TraceThreadProc" : 
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   641
        err ? "TraceThreadProc ended because of error" : 
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   642
        "TraceThreadProc ended normally");
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   643
}
d303aab8f68b C++ runtime: WIP. Continue PLCObject implementation.
Edouard Tisserant <edouard.tisserant@gmail.com>
parents: 3940
diff changeset
   644