31 #include <arpa/inet.h>
32 #include <sys/socket.h>
42 #include <uuid/uuid.h>
48 #include <sys/syscall.h>
60 #include "trm/MessageProcessor.h"
61 #include "trm/Activity.h"
62 #include "trm/JsonEncoder.h"
63 #include "trm/JsonDecoder.h"
65 #include "trm/Klass.h"
66 #include "trm/TunerReservation.h"
68 #include "safec_lib.h"
75 #define MAX_PAYLOAD_LEN 4096
76 static int trm_diag_fd = -1;
77 static const char* ip =
"127.0.0.1";
78 static int port = 9987;
79 static int is_connected = 0;
80 static const uint32_t kTRMMgrClientId = 0XFFFFFF04;
81 static bool TrmResponseRcvd =
false;
82 static uint32_t numofTRMErrors = 0;
86 static int connect_to_trm();
87 static void processBuffer(
const char* ,
int );
88 static void * ProcessTRMMessage (
void* arg);
89 static bool url_request_post(
const char *payload,
int payload_length,
unsigned int clientId);
90 static char responseStr[MAX_PAYLOAD_LEN];
91 static bool waitForTRMResponse();
92 static bool getAllTunerReservationsResponse();
98 NOTIFICATION = 0x1400,
105 std::list<std::string> conDeviceList;
107 std::list<NotifyClientConnectionEvent> helperConEventLists;
124 static pthread_mutex_t Helper_mutex;
130 pthread_mutex_lock(&Helper_mutex);
133 pthread_mutex_unlock(&Helper_mutex);
136 #define HelperLock() HelperLock_ t()
138 bool TRMMgrHelperImpl::inited =
false;
140 TRMMgrHelperImpl::TRMMgrHelperImpl()
142 DIAG_TRACE((
" %s():%d \r\n" , __FUNCTION__, __LINE__));
145 TRMMgrHelperImpl::~TRMMgrHelperImpl()
147 DIAG_TRACE((
"%s():%d \r\n" , __FUNCTION__, __LINE__));
157 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
159 if (
false == inited )
166 pthread_mutex_init(&Helper_mutex, NULL);
169 pthread_t trm_process_message_thread;
170 pthread_create(&trm_process_message_thread, NULL,ProcessTRMMessage, (
void *)trm_diag_fd);
174 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
185 return TRMMgrHelperInstance;
197 std::vector<uint8_t> out;
201 uuid_generate(value);
202 uuid_unparse(value, guid);
205 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
207 if (NULL == responseMsg)
209 DIAG_WARN((
"responseMsg is NULL \r\n"));
214 JsonEncode(msg, out);
217 int len = strlen((
const char*)&out[0]);
219 errno_t safec_rc = -1;
223 TrmResponseRcvd =
false;
224 safec_rc = memset_s(responseStr,
sizeof(responseStr), 0,
sizeof(responseStr));
226 ret = url_request_post( (
char *) &out[0], len, kTRMMgrClientId);
228 }
while ((ret ==
false) && (retry_count >0));
233 ret = waitForTRMResponse();
237 safec_rc = strcpy_s(responseMsg,MAX_PAYLOAD_LEN, responseStr);
238 if(safec_rc != EOK) {
243 *length = strlen(responseStr);
245 DIAG_DEBUG((
"Message Length of getVersion response = %d ...\r\n",*length));
246 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
261 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
264 errno_t safec_rc = -1;
266 if (NULL == responseMsg)
268 DIAG_WARN((
"responseMsg is NULL ...\r\n"));
273 ret = getAllTunerReservationsResponse();
278 safec_rc = strcpy_s(responseMsg,MAX_PAYLOAD_LEN, responseStr);
279 if(safec_rc != EOK) {
283 *length = strlen(responseStr);
284 DIAG_DEBUG((
"Message Length of getTunerReservationsList = %d ...\r\n",*length));
289 DIAG_WARN((
"getAllTunerReservationsResponse Failed ...\r\n"));
292 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
306 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
310 if (NULL == responseMsg)
312 DIAG_WARN((
"responseMsg is NULL ...\r\n"));
317 ret = getAllTunerReservationsResponse();
323 std::vector<uint8_t> out;
326 uuid_generate(value);
327 uuid_unparse(value, guid);
334 JsonEncode(msg, out);
338 std::copy(out.begin(), out.end(),responseMsg);
339 responseMsg[out.size()] =
'\0';
340 *length = out.size();
342 DIAG_DEBUG((
"Message Length of GetAllConnectedDeviceIdsResponse = %d ...\r\n",*length));
347 DIAG_WARN((
"getAllTunerReservationsResponse Failed --->\r\n"));
351 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
361 static bool getAllTunerReservationsResponse()
364 std::vector<uint8_t> out;
367 uuid_generate(value);
368 uuid_unparse(value, guid);
370 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
376 JsonEncode(msg, out);
378 int len = strlen((
const char*)&out[0]);
380 errno_t safec_rc = -1;
385 TrmResponseRcvd =
false;
386 safec_rc = memset_s(responseStr,
sizeof(responseStr), 0,
sizeof(responseStr));
389 ret = url_request_post( (
char *) &out[0], len, kTRMMgrClientId);
391 }
while ((ret ==
false) && (retry_count >0));
396 ret = waitForTRMResponse();
399 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
404 bool TRMMgrHelperImpl::getNumofTRMErrors(uint32_t *errCount)
407 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
411 if (NULL == errCount)
413 DIAG_WARN((
"Param errCount is NULL ...\r\n"));
417 *errCount = numofTRMErrors;
419 DIAG_DEBUG((
"Num of TRM Errors are %d ...\r\n",*errCount));
421 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
433 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
437 if (NULL == responseMsg)
439 DIAG_WARN((
"responseMsg is NULL ...\r\n"));
443 if (0 == helperConEventLists.size())
446 DIAG_WARN((
"No Event to Send ...\r\n"));
453 std::vector<uint8_t> out;
456 uuid_generate(value);
457 uuid_unparse(value, guid);
463 JsonEncode(msg, out);
467 std::copy(out.begin(), out.end(),responseMsg);
468 responseMsg[out.size()] =
'\0';
469 *length = out.size();
471 DIAG_DEBUG((
"Message Length of getTRMConnectionEventList = %d ...\r\n",*length));
472 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
486 int statusCode = msg.getStatus().getStatusCode();
487 if (TRM::ResponseStatus::kOk == statusCode)
489 DIAG_DEBUG((
"(GetVersionResponse) Sucess = %d\r\n",statusCode));
493 DIAG_WARN((
"(GetVersionResponse) Error code = %d\r\n",statusCode));
496 TrmResponseRcvd =
true;
509 int statusCode = msg.getStatus().getStatusCode();
511 if (TRM::ResponseStatus::kOk == statusCode)
514 const std::map<std::string, std::list<TunerReservation> > & allReservations = msg.getAllReservations();
515 std::map<std::string, std::list<TunerReservation> >::const_iterator it;
518 conDeviceList.clear();
519 for (it = allReservations.begin(); it != allReservations.end(); it++)
521 const std::list<TunerReservation> & tunerReservations = it->second;
522 std::list<TunerReservation>::const_iterator it1;
523 for (it1 = tunerReservations.begin(); it1 != tunerReservations.end(); it1++) {
524 if (!((*it1).getDevice().empty()))
526 DIAG_DEBUG((
"Add device %s to List \r\n",(*it1).getDevice().c_str()));
527 conDeviceList.push_back ((*it1).getDevice());
534 DIAG_WARN((
"(GetVersionResponse) Error code = %d\r\n",statusCode));
537 TrmResponseRcvd =
true;
547 DIAG_DEBUG((
"NotifyClientConnectionEvent) Event Name = %s\r\n",msg.getEventName().c_str()));
548 DIAG_DEBUG((
"NotifyClientConnectionEvent) Device IP = %s\r\n", msg.getClientIP().c_str()));
549 DIAG_DEBUG((
"NotifyClientConnectionEvent) Time Stamp = %llu\r\n",msg.getEventTimeStamp()));
552 helperConEventLists.push_back(msg);
557 TrmResponseRcvd =
true;
568 const TRM::ReserveTunerResponse::ConflictCT &conflicts = msg.
getConflicts();
570 if (conflicts.size() != 0)
572 DIAG_WARN((
"Diag Helper : Found %d conflict(s)\r\n",conflicts.size()));
575 TRM::ReserveTunerResponse::ConflictCT::const_iterator it = conflicts.begin();
576 for (it = conflicts.begin(); it != conflicts.end(); it++)
578 DIAG_DEBUG((
"Device:[%s] with Activity:[%s] Locator:[%s] Token:[%s] is in conflict with:\r\n",
579 (*it).getDevice().c_str(),
580 (
const char *)(*it).getActivity().getActivity(),
581 (*it).getServiceLocator().c_str(),
582 (*it).getReservationToken().c_str()));
585 DIAG_DEBUG((
"Device:[%s] with Activity:[%s] Locator:[%s] Token:[%s]\r\n",
592 TrmResponseRcvd =
true;
602 DIAG_DEBUG((
"Received ReserveTunerResponse message \r\n"));
604 if ( status == TRM::ResponseStatus::kOk )
606 DIAG_DEBUG((
"%s - ResponseStatus - kOK \r\n", __FUNCTION__));
607 const TRM::ReserveTunerResponse::ConflictCT &conflicts = msg.
getConflicts();
608 if (conflicts.size() != 0)
610 DIAG_WARN((
"%s- Has %d Conflict(s)\r\n", __FUNCTION__,conflicts.size()));
615 DIAG_DEBUG((
"%s- Has No(%d) Conflict(s)\r\n", __FUNCTION__,conflicts.size()));
625 static void* ProcessTRMMessage (
void* arg)
627 int read_trm_count = 0;
629 const int header_length = 16;
630 unsigned int payload_length = 0;
631 errno_t safec_rc = -1;
633 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
637 if (is_connected == 0)
643 buf = (
char *) malloc(header_length);
646 DIAG_WARN((
"%s:%d : Malloc failed for %d bytes \r\n", __FUNCTION__, __LINE__, header_length));
650 safec_rc = memset_s(buf, header_length, 0, header_length);
655 read_trm_count = read(trm_diag_fd, buf, header_length);
657 DIAG_TRACE((
"\r\n Read Header from TRM %d vs expected %d\r\n", read_trm_count, header_length));
660 __TIMESTAMP();printf(
"=====RESPONSE HEADER===================================================\r\n[");
661 for (idx = 0; idx < (header_length); idx++) {
662 DIAG_TRACE((
"%02x:", buf[idx]));
667 if (read_trm_count == header_length)
669 unsigned int payload_length_offset = 12;
670 payload_length =((((
unsigned char)(buf[payload_length_offset+0])) << 24) |
671 (((
unsigned char)(buf[payload_length_offset+1])) << 16) |
672 (((
unsigned char)(buf[payload_length_offset+2])) << 8 ) |
673 (((
unsigned char)(buf[payload_length_offset+3])) << 0 ));
675 if((payload_length > 0) && (payload_length < MAX_PAYLOAD_LEN))
679 DIAG_DEBUG((
"TRM Response payloads is %d and header %d\r\n", payload_length, header_length));
682 buf = (
char *) malloc(payload_length+1);
683 safec_rc = memset_s(buf, payload_length+1, 0, payload_length+1);
686 read_trm_count = read(trm_diag_fd, buf, payload_length);
687 DIAG_DEBUG((
"Read Payload from TRM %d vs expected %d\r\n", read_trm_count, payload_length));
689 if (read_trm_count != 0)
691 buf[payload_length] =
'\0';
692 processBuffer(buf, read_trm_count);
702 DIAG_WARN((
"%s:%d : read_trm_count is 0 \r\n", __FUNCTION__, __LINE__));
713 DIAG_WARN((
"%s:%d : payload_length = %d Count Mismatch \r\n", __FUNCTION__, __LINE__,payload_length));
718 DIAG_WARN((
"%s:%d : header-read failure read_trm_count %d \r\n", __FUNCTION__, __LINE__, read_trm_count));
727 DIAG_WARN((
"%s - Not connected- Sleep and retry \r\n", __FUNCTION__));
731 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
739 static void processBuffer(
const char* buf,
int len)
741 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
745 DIAG_DEBUG((
"Response %s \r\n", buf));
746 DIAG_DEBUG((
"Response Length %d - %d\r\n", strlen(buf),len));
748 errno_t safec_rc = -1;
749 safec_rc = memset_s(responseStr,
sizeof(responseStr), 0,
sizeof(responseStr));
752 std::vector<uint8_t> response;
754 safec_rc = strcpy_s(responseStr,
sizeof(responseStr), buf);
757 response.insert( response.begin(), buf, buf+len);
761 jdecoder.decode( response);
763 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
772 static int connect_to_trm()
775 int socket_error = 0;
776 struct sockaddr_in trm_address = {0};
780 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
783 if (is_connected == 0)
786 DIAG_WARN((
"Connecting to remote %s-%d \r\n",__FUNCTION__, __LINE__));
787 trm_address.sin_family = AF_INET;
788 trm_address.sin_addr.s_addr = inet_addr(ip);
789 trm_address.sin_port = htons(port);
790 if (trm_diag_fd == -1 )
792 socket_fd = socket(AF_INET, SOCK_STREAM, 0);
796 socket_fd = trm_diag_fd;
801 int retry_count = 10;
802 socket_error = connect(socket_fd, (
struct sockaddr *) &trm_address,
sizeof(
struct sockaddr_in));
803 if (socket_error == ECONNREFUSED && retry_count > 0)
805 DIAG_WARN((
"TRM Server is not started...retry to connect \r\n" , __FUNCTION__, __LINE__));
815 if (socket_error == 0)
817 DIAG_WARN((
"%s:%d : Connected \r\n" , __FUNCTION__, __LINE__));
818 int current_flags = fcntl(socket_fd, F_GETFL, 0);
819 current_flags &= (~O_NONBLOCK);
820 fcntl(socket_fd, F_SETFL, current_flags);
821 trm_diag_fd = socket_fd;
826 DIAG_WARN((
"%s:%d : socket_error %d, closing socket\r\n" , __FUNCTION__, __LINE__, socket_error));
832 DIAG_TRACE((
"%s:%d : Exit with socket error code %d\r\n",__FUNCTION__, __LINE__, socket_error));
841 static bool url_request_post(
const char *payload,
int payload_length,
unsigned int clientId)
845 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
847 if ( is_connected == 0)
852 if (payload_length != 0)
855 static int message_id = 0x1000;
856 const int header_length = 16;
857 unsigned char *buf = NULL;
858 buf = (
unsigned char *) malloc(payload_length + header_length);
866 buf[idx++] = (UNKNOWN & 0xFF000000) >> 24;
867 buf[idx++] = (UNKNOWN & 0x00FF0000) >> 16;
868 buf[idx++] = (UNKNOWN & 0x0000FF00) >> 8;
869 buf[idx++] = (UNKNOWN & 0x000000FF) >> 0;
873 DIAG_WARN((
"CONNECTION CLIENTID: %02x\r\n",clientId));
875 buf[idx++] = (clientId & 0xFF000000) >> 24;
876 buf[idx++] = (clientId & 0x00FF0000) >> 16;
877 buf[idx++] = (clientId & 0x0000FF00) >> 8;
878 buf[idx++] = (clientId & 0x000000FF) >> 0;
880 buf[idx++] = (payload_length & 0xFF000000) >> 24;
881 buf[idx++] = (payload_length & 0x00FF0000) >> 16;
882 buf[idx++] = (payload_length & 0x0000FF00) >> 8;
883 buf[idx++] = (payload_length & 0x000000FF) >> 0;
885 for (
int i =0; i< payload_length; i++)
886 buf[idx+i] = payload[i];
887 DIAG_TRACE((
"====== REQUEST MSG ======\r\n["));
889 for (idx = 0; idx < (header_length); idx++) {
890 DIAG_TRACE((
"%02x", buf[idx]));
892 DIAG_TRACE((
"]\r\n\n"));
894 for (; idx < (payload_length + header_length); idx++) {
895 DIAG_TRACE((
"%c", buf[idx]));
897 DIAG_TRACE((
"\n\n"));
900 int write_trm_count = write(trm_diag_fd, buf, payload_length + header_length);
901 DIAG_TRACE((
"Send to TRM %d vs expected %d\r\n", write_trm_count, payload_length + header_length));
905 if (write_trm_count == 0)
908 DIAG_WARN((
"%s():%d : Error in Sending Data to Socket:write_trm_count is now 0 \r\n", __FUNCTION__, __LINE__));
918 DIAG_WARN((
"%s():%d : Not Connected to TRM Server\r\n", __FUNCTION__, __LINE__));
921 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));
929 static bool waitForTRMResponse()
931 int retry_count = 500;
933 DIAG_TRACE((
"Enter %s():%d \r\n" , __FUNCTION__, __LINE__));
935 while ((
false == TrmResponseRcvd) && (retry_count > 0))
937 DIAG_DEBUG((
"%s():%d ..and loop ..\r\n", __FUNCTION__, __LINE__));
942 if((retry_count == 0))
944 DIAG_WARN((
"Time out.. waitForTRMResponse %s():%d \r\n" , __FUNCTION__, __LINE__));
947 DIAG_TRACE((
"Exit %s():%d \r\n" , __FUNCTION__, __LINE__));