CANBUS.xs

filter: Statementstmt 0.0% 0 / 27 Branchbran 0.0% 0 / 18 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 0.0% 0 / 45 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
↑ Summary blib/lib/CANBUS.pm »
1 #define PERL_NO_GET_CONTEXT
2 #include "EXTERN.h"
3 #include "perl.h"
4 #include "XSUB.h"
5 #include "ppport.h"
6  
7 #include <linux/can.h>
8 #include <linux/can/raw.h>
9 #include <net/if.h>
10 #include <sys/socket.h>
11 #include <sys/ioctl.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16  
17 static int s ;
18 static struct sockaddr_can addr;
19  
20  
21 MODULE = CANBUS PACKAGE = CANBUS
22  
23 void teardown()
24 CODE:
25 0 close(s);
26  
27 void setup(const char *iface)
28  
29 CODE:
30 // Open a socket
31 // s = socket(PF_CAN, SOCK_RAW|SOCK_NONBLOCK, CAN_RAW);
32 0 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
33  
34 // Find the interface index
35 struct ifreq ifr;
36  
37 0 strcpy(ifr.ifr_name, iface);
38 0 ioctl(s, SIOCGIFINDEX, &ifr);
39  
40 // Bind to the interface
41  
42 0 addr.can_family = AF_CAN;
43 0 addr.can_ifindex = ifr.ifr_ifindex;
44  
45 0 bind(s, (struct sockaddr *)&addr, sizeof(addr));
46  
47 void send(int id, SV *data)
48 INIT:
49 0 I32 dlc = 0;
50 int n ;
51 AV *av;
52  
53  
54 CODE:
55 0 ▶ if ((!SvROK(data)) || (SvTYPE(SvRV(data)) != SVt_PVAV))
Branch: 2 branches
BranchTrueFalseTotal
if ((!SvROK(data)) || (SvTYPE(SvRV(data)) != SVt_PVAV)) 0 0 0
if ((!SvROK(data)) || (SvTYPE(SvRV(data)) != SVt_PVAV)) 0 0 0
56 0 croak("First argument must be an array reference");
57  
58 0 av = (AV *)SvRV(data);
59 0 dlc = av_len(av) + 1 ;
60  
61  
62 // Send a frame
63 struct can_frame frame;
64  
65 0 ▶ for (n = 0; n < dlc; n++) {
Branch: 1 branch
BranchTrueFalseTotal
for (n = 0; n < dlc; n++) { 0 0 0
66 0 SV **svp = av_fetch(av, n, 0);
67 0 ▶ if (svp) {
Branch: 1 branch
BranchTrueFalseTotal
if (svp) { 0 0 0
68 0 frame.data[n] = SvIV(*svp);
69 }
70 }
71  
72 0 frame.can_id = id;
73 0 frame.can_dlc = dlc;
74  
75 int nb ;
76 0 nb = write(s, &frame, sizeof(frame));
77 0 ▶ if (nb != 16) printf("wrote %d bytes not 16\n", nb);
Branch: 1 branch
BranchTrueFalseTotal
if (nb != 16) printf("wrote %d bytes not 16\n", nb); 0 0 0
78  
79  
80 void receive()
81 INIT:
82 int nd ;
83  
84 PPCODE:
85  
86 // Receive a frame
87 struct can_frame frame;
88  
89 0 nd = read(s, &frame, sizeof(frame));
90 0 ▶ if (nd != 16) printf("read %d bytes not 16\n", nd);
Branch: 1 branch
BranchTrueFalseTotal
if (nd != 16) printf("read %d bytes not 16\n", nd); 0 0 0
91
92 0 ▶ EXTEND(SP, 2);
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, 2); 0 0 0
93 0 PUSHs(sv_2mortal(newSViv(frame.can_id & CAN_SFF_MASK)));
94 0 PUSHs(sv_2mortal(newSViv(frame.can_dlc)));
95  
96 0 ▶ EXTEND(SP, frame.can_dlc) ;
Branch: 1 branch
BranchTrueFalseTotal
EXTEND(SP, frame.can_dlc) ; 0 0 0
97 0 ▶ for(int i=0;i<frame.can_dlc;i++) PUSHs(sv_2mortal(newSViv(frame.data[i])));
Branch: 1 branch
BranchTrueFalseTotal
for(int i=0;i<frame.can_dlc;i++) PUSHs(sv_2mortal(newSViv(frame.data[i]))); 0 0 0
↑ Summary blib/lib/CANBUS.pm »