UniSaSA  1.0
Embedded Library
unisasa.c
1 #include"unisasa.h"
2 
3 //Global Variables
4 sensor_t sensors[NUMSENS];
5 actuator_t actuators[NUMACTS];
6 device_t thisDevice;
7 unsigned int nextIDSen = FIRST_ID_SEN;
8 unsigned int nextIDAct = FIRST_ID_ACT;
9 
10 unsigned char numSens=0;
11 unsigned char numActs=0;
12 
13 
14 // ---------------------- Internal tools ------------------------- //
15 
16 converter_t converter;
17 
18 void _reverseArray(unsigned char* src, unsigned int size, unsigned char* dest)
19 {
20  int i,j;
21  i=0;
22  j=size -1 ;
23  while (j >= 0)
24  {
25  dest[i] = src[j];
26  i++;
27  j--;
28  }
29 }
30 
31 
32 unsigned char _getSizeBytes(unsigned int i)
33 {
34  converter_t con;
35  con.integer = i;
36 #ifdef BIG_ENDIAN
37  converter_t tmp;
38  tmp.integer=con.integer;
39  _reverseArray(tmp.byte, BYTES_UNSIGNED_INT, con.byte);
40 #endif
41  unsigned char size = 0;
42  for(i=0; i<BYTES_UNSIGNED_INT; i++)
43  if(con.byte[i] != 0)
44  size = i;
45  return size + 1;
46 }
47 
48 union message_t {
49  unsigned int integer;
50  unsigned char byte[BYTES_MESSAGE];
51 } message,answer;
52 // ------------------------------------------------------------- //
53 
54 void createDevice(unsigned int address, unsigned char sizeIOmsg,messageHandlerFunc msgHandler, messageAndAnswerHandlerFunc msgAnsHandler)
55 {
56  thisDevice.address.integer = address;
57  thisDevice.sizeIOMsg = sizeIOmsg;
58  thisDevice.sizeIDSen = _getSizeBytes(NUMSENS);
59  thisDevice.sizeIDAct = _getSizeBytes(NUMACTS);
60  thisDevice.messageHandler = msgHandler;
61  thisDevice.messageAndAnswerHandler = msgAnsHandler;
62 }
63 
64 void addSensor(unsigned int typeSensor,unsigned char sizeOutput,unsigned char typeOutput, getSensorValueFunc getValue)
65 {
66  sensors[nextIDSen].ID.integer = nextIDSen;
67  sensors[nextIDSen].typeSensor.integer = typeSensor;
68  sensors[nextIDSen].sizeOutput = sizeOutput;
69  sensors[nextIDSen].typeOutput = typeOutput;
70  sensors[nextIDSen].getValue = getValue;
71  nextIDSen++;
72  numSens++;
73 }
74 
75 void addActuator(unsigned int typeActuator,unsigned char sizeInput,unsigned char typeInput, setActuatorValueFunc setValue)
76 {
77  actuators[nextIDAct].ID.integer = nextIDAct;
78  actuators[nextIDAct].typeActuator.integer = typeActuator;
79  actuators[nextIDAct].sizeInput = sizeInput;
80  actuators[nextIDAct].typeInput = typeInput;
81  actuators[nextIDAct].setValue = setValue;
82  nextIDAct++;
83  numActs++;
84 }
85 
86 
88 {
89  unsigned int id; //sen or act
90  int s,a;
91 /* while(1)
92  { */
93  receive(message.byte,1);
94  switch (message.byte[0])
95  {
96  case DISCOVERY:
97  /*
98  * 1 byte sizeAddr 1 byte
99  * | sizeaddr | addr | sizeIOMsg | ...
100  *
101  * 1 byte sizeIDSen sizeIDSen 2 byte 1 byte 1 byte
102  * ... | sizeIDSen | num Sens | ID Sen | type Sen | size Out | type Out ...
103  *
104  * 1 byte sizeIDAct sizeIDAct 2 byte 1 byte 1 byte
105  * ... | sizeIDAct | num Acts | ID Act | type Act | size In | type In |
106  *
107  */
108  message.byte[0] = BYTES_ADDRESS_DEVICE;
109  send(message.byte,1);
110  SEND(thisDevice.address.byte,BYTES_ADDRESS_DEVICE,converter.byte);
111  send(&(thisDevice.sizeIOMsg),1);
112 
113  send(&thisDevice.sizeIDSen,1);
114  SEND(&numSens,thisDevice.sizeIDSen,converter.byte);
115  for(s=0;s< numSens; s++)
116  {
117  SEND(sensors[s].ID.byte,thisDevice.sizeIDSen,converter.byte);
118  SEND(sensors[s].typeSensor.byte,SIZE_TYPE_SENSOR,converter.byte);
119  send(&(sensors[s].sizeOutput),1);
120  send(&(sensors[s].typeOutput),1);
121  }
122  send(&(thisDevice.sizeIDAct),1);
123  SEND(&numActs,thisDevice.sizeIDAct,converter.byte);
124  for(a=0;a< numActs; a++)
125  {
126  SEND(actuators[a].ID.byte,thisDevice.sizeIDAct,converter.byte);
127  SEND(actuators[a].typeActuator.byte,SIZE_TYPE_ACTUATOR,converter.byte);
128  send(&(actuators[a].sizeInput),1);
129  send(&(actuators[a].typeInput),1);
130  }
131  break;
132  case CHECK_DEVICE:
133  message.byte[0] = ACTIVE_DEVICE_ACK;
134  send(message,1);
135  SEND(thisDevice.address.byte,BYTES_ADDRESS_DEVICE,converter.byte);
136  break;
137  case MSG_TO_DEVICE:
138  message.integer=0;
139  RECV(message.byte,thisDevice.sizeIOMsg,converter.byte);
140  thisDevice.messageHandler(message.byte);
141  break;
142  case MSG_AND_ANSWER_TO_DEVICE:
143  RECV(message.byte,thisDevice.sizeIOMsg,converter.byte);
144  thisDevice.messageAndAnswerHandler(message.byte,answer.byte);
145  SEND(answer.byte,thisDevice.sizeIOMsg,converter.byte);
146  break;
147  case GET_SENSOR_VALUE:
148  message.integer=0;
149  answer.integer=0;
150  RECV(message.byte,thisDevice.sizeIDSen,converter.byte);
151  id=message.integer;
152  sensors[id].getValue(answer.byte);
153  SEND(answer.byte,sensors[id].sizeOutput,converter.byte);
154  break;
155  case SET_ACTUATOR_VALUE:
156  message.integer=0;
157  RECV(message.byte,thisDevice.sizeIDAct,converter.byte);
158  id = message.integer;
159  RECV(message.byte,actuators[id].sizeInput,converter.byte);
160  actuators[id].setValue(message.byte);
161  break;
162  default:
163  break;
164  }//end switch/case
165  //}//end while
166 }
void addSensor(unsigned int typeSensor, unsigned char sizeOutput, unsigned char typeOutput, getSensorValueFunc getValue)
Definition: unisasa.c:64
void createDevice(unsigned int address, unsigned char sizeIOmsg, messageHandlerFunc callback1, messageAndAnswerHandlerFunc callback2)
Definition: unisasa.c:54
void(* messageHandlerFunc)(unsigned char *)
Definition: unisasa.h:109
void communication(receiveUnisasa recvFunc, sendUnisasa sendFunc)
Definition: unisasa.c:87
void(* setActuatorValueFunc)(unsigned char *act_value)
Definition: unisasa.h:198
#define BYTES_ADDRESS_DEVICE
Definition: unisasa.h:46
void(* getSensorValueFunc)(unsigned char *sensor_value)
Definition: unisasa.h:154
#define NUMSENS
Definition: unisasa.h:42
void(* receiveUnisasa)(unsigned char *message, unsigned int size)
Definition: unisasa.h:245
void addActuator(unsigned int typeActuator, unsigned char sizeInput, unsigned char typeInput, setActuatorValueFunc setValue)
Definition: unisasa.c:75
void(* messageAndAnswerHandlerFunc)(unsigned char *request, unsigned char *reply)
Definition: unisasa.h:118
#define NUMACTS
Definition: unisasa.h:43
void(* sendUnisasa)(unsigned char *message, unsigned int size)
Definition: unisasa.h:254