|
1 /* |
|
2 This file is part of CanFestival, a library implementing CanOpen Stack. |
|
3 |
|
4 Copyright (C): Edouard TISSERANT |
|
5 |
|
6 See COPYING file for copyrights details. |
|
7 |
|
8 This library is free software; you can redistribute it and/or |
|
9 modify it under the terms of the GNU Lesser General Public |
|
10 License as published by the Free Software Foundation; either |
|
11 version 2.1 of the License, or (at your option) any later version. |
|
12 |
|
13 This library is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 Lesser General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU Lesser General Public |
|
19 License along with this library; if not, write to the Free Software |
|
20 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
|
21 */ |
|
22 |
|
23 #include "Socket.h" |
|
24 |
|
25 #include <iostream> |
|
26 |
|
27 using namespace std; |
|
28 |
|
29 extern "C" { |
|
30 #include "can_driver.h" |
|
31 } |
|
32 |
|
33 //------------------------------------------------------------------------ |
|
34 extern "C" |
|
35 UNS8 canReceive_driver(CAN_HANDLE fd0, Message *m) |
|
36 { |
|
37 |
|
38 string l = reinterpret_cast<SocketClient*>(fd0)->ReceiveLine(); |
|
39 |
|
40 int res = sscanf(l.c_str(),"{0x%3hx,%1hhd,%1hhd,{0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx,0x%2hhx}}", |
|
41 &m->cob_id, |
|
42 &m->rtr, |
|
43 &m->len, |
|
44 &m->data[0], |
|
45 &m->data[1], |
|
46 &m->data[2], |
|
47 &m->data[3], |
|
48 &m->data[4], |
|
49 &m->data[5], |
|
50 &m->data[6], |
|
51 &m->data[7] |
|
52 ); |
|
53 |
|
54 |
|
55 #if defined DEBUG_MSG_CONSOLE_ON |
|
56 printf("in : "); |
|
57 print_message(m); |
|
58 #endif |
|
59 |
|
60 return res==11 ? 0 : 1 ; |
|
61 } |
|
62 |
|
63 extern "C" |
|
64 UNS8 canSend_driver(CAN_HANDLE fd0, Message *m) |
|
65 { |
|
66 char s[1024]; |
|
67 sprintf(s,"{0x%3.3x,%1d,%1d,{0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x,0x%2.2x}}", |
|
68 m->cob_id, |
|
69 m->rtr, |
|
70 m->len, |
|
71 m->data[0], |
|
72 m->data[1], |
|
73 m->data[2], |
|
74 m->data[3], |
|
75 m->data[4], |
|
76 m->data[5], |
|
77 m->data[6], |
|
78 m->data[7] |
|
79 ); |
|
80 |
|
81 reinterpret_cast<SocketClient*>(fd0)->SendLine(s); |
|
82 #if defined DEBUG_MSG_CONSOLE_ON |
|
83 printf("out : "); |
|
84 print_message(m); |
|
85 #endif |
|
86 return 0; |
|
87 } |
|
88 |
|
89 extern "C" |
|
90 CAN_HANDLE canOpen_driver(s_BOARD *board) |
|
91 { |
|
92 Socket* s; |
|
93 try { |
|
94 s = new SocketClient(board->busname, 11898); |
|
95 |
|
96 //s.SendLine("GET / HTTP/1.0"); |
|
97 //s.SendLine("Host: www.google.com"); |
|
98 } |
|
99 catch (const char* _s) { |
|
100 cerr << _s << endl; |
|
101 return NULL; |
|
102 } |
|
103 catch (std::string _s) { |
|
104 cerr << _s << endl; |
|
105 return NULL; |
|
106 } |
|
107 catch (...) { |
|
108 cerr << "unhandled exception\n"; |
|
109 return NULL; |
|
110 } |
|
111 return (CAN_HANDLE) s; |
|
112 } |
|
113 |
|
114 extern "C" |
|
115 int canClose_driver(CAN_HANDLE inst) |
|
116 { |
|
117 delete reinterpret_cast<SocketClient*>(inst); |
|
118 return 1; |
|
119 } |
|
120 |