summaryrefslogtreecommitdiff
path: root/src/bindings/mono/eldbus_mono/eldbus_common.cs
blob: 80b1e25c60fe6c7ff055657d3dc9b65bbd3855b2 (plain)
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
#pragma warning disable 1591

using System;
using System.Runtime.InteropServices;

using static eldbus.EldbusMessageNativeFunctions;

namespace eldbus
{

public static class Timeout
{
    public const int Infinite = 0x7fffffff;
}

[StructLayout(LayoutKind.Sequential)]
public struct ObjectPath
{
    public string value;

    public ObjectPath(string str)
    {
        value = str;
    }

    public static implicit operator ObjectPath(string str)
    {
        return new ObjectPath(str);
    }

    public static implicit operator string(ObjectPath path)
    {
        return path.value;
    }
}

[StructLayout(LayoutKind.Sequential)]
public struct SignatureString
{
    public string value;

    public SignatureString(string str)
    {
        value = str;
    }

    public static implicit operator SignatureString(string str)
    {
        return new SignatureString(str);
    }

    public static implicit operator string(SignatureString sig)
    {
        return sig.value;
    }
}

[StructLayout(LayoutKind.Sequential)]
public struct UnixFd
{
    public Int32 value;

    public UnixFd(Int32 fd)
    {
        value = fd;
    }

    public static implicit operator UnixFd(Int32 fd)
    {
        return new UnixFd(fd);
    }

    public static implicit operator Int32(UnixFd unix_fd)
    {
        return unix_fd.value;
    }
}


public static class Argument
{
    public class ByteType
    {
        public const char Code = 'y';
        public const string Signature = "y";
    }

    public class BooleanType
    {
        public const char Code = 'b';
        public const string Signature = "b";
    }

    public class Int16Type
    {
        public const char Code = 'n';
        public const string Signature = "n";
    }

    public class UInt16Type
    {
        public const char Code = 'q';
        public const string Signature = "q";
    }

    public class Int32Type
    {
        public const char Code = 'i';
        public const string Signature = "i";
    }

    public class UInt32Type
    {
        public const char Code = 'u';
        public const string Signature = "u";
    }

    public class Int64Type
    {
        public const char Code = 'x';
        public const string Signature = "x";
    }

    public class UInt64Type
    {
        public const char Code = 't';
        public const string Signature = "t";
    }

    public class DoubleType
    {
        public const char Code = 'd';
        public const string Signature = "d";
    }

    public class StringType
    {
        public const char Code = 's';
        public const string Signature = "s";
    }

    public class ObjectPathType
    {
        public const char Code = 'o';
        public const string Signature = "o";
    }

    public class SignatureType
    {
        public const char Code = 'g';
        public const string Signature = "g";
    }

    public class ArrayType
    {
        public const char Code = 'a';
        public const string Signature = "a";
    }

    public class StructType
    {
        public const char Code = 'r';
        public const string Signature = "r";
    }

    public class VariantType
    {
        public const char Code = 'v';
        public const string Signature = "v";
    }

    public class DictEntryType
    {
        public const char Code = 'e';
        public const string Signature = "e";
    }

