RDK Documentation (Open Sourced RDK Components)
mbwsr_rpc_cli.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 2021 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 "mediabrowser.h"
21 #include <rbus.h>
22 #include <safec_lib.h>
23 #include <stdlib.h>
24 #define RBUS_XUPNP_SERVICE_CONSUMER "rbus.xupnp.dlna.client"
25 
26 #define BUFFER_SIZE 20
27 char keyBuffer[BUFFER_SIZE] = {0};
28 
29 rbusHandle_t handle;
30 int rbus_response = RBUS_ERROR_SUCCESS;
31 
32 bool InitializeRPC()
33 {
34  rbus_response = rbus_open(&handle, RBUS_XUPNP_SERVICE_CONSUMER);
35  return (RBUS_ERROR_SUCCESS == rbus_response)?true:false;
36 }
37 
38 static bool getMediaServer(rbusProperty_t outProps, char ** friendly_name, char **udn)
39 {
40 
41  bool status = false;
42 
43  int result = rbusProperty_Count(outProps);
44  if (result % 2 != 0)
45  {
46  fprintf(stderr, "[%s] Expected name/udn pair, but found odd property entries. = %d \n", __FUNCTION__, result);
47  return status;
48  }
49 
50  rbusValue_t value = rbusProperty_GetValue(outProps);
51  if (value)
52  *friendly_name = rbusValue_ToString(value, NULL, 0); // This is a copy
53  outProps = rbusProperty_GetNext(outProps);
54  value = rbusProperty_GetValue(outProps);
55  if (value)
56  {
57  *udn = rbusValue_ToString(value, NULL, 0); // This is a copy
58  status = true;
59  }
60  return status;
61 }
62 bool getDiscoveredMediaServers(int *size)
63 {
64  rbusObject_t inParams;
65  rbusObject_t outParams;
66  rbusValue_t value;
67  bool status = false;
68 
69  rbusObject_Init(&inParams, NULL);
70  rbus_response = rbusMethod_Invoke(handle, METHOD_DISCOVER_MEDIA_DEVICES_SIZE, inParams, &outParams);
71  rbusObject_Release(inParams);
72  if (RBUS_ERROR_SUCCESS == rbus_response)
73  {
74  rbusProperty_t outProps = rbusObject_GetProperties(outParams);
75  int payload = 0;
76  int count = 0;
77  fprintf(stderr, "[%s] Successfully invoked METHOD_DISCOVER_MEDIA_DEVICES_SIZE \n", __FUNCTION__ );
78  // First property is the payload size
79  if (outProps)
80  {
81  value = rbusProperty_GetValue(outProps);
82  if (value && rbusValue_GetType(value) == RBUS_INT32)
83  {
84  payload = rbusValue_GetInt32(value);
85  }
86  else
87  {
88  fprintf(stderr, "[%s] Expected payload but got type %d \n", __FUNCTION__, rbusValue_GetType(value));
89  return false;
90  }
91  fprintf(stderr, "[%s] payload size %d \n", __FUNCTION__, payload);
92  *size = payload;
93  status = true;
94  }
95  else
96  {
97  fprintf(stderr, "[%s] Failed to retrieve properties \n", __FUNCTION__ );
98  }
99  rbusObject_Release(outParams);
100  }
101  return status;
102 }
103 bool getDiscoveredMediaServerAt(int index, char **friendlyName, char ** udn, int *err)
104 {
105  rbusObject_t inParams;
106  rbusObject_t outParams;
107  rbusValue_t value;
108  bool status = false;
109 
110  *err = 0;
111 
112  rbusObject_Init(&inParams, NULL);
113  rbusValue_Init(&value);
114  rbusValue_SetInt32(value, index);
115  rbusObject_SetValue(inParams, "pos", value);
116  rbusValue_Release(value);
117 
118  rbus_response = rbusMethod_Invoke(handle, METHOD_GET_MEDIA_DEVICE_AT, inParams, &outParams);
119  rbusObject_Release(inParams);
120 
121  if (RBUS_ERROR_SUCCESS == rbus_response)
122  {
123  rbusProperty_t outProps = rbusObject_GetProperties(outParams);
124  int payload = 0;
125  int count = 0;
126 
127  if (outProps)
128  {
129  status = getMediaServer(outProps, friendlyName,udn);
130  if (!status)
131  {
132  fprintf(stderr, "[%s] Failed to retrieve media details %d \n", __FUNCTION__, count);
133  *err = 1;
134  }
135  }
136  else
137  {
138  *err = 1;
139  }
140  status = true; // Indicate RPC communication worked
141  rbusObject_Release(outParams);
142  }
143  else
144  {
145  fprintf(stderr, "[%s] Critical : Failed to invoke rpc communication %d \n", __FUNCTION__, rbus_response);
146  }
147  return status;
148 }
149 bool browseContentOnServer(const char * server_udn, const char * path_id, int start_index,
150  int max_entries, int * total_count, char ** content_list, int *err)
151 {
152  rbusObject_t inParams;
153  rbusObject_t outParams;
154  rbusValue_t value;
155  bool status = false;
156 
157  *err = 0;
158 
159  fprintf(stderr, "[%s] Received request for browsing udn = [%s], path=[%s] \n", __FUNCTION__, server_udn, path_id);
160  rbusObject_Init(&inParams, NULL);
161 
162  rbusValue_Init(&value);
163  rbusValue_SetString(value, server_udn);
164  rbusObject_SetValue(inParams, "udn", value);
165  rbusValue_Release(value);
166 
167  rbusValue_Init(&value);
168  rbusValue_SetString(value, path_id);
169  rbusObject_SetValue(inParams, "path", value);
170  rbusValue_Release(value);
171 
172  rbusValue_Init(&value);
173  rbusValue_SetInt32(value, start_index);
174  rbusObject_SetValue(inParams, "start_index", value);
175  rbusValue_Release(value);
176 
177  rbusValue_Init(&value);
178  rbusValue_SetInt32(value, max_entries);
179  rbusObject_SetValue(inParams, "max_results", value);
180  rbusValue_Release(value);
181  rbus_response = rbusMethod_Invoke(handle, METHOD_BROWSE_CONTENT_ON_MEDIA_SERVER, inParams, &outParams);
182  rbusObject_Release(inParams);
183 
184  *content_list = NULL;
185  *total_count = -1;
186 
187  if (RBUS_ERROR_SUCCESS == rbus_response)
188  {
189  //fprintf(stderr, "[%s] Received response from Xupnp \n", __FUNCTION__);
190  rbusProperty_t outProps = rbusObject_GetProperties(outParams);
191  if(outProps)
192  {
193  value = rbusProperty_GetValue(outProps);
194 
195  if (value)
196  *content_list = rbusValue_ToString(value, NULL, 0); // This is a copy
197 
198  outProps = rbusProperty_GetNext(outProps);
199  }
200  else
201  *err = 1;
202 
203  if(outProps)
204  {
205  value = rbusProperty_GetValue(outProps);
206  if (value)
207  *total_count = rbusValue_GetInt32(value); // This is a copy
208  }
209  else
210  *err = 1;
211  status = true; //RPC communication is successfull.
212  rbusObject_Release(outParams);
213  }
214  else
215  {
216  fprintf(stderr, "[%s] Received error from Xupnp : %d \n", __FUNCTION__, rbus_response);
217  status = false;
218  }
219  fprintf(stderr, "[%s] returning %d \n" , __FUNCTION__, status);
220  return status;
221 }
222 void cleanup()
223 {
224 
225 }