|
| 1 | +/* |
| 2 | + * Copyright (c) 2024 Arduino |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: MPL-2.0 |
| 5 | + * This Source Code Form is subject to the terms of the Mozilla Public |
| 6 | + * License, v. 2.0. If a copy of the MPL was not distributed with this |
| 7 | + * file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 8 | + */ |
| 9 | + |
| 10 | +/************************************************************************************** |
| 11 | + * INCLUDE |
| 12 | + **************************************************************************************/ |
| 13 | + |
| 14 | +#include "PwmOutput.h" |
| 15 | + |
| 16 | +/************************************************************************************** |
| 17 | + * NAMESPACE |
| 18 | + **************************************************************************************/ |
| 19 | + |
| 20 | +namespace opcua |
| 21 | +{ |
| 22 | + |
| 23 | +/************************************************************************************** |
| 24 | + * FUNCTION DEFINITION |
| 25 | + **************************************************************************************/ |
| 26 | + |
| 27 | +static void |
| 28 | +pwm_output_on_write_request_pwm_period( |
| 29 | + UA_Server *server, |
| 30 | + const UA_NodeId *sessionId, |
| 31 | + void *sessionContext, |
| 32 | + const UA_NodeId *nodeid, |
| 33 | + void *nodeContext, |
| 34 | + const UA_NumericRange *range, |
| 35 | + const UA_DataValue *data) |
| 36 | +{ |
| 37 | + PwmOutput * this_ptr = reinterpret_cast<PwmOutput *>(nodeContext); |
| 38 | + uint32_t const pwm_period_ms = *(UA_UInt32 *)(data->value.data); |
| 39 | + this_ptr->onWriteRequestPwmPeriod(server, nodeid, pwm_period_ms); |
| 40 | +} |
| 41 | + |
| 42 | +static void |
| 43 | +pwm_output_on_write_request_pwm_pulse_width( |
| 44 | + UA_Server *server, |
| 45 | + const UA_NodeId *sessionId, |
| 46 | + void *sessionContext, |
| 47 | + const UA_NodeId *nodeid, |
| 48 | + void *nodeContext, |
| 49 | + const UA_NumericRange *range, |
| 50 | + const UA_DataValue *data) |
| 51 | +{ |
| 52 | + PwmOutput * this_ptr = reinterpret_cast<PwmOutput *>(nodeContext); |
| 53 | + uint32_t const pwm_pulse_width_ms = *(UA_UInt32 *)(data->value.data); |
| 54 | + this_ptr->onWriteRequestPwmPulseWidth(server, nodeid, pwm_pulse_width_ms); |
| 55 | +} |
| 56 | + |
| 57 | +/************************************************************************************** |
| 58 | + * CTOR/DTOR |
| 59 | + **************************************************************************************/ |
| 60 | + |
| 61 | +PwmOutput::PwmOutput( |
| 62 | + UA_NodeId const & node_id, |
| 63 | + SetPwmFunc const set_pwm_func) |
| 64 | + : _node_id{node_id} |
| 65 | + , _set_pwm_func{set_pwm_func} |
| 66 | + , _pwm_period_ms{0} |
| 67 | + , _pwm_pulse_width_ms{0} |
| 68 | +{ |
| 69 | + |
| 70 | +} |
| 71 | + |
| 72 | +/************************************************************************************** |
| 73 | + * PUBLIC MEMBER FUNCTIONS |
| 74 | + **************************************************************************************/ |
| 75 | + |
| 76 | +PwmOutput::SharedPtr |
| 77 | +PwmOutput::create( |
| 78 | + UA_Server * server, |
| 79 | + UA_NodeId const & parent_node_id, |
| 80 | + const char * display_name, |
| 81 | + SetPwmFunc const set_pwm_func) |
| 82 | +{ |
| 83 | + UA_StatusCode rc = UA_STATUSCODE_GOOD; |
| 84 | + |
| 85 | + UA_VariableAttributes pwm_out_period_value_attr = UA_VariableAttributes_default; |
| 86 | + |
| 87 | + UA_Boolean pwm_output_period_value = 0; |
| 88 | + UA_Variant_setScalar(&pwm_out_period_value_attr.value, &pwm_output_period_value, &UA_TYPES[UA_TYPES_UINT32]); |
| 89 | + |
| 90 | + pwm_out_period_value_attr.displayName = UA_LOCALIZEDTEXT("en-US", (char *)display_name); |
| 91 | + pwm_out_period_value_attr.dataType = UA_TYPES[UA_TYPES_UINT32].typeId; |
| 92 | + pwm_out_period_value_attr.accessLevel = |
| 93 | + UA_ACCESSLEVELMASK_READ | |
| 94 | + UA_ACCESSLEVELMASK_WRITE | UA_ACCESSLEVELMASK_STATUSWRITE | |
| 95 | + UA_ACCESSLEVELMASK_TIMESTAMPWRITE; /* Status and timestamp write access necessary for opcua-client. */ |
| 96 | + |
| 97 | + UA_NodeId node_id; |
| 98 | + rc = UA_Server_addVariableNode(server, |
| 99 | + UA_NODEID_NULL, |
| 100 | + parent_node_id, |
| 101 | + UA_NODEID_NUMERIC(0, UA_NS0ID_HASCOMPONENT), |
| 102 | + UA_QUALIFIEDNAME(1, "Value"), |
| 103 | + UA_NODEID_NUMERIC(0, UA_NS0ID_BASEDATAVARIABLETYPE), |
| 104 | + pwm_out_period_value_attr, |
| 105 | + NULL, |
| 106 | + &node_id); |
| 107 | + if (UA_StatusCode_isBad(rc)) |
| 108 | + { |
| 109 | + UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, |
| 110 | + "%s: failed with %s", __PRETTY_FUNCTION__, UA_StatusCode_name(rc)); |
| 111 | + return nullptr; |
| 112 | + } |
| 113 | + |
| 114 | + /* Create an instance of AnalogOutput here. */ |
| 115 | + auto const instance_ptr = std::make_shared<PwmOutput>(node_id, set_pwm_func); |
| 116 | + |
| 117 | + rc = UA_Server_setNodeContext(server, node_id, reinterpret_cast<void *>(instance_ptr.get())); |
| 118 | + if (UA_StatusCode_isBad(rc)) |
| 119 | + { |
| 120 | + UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, |
| 121 | + "%s: UA_Server_setNodeContext(...) failed with %s", __PRETTY_FUNCTION__, UA_StatusCode_name(rc)); |
| 122 | + return nullptr; |
| 123 | + } |
| 124 | + |
| 125 | + UA_ValueCallback callback; |
| 126 | + callback.onRead = NULL; |
| 127 | + callback.onWrite = pwm_output_on_write_request_pwm_period; |
| 128 | + rc = UA_Server_setVariableNode_valueCallback(server, node_id, callback); |
| 129 | + if (UA_StatusCode_isBad(rc)) |
| 130 | + { |
| 131 | + UA_LOG_ERROR(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, |
| 132 | + "%s: UA_Server_setVariableNode_valueCallback(...) failed with %s", __PRETTY_FUNCTION__, UA_StatusCode_name(rc)); |
| 133 | + return nullptr; |
| 134 | + } |
| 135 | + |
| 136 | + return instance_ptr; |
| 137 | +} |
| 138 | + |
| 139 | +void |
| 140 | +PwmOutput::onWriteRequestPwmPeriod( |
| 141 | + UA_Server * server, |
| 142 | + UA_NodeId const * node_id, |
| 143 | + uint32_t const pwm_period_ms) |
| 144 | +{ |
| 145 | + _pwm_period_ms = pwm_period_ms; |
| 146 | + UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "%s: pwm period = %d ms", __PRETTY_FUNCTION__, _pwm_period_ms); |
| 147 | + _set_pwm_func(_pwm_period_ms, _pwm_pulse_width_ms); |
| 148 | +} |
| 149 | + |
| 150 | +void |
| 151 | +PwmOutput::onWriteRequestPwmPulseWidth( |
| 152 | + UA_Server * server, |
| 153 | + UA_NodeId const * node_id, |
| 154 | + uint32_t const pwm_pulse_width_ms) |
| 155 | +{ |
| 156 | + _pwm_pulse_width_ms = pwm_pulse_width_ms; |
| 157 | + UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, "%s: pwm pulse width = %d ms", __PRETTY_FUNCTION__, _pwm_pulse_width_ms); |
| 158 | + _set_pwm_func(_pwm_period_ms, _pwm_pulse_width_ms); |
| 159 | +} |
| 160 | + |
| 161 | +/************************************************************************************** |
| 162 | + * NAMESPACE |
| 163 | + **************************************************************************************/ |
| 164 | + |
| 165 | +} /* opcua */ |
0 commit comments