    public class UnixFdType
    {
        public const char Code = 'h';
        public const string Signature = "h";
    }

//     public static readonly ByteType       ByteT       = new ByteType();
//     public static readonly BooleanType    BooleanT    = new BooleanType();
//     public static readonly Int16Type      Int16T      = new Int16Type();
//     public static readonly UInt16Type     UInt16T     = new UInt16Type();
//     public static readonly Int32Type      Int32T      = new Int32Type();
//     public static readonly UInt32Type     UInt32T     = new UInt32Type();
//     public static readonly Int64Type      Int64T      = new Int64Type();
//     public static readonly UInt64Type     UInt64T     = new UInt64Type();
//     public static readonly DoubleType     DoubleT     = new DoubleType();
//     public static readonly StringType     StringT     = new StringType();
//     public static readonly ObjectPathType ObjectPathT = new ObjectPathType();
//     public static readonly SignatureType  SignatureT  = new SignatureType();
//     public static readonly ArrayType      ArrayT      = new ArrayType();
//     public static readonly StructType     StructT     = new StructType();
//     public static readonly VariantType    VariantT    = new VariantType();
//     public static readonly DictEntryType  DictEntryT  = new DictEntryType();
//     public static readonly UnixFdType     UnixFdT     = new UnixFdType();
//
//     public static readonly ByteType       y = ByteT;
//     public static readonly BooleanType    b = BooleanT;
//     public static readonly Int16Type      n = Int16T;
//     public static readonly UInt16Type     q = UInt16T;
//     public static readonly Int32Type      i = Int32T;
//     public static readonly UInt32Type     u = UInt32T;
//     public static readonly Int64Type      x = Int64T;
//     public static readonly UInt64Type     t = UInt64T;
//     public static readonly DoubleType     d = DoubleT;
//     public static readonly StringType     s = StringT;
//     public static readonly ObjectPathType o = ObjectPathT;
//     public static readonly SignatureType  g = SignatureT;
//     public static readonly ArrayType      a = ArrayT;
//     public static readonly StructType     r = StructT;
//     public static readonly VariantType    v = VariantT;
//     public static readonly DictEntryType  e = DictEntryT;
//     public static readonly UnixFdType     h = UnixFdT;
}

public abstract class BasicMessageArgument
{
    public void AppendTo(eldbus.Message msg)
    {
        if (!InternalAppendTo(msg))
        {
            throw new SEHException("Eldbus: could not append basic type to eldbus.Message");
        }
    }

    public void AppendTo(eldbus.MessageIterator iter)
    {
        if (!InternalAppendTo(iter))
        {
            throw new SEHException("Eldbus: could not append basic type to eldbus.MessageIterator");
        }
    }

    public abstract char TypeCode {get;}
    public abstract string Signature {get;}
    protected abstract bool InternalAppendTo(eldbus.Message msg);
    protected abstract bool InternalAppendTo(eldbus.MessageIterator iter);

