Commit f95876da authored by Leon Hiemstra's avatar Leon Hiemstra

added UPE python serive ports (read works)

parent 556915bc
......@@ -344,7 +344,7 @@ CMDstatus CMD::Mwrite(int argc, char* argv[], TermOutput& termout, Serverdat *sd
if (vm.count("help")) {
termout.strout << "usage: " << MWRITE << " [options] file(s)" << endl
<< "Options: " << generic << endl
<< "Example: mwrite --offs=1 --data=[1,2,3,4] /unb0/pn3/mm/sens/sens/temp_high" << endl;
<< "Example: write --offs=1 --data=[1,2,3,4] /unb0/pn3/mm/sens/sens/temp_high" << endl;
return {CMD_STATUS_OK,0,0};
} else if (vm.count("file")) {
string addr_str = "";
......@@ -360,13 +360,13 @@ CMDstatus CMD::Mwrite(int argc, char* argv[], TermOutput& termout, Serverdat *sd
data.resize(data64.size());
for(uint i=0; i<data64.size(); i++) {
if((data64[i] & 0xffffffff00000000) != 0) {
cout << "cmd() mwrite() warning 64bit value detected, truncating!!!" << endl;
termout.strerr << "cmd() mwrite() warning 64bit value detected, truncating!!!" << endl;
cout << "cmd() write() warning 64bit value detected, truncating!!!" << endl;
termout.strerr << "cmd() write() warning 64bit value detected, truncating!!!" << endl;
}
data[i]=(int)data64[i];
}
}
if(sd->unb->mwrite(termout, addr_str, offs, data)) ret.status=CMD_STATUS_OK;
if(sd->unb->write(termout, addr_str, offs, data)) ret.status=CMD_STATUS_OK;
} else throw runtime_error("missing argument(s)");
} catch(po::error& e) {
termout.strerr << e.what() << endl;
......
......@@ -52,7 +52,7 @@ private:
const std::string QUIT="quit";
const std::string INFO="ls";
const std::string MREAD="read";
const std::string MWRITE="mwrite";
const std::string MWRITE="write";
const std::string RELOAD="reload";
......
......@@ -71,6 +71,11 @@ void Common::print_node_id(ostringstream& strs, Node *node)
<< node->GetLocalNr() << "/" << endl;
}
uint Common::node_number(Node *node)
{
return (node->GetUniboardNr() * 4) + node->GetLocalNr();
}
std::string Common::string_node_id(int node)
{
auto n = select_node(node);
......@@ -105,7 +110,7 @@ bool Common::monitor(TermOutput& termout)
if(idx > 0) termout.strout << ",";
termout.strout << "[";
try {
if(node->composite_call(termout.strout, "system")) {
if(node->cread(termout.strout, "system")) {
retcnt++;
}
} catch(std::exception& e) {
......@@ -128,11 +133,13 @@ bool Common::read(TermOutput& termout, const string addr, const uint offs, const
std::string type = addr_to_type(addr);
std::string peripheral = addr_to_peripheral(addr);
termout.strout << "[";
for(uint idx=0;idx<nodes.size();idx++) {
auto node = select_node(nodes[idx]);
print_node_id(termout.strout,node);
//print_node_id(termout.strout,node);
uint node_nr = node_number(node);
if(idx > 0) termout.strout << ",";
termout.strout << "[";
termout.strout << "(" << node_nr << ",0,";
if(type == "mm") {
RegisterMap *regmap = node->get_RegisterMap(termout.strout);
......@@ -142,7 +149,7 @@ bool Common::read(TermOutput& termout, const string addr, const uint offs, const
retcnt++;
}
} else if(type == "dev") {
if(node->composite_call(termout.strout, peripheral)) {
if(node->cread(termout.strout, peripheral)) {
retcnt++;
} else {
termout.strerr << "no such peripheral: " << peripheral;
......@@ -150,36 +157,55 @@ bool Common::read(TermOutput& termout, const string addr, const uint offs, const
} else {
termout.strerr << "no such type: " << type;
}
termout.strout << "]";
termout.strout << ")";
}
termout.strout << "]";
retval = (retcnt==nodes.size());
if(nodes.size() == 0) {
termout.strerr << "no nodes";
retval = false;
}
return retval;
}
bool Common::mwrite(TermOutput& termout, const string addr,
bool Common::write(TermOutput& termout, const string addr,
const uint offs, const std::vector<int>& data)
{
bool retval = false;
uint retcnt = 0;
std::vector<int> nodes;
std::vector<int> nodes = path_to_node(addr);
std::string relative_addr = addr_to_relative_addr(addr);
std::string type = addr_to_type(addr);
std::string peripheral = addr_to_peripheral(addr);
termout.strout << "[";
for(uint idx = 0; idx<nodes.size(); idx++) {
auto node = select_node(nodes[idx]);
print_node_id(termout.strout, node);
//print_node_id(termout.strout, node);
uint node_nr = node_number(node);
if(idx > 0) termout.strout << ",";
termout.strout << "[" << node->GetGlobalNr() << "],";
termout.strout << "(" << node_nr << ",0,";
termout.strout << "[";
for(uint i = 0; i < data.size(); i++) {
if(i > 0) termout.strout << ",";
termout.strout << data[i];
if(type == "mm") {
if(node->mwrite(termout.strout, addr, offs, data)) retcnt++;
} else if(type == "dev") {
if(node->cwrite(termout.strout, peripheral)) {
retcnt++;
} else {
termout.strerr << "no such peripheral: " << peripheral;
}
} else {
termout.strerr << "no such type: " << type;
}
termout.strout << "]";
termout.strout << ")";
if(node->mwrite(termout.strout,addr, offs, data)) retcnt++;
}
retval = (retcnt==nodes.size());
if(nodes.size() == 0) {
termout.strerr << "no nodes";
retval = false;
}
return retval;
}
......@@ -233,8 +259,8 @@ std::vector<int> Common::unb_name_to_number(const std::string unb_name)
try {
unbs.push_back(std::stoi(unb_range, nullptr));
} catch (const std::invalid_argument& ia) {
cerr << "Common::unb_name_to_number: Invalid argument: "
<< ia.what() << " - but trying vector..." << endl;
//cerr << "Common::unb_name_to_number: Invalid argument: "
// << ia.what() << " - but trying vector..." << endl;
istringstream vecstr(unb_range);
unbs=parse_int_vector_range(vecstr);
}
......@@ -253,8 +279,8 @@ std::vector<int> Common::pn_name_to_number(const std::string pn_name)
try {
pns.push_back(std::stoi(pn_range,nullptr));
} catch (const std::invalid_argument& ia) {
cerr << "Common::pn_name_to_number: Invalid argument: "
<< ia.what() << " - but trying vector..." << endl;
//cerr << "Common::pn_name_to_number: Invalid argument: "
// << ia.what() << " - but trying vector..." << endl;
istringstream vecstr(pn_range);
pns=parse_int_vector_range(vecstr);
}
......
......@@ -42,6 +42,7 @@ private:
Node * select_node(const int nodeId);
void print_node_id(ostringstream& strs, Node *node);
uint node_number(Node *node);
public:
Common(std::list<class Node*>& nodelist);
......@@ -50,7 +51,7 @@ public:
std::vector<int> get_nodes(void);
bool monitor(TermOutput& termout);
bool mwrite(TermOutput& termout, const std::string addr,
bool write(TermOutput& termout, const std::string addr,
const uint offs, const std::vector<int>& data);
bool read(TermOutput& termout, const string addr, const uint offs, const int len);
......
......@@ -63,7 +63,7 @@ Node::~Node()
delete periph_system;
}
bool Node::composite_call(ostringstream& strs,const string peripheral) {
bool Node::cread(ostringstream& strs,const string peripheral) {
if(peripheral == "system") {
return periph_system->read_system_info(strs);
} else if(peripheral == "name") {
......@@ -82,6 +82,11 @@ bool Node::composite_call(ostringstream& strs,const string peripheral) {
else return false;
}
bool Node::cwrite(ostringstream& strs,const string peripheral) {
strs << "cwrite() not yet implemented TODO";
return false;
}
RegisterMap * Node::get_RegisterMap(std::ostringstream& strs)
{
return periph_system->getRegisterMap();
......
......@@ -82,7 +82,8 @@ class Node {
const uint GetNr() { return LocalNr; }
const std::string GetType() { return Type; }
bool composite_call(std::ostringstream& strs, const std::string peripheral);
bool cread(std::ostringstream& strs, const std::string peripheral);
bool cwrite(std::ostringstream& strs, const std::string peripheral);
bool mread(std::ostringstream& strs,const std::string addr, const uint offs,
std::vector<int>& data, const uint len);
bool mwrite(std::ostringstream& strs,const std::string addr, const uint offs,
......
......@@ -55,11 +55,11 @@ Serverdat SD;
string print_termout(TermOutput& termout)
{
string s = "output={\n";
string s = "output={";
s += termout.strout.str();
s += "}\n";
s += "errors={\n";
s += "errors={";
s += termout.strerr.str();
s += "}\n";
return s;
......@@ -76,26 +76,29 @@ void monitor(void)
#define c_NOF_RETRIES_MONITOR 3
int retries = c_NOF_RETRIES_MONITOR;
// while(ServerRunning) {
// usleep(1000000);
// }
while(ServerRunning) {
usleep(1000000);
while(!SD.monitor_mutex.try_lock()) { cerr << "mutex not ready\n"; usleep(100000); }
if(!SD.unb->monitor(termout)) {
retries--;
cerr << "Retrying " << retries << endl;
} else {
retries = c_NOF_RETRIES_MONITOR;
}
SD.monitor_mutex.unlock();
usleep(1000000);
while(!SD.monitor_mutex.try_lock()) { cerr << "mutex not ready\n"; usleep(100000); }
if(!SD.unb->monitor(termout)) {
retries--;
cerr << "Retrying " << retries << endl;
} else {
retries = c_NOF_RETRIES_MONITOR;
}
SD.monitor_mutex.unlock();
if(cmdstatusnew.status != CMD_EMPTY) cmdstatus = cmdstatusnew;
cout << "Monitor thread: " << print_termout(termout) << endl;
termout.clear();
if(cmdstatusnew.status != CMD_EMPTY) cmdstatus = cmdstatusnew;
cout << "Monitor thread: " << print_termout(termout) << endl;
termout.clear();
if(retries <= 0) {
//cerr << "Re-initializing, Read register maps again!" << endl;
//raise(SIGHUP)
}
if(retries <= 0) {
//cerr << "Re-initializing, Read register maps again!" << endl;
//raise(SIGHUP)
}
}
}
......@@ -121,7 +124,6 @@ void control_server(TCPSSocket *sock, const int clientId, std::atomic<size_t> *n
while(!quit) {
string prompt = "sdpunb:" + cmdname + ":" + to_string(cmdstatus.status) + ":CMD>";
SD.controlSocket[clientId]->tx((unsigned char *)prompt.c_str(), strlen(prompt.c_str()));
int len = SD.controlSocket[clientId]->rx(tcpbuf, c_TCP_CONTROL_BUFFERSIZE);
if(len < 1) {
quit = true;
......
......@@ -227,7 +227,8 @@ RegisterMap ccfg_upe_to_regmap_add(std::istringstream& iss, RegisterMap &regmap)
ss >> c; // 'a'
ss >> c; // 'n'
ss >> c; // '='
ss >> hex >> peripheral_span; // in dwords
ss >> hex >> peripheral_span;
peripheral_span /= 4; // byte to dwords addressing
stage = 1;
......
This diff is collapsed.
#!/usr/bin/env python
import time
import socket
import struct
class UNBclientIO():
def __init__(self, serverhost, tcpport=3335):
self.myName = 'UNBclientIO'
self.pId = self.myName + ' - '
self.tcpport = tcpport
self.serverhost = serverhost
self.serverhost = 'localhost'
self.s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
self.s.connect((self.serverhost, self.tcpport))
(ret,banner) = self.read_section('output')
self.read_prompt('')
print("Connected to unbserver on: ",self.serverhost," (tcpport=",self.tcpport,")")
print("Received banner: ",banner)
def __del__(self):
self.s.close()
def verify_section_name(self,buf,section_name):
ret=False
result=buf
#print("verify_section_name=",result)
col=buf.split('=')
verifyname=col[0].strip('\n')
if verifyname == section_name:
ret=True
result=buf.strip('}')
col=result.split('{')
result=col[1]
return (ret,result)
def prefixok(self,buf):
col=buf.split(':')
prefix=col[0].strip('\n')
return prefix == 'sdpunb'
def cmdname(self,buf):
col=buf.split(':')
return col[1]
def cmdok(self,buf):
col=buf.split(':')
print("cmdok=",col[2])
return col[2] == '0'
def read_until_eos(self,eos):
new_in = 0
can_read = 0
while new_in==0:
buf = self.s.recv(600000, socket.MSG_PEEK)
if not buf:
print('server is disconnected')
break
else:
i=0
for c in buf:
if chr(c)==eos:
can_read=i+1
new_in=1
break
else:
i+=1
buf = self.s.recv(can_read)
print('received:',buf)
return buf.decode('utf-8')
def read_section(self,section):
result_found=False
response=''
while result_found==False:
buf=self.read_until_eos('}')
if not buf:
print('server is disconnected')
result_found=True
break
else:
(result_found,response)=self.verify_section_name(buf,section)
if result_found==False:
print('Unexpected Response of section=',section)
return (result_found,response)
def read_prompt(self,cmdname):
result_found=False
response=''
while result_found==False:
buf=self.read_until_eos('>')
if not buf:
print('server is disconnected')
result_found=True
break
else:
if self.prefixok(buf) == False:
print('Unexpected Response, Prefix NOT OK')
else:
if self.cmdname(buf) == cmdname:
result_found=True
return self.cmdok(buf)
def find_response(self, cmdname=''):
(ret,out) = self.read_section('output')
(ret,err) = self.read_section('errors')
if cmdname != '':
ret=self.read_prompt(cmdname)
return (ret,out,err)
#==================================================================================
def nodes_to_unb_pn(self, nodes):
unbs=[]
pns=[]
for n in nodes:
unbs.append(n >> 3)
pns.append(n % 4)
u_unbs = list(set(unbs)) # use only unique values
u_pns = list(set(pns))
return (u_unbs, u_pns)
def mwrite(self, nodes, addrname, offset, data):
cmdname='write'
ret=False
(unbs,pns) = self.nodes_to_unb_pn(nodes)
cmd = cmdname + ' --offs=' + str(offset)
cmd += ' --data=' + str(data).replace(" ","")
cmd += ' /unb' + str(unbs).replace(" ","") + '/pn' + str(pns).replace(" ","")
cmd += '/mm/' + addrname
print("send:",cmd)
my_cmd_as_bytes = str.encode(cmd + '\n')
self.s.send(my_cmd_as_bytes)
(ret,out,err) = self.find_response(cmdname)
err=err.strip('\n')
print('err=',err)
print('out=',out)
print('ret=',ret)
return (ret,out,err.strip('\n'))
def mread(self, nodes, addrname, offset, nvalues):
cmdname='read'
ret=False
(unbs,pns) = self.nodes_to_unb_pn(nodes)
cmd = cmdname + ' --nvalues=' + str(nvalues)
cmd += ' --offs=' + str(offset)
cmd += ' /unb' + str(unbs).replace(" ","") + '/pn' + str(pns).replace(" ","")
cmd += '/mm/' + addrname
print("send:",cmd)
my_cmd_as_bytes = str.encode(cmd + '\n')
self.s.send(my_cmd_as_bytes)
(ret,out,err) = self.find_response(cmdname)
err=err.strip('\n')
print('err=',err)
print('out=',out)
print('ret=',ret)
if ret == True:
node_and_value=eval(out)
else:
node_and_value=[]
for n in nodes:
node_and_value.append((n,1,[])) # FIXME: sdpunb server better do this
return (ret,node_and_value,err.strip('\n'))
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