Ejemplo n.º 1
0
int _tmain(int argc, _TCHAR* argv[])
{
	char machine_id[33];
	GetMachineID(machine_id);
	std::cout << machine_id<< std::endl;
	getchar();
	return 0;
}
/*******************************************************************************
*	MQTT Callback Handler
*
*	Is called when the LEd status changes ()
********************************************************************************/
int MQTTcallbackHandler(MQTTCallbackParams params)
{
	IoT_Error_t rc = NONE_ERROR;

	int ret;
	bool des_state, rep_state;
	int payload_len = (int)params.MessageParams.PayloadLen;
	char* payload = (char*)params.MessageParams.pPayload;

	//Receive desired LED state in payload
	INFO("Subscribe callback");
	INFO("%.*s\t%.*s",
				(int)params.TopicNameLen, params.pTopicName,
				(int)params.MessageParams.PayloadLen, (char*)params.MessageParams.pPayload);

	//Read the desired state from message
	ret = MSG_GetDesiredState( payload_len, payload, &des_state);
	if(ret !=0)
		goto JSON_ERROR;

	//Update current LED- and GPIO- state to the desired state
	INFO("Updating state: %d\n",des_state);
	SetLEDState(UserLED_4, des_state);
	Write_GPIO(GPIO12, des_state);

	//Write response message
	char msg_payload[MAX_PAYLOAD];
	payload = msg_payload;
	payload_len = MAX_PAYLOAD;

	LED_State led_state;
	GetLEDState(UserLED_4, &led_state);
	ret = MSG_SetReportedState(  payload_len, payload, (int)led_state);
	if(ret !=0)
		goto JSON_ERROR;

	char topic[512];
        sprintf(topic, led_state_pub_topic, GetMachineID());
	printf("Sending payload: %s", payload);
	rc = MQTT_Send_Message(topic, payload, strlen(payload) );
	if (NONE_ERROR != rc)
		ERROR("Could not publish new LED state to topic: %s", topic );

	return 0;

JSON_ERROR:
	ERROR("JSON format Error! %s", (char*)params.MessageParams.pPayload);
	return -1;
}
Ejemplo n.º 3
0
		bool ProductInfo::CheckEnable()const
		{
			MojingDeviceParameters *pDeviceParameters = Manager::GetMojingManager()->GetParameters()->GetDeviceParameters();
#if defined(MJ_OS_ANDROID)
			const char* szOS = "Android";
#elif defined(MJ_OS_MAC)
			const char* szOS = "iOS";
#elif defined(MJ_OS_WIN32)
			const char* szOS = "Windows";
#endif
			MachineListNode CurrentMachineType = pDeviceParameters->GetCurrentMachine();
			bool bCheckOS = szOS == NULL || *szOS == 0 || m_szOS.GetLength() < 1 || 0 == MJ_stricmp(szOS , m_szOS.ToCStr());

			bool bCheckMachine = GetMachineID() == CurrentMachineType.m_iID;
			bool bRet = bCheckOS && bCheckMachine;
			return bRet;
		}
/*******************************************************************************
*	Main
*
********************************************************************************/
int main(int argc, char** argv)
{
	IoT_Error_t rc = NONE_ERROR;
	int32_t i = 0;
	int ret =0;

	char rootCA[PATH_MAX + 1];
	char clientCRT[PATH_MAX + 1];
	char clientKey[PATH_MAX + 1];
	char CurrentWD[PATH_MAX + 1];

	char cafileName[] = "/rootCA.crt";
	char clientCRTName[] = "/aws.crt";
	char clientKeyName[] = "/aws.key";

	char* thingID;

	//
	//Register ctrl-c handler
	//
	signal(SIGINT, CTRL_C_Handler);

	//
	//Parse Input-parameters
	//
	parseInputArgsForConnectParams(argc, argv);

	//
	// Get the ThingID/MachineID
	//
	thingID = GetMachineID();

	//
	// Export GPIO12 for LED output
	//
	ret = Export_GPIO(GPIO12); 	//Export GPIO12 for LED output
	if (ret != 0) {
		ERROR("Could not export LED GPIO");
	}

	//
	//Register Event-handler for Vol+/- button
	//
	ret = RegisterEventHandler(VOL_UP_EVENT, On_VolUp_ButtonPress, (void*) thingID);
	if (ret != 0) {
		ERROR("Could not register EventHandler");
	}

	ret = RegisterEventHandler(VOL_DOWN_EVENT, On_VolDown_ButtonPress,(void*) thingID);
	if (ret != 0) {
		ERROR("Could not register EventHandler");
	}


	//
	//Create the mainloop for polling the button events
	//
	loop = g_main_loop_new( NULL, false );
	if(!loop) {
		ERROR("Could not Create Main loop");
		return -1;
	}

	//
	//Setting path to private key and certificates
	//
	sprintf(rootCA, "%s%s", certDirectory, cafileName);
	sprintf(clientCRT, "%s%s", certDirectory, clientCRTName);
	sprintf(clientKey, "%s%s", certDirectory, clientKeyName);

	INFO("ThingID:       %s", thingID);
	INFO("AWS IoT SDK:   %d.%d.%d-%s", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
	INFO("rootCA:        %s", rootCA);
	INFO("clientCRT:     %s", clientCRT);
	INFO("clientKey:     %s\n", clientKey);

	struct stat reqFileStat;
	if (stat(rootCA, &reqFileStat) < 0 || stat(clientCRT, &reqFileStat) < 0 ||
	    stat(clientKey, &reqFileStat) < 0)
	{
		ERROR("Root certificate and client certificate and key MUST be present.");
		exit(1);
	}

	//
	// Connect MQTT client
	//
	INFO("Connecting to %s:%d", HostAddress, port);
	rc = MQTT_Connect(HostAddress,port, thingID, rootCA, clientCRT, clientKey);
	if (NONE_ERROR != rc) {
		ERROR("Error[%d] connecting to %s:%d", rc, HostAddress, port);
	}

	//
	// Subscribe to LED status-changes topic
	//
        char topic[512];
        sprintf(topic, led_state_sub_topic, thingID);
	INFO("Subscribing to topic:%s", topic);
	rc = MQTT_Subscribe(topic, QOS_0, MQTTcallbackHandler);
	if (NONE_ERROR != rc) {
		ERROR("Error[%d] subscribing to topic: %s", rc, led_state_sub_topic);
	}


	//iot_mqtt_yield(1000); 	//TODO: clarify

	//
	//Hook in  a function into main loop that calls iot_mqtt_yield in regular intervals
	//
	g_timeout_add(1000, timer_func, 0);



	//
	//start the main loop
	//This call is blocking until the main loop is exited with a call to g_main_loop_quit(loop)
	//from the CTRL-C handler;
	INFO("Entering main-loop, please press ctrl-c to quit the demo-app:");
	g_main_loop_run( loop );	


	INFO("Cleaning up application ...");
	
	//Unsubscribe from Topics

	//Disconnect MQTT connection

	//Unregister GPIO-EventHandlers

	//UnExport GPIO's

	//Destroy main loop
	if(loop)
		g_main_loop_unref(loop);

	return rc;
}