コード例 #1
0
ファイル: iso_server.c プロジェクト: feuvan/libiec61850
void
IsoServer_startListening(IsoServer self)
{
    if (self->state == ISO_SVR_STATE_RUNNING) {
        if (DEBUG_ISO_SERVER)
                printf("ISO_SERVER: server already in RUNNING state!\n");

        goto exit_function;
    }

    self->state = ISO_SVR_STATE_IDLE;

    self->serverThread = Thread_create((ThreadExecutionFunction) isoServerThread, self, false);

    Thread_start(self->serverThread);

    /* wait until server is up */
    while (self->state == ISO_SVR_STATE_IDLE)
        Thread_sleep(1);

    if (DEBUG_ISO_SERVER)
        printf("ISO_SERVER: new iso server thread started\n");

exit_function:
    return;
}
コード例 #2
0
ファイル: Thread.c プロジェクト: Adamwuming/paho.mqtt.c
int main(int argc, char *argv[])
{
	int rc = 0;

	sem_type sem = Thread_create_sem();

	printf("check sem %d\n", Thread_check_sem(sem));

	printf("post secondary\n");
	rc = Thread_post_sem(sem);
	printf("posted secondary %d\n", rc);

	printf("check sem %d\n", Thread_check_sem(sem));

	printf("Starting secondary thread\n");
	Thread_start(secondary, (void*)sem);

	sleep(3);
	printf("check sem %d\n", Thread_check_sem(sem));

	printf("post secondary\n");
	rc = Thread_post_sem(sem);
	printf("posted secondary %d\n", rc);

	sleep(3);

	printf("Main thread ending\n");
}
コード例 #3
0
ファイル: Customer.c プロジェクト: tachyo/SNP
struct Customer* new_Customer(struct Restaurant* restaurant, unsigned int counter)
{
  struct Customer* this = malloc(sizeof(struct Customer));
  this->restaurant_ = restaurant;
  sprintf(this->name_, "Customer %u", counter);
  Thread_start(Customer_run, this);
  return this;
}
コード例 #4
0
ファイル: MQTTClient.c プロジェクト: krattai/noo-ebs
int MQTTClient_disconnect1(MQTTClient handle, int timeout, int internal, int stop)
{
	MQTTClients* m = handle;
	START_TIME_TYPE start;
	int rc = MQTTCLIENT_SUCCESS;
	int was_connected = 0;

	FUNC_ENTRY;
	Thread_lock_mutex(mqttclient_mutex);

	if (m == NULL || m->c == NULL)
	{
		rc = MQTTCLIENT_FAILURE;
		goto exit;
	}
	if (m->c->connected == 0 && m->c->connect_state == 0)
	{
		rc = MQTTCLIENT_DISCONNECTED;
		goto exit;
	}
	was_connected = m->c->connected; /* should be 1 */
	if (m->c->connected != 0)
	{
		start = MQTTClient_start_clock();
		m->c->connect_state = -2; /* indicate disconnecting */
		while (m->c->inboundMsgs->count > 0 || m->c->outboundMsgs->count > 0)
		{ /* wait for all inflight message flows to finish, up to timeout */
			if (MQTTClient_elapsed(start) >= timeout)
				break;
			Thread_unlock_mutex(mqttclient_mutex);
			MQTTClient_yield();
			Thread_lock_mutex(mqttclient_mutex);
		}
	}

	MQTTClient_closeSession(m->c);

	while (Thread_check_sem(m->connect_sem))
		Thread_wait_sem(m->connect_sem, 100);
	while (Thread_check_sem(m->connack_sem))
		Thread_wait_sem(m->connack_sem, 100);
	while (Thread_check_sem(m->suback_sem))
		Thread_wait_sem(m->suback_sem, 100);
	while (Thread_check_sem(m->unsuback_sem))
		Thread_wait_sem(m->unsuback_sem, 100);
exit:
	if (stop)
		MQTTClient_stop();
	if (internal && m->cl && was_connected)
	{
		Log(TRACE_MIN, -1, "Calling connectionLost for client %s", m->c->clientID);
		Thread_start(connectionLost_call, m);
	}
	Thread_unlock_mutex(mqttclient_mutex);
	FUNC_EXIT_RC(rc);
	return rc;
}
コード例 #5
0
ファイル: iso_server.c プロジェクト: brennane/gridpot
void
IsoServer_startListening(IsoServer self)
{
    self->serverThread = Thread_create((ThreadExecutionFunction) isoServerThread, self, false);

    Thread_start(self->serverThread);

    /* wait until server is up */
    while (self->state == ISO_SVR_STATE_IDLE)
        Thread_sleep(1);

    if (DEBUG_ISO_SERVER)
        printf("ISO_SERVER: new iso server thread started\n");
}
コード例 #6
0
ファイル: ied_server.c プロジェクト: nono19710321/libiec61850
void
IedServer_start(IedServer self, int tcpPort)
{
#if (CONFIG_MMS_SINGLE_THREADED == 1)
    MmsServer_startListeningThreadless(self->mmsServer, tcpPort);

    Thread serverThread = Thread_create((ThreadExecutionFunction) singleThreadedServerThread, (void*) self, true);

    Thread_start(serverThread);
#else

    MmsServer_startListening(self->mmsServer, tcpPort);
    MmsMapping_startEventWorkerThread(self->mmsMapping);
#endif
}
コード例 #7
0
ファイル: goose_receiver.c プロジェクト: sagok/SmI_IEC104
// start GOOSE receiver in a separate thread
void
GooseReceiver_start(GooseReceiver self)
{
    Thread thread = Thread_create((ThreadExecutionFunction) gooseReceiverLoop, (void*) self, true);

    if (thread != NULL) {
        if (DEBUG_GOOSE_SUBSCRIBER)
            printf("GOOSE_SUBSCRIBER: GOOSE receiver started for interface %s\n", self->interfaceId);

        Thread_start(thread);
    }
    else {
        if (DEBUG_GOOSE_SUBSCRIBER)
            printf("GOOSE_SUBSCRIBER: Starting GOOSE receiver failed for interface %s\n", self->interfaceId);
    }

}
コード例 #8
0
ファイル: iso_connection.c プロジェクト: andy-w/libiec61850
IsoConnection
IsoConnection_create(Socket socket, IsoServer isoServer)
{
	IsoConnection self = calloc(1, sizeof(struct sIsoConnection));
	self->socket = socket;
	self->receive_buf = malloc(RECEIVE_BUF_SIZE);
	self->send_buf_1 = malloc(SEND_BUF_SIZE);
	self->send_buf_2 = malloc(SEND_BUF_SIZE);
	self->msgRcvdHandler = NULL;
	self->msgRcvdHandlerParameter = NULL;
	self->isoServer = isoServer;
	self->state = ISO_CON_STATE_RUNNING;

	Thread thread = Thread_create(handleTcpConnection, self, true);

	Thread_start(thread);

	if (DEBUG) printf("new iso connection thread started\n");

	return self;
}
コード例 #9
0
ファイル: IoThread.c プロジェクト: cdcarter/io
IoObject *IoThread_createThread(IoObject *self, IoObject *locals, IoMessage *m)
{
	IoSeq *s = IoMessage_locals_seqArgAt_(m, locals, 0);
	IoState *newState = IoState_new();
	Thread *t;

	Thread_Init();

	t = Thread_new();

	IoThreadInfo *ti = IoThreadInfo_new();
	IoThreadInfo_setState_(ti, newState);
	IoThreadInfo_setThread_(ti, t);
	IoThreadInfo_setEvalString_(ti, CSTRING(s));

	Thread_setFunc_(t, IoThread_BeginThread);
	Thread_setFuncArg_(t, (void *)ti);
	Thread_start(t);

	return self;
}
コード例 #10
0
ファイル: iso_connection.c プロジェクト: duchuan1/libIEC61850
IsoConnection
IsoConnection_create(Socket socket, IsoServer isoServer)
{
    IsoConnection self = (IsoConnection) calloc(1, sizeof(struct sIsoConnection));
    self->socket = socket;
    self->receiveBuffer = (uint8_t*) malloc(RECEIVE_BUF_SIZE);
    self->sendBuffer = (uint8_t*) malloc(SEND_BUF_SIZE);
    self->msgRcvdHandler = NULL;
    self->msgRcvdHandlerParameter = NULL;
    self->isoServer = isoServer;
    self->state = ISO_CON_STATE_RUNNING;
    self->clientAddress = Socket_getPeerAddress(self->socket);

    self->thread = Thread_create((ThreadExecutionFunction) handleTcpConnection, self, true);
    self->conMutex = Semaphore_create(1);

    Thread_start(self->thread);

    if (DEBUG_ISO_SERVER)
        printf("ISO_SERVER: new iso connection thread started\n");

    return self;
}
コード例 #11
0
ファイル: MQTTClient.c プロジェクト: brc859844/paho.mqtt.c
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
{
	MQTTClients* m = handle;
	int rc = SOCKET_ERROR;
	START_TIME_TYPE start;
	long millisecsTimeout = 30000L;

	FUNC_ENTRY;
	Thread_lock_mutex(mqttclient_mutex);

	if (options == NULL)
	{
		rc = MQTTCLIENT_NULL_PARAMETER;
		goto exit;
	}

	if (strncmp(options->struct_id, "MQTC", 4) != 0 || (options->struct_version != 0 && options->struct_version != 1))
	{
		rc = MQTTCLIENT_BAD_STRUCTURE;
		goto exit;
	}

	if (options->will) /* check validity of will options structure */
	{
		if (strncmp(options->will->struct_id, "MQTW", 4) != 0 || options->will->struct_version != 0)
		{
			rc = MQTTCLIENT_BAD_STRUCTURE;
			goto exit;
		}
	}

#if defined(OPENSSL)
	if (options->struct_version != 0 && options->ssl) /* check validity of SSL options structure */
	{
		if (strncmp(options->ssl->struct_id, "MQTS", 4) != 0 || options->ssl->struct_version != 0)
		{
			rc = MQTTCLIENT_BAD_STRUCTURE;
			goto exit;
		}
	}
#endif

	if ((options->username && !UTF8_validateString(options->username)) ||
		(options->password && !UTF8_validateString(options->password)))
	{
		rc = MQTTCLIENT_BAD_UTF8_STRING;
		goto exit;
	}

	millisecsTimeout = options->connectTimeout * 1000;
	start = MQTTClient_start_clock();
	if (m->ma && !running)
	{
		Thread_start(MQTTClient_run, handle);
		if (MQTTClient_elapsed(start) >= millisecsTimeout)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
		MQTTClient_sleep(100L);
	}

	m->c->keepAliveInterval = options->keepAliveInterval;
	m->c->cleansession = options->cleansession;
	m->c->maxInflightMessages = (options->reliable) ? 1 : 10;

	if (options->will && options->will->struct_version == 0)
	{
	    m->c->will = malloc(sizeof(willMessages));
		m->c->will->msg = options->will->message;
		m->c->will->qos = options->will->qos;
		m->c->will->retained = options->will->retained;
		m->c->will->topic = options->will->topicName;
	}

#if defined(OPENSSL)
	if (options->struct_version != 0 && options->ssl)
		m->c->sslopts = options->ssl;
#endif

	m->c->username = options->username;
	m->c->password = options->password;
	m->c->retryInterval = options->retryInterval;

	Log(TRACE_MIN, -1, "Connecting to serverURI %s", m->serverURI);
#if defined(OPENSSL)
	rc = MQTTProtocol_connect(m->serverURI, m->c, m->ssl);
#else
	rc = MQTTProtocol_connect(m->serverURI, m->c);
#endif
	if (rc == SOCKET_ERROR)
		goto exit;

	if (m->c->connect_state == 0)
	{
		rc = SOCKET_ERROR;
		goto exit;
	}

	if (m->c->connect_state == 1) /* TCP connect started - wait for completion */
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (rc != 0)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}

