16 Remote::Remote(Programmer *prog) : connected(false),
17 clock(1000), programmer(prog), manager(prog)
19 if(programmer == NULL) return;
21 programmer->SetTransferSpeed(1000);
24 WSAStartup(0x202,&data);*/
27 bool Remote::ReceiveData(char* buf, int len)
32 int r = recv(clientfd,&buf[readed],len,0);
40 bool Remote::WaitForClient()
42 struct sockaddr_in serv_addr;
44 serverfd = socket(AF_INET, SOCK_STREAM, 0);
47 memset((char *) &serv_addr,0, sizeof(serv_addr));
48 serv_addr.sin_family = AF_INET;
49 serv_addr.sin_addr.s_addr = INADDR_ANY;
50 serv_addr.sin_port = htons(10122);
51 if (bind(serverfd, (struct sockaddr *) &serv_addr,
52 sizeof(serv_addr)) < 0)
56 clientfd = accept(serverfd, 0, 0);
57 connected = clientfd >= 0;
61 void Remote::SendOK(int len)
63 unsigned int size = htonl(len);
64 unsigned short status = 0;
65 send(clientfd,(char*)&size,4,0);
66 send(clientfd,(char*)&status,2,0);
69 void Remote::SendFail()
71 unsigned int size = htonl(2);
72 unsigned short status = 1;
73 send(clientfd,(char*)&size,4,0);
74 send(clientfd,(char*)&status,2,0);
77 void Remote::Lock(char *data)
87 void Remote::Command(char* data)
89 if(!strcmp("SPISLOW",data))
92 programmer->SetTransferSpeed(clock);
98 void Remote::GetVar(char* data)
102 if(!strcmp("SPIMAXCLOCK",data))
104 else if(!strcmp("SPICLOCK",data))
105 val = static_cast<ostringstream*>(&(ostringstream() << clock))->str();
106 else //unknown variable
108 cout << "GetVar - UNKNOWN: " << data << endl;
113 unsigned int length = val.length() + 3;
116 send(clientfd,val.c_str(),length-2,0);
119 int Remote::SequenceSetVar(char* data, int len)
125 while(*data >= 'A' && *data <= 'Z' && len > 0)
131 if(len > 0 && *data == 0)
137 while(*data >= '0' && *data <= '9' && len > 0)
143 if(len > 0 && *data == 0)
146 nValue = strtol(value.c_str(),NULL,10);
148 if(!var.compare("SPICLOCK"))
151 programmer->SetTransferSpeed(clock);
157 void Remote::Sequence(char* data, int len)
159 unsigned short *buf = (unsigned short*)data;
160 struct retdata *tmp = NULL;
161 list<struct retdata*> ret;
162 list<struct retdata*>::iterator it;
169 for(int i=0;i<buf[0] && !failed;i++) {
180 tmp = new struct retdata;
181 tmp->len = buf[ptr+1];
182 tmp->data = new unsigned short[tmp->len];
185 cout << "Sequence - Read: 0x" << hex << buf[ptr] << " 0x" << buf[ptr+1] << dec << endl;
187 if(!programmer->ReadBlock(buf[ptr],buf[ptr+1],tmp->data))
193 resplen += tmp->len*2;
200 if(len < 6 || len < buf[ptr+1]*2)
206 cout << "Sequence - Write: 0x" << hex << buf[ptr] << " 0x" << buf[ptr+1] << dec << endl;
208 if(!programmer->WriteBlock(buf[ptr],buf[ptr+1],&buf[ptr+2]))
214 len -= 4+buf[ptr+1]*2;
218 int p = SequenceSetVar(&data[ptr*2],len);
229 for (it=ret.begin(); it != ret.end(); ++it)
230 send(clientfd,(char*)(*it)->data,(*it)->len*2,0);
234 for (it=ret.begin(); it != ret.end(); ++it)
241 void Remote::SetCoreType(char* data)
243 coretype = *(unsigned short*)data;
247 void Remote::GetCoreType()
250 send(clientfd,(char*)&coretype,2,0);
253 void Remote::IsXapStopped()
255 cout << "IsXapStopped" << endl;
256 unsigned short stopped = programmer->IsXAPStopped();
258 send(clientfd,(char*)&stopped,2,0);
261 void Remote::ResetAndStop()
263 cout << "ResetAndStop" << endl;
264 if(manager.XapResetAndStop())
272 cout << "Stop" << endl;
273 if(manager.XapStop())
281 cout << "Go" << endl;
288 void Remote::ResetAndGo()
290 cout << "ResetAndGo" << endl;
291 if(manager.XapResetAndGo())
297 void Remote::GetProcType()
300 send(clientfd,(char*)&proctype,2,0);
303 void Remote::SetProcType(char *data)
305 proctype = *(unsigned short*)data;
306 cout << "Proc type: " << proctype << endl;
312 if(!programmer || !programmer->IsConnected()) {
313 cout << "Programmer is not set or initialized!" << endl;
317 unsigned int datalen;
322 if(!connected) return;
326 if(!ReceiveData((char*)&datalen,4))
328 datalen = ntohl(datalen);
330 if(!ReceiveData(cmd,4))
334 data = new char[datalen];
335 if(!ReceiveData(data,datalen))
341 if(!strcmp("lock",cmd))
343 else if(!strcmp("getv",cmd))
345 else if(!strcmp("sequ",cmd))
346 Sequence(data,datalen);
347 else if(!strcmp("unlk",cmd))
349 else if(!strcmp("cmnd",cmd))
351 else if(!strcmp("stct",cmd))
353 else if(!strcmp("stpd",cmd))
355 else if(!strcmp("gtct",cmd))
357 else if(!strcmp("rsst",cmd))
359 else if(!strcmp("stop",cmd))
361 else if(!strcmp("go ",cmd))
363 else if(!strcmp("rsgo",cmd))
365 else if(!strcmp("gtpt",cmd))
367 else if(!strcmp("stpt",cmd))
369 else if(!strcmp("ver ",cmd))
373 cout << "Unknown command: " << cmd << endl;
374 cout << "Skipping " << datalen << " bytes..." << endl;
382 void Remote::Disconnect()
385 closesocket(clientfd);
387 closesocket(serverfd);*/