    public static implicit operator BasicMessageArgument(byte arg)
    {
        return new ByteMessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(bool arg)
    {
        return new BoolMessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(Int16 arg)
    {
        return new Int16MessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(UInt16 arg)
    {
        return new UInt16MessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(Int32 arg)
    {
        return new Int32MessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(UInt32 arg)
    {
        return new UInt32MessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(Int64 arg)
    {
        return new Int64MessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(UInt64 arg)
    {
        return new UInt64MessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(string arg)
    {
        return new StringMessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(SignatureString arg)
    {
        return new SignatureMessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(ObjectPath arg)
    {
        return new ObjectPathMessageArgument(arg);
    }

    public static implicit operator BasicMessageArgument(UnixFd arg)
    {
        return new UnixFdMessageArgument(arg);
    }
}

public class ByteMessageArgument : BasicMessageArgument
{
    private byte value;

    public ByteMessageArgument(byte arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.ByteType.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class BoolMessageArgument : BasicMessageArgument
{
    private Int32 value;

    public BoolMessageArgument(bool arg)
    {
        value = Convert.ToInt32(arg);
    }

    public override char TypeCode
    {
        get { return Argument.BooleanType.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class Int16MessageArgument : BasicMessageArgument
{
    private Int16 value;

    public Int16MessageArgument(Int16 arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.Int16Type.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class UInt16MessageArgument : BasicMessageArgument
{
    private UInt16 value;

    public UInt16MessageArgument(UInt16 arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.UInt16Type.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class Int32MessageArgument : BasicMessageArgument
{
    private Int32 value;

    public Int32MessageArgument(Int32 arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.Int32Type.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class UInt32MessageArgument : BasicMessageArgument
{
    private UInt32 value;

    public UInt32MessageArgument(UInt32 arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.UInt32Type.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class Int64MessageArgument : BasicMessageArgument
{
    private Int64 value;

    public Int64MessageArgument(Int64 arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.Int64Type.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class UInt64MessageArgument : BasicMessageArgument
{
    private UInt64 value;

    public UInt64MessageArgument(UInt64 arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.UInt64Type.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class DoubleMessageArgument : BasicMessageArgument
{
    private double value;

    public DoubleMessageArgument(double arg)
    {
        value = arg;
    }

    public override char TypeCode
    {
        get { return Argument.DoubleType.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public abstract class StringLikeMessageArgument : BasicMessageArgument
{
    private string value;

    public StringLikeMessageArgument(string arg)
    {
        value = arg;
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public class StringMessageArgument : StringLikeMessageArgument
{
    public StringMessageArgument(string arg) : base(arg)
    {
    }

    public override char TypeCode
    {
        get { return Argument.StringType.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }
}

public class ObjectPathMessageArgument : StringLikeMessageArgument
{
    public ObjectPathMessageArgument(ObjectPath arg) : base(arg.value)
    {
    }

    public override char TypeCode
    {
        get { return Argument.ObjectPathType.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }
}

public class SignatureMessageArgument : StringLikeMessageArgument
{
    public SignatureMessageArgument(SignatureString arg) : base(arg.value)
    {
    }

    public override char TypeCode
    {
        get { return Argument.SignatureType.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }
}

public class UnixFdMessageArgument : BasicMessageArgument
{
    private Int32 value;

    public UnixFdMessageArgument(UnixFd arg)
    {
        value = arg.value;
    }

    public override char TypeCode
    {
        get { return Argument.UnixFdType.Code; }
    }

    public override string Signature
    {
        get { return Argument.ByteType.Signature; }
    }

    protected override bool InternalAppendTo(eldbus.Message msg)
    {
        return eldbus_message_arguments_append(msg.Handle, Signature, value);
    }

    protected override bool InternalAppendTo(eldbus.MessageIterator iter)
    {
        return eldbus_message_iter_basic_append(iter.Handle, TypeCode, value);
    }
}

public delegate void MessageDelegate(eldbus.Message msg, eldbus.Pending pending);

public static class Common
{
    public static void RaiseNullHandle()
    {
        if (NullHandleError == 0)
        {
            NullHandleError = Eina.Error.Register("Eldbus: null handle");
        }

        Eina.Error.Raise(NullHandleError);
    }

    public delegate void Eldbus_Message_Cb(IntPtr data, IntPtr msg, IntPtr pending);

    public static IntPtr GetMessageCbWrapperPtr()
    {
        return Marshal.GetFunctionPointerForDelegate(GetMessageCbWrapper());
    }

    public static Eldbus_Message_Cb GetMessageCbWrapper()
    {
        if (message_cb_wrapper == null)
        {
            message_cb_wrapper = new Eldbus_Message_Cb(MessageCbWrapper);
        }

        return message_cb_wrapper;
    }

    public static void MessageCbWrapper(IntPtr data, IntPtr msg_hdl, IntPtr pending_hdl)
    {
        MessageDelegate dlgt = Marshal.GetDelegateForFunctionPointer(data, typeof(MessageDelegate)) as MessageDelegate;
        if (dlgt == null)
        {
            Eina.Log.Error("Eldbus: invalid delegate pointer from Eldbus_Message_Cb");
            return;
        }

        eldbus.Message msg;
        eldbus.Pending pending;

        try
        {
            msg = new eldbus.Message(msg_hdl, false);
            pending = new eldbus.Pending(pending_hdl, false);
        }
        catch (Exception e)
        {
            Eina.Log.Error("Eldbus: could not convert Eldbus_Message_Cb parameters. Exception: " + e.ToString());
            return;
        }

        try
        {
            dlgt(msg, pending);
        }
        catch (Exception e)
        {
            Eina.Log.Error("Eldbus: Eldbus_Message_Cb delegate error. Exception: " + e.ToString());
        }
    }

    private static Eldbus_Message_Cb message_cb_wrapper = null;
    private static Eina.Error NullHandleError = 0;
}

}