<repo_name>mobangjack/mqtt_led_iot<file_sep>/start_listen_led_cur_state.sh
mosquitto_sub -h mqtt -t 'led/curstate' -v
<file_sep>/io.cpp
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>   //define O_WRONLY and O_RDONLY

void help()
{
	printf("Usage: io <file> <r/w? <value>\n");
}

int main(int argc, char** argv)
{
	if (argc < 3)
	{
		help();
		return -1;
	}

	char* file = argv[1];
	char input = 0;
	char* value = 0;

	if (strcmp(argv[2], "w") == 0)
	{
		if (argc < 4)
		{
			help();
			return -1;
		}
		else
		{
			input = 1;
			value = argv[3];
		}
	}
	else if (strcmp(argv[2], "r") == 0)
	{
		input = 0;
	}
	else
	{
		help();
		return -1;
	}

	int fd = -1;
	if (input)
	{
		fd = open(file, O_WRONLY);
	}
	else
	{
		fd = open(file, O_RDONLY);
	}
	if (fd == -1)
	{
		printf("[ERROR] Cannot open file\n");
		return -1;
	}

	if (input)
	{
		write(fd, value, 1);
		printf("Write val: %c\n", value[0]);
	}
	else
	{
		char buf[2];
		memset(buf, 0, 2 * sizeof(char));
		read(fd, buf, 1);
		printf("Read val: %c\n", buf[0]);
	}
	close(fd);
	fd = -1;
	return 0;
}

<file_sep>/Makefile
CFLAGS=-Wall -ggdb -I../../lib -I../../lib/cpp
LDFLAGS=-L../../lib ../../lib/cpp/libmosquittopp.so.1 ../../lib/libmosquitto.so.1

.PHONY: all clean

all : mqtt_led_new_state_sub

mqtt_led_new_state_sub : main.o mqtt_led_new_state_sub.o
	${CXX} $^ -o $@ ${LDFLAGS}

main.o : main.cpp
	${CXX} -c $^ -o $@ ${CFLAGS}

mqtt_led_new_state_sub.o : mqtt_led_new_state_sub.cpp
	${CXX} -c $^ -o $@ ${CFLAGS}

clean : 
	-rm -f *.o mqtt_led_new_state_sub
<file_sep>/test.cpp
#include "gpio.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char** argv)
{
	if (argc < 2)
	{
		printf("Usage: gpio_read <port>\n");
		return -1;
	}
	gpio line(argv[1]);
	line.wire(O_RDONLY);
	if (!line.isWired())
	{
		printf("[ERROR] Cannot wire gpio\n");
		return -1;
	}

	int hz = 10;
	int us = (1.0 / (double)hz) * 1e6;

	while (1)
	{
		int result = line.stat();
		printf("[INFO] gpio stat: %c\n", result);
		//usleep(us);
		int i = 1e8;
		while(--i);
	}

	line.cutoff();
	return 0;
}
<file_sep>/pub_led_new_state.sh
mosquitto_pub -h mqtt -t 'led/newstate' -m $1
<file_sep>/gpio_write.cpp
#include "gpio.h"
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char** argv)
{
	if (argc < 3)
	{
		printf("Usage: gpio_write <port> <value>\n");
		return -1;
	}
	gpio line(argv[1]);
	line.wire(O_WRONLY);
	if (!line.isWired())
	{
		printf("[ERROR] Unable to wire gpio\n");
		return -1;
	}
	line.assign(argv[2][0]);
	line.cutoff();
	return 0;
}
<file_sep>/mqtt_led_new_state_sub.h
#ifndef MQTT_LED_NEW_STATE_SUB_H
#define MQTT_LED_NEW_STATE_SUB_H

#include <mosquittopp.h>
#include <string>
#include "gpio.h"

class mqtt_led_new_state_sub : public mosqpp::mosquittopp
{
	public:
		mqtt_led_new_state_sub(const char *id, const char *topic, const char *device);
		~mqtt_led_new_state_sub();

		void on_connect(int rc);
		void on_message(const struct mosquitto_message *message);
		void on_subscribe(int mid, int qos_count, const int *granted_qos);
		
