forked from smaccm/camera_demo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchirp.cpp
1375 lines (1178 loc) · 37.7 KB
/
chirp.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
//
// begin license header
//
// This file is part of Pixy CMUcam5 or "Pixy" for short
//
// All Pixy source code is provided under the terms of the
// GNU General Public License v2 (http://www.gnu.org/licenses/gpl-2.0.html).
// Those wishing to use Pixy source code, software and/or
// technologies under different licensing terms should contact us at
// [email protected]. Such licensing terms are available for
// all portions of the Pixy codebase presented here.
//
// end license header
//
#include <string.h>
#include <new>
#include "chirp.hpp"
#include "debug.h"
// todo yield, sleep() while waiting for sync response
// todo
// assume that destination is aligned on the correct boundary and copy the source byte by byte
void copyAlign(char *dest, const char *src, int size)
{
int i;
for (i=0; i<size; i++)
dest[i] = src[i];
}
Chirp::Chirp(bool hinterested, bool client, Link *link)
{
log("pixydebug: Chirp::Chirp()\n");
m_link = NULL;
m_errorCorrected = false;
m_sharedMem = false;
m_buf = NULL;
m_bufSave = NULL;
m_maxNak = CRP_MAX_NAK;
m_retries = CRP_RETRIES;
m_headerTimeout = CRP_HEADER_TIMEOUT;
m_dataTimeout = CRP_DATA_TIMEOUT;
m_idleTimeout = CRP_IDLE_TIMEOUT;
m_sendTimeout = CRP_SEND_TIMEOUT;
m_call = false;
m_connected = false;
m_hinformer = false;
m_hinterested = hinterested;
m_client = client;
m_procTableSize = CRP_PROCTABLE_LEN;
m_procTable = new (std::nothrow) ProcTableEntry[m_procTableSize];
memset(m_procTable, 0, sizeof(ProcTableEntry)*m_procTableSize);
if (link)
setLink(link);
log("pixydebug: Chirp::Chirp() returned\n");
}
Chirp::~Chirp()
{
log("pixydebug: Chirp::~Chirp()\n");
// if we're a client, disconnect (let server know)
if (m_client)
remoteInit(false);
if (!m_sharedMem)
{
restoreBuffer();
delete[] m_buf;
}
delete[] m_procTable;
log("pixydebug: Chirp::~Chirp() returned\n");
}
int Chirp::init(bool connect)
{
return CRP_RES_OK;
}
int Chirp::setLink(Link *link)
{
int return_value;
log("pixydebug: Chirp::setLink()\n");
m_link = link;
m_errorCorrected = m_link->getFlags()&LINK_FLAG_ERROR_CORRECTED;
m_sharedMem = m_link->getFlags()&LINK_FLAG_SHARED_MEM;
m_blkSize = m_link->blockSize();
if (m_errorCorrected)
m_headerLen = 12; // startcode (uint32_t), type (uint8_t), (pad), proc (uint16_t), len (uint32_t)
else
m_headerLen = 8; // type (uint8_t), (pad), proc (uint16_t), len (uint32_t)
if (m_sharedMem)
{
m_buf = (uint8_t *)m_link->getFlags(LINK_FLAG_INDEX_SHARED_MEMORY_LOCATION);
m_bufSize = m_link->getFlags(LINK_FLAG_INDEX_SHARED_MEMORY_SIZE);
}
else
{
m_bufSize = CRP_BUFSIZE;
m_buf = new (std::nothrow) uint8_t[m_bufSize];
}
// link is set up, need to call init
if (m_client) {
return_value = remoteInit(true);
log("pixydebug: remoteInit() = %d\n", return_value);
log("pixydebug: setLink() returned %d\n", return_value);
return return_value;
}
log("pixydebug: setLink() returned %d\n", CRP_RES_OK);
return CRP_RES_OK;
}
int Chirp::assemble(uint8_t type, ...)
{
int res;
va_list args;
bool save = m_call;
uint32_t saveLen = m_len;
if (type==CRP_XDATA)
m_call = false;
va_start(args, type);
res = vassemble(&args);
va_end(args);
if (type==CRP_XDATA || (!m_call && res==CRP_RES_OK)) // if we're not a call, we're extra data, so we need to send
{
res = sendChirpRetry(CRP_XDATA, 0);
m_len = saveLen;
}
m_call = save;
return res;
}
int Chirp::vassemble(va_list *args)
{
int len;
len = vserialize(this, m_buf, m_bufSize, args);
// check for error
if (len<0)
return len;
// set length (don't include header)
m_len = len - m_headerLen;
return CRP_RES_OK;
}
bool Chirp::connected()
{
return m_connected;
}
int Chirp::useBuffer(uint8_t *buf, uint32_t len)
{
int res;
if (m_bufSave==NULL)
{
m_bufSave = m_buf;
m_buf = buf;
}
else if (buf!=m_buf)
return CRP_RES_ERROR_MEMORY;
m_len = len-m_headerLen;
if (!m_call) // if we're not a call, we're extra data, so we need to send
{
res = sendChirpRetry(CRP_XDATA, 0);
restoreBuffer(); // restore buffer immediately!
if (res!=CRP_RES_OK) // convert call into response
return res;
}
return CRP_RES_OK;
}
void Chirp::restoreBuffer()
{
if (m_bufSave)
{
m_buf = m_bufSave;
m_bufSave = NULL;
}
}
int Chirp::serialize(Chirp *chirp, uint8_t *buf, uint32_t bufSize, ...)
{
int res;
va_list args;
va_start(args, bufSize);
res = vserialize(chirp, buf, bufSize, &args);
va_end(args);
return res;
}
#define RESIZE_BUF(size) \
if (size > bufSize-CRP_BUFPAD) \
{ \
if (!chirp) \
return CRP_RES_ERROR_MEMORY; \
else \
{ \
if ((res=chirp->realloc(size))<0) \
return res; \
buf = chirp->m_buf; \
bufSize = chirp->m_bufSize; \
} \
} \
int Chirp::vserialize(Chirp *chirp, uint8_t *buf, uint32_t bufSize, va_list *args)
{
int res;
uint8_t type, origType;
uint32_t i, si;
bool copy = true;
if (chirp)
{
if (chirp->m_call) // reserve an extra 4 for responseint
i = chirp->m_headerLen+4;
else // if it's a chirp call, just reserve the header
i = chirp->m_headerLen;
}
else
i = 0;
bufSize -= i;
while(1)
{
#if 1
type = va_arg(*args, int);
#else
type = va_arg(*args, uint8_t);
#endif
if (type==END)
break;
si = i; // save index so we can skip over data if needed
buf[i++] = type;
// treat hints like other types for now
// but if gotoe (guy on the other end) isn't interested in hints (m_hinformer=false),
// we'll restore index to si and effectively skip data.
origType = type;
type &= ~CRP_HINT;
if (type==CRP_INT8)
{
#if 1
int8_t val = va_arg(*args, int);
#else
int8_t val = va_arg(*args, int8_t);
#endif
*(int8_t *)(buf+i) = val;
i += 1;
}
else if (type==CRP_INT16)
{
#if 1
int16_t val = va_arg(*args, int);
#else
int16_t val = va_arg(*args, int16_t);
#endif
ALIGN(i, 2);
// rewrite type so getType will work (even though we might add padding between type and data)
buf[i-1] = origType;
*(int16_t *)(buf+i) = val;
i += 2;
}
else if (type==CRP_INT32 || origType==CRP_TYPE_HINT) // CRP_TYPE_HINT is a special case...
{
int32_t val = va_arg(*args, int32_t);
ALIGN(i, 4);
buf[i-1] = origType;
*(int32_t *)(buf+i) = val;
i += 4;
}
else if (type==CRP_FLT32)
{
#if 1
float val = va_arg(*args, double);
#else
float val = va_arg(*args, float);
#endif
ALIGN(i, 4);
buf[i-1] = origType;
*(float *)(buf+i) = val;
i += 4;
}
else if (type==CRP_STRING)
{
int8_t *s = va_arg(*args, int8_t *);
uint32_t len = strlen((char *)s)+1; // include null
RESIZE_BUF(len+i);
memcpy(buf+i, s, len);
i += len;
}
else if (type&CRP_ARRAY)
{
uint8_t size = type&0x0f;
uint32_t len = va_arg(*args, int32_t);
// deal with no copy case (use our own buffer)
if ((type&CRP_NO_COPY)==CRP_NO_COPY)
{
// rewrite type so as not to confuse gotoe
origType = type &= ~CRP_NO_COPY;
buf[i-1] = origType;
copy = false;
}
ALIGN(i, 4);
buf[i-1] = origType;
*(uint32_t *)(buf+i) = len;
i += 4;
ALIGN(i, size);
if (copy)
{
len *= size; // scale by size of array elements
RESIZE_BUF(len+i);
int8_t *ptr = va_arg(*args, int8_t *);
memcpy(buf+i, ptr, len);
i += len;
}
}
else
return CRP_RES_ERROR_PARSE;
// skip hint data if we're not a source
if (chirp && !chirp->m_hinformer && origType&CRP_HINT)
i = si;
RESIZE_BUF(i);
}
// return length
return i;
}
// this isn't completely necessary, but it makes things a lot easier to use.
// passing a pointer to a pointer and then having to dereference is just confusing....
// so for scalars (ints, floats) you don't need to pass in ** pointers, just * pointers so
// chirp can write the value into the * pointer and hand it back.
// But for arrays you need ** pointers, so chirp doesn't need to copy the whole array into your buffer---
// chirp will write the * pointer value into your ** pointer.
int Chirp::loadArgs(va_list *args, void *recvArgs[])
{
int i;
uint8_t type, size;
void **recvArg;
for (i=0; recvArgs[i]!=NULL && i<CRP_MAX_ARGS; i++)
{
type = getType(recvArgs[i]);
recvArg = va_arg(*args, void **);
if (recvArg==NULL)
return CRP_RES_ERROR_PARSE;
if (!(type&CRP_ARRAY)) // if we're a scalar
{
size = type&0x0f;
if (size==1) *(uint8_t *)recvArg = *(uint8_t *)recvArgs[i];
else if (size==2) *(uint16_t *)recvArg = *(uint16_t *)recvArgs[i];
else if (size==4) *(uint32_t *)recvArg = *(uint32_t *)recvArgs[i];
//else if (size==8) *recvArg = *(double *)recvArgs[i];
else return CRP_RES_ERROR_PARSE;
}
else // we're an array
{
if (type==CRP_STRING)
*(char **)recvArg = (char *)recvArgs[i];
else
{
*(uint32_t *)recvArg = *(uint32_t *)recvArgs[i++];
recvArg = va_arg(*args, void **);
if (recvArg==NULL)
return CRP_RES_ERROR_PARSE;
*(void **)recvArg = recvArgs[i];
}
}
}
// check to see if last arg is NULL, if not, we have a parse error
// if the arg isn't null, it means the caller is expecting data to be
// put there. If data isn't put there, and the caller dereferences, segfault
if (va_arg(*args, void **)!=NULL)
return CRP_RES_ERROR_PARSE;
return CRP_RES_OK;
}
int Chirp::call(uint8_t service, ChirpProc proc, va_list args)
{
int res, i;
uint8_t type;
va_list arguments;
va_copy(arguments, args);
// if it's just a regular call (not init or enumerate), we need to be connected
if (!(service&CRP_CALL) && !m_connected)
return CRP_RES_ERROR_NOT_CONNECTED;
// parse arguments and assemble in m_buf
m_len = 0;
// restore buffer in case it was changed
restoreBuffer();
if ((res=vassemble(&arguments))<0)
{
va_end(arguments);
return res;
}
if (service&CRP_CALL) // special case for enumerate and init (internal calls)
{
type = service;
service = SYNC;
}
else
type = CRP_CALL;
// send call data
if ((res=sendChirpRetry(type, proc))!=CRP_RES_OK) // convert call into response
{
va_end(arguments);
return res;
}
// if the service is synchronous, receive response while servicing other calls
if (!(service&ASYNC))
{
ChirpProc recvProc;
void *recvArgs[CRP_MAX_ARGS+1];
m_link->setTimer(); // set timer, so we can check to see if we're taking too much time
while(1)
{
if ((res=recvChirp(&type, &recvProc, recvArgs, true))==CRP_RES_OK)
{
if (type&CRP_RESPONSE)
break;
else // handle calls as they come in
handleChirp(type, recvProc, recvArgs);
}
else
{
va_end(arguments);
return res;
}
if (m_link->getTimer()>m_headerTimeout) // we could receive XDATA (for example) and never exit this while loop
return CRP_RES_ERROR_RECV_TIMEOUT;
}
// deal with arguments
if (service&RETURN_ARRAY) // copy array of arguments
{
void **recvArray;
while(1)
{
recvArray = va_arg(arguments, void **);
if (recvArray!=NULL)
break;
}
for (i=0; recvArgs[i]; i++)
recvArray[i] = recvArgs[i];
recvArray[i] = NULL;
}
else if ((res=loadArgs(&arguments, recvArgs))<0)
{
va_end(arguments);
return res;
}
}
va_end(arguments);
return CRP_RES_OK;
}
int Chirp::call(uint8_t service, ChirpProc proc, ...)
{
int result;
va_list arguments;
va_start(arguments, proc);
result = call(service, proc, arguments);
va_end(arguments);
return result;
}
int Chirp::sendChirpRetry(uint8_t type, ChirpProc proc)
{
int i, res=-1;
if (!m_connected && !(type&CRP_INTRINSIC))
return CRP_RES_ERROR_NOT_CONNECTED;
// deal with case where there is no actual data (e.g. it's all hint data and gotoe isn't hinterested)
// but chirp calls can have no data of course
if (m_len==0 && !(type&CRP_CALL))
return CRP_RES_OK;
for (i=0; i<m_retries; i++)
{
res = sendChirp(type, proc);
if (res==CRP_RES_OK)
break;
}
// if sending the chirp fails after retries, we should assume we're no longer connected
if (res<0)
m_connected = false;
return res;
}
int Chirp::sendChirp(uint8_t type, ChirpProc proc)
{
int res;
if (m_errorCorrected)
res = sendFull(type, proc);
else
{
// we'll send forever as long as we get naks
// we rely on receiver to give up
while((res=sendHeader(type, proc))==CRP_RES_ERROR_CRC);
if (res!=CRP_RES_OK)
return res;
res = sendData();
}
if (res!=CRP_RES_OK)
return res;
return CRP_RES_OK;
}
int Chirp::handleChirp(uint8_t type, ChirpProc proc, void *args[])
{
int res;
int32_t responseInt = 0;
uint8_t n;
// default case, we return one integer (responseint)
m_len = 4;
// check for intrinsic calls
if (type&CRP_INTRINSIC)
{
m_call = true; // indicate to ourselves that this is a chirp call
if (type==CRP_CALL_ENUMERATE)
responseInt = handleEnumerate((char *)args[0], (ChirpProc *)args[1]);
else if (type==CRP_CALL_INIT)
responseInt = handleInit((uint16_t *)args[0], (uint8_t *)args[1]);
else if (type==CRP_CALL_ENUMERATE_INFO)
responseInt = handleEnumerateInfo((ChirpProc *)args[0]);
else
responseInt = CRP_RES_ERROR;
m_call = false;
}
else if (type==CRP_XDATA)
{
handleXdata(args);
return CRP_RES_OK;
}
else // normal call
{
if (proc>=m_procTableSize)
return CRP_RES_ERROR; // index exceeded
ProcPtr ptr = m_procTable[proc].procPtr;
if (ptr==NULL)
return CRP_RES_ERROR; // some chirps are not meant to be called in both directions
// count args
for (n=0; args[n]!=NULL; n++);
m_call = true; // indicate to ourselves that this is a chirp call
// this is probably overkill....
if (n==0)
responseInt = (*ptr)(this);
else if (n==1)
responseInt = (*(uint32_t(*)(void*,Chirp*))ptr)(args[0],this);
else if (n==2)
responseInt = (*(uint32_t(*)(void*,void*,Chirp*))ptr)(args[0],args[1],this);
else if (n==3)
responseInt = (*(uint32_t(*)(void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],this);
else if (n==4)
responseInt = (*(uint32_t(*)(void*,void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],args[3],this);
else if (n==5)
responseInt = (*(uint32_t(*)(void*,void*,void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],args[3],args[4],this);
else if (n==6)
responseInt = (*(uint32_t(*)(void*,void*,void*,void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],args[3],args[4],args[5],this);
else if (n==7)
responseInt = (*(uint32_t(*)(void*,void*,void*,void*,void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],args[3],args[4],args[5],args[6],this);
else if (n==8)
responseInt = (*(uint32_t(*)(void*,void*,void*,void*,void*,void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],this);
else if (n==9)
responseInt = (*(uint32_t(*)(void*,void*,void*,void*,void*,void*,void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],this);
else if (n==10)
responseInt = (*(uint32_t(*)(void*,void*,void*,void*,void*,void*,void*,void*,void*,void*,Chirp*))ptr)(args[0],args[1],args[2],args[3],args[4],args[5],args[6],args[7],args[8],args[9],this);
else
responseInt = CRP_RES_ERROR;
m_call = false;
}
// if it's a chirp call, we need to send back the result
// result is in m_buf
if (type&CRP_CALL)
{
// write responseInt
*(uint32_t *)(m_buf+m_headerLen) = responseInt;
// send response
res = sendChirpRetry(CRP_RESPONSE | (type&~CRP_CALL), m_procTable[proc].chirpProc); // convert call into response
restoreBuffer(); // restore buffer immediately!
if (res!=CRP_RES_OK)
return res;
}
return CRP_RES_OK;
}
int Chirp::reallocTable()
{
ProcTableEntry *newProcTable;
int newProcTableSize;
// allocate new table, zero
newProcTableSize = m_procTableSize+CRP_PROCTABLE_LEN;
newProcTable = new (std::nothrow) ProcTableEntry[newProcTableSize];
if (newProcTable==NULL)
return CRP_RES_ERROR_MEMORY;
memset(newProcTable, 0, sizeof(ProcTableEntry)*newProcTableSize);
// copy to new table
memcpy(newProcTable, m_procTable, sizeof(ProcTableEntry)*m_procTableSize);
// delete old table
delete [] m_procTable;
// set to new
m_procTable = newProcTable;
m_procTableSize = newProcTableSize;
return CRP_RES_OK;
}
ChirpProc Chirp::lookupTable(const char *procName)
{
ChirpProc i;
for(i=0; i<m_procTableSize; i++)
{
if (m_procTable[i].procName!=NULL && strcmp(m_procTable[i].procName, procName)==0)
return i;
}
return -1;
}
ChirpProc Chirp::updateTable(const char *procName, ProcPtr procPtr)
{
// if it exists already, update,
// if it doesn't exist, add it
if (procName==NULL)
return -1;
ChirpProc proc = lookupTable(procName);
if (proc<0) // next empty entry
{
for (proc=0; proc<m_procTableSize && m_procTable[proc].procName; proc++);
if (proc==m_procTableSize)
{
reallocTable();
return updateTable(procName, procPtr);
}
}
// add to table
m_procTable[proc].procName = procName;
m_procTable[proc].procPtr = procPtr;
return proc;
}
ChirpProc Chirp::getProc(const char *procName, ProcPtr callback)
{
uint32_t res;
ChirpProc cproc = -1;
if (callback)
cproc = updateTable(procName, callback);
if (call(CRP_CALL_ENUMERATE, 0,
STRING(procName), // send name
INT16(cproc), // send local index
END_OUT_ARGS,
&res, // get remote index
END_IN_ARGS
)>=0)
return res;
// a negative ChirpProc is an error
return -1;
}
int Chirp::remoteInit(bool connect)
{
int res;
uint32_t responseInt;
uint8_t hinformer;
res = call(CRP_CALL_INIT, 0,
UINT16(connect ? m_blkSize : 0), // send block size
UINT8(m_hinterested), // send whether we're interested in hints or not
END_OUT_ARGS,
&responseInt,
&hinformer, // receive whether we should send hints
END_IN_ARGS
);
if (res>=0)
{
m_connected = connect;
m_hinformer = hinformer;
return responseInt;
}
return res;
}
int Chirp::getProcInfo(ChirpProc proc, ProcInfo *info)
{
uint32_t responseInt;
int res;
res = call(CRP_CALL_ENUMERATE_INFO, 0,
UINT16(proc),
END_OUT_ARGS,
&responseInt,
&info->procName,
&info->argTypes,
&info->procInfo,
END_IN_ARGS
);
if (res==CRP_RES_OK)
return responseInt;
return res;
}
int Chirp::setProc(const char *procName, ProcPtr proc, ProcTableExtension *extension)
{
ChirpProc cProc = updateTable(procName, proc);
if (cProc<0)
return CRP_RES_ERROR;
m_procTable[cProc].extension = extension;
return CRP_RES_OK;
}
int Chirp::registerModule(const ProcModule *module)
{
int i;
for (i=0; module[i].procName; i++)
setProc(module[i].procName, module[i].procPtr, (ProcTableExtension *)module[i].argTypes);
return CRP_RES_OK;
}
int32_t Chirp::handleEnumerate(char *procName, ChirpProc *callback)
{
ChirpProc proc;
// lookup in table
proc = lookupTable(procName);
// set remote index in table
m_procTable[proc].chirpProc = *callback;
return proc;
}
int32_t Chirp::handleInit(uint16_t *blkSize, uint8_t *hinformer)
{
int32_t responseInt;
bool connect = *blkSize ? true : false;
responseInt = init(connect);
m_connected = connect;
m_blkSize = *blkSize; // get block size, write it
m_hinformer = *hinformer;
CRP_RETURN(this, UINT8(m_hinterested), END);
return responseInt;
}
int32_t Chirp::handleEnumerateInfo(ChirpProc *proc)
{
const ProcTableExtension *extension;
uint8_t null = '\0';
if (*proc>=m_procTableSize || m_procTable[*proc].procName==NULL)
extension = NULL;
else
extension = m_procTable[*proc].extension;
if (extension)
{
CRP_RETURN(this, STRING(m_procTable[*proc].procName), STRING(extension->argTypes),
STRING(extension->procInfo), END);
return CRP_RES_OK;
}
else // no extension, just send procedure name
{
CRP_RETURN(this, STRING(m_procTable[*proc].procName), STRING(&null),
STRING(&null), END);
return CRP_RES_ERROR;
}
}
int Chirp::realloc(uint32_t min)
{
if (m_sharedMem)
return CRP_RES_ERROR_MEMORY;
if (!min)
min = m_bufSize+CRP_BUFSIZE;
else
min += CRP_BUFSIZE;
uint8_t *newbuf = new (std::nothrow) uint8_t[min];
if (newbuf==NULL)
return CRP_RES_ERROR_MEMORY;
memcpy(newbuf, m_buf, m_bufSize);
delete[] m_buf;
m_buf = newbuf;
m_bufSize = min;
return CRP_RES_OK;
}
// service deals with calls and callbacks
int Chirp::service(bool all)
{
int i;
uint8_t type;
ChirpProc recvProc;
void *args[CRP_MAX_ARGS+1];
for (i=0; true; i++)
{
if (recvChirp(&type, &recvProc, args)==CRP_RES_OK)
handleChirp(type, recvProc, args);
else
break;
if (!all)
break;
}
return i;
}
int Chirp::recvChirp(uint8_t *type, ChirpProc *proc, void *args[], bool wait) // null pointer terminates
{
int res;
uint32_t i, offset;
restoreBuffer();
// receive
if (m_errorCorrected)
res = recvFull(type, proc, wait);
else
{
for (i=0; true; i++)
{
res = recvHeader(type, proc, wait);
if (res==CRP_RES_ERROR_CRC)
{
if (i<m_maxNak)
continue;
else
return CRP_RES_ERROR_MAX_NAK;
}
else if (res==CRP_RES_OK)
break;
else
return res;
}
res = recvData();
}
if (res!=CRP_RES_OK)
return res;
// get responseInt from response
if (*type&CRP_RESPONSE)
{
// fake responseInt so it gets inserted
*(m_buf+m_headerLen-4) = CRP_UINT32; // write type so it parses correctly
*(m_buf+m_headerLen-1) = CRP_UINT32;
// increment pointer
offset = m_headerLen-4;
m_len+=4;
}
else // call has no responseInt
offset = m_headerLen;
return deserializeParse(m_buf+offset, m_len, args);
}
int Chirp::deserialize(uint8_t *buf, uint32_t len, ...)
{
int res;
va_list args;
void *recvArgs[CRP_MAX_ARGS+1];
if ((res=deserializeParse(buf, len, recvArgs))<0)
return res;
va_start(args, len);
res = loadArgs(&args, recvArgs);
va_end(args);
return res;
}
int Chirp::vdeserialize(uint8_t *buf, uint32_t len, va_list *args)
{
int res;
void *recvArgs[CRP_MAX_ARGS+1];
if ((res=deserializeParse(buf, len, recvArgs))<0)
return res;
return loadArgs(args, recvArgs);
}
int Chirp::getArgList(uint8_t *buf, uint32_t len, uint8_t *argList)
{
uint8_t dataType, size, a;
uint32_t i;
// parse remaining args
for(i=0, a=0; i<len; a++)
{
if (a==CRP_MAX_ARGS)
return CRP_RES_ERROR;
dataType = buf[i++];
argList[a] = dataType;
size = dataType&0x0f;
if (!(dataType&CRP_ARRAY)) // if we're a scalar
{
ALIGN(i, size);
i += dataType&0x0f; // extract size of scalar, add it
}
else // we're an array
{
if (dataType==CRP_STRING || dataType==CRP_HSTRING) // string is a special case
i += strlen((char *)(buf+i))+1; // +1 include null character
else
{
ALIGN(i, 4);
uint32_t len = *(uint32_t *)(buf+i);
i += 4;
ALIGN(i, size);
i += len*size;
}
}
}
argList[a] = '\0'; // terminate list
return CRP_RES_OK;
}
int Chirp::deserializeParse(uint8_t *buf, uint32_t len, void *args[])
{
uint8_t dataType, size, a;
uint32_t i;
// parse remaining args
for(i=0, a=0; i<len; a++)