• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2019, The Linux Foundation. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *    * Redistributions of source code must retain the above copyright
8  *      notice, this list of conditions and the following disclaimer.
9  *    * Redistributions in binary form must reproduce the above
10  *      copyright notice, this list of conditions and the following
11  *      disclaimer in the documentation and/or other materials provided
12  *      with the distribution.
13  *    * Neither the name of The Linux Foundation nor the names of its
14  *      contributors may be used to endorse or promote products derived
15  *      from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20  * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24  * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 #ifndef _APPS_REMOTECTL_SKEL_H
30 #define _APPS_REMOTECTL_SKEL_H
31 #include "apps_remotectl.h"
32 #include "remote.h"
33 #include <string.h>
34 #ifndef ALLOCATOR_H
35 #define ALLOCATOR_H
36 
37 #include <stdlib.h>
38 #include <stdint.h>
39 
40 typedef struct _heap _heap;
41 struct _heap {
42    _heap* pPrev;
43    const char* loc;
44    uint64_t buf;
45 };
46 
47 typedef struct allocator {
48    _heap* pheap;
49    uint8_t* stack;
50    uint8_t* stackEnd;
51    int nSize;
52 } allocator;
53 
_heap_alloc(_heap ** ppa,const char * loc,int size,void ** ppbuf)54 static __inline int _heap_alloc(_heap** ppa, const char* loc, int size, void** ppbuf) {
55    _heap* pn = 0;
56    pn = malloc(size + sizeof(_heap) - sizeof(uint64_t));
57    if(pn != 0) {
58       pn->pPrev = *ppa;
59       pn->loc = loc;
60       *ppa = pn;
61       *ppbuf = (void*)&(pn->buf);
62       return 0;
63    } else {
64       return -1;
65    }
66 }
67 #define _ALIGN_SIZE(x, y) (((x) + (y-1)) & ~(y-1))
68 
69 
allocator_alloc(allocator * me,const char * loc,int size,unsigned int al,void ** ppbuf)70 static __inline int allocator_alloc(allocator* me,
71                                     const char* loc,
72                                     int size,
73                                     unsigned int al,
74                                     void** ppbuf) {
75    if(size < 0) {
76       return -1;
77    } else if (size == 0) {
78       *ppbuf = 0;
79       return 0;
80    }
81    if((_ALIGN_SIZE((uintptr_t)me->stackEnd, al) + size) < (uintptr_t)me->stack + me->nSize) {
82       *ppbuf = (uint8_t*)_ALIGN_SIZE((uintptr_t)me->stackEnd, al);
83       me->stackEnd = (uint8_t*)_ALIGN_SIZE((uintptr_t)me->stackEnd, al) + size;
84       return 0;
85    } else {
86       return _heap_alloc(&me->pheap, loc, size, ppbuf);
87    }
88 }
89 
90 
allocator_deinit(allocator * me)91 static __inline void allocator_deinit(allocator* me) {
92    _heap* pa = me->pheap;
93    while(pa != 0) {
94       _heap* pn = pa;
95       const char* loc = pn->loc;
96       (void)loc;
97       pa = pn->pPrev;
98       free(pn);
99    }
100 }
101 
allocator_init(allocator * me,uint8_t * stack,int stackSize)102 static __inline void allocator_init(allocator* me, uint8_t* stack, int stackSize) {
103    me->stack =  stack;
104    me->stackEnd =  stack + stackSize;
105    me->nSize = stackSize;
106    me->pheap = 0;
107 }
108 
109 
110 #endif // ALLOCATOR_H
111 
112 #ifndef SLIM_H
113 #define SLIM_H
114 
115 #include <stdint.h>
116 
117 //a C data structure for the idl types that can be used to implement
118 //static and dynamic language bindings fairly efficiently.
119 //
120 //the goal is to have a minimal ROM and RAM footprint and without
121 //doing too many allocations.  A good way to package these things seemed
122 //like the module boundary, so all the idls within  one module can share
123 //all the type references.
124 
125 
126 #define PARAMETER_IN       0x0
127 #define PARAMETER_OUT      0x1
128 #define PARAMETER_INOUT    0x2
129 #define PARAMETER_ROUT     0x3
130 #define PARAMETER_INROUT   0x4
131 
132 //the types that we get from idl
133 #define TYPE_OBJECT             0x0
134 #define TYPE_INTERFACE          0x1
135 #define TYPE_PRIMITIVE          0x2
136 #define TYPE_ENUM               0x3
137 #define TYPE_STRING             0x4
138 #define TYPE_WSTRING            0x5
139 #define TYPE_STRUCTURE          0x6
140 #define TYPE_UNION              0x7
141 #define TYPE_ARRAY              0x8
142 #define TYPE_SEQUENCE           0x9
143 
144 //these require the pack/unpack to recurse
145 //so it's a hint to those languages that can optimize in cases where
146 //recursion isn't necessary.
147 #define TYPE_COMPLEX_STRUCTURE  (0x10 | TYPE_STRUCTURE)
148 #define TYPE_COMPLEX_UNION      (0x10 | TYPE_UNION)
149 #define TYPE_COMPLEX_ARRAY      (0x10 | TYPE_ARRAY)
150 #define TYPE_COMPLEX_SEQUENCE   (0x10 | TYPE_SEQUENCE)
151 
152 
153 typedef struct Type Type;
154 
155 #define INHERIT_TYPE\
156    int32_t nativeSize;                /*in the simple case its the same as wire size and alignment*/\
157    union {\
158       struct {\
159          const uintptr_t         p1;\
160          const uintptr_t         p2;\
161       } _cast;\
162       struct {\
163          uint32_t  iid;\
164          uint32_t  bNotNil;\
165       } object;\
166       struct {\
167          const Type  *arrayType;\
168          int32_t      nItems;\
169       } array;\
170       struct {\
171          const Type *seqType;\
172          int32_t      nMaxLen;\
173       } seqSimple; \
174       struct {\
175          uint32_t bFloating;\
176          uint32_t bSigned;\
177       } prim; \
178       const SequenceType* seqComplex;\
179       const UnionType  *unionType;\
180       const StructType *structType;\
181       int32_t         stringMaxLen;\
182       uint8_t        bInterfaceNotNil;\
183    } param;\
184    uint8_t    type;\
185    uint8_t    nativeAlignment\
186 
187 typedef struct UnionType UnionType;
188 typedef struct StructType StructType;
189 typedef struct SequenceType SequenceType;
190 struct Type {
191    INHERIT_TYPE;
192 };
193 
194 struct SequenceType {
195    const Type *         seqType;
196    uint32_t               nMaxLen;
197    uint32_t               inSize;
198    uint32_t               routSizePrimIn;
199    uint32_t               routSizePrimROut;
200 };
201 
202 //byte offset from the start of the case values for
203 //this unions case value array.  it MUST be aligned
204 //at the alignment requrements for the descriptor
205 //
206 //if negative it means that the unions cases are
207 //simple enumerators, so the value read from the descriptor
208 //can be used directly to find the correct case
209 typedef union CaseValuePtr CaseValuePtr;
210 union CaseValuePtr {
211    const uint8_t*   value8s;
212    const uint16_t*  value16s;
213    const uint32_t*  value32s;
214    const uint64_t*  value64s;
215 };
216 
217 //these are only used in complex cases
218 //so I pulled them out of the type definition as references to make
219 //the type smaller
220 struct UnionType {
221    const Type           *descriptor;
222    uint32_t               nCases;
223    const CaseValuePtr   caseValues;
224    const Type * const   *cases;
225    int32_t               inSize;
226    int32_t               routSizePrimIn;
227    int32_t               routSizePrimROut;
228    uint8_t                inAlignment;
229    uint8_t                routAlignmentPrimIn;
230    uint8_t                routAlignmentPrimROut;
231    uint8_t                inCaseAlignment;
232    uint8_t                routCaseAlignmentPrimIn;
233    uint8_t                routCaseAlignmentPrimROut;
234    uint8_t                nativeCaseAlignment;
235    uint8_t              bDefaultCase;
236 };
237 
238 struct StructType {
239    uint32_t               nMembers;
240    const Type * const   *members;
241    int32_t               inSize;
242    int32_t               routSizePrimIn;
243    int32_t               routSizePrimROut;
244    uint8_t                inAlignment;
245    uint8_t                routAlignmentPrimIn;
246    uint8_t                routAlignmentPrimROut;
247 };
248 
249 typedef struct Parameter Parameter;
250 struct Parameter {
251    INHERIT_TYPE;
252    uint8_t    mode;
253    uint8_t  bNotNil;
254 };
255 
256 #define SLIM_SCALARS_IS_DYNAMIC(u) (((u) & 0x00ffffff) == 0x00ffffff)
257 
258 typedef struct Method Method;
259 struct Method {
260    uint32_t                    uScalars;            //no method index
261    int32_t                     primInSize;
262    int32_t                     primROutSize;
263    int                         maxArgs;
264    int                         numParams;
265    const Parameter * const     *params;
266    uint8_t                       primInAlignment;
267    uint8_t                       primROutAlignment;
268 };
269 
270 typedef struct Interface Interface;
271 
272 struct Interface {
273    int                            nMethods;
274    const Method  * const          *methodArray;
275    int                            nIIds;
276    const uint32_t                   *iids;
277    const uint16_t*                  methodStringArray;
278    const uint16_t*                  methodStrings;
279    const char*                    strings;
280 };
281 
282 
283 #endif //SLIM_H
284 
285 
286 #ifndef _APPS_REMOTECTL_SLIM_H
287 #define _APPS_REMOTECTL_SLIM_H
288 #include "remote.h"
289 #include <stdint.h>
290 
291 #ifndef __QAIC_SLIM
292 #define __QAIC_SLIM(ff) ff
293 #endif
294 #ifndef __QAIC_SLIM_EXPORT
295 #define __QAIC_SLIM_EXPORT
296 #endif
297 
298 static const Type types[1];
299 static const Type types[1] = {{0x1,{{(const uintptr_t)0,(const uintptr_t)0}}, 2,0x1}};
300 static const Parameter parameters[4] = {{0x8,{{(const uintptr_t)0x0,0}}, 4,0x4,0,0},{0x4,{{(const uintptr_t)0,(const uintptr_t)1}}, 2,0x4,3,0},{0x8,{{(const uintptr_t)&(types[0]),(const uintptr_t)0x0}}, 9,0x4,3,0},{0x4,{{(const uintptr_t)0,(const uintptr_t)1}}, 2,0x4,0,0}};
301 static const Parameter* const parameterArrays[7] = {(&(parameters[0])),(&(parameters[1])),(&(parameters[2])),(&(parameters[1])),(&(parameters[3])),(&(parameters[2])),(&(parameters[1]))};
302 static const Method methods[2] = {{REMOTE_SCALARS_MAKEX(0,0,0x2,0x2,0x0,0x0),0x8,0x8,6,4,(&(parameterArrays[0])),0x4,0x4},{REMOTE_SCALARS_MAKEX(0,0,0x1,0x2,0x0,0x0),0x8,0x4,5,3,(&(parameterArrays[4])),0x4,0x4}};
303 static const Method* const methodArrays[2] = {&(methods[0]),&(methods[1])};
304 static const char strings[36] = "dlerror\0handle\0close\0nErr\0name\0open\0";
305 static const uint16_t methodStrings[9] = {31,26,8,0,21,15,8,0,21};
306 static const uint16_t methodStringsArrays[2] = {0,5};
307 __QAIC_SLIM_EXPORT const Interface __QAIC_SLIM(apps_remotectl_slim) = {2,&(methodArrays[0]),0,0,&(methodStringsArrays [0]),methodStrings,strings};
308 #endif //_APPS_REMOTECTL_SLIM_H
309 #ifdef __GNUC__
310 #pragma GCC diagnostic ignored "-Wpragmas"
311 #pragma GCC diagnostic ignored "-Wuninitialized"
312 #pragma GCC diagnostic ignored "-Wunused-parameter"
313 #endif
314 
315 #ifndef __QAIC_REMOTE
316 #define __QAIC_REMOTE(ff) ff
317 #endif //__QAIC_REMOTE
318 
319 #ifndef __QAIC_HEADER
320 #define __QAIC_HEADER(ff) ff
321 #endif //__QAIC_HEADER
322 
323 #ifndef __QAIC_HEADER_EXPORT
324 #define __QAIC_HEADER_EXPORT
325 #endif // __QAIC_HEADER_EXPORT
326 
327 #ifndef __QAIC_HEADER_ATTRIBUTE
328 #define __QAIC_HEADER_ATTRIBUTE
329 #endif // __QAIC_HEADER_ATTRIBUTE
330 
331 #ifndef __QAIC_IMPL
332 #define __QAIC_IMPL(ff) ff
333 #endif //__QAIC_IMPL
334 
335 #ifndef __QAIC_IMPL_EXPORT
336 #define __QAIC_IMPL_EXPORT
337 #endif // __QAIC_IMPL_EXPORT
338 
339 #ifndef __QAIC_IMPL_ATTRIBUTE
340 #define __QAIC_IMPL_ATTRIBUTE
341 #endif // __QAIC_IMPL_ATTRIBUTE
342 
343 #ifndef __QAIC_STUB
344 #define __QAIC_STUB(ff) ff
345 #endif //__QAIC_STUB
346 
347 #ifndef __QAIC_STUB_EXPORT
348 #define __QAIC_STUB_EXPORT
349 #endif // __QAIC_STUB_EXPORT
350 
351 #ifndef __QAIC_STUB_ATTRIBUTE
352 #define __QAIC_STUB_ATTRIBUTE
353 #endif // __QAIC_STUB_ATTRIBUTE
354 
355 #ifndef __QAIC_SKEL
356 #define __QAIC_SKEL(ff) ff
357 #endif //__QAIC_SKEL__
358 
359 #ifndef __QAIC_SKEL_EXPORT
360 #define __QAIC_SKEL_EXPORT
361 #endif // __QAIC_SKEL_EXPORT
362 
363 #ifndef __QAIC_SKEL_ATTRIBUTE
364 #define __QAIC_SKEL_ATTRIBUTE
365 #endif // __QAIC_SKEL_ATTRIBUTE
366 
367 #ifdef __QAIC_DEBUG__
368    #ifndef __QAIC_DBG_PRINTF__
369    #define __QAIC_DBG_PRINTF__( ee ) do { printf ee ; } while(0)
370    #endif
371 #else
372    #define __QAIC_DBG_PRINTF__( ee ) (void)0
373 #endif
374 
375 
376 #define _OFFSET(src, sof)  ((void*)(((char*)(src)) + (sof)))
377 
378 #define _COPY(dst, dof, src, sof, sz)  \
379    do {\
380          struct __copy { \
381             char ar[sz]; \
382          };\
383          *(struct __copy*)_OFFSET(dst, dof) = *(struct __copy*)_OFFSET(src, sof);\
384    } while (0)
385 
386 #define _ASSIGN(dst, src, sof)  \
387    do {\
388       dst = OFFSET(src, sof); \
389    } while (0)
390 
391 #define _STD_STRLEN_IF(str) (str == 0 ? 0 : strlen(str))
392 
393 #include "AEEStdErr.h"
394 
395 #define _TRY(ee, func) \
396    do { \
397       if (AEE_SUCCESS != ((ee) = func)) {\
398          __QAIC_DBG_PRINTF__((__FILE_LINE__  ": error: %d\n", (int)(ee)));\
399          goto ee##bail;\
400       } \
401    } while (0)
402 
403 #define _CATCH(exception) exception##bail: if (exception != AEE_SUCCESS)
404 
405 #define _ASSERT(nErr, ff) _TRY(nErr, 0 == (ff) ? AEE_EBADPARM : AEE_SUCCESS)
406 
407 #ifdef __QAIC_DEBUG__
408 #define _ALLOCATE(nErr, pal, size, alignment, pv) _TRY(nErr, allocator_alloc(pal, __FILE_LINE__, size, alignment, (void**)&pv))
409 #else
410 #define _ALLOCATE(nErr, pal, size, alignment, pv) _TRY(nErr, allocator_alloc(pal, 0, size, alignment, (void**)&pv))
411 #endif
412 
413 
414 #ifdef __cplusplus
415 extern "C" {
416 #endif
_skel_method(int (* _pfn)(uint32_t,char *,uint32_t,uint32_t *),uint32_t _sc,remote_arg * _pra)417 static __inline int _skel_method(int (*_pfn)(uint32_t, char*, uint32_t, uint32_t*), uint32_t _sc, remote_arg* _pra) {
418    remote_arg* _praEnd;
419    uint32_t _in0[1];
420    char* _rout1[1];
421    uint32_t _rout1Len[1];
422    uint32_t _rout2[1];
423    uint32_t* _primIn;
424    int _numIn[1];
425    uint32_t* _primROut;
426    remote_arg* _praIn;
427    remote_arg* _praROut;
428    int _nErr = 0;
429    _praEnd = ((_pra + REMOTE_SCALARS_INBUFS(_sc)) + REMOTE_SCALARS_OUTBUFS(_sc));
430    _ASSERT(_nErr, (_pra + 3) <= _praEnd);
431    _numIn[0] = (REMOTE_SCALARS_INBUFS(_sc) - 1);
432    _ASSERT(_nErr, _pra[0].buf.nLen >= 8);
433    _primIn = _pra[0].buf.pv;
434    _ASSERT(_nErr, _pra[(_numIn[0] + 1)].buf.nLen >= 4);
435    _primROut = _pra[(_numIn[0] + 1)].buf.pv;
436    _COPY(_in0, 0, _primIn, 0, 4);
437    _COPY(_rout1Len, 0, _primIn, 4, 4);
438    _praIn = (_pra + 1);
439    _praROut = (_praIn + _numIn[0] + 1);
440    _ASSERT(_nErr, (int)((_praROut[0].buf.nLen / 1)) >= (int)(_rout1Len[0]));
441    _rout1[0] = _praROut[0].buf.pv;
442    _TRY(_nErr, _pfn(*_in0, *_rout1, *_rout1Len, _rout2));
443    _COPY(_primROut, 0, _rout2, 0, 4);
444    _CATCH(_nErr) {}
445    return _nErr;
446 }
_skel_method_1(int (* _pfn)(char *,uint32_t *,char *,uint32_t,uint32_t *),uint32_t _sc,remote_arg * _pra)447 static __inline int _skel_method_1(int (*_pfn)(char*, uint32_t*, char*, uint32_t, uint32_t*), uint32_t _sc, remote_arg* _pra) {
448    remote_arg* _praEnd;
449    char* _in0[1];
450    uint32_t _in0Len[1];
451    uint32_t _rout1[1];
452    char* _rout2[1];
453    uint32_t _rout2Len[1];
454    uint32_t _rout3[1];
455    uint32_t* _primIn;
456    int _numIn[1];
457    uint32_t* _primROut;
458    remote_arg* _praIn;
459    remote_arg* _praROut;
460    int _nErr = 0;
461    _praEnd = ((_pra + REMOTE_SCALARS_INBUFS(_sc)) + REMOTE_SCALARS_OUTBUFS(_sc));
462    _ASSERT(_nErr, (_pra + 4) <= _praEnd);
463    _numIn[0] = (REMOTE_SCALARS_INBUFS(_sc) - 1);
464    _ASSERT(_nErr, _pra[0].buf.nLen >= 8);
465    _primIn = _pra[0].buf.pv;
466    _ASSERT(_nErr, _pra[(_numIn[0] + 1)].buf.nLen >= 8);
467    _primROut = _pra[(_numIn[0] + 1)].buf.pv;
468    _COPY(_in0Len, 0, _primIn, 0, 4);
469    _praIn = (_pra + 1);
470    _ASSERT(_nErr, (int)((_praIn[0].buf.nLen / 1)) >= (int)(_in0Len[0]));
471    _in0[0] = _praIn[0].buf.pv;
472    if(_in0Len[0] > 0)
473    {
474       _in0[0][(_in0Len[0] - 1)] = 0;
475    }
476    _COPY(_rout2Len, 0, _primIn, 4, 4);
477    _praROut = (_praIn + _numIn[0] + 1);
478    _ASSERT(_nErr, (int)((_praROut[0].buf.nLen / 1)) >= (int)(_rout2Len[0]));
479    _rout2[0] = _praROut[0].buf.pv;
480    _TRY(_nErr, _pfn(*_in0, _rout1, *_rout2, *_rout2Len, _rout3));
481    _COPY(_primROut, 0, _rout1, 0, 4);
482    _COPY(_primROut, 4, _rout3, 0, 4);
483    _CATCH(_nErr) {}
484    return _nErr;
485 }
__QAIC_SKEL(apps_remotectl_skel_invoke)486 __QAIC_SKEL_EXPORT int __QAIC_SKEL(apps_remotectl_skel_invoke)(uint32_t _sc, remote_arg* _pra) __QAIC_SKEL_ATTRIBUTE {
487    switch(REMOTE_SCALARS_METHOD(_sc))
488    {
489       case 0:
490       return _skel_method_1((void*)__QAIC_IMPL(apps_remotectl_open), _sc, _pra);
491       case 1:
492       return _skel_method((void*)__QAIC_IMPL(apps_remotectl_close), _sc, _pra);
493    }
494    return AEE_EUNSUPPORTED;
495 }
496 #ifdef __cplusplus
497 }
498 #endif
499 #endif //_APPS_REMOTECTL_SKEL_H
500