#if defined(OPENSSL)
		if (m->ssl)
		{
			if (SSLSocket_setSocketForSSL(&m->c->net, m->c->sslopts) != MQTTCLIENT_SUCCESS)
			{
				if (m->c->session != NULL)
					if ((rc = SSL_set_session(m->c->net.ssl, m->c->session)) != 1)
						Log(TRACE_MIN, -1, "Failed to set SSL session with stored data, non critical");
				rc = SSLSocket_connect(m->c->net.ssl, m->c->net.socket);
				if (rc == -1)
					m->c->connect_state = 2;
            	else if (rc == SSL_FATAL)
				{
					rc = SOCKET_ERROR;
               		goto exit;
				}
            	else if (rc == 1 && !m->c->cleansession && m->c->session == NULL)
					m->c->session = SSL_get1_session(m->c->net.ssl);
			}
			else
			{
				rc = SOCKET_ERROR;
				goto exit;
			}
		}
		else
		{
#endif
			m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */
			if (MQTTPacket_send_connect(m->c) == SOCKET_ERROR)
			{
				rc = SOCKET_ERROR;
				goto exit;
			}
#if defined(OPENSSL)
		}
#endif
	}

#if defined(OPENSSL)
	if (m->c->connect_state == 2) /* SSL connect sent - wait for completion */
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (rc != 1)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
		m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */
		if (MQTTPacket_send_connect(m->c) == SOCKET_ERROR)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
	}
