+#include "FWSN.h"
+
#include <stdio.h>
#include <stdlib.h>
#include <stdio.h>
#include <semaphore.h>
#include "serialsource.h"
-//#include "FWSN.h"
-
-
-#define SLOT_TIME 128
-#define MAX_DATA_LENGTH 64
-#define DEFAULT_PACKET_ADD_DEVICE 0x00, 0xff, 0xff, 0x00, 0x00, 0x02, 0x00, 0x07, 0xc2, 0x02, 0xc5, 0x00
-#define DEFAULT_PACKET_REMOVE_DEVICE 0x00, 0xff, 0xff, 0x00, 0x00, 0x02, 0x00, 0x07, 0xc2, 0x02, 0x5C, 0x00
-#define PACKET_LENGTH 12
-#define DELAY_BETWEEN_SENDS 3000
-#define MAXIMUM_REPEAT_COUNTER 5
-#define MIN_COUNTER_TO_APPLAY_CHANGE 2
-#define BASESTATION_ID 01
int frameLength; //WSN frame length, readed form the network
int numberOfSlots; //number of assigned slots in WSN, readed form the network
-int slotTime; // time of the TDMA slot in WSN
+int slotTime; // time of the TDMA slot in WSN (from DEFINE)
unsigned char data[MAX_DATA_LENGTH]; //las read data
int dataLength;
int dataValid;
-
-
-sem_t applayAdd; //there is a demand to add a ne node
-unsigned char applayRemove; //there is a demand to remove a ne node
+// counters for maximum number of add/remove attemps.
+int RepeatCounter_overPeriod; // counts number of used period to add/remove a device. (lost of the communication chanel)
+int communication_error; // information, that maximum counter has been achieved without adding/removing the node
+
+
+sem_t actionLock; //there is a demand to add or remove a node
+sem_t waitAdd; //wait until the action is finished
+sem_t waitRemove; //wait until the action is finished
+// there is a demand to add/remove a node
+int applayAdd;
+int applayRemove;
+//unsigned char applayRemove; //there is a demand to remove a ne node
unsigned char addDeviceID; //node ID
unsigned char remDeviceID; //node ID
pthread_t init(void *argv)
{
pthread_t threadRead;
-
+ //initialize semaphors for add/remove node control
+ sem_init(&waitAdd, 0, 0);
+ sem_init(&waitRemove, 0, 0);
+ sem_init(&actionLock, 0, 1);
+ // start the reading thread
pthread_create( &threadRead, NULL, readData, argv);
return threadRead;
}
unsigned char packet[] = {DEFAULT_PACKET_ADD_DEVICE};
int sendResult;
int repeatCount =0;
-
+ // write node ID in to the packet
packet[11] = id;
-
+ // send packet
sendResult = write_serial_packet(src, packet, PACKET_LENGTH);
-
- //TODO
- {
- int i;
- for (i = 0; i < PACKET_LENGTH; i++)
- printf("%02x ", packet[i]);
- putchar('\n');
- }
-
-
+ // repeat if not sent
while ((sendResult != 0) && (repeatCount < MAXIMUM_REPEAT_COUNTER))
{
usleep(DELAY_BETWEEN_SENDS);
sendResult = write_serial_packet(src, packet, PACKET_LENGTH);
- /*
- if (sendResult == 0)
- printf("ack\n");
- else
- printf("noack\n");
- */
repeatCount++;
}
if (sendResult ==0)
unsigned char packet[] = {DEFAULT_PACKET_REMOVE_DEVICE};
int sendResult;
int repeatCount =0;
-
+ // write node ID in to the packet
packet[11] = id;
-
+ // send packet
sendResult = write_serial_packet(src, packet, PACKET_LENGTH);
- //TODO
- {
- int i;
- for (i = 0; i < PACKET_LENGTH; i++)
- printf("%02x ", packet[i]);
- putchar('\n');
- }
-
- /*
- if (sendResult == 0)
- printf("ack\n");
- else
- printf("noack\n");
- */
+ // repeat if not sent
while ((sendResult != 0) && (repeatCount < MAXIMUM_REPEAT_COUNTER))
{
usleep(DELAY_BETWEEN_SENDS);
for(;;)
{
int len, dataLengthOld;
- const unsigned char *packet = read_serial_packet(src, &len);
+ const unsigned char *packet = read_serial_packet(src, &len); //read the data form the network
if (!packet)
- exit(0);
-
-
+ exit(0);
dataLengthOld = dataLength;
dataLength = 0;
- if ((len > 15) && ((len-15)%4 == 1) && (packet[2] == 0x7E))
+ if ((len > 15) && ((len-15)%4 == 1) && (packet[2] == 0x7E)) //parse the received data
{
memcpy(data,&packet[15],len - 15);
- dataLength = len - 15; //minus the head of the message and -1 byte for number of slots information, at the end of package.
-
- frameLength = packet[12];
-
- slotTime = SLOT_TIME;
-
- numberOfSlots = packet[len-1];
-
+ dataLength = len - 15; //minus the head of the message and -1 byte for number of slots information, at the end of package.
+ frameLength = packet[12];
+ slotTime = SLOT_TIME * 2;
+ numberOfSlots = packet[len-1];
dataValid = 1;
}
else
dataValid = 0;
}
- //TODO
- if (dataLength > 50)
- { int i;
- printf("dlouha data 1 %i \n",len);
-
- for (i = 0; i < len; i++)
- printf("%02x ", packet[i]);
- putchar('\n');
-
- exit(0);
- }
-
//check if the device has been added
- if (sem_trywait(&applayAdd) == 0)
- {
- sem_post(&applayAdd);
- }
- else
- {
+ if (applayAdd == 1) // if there is a demand to add a new device, check if it has been allready added.
+ {
int i;
- for (i=0;i<dataLength;i=i+4)
+ for (i=0;i<dataLength;i=i+4) //has been added?
{
if (data[i+1] == addDeviceID)
- sem_post(&applayAdd);
+ applayAdd = 0;
+ }
+ if (RepeatCounter_overPeriod > MAXIMUM_REPEAT_COUNTER_OVER_PERIOD && applayAdd == 1) // check the repeat counter
+ {
+ communication_error = 1;
+ applayAdd = 0;
}
+ if (applayAdd == 0) // free the demand
+ sem_post(&waitAdd);
+ else
+ RepeatCounter_overPeriod++;
+
+
}
-
+
//check if the device has been removed
- if (applayRemove ==1)
- {
- int i;
+ if (applayRemove == 1) // if there is a demand to remove a device, check if it has been allready removed.
+ {
+ int i;
int applayRemoveLocal = 0;
- for (i=0;i<dataLength;i=i+4)
+ for (i=0;i<dataLength;i=i+4) // has been removed?
{
if (data[i+1] == remDeviceID)
applayRemoveLocal = 1;
}
- applayRemove = applayRemoveLocal;
+ if (RepeatCounter_overPeriod > MAXIMUM_REPEAT_COUNTER_OVER_PERIOD && applayRemoveLocal == 1) // check the counter
+ {
+ communication_error = 1;
+ applayRemoveLocal = 0;
+ }
+ if (applayRemoveLocal == 0) //free the demand
+ {
+ applayRemove = 0;
+ sem_post(&waitRemove);
+ }
+ else
+ RepeatCounter_overPeriod++;
}
free((void *)packet);
usleep(DELAY_BETWEEN_SENDS);
- if (sem_trywait(&applayAdd) == 0)
- {
- sem_post(&applayAdd);
- }
- else
+ // if there is a demand to add a device, send the control package
+ if (applayAdd == 1)
{
sendAddDevice(src, addDeviceID);
usleep(DELAY_BETWEEN_SENDS);
}
- if(applayRemove == 1)
- {
+ // if there is a demand to remove a device, send the control package
+ if (applayRemove == 1)
+ {
sendRemoveDevice(src, remDeviceID);
- }
+ usleep(DELAY_BETWEEN_SENDS);
+ }
}
return NULL;
}
int isFreeSlot(int maxResponseTime)
{
int maxFrameLength;
- int frameLength;
-
- maxFrameLength = floor((float)maxResponseTime/(float)slotTime);
-
+ int queuedDev;
+ // compute the demanded framelength of the ITEM according the demanded maximum commuication delay.
+ maxFrameLength = floor((float)maxResponseTime/(float)slotTime);
if (maxFrameLength < 4) return -1;
if (maxFrameLength >= 8) maxFrameLength = 8;
else
maxFrameLength = 4;
+ // check if there is no adding demand in process
+ if (applayAdd == 1)
+ queuedDev = 1; // one node is waiting in the queue to bee added.
+
+
- if (numberOfSlots+1 < maxFrameLength)
+ if (numberOfSlots + 1 + queuedDev < maxFrameLength)
{ // number of devices is smaller than the number of slots in the demanded period
if (frameLength <= maxFrameLength) return 1; //and the distribution of assigned slots is the way, that the actual period is smaller then the demanded one
else return 0; //wrong distribution of slots, try it after a while, (after an automatic system reconfiguration)
return -1; // not posible to add a new device.
}
+
+//*************************************************************************************************************************
+/*
+returns -1, the demanded period is higher than the actual one
+returns -2, node adding in process, not posiyble to determine the number of free slots
+returns 0, no free slot remains
+returns X, there is X free slots
+*/
+
+int noOfFreeSlots(int maxResponseTime)
+{
+ int maxFrameLength;
+ int freeSlots;
+ // check if there is no adding demand in the process
+ if (applayAdd == 1)
+ return -2; // node adding in process, not posiyble to determine the number of free slots
+
+
+ // compute the demanded framelength of the ITEM according the demanded maximum commuication delay.
+ maxFrameLength = floor((float)maxResponseTime/(float)slotTime);
+ if (maxFrameLength < 4) return -1;
+ if (maxFrameLength >= 8) maxFrameLength = 8;
+ else
+ maxFrameLength = 4;
+
+ if (frameLength > maxFrameLength) return -1; // actual period is higher than the demanded one.
+ freeSlots = maxFrameLength;// - numberOfSlots -1; //nubmer of free slots
+
+ return freeSlots; // !!!!!!!!!! number of free slots in the frame length. Does not depend on number of ocupied slots.
+}
+
+
+
//*************************************************************************************************************************
/*
-Try to add the device "id", if the maximum response time is smaller that "responseTime".
-returns 1 if the device is possible to add.
+Try to add the device "id".
+returns 1 if the device has been added.
returns -1 if not.
-returns 0 if a proces of adding some node is curently running. (try later)
*/
-int addDevice ( unsigned char id, int responseTime)
+int addDevice ( unsigned char id)
{
- int freeResponse;
- freeResponse = isFreeSlot(responseTime);
+ sem_wait(&actionLock); // lock the control section, or wait for free access.
+ // reset the counter and the error flag
+ RepeatCounter_overPeriod = 0;
+ communication_error = 0;
+ //strat the task
+ addDeviceID = id;
+ applayAdd = 1;
+ //wait until the node is added
+ sem_wait(&waitAdd);
+ sem_post(&actionLock);
- if ((freeResponse == 1))// && (applayAdd == 0))// && (counterApplay > MIN_COUNTER_TO_APPLAY_CHANGE))
- {
- sem_wait(&applayAdd);
- addDeviceID = id;
- return 1;
- }
- else return -1; //no free space, or wrong slot distribution or adding is curently running,
+ if (communication_error == 1)
+ return -1;
+ return 1;
}
//*************************************************************************************************************************
/*
-Try to remove the device "id", if the maximum response time is smaller that "responseTime".
-returns 1 if the device is possible to add.
+Try to remove the device "id".
+returns 1 if the device has been removed
returns -1 if not.
-returns 0 if a proces of romeveing some node is curently running. (try later)
returns -2 if "id" is basestation (it is not possible to remove the basestation)
*/
int removeDevice (unsigned char id)
{
- if (id == BASESTATION_ID) return -2;
- if (applayRemove == 1) return 0;
- if ( (applayRemove == 0))// && (counterApplay > MIN_COUNTER_TO_APPLAY_CHANGE))
- {
- remDeviceID = id;
- applayRemove = 1;
- return 1;
- }
- else return -1; //no free space, or wrong slot distribution or adding is curently running,
+ if (id == BASESTATION_ID) return -2;
+
+ sem_wait(&actionLock); //lock the control section or wait
+ //reset the counter and the communication error flag
+ RepeatCounter_overPeriod = 0;
+ communication_error = 0;
+ //strat the action
+ remDeviceID = id;
+ applayRemove = 1;
+ //wait untkil the node is removed
+ sem_wait(&waitRemove);
+ sem_post(&actionLock);
+
+ if (communication_error == 1)
+ return -1;
+ return 1;
}
//*************************************************************************************************************************
/*
returns the data length.
-dat has to be an array of size "MAX_DATA_LENGTH".
-if dat == NULL, the function returns just the data length. (does not save data)
+and the data arrauy in dat.
+returns 0 if there are no available data.
*/
-int getData(unsigned char *dat)
+int getData(unsigned char **dat)
{
- if (dat == NULL) return dataLength;
if ((dataLength > 0) && (dataValid == 1))
{
- memcpy(dat,data,dataLength);
+ *dat = (unsigned char *) malloc(dataLength);
+ if (*dat == NULL) exit(-12);
+ memcpy(*dat,data,dataLength);
return dataLength;
}
else
{
+ *dat = NULL;
return 0;
}
}
//*************************************************************************************************************************
//*************************************************************************************************************************
-
-void printdata()
-{
- unsigned char dat[MAX_DATA_LENGTH];
- int len,i;
-
- len = getData(dat);
-
- for (i = 0; i < len-1; i++)
- {
- printf("%02x ", dat[i]);
- if (i%4 == 3)
- printf(" | ");
- }
- putchar('\n');
-}
-//*************************************************************************************************************************
-void mysleep(int time)
-{
- int i;
- for(i=0;i<time;i++)
- {
- sleep(1);
- printdata();
- }
-}
-
-//*************************************************************************************************************************
-
-int main(int argc, char **argv)
-{
- pthread_t threadRead;
- sem_init(&applayAdd, 0, 1);
-
- if (argc != 3)
- {
- fprintf(stderr, "Usage: %s <device> <rate> - dump packets from a serial port\n", argv[0]);
- exit(2);
- }
-
- threadRead = init(argv);
-
- for (;;)
- {
-
-//ADDing of the nodes.
-
- printdata();
- mysleep(10);
- printf("addDevice(0x02,600) \n");
- if (addDevice(0x02,600) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
- printdata();
- mysleep(10);
- printf("addDevice(0x03,500) \n");
- if (addDevice(0x03,500) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-
- printdata();
- mysleep(10);
- printf("addDevice(0x03,1500) \n");
- if (addDevice(0x03,1500) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-
- printdata();
- mysleep(10);
- printf("addDevice(0x04,800) \n");
- if (addDevice(0x04,800) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-
- printdata();
- mysleep(10);
- printf("addDevice(0x05,1500) \n");
- if (addDevice(0x05,1500) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-
- printdata();
- mysleep(10);
- printf("addDevice(0x04,2000) \n");
- if (addDevice(0x04,2000) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-
- printdata();
- mysleep(10);
- printf("addDevice(0x06,2000) \n");
- if (addDevice(0x06,2000) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-//remove 2 nodes
- printdata();
- mysleep(10);
- printf("removeDevice(0x04) \n");
- if (removeDevice(0x04) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
-
-
- printdata();
- mysleep(10);
- printf("removeDevice(0x05) \n");
- if (removeDevice(0x05) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
-
-//add in a right order
- printdata();
- mysleep(10);
- printf("addDevice(0x04,2000) \n");
- if (addDevice(0x04,2000) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-
- printdata();
- mysleep(10);
- printf("addDevice(0x05,2000) \n");
- if (addDevice(0x05,2000) == 1 ) printf("ADD \n");
- else printf("NOT - ADD \n");
- putchar('\n');
-
-
- mysleep(10);
-/*
- for(i=0;i<5;i++)
- {
- printdata();
- mysleep(2);
- }
-*/
-// REMOVE ALL nodes
- printdata();
- mysleep(10);
- printf("removeDevice(0x06) \n");
- if (removeDevice(0x06) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
-
- printdata();
- mysleep(10);
- printf("removeDevice(0x05) \n");
- if (removeDevice(0x05) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
-
- printdata();
- mysleep(10);
- printf("removeDevice(0x04) \n");
- if (removeDevice(0x04) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
-
- printdata();
- mysleep(10);
- printf("removeDevice(0x03) \n");
- if (removeDevice(0x03) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
-
- printdata();
- mysleep(10);
- printf("removeDevice(0x02) \n");
- if (removeDevice(0x02) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
-
- /* printdata();
- mysleep(10);
- printf("removeDevice(0x01) \n");
- if (removeDevice(0x01) == 1 ) printf("REM \n");
- else printf("NOT - REM \n");
- putchar('\n');
- */
- mysleep(5);
- }
- /*
- for(;;)
- {
- printdata();
- sleep(2);
- }
- */
- //pthread_join(threadRead, NULL);
-
-}
#define FWSN_H
+#include <pthread.h>
+
#define SLOT_TIME 128
#define MAX_DATA_LENGTH 64
-#define DEFAULT_PACKET_ADD_DEVICE 0x00, 0xff, 0xff, 0x00, 0x00, 0x02, 0x00, 0x07, 0xc2, 0x02, 0xc5, 0x05
-#define DEFAULT_PACKET_REMOVE_DEVICE 0x00, 0xff, 0xff, 0x00, 0x00, 0x02, 0x00, 0x07, 0xc2, 0x02, 0x5C, 0x05
+#define DEFAULT_PACKET_ADD_DEVICE 0x00, 0xff, 0xff, 0x00, 0x00, 0x02, 0x00, 0x07, 0xc2, 0x02, 0xc5, 0x00
+#define DEFAULT_PACKET_REMOVE_DEVICE 0x00, 0xff, 0xff, 0x00, 0x00, 0x02, 0x00, 0x07, 0xc2, 0x02, 0x5C, 0x00
#define PACKET_LENGTH 12
#define DELAY_BETWEEN_SENDS 3000
#define MAXIMUM_REPEAT_COUNTER 5
-#define MIN_COUNTER_TO_APPLAY_CHANGE 2
-
+#define MAXIMUM_REPEAT_COUNTER_OVER_PERIOD 7
+#define BASESTATION_ID 01
pthread_t init(void *argv);
returns -1 if not.
*/
+//*************************************************************************************************************************
+int noOfFreeSlots(int maxResponseTime);
+/*
+returns -1, the demanded period is higher than the actual one
+returns -2, node adding in process, not posiyble to determine the number of free slots
+returns 0, no free slot remains
+returns X, there is X free slots in the available frame length
+*/
//*************************************************************************************************************************
-int addDevice ( unsigned char id, int responseTime);
+int addDevice ( unsigned char id);
/*
-Try to add the device "id", if the maximum response time is smaller that "responseTime".
-returns 1 if the device is possible to add. Does not ensure that the device is added. (not acknowledged)
+Try to add the device "id".
+returns 1 if the device has been added.
returns -1 if not.
*/
//*************************************************************************************************************************
int removeDevice (unsigned char id);
/*
-Try to remove the device "id", if the maximum response time is smaller that "responseTime".
-returns 1 if the device is possible to add. Does not ensure that the device is added. (not acknowledged)
+Try to remove the device "id".
+returns 1 if the device has been removed
returns -1 if not.
+returns -2 if "id" is basestation (it is not possible to remove the basestation)
*/
//*************************************************************************************************************************
-int getData(char *dat);
+int getData(unsigned char **dat);
/*
returns the data length.
-dat has to be an array of size "MAX_DATA_LENGTH".
-if dat == NULL, the function returns just the data length. (does not save data)
+and the data arrauy in dat.
+returns 0 if there are no available data.
*/
-#endif
\ No newline at end of file
+#endif
-# Makefile for standalone build (without OMK)
-CC=gcc
-CFLAGS=-g -std=c99 -pedantic -Wall
-
-%.o:%.c
- $(CC) $(CFLAGS) -c $<
-
-all: serialsend.o serialsource.o seriallisten.o FWSN.o
- gcc serialsend.o serialsource.o -o send
- gcc seriallisten.o serialsource.o -o listen
- gcc -lm -lpthread FWSN.o serialsource.o -o FWSN
-
-FWSN: FWSN.o serialsource.o
- gcc -lm -lc -lpthread FWSN.o serialsource.o -o FWSN
-
-
-clean:
- rm -f *.o send listen FWSN example_FWSN
+CC=gcc\r
+CFLAGS=-g -std=c99 -pedantic -Wall\r
+\r
+%.o:%.c\r
+ $(CC) $(CFLAGS) -c $<\r
+\r
+FWSN: example_FWSN.o FWSN.o serialsource.o\r
+ gcc -lm -lc -lpthread example_FWSN.o FWSN.o serialsource.o -o example_FWSN\r
+\r
+\r
+clean:\r
+ rm -f *.o send listen FWSN example_FWSN\r
+\r
--- /dev/null
+#include "FWSN.h"
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+
+
+void printdata()
+{
+ unsigned char *d;
+ int len,i;
+
+ len = getData(&d);
+
+ for (i = 0; i < len-1; i++)
+ {
+ printf("%02x ", d[i]);
+ if (i%4 == 3)
+ printf(" | ");
+ }
+ free(d);
+ putchar('\n');
+}
+//*************************************************************************************************************************
+void mysleep(int time)
+{
+ int i;
+ for(i=0;i<time;i++)
+ {
+ sleep(1);
+ printdata();
+ }
+
+}
+
+//*************************************************************************************************************************
+
+int main(int argc, char **argv)
+{
+ pthread_t threadRead;
+
+ if (argc != 3)
+ {
+ fprintf(stderr, "Usage: %s <device> <rate> - dump packets from a serial port\n", argv[0]);
+ exit(2);
+ }
+
+ threadRead = init(argv);
+
+ for (;;)
+ {
+
+//ADDing of the nodes.
+
+ printdata();
+ mysleep(2);
+ printf("addDevice(0x02) \n");
+ if (addDevice(0x02) == 1 ) printf("ADD \n");
+
+
+ printdata();
+ //mysleep(10);
+ printf("addDevice(0x03) \n");
+ if (addDevice(0x03) == 1 ) printf("ADD \n");
+
+
+ printdata();
+ //mysleep(10);
+ printf("addDevice(0x03) \n");
+ if (addDevice(0x03) == 1 ) printf("ADD \n");
+
+
+ printdata();
+ // mysleep(10);
+ printf("addDevice(0x04) \n");
+ if (addDevice(0x04) == 1 ) printf("ADD \n");
+
+
+
+ printdata();
+ //mysleep(10);
+ printf("addDevice(0x06) \n");
+ if (addDevice(0x06) == 1 ) printf("ADD \n");
+
+
+
+// REMOVE ALL nodes
+ printdata();
+ mysleep(10);
+ printf("removeDevice(0x04) \n");
+ if (removeDevice(0x04) == 1 ) printf("REM \n");
+
+
+ printdata();
+ //mysleep(10);
+ printf("removeDevice(0x06) \n");
+ if (removeDevice(0x06) == 1 ) printf("REM \n");
+
+
+ printdata();
+ //mysleep(10);
+ printf("removeDevice(0x03) \n");
+ if (removeDevice(0x03) == 1 ) printf("REM \n");
+ //else printf("NOT - REM \n");
+ //putchar('\n');
+
+ printdata();
+ //mysleep(10);
+ printf("removeDevice(0x02) \n");
+ if (removeDevice(0x02) == 1 ) printf("REM \n");
+ else printf("NOT - REM \n");
+ putchar('\n');
+
+
+
+ mysleep(5);
+ }
+
+
+}
+
#include <frsh_distributed.h>
#include <item.h>
-
+//ITEM ADD
+//#include "FWSN.h"
static inline bool node_present(fres_item_node_mask nodes, int node)
{
}
}
- if (!fosa_rel_time_is_null(min_deadline)) {
+
+ if (!fosa_rel_time_is_null(min_deadline)) {
/* TODO: Compare number of nodes and min_deadline */
*schedulable = false;
- } else {
+ } else {
+ //ITEM ADD
+ /* int i;
+ int count_nodes;
+ int free_slots;
+ count_nodes = 0;
+ for (i = 0; i <= sizeof(all_nodes)-1; i++)
+ {
+ count_nodes = count_nodes + all_nodes(i);
+ }
+ free_slots = noOfFreeSlots(min_deadline);
+ if (free_slots < 0)
+ *schedulable = false; // adding of new nodes in action or the actual period is higher then the minimum demanded one.
+ else
+ {
+ if (free_slots < count_nodes)
+ *schedulable = false; // too much nodes are demanded
+ }
+ */
*schedulable = true;
}
-
+
return 0;
}
int node;
struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
- for (node=0; node<64; node++) {
+ for (node=2; node<64; node++) {
if (node_present(mask, node)) {
if (item_status->node_requested[node] == 0)
{
/* TODO: Add node */
+ //addDevice(node); //ITEM ADD
}
item_status->node_requested[node]++;
}
struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
int node;
- for (node=0; node<64; node++) {
+ for (node=2; node<64; node++) {
if (node_present(mask, node)) {
item_status->node_requested[node]--;
if (item_status->node_requested[node] == 0)
{
/* TODO: Remove node */
+ //removeDevice(node); //ITEM ADD
}
}
}
fres_resource_manager frm;
fres_item_scheduler fis;
struct frm_data frm_data;
-
+
+ //ITEM ADD
+ /* pthread_t threadRead;
+ char device[] ="/dev/ttyUSB0";
+ char platform[]="telosb";
+ char *parametrsITEM[] ={NULL,device,platform};
+*/
orb = forb_init(&argc, &argv, "frm_item");
if (!orb) error(1, errno, "forb_init");
forb_executor_run(&executor);
+
+ // ITEM ADD
+ /* threadRead = init(parametrsITEM);
+ if (threadRead == NULL) error (1, errno, "Cannot run the ITEM PC Manager");
+ */
return 0;
}
--- /dev/null
+#include <item.h>
+#include <ul_log.h>
+#include <fra_generic.h>
+#include <stdio.h>
+#include <frsh_distributed.h>
+
+UL_LOG_CUST(ulogd_frs_item);
+ul_log_domain_t ulogd_frs_item = {UL_LOGL_MSG, "frs_item"};
+
+struct item {
+ fres_item_scheduler scheduler;
+};
+
+static int create_vres(fres_vres_t *vres, void *priv)
+{
+ struct item *item = priv;
+ fres_block_item_nodes *nodes;
+ CORBA_Environment env;
+ int ret;
+
+ nodes = fres_contract_get_item_nodes(vres->new);
+ if (!nodes) return FRES_ERR_NEEDS_MORE_DATA_IN_CONTRACT;
+
+ ret = fres_item_scheduler_add_nodes(item->scheduler, nodes->mask, &env);
+ if (forb_exception_occurred(&env)) return FRES_ERR_FORB_EXCEPTION;
+ if (ret) return ret;
+ return 0;
+}
+
+static int cancel_vres(fres_vres_t *vres, void *priv)
+{
+ struct item *item = priv;
+ fres_block_item_nodes *nodes;
+ CORBA_Environment env;
+ int ret;
+
+ nodes = fres_contract_get_item_nodes(vres->allocated);
+ if (!nodes) return FRES_ERR_NEEDS_MORE_DATA_IN_CONTRACT;
+
+ ret = fres_item_scheduler_del_nodes(item->scheduler, nodes->mask, &env);
+ if (forb_exception_occurred(&env)) return FRES_ERR_FORB_EXCEPTION;
+ if (ret) return ret;
+ return 0;
+}
+
+static int change_vres(fres_vres_t *vres, void *priv)
+{
+ return FRSH_ERR_NOT_IMPLEMENTED;
+}
+
+
+
+static struct item item;
+
+static struct fres_allocator item_allocator = {
+ .res_type = FRSH_RT_NETWORK,
+ .res_id = FRSH_NETPF_ITEM,
+ /* Here we are using the "simple interface" */
+ .create_vres = create_vres,
+ .cancel_vres = cancel_vres,
+ .change_vres = change_vres,
+ .priv = &item
+};
+
+int fra_item_init(forb_orb orb, fres_contract_broker fcb, forb_executor_t *executor)
+{
+ fres_block_register_item();
+
+ item.scheduler = forb_resolve_reference(orb, "item_scheduler");
+ if (!item.scheduler) {
+ ul_logerr("forb_resolve_reference(item_scheduler) failed\n");
+ return errno;
+ }
+ return fra_register(orb, fcb, executor, &item_allocator);
+}
+
+/**
+ * Receives data from sensor network.
+ *
+ * @param[in] vres
+ *
+ * @param[out] data Where to store pointer to the array with received
+ * data. The programmer should free the returned array via
+ * forb_free(), when the data are not needed anymore.
+ *
+ * @return Number of elements returned in @a data array, -1 on error.
+ */
+int fra_item_receive(frsh_vres_id_t vres, unsigned *data[])
+{
+ fres_item_data_seq *seq;
+ fres_block_item_nodes *nodes;
+ int ret;
+ CORBA_Environment env;
+
+ nodes = fres_contract_get_item_nodes(vres->allocated);
+ if (!nodes) return FRES_ERR_NEEDS_MORE_DATA_IN_CONTRACT;
+
+ fres_item_scheduler_get_data(item.scheduler, nodes->mask, &seq, &env);
+ CORBA_sequence_set_release(seq, CORBA_FALSE);
+ ret = seq->_length;
+ *data = seq->_buffer;
+ forb_free(seq);
+ return ret;
+}
--- /dev/null
+/**
+ * @file frm_item.c
+ * @author Michal Sojka <sojkam1@fel.cvut.cz>
+ * @date Thu Nov 6 17:21:48 2008
+ *
+ * @brief Resource manager for ITEM protocol
+ *
+ *
+ */
+#include <frm_generic.h>
+#include <forb.h>
+#include <error.h>
+#include <errno.h>
+#include <fres_sa_scenario.h>
+#include <stdbool.h>
+#include <ul_log.h>
+#include <stdio.h>
+#include <frsh_distributed.h>
+#include <item.h>
+
+//ITEM ADD
+#include "FWSN.h"
+
+
+
+static inline bool node_present(fres_item_node_mask nodes, int node)
+{
+ return (nodes & (1LL<<node)) != 0;
+}
+
+static inline void node_add(fres_item_node_mask *nodes, int node)
+{
+ *nodes |= (1LL<<node);
+}
+
+static inline void node_del(fres_item_node_mask *nodes, int node)
+{
+ *nodes &= (~(1LL<<node));
+}
+
+static int
+admission_test(struct fres_sa_scenario *scenario, void *priv, bool *schedulable)
+{
+ struct fres_sa_contract *c;
+ fres_item_node_mask all_nodes;
+ frsh_rel_time_t min_deadline = fosa_msec_to_rel_time(0);
+
+ /* Find communicating nodes and the most restricting deadline */
+ all_nodes = 0;
+ fres_sa_scenario_for_each_contract(scenario, c) {
+ fres_block_item_nodes *nodes;
+ fosa_rel_time_t deadline;
+
+ nodes = fres_contract_get_item_nodes(c->contract);
+ if (!nodes) return FRES_ERR_NEEDS_MORE_DATA_IN_CONTRACT;
+ all_nodes |= nodes->mask;
+
+ if (fres_contract_get_deadline(&c->contract, &deadline)) {
+ if (fosa_rel_time_is_null(min_deadline) ||
+ fosa_rel_time_smaller(deadline, min_deadline)) {
+ min_deadline = deadline;
+ }
+ }
+ }
+
+ if (!fosa_rel_time_is_null(min_deadline)) {
+ /* TODO: Compare number of nodes and min_deadline */
+ *schedulable = false;
+ } else {
+ //ITEM ADD
+ int i;
+ int count_nodes;
+ int free_slots;
+ count_nodes = 0;
+ for (i = 0; i <= sizeof(all_nodes)-1; i++)
+ {
+ count_nodes = count_nodes + all_nodes(i);
+ }
+ free_slots = noOfFreeSlots(min_deadline);
+ if (free_slots < 0)
+ *schedulable = false; // adding of new nodes in action or the actual period is higher then the minimum demanded one.
+ else
+ {
+ if (free_slots < count_nodes)
+ *schedulable = false; // too much nodes are demanded
+ }
+
+ *schedulable = true;
+ }
+
+ return 0;
+}
+
+struct item_status {
+ /** Specifies how many applications want to read from a which
+ * node */
+ int node_requested[64];
+};
+
+CORBA_long
+add_nodes(fres_item_scheduler obj, const fres_item_node_mask mask, CORBA_Environment *ev)
+{
+ int node;
+ struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
+
+ for (node=2; node<64; node++) {
+ if (node_present(mask, node)) {
+ if (item_status->node_requested[node] == 0)
+ {
+ /* TODO: Add node */
+ addDevice(node); //ITEM ADD
+
+ }
+ item_status->node_requested[node]++;
+ }
+ }
+ return /* TODO */ 0;
+}
+
+CORBA_long
+del_nodes(fres_item_scheduler obj, const fres_item_node_mask mask, CORBA_Environment *ev)
+{
+ struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
+ int node;
+
+ for (node=2; node<64; node++) {
+ if (node_present(mask, node)) {
+ item_status->node_requested[node]--;
+ if (item_status->node_requested[node] == 0)
+ {
+ /* TODO: Remove node */
+ removeDevice(node); //ITEM ADD
+ }
+ }
+ }
+ return 0;
+}
+
+CORBA_long
+get_data(fres_item_scheduler obj, const fres_item_node_mask mask, fres_item_data_seq** data, CORBA_Environment *ev)
+{
+ //struct item_status *item_status = (struct item_status*)forb_instance_data(obj);
+ /* TODO: */
+ return 0;
+}
+
+static const struct fres_res_manager frm_item_desc = {
+ .res_type = FRSH_RT_NETWORK,
+ .res_id = FRSH_NETPF_ITEM,
+ .admission_test = admission_test,
+ .priv = NULL,
+};
+
+static const struct forb_fres_item_scheduler_impl item_scheduler_impl = {
+ .add_nodes = add_nodes,
+ .del_nodes = del_nodes,
+ .get_data = get_data,
+};
+
+struct item_status item_status;
+
+int main(int argc, char *argv[])
+{
+ forb_orb orb;
+ int ret;
+ forb_executor_t executor;
+ fres_resource_manager frm;
+ fres_item_scheduler fis;
+ struct frm_data frm_data;
+//ITEM ADD
+ pthread_t threadRead;
+ char device[] ="/dev/ttyUSB0";
+ char platform[]="telosb";
+ char *parametrsITEM[] ={NULL,device,platform};
+
+
+ orb = forb_init(&argc, &argv, "frm_item");
+ if (!orb) error(1, errno, "forb_init");
+
+ fres_block_register_item();
+
+ ret = forb_executor_init(&executor);
+ if (ret) error(1, errno, "forb_executor_init failed");
+
+ frm = frm_register(orb, &frm_data, &executor, &frm_item_desc);
+ if (!frm) {
+ error(1, errno, "frm_register failed");
+ }
+
+ fis = forb_fres_item_scheduler_new(orb, &item_scheduler_impl, &item_status);
+ if (!fis) error(1, errno, "forb_fres_item_scheduler_new failed");
+
+ ret = forb_register_reference(fis, "item_scheduler");
+ if (ret) error (1, errno, "Cannot register ITEM scheduler");
+
+ forb_executor_register_object(&executor, fis);
+
+ forb_executor_run(&executor);
+
+
+// ITEM ADD
+ threadRead = init(parametrsITEM);
+ if (threadRead == NULL) error (1, errno, "Cannot run the ITEM PC Manager");
+
+
+
+ return 0;
+}
#include <item.h>
#include <fres_contract.h>
+//ITEM ADD
+//#include "FWSN.h"
+
#define N 3
int contract_deadline_sec[N] = { 8, 8, 4 };
+//int contract_deadline_sec[N] = { 8*SLOT_TIME, 8*SLOT_TIME, 4*SLOT_TIME }; //ITEM ADD
int contract_nodes[N] = { 0x01, 0x03, 0x02 };
int main(int argc, char *argv[])