RDK Documentation (Open Sourced RDK Components)
idm_library.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 2018 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 #include <sys/types.h>
20 #include <string.h>
21 #include "rdk_safeclib.h"
22 #ifdef BROADBAND
23 #include <syscfg/syscfg.h>
24 #endif
25 #include <glib/gprintf.h>
26 #include <glib/gstdio.h>
27 #ifndef BROADBAND
28 #ifdef ENABLE_RFC
29 #include "rfcapi.h"
30 #endif
31 #else
32 #include <platform_hal.h>
33 #endif
34 #if defined(CLIENT_XCAL_SERVER) && !defined(BROADBAND)
35 #include "mfrMgr.h"
36 #endif
37 #include <net/if.h>
38 #include <arpa/inet.h>
39 #include <ifaddrs.h>
40 gboolean bSerialNum=FALSE;
41 #define BOOL unsigned char
42 #define ACCOUNTID_SIZE 30
43 gboolean getserialnum(GString* ownSerialNo)
44 {
45 #ifndef CLIENT_XCAL_SERVER
46  GError *error=NULL;
47  gboolean result = FALSE;
48  gchar* udhcpcvendorfile = NULL;
49 
50  result = g_file_get_contents ("//etc//udhcpc.vendor_specific", &udhcpcvendorfile, NULL, &error);
51  if (result == FALSE) {
52  g_message("Problem in reading /etc/udhcpcvendorfile file %s", error->message);
53  }
54  else
55  {
56  /* reset result = FALSE to identify serial number from udhcpcvendorfile contents */
57  result = FALSE;
58  gchar **tokens = g_strsplit_set(udhcpcvendorfile," \n\t\b\0", -1);
59  guint toklength = g_strv_length(tokens);
60  guint loopvar=0;
61  for (loopvar=0; loopvar<toklength; loopvar++)
62  {
63  //g_print("Token is %s\n",g_strstrip(tokens[loopvar]));
64  if (g_strrstr(g_strstrip(tokens[loopvar]), "SUBOPTION4"))
65  {
66  if ((loopvar+1) < toklength )
67  {
68  g_string_assign(ownSerialNo, g_strstrip(tokens[loopvar+2]));
69  bSerialNum=TRUE;
70  g_message("serialNumber fetched from udhcpcvendorfile:%s", ownSerialNo->str);
71  }
72  result = TRUE;
73  break;
74  }
75  }
76  g_strfreev(tokens);
77  }
78  //diagid=1000;
79  if(error)
80  {
81  /* g_clear_error() frees the GError *error memory and reset pointer if set in above operation */
82  g_clear_error(&error);
83  }
84  if (udhcpcvendorfile) g_free(udhcpcvendorfile);
85  return result;
86 #elif BROADBAND
87  gboolean result = FALSE;
88  char serialNumber[50] = {0};
89  if ( platform_hal_GetSerialNumber(serialNumber) == 0)
90  {
91  g_message("serialNumber returned from hal:%s", serialNumber);
92  g_string_assign(ownSerialNo, serialNumber);
93  result = TRUE;
94  bSerialNum=TRUE;
95  }
96  else
97  {
98  g_error("Unable to get SerialNumber");
99  }
100  return result;
101 #else
102  bool bRet;
104  IARM_Result_t iarmRet = IARM_RESULT_IPCCORE_FAIL;
105  memset(&param, 0, sizeof(param));
106  param.type = mfrSERIALIZED_TYPE_SERIALNUMBER;
108  if(iarmRet == IARM_RESULT_SUCCESS)
109  {
110  if(param.buffer && param.bufLen)
111  {
112  g_message( " serialized data %s \n",param.buffer );
113  g_string_assign(ownSerialNo,param.buffer);
114  bRet = true;
115  bSerialNum=TRUE;
116  }
117  else
118  {
119  g_message( " serialized data is empty \n" );
120  bRet = false;
121  }
122  }
123  else
124  {
125  bRet = false;
126  g_message( "IARM CALL failed for mfrtype \n");
127  }
128  return bRet;
129 #endif
130 }
131 int getipaddress(const char *ifname, char *ipAddressBuffer,gboolean ipv6Enabled)
132 {
133  struct ifaddrs *ifAddrStruct = NULL;
134  struct ifaddrs *ifa = NULL;
135  void *tmpAddrPtr = NULL;
136  getifaddrs(&ifAddrStruct);
137  errno_t rc = -1;
138  int ind = -1;
139  //char addressBuffer[INET_ADDRSTRLEN] = NULL;
140  int found = 0;
141  for (ifa = ifAddrStruct; ifa != NULL; ifa = ifa->ifa_next) {
142  if (ifa->ifa_addr == NULL) continue;
143  if (ipv6Enabled == TRUE) {
144  // check it is IP6
145  // is a valid IP6 Address
146  rc = strcmp_s(ifa->ifa_name,strlen(ifa->ifa_name),ifname,&ind);
147  ERR_CHK(rc);
148  if (((!ind) && (rc == EOK)) && (ifa ->ifa_addr->sa_family == AF_INET6)) {
149  tmpAddrPtr = &((struct sockaddr_in6 *)ifa->ifa_addr)->sin6_addr;
150  inet_ntop(AF_INET6, tmpAddrPtr, ipAddressBuffer, INET6_ADDRSTRLEN);
151  if (strcmp_s(ifa->ifa_name,strlen(ifa->ifa_name),ifname,&ind)==EOK){
152  found = 1;
153  break;
154  }
155  }
156  } else {
157  if (ifa ->ifa_addr->sa_family == AF_INET) { // check it is IP4
158  // is a valid IP4 Address
159  tmpAddrPtr = &((struct sockaddr_in *)ifa->ifa_addr)->sin_addr;
160  inet_ntop(AF_INET, tmpAddrPtr, ipAddressBuffer, INET_ADDRSTRLEN);
161  //if (strcmp(ifa->ifa_name,"eth0")==0)
162  rc = strcmp_s(ifa->ifa_name,strlen(ifa->ifa_name),ifname,&ind);
163  ERR_CHK(rc);
164  if((!ind) && (rc == EOK)) {
165  found = 1;
166  break;
167  }
168  }
169  }
170  }
171  if (ifAddrStruct != NULL) freeifaddrs(ifAddrStruct);
172  return found;
173 }
174 BOOL check_null(char *str)
175 {
176  if (str) {
177  return TRUE;
178  }
179  return FALSE;
180 }
181 BOOL check_empty(char *str)
182 {
183  if (str[0]) {
184  return TRUE;
185  }
186  return FALSE;
187 }
188 #ifdef BROADBAND
189 BOOL getAccountId(char *outValue)
190 {
191  char temp[ACCOUNTID_SIZE] = {0};
192  int rc;
193  syscfg_init();
194  rc = syscfg_get(NULL, "AccountID", temp, sizeof(temp));
195  if(!rc)
196  {
197  if ((outValue != NULL))
198  {
199  strncpy(outValue, temp, ACCOUNTID_SIZE-1);
200  return TRUE;
201  }
202  }
203  else
204  {
205  g_message("getAccountId: Unable to get the Account Id");
206  }
207  return FALSE;
208 }
209 #else
210 BOOL getAccountId(char *outValue)
211 {
212  gboolean result = FALSE;
213 #ifdef ENABLE_RFC
214  RFC_ParamData_t param = {0};
215  WDMP_STATUS status = getRFCParameter("XUPNP","Device.DeviceInfo.X_RDKCENTRAL-COM_RFC.Feature.AccountInfo.AccountID",&param);
216  if (status == WDMP_SUCCESS)
217  {
218  if (((param.value == NULL)))
219  {
220  g_message("getAccountId : NULL string !");
221  return result;
222  }
223  else
224  {
225  if (strncpy(outValue,param.value, ACCOUNTID_SIZE-1))
226  {
227  result = TRUE;
228  }
229  }
230  }
231  else
232  {
233  g_message("getAccountId: getRFCParameter Failed : %s\n", getRFCErrorString(status));
234  }
235 #else
236  g_message("Not built with RFC support.");
237 #endif
238  return result;
239 }
240 #endif
_IARM_Bus_MFRLib_GetSerializedData_Param_t::buffer
char buffer[(1280)]
Definition: mfrMgr.h:120
IARM_Bus_Call
IARM_Result_t IARM_Bus_Call(const char *ownerName, const char *methodName, void *arg, size_t argLen)
This API is used to Invoke RPC method by its application name and method name.
Definition: iarm_bus.c:57
check_empty
unsigned char check_empty(char *str)
This function is used to get the IP address based on IPv6 or IPv4 is enabled.
Definition: idm_library.c:181
_IARM_Bus_MFRLib_GetSerializedData_Param_t::bufLen
int bufLen
Definition: mfrMgr.h:121
getserialnum
gboolean getserialnum(GString *serial_num)
This function is used to get the serial number of the device from the vendor specific file.
Definition: xcal-device-library.c:227
getipaddress
int getipaddress(const char *ifname, char *ipAddressBuffer, gboolean ipv6Enabled)
This function is used to get the IP address based on IPv6 or IPv4 is enabled.
Definition: idm_library.c:131
IARM_BUS_MFRLIB_API_GetSerializedData
#define IARM_BUS_MFRLIB_API_GetSerializedData
Definition: mfrMgr.h:100
IARM_BUS_MFRLIB_NAME
#define IARM_BUS_MFRLIB_NAME
Definition: mfrMgr.h:98
_IARM_Bus_MFRLib_GetSerializedData_Param_t
Definition: mfrMgr.h:118
_IARM_Bus_MFRLib_GetSerializedData_Param_t::type
mfrSerializedType_t type
Definition: mfrMgr.h:119
TRUE
#define TRUE
Defines for TRUE/FALSE/ENABLE flags.
Definition: wifi_common_hal.h:199