#endif

	if (m->c->connect_state == 3) /* MQTT connect sent - wait for CONNACK */
	{
		MQTTPacket* pack = NULL;

		Thread_unlock_mutex(mqttclient_mutex);
		pack = MQTTClient_waitfor(handle, CONNACK, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (pack == NULL)
			rc = SOCKET_ERROR;
		else
		{
			Connack* connack = (Connack*)pack;
			Log(LOG_PROTOCOL, 1, NULL, m->c->net.socket, m->c->clientID, connack->rc);
			if ((rc = connack->rc) == MQTTCLIENT_SUCCESS)
			{
				m->c->connected = 1;
				m->c->good = 1;
				m->c->connect_state = 0;
				if (m->c->cleansession)
					rc = MQTTClient_cleanSession(m->c);
				if (m->c->outboundMsgs->count > 0)
				{
					ListElement* outcurrent = NULL;

					while (ListNextElement(m->c->outboundMsgs, &outcurrent))
					{
						Messages* m = (Messages*)(outcurrent->content);
						m->lastTouch = 0;
					}
					MQTTProtocol_retry(m->c->net.lastContact, 1);
					if (m->c->connected != 1)
						rc = MQTTCLIENT_DISCONNECTED;
				}
			}
			free(connack);
			m->pack = NULL;
		}
	}

exit:
	if (rc != MQTTCLIENT_SUCCESS)
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_disconnect(handle, 0); /* not "internal" because we don't want to call connection lost */
		Thread_lock_mutex(mqttclient_mutex);
	}

    if (m->c->will)
    {
      free(m->c->will);
      m->c->will = NULL;
    }
	Thread_unlock_mutex(mqttclient_mutex);
	FUNC_EXIT_RC(rc);
	return rc;
}
コード例 #12
0
void
IsoClientConnection_associate(IsoClientConnection self, IsoConnectionParameters* params,
		ByteBuffer* payload)
{
	Socket socket = TcpSocket_create();

	self->socket = socket;

	if (!Socket_connect(socket, params->hostname, params->tcpPort))
		goto returnError;

	self->cotpBuf = malloc(ISO_CLIENT_BUFFER_SIZE);
	self->cotpBuffer = calloc(1, sizeof(ByteBuffer));
	ByteBuffer_wrap(self->cotpBuffer, self->cotpBuf, 0, ISO_CLIENT_BUFFER_SIZE);

	self->cotpConnection = calloc(1, sizeof(CotpConnection));
	CotpConnection_init(self->cotpConnection, socket, self->cotpBuffer);

	/* COTP handshake */
	CotpIndication cotpIndication =
			CotpConnection_sendConnectionRequestMessage(self->cotpConnection);

	cotpIndication = CotpConnection_parseIncomingMessage(self->cotpConnection);

	if (cotpIndication != CONNECT_INDICATION)
		goto returnError;

	/* Upper layers handshake */
	AcseConnection acse;

	ByteBuffer acseBuffer;
	ByteBuffer_wrap(&acseBuffer, self->buffer1, 0, ISO_CLIENT_BUFFER_SIZE);

	AcseConnection_init(&acse);

	if (params != NULL)
		AcseConnection_setAuthenticationParameter(&acse, params->acseAuthParameter);

	AcseConnection_createAssociateRequestMessage(&acse, &acseBuffer, payload);

	ByteBuffer presentationBuffer;
	ByteBuffer_wrap(&presentationBuffer, self->buffer2, 0, ISO_CLIENT_BUFFER_SIZE);

	self->presentation = calloc(1, sizeof(IsoPresentation));
	IsoPresentation_init(self->presentation);
	IsoPresentation_createConnectPdu(self->presentation, &presentationBuffer, &acseBuffer);

	ByteBuffer sessionBuffer;
	ByteBuffer_wrap(&sessionBuffer, self->buffer1, 0, ISO_CLIENT_BUFFER_SIZE);

	self->session = calloc(1, sizeof(IsoSession));
	IsoSession_init(self->session);
	IsoSession_createConnectSpdu(self->session, &sessionBuffer,
			ByteBuffer_getSize(&presentationBuffer));

	ByteBuffer_append(&sessionBuffer, ByteBuffer_getBuffer(&presentationBuffer),
			ByteBuffer_getSize(&presentationBuffer));


	CotpConnection_sendDataMessage(self->cotpConnection, &sessionBuffer);

	cotpIndication = CotpConnection_parseIncomingMessage(self->cotpConnection);

	if (cotpIndication != DATA_INDICATION)
		goto returnError;

	IsoSessionIndication sessionIndication;

	sessionIndication =
			IsoSession_parseMessage(self->session, CotpConnection_getPayload(self->cotpConnection));

	if (sessionIndication != SESSION_CONNECT) {
		if (DEBUG) printf("IsoClientConnection_associate: no session connect indication\n");
		goto returnError;
	}


	IsoPresentationIndication presentationIndication;
	presentationIndication =
			IsoPresentation_parseAcceptMessage(self->presentation, IsoSession_getUserData(self->session));

	if (presentationIndication != PRESENTATION_OK) {
		if (DEBUG) printf("IsoClientConnection_associate: no presentation ok indication\n");
		goto returnError;
	}

	AcseIndication acseIndication;

	acseIndication = AcseConnection_parseMessage(&acse, &self->presentation->nextPayload);

	if (acseIndication != ACSE_ASSOCIATE) {
		if (DEBUG) printf("IsoClientConnection_associate: no ACSE_ASSOCIATE indication\n");
		goto returnError;
	}

	ByteBuffer acsePayload; //TODO allocate buffer dynamically???
	ByteBuffer_wrap(&acsePayload, acse.userDataBuffer, acse.userDataBufferSize, 1024);

	self->callback(ISO_IND_ASSOCIATION_SUCCESS, self->callbackParameter, &acsePayload);

	self->state = STATE_ASSOCIATED;

	AcseConnection_destroy(&acse);

	self->thread = Thread_create(connectionHandlingThread, self, false);
	Thread_start(self->thread);

	return;

returnError:
	self->callback(ISO_IND_ASSOCIATION_FAILED, self->callbackParameter, &acsePayload);

	AcseConnection_destroy(&acse);

	self->state = STATE_ERROR;
	return;
}
コード例 #13
0
ファイル: MQTTClient.c プロジェクト: krattai/noo-ebs
int MQTTClient_connectURIVersion(MQTTClient handle, MQTTClient_connectOptions* options, const char* serverURI, int MQTTVersion,
	START_TIME_TYPE start, long millisecsTimeout)
{
	MQTTClients* m = handle;
	int rc = SOCKET_ERROR;
	int sessionPresent = 0;

	FUNC_ENTRY;
	if (m->ma && !running)
	{
		Thread_start(MQTTClient_run, handle);
		if (MQTTClient_elapsed(start) >= millisecsTimeout)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
		MQTTClient_sleep(100L);
	}

	Log(TRACE_MIN, -1, "Connecting to serverURI %s with MQTT version %d", serverURI, MQTTVersion);
#if defined(OPENSSL)
	rc = MQTTProtocol_connect(serverURI, m->c, m->ssl, MQTTVersion);
#else
	rc = MQTTProtocol_connect(serverURI, m->c, MQTTVersion);
#endif
	if (rc == SOCKET_ERROR)
		goto exit;

	if (m->c->connect_state == 0)
	{
		rc = SOCKET_ERROR;
		goto exit;
	}

	if (m->c->connect_state == 1) /* TCP connect started - wait for completion */
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (rc != 0)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
		
#if defined(OPENSSL)
		if (m->ssl)
		{
			if (SSLSocket_setSocketForSSL(&m->c->net, m->c->sslopts) != MQTTCLIENT_SUCCESS)
			{
				if (m->c->session != NULL)
					if ((rc = SSL_set_session(m->c->net.ssl, m->c->session)) != 1)
						Log(TRACE_MIN, -1, "Failed to set SSL session with stored data, non critical");
				rc = SSLSocket_connect(m->c->net.ssl, m->c->net.socket);
				if (rc == TCPSOCKET_INTERRUPTED)
					m->c->connect_state = 2;  /* the connect is still in progress */
				else if (rc == SSL_FATAL)
				{
					rc = SOCKET_ERROR;
					goto exit;
				}
				else if (rc == 1) 
				{
					rc = MQTTCLIENT_SUCCESS;
					m->c->connect_state = 3;
					if (MQTTPacket_send_connect(m->c, MQTTVersion) == SOCKET_ERROR)
					{
						rc = SOCKET_ERROR;
						goto exit;
					}
					if (!m->c->cleansession && m->c->session == NULL)
						m->c->session = SSL_get1_session(m->c->net.ssl);
				}
			}
			else
			{
				rc = SOCKET_ERROR;
				goto exit;
			}
		}
		else
		{
#endif
			m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */
			if (MQTTPacket_send_connect(m->c, MQTTVersion) == SOCKET_ERROR)
			{
				rc = SOCKET_ERROR;
				goto exit;
			}
#if defined(OPENSSL)
		}
#endif
	}
	
