56#define SCI_ERR(errcode) (SCI_ERROR | ((errcode)<<16))
58#define SCI_ERR_RC(rc,errcode) (SCI_ERROR | ((errcode)<<16) | (rc))
82 unsigned char realport;
85 unsigned char reallength;
93static pthread_mutex_t global_semaphore;
105static void mldelay(
long milliseconds)
108 {
struct timespec rq,rt;
111 if (milliseconds >= 1000L)
112 { rq.tv_sec = milliseconds / 1000L;
113 rq.tv_nsec= (milliseconds-1000L*rq.tv_sec) * 1000000L;
117 rq.tv_nsec= milliseconds * 1000000L;
119 nanosleep( &rq, &rt);
126static bool mutex_init(pthread_mutex_t *mutex)
131 pthread_mutexattr_t mutexattr;
133 rc= pthread_mutexattr_init(&mutexattr);
138 rc= pthread_mutexattr_setprotocol(&mutexattr, PTHREAD_PRIO_INHERIT);
143 rc= pthread_mutex_init(mutex, &mutexattr);
151_Atomic
static int _lock= 0;
153static int initialized= 0;
158static bool init_sci_lib(
void)
173 while (initialized==0)
181 if (!mutex_init(&global_semaphore))
188 ps= &(sci_plug_in[i]);
202static bool map_socan_rc(
socan_rc rc,
264 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
292 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
315 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
325#define CCODE(x) if (val==x) return(#x)
327char *sci_str_status(sci_Return val)
338 fprintf(stderr,
"%s line %d: unknown status code\n", __FILE__, __LINE__);
339 return(
"UNKNOWN STATUS!");
342void sci_print_status(sci_Return flag)
344 puts(sci_str_status(flag));
373 fprintf(stderr,
"%s line %d: unknown error code\n", __FILE__, __LINE__);
374 return(
"UNKNOWN ERROR CODE!");
379 puts(sci_str_err(err));
401 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
404 *ps= &(sci_plug_in[plug]);
405 if ((*ps)->real_port==-1)
407 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
413static bool plug_real_port(
unsigned short port,
unsigned short *realport)
423 if (!plug_get(port, &ps))
427 *realport= ps->real_port;
431sci_Return sci_def_plug_in(
unsigned short plug_port,
432 unsigned short real_port,
450 pthread_mutex_lock(&global_semaphore);
452 if (ps->real_port!=-1)
455 pthread_mutex_unlock(&global_semaphore);
458 ps->real_port= real_port;
462 pthread_mutex_unlock(&global_semaphore);
470sci_Return sci_open(
sci_Struc **s_struc,
const char *app_name,
487 pthread_mutex_lock(&global_semaphore);
491 pthread_mutex_unlock(&global_semaphore);
498 scistruc_p->hdl= hdl;
500 *s_struc= scistruc_p;
501 pthread_mutex_unlock(&global_semaphore);
511 pthread_mutex_lock(&global_semaphore);
514 pthread_mutex_unlock(&global_semaphore);
532sci_Return sci_reset(
sci_Struc *s_struc,
unsigned short port)
547 if (!plug_real_port(port, &port))
550 return SCI_ERR(s_struc->errcode);
554 if (!map_socan_rc(rc, &sci_rc, &sci_err))
556 s_struc->errcode= sci_err;
557 return SCI_ERR_RC(sci_rc, sci_err);
593 return SCI_ERR(s_struc->errcode);
605 *errcode= s_struc->errcode;
614 unsigned short port,
int id)
624 if (!plug_real_port(port, &port))
627 return SCI_ERR(s_struc->errcode);
631 if (!map_socan_rc(rc, &sci_rc, &sci_err))
633 s_struc->errcode= sci_err;
634 return SCI_ERR_RC(sci_rc, sci_err);
638 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
651 return (object->userp);
659 unsigned short *port,
int *
id,
int *length,
669 object->cob, &l, &tmo, &tp);
670 if (!map_socan_rc(rc, &sci_rc, &sci_err))
672 s_struc->errcode= sci_err;
673 return SCI_ERR_RC(sci_rc, sci_err);
677 *length=
object->length;
679 if (map_socan_type(tp, type))
682 return SCI_ERR_RC(rc, s_struc->errcode);
692 unsigned long long *pstamp,
int *pvalid,
693 unsigned char *mode,
unsigned char *irq_cnt)
701 if (!map_socan_rc(rc, &sci_rc, &sci_err))
703 s_struc->errcode= sci_err;
704 return SCI_ERR_RC(sci_rc, sci_err);
717sci_Return sci_obj_dump_buf(
char *buf,
726 const char *str=
"UNKNOWN TYPE!";
728 sci_rc= sci_get_info(s_struc,
object, &port, &
id, &length,
731 { sprintf(buf,
"sci_obj_dump_buf: sci_get_info failed\n");
735 {
case SCI_READ: str=
"SCI_READ";
break;
740 sprintf(buf,
"id:%d length:%d tmout:%d port:%d type:%s\n",
741 id,length,timeout,port,str);
755 unsigned short port,
int id,
int length,
763 unsigned short realport= port;
764 unsigned char reallength= (
unsigned char)length;
769 if (!plug_get(port, &ps))
772 return SCI_ERR(s_struc->errcode);
778 realport= ps->real_port;
779 if (!(ps->lcalc)(length, &l))
782 return SCI_ERR(s_struc->errcode);
784 reallength= (
unsigned char)l;
786 if (!map_sci_type(type, &stype))
789 return SCI_ERR(s_struc->errcode);
794 (
unsigned int)timeout, stype);
795 if (!map_socan_rc(rc, &sci_rc, &sci_err))
797 s_struc->errcode= sci_err;
798 return SCI_ERR_RC(sci_rc, sci_err);
802 pthread_mutex_lock(&global_semaphore);
804 if (!map_socan_rc(rc, &sci_rc, &sci_err))
806 s_struc->errcode= sci_err;
807 pthread_mutex_unlock(&global_semaphore);
808 return SCI_ERR_RC(sci_rc, sci_err);
812 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
814 pthread_mutex_unlock(&global_semaphore);
815 return SCI_ERR(s_struc->errcode);
817 for(parent= root; parent->child!=NULL; parent= parent->child);
820 new->port= parent->port;
821 new->realport= parent->realport;
822 new->cob= parent->cob;
823 new->length= parent->length;
824 new->reallength= parent->reallength;
826 pthread_mutex_unlock(&global_semaphore);
832 if (!map_socan_rc(rc, &sci_rc, &sci_err))
834 s_struc->errcode= sci_err;
835 return SCI_ERR_RC(sci_rc, sci_err);
839 obj->realport= realport;
841 obj->length= (
unsigned char)length;
842 obj->reallength= reallength;
868 rc=
socan_user_area((*object)->realport, (*object)->cob, (
void**)(&root));
869 if (!map_socan_rc(rc, &sci_rc, &sci_err))
871 s_struc->errcode= sci_err;
872 return SCI_ERR_RC(sci_rc, sci_err);
877 return SCI_ERR(s_struc->errcode);
879 pthread_mutex_lock(&global_semaphore);
882 if ((*object)->child != NULL)
886 if (!map_socan_rc(rc, &sci_rc, &sci_err))
888 fprintf(stderr,
"%s line %d: internal error\n",
890 s_struc->errcode= sci_err;
891 pthread_mutex_unlock(&global_semaphore);
892 return SCI_ERR_RC(sci_rc, sci_err);
896 pthread_mutex_unlock(&global_semaphore);
904 (parent->child!=(*object)) && (parent->child!=NULL);
905 parent= parent->child);
906 if (parent->child != (*
object))
908 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
910 pthread_mutex_unlock(&global_semaphore);
911 return SCI_ERR(s_struc->errcode);
913 parent->child= (*object)->child;
916 pthread_mutex_unlock(&global_semaphore);
921 if (!map_socan_rc(rc, &sci_rc, &sci_err))
923 s_struc->errcode= sci_err;
924 pthread_mutex_unlock(&global_semaphore);
925 return SCI_ERR_RC(sci_rc, sci_err);
928 pthread_mutex_unlock(&global_semaphore);
938sci_Return sci_link_object(
sci_Struc *s_struc,
939 unsigned short read_port ,
int read_id,
940 unsigned short write_port,
int write_id,
941 int length,
int timeout,
942 void *read_user,
void *write_user)
950 unsigned long inhibit_time)
957 if (!map_socan_rc(rc, &sci_rc, &sci_err))
959 s_struc->errcode= sci_err;
960 return SCI_ERR_RC(sci_rc, sci_err);
978 if (!map_socan_rc(rc, &sci_rc, &sci_err))
980 s_struc->errcode= sci_err;
981 return SCI_ERR_RC(sci_rc, sci_err);
992 char *data,
unsigned long *rest_time)
1000 if (!plug_get(object->port, &ps))
1003 return SCI_ERR(s_struc->errcode);
1009 (ps->h2l)(object->reallength, buf, object->length, data);
1013 if (rest_time==NULL)
1019 if (!map_socan_rc(rc, &sci_rc, &sci_err))
1021 s_struc->errcode= sci_err;
1022 return SCI_ERR_RC(sci_rc, sci_err);
1030 return can_write(s_struc,
object, data, NULL);
1034 char *data,
unsigned long *rest_time)
1037 return can_write(s_struc,
object, data, rest_time);
1045 char *data,
bool read_now)
1053 if (!plug_get(object->port, &ps))
1056 return SCI_ERR(s_struc->errcode);
1059 rc=
socan_readnow(s_struc->hdl, object->realport, object->cob, data);
1061 rc=
socan_read(s_struc->hdl, object->realport, object->cob, data);
1063 if (!map_socan_rc(rc, &sci_rc, &sci_err))
1065 s_struc->errcode= sci_err;
1066 return SCI_ERR_RC(sci_rc, sci_err);
1072 (ps->l2h)(object->reallength, data, object->length, buf);
1073 memcpy(data, buf, object->length);
1081 return can_read(s_struc,
object, data,
true);
1092 return can_read(s_struc,
object, data,
false);
1112 if (!map_socan_rc(rc, &sci_rc, &sci_err))
1114 s_struc->errcode= sci_err;
1115 return SCI_ERR_RC(sci_rc, sci_err);
1118 if (!map_socan_rc(rc, &sci_rc, &sci_err))
1120 s_struc->errcode= sci_err;
1121 return SCI_ERR_RC(sci_rc, sci_err);
1125 fprintf(stderr,
"%s line %d: internal error\n", __FILE__, __LINE__);
1128 if (!plug_get(obj->port, &ps))
1131 return SCI_ERR_RC(sci_rc, sci_err);
1138 (ps->l2h)(obj->reallength, data, obj->length, buf);
1139 memcpy(data, buf, obj->length);
c header file for the simple CAN interface (sci)
#define SCI_PLUGPORT_START
void(* sci_Plug_Func)(int l_len, char *l_data, int h_len, char *h_data)
enum sci_Errcode_Constants sci_Errcode
#define SCI_MAX_PLUGPORTS
int(* sci_Plug_Lencalc)(int h_len, int *l_len)
enum sci_Bitrate_Constants sci_Bitrate
socan_rc socan_user_area(unsigned char port, unsigned short cob, void **area)
bool socan_close(socan_hdl h)
socan_rc socan_write_inhibit(socan_hdl hdl, unsigned char port, unsigned short cob, const void *data, unsigned long *inhibit_time)
socan_rc socan_set_user_area(unsigned char port, unsigned short cob, void *ptr)
socan_rc socan_port_bitrate(unsigned char port, unsigned int *bitrate)
socan_rc socan_del_obj(unsigned char port, unsigned short cob)
socan_rc socan_read(socan_hdl hdl, unsigned char port, unsigned short cob, void *data)
socan_rc socan_obj_ts(unsigned char port, unsigned short cob, unsigned long *ts)
socan_rc socan_use_queue(unsigned char port, unsigned short cob)
socan_rc socan_readnow(socan_hdl hdl, unsigned char port, unsigned short cob, void *data)
socan_rc socan_set_inhibit(unsigned char port, unsigned short cob, unsigned long inhibit_time)
socan_hdl socan_open(void)
socan_rc socan_queue_read(socan_hdl hdl, unsigned char *port, unsigned short *cob, void *data)
socan_rc socan_add_obj(socan_hdl hdl, unsigned char port, unsigned short cob, unsigned char length, unsigned int tmo_ms, socan_obj_type type)
socan_rc socan_writelater(socan_hdl hdl, unsigned char port, unsigned short cob, const void *data)
socan_rc socan_obj_info(socan_hdl hdl, unsigned char port, unsigned short cob, unsigned char *length, unsigned int *tmo_ms, socan_obj_type *type)
socan_rc socan_new_user_area(unsigned char port, unsigned short cob, unsigned int size, void **area)
c header file for socan object layer library.
#define SOCAN_FRAME_LENGTH