]> rtime.felk.cvut.cz Git - hydro.git/blob - app/hydro/hydroponie.c
7f0c3214475c7e372f0e710454965ad19141b096
[hydro.git] / app / hydro / hydroponie.c
1 /***************************************************************************
2  *   Copyright (C) 2008 by stefic    *
3  *   stefaj1@fel.cvut.cz   *
4  *                                                                         *
5  *   This program is free software; you can redistribute it and/or modify  *
6  *   it under the terms of the GNU General Public License as published by  *
7  *   the Free Software Foundation; either version 2 of the License, or     *
8  *   (at your option) any later version.                                   *
9  *                                                                         *
10  *   This program is distributed in the hope that it will be useful,       *
11  *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
12  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
13  *   GNU General Public License for more details.                          *
14  *                                                                         *
15  *   You should have received a copy of the GNU General Public License     *
16  *   along with this program; if not, write to the                         *
17  *   Free Software Foundation, Inc.,                                       *
18  *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
19  ***************************************************************************/
20
21 //#define OS_POSIX
22
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30
31 #include <ul_lib/ulan.h>
32 #include <uldy_base.h>
33
34 #ifdef OS_POSIX
35     #include <time.h>
36 #else
37     #include <lt_timer.h>
38     #include <system_def.h>
39     #include <cpu_def.h>
40 #endif
41
42 // #include <lpciap.h>
43 // #include <keyval_id.h>
44 // #include <keyval_loc.h>
45 // #include <lpciap_kvpb.h>
46
47 //INCLUDE IN CURRENT DIRECTORY
48 #include "ul_idstr.h"
49 #include "hydroponie.h"
50
51 // CONSTANTS
52 #define UL_DEV "/dev/ulan"
53 #define SN 101
54 #define ULDY_TMBUF_LEN 5
55 #define HEART_BUF_LEN 6
56 #define DATA_BUF_LEN 16
57 #define CID_HEART 1023
58 #define CID_TEMP 100
59 #define CID_HUM 200
60
61 // DEFINED TYPES
62 #ifdef OS_POSIX
63     typedef unsigned long mstime_t; 
64 #else
65     typedef lt_mstime_t mstime_t;
66 #endif
67
68 // GLOBAL VARIABLES
69 long sn = SN;
70 uchar server_adr;
71 ul_dyac_t ul_dyac_global;
72 uloi_coninfo_t uloi_coninfo_global;
73 //kvpb_block_t kvpb_block_global;
74
75 ul_fd_t ul_fd;
76 ul_fd_t ul_fd1;
77 ul_msginfo msginfo;
78 int addfilt;
79
80 adc_stat_t adcst;
81 // adc_cal_t adccal;
82
83 #ifndef UL_WITHOUT_HANDLE
84 //kvpb_block_t *kvpb_block=&kvpb_block_global;
85 uloi_coninfo_t *coninfo=&uloi_coninfo_global;
86 ul_dyac_t *ul_dyac=&ul_dyac_global;
87 #endif /* UL_WITHOUT_HANDLE */
88
89 // OBJECT INTERFACE VARIABLES
90 uint16_t status_val;
91 int16_t oi_temperature;
92 int16_t oi_cid_temp;
93 int16_t oi_per_temp;
94 int16_t oi_humidity;
95 int16_t oi_cid_hum;
96 int16_t oi_per_hum;
97
98 // OBJECT INTERFACE FUNCTIONS
99 int oi_temperature_rdfnc(ULOI_PARAM_coninfo void *context){
100     return uloi_uint_rdfnc(ULOI_ARG_coninfo &oi_temperature);
101 }
102
103 int oi_humidity_rdfnc(ULOI_PARAM_coninfo void *context){
104     return uloi_uint_rdfnc(ULOI_ARG_coninfo &oi_humidity);
105 }
106
107 int oi_cid_temp_wrfnc(ULOI_PARAM_coninfo void *context){
108
109 }
110
111 int oi_per_temp_wrfnc(ULOI_PARAM_coninfo void *context){
112
113 }
114
115 int oi_cid_hum_wrfnc(ULOI_PARAM_coninfo void *context){
116
117 }
118
119 int oi_per_hum_wrfnc(ULOI_PARAM_coninfo void *context){
120
121 }
122
123 // CLASS FUNCTIONS
124 void long2buf(uchar *buf,unsigned long mod)
125 {
126   buf[0]=mod>>0;
127   buf[1]=mod>>8;
128   buf[2]=mod>>16;
129   buf[3]=mod>>24;
130 }
131
132 void int2buf(uchar *buf,int mod)
133 {
134   buf[0]=mod>>0;
135   buf[1]=mod>>8;
136 }
137
138 char ul_save_sn(uint32_t usn)
139 {
140   //kvpb_set_key(kvpb_block,KVPB_KEYID_ULAN_SN,sizeof(unsigned long),&usn);
141   return 0;
142 }
143
144 char ul_save_adr(uint8_t uaddr)
145 {
146   //unsigned int v=uaddr;
147   //kvpb_set_key(kvpb_block,KVPB_KEYID_ULAN_ADDR,sizeof(unsigned int),&v);
148   return 0;
149 }
150
151
152 void send_sn(void){
153     int msgsend;
154
155     //.preparing of msg
156     uchar buf_out[ULDY_TMBUF_LEN];
157     buf_out[0]=ULNCS_ADR_RQ;
158     long2buf(&buf_out[1],sn);
159
160     //address set and sign on the ulan
161     ul_setmyadr(ul_fd,255);
162     msgsend = ul_send_command(ul_fd, msginfo.dadr, UL_CMD_NCS, UL_BFL_NORE, (void*) buf_out, ULDY_TMBUF_LEN);
163     printf("[I] sn was send\n");
164     ul_freemsg(ul_fd);
165
166 }
167
168 void heartbeat(void){
169     //int period;
170     int msgsend, i;
171     int len = HEART_BUF_LEN;
172     uchar *buf;
173
174     //.preparing of msg
175     buf = (uchar *) malloc(len*sizeof(uchar));  // NULL NULL NULL CID_HEART status_val
176     for (i=0; i<=2; i++) buf[i] = 0;
177     int2buf(&buf[3],CID_HEART);
178     int2buf(&buf[5],status_val);
179
180     msgsend = ul_send_command(ul_fd, ul_dyac->ul_dysa, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, HEART_BUF_LEN);
181     printf("[I] BEAT\n");
182     free(buf);
183 }
184
185 void send_data(void) {
186     int msgsend;
187     uchar *buf;
188     int i;
189     int len = DATA_BUF_LEN;
190
191     init_adc();
192     get_adc(&adcst);
193 //     PC_adc();
194
195     //.preparing of msg
196     buf = (uchar *) malloc(len*sizeof(uchar));  // NULL NULL NULL CID_TEMP len_data oi_temperature CID_HUM len_data oi_humidity
197     for (i=0; i<=2; i++) buf[i] = 0;
198     int2buf(&buf[3],CID_TEMP);
199     int2buf(&buf[5],2);
200     int2buf(&buf[7],oi_temperature);
201     int2buf(&buf[9],CID_HUM);
202     int2buf(&buf[11],2);
203     int2buf(&buf[13],oi_humidity);
204
205     msgsend = ul_send_query(ul_fd, ul_dyac->ul_dysa, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, DATA_BUF_LEN);
206     printf("[I] DATA\n");
207     free(buf);
208     ul_freemsg(ul_fd);
209
210 }
211
212 void PC_adc(void){
213 #ifdef OS_POSIX
214     oi_temperature = 25;
215     oi_humidity = 99;
216 #else
217     oi_temperature = AD0DR1;
218     oi_humidity = AD0DR2;
219 #endif
220 }
221
222 void accept_SDO(void) {
223
224         // setting lan configuration
225         if ((ul_inepoll(ul_fd)>0) && (ul_acceptmsg(ul_fd, &msginfo)>=0)) {
226             if ((msginfo.cmd != UL_CMD_PDO) && (msginfo.sadr != 255)) {
227                 if (!(msginfo.flg&(UL_BFL_PROC | UL_BFL_FAIL))) {
228                     //waiting for msg from bus
229                     if (uloi_process_msg(ULOI_ARG_coninfo (uloi_objdes_array_t*)&uloi_objdes_main, &msginfo)<0) {
230                         //checking dynamic adresation
231                         if (uldy_process_msg(ULDY_ARG_ul_dyac &msginfo)>=0){
232                             printf("message recieved - UL_CMD_NCS, sadr: %i, dadr: %i, cmd: %i\n",msginfo.sadr, msginfo.dadr, msginfo.cmd);
233                             printf("server addr :%i\n",ul_dyac->ul_dysa);
234                         } else {
235                             ul_freemsg(ul_fd);
236                             printf("problem in uldy_process_msg\n");
237                         }
238                     } else {
239                         printf("ULOI message processed\n");
240                     }
241                 } else {
242                     ul_freemsg(ul_fd);
243                 }
244             }
245         }
246 }
247
248
249 mstime_t current_time(){
250     mstime_t ret = 0;
251
252     #ifdef OS_POSIX
253         struct timespec tp;
254         if(clock_gettime(CLOCK_REALTIME, &tp) == 0){
255             ret = tp.tv_sec*1000;
256             ret += tp.tv_nsec/1000000;
257         }
258     #else
259         lt_mstime_update();
260         ret = actual_msec;
261     #endif
262
263    return ret;
264 }
265
266 void loop(void){
267    //time setting
268
269     mstime_t ltime;
270     mstime_t l2time;
271     mstime_t ctime;
272
273     oi_per_temp = 2;
274
275     while(1){
276         send_sn();
277         ltime = current_time();
278
279         while(1){
280             ctime = current_time();
281             if((ctime-ltime) > 1000){
282                 break;
283             }
284
285             #ifdef OS_POSIX
286                 usleep(1);
287             #endif
288
289             accept_SDO();
290
291         }
292         if (ul_dyac->ul_dysa != 0) break;
293     }
294
295     ltime = current_time();
296     l2time = current_time();
297
298     while(1){
299         ctime = current_time();
300         if((ctime-ltime) > 1000){
301 //             heartbeat();
302             ltime = current_time();
303         }
304         if((ctime-l2time) > 1000*oi_per_temp){
305             send_data();
306             l2time = current_time();
307         }
308         accept_SDO();
309         #ifdef OS_POSIX
310             usleep(1);
311         #endif
312     }
313
314 }
315
316 void blink(void){
317     #ifndef OS_POSIX
318     while(1){
319         int led1 = 200;
320         int led2 = 0;
321         mstime_t led1_time, led2_time;
322         led1_time = current_time();
323         led2_time = current_time();
324
325         if(led1 == 0) IO0SET=LED1_BIT;
326         else if(led1 == 1) IO0CLR=LED1_BIT;
327         else if((current_time()-led1_time) > led1){
328         IO0PIN=IO0PIN^LED1_BIT;
329         led1_time+=led1;
330         }
331
332         if(led2 == 0) IO0SET=LED2_BIT;
333         else if(led2 == 1) IO0CLR=LED2_BIT;
334         else if((current_time()-led2_time) > led2){
335         IO0PIN=IO0PIN^LED2_BIT;
336         led2_time+=led2;
337         }
338     }
339     #endif
340
341 }
342
343 int main(int argc, char *argv[]) { 
344
345     //opening file descriptor
346     ul_fd = ul_open(UL_DEV,NULL);
347     ul_fd1 = ul_open(NULL,NULL);
348     if ((ul_fd == UL_FD_INVALID) && (ul_fd1 == UL_FD_INVALID)){
349         printf("error in creating ul_fd\n");
350         return -1;
351     }
352
353     //memory set and filter set
354     memset(&msginfo,0,sizeof(msginfo));
355     addfilt=ul_addfilt(ul_fd,&msginfo);
356     if(addfilt<0) { 
357         printf("spy_messages : add filter failed\n");
358         return addfilt;
359     }
360
361     // uLan object interface init
362     coninfo->cmd = UL_CMD_OISV;
363     coninfo->sn = SN;
364     coninfo->bsn = 0;
365     coninfo->ul_fd = ul_fd;
366     coninfo->ul_fd1 = ul_fd1;
367
368     // uLan dyac init
369     uldy_init(ul_dyac,ul_fd,ul_save_sn,ul_save_adr,(char*)ul_idstr,sn);
370
371     loop();
372 //     blink();
373
374     ul_close(ul_fd);
375
376     return EXIT_SUCCESS;
377 }