RDK Documentation (Open Sourced RDK Components)
TunerReservation.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 /**
22 * @defgroup trm
23 * @{
24 * @defgroup common
25 * @{
26 **/
27 
28 
29 #include <string>
30 #include <iostream>
31 
32 #include "trm/TRM.h"
33 #include "trm/TunerReservation.h"
34 #include "ReservationCustomAttributes.h"
35 
36 #include "Util.h"
37 
38 using namespace TRM;
39 
40 TunerReservationBase::TunerReservationBase(
41  const std::string &device,
42  const std::string &serviceLocator,
43  const uint64_t startTime,
44  const uint64_t duration,
45  const Activity &activity,
46  const std::string &reservationToken,
47  ReservationCustomAttributes *customAttributes)
48 : reservationToken(reservationToken),
49  device(device),
50  serviceLocator(serviceLocator),
51  startTime(startTime),
52  duration(duration),
53  activity(activity),
54  customAttributes(customAttributes)
55 {
56  state = IDLE;
57 };
58 
59 TunerReservationBase::~TunerReservationBase(void)
60 {
61 }
62 
63 /**
64  * @brief This function is used to set a unique token generated when a reservation
65  * is created. After a reservation is created, this token is used in all messages to uniquely
66  * identify an existing reservation within TRM.
67  *
68  * @param[in] token A unique token that the requesting device can use to make the remote tuning request.
69  * @return None.
70  */
71 void TunerReservationBase::setReservationToken(const std::string &token)
72 {
73  reservationToken = token;
74 }
75 
76 
77 /**
78  * @brief This function is used to return the unique token generated when a reservation
79  * is created.
80  *
81  * @return Returns a token that the requesting device can use to make the remote tuning request.
82  */
83 const std::string & TunerReservationBase::getReservationToken(void) const
84 {
85  return reservationToken;
86 }
87 
88 
89 /**
90  * @brief This function is used to return the locator of the service that the tuner is tuned to.
91  *
92  * @return Returns a service locator in string format.
93  */
94 const std::string & TunerReservationBase::getServiceLocator(void) const
95 {
96  return serviceLocator;
97 };
98 
99 
100 /**
101  * @brief This function is used to get the start time of the reservation in milliseconds from the epoch.
102  *
103  * @return Returns a time in terms of milliseconds.
104  */
106 {
107  return startTime;
108 }
109 
110 
111 /**
112  * @brief This function is used to get the reservation period measured from the start in milliseconds.
113  *
114  * @return Returns a time in terms of millisecond.
115  */
117 {
118  return duration;
119 }
120 
121 
122 /**
123  * @brief This function is used to get the remote device id requesting for tuner reservation.
124  *
125  * @return Returns the device Id in string format.
126  */
127 const std::string &TunerReservationBase::getDevice(void) const
128 {
129  return device;
130 };
131 
132 
133 /**
134  * @brief This function is used to return the granted @b activity. Granted @b activity may or may not
135  * be the same as the requested. In the later case the owner of the reservation will need to comply with
136  * the returned activity, or initiate conflict resolution.
137  * @n For example, a client requests for @b Live activity when EAS is in progress, the returned
138  * reservation will have the EAS activity.
139  *
140  * @return Returns a pointer to an object of class Activity.
141  */
143 {
144  return activity;
145 }
146 
147 
148 /**
149  * @brief This function is used to set the locator of the service that the tuner will tune to.
150  * The service locator is a URL containing tune parameters of the remote device.
151  *
152  * @param[in] serviceLocator Locator of the service that the tuner will tune to.
153  * @return None.
154  */
155 void TunerReservationBase::setServiceLocator(const std::string &_serviceLocator)
156 {
157  serviceLocator = _serviceLocator;
158 };
159 
160 
161 /**
162  * @brief This function is used to set the start time of the reservation in milliseconds from the epoch.
163  * If the @b startTime not present a requested message, this is set to when the reservation is granted or renewed.
164  * @note The @b startTime is always included in a reservation response message.
165  *
166  * @param[in] startTime Time in milliseconds from the epoch.
167  * @return None.
168  */
169 void TunerReservationBase::setStartTime(const uint64_t &startTime)
170 {
171  this->startTime = startTime;
172 }
173 
174 
175 /**
176  * @brief This function is used to set the reservation duration measured from the start in milliseconds.
177  * If the duration field not present in a request message, the token is valid for a default duration.
178  * @note The @b duration is always included in a reservation response message.
179  *
180  * @param[in] duration A time period measured from the start in milliseconds.
181  * @return None.
182  */
183 void TunerReservationBase::setDuration(const uint64_t &duration)
184 {
185  this->duration = duration;
186 }
187 
188 
189 /**
190  * @brief This function is used to return remaining tuner reservation time.
191  *
192  * @return Returns a positive value means the reservation time is not yet expired.
193  * Returns 0 when tuner reservation time has expired.
194  */
196 {
197  /*
198  * Expiration = (Start - Current) + Duration;
199  */
200  int64_t currentEpochMillis = GetCurrentEpoch();
201 
202  int64_t expirationTime = static_cast<int64_t>((((int64_t)startTime) - ((int64_t)currentEpochMillis)) + ((int64_t)duration));
203  if (expirationTime < 0) expirationTime = 0;
204 
205  return expirationTime;
206 }
207 
208 /**
209  * @brief This function is used to add the details describing the tuner reservation activity in to the activity list.
210  *
211  * The field specified here are required for the associated activity. The requestor is
212  * allowed to insert unspecified fields in the details. These unspecified fields are ignored by TRM,
213  * and echoed back in response message that have the activity field.
214  * @n The defined fields are:
215  * - recordingId : required when requesting Record activity for a tuner.
216  * - hot : flag (true or false) indicating of the recording is scheduled or hot.
217  *
218  * @param[in] key it could be "recordingId" or "hot" when requesting Record activity for a tuner.
219  * @param[in] value value associated for key.
220  * @return None.
221  */
222 void TunerReservationBase::addDetail(const std::string &key, const std::string &value)
223 {
224  activity.addDetail(key, value);
225 }
226 
227 
228 /**
229  * @brief This function is used to return the custom attributes assigned by the application. These attributes are
230  * associated with the reservation token for the lifetime of the reservation.
231  *
232  * @return Returns a pointer to an object of class ReservationCustomAttributes.
233  */
235 {
236  return customAttributes;
237 }
238 
239 
240 /**
241  * @brief This function is used to set the attributes assigned by the application. These attributes are
242  * associated with the reservation token for the lifetime of the reservation.
243  *
244  * The @b customAttributes contains JSON entities defined by the application. It is sent by the application
245  * when it requests for a tuner reservation. After the reservation is granted, and as long as the content
246  * of the reservation is not modified, the @b customAttributes will be present in any messages, including
247  * the asynchronous notifications, that contains the corresponding TunerReservation.
248  * @n If a renewd TunerReservation also contains @b customAttributes, the new attributes object will take
249  * the place.
250  * @see notifyTunerReservationUpdate()
251  *
252  * @param[in] customAttributes A set of attributes assigned by the application.
253  * @return None.
254  */
256 {
257  this->customAttributes = customAttributes;
258 }
259 
260 
261 /**
262  * @brief This function is used to print the following attributes of tunerReservation token.
263  * - @b reservationToken = A unique token generated when a reservation is created.
264  * - @b device = The remote device requesting the reservation.
265  * - @b serviceLocator = The service locator that the tuner will tune to.
266  * - @b duration = The reservation period measured from the start in milliseconds.
267  *
268  * @return None.
269  */
271 {
272  std::cout << "[OBJ][" << klassName() << "] reservationToken = "<< reservationToken << std::endl;
273  std::cout << "[OBJ][" << klassName() << "] device = " << device << std::endl;
274  std::cout << "[OBJ][" << klassName() << "] serviceLocator = " << serviceLocator << std::endl;
275  std::cout << "[OBJ][" << klassName() << "] startTime = " << startTime << std::endl;
276  std::cout << "[OBJ][" << klassName() << "] duration = " << duration << std::endl;
277  activity.print();
278 }
279 
280 
281 /** @} */
282 /** @} */
TRM::TunerReservationBase::setServiceLocator
void setServiceLocator(const std::string &_serviceLocator)
This function is used to set the locator of the service that the tuner will tune to....
Definition: TunerReservation.cpp:155
TRM::TunerReservationBase::getStartTime
uint64_t getStartTime(void) const
This function is used to get the start time of the reservation in milliseconds from the epoch.
Definition: TunerReservation.cpp:105
TRM::TunerReservationBase::print
virtual void print(void) const
This function is used to print the following attributes of tunerReservation token.
Definition: TunerReservation.cpp:270
ReservationCustomAttributes
Definition: ReservationCustomAttributes.h:38
TRM::Activity
The Activity class represents the request or granted usage of a tuner. The activity field in the clas...
Definition: Activity.h:92
TRM::TunerReservationBase::setStartTime
void setStartTime(const uint64_t &_startTime)
This function is used to set the start time of the reservation in milliseconds from the epoch....
Definition: TunerReservation.cpp:169
TRM::TunerReservationBase::setReservationToken
void setReservationToken(const std::string &token)
This function is used to set a unique token generated when a reservation is created....
Definition: TunerReservation.cpp:71
TRM::TunerReservationBase::getDevice
const std::string & getDevice(void) const
This function is used to get the remote device id requesting for tuner reservation.
Definition: TunerReservation.cpp:127
TRM::TunerReservationBase::getCustomAttributes
const ReservationCustomAttributes * getCustomAttributes(void) const
This function is used to return the custom attributes assigned by the application....
Definition: TunerReservation.cpp:234
TRM::TunerReservationBase::setCustomAttributes
void setCustomAttributes(ReservationCustomAttributes *)
This function is used to set the attributes assigned by the application. These attributes are associa...
Definition: TunerReservation.cpp:255
TRM::TunerReservationBase::getExpirationTime
uint64_t getExpirationTime(void) const
This function is used to return remaining tuner reservation time.
Definition: TunerReservation.cpp:195
TRM::Activity::print
void print(void) const
Print the list of recording(s) details which are being scheduled. The defined fields are,...
Definition: Activity.cpp:166
TRM::TunerReservationBase::getActivity
const Activity & getActivity(void) const
This function is used to return the granted activity. Granted activity may or may not be the same as ...
Definition: TunerReservation.cpp:142
TRM::TunerReservationBase::getServiceLocator
const std::string & getServiceLocator(void) const
This function is used to return the locator of the service that the tuner is tuned to.
Definition: TunerReservation.cpp:94
TRM::TunerReservationBase::getDuration
uint64_t getDuration(void) const
This function is used to get the reservation period measured from the start in milliseconds.
Definition: TunerReservation.cpp:116
TRM::TunerReservationBase::setDuration
void setDuration(const uint64_t &_duratioin)
This function is used to set the reservation duration measured from the start in milliseconds....
Definition: TunerReservation.cpp:183
TRM::TunerReservationBase::getReservationToken
const std::string & getReservationToken(void) const
This function is used to return the unique token generated when a reservation is created.
Definition: TunerReservation.cpp:83
TRM::Activity::addDetail
void addDetail(const std::string &key, const std::string &value)
This API is used to add the details describing the activity in to a list.
Definition: Activity.cpp:147
TRM::TunerReservationBase::addDetail
void addDetail(const std::string &key, const std::string &value)
This function is used to add the details describing the tuner reservation activity in to the activity...
Definition: TunerReservation.cpp:222