#if defined(OPENSSL)
	if (m->c->connect_state == 2) /* SSL connect sent - wait for completion */
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (rc != 1)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
		if(!m->c->cleansession && m->c->session == NULL)
			m->c->session = SSL_get1_session(m->c->net.ssl);
		m->c->connect_state = 3; /* TCP connect completed, in which case send the MQTT connect packet */
		if (MQTTPacket_send_connect(m->c, MQTTVersion) == SOCKET_ERROR)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
	}
#endif

	if (m->c->connect_state == 3) /* MQTT connect sent - wait for CONNACK */
	{
		MQTTPacket* pack = NULL;

		Thread_unlock_mutex(mqttclient_mutex);
		pack = MQTTClient_waitfor(handle, CONNACK, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (pack == NULL)
			rc = SOCKET_ERROR;
		else
		{
			Connack* connack = (Connack*)pack;
			Log(TRACE_PROTOCOL, 1, NULL, m->c->net.socket, m->c->clientID, connack->rc);
			if ((rc = connack->rc) == MQTTCLIENT_SUCCESS)
			{
				m->c->connected = 1;
				m->c->good = 1;
				m->c->connect_state = 0;
				if (MQTTVersion == 4)
					sessionPresent = connack->flags.bits.sessionPresent;
				if (m->c->cleansession)
					rc = MQTTClient_cleanSession(m->c);
				if (m->c->outboundMsgs->count > 0)
				{
					ListElement* outcurrent = NULL;

					while (ListNextElement(m->c->outboundMsgs, &outcurrent))
					{
						Messages* m = (Messages*)(outcurrent->content);
						m->lastTouch = 0;
					}
					MQTTProtocol_retry((time_t)0, 1, 1);
					if (m->c->connected != 1)
						rc = MQTTCLIENT_DISCONNECTED;
				}
			}
			free(connack);
			m->pack = NULL;
		}
	}
exit:
	if (rc == MQTTCLIENT_SUCCESS)
	{
		if (options->struct_version == 4) /* means we have to fill out return values */
		{			
			options->returned.serverURI = serverURI;
			options->returned.MQTTVersion = MQTTVersion;    
			options->returned.sessionPresent = sessionPresent;
		}
	}
	else
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_disconnect1(handle, 0, 0, (MQTTVersion == 3)); /* not "internal" because we don't want to call connection lost */
		Thread_lock_mutex(mqttclient_mutex);
	}
	FUNC_EXIT_RC(rc);
  return rc;
}
コード例 #14
0
/*
 *  ======== Processor_init ========
 */
