RDK Documentation (Open Sourced RDK Components)
Device_Time.cpp
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 /**
21  * @file Device_Time.c
22  *
23  * @brief DeviceTime API Implementation.
24  *
25  * This is the implementation of the DeviceTime API.
26  *
27  * @par Document
28  * TBD Relevant design or API documentation.
29  *
30  */
31 
32 
33 /*****************************************************************************
34  * STANDARD INCLUDE FILES
35  *****************************************************************************/
36 
37 
38 /**
39 * @defgroup tr69hostif
40 * @{
41 * @defgroup hostif
42 * @{
43 **/
44 
45 
46 #include <time.h>
47 #include <sys/time.h>
48 #include <string.h>
49 #include "Device_Time.h"
50 #include "safec_lib.h"
51 
52 #define TIME_ZONE_LENGTH 8
53 
54 GHashTable* hostIf_Time::ifHash = NULL;
55 GMutex* hostIf_Time::m_mutex = NULL;
56 GHashTable* hostIf_Time::m_notifyHash = NULL;
57 XBSStore* hostIf_Time::m_bsStore;
58 
59 /****************************************************************************************************************************************************/
60 // Device.DeviceInfo Profile. Getters:
61 /****************************************************************************************************************************************************/
62 hostIf_Time::hostIf_Time(int dev_id):
63  dev_id(dev_id),
64  bCalledLocalTimeZone(false),
65  bCalledCurrentLocalTime(false)
66 {
67  backupLocalTimeZone[0]='\0';
68  backupCurrentLocalTime[0]='\0';
69  m_bsStore = XBSStore::getInstance();
70 }
71 
72 hostIf_Time* hostIf_Time::getInstance(int dev_id)
73 {
74  hostIf_Time* pRet = NULL;
75 
76  if(ifHash)
77  {
78  pRet = (hostIf_Time *)g_hash_table_lookup(ifHash,(gpointer) dev_id);
79  }
80  else
81  {
82  ifHash = g_hash_table_new(NULL,NULL);
83  }
84 
85  if(!pRet)
86  {
87  try {
88  pRet = new hostIf_Time(dev_id);
89  } catch(int e)
90  {
91  RDK_LOG(RDK_LOG_WARN,LOG_TR69HOSTIF,"Caught exception, not able create MoCA Interface instance..\n");
92  }
93  g_hash_table_insert(ifHash, (gpointer)dev_id, pRet);
94  }
95  return pRet;
96 }
97 
98 GList* hostIf_Time::getAllInstances()
99 {
100  if(ifHash)
101  return g_hash_table_get_keys(ifHash);
102  return NULL;
103 }
104 
105 void hostIf_Time::closeInstance(hostIf_Time *pDev)
106 {
107  if(pDev)
108  {
109  g_hash_table_remove(ifHash, (gconstpointer)pDev->dev_id);
110  delete pDev;
111  }
112 }
113 
114 void hostIf_Time::closeAllInstances()
115 {
116  if(ifHash)
117  {
118  GList* tmp_list = g_hash_table_get_values (ifHash);
119 
120  while(tmp_list)
121  {
122  hostIf_Time* pDev = (hostIf_Time *)tmp_list->data;
123  tmp_list = tmp_list->next;
124  closeInstance(pDev);
125  }
126  }
127 }
128 
129 void hostIf_Time::getLock()
130 {
131  if(!m_mutex)
132  {
133  m_mutex = g_mutex_new();
134  }
135  g_mutex_lock(m_mutex);
136 }
137 
138 void hostIf_Time::releaseLock()
139 {
140  g_mutex_unlock(m_mutex);
141 }
142 
143 GHashTable* hostIf_Time::getNotifyHash()
144 {
145  if(m_notifyHash)
146  {
147  return m_notifyHash;
148  }
149  else
150  {
151  return m_notifyHash = g_hash_table_new(g_str_hash, g_str_equal);
152  }
153 }
154 
155 hostIf_Time::~hostIf_Time()
156 {
157  if(m_notifyHash)
158  {
159  g_hash_table_destroy(m_notifyHash);
160  }
161 }
162 
164 {
165  struct timeval time_now;
166  struct tm *newtime = NULL;
167 
168  char tmp[_BUF_LEN_64];
169 
170  gettimeofday(&time_now, NULL);
171 
172  newtime = localtime((const time_t*)&(time_now.tv_sec));
173 
174  if(0 == strftime(tmp,sizeof(tmp),"%Z", newtime))
175  {
176  return NOK;
177  }
178 
179  if(bCalledLocalTimeZone && pChanged && strncmp(tmp, backupLocalTimeZone,_BUF_LEN_64))
180  {
181  *pChanged = true;
182  }
183 
184  bCalledLocalTimeZone = true;
185  strncpy(stMsgData->paramValue,tmp,_BUF_LEN_64-1);
186  strncpy(backupLocalTimeZone,tmp,_BUF_LEN_64-1);
187 
188  stMsgData->paramtype = hostIf_StringType;
189  stMsgData->paramLen = strlen(stMsgData->paramValue);
190  return OK;
191 }
192 int hostIf_Time::get_Device_Time_Enable(HOSTIF_MsgData_t *, bool *pChanged )
193 {
194  return NOK;
195 }
197 {
198  return NOK;
199 }
201 {
202  return NOK;
203 }
205 {
206  return NOK;
207 }
209 {
210  return NOK;
211 }
213 {
214  return NOK;
215 }
217 {
218  return NOK;
219 }
220 
222 {
223  time_t rawtime;
224  struct tm * timeinfo;
225  errno_t rc = -1;
226  char buffer [_BUF_LEN_64] = {'\0'};
227  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s] Entering..\n", __FILE__, __FUNCTION__);
228  char timeZoneTmp[7];
229  memset(timeZoneTmp, 0, sizeof(timeZoneTmp));
230 
231  time (&rawtime);
232  timeinfo = localtime (&rawtime);
233 
234  strftime(buffer,_BUF_LEN_64-1, "%Y-%m-%dT%H:%M:%S", timeinfo);
235  strftime(timeZoneTmp, sizeof(timeZoneTmp), "%z", timeinfo);
236  sprintf(buffer + strlen(buffer), ".%0.6d%s", timeinfo->tm_sec, timeZoneTmp);
237 
238  if(bCalledCurrentLocalTime && pChanged && strncmp(buffer, backupCurrentLocalTime, _BUF_LEN_64 ))
239  {
240  *pChanged = true;
241  }
242 
243  bCalledCurrentLocalTime = true;
244  rc=strcpy_s(stMsgData->paramValue,sizeof(stMsgData->paramValue), buffer);
245  if(rc!=EOK)
246  {
247  ERR_CHK(rc);
248  }
249  rc=strcpy_s(backupCurrentLocalTime,sizeof(backupCurrentLocalTime),buffer);
250  if(rc!=EOK)
251  {
252  ERR_CHK(rc);
253  }
254 
255  RDK_LOG(RDK_LOG_DEBUG,LOG_TR69HOSTIF,"[%s:%s] buffer : %s stMsgData->paramValue: %s\n", __FILE__, __FUNCTION__, buffer, stMsgData->paramValue);
256 
257  stMsgData->paramtype = hostIf_StringType;
258  stMsgData->paramLen = strlen(stMsgData->paramValue);
259  RDK_LOG(RDK_LOG_TRACE1,LOG_TR69HOSTIF,"[%s:%s] Exiting..\n", __FILE__, __FUNCTION__);
260  return OK;
261 }
262 
264 {
265  return m_bsStore->getValue(stMsgData);
266 }
267 
269 {
270  return m_bsStore->overrideValue(stMsgData);
271 }
272 
274 {
275  return NOK;
276 }
278 {
279  return NOK;
280 }
282 {
283  return NOK;
284 }
286 {
287  return NOK;
288 }
290 {
291  return NOK;
292 }
294 {
295  return NOK;
296 }
298 {
299  return NOK;
300 }
301 
302 
303 /** @} */
304 /** @} */
hostIf_Time::set_Device_Time_NTPServer5
int set_Device_Time_NTPServer5(HOSTIF_MsgData_t *)
Set the fifth NTP timeserver.
Definition: Device_Time.cpp:293
hostIf_Time::get_Device_Time_Status
int get_Device_Time_Status(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the status of time support on the CPE.
Definition: Device_Time.cpp:196
hostIf_Time::get_Device_Time_NTPServer5
int get_Device_Time_NTPServer5(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the fifth NTP timeserver.
Definition: Device_Time.cpp:216
XBSStore
Definition: XrdkCentralComBSStore.h:33
hostIf_Time::get_Device_Time_LocalTimeZone
int get_Device_Time_LocalTimeZone(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the local time zone definition.
Definition: Device_Time.cpp:163
_HostIf_MsgData_t
Definition: hostIf_tr69ReqHandler.h:170
hostIf_Time::set_Device_Time_NTPServer1
int set_Device_Time_NTPServer1(HOSTIF_MsgData_t *)
Set the first NTP timeserver.
Definition: Device_Time.cpp:277
hostIf_Time::set_Device_Time_NTPServer3
int set_Device_Time_NTPServer3(HOSTIF_MsgData_t *)
Set the third NTP timeserver.
Definition: Device_Time.cpp:285
hostIf_Time::get_Device_Time_NTPServer4
int get_Device_Time_NTPServer4(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the fourth NTP timeserver.
Definition: Device_Time.cpp:212
hostIf_Time::get_xRDKCentralComBootstrap
int get_xRDKCentralComBootstrap(HOSTIF_MsgData_t *)
Get the bootstrap parameters.
Definition: Device_Time.cpp:263
Device_Time.h
TR-069 Device.Time object Public API.
hostIf_Time::set_Device_Time_NTPServer2
int set_Device_Time_NTPServer2(HOSTIF_MsgData_t *)
Set the second NTP timeserver.
Definition: Device_Time.cpp:281
_HostIf_MsgData_t::paramtype
HostIf_ParamType_t paramtype
Definition: hostIf_tr69ReqHandler.h:177
RDK_LOG
#define RDK_LOG
Definition: rdk_debug.h:258
hostIf_Time::get_Device_Time_NTPServer1
int get_Device_Time_NTPServer1(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the first NTP timeserver.
Definition: Device_Time.cpp:200
hostIf_Time::get_Device_Time_NTPServer2
int get_Device_Time_NTPServer2(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the second NTP timeserver.
Definition: Device_Time.cpp:204
hostIf_Time::set_Device_Time_Enable
int set_Device_Time_Enable(HOSTIF_MsgData_t *)
Set the status of the time client.
Definition: Device_Time.cpp:273
hostIf_Time::get_Device_Time_CurrentLocalTime
int get_Device_Time_CurrentLocalTime(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the current dates & time.
Definition: Device_Time.cpp:221
_HostIf_MsgData_t::paramValue
char paramValue[(4 *1024)]
Definition: hostIf_tr69ReqHandler.h:172
hostIf_Time::get_Device_Time_NTPServer3
int get_Device_Time_NTPServer3(HOSTIF_MsgData_t *, bool *pChanged=NULL)
Get the third NTP timeserver.
Definition: Device_Time.cpp:208
hostIf_Time
Get the status of the time client.
Definition: Device_Time.h:134
hostIf_Time::set_Device_Time_LocalTimeZone
int set_Device_Time_LocalTimeZone(HOSTIF_MsgData_t *)
Set the local time zone definition.
Definition: Device_Time.cpp:297
hostIf_Time::set_Device_Time_NTPServer4
int set_Device_Time_NTPServer4(HOSTIF_MsgData_t *)
Set the fourth NTP timeserver.
Definition: Device_Time.cpp:289
_HostIf_MsgData_t::paramLen
short paramLen
Definition: hostIf_tr69ReqHandler.h:175
hostIf_Time::set_xRDKCentralComBootstrap
int set_xRDKCentralComBootstrap(HOSTIF_MsgData_t *)
Set the bootstrap parameters.
Definition: Device_Time.cpp:268