1 /***************************************************************************
2 * Copyright (C) 2008 by stefic *
3 * stefaj1@fel.cvut.cz *
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. *
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. *
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 ***************************************************************************/
31 #include <ul_lib/ulan.h>
32 #include <uldy_base.h>
38 #include <system_def.h>
42 // #include <lpciap.h>
43 // #include <keyval_id.h>
44 // #include <keyval_loc.h>
45 // #include <lpciap_kvpb.h>
47 //INCLUDE IN CURRENT DIRECTORY
49 #include "hydroponie.h"
52 #define UL_DEV "/dev/ulan"
54 #define ULDY_TMBUF_LEN 5
55 #define HEART_BUF_LEN 6
56 #define DATA_BUF_LEN 16
57 #define CID_HEART 1023
63 typedef unsigned long mstime_t;
65 typedef lt_mstime_t mstime_t;
71 ul_dyac_t ul_dyac_global;
72 uloi_coninfo_t uloi_coninfo_global;
73 //kvpb_block_t kvpb_block_global;
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 */
89 // OBJECT INTERFACE VARIABLES
91 int16_t oi_temperature;
98 // OBJECT INTERFACE FUNCTIONS
99 int oi_temperature_rdfnc(ULOI_PARAM_coninfo void *context){
100 return uloi_uint_rdfnc(ULOI_ARG_coninfo &oi_temperature);
103 int oi_humidity_rdfnc(ULOI_PARAM_coninfo void *context){
104 return uloi_uint_rdfnc(ULOI_ARG_coninfo &oi_humidity);
107 int oi_cid_temp_wrfnc(ULOI_PARAM_coninfo void *context){
111 int oi_per_temp_wrfnc(ULOI_PARAM_coninfo void *context){
115 int oi_cid_hum_wrfnc(ULOI_PARAM_coninfo void *context){
119 int oi_per_hum_wrfnc(ULOI_PARAM_coninfo void *context){
124 void long2buf(uchar *buf,unsigned long mod)
132 void int2buf(uchar *buf,int mod)
138 char ul_save_sn(uint32_t usn)
140 //kvpb_set_key(kvpb_block,KVPB_KEYID_ULAN_SN,sizeof(unsigned long),&usn);
144 char ul_save_adr(uint8_t uaddr)
146 //unsigned int v=uaddr;
147 //kvpb_set_key(kvpb_block,KVPB_KEYID_ULAN_ADDR,sizeof(unsigned int),&v);
156 uchar buf_out[ULDY_TMBUF_LEN];
157 buf_out[0]=ULNCS_ADR_RQ;
158 long2buf(&buf_out[1],sn);
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");
168 void heartbeat(void){
171 int len = HEART_BUF_LEN;
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);
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");
185 void send_data(void) {
189 int len = DATA_BUF_LEN;
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);
200 int2buf(&buf[7],oi_temperature);
201 int2buf(&buf[9],CID_HUM);
203 int2buf(&buf[13],oi_humidity);
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");
217 oi_temperature = AD0DR1;
218 oi_humidity = AD0DR2;
222 void accept_SDO(void) {
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);
236 printf("problem in uldy_process_msg\n");
239 printf("ULOI message processed\n");
249 mstime_t current_time(){
254 if(clock_gettime(CLOCK_REALTIME, &tp) == 0){
255 ret = tp.tv_sec*1000;
256 ret += tp.tv_nsec/1000000;
277 ltime = current_time();
280 ctime = current_time();
281 if((ctime-ltime) > 1000){
292 if (ul_dyac->ul_dysa != 0) break;
295 ltime = current_time();
296 l2time = current_time();
299 ctime = current_time();
300 if((ctime-ltime) > 1000){
302 ltime = current_time();
304 if((ctime-l2time) > 1000*oi_per_temp){
306 l2time = current_time();
321 mstime_t led1_time, led2_time;
322 led1_time = current_time();
323 led2_time = current_time();
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;
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;
343 int main(int argc, char *argv[]) {
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");
353 //memory set and filter set
354 memset(&msginfo,0,sizeof(msginfo));
355 addfilt=ul_addfilt(ul_fd,&msginfo);
357 printf("spy_messages : add filter failed\n");
361 // uLan object interface init
362 coninfo->cmd = UL_CMD_OISV;
365 coninfo->ul_fd = ul_fd;
366 coninfo->ul_fd1 = ul_fd1;
369 uldy_init(ul_dyac,ul_fd,ul_save_sn,ul_save_adr,(char*)ul_idstr,sn);