Ejemplo n.º 1
0
static void *MqttHal_Thread(void *arg) {
	//int tmp = (int *)arg;
	int rc;
	
	while(UpdateCbf_IsSet() != 0) {
		//ADV_WARN("Callback function is not registered.\n");
		sleep(1);
	}
	ADV_INFO("Callback function registered.\n");
	
	MqttHal_Init();
	rc = mqtt_client_connect(g_mosq, &g_mosq_cfg);
	if(rc) {
		ADV_WARN("MQTT Connect Fail(%d).\n", rc);
	} else {
		ADV_INFO("MQTT Client start.\n");
		//printf("MQTT Client start.\n");
		mosquitto_loop_forever(g_mosq, -1, 1);
		ADV_INFO("MQTT Client leave.\n");
		//printf("MQTT Client leave.\n");
	}
	MqttHal_Uninit();

	pthread_exit(NULL);
}
static int InitializeConnection(PMQTTTRANSPORT_HANDLE_DATA transportState, bool initialConnection)
{
    int result = 0;
    if (!transportState->connected && !transportState->destroyCalled)
    {
        // Construct SAS token
        size_t secSinceEpoch = (size_t)(difftime(get_time(NULL), EPOCH_TIME_T_VALUE) + 0);
        size_t expiryTime = secSinceEpoch + SAS_TOKEN_DEFAULT_LIFETIME;

        // Not checking the success of this variable, if fail it will fail in the SASToken creation and return false;
        STRING_HANDLE emptyKeyName = STRING_new();
        STRING_HANDLE sasToken = SASToken_Create(transportState->device_key, transportState->sasTokenSr, emptyKeyName, expiryTime);
        if (sasToken == NULL)
        {
            result = __LINE__;
        }
        else
        {
            MQTT_CLIENT_OPTIONS options = { 0 };
            options.clientId = (char*)STRING_c_str(transportState->device_id);
            options.willMessage = NULL;
            options.username = (char*)STRING_c_str(transportState->configPassedThroughUsername);
            options.password = (char*)STRING_c_str(sasToken);
            options.keepAliveInterval = DEFAULT_MQTT_KEEPALIVE;
            options.useCleanSession = false;
            options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE;

            // construct address
            const char* hostAddress = STRING_c_str(transportState->hostAddress);
            const char* hostName = strstr(hostAddress, "//");
            if (hostName == NULL)
            {
                hostName = hostAddress;
            }
            else
            {
                // Increment beyond the double backslash
                hostName += 2;
            }

            transportState->xioTransport = getIoTransportProvider(hostName, transportState->portNum);
            if (mqtt_client_connect(transportState->mqttClient, transportState->xioTransport, &options) != 0)
            {
                LogError("failure connecting to address %s:%d.\r\n", STRING_c_str(transportState->hostAddress), transportState->portNum);
                result = __LINE__;
            }
            else
            {
                transportState->connected = true;
                result = 0;
            }
            STRING_delete(emptyKeyName);
            STRING_delete(sasToken);
        }
    }
    return result;
}
Ejemplo n.º 3
0
void mqtt_client_sample_run()
{
    if (platform_init() != 0)
    {
        PrintLogFunction(LOG_LINE, "platform_init failed");
    }
    else
    {
        MQTT_CLIENT_HANDLE mqttHandle = mqtt_client_init(OnRecvCallback, OnOperationComplete, NULL, PrintLogFunction);
        if (mqttHandle == NULL)
        {
            PrintLogFunction(LOG_LINE, "mqtt_client_init failed");
        }
        else
        {
            MQTT_CLIENT_OPTIONS options = { 0 };
            options.clientId = "azureiotclient";
            options.willMessage = NULL;
            options.username = NULL;
            options.password = NULL;
            options.keepAliveInterval = 10;
            options.useCleanSession = true;
            options.qualityOfServiceValue = DELIVER_AT_MOST_ONCE;

            SOCKETIO_CONFIG config = {"test.mosquitto.org", PORT_NUM_UNENCRYPTED, NULL};

            XIO_HANDLE xio = xio_create(socketio_get_interface_description(), &config, PrintLogFunction);
            if (xio == NULL)
            {
                PrintLogFunction(LOG_LINE, "xio_create failed");
            }
            else
            {
                if (mqtt_client_connect(mqttHandle, xio, &options) != 0)
                {
                    PrintLogFunction(LOG_LINE, "mqtt_client_connect failed");
                }
                else
                {
                    do
                    {
                        mqtt_client_dowork(mqttHandle);
                    } while (g_continue);
                }
                xio_close(xio, OnCloseComplete, NULL);
            }
            mqtt_client_deinit(mqttHandle);
        }
        platform_deinit();
    }

#ifdef _CRT_DBG_MAP_ALLOC
    _CrtDumpMemoryLeaks();
#endif
}
static int create_connection(PROV_TRANSPORT_MQTT_INFO* mqtt_info)
{
    int result;
    MQTT_CLIENT_OPTIONS options = { 0 };
    char* username_info;

    if ((username_info = construct_username(mqtt_info)) == NULL)
    {
        LogError("Failure creating username info");
        result = __FAILURE__;
    }
    else if (construct_transport(mqtt_info))
    {
        LogError("Failure constructing transport");
        free(username_info);
        result = __FAILURE__;
    }
    else
    {
        (void)mqtt_client_set_trace(mqtt_info->mqtt_client, mqtt_info->log_trace, false);

        options.username = username_info;
        options.clientId = mqtt_info->registration_id;
        options.useCleanSession = 1;
        options.log_trace = mqtt_info->log_trace;
        options.qualityOfServiceValue = DELIVER_AT_LEAST_ONCE;
        if (mqtt_client_connect(mqtt_info->mqtt_client, mqtt_info->transport_io, &options) != 0)
        {
            xio_destroy(mqtt_info->transport_io);
            mqtt_info->transport_io = NULL;
            LogError("Failure connecting to mqtt server");
            result = __FAILURE__;
        }
        else
        {
            result = 0;
        }
        free(username_info);
    }
    return result;
}
Ejemplo n.º 5
0
int main(void)
{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  /* MCU Configuration----------------------------------------------------------*/

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_LWIP_Init();
  MX_USART1_UART_Init();
  MX_USB_DEVICE_Init();

  /* USER CODE BEGIN 2 */
  printf("Application startup successful....\n");
  httpd_init();

  ip4_addr_t ip_addr;

  IP4_ADDR(&ip_addr, 192, 168, 1, 1);

  err_t err;

  // Setup an empty client info structure
  struct mqtt_connect_client_info_t ci;
  memset(&ci, 0, sizeof(ci));

  ci.client_id = "lwip_test";

  err = mqtt_client_connect(&client, &ip_addr, MQTT_PORT, mqtt_connection_cb, 0, &ci);

    // For now just print the result code if something goes wrong
    if(err != ERR_OK) {
      printf("mqtt_connect return %d\n", err);
    }

   uint8_t published=0;

  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
	  MX_LWIP_Process();

	  if(mqtt_client_is_connected(&client) && !published)
	  {
		  example_publish(&client, "Hallo Welt!");
		  published=1;
	  }

  /* USER CODE END WHILE */

  /* USER CODE BEGIN 3 */

  }
  /* USER CODE END 3 */

}
Ejemplo n.º 6
0
Archivo: main.c Proyecto: wuwx/simba
static int test_connect(struct harness_t *harness_p)
{
    BTASSERT(mqtt_client_connect(&client) == 0);

    return (0);
}