Skip to content

Commit 93a7077

Browse files
committed
fix: Operation parameters, SharedMemory struct
Change-Id: Ic3fcf8729f97b74cdbf830e1d34461871c2acab5
1 parent 7cbaf86 commit 93a7077

File tree

2 files changed

+123
-21
lines changed

2 files changed

+123
-21
lines changed

src/Tizen.Security.TEEC/Interop/Interop.Types.cs

+26-8
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,45 @@ internal static TEEC_UUID ToTeecUuid(Guid guid)
5757
}
5858

5959
[StructLayout(LayoutKind.Sequential)]
60-
internal struct sTEEC_SharedMemoryImp
60+
internal class TEEC_SharedMemory
6161
{
62-
public IntPtr context;
63-
public IntPtr context_imp;
62+
public IntPtr buffer;
63+
public UInt32 size;
6464
public UInt32 flags;
65-
public UInt32 memid;
65+
public IntPtr imp;
66+
}
67+
68+
[StructLayout(LayoutKind.Sequential)]
69+
internal struct TEEC_Value
70+
{
71+
public UInt32 a;
72+
public UInt32 b;
6673
}
6774

6875
[StructLayout(LayoutKind.Sequential)]
69-
internal struct TEEC_SharedMemory
76+
internal struct TEEC_TempMemoryReference
7077
{
7178
public IntPtr buffer;
7279
public UInt32 size;
73-
public UInt32 flags;
7480
}
7581

7682
[StructLayout(LayoutKind.Sequential)]
83+
internal struct TEEC_RegisteredMemoryReference
84+
{
85+
public TEEC_SharedMemory parent;
86+
public UInt32 size;
87+
public UInt32 offset;
88+
}
89+
90+
[StructLayout(LayoutKind.Explicit)]
7791
internal struct TEEC_Parameter
7892
{
79-
public UInt32 a;
80-
public UInt32 b;
93+
[FieldOffset(0)]
94+
public TEEC_TempMemoryReference tmpref;
95+
[FieldOffset(0)]
96+
public TEEC_RegisteredMemoryReference memref;
97+
[FieldOffset(0)]
98+
public TEEC_Value value;
8199
}
82100

83101
[StructLayout(LayoutKind.Sequential)]

src/Tizen.Security.TEEC/Tizen.Security.TEEC/Libteec.cs

+97-13
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ public TempMemoryReference(IntPtr buffer, uint size, TEFTempMemoryType type) :
216216
/// This property represents the size of the buffer.
217217
/// </summary>
218218
/// <since_tizen> 3 </since_tizen>
219-
public uint Size { get; }
219+
public uint Size { get; internal set; }
220220
};
221221

222222
/// <summary>
@@ -249,7 +249,7 @@ public RegisteredMemoryReference(SharedMemory parent, uint size, uint offset, TE
249249
/// This property represents the size (in bytes) of the shared memory.
250250
/// </summary>
251251
/// <since_tizen> 3 </since_tizen>
252-
public uint Size { get; }
252+
public uint Size { get; internal set; }
253253
/// <summary>
254254
/// This property represents the offset (in bytes) from the start of the shared memory.
255255
/// </summary>
@@ -281,12 +281,12 @@ public Value(uint a, uint b, TEFValueType type) :
281281
/// This property represents an unsigned integer A.
282282
/// </summary>
283283
/// <since_tizen> 3 </since_tizen>
284-
public uint A { get; }
284+
public uint A { get; internal set; }
285285
/// <summary>
286286
/// This property represents an unsigned integer B.
287287
/// </summary>
288288
/// <since_tizen> 3 </since_tizen>
289-
public uint B { get; }
289+
public uint B { get; internal set; }
290290
};
291291

292292

@@ -304,20 +304,90 @@ internal Session(Interop.TEEC_Context context) {
304304
this.session = new Interop.TEEC_Session();
305305
}
306306

307+
/// <summary>
308+
/// Destructor of the class.
309+
/// </summary>
307310
~Session()
308311
{
309312
Close();
310313
}
311314

