RDK Documentation (Open Sourced RDK Components)
test_vrex_client.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 
21 
22 /**
23 * @defgroup iarmmgrs
24 * @{
25 * @defgroup test
26 * @{
27 **/
28 
29 
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <unistd.h>
33 #include <string.h>
34 #include "libIBus.h"
35 #include "vrexMgr.h"
36 #include <pthread.h>
37 #include "safec_lib.h"
38 #if !defined(MIN)
39 #define MIN(a,b) (((a)<(b))?(a):(b))
40 #endif
41 
42 #define DEFAULT_REMOTE_ID 12
43 #define DEFAULT_PCM_FILENAME "voice-cmd.raw"
44 
45 static unsigned char remoteId = DEFAULT_REMOTE_ID;
46 static char currentCodec[] = "PCM_16_16K";
47 static pthread_mutex_t tMutexLock;
48 
49 static void safe_copy(unsigned char *dst, const char *src, size_t len)
50 {
51  if (len < 0) return;
52 
53  len = MIN(strlen(src), (len - 1));
54 
55  // Because glibc maintainers don't like strlcpy....
56  *((char *) mempcpy (dst, src, len)) = '\0';
57 
58  printf("len=%i, dst=<%s>, src=<%s>\n", len, (char *)dst, src);
59 }
60 
61 void send_file(const char *filename)
62 {
64 
65  FILE *fp = fopen(filename, "r");
66  if (fp == NULL) return;
67 
68  _SPEECH_EVENT *se = &eventData.data.speechEvent;
69  int fragmentSizeRead = 0;
70  int readBytes = sizeof(se->data.fragment.fragment);
71 
72  eventData.remoteId = remoteId;
73 
74  eventData.data.speechEvent.type = IARM_BUS_VREXMGR_SPEECH_FRAGMENT;
75 
76  while (!feof(fp)) {
77  fragmentSizeRead = fread(&se->data.fragment.fragment[0], 1, readBytes, fp);
78  se->data.fragment.length = fragmentSizeRead;
79 
80  printf("<<<<<<< Send Speech Fragment Event len: <%i>>>>>>>>>\n", fragmentSizeRead);
81  IARM_Bus_BroadcastEvent(IARM_BUS_VREXMGR_NAME, (IARM_EventId_t)IARM_BUS_VREXMGR_EVENT_SPEECH,
82  (void *)&eventData,sizeof(eventData));
83  }
84  fclose(fp);
85 }
86 
87 static void _eventHandler(const char *owner, IARM_EventId_t eventId, void *data, size_t len)
88 {
89  /*Handle only VREX Manager Events */
90  if (strcmp(owner, IARM_BUS_VREXMGR_NAME) == 0)
91  {
92  _JSON_EVENT *eEvent;
93 
94  //pthread_mutex_lock(&tMutexLock);
95 
97  unsigned char remoteId = vrexEventData->remoteId;
98 
99 
100  switch(eventId) {
101  case IARM_BUS_VREXMGR_EVENT_ERROR:
102  {
103 // eEvent = (_ERROR_EVENT *)&vrexEventData->data.errorEvent;
104 // printf("\n_eventHandler Error Event remote=%d, responseCode=%ld, returnCode=%ld, message=%s\n", (int)vrexEventData->remoteId, eEvent->responseCode, eEvent->returnCode, eEvent->message);
105  }
106  break;
107  default:
108  printf("\n_eventHandler unknown event type \n");
109  break;
110  }
111 
112  //pthread_mutex_unlock(&tMutexLock);
113  }
114  else {
115  printf("_eventHandler event type not meant for me <%s>...\n", owner);
116  }
117 }
118 
119 int main(int argc, char *argv[])
120 {
121  errno_t rc = -1;
122  IARM_Result_t err;
123  int input;
124  char filename[] = DEFAULT_PCM_FILENAME;
125 
126  //pthread_mutex_init(&tMutexLock, NULL);
127  //pthread_mutex_lock(&tMutexLock);
128 
129  do {
130  printf("Tring to initialize IARM..\n");
131 
132  err = IARM_Bus_Init("vrexMgrTest");
133 
134  if(IARM_RESULT_SUCCESS != err)
135  {
136  printf("Error initialing IARM bus()... error code : %d\n",err);
137  break;
138  }
139 
140  printf("Trying to connect..\n");
141 
142  err = IARM_Bus_Connect();
143 
144  if(IARM_RESULT_SUCCESS != err)
145  {
146  printf("Error connecting to IARM bus()... error code : %d\n",err);
147  break;
148  }
149  IARM_Bus_RegisterEventHandler(IARM_BUS_VREXMGR_NAME, IARM_BUS_VREXMGR_EVENT_ERROR, _eventHandler);
150 
151  } while(0);
152  //pthread_mutex_lock(&tMutexLock);
153 
154  do {
155  printf("Enter command..\n");
156  printf("m - send motion event\n");
157  printf("v - send voice event(s)\n");
158  printf("r - randomize remoteId \n");
159  printf("c - switch codec\n");
160  printf("x - exit..\n");
161 
162  input = getchar();
163 
164  switch(input)
165  {
166  case 'c':
167  {
168  if (currentCodec[0] == 'P')
169  {
170  rc = strcpy_s(currentCodec,sizeof(currentCodec), "ADPCM");
171  if(rc!=EOK)
172  {
173  ERR_CHK(rc);
174  }
175  }
176  else
177  {
178  rc = strcpy_s(currentCodec,sizeof(currentCodec), "PCM_16_16K");
179  if(rc!=EOK)
180  {
181  ERR_CHK(rc);
182  }
183  }
184  printf("Codec is now: %s\n", currentCodec);
185  }
186  break;
187  case 'r':
188  {
189  remoteId = rand() % 16;
190  printf("Remote ID is now: %i\n", (int)remoteId);
191  }
192  break;
193  case 'm':
194  {
196  eventData.remoteId = remoteId;
197 
198  eventData.data.motionEvent.x = 128;
199  eventData.data.motionEvent.y = 128;
200  eventData.data.motionEvent.z = 128;
201 
202  printf("<<<<<<< Send Motion Event is >>>>>>>>\n");
203 
204  IARM_Bus_BroadcastEvent(IARM_BUS_VREXMGR_NAME, (IARM_EventId_t)IARM_BUS_VREXMGR_EVENT_MOTION,(void *)&eventData,sizeof(eventData));
205 
206  }
207  break;
208  case 'v':
209  {
211  eventData.remoteId = remoteId;
212 
213  eventData.data.speechEvent.type = IARM_BUS_VREXMGR_SPEECH_BEGIN;
214  safe_copy(eventData.data.speechEvent.data.begin.mimeType, "\"audio/vnd.wave;codec=1\"", sizeof(eventData.data.speechEvent.data.begin.mimeType));
215  //safe_copy(eventData.data.speechEvent.data.begin.subType, "PCM_16_16K", sizeof(eventData.data.speechEvent.data.begin.subType));
216  safe_copy(eventData.data.speechEvent.data.begin.subType, currentCodec, sizeof(eventData.data.speechEvent.data.begin.subType));
217  safe_copy(eventData.data.speechEvent.data.begin.language, "en", sizeof(eventData.data.speechEvent.data.begin.language));
218  printf("<<<<<<< Send Speech Begin Event >>>>>>>>\n");
219  IARM_Bus_BroadcastEvent(IARM_BUS_VREXMGR_NAME, (IARM_EventId_t)IARM_BUS_VREXMGR_EVENT_SPEECH,(void *)&eventData,sizeof(eventData));
220 
221  send_file(filename);
222 
223  printf("<<<<<<< Send Speech End Event >>>>>>>>\n");
224  eventData.data.speechEvent.type = IARM_BUS_VREXMGR_SPEECH_END;
225  eventData.data.speechEvent.data.end.reason = IARM_BUS_VREXMGR_SPEECH_DONE;
226  //eventData.data.speechEvent.data.end.reason = IARM_BUS_VREXMGR_SPEECH_ABORT;
227  IARM_Bus_BroadcastEvent(IARM_BUS_VREXMGR_NAME, (IARM_EventId_t)IARM_BUS_VREXMGR_EVENT_SPEECH,(void *)&eventData,sizeof(eventData));
228  }
229  break;
230  }
231  getchar();
232  } while(input != 'x');
233 
235  IARM_Bus_Term();
236 }
237 
238 
239 /** @} */
240 /** @} */
IARM_Bus_Term
IARM_Result_t IARM_Bus_Term(void)
This API is used to terminate the IARM-Bus library.
_IARM_BUS_VREXMgr_EventData_t::remoteId
unsigned char remoteId
A unique identifier of the remote that transmitted the motion command.
Definition: vrexMgr.h:181
IARM_BUS_VREXMGR_NAME
#define IARM_BUS_VREXMGR_NAME
Definition: vrexMgr.h:99
IARM_Bus_RegisterEventHandler
IARM_Result_t IARM_Bus_RegisterEventHandler(const char *ownerName, IARM_EventId_t eventId, IARM_EventHandler_t handler)
This API register to listen to event and provide the callback function for event notification....
Definition: iarmMgrMocks.cpp:43
vrexMgr.h
IARM-Bus VREX Manager Public API.
IARM_Bus_Disconnect
IARM_Result_t IARM_Bus_Disconnect(void)
This API disconnect Application from IARM Bus so the application will not receive any IARM event or R...
_SPEECH_EVENT
Definition: vrexMgr.h:150
IARM_Bus_BroadcastEvent
IARM_Result_t IARM_Bus_BroadcastEvent(const char *ownerName, IARM_EventId_t eventId, void *data, size_t len)
This API is used to publish an Asynchronous event to all IARM client registered for this perticular e...
libIBus.h
RDK IARM-Bus API Declarations.
_JSON_EVENT
Definition: vrexMgr.h:146
IARM_Bus_Connect
IARM_Result_t IARM_Bus_Connect(void)
This API is used to connect application to the IARM bus daemon. After connected, the application can ...
Definition: iarmMgrMocks.cpp:33
_IARM_BUS_VREXMgr_EventData_t
Definition: vrexMgr.h:179
IARM_Bus_Init
IARM_Result_t IARM_Bus_Init(const char *name)
This API is used to initialize the IARM-Bus library.
Definition: iarmMgrMocks.cpp:38