Void Processor_init(Void)
{
    GateThread_Params params;
    Registry_Result   result;

    /*
     *  No need to reference count for Registry_addModule(), since there
     *  is no way to remove the module.
     */
    if (regInit == 0) {
        /* Register this module for logging */
        result = Registry_addModule(&ti_sdo_ce_ipc_processor_desc,
                Processor_MODNAME);
        Assert_isTrue(result == Registry_SUCCESS, (Assert_Id)NULL);

        if (result == Registry_SUCCESS) {
            /* Set the diags mask to the CE default */
            CESettings_init();
            CESettings_setDiags(Processor_MODNAME);
        }
        regInit = 1;
    }

    if (curInit != TRUE) {
        curInit = TRUE;

        /* Semaphore with count 0, will be posted when a command is present */
        dcmd.cmdPresent = SemThread_create(0, NULL, NULL);

        /* Semaphore with count 0, will be posted when reply is ready */
        dcmd.replyPresent = SemThread_create(0, NULL, NULL);

        /*
         *  Create lock to allow only one thread at a time to send command
         *  to the daemon.
         */
        GateThread_Params_init(&params);
        dcmd.gate = GateThread_create(&params, NULL);

        if ((dcmd.cmdPresent == NULL) || (dcmd.replyPresent == NULL) ||
                (dcmd.gate == NULL)) {
            // TODO: Shouldn't we abort?
            Log_print0(Diags_USER7, "[+7] Processor_init> ERROR: cannot"
                    " create semaphores or lock");
        }

        Log_print0(Diags_USER2, "[+2] Processor_init> SysLink_setup()...");

        SysLink_setup();
        Log_print0(Diags_USER2, "[+2] Processor_init> "
                "... SysLink_setup() done");

        if ((dcmd.dproc = Thread_create((Thread_RunFxn)daemon, NULL, NULL))
                == NULL) {
            Log_print0(Diags_USER7, "[+7] Processor_init> "
                    "ERROR: cannot create DSP daemon");
        }

        if (Thread_start(NULL) != TRUE) {
            Log_print0(Diags_USER7, "[+7] Processor_init> "
                    "ERROR: cannot start threads");
        }

        Global_atexit((Fxn)cleanup);
    }
}
コード例 #15
0
Void smain(UArg arg0, UArg arg1)
{
    Int                 status = 0;
    SystemCfg_Params    systemCfgP;
    SystemCfg_Handle    systemCfgH = NULL;
    ComputeDevice_Cfg   compDevCfg;


    /* initialize modules */
    SystemCfg_init();
    ComputeDevice_init();

    /* start all statically created threads now */
    Thread_start(NULL);

    /* turn on some trace */
//  Diags_setMask("SystemCfg+F;ComputeDevice+F");
    Diags_setMask("ComputeDevice+FEX");

    /* create a SystemCfg instance for the remote processor */
    SystemCfg_Params_init(&systemCfgP);
    systemCfgP.remoteProcName = SystemCfg_Host_ProcName;

    status = SystemCfg_create(&systemCfgP, &systemCfgH);

    if (status < 0) {
        goto leave;
    }

    /* let the host know this processor has started */
    status = SystemCfg_startAck(systemCfgH);

    if (status < 0) {
        Log_error0(FXNN": SystemCfg_startAck() failed");
        goto leave;
    }

    /* attach to the remote processor, blocks until remote side attaches */
    status = SystemCfg_attach(systemCfgH);

    if (status < 0) {
        Log_error0(FXNN": SystemCfg_attach() failed");
        goto leave;
    }

    /* start the compute device, start the OpenCL Runtime */
    compDevCfg.rcmServerName = SystemCfg_RcmServer_CompDev;

    status = ComputeDevice_start((Ptr)(&compDevCfg));

    if (status < 0) {
        Log_error0(FXNN": ComputeDevice_start() failed");
        goto leave;
    }

    /* send ready event */
    SystemCfg_sendEvent(systemCfgH, SystemCfg_EvtReady);

    /*
     *  BEGIN execute phase
     */

    /* wait for the shutdown signal */
    SystemCfg_waitForEvent(systemCfgH, SystemCfg_EvtShutdown);

    /*
     *  END execute phase
     */

    /* stop the compute device */
    status = ComputeDevice_stop((Ptr)(&compDevCfg));

    if (status < 0) {
        Log_error0(FXNN": ComputeDevice_stop() failed");
        goto leave;
    }

    /* wait for handshake before terminating */
    SystemCfg_handshakeEvent(systemCfgH, SystemCfg_EvtDone);

    /* detach from the remote processor */
    SystemCfg_detach(systemCfgH);

    /* prepare to be stopped */
    SystemCfg_stop(systemCfgH);

    /* delete SystemCfg instance */
    SystemCfg_delete(&systemCfgH);

    /* finalize modules */
    ComputeDevice_exit();
    SystemCfg_exit();

leave:
    Log_print1(Diags_EXIT, "<-- "FXNN": %d", (IArg)status);
    return;
}
コード例 #16
0
int MQTTClient_connect(MQTTClient handle, MQTTClient_connectOptions* options)
{
	MQTTClients* m = handle;
	int rc = SOCKET_ERROR;
	START_TIME_TYPE start;
	long millisecsTimeout = 30000L;

	FUNC_ENTRY;
	Thread_lock_mutex(mqttclient_mutex);

	if (options == NULL)
	{
		rc = MQTTCLIENT_NULL_PARAMETER;
		goto exit;
	}

	if (strncmp(options->struct_id, "MQTC", 4) != 0 || options->struct_version != 0)
	{
		rc = MQTTCLIENT_BAD_STRUCTURE;
		goto exit;
	}

	if (options->will) /* check validity of will options structure */
	{
		if (strncmp(options->will->struct_id, "MQTW", 4) != 0 || options->will->struct_version != 0)
		{
			rc = MQTTCLIENT_BAD_STRUCTURE;
			goto exit;
		}
	}

	if ((options->username && !UTF8_validateString(options->username)) ||
		(options->password && !UTF8_validateString(options->password)))
	{
		rc = MQTTCLIENT_BAD_UTF8_STRING;
		goto exit;
	}

	millisecsTimeout = options->connectTimeout * 1000;
	start = MQTTClient_start_clock();
	if (m->ma && !running)
	{
		Thread_start(MQTTClient_run, handle);
		if (MQTTClient_elapsed(start) >= millisecsTimeout)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
		MQTTClient_sleep(100L);
	}

	m->c->keepAliveInterval = options->keepAliveInterval;
	m->c->cleansession = options->cleansession;
	m->c->maxInflightMessages = (options->reliable) ? 1 : 10;

	if (options->will && options->will->struct_version == 0)
	{
		m->c->will = malloc(sizeof(willMessages));
		m->c->will->msg = options->will->message;
		m->c->will->qos = options->will->qos;
		m->c->will->retained = options->will->retained;
		m->c->will->topic = options->will->topicName;
	}

	m->c->username = options->username;
	m->c->password = options->password;
	m->c->retryInterval = options->retryInterval;
	m->c->connectOptionsVersion = options->struct_version;

	Log(TRACE_MIN, -1, "Connecting to serverURI %s", m->serverURI);
	rc = MQTTProtocol_connect(m->serverURI, m->c);

	if (m->c->connect_state == 0)
	{
		rc = SOCKET_ERROR;
		goto exit;
	}

	if (m->c->connect_state == 1)
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_waitfor(handle, CONNECT, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (rc != 0)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}

		m->c->connect_state = 2; /* TCP connect completed, in which case send the MQTT connect packet */
		if (MQTTPacket_send_connect(m->c) == SOCKET_ERROR)
		{
			rc = SOCKET_ERROR;
			goto exit;
		}
	}

	if (m->c->connect_state == 2)
	{
		MQTTPacket* pack = NULL;

		Thread_unlock_mutex(mqttclient_mutex);
		pack = MQTTClient_waitfor(handle, CONNACK, &rc, millisecsTimeout - MQTTClient_elapsed(start));
		Thread_lock_mutex(mqttclient_mutex);
		if (pack == NULL)
			rc = SOCKET_ERROR;
		else
		{
			Connack* connack = (Connack*)pack;
			if ((rc = connack->rc) == MQTTCLIENT_SUCCESS)
			{
				m->c->connected = 1;
				m->c->good = 1;
				m->c->connect_state = 3;
				time(&(m->c->lastContact));
				if (m->c->cleansession)
					rc = MQTTClient_cleanSession(m->c);
				if (m->c->outboundMsgs->count > 0)
				{
					ListElement* outcurrent = NULL;

					while (ListNextElement(m->c->outboundMsgs, &outcurrent))
					{
						Messages* m = (Messages*)(outcurrent->content);
						m->lastTouch = 0;
					}
					MQTTProtocol_retry(m->c->lastContact, 1);
					if (m->c->connected != 1)
						rc = MQTTCLIENT_DISCONNECTED;
				}
			}
			free(connack);
			m->pack = NULL;
		}
	}

	if (rc == SOCKET_ERROR || rc == MQTTCLIENT_PERSISTENCE_ERROR)
	{
		Thread_unlock_mutex(mqttclient_mutex);
		MQTTClient_disconnect(handle, 0); /* not "internal" because we don't want to call connection lost */
		Thread_lock_mutex(mqttclient_mutex);
	}