315+
internal UInt32 InitParam(ref Interop.TEEC_Parameter dst, Parameter src)
316+
{
317+
switch (src.NativeType) {
318+
case (int)TEFValueType.Input:
319+
case (int)TEFValueType.Output:
320+
case (int)TEFValueType.InOut:
321+
dst.value.a = ((Value)src).A;
322+
dst.value.b = ((Value)src).B;
323+
break;
324+
325+
case (int)TEFTempMemoryType.Input:
326+
case (int)TEFTempMemoryType.Output:
327+
case (int)TEFTempMemoryType.InOut:
328+
dst.tmpref.buffer = ((TempMemoryReference)src).Buffer;
329+
dst.tmpref.size = ((TempMemoryReference)src).Size;
330+
break;
331+
332+
case (int)TEFRegisteredMemoryType.Whole:
333+
case (int)TEFRegisteredMemoryType.PartialInput:
334+
case (int)TEFRegisteredMemoryType.PartialOutput:
335+
case (int)TEFRegisteredMemoryType.PartialInOut:
336+
dst.memref.parent = ((RegisteredMemoryReference)src).Parent.shm;
337+
dst.memref.size = ((RegisteredMemoryReference)src).Size;
338+
dst.memref.offset = ((RegisteredMemoryReference)src).Offset;
339+
break;
340+
341+
default: return 0;
342+
}
343+
return src.NativeType;
344+
}
345+
346+
internal void UpdateParam(Interop.TEEC_Parameter src, ref Parameter dst)
347+
{
348+
switch (dst.NativeType) {
349+
case (int)TEFValueType.Input:
350+
case (int)TEFValueType.Output:
351+
case (int)TEFValueType.InOut:
352+
((Value)dst).A = src.value.a;
353+
((Value)dst).B = src.value.b;
354+
break;
355+
356+
case (int)TEFTempMemoryType.Input:
357+
case (int)TEFTempMemoryType.Output:
358+
case (int)TEFTempMemoryType.InOut:
359+
((TempMemoryReference)dst).Size = src.tmpref.size;
360+
break;
361+
362+
case (int)TEFRegisteredMemoryType.Whole:
363+
case (int)TEFRegisteredMemoryType.PartialInput:
364+
case (int)TEFRegisteredMemoryType.PartialOutput:
365+
case (int)TEFRegisteredMemoryType.PartialInOut:
366+
((RegisteredMemoryReference)dst).Size = src.memref.size;
367+
break;
368+
369+
default: break;
370+
}
371+
}
372+
312373
internal void Open(Guid destination, uint loginMethod, byte[] connectionData, Parameter[] paramlist)
313374
{
314375
Interop.TEEC_UUID uuid = Interop.TEEC_UUID.ToTeecUuid(destination);
315376
Interop.TEEC_Operation op = new Interop.TEEC_Operation();
316-
uint ro;
317377

378+
op.started=0;
379+
op.paramTypes=0;
380+
for (int i=0; i < 4 && i < paramlist.Length; ++i) {
381+
op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i);
382+
}
383+
384+
uint ro;
318385
int ret = Interop.Libteec.OpenSession(ref context, ref session, ref uuid, loginMethod, connectionData, ref op, out ro);
319386
//MAYBE map origin of return code to specyfic Exception
320387
Interop.CheckNThrowException(ret, string.Format("OpenSession('{0}')", destination));
388+
for (int i=0; i < 4 && i < paramlist.Length; ++i) {
389+
UpdateParam(op.paramlist[i], ref paramlist[i]);
390+
}
321391
}
322392

323393
/// <summary>
@@ -358,16 +428,17 @@ public void InvokeCommand(uint commandID, Parameter[] paramlist)
358428
Interop.TEEC_Operation op = new Interop.TEEC_Operation();
359429
op.started=0;
360430
op.paramTypes=0;
361-
362-
for (int i=0; i < 4; ++i) {
363-
Parameter p = paramlist[i];
364-
//TODO fill TEEC_Operation struct
431+
for (int i=0; i < 4 && i < paramlist.Length; ++i) {
432+
op.paramTypes |= InitParam(ref op.paramlist[i], paramlist[i]) << (4*i);
365433
}
366434

367435
uint ro;
368436
int ret = Interop.Libteec.InvokeCommand(ref session, commandID, ref op, out ro);
369437
//MAYBE map origin of return code to specific Exception
370438
Interop.CheckNThrowException(ret, string.Format("InvokeCommand({0})", commandID));
439+
for (int i=0; i < 4 && i < paramlist.Length; ++i) {
440+
UpdateParam(op.paramlist[i], ref paramlist[i]);
441+
}
371442
}
372443

373444
/// <summary>
@@ -417,10 +488,21 @@ public Context(string name)
417488
context = new Interop.TEEC_Context();
418489
if (name != null && name.Length == 0)
419490
name = null;
420-
int ret = Interop.Libteec.InitializeContext(name, ref context);
421-
Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
491+
try {
492+
int ret = Interop.Libteec.InitializeContext(name, ref context);
493+
Interop.CheckNThrowException(ret, string.Format("InititalizeContext('{0}')", name));
494+
}
495+
catch (global::System.DllNotFoundException e)
496+
{
497+
unchecked {
498+
Interop.CheckNThrowException((int)Interop.LibteecError.NotImplemented, "Not found: " + e.Message);
499+
}
500+
}
422501
}
423502

503+
/// <summary>
504+
/// Destructor of the class.
505+
/// </summary>
424506
~Context()
425507
{
426508
Dispose();
@@ -434,8 +516,10 @@ public Context(string name)
434516
/// <privlevel>partner</privlevel>
435517
/// <feature>http://tizen.org/feature/security.tee</feature>
436518
public void Dispose() {
437-
Interop.Libteec.FinalizeContext(ref context);
438-
//context.imp = null;
519+
try {
520+
Interop.Libteec.FinalizeContext(ref context);
521+
}
522+
catch (global::System.DllNotFoundException) { }
439523
}
440524

441525
/// <summary>

0 commit comments

Comments
 (0)