[Thread] Implement init/deinit socket interface

Bug: 313425570
Test: build pass & manual test
Change-Id: I34388f76972f9b88f390a04b100c2fbf61019a1d
diff --git a/threadnetwork/aidl/default/socket_interface.cpp b/threadnetwork/aidl/default/socket_interface.cpp
index 3a8b1cf..e42f03d 100644
--- a/threadnetwork/aidl/default/socket_interface.cpp
+++ b/threadnetwork/aidl/default/socket_interface.cpp
@@ -22,6 +22,17 @@
 
 #include "socket_interface.hpp"
 
+#include <errno.h>
+#include <openthread/logging.h>
+#include <sys/socket.h>
+#include <sys/stat.h>
+#include <sys/un.h>
+#include <sys/wait.h>
+#include <unistd.h>
+
+#include "common/code_utils.hpp"
+#include "openthread/openthread-system.h"
+
 namespace aidl {
 namespace android {
 namespace hardware {
@@ -37,6 +48,83 @@
     mInterfaceMetrics.mRcpInterfaceType = kSpinelInterfaceTypeVendor;
 }
 
+otError SocketInterface::Init(ReceiveFrameCallback aCallback, void* aCallbackContext,
+                              RxFrameBuffer& aFrameBuffer) {
+    otError error = OT_ERROR_NONE;
+
+    VerifyOrExit(mSockFd == -1, error = OT_ERROR_ALREADY);
+
+    mSockFd = OpenFile(mRadioUrl);
+    VerifyOrExit(mSockFd != -1, error = OT_ERROR_FAILED);
+
+    mReceiveFrameCallback = aCallback;
+    mReceiveFrameContext = aCallbackContext;
+    mReceiveFrameBuffer = &aFrameBuffer;
+
+exit:
+    return error;
+}
+
+SocketInterface::~SocketInterface(void) {
+    Deinit();
+}
+
+void SocketInterface::Deinit(void) {
+    CloseFile();
+
+    mReceiveFrameCallback = nullptr;
+    mReceiveFrameContext = nullptr;
+    mReceiveFrameBuffer = nullptr;
+}
+
+void SocketInterface::UpdateFdSet(void* aMainloopContext) {
+    otSysMainloopContext* context = reinterpret_cast<otSysMainloopContext*>(aMainloopContext);
+
+    assert(context != nullptr);
+
+    FD_SET(mSockFd, &context->mReadFdSet);
+
+    if (context->mMaxFd < mSockFd) {
+        context->mMaxFd = mSockFd;
+    }
+}
+
+int SocketInterface::OpenFile(const ot::Url::Url& aRadioUrl) {
+    int fd = -1;
+    sockaddr_un serverAddress;
+
+    VerifyOrExit(sizeof(serverAddress.sun_path) > strlen(aRadioUrl.GetPath()),
+                 otLogCritPlat("Invalid file path length"));
+    strncpy(serverAddress.sun_path, aRadioUrl.GetPath(), sizeof(serverAddress.sun_path));
+    serverAddress.sun_family = AF_UNIX;
+
+    fd = socket(AF_UNIX, SOCK_SEQPACKET, 0);
+    VerifyOrExit(fd != -1, otLogCritPlat("open(): errno=%s", strerror(errno)));
+
+    if (connect(fd, reinterpret_cast<struct sockaddr*>(&serverAddress), sizeof(serverAddress)) ==
+        -1) {
+        otLogCritPlat("connect(): errno=%s", strerror(errno));
+        close(fd);
+        fd = -1;
+    }
+
+exit:
+    return fd;
+}
+
+void SocketInterface::CloseFile(void) {
+    VerifyOrExit(mSockFd != -1);
+
+    VerifyOrExit(0 == close(mSockFd), otLogCritPlat("close(): errno=%s", strerror(errno)));
+    VerifyOrExit(wait(nullptr) != -1 || errno == ECHILD,
+                 otLogCritPlat("wait(): errno=%s", strerror(errno)));
+
+    mSockFd = -1;
+
+exit:
+    return;
+}
+
 }  // namespace threadnetwork
 }  // namespace hardware
 }  // namespace android
diff --git a/threadnetwork/aidl/default/socket_interface.hpp b/threadnetwork/aidl/default/socket_interface.hpp
index 2d1b614..5c679cd 100644
--- a/threadnetwork/aidl/default/socket_interface.hpp
+++ b/threadnetwork/aidl/default/socket_interface.hpp
@@ -49,6 +49,57 @@
     ~SocketInterface();
 
     /**
+     * Initializes the interface to the Radio Co-processor (RCP)
+     *
+     * @note This method should be called before reading and sending Spinel
+     * frames to the interface.
+     *
+     * @param[in] aCallback         Callback on frame received
+     * @param[in] aCallbackContext  Callback context
+     * @param[in] aFrameBuffer      A reference to a `RxFrameBuffer` object.
+     *
+     * @retval OT_ERROR_NONE       The interface is initialized successfully
+     * @retval OT_ERROR_ALREADY    The interface is already initialized.
+     * @retval OT_ERROR_FAILED     Failed to initialize the interface.
+     *
+     */
+    otError Init(ReceiveFrameCallback aCallback, void* aCallbackContext,
+                 RxFrameBuffer& aFrameBuffer);
+
+    /**
+     * Deinitializes the interface to the RCP.
+     *
+     */
+    void Deinit(void);
+
+    /**
+     * Updates the file descriptor sets with file descriptors used by the radio
+     * driver.
+     *
+     * @param[in,out]   aMainloopContext  A pointer to the mainloop context
+     * containing fd_sets.
+     *
+     */
+    void UpdateFdSet(void* aMainloopContext);
+
+    /**
+     * Returns the bus speed between the host and the radio.
+     *
+     * @return   Bus speed in bits/second.
+     *
+     */
+    uint32_t GetBusSpeed(void) const { return 1000000; }
+
+    /**
+     * Hardware resets the RCP.
+     *
+     * @retval OT_ERROR_NONE            Successfully reset the RCP.
+     * @retval OT_ERROR_NOT_IMPLEMENT   The hardware reset is not implemented.
+     *
+     */
+    otError HardwareReset(void) { return OT_ERROR_NOT_IMPLEMENTED; }
+
+    /**
      * Returns the RCP interface metrics.
      *
      * @return The RCP interface metrics.
@@ -71,6 +122,24 @@
     }
 
   private:
+    /**
+     * Opens file specified by aRadioUrl.
+     *
+     * @param[in] aRadioUrl  A reference to object containing path to file and
+     * data for configuring the connection with tty type file.
+     *
+     * @retval The file descriptor of newly opened file.
+     * @retval -1 Fail to open file.
+     *
+     */
+    int OpenFile(const ot::Url::Url& aRadioUrl);
+
+    /**
+     * Closes file associated with the file descriptor.
+     *
+     */
+    void CloseFile(void);
+
     ReceiveFrameCallback mReceiveFrameCallback;
     void* mReceiveFrameContext;
     RxFrameBuffer* mReceiveFrameBuffer;