--- /dev/null
+/***************************************************************************
+ * Copyright (C) 2008 by stefic *
+ * stefaj1@fel.cvut.cz *
+ * *
+ * This program is free software; you can redistribute it and/or modify *
+ * it under the terms of the GNU General Public License as published by *
+ * the Free Software Foundation; either version 2 of the License, or *
+ * (at your option) any later version. *
+ * *
+ * This program is distributed in the hope that it will be useful, *
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of *
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
+ * GNU General Public License for more details. *
+ * *
+ * You should have received a copy of the GNU General Public License *
+ * along with this program; if not, write to the *
+ * Free Software Foundation, Inc., *
+ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
+ ***************************************************************************/
+
+//#define OS_POSIX
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include <ul_lib/ulan.h>
+#include <uldy_base.h>
+
+#ifdef OS_POSIX
+ #include <time.h>
+#else
+ #include <lt_timer.h>
+ #include <system_def.h>
+ #include <cpu_def.h>
+#endif
+// #include <lpciap.h>
+// #include <keyval_id.h>
+// #include <keyval_loc.h>
+// #include <lpciap_kvpb.h>
+
+//INCLUDE IN CURRENT DIRECTORY
+#include "hydroponie.h"
+#include "ul_idstr.h"
+
+// CONSTANTS
+#define UL_DEV "/dev/ulan"
+#define SN 101
+#define ULDY_TMBUF_LEN 5
+#define HEART_BUF_LEN 6
+#define DATA_BUF_LEN 16
+#define CID_HEART 1023
+#define CID_TEMP 100
+#define CID_HUM 200
+
+// DEFINED TYPES
+#ifdef OS_POSIX
+ typedef unsigned long mstime_t;
+#else
+ typedef lt_mstime_t mstime_t;
+#endif
+
+// GLOBAL VARIABLES
+long sn = SN;
+uchar server_adr;
+ul_dyac_t ul_dyac_global;
+uloi_coninfo_t uloi_coninfo_global;
+//kvpb_block_t kvpb_block_global;
+
+ul_fd_t ul_fd;
+ul_fd_t ul_fd1;
+ul_msginfo msginfo;
+int addfilt;
+
+#ifndef UL_WITHOUT_HANDLE
+//kvpb_block_t *kvpb_block=&kvpb_block_global;
+uloi_coninfo_t *coninfo=&uloi_coninfo_global;
+ul_dyac_t *ul_dyac=&ul_dyac_global;
+#endif /* UL_WITHOUT_HANDLE */
+
+// OBJECT INTERFACE VARIABLES
+uint16_t status_val;
+int16_t oi_temperature = 20;
+int16_t oi_cid_temp;
+int16_t oi_per_temp;
+int16_t oi_humidity = 100;
+int16_t oi_cid_hum;
+int16_t oi_per_hum;
+
+// OBJECT INTERFACE FUNCTIONS
+int oi_temperature_rdfnc(ULOI_PARAM_coninfo void *context){
+ return uloi_uint_rdfnc(ULOI_ARG_coninfo &oi_temperature);
+}
+
+int oi_humidity_rdfnc(ULOI_PARAM_coninfo void *context){
+ return uloi_uint_rdfnc(ULOI_ARG_coninfo &oi_humidity);
+}
+
+int oi_cid_temp_wrfnc(ULOI_PARAM_coninfo void *context){
+
+}
+
+int oi_per_temp_wrfnc(ULOI_PARAM_coninfo void *context){
+
+}
+
+int oi_cid_hum_wrfnc(ULOI_PARAM_coninfo void *context){
+
+}
+
+int oi_per_hum_wrfnc(ULOI_PARAM_coninfo void *context){
+
+}
+
+// CLASS FUNCTIONS
+void long2buf(uchar *buf,unsigned long mod)
+{
+ buf[0]=mod>>0;
+ buf[1]=mod>>8;
+ buf[2]=mod>>16;
+ buf[3]=mod>>24;
+}
+
+void int2buf(uchar *buf,int mod)
+{
+ buf[0]=mod>>0;
+ buf[1]=mod>>8;
+}
+
+char ul_save_sn(uint32_t usn)
+{
+ //kvpb_set_key(kvpb_block,KVPB_KEYID_ULAN_SN,sizeof(unsigned long),&usn);
+ return 0;
+}
+
+char ul_save_adr(uint8_t uaddr)
+{
+ //unsigned int v=uaddr;
+ //kvpb_set_key(kvpb_block,KVPB_KEYID_ULAN_ADDR,sizeof(unsigned int),&v);
+ return 0;
+}
+
+
+int send_sn(void){
+ //declaration
+ int msgsend;
+
+ //.preparing of msg
+ uchar buf_out[ULDY_TMBUF_LEN];
+ buf_out[0]=ULNCS_ADR_RQ;
+ long2buf(&buf_out[1],sn);
+
+ //address set and sign on the ulan
+ ul_setmyadr(ul_fd,255);
+ msgsend = ul_send_command(ul_fd, msginfo.dadr, UL_CMD_NCS, UL_BFL_NORE, (void*) buf_out, ULDY_TMBUF_LEN);
+ printf("[I] sn was send\n");
+ ul_freemsg(ul_fd);
+
+}
+
+int heartbeat(void){
+ //int period;
+ int msgsend, i;
+ int len = HEART_BUF_LEN;
+ uchar *buf;
+
+ //.preparing of msg
+ buf = (uchar *) malloc(len*sizeof(uchar)); // NULL NULL NULL CID_HEART status_val
+ for (i=0; i<=2; i++) buf[i] = 0;
+ int2buf(&buf[3],CID_HEART);
+ int2buf(&buf[5],status_val);
+
+ msgsend = ul_send_command(ul_fd, ul_dyac->ul_dysa, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, HEART_BUF_LEN);
+ printf("[I] BEAT\n");
+ free(buf);
+}
+
+int send_data(void) {
+ int msgsend;
+ uchar *buf;
+ int i;
+ int len = DATA_BUF_LEN;
+
+ get_data();
+
+ //.preparing of msg
+ buf = (uchar *) malloc(len*sizeof(uchar)); // NULL NULL NULL CID_TEMP len_data oi_temperature CID_HUM len_data oi_humidity
+ for (i=0; i<=2; i++) buf[i] = 0;
+ int2buf(&buf[3],CID_TEMP);
+ int2buf(&buf[5],2);
+ int2buf(&buf[7],oi_temperature);
+// int2buf(&buf[9],CID_HUM);
+// int2buf(&buf[11],2);
+// int2buf(&buf[13],oi_humidity);
+
+ msgsend = ul_send_query(ul_fd, ul_dyac->ul_dysa, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, /*DATA_BUF_LEN*/9);
+ printf("[I] DATA\n");
+ free(buf);
+ ul_freemsg(ul_fd);
+
+}
+
+int get_data(void){
+#ifdef OS_POSIX
+ oi_temperature = -25000;
+ oi_humidity = 99;
+#else
+ oi_temperature = AD0DR1;
+ oi_humidity = AD0DR2;
+#endif
+}
+
+int accept_SDO(void) {
+
+ // setting lan configuration
+ if ((ul_inepoll(ul_fd)>0) && (ul_acceptmsg(ul_fd, &msginfo)>=0)) {
+ if ((msginfo.cmd != UL_CMD_PDO) && (msginfo.sadr != 255)) {
+ if (!(msginfo.flg&(UL_BFL_PROC | UL_BFL_FAIL))) {
+ //waiting for msg from bus
+ if (uloi_process_msg(ULOI_ARG_coninfo (uloi_objdes_array_t*)&uloi_objdes_main, &msginfo)<0) {
+ //checking dynamic adresation
+ if (uldy_process_msg(ULDY_ARG_ul_dyac &msginfo)>=0){
+ printf("message recieved - UL_CMD_NCS, sadr: %i, dadr: %i, cmd: %i\n",msginfo.sadr, msginfo.dadr, msginfo.cmd);
+ printf("server addr :%i\n",ul_dyac->ul_dysa);
+ } else {
+ ul_freemsg(ul_fd);
+ printf("problem in uldy_process_msg\n");
+ }
+ } else {
+ printf("ULOI message processed\n");
+ }
+ } else {
+ ul_freemsg(ul_fd);
+ }
+ }
+ }
+}
+
+
+mstime_t current_time(){
+ mstime_t ret = 0;
+
+ #ifdef OS_POSIX
+ struct timespec tp;
+ if(clock_gettime(CLOCK_REALTIME, &tp) == 0){
+ ret = tp.tv_sec*1000;
+ ret += tp.tv_nsec/1000000;
+ }
+ #else
+ lt_mstime_update();
+ ret = actual_msec;
+ #endif
+
+ return ret;
+}
+
+void loop(void){
+ //time setting
+
+ mstime_t ltime;
+ mstime_t l2time;
+ mstime_t ctime;
+
+ oi_per_temp = 2;
+
+ while(1){
+ send_sn();
+ ltime = current_time();
+
+ while(1){
+ ctime = current_time();
+ if((ctime-ltime) > 1000){
+ break;
+ }
+
+ #ifdef OS_POSIX
+ usleep(1);
+ #endif
+
+ accept_SDO();
+
+ }
+ if (ul_dyac->ul_dysa != 0) break;
+ }
+
+ ltime = current_time();
+ l2time = current_time();
+
+ while(1){
+ ctime = current_time();
+ if((ctime-ltime) > 1000){
+// heartbeat();
+ ltime = current_time();
+ }
+ if((ctime-l2time) > 1000*oi_per_temp){
+ send_data();
+ l2time = current_time();
+ }
+ accept_SDO();
+ #ifdef OS_POSIX
+ usleep(1);
+ #endif
+ }
+
+}
+
+int main(int argc, char *argv[]) {
+
+ //opening file descriptor
+ ul_fd = ul_open(UL_DEV,NULL);
+ ul_fd1 = ul_open(NULL,NULL);
+ if ((ul_fd == UL_FD_INVALID) && (ul_fd1 == UL_FD_INVALID)){
+ printf("error in creating ul_fd\n");
+ return -1;
+ }
+
+ //memory set and filter set
+ memset(&msginfo,0,sizeof(msginfo));
+ addfilt=ul_addfilt(ul_fd,&msginfo);
+ if(addfilt<0) {
+ printf("spy_messages : add filter failed\n");
+ return addfilt;
+ }
+
+ // uLan object interface init
+ coninfo->cmd = UL_CMD_OISV;
+ coninfo->sn = SN;
+ coninfo->bsn = 0;
+ coninfo->ul_fd = ul_fd;
+ coninfo->ul_fd1 = ul_fd1;
+
+ // uLan dyac init
+ uldy_init(ul_dyac,ul_fd,ul_save_sn,ul_save_adr,(char*)ul_idstr,sn);
+
+ loop();
+
+ ul_close(ul_fd);
+ return EXIT_SUCCESS;
+}
--- /dev/null
+/*
+ OBJECT INTERFACE FOR HYDRO
+*/
+
+#include <uloi_base.h>
+#include "hydroponie.h"
+
+#define I_TEMPERATURE 100
+#define I_CID_TEMP 110
+#define I_PER_TEMP 120
+#define I_HUMIDITY 200
+#define I_CID_HUM 210
+#define I_PER_HUM 220
+
+
+uint16_t status_val;
+// promene ktere jsou v headeru jako extern musi byt deklarovany normalne i ve fci kde je pouzivam
+int16_t oi_temperature;
+int16_t oi_cid_temp;
+int16_t oi_per_temp;
+int16_t oi_humidity;
+int16_t oi_cid_hum;
+int16_t oi_per_hum;
+
+int status_rdfnc(ULOI_PARAM_coninfo void *context)
+{
+ return uloi_uint_rdfnc(ULOI_ARG_coninfo &status_val);
+}
+
+int errclr_wrfnc(ULOI_PARAM_coninfo void *context)
+{
+ status_val=0;
+ return 1;
+}
+
+/* description of input objects */
+const ULOI_CODE uloi_objdes_t uloid_objdes_DOII =
+ULOI_GENOBJDES_RAW(ULOI_DOII,NULL,NULL_CODE,NULL,uloi_doii_fnc,(void*)&uloi_objdes_main)
+/* description of output objects */
+const ULOI_CODE uloi_objdes_t uloid_objdes_DOIO =
+ULOI_GENOBJDES_RAW(ULOI_DOIO,NULL,NULL_CODE,NULL,uloi_doio_fnc,(void*)&uloi_objdes_main)
+/* ID numbers of recognized input objects */
+const ULOI_CODE uloi_objdes_t uloid_objdes_QOII =
+ULOI_GENOBJDES_RAW(ULOI_QOII,NULL,NULL_CODE,NULL,uloi_qoii_fnc,(void*)&uloi_objdes_main)
+/* ID numbers of recognized output objects */
+const ULOI_CODE uloi_objdes_t uloid_objdes_QOIO =
+ULOI_GENOBJDES_RAW(ULOI_QOIO,NULL,NULL_CODE,NULL,uloi_qoio_fnc,(void*)&uloi_objdes_main)
+/* object values read request */
+const ULOI_CODE uloi_objdes_t uloid_objdes_RDRQ =
+ULOI_GENOBJDES_RAW(ULOI_RDRQ,NULL,NULL_CODE,NULL,uloi_rdrq_fnc,(void*)&uloi_objdes_main)
+
+ULOI_GENOBJDES(STATUS,ULOI_STATUS,"u2",status_rdfnc,&status_val,NULL_CODE,NULL)
+ULOI_GENOBJDES(ERRCLR,ULOI_ERRCLR,"e",NULL_CODE,NULL,errclr_wrfnc,&status_val)
+ULOI_GENOBJDES(TEMPERATURE,I_TEMPERATURE,"s2",oi_temperature_rdfnc,&oi_temperature,NULL_CODE,NULL)
+ULOI_GENOBJDES(CID_TEMP,I_CID_TEMP,"u2",uloi_uint_rdfnc,&oi_cid_temp,oi_cid_temp_wrfnc,&oi_cid_temp)
+ULOI_GENOBJDES(PER_TEMP,I_PER_TEMP,"u2",uloi_uint_rdfnc,&oi_per_temp,oi_per_temp_wrfnc,&oi_per_temp)
+ULOI_GENOBJDES(HUMIDITY,I_HUMIDITY,"s2",oi_humidity_rdfnc,&oi_temperature,NULL_CODE,NULL)
+ULOI_GENOBJDES(CID_HUM,I_CID_HUM,"u2",uloi_uint_rdfnc,&oi_cid_hum,oi_cid_hum_wrfnc,&oi_cid_hum)
+ULOI_GENOBJDES(PER_HUM,I_PER_HUM,"u2",uloi_uint_rdfnc,&oi_per_hum,oi_per_hum_wrfnc,&oi_per_hum)
+
+const uloi_objdes_t * ULOI_CODE uloi_objdes_main_items[]={
+ &uloid_objdes_DOII,
+ &uloid_objdes_DOIO,
+ &uloid_objdes_QOII,
+ &uloid_objdes_QOIO,
+ &uloid_objdes_RDRQ,
+
+ &uloid_objdes_STATUS,
+ &uloid_objdes_ERRCLR,
+ &uloid_objdes_TEMPERATURE,
+ &uloid_objdes_CID_TEMP,
+ &uloid_objdes_PER_TEMP,
+ &uloid_objdes_HUMIDITY,
+ &uloid_objdes_CID_HUM,
+ &uloid_objdes_PER_HUM
+
+};
+
+const ULOI_CODE uloi_objdes_array_t uloi_objdes_main={
+ {
+ /*(void *)*/(uloi_objdes_t **)uloi_objdes_main_items,
+ sizeof(uloi_objdes_main_items)/sizeof(uloi_objdes_main_items[0]),
+ -1
+ }
+};