Esempio n. 1
0
/**
 * This method will be invoked whene the rw.cli executes a command and 
 * requires a transport to send the message.
 */
static rw_status_t messaging_hook(NetconfReq *req, NetconfRsp **rsp)
{
  // Send message to the CLI-AGENT and wait for a message back
  unsigned msg_len = netconf_req__get_packed_size(NULL, req);
  uint8_t msg_buf[msg_len];
  rw_status_t status;
  
  netconf_req__pack(NULL, req, msg_buf);

  if (rwcli_agent_ch.in.fd.write == -1) {
    RWTRACE_CRIT(rwcli_trace, RWTRACE_CATEGORY_RWCLI,
        "Messaging not initialized, failed to execute the command");
    return RW_STATUS_FAILURE;
  }

  // Consume any prvious unread messages on the stream
  consume_unread_agent_messages();

  // TODO handle EPIPE
  write(rwcli_agent_ch.in.fd.write, (const void*)(&msg_len), sizeof(unsigned));
  write(rwcli_agent_ch.in.fd.write, msg_buf, msg_len);

  RWTRACE_DEBUG(rwcli_trace, RWTRACE_CATEGORY_RWCLI,
      "\nSHELL: sent %d bytes to CLI-AGENT", msg_len);

  status = recv_msg_from_agent(&msg_len);
  if (status != RW_STATUS_SUCCESS) {
    *rsp = NULL;
    return status;
  }

  RWTRACE_DEBUG(rwcli_trace, RWTRACE_CATEGORY_RWCLI,
      "\nSHELL: received %u bytes from CLI-AGENT msglen\n", msg_len);

  rw_resp = netconf_rsp__unpack(NULL, msg_len, recv_buf);
  if (rw_resp == NULL) {
    RWTRACE_ERROR(rwcli_trace, RWTRACE_CATEGORY_RWCLI,
          "\nReceived message unpack failed\n");
    *rsp = NULL;
    return RW_STATUS_FAILURE;
  }

  *rsp = rw_resp;

  return RW_STATUS_SUCCESS;
}
Esempio n. 2
0
/**
 * Handler for Netconf message type. Sends the encoded the NetconfReq protobuf
 * and receives the decoded NetconfRsp protobuf.
 *
 * @param[in] inst - RW.CLI Controller instance
 * @param[in] req  - Netconf Request decoded protobuf
 * @param[out] rsp - Netconf response (decoded)
 *
 * @returns RW_STATUS_SUCCESS on success, RW_STATUS_FAILURE otherwise.
 */
static rw_status_t controller_handle_rwnetconf_msg(
                    rwcli_controller_t* inst,
                    NetconfReq* req, NetconfRsp **rsp)
{
  unsigned recv_msg_len = 0;
  unsigned msg_len = netconf_req__get_packed_size(NULL, req);
  uint8_t msg_buf[msg_len];
  rw_status_t status;
 
  // Encode the protobuf
  netconf_req__pack(NULL, req, msg_buf);

  status = controller_execute(inst, inst->agent_type,
                              RWCLI_MSG_TYPE_RW_NETCONF,
                              msg_buf, msg_len, &recv_msg_len);
  if (status != RW_STATUS_SUCCESS) {
    *rsp = NULL;
    return status;
  }

  // Decode the response
  inst->resp = netconf_rsp__unpack(NULL, recv_msg_len, 
                                  inst->recv_buf);
  if (inst->resp == NULL) {
    RWTRACE_ERROR(inst->trace_ctxt, RWTRACE_CATEGORY_RWCLI,
          "\nReceived message unpack failed\n");
    *rsp = NULL;
    return RW_STATUS_FAILURE;
  }

  // The response is stored in the controller to be deleted later after the
  // response is processed by the RW.CLI plugin.
  *rsp = inst->resp;

  return RW_STATUS_SUCCESS;
}