]> git.tdb.fi Git - r2c2.git/blob - source/libr2c2/centralstation.cpp
Fix MFX speedsteps in Central Station driver
[r2c2.git] / source / libr2c2 / centralstation.cpp
1 /* $Id$
2
3 This file is part of R²C²
4 Copyright © 2010  Mikkosoft Productions, Mikko Rasa
5 Distributed under the GPL
6 */
7
8 #include <algorithm>
9 #include <msp/core/refptr.h>
10 #include <msp/io/print.h>
11 #include <msp/net/resolve.h>
12 #include <msp/strings/utils.h>
13 #include <msp/time/units.h>
14 #include <msp/time/utils.h>
15 #include "centralstation.h"
16 #include "tracktype.h"
17 #include "vehicletype.h"
18
19 using namespace std;
20 using namespace Msp;
21
22 namespace R2C2 {
23
24 CentralStation::CentralStation(const string &host):
25         socket(Net::INET),
26         pending_commands(0),
27         power(false),
28         halted(false),
29         locos_synced(false),
30         turnouts_synced(false),
31         sensors_synced(false)
32 {
33         RefPtr<Net::SockAddr> addr = Net::resolve(host+":15471");
34         socket.connect(*addr);
35
36         IO::print("Connected to central station at %s\n", addr->str());
37
38         command("get(1, status)");
39         command("request(1, view)");
40         command("queryObjects(10, addr, name)");
41         command("queryObjects(11, addr)");
42         command("queryObjects(26)");
43 }
44
45 CentralStation::~CentralStation()
46 {
47         command("release(1, view)", true);
48         for(LocoMap::iterator i=locos.begin(); (i!=locos.end() && !(i->first&0x10000)); ++i)
49                 command(format("release(%d, view, control)", i->first));
50         for(TurnoutMap::iterator i=turnouts.begin(); (i!=turnouts.end() && !(i->first&0x10000)); ++i)
51                 command(format("release(%d, view, control)", i->first));
52         while(IO::poll(socket, IO::P_INPUT, 100*Time::msec))
53                 while(receive()) ;
54 }
55
56 void CentralStation::set_power(bool p)
57 {
58         power = p;
59         command(format("set(1, %s)", (power ? "go" : "stop")));
60 }
61
62 void CentralStation::halt(bool h)
63 {
64         halted = h;
65         if(halted)
66         {
67                 for(LocoMap::iterator i=locos.begin(); i!=locos.end(); ++i)
68                         if(i->second.speed)
69                                 set_loco_speed(i->first, 0);
70         }
71
72         signal_halt.emit(halted);
73 }
74
75 const char *CentralStation::enumerate_protocols(unsigned index) const
76 {
77         if(index==MM)
78                 return "MM";
79         else if(index==MM_27)
80                 return "MM-27";
81         else if(index==MFX)
82                 return "MFX";
83         else
84                 return 0;
85 }
86
87 unsigned CentralStation::get_protocol_speed_steps(const string &name) const
88 {
89         switch(map_protocol(name))
90         {
91         case MM: return 14;
92         case MM_27: return 27;
93         case MFX: return 126;
94         default: return 0;
95         }
96 }
97
98 void CentralStation::add_loco(unsigned addr, const string &proto_name, const VehicleType &type)
99 {
100         Protocol proto = map_protocol(proto_name);
101
102         unsigned id = map_address(locos, loco_addr, addr);
103         if(!id)
104         {
105                 Locomotive &loco = locos[addr|0x10000];
106                 loco.name = type.get_name();
107                 loco.protocol = proto;
108                 loco.address = addr;
109
110                 if(locos_synced && proto!=MFX)
111                         command("create(10)");
112         }
113         else
114                 command(format("request(%d, view, control, force)", id));
115 }
116
117 void CentralStation::set_loco_speed(unsigned addr, unsigned speed)
118 {
119         if(speed && halted)
120                 return;
121
122         unsigned id = map_address(locos, loco_addr, addr);
123         if(id)
124         {
125                 Locomotive &loco = locos[id];
126                 if(loco.protocol==MFX && speed)
127                         ++speed;
128                 command(format("set(%d, speedstep[%d])", id, speed));
129         }
130 }
131
132 void CentralStation::set_loco_reverse(unsigned addr, bool rev)
133 {
134         unsigned id = map_address(locos, loco_addr, addr);
135         if(id)
136                 command(format("set(%d, dir[%d])", id, rev));
137 }
138
139 void CentralStation::set_loco_function(unsigned addr, unsigned func, bool state)
140 {
141         unsigned id = map_address(locos, loco_addr, addr);
142         if(id)
143                 command(format("set(%d, func[%d, %d])", id, func, state));
144 }
145
146 void CentralStation::add_turnout(unsigned addr, const TrackType &type)
147 {
148         bool left = false;
149         bool right = false;
150         bool cross = false;
151
152         const vector<TrackPart> &parts = type.get_parts();
153         for(vector<TrackPart>::const_iterator i=parts.begin(); i!=parts.end(); ++i)
154         {
155                 TrackPoint start = i->get_point(0);
156                 TrackPoint end = i->get_point(i->get_length());
157                 if(end.dir>start.dir+0.01)
158                         left = true;
159                 else if(end.dir<start.dir-0.01)
160                         right = true;
161                 else if(start.dir<-0.01 || start.dir>0.01)
162                         cross = true;
163         }
164
165         unsigned symbol = Turnout::LEFT;
166         if(cross)
167                 symbol = Turnout::DOUBLESLIP;
168         else if(left && right)
169                 symbol = Turnout::THREEWAY;
170         else if(left)
171                 symbol = Turnout::LEFT;
172         else if(right)
173                 symbol = Turnout::RIGHT;
174
175         unsigned id = map_address(turnouts, turnout_addr, addr);
176         if(!id)
177         {
178                 id = addr|0x10000;
179
180                 Turnout &turnout = turnouts[id];
181                 turnout.address = addr;
182                 turnout.bits = type.get_state_bits();
183                 turnout.symbol = symbol;
184
185                 turnout_addr[addr] = id;
186
187                 if(turnouts_synced)
188                         command("create(11, append)");
189         }
190         else
191         {
192                 Turnout &turnout = turnouts[id];
193                 command(format("request(%d, view, control)", id));
194                 if(turnout.symbol!=symbol)
195                         command(format("set(%d, symbol[%d])", symbol));
196         }
197 }
198
199 void CentralStation::set_turnout(unsigned addr, unsigned state)
200 {
201         unsigned id = map_address(turnouts, turnout_addr, addr);
202         if(id)
203         {
204                 Turnout &turnout = turnouts[id];
205                 unsigned mask = (1<<turnout.bits)-1;
206
207                 if(((state^turnout.state)&mask)==0 || !turnout.synced)
208                 {
209                         turnout.state = state;
210                         signal_turnout.emit(addr, turnout.state);
211                         return;
212                 }
213
214                 turnout.state = (turnout.state&mask) | (state&~mask);
215
216                 command(format("set(%d, state[%d])", id, state&mask));
217         }
218 }
219
220 unsigned CentralStation::get_turnout(unsigned addr) const
221 {
222         unsigned id = map_address(turnouts, turnout_addr, addr);
223         if(id)
224         {
225                 TurnoutMap::const_iterator i = turnouts.find(id);
226                 if(i!=turnouts.end())
227                         return i->second.state;
228         }
229         return 0;
230 }
231
232 void CentralStation::add_sensor(unsigned addr)
233 {
234         sensors.insert(SensorMap::value_type(addr, Sensor()));
235
236         if(sensors_synced)
237         {
238                 if(addr>s88.size()*16)
239                         command("create(26, add[0])");
240         }
241 }
242
243 bool CentralStation::get_sensor(unsigned addr) const
244 {
245         SensorMap::const_iterator i = sensors.find(addr);
246         if(i!=sensors.end())
247                 return i->second.state;
248         return false;
249 }
250
251 void CentralStation::tick()
252 {
253         Time::TimeStamp t = Time::now();
254         for(SensorMap::iterator i=sensors.begin(); i!=sensors.end(); ++i)
255                 if(i->second.off_timeout && t>i->second.off_timeout)
256                 {
257                         i->second.state = false;
258                         i->second.off_timeout = Time::TimeStamp();
259                         signal_sensor.emit(i->first, i->second.state);
260                 }
261
262         while(Message msg = receive())
263         {
264                 if(msg.footer.code)
265                         IO::print("\033[31m*** ERROR: %s: %d %s ***\033[0m\n", msg.header.value, msg.footer.code, msg.footer.value);
266
267                 if(msg.header.type=="REPLY")
268                         process_reply(msg);
269                 else if(msg.header.type=="EVENT")
270                         process_event(msg);
271         }
272 }
273
274 void CentralStation::flush()
275 {
276 }
277
278 void CentralStation::command(const string &cmd, bool force)
279 {
280         if(pending_commands<10 || force)
281         {
282                 socket.write(cmd+"\r\n");
283                 ++pending_commands;
284         }
285         else
286                 cmd_queue.push_back(cmd);
287 }
288
289 CentralStation::Message CentralStation::receive()
290 {
291         while(IO::poll(socket, IO::P_INPUT, Time::zero))
292         {
293                 char rbuf[1024];
294                 unsigned len = socket.read(rbuf, sizeof(rbuf));
295                 if(!len)
296                         return Message();
297
298                 in_buffer.append(rbuf, len);
299         }
300
301         if(!in_buffer.empty())
302         {
303                 string::iterator iter = in_buffer.begin();
304                 if(Message msg = parse_message(iter, in_buffer.end()))
305                 {
306                         skip(iter, in_buffer.end(), "\r\n");
307                         in_buffer.erase(in_buffer.begin(), iter);
308
309                         if(msg.header.type=="REPLY" && pending_commands>0)
310                         {
311                                 --pending_commands;
312                                 if(!cmd_queue.empty())
313                                 {
314                                         command(cmd_queue.front());
315                                         cmd_queue.pop_front();
316                                 }
317                         }
318
319                         return msg;
320                 }
321         }
322
323         return Message();
324 }
325
326 void CentralStation::process_reply(const Message &msg)
327 {
328         if(!msg.header.value.compare(0, 4, "get("))
329         {
330                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
331                 {
332                         if(turnouts.count(i->first))
333                                 turnouts[i->first].synced = true;
334
335                         process_object(i->first, i->second);
336                 }
337         }
338         else if(!msg.header.value.compare(0, 16, "queryObjects(10,"))
339         {
340                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
341                 {
342                         LocoMap::iterator j = locos.find(i->first);
343                         if(j==locos.end())
344                         {
345                                 bool found = false;
346                                 Message::AttribMap::const_iterator k = i->second.find("addr");
347                                 if(k!=i->second.end())
348                                 {
349                                         unsigned addr = lexical_cast<unsigned>(k->second);
350
351                                         j = locos.find(addr|0x10000);
352                                         if(j!=locos.end())
353                                         {
354                                                 command(format("request(%d, view, control, force)", i->first));
355                                                 command(format("get(%d, dir, func[0])", i->first));
356
357                                                 locos.insert(LocoMap::value_type(i->first, j->second));
358                                                 locos.erase(j);
359
360                                                 found = true;
361                                         }
362                                 }
363
364                                 if(!found)
365                                         locos.insert(LocoMap::value_type(i->first, Locomotive()));
366                         }
367
368                         process_object(i->first, i->second);
369                 }
370
371                 locos_synced = true;
372
373                 if(locos.lower_bound(0x10000)!=locos.end())
374                         command("create(10)");
375         }
376         else if(!msg.header.value.compare(0, 16, "queryObjects(11,"))
377         {
378                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
379                 {
380                         TurnoutMap::iterator j = turnouts.find(i->first);
381                         if(j==turnouts.end())
382                         {
383                                 bool found = false;
384                                 Message::AttribMap::const_iterator k = i->second.find("addr");
385                                 if(k!=i->second.end())
386                                 {
387                                         unsigned addr = lexical_cast<unsigned>(k->second);
388
389                                         j = turnouts.find(addr|0x10000);
390                                         if(j!=turnouts.end())
391                                         {
392                                                 command(format("request(%d, view, control)", i->first));
393                                                 command(format("set(%d, symbol[%d])", i->first, j->second.symbol));
394                                                 command(format("get(%d, state)", i->first));
395
396                                                 turnouts.insert(TurnoutMap::value_type(i->first, j->second));
397                                                 turnouts.erase(j);
398
399                                                 found = true;
400                                         }
401                                 }
402
403                                 if(!found)
404                                         turnouts.insert(TurnoutMap::value_type(i->first, Turnout()));
405                         }
406
407                         process_object(i->first, i->second);
408                 }
409
410                 turnouts_synced = true;
411
412                 for(TurnoutMap::const_iterator i=turnouts.lower_bound(0x10000); i!=turnouts.end(); ++i)
413                         command("create(11, append)");
414         }
415         else if(msg.header.value=="queryObjects(26)")
416         {
417                 s88.clear();
418                 for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
419                 {
420                         s88.push_back(i->first);
421                         command(format("request(%d, view)", i->first));
422                 }
423
424                 sensors_synced = true;
425
426                 if(!sensors.empty())
427                 {
428                         unsigned high_addr = (--sensors.end())->first;
429                         if(high_addr>16*s88.size())
430                                 command("create(26, add[0])");
431                 }
432         }
433         else if(msg.header.value=="create(10)")
434         {
435                 Message::ObjectMap::const_iterator i = msg.content.find(10);
436                 if(i!=msg.content.end())
437                 {
438                         Message::AttribMap::const_iterator j = i->second.find("id");
439                         if(j!=i->second.end())
440                         {
441                                 unsigned id = lexical_cast<unsigned>(j->second);
442                                 LocoMap::iterator k = locos.lower_bound(0x10000);
443                                 if(k!=locos.end())
444                                 {
445                                         command(format("request(%d, view, control)", id));
446                                         command(format("set(%d, addr[%d], protocol[%s], name[\"%s\"])",
447                                                 id, k->second.address, (k->second.protocol==MM_27 ? "MM27" : "MM14"), k->second.name));
448                                         command("create(10, append)");
449
450                                         locos.insert(LocoMap::value_type(id, k->second));
451                                         locos.erase(k);
452                                 }
453                         }
454                 }
455
456                 if(locos.lower_bound(0x10000)!=locos.end())
457                         command("create(10)");
458         }
459         else if(!msg.header.value.compare(0, 10, "create(11,"))
460         {
461                 Message::ObjectMap::const_iterator i = msg.content.find(11);
462                 if(i!=msg.content.end())
463                 {
464                         Message::AttribMap::const_iterator j = i->second.find("id");
465                         if(j!=i->second.end())
466                         {
467                                 unsigned id = lexical_cast<unsigned>(j->second);
468                                 TurnoutMap::iterator k = turnouts.lower_bound(0x10000);
469                                 if(k!=turnouts.end())
470                                 {
471                                         command(format("request(%d, view, control)", id));
472                                         command(format("set(%d, addr[%d], symbol[%d], name1[\"Switch\"], name2[\"%d\"], name3[\"\"])",
473                                                 id, k->second.address, k->second.symbol, k->second.address));
474                                         command(format("set(%d, state[%d])", id, k->second.state&((1<<k->second.bits)-1)));
475
476                                         k->second.synced = true;
477                                         turnouts.insert(TurnoutMap::value_type(id, k->second));
478                                         turnouts.erase(k);
479                                 }
480                         }
481                 }
482         }
483         else if(!msg.header.value.compare(0, 10, "create(26,"))
484                 command("queryObjects(26)");
485 }
486
487 void CentralStation::process_event(const Message &msg)
488 {
489         for(Message::ObjectMap::const_iterator i=msg.content.begin(); i!=msg.content.end(); ++i)
490                 process_object(i->first, i->second);
491 }
492
493 void CentralStation::process_object(unsigned id, const Message::AttribMap &attribs)
494 {
495         if(id==1)
496         {
497                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
498                         if(i->first=="status")
499                         {
500                                 power = (i->second=="GO");
501                                 signal_power.emit(power);
502                         }
503         }
504         else if(locos.count(id))
505         {
506                 Locomotive &loco = locos[id];
507                 bool speed_changed = false;
508                 unsigned funcs_changed = 0;
509                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
510                 {
511                         if(i->first=="name")
512                                 loco.name = i->second.substr(1, i->second.size()-2);
513                         else if(i->first=="addr")
514                         {
515                                 loco_addr.erase(loco.address);
516                                 loco.address = lexical_cast<unsigned>(i->second);
517                                 loco_addr[loco.address] = id;
518                         }
519                         else if(i->first=="protocol")
520                         {
521                                 if(i->second=="MM")
522                                         loco.protocol = MM;
523                                 else if(i->second=="MM27")
524                                         loco.protocol = MM_27;
525                                 else if(i->second=="MFX")
526                                         loco.protocol = MFX;
527                         }
528                         else if(i->first=="speedstep")
529                         {
530                                 loco.speed = lexical_cast<unsigned>(i->second);
531                                 if(loco.protocol==MFX && loco.speed)
532                                         --loco.speed;
533                                 speed_changed = true;
534                         }
535                         else if(i->first=="dir")
536                         {
537                                 loco.reverse = i->second[0]!='0';
538                                 speed_changed = true;
539                         }
540                         else if(i->first=="func")
541                         {
542                                 vector<string> parts = split(i->second, ", ");
543                                 unsigned func = lexical_cast<unsigned>(parts[0]);
544                                 bool value = lexical_cast<unsigned>(parts[1]);
545                                 loco.funcs &= ~(1<<func);
546                                 if(value)
547                                         loco.funcs |= 1<<func;
548                                 funcs_changed |= 1<<func;
549                         }
550                         else if(i->first=="msg")
551                         {
552                                 if(i->second=="CONTROL_LOST")
553                                         command(format("request(%d, control, force)", id));
554                         }
555                 }
556
557                 if(speed_changed)
558                         signal_loco_speed.emit(loco.address, loco.speed, loco.reverse);
559                 for(unsigned i=0; funcs_changed>>i; ++i)
560                         if(funcs_changed&(1<<i))
561                                 signal_loco_function.emit(loco.address, i, loco.funcs&(1<<i));
562         }
563         else if(turnouts.count(id))
564         {
565                 Turnout &turnout = turnouts[id];
566                 bool state_changed = false;
567                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
568                 {
569                         if(i->first=="addr")
570                         {
571                                 turnout_addr.erase(turnout.address);
572                                 turnout.address = lexical_cast<unsigned>(i->second);
573                                 turnout_addr[turnout.address] = id;
574                         }
575                         else if(i->first=="state")
576                         {
577                                 unsigned state = lexical_cast<unsigned>(i->second);
578                                 unsigned mask = (1<<turnout.bits)-1;
579                                 turnout.state = (turnout.state&~mask) | (state&mask);
580                                 state_changed = true;
581                         }
582                 }
583
584                 if(state_changed)
585                         signal_turnout.emit(turnout.address, turnout.state);
586         }
587         else if(find(s88.begin(), s88.end(), id)!=s88.end())
588         {
589                 unsigned base = 0;
590                 for(; (base<s88.size() && s88[base]!=id); ++base) ;
591
592                 for(Message::AttribMap::const_iterator i=attribs.begin(); i!=attribs.end(); ++i)
593                 {
594                         if(i->first=="state")
595                         {
596                                 unsigned state = lexical_cast<unsigned>(i->second, "%i");
597                                 for(unsigned j=0; j<16; ++j)
598                                 {
599                                         unsigned addr = base*16+j+1;
600                                         Sensor &sensor = sensors[addr];
601                                         bool s = state&(1<<j);
602                                         if(s)
603                                         {
604                                                 sensor.off_timeout = Time::TimeStamp();
605                                                 if(!sensor.state)
606                                                 {
607                                                         sensor.state = true;
608                                                         signal_sensor.emit(addr, sensor.state);
609                                                 }
610                                         }
611                                         else if(sensor.state)
612                                                 sensor.off_timeout = Time::now()+700*Time::msec;
613                                 }
614                         }
615                 }
616         }
617 }
618
619 CentralStation::Protocol CentralStation::map_protocol(const string &name) const
620 {
621         if(name=="MM")
622                 return MM;
623         else if(name=="MM-27")
624                 return MM_27;
625         else if(name=="MFX")
626                 return MFX;
627         else
628                 throw InvalidParameterValue("Unknown protocol");
629 }
630
631 template<typename T>
632 unsigned CentralStation::map_address(const map<unsigned, T> &omap, const AddressMap &amap, unsigned addr) const
633 {
634         if(omap.count(addr))
635                 return addr;
636         else
637         {
638                 AddressMap::const_iterator i = amap.find(addr);
639                 if(i!=amap.end())
640                         return i->second;
641                 else
642                         return 0;
643         }
644 }
645
646 void CentralStation::skip(string::iterator &iter, const string::iterator &end, const string &what) const
647 {
648         for(; (iter!=end && what.find(*iter)!=string::npos); ++iter) ;
649 }
650
651 string CentralStation::parse_token(string::iterator &iter, const string::iterator &end, const string &stop) const
652 {
653         vector<char> parens;
654         bool quote = false;
655         string token;
656
657         skip(iter, end, stop);
658
659         for(; iter!=end; ++iter)
660         {
661                 if(stop.find(*iter)!=string::npos && parens.empty() && !quote)
662                         break;
663                 else if(*iter=='(' || *iter=='[')
664                         parens.push_back(*iter);
665                 else if((*iter==')' || *iter==']') && !parens.empty())
666                 {
667                         if((*iter==')' && parens.back()!='(') || (*iter==']' && parens.back()!='['))
668                                 IO::print("Mismatched parentheses\n");
669                         parens.pop_back();
670                 }
671                 else if(*iter=='"')
672                         quote = !quote;
673
674                 token += *iter;
675         }
676
677         return token;
678 }
679
680 CentralStation::Tag CentralStation::parse_tag(string::iterator &iter, const string::iterator &end) const
681 {
682         Tag tag;
683
684         for(; (iter!=end && *iter!='<'); ++iter) ;
685         if(iter==end)
686                 return Tag();
687
688         tag.type = parse_token(++iter, end, " >");
689         if(tag.type=="END")
690         {
691                 string code = parse_token(iter, end, " >");
692                 tag.code = lexical_cast<unsigned>(code);
693         }
694         skip(iter, end, " ");
695         tag.value = parse_token(iter, end, ">");
696         if(iter==end)
697                 return Tag();
698         ++iter;
699
700         return tag;
701 }
702
703 CentralStation::Message CentralStation::parse_message(string::iterator &iter, const string::iterator &end) const
704 {
705         Message msg;
706
707         msg.header = parse_tag(iter, end);
708
709         while(iter!=end)
710         {
711                 skip(iter, end, "\r\n");
712                 if(*iter=='<')
713                         break;
714
715                 string id = parse_token(iter, end, " \r\n<");
716                 Message::AttribMap &attribs = msg.content[lexical_cast<unsigned>(id)];
717                 while(iter!=end && *iter!='\n' && *iter!='\r')
718                 {
719                         string attr = parse_token(iter, end, " \r\n<");
720                         string::size_type open_bracket = attr.find('[');
721                         if(open_bracket!=string::npos)
722                         {
723                                 string::size_type close_bracket = attr.rfind(']');
724                                 attribs[attr.substr(0, open_bracket)] = attr.substr(open_bracket+1, close_bracket-open_bracket-1);
725                         }
726                         else
727                                 attribs[attr];
728                 }
729         }
730
731         msg.footer = parse_tag(iter, end);
732         if(msg.footer.type.empty())
733                 return Message();
734
735         return msg;
736 }
737
738
739 CentralStation::Tag::Tag():
740         code(0)
741 { }
742
743 CentralStation::Tag::operator bool() const
744 {
745         return !type.empty();
746 }
747
748
749 CentralStation::Message::operator bool() const
750 {
751         return header && footer;
752 }
753
754
755 CentralStation::Locomotive::Locomotive():
756         address(0),
757         speed(0),
758         reverse(false),
759         funcs(0),
760         control(false)
761 { }
762
763
764 CentralStation::Turnout::Turnout():
765         address(0),
766         symbol(0),
767         state(0),
768         bits(0),
769         synced(false)
770 { }
771
772
773 CentralStation::Sensor::Sensor():
774         state(false)
775 { }
776
777 } // namespace R2C2