4 #include <ul_lib/ulan.h>
5 #include <system_def.h>
8 #include "definitions.h"
13 #ifndef UL_WITHOUT_HANDLE
14 uloi_coninfo_t *coninfo=&uloi_coninfo_global;
15 ul_dyac_t *ul_dyac=&ul_dyac_global;
22 void long2buf(uchar *buf,unsigned long mod){
29 void int2buf(uchar *buf,int mod){
34 int buf2int(uchar *buf){
37 i = ((uchar) buf[1]<<8) | i;
41 uint buf2uint(uchar *buf){
44 i = ((uchar) buf[1]<<8) | i;
48 mstime_t current_time(){
53 if(clock_gettime(CLOCK_REALTIME, &tp) == 0){
55 ret += tp.tv_nsec/1000000;
67 if(led1 == 0) IO0SET=LED1_BIT;
68 else if(led1 == 1) IO0CLR=LED1_BIT;
69 else if((current_time()-led1_time) > led1){
70 IO0PIN=IO0PIN^LED1_BIT;
74 if(led2 == 0) IO0SET=LED2_BIT;
75 else if(led2 == 1) IO0CLR=LED2_BIT;
76 else if((current_time()-led2_time) > led2){
77 IO0PIN=IO0PIN^LED2_BIT;
85 //opening file descriptor
86 ul_fd = ul_open(NULL,NULL);
87 ul_fd1 = ul_open(NULL,NULL);
88 if ((ul_fd == UL_FD_INVALID) || (ul_fd1 == UL_FD_INVALID)){
89 printf("error in creating ul_fd\n");
93 //memory set and filter set
94 memset(&msginfo,0,sizeof(msginfo));
95 addfilt=ul_addfilt(ul_fd,&msginfo);
97 printf("spy_messages : add filter failed\n");
101 ul_setmyadr(ul_fd,99);
102 ul_setidstr(ul_fd,ul_idstr);
104 // uLan object interface init
105 coninfo->cmd = UL_CMD_OISV;
108 coninfo->ul_fd = ul_fd;
109 coninfo->ul_fd1 = ul_fd1;
114 uldy_init(ul_dyac,ul_fd,ul_save_sn,ul_save_adr,(char*)ul_idstr,sn);
124 uchar buf_out[ULDY_TMBUF_LEN];
125 buf_out[0]=ULNCS_ADR_RQ;
126 long2buf(&buf_out[1],sn);
128 //address set and sign on the ulan
129 msgsend = ul_send_command(ul_fd, msginfo.dadr, UL_CMD_NCS, UL_BFL_NORE, (void*) buf_out, ULDY_TMBUF_LEN);
130 printf("[I] sn was send\n");
134 void heartbeat(void){
137 int len = HEART_BUF_LEN;
141 buf = (uchar *) malloc(len*sizeof(uchar)); // NULL NULL NULL CID_HEART status_val
142 for (i=0; i<=2; i++) buf[i] = 0;
143 int2buf(&buf[3],HEART_CID);
145 int2buf(&buf[7],status_val);
147 msgsend = ul_send_command(ul_fd, ul_dyac->ul_dysa, UL_CMD_PDO, UL_BFL_NORE, (void*) buf, HEART_BUF_LEN);
148 printf("[I] BEAT\n");
152 void accept_SDO(void) {
153 // setting lan configuration
154 if ((ul_inepoll(ul_fd)>0) && (ul_acceptmsg(ul_fd, &msginfo)>=0)) {
155 if (msginfo.cmd == UL_CMD_PDO){
158 printf("accept PDO msg: sadr: %i, dadr: %i\n", msginfo.sadr, msginfo.dadr);
159 } else if (msginfo.sadr != 99) {
160 if (!(msginfo.flg&(UL_BFL_PROC | UL_BFL_FAIL))) {
161 //waiting for msg from bus
162 if (uloi_process_msg(ULOI_ARG_coninfo (uloi_objdes_array_t*)&uloi_objdes_main, &msginfo)<0) {
163 //checking dynamic adresation
164 if (uldy_process_msg(ULDY_ARG_ul_dyac &msginfo)>=0){
165 printf("message recieved - UL_CMD_NCS, sadr: %i, dadr: %i, cmd: %i\n",msginfo.sadr, msginfo.dadr, msginfo.cmd);
166 printf("server addr :%i\n",ul_dyac->ul_dysa);
169 printf("problem in uldy_process_msg\n");
172 printf("ULOI message processed: sadr: %i, dadr: %i\n", msginfo.sadr, msginfo.dadr);
181 void process_PDO(void){
191 buf_len = msginfo.len;
192 buf = (uchar *) malloc(buf_len*sizeof(uchar));
193 ul_read(ul_fd, buf, buf_len);
195 if ((position+2) > buf_len) break;
196 cid = take_cid(&buf[position]);
199 if ((position+2) > buf_len) break;
200 len_data = take_len(&buf[position]);
203 if ((position+len_data) > buf_len) break;
204 data = take_data(&buf[position], len_data);
205 position += len_data;
207 check_PDO(cid, data);
211 uint take_cid(uchar * buf){
212 return buf2uint(buf);
215 int take_len(uchar * buf){
219 int take_data(uchar * buf, int len){
220 if (len == 2) return buf2int(buf);
224 char ul_save_sn(uint32_t usn){
228 char ul_save_adr(uint8_t uaddr){