RDK Documentation (Open Sourced RDK Components)
librmh_wrap.c
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 
20 #include "librmh.h"
21 #include "rdk_moca_hal.h"
22 
23 static
24 __attribute__((constructor(200))) void pRMH_APIWRAP_Initialize() {
25  strncpy( hRMHGeneric_APIList.apiListName, "All APIs", sizeof(hRMHGeneric_APIList.apiListName));
26  hRMHGeneric_APIList.apiListSize=0;
27 
28  strncpy( hRMHGeneric_SoCUnimplementedAPIList.apiListName, "Unimplemented APIs", sizeof(hRMHGeneric_SoCUnimplementedAPIList.apiListName));
29  hRMHGeneric_SoCUnimplementedAPIList.apiListSize=0;
30  hRMHGeneric_APITags.tagListSize=0;
31 }
32 
33 int pRMH_APIWRAP_Compare(const void* a, const void* b) {
34  const RMH_API* _a=* (RMH_API * const *)a;
35  const RMH_API* _b=* (RMH_API * const *)b;
36  return strcasecmp(_a->apiName, _b->apiName);
37 }
38 
39 static
40 __attribute__((constructor(400))) void pRMH_APIWRAP_Finalize() {
41  /* Sort the list to ensure consistant ordering */
42  qsort(hRMHGeneric_APIList.apiList, hRMHGeneric_APIList.apiListSize, sizeof(RMH_API*), pRMH_APIWRAP_Compare);
43 }
44 
45 static inline
46 void pRMH_APIWRAP_PreAPIExecute(const RMH_Handle handle, const char *api, const bool socEnabled, const bool socBeforeGeneric) {
47  handle->apiDepth++;
48  RMH_PrintTrace("+++++ Enter %s [%s] ++++\n", api, !socEnabled ? "Generic Only" :
49  socBeforeGeneric ? "SoC Before Generic" : "SoC After Generic");
50  gettimeofday(&handle->startTime, NULL);
51 }
52 
53 static inline
54 RMH_Result pRMH_APIWRAP_PostAPIExecute(const RMH_Handle handle, const char *api, const RMH_Result genRet, const RMH_Result socRet) {
55  double elapsedTime;
56  struct timeval stopTime;
57  RMH_Result ret = (socRet == RMH_SUCCESS) ? genRet : socRet;
58  gettimeofday(&stopTime, NULL);
59  elapsedTime = (stopTime.tv_sec - handle->startTime.tv_sec) * 1000.0; /* sec to ms */
60  elapsedTime += (stopTime.tv_usec - handle->startTime.tv_usec) / 1000.0; /* us to ms */
61  RMH_PrintTrace("------ Exit %s [%s] -- [Time: %.02fms] ----\n", api, RMH_ResultToString(ret), elapsedTime);
62  handle->apiDepth--;
63  return ret;
64 }
65 
66 static inline
67 RMH_Result pRMH_APIWRAP_GetSoCAPI(const RMH_Handle handle, const char *apiName, RMH_Result (**apiFunc)()) {
68  RMH_Result ret=RMH_UNIMPLEMENTED;
69  if (handle->soclib) {
70  char *dlErr = dlerror(); /* Check error first to clear it out */
71  *apiFunc=dlsym(handle->soclib, apiName);
72  dlErr = dlerror();
73  if (dlErr) {
74  RMH_PrintTrace("Error for '%s' in dlopen: '%s'\n", apiName, dlErr);
75  }
76  else if (*apiFunc) {
77  RMH_PrintTrace("Located SoC API '%s' at 0x%p\n", apiName, *apiFunc);
78  if (handle->handle) {
79  ret = RMH_SUCCESS;
80  } else {
81  RMH_PrintTrace("Located SoC API '%s' at 0x%p but SoC library is not initialized!\n", apiName, *apiFunc);
82  ret = RMH_INVALID_INTERNAL_STATE;
83  }
84  }
85  else {
86  RMH_PrintTrace("Unable to find SoC implementation of '%s'\n", apiName);
87  }
88  }
89  return ret;
90 }
91 
92 static inline
93 void pRMH_APIWRAP_RegisterAPI(RMH_API *api) {
94  if (hRMHGeneric_APIList.apiListSize >= RMH_MAX_NUM_APIS) {
95  fprintf(stderr, "ERROR: Unable to expose API %s! Increase the size of RMH_MAX_NUM_APIS\n", api->apiName);
96  return;
97  }
98  hRMHGeneric_APIList.apiList[hRMHGeneric_APIList.apiListSize++]=api;
99 }
100 
101 
102 
103 /**********************************************************************************************************************
104 The meaning of each of these RMH_API_IMPLEMENTATION macros is listed in rdk_moca_hal_types. For our purpose herem these
105 just redirect to __RMH_WRAP_API() if an API wrapper is to be defined. The first three parameters passed to
106 __RMH_WRAP_API() indicate how it should call the APIs.
107 
108 In the case of RMH_API_IMPLEMENTATION_NO_WRAP() no wrapper is requred so it maps directly to __RMH_REGISTER_API().
109 **********************************************************************************************************************/
110 #undef RMH_API_IMPLEMENTATION_SOC_ONLY
111 #undef RMH_API_IMPLEMENTATION_GENERIC_ONLY
112 #undef RMH_API_IMPLEMENTATION_SOC_THEN_GENERIC
113 #undef RMH_API_IMPLEMENTATION_GENERIC_THEN_SOC
114 
115 #define RMH_API_IMPLEMENTATION_SOC_ONLY(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, WRAP_API, TAGS_STR) \
116  __RMH_API_WRAP_##WRAP_API(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, TAGS_STR, false, true, false);
117 
118 #define RMH_API_IMPLEMENTATION_GENERIC_ONLY(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, WRAP_API, TAGS_STR) \
119  __RMH_API_WRAP_##WRAP_API(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, TAGS_STR, true, false, false);
120 
121 #define RMH_API_IMPLEMENTATION_SOC_THEN_GENERIC(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, WRAP_API, TAGS_STR) \
122  __RMH_API_WRAP_##WRAP_API(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, TAGS_STR, true, true, true);
123 
124 #define RMH_API_IMPLEMENTATION_GENERIC_THEN_SOC(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, WRAP_API, TAGS_STR) \
125  __RMH_API_WRAP_##WRAP_API(DECLARATION, API_NAME, DESCRIPTION_STR, PARAMS_LIST, TAGS_STR, true, true, false);
126 
127 #include "librmh_wrap.h"
128 
129 /* Reinclude API header to use the redefined macros to setup necessary functions and structs */
130 #undef RMH_API_H
131 #include "rmh_api.h"
RMH
Definition: librmh.h:57
RMH_API
Definition: rmh_type.h:222
RMH_ResultToString
const char *const RMH_ResultToString(const RMH_Result value)
Convert RMH_Result to a string.
Definition: librmh_api_no_wrap.c:84