	protected:
		std::string m_topic;
		gpio m_led;
};

#endif
<file_sep>/gpio.h
#ifndef GPIO_H
#define GPIO_H

#include <string>

class gpio
{
public:
	gpio(std::string port);
	~gpio();
	bool wire(int flag);
	bool isWired();
	int stat();
	bool assign(char val);
	void cutoff();
protected:
	int fd;
	std::string port;
	bool is_wired;
};

#endif

<file_sep>/mqtt_led_cur_state_pub.h
#ifndef MQTT_LED_CUR_STATE_PUB_H
#define MQTT_LED_CUR_STATE_PUB_H

#include <mosquittopp.h>
#include "gpio.h"

class mqtt_led_cur_state_pub : public mosqpp::mosquittopp
{
	public:
		mqtt_led_cur_state_pub(const char *id, const char *topic, const char *device);
		~mqtt_led_cur_state_pub();

		void on_connect(int rc);
		void on_message(const struct mosquitto_message *message);
		void on_subscribe(int mid, int qos_count, const int *granted_qos);
		void advertise();
		void spin(int hz);
	protected:
		std::string m_topic;
		gpio m_led;
};

#endif
<file_sep>/mqtt_led_cur_state_pub.cpp
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include <unistd.h>
#include "mqtt_led_cur_state_pub.h"
#include <mosquittopp.h>
#include <string.h>

mqtt_led_cur_state_pub::mqtt_led_cur_state_pub(const char *id, const char *topic, const char *device) : mosquittopp(id), m_topic(topic), m_led(device)
{
	m_led.wire(O_RDONLY);
	if (!m_led.isWired())
	{
		printf("[ERROR] Cannot wire led\n");
	}
};

mqtt_led_cur_state_pub::~mqtt_led_cur_state_pub()
{
	if (m_led.isWired())
	{
		m_led.cutoff();
	}
}

void mqtt_led_cur_state_pub::on_connect(int rc)
{
	printf("Connected with code %d.\n", rc);
//	if(rc == 0){
		/* Only attempt to subscribe on a successful connect. */
//		subscribe(NULL, m_topic.c_str());
//	}
}

void mqtt_led_cur_state_pub::on_message(const struct mosquitto_message *message)
{
//	char buf[2];

//	if(!strcmp(message->topic, "led/brightness")){
//		memset(buf, 0, 2*sizeof(char));
		/* Copy N-1 bytes to ensure always 0 terminated. */
//		memcpy(buf, message->payload, 1*sizeof(char));
			
//	}
}

void mqtt_led_cur_state_pub::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
	printf("Subscription succeeded.\n");
}

void mqtt_led_cur_state_pub::advertise()
{
	if (m_led.isWired())
	{
		int state = m_led.stat();
		if (state == -1)
		{
			printf("[WARN] Led stat exception (would not be published)\n");
			return;
		}
		char buf[2];
		memset(buf, 0, 2 * sizeof(char));
		sprintf( buf,"%c",  state);
		publish(NULL, m_topic.c_str(), 2, buf);
	}
}

void mqtt_led_cur_state_pub::spin(int hz)
{
	int us = (1.0 / (double)hz) * 1e6;
	while (1)
	{
		advertise();
		usleep(us);
	}
}

<file_sep>/mqtt_led_cur_state_pub_main.cpp
#include "mqtt_led_cur_state_pub.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

void help()
{
	printf("Usage: mqtt_led_cur_state_pub_main <id> <topic> <device> <host> <port> <keepalive> <hz>\n");
}

int main(int argc, char *argv[])
{
	if (argc < 8)
	{
		help();
		return -1;
	}

	const char* id = argv[1];
	const char* topic = argv[2];
	const char* device = argv[3];
	const char* host = argv[4];
	const int port = atoi(argv[5]);
	const int keepalive = atoi(argv[6]);
	const int hz = atoi(argv[7]);
	mqtt_led_cur_state_pub led_cur_state_pub(id, topic, device);
	led_cur_state_pub.connect(host, port, keepalive);

//	int hz = 1;
//	int us = (1.0 / (double)hz) * 1e6;

//	int rc;

	mosqpp::lib_init();

	led_cur_state_pub.spin(hz);
//	while(1){
//		rc = led_cur_state_pub.loop();
//		if(rc){
//			led_cur_state_pub.reconnect();
//		}
//		led_cur_state_pub.advertise();
//		usleep(us);
//	}

	mosqpp::lib_cleanup();

	return 0;
}

<file_sep>/gpio_read.cpp
#include "gpio.h"
#include <stdio.h>
#include <fcntl.h>

int main(int argc, char** argv)
{
	if (argc < 2)
	{
		printf("Usage: gpio_read <port>\n");
		return -1;
	}
	gpio line(argv[1]);
	line.wire(O_RDONLY);
	if (!line.isWired())
	{
		printf("[ERROR] Cannot wire gpio\n");
		return -1;
	}
	int result = line.stat();
	printf("[INFO] gpio stat: %c\n", result);
	line.cutoff();
	return 0;
}
<file_sep>/gpio.cpp
#include "gpio.h"
#include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>   //define O_WRONLY and O_RDONLY

gpio::gpio(std::string port)
{
	this->fd = -1;
	this->port = port;
	this->is_wired = false;
}

bool gpio::wire(int flag)
{
	fd = open(port.c_str(), flag);
	if (fd != -1)
	{
		is_wired = true;
	}
	else
	{
		is_wired = false;
	}
	return is_wired;
}

bool gpio::isWired()
{
	return this->is_wired;
}

int gpio::stat()
{
	if (!is_wired)
	{
		return -1;
	}
	char buf[2];
	memset(buf, 0, 2 * sizeof(char));
	int len = read(fd, buf, 1);
	if (len != 1)
	{
		return -1;
	}
	lseek(fd, 0, SEEK_SET); // important!!!
	return buf[0];
}

bool gpio::assign(char val)
{
	if (!is_wired)
	{
		return false;
	}
	int len = write(fd, &val, 1);
	if (len != 1)
	{
		return false;
	}
	return true;
}

void gpio::cutoff()
{
	if (fd != -1)
	{
		close(fd);
		fd = -1;
	}
	is_wired = false;
}

gpio::~gpio()
{
	cutoff();
}

<file_sep>/mqtt_led_new_state_sub.cpp
#include <cstdio>
#include <cstring>
#include <fcntl.h>
#include "mqtt_led_new_state_sub.h"
#include <mosquittopp.h>

mqtt_led_new_state_sub::mqtt_led_new_state_sub(const char *id, const char *topic, const char *device) : mosquittopp(id), m_topic(topic), m_led(device)
{
	m_led.wire(O_WRONLY);
	if (!m_led.isWired())
	{
		printf("[ERROR] Cannot wire led\n");
	}
};

mqtt_led_new_state_sub::~mqtt_led_new_state_sub()
{
	if (m_led.isWired())
	{
		m_led.cutoff();
	}
}

void mqtt_led_new_state_sub::on_connect(int rc)
{
	printf("Connected with code %d.\n", rc);
	if(rc == 0){
		/* Only attempt to subscribe on a successful connect. */
		subscribe(NULL, m_topic.c_str());
	}
}

void mqtt_led_new_state_sub::on_message(const struct mosquitto_message *message)
{
	char buf[2];

	if(!strcmp(message->topic, m_topic.c_str())){
		memset(buf, 0, 2*sizeof(char));
		/* Copy N-1 bytes to ensure always 0 terminated. */
		memcpy(buf, message->payload, 1*sizeof(char));
		bool success = false;
		if (m_led.isWired())
		{
			success = m_led.assign(buf[0]);
			printf("[INFO] Assign led to %c\n: %d", buf[0], success);
		}
		else
		{
			printf("[WARN] Led not wired, io operation was not available\n");
		}
	}
}

void mqtt_led_new_state_sub::on_subscribe(int mid, int qos_count, const int *granted_qos)
{
	printf("Subscription succeeded.\n");
}

