Add gitlab issue/merge request templates
[apps/agl-service-can-low-level.git] / low-can-binding / utils / socketcan.cpp
index 71588a6..e8e0198 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2015, 2016 ,2017 "IoT.bzh"
+ * Copyright (C) 2015, 2016 , 2017, 2018, 2019 "IoT\.bzh"
  * Author "Romain Forlot" <romain.forlot@iot.bzh>
  * Author "Loïc Collignon" <loic.collignon@iot.bzh>
  * Licensed under the Apache License, Version 2.0 (the "License");
@@ -29,7 +29,9 @@ namespace utils
        /// @brief Construct a default, invalid, socket.
        socketcan_t::socketcan_t()
                : socket_{INVALID_SOCKET}
-       {}
+       {
+               ::memset(&tx_address_, 0, sizeof(tx_address_));
+       }
 
        /// @brief Construct a socket by moving an existing one.
        socketcan_t::socketcan_t(socketcan_t&& s)
@@ -71,6 +73,9 @@ namespace utils
        {
                close();
                socket_ = ::socket(domain, type, protocol);
+               if (socket_ < 0)
+                       AFB_ERROR("Open failed. %s", strerror(errno));
+
                return socket_;
        }
 
@@ -94,4 +99,32 @@ namespace utils
        {
                return socket_;
        }
-}
\ No newline at end of file
+
+       /// @brief Connect the socket.
+       /// @return 0 if success.
+       int socketcan_t::connect(const struct sockaddr* addr, socklen_t len)
+       {
+               return socket_ != INVALID_SOCKET ? ::connect(socket_, addr, len) : 0;
+       }
+
+       /// @brief Bind the socket.
+       /// @return 0 if success.
+       int socketcan_t::bind(const struct sockaddr* addr, socklen_t len)
+       {
+               return socket_ != INVALID_SOCKET ? ::bind(socket_, addr, len) : 0;
+       }
+
+       int socketcan_t::write_message(std::vector<message_t>& vobj)
+       {
+               for(int i=0;i<vobj.size();i++)
+               {
+                       if(write_message(vobj[i])<0)
+                       {
+                               AFB_ERROR("Error send message %d", i);
+                               return -1;
+                       }
+               }
+               return 0;
+       }
+
+}