Coming out. My name is Eric Evanczyk. I work for a company called Atreides. We do embedded and mobile and all sorts of assessments of things. And I also play with cars a bunch and can bus a bunch, and that's kind of my deal. So recently I was playing around with some tools, and I was like, hey, Golang is kind of nifty. I want to play around with cars and Golang at the same time. So we have the little Go-Gopher, and we have your quintessential car hacker, because that's how you hack a car. You pick a MacBook, wear a balaclava, you're good to go. So has anyone here written some can bus tools? Maybe played around with socket can, written some stuff? Maybe some folks in the audience. I don't know. But if you want to write a tool, you need some things. And the first thing is, because cars mostly talk can nowadays, you need to be able to talk can. That's like a first pass thing. Concurrency is super. It's super useful. And the reason for this is whenever you're dealing with a can bus, there's a lot of times that you're sitting there and you're waiting for a frame to come in, and you're waiting, and you want to do other things. You want to be able to time out, you want to fail nicely, you want to be able to do that sort of thing. If you don't have concurrency, then you deadlock, you know, things like that. So having access to nice concurrency features is a really, really good thing when you're writing these types of tools. So we're going to pick a language that we're going to write our tool in, and traditionally, a lot of these have been C and C++ and Python. I've written a lot of stuff in Python, but I'm not smart enough to understand Python's new AsyncIO because it's crazy. And Pthreads are a bit of a pain, so that's why you might want Go. There's a few reasons. Well, it's in concurrency, so anyone in the audience written a line of Golang in their life? Maybe? A couple people. Cool. If you haven't, I recommend it. The tutorial on the Golang website, it, like, runs in your browser, and you can step through it. It's got lots of great examples. It's lovely. But one really nifty thing about Go is it's got these things that it calls Go routines, which is a pun, which is one reason I love the language. It's got puns. And that is actually a co-routine, so it will allow you to run a separate, you know, not thread. It's a co-routine, but it's run over here at the same time, and it also has channels, which allow you to do synchronized communication back and forth between these different routines that are running. And all this is implemented in the Go routine. So if you run it on Windows, Linux, OSX, whatever, you get the same sort of thing happening. You're not relying on some OS API. So the concurrency that's built in is really nice. It was thought of originally because Google wrote this language in order to actually, like, write web servers, where concurrency is kind of important. It's typed, which, if you've ever, like, started writing a project in Python and it got to a certain size, you probably went, I regret choosing Python now. Typing is really nice. It's where you are passing things around with different modules and things like that, and you want the data to not go to space. So I like typed languages, and, yeah. It's a compiled language, and this is great because often, when you're using scripting languages, you write, you know, a tool in Python, whatever it is, and then you have all these dependencies you've got to pull down, and if they have the wrong version, it's not going to work, and they're not online, they can't pull it down from pip and all that sort of stuff. You build a Go binary, you can build a, like, statically linked binary that's a single binary that you run it, and it works. And as of the whole purpose of this talk is, as of right now, it supports SocketCAN, which means that we can do CAN bots from it. So SocketCAN, what is this? I think there's talks on SocketCAN here. I'm going to be kind of brief. But SocketCAN is CAN over standard Unix sockets. So Unix sockets, if you've ever written any code on Unix or any Unix system, some web server, something like that, you know, you have these, Socket has a file descriptor, you set the thing up, you bind to it, and then you can read and write to it. You can also do a lot of other things, but that's the basic functionality. And that's usually used for network stuff, TCP IP, you know, that's how it's been used traditionally. But we can also do CAN from it. Any questions here? Is this available online? Slides? I will make them available. I will toss them in with the repo for this. So whether this works for you when you're doing CAN, you have an application, and I stuck the little gopher in a plane, because that's going to be written in Go. And then that's going to talk to the socket layer of your OS, which is in the kernel. And where SocketCAN jumps in here, you know, most of the time you'd have a protocol family, which is your standard Ethernet, Internet protocols, UDP, TCP. But we can have a CAN protocol family, and we can talk to it that way. And then that will talk to the device drivers and eventually the actual hardware. And this is super nice because it means it's hardware agnostic. If your hardware device supports SocketCAN, you don't care what device it is. You just send frames, and it works. Except for when it doesn't. But usually if they've implemented it right, it works. And so it's hardware agnostic. We don't care. As long as this thing is going to create a socket, we can make it go. So how do we do this in Go? Well, we can do this like any other socket we do. You know, file descriptor and error is equal to. We're going to make a Unix socket. The family there is CAN. It's going to be a raw CAN socket, and the type is CANRAW. And then we, you know, do this, set up an address. Usually this would be like your IP address you're binding to, and your port you're binding to. But now we need to, like, make a SocketCAN one. Then we bind it. This is kind of ugly, though. You don't want to do this every time you're writing something. So let's make a library. There's some other stuff that you need to do. And one of them that's kind of annoying is you've got to pack and unpack the structs that SocketCAN expects. Because SocketCAN, the way it ends up working, is there's a header file that's got these structs defined for, like, a CAN frame, for example. And, you know, Go doesn't interoperate that nicely with C structs, unfortunately. Rust does it better, but hey. So you need to pack these up the right way. You need to unpack them the right way. You need to set some SOC options, blah, blah, blah. You don't want to do this every time you write, like, little scripts. So we make a library. And this makes it a lot easier. So what you end up doing is you just create a device. This is a raw interface. You know, SocketCAN dot new rounded face. And you give it the, that CANIF is literally the string of the name, you know, CAN0, CAN1, whatever. Go's got this nice feature called defer, which stops you from leaving devices open by mistake. So you can say defer device dot close. And whenever that device goes out of scope, it'll call the close function and clean it up for you. Which is great, because I always forget to write those at the end of my functions. And then I end up with all these dangling file descriptors. And eventually, your OS goes, I won't let you create anymore. And then what you have is the ability to receive and send frames. So these are actually now Go structs. And those fields that go struct to the RBID, the data length code, and the data. And you can just send and receive those, work with them as normal structs to all the sort of normal Go things that you would do on them. And it just makes it a lot easier. Now, another thing that you might want to do is ISOTP, which is used for diagnostics in automotive. I won't get super into it. The real quick explanation is CAN frames are 8 bytes. You want to send more than 8 bytes, there's a sort of higher level protocol that will let you send up to 4095 bytes in multiple frames. And yes, it also does this. So there is a kernel module called CAN ISOTP. Unfortunately, unlike other kernel modules for socket CAN, it is not typically built into the kernel or available on most distros. You need to go download it and build it yourself. It's really easy. It's like you get, pull the thing and type make and then sudo insmod it. It's like three steps. But once you get that up and going, now you have inside the kernel the ability to do this chaining of messages so you can send larger buffers. That's nice because in the kernel it's not in your application, meaning one, you don't have to write the code for it, which is awesome. Laziness is good. But also, quite importantly, it's going to be faster because you don't have all the delays of stuff like going up to the application layer, every frame, and then sending it back down. It's happening in the kernel nice and quick. And this works very, very similarly. The difference is we now need to provide a RX and TX ID. So whenever you do ISOTP, you're going to be targeting a specific CAN ID and expecting data back on a specific CAN ID. So we enter those. We defer our device.close, which is the same thing. It's going to close down. And now instead of sending specific frames, we actually just send go byte arrays, so just an array of up to 4095 bytes. You shoot that out down the wire, and it'll come back with those. If it sees a valid one, it will trigger the send buff. And this is where the receive buff will actually complete. This is where it's very useful to have concurrency because you can sit in a go routine and wait for a buffer and maybe even receive a buffer and check some things to make sure it's actually the one that you want and it's got the right things. And then you can actually send it out. But you can be doing other things in the background while you're waiting for that, and you can have timeouts and things like that. What's really convenient is you can do both these things at the same time. So you can have a raw interface and an isotp interface, and you can send isotp data and then be receiving raw frames at the same time. You can have multiple raw interfaces if you wanted to. You can have multiple isotp interfaces that are listening and sending on different addresses. That's just handled by, you know, the Linux device or network device subsystem knows how to do that, so you don't have to do it. You don't have to deal with all the synchronous accessing of the hardware. It's all just done for you. So that's really convenient. So let's just do a little demo. All the code for this is, in fact, online. And there's like this really hard problem of trying to make VMs... Sure, let's go full screen mode. VMs look sort of legible on a projector. And also my resolution is like... We're back in the 90s here. With the plus. Cool. We'll roll with this. And actually, if we could, just be nice to you and set the background color to white. I think that will... White. Cool. Hopefully a little more contrast. So first off, we have tests, which we can run. Why don't you do a touch V. So tests are nice. They actually test itself and make sure that CAN frames get sent and received and are in the right format. And it's actually doing this all using what's called the V-CAN module. And that's a virtual CAN driver on socket CAN. So none of this has to leave my computer. I don't need an actual piece of hardware in CAN bus. I can just send it around inside the kernel. But all this would work and does work if you have an external device the exact same way. But it's just nice for testing because, you know, I don't want it to plug in two CAN things and, like, make a loop back and all that just to test my library. So some of the tools that are as examples in here... This window is going to be trouble. Okay. Two examples are a pretty simple implementation of CAN dump and CAN send. To give you an idea, like... Go in there. You know, this is... I didn't count earlier. It's going to look awful on a projector, but it's, like, well under 100 lines of code. Like, it's, like, 30 lines of code or something. And we can run CAN dump on interface. And what it's going to do is... Ooh, V-CAN 0. And we can do our typical... Run our system CAN send on V-CAN 0 with 1, 2, 3, I don't know, whatever. And it's, you know, working as you'd expect with a CAN dump tool. And so that example's in there. And there's a similar one for CAN send. What's also nice is we can use that ISOTP stuff and run the tests again. And we'll actually see ISOTP data fly back around. It's actually just loading up. So the first one that it's doing is a single... If you look at the tests, it'll tell you what they were. There was a RXTX, a single frame, and RXTX, a multi-frame. So the first one is a SF, single frame. And it comes back. And then we do a first frame, and we're going to send the full 4,095 bytes of data. And we just have a buffer that goes up. And as you can see, the SN there is the serial number of the frame. And it's going to keep... First frame, you have a flow control frame. And after that, you have all these consecutive frames for 4,095 bytes, which goes on for a while and comes out. So if you're writing your own tools, writing something like a diagnostic tool, that's really handy. You don't need to deal with all that framing. You just put your UDS request together and send it and wait for one to come back. And all that's taken care of by the kernel. And let's jump back here. I thought I had a slide about next steps in this thing. So I'll tell you. This is pretty proof-of-concept-y now, but I'm using it in some actual projects. So I'm going to keep contributing or making commits to the Go stuff in here as I need features. There's some weirdness around how the ISOTP kernel module deals with some SOC options for, like, does it pad the frames out to 8 bytes? They did it in a weird way, so I have to figure out how to make that work. But that's all going to get done so that I can actually control it. And you'll have a nice API rather than having to do these, you know, set SOC ops with weird structs. So, yeah, that's the idea there. And it's just another option if you're trying to write CAN bus tools. You've got something that does socket CAN or you want to play around with V-CAN on your device. That's a way to play with it. I don't know how I'm doing on time. That was pretty quick, but I'm happy to take any questions or comments or whatever you'd like to do. But thanks for coming and filling up the room. It was nice.