exit:
	if (m->c->will)
	{
		free(m->c->will);
		m->c->will = NULL;
	}
	Thread_unlock_mutex(mqttclient_mutex);
	FUNC_EXIT_RC(rc);
	return rc;
}
コード例 #17
0
/*
 *  ======== smain ========
 */
Void smain(UArg arg0, UArg arg1)
{
    Int                 status = 0;
    Error_Block         eb;
    SystemCfg_Params    systemCfgP;
    SystemCfg_Handle    systemCfgH = NULL;
    ComputeDevice_Cfg   compDevCfg;


    Log_print0(Diags_ENTRY | Diags_INFO, LOGSTR(2));

    Error_init(&eb);

    /* initialize modules */
    SystemCfg_init();
    ComputeDevice_init();

    /* start all statically created threads now */
    Thread_start(NULL);

    /* turn on some trace */
//  Diags_setMask("SystemCfg+F;ComputeDevice+F");
    Diags_setMask("ComputeDevice+F");
//  Diags_setMask("TsrKnl+F1");

    /* create a SystemCfg instance for the remote processor */
    SystemCfg_Params_init(&systemCfgP);
    systemCfgP.remoteProcName = SystemCfg_Host_ProcName;

    status = SystemCfg_create(&systemCfgP, &systemCfgH);

    if (status < 0) {
        goto leave;
    }

    /* let the host know this processor has started */
    status = SystemCfg_startAck(systemCfgH);

    if (status < 0) {
        Log_error0(LOGSTR(4));
        goto leave;
    }

    /* attach to the remote processor */
    status = SystemCfg_attach(systemCfgH);

    if (status < 0) {
        Log_error0(LOGSTR(5));
        goto leave;
    }

    /* create local resources */
    status = SystemCfg_createResources(systemCfgH);

    if (status < 0) {
        Log_error0(LOGSTR(6));
        goto leave;
    }

    /* wait for handshake before opening remote resources */
    SystemCfg_handshakeEvent(systemCfgH, SystemCfg_EvtCreateDone);

    /* open shared resources from remote core */
    status = SystemCfg_openResources(systemCfgH);

    if (status < 0) {
        Log_error0(LOGSTR(7));
        goto leave;
    }

    /* start the compute device, start the OpenCL Runtime */
    compDevCfg.rcmServerName = SystemCfg_RcmServer_CompDev;
    status = ComputeDevice_start((Ptr)(&compDevCfg));

    if (status < 0) {
        Log_error0(LOGSTR(8));
        goto leave;
    }

    /* send ready event */
    SystemCfg_sendEvent(systemCfgH, SystemCfg_EvtReady);

    /*
     *  BEGIN execute phase
     */

    /* wait for the shutdown signal */
    SystemCfg_waitForEvent(systemCfgH, SystemCfg_EvtShutdown);

    /*
     *  END execute phase
     */

    /* stop the compute device */
    status = ComputeDevice_stop((Ptr)(&compDevCfg));

    if (status < 0) {
        Log_error0(LOGSTR(9));
        goto leave;
    }

    /* close shared resources from remote core */
    status = SystemCfg_closeResources(systemCfgH);

    if (status < 0) {
        Log_error0(LOGSTR(10));
        goto leave;
    }

    /* wait for handshake before deleting local resources */
    SystemCfg_handshakeEvent(systemCfgH, SystemCfg_EvtCloseDone);

    /* delete local resources */
    status = SystemCfg_deleteResources(systemCfgH);

    if (status < 0) {
        Log_error0(LOGSTR(11));
        goto leave;
    }

    /* wait for handshake before terminating */
    SystemCfg_handshakeEvent(systemCfgH, SystemCfg_EvtDone);

#if 0  /* SDOCM85206, SDOCM85207 */
    /* detach from the remote processor */
    status = SystemCfg_detach(systemCfgH);

    if (status < 0) {
        Log_error1(LOGSTR(12), (IArg)status);
        goto leave;
    }

    /* prepare to be stopped */
    SystemCfg_stop(systemCfgH);
#endif

    /* delete SystemCfg instance */
    if (systemCfgH != NULL) {
        SystemCfg_delete(&systemCfgH);
    }

    /* finalize modules */
    ComputeDevice_exit();
    SystemCfg_exit();

leave:
    Log_print1(Diags_EXIT, LOGSTR(3), (IArg)status);
    return;
}