Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 20 additions & 3 deletions source/InterDeviceManager/Idm_TCP_apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ extern char g_sslSeCA[128];
#endif
bool ssl_lib_init = false;
bool TCP_server_started = false;

bool connect_reset = false;
pthread_mutex_t connect_reset_mutex = PTHREAD_MUTEX_INITIALIZER;
typedef int (*callback_recv)( connection_info_t* conn_info, void *payload);

typedef struct tcp_server_threadargs
Expand Down Expand Up @@ -526,8 +527,20 @@ int open_remote_connection(connection_config_t* connectionConf, int (*connection
servaddr.sin_port = htons(connectionConf->port);

CcspTraceInfo(("waiting to connect to the IDM server..\n"));
connect_reset = false;

while (1)
{
pthread_mutex_lock(&connect_reset_mutex);
if (connect_reset == true)
{
CcspTraceInfo(("Connect stopped since discovery is restarted"));
pthread_mutex_unlock(&connect_reset_mutex);
close(client_sockfd);
return -1; //This discovery is omitted due to discovery restart
}
pthread_mutex_unlock(&connect_reset_mutex);

// Wait indefinitely untill other end idm server accepts the connection
if (connect(client_sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0)
{
Expand All @@ -538,6 +551,7 @@ int open_remote_connection(connection_config_t* connectionConf, int (*connection
break;
}
}

//TODO: check for dynamic allocation
connection_info_t conn_info;
conn_info.conn = client_sockfd;
Expand Down Expand Up @@ -846,12 +860,15 @@ int send_remote_message(connection_info_t* conn_info,void *payload)
#ifndef IDM_DEBUG
int val;
if (conn_info->enc.ctx != NULL && conn_info->enc.ssl != NULL) {
if ((val = SSL_write(conn_info->enc.ssl, payload, sizeof(payload_t))) > 0) {
val = SSL_write(conn_info->enc.ssl, payload, sizeof(payload_t));
if (val > 0) {
CcspTraceInfo(("(%s:%d) SSL_write successful connection id %d \n", __FUNCTION__, __LINE__,conn_info->conn));
return 0;
}
else
{
CcspTraceError(("(%s:%d) Data encryption failed (Err: %d)", __FUNCTION__, __LINE__, val));
int ssl_err = SSL_get_error(conn_info->enc.ssl, val);
CcspTraceError(("(%s:%d) SSL_write failed (Ret: %d, SSL Error: %d)\n", __FUNCTION__, __LINE__, val, ssl_err));
}
}
else
Expand Down
3 changes: 3 additions & 0 deletions source/InterDeviceManager/Idm_TCP_apis.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,9 @@
#include "Idm_rbus.h"
#include "Idm_msg_process.h"

extern bool connect_reset;
extern pthread_mutex_t connect_reset_mutex;

int open_remote_connection(connection_config_t* connectionConf, int (*connection_cb)(device_info_t* Device, connection_info_t* conn_info, uint encryption_status), int (*rcv_message_cb)( connection_info_t* conn_info, void *payload)) ;

int send_remote_message(connection_info_t* conn_info, void *payload);
Expand Down
8 changes: 7 additions & 1 deletion source/InterDeviceManager/Idm_call_back_apis.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,10 @@ int stop_discovery();

int rcv_message_cb( connection_info_t* conn_info, void *payload)
{
CcspTraceInfo(("%s %d - \n", __FUNCTION__, __LINE__));

payload_t *recvData = (payload_t*)payload;
CcspTraceInfo(("%s %d - msgType-%d \n", __FUNCTION__, __LINE__,recvData->msgType));

if(recvData->msgType == REQ)
{
IDM_Incoming_Request_handler(recvData);
Expand Down Expand Up @@ -823,6 +824,11 @@ ANSC_STATUS IDM_Start_Device_Discovery()
ANSC_STATUS IDM_Stop_Device_Discovery()
{
CcspTraceInfo(("%s %d - called\n", __FUNCTION__, __LINE__ ));

pthread_mutex_lock(&connect_reset_mutex);
connect_reset = true; //To exit any previous waiting connect
pthread_mutex_unlock(&connect_reset_mutex);

if(stop_discovery() !=0)
{
CcspTraceError(("%s %d - stop_discovery failed\n", __FUNCTION__, __LINE__));
Expand Down
81 changes: 65 additions & 16 deletions source/InterDeviceManager/Idm_msg_process.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,28 @@ sendReqList* IDM_getFromSendRequestList(uint reqID)
}
}

sendReqList* IDM_searchFromSendRequestList(const char *param_mac, const char *param_name)
{

if(param_mac == NULL)
{
return NULL;
}

sendReqList *cur = headsendReqList;
while (cur != NULL)
{
if (strncmp(cur->Mac_dest, param_mac, sizeof(cur->Mac_dest) - 1) == 0)
{
if (strncmp(cur->param_name, param_name, sizeof(cur->param_name) -1) == 0)
{
return cur;
}
}
cur = cur->next;
}
return NULL;
}

void IDM_addToSendSubscriptionuestList( sendSubscriptionList *newSubscription)
{
Expand Down Expand Up @@ -256,6 +278,7 @@ ANSC_STATUS IDM_getFile_from_Remote_device(char* Mac_dest,char* filename,char* o
strncpy(payload.param_name,filename,sizeof(payload.param_name)-1);
strncpy(newReq->output_location,output_location,sizeof(newReq->output_location)-1);
payload.reqID = newReq->reqId;
strncpy(newReq->param_name, payload.param_name, sizeof(newReq->param_name)-1);
IDM_addToSendRequestList(newReq);
CcspTraceDebug(("Inside %s:%d peer MAC=%s\n",__FUNCTION__,__LINE__,Mac_dest));
send_remote_message(&remoteDevice->stRemoteDeviceInfo.conn_info, &payload);
Expand Down Expand Up @@ -318,31 +341,40 @@ ANSC_STATUS IDM_sendMsg_to_Remote_device(idm_send_msg_Params_t *param)
memset(&payload, 0, sizeof(payload_t));
if(param->operation == GET || param->operation == SET || param->operation == IDM_REQUEST)
{
/* Create request entry */
sendReqList *newReq = malloc(sizeof(sendReqList));
if (newReq != NULL) {
memset(newReq, 0, sizeof(sendReqList));
newReq->reqId = gReqIdCounter++;
strncpy(newReq->Mac_dest,param->Mac_dest, sizeof(newReq->Mac_dest)-1);
newReq->resCb = param->resCb;
newReq->timeout = param->timeout;
newReq->next = NULL;

IDM_addToSendRequestList(newReq);
payload.reqID = newReq->reqId;
}
sendReqList *SendReq = IDM_searchFromSendRequestList(param->Mac_dest, param->param_name);
if(SendReq != NULL)
{
CcspTraceInfo(("%s:%d Resending the same request with request id %d \n",__FUNCTION__, __LINE__,SendReq->reqId));
payload.reqID = SendReq->reqId;
}
else
{
/* Create request entry */
sendReqList *newReq = malloc(sizeof(sendReqList));
if (newReq != NULL) {
memset(newReq, 0, sizeof(sendReqList));
newReq->reqId = gReqIdCounter++;
strncpy(newReq->Mac_dest,param->Mac_dest, sizeof(newReq->Mac_dest)-1);
newReq->resCb = param->resCb;
newReq->timeout = param->timeout;
newReq->next = NULL;
strncpy(newReq->param_name, param->param_name, sizeof(newReq->param_name)-1);
IDM_addToSendRequestList(newReq);
payload.reqID = newReq->reqId;
}
}
}else if(param->operation == IDM_SUBS)
{
/* Create request entry */
sendSubscriptionList *newReq = malloc(sizeof(sendSubscriptionList));
if (newReq != NULL) {
if (newReq != NULL) {
memset(newReq, 0, sizeof(sendSubscriptionList));
newReq->reqId = gReqIdCounter++;
newReq->resCb = param->resCb;
newReq->next = NULL;
IDM_addToSendSubscriptionuestList(newReq);
payload.reqID = newReq->reqId;
}
}
}

payload.operation = param->operation;
Expand All @@ -353,8 +385,24 @@ ANSC_STATUS IDM_sendMsg_to_Remote_device(idm_send_msg_Params_t *param)
payload.type = param->type;

/* send message */
send_remote_message(&remoteDevice->stRemoteDeviceInfo.conn_info, &payload);
int ret = send_remote_message(&remoteDevice->stRemoteDeviceInfo.conn_info, &payload);
usleep(250000); //Sleep for 250ms
if(ret != 0)
{
CcspTraceError(("%s:%d send_remote_message failed for request id %d\n",__FUNCTION__, __LINE__,payload.reqID));
if(param->operation == GET || param->operation == SET || param->operation == IDM_REQUEST)
{
sendReqList *req;
req = IDM_getFromSendRequestList(payload.reqID);
if(req == NULL)
{
CcspTraceError(("%s:%d Request not found in SendRequestList \n",__FUNCTION__, __LINE__));
}else{
CcspTraceInfo(("%s:%d Removing request from SendRequestList \n",__FUNCTION__, __LINE__));
free(req);
}
}
}
}else
{
IdmMgrDml_GetConfigData_release(pidmDmlInfo);
Expand Down Expand Up @@ -490,6 +538,7 @@ int IDM_Incoming_Response_handler(payload_t * payload)
{
rbusMethodAsyncHandle_t async_callBack_handler;
rbusError_t ret = RBUS_ERROR_SUCCESS;
CcspTraceInfo(("%s:%d operation - %d req id %d \n",__FUNCTION__, __LINE__,payload->operation, payload->reqID));
/* find req entry in LL */
if(payload->operation == IDM_SUBS)
{
Expand Down
1 change: 1 addition & 0 deletions source/InterDeviceManager/Idm_msg_process.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ typedef struct _sendReqList
uint reqId;
char Mac_dest[MAC_ADDR_SIZE];
rbusMethodAsyncHandle_t resCb;
char param_name[128];
uint timeout;
char output_location[LOC_SIZE];
struct _sendReqList *next;
Expand Down