Commit 1689a39b authored by Leon Hiemstra's avatar Leon Hiemstra

using 'mm' and 'dev' for exposig nodes in OPCua

parent fb28fa0e
......@@ -37,7 +37,7 @@ cd src
# Connect with a client over control socket (tcp 3338)
# Connect with a client over control socket (tcp 3335)
for example:
`telnet localhost 3335`
......
......@@ -59,6 +59,16 @@ std::string Common::string_node_id(int node)
return "/unb" + to_string(n->GetUniboardNr()) + "/" + n->GetType() + to_string(n->GetLocalNr()) + "/";
}
std::string Common::string_mm(void)
{
return "mm/";
}
std::string Common::string_peripheral(void)
{
return "dev/";
}
std::vector<int> Common::get_nodes(void)
{
std::vector<int> nodes;
......@@ -134,11 +144,11 @@ bool Common::reg_read(TermOutput& termout, const string addr, const uint offs, c
uint retcnt=0;
auto node = select_node(path_to_node(addr));
std::string rel_addr = addr_to_rel_addr(addr);
std::string relative_addr = addr_to_relative_addr(addr);
termout.strout << "node " << node->GetGlobalNr() << "(" << rel_addr << ")=[";
termout.strout << "node " << node->GetGlobalNr() << "(" << relative_addr << ")=[";
std::vector<int> datavec;
if(node->mread(termout.strhuman,rel_addr,offs,datavec,len)) {
if(node->mread(termout.strhuman,relative_addr,offs,datavec,len)) {
retcnt++;
for(uint i=0;i<datavec.size();i++) {
if(i>0) termout.strout << ",";
......@@ -147,7 +157,24 @@ bool Common::reg_read(TermOutput& termout, const string addr, const uint offs, c
}
termout.strout << "]";
retval = 1;
retval = retcnt;
return retval;
}
bool Common::peripheral_read(TermOutput& termout, const string addr)
{
bool retval=false;
uint retcnt=0;
auto node = select_node(path_to_node(addr));
std::string relative_addr = addr_to_relative_addr(addr);
std::string peripheral = addr_to_peripheral(addr);
termout.strout << "node " << node->GetGlobalNr() << "(" << relative_addr << ")=[";
if(node->get_system_info(termout.strout,peripheral)) retcnt++;
termout.strout << "]";
retval = retcnt;
return retval;
}
......@@ -273,7 +300,7 @@ int Common::path_to_node(std::string path_str)
return node;
}
std::string Common::addr_to_rel_addr(std::string addr)
std::string Common::addr_to_relative_addr(std::string addr)
{
std::string rel_addr;
std::stringstream ss(addr);
......@@ -284,12 +311,32 @@ std::string Common::addr_to_rel_addr(std::string addr)
tokens.push_back(token);
}
// "/unb0/PN3/blah"
// 0 1 2 3
// "/unb0/PN3/mm/rel_addr"
// 0 1 2 3 4
for(uint i=3;i<tokens.size();i++) {
if(i>3) rel_addr += "/";
for(uint i=4;i<tokens.size();i++) {
if(i>4) rel_addr += "/";
rel_addr += tokens[i];
}
return rel_addr;
}
std::string Common::addr_to_peripheral(std::string addr)
{
std::string peripheral;
std::stringstream ss(addr);
std::string token;
std::vector<std::string> tokens;
while (std::getline(ss, token, '/')) {
tokens.push_back(token);
}
// "/unb0/PN3/dev/peripheral"
// 0 1 2 3
if(tokens.size() >= 5) {
peripheral = tokens[4];
}
return peripheral;
}
......@@ -40,16 +40,22 @@ public:
const uint offs, const std::vector<int>& data);
bool reg_read(TermOutput& termout, const std::string addr, const uint offs, const uint len);
bool peripheral_read(TermOutput& termout, const string addr);
std::vector<int> unb_pn__to__nodes(std::vector<int> unbs, std::vector<int> pns);
uint ipaddr_to_id(const string ipaddr);
RegisterMap * get_RegisterMap(TermOutput& termout, int node);
std::string string_node_id(int node);
std::string string_mm(void);
std::string string_peripheral(void);
int path_to_node(std::string path_str);
int unb_name_to_number(const std::string unb_name);
int pn_name_to_number(const std::string pn_name);
std::string addr_to_rel_addr(std::string addr);
std::string addr_to_relative_addr(std::string addr);
std::string addr_to_peripheral(std::string addr);
};
......
......@@ -26,6 +26,7 @@ string p_termout(TermOutput& termout)
s += termout.strerr.str();
s += "}\n";
*/
//cout << "p_termout: " << s << endl;
return s;
}
......@@ -64,15 +65,15 @@ static void addVariable(UA_Server *server)
}
static UA_StatusCode
read_UNB_registers_MethodCallback(UA_Server *server,
const UA_NodeId *sessionId, void *sessionHandle,
const UA_NodeId *methodId, void *methodContext,
const UA_NodeId *objectId, void *objectContext,
size_t inputSize, const UA_Variant *input,
size_t outputSize, UA_Variant *output) {
UNB_registers_MM_MethodCallback(UA_Server *server,
const UA_NodeId *sessionId, void *sessionHandle,
const UA_NodeId *methodId, void *methodContext,
const UA_NodeId *objectId, void *objectContext,
size_t inputSize, const UA_Variant *input,
size_t outputSize, UA_Variant *output) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, (char *)"read_UNB_registers_MethodCallback");
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, (char *)"UNB_registers_MM_MethodCallback");
TermOutput termout(1);
UA_String *inputStr = (UA_String*)input->data;
......@@ -80,11 +81,11 @@ read_UNB_registers_MethodCallback(UA_Server *server,
cout <<"inputStr->data=" << inputStr->data <<endl;
try {
SD->unb->reg_read(termout,std::string((char *)inputStr->data),0,1);
SD->unb->reg_read(termout,std::string((char *)inputStr->data),0,1); // TODO: make read/write
} catch(runtime_error& e) {
cerr << "read_UNB_registers_MethodCallback: " << e.what() << endl;
cerr << "UNB_registers_MM_MethodCallback: " << e.what() << endl;
termout.strout << e.what();
}
p_termout(termout);
UA_String tmp = UA_STRING_ALLOC((char *)p_termout(termout).c_str());
UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
......@@ -97,8 +98,77 @@ read_UNB_registers_MethodCallback(UA_Server *server,
return UA_STATUSCODE_GOOD;
}
static void add_UNB_registers_Method(UA_Server *server, std::string regname)
static void add_UNB_registers_MM_Method(UA_Server *server, std::string regname)
{
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,"add_UNB_registers_MM_Method: %s",
(char *)regname.c_str());
UA_Argument inputArgument;
UA_Argument_init(&inputArgument);
inputArgument.description = UA_LOCALIZEDTEXT((char *)"en-US",(char *) "A String");
inputArgument.name = UA_STRING((char *)"MyInput");
inputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
inputArgument.valueRank = UA_VALUERANK_SCALAR;
UA_Argument outputArgument;
UA_Argument_init(&outputArgument);
outputArgument.description = UA_LOCALIZEDTEXT((char *)"en-US",(char *) "A String");
outputArgument.name = UA_STRING((char *)"MyOutput");
outputArgument.dataType = UA_TYPES[UA_TYPES_STRING].typeId;
outputArgument.valueRank = UA_VALUERANK_SCALAR;
UA_MethodAttributes helloAttr = UA_MethodAttributes_default;
helloAttr.description = UA_LOCALIZEDTEXT((char *)"en-US",(char *)"UNB MM register");
helloAttr.displayName = UA_LOCALIZEDTEXT((char *)"en-US",(char *)"UNB MM register");
helloAttr.executable = true;
helloAttr.userExecutable = true;
UA_Server_addMethodNode(server, UA_NODEID_STRING(1, (char *)regname.c_str()),
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
UA_QUALIFIEDNAME(1, (char *)regname.c_str()),
helloAttr, &UNB_registers_MM_MethodCallback,
1, &inputArgument, 1, &outputArgument, NULL, NULL);
}
static UA_StatusCode
UNB_peripheral_MethodCallback(UA_Server *server,
const UA_NodeId *sessionId, void *sessionHandle,
const UA_NodeId *methodId, void *methodContext,
const UA_NodeId *objectId, void *objectContext,
size_t inputSize, const UA_Variant *input,
size_t outputSize, UA_Variant *output) {
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_SERVER, (char *)"UNB_peripheral_MethodCallback");
TermOutput termout(1);
UA_String *inputStr = (UA_String*)input->data;
if(inputStr->length > 0) {
cout <<"inputStr->data=" << inputStr->data <<endl;
try {
SD->unb->peripheral_read(termout,std::string((char *)inputStr->data)); // TODO: make read/write
} catch(runtime_error& e) {
cerr << "UNB_peripheral_MethodCallback: " << e.what() << endl;
termout.strout << e.what();
}
UA_String tmp = UA_STRING_ALLOC((char *)p_termout(termout).c_str());
UA_Variant_setScalarCopy(output, &tmp, &UA_TYPES[UA_TYPES_STRING]);
UA_String_clear(&tmp);
}
UA_String *outputStr = (UA_String*)output->data;
cout <<"outputStr->data=" << outputStr->data <<endl;
return UA_STATUSCODE_GOOD;
}
static void add_UNB_peripheral_Method(UA_Server *server, std::string regname)
{
UA_LOG_INFO(UA_Log_Stdout, UA_LOGCATEGORY_USERLAND,"add_UNB_peripheral_Method: %s",
(char *)regname.c_str());
UA_Argument inputArgument;
UA_Argument_init(&inputArgument);
inputArgument.description = UA_LOCALIZEDTEXT((char *)"en-US",(char *) "A String");
......@@ -114,15 +184,15 @@ static void add_UNB_registers_Method(UA_Server *server, std::string regname)
outputArgument.valueRank = UA_VALUERANK_SCALAR;
UA_MethodAttributes helloAttr = UA_MethodAttributes_default;
helloAttr.description = UA_LOCALIZEDTEXT((char *)"en-US",(char *)"UNB register");
helloAttr.displayName = UA_LOCALIZEDTEXT((char *)"en-US",(char *)"UNB register");
helloAttr.description = UA_LOCALIZEDTEXT((char *)"en-US",(char *)"UNB peripheral");
helloAttr.displayName = UA_LOCALIZEDTEXT((char *)"en-US",(char *)"UNB peripheral");
helloAttr.executable = true;
helloAttr.userExecutable = true;
UA_Server_addMethodNode(server, UA_NODEID_STRING(1, (char *)regname.c_str()),
UA_NODEID_NUMERIC(0, UA_NS0ID_OBJECTSFOLDER),
UA_NODEID_NUMERIC(0, UA_NS0ID_HASORDEREDCOMPONENT),
UA_QUALIFIEDNAME(1, (char *)regname.c_str()),
helloAttr, &read_UNB_registers_MethodCallback,
helloAttr, &UNB_peripheral_MethodCallback,
1, &inputArgument, 1, &outputArgument, NULL, NULL);
}
......@@ -130,7 +200,7 @@ int ua_server(Serverdat *sd) {
SD = sd;
TermOutput termout(1);
std::vector<int> nodes = SD->unb->get_nodes();
std::vector<int> nodes = SD->unb->get_nodes(); // all nodes
UA_Server *mUaServer = UA_Server_new();
......@@ -141,10 +211,17 @@ int ua_server(Serverdat *sd) {
for(auto node : nodes) {
RegisterMap *regmap = SD->unb->get_RegisterMap(termout, node);
std::vector<std::string> regnames = regmap->getRegnames(SD->unb->string_node_id(node));
std::string mm_prefix = SD->unb->string_node_id(node) + SD->unb->string_mm();
std::vector<std::string> regnames = regmap->getRegnames(mm_prefix);
for(auto m : regnames) {
add_UNB_registers_Method(mUaServer, m);
add_UNB_registers_MM_Method(mUaServer, m);
}
std::string peripheral_prefix = SD->unb->string_node_id(node) + SD->unb->string_peripheral();
add_UNB_peripheral_Method(mUaServer, peripheral_prefix + "name");
add_UNB_peripheral_Method(mUaServer, peripheral_prefix + "stamps");
add_UNB_peripheral_Method(mUaServer, peripheral_prefix + "note");
add_UNB_peripheral_Method(mUaServer, peripheral_prefix + "sensors");
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment