1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
   |  
/*
 
#include "thread.h"
 
// constants
#define GPIO_FN_MAXLEN  32
#define POLL_TIMEOUT    10000
#define RDBUF_LEN       5
 
 
 
// monitoring thread data structure
struct monitor_data{
   int  thread_id;
   int  pin;
   int  edgePin;
   int  lastKnownState;
   int  running;
};
 
struct monitor_data monitor_data_array[21];
 
pthread_t threads[21];
 
 
Thread::Thread()
{
 
}
 
Thread::~Thread()
{
 
}
 
 void* Interrupt::monitorPinInterrupt(void *threadarg)
{
 
// obtain the monitoring data structure from the thread argument
 
        struct monitor_data *monitorData;
 	monitorData = (struct monitor_data *) threadarg;
 
 
	// cache a local pin value variabl
	int pin = monitorData->pin;
	int edgePin = monitorData->edgePin;
 
	printf("\nNATIVE (GpioInterrupt) MONITORING PIN %d @ EDGE %d\n", pin, edgePin);
 
    // monitoring instance variables
	char fn[GPIO_FN_MAXLEN];
	int fd,ret;
	struct pollfd pfd;
	char rdbuf[RDBUF_LEN];
 
	// allocate memory
	memset(rdbuf, 0x00, RDBUF_LEN);
	memset(fn, 0x00, GPIO_FN_MAXLEN);
 
	// attempt to access the pin state from the linux sysfs
	// (each GPIO pin value is stored in file: '/sys/class/gpio/gpio#/value' )
	snprintf(fn, GPIO_FN_MAXLEN-1, "/sys/class/gpio/gpio%d/value", edgePin);
	fd=open(fn, O_RDONLY);
	if(fd<0)
	{
		// return error; unable to get file descriptor
		// (this is likely because the pin has not been exported)
		perror(fn);
	//	return 2;
 
	}
 
	// set polling config structure
	pfd.fd=fd;
	pfd.events=POLLPRI; // High priority data may be read.
 
	// attempt to read the pin state from the linux sysfs
	ret=read(fd, rdbuf, RDBUF_LEN-1);
	if(ret<0)
	{
		// return error; unable to read the data file
		// (this is likely because the user has insufficient permissions)
		perror("read()");
	//	return 4;
	}
 
	printf("value is: %s\n", rdbuf);
 
	// set the running state of the instance monitor data structure
	monitorData->running = 1;
 
	// initialize last known value and cache the value as the last known state
	int compareLastKnown = strncmp(rdbuf, "1", 1); // only compare the first character; rdbuff may have more junk chars
	monitorData->lastKnownState = compareLastKnown;
 
	// continuous thread loop
	for(;;)
	{
		// clear/reset the data buffer
		memset(rdbuf, 0x00, RDBUF_LEN);
 
		// seek to the fist position in the data file
		lseek(fd, 0, SEEK_SET);
 
		// wait for data to be written to the GPIO value file
		// (timeout every 10 seconds and restart)
		ret=poll(&pfd, 1, POLL_TIMEOUT);
 
		// if the return value is less than '0' then
		// an error was thrown; bail out of the thread
		if(ret<0)
		{
			perror("poll()");
			close(fd);
		//	return 6;
		}
 
		// if the return value is equal to '0' then
		// the polling simply timed out and we can restart
		else if(ret==0)
		{
			printf("timeout\n");
			continue;
		}
 
		// if the return value is greater than '0' then
		// a change to the GPIO data file occurred
		else
		{
			// read the data from the file into the data buffer
			ret=read(fd, rdbuf, RDBUF_LEN-1);
			if(ret<0)
			{
				// data read error
				perror("read()");
				continue;
			}
	                printf("interrupt, value is: %s\n", rdbuf);
 
			// compare the data in the data buffer with the last known value state
			// (we do this to prevent double event invocation for the same value)
			int compareResult = strncmp(rdbuf, "1", 1); // only compare the first character; rdbuff may have more junk chars
 
 
			Thread* cptr = (Thread*)threadarg;
			cptr->signaldata.emit();
 
 
		}
	}
 
	// if we reached this code (unlikely),
	// then close the data file and exit the thread
	close(fd);
	return 0;
}
 
 
 
int Thread::Thread_enableChangeCallback (int pin)
{
 
	// get the index position for the requested pin number
	int index = pin;
	int edgePin = pin;
 
	// ensure that the requested pin index is valid
	if(index >= 0 && edgePin >= 0)
	{
		// only start this thread monitor if it has not already been started
		if(monitor_data_array[index].running <= 0)
		{
			// configure the monitor instance data
			monitor_data_array[index].thread_id = index;
			monitor_data_array[index].pin = pin;
			monitor_data_array[index].edgePin = edgePin;
 
			// create monitoring instance thread
 
			pthread_create(&threads[index], NULL,monitorPinInterrupt,static_cast<void*>(&monitor_data_array[index]));
			return 1;
		}
 
		// return '0' when no action was taken;
		// (monitor already running)
 
		return 0;
 
	}
 
	// return '-1' on error; not a valid pin
	return -1;
}
 
int Thread::Thread_disableChangeCallback (int pin)
{
	// get the index position for the requested pin number
	int index = pin;
 
	// ensure that the requested pin index is valid
	if(index >= 0)
	{
		// kill the monitoring thread
		if(monitor_data_array[index].running > 0)
		{;
			pthread_cancel(threads[index]);
 
			// return '1' when a thread was actively killed
			return 1;
		}
 
		// return '0' when no action was taken
		// (monitor is not currently active/running)
		return 0;
	}
 
	// return '-1' on error; not a valid pin
	return -1;